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. get_tab_bar_item_kbd
  204. handle_tab_bar_click
  205. note_tab_bar_highlight
  206. tty_get_tab_bar_item
  207. tty_handle_tab_bar_click
  208. update_tool_bar
  209. build_desired_tool_bar_string
  210. display_tool_bar_line
  211. tool_bar_height
  212. redisplay_tool_bar
  213. tool_bar_item_info
  214. get_tool_bar_item
  215. handle_tool_bar_click_with_device
  216. handle_tool_bar_click
  217. note_tool_bar_highlight
  218. hscroll_window_tree
  219. hscroll_windows
  220. debug_method_add
  221. text_outside_line_unchanged_p
  222. redisplay
  223. overlay_arrow_string_or_property
  224. overlay_arrow_in_current_buffer_p
  225. overlay_arrows_changed_p
  226. update_overlay_arrows
  227. overlay_arrow_at_row
  228. check_point_in_composition
  229. reconsider_clip_changes
  230. propagate_buffer_redisplay
  231. redisplay_internal
  232. unwind_redisplay_preserve_echo_area
  233. redisplay_preserve_echo_area
  234. unwind_redisplay
  235. unwind_display_working_on_window
  236. mark_window_display_accurate_1
  237. mark_window_display_accurate
  238. disp_char_vector
  239. block_buffer_flips
  240. unblock_buffer_flips
  241. buffer_flipping_blocked_p
  242. redisplay_windows
  243. redisplay_window_error
  244. redisplay_window_0
  245. redisplay_window_1
  246. update_redisplay_ticks
  247. set_cursor_from_row
  248. run_window_scroll_functions
  249. cursor_row_fully_visible_p
  250. try_scrolling
  251. compute_window_start_on_continuation_line
  252. try_cursor_movement
  253. set_vertical_scroll_bar
  254. set_horizontal_scroll_bar
  255. window_start_acceptable_p
  256. DEFUN
  257. redisplay_window
  258. try_window
  259. try_window_reusing_current_matrix
  260. find_last_row_displaying_text
  261. find_last_unchanged_at_beg_row
  262. find_first_unchanged_at_end_row
  263. sync_frame_with_window_matrix_rows
  264. row_containing_pos
  265. try_window_id
  266. dump_glyph_row
  267. dump_glyph
  268. dump_glyph_row
  269. DEFUN
  270. DEFUN
  271. DEFUN
  272. get_overlay_arrow_glyph_row
  273. insert_left_trunc_glyphs
  274. row_hash
  275. compute_line_metrics
  276. clear_position
  277. append_space_for_newline
  278. extend_face_to_end_of_line
  279. trailing_whitespace_p
  280. highlight_trailing_whitespace
  281. row_for_charpos_p
  282. cursor_row_p
  283. push_prefix_prop
  284. get_it_property
  285. get_line_prefix_it_property
  286. handle_line_prefix
  287. unproduce_glyphs
  288. find_row_edges
  289. display_count_lines_logically
  290. display_count_lines_visually
  291. maybe_produce_line_number
  292. should_produce_line_number
  293. row_text_area_empty
  294. display_line
  295. DEFUN
  296. DEFUN
  297. DEFUN
  298. display_menu_bar
  299. deep_copy_glyph_row
  300. display_tty_menu_item
  301. redisplay_mode_lines
  302. display_mode_lines
  303. display_mode_line
  304. move_elt_to_front
  305. safe_set_text_properties
  306. display_mode_element
  307. store_mode_line_string
  308. pint2str
  309. pint2hrstr
  310. decode_mode_spec_coding
  311. percent99
  312. decode_mode_spec
  313. count_lines
  314. display_count_lines
  315. display_string
  316. invisible_prop
  317. DEFUN
  318. calc_pixel_width_or_height
  319. get_font_ascent_descent
  320. dump_glyph_string
  321. init_glyph_string
  322. append_glyph_string_lists
  323. prepend_glyph_string_lists
  324. append_glyph_string
  325. get_char_face_and_encoding
  326. get_glyph_face_and_encoding
  327. get_char_glyph_code
  328. fill_composite_glyph_string
  329. fill_gstring_glyph_string
  330. fill_glyphless_glyph_string
  331. fill_glyph_string
  332. fill_image_glyph_string
  333. fill_xwidget_glyph_string
  334. fill_stretch_glyph_string
  335. get_per_char_metric
  336. normal_char_ascent_descent
  337. normal_char_height
  338. gui_get_glyph_overhangs
  339. left_overwritten
  340. left_overwriting
  341. right_overwritten
  342. right_overwriting
  343. set_glyph_string_background_width
  344. glyph_string_containing_background_width
  345. compute_overhangs_and_x
  346. draw_glyphs
  347. font_for_underline_metrics
  348. append_glyph
  349. append_composite_glyph
  350. take_vertical_position_into_account
  351. produce_image_glyph
  352. produce_xwidget_glyph
  353. append_stretch_glyph
  354. produce_stretch_glyph
  355. produce_special_glyphs
  356. pad_mode_line
  357. calc_line_height_property
  358. append_glyphless_glyph
  359. produce_glyphless_glyph
  360. gui_produce_glyphs
  361. gui_write_glyphs
  362. gui_insert_glyphs
  363. gui_clear_end_of_line
  364. get_specified_cursor_type
  365. set_frame_cursor_types
  366. get_window_cursor_type
  367. notice_overwritten_cursor
  368. gui_fix_overlapping_area
  369. draw_phys_cursor_glyph
  370. erase_phys_cursor
  371. display_and_set_cursor
  372. update_window_cursor
  373. update_cursor_in_window_tree
  374. gui_update_cursor
  375. gui_clear_cursor
  376. draw_row_with_mouse_face
  377. show_mouse_face
  378. clear_mouse_face
  379. coords_in_mouse_face_p
  380. cursor_in_mouse_face_p
  381. rows_from_pos_range
  382. mouse_face_from_buffer_pos
  383. fast_find_string_pos
  384. mouse_face_from_string_pos
  385. on_hot_spot_p
  386. find_hot_spot
  387. define_frame_cursor1
  388. note_mode_line_or_margin_highlight
  389. note_mouse_highlight
  390. gui_clear_window_mouse_face
  391. cancel_mouse_face
  392. expose_area
  393. expose_line
  394. expose_overlaps
  395. phys_cursor_in_rect_p
  396. gui_draw_vertical_border
  397. gui_draw_right_divider
  398. gui_draw_bottom_divider
  399. expose_window
  400. expose_window_tree
  401. expose_frame
  402. gui_intersect_rectangles
  403. gui_union_rectangles
  404. syms_of_xdisp
  405. init_xdisp
  406. show_hourglass
  407. start_hourglass
  408. cancel_hourglass
  409. adjust_glyph_width_for_mouse_face
  410. 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 (!ckd_mul (&icol, icol, char_width)
   641               && !ckd_add (&icol, icol, it->lnum_pixel_width))
   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 pad_mode_line (struct it *, bool);
  1233 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
  1234 static bool coords_in_mouse_face_p (struct window *, int, int);
  1235 static void reset_box_start_end_flags (struct it *);
  1236 
  1237 
  1238 
  1239 /***********************************************************************
  1240                       Window display dimensions
  1241  ***********************************************************************/
  1242 
  1243 /* Return the bottom boundary y-position for text lines in window W.
  1244    This is the first y position at which a line cannot start.
  1245    It is relative to the top of the window.
  1246 
  1247    This is the height of W minus the height of a mode line, if any.  */
  1248 
  1249 int
  1250 window_text_bottom_y (struct window *w)
  1251 {
  1252   int height = WINDOW_PIXEL_HEIGHT (w);
  1253 
  1254   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1255 
  1256   if (window_wants_mode_line (w))
  1257     height -= CURRENT_MODE_LINE_HEIGHT (w);
  1258 
  1259   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1260 
  1261   return height;
  1262 }
  1263 
  1264 /* Return the pixel width of display area AREA of window W.
  1265    ANY_AREA means return the total width of W, not including
  1266    fringes to the left and right of the window.  */
  1267 
  1268 int
  1269 window_box_width (struct window *w, enum glyph_row_area area)
  1270 {
  1271   int width = w->pixel_width;
  1272 
  1273   if (!w->pseudo_window_p)
  1274     {
  1275       width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  1276       width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
  1277 
  1278       if (area == TEXT_AREA)
  1279         width -= (WINDOW_MARGINS_WIDTH (w)
  1280                    + WINDOW_FRINGES_WIDTH (w));
  1281       else if (area == LEFT_MARGIN_AREA)
  1282         width = WINDOW_LEFT_MARGIN_WIDTH (w);
  1283       else if (area == RIGHT_MARGIN_AREA)
  1284         width = WINDOW_RIGHT_MARGIN_WIDTH (w);
  1285     }
  1286 
  1287   /* With wide margins, fringes, etc. we might end up with a negative
  1288      width, correct that here.  */
  1289   return max (0, width);
  1290 }
  1291 
  1292 
  1293 /* Return the pixel height of the display area of window W, not
  1294    including mode lines of W, if any.  */
  1295 
  1296 int
  1297 window_box_height (struct window *w)
  1298 {
  1299   struct frame *f = XFRAME (w->frame);
  1300   int height = WINDOW_PIXEL_HEIGHT (w);
  1301 
  1302   eassert (height >= 0);
  1303 
  1304   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1305   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1306 
  1307   /* Note: the code below that determines the mode-line/header-line/tab-line
  1308      height is essentially the same as that contained in the macro
  1309      CURRENT_{MODE,HEADER,TAB}_LINE_HEIGHT, except that it checks whether
  1310      the appropriate glyph row has its `mode_line_p' flag set, and if
  1311      it doesn't, uses estimate_mode_line_height instead.  */
  1312 
  1313   if (window_wants_mode_line (w))
  1314     {
  1315       if (w->mode_line_height >= 0)
  1316         height -= w->mode_line_height;
  1317       else
  1318         {
  1319           struct glyph_row *ml_row
  1320             = (w->current_matrix && w->current_matrix->rows
  1321                ? MATRIX_MODE_LINE_ROW (w->current_matrix)
  1322                : 0);
  1323           if (ml_row && ml_row->mode_line_p)
  1324             height -= ml_row->height;
  1325           else
  1326             height -= estimate_mode_line_height
  1327               (f, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w));
  1328         }
  1329     }
  1330 
  1331   if (window_wants_tab_line (w))
  1332     {
  1333       if (w->tab_line_height >= 0)
  1334         height -= w->tab_line_height;
  1335       else
  1336         {
  1337           struct glyph_row *tl_row
  1338             = (w->current_matrix && w->current_matrix->rows
  1339                ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  1340                : 0);
  1341           if (tl_row && tl_row->mode_line_p)
  1342             height -= tl_row->height;
  1343           else
  1344             height -= estimate_mode_line_height (f, TAB_LINE_FACE_ID);
  1345         }
  1346     }
  1347 
  1348   if (window_wants_header_line (w))
  1349     {
  1350       if (w->header_line_height >= 0)
  1351         height -= w->header_line_height;
  1352       else
  1353         {
  1354           struct glyph_row *hl_row
  1355             = (w->current_matrix && w->current_matrix->rows
  1356                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  1357                : 0);
  1358           if (hl_row && hl_row->mode_line_p)
  1359             height -= hl_row->height;
  1360           else
  1361             height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
  1362         }
  1363     }
  1364 
  1365   /* With a very small font and a mode-line that's taller than
  1366      default, we might end up with a negative height.  */
  1367   return max (0, height);
  1368 }
  1369 
  1370 /* Return the window-relative coordinate of the left edge of display
  1371    area AREA of window W.  ANY_AREA means return the left edge of the
  1372    whole window, to the right of the left fringe of W.  */
  1373 
  1374 int
  1375 window_box_left_offset (struct window *w, enum glyph_row_area area)
  1376 {
  1377   int x;
  1378 
  1379   if (w->pseudo_window_p)
  1380     return 0;
  1381 
  1382   x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
  1383 
  1384   if (area == TEXT_AREA)
  1385     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1386           + window_box_width (w, LEFT_MARGIN_AREA));
  1387   else if (area == RIGHT_MARGIN_AREA)
  1388     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1389           + window_box_width (w, LEFT_MARGIN_AREA)
  1390           + window_box_width (w, TEXT_AREA)
  1391           + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  1392              ? 0
  1393              : WINDOW_RIGHT_FRINGE_WIDTH (w)));
  1394   else if (area == LEFT_MARGIN_AREA
  1395            && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
  1396     x += WINDOW_LEFT_FRINGE_WIDTH (w);
  1397 
  1398   /* Don't return more than the window's pixel width.  */
  1399   return min (x, w->pixel_width);
  1400 }
  1401 
  1402 
  1403 /* Return the window-relative coordinate of the right edge of display
  1404    area AREA of window W.  ANY_AREA means return the right edge of the
  1405    whole window, to the left of the right fringe of W.  */
  1406 
  1407 static int
  1408 window_box_right_offset (struct window *w, enum glyph_row_area area)
  1409 {
  1410   /* Don't return more than the window's pixel width.  */
  1411   return min (window_box_left_offset (w, area) + window_box_width (w, area),
  1412               w->pixel_width);
  1413 }
  1414 
  1415 /* Return the frame-relative coordinate of the left edge of display
  1416    area AREA of window W.  ANY_AREA means return the left edge of the
  1417    whole window, to the right of the left fringe of W.  */
  1418 
  1419 int
  1420 window_box_left (struct window *w, enum glyph_row_area area)
  1421 {
  1422   struct frame *f = XFRAME (w->frame);
  1423   int x;
  1424 
  1425   if (w->pseudo_window_p)
  1426     return FRAME_INTERNAL_BORDER_WIDTH (f);
  1427 
  1428   x = (WINDOW_LEFT_EDGE_X (w)
  1429        + window_box_left_offset (w, area));
  1430 
  1431   return x;
  1432 }
  1433 
  1434 
  1435 /* Return the frame-relative coordinate of the right edge of display
  1436    area AREA of window W.  ANY_AREA means return the right edge of the
  1437    whole window, to the left of the right fringe of W.  */
  1438 
  1439 int
  1440 window_box_right (struct window *w, enum glyph_row_area area)
  1441 {
  1442   return window_box_left (w, area) + window_box_width (w, area);
  1443 }
  1444 
  1445 /* Get the bounding box of the display area AREA of window W, without
  1446    mode lines, in frame-relative coordinates.  ANY_AREA means the
  1447    whole window, not including the left and right fringes of
  1448    the window.  Return in *BOX_X and *BOX_Y the frame-relative pixel
  1449    coordinates of the upper-left corner of the box.  Return in
  1450    *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box.  */
  1451 
  1452 void
  1453 window_box (struct window *w, enum glyph_row_area area, int *box_x,
  1454             int *box_y, int *box_width, int *box_height)
  1455 {
  1456   if (box_width)
  1457     *box_width = window_box_width (w, area);
  1458   if (box_height)
  1459     *box_height = window_box_height (w);
  1460   if (box_x)
  1461     *box_x = window_box_left (w, area);
  1462   if (box_y)
  1463     {
  1464       *box_y = WINDOW_TOP_EDGE_Y (w);
  1465       if (window_wants_tab_line (w))
  1466         *box_y += CURRENT_TAB_LINE_HEIGHT (w);
  1467       if (window_wants_header_line (w))
  1468         *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
  1469     }
  1470 }
  1471 
  1472 #ifdef HAVE_WINDOW_SYSTEM
  1473 
  1474 /* Get the bounding box of the display area AREA of window W, without
  1475    mode lines and both fringes of the window.  Return in *TOP_LEFT_X
  1476    and TOP_LEFT_Y the frame-relative pixel coordinates of the
  1477    upper-left corner of the box.  Return in *BOTTOM_RIGHT_X, and
  1478    *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
  1479    box.  */
  1480 
  1481 static void
  1482 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
  1483                   int *bottom_right_x, int *bottom_right_y)
  1484 {
  1485   window_box (w, ANY_AREA, top_left_x, top_left_y,
  1486               bottom_right_x, bottom_right_y);
  1487   *bottom_right_x += *top_left_x;
  1488   *bottom_right_y += *top_left_y;
  1489 }
  1490 
  1491 #endif /* HAVE_WINDOW_SYSTEM */
  1492 
  1493 /***********************************************************************
  1494                               Utilities
  1495  ***********************************************************************/
  1496 
  1497 /* Return the bottom y-position of the line the iterator IT is in.
  1498    This can modify IT's settings.  */
  1499 
  1500 int
  1501 line_bottom_y (struct it *it)
  1502 {
  1503   int line_height = it->max_ascent + it->max_descent;
  1504   int line_top_y = it->current_y;
  1505 
  1506   if (line_height == 0)
  1507     {
  1508       if (last_height)
  1509         line_height = last_height;
  1510       else if (IT_CHARPOS (*it) < ZV)
  1511         {
  1512           move_it_by_lines (it, 1);
  1513           line_height = (it->max_ascent || it->max_descent
  1514                          ? it->max_ascent + it->max_descent
  1515                          : last_height);
  1516         }
  1517       else
  1518         {
  1519           struct glyph_row *row = it->glyph_row;
  1520 
  1521           /* Use the default character height.  */
  1522           it->glyph_row = NULL;
  1523           it->what = IT_CHARACTER;
  1524           it->c = ' ';
  1525           it->len = 1;
  1526           PRODUCE_GLYPHS (it);
  1527           line_height = it->ascent + it->descent;
  1528           it->glyph_row = row;
  1529         }
  1530     }
  1531 
  1532   return line_top_y + line_height;
  1533 }
  1534 
  1535 DEFUN ("line-pixel-height", Fline_pixel_height,
  1536        Sline_pixel_height, 0, 0, 0,
  1537        doc: /* Return height in pixels of text line in the selected window.
  1538 
  1539 Value is the height in pixels of the line at point.  */)
  1540   (void)
  1541 {
  1542   struct it it;
  1543   struct text_pos pt;
  1544   struct window *w = XWINDOW (selected_window);
  1545   struct buffer *old_buffer = NULL;
  1546   Lisp_Object result;
  1547 
  1548   if (XBUFFER (w->contents) != current_buffer)
  1549     {
  1550       old_buffer = current_buffer;
  1551       set_buffer_internal_1 (XBUFFER (w->contents));
  1552     }
  1553   SET_TEXT_POS (pt, PT, PT_BYTE);
  1554   void *itdata = bidi_shelve_cache ();
  1555   start_display (&it, w, pt);
  1556   /* Start from the beginning of the screen line, to make sure we
  1557      traverse all of its display elements, and thus capture the
  1558      correct metrics.  */
  1559   move_it_by_lines (&it, 0);
  1560   it.vpos = it.current_y = 0;
  1561   last_height = 0;
  1562   result = make_fixnum (line_bottom_y (&it));
  1563   if (old_buffer)
  1564     set_buffer_internal_1 (old_buffer);
  1565 
  1566   bidi_unshelve_cache (itdata, false);
  1567   return result;
  1568 }
  1569 
  1570 /* Return the default pixel height of text lines in window W.  The
  1571    value is the canonical height of the W frame's default font, plus
  1572    any extra space required by the line-spacing variable or frame
  1573    parameter.
  1574 
  1575    Implementation note: this ignores any line-spacing text properties
  1576    put on the newline characters.  This is because those properties
  1577    only affect the _screen_ line ending in the newline (i.e., in a
  1578    continued line, only the last screen line will be affected), which
  1579    means only a small number of lines in a buffer can ever use this
  1580    feature.  Since this function is used to compute the default pixel
  1581    equivalent of text lines in a window, we can safely ignore those
  1582    few lines.  For the same reasons, we ignore the line-height
  1583    properties.  */
  1584 int
  1585 default_line_pixel_height (struct window *w)
  1586 {
  1587   struct frame *f = WINDOW_XFRAME (w);
  1588   int height = FRAME_LINE_HEIGHT (f);
  1589 
  1590   if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
  1591     {
  1592       struct buffer *b = XBUFFER (w->contents);
  1593       Lisp_Object val = BVAR (b, extra_line_spacing);
  1594 
  1595       if (NILP (val))
  1596         val = BVAR (&buffer_defaults, extra_line_spacing);
  1597       if (!NILP (val))
  1598         {
  1599           if (RANGED_FIXNUMP (0, val, INT_MAX))
  1600             height += XFIXNAT (val);
  1601           else if (FLOATP (val))
  1602             {
  1603               int addon = XFLOAT_DATA (val) * height + 0.5;
  1604 
  1605               if (addon >= 0)
  1606                 height += addon;
  1607             }
  1608         }
  1609       else
  1610         height += f->extra_line_spacing;
  1611     }
  1612 
  1613   return height;
  1614 }
  1615 
  1616 /* Subroutine of pos_visible_p below.  Extracts a display string, if
  1617    any, from the display spec given as its argument.  */
  1618 static Lisp_Object
  1619 string_from_display_spec (Lisp_Object spec)
  1620 {
  1621   if (VECTORP (spec))
  1622     {
  1623       for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
  1624         if (STRINGP (AREF (spec, i)))
  1625           return AREF (spec, i);
  1626     }
  1627   else
  1628     {
  1629       for (; CONSP (spec); spec = XCDR (spec))
  1630         if (STRINGP (XCAR (spec)))
  1631           return XCAR (spec);
  1632     }
  1633   return spec;
  1634 }
  1635 
  1636 
  1637 /* Limit insanely large values of W->hscroll on frame F to the largest
  1638    value that will still prevent first_visible_x and last_visible_x of
  1639    'struct it' from overflowing an int.  */
  1640 static int
  1641 window_hscroll_limited (struct window *w, struct frame *f)
  1642 {
  1643   ptrdiff_t window_hscroll = w->hscroll;
  1644   int window_text_width = window_box_width (w, TEXT_AREA);
  1645   int colwidth = FRAME_COLUMN_WIDTH (f);
  1646 
  1647   if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
  1648     window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
  1649 
  1650   return window_hscroll;
  1651 }
  1652 
  1653 /* Reset the box-face start and end flags in the iterator.  This is
  1654    called after producing glyphs, such that we reset these flags only
  1655    after producing a glyph with the flag set.  */
  1656 
  1657 static void
  1658 reset_box_start_end_flags (struct it *it)
  1659 {
  1660   /* Don't reset if we've drawn the glyph in the display margins --
  1661      those don't count as "produced glyphs".  */
  1662   if (it->area == TEXT_AREA
  1663       /* Don't reset if we displayed a fringe bitmap.  */
  1664       && !(it->what == IT_IMAGE && it->image_id < 0))
  1665     {
  1666       /* Don't reset if the face is not a box face: that might mean we
  1667          are iterating some overlay or display string, and the first
  1668          character to have the box face is yet to be seen, when we pop
  1669          the iterator stack. */
  1670       if (it->face_box_p)
  1671         it->start_of_box_run_p = false;
  1672       it->end_of_box_run_p = false;
  1673     }
  1674 }
  1675 
  1676 /* Return true if position CHARPOS is visible in window W.
  1677    CHARPOS < 0 means return info about WINDOW_END position.
  1678    If visible, set *X and *Y to pixel coordinates of top left corner.
  1679    Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
  1680    Set *ROWH and *VPOS to row's visible height and VPOS (row number).  */
  1681 
  1682 bool
  1683 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
  1684                int *rtop, int *rbot, int *rowh, int *vpos)
  1685 {
  1686   struct it it;
  1687   void *itdata = bidi_shelve_cache ();
  1688   struct text_pos top;
  1689   bool visible_p = false;
  1690   struct buffer *old_buffer = NULL;
  1691   bool r2l = false;
  1692 
  1693   if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
  1694     return visible_p;
  1695 
  1696   if (XBUFFER (w->contents) != current_buffer)
  1697     {
  1698       old_buffer = current_buffer;
  1699       set_buffer_internal_1 (XBUFFER (w->contents));
  1700     }
  1701 
  1702   SET_TEXT_POS_FROM_MARKER (top, w->start);
  1703   /* Scrolling a minibuffer window via scroll bar when the echo area
  1704      shows long text sometimes resets the minibuffer contents behind
  1705      our backs.  Also, someone might narrow-to-region and immediately
  1706      call a scroll function.  */
  1707   if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
  1708     SET_TEXT_POS (top, BEGV, BEGV_BYTE);
  1709 
  1710   /* If the top of the window is after CHARPOS, the latter is surely
  1711      not visible.  */
  1712   if (charpos >= 0 && CHARPOS (top) > charpos)
  1713     return visible_p;
  1714 
  1715   /* Some Lisp hook could call us in the middle of redisplaying this
  1716      very window.  If, by some bad luck, we are retrying redisplay
  1717      because we found that the mode-line height and/or tab/header-line
  1718      height needs to be updated, the assignment of mode_line_height
  1719      and header_line_height below could disrupt that, due to the
  1720      selected/nonselected window dance during mode-line display, and
  1721      we could infloop.  Avoid that.  */
  1722   int prev_mode_line_height = w->mode_line_height;
  1723   int prev_header_line_height = w->header_line_height;
  1724   int prev_tab_line_height = w->tab_line_height;
  1725   /* Compute exact mode line heights.  */
  1726   if (window_wants_mode_line (w))
  1727     {
  1728       Lisp_Object window_mode_line_format
  1729         = window_parameter (w, Qmode_line_format);
  1730 
  1731       w->mode_line_height
  1732         = display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
  1733                              NILP (window_mode_line_format)
  1734                              ? BVAR (current_buffer, mode_line_format)
  1735                              : window_mode_line_format);
  1736     }
  1737 
  1738   if (window_wants_tab_line (w))
  1739     {
  1740       Lisp_Object window_tab_line_format
  1741         = window_parameter (w, Qtab_line_format);
  1742 
  1743       w->tab_line_height
  1744         = display_mode_line (w, TAB_LINE_FACE_ID,
  1745                              NILP (window_tab_line_format)
  1746                              ? BVAR (current_buffer, tab_line_format)
  1747                              : window_tab_line_format);
  1748     }
  1749 
  1750   if (window_wants_header_line (w))
  1751     {
  1752       Lisp_Object window_header_line_format
  1753         = window_parameter (w, Qheader_line_format);
  1754 
  1755       w->header_line_height
  1756         = display_mode_line (w, HEADER_LINE_FACE_ID,
  1757                              NILP (window_header_line_format)
  1758                              ? BVAR (current_buffer, header_line_format)
  1759                              : window_header_line_format);
  1760     }
  1761 
  1762   start_display (&it, w, top);
  1763   move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
  1764               (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
  1765 
  1766   /* Adjust for line numbers, if CHARPOS is at or beyond first_visible_x,
  1767      but we didn't yet produce the line-number glyphs.  */
  1768   if (!NILP (Vdisplay_line_numbers)
  1769       && it.current_x >= it.first_visible_x
  1770       && IT_CHARPOS (it) == charpos
  1771       && !it.line_number_produced_p)
  1772     {
  1773       /* If the pixel width of line numbers was not yet known, compute
  1774          it now.  This usually happens in the first display line of a
  1775          window.  */
  1776       if (!it.lnum_pixel_width)
  1777         {
  1778           struct it it2;
  1779           void *it2data = NULL;
  1780 
  1781           SAVE_IT (it2, it, it2data);
  1782           move_it_by_lines (&it, 1);
  1783           it2.lnum_pixel_width = it.lnum_pixel_width;
  1784           RESTORE_IT (&it, &it2, it2data);
  1785         }
  1786       it.current_x += it.lnum_pixel_width;
  1787     }
  1788 
  1789   if (charpos >= 0
  1790       && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
  1791            && IT_CHARPOS (it) >= charpos)
  1792           /* When scanning backwards under bidi iteration, move_it_to
  1793              stops at or _before_ CHARPOS, because it stops at or to
  1794              the _right_ of the character at CHARPOS.  */
  1795           || (it.bidi_p && it.bidi_it.scan_dir == -1
  1796               && IT_CHARPOS (it) <= charpos)))
  1797     {
  1798       /* We have reached CHARPOS, or passed it.  How the call to
  1799          move_it_to can overshoot: (i) If CHARPOS is on invisible text
  1800          or covered by a display property, move_it_to stops at the end
  1801          of the invisible text, to the right of CHARPOS.  (ii) If
  1802          CHARPOS is in a display vector, move_it_to stops on its last
  1803          glyph.  */
  1804       int top_x = it.current_x;
  1805       int top_y = it.current_y;
  1806       int window_top_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  1807       int bottom_y;
  1808       struct it save_it;
  1809       void *save_it_data = NULL;
  1810 
  1811       /* Calling line_bottom_y may change it.method, it.position, etc.  */
  1812       SAVE_IT (save_it, it, save_it_data);
  1813       last_height = 0;
  1814       bottom_y = line_bottom_y (&it);
  1815       if (top_y < window_top_y)
  1816         visible_p = bottom_y > window_top_y;
  1817       else if (top_y < it.last_visible_y)
  1818         visible_p = true;
  1819       if (bottom_y >= it.last_visible_y
  1820           && it.bidi_p && it.bidi_it.scan_dir == -1
  1821           && IT_CHARPOS (it) < charpos)
  1822         {
  1823           /* When the last line of the window is scanned backwards
  1824              under bidi iteration, we could be duped into thinking
  1825              that we have passed CHARPOS, when in fact move_it_to
  1826              simply stopped short of CHARPOS because it reached
  1827              last_visible_y.  To see if that's what happened, we call
  1828              move_it_to again with a slightly larger vertical limit,
  1829              and see if it actually moved vertically; if it did, we
  1830              didn't really reach CHARPOS, which is beyond window end.  */
  1831           /* Why 10? because we don't know how many canonical lines
  1832              will the height of the next line(s) be.  So we guess.  */
  1833           int ten_more_lines = 10 * default_line_pixel_height (w);
  1834 
  1835           move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
  1836                       MOVE_TO_POS | MOVE_TO_Y);
  1837           if (it.current_y > top_y)
  1838             visible_p = false;
  1839 
  1840         }
  1841       RESTORE_IT (&it, &save_it, save_it_data);
  1842       if (visible_p)
  1843         {
  1844           if (it.method == GET_FROM_DISPLAY_VECTOR)
  1845             {
  1846               /* We stopped on the last glyph of a display vector.
  1847                  Try and recompute.  Hack alert!  */
  1848               if (charpos < 2 || top.charpos >= charpos)
  1849                 top_x = it.glyph_row->x;
  1850               else
  1851                 {
  1852                   struct it it2, it2_prev;
  1853                   /* The idea is to get to the previous buffer
  1854                      position, consume the character there, and use
  1855                      the pixel coordinates we get after that.  But if
  1856                      the previous buffer position is also displayed
  1857                      from a display vector, we need to consume all of
  1858                      the glyphs from that display vector.  */
  1859                   start_display (&it2, w, top);
  1860                   it2.glyph_row = NULL;
  1861                   move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
  1862                   /* If we didn't get to CHARPOS - 1, there's some
  1863                      replacing display property at that position, and
  1864                      we stopped after it.  That is exactly the place
  1865                      whose coordinates we want.  */
  1866                   if (IT_CHARPOS (it2) != charpos - 1)
  1867                     it2_prev = it2;
  1868                   else
  1869                     {
  1870                       /* Iterate until we get out of the display
  1871                          vector that displays the character at
  1872                          CHARPOS - 1.  */
  1873                       do {
  1874                         get_next_display_element (&it2);
  1875                         PRODUCE_GLYPHS (&it2);
  1876                         it2_prev = it2;
  1877                         set_iterator_to_next (&it2, true);
  1878                       } while (it2.method == GET_FROM_DISPLAY_VECTOR
  1879                                && IT_CHARPOS (it2) < charpos);
  1880                     }
  1881                   if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
  1882                       || it2_prev.current_x > it2_prev.last_visible_x)
  1883                     top_x = it.glyph_row->x;
  1884                   else
  1885                     {
  1886                       top_x = it2_prev.current_x;
  1887                       top_y = it2_prev.current_y;
  1888                     }
  1889                 }
  1890             }
  1891           else if (IT_CHARPOS (it) != charpos)
  1892             {
  1893               Lisp_Object cpos = make_fixnum (charpos);
  1894               Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  1895               Lisp_Object string = string_from_display_spec (spec);
  1896               struct text_pos tpos;
  1897               bool newline_in_string
  1898                 = (STRINGP (string)
  1899                    && memchr (SDATA (string), '\n', SBYTES (string)));
  1900 
  1901               SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
  1902               bool replacing_spec_p
  1903                 = (!NILP (spec)
  1904                    && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
  1905                                            charpos, FRAME_WINDOW_P (it.f)));
  1906               /* The tricky code below is needed because there's a
  1907                  discrepancy between move_it_to and how we set cursor
  1908                  when PT is at the beginning of a portion of text
  1909                  covered by a display property or an overlay with a
  1910                  display property, or the display line ends in a
  1911                  newline from a display string.  move_it_to will stop
  1912                  _after_ such display strings, whereas
  1913                  set_cursor_from_row conspires with cursor_row_p to
  1914                  place the cursor on the first glyph produced from the
  1915                  display string.  */
  1916 
  1917               /* We have overshoot PT because it is covered by a
  1918                  display property that replaces the text it covers.
  1919                  If the string includes embedded newlines, we are also
  1920                  in the wrong display line.  Backtrack to the correct
  1921                  line, where the display property begins.  */
  1922               if (replacing_spec_p)
  1923                 {
  1924                   Lisp_Object startpos, endpos;
  1925                   EMACS_INT start, end;
  1926                   struct it it3;
  1927 
  1928                   /* Find the first and the last buffer positions
  1929                      covered by the display string.  */
  1930                   endpos =
  1931                     Fnext_single_char_property_change (cpos, Qdisplay,
  1932                                                        Qnil, Qnil);
  1933                   startpos =
  1934                     Fprevious_single_char_property_change (endpos, Qdisplay,
  1935                                                            Qnil, Qnil);
  1936                   start = XFIXNAT (startpos);
  1937                   end = XFIXNAT (endpos);
  1938                   /* Move to the last buffer position before the
  1939                      display property.  */
  1940                   start_display (&it3, w, top);
  1941                   if (start > CHARPOS (top))
  1942                     move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
  1943                   /* Move forward one more line if the position before
  1944                      the display string is a newline or if it is the
  1945                      rightmost character on a line that is
  1946                      continued or word-wrapped.  */
  1947                   if (it3.method == GET_FROM_BUFFER
  1948                       && (it3.c == '\n'
  1949                           || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
  1950                     move_it_by_lines (&it3, 1);
  1951                   else if (move_it_in_display_line_to (&it3, -1,
  1952                                                        it3.current_x
  1953                                                        + it3.pixel_width,
  1954                                                        MOVE_TO_X)
  1955                            == MOVE_LINE_CONTINUED)
  1956                     {
  1957                       move_it_by_lines (&it3, 1);
  1958                       /* When we are under word-wrap, the #$@%!
  1959                          move_it_by_lines moves 2 lines, so we need to
  1960                          fix that up.  */
  1961                       if (it3.line_wrap == WORD_WRAP)
  1962                         move_it_by_lines (&it3, -1);
  1963                     }
  1964 
  1965                   /* Record the vertical coordinate of the display
  1966                      line where we wound up.  */
  1967                   top_y = it3.current_y;
  1968                   if (it3.bidi_p)
  1969                     {
  1970                       /* When characters are reordered for display,
  1971                          the character displayed to the left of the
  1972                          display string could be _after_ the display
  1973                          property in the logical order.  Use the
  1974                          smallest vertical position of these two.  */
  1975                       start_display (&it3, w, top);
  1976                       move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
  1977                       if (it3.current_y < top_y)
  1978                         top_y = it3.current_y;
  1979                     }
  1980                   /* Move from the top of the window to the beginning
  1981                      of the display line where the display string
  1982                      begins.  */
  1983                   start_display (&it3, w, top);
  1984                   it3.glyph_row = NULL;
  1985                   move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
  1986                   /* If it3_moved stays false after the 'while' loop
  1987                      below, that means we already were at a newline
  1988                      before the loop (e.g., the display string begins
  1989                      with a newline), so we don't need to return to
  1990                      the last position before the display string,
  1991                      because PRODUCE_GLYPHS will not produce anything
  1992                      for a newline.  */
  1993                   bool it3_moved = false;
  1994                   int top_x_before_string = it3.current_x;
  1995                   /* Finally, advance the iterator until we hit the
  1996                      first display element whose character position is
  1997                      at or beyond CHARPOS, or until the first newline
  1998                      from the display string, which signals the end of
  1999                      the display line.  */
  2000                   while (get_next_display_element (&it3))
  2001                     {
  2002                       if (!EQ (it3.object, string))
  2003                         top_x_before_string = it3.current_x;
  2004                       PRODUCE_GLYPHS (&it3);
  2005                       if ((it3.bidi_it.scan_dir == 1
  2006                            && IT_CHARPOS (it3) >= charpos)
  2007                           || (it3.bidi_it.scan_dir == -1
  2008                               && IT_CHARPOS (it3) <= charpos)
  2009                           || ITERATOR_AT_END_OF_LINE_P (&it3))
  2010                         break;
  2011                       it3_moved = true;
  2012                       set_iterator_to_next (&it3, false);
  2013                     }
  2014                   top_x = it3.current_x - it3.pixel_width;
  2015                   /* Account for line-number display, if IT3 still
  2016                      didn't.  This can happen if START - 1 is the
  2017                      first or the last character on its display line.  */
  2018                   if (!it3.line_number_produced_p)
  2019                     {
  2020                       if (it3.lnum_pixel_width > 0)
  2021                         {
  2022                           top_x += it3.lnum_pixel_width;
  2023                           top_x_before_string += it3.lnum_pixel_width;
  2024                         }
  2025                       else if (it.line_number_produced_p)
  2026                         {
  2027                           top_x += it.lnum_pixel_width;
  2028                           top_x_before_string += it3.lnum_pixel_width;
  2029                         }
  2030                     }
  2031                   /* Normally, we would exit the above loop because we
  2032                      found the display element whose character
  2033                      position is CHARPOS.  For the contingency that we
  2034                      didn't, and stopped at the first newline from the
  2035                      display string, reset top_x to the coordinate of
  2036                      the rightmost glyph not from the string.  */
  2037                   if (it3_moved
  2038                       && newline_in_string
  2039                       && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
  2040                     top_x = top_x_before_string;
  2041                 }
  2042             }
  2043 
  2044           *x = top_x;
  2045           /* The condition below is a heuristic fix for the situation
  2046              where move_it_to stops just after finishing the display
  2047              of a fringe bitmap, which resets it.ascent to zero, and
  2048              thus causes Y to be offset by it.max_ascent.  */
  2049           if (it.ascent == 0 && it.what == IT_IMAGE
  2050               && it.method != GET_FROM_IMAGE
  2051               && it.image_id < 0
  2052               && it.max_ascent > 0)
  2053             *y = max (top_y, window_top_y);
  2054           else
  2055             *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
  2056           *rtop = max (0, window_top_y - top_y);
  2057           *rbot = max (0, bottom_y - it.last_visible_y);
  2058           *rowh = max (0, (min (bottom_y, it.last_visible_y)
  2059                            - max (top_y, window_top_y)));
  2060           *vpos = it.vpos;
  2061           if (it.bidi_it.paragraph_dir == R2L)
  2062             r2l = true;
  2063         }
  2064     }
  2065   else
  2066     {
  2067       /* Either we were asked to provide info about WINDOW_END, or
  2068          CHARPOS is in the partially visible glyph row at end of
  2069          window.  */
  2070       struct it it2;
  2071       void *it2data = NULL;
  2072 
  2073       SAVE_IT (it2, it, it2data);
  2074       if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
  2075         move_it_by_lines (&it, 1);
  2076       if (charpos < IT_CHARPOS (it)
  2077           || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
  2078         {
  2079           visible_p = true;
  2080           RESTORE_IT (&it2, &it2, it2data);
  2081           move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
  2082           *x = it2.current_x;
  2083           if (it2.ascent == 0 && it2.what == IT_IMAGE
  2084               && it2.method != GET_FROM_IMAGE
  2085               && it2.image_id < 0
  2086               && it2.max_ascent > 0)
  2087             *y = it2.current_y;
  2088           else
  2089             *y = it2.current_y + it2.max_ascent - it2.ascent;
  2090           *rtop = max (0, -it2.current_y);
  2091           *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
  2092                            - it.last_visible_y));
  2093           *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
  2094                                 it.last_visible_y)
  2095                            - max (max (it2.current_y,
  2096                                        WINDOW_TAB_LINE_HEIGHT (w)),
  2097                                   WINDOW_HEADER_LINE_HEIGHT (w))));
  2098           *vpos = it2.vpos;
  2099           if (it2.bidi_it.paragraph_dir == R2L)
  2100             r2l = true;
  2101         }
  2102       else
  2103         bidi_unshelve_cache (it2data, true);
  2104     }
  2105   bidi_unshelve_cache (itdata, false);
  2106 
  2107   if (old_buffer)
  2108     set_buffer_internal_1 (old_buffer);
  2109 
  2110   if (visible_p)
  2111     {
  2112       if (w->hscroll > 0)
  2113         *x -=
  2114           window_hscroll_limited (w, WINDOW_XFRAME (w))
  2115           * WINDOW_FRAME_COLUMN_WIDTH (w);
  2116       /* For lines in an R2L paragraph, we need to mirror the X pixel
  2117          coordinate wrt the text area.  For the reasons, see the
  2118          commentary in buffer_posn_from_coords and the explanation of
  2119          the geometry used by the move_it_* functions at the end of
  2120          the large commentary near the beginning of this file.  */
  2121       if (r2l)
  2122         *x = window_box_width (w, TEXT_AREA) - *x - 1;
  2123     }
  2124 
  2125 #if false
  2126   /* Debugging code.  */
  2127   if (visible_p)
  2128     fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
  2129              charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
  2130   else
  2131     fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
  2132 #endif
  2133 
  2134   /* Restore potentially overwritten values.  */
  2135   w->mode_line_height = prev_mode_line_height;
  2136   w->header_line_height = prev_header_line_height;
  2137   w->tab_line_height = prev_tab_line_height;
  2138 
  2139   return visible_p;
  2140 }
  2141 
  2142 
  2143 /* Return the next character from STR.  Return in *LEN the length of
  2144    the character.  This is like string_char_and_length but never
  2145    returns an invalid character.  If we find one, we return a `?', but
  2146    with the length of the invalid character.  */
  2147 
  2148 static int
  2149 check_char_and_length (const unsigned char *str, int *len)
  2150 {
  2151   int c = string_char_and_length (str, len);
  2152   if (!CHAR_VALID_P (c))
  2153     /* We may not change the length here because other places in Emacs
  2154        don't use this function, i.e. they silently accept invalid
  2155        characters.  */
  2156     c = '?';
  2157 
  2158   return c;
  2159 }
  2160 
  2161 
  2162 
  2163 /* Given a position POS containing a valid character and byte position
  2164    in STRING, return the position NCHARS ahead (NCHARS >= 0).  */
  2165 
  2166 static struct text_pos
  2167 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
  2168 {
  2169   eassert (STRINGP (string) && nchars >= 0);
  2170 
  2171   if (STRING_MULTIBYTE (string))
  2172     {
  2173       const unsigned char *p = SDATA (string) + BYTEPOS (pos);
  2174 
  2175       while (nchars--)
  2176         {
  2177           int len = BYTES_BY_CHAR_HEAD (*p);
  2178           p += len;
  2179           CHARPOS (pos) += 1;
  2180           BYTEPOS (pos) += len;
  2181         }
  2182     }
  2183   else
  2184     SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
  2185 
  2186   return pos;
  2187 }
  2188 
  2189 
  2190 /* Value is the text position, i.e. character and byte position,
  2191    for character position CHARPOS in STRING.  */
  2192 
  2193 static struct text_pos
  2194 string_pos (ptrdiff_t charpos, Lisp_Object string)
  2195 {
  2196   struct text_pos pos;
  2197   eassert (STRINGP (string));
  2198   eassert (charpos >= 0);
  2199   SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
  2200   return pos;
  2201 }
  2202 
  2203 
  2204 /* Value is a text position, i.e. character and byte position, for
  2205    character position CHARPOS in C string S.  MULTIBYTE_P
  2206    means recognize multibyte characters.  */
  2207 
  2208 static struct text_pos
  2209 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
  2210 {
  2211   struct text_pos pos;
  2212 
  2213   eassert (s != NULL);
  2214   eassert (charpos >= 0);
  2215 
  2216   if (multibyte_p)
  2217     {
  2218       SET_TEXT_POS (pos, 0, 0);
  2219       while (charpos--)
  2220         {
  2221           int len = BYTES_BY_CHAR_HEAD (*s);
  2222           s += len;
  2223           CHARPOS (pos) += 1;
  2224           BYTEPOS (pos) += len;
  2225         }
  2226     }
  2227   else
  2228     SET_TEXT_POS (pos, charpos, charpos);
  2229 
  2230   return pos;
  2231 }
  2232 
  2233 
  2234 /* Value is the number of characters in C string S.  MULTIBYTE_P
  2235    means recognize multibyte characters.  */
  2236 
  2237 static ptrdiff_t
  2238 number_of_chars (const char *s, bool multibyte_p)
  2239 {
  2240   ptrdiff_t nchars;
  2241 
  2242   if (multibyte_p)
  2243     {
  2244       ptrdiff_t rest = strlen (s);
  2245       const unsigned char *p = (const unsigned char *) s;
  2246 
  2247       for (nchars = 0; rest > 0; ++nchars)
  2248         {
  2249           int len = BYTES_BY_CHAR_HEAD (*p);
  2250           rest -= len, p += len;
  2251         }
  2252     }
  2253   else
  2254     nchars = strlen (s);
  2255 
  2256   return nchars;
  2257 }
  2258 
  2259 
  2260 /* Compute byte position NEWPOS->bytepos corresponding to
  2261    NEWPOS->charpos.  POS is a known position in string STRING.
  2262    NEWPOS->charpos must be >= POS.charpos.  */
  2263 
  2264 static void
  2265 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
  2266 {
  2267   eassert (STRINGP (string));
  2268   eassert (CHARPOS (*newpos) >= CHARPOS (pos));
  2269 
  2270   if (STRING_MULTIBYTE (string))
  2271     *newpos = string_pos_nchars_ahead (pos, string,
  2272                                        CHARPOS (*newpos) - CHARPOS (pos));
  2273   else
  2274     BYTEPOS (*newpos) = CHARPOS (*newpos);
  2275 }
  2276 
  2277 /* EXPORT:
  2278    Return an estimation of the pixel height of mode or header lines on
  2279    frame F.  FACE_ID specifies what line's height to estimate.  */
  2280 
  2281 int
  2282 estimate_mode_line_height (struct frame *f, enum face_id face_id)
  2283 {
  2284 #ifdef HAVE_WINDOW_SYSTEM
  2285   if (FRAME_WINDOW_P (f))
  2286     {
  2287       int height = FONT_HEIGHT (FRAME_FONT (f));
  2288 
  2289       /* This function is called so early when Emacs starts that the face
  2290          cache and mode line face are not yet initialized.  */
  2291       if (FRAME_FACE_CACHE (f))
  2292         {
  2293           struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
  2294           if (face)
  2295             {
  2296               if (face->font)
  2297                 height = normal_char_height (face->font, -1);
  2298               if (face->box_horizontal_line_width > 0)
  2299                 height += 2 * face->box_horizontal_line_width;
  2300             }
  2301         }
  2302 
  2303       return height;
  2304     }
  2305 #endif
  2306 
  2307   return 1;
  2308 }
  2309 
  2310 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
  2311    coordinates in (*X, *Y).  Set *BOUNDS to the rectangle that the
  2312    glyph at X, Y occupies, if BOUNDS != 0.  If NOCLIP, do
  2313    not force the value into range.  */
  2314 
  2315 void
  2316 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
  2317                        NativeRectangle *bounds, bool noclip)
  2318 {
  2319 
  2320 #ifdef HAVE_WINDOW_SYSTEM
  2321   if (FRAME_WINDOW_P (f))
  2322     {
  2323       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
  2324          even for negative values.  */
  2325       if (pix_x < 0)
  2326         pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
  2327       if (pix_y < 0)
  2328         pix_y -= FRAME_LINE_HEIGHT (f) - 1;
  2329 
  2330       pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
  2331       pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
  2332 
  2333       if (bounds)
  2334         STORE_NATIVE_RECT (*bounds,
  2335                            FRAME_COL_TO_PIXEL_X (f, pix_x),
  2336                            FRAME_LINE_TO_PIXEL_Y (f, pix_y),
  2337                            FRAME_COLUMN_WIDTH (f) - 1,
  2338                            FRAME_LINE_HEIGHT (f) - 1);
  2339 
  2340       /* PXW: Should we clip pixels before converting to columns/lines?  */
  2341       if (!noclip)
  2342         {
  2343           if (pix_x < 0)
  2344             pix_x = 0;
  2345           else if (pix_x > FRAME_TOTAL_COLS (f))
  2346             pix_x = FRAME_TOTAL_COLS (f);
  2347 
  2348           if (pix_y < 0)
  2349             pix_y = 0;
  2350           else if (pix_y > FRAME_TOTAL_LINES (f))
  2351             pix_y = FRAME_TOTAL_LINES (f);
  2352         }
  2353     }
  2354 #endif
  2355 
  2356   *x = pix_x;
  2357   *y = pix_y;
  2358 }
  2359 
  2360 
  2361 /* Find the glyph under window-relative coordinates X/Y in window W.
  2362    Consider only glyphs from buffer text, i.e. no glyphs from overlay
  2363    strings.  Return in *HPOS and *VPOS the row and column number of
  2364    the glyph found.  Return in *AREA the glyph area containing X.
  2365    Value is a pointer to the glyph found or null if X/Y is not on
  2366    text, or we can't tell because W's current matrix is not up to
  2367    date.  */
  2368 
  2369 struct glyph *
  2370 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
  2371                   int *dx, int *dy, int *area)
  2372 {
  2373   struct glyph *glyph, *end;
  2374   struct glyph_row *row = NULL;
  2375   int x0, i;
  2376 
  2377   /* Find row containing Y.  Give up if some row is not enabled.  */
  2378   for (i = 0; i < w->current_matrix->nrows; ++i)
  2379     {
  2380       row = MATRIX_ROW (w->current_matrix, i);
  2381       if (!row->enabled_p)
  2382         return NULL;
  2383       if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
  2384         break;
  2385     }
  2386 
  2387   *vpos = i;
  2388   *hpos = 0;
  2389 
  2390   /* Give up if Y is not in the window.  */
  2391   if (i == w->current_matrix->nrows)
  2392     return NULL;
  2393 
  2394   /* Get the glyph area containing X.  */
  2395   if (w->pseudo_window_p)
  2396     {
  2397       *area = TEXT_AREA;
  2398       x0 = 0;
  2399     }
  2400   else
  2401     {
  2402       if (x < window_box_left_offset (w, TEXT_AREA))
  2403         {
  2404           *area = LEFT_MARGIN_AREA;
  2405           x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
  2406         }
  2407       else if (x < window_box_right_offset (w, TEXT_AREA))
  2408         {
  2409           *area = TEXT_AREA;
  2410           x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
  2411         }
  2412       else
  2413         {
  2414           *area = RIGHT_MARGIN_AREA;
  2415           x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
  2416         }
  2417     }
  2418 
  2419   /* Find glyph containing X.  */
  2420   glyph = row->glyphs[*area];
  2421   end = glyph + row->used[*area];
  2422   x -= x0;
  2423   while (glyph < end && x >= glyph->pixel_width)
  2424     {
  2425       x -= glyph->pixel_width;
  2426       ++glyph;
  2427     }
  2428 
  2429   if (glyph == end)
  2430     return NULL;
  2431 
  2432   if (dx)
  2433     {
  2434       *dx = x;
  2435       *dy = y - (row->y + row->ascent - glyph->ascent);
  2436     }
  2437 
  2438   *hpos = glyph - row->glyphs[*area];
  2439   return glyph;
  2440 }
  2441 
  2442 /* Convert frame-relative x/y to coordinates relative to window W.
  2443    Takes pseudo-windows into account.  */
  2444 
  2445 static void
  2446 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
  2447 {
  2448   if (w->pseudo_window_p)
  2449     {
  2450       /* A pseudo-window is always full-width, and starts at the
  2451          left edge of the frame, plus a frame border.  */
  2452       struct frame *f = XFRAME (w->frame);
  2453       *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
  2454       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2455     }
  2456   else
  2457     {
  2458       *x -= WINDOW_LEFT_EDGE_X (w);
  2459       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2460     }
  2461 }
  2462 
  2463 #ifdef HAVE_WINDOW_SYSTEM
  2464 
  2465 /* EXPORT:
  2466    Return in RECTS[] at most N clipping rectangles for glyph string S.
  2467    Return the number of stored rectangles.  */
  2468 
  2469 int
  2470 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
  2471 {
  2472   Emacs_Rectangle r;
  2473 
  2474   if (n <= 0)
  2475     return 0;
  2476 
  2477   if (s->row->full_width_p)
  2478     {
  2479       /* Draw full-width.  X coordinates are relative to S->w->left_col.  */
  2480       r.x = WINDOW_LEFT_EDGE_X (s->w);
  2481       if (s->row->mode_line_p)
  2482         r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
  2483       else
  2484         r.width = WINDOW_PIXEL_WIDTH (s->w);
  2485 
  2486       /* Unless displaying a mode or menu bar line, which are always
  2487          fully visible, clip to the visible part of the row.  */
  2488       if (s->w->pseudo_window_p)
  2489         r.height = s->row->visible_height;
  2490       else
  2491         r.height = s->height;
  2492     }
  2493   else
  2494     {
  2495       /* This is a text line that may be partially visible.  */
  2496       r.x = window_box_left (s->w, s->area);
  2497       r.width = window_box_width (s->w, s->area);
  2498       r.height = s->row->visible_height;
  2499     }
  2500 
  2501   if (s->clip_head)
  2502     if (r.x < s->clip_head->x)
  2503       {
  2504         if (r.width >= s->clip_head->x - r.x)
  2505           r.width -= s->clip_head->x - r.x;
  2506         else
  2507           r.width = 0;
  2508         r.x = s->clip_head->x;
  2509       }
  2510   if (s->clip_tail)
  2511     if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
  2512       {
  2513         if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
  2514           r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
  2515         else
  2516           r.width = 0;
  2517       }
  2518 
  2519   /* If S draws overlapping rows, it's sufficient to use the top and
  2520      bottom of the window for clipping because this glyph string
  2521      intentionally draws over other lines.  */
  2522   if (s->for_overlaps)
  2523     {
  2524       r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2525       r.height = window_text_bottom_y (s->w) - r.y;
  2526 
  2527       /* Alas, the above simple strategy does not work for the
  2528          environments with anti-aliased text: if the same text is
  2529          drawn onto the same place multiple times, it gets thicker.
  2530          If the overlap we are processing is for the erased cursor, we
  2531          take the intersection with the rectangle of the cursor.  */
  2532       if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
  2533         {
  2534           Emacs_Rectangle rc, r_save = r;
  2535 
  2536           rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
  2537           rc.y = s->w->phys_cursor.y;
  2538           rc.width = s->w->phys_cursor_width;
  2539           rc.height = s->w->phys_cursor_height;
  2540 
  2541           gui_intersect_rectangles (&r_save, &rc, &r);
  2542         }
  2543     }
  2544   else
  2545     {
  2546       /* Don't use S->y for clipping because it doesn't take partially
  2547          visible lines into account.  For example, it can be negative for
  2548          partially visible lines at the top of a window.  */
  2549       if (!s->row->full_width_p
  2550           && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
  2551         r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2552       else
  2553         r.y = max (0, s->row->y);
  2554     }
  2555 
  2556   r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
  2557 
  2558   /* If drawing the cursor, don't let glyph draw outside its
  2559      advertised boundaries. Cleartype does this under some circumstances.  */
  2560   if (s->hl == DRAW_CURSOR)
  2561     {
  2562       struct glyph *glyph = s->first_glyph;
  2563       int height, max_y;
  2564 
  2565       if (s->x > r.x)
  2566         {
  2567           if (r.width >= s->x - r.x)
  2568             r.width -= s->x - r.x;
  2569           else  /* R2L hscrolled row with cursor outside text area */
  2570             r.width = 0;
  2571           r.x = s->x;
  2572         }
  2573       r.width = min (r.width, glyph->pixel_width);
  2574 
  2575       /* If r.y is below window bottom, ensure that we still see a cursor.  */
  2576       height = min (glyph->ascent + glyph->descent,
  2577                     min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
  2578       max_y = window_text_bottom_y (s->w) - height;
  2579       max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
  2580       if (s->ybase - glyph->ascent > max_y)
  2581         {
  2582           r.y = max_y;
  2583           r.height = height;
  2584         }
  2585       else
  2586         {
  2587           /* Don't draw cursor glyph taller than our actual glyph.  */
  2588           height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
  2589           if (height < r.height)
  2590             {
  2591               max_y = r.y + r.height;
  2592               r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
  2593               r.height = min (max_y - r.y, height);
  2594             }
  2595         }
  2596     }
  2597 
  2598   if (s->row->clip)
  2599     {
  2600       Emacs_Rectangle r_save = r;
  2601 
  2602       if (! gui_intersect_rectangles (&r_save, s->row->clip, &r))
  2603         r.width = 0;
  2604     }
  2605 
  2606   if ((s->for_overlaps & OVERLAPS_BOTH) == 0
  2607       || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
  2608     {
  2609 #ifdef CONVERT_FROM_EMACS_RECT
  2610       CONVERT_FROM_EMACS_RECT (r, *rects);
  2611 #else
  2612       *rects = r;
  2613 #endif
  2614       return 1;
  2615     }
  2616   else
  2617     {
  2618       /* If we are processing overlapping and allowed to return
  2619          multiple clipping rectangles, we exclude the row of the glyph
  2620          string from the clipping rectangle.  This is to avoid drawing
  2621          the same text on the environment with anti-aliasing.  */
  2622 #ifdef CONVERT_FROM_EMACS_RECT
  2623       Emacs_Rectangle rs[2];
  2624 #else
  2625       Emacs_Rectangle *rs = rects;
  2626 #endif
  2627       int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
  2628 
  2629       if (s->for_overlaps & OVERLAPS_PRED)
  2630         {
  2631           rs[i] = r;
  2632           if (r.y + r.height > row_y)
  2633             {
  2634               if (r.y < row_y)
  2635                 rs[i].height = row_y - r.y;
  2636               else
  2637                 rs[i].height = 0;
  2638             }
  2639           i++;
  2640         }
  2641       if (s->for_overlaps & OVERLAPS_SUCC)
  2642         {
  2643           rs[i] = r;
  2644           if (r.y < row_y + s->row->visible_height)
  2645             {
  2646               if (r.y + r.height > row_y + s->row->visible_height)
  2647                 {
  2648                   rs[i].y = row_y + s->row->visible_height;
  2649                   rs[i].height = r.y + r.height - rs[i].y;
  2650                 }
  2651               else
  2652                 rs[i].height = 0;
  2653             }
  2654           i++;
  2655         }
  2656 
  2657       n = i;
  2658 #ifdef CONVERT_FROM_EMACS_RECT
  2659       for (i = 0; i < n; i++)
  2660         CONVERT_FROM_EMACS_RECT (rs[i], rects[i]);
  2661 #endif
  2662       return n;
  2663     }
  2664 }
  2665 
  2666 /* EXPORT:
  2667    Return in *NR the clipping rectangle for glyph string S.  */
  2668 
  2669 void
  2670 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
  2671 {
  2672   get_glyph_string_clip_rects (s, nr, 1);
  2673 }
  2674 
  2675 
  2676 /* EXPORT:
  2677    Return the position and height of the phys cursor in window W.
  2678    Set w->phys_cursor_width to width of phys cursor.
  2679 */
  2680 
  2681 void
  2682 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
  2683                           struct glyph *glyph, int *xp, int *yp, int *heightp)
  2684 {
  2685   struct frame *f = XFRAME (WINDOW_FRAME (w));
  2686   int x, y, wd, h, h0, y0, ascent;
  2687 
  2688   /* Compute the width of the rectangle to draw.  If on a stretch
  2689      glyph, and `x-stretch-cursor' is nil, don't draw a rectangle
  2690      as wide as the glyph, but use a canonical character width
  2691      instead.  */
  2692   wd = glyph->pixel_width;
  2693 
  2694   x = w->phys_cursor.x;
  2695   if (x < 0)
  2696     {
  2697       wd += x;
  2698       x = 0;
  2699     }
  2700 
  2701   if (glyph->type == STRETCH_GLYPH
  2702       && !x_stretch_cursor_p)
  2703     wd = min (FRAME_COLUMN_WIDTH (f), wd);
  2704   w->phys_cursor_width = wd;
  2705 
  2706   /* Don't let the hollow cursor glyph descend below the glyph row's
  2707      ascent value, lest the hollow cursor looks funny.  */
  2708   y = w->phys_cursor.y;
  2709   ascent = row->ascent;
  2710   /* The test for row at ZV is for when line numbers are displayed and
  2711      point is at EOB: the cursor could then be smaller or larger than
  2712      the default face's font.  */
  2713   if (!row->ends_at_zv_p && row->ascent < glyph->ascent)
  2714     {
  2715       y -= glyph->ascent - row->ascent;
  2716       ascent = glyph->ascent;
  2717     }
  2718 
  2719   /* If y is below window bottom, ensure that we still see a cursor.  */
  2720   h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
  2721 
  2722   h = max (h0, ascent + glyph->descent);
  2723   /* Don't let the cursor exceed the dimensions of the row, so that
  2724      the upper/lower side of the box aren't clipped.  */
  2725   h = min (h, row->height);
  2726   h0 = min (h0, ascent + glyph->descent);
  2727 
  2728   y0 = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  2729   if (y < y0)
  2730     {
  2731       h = max (h - (y0 - y) + 1, h0);
  2732       y = y0 - 1;
  2733     }
  2734   else
  2735     {
  2736       y0 = window_text_bottom_y (w) - h0;
  2737       if (y > y0)
  2738         {
  2739           h += y - y0;
  2740           y = y0;
  2741         }
  2742     }
  2743 
  2744   *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
  2745   *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
  2746   *heightp = h;
  2747 }
  2748 
  2749 /*
  2750  * Remember which glyph the mouse is over.
  2751  */
  2752 
  2753 void
  2754 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
  2755 {
  2756   Lisp_Object window;
  2757   struct window *w;
  2758   struct glyph_row *r, *gr, *end_row;
  2759   enum window_part part;
  2760   enum glyph_row_area area;
  2761   int x, y, width, height;
  2762   int original_gx;
  2763 
  2764   if (mouse_fine_grained_tracking)
  2765     {
  2766       STORE_NATIVE_RECT (*rect, gx, gy, 1, 1);
  2767       return;
  2768     }
  2769 
  2770   /* Try to determine frame pixel position and size of the glyph under
  2771      frame pixel coordinates X/Y on frame F.  */
  2772 
  2773   original_gx = gx;
  2774 
  2775   if (window_resize_pixelwise)
  2776     {
  2777       width = height = 1;
  2778       goto virtual_glyph;
  2779     }
  2780   else if (!f->glyphs_initialized_p
  2781            || (window = window_from_coordinates (f, gx, gy, &part, false, false),
  2782                NILP (window)))
  2783     {
  2784       width = FRAME_SMALLEST_CHAR_WIDTH (f);
  2785       height = FRAME_SMALLEST_FONT_HEIGHT (f);
  2786       goto virtual_glyph;
  2787     }
  2788 
  2789   w = XWINDOW (window);
  2790   width = WINDOW_FRAME_COLUMN_WIDTH (w);
  2791   height = WINDOW_FRAME_LINE_HEIGHT (w);
  2792 
  2793   x = window_relative_x_coord (w, part, gx);
  2794   y = gy - WINDOW_TOP_EDGE_Y (w);
  2795 
  2796   r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  2797   end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
  2798 
  2799   if (w->pseudo_window_p)
  2800     {
  2801       area = TEXT_AREA;
  2802       part = ON_MODE_LINE; /* Don't adjust margin. */
  2803       goto text_glyph;
  2804     }
  2805 
  2806   switch (part)
  2807     {
  2808     case ON_LEFT_MARGIN:
  2809       area = LEFT_MARGIN_AREA;
  2810       goto text_glyph;
  2811 
  2812     case ON_RIGHT_MARGIN:
  2813       area = RIGHT_MARGIN_AREA;
  2814       goto text_glyph;
  2815 
  2816     case ON_TAB_LINE:
  2817     case ON_HEADER_LINE:
  2818     case ON_MODE_LINE:
  2819       gr = (part == ON_TAB_LINE
  2820             ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  2821             : (part == ON_HEADER_LINE
  2822                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  2823                : MATRIX_MODE_LINE_ROW (w->current_matrix)));
  2824       gy = gr->y;
  2825       area = TEXT_AREA;
  2826       goto text_glyph_row_found;
  2827 
  2828     case ON_TEXT:
  2829       area = TEXT_AREA;
  2830 
  2831     text_glyph:
  2832       gr = 0; gy = 0;
  2833       for (; r <= end_row && r->enabled_p; ++r)
  2834         if (r->y + r->height > y)
  2835           {
  2836             gr = r; gy = r->y;
  2837             break;
  2838           }
  2839 
  2840     text_glyph_row_found:
  2841       if (gr && gy <= y)
  2842         {
  2843           struct glyph *g = gr->glyphs[area];
  2844           struct glyph *end = g + gr->used[area];
  2845 
  2846           height = gr->height;
  2847           for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
  2848             if (gx + g->pixel_width > x)
  2849               break;
  2850 
  2851           if (g < end)
  2852             {
  2853               if (g->type == IMAGE_GLYPH)
  2854                 {
  2855                   /* Don't remember when mouse is over image, as
  2856                      image may have hot-spots.  */
  2857                   STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
  2858                   return;
  2859                 }
  2860               width = g->pixel_width;
  2861             }
  2862           else
  2863             {
  2864               /* Use nominal char spacing at end of line.  */
  2865               x -= gx;
  2866               gx += (x / width) * width;
  2867             }
  2868 
  2869           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2870               && part != ON_TAB_LINE)
  2871             {
  2872               gx += window_box_left_offset (w, area);
  2873               /* Don't expand over the modeline to make sure the vertical
  2874                  drag cursor is shown early enough.  */
  2875               height = min (height,
  2876                             max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2877             }
  2878         }
  2879       else
  2880         {
  2881           /* Use nominal line height at end of window.  */
  2882           gx = (x / width) * width;
  2883           y -= gy;
  2884           gy += (y / height) * height;
  2885           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2886               && part != ON_TAB_LINE)
  2887             /* See comment above.  */
  2888             height = min (height,
  2889                           max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2890         }
  2891       break;
  2892 
  2893     case ON_LEFT_FRINGE:
  2894       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2895             ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
  2896             : window_box_right_offset (w, LEFT_MARGIN_AREA));
  2897       width = WINDOW_LEFT_FRINGE_WIDTH (w);
  2898       goto row_glyph;
  2899 
  2900     case ON_RIGHT_FRINGE:
  2901       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2902             ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2903             : window_box_right_offset (w, TEXT_AREA));
  2904       if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
  2905           && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
  2906           && !WINDOW_RIGHTMOST_P (w))
  2907         if (gx < WINDOW_PIXEL_WIDTH (w) - width)
  2908           /* Make sure the vertical border can get her own glyph to the
  2909              right of the one we build here.  */
  2910           width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
  2911         else
  2912           width = WINDOW_PIXEL_WIDTH (w) - gx;
  2913       else
  2914         width = WINDOW_RIGHT_FRINGE_WIDTH (w);
  2915 
  2916       goto row_glyph;
  2917 
  2918     case ON_VERTICAL_BORDER:
  2919       gx = WINDOW_PIXEL_WIDTH (w) - width;
  2920       goto row_glyph;
  2921 
  2922     case ON_VERTICAL_SCROLL_BAR:
  2923       gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
  2924             ? 0
  2925             : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2926                + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2927                   ? WINDOW_RIGHT_FRINGE_WIDTH (w)
  2928                   : 0)));
  2929       width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  2930 
  2931     row_glyph:
  2932       gr = 0, gy = 0;
  2933       for (; r <= end_row && r->enabled_p; ++r)
  2934         if (r->y + r->height > y)
  2935           {
  2936             gr = r; gy = r->y;
  2937             break;
  2938           }
  2939 
  2940       if (gr && gy <= y)
  2941         height = gr->height;
  2942       else
  2943         {
  2944           /* Use nominal line height at end of window.  */
  2945           y -= gy;
  2946           gy += (y / height) * height;
  2947         }
  2948       break;
  2949 
  2950     case ON_RIGHT_DIVIDER:
  2951       gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2952       width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2953       gy = 0;
  2954       /* The bottom divider prevails. */
  2955       height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2956       goto add_edge;
  2957 
  2958     case ON_BOTTOM_DIVIDER:
  2959       gx = 0;
  2960       width = WINDOW_PIXEL_WIDTH (w);
  2961       gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2962       height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2963       goto add_edge;
  2964 
  2965     default:
  2966       ;
  2967     virtual_glyph:
  2968       /* If there is no glyph under the mouse, then we divide the screen
  2969          into a grid of the smallest glyph in the frame, and use that
  2970          as our "glyph".  */
  2971 
  2972       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
  2973          round down even for negative values.  */
  2974       if (gx < 0)
  2975         gx -= width - 1;
  2976       if (gy < 0)
  2977         gy -= height - 1;
  2978 
  2979       gx = (gx / width) * width;
  2980       gy = (gy / height) * height;
  2981 
  2982       goto store_rect;
  2983     }
  2984 
  2985  add_edge:
  2986   gx += WINDOW_LEFT_EDGE_X (w);
  2987   gy += WINDOW_TOP_EDGE_Y (w);
  2988 
  2989  store_rect:
  2990   if (mouse_prefer_closest_glyph)
  2991     {
  2992       int half_width = width / 2;
  2993       width = half_width;
  2994 
  2995       int bisection = gx + half_width;
  2996       if (original_gx > bisection)
  2997         gx = bisection;
  2998     }
  2999   STORE_NATIVE_RECT (*rect, gx, gy, width, height);
  3000 
  3001   /* Visible feedback for debugging.  */
  3002 #if false && defined HAVE_X_WINDOWS
  3003   XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
  3004                   f->output_data.x->normal_gc,
  3005                   gx, gy, width, height);
  3006 #endif
  3007 }
  3008 
  3009 
  3010 #endif /* HAVE_WINDOW_SYSTEM */
  3011 
  3012 static void
  3013 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
  3014 {
  3015   eassert (w);
  3016   w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
  3017   w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
  3018   w->window_end_vpos
  3019     = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
  3020 }
  3021 
  3022 static bool
  3023 hscrolling_current_line_p (struct window *w)
  3024 {
  3025   return (!w->suspend_auto_hscroll
  3026           && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
  3027                  Qcurrent_line));
  3028 }
  3029 
  3030 /***********************************************************************
  3031                         Lisp form evaluation
  3032  ***********************************************************************/
  3033 
  3034 /* Error handler for safe_eval and safe_call.  */
  3035 
  3036 static Lisp_Object
  3037 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
  3038 {
  3039   add_to_log ("Error during redisplay: %S signaled %S",
  3040               Flist (nargs, args), arg);
  3041   return Qnil;
  3042 }
  3043 
  3044 /* Call function FUNC with the rest of NARGS - 1 arguments
  3045    following.  Return the result, or nil if something went
  3046    wrong.  Prevent redisplay during the evaluation.  */
  3047 
  3048 static Lisp_Object
  3049 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
  3050 {
  3051   Lisp_Object val;
  3052 
  3053   if (inhibit_eval_during_redisplay)
  3054     val = Qnil;
  3055   else
  3056     {
  3057       ptrdiff_t i;
  3058       specpdl_ref count = SPECPDL_INDEX ();
  3059       Lisp_Object *args;
  3060       USE_SAFE_ALLOCA;
  3061       SAFE_ALLOCA_LISP (args, nargs);
  3062 
  3063       args[0] = func;
  3064       for (i = 1; i < nargs; i++)
  3065         args[i] = va_arg (ap, Lisp_Object);
  3066 
  3067       specbind (Qinhibit_redisplay, Qt);
  3068       if (inhibit_quit)
  3069         specbind (Qinhibit_quit, Qt);
  3070       /* Use Qt to ensure debugger does not run,
  3071          so there is no possibility of wanting to redisplay.  */
  3072       val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
  3073                                        safe_eval_handler);
  3074       val = SAFE_FREE_UNBIND_TO (count, val);
  3075     }
  3076 
  3077   return val;
  3078 }
  3079 
  3080 Lisp_Object
  3081 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
  3082 {
  3083   Lisp_Object retval;
  3084   va_list ap;
  3085 
  3086   va_start (ap, func);
  3087   retval = safe__call (false, nargs, func, ap);
  3088   va_end (ap);
  3089   return retval;
  3090 }
  3091 
  3092 /* Call function FN with one argument ARG.
  3093    Return the result, or nil if something went wrong.  */
  3094 
  3095 Lisp_Object
  3096 safe_call1 (Lisp_Object fn, Lisp_Object arg)
  3097 {
  3098   return safe_call (2, fn, arg);
  3099 }
  3100 
  3101 static Lisp_Object
  3102 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
  3103 {
  3104   Lisp_Object retval;
  3105   va_list ap;
  3106 
  3107   va_start (ap, fn);
  3108   retval = safe__call (inhibit_quit, 2, fn, ap);
  3109   va_end (ap);
  3110   return retval;
  3111 }
  3112 
  3113 Lisp_Object
  3114 safe_eval (Lisp_Object sexpr)
  3115 {
  3116   return safe__call1 (false, Qeval, sexpr);
  3117 }
  3118 
  3119 static Lisp_Object
  3120 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
  3121 {
  3122   return safe__call1 (inhibit_quit, Qeval, sexpr);
  3123 }
  3124 
  3125 /* Call function FN with two arguments ARG1 and ARG2.
  3126    Return the result, or nil if something went wrong.  */
  3127 
  3128 Lisp_Object
  3129 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
  3130 {
  3131   return safe_call (3, fn, arg1, arg2);
  3132 }
  3133 
  3134 
  3135 
  3136 /***********************************************************************
  3137                               Debugging
  3138  ***********************************************************************/
  3139 
  3140 /* Define CHECK_IT to perform sanity checks on iterators.
  3141    This is for debugging.  It is too slow to do unconditionally.  */
  3142 
  3143 static void
  3144 CHECK_IT (struct it *it)
  3145 {
  3146 #if false
  3147   if (it->method == GET_FROM_STRING)
  3148     {
  3149       eassert (STRINGP (it->string));
  3150       eassert (IT_STRING_CHARPOS (*it) >= 0);
  3151     }
  3152   else
  3153     {
  3154       eassert (IT_STRING_CHARPOS (*it) < 0);
  3155       if (it->method == GET_FROM_BUFFER)
  3156         {
  3157           /* Check that character and byte positions agree.  */
  3158           eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
  3159         }
  3160     }
  3161 
  3162   if (it->dpvec)
  3163     eassert (it->current.dpvec_index >= 0);
  3164   else
  3165     eassert (it->current.dpvec_index < 0);
  3166 #endif
  3167 }
  3168 
  3169 
  3170 /* Check that the window end of window W is what we expect it
  3171    to be---the last row in the current matrix displaying text.  */
  3172 
  3173 static void
  3174 CHECK_WINDOW_END (struct window *w)
  3175 {
  3176 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
  3177   if (!MINI_WINDOW_P (w) && w->window_end_valid)
  3178     {
  3179       struct glyph_row *row;
  3180       eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
  3181                 !row->enabled_p
  3182                 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
  3183                 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
  3184     }
  3185 #endif
  3186 }
  3187 
  3188 /***********************************************************************
  3189                        Iterator initialization
  3190  ***********************************************************************/
  3191 
  3192 /* Initialize IT for displaying current_buffer in window W, starting
  3193    at character position CHARPOS.  CHARPOS < 0 means that no buffer
  3194    position is specified which is useful when the iterator is assigned
  3195    a position later.  BYTEPOS is the byte position corresponding to
  3196    CHARPOS.
  3197 
  3198    If ROW is not null, calls to produce_glyphs with IT as parameter
  3199    will produce glyphs in that row.
  3200 
  3201    BASE_FACE_ID is the id of a base face to use.  It must be one of
  3202    DEFAULT_FACE_ID for normal text, MODE_LINE_ACTIVE_FACE_ID,
  3203    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
  3204    mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
  3205 
  3206    If ROW is null and BASE_FACE_ID is equal to MODE_LINE_ACTIVE_FACE_ID,
  3207    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
  3208    will be initialized to use the corresponding mode line glyph row of
  3209    the desired matrix of W.  */
  3210 
  3211 void
  3212 init_iterator (struct it *it, struct window *w,
  3213                ptrdiff_t charpos, ptrdiff_t bytepos,
  3214                struct glyph_row *row, enum face_id base_face_id)
  3215 {
  3216   enum face_id remapped_base_face_id = base_face_id;
  3217   int body_width = 0, body_height = 0;
  3218 
  3219   /* Some precondition checks.  */
  3220   eassert (w != NULL && it != NULL);
  3221   eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
  3222                            && charpos <= ZV));
  3223 
  3224   /* If face attributes have been changed since the last redisplay,
  3225      free realized faces now because they depend on face definitions
  3226      that might have changed.  Don't free faces while there might be
  3227      desired matrices pending which reference these faces.  */
  3228   if (!inhibit_free_realized_faces)
  3229     {
  3230       if (face_change)
  3231         {
  3232           face_change = false;
  3233           XFRAME (w->frame)->face_change = 0;
  3234           free_all_realized_faces (Qnil);
  3235         }
  3236       else if (XFRAME (w->frame)->face_change)
  3237         {
  3238           XFRAME (w->frame)->face_change = 0;
  3239           free_all_realized_faces (w->frame);
  3240         }
  3241     }
  3242 
  3243   /* Perhaps remap BASE_FACE_ID to a user-specified alternative.  */
  3244   if (! NILP (Vface_remapping_alist))
  3245     remapped_base_face_id
  3246       = lookup_basic_face (w, XFRAME (w->frame), base_face_id);
  3247 
  3248   /* Use one of the mode line rows of W's desired matrix if
  3249      appropriate.  */
  3250   if (row == NULL)
  3251     {
  3252       if (base_face_id == MODE_LINE_ACTIVE_FACE_ID
  3253           || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
  3254         row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
  3255       else if (base_face_id == TAB_LINE_FACE_ID)
  3256         row = MATRIX_TAB_LINE_ROW (w->desired_matrix);
  3257       else if (base_face_id == HEADER_LINE_FACE_ID)
  3258         {
  3259           /* Header line row depends on whether tab line is enabled.  */
  3260           w->desired_matrix->tab_line_p = window_wants_tab_line (w);
  3261           row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
  3262         }
  3263     }
  3264 
  3265   /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
  3266      Other parts of redisplay rely on that.  */
  3267   memclear (it, sizeof *it);
  3268   it->current.overlay_string_index = -1;
  3269   it->current.dpvec_index = -1;
  3270   it->base_face_id = remapped_base_face_id;
  3271   IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  3272   it->paragraph_embedding = L2R;
  3273   it->bidi_it.w = w;
  3274 
  3275   /* The window in which we iterate over current_buffer:  */
  3276   XSETWINDOW (it->window, w);
  3277   it->w = w;
  3278   it->f = XFRAME (w->frame);
  3279 
  3280   it->cmp_it.id = -1;
  3281   it->cmp_it.parent_it = it;
  3282 
  3283   if (max_redisplay_ticks > 0)
  3284     update_redisplay_ticks (0, w);
  3285 
  3286   /* Extra space between lines (on window systems only).  */
  3287   if (base_face_id == DEFAULT_FACE_ID
  3288       && FRAME_WINDOW_P (it->f))
  3289     {
  3290       if (FIXNATP (BVAR (current_buffer, extra_line_spacing)))
  3291         it->extra_line_spacing = XFIXNAT (BVAR (current_buffer, extra_line_spacing));
  3292       else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
  3293         it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
  3294                                   * FRAME_LINE_HEIGHT (it->f));
  3295       else if (it->f->extra_line_spacing > 0)
  3296         it->extra_line_spacing = it->f->extra_line_spacing;
  3297     }
  3298 
  3299   /* If realized faces have been removed, e.g. because of face
  3300      attribute changes of named faces, recompute them.  When running
  3301      in batch mode, the face cache of the initial frame is null.  If
  3302      we happen to get called, make a dummy face cache.  */
  3303   if (FRAME_FACE_CACHE (it->f) == NULL)
  3304     init_frame_faces (it->f);
  3305   if (FRAME_FACE_CACHE (it->f)->used == 0)
  3306     recompute_basic_faces (it->f);
  3307 
  3308   it->override_ascent = -1;
  3309 
  3310   /* Are control characters displayed as `^C'?  */
  3311   it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
  3312 
  3313   /* -1 means everything between a CR and the following line end
  3314      is invisible.  >0 means lines indented more than this value are
  3315      invisible.  */
  3316   it->selective = (FIXNUMP (BVAR (current_buffer, selective_display))
  3317                    ? (clip_to_bounds
  3318                       (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
  3319                        PTRDIFF_MAX))
  3320                    : (!NILP (BVAR (current_buffer, selective_display))
  3321                       ? -1 : 0));
  3322   it->selective_display_ellipsis_p
  3323     = !NILP (BVAR (current_buffer, selective_display_ellipses));
  3324 
  3325   /* Display table to use.  */
  3326   it->dp = window_display_table (w);
  3327 
  3328   /* Are multibyte characters enabled in current_buffer?  */
  3329   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  3330 
  3331   it->tab_width = SANE_TAB_WIDTH (current_buffer);
  3332 
  3333   /* Are lines in the display truncated?  */
  3334   if (TRUNCATE != 0)
  3335     it->line_wrap = TRUNCATE;
  3336   if (base_face_id == DEFAULT_FACE_ID
  3337       && !it->w->hscroll
  3338       && (WINDOW_FULL_WIDTH_P (it->w)
  3339           || NILP (Vtruncate_partial_width_windows)
  3340           || (FIXNUMP (Vtruncate_partial_width_windows)
  3341               /* PXW: Shall we do something about this?  */
  3342               && (XFIXNUM (Vtruncate_partial_width_windows)
  3343                   <= WINDOW_TOTAL_COLS (it->w))))
  3344       && NILP (BVAR (current_buffer, truncate_lines)))
  3345     it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
  3346       ? WINDOW_WRAP : WORD_WRAP;
  3347 
  3348   /* Get dimensions of truncation and continuation glyphs.  These are
  3349      displayed as fringe bitmaps under X, but we need them for such
  3350      frames when the fringes are turned off.  The no_special_glyphs slot
  3351      of the iterator's frame, when set, suppresses their display - by
  3352      default for tooltip frames and when set via the 'no-special-glyphs'
  3353      frame parameter.  */
  3354 #ifdef HAVE_WINDOW_SYSTEM
  3355   if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
  3356 #endif
  3357     {
  3358       if (it->line_wrap == TRUNCATE)
  3359         {
  3360           /* We will need the truncation glyph.  */
  3361           eassert (it->glyph_row == NULL);
  3362           produce_special_glyphs (it, IT_TRUNCATION);
  3363           it->truncation_pixel_width = it->pixel_width;
  3364         }
  3365       else
  3366         {
  3367           /* We will need the continuation glyph.  */
  3368           eassert (it->glyph_row == NULL);
  3369           produce_special_glyphs (it, IT_CONTINUATION);
  3370           it->continuation_pixel_width = it->pixel_width;
  3371         }
  3372     }
  3373 
  3374   /* Reset these values to zero because the produce_special_glyphs
  3375      above has changed them.  */
  3376   it->pixel_width = it->ascent = it->descent = 0;
  3377   it->phys_ascent = it->phys_descent = 0;
  3378 
  3379   /* Set this after getting the dimensions of truncation and
  3380      continuation glyphs, so that we don't produce glyphs when calling
  3381      produce_special_glyphs, above.  */
  3382   it->glyph_row = row;
  3383   it->area = TEXT_AREA;
  3384 
  3385   /* Get the dimensions of the display area.  The display area
  3386      consists of the visible window area plus a horizontally scrolled
  3387      part to the left of the window.  All x-values are relative to the
  3388      start of this total display area.  */
  3389   if (base_face_id != DEFAULT_FACE_ID)
  3390     {
  3391       /* Mode lines, menu bar in terminal frames.  */
  3392       it->first_visible_x = 0;
  3393       it->last_visible_x =
  3394         WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  3395     }
  3396   else
  3397     {
  3398       /* When hscrolling only the current line, don't apply the
  3399          hscroll here, it will be applied by display_line when it gets
  3400          to laying out the line showing point.  However, if the
  3401          window's min_hscroll is positive, the user specified a lower
  3402          bound for automatic hscrolling, so they expect the
  3403          non-current lines to obey that hscroll amount.  */
  3404       if (hscrolling_current_line_p (w))
  3405         {
  3406           if (w->min_hscroll > 0)
  3407             it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
  3408           else
  3409             it->first_visible_x = 0;
  3410         }
  3411       else
  3412         it->first_visible_x =
  3413           window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
  3414 
  3415       body_width = window_box_width (w, TEXT_AREA);
  3416       if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3417           && body_width != w->old_body_pixel_width)
  3418         FRAME_WINDOW_CHANGE (it->f) = true;
  3419       it->last_visible_x = it->first_visible_x + body_width;
  3420 
  3421       /* If we truncate lines, leave room for the truncation glyph(s) at
  3422          the right margin.  Otherwise, leave room for the continuation
  3423          glyph(s).  Done only if the window has no right fringe.  */
  3424       if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
  3425         {
  3426           if (it->line_wrap == TRUNCATE)
  3427             it->last_visible_x -= it->truncation_pixel_width;
  3428           else
  3429             it->last_visible_x -= it->continuation_pixel_width;
  3430         }
  3431 
  3432       it->tab_line_p = window_wants_tab_line (w);
  3433       it->header_line_p = window_wants_header_line (w);
  3434       body_height = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  3435       it->current_y =  body_height + w->vscroll;
  3436     }
  3437 
  3438   /* Leave room for a border glyph.  */
  3439   if (!FRAME_WINDOW_P (it->f)
  3440       && !WINDOW_RIGHTMOST_P (it->w))
  3441     it->last_visible_x -= 1;
  3442 
  3443   it->last_visible_y = window_text_bottom_y (w);
  3444   body_height += it->last_visible_y;
  3445   if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3446       && body_height != w->old_body_pixel_height)
  3447     FRAME_WINDOW_CHANGE (it->f) = true;
  3448 
  3449   /* For mode lines and alike, arrange for the first glyph having a
  3450      left box line if the face specifies a box.  */
  3451   if (base_face_id != DEFAULT_FACE_ID)
  3452     {
  3453       struct face *face;
  3454 
  3455       it->face_id = remapped_base_face_id;
  3456 
  3457       /* If we have a boxed mode line, make the first character appear
  3458          with a left box line.  */
  3459       face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
  3460       if (face && face->box != FACE_NO_BOX)
  3461         {
  3462           int box_thickness = face->box_vertical_line_width;
  3463           it->face_box_p = true;
  3464           it->start_of_box_run_p = true;
  3465           /* Make sure we will have enough horizontal space to add the
  3466              right box line at the end.  */
  3467           if (box_thickness > 0)
  3468             it->last_visible_x -= box_thickness;
  3469         }
  3470     }
  3471 
  3472   /* If a buffer position was specified, set the iterator there,
  3473      getting overlays and face properties from that position.  */
  3474   if (charpos >= BUF_BEG (current_buffer))
  3475     {
  3476       it->stop_charpos = charpos;
  3477       it->end_charpos = ZV;
  3478       eassert (charpos == BYTE_TO_CHAR (bytepos));
  3479       IT_CHARPOS (*it) = charpos;
  3480       IT_BYTEPOS (*it) = bytepos;
  3481 
  3482       /* We will rely on `reseat' to set this up properly, via
  3483          handle_face_prop.  */
  3484       it->face_id = it->base_face_id;
  3485 
  3486       it->start = it->current;
  3487       /* Do we need to reorder bidirectional text?  Not if this is a
  3488          unibyte buffer: by definition, none of the single-byte
  3489          characters are strong R2L, so no reordering is needed.  And
  3490          bidi.c doesn't support unibyte buffers anyway.  Also, don't
  3491          reorder while we are loading loadup.el, since the tables of
  3492          character properties needed for reordering are not yet
  3493          available.  */
  3494       it->bidi_p =
  3495         !redisplay__inhibit_bidi
  3496         && !NILP (BVAR (current_buffer, bidi_display_reordering))
  3497         && it->multibyte_p;
  3498 
  3499       /* If we are to reorder bidirectional text, init the bidi
  3500          iterator.  */
  3501       if (it->bidi_p)
  3502         {
  3503           /*  Since we don't know at this point whether there will be
  3504               any R2L lines in the window, we reserve space for
  3505               truncation/continuation glyphs even if only the left
  3506               fringe is absent.  */
  3507           if (base_face_id == DEFAULT_FACE_ID
  3508               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
  3509               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
  3510             {
  3511               if (it->line_wrap == TRUNCATE)
  3512                 it->last_visible_x -= it->truncation_pixel_width;
  3513               else
  3514                 it->last_visible_x -= it->continuation_pixel_width;
  3515             }
  3516           /* Note the paragraph direction that this buffer wants to
  3517              use.  */
  3518           if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3519                   Qleft_to_right))
  3520             it->paragraph_embedding = L2R;
  3521           else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3522                        Qright_to_left))
  3523             it->paragraph_embedding = R2L;
  3524           else
  3525             it->paragraph_embedding = NEUTRAL_DIR;
  3526           bidi_unshelve_cache (NULL, false);
  3527           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  3528                         &it->bidi_it);
  3529         }
  3530 
  3531       /* This is set only when long_line_optimizations_p is non-zero
  3532          for the current buffer.  */
  3533       it->medium_narrowing_begv = 0;
  3534 
  3535       /* Compute faces etc.  */
  3536       reseat (it, it->current.pos, true);
  3537     }
  3538 
  3539   CHECK_IT (it);
  3540 }
  3541 
  3542 /* How Emacs deals with long lines.
  3543 
  3544    (1) When a buffer is about to be (re)displayed, 'redisplay_window'
  3545    detects, with a heuristic, whether it contains long lines.
  3546 
  3547    This happens in 'redisplay_window' because it is only displaying
  3548    buffers with long lines that is problematic.  In other words, none
  3549    of the optimizations described below is ever used in buffers that
  3550    are never displayed.
  3551 
  3552    This happens with a heuristic, which checks whether a buffer
  3553    contains long lines, each time its contents have changed "enough"
  3554    between two redisplay cycles, because a buffer without long lines
  3555    can become a buffer with long lines at any time, for example after
  3556    a yank command, or after a replace command, or while the output of
  3557    an external process is inserted in a buffer.
  3558 
  3559    When Emacs has detected that a buffer contains long lines, the
  3560    buffer-local variable 'long_line_optimizations_p' (in 'struct
  3561    buffer') is set, and Emacs does not try to detect whether the
  3562    buffer does or does not contain long lines anymore.
  3563 
  3564    What a long line is depends on the variable 'long-line-threshold',
  3565    whose default value is 50000 (characters).
  3566 
  3567    (2) When a buffer with long lines is (re)displayed, the amount of
  3568    data that the display routines consider is, in a few well-chosen
  3569    places, limited with a temporary restriction, whose bounds are
  3570    calculated with the functions below.
  3571 
  3572    (2.1) 'get_small_narrowing_begv' is used to create a restriction
  3573    which starts a few hundred characters before point.  The exact
  3574    number of characters depends on the width of the window in which
  3575    the buffer is displayed.
  3576 
  3577    There is no corresponding 'get_small_narrowing_zv' function,
  3578    because it is not necessary to set the end limit of that
  3579    restriction.
  3580 
  3581    This restriction is used in four places, namely:
  3582    'back_to_previous_line_start' and 'move_it_vertically_backward'
  3583    (with the 'SET_WITH_NARROWED_BEGV' macro), and in
  3584    'composition_compute_stop_pos' and 'find_automatic_composition' (in
  3585    a conditional statement depending on 'long_line_optimizations_p').
  3586 
  3587    (2.2) 'get_medium_narrowing_begv' is used to create a restriction
  3588    which starts a few thousand characters before point.  The exact
  3589    number of characters depends on the size (width and height) of the
  3590    window in which the buffer is displayed.  For performance reasons,
  3591    the return value of that function is cached in 'struct it', in the
  3592    'medium_narrowing_begv' field.
  3593 
  3594    The corresponding function 'get_medium_narrowing_zv' (and
  3595    'medium_narrowing_zv' field in 'struct it') is not used to set the
  3596    end limit of the restriction, which is again unnecessary, but to
  3597    determine, in 'reseat', whether the iterator has moved far enough
  3598    from its original position, and whether the start position of the
  3599    restriction must be computed anew.
  3600 
  3601    This restriction is used in a single place:
  3602    'get_visually_first_element', with the 'SET_WITH_NARROWED_BEGV'
  3603    macro.
  3604 
  3605    (2.3) 'get_large_narrowing_begv' and 'get_large_narrowing_zv' are
  3606    used to create a restriction which starts a few hundred thousand
  3607    characters before point and ends a few hundred thousand characters
  3608    after point.  The size of that restriction depends on the variable
  3609    'long-line-optimizations-region-size', whose default value is
  3610    500000 (characters); it can be adjusted by a few hundred characters
  3611    depending on 'long-line-optimizations-bol-search-limit', whose
  3612    default value is 128 (characters).
  3613 
  3614    For performance reasons again, the return values of these functions
  3615    are stored in the 'large_narrowing_begv' and 'large_narrowing_zv'
  3616    fields in 'struct it'.
  3617 
  3618    The restriction defined by these values is used around three
  3619    low-level hooks: around 'fontification-functions', in
  3620    'handle_fontified_prop', and around 'pre-command-hook' and
  3621    'post-command-hook', in 'safe_run_hooks_maybe_narrowed', which is
  3622    called in 'command_loop_1'.  These restrictions are set around
  3623    these hooks with 'labeled_narrow_to_region'; the restrictions are
  3624    labeled, and cannot be removed with a call to 'widen', but can be
  3625    removed with 'without-restriction' with a :label argument.
  3626 */
  3627 
  3628 static int
  3629 get_narrowed_width (struct window *w)
  3630 {
  3631   /* In a character-only terminal, only one font size is used, so we
  3632      can use a smaller factor.  */
  3633   int fact = FRAME_WINDOW_P (XFRAME (w->frame)) ? 3 : 2;
  3634   /* If the window has no fringes (in a character-only terminal or in
  3635      a GUI frame without fringes), subtract 1 from the width for the
  3636      '\' line wrapping character.  */
  3637   int width = window_body_width (w, WINDOW_BODY_IN_CANONICAL_CHARS)
  3638     - ((WINDOW_RIGHT_FRINGE_WIDTH (w) == 0
  3639         || WINDOW_LEFT_FRINGE_WIDTH (w) == 0) ? 1 : 0);
  3640   return fact * max (1, width);
  3641 }
  3642 
  3643 static int
  3644 get_narrowed_len (struct window *w)
  3645 {
  3646   int height = window_body_height (w, WINDOW_BODY_IN_CANONICAL_CHARS);
  3647   return get_narrowed_width (w) * max (1, height);
  3648 }
  3649 
  3650 static ptrdiff_t
  3651 get_medium_narrowing_begv (struct window *w, ptrdiff_t pos)
  3652 {
  3653   int len = get_narrowed_len (w);
  3654   return max ((pos / len - 1) * len, BEGV);
  3655 }
  3656 
  3657 static ptrdiff_t
  3658 get_medium_narrowing_zv (struct window *w, ptrdiff_t pos)
  3659 {
  3660   int len = get_narrowed_len (w);
  3661   return min ((pos / len + 1) * len, ZV);
  3662 }
  3663 
  3664 /* Find the position of the last BOL before POS, unless it is too far
  3665    away.  The buffer portion in which the search occurs is gradually
  3666    enlarged: [POS-500..POS], [POS-5500..POS-500],
  3667    [POS-55500..POS-5500], and finally [POS-555500..POS-55500].  Return
  3668    BEGV-1 if no BOL was found in [POS-555500..POS].  */
  3669 static ptrdiff_t
  3670 get_nearby_bol_pos (ptrdiff_t pos)
  3671 {
  3672   ptrdiff_t start, pos_bytepos, cur, next, found, bol = BEGV - 1, init_pos = pos;
  3673   int dist;
  3674   for (dist = 500; dist <= 500000; dist *= 10)
  3675     {
  3676       pos_bytepos = pos == BEGV ? BEGV_BYTE : CHAR_TO_BYTE (pos);
  3677       start = pos - dist < BEGV ? BEGV : pos - dist;
  3678       for (cur = start; cur < pos; cur = next)
  3679         {
  3680           next = find_newline1 (cur, CHAR_TO_BYTE (cur),
  3681                                 pos, pos_bytepos,
  3682                                 1, &found, NULL, false);
  3683           if (found)
  3684             bol = next;
  3685           else
  3686             break;
  3687         }
  3688       if (bol >= BEGV || start == BEGV)
  3689         break;
  3690       else
  3691         pos = pos - dist < BEGV ? BEGV : pos - dist;
  3692     }
  3693   eassert (bol <= init_pos);
  3694   return bol;
  3695 }
  3696 
  3697 ptrdiff_t
  3698 get_small_narrowing_begv (struct window *w, ptrdiff_t pos)
  3699 {
  3700   int len = get_narrowed_width (w);
  3701   ptrdiff_t bol_pos = max (get_nearby_bol_pos (pos), BEGV);
  3702   return max (bol_pos + ((pos - bol_pos) / len - 1) * len, BEGV);
  3703 }
  3704 
  3705 ptrdiff_t
  3706 get_large_narrowing_begv (ptrdiff_t pos)
  3707 {
  3708   if (long_line_optimizations_region_size <= 0)
  3709     return BEGV;
  3710   int len = long_line_optimizations_region_size / 2;
  3711   int begv = max (pos - len, BEGV);
  3712   int limit = long_line_optimizations_bol_search_limit;
  3713   while (limit > 0)
  3714     {
  3715       if (begv == BEGV || FETCH_BYTE (CHAR_TO_BYTE (begv) - 1) == '\n')
  3716         return begv;
  3717       begv--;
  3718       limit--;
  3719     }
  3720   return begv;
  3721 }
  3722 
  3723 ptrdiff_t
  3724 get_large_narrowing_zv (ptrdiff_t pos)
  3725 {
  3726   if (long_line_optimizations_region_size <= 0)
  3727     return ZV;
  3728   int len = long_line_optimizations_region_size / 2;
  3729   return min (pos + len, ZV);
  3730 }
  3731 
  3732 static void
  3733 unwind_narrowed_begv (Lisp_Object point_min)
  3734 {
  3735   SET_BUF_BEGV (current_buffer, XFIXNUM (point_min));
  3736 }
  3737 
  3738 /* Set DST to EXPR.  When IT indicates that BEGV should temporarily be
  3739    updated to optimize display, evaluate EXPR with BEGV set to BV.  */
  3740 
  3741 #define SET_WITH_NARROWED_BEGV(IT,DST,EXPR,BV)                          \
  3742   do {                                                                  \
  3743     if (IT->medium_narrowing_begv)                                      \
  3744       {                                                                 \
  3745         specpdl_ref count = SPECPDL_INDEX ();                           \
  3746         record_unwind_protect (unwind_narrowed_begv, Fpoint_min ());    \
  3747         SET_BUF_BEGV (current_buffer, BV);                              \
  3748         DST = EXPR;                                                     \
  3749         unbind_to (count, Qnil);                                        \
  3750       }                                                                 \
  3751     else                                                                \
  3752       DST = EXPR;                                                       \
  3753   } while (0)
  3754 
  3755 /* Initialize IT for the display of window W with window start POS.  */
  3756 
  3757 void
  3758 start_display (struct it *it, struct window *w, struct text_pos pos)
  3759 {
  3760   struct glyph_row *row;
  3761   int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
  3762 
  3763   row = w->desired_matrix->rows + first_vpos;
  3764   init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
  3765   it->first_vpos = first_vpos;
  3766 
  3767   /* Don't reseat to previous visible line start if current start
  3768      position is in a string or image.  */
  3769   if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
  3770     {
  3771       int first_y = it->current_y;
  3772 
  3773       /* If window start is not at a line start, skip forward to POS to
  3774          get the correct continuation lines width.  */
  3775       bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
  3776                                   || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
  3777       if (!start_at_line_beg_p)
  3778         {
  3779           int new_x;
  3780 
  3781           reseat_at_previous_visible_line_start (it);
  3782           move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
  3783 
  3784           new_x = it->current_x + it->pixel_width;
  3785 
  3786           /* If lines are continued, this line may end in the middle
  3787              of a multi-glyph character (e.g. a control character
  3788              displayed as \003, or in the middle of an overlay
  3789              string).  In this case move_it_to above will not have
  3790              taken us to the start of the continuation line but to the
  3791              end of the continued line.  */
  3792           if (it->current_x > 0
  3793               && it->line_wrap != TRUNCATE /* Lines are continued.  */
  3794               && (/* And glyph doesn't fit on the line.  */
  3795                   new_x > it->last_visible_x
  3796                   /* Or it fits exactly and we're on a window
  3797                      system frame.  */
  3798                   || (new_x == it->last_visible_x
  3799                       && FRAME_WINDOW_P (it->f)
  3800                       && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  3801                           ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  3802                           : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  3803             {
  3804               if ((it->current.dpvec_index >= 0
  3805                    || it->current.overlay_string_index >= 0)
  3806                   /* If we are on a newline from a display vector or
  3807                      overlay string, then we are already at the end of
  3808                      a screen line; no need to go to the next line in
  3809                      that case, as this line is not really continued.
  3810                      (If we do go to the next line, C-e will not DTRT.)  */
  3811                   && it->c != '\n')
  3812                 {
  3813                   set_iterator_to_next (it, true);
  3814                   move_it_in_display_line_to (it, -1, -1, 0);
  3815                 }
  3816 
  3817               it->continuation_lines_width += it->current_x;
  3818             }
  3819           /* If the character at POS is displayed via a display
  3820              vector, move_it_to above stops at the final glyph of
  3821              IT->dpvec.  To make the caller redisplay that character
  3822              again (a.k.a. start at POS), we need to reset the
  3823              dpvec_index to the beginning of IT->dpvec.  */
  3824           else if (it->current.dpvec_index >= 0)
  3825             it->current.dpvec_index = 0;
  3826 
  3827           /* We're starting a new display line, not affected by the
  3828              height of the continued line, so clear the appropriate
  3829              fields in the iterator structure.  */
  3830           it->max_ascent = it->max_descent = 0;
  3831           it->max_phys_ascent = it->max_phys_descent = 0;
  3832 
  3833           it->current_y = first_y;
  3834           it->vpos = 0;
  3835           it->current_x = it->hpos = 0;
  3836         }
  3837     }
  3838 }
  3839 
  3840 
  3841 /* Return true if POS is a position in ellipses displayed for invisible
  3842    text.  W is the window we display, for text property lookup.  */
  3843 
  3844 static bool
  3845 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
  3846 {
  3847   Lisp_Object prop, window;
  3848   bool ellipses_p = false;
  3849   ptrdiff_t charpos = CHARPOS (pos->pos);
  3850 
  3851   /* If POS specifies a position in a display vector, this might
  3852      be for an ellipsis displayed for invisible text.  We won't
  3853      get the iterator set up for delivering that ellipsis unless
  3854      we make sure that it gets aware of the invisible text.  */
  3855   if (pos->dpvec_index >= 0
  3856       && pos->overlay_string_index < 0
  3857       && CHARPOS (pos->string_pos) < 0
  3858       && charpos > BEGV
  3859       && (XSETWINDOW (window, w),
  3860           prop = Fget_char_property (make_fixnum (charpos),
  3861                                      Qinvisible, window),
  3862           TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
  3863     {
  3864       prop = Fget_char_property (make_fixnum (charpos - 1), Qinvisible,
  3865                                  window);
  3866       ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
  3867     }
  3868 
  3869   return ellipses_p;
  3870 }
  3871 
  3872 
  3873 /* Initialize IT for stepping through current_buffer in window W,
  3874    starting at position POS that includes overlay string and display
  3875    vector/ control character translation position information.  Value
  3876    is false if there are overlay strings with newlines at POS.  */
  3877 
  3878 static bool
  3879 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
  3880 {
  3881   ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
  3882   int i;
  3883   bool overlay_strings_with_newlines = false;
  3884 
  3885   /* If POS specifies a position in a display vector, this might
  3886      be for an ellipsis displayed for invisible text.  We won't
  3887      get the iterator set up for delivering that ellipsis unless
  3888      we make sure that it gets aware of the invisible text.  */
  3889   if (in_ellipses_for_invisible_text_p (pos, w))
  3890     {
  3891       --charpos;
  3892       bytepos = 0;
  3893     }
  3894 
  3895   /* Keep in mind: the call to reseat in init_iterator skips invisible
  3896      text, so we might end up at a position different from POS.  This
  3897      is only a problem when POS is a row start after a newline and an
  3898      overlay starts there with an after-string, and the overlay has an
  3899      invisible property.  Since we don't skip invisible text in
  3900      display_line and elsewhere immediately after consuming the
  3901      newline before the row start, such a POS will not be in a string,
  3902      but the call to init_iterator below will move us to the
  3903      after-string.  */
  3904   init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
  3905 
  3906   /* This only scans the current chunk -- it should scan all chunks.
  3907      However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
  3908      to 16 in 22.1 to make this a lesser problem.  */
  3909   for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
  3910     {
  3911       const char *s = SSDATA (it->overlay_strings[i]);
  3912       const char *e = s + SBYTES (it->overlay_strings[i]);
  3913 
  3914       while (s < e && *s != '\n')
  3915         ++s;
  3916 
  3917       if (s < e)
  3918         {
  3919           overlay_strings_with_newlines = true;
  3920           break;
  3921         }
  3922     }
  3923 
  3924   /* If position is within an overlay string, set up IT to the right
  3925      overlay string.  */
  3926   if (pos->overlay_string_index >= 0)
  3927     {
  3928       int relative_index;
  3929 
  3930       /* If the first overlay string happens to have a `display'
  3931          property for an image, the iterator will be set up for that
  3932          image, and we have to undo that setup first before we can
  3933          correct the overlay string index.  */
  3934       if (it->method == GET_FROM_IMAGE)
  3935         pop_it (it);
  3936 
  3937       /* We already have the first chunk of overlay strings in
  3938          IT->overlay_strings.  Load more until the one for
  3939          pos->overlay_string_index is in IT->overlay_strings.  */
  3940       if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
  3941         {
  3942           ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
  3943           it->current.overlay_string_index = 0;
  3944           while (n--)
  3945             {
  3946               load_overlay_strings (it, 0);
  3947               it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
  3948             }
  3949         }
  3950 
  3951       it->current.overlay_string_index = pos->overlay_string_index;
  3952       relative_index = (it->current.overlay_string_index
  3953                         % OVERLAY_STRING_CHUNK_SIZE);
  3954       it->string = it->overlay_strings[relative_index];
  3955       eassert (STRINGP (it->string));
  3956       it->current.string_pos = pos->string_pos;
  3957       it->method = GET_FROM_STRING;
  3958       it->end_charpos = SCHARS (it->string);
  3959       /* Set up the bidi iterator for this overlay string.  */
  3960       if (it->bidi_p)
  3961         {
  3962           it->bidi_it.string.lstring = it->string;
  3963           it->bidi_it.string.s = NULL;
  3964           it->bidi_it.string.schars = SCHARS (it->string);
  3965           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  3966           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  3967           it->bidi_it.string.unibyte = !it->multibyte_p;
  3968           it->bidi_it.w = it->w;
  3969           bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3970                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  3971 
  3972           /* Synchronize the state of the bidi iterator with
  3973              pos->string_pos.  For any string position other than
  3974              zero, this will be done automagically when we resume
  3975              iteration over the string and get_visually_first_element
  3976              is called.  But if string_pos is zero, and the string is
  3977              to be reordered for display, we need to resync manually,
  3978              since it could be that the iteration state recorded in
  3979              pos ended at string_pos of 0 moving backwards in string.  */
  3980           if (CHARPOS (pos->string_pos) == 0)
  3981             {
  3982               get_visually_first_element (it);
  3983               if (IT_STRING_CHARPOS (*it) != 0)
  3984                 do {
  3985                   /* Paranoia.  */
  3986                   eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
  3987                   bidi_move_to_visually_next (&it->bidi_it);
  3988                 } while (it->bidi_it.charpos != 0);
  3989             }
  3990           eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  3991                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
  3992         }
  3993     }
  3994 
  3995   if (CHARPOS (pos->string_pos) >= 0)
  3996     {
  3997       /* Recorded position is not in an overlay string, but in another
  3998          string.  This can only be a string from a `display' property.
  3999          IT should already be filled with that string.  */
  4000       it->current.string_pos = pos->string_pos;
  4001       eassert (STRINGP (it->string));
  4002       if (it->bidi_p)
  4003         bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  4004                       FRAME_WINDOW_P (it->f), &it->bidi_it);
  4005     }
  4006 
  4007   /* Restore position in display vector translations, control
  4008      character translations or ellipses.  */
  4009   if (pos->dpvec_index >= 0)
  4010     {
  4011       if (it->dpvec == NULL)
  4012         get_next_display_element (it);
  4013       eassert (it->dpvec && it->current.dpvec_index == 0);
  4014       it->current.dpvec_index = pos->dpvec_index;
  4015     }
  4016 
  4017   CHECK_IT (it);
  4018   return !overlay_strings_with_newlines;
  4019 }
  4020 
  4021 
  4022 /* Initialize IT for stepping through current_buffer in window W
  4023    starting at ROW->start.  */
  4024 
  4025 static void
  4026 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
  4027 {
  4028   init_from_display_pos (it, w, &row->start);
  4029   it->start = row->start;
  4030   it->continuation_lines_width = row->continuation_lines_width;
  4031   CHECK_IT (it);
  4032 }
  4033 
  4034 
  4035 /* Initialize IT for stepping through current_buffer in window W
  4036    starting in the line following ROW, i.e. starting at ROW->end.
  4037    Value is false if there are overlay strings with newlines at ROW's
  4038    end position, or if the following row begins with bidi-reordered
  4039    characters that could be composed.  */
  4040 
  4041 static bool
  4042 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
  4043 {
  4044   bool success = false;
  4045 
  4046   if (init_from_display_pos (it, w, &row->end))
  4047     {
  4048       if (row->continued_p)
  4049         it->continuation_lines_width
  4050           = row->continuation_lines_width + row->pixel_width;
  4051       CHECK_IT (it);
  4052       /* Initializing IT in the presence of compositions in reordered
  4053          rows is tricky: row->end above will generally cause us to
  4054          start at position that is not the first one in the logical
  4055          order, and we might therefore miss the composition earlier in
  4056          the buffer that affects how glypsh are laid out in this row.
  4057          So we punt instead.  Note: the test below works because
  4058          get_next_display_element calls get_visually_first_element,
  4059          which calls composition_compute_stop_pos, which populates
  4060          it->cmp_it.  */
  4061       if (get_next_display_element (it)
  4062           && (it->bidi_it.scan_dir == -1 && it->cmp_it.id >= 0))
  4063         success = false;
  4064       else
  4065         success = true;
  4066     }
  4067 
  4068   return success;
  4069 }
  4070 
  4071 
  4072 
  4073 
  4074 /***********************************************************************
  4075                            Text properties
  4076  ***********************************************************************/
  4077 
  4078 /* Called when IT reaches IT->stop_charpos.  Handle text property and
  4079    overlay changes.  Set IT->stop_charpos to the next position where
  4080    to stop.  */
  4081 
  4082 static void
  4083 handle_stop (struct it *it)
  4084 {
  4085   enum prop_handled handled;
  4086   bool handle_overlay_change_p;
  4087   struct props *p;
  4088 
  4089   it->dpvec = NULL;
  4090   it->current.dpvec_index = -1;
  4091   handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
  4092   it->ellipsis_p = false;
  4093 
  4094   /* Use face of preceding text for ellipsis (if invisible) */
  4095   if (it->selective_display_ellipsis_p)
  4096     it->saved_face_id = it->face_id;
  4097 
  4098   /* Here's the description of the semantics of, and the logic behind,
  4099      the various HANDLED_* statuses:
  4100 
  4101      HANDLED_NORMALLY means the handler did its job, and the loop
  4102      should proceed to calling the next handler in order.
  4103 
  4104      HANDLED_RECOMPUTE_PROPS means the handler caused a significant
  4105      change in the properties and overlays at current position, so the
  4106      loop should be restarted, to re-invoke the handlers that were
  4107      already called.  This happens when fontification-functions were
  4108      called by handle_fontified_prop, and actually fontified
  4109      something.  Another case where HANDLED_RECOMPUTE_PROPS is
  4110      returned is when we discover overlay strings that need to be
  4111      displayed right away.  The loop below will continue for as long
  4112      as the status is HANDLED_RECOMPUTE_PROPS.
  4113 
  4114      HANDLED_RETURN means return immediately to the caller, to
  4115      continue iteration without calling any further handlers.  This is
  4116      used when we need to act on some property right away, for example
  4117      when we need to display the ellipsis or a replacing display
  4118      property, such as display string or image.
  4119 
  4120      HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
  4121      consumed, and the handler switched to the next overlay string.
  4122      This signals the loop below to refrain from looking for more
  4123      overlays before all the overlay strings of the current overlay
  4124      are processed.
  4125 
  4126      Some of the handlers called by the loop push the iterator state
  4127      onto the stack (see 'push_it'), and arrange for the iteration to
  4128      continue with another object, such as an image, a display string,
  4129      or an overlay string.  In most such cases, it->stop_charpos is
  4130      set to the first character of the string, so that when the
  4131      iteration resumes, this function will immediately be called
  4132      again, to examine the properties at the beginning of the string.
  4133 
  4134      When a display or overlay string is exhausted, the iterator state
  4135      is popped (see 'pop_it'), and iteration continues with the
  4136      previous object.  Again, in many such cases this function is
  4137      called again to find the next position where properties might
  4138      change.  */
  4139 
  4140   do
  4141     {
  4142       handled = HANDLED_NORMALLY;
  4143 
  4144       /* Call text property handlers.  */
  4145       for (p = it_props; p->handler; ++p)
  4146         {
  4147           handled = p->handler (it);
  4148 
  4149           if (handled == HANDLED_RECOMPUTE_PROPS)
  4150             break;
  4151           else if (handled == HANDLED_RETURN)
  4152             {
  4153               /* We still want to show before and after strings from
  4154                  overlays even if the actual buffer text is replaced.  */
  4155               if (!handle_overlay_change_p
  4156                   || it->sp > 1
  4157                   /* Don't call get_overlay_strings_1 if we already
  4158                      have overlay strings loaded, because doing so
  4159                      will load them again and push the iterator state
  4160                      onto the stack one more time, which is not
  4161                      expected by the rest of the code that processes
  4162                      overlay strings.  */
  4163                   || (it->current.overlay_string_index < 0
  4164                       && !get_overlay_strings_1 (it, 0, false)))
  4165                 {
  4166                   if (it->ellipsis_p)
  4167                     setup_for_ellipsis (it, 0);
  4168                   /* When handling a display spec, we might load an
  4169                      empty string.  In that case, discard it here.  We
  4170                      used to discard it in handle_single_display_spec,
  4171                      but that causes get_overlay_strings_1, above, to
  4172                      ignore overlay strings that we must check.  */
  4173                   if (STRINGP (it->string) && !SCHARS (it->string))
  4174                     pop_it (it);
  4175                   return;
  4176                 }
  4177               else if (STRINGP (it->string) && !SCHARS (it->string))
  4178                 pop_it (it);
  4179               else
  4180                 {
  4181                   it->string_from_display_prop_p = false;
  4182                   it->from_disp_prop_p = false;
  4183                   handle_overlay_change_p = false;
  4184                 }
  4185               handled = HANDLED_RECOMPUTE_PROPS;
  4186               break;
  4187             }
  4188           else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
  4189             handle_overlay_change_p = false;
  4190         }
  4191 
  4192       if (handled != HANDLED_RECOMPUTE_PROPS)
  4193         {
  4194           /* Don't check for overlay strings below when set to deliver
  4195              characters from a display vector.  */
  4196           if (it->method == GET_FROM_DISPLAY_VECTOR)
  4197             handle_overlay_change_p = false;
  4198 
  4199           /* Handle overlay changes.
  4200              This sets HANDLED to HANDLED_RECOMPUTE_PROPS
  4201              if it finds overlays.  */
  4202           if (handle_overlay_change_p)
  4203             handled = handle_overlay_change (it);
  4204         }
  4205 
  4206       if (it->ellipsis_p)
  4207         {
  4208           setup_for_ellipsis (it, 0);
  4209           break;
  4210         }
  4211     }
  4212   while (handled == HANDLED_RECOMPUTE_PROPS);
  4213 
  4214   /* Determine where to stop next.  */
  4215   if (handled == HANDLED_NORMALLY)
  4216     compute_stop_pos (it);
  4217 }
  4218 
  4219 
  4220 /* Compute IT->stop_charpos from text property and overlay change
  4221    information for IT's current position.  */
  4222 
  4223 static void
  4224 compute_stop_pos (struct it *it)
  4225 {
  4226   register INTERVAL iv, next_iv;
  4227   Lisp_Object object, limit, position;
  4228   ptrdiff_t charpos, bytepos, cmp_limit_pos = -1;
  4229 
  4230   if (STRINGP (it->string))
  4231     {
  4232       /* Strings are usually short, so don't limit the search for
  4233          properties.  */
  4234       it->stop_charpos = it->end_charpos;
  4235       object = it->string;
  4236       limit = Qnil;
  4237       charpos = IT_STRING_CHARPOS (*it);
  4238       bytepos = IT_STRING_BYTEPOS (*it);
  4239     }
  4240   else
  4241     {
  4242       ptrdiff_t pos;
  4243 
  4244       /* If end_charpos is out of range for some reason, such as a
  4245          misbehaving display function, rationalize it (Bug#5984).  */
  4246       if (it->end_charpos > ZV)
  4247         it->end_charpos = ZV;
  4248       it->stop_charpos = it->end_charpos;
  4249 
  4250       /* If next overlay change is in front of the current stop pos
  4251          (which is IT->end_charpos), stop there.  Note: value of
  4252          next_overlay_change is point-max if no overlay change
  4253          follows.  */
  4254       charpos = IT_CHARPOS (*it);
  4255       bytepos = IT_BYTEPOS (*it);
  4256       pos = next_overlay_change (charpos);
  4257       if (pos < it->stop_charpos)
  4258         it->stop_charpos = pos;
  4259       /* If we are breaking compositions at point, stop at point.  */
  4260       if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
  4261           && !NILP (Vauto_composition_mode)
  4262           && composition_break_at_point
  4263           && charpos < PT && PT < it->stop_charpos)
  4264         it->stop_charpos = PT;
  4265 
  4266       /* Set up variables for computing the stop position from text
  4267          property changes.  */
  4268       XSETBUFFER (object, current_buffer);
  4269       pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4270       /* Make sure the above arbitrary limit position is not in the
  4271          middle of composable text, so we don't break compositions by
  4272          submitting the composable text to the shaper in separate
  4273          chunks.  We play safe here by assuming that only SPC, TAB,
  4274          FF, and NL cannot be in some composition; in particular, most
  4275          ASCII punctuation characters could be composed into ligatures.  */
  4276       if (!composition_break_at_point
  4277           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  4278           && !NILP (Vauto_composition_mode))
  4279         {
  4280           ptrdiff_t endpos = charpos + 10 * TEXT_PROP_DISTANCE_LIMIT;
  4281           bool found = false;
  4282 
  4283           if (pos > ZV)
  4284             pos = ZV;
  4285           if (endpos > ZV)
  4286             endpos = ZV;
  4287           ptrdiff_t bpos = CHAR_TO_BYTE (pos);
  4288           while (pos < endpos)
  4289             {
  4290               int ch = fetch_char_advance_no_check (&pos, &bpos);
  4291               if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\f')
  4292                 {
  4293                   found = true;
  4294                   break;
  4295                 }
  4296             }
  4297           if (found)
  4298             {
  4299               pos--;
  4300               cmp_limit_pos = pos;
  4301             }
  4302           else if (it->stop_charpos < endpos)
  4303             pos = it->stop_charpos;
  4304           else
  4305             {
  4306               /* Give up and use the original arbitrary limit.  */
  4307               pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4308             }
  4309         }
  4310       limit = make_fixnum (pos);
  4311     }
  4312 
  4313   /* Get the interval containing IT's position.  Value is a null
  4314      interval if there isn't such an interval.  */
  4315   position = make_fixnum (charpos);
  4316   iv = validate_interval_range (object, &position, &position, false);
  4317   if (iv)
  4318     {
  4319       Lisp_Object values_here[LAST_PROP_IDX];
  4320       struct props *p;
  4321 
  4322       /* Get properties here.  */
  4323       for (p = it_props; p->handler; ++p)
  4324         values_here[p->idx] = textget (iv->plist,
  4325                                        builtin_lisp_symbol (p->name));
  4326 
  4327       /* Look for an interval following iv that has different
  4328          properties.  */
  4329       for (next_iv = next_interval (iv);
  4330            (next_iv
  4331             && (NILP (limit)
  4332                 || XFIXNAT (limit) > next_iv->position));
  4333            next_iv = next_interval (next_iv))
  4334         {
  4335           for (p = it_props; p->handler; ++p)
  4336             {
  4337               Lisp_Object new_value = textget (next_iv->plist,
  4338                                                builtin_lisp_symbol (p->name));
  4339               if (!EQ (values_here[p->idx], new_value))
  4340                 break;
  4341             }
  4342 
  4343           if (p->handler)
  4344             break;
  4345         }
  4346 
  4347       if (next_iv)
  4348         {
  4349           if (FIXNUMP (limit)
  4350               && next_iv->position >= XFIXNAT (limit))
  4351             /* No text property change up to limit.  */
  4352             it->stop_charpos = min (XFIXNAT (limit), it->stop_charpos);
  4353           else
  4354             /* Text properties change in next_iv.  */
  4355             it->stop_charpos = min (it->stop_charpos, next_iv->position);
  4356         }
  4357     }
  4358 
  4359   if (it->cmp_it.id < 0
  4360       && (STRINGP (it->string)
  4361           || ((!it->bidi_p || it->bidi_it.scan_dir >= 0)
  4362               && it->cmp_it.stop_pos <= IT_CHARPOS (*it))))
  4363     {
  4364       ptrdiff_t stoppos = it->end_charpos;
  4365 
  4366       /* If we found, above, a buffer position that cannot be part of
  4367          an automatic composition, limit the search of composable
  4368          characters to that position.  */
  4369       if (it->bidi_p && it->bidi_it.scan_dir < 0)
  4370         stoppos = -1;
  4371       else if (cmp_limit_pos > 0)
  4372         stoppos = cmp_limit_pos;
  4373       /* Force composition_compute_stop_pos avoid the costly search
  4374          for static compositions, since those were already found by
  4375          looking at text properties, above.  */
  4376       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
  4377                                     stoppos, it->string, false);
  4378     }
  4379 
  4380   eassert (STRINGP (it->string)
  4381            || (it->stop_charpos >= BEGV
  4382                && it->stop_charpos >= IT_CHARPOS (*it)));
  4383 }
  4384 
  4385 /* How many characters forward to search for a display property or
  4386    display string.  Searching too far forward makes the bidi display
  4387    sluggish, especially in small windows.  */
  4388 #define MAX_DISP_SCAN 250
  4389 
  4390 /* Return the character position of a display string at or after
  4391    position specified by POSITION.  If no display string exists at or
  4392    after POSITION, return ZV.  A display string is either an overlay
  4393    with `display' property whose value is a string, or a `display'
  4394    text property whose value is a string.  STRING is data about the
  4395    string to iterate; if STRING->lstring is nil, we are iterating a
  4396    buffer.  FRAME_WINDOW_P is true when we are displaying a window
  4397    on a GUI frame.  DISP_PROP is set to zero if we searched
  4398    MAX_DISP_SCAN characters forward without finding any display
  4399    strings, non-zero otherwise.  It is set to 2 if the display string
  4400    uses any kind of `(space ...)' spec that will produce a stretch of
  4401    white space in the text area.  */
  4402 ptrdiff_t
  4403 compute_display_string_pos (struct text_pos *position,
  4404                             struct bidi_string_data *string,
  4405                             struct window *w,
  4406                             bool frame_window_p, int *disp_prop)
  4407 {
  4408   /* OBJECT = nil means current buffer.  */
  4409   Lisp_Object object, object1;
  4410   Lisp_Object pos, spec, limpos;
  4411   bool string_p = string && (STRINGP (string->lstring) || string->s);
  4412   ptrdiff_t eob = string_p ? string->schars : ZV;
  4413   ptrdiff_t begb = string_p ? 0 : BEGV;
  4414   ptrdiff_t bufpos, charpos = CHARPOS (*position);
  4415   ptrdiff_t lim =
  4416     (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
  4417   struct text_pos tpos;
  4418   int rv = 0;
  4419 
  4420   if (string && STRINGP (string->lstring))
  4421     object1 = object = string->lstring;
  4422   else if (w && !string_p)
  4423     {
  4424       XSETWINDOW (object, w);
  4425       object1 = Qnil;
  4426     }
  4427   else
  4428     object1 = object = Qnil;
  4429 
  4430   *disp_prop = 1;
  4431 
  4432   if (charpos >= eob
  4433       /* We don't support display properties whose values are strings
  4434          that have display string properties.  */
  4435       || string->from_disp_str
  4436       /* C strings cannot have display properties.  */
  4437       || (string->s && !STRINGP (object)))
  4438     {
  4439       *disp_prop = 0;
  4440       return eob;
  4441     }
  4442 
  4443   /* If the character at CHARPOS is where the display string begins,
  4444      return CHARPOS.  */
  4445   pos = make_fixnum (charpos);
  4446   if (STRINGP (object))
  4447     bufpos = string->bufpos;
  4448   else
  4449     bufpos = charpos;
  4450   tpos = *position;
  4451   if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
  4452       && (charpos <= begb
  4453           || !EQ (Fget_char_property (make_fixnum (charpos - 1), Qdisplay,
  4454                                       object),
  4455                   spec))
  4456       && (rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos, bufpos,
  4457                                     frame_window_p)))
  4458     {
  4459       if (rv == 2)
  4460         *disp_prop = 2;
  4461       return charpos;
  4462     }
  4463 
  4464   /* Look forward for the first character with a `display' property
  4465      that will replace the underlying text when displayed.  */
  4466   limpos = make_fixnum (lim);
  4467   do {
  4468     pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
  4469     CHARPOS (tpos) = XFIXNAT (pos);
  4470     if (CHARPOS (tpos) >= lim)
  4471       {
  4472         *disp_prop = 0;
  4473         break;
  4474       }
  4475     if (STRINGP (object))
  4476       BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
  4477     else
  4478       BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
  4479     spec = Fget_char_property (pos, Qdisplay, object);
  4480     if (!STRINGP (object))
  4481       bufpos = CHARPOS (tpos);
  4482   } while (NILP (spec)
  4483            || !(rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos,
  4484                                           bufpos, frame_window_p)));
  4485   if (rv == 2)
  4486     *disp_prop = 2;
  4487 
  4488   return CHARPOS (tpos);
  4489 }
  4490 
  4491 /* Return the character position of the end of the display string that
  4492    started at CHARPOS.  If there's no display string at CHARPOS,
  4493    return -1.  A display string is either an overlay with `display'
  4494    property whose value is a string or a `display' text property whose
  4495    value is a string.  */
  4496 ptrdiff_t
  4497 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
  4498 {
  4499   /* OBJECT = nil means current buffer.  */
  4500   Lisp_Object object =
  4501     (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
  4502   Lisp_Object pos = make_fixnum (charpos);
  4503   ptrdiff_t eob =
  4504     (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
  4505 
  4506   if (charpos >= eob || (string->s && !STRINGP (object)))
  4507     return eob;
  4508 
  4509   /* It could happen that the display property or overlay was removed
  4510      since we found it in compute_display_string_pos above.  One way
  4511      this can happen is if JIT font-lock was called (through
  4512      handle_fontified_prop), and jit-lock-functions remove text
  4513      properties or overlays from the portion of buffer that includes
  4514      CHARPOS.  Muse mode is known to do that, for example.  In this
  4515      case, we return -1 to the caller, to signal that no display
  4516      string is actually present at CHARPOS.  See bidi_fetch_char for
  4517      how this is handled.
  4518 
  4519      An alternative would be to never look for display properties past
  4520      it->stop_charpos.  But neither compute_display_string_pos nor
  4521      bidi_fetch_char that calls it know or care where the next
  4522      stop_charpos is.  */
  4523   if (NILP (Fget_char_property (pos, Qdisplay, object)))
  4524     return -1;
  4525 
  4526   /* Look forward for the first character where the `display' property
  4527      changes.  */
  4528   pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
  4529 
  4530   return XFIXNAT (pos);
  4531 }
  4532 
  4533 
  4534 
  4535 /***********************************************************************
  4536                             Fontification
  4537  ***********************************************************************/
  4538 
  4539 /* Handle changes in the `fontified' property of the current buffer by
  4540    calling hook functions from Qfontification_functions to fontify
  4541    regions of text.  */
  4542 
  4543 static enum prop_handled
  4544 handle_fontified_prop (struct it *it)
  4545 {
  4546   Lisp_Object prop, pos;
  4547   enum prop_handled handled = HANDLED_NORMALLY;
  4548 
  4549   if (!NILP (Vmemory_full))
  4550     return handled;
  4551 
  4552   /* Get the value of the `fontified' property at IT's current buffer
  4553      position.  (The `fontified' property doesn't have a special
  4554      meaning in strings.)  If the value is nil, call functions from
  4555      Qfontification_functions.  */
  4556   if (!STRINGP (it->string)
  4557       && it->s == NULL
  4558       && !NILP (Vfontification_functions)
  4559       && !(input_was_pending && redisplay_skip_fontification_on_input)
  4560       && !NILP (Vrun_hooks)
  4561       && (pos = make_fixnum (IT_CHARPOS (*it)),
  4562           prop = Fget_char_property (pos, Qfontified, Qnil),
  4563           /* Ignore the special cased nil value always present at EOB since
  4564              no amount of fontifying will be able to change it.  */
  4565           NILP (prop) && IT_CHARPOS (*it) < Z))
  4566     {
  4567       specpdl_ref count = SPECPDL_INDEX ();
  4568       Lisp_Object val;
  4569       struct buffer *obuf = current_buffer;
  4570       ptrdiff_t begv = BEGV, zv = ZV;
  4571       bool old_clip_changed = current_buffer->clip_changed;
  4572       bool saved_inhibit_flag = it->f->inhibit_clear_image_cache;
  4573 
  4574       val = Vfontification_functions;
  4575       specbind (Qfontification_functions, Qnil);
  4576 
  4577       eassert (it->end_charpos == ZV);
  4578 
  4579       if (current_buffer->long_line_optimizations_p
  4580           && long_line_optimizations_region_size > 0)
  4581         {
  4582           ptrdiff_t begv = it->large_narrowing_begv;
  4583           ptrdiff_t zv = it->large_narrowing_zv;
  4584           ptrdiff_t charpos = IT_CHARPOS (*it);
  4585           if (charpos < begv || charpos > zv)
  4586             {
  4587               begv = get_large_narrowing_begv (charpos);
  4588               zv = get_large_narrowing_zv (charpos);
  4589             }
  4590           if (begv != BEG || zv != Z)
  4591             labeled_narrow_to_region (make_fixnum (begv), make_fixnum (zv),
  4592                                       Qlong_line_optimizations_in_fontification_functions);
  4593         }
  4594 
  4595       /* Don't allow Lisp that runs from 'fontification-functions'
  4596          clear our face and image caches behind our back.  */
  4597       it->f->inhibit_clear_image_cache = true;
  4598 
  4599       if (!CONSP (val) || EQ (XCAR (val), Qlambda))
  4600         safe_call1 (val, pos);
  4601       else
  4602         {
  4603           Lisp_Object fns, fn;
  4604 
  4605           fns = Qnil;
  4606 
  4607           for (; CONSP (val); val = XCDR (val))
  4608             {
  4609               fn = XCAR (val);
  4610 
  4611               if (EQ (fn, Qt))
  4612                 {
  4613                   /* A value of t indicates this hook has a local
  4614                      binding; it means to run the global binding too.
  4615                      In a global value, t should not occur.  If it
  4616                      does, we must ignore it to avoid an endless
  4617                      loop.  */
  4618                   for (fns = Fdefault_value (Qfontification_functions);
  4619                        CONSP (fns);
  4620                        fns = XCDR (fns))
  4621                     {
  4622                       fn = XCAR (fns);
  4623                       if (!EQ (fn, Qt))
  4624                         safe_call1 (fn, pos);
  4625                     }
  4626                 }
  4627               else
  4628                 safe_call1 (fn, pos);
  4629             }
  4630         }
  4631 
  4632       it->f->inhibit_clear_image_cache = saved_inhibit_flag;
  4633       unbind_to (count, Qnil);
  4634 
  4635       /* Fontification functions routinely call `save-restriction'.
  4636          Normally, this tags clip_changed, which can confuse redisplay
  4637          (see discussion in Bug#6671).  Since we don't perform any
  4638          special handling of fontification changes in the case where
  4639          `save-restriction' isn't called, there's no point doing so in
  4640          this case either.  So, if the buffer's restrictions are
  4641          actually left unchanged, reset clip_changed.  */
  4642       if (obuf == current_buffer)
  4643         {
  4644           if (begv == BEGV && zv == ZV)
  4645             current_buffer->clip_changed = old_clip_changed;
  4646         }
  4647       /* There isn't much we can reasonably do to protect against
  4648          misbehaving fontification, but here's a fig leaf.  */
  4649       else if (BUFFER_LIVE_P (obuf))
  4650         set_buffer_internal_1 (obuf);
  4651 
  4652       /* The fontification code may have added/removed text.
  4653          It could do even a lot worse, but let's at least protect against
  4654          the most obvious case where only the text past `pos' gets changed',
  4655          as is/was done in grep.el where some escapes sequences are turned
  4656          into face properties (bug#7876).  */
  4657       it->end_charpos = ZV;
  4658 
  4659       /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
  4660          something.  This avoids an endless loop if they failed to
  4661          fontify the text for which reason ever.  */
  4662       if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
  4663         handled = HANDLED_RECOMPUTE_PROPS;
  4664     }
  4665 
  4666   return handled;
  4667 }
  4668 
  4669 
  4670 
  4671 /***********************************************************************
  4672                                 Faces
  4673  ***********************************************************************/
  4674 
  4675 static int
  4676 face_at_pos (const struct it *it, enum lface_attribute_index attr_filter)
  4677 {
  4678   ptrdiff_t next_stop;
  4679 
  4680   if (!STRINGP (it->string))
  4681     {
  4682       return face_at_buffer_position (it->w,
  4683                                       IT_CHARPOS (*it),
  4684                                       &next_stop,
  4685                                       (IT_CHARPOS (*it)
  4686                                        + TEXT_PROP_DISTANCE_LIMIT),
  4687                                       false, it->base_face_id,
  4688                                       attr_filter);
  4689     }
  4690   else
  4691     {
  4692       int base_face_id;
  4693       ptrdiff_t bufpos;
  4694       int i;
  4695       Lisp_Object from_overlay
  4696         = (it->current.overlay_string_index >= 0
  4697            ? it->string_overlays[it->current.overlay_string_index
  4698                                  % OVERLAY_STRING_CHUNK_SIZE]
  4699            : Qnil);
  4700 
  4701       /* See if we got to this string directly or indirectly from
  4702          an overlay property.  That includes the before-string or
  4703          after-string of an overlay, strings in display properties
  4704          provided by an overlay, their text properties, etc.
  4705 
  4706          FROM_OVERLAY is the overlay that brought us here, or nil if none.  */
  4707       if (! NILP (from_overlay))
  4708         for (i = it->sp - 1; i >= 0; i--)
  4709           {
  4710             if (it->stack[i].current.overlay_string_index >= 0)
  4711               from_overlay
  4712                 = it->string_overlays[it->stack[i].current.overlay_string_index
  4713                                       % OVERLAY_STRING_CHUNK_SIZE];
  4714             else if (! NILP (it->stack[i].from_overlay))
  4715               from_overlay = it->stack[i].from_overlay;
  4716 
  4717             if (!NILP (from_overlay))
  4718               break;
  4719           }
  4720 
  4721       if (! NILP (from_overlay))
  4722         {
  4723           bufpos = IT_CHARPOS (*it);
  4724           /* For a string from an overlay, the base face depends
  4725              only on text properties and ignores overlays.  */
  4726           base_face_id
  4727             = face_for_overlay_string (it->w,
  4728                                        IT_CHARPOS (*it),
  4729                                        &next_stop,
  4730                                        (IT_CHARPOS (*it)
  4731                                         + TEXT_PROP_DISTANCE_LIMIT),
  4732                                        false,
  4733                                        from_overlay, attr_filter);
  4734         }
  4735       else
  4736         {
  4737           bufpos = 0;
  4738 
  4739           /* For strings from a `display' property, use the face at
  4740              IT's current buffer position as the base face to merge
  4741              with, so that overlay strings appear in the same face as
  4742              surrounding text, unless they specify their own faces.
  4743              For strings from wrap-prefix and line-prefix properties,
  4744              use the default face, possibly remapped via
  4745              Vface_remapping_alist.  */
  4746           /* Note that the fact that we use the face at _buffer_
  4747              position means that a 'display' property on an overlay
  4748              string will not inherit the face of that overlay string,
  4749              but will instead revert to the face of buffer text
  4750              covered by the overlay.  This is visible, e.g., when the
  4751              overlay specifies a box face, but neither the buffer nor
  4752              the display string do.  This sounds like a design bug,
  4753              but Emacs always did that since v21.1, so changing that
  4754              might be a big deal.  */
  4755           base_face_id = it->string_from_prefix_prop_p
  4756             ? (!NILP (Vface_remapping_alist)
  4757                ? lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
  4758                : DEFAULT_FACE_ID)
  4759             : underlying_face_id (it);
  4760         }
  4761 
  4762       return face_at_string_position (it->w,
  4763                                       it->string,
  4764                                       IT_STRING_CHARPOS (*it),
  4765                                       bufpos,
  4766                                       &next_stop,
  4767                                       base_face_id, false,
  4768                                       attr_filter);
  4769     } /* !STRINGP (it->string) */
  4770 }
  4771 
  4772 
  4773 /* Set up iterator IT from face properties at its current position.
  4774    Called from handle_stop.  */
  4775 static enum prop_handled
  4776 handle_face_prop (struct it *it)
  4777 {
  4778   specpdl_ref count = SPECPDL_INDEX ();
  4779   /* Don't allow the user to quit out of face-merging code, in case
  4780      this is called when redisplaying a non-selected window, with
  4781      point temporarily moved to window-point.  */
  4782   specbind (Qinhibit_quit, Qt);
  4783   const int new_face_id = face_at_pos (it, 0);
  4784   unbind_to (count, Qnil);
  4785 
  4786 
  4787   /* Is this a start of a run of characters with box face?
  4788      Caveat: this can be called for a freshly initialized
  4789      iterator; face_id is -1 in this case.  We know that the new
  4790      face will not change until limit, i.e. if the new face has a
  4791      box, all characters up to limit will have one.  But, as
  4792      usual, we don't know whether limit is really the end.  */
  4793   if (new_face_id != it->face_id)
  4794     {
  4795       struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
  4796       /* If it->face_id is -1, old_face below will be NULL, see
  4797          the definition of FACE_FROM_ID_OR_NULL.  This will happen
  4798          if this is the initial call that gets the face.  */
  4799       struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  4800 
  4801       /* If the value of face_id of the iterator is -1, we have to
  4802          look in front of IT's position and see whether there is a
  4803          face there that's different from new_face_id.  */
  4804       if (!STRINGP (it->string)
  4805           && !old_face
  4806           && IT_CHARPOS (*it) > BEG)
  4807         {
  4808           const int prev_face_id = face_before_it_pos (it);
  4809 
  4810           old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  4811         }
  4812 
  4813       /* If the new face has a box, but the old face does not,
  4814          this is the start of a run of characters with box face,
  4815          i.e. this character has a shadow on the left side.  */
  4816       it->face_id = new_face_id;
  4817       /* Don't reset the start_of_box_run_p flag, only set it if
  4818          needed.  */
  4819       if (!(it->start_of_box_run_p && old_face && old_face->box))
  4820         it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
  4821                                   && (old_face == NULL || !old_face->box));
  4822       it->face_box_p = new_face->box != FACE_NO_BOX;
  4823     }
  4824 
  4825   return HANDLED_NORMALLY;
  4826 }
  4827 
  4828 
  4829 /* Return the ID of the face ``underlying'' IT's current position,
  4830    which is in a string.  If the iterator is associated with a
  4831    buffer, return the face at IT's current buffer position.
  4832    Otherwise, use the iterator's base_face_id.  */
  4833 
  4834 static int
  4835 underlying_face_id (const struct it *it)
  4836 {
  4837   int face_id = it->base_face_id, i;
  4838 
  4839   eassert (STRINGP (it->string));
  4840 
  4841   for (i = it->sp - 1; i >= 0; --i)
  4842     if (NILP (it->stack[i].string))
  4843       face_id = it->stack[i].face_id;
  4844 
  4845   return face_id;
  4846 }
  4847 
  4848 
  4849 /* Compute the face one character before or after the current position
  4850    of IT, in the visual order.  BEFORE_P means get the face
  4851    in front (to the left in L2R paragraphs, to the right in R2L
  4852    paragraphs) of IT's screen position.  Value is the ID of the face.  */
  4853 
  4854 static int
  4855 face_before_or_after_it_pos (struct it *it, bool before_p)
  4856 {
  4857   int face_id, limit;
  4858   ptrdiff_t next_check_charpos;
  4859   struct it it_copy;
  4860   void *it_copy_data = NULL;
  4861 
  4862   eassert (it->s == NULL);
  4863 
  4864   if (STRINGP (it->string))
  4865     {
  4866       ptrdiff_t bufpos, charpos;
  4867       int base_face_id;
  4868 
  4869       /* No face change past the end of the string (for the case we
  4870          are padding with spaces).  No face change before the string
  4871          start.  Ignore face changes before the first visible
  4872          character on this display line.  */
  4873       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
  4874           || (IT_STRING_CHARPOS (*it) == 0 && before_p)
  4875           || it->current_x <= it->first_visible_x)
  4876         return it->face_id;
  4877 
  4878       if (!it->bidi_p)
  4879         {
  4880           /* Set charpos to the position before or after IT's current
  4881              position, in the logical order, which in the non-bidi
  4882              case is the same as the visual order.  */
  4883           if (before_p)
  4884             charpos = IT_STRING_CHARPOS (*it) - 1;
  4885           else if (it->what == IT_COMPOSITION)
  4886             /* For composition, we must check the character after the
  4887                composition.  */
  4888             charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
  4889           else
  4890             charpos = IT_STRING_CHARPOS (*it) + 1;
  4891         }
  4892       else
  4893         {
  4894           /* With bidi iteration, the character before the current in
  4895              the visual order cannot be found by simple iteration,
  4896              because "reverse" reordering is not supported.  Instead,
  4897              we need to start from the string beginning and go all the
  4898              way to the current string position, remembering the
  4899              visually-previous position.  We need to start from the
  4900              string beginning for the character after the current as
  4901              well, since the iterator state in IT may have been
  4902              pushed, and the bidi cache is no longer coherent with the
  4903              string's text.  */
  4904           SAVE_IT (it_copy, *it, it_copy_data);
  4905           IT_STRING_CHARPOS (it_copy) = 0;
  4906           bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
  4907           it_copy.bidi_it.scan_dir = 0;
  4908 
  4909           do
  4910             {
  4911               charpos = it_copy.bidi_it.charpos;
  4912               if (charpos >= SCHARS (it->string))
  4913                 break;
  4914               bidi_move_to_visually_next (&it_copy.bidi_it);
  4915             }
  4916           while (it_copy.bidi_it.charpos != IT_STRING_CHARPOS (*it));
  4917 
  4918           if (!before_p)
  4919             {
  4920               /* Set charpos to the string position of the character
  4921                  that comes after IT's current position in the visual
  4922                  order.  */
  4923               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4924               /* If this is the first string character,
  4925                  bidi_move_to_visually_next will deliver character at
  4926                  current position without moving, so we need to enlarge N.  */
  4927               if (it_copy.bidi_it.first_elt)
  4928                 n++;
  4929               while (n--)
  4930                 bidi_move_to_visually_next (&it_copy.bidi_it);
  4931 
  4932               charpos = it_copy.bidi_it.charpos;
  4933             }
  4934 
  4935           RESTORE_IT (it, it, it_copy_data);
  4936         }
  4937       eassert (0 <= charpos && charpos <= SCHARS (it->string));
  4938 
  4939       if (it->current.overlay_string_index >= 0)
  4940         bufpos = IT_CHARPOS (*it);
  4941       else
  4942         bufpos = 0;
  4943 
  4944       base_face_id = underlying_face_id (it);
  4945 
  4946       /* Get the face for ASCII, or unibyte.  */
  4947       face_id = face_at_string_position (it->w, it->string, charpos,
  4948                                          bufpos, &next_check_charpos,
  4949                                          base_face_id, false, 0);
  4950 
  4951       /* Correct the face for charsets different from ASCII.  Do it
  4952          for the multibyte case only.  The face returned above is
  4953          suitable for unibyte text if IT->string is unibyte.  */
  4954       if (STRING_MULTIBYTE (it->string))
  4955         {
  4956           struct text_pos pos1 = string_pos (charpos, it->string);
  4957           const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
  4958           struct face *face = FACE_FROM_ID (it->f, face_id);
  4959           int len, c = check_char_and_length (p, &len);
  4960           face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
  4961         }
  4962     }
  4963   else
  4964     {
  4965       struct text_pos pos;
  4966 
  4967       if ((IT_CHARPOS (*it) >= ZV && !before_p)
  4968           || (IT_CHARPOS (*it) <= BEGV && before_p))
  4969         return it->face_id;
  4970 
  4971       limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
  4972       pos = it->current.pos;
  4973 
  4974       if (!it->bidi_p)
  4975         {
  4976           if (before_p)
  4977             DEC_TEXT_POS (pos, it->multibyte_p);
  4978           else
  4979             {
  4980               if (it->what == IT_COMPOSITION)
  4981                 {
  4982                   /* For composition, we must check the position after
  4983                      the composition.  */
  4984                   pos.charpos += it->cmp_it.nchars;
  4985                   pos.bytepos += it->len;
  4986                 }
  4987               else
  4988                 INC_TEXT_POS (pos, it->multibyte_p);
  4989             }
  4990         }
  4991       else
  4992         {
  4993           if (before_p)
  4994             {
  4995               int current_x;
  4996 
  4997               /* With bidi iteration, the character before the current
  4998                  in the visual order cannot be found by simple
  4999                  iteration, because "reverse" reordering is not
  5000                  supported.  Instead, we need to use the move_it_*
  5001                  family of functions, and move to the previous
  5002                  character starting from the beginning of the visual
  5003                  line.  */
  5004               /* Ignore face changes before the first visible
  5005                  character on this display line.  */
  5006               if (it->current_x <= it->first_visible_x)
  5007                 return it->face_id;
  5008               SAVE_IT (it_copy, *it, it_copy_data);
  5009               /* Implementation note: Since move_it_in_display_line
  5010                  works in the iterator geometry, and thinks the first
  5011                  character is always the leftmost, even in R2L lines,
  5012                  we don't need to distinguish between the R2L and L2R
  5013                  cases here.  */
  5014               current_x = it_copy.current_x;
  5015               move_it_vertically_backward (&it_copy, 0);
  5016               move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
  5017               pos = it_copy.current.pos;
  5018               RESTORE_IT (it, it, it_copy_data);
  5019             }
  5020           else
  5021             {
  5022               /* Set charpos to the buffer position of the character
  5023                  that comes after IT's current position in the visual
  5024                  order.  */
  5025               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  5026 
  5027               it_copy = *it;
  5028               /* If this is the first display element,
  5029                  bidi_move_to_visually_next will deliver character at
  5030                  current position without moving, so we need to enlarge N.  */
  5031               if (it->bidi_it.first_elt)
  5032                 n++;
  5033               while (n--)
  5034                 bidi_move_to_visually_next (&it_copy.bidi_it);
  5035 
  5036               SET_TEXT_POS (pos,
  5037                             it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
  5038             }
  5039         }
  5040       eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
  5041 
  5042       /* Determine face for CHARSET_ASCII, or unibyte.  */
  5043       face_id = face_at_buffer_position (it->w,
  5044                                          CHARPOS (pos),
  5045                                          &next_check_charpos,
  5046                                          limit, false, -1, 0);
  5047 
  5048       /* Correct the face for charsets different from ASCII.  Do it
  5049          for the multibyte case only.  The face returned above is
  5050          suitable for unibyte text if current_buffer is unibyte.  */
  5051       if (it->multibyte_p)
  5052         {
  5053           int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
  5054           struct face *face = FACE_FROM_ID (it->f, face_id);
  5055           face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
  5056         }
  5057     }
  5058 
  5059   return face_id;
  5060 }
  5061 
  5062 
  5063 
  5064 /***********************************************************************
  5065                             Invisible text
  5066  ***********************************************************************/
  5067 
  5068 /* Set up iterator IT from invisible properties at its current
  5069    position.  Called from handle_stop.  */
  5070 
  5071 static enum prop_handled
  5072 handle_invisible_prop (struct it *it)
  5073 {
  5074   enum prop_handled handled = HANDLED_NORMALLY;
  5075   int invis;
  5076   Lisp_Object prop;
  5077 
  5078   if (STRINGP (it->string))
  5079     {
  5080       Lisp_Object end_charpos, limit;
  5081 
  5082       /* Get the value of the invisible text property at the
  5083          current position.  Value will be nil if there is no such
  5084          property.  */
  5085       end_charpos = make_fixnum (IT_STRING_CHARPOS (*it));
  5086       prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5087       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5088 
  5089       if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
  5090         {
  5091           /* Record whether we have to display an ellipsis for the
  5092              invisible text.  */
  5093           bool display_ellipsis_p = (invis == 2);
  5094           ptrdiff_t len, endpos;
  5095 
  5096           handled = HANDLED_RECOMPUTE_PROPS;
  5097 
  5098           /* Get the position at which the next visible text can be
  5099              found in IT->string, if any.  */
  5100           endpos = len = SCHARS (it->string);
  5101           XSETINT (limit, len);
  5102           do
  5103             {
  5104               end_charpos
  5105                 = Fnext_single_property_change (end_charpos, Qinvisible,
  5106                                                 it->string, limit);
  5107               /* Since LIMIT is always an integer, so should be the
  5108                  value returned by Fnext_single_property_change.  */
  5109               eassert (FIXNUMP (end_charpos));
  5110               if (FIXNUMP (end_charpos))
  5111                 {
  5112                   endpos = XFIXNAT (end_charpos);
  5113                   prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5114                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5115                   if (invis == 2)
  5116                     display_ellipsis_p = true;
  5117                 }
  5118               else /* Should never happen; but if it does, exit the loop.  */
  5119                 endpos = len;
  5120             }
  5121           while (invis != 0 && endpos < len);
  5122 
  5123           if (display_ellipsis_p)
  5124             it->ellipsis_p = true;
  5125 
  5126           if (endpos < len)
  5127             {
  5128               /* Text at END_CHARPOS is visible.  Move IT there.  */
  5129               struct text_pos old;
  5130               ptrdiff_t oldpos;
  5131 
  5132               old = it->current.string_pos;
  5133               oldpos = CHARPOS (old);
  5134               if (it->bidi_p)
  5135                 {
  5136                   if (it->bidi_it.first_elt
  5137                       && it->bidi_it.charpos < SCHARS (it->string))
  5138                     bidi_paragraph_init (it->paragraph_embedding,
  5139                                          &it->bidi_it, true);
  5140                   /* Bidi-iterate out of the invisible text.  */
  5141                   do
  5142                     {
  5143                       bidi_move_to_visually_next (&it->bidi_it);
  5144                     }
  5145                   while (oldpos <= it->bidi_it.charpos
  5146                          && it->bidi_it.charpos < endpos
  5147                          && it->bidi_it.charpos < it->bidi_it.string.schars);
  5148 
  5149                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  5150                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  5151                   if (IT_CHARPOS (*it) >= endpos)
  5152                     it->prev_stop = endpos;
  5153                 }
  5154               else
  5155                 {
  5156                   IT_STRING_CHARPOS (*it) = endpos;
  5157                   compute_string_pos (&it->current.string_pos, old, it->string);
  5158                 }
  5159             }
  5160           else
  5161             {
  5162               /* The rest of the string is invisible.  If this is an
  5163                  overlay string, proceed with the next overlay string
  5164                  or whatever comes and return a character from there.  */
  5165               if (it->current.overlay_string_index >= 0
  5166                   && !display_ellipsis_p)
  5167                 {
  5168                   next_overlay_string (it);
  5169                   /* Don't check for overlay strings when we just
  5170                      finished processing them.  */
  5171                   handled = HANDLED_OVERLAY_STRING_CONSUMED;
  5172                 }
  5173               else
  5174                 {
  5175                   IT_STRING_CHARPOS (*it) = SCHARS (it->string);
  5176                   IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
  5177                 }
  5178             }
  5179         }
  5180     }
  5181   else
  5182     {
  5183       ptrdiff_t newpos, next_stop, start_charpos, tem;
  5184       Lisp_Object pos, overlay;
  5185 
  5186       /* First of all, is there invisible text at this position?  */
  5187       tem = start_charpos = IT_CHARPOS (*it);
  5188       pos = make_fixnum (tem);
  5189       prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
  5190                                             &overlay);
  5191       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5192 
  5193       /* If we are on invisible text, skip over it.  */
  5194       if (invis != 0 && start_charpos < it->end_charpos)
  5195         {
  5196           /* Record whether we have to display an ellipsis for the
  5197              invisible text.  */
  5198           bool display_ellipsis_p = invis == 2;
  5199 
  5200           handled = HANDLED_RECOMPUTE_PROPS;
  5201 
  5202           /* Loop skipping over invisible text.  The loop is left at
  5203              ZV or with IT on the first char being visible again.  */
  5204           do
  5205             {
  5206               /* Try to skip some invisible text.  Return value is the
  5207                  position reached which can be equal to where we start
  5208                  if there is nothing invisible there.  This skips both
  5209                  over invisible text properties and overlays with
  5210                  invisible property.  */
  5211               newpos = skip_invisible (tem, &next_stop, ZV, it->window);
  5212 
  5213               /* If we skipped nothing at all we weren't at invisible
  5214                  text in the first place.  If everything to the end of
  5215                  the buffer was skipped, end the loop.  */
  5216               if (newpos == tem || newpos >= ZV)
  5217                 invis = 0;
  5218               else
  5219                 {
  5220                   /* We skipped some characters but not necessarily
  5221                      all there are.  Check if we ended up on visible
  5222                      text.  Fget_char_property returns the property of
  5223                      the char before the given position, i.e. if we
  5224                      get invis = 0, this means that the char at
  5225                      newpos is visible.  */
  5226                   pos = make_fixnum (newpos);
  5227                   prop = Fget_char_property (pos, Qinvisible, it->window);
  5228                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5229                 }
  5230 
  5231               /* If we ended up on invisible text, proceed to
  5232                  skip starting with next_stop.  */
  5233               if (invis != 0)
  5234                 tem = next_stop;
  5235 
  5236               /* If there are adjacent invisible texts, don't lose the
  5237                  second one's ellipsis.  */
  5238               if (invis == 2)
  5239                 display_ellipsis_p = true;
  5240             }
  5241           while (invis != 0);
  5242 
  5243           /* The position newpos is now either ZV or on visible text.  */
  5244           if (it->bidi_p)
  5245             {
  5246               ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
  5247               bool on_newline
  5248                 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
  5249               bool after_newline
  5250                 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
  5251 
  5252               /* If the invisible text ends on a newline or on a
  5253                  character after a newline, we can avoid the costly,
  5254                  character by character, bidi iteration to NEWPOS, and
  5255                  instead simply reseat the iterator there.  That's
  5256                  because all bidi reordering information is tossed at
  5257                  the newline.  This is a big win for modes that hide
  5258                  complete lines, like Outline, Org, etc.  */
  5259               if (on_newline || after_newline)
  5260                 {
  5261                   struct text_pos tpos;
  5262                   bidi_dir_t pdir = it->bidi_it.paragraph_dir;
  5263 
  5264                   SET_TEXT_POS (tpos, newpos, bpos);
  5265                   reseat_1 (it, tpos, false);
  5266                   /* If we reseat on a newline/ZV, we need to prep the
  5267                      bidi iterator for advancing to the next character
  5268                      after the newline/EOB, keeping the current paragraph
  5269                      direction (so that PRODUCE_GLYPHS does TRT wrt
  5270                      prepending/appending glyphs to a glyph row).  */
  5271                   if (on_newline)
  5272                     {
  5273                       it->bidi_it.first_elt = false;
  5274                       it->bidi_it.paragraph_dir = pdir;
  5275                       it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
  5276                       it->bidi_it.nchars = 1;
  5277                       it->bidi_it.ch_len = 1;
  5278                     }
  5279                 }
  5280               else      /* Must use the slow method.  */
  5281                 {
  5282                   /* With bidi iteration, the region of invisible text
  5283                      could start and/or end in the middle of a
  5284                      non-base embedding level.  Therefore, we need to
  5285                      skip invisible text using the bidi iterator,
  5286                      starting at IT's current position, until we find
  5287                      ourselves outside of the invisible text.
  5288                      Skipping invisible text _after_ bidi iteration
  5289                      avoids affecting the visual order of the
  5290                      displayed text when invisible properties are
  5291                      added or removed.  */
  5292                   if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
  5293                     {
  5294                       /* If we were `reseat'ed to a new paragraph,
  5295                          determine the paragraph base direction.  We
  5296                          need to do it now because
  5297                          next_element_from_buffer may not have a
  5298                          chance to do it, if we are going to skip any
  5299                          text at the beginning, which resets the
  5300                          FIRST_ELT flag.  */
  5301                       bidi_paragraph_init (it->paragraph_embedding,
  5302                                            &it->bidi_it, true);
  5303                     }
  5304                   do
  5305                     {
  5306                       bidi_move_to_visually_next (&it->bidi_it);
  5307                     }
  5308                   while (it->stop_charpos <= it->bidi_it.charpos
  5309                          && it->bidi_it.charpos < newpos);
  5310                   IT_CHARPOS (*it) = it->bidi_it.charpos;
  5311                   IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  5312                   /* If we overstepped NEWPOS, record its position in
  5313                      the iterator, so that we skip invisible text if
  5314                      later the bidi iteration lands us in the
  5315                      invisible region again. */
  5316                   if (IT_CHARPOS (*it) >= newpos)
  5317                     it->prev_stop = newpos;
  5318                 }
  5319             }
  5320           else
  5321             {
  5322               IT_CHARPOS (*it) = newpos;
  5323               IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
  5324             }
  5325 
  5326           if (display_ellipsis_p)
  5327             {
  5328               /* Make sure that the glyphs of the ellipsis will get
  5329                  correct `charpos' values.  If we would not update
  5330                  it->position here, the glyphs would belong to the
  5331                  last visible character _before_ the invisible
  5332                  text, which confuses `set_cursor_from_row'.
  5333 
  5334                  We use the last invisible position instead of the
  5335                  first because this way the cursor is always drawn on
  5336                  the first "." of the ellipsis, whenever PT is inside
  5337                  the invisible text.  Otherwise the cursor would be
  5338                  placed _after_ the ellipsis when the point is after the
  5339                  first invisible character.  */
  5340               if (!STRINGP (it->object))
  5341                 {
  5342                   it->position.charpos = newpos - 1;
  5343                   it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
  5344                 }
  5345             }
  5346 
  5347           /* If there are before-strings at the start of invisible
  5348              text, and the text is invisible because of a text
  5349              property, arrange to show before-strings because 20.x did
  5350              it that way.  (If the text is invisible because of an
  5351              overlay property instead of a text property, this is
  5352              already handled in the overlay code.)  */
  5353           if (NILP (overlay)
  5354               && get_overlay_strings (it, it->stop_charpos))
  5355             {
  5356               handled = HANDLED_RECOMPUTE_PROPS;
  5357               if (it->sp > 0)
  5358                 {
  5359                   it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
  5360                   /* The call to get_overlay_strings above recomputes
  5361                      it->stop_charpos, but it only considers changes
  5362                      in properties and overlays beyond iterator's
  5363                      current position.  This causes us to miss changes
  5364                      that happen exactly where the invisible property
  5365                      ended.  So we play it safe here and force the
  5366                      iterator to check for potential stop positions
  5367                      immediately after the invisible text.  Note that
  5368                      if get_overlay_strings returns true, it
  5369                      normally also pushed the iterator stack, so we
  5370                      need to update the stop position in the slot
  5371                      below the current one.  */
  5372                   it->stack[it->sp - 1].stop_charpos
  5373                     = CHARPOS (it->stack[it->sp - 1].current.pos);
  5374                 }
  5375             }
  5376           else if (display_ellipsis_p)
  5377             {
  5378               it->ellipsis_p = true;
  5379               /* Let the ellipsis display before
  5380                  considering any properties of the following char.
  5381                  Fixes jasonr@gnu.org 01 Oct 07 bug.  */
  5382               handled = HANDLED_RETURN;
  5383             }
  5384         }
  5385     }
  5386 
  5387   return handled;
  5388 }
  5389 
  5390 
  5391 /* Make iterator IT return `...' next.
  5392    Replaces LEN characters from buffer.  */
  5393 
  5394 static void
  5395 setup_for_ellipsis (struct it *it, int len)
  5396 {
  5397   /* Use the display table definition for `...'.  Invalid glyphs
  5398      will be handled by the method returning elements from dpvec.  */
  5399   if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
  5400     {
  5401       struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
  5402       it->dpvec = v->contents;
  5403       it->dpend = v->contents + v->header.size;
  5404     }
  5405   else
  5406     {
  5407       /* Default `...'.  */
  5408       it->dpvec = default_invis_vector;
  5409       it->dpend = default_invis_vector + 3;
  5410     }
  5411 
  5412   it->dpvec_char_len = len;
  5413   it->current.dpvec_index = 0;
  5414   it->dpvec_face_id = -1;
  5415 
  5416   /* Use IT->saved_face_id for the ellipsis, so that it has the same
  5417      face as the preceding text.  IT->saved_face_id was set in
  5418      handle_stop to the face of the preceding character, and will be
  5419      different from IT->face_id only if the invisible text skipped in
  5420      handle_invisible_prop has some non-default face on its first
  5421      character.  We thus ignore the face of the invisible text when we
  5422      display the ellipsis.  IT's face is restored in set_iterator_to_next.  */
  5423   if (it->saved_face_id >= 0)
  5424     it->face_id = it->saved_face_id;
  5425 
  5426   /* If the ellipsis represents buffer text, it means we advanced in
  5427      the buffer, so we should no longer ignore overlay strings.  */
  5428   if (it->method == GET_FROM_BUFFER)
  5429     it->ignore_overlay_strings_at_pos_p = false;
  5430 
  5431   it->method = GET_FROM_DISPLAY_VECTOR;
  5432   it->ellipsis_p = true;
  5433 }
  5434 
  5435 
  5436 static Lisp_Object
  5437 find_display_property (Lisp_Object disp, Lisp_Object prop)
  5438 {
  5439   if (NILP (disp))
  5440     return Qnil;
  5441   /* We have a vector of display specs.  */
  5442   if (VECTORP (disp))
  5443     {
  5444       for (ptrdiff_t i = 0; i < ASIZE (disp); i++)
  5445         {
  5446           Lisp_Object elem = AREF (disp, i);
  5447           if (CONSP (elem)
  5448               && CONSP (XCDR (elem))
  5449               && EQ (XCAR (elem), prop))
  5450             return XCAR (XCDR (elem));
  5451         }
  5452       return Qnil;
  5453     }
  5454   /* We have a list of display specs.  */
  5455   else if (CONSP (disp)
  5456            && CONSP (XCAR (disp)))
  5457     {
  5458       while (!NILP (disp))
  5459         {
  5460           Lisp_Object elem = XCAR (disp);
  5461           if (CONSP (elem)
  5462               && CONSP (XCDR (elem))
  5463               && EQ (XCAR (elem), prop))
  5464             return XCAR (XCDR (elem));
  5465 
  5466           /* Check that we have a proper list before going to the next
  5467              element.  */
  5468           if (CONSP (XCDR (disp)))
  5469             disp = XCDR (disp);
  5470           else
  5471             disp = Qnil;
  5472         }
  5473       return Qnil;
  5474     }
  5475   /* A simple display spec.  */
  5476   else if (CONSP (disp)
  5477            && CONSP (XCDR (disp))
  5478            && EQ (XCAR (disp), prop))
  5479     return XCAR (XCDR (disp));
  5480   else
  5481     return Qnil;
  5482 }
  5483 
  5484 static Lisp_Object
  5485 get_display_property (ptrdiff_t bufpos, Lisp_Object prop, Lisp_Object object)
  5486 {
  5487   return find_display_property (Fget_text_property (make_fixnum (bufpos),
  5488                                                     Qdisplay, object),
  5489                                 prop);
  5490 }
  5491 
  5492 static void
  5493 display_min_width (struct it *it, ptrdiff_t bufpos,
  5494                    Lisp_Object object, Lisp_Object width_spec)
  5495 {
  5496   /* We're being called at the end of the `min-width' sequence,
  5497      probably. */
  5498   if (!NILP (it->min_width_property)
  5499       && !EQ (width_spec, it->min_width_property))
  5500     {
  5501       if (!it->glyph_row)
  5502         return;
  5503 
  5504       /* When called from display_string (i.e., the mode line),
  5505          we're being called with a string as the object, and we
  5506          may be called with many sub-strings belonging to the same
  5507          :propertize run. */
  5508       if ((bufpos == 0
  5509            && !EQ (it->min_width_property,
  5510                    get_display_property (0, Qmin_width, object)))
  5511           /* In a buffer -- check that we're really right after the
  5512              sequence of characters covered by this `min-width'.  */
  5513           || (bufpos > BEGV
  5514               && EQ (it->min_width_property,
  5515                      get_display_property (bufpos - 1, Qmin_width, object))))
  5516         {
  5517           Lisp_Object w = Qnil;
  5518           double width;
  5519 #ifdef HAVE_WINDOW_SYSTEM
  5520           if (FRAME_WINDOW_P (it->f))
  5521             {
  5522               struct font *font = NULL;
  5523               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5524               font = face->font ? face->font : FRAME_FONT (it->f);
  5525               calc_pixel_width_or_height (&width, it,
  5526                                           XCAR (it->min_width_property),
  5527                                           font, true, NULL);
  5528               width -= it->current_x - it->min_width_start;
  5529               w = list1 (make_int (width));
  5530             }
  5531           else
  5532 #endif
  5533             {
  5534               calc_pixel_width_or_height (&width, it,
  5535                                           XCAR (it->min_width_property),
  5536                                           NULL, true, NULL);
  5537               width -= (it->current_x - it->min_width_start) /
  5538                 FRAME_COLUMN_WIDTH (it->f);
  5539               w = make_int (width);
  5540             }
  5541 
  5542           /* Insert the stretch glyph.  */
  5543           it->object = list3 (Qspace, QCwidth, w);
  5544           produce_stretch_glyph (it);
  5545           if (it->area == TEXT_AREA)
  5546             it->current_x += it->pixel_width;
  5547           it->min_width_property = Qnil;
  5548         }
  5549     }
  5550 
  5551   /* We're at the start of a `min-width' sequence -- record the
  5552      position and the property, so that we can later see if we're at
  5553      the end.  */
  5554   if (CONSP (width_spec))
  5555     {
  5556       if (bufpos == BEGV
  5557           /* Mode line (see above).  */
  5558           || (bufpos == 0
  5559               && !EQ (it->min_width_property,
  5560                       get_display_property (0, Qmin_width, object)))
  5561           /* Buffer.  */
  5562           || (bufpos > BEGV
  5563               && !EQ (width_spec,
  5564                       get_display_property (bufpos - 1, Qmin_width, object))))
  5565         {
  5566           it->min_width_property = width_spec;
  5567           it->min_width_start = it->current_x;
  5568         }
  5569     }
  5570 }
  5571 
  5572 DEFUN ("get-display-property", Fget_display_property,
  5573        Sget_display_property, 2, 4, 0,
  5574        doc: /* Get the value of the `display' property PROP at POSITION.
  5575 If OBJECT, this should be a buffer or string where the property is
  5576 fetched from.  If omitted, OBJECT defaults to the current buffer.
  5577 
  5578 If PROPERTIES, look for value of PROP in PROPERTIES instead of the
  5579 properties at POSITION.  */)
  5580   (Lisp_Object position, Lisp_Object prop, Lisp_Object object,
  5581    Lisp_Object properties)
  5582 {
  5583   if (NILP (properties))
  5584     properties = Fget_text_property (position, Qdisplay, object);
  5585   else
  5586     CHECK_LIST (properties);
  5587 
  5588   return find_display_property (properties, prop);
  5589 }
  5590 
  5591 
  5592 
  5593 /***********************************************************************
  5594                             'display' property
  5595  ***********************************************************************/
  5596 
  5597 /* Set up iterator IT from `display' property at its current position.
  5598    Called from handle_stop.
  5599    We return HANDLED_RETURN if some part of the display property
  5600    overrides the display of the buffer text itself.
  5601    Otherwise we return HANDLED_NORMALLY.  */
  5602 
  5603 static enum prop_handled
  5604 handle_display_prop (struct it *it)
  5605 {
  5606   Lisp_Object propval, object, overlay;
  5607   struct text_pos *position;
  5608   ptrdiff_t bufpos;
  5609   /* Nonzero if some property replaces the display of the text itself.  */
  5610   int display_replaced = 0;
  5611 
  5612   if (STRINGP (it->string))
  5613     {
  5614       object = it->string;
  5615       position = &it->current.string_pos;
  5616       bufpos = CHARPOS (it->current.pos);
  5617     }
  5618   else
  5619     {
  5620       XSETWINDOW (object, it->w);
  5621       position = &it->current.pos;
  5622       bufpos = CHARPOS (*position);
  5623     }
  5624 
  5625   /* Reset those iterator values set from display property values.  */
  5626   it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
  5627   it->space_width = Qnil;
  5628   it->font_height = Qnil;
  5629   it->voffset = 0;
  5630 
  5631   /* We don't support recursive `display' properties, i.e. string
  5632      values that have a string `display' property, that have a string
  5633      `display' property etc.  */
  5634   if (!it->string_from_display_prop_p)
  5635     it->area = TEXT_AREA;
  5636 
  5637   propval = get_char_property_and_overlay (make_fixnum (position->charpos),
  5638                                            Qdisplay, object, &overlay);
  5639 
  5640   /* Rest of the code must have OBJECT be either a string or a buffer.  */
  5641   if (!STRINGP (it->string))
  5642     object = it->w->contents;
  5643 
  5644   /* Handle min-width ends. */
  5645   if (!NILP (it->min_width_property)
  5646       && NILP (find_display_property (propval, Qmin_width)))
  5647     display_min_width (it, bufpos, object, Qnil);
  5648 
  5649   if (NILP (propval))
  5650     return HANDLED_NORMALLY;
  5651   /* Now OVERLAY is the overlay that gave us this property, or nil
  5652      if it was a text property.  */
  5653 
  5654   display_replaced = handle_display_spec (it, propval, object, overlay,
  5655                                           position, bufpos,
  5656                                           FRAME_WINDOW_P (it->f));
  5657   return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
  5658 }
  5659 
  5660 /* Subroutine of handle_display_prop.  Returns non-zero if the display
  5661    specification in SPEC is a replacing specification, i.e. it would
  5662    replace the text covered by `display' property with something else,
  5663    such as an image or a display string.  If SPEC includes any kind or
  5664    `(space ...) specification, the value is 2; this is used by
  5665    compute_display_string_pos, which see.
  5666 
  5667    See handle_single_display_spec for documentation of arguments.
  5668    FRAME_WINDOW_P is true if the window being redisplayed is on a
  5669    GUI frame; this argument is used only if IT is NULL, see below.
  5670 
  5671    IT can be NULL, if this is called by the bidi reordering code
  5672    through compute_display_string_pos, which see.  In that case, this
  5673    function only examines SPEC, but does not otherwise "handle" it, in
  5674    the sense that it doesn't set up members of IT from the display
  5675    spec.  */
  5676 static int
  5677 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5678                      Lisp_Object overlay, struct text_pos *position,
  5679                      ptrdiff_t bufpos, bool frame_window_p)
  5680 {
  5681   int replacing = 0;
  5682   bool enable_eval = true;
  5683 
  5684   /* Support (disable-eval PROP) which is used by enriched.el.  */
  5685   if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
  5686     {
  5687       enable_eval = false;
  5688       spec = CONSP (XCDR (spec)) ? XCAR (XCDR (spec)) : Qnil;
  5689     }
  5690 
  5691   if (CONSP (spec)
  5692       /* Simple specifications.  */
  5693       && !EQ (XCAR (spec), Qimage)
  5694 #ifdef HAVE_XWIDGETS
  5695       && !EQ (XCAR (spec), Qxwidget)
  5696 #endif
  5697       && !EQ (XCAR (spec), Qspace)
  5698       && !EQ (XCAR (spec), Qwhen)
  5699       && !EQ (XCAR (spec), Qslice)
  5700       && !EQ (XCAR (spec), Qspace_width)
  5701       && !EQ (XCAR (spec), Qheight)
  5702       && !EQ (XCAR (spec), Qraise)
  5703       /* Marginal area specifications.  */
  5704       && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
  5705       && !EQ (XCAR (spec), Qleft_fringe)
  5706       && !EQ (XCAR (spec), Qright_fringe)
  5707       && !EQ (XCAR (spec), Qmin_width)
  5708       && !NILP (XCAR (spec)))
  5709     {
  5710       for (; CONSP (spec); spec = XCDR (spec))
  5711         {
  5712           int rv = handle_single_display_spec (it, XCAR (spec), object,
  5713                                                overlay, position, bufpos,
  5714                                                replacing, frame_window_p,
  5715                                                enable_eval);
  5716           if (rv != 0)
  5717             {
  5718               replacing = rv;
  5719               /* If some text in a string is replaced, `position' no
  5720                  longer points to the position of `object'.  */
  5721               if (!it || STRINGP (object))
  5722                 break;
  5723             }
  5724         }
  5725     }
  5726   else if (VECTORP (spec))
  5727     {
  5728       ptrdiff_t i;
  5729       for (i = 0; i < ASIZE (spec); ++i)
  5730         {
  5731           int rv = handle_single_display_spec (it, AREF (spec, i), object,
  5732                                                overlay, position, bufpos,
  5733                                                replacing, frame_window_p,
  5734                                                enable_eval);
  5735           if (rv != 0)
  5736             {
  5737               replacing = rv;
  5738               /* If some text in a string is replaced, `position' no
  5739                  longer points to the position of `object'.  */
  5740               if (!it || STRINGP (object))
  5741                 break;
  5742             }
  5743         }
  5744     }
  5745   else
  5746     replacing = handle_single_display_spec (it, spec, object, overlay, position,
  5747                                             bufpos, 0, frame_window_p,
  5748                                             enable_eval);
  5749   return replacing;
  5750 }
  5751 
  5752 /* Value is the position of the end of the `display' property starting
  5753    at START_POS in OBJECT.  */
  5754 
  5755 static struct text_pos
  5756 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
  5757 {
  5758   Lisp_Object end;
  5759   struct text_pos end_pos;
  5760 
  5761   end = Fnext_single_char_property_change (make_fixnum (CHARPOS (start_pos)),
  5762                                            Qdisplay, object, Qnil);
  5763   CHARPOS (end_pos) = XFIXNAT (end);
  5764   if (STRINGP (object))
  5765     compute_string_pos (&end_pos, start_pos, it->string);
  5766   else
  5767     BYTEPOS (end_pos) = CHAR_TO_BYTE (XFIXNAT (end));
  5768 
  5769   return end_pos;
  5770 }
  5771 
  5772 
  5773 /* Set up IT from a single `display' property specification SPEC.  OBJECT
  5774    is the object in which the `display' property was found.  *POSITION
  5775    is the position in OBJECT at which the `display' property was found.
  5776    BUFPOS is the buffer position of OBJECT (different from POSITION if
  5777    OBJECT is not a buffer).  DISPLAY_REPLACED non-zero means that we
  5778    previously saw a display specification which already replaced text
  5779    display with something else, for example an image; we ignore such
  5780    properties after the first one has been processed.
  5781 
  5782    OVERLAY is the overlay this `display' property came from,
  5783    or nil if it was a text property.
  5784 
  5785    If SPEC is a `space' or `image' specification, and in some other
  5786    cases too, set *POSITION to the position where the `display'
  5787    property ends.
  5788 
  5789    If IT is NULL, only examine the property specification in SPEC, but
  5790    don't set up IT.  In that case, FRAME_WINDOW_P means SPEC
  5791    is intended to be displayed in a window on a GUI frame.
  5792 
  5793    Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
  5794 
  5795    Value is non-zero if something was found which replaces the display
  5796    of buffer or string text.  */
  5797 
  5798 static int
  5799 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5800                             Lisp_Object overlay, struct text_pos *position,
  5801                             ptrdiff_t bufpos, int display_replaced,
  5802                             bool frame_window_p, bool enable_eval_p)
  5803 {
  5804   Lisp_Object form;
  5805   Lisp_Object location, value;
  5806   struct text_pos start_pos = *position;
  5807   void *itdata = NULL;
  5808 
  5809   /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
  5810      If the result is non-nil, use VALUE instead of SPEC.  */
  5811   form = Qt;
  5812   if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
  5813     {
  5814       spec = XCDR (spec);
  5815       if (!CONSP (spec))
  5816         return 0;
  5817       form = XCAR (spec);
  5818       spec = XCDR (spec);
  5819     }
  5820 
  5821   if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
  5822     form = Qnil;
  5823   if (!NILP (form) && !EQ (form, Qt))
  5824     {
  5825       specpdl_ref count = SPECPDL_INDEX ();
  5826 
  5827       /* Bind `object' to the object having the `display' property, a
  5828          buffer or string.  Bind `position' to the position in the
  5829          object where the property was found, and `buffer-position'
  5830          to the current position in the buffer.  */
  5831 
  5832       if (NILP (object))
  5833         XSETBUFFER (object, current_buffer);
  5834       specbind (Qobject, object);
  5835       specbind (Qposition, make_fixnum (CHARPOS (*position)));
  5836       specbind (Qbuffer_position, make_fixnum (bufpos));
  5837       /* Save and restore the bidi cache, since FORM could be crazy
  5838          enough to re-enter redisplay, e.g., by calling 'message'.  */
  5839       itdata = bidi_shelve_cache ();
  5840       form = safe_eval (form);
  5841       bidi_unshelve_cache (itdata, false);
  5842       form = unbind_to (count, form);
  5843     }
  5844 
  5845   if (NILP (form))
  5846     return 0;
  5847 
  5848   /* Handle `(height HEIGHT)' specifications.  */
  5849   if (CONSP (spec)
  5850       && EQ (XCAR (spec), Qheight)
  5851       && CONSP (XCDR (spec)))
  5852     {
  5853       if (it)
  5854         {
  5855           if (!FRAME_WINDOW_P (it->f))
  5856             return 0;
  5857 
  5858           it->font_height = XCAR (XCDR (spec));
  5859           if (!NILP (it->font_height))
  5860             {
  5861               int new_height = -1;
  5862 
  5863               if (CONSP (it->font_height)
  5864                   && (EQ (XCAR (it->font_height), Qplus)
  5865                       || EQ (XCAR (it->font_height), Qminus))
  5866                   && CONSP (XCDR (it->font_height))
  5867                   && RANGED_FIXNUMP (0, XCAR (XCDR (it->font_height)), INT_MAX))
  5868                 {
  5869                   /* `(+ N)' or `(- N)' where N is an integer.  */
  5870                   int steps = XFIXNUM (XCAR (XCDR (it->font_height)));
  5871                   if (EQ (XCAR (it->font_height), Qplus))
  5872                     steps = - steps;
  5873                   it->face_id = smaller_face (it->f, it->face_id, steps);
  5874                 }
  5875               else if (FUNCTIONP (it->font_height) && enable_eval_p)
  5876                 {
  5877                   /* Call function with current height as argument.
  5878                      Value is the new height.  */
  5879                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5880                   Lisp_Object height;
  5881                   itdata = bidi_shelve_cache ();
  5882                   height = safe_call1 (it->font_height,
  5883                                        face->lface[LFACE_HEIGHT_INDEX]);
  5884                   bidi_unshelve_cache (itdata, false);
  5885                   if (NUMBERP (height))
  5886                     new_height = XFLOATINT (height);
  5887                 }
  5888               else if (NUMBERP (it->font_height))
  5889                 {
  5890                   /* Value is a multiple of the canonical char height.  */
  5891                   struct face *f;
  5892 
  5893                   f = FACE_FROM_ID (it->f,
  5894                                     lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID));
  5895                   new_height = (XFLOATINT (it->font_height)
  5896                                 * XFIXNUM (f->lface[LFACE_HEIGHT_INDEX]));
  5897                 }
  5898               else if (enable_eval_p)
  5899                 {
  5900                   /* Evaluate IT->font_height with `height' bound to the
  5901                      current specified height to get the new height.  */
  5902                   specpdl_ref count = SPECPDL_INDEX ();
  5903                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5904 
  5905                   specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
  5906                   itdata = bidi_shelve_cache ();
  5907                   value = safe_eval (it->font_height);
  5908                   bidi_unshelve_cache (itdata, false);
  5909                   value = unbind_to (count, value);
  5910 
  5911                   if (NUMBERP (value))
  5912                     new_height = XFLOATINT (value);
  5913                 }
  5914 
  5915               if (new_height > 0)
  5916                 it->face_id = face_with_height (it->f, it->face_id, new_height);
  5917             }
  5918         }
  5919 
  5920       return 0;
  5921     }
  5922 
  5923   /* Handle `(space-width WIDTH)'.  */
  5924   if (CONSP (spec)
  5925       && EQ (XCAR (spec), Qspace_width)
  5926       && CONSP (XCDR (spec)))
  5927     {
  5928       if (it)
  5929         {
  5930           if (!FRAME_WINDOW_P (it->f))
  5931             return 0;
  5932 
  5933           value = XCAR (XCDR (spec));
  5934           if (NUMBERP (value) && XFLOATINT (value) > 0)
  5935             it->space_width = value;
  5936         }
  5937 
  5938       return 0;
  5939     }
  5940 
  5941   /* Handle `(min-width (WIDTH))'.  */
  5942   if (CONSP (spec)
  5943       && EQ (XCAR (spec), Qmin_width)
  5944       && CONSP (XCDR (spec))
  5945       && CONSP (XCAR (XCDR (spec))))
  5946     {
  5947       if (it)
  5948         display_min_width (it, bufpos, object, XCAR (XCDR (spec)));
  5949       return 0;
  5950     }
  5951 
  5952   /* Handle `(slice X Y WIDTH HEIGHT)'.  */
  5953   if (CONSP (spec)
  5954       && EQ (XCAR (spec), Qslice))
  5955     {
  5956       Lisp_Object tem;
  5957 
  5958       if (it)
  5959         {
  5960           if (!FRAME_WINDOW_P (it->f))
  5961             return 0;
  5962 
  5963           if (tem = XCDR (spec), CONSP (tem))
  5964             {
  5965               it->slice.x = XCAR (tem);
  5966               if (tem = XCDR (tem), CONSP (tem))
  5967                 {
  5968                   it->slice.y = XCAR (tem);
  5969                   if (tem = XCDR (tem), CONSP (tem))
  5970                     {
  5971                       it->slice.width = XCAR (tem);
  5972                       if (tem = XCDR (tem), CONSP (tem))
  5973                         it->slice.height = XCAR (tem);
  5974                     }
  5975                 }
  5976             }
  5977         }
  5978 
  5979       return 0;
  5980     }
  5981 
  5982   /* Handle `(raise FACTOR)'.  */
  5983   if (CONSP (spec)
  5984       && EQ (XCAR (spec), Qraise)
  5985       && CONSP (XCDR (spec)))
  5986     {
  5987       if (it)
  5988         {
  5989           if (!FRAME_WINDOW_P (it->f))
  5990             return 0;
  5991 
  5992 #ifdef HAVE_WINDOW_SYSTEM
  5993           value = XCAR (XCDR (spec));
  5994           if (NUMBERP (value))
  5995             {
  5996               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5997               it->voffset = - (XFLOATINT (value)
  5998                                * (normal_char_height (face->font, -1)));
  5999             }
  6000 #endif /* HAVE_WINDOW_SYSTEM */
  6001         }
  6002 
  6003       return 0;
  6004     }
  6005 
  6006   /* Don't handle the other kinds of display specifications
  6007      inside a string that we got from a `display' property.  */
  6008   if (it && it->string_from_display_prop_p)
  6009     return 0;
  6010 
  6011   /* Characters having this form of property are not displayed, so
  6012      we have to find the end of the property.  */
  6013   if (it)
  6014     {
  6015       start_pos = *position;
  6016       *position = display_prop_end (it, object, start_pos);
  6017       /* If the display property comes from an overlay, don't consider
  6018          any potential stop_charpos values before the end of that
  6019          overlay.  Since display_prop_end will happily find another
  6020          'display' property coming from some other overlay or text
  6021          property on buffer positions before this overlay's end, we
  6022          need to ignore them, or else we risk displaying this
  6023          overlay's display string/image twice.  */
  6024       if (!NILP (overlay))
  6025         {
  6026           ptrdiff_t ovendpos = OVERLAY_END (overlay);
  6027 
  6028           /* Some borderline-sane Lisp might call us with the current
  6029              buffer narrowed so that overlay-end is outside the
  6030              POINT_MIN..POINT_MAX region, which will then cause
  6031              various assertion violations and crashes down the road,
  6032              starting with pop_it when it will attempt to use POSITION
  6033              set below.  Prevent that.  */
  6034           ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
  6035 
  6036           if (ovendpos > CHARPOS (*position))
  6037             SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
  6038         }
  6039     }
  6040   value = Qnil;
  6041 
  6042   /* Stop the scan at that end position--we assume that all
  6043      text properties change there.  */
  6044   if (it)
  6045     it->stop_charpos = position->charpos;
  6046 
  6047   /* Handle `(left-fringe BITMAP [FACE])'
  6048      and `(right-fringe BITMAP [FACE])'.  */
  6049   if (CONSP (spec)
  6050       && (EQ (XCAR (spec), Qleft_fringe)
  6051           || EQ (XCAR (spec), Qright_fringe))
  6052       && CONSP (XCDR (spec)))
  6053     {
  6054       if (it)
  6055         {
  6056           if (!FRAME_WINDOW_P (it->f))
  6057             /* If we return here, POSITION has been advanced
  6058                across the text with this property.  */
  6059             {
  6060               /* Synchronize the bidi iterator with POSITION.  This is
  6061                  needed because we are not going to push the iterator
  6062                  on behalf of this display property, so there will be
  6063                  no pop_it call to do this synchronization for us.  */
  6064               if (it->bidi_p)
  6065                 {
  6066                   it->position = *position;
  6067                   iterate_out_of_display_property (it);
  6068                   *position = it->position;
  6069                 }
  6070               return 1;
  6071             }
  6072         }
  6073       else if (!frame_window_p)
  6074         return 1;
  6075 
  6076 #ifdef HAVE_WINDOW_SYSTEM
  6077       value = XCAR (XCDR (spec));
  6078       int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
  6079       if (! fringe_bitmap)
  6080         /* If we return here, POSITION has been advanced
  6081            across the text with this property.  */
  6082         {
  6083           if (it && it->bidi_p)
  6084             {
  6085               it->position = *position;
  6086               iterate_out_of_display_property (it);
  6087               *position = it->position;
  6088             }
  6089           return 1;
  6090         }
  6091 
  6092       if (it)
  6093         {
  6094           int face_id = lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
  6095 
  6096           if (CONSP (XCDR (XCDR (spec))))
  6097             {
  6098               Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
  6099               int face_id2;
  6100               /* Don't allow quitting from lookup_derived_face, for when
  6101                  we are displaying a non-selected window, and the buffer's
  6102                  point was temporarily moved to the window-point.  */
  6103               specpdl_ref count1 = SPECPDL_INDEX ();
  6104               specbind (Qinhibit_quit, Qt);
  6105               face_id2 = lookup_derived_face (it->w, it->f, face_name,
  6106                                               FRINGE_FACE_ID, false);
  6107               unbind_to (count1, Qnil);
  6108               if (face_id2 >= 0)
  6109                 face_id = face_id2;
  6110             }
  6111 
  6112           /* Save current settings of IT so that we can restore them
  6113              when we are finished with the glyph property value.  */
  6114           push_it (it, position);
  6115 
  6116           it->area = TEXT_AREA;
  6117           it->what = IT_IMAGE;
  6118           it->image_id = -1; /* no image */
  6119           it->position = start_pos;
  6120           it->object = NILP (object) ? it->w->contents : object;
  6121           it->method = GET_FROM_IMAGE;
  6122           it->from_overlay = Qnil;
  6123           it->face_id = face_id;
  6124           it->from_disp_prop_p = true;
  6125 
  6126           /* Say that we haven't consumed the characters with
  6127              `display' property yet.  The call to pop_it in
  6128              set_iterator_to_next will clean this up.  */
  6129           *position = start_pos;
  6130 
  6131           if (EQ (XCAR (spec), Qleft_fringe))
  6132             {
  6133               it->left_user_fringe_bitmap = fringe_bitmap;
  6134               it->left_user_fringe_face_id = face_id;
  6135             }
  6136           else
  6137             {
  6138               it->right_user_fringe_bitmap = fringe_bitmap;
  6139               it->right_user_fringe_face_id = face_id;
  6140             }
  6141         }
  6142 #endif /* HAVE_WINDOW_SYSTEM */
  6143       return 1;
  6144     }
  6145 
  6146   /* Prepare to handle `((margin left-margin) ...)',
  6147      `((margin right-margin) ...)' and `((margin nil) ...)'
  6148      prefixes for display specifications.  */
  6149   location = Qunbound;
  6150   if (CONSP (spec) && CONSP (XCAR (spec)))
  6151     {
  6152       Lisp_Object tem;
  6153 
  6154       value = XCDR (spec);
  6155       if (CONSP (value))
  6156         value = XCAR (value);
  6157 
  6158       tem = XCAR (spec);
  6159       if (EQ (XCAR (tem), Qmargin)
  6160           && (tem = XCDR (tem),
  6161               tem = CONSP (tem) ? XCAR (tem) : Qnil,
  6162               (NILP (tem)
  6163                || EQ (tem, Qleft_margin)
  6164                || EQ (tem, Qright_margin))))
  6165         location = tem;
  6166     }
  6167 
  6168   if (BASE_EQ (location, Qunbound))
  6169     {
  6170       location = Qnil;
  6171       value = spec;
  6172     }
  6173 
  6174   /* After this point, VALUE is the property after any
  6175      margin prefix has been stripped.  It must be a string,
  6176      an image specification, or `(space ...)'.
  6177 
  6178      LOCATION specifies where to display: `left-margin',
  6179      `right-margin' or nil.  */
  6180 
  6181   bool valid_p = (STRINGP (value)
  6182 #ifdef HAVE_WINDOW_SYSTEM
  6183                   || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6184                       && valid_image_p (value))
  6185 #endif /* not HAVE_WINDOW_SYSTEM */
  6186              || (CONSP (value) && EQ (XCAR (value), Qspace))
  6187              || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6188                  && valid_xwidget_spec_p (value)));
  6189 
  6190   if (valid_p && display_replaced == 0)
  6191     {
  6192       int retval = 1;
  6193 
  6194       if (!it)
  6195         {
  6196           /* Callers need to know whether the display spec is any kind
  6197              of `(space ...)' spec that is about to affect text-area
  6198              display.  */
  6199           if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
  6200             retval = 2;
  6201           return retval;
  6202         }
  6203 
  6204       /* Save current settings of IT so that we can restore them
  6205          when we are finished with the glyph property value.  */
  6206       push_it (it, position);
  6207       it->from_overlay = overlay;
  6208       it->from_disp_prop_p = true;
  6209 
  6210       if (NILP (location))
  6211         it->area = TEXT_AREA;
  6212       else if (EQ (location, Qleft_margin))
  6213         it->area = LEFT_MARGIN_AREA;
  6214       else
  6215         it->area = RIGHT_MARGIN_AREA;
  6216 
  6217       if (STRINGP (value))
  6218         {
  6219           it->string = value;
  6220           it->multibyte_p = STRING_MULTIBYTE (it->string);
  6221           it->current.overlay_string_index = -1;
  6222           IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6223           it->end_charpos = it->string_nchars = SCHARS (it->string);
  6224           it->method = GET_FROM_STRING;
  6225           it->stop_charpos = 0;
  6226           it->prev_stop = 0;
  6227           it->base_level_stop = 0;
  6228           it->string_from_display_prop_p = true;
  6229           it->cmp_it.id = -1;
  6230           /* Say that we haven't consumed the characters with
  6231              `display' property yet.  The call to pop_it in
  6232              set_iterator_to_next will clean this up.  */
  6233           if (BUFFERP (object))
  6234             *position = start_pos;
  6235 
  6236           /* Force paragraph direction to be that of the parent
  6237              object.  If the parent object's paragraph direction is
  6238              not yet determined, default to L2R.  */
  6239           if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6240             it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6241           else
  6242             it->paragraph_embedding = L2R;
  6243 
  6244           /* Set up the bidi iterator for this display string.  */
  6245           if (it->bidi_p)
  6246             {
  6247               it->bidi_it.string.lstring = it->string;
  6248               it->bidi_it.string.s = NULL;
  6249               it->bidi_it.string.schars = it->end_charpos;
  6250               it->bidi_it.string.bufpos = bufpos;
  6251               it->bidi_it.string.from_disp_str = true;
  6252               it->bidi_it.string.unibyte = !it->multibyte_p;
  6253               it->bidi_it.w = it->w;
  6254               bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6255             }
  6256         }
  6257       else if (CONSP (value) && EQ (XCAR (value), Qspace))
  6258         {
  6259           it->method = GET_FROM_STRETCH;
  6260           it->object = value;
  6261           *position = it->position = start_pos;
  6262           retval = 1 + (it->area == TEXT_AREA);
  6263         }
  6264       else if (valid_xwidget_spec_p (value))
  6265         {
  6266           it->what = IT_XWIDGET;
  6267           it->method = GET_FROM_XWIDGET;
  6268           it->position = start_pos;
  6269           it->object = NILP (object) ? it->w->contents : object;
  6270           *position = start_pos;
  6271           it->xwidget = lookup_xwidget (value);
  6272         }
  6273 #ifdef HAVE_WINDOW_SYSTEM
  6274       else
  6275         {
  6276           specpdl_ref count = SPECPDL_INDEX ();
  6277 
  6278           it->what = IT_IMAGE;
  6279           /* Don't allow quitting from lookup_image, for when we are
  6280              displaying a non-selected window, and the buffer's point
  6281              was temporarily moved to the window-point.  */
  6282           specbind (Qinhibit_quit, Qt);
  6283           it->image_id = lookup_image (it->f, value, it->face_id);
  6284           unbind_to (count, Qnil);
  6285           it->position = start_pos;
  6286           it->object = NILP (object) ? it->w->contents : object;
  6287           it->method = GET_FROM_IMAGE;
  6288 
  6289           /* Say that we haven't consumed the characters with
  6290              `display' property yet.  The call to pop_it in
  6291              set_iterator_to_next will clean this up.  */
  6292           *position = start_pos;
  6293         }
  6294 #endif /* HAVE_WINDOW_SYSTEM */
  6295 
  6296       return retval;
  6297     }
  6298 
  6299   /* Invalid property or property not supported.  Restore
  6300      POSITION to what it was before.  */
  6301   *position = start_pos;
  6302   return 0;
  6303 }
  6304 
  6305 /* Check if PROP is a display property value whose text should be
  6306    treated as intangible.  OVERLAY is the overlay from which PROP
  6307    came, or nil if it came from a text property.  CHARPOS and BYTEPOS
  6308    specify the buffer position covered by PROP.  */
  6309 
  6310 bool
  6311 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
  6312                            ptrdiff_t charpos, ptrdiff_t bytepos)
  6313 {
  6314   bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
  6315   struct text_pos position;
  6316 
  6317   SET_TEXT_POS (position, charpos, bytepos);
  6318   return (handle_display_spec (NULL, prop, Qnil, overlay,
  6319                                &position, charpos, frame_window_p)
  6320           != 0);
  6321 }
  6322 
  6323 
  6324 /* Return true if PROP is a display sub-property value containing STRING.
  6325 
  6326    Implementation note: this and the following function are really
  6327    special cases of handle_display_spec and
  6328    handle_single_display_spec, and should ideally use the same code.
  6329    Until they do, these two pairs must be consistent and must be
  6330    modified in sync.  */
  6331 
  6332 static bool
  6333 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
  6334 {
  6335   if (EQ (string, prop))
  6336     return true;
  6337 
  6338   /* Skip over `when FORM'.  */
  6339   if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
  6340     {
  6341       prop = XCDR (prop);
  6342       if (!CONSP (prop))
  6343         return false;
  6344       /* Actually, the condition following `when' should be eval'ed,
  6345          like handle_single_display_spec does, and we should return
  6346          false if it evaluates to nil.  However, this function is
  6347          called only when the buffer was already displayed and some
  6348          glyph in the glyph matrix was found to come from a display
  6349          string.  Therefore, the condition was already evaluated, and
  6350          the result was non-nil, otherwise the display string wouldn't
  6351          have been displayed and we would have never been called for
  6352          this property.  Thus, we can skip the evaluation and assume
  6353          its result is non-nil.  */
  6354       prop = XCDR (prop);
  6355     }
  6356 
  6357   if (CONSP (prop))
  6358     /* Skip over `margin LOCATION'.  */
  6359     if (EQ (XCAR (prop), Qmargin))
  6360       {
  6361         prop = XCDR (prop);
  6362         if (!CONSP (prop))
  6363           return false;
  6364 
  6365         prop = XCDR (prop);
  6366         if (!CONSP (prop))
  6367           return false;
  6368       }
  6369 
  6370   return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
  6371 }
  6372 
  6373 
  6374 /* Return true if STRING appears in the `display' property PROP.  */
  6375 
  6376 static bool
  6377 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
  6378 {
  6379   if (CONSP (prop)
  6380       && !EQ (XCAR (prop), Qwhen)
  6381       && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
  6382     {
  6383       /* A list of sub-properties.  */
  6384       while (CONSP (prop))
  6385         {
  6386           if (single_display_spec_string_p (XCAR (prop), string))
  6387             return true;
  6388           prop = XCDR (prop);
  6389         }
  6390     }
  6391   else if (VECTORP (prop))
  6392     {
  6393       /* A vector of sub-properties.  */
  6394       ptrdiff_t i;
  6395       for (i = 0; i < ASIZE (prop); ++i)
  6396         if (single_display_spec_string_p (AREF (prop, i), string))
  6397           return true;
  6398     }
  6399   else
  6400     return single_display_spec_string_p (prop, string);
  6401 
  6402   return false;
  6403 }
  6404 
  6405 /* Look for STRING in overlays and text properties in the current
  6406    buffer, between character positions FROM and TO (excluding TO).
  6407    BACK_P means look back (in this case, TO is supposed to be
  6408    less than FROM).
  6409    Value is the first character position where STRING was found, or
  6410    zero if it wasn't found before hitting TO.
  6411 
  6412    This function may only use code that doesn't eval because it is
  6413    called asynchronously from note_mouse_highlight.  */
  6414 
  6415 static ptrdiff_t
  6416 string_buffer_position_lim (Lisp_Object string,
  6417                             ptrdiff_t from, ptrdiff_t to, bool back_p)
  6418 {
  6419   Lisp_Object limit, prop, pos;
  6420   bool found = false;
  6421 
  6422   pos = make_fixnum (max (from, BEGV));
  6423 
  6424   if (!back_p)  /* looking forward */
  6425     {
  6426       limit = make_fixnum (min (to, ZV));
  6427       while (!found && !EQ (pos, limit))
  6428         {
  6429           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6430           if (!NILP (prop) && display_prop_string_p (prop, string))
  6431             found = true;
  6432           else
  6433             pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
  6434                                                      limit);
  6435         }
  6436     }
  6437   else          /* looking back */
  6438     {
  6439       limit = make_fixnum (max (to, BEGV));
  6440       while (!found && !EQ (pos, limit))
  6441         {
  6442           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6443           if (!NILP (prop) && display_prop_string_p (prop, string))
  6444             found = true;
  6445           else
  6446             pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
  6447                                                          limit);
  6448         }
  6449     }
  6450 
  6451   return found ? XFIXNUM (pos) : 0;
  6452 }
  6453 
  6454 /* Determine which buffer position in current buffer STRING comes from.
  6455    AROUND_CHARPOS is an approximate position where it could come from.
  6456    Value is the buffer position or 0 if it couldn't be determined.
  6457 
  6458    This function is necessary because we don't record buffer positions
  6459    in glyphs generated from strings (to keep struct glyph small).
  6460    This function may only use code that doesn't eval because it is
  6461    called asynchronously from note_mouse_highlight.  */
  6462 
  6463 static ptrdiff_t
  6464 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
  6465 {
  6466   const int MAX_DISTANCE = 1000;
  6467   ptrdiff_t forward_limit = min (around_charpos + MAX_DISTANCE, ZV);
  6468   ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
  6469                                                 forward_limit, false);
  6470 
  6471   if (!found)
  6472     {
  6473       ptrdiff_t backward_limit = max (around_charpos - MAX_DISTANCE, BEGV);
  6474       found = string_buffer_position_lim (string, around_charpos,
  6475                                           backward_limit, true);
  6476     }
  6477   return found;
  6478 }
  6479 
  6480 
  6481 
  6482 /***********************************************************************
  6483                         `composition' property
  6484  ***********************************************************************/
  6485 
  6486 /* Set up iterator IT from `composition' property at its current
  6487    position.  Called from handle_stop.  */
  6488 
  6489 static enum prop_handled
  6490 handle_composition_prop (struct it *it)
  6491 {
  6492   Lisp_Object prop, string;
  6493   ptrdiff_t pos, pos_byte, start, end;
  6494 
  6495   if (STRINGP (it->string))
  6496     {
  6497       unsigned char *s;
  6498 
  6499       pos = IT_STRING_CHARPOS (*it);
  6500       pos_byte = IT_STRING_BYTEPOS (*it);
  6501       string = it->string;
  6502       s = SDATA (string) + pos_byte;
  6503       if (STRING_MULTIBYTE (string))
  6504         it->c = STRING_CHAR (s);
  6505       else
  6506         it->c = *s;
  6507     }
  6508   else
  6509     {
  6510       pos = IT_CHARPOS (*it);
  6511       pos_byte = IT_BYTEPOS (*it);
  6512       string = Qnil;
  6513       it->c = FETCH_CHAR (pos_byte);
  6514     }
  6515 
  6516   /* If there's a valid composition and point is not inside of the
  6517      composition (in the case that the composition is from the current
  6518      buffer), draw a glyph composed from the composition components.  */
  6519   if (find_composition (pos, -1, &start, &end, &prop, string)
  6520       && composition_valid_p (start, end, prop)
  6521       && (STRINGP (it->string) || (PT <= start || PT >= end)))
  6522     {
  6523       if (start < pos)
  6524         /* As we can't handle this situation (perhaps font-lock added
  6525            a new composition), we just return here hoping that next
  6526            redisplay will detect this composition much earlier.  */
  6527         return HANDLED_NORMALLY;
  6528       if (start != pos)
  6529         {
  6530           if (STRINGP (it->string))
  6531             pos_byte = string_char_to_byte (it->string, start);
  6532           else
  6533             pos_byte = CHAR_TO_BYTE (start);
  6534         }
  6535       it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
  6536                                                prop, string);
  6537 
  6538       if (it->cmp_it.id >= 0)
  6539         {
  6540           it->cmp_it.ch = -1;
  6541           it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
  6542           it->cmp_it.nglyphs = -1;
  6543         }
  6544     }
  6545 
  6546   return HANDLED_NORMALLY;
  6547 }
  6548 
  6549 
  6550 
  6551 /***********************************************************************
  6552                            Overlay strings
  6553  ***********************************************************************/
  6554 
  6555 /* The following structure is used to record overlay strings for
  6556    later sorting in load_overlay_strings.  */
  6557 
  6558 struct overlay_entry
  6559 {
  6560   Lisp_Object overlay;
  6561   Lisp_Object string;
  6562   EMACS_INT priority;
  6563   bool after_string_p;
  6564 };
  6565 
  6566 
  6567 /* Set up iterator IT from overlay strings at its current position.
  6568    Called from handle_stop.  */
  6569 
  6570 static enum prop_handled
  6571 handle_overlay_change (struct it *it)
  6572 {
  6573   if (!STRINGP (it->string) && get_overlay_strings (it, 0))
  6574     return HANDLED_RECOMPUTE_PROPS;
  6575   else
  6576     return HANDLED_NORMALLY;
  6577 }
  6578 
  6579 
  6580 /* Set up the next overlay string for delivery by IT, if there is an
  6581    overlay string to deliver.  Called by set_iterator_to_next when the
  6582    end of the current overlay string is reached.  If there are more
  6583    overlay strings to display, IT->string and
  6584    IT->current.overlay_string_index are set appropriately here.
  6585    Otherwise IT->string is set to nil.  */
  6586 
  6587 static void
  6588 next_overlay_string (struct it *it)
  6589 {
  6590   ++it->current.overlay_string_index;
  6591   if (it->current.overlay_string_index == it->n_overlay_strings)
  6592     {
  6593       /* No more overlay strings.  Restore IT's settings to what
  6594          they were before overlay strings were processed, and
  6595          continue to deliver from current_buffer.  */
  6596 
  6597       it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
  6598       pop_it (it);
  6599       eassert (it->sp > 0
  6600                || (NILP (it->string)
  6601                    && it->method == GET_FROM_BUFFER
  6602                    && it->stop_charpos >= BEGV
  6603                    && it->stop_charpos <= it->end_charpos));
  6604       it->current.overlay_string_index = -1;
  6605       it->n_overlay_strings = 0;
  6606       /* If there's an empty display string on the stack, pop the
  6607          stack, to resync the bidi iterator with IT's position.  Such
  6608          empty strings are pushed onto the stack in
  6609          get_overlay_strings_1.  */
  6610       if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
  6611         pop_it (it);
  6612 
  6613       /* Since we've exhausted overlay strings at this buffer
  6614          position, set the flag to ignore overlays until we move to
  6615          another position.  (The flag will be reset in
  6616          next_element_from_buffer.)  But don't do that if the overlay
  6617          strings were loaded at position other than the current one,
  6618          which could happen if we called pop_it above, or if the
  6619          overlay strings were loaded by handle_invisible_prop at the
  6620          beginning of invisible text.  */
  6621       if (it->overlay_strings_charpos == IT_CHARPOS (*it))
  6622         it->ignore_overlay_strings_at_pos_p = true;
  6623 
  6624       /* If we're at the end of the buffer, record that we have
  6625          processed the overlay strings there already, so that
  6626          next_element_from_buffer doesn't try it again.  */
  6627       if (NILP (it->string)
  6628           && IT_CHARPOS (*it) >= it->end_charpos
  6629           && it->overlay_strings_charpos >= it->end_charpos)
  6630         it->overlay_strings_at_end_processed_p = true;
  6631       /* Note: we reset overlay_strings_charpos only here, to make
  6632          sure the just-processed overlays were indeed at EOB.
  6633          Otherwise, overlays on text with invisible text property,
  6634          which are processed with IT's position past the invisible
  6635          text, might fool us into thinking the overlays at EOB were
  6636          already processed (linum-mode can cause this, for
  6637          example).  */
  6638       it->overlay_strings_charpos = -1;
  6639     }
  6640   else
  6641     {
  6642       /* There are more overlay strings to process.  If
  6643          IT->current.overlay_string_index has advanced to a position
  6644          where we must load IT->overlay_strings with more strings, do
  6645          it.  We must load at the IT->overlay_strings_charpos where
  6646          IT->n_overlay_strings was originally computed; when invisible
  6647          text is present, this might not be IT_CHARPOS (Bug#7016).  */
  6648       int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
  6649 
  6650       if (it->current.overlay_string_index && i == 0)
  6651         load_overlay_strings (it, it->overlay_strings_charpos);
  6652 
  6653       /* Initialize IT to deliver display elements from the overlay
  6654          string.  */
  6655       it->string = it->overlay_strings[i];
  6656       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6657       SET_TEXT_POS (it->current.string_pos, 0, 0);
  6658       it->method = GET_FROM_STRING;
  6659       it->stop_charpos = 0;
  6660       it->end_charpos = SCHARS (it->string);
  6661       if (it->cmp_it.stop_pos >= 0)
  6662         it->cmp_it.stop_pos = 0;
  6663       it->prev_stop = 0;
  6664       it->base_level_stop = 0;
  6665 
  6666       /* Set up the bidi iterator for this overlay string.  */
  6667       if (it->bidi_p)
  6668         {
  6669           it->bidi_it.string.lstring = it->string;
  6670           it->bidi_it.string.s = NULL;
  6671           it->bidi_it.string.schars = SCHARS (it->string);
  6672           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  6673           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6674           it->bidi_it.string.unibyte = !it->multibyte_p;
  6675           it->bidi_it.w = it->w;
  6676           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6677         }
  6678     }
  6679 
  6680   CHECK_IT (it);
  6681 }
  6682 
  6683 
  6684 /* Compare two overlay_entry structures E1 and E2.  Used as a
  6685    comparison function for qsort in load_overlay_strings.  Overlay
  6686    strings for the same position are sorted so that
  6687 
  6688    1. All after-strings come in front of before-strings, except
  6689    when they come from the same overlay.
  6690 
  6691    2. Within after-strings, strings are sorted so that overlay strings
  6692    from overlays with higher priorities come first.
  6693 
  6694    2. Within before-strings, strings are sorted so that overlay
  6695    strings from overlays with higher priorities come last.
  6696 
  6697    Value is analogous to strcmp.  */
  6698 
  6699 
  6700 static int
  6701 compare_overlay_entries (const void *e1, const void *e2)
  6702 {
  6703   struct overlay_entry const *entry1 = e1;
  6704   struct overlay_entry const *entry2 = e2;
  6705   int result;
  6706 
  6707   if (entry1->after_string_p != entry2->after_string_p)
  6708     {
  6709       /* Let after-strings appear in front of before-strings if
  6710          they come from different overlays.  */
  6711       if (EQ (entry1->overlay, entry2->overlay))
  6712         result = entry1->after_string_p ? 1 : -1;
  6713       else
  6714         result = entry1->after_string_p ? -1 : 1;
  6715     }
  6716   else if (entry1->priority != entry2->priority)
  6717     {
  6718       if (entry1->after_string_p)
  6719         /* After-strings sorted in order of decreasing priority.  */
  6720         result = entry2->priority < entry1->priority ? -1 : 1;
  6721       else
  6722         /* Before-strings sorted in order of increasing priority.  */
  6723         result = entry1->priority < entry2->priority ? -1 : 1;
  6724     }
  6725   else
  6726     result = 0;
  6727 
  6728   return result;
  6729 }
  6730 
  6731 
  6732 /* Load the vector IT->overlay_strings with overlay strings from IT's
  6733    current buffer position, or from CHARPOS if that is > 0.  Set
  6734    IT->n_overlays to the total number of overlay strings found.
  6735 
  6736    Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
  6737    a time.  On entry into load_overlay_strings,
  6738    IT->current.overlay_string_index gives the number of overlay
  6739    strings that have already been loaded by previous calls to this
  6740    function.
  6741 
  6742    IT->add_overlay_start contains an additional overlay start
  6743    position to consider for taking overlay strings from, if non-zero.
  6744    This position comes into play when the overlay has an `invisible'
  6745    property, and both before and after-strings.  When we've skipped to
  6746    the end of the overlay, because of its `invisible' property, we
  6747    nevertheless want its before-string to appear.
  6748    IT->add_overlay_start will contain the overlay start position
  6749    in this case.
  6750 
  6751    Overlay strings are sorted so that after-string strings come in
  6752    front of before-string strings.  Within before and after-strings,
  6753    strings are sorted by overlay priority.  See also function
  6754    compare_overlay_entries.  */
  6755 
  6756 static void
  6757 load_overlay_strings (struct it *it, ptrdiff_t charpos)
  6758 {
  6759   ptrdiff_t n = 0;
  6760   struct overlay_entry entriesbuf[20];
  6761   ptrdiff_t size = ARRAYELTS (entriesbuf);
  6762   struct overlay_entry *entries = entriesbuf;
  6763   struct itree_node *node;
  6764 
  6765   USE_SAFE_ALLOCA;
  6766 
  6767   if (charpos <= 0)
  6768     charpos = IT_CHARPOS (*it);
  6769 
  6770   /* Append the overlay string STRING of overlay OVERLAY to vector
  6771      `entries' which has size `size' and currently contains `n'
  6772      elements.  AFTER_P means STRING is an after-string of
  6773      OVERLAY.  */
  6774 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P)                 \
  6775   do                                                                    \
  6776     {                                                                   \
  6777       Lisp_Object priority;                                             \
  6778                                                                         \
  6779       if (n == size)                                                    \
  6780         {                                                               \
  6781           struct overlay_entry *old = entries;                          \
  6782           SAFE_NALLOCA (entries, 2, size);                              \
  6783           memcpy (entries, old, size * sizeof *entries);                \
  6784           size *= 2;                                                    \
  6785         }                                                               \
  6786                                                                         \
  6787       entries[n].string = (STRING);                                     \
  6788       entries[n].overlay = (OVERLAY);                                   \
  6789       priority = Foverlay_get ((OVERLAY), Qpriority);                   \
  6790       entries[n].priority = FIXNUMP (priority) ? XFIXNUM (priority) : 0;  \
  6791       entries[n].after_string_p = (AFTER_P);                            \
  6792       ++n;                                                              \
  6793     }                                                                   \
  6794   while (false)
  6795 
  6796 
  6797   /* Process overlays.  */
  6798   ITREE_FOREACH (node, current_buffer->overlays, charpos - 1, charpos + 1, DESCENDING)
  6799     {
  6800       Lisp_Object overlay = node->data;
  6801       eassert (OVERLAYP (overlay));
  6802       ptrdiff_t start = node->begin;
  6803       ptrdiff_t end = node->end;
  6804 
  6805       /* Skip this overlay if it doesn't start or end at IT's current
  6806          position.  */
  6807       if (end != charpos && start != charpos)
  6808         continue;
  6809 
  6810       /* Skip this overlay if it doesn't apply to IT->w.  */
  6811       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  6812       if (WINDOWP (window) && XWINDOW (window) != it->w)
  6813         continue;
  6814 
  6815       /* If the text ``under'' the overlay is invisible, both before-
  6816          and after-strings from this overlay are visible; start and
  6817          end position are indistinguishable.  */
  6818       Lisp_Object invisible = Foverlay_get (overlay, Qinvisible);
  6819       int invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
  6820 
  6821       /* If overlay has a non-empty before-string, record it.  */
  6822       Lisp_Object str;
  6823       if ((start == charpos || (end == charpos && invis != 0))
  6824           && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
  6825           && SCHARS (str))
  6826         RECORD_OVERLAY_STRING (overlay, str, false);
  6827 
  6828       /* If overlay has a non-empty after-string, record it.  */
  6829       if ((end == charpos || (start == charpos && invis != 0))
  6830           && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
  6831           && SCHARS (str))
  6832         RECORD_OVERLAY_STRING (overlay, str, true);
  6833     }
  6834 
  6835 #undef RECORD_OVERLAY_STRING
  6836 
  6837   /* Sort entries.  */
  6838   if (n > 1)
  6839     qsort (entries, n, sizeof *entries, compare_overlay_entries);
  6840 
  6841   /* Record number of overlay strings, and where we computed it.  */
  6842   it->n_overlay_strings = n;
  6843   it->overlay_strings_charpos = charpos;
  6844 
  6845   /* IT->current.overlay_string_index is the number of overlay strings
  6846      that have already been consumed by IT.  Copy some of the
  6847      remaining overlay strings to IT->overlay_strings.  */
  6848   ptrdiff_t j = it->current.overlay_string_index;
  6849   for (ptrdiff_t i = 0; i < OVERLAY_STRING_CHUNK_SIZE && j < n; i++, j++)
  6850     {
  6851       it->overlay_strings[i] = entries[j].string;
  6852       it->string_overlays[i] = entries[j].overlay;
  6853     }
  6854 
  6855   CHECK_IT (it);
  6856   SAFE_FREE ();
  6857 }
  6858 
  6859 
  6860 /* Get the first chunk of overlay strings at IT's current buffer
  6861    position, or at CHARPOS if that is > 0.  Value is true if at
  6862    least one overlay string was found.  */
  6863 
  6864 static bool
  6865 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
  6866 {
  6867   /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
  6868      process.  This fills IT->overlay_strings with strings, and sets
  6869      IT->n_overlay_strings to the total number of strings to process.
  6870      IT->pos.overlay_string_index has to be set temporarily to zero
  6871      because load_overlay_strings needs this; it must be set to -1
  6872      when no overlay strings are found because a zero value would
  6873      indicate a position in the first overlay string.  */
  6874   it->current.overlay_string_index = 0;
  6875   load_overlay_strings (it, charpos);
  6876 
  6877   /* If we found overlay strings, set up IT to deliver display
  6878      elements from the first one.  Otherwise set up IT to deliver
  6879      from current_buffer.  */
  6880   if (it->n_overlay_strings)
  6881     {
  6882       /* Make sure we know settings in current_buffer, so that we can
  6883          restore meaningful values when we're done with the overlay
  6884          strings.  */
  6885       if (compute_stop_p)
  6886         compute_stop_pos (it);
  6887       eassert (it->face_id >= 0);
  6888 
  6889       /* Save IT's settings.  They are restored after all overlay
  6890          strings have been processed.  */
  6891       eassert (!compute_stop_p || it->sp == 0);
  6892 
  6893       /* When called from handle_stop, there might be an empty display
  6894          string loaded.  In that case, don't bother saving it.  But
  6895          don't use this optimization with the bidi iterator, since we
  6896          need the corresponding pop_it call to resync the bidi
  6897          iterator's position with IT's position, after we are done
  6898          with the overlay strings.  (The corresponding call to pop_it
  6899          in case of an empty display string is in
  6900          next_overlay_string.)  */
  6901       if (!(!it->bidi_p
  6902             && STRINGP (it->string) && !SCHARS (it->string)))
  6903         push_it (it, NULL);
  6904 
  6905       /* Set up IT to deliver display elements from the first overlay
  6906          string.  */
  6907       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6908       it->string = it->overlay_strings[0];
  6909       it->from_overlay = Qnil;
  6910       it->stop_charpos = 0;
  6911       eassert (STRINGP (it->string));
  6912       it->end_charpos = SCHARS (it->string);
  6913       it->prev_stop = 0;
  6914       it->base_level_stop = 0;
  6915       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6916       it->method = GET_FROM_STRING;
  6917       it->from_disp_prop_p = 0;
  6918       it->cmp_it.id = -1;
  6919 
  6920       /* Force paragraph direction to be that of the parent
  6921          buffer.  */
  6922       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6923         it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6924       else
  6925         it->paragraph_embedding = L2R;
  6926 
  6927       /* Set up the bidi iterator for this overlay string.  */
  6928       if (it->bidi_p)
  6929         {
  6930           ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
  6931 
  6932           it->bidi_it.string.lstring = it->string;
  6933           it->bidi_it.string.s = NULL;
  6934           it->bidi_it.string.schars = SCHARS (it->string);
  6935           it->bidi_it.string.bufpos = pos;
  6936           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6937           it->bidi_it.string.unibyte = !it->multibyte_p;
  6938           it->bidi_it.w = it->w;
  6939           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6940         }
  6941       return true;
  6942     }
  6943 
  6944   it->current.overlay_string_index = -1;
  6945   return false;
  6946 }
  6947 
  6948 static bool
  6949 get_overlay_strings (struct it *it, ptrdiff_t charpos)
  6950 {
  6951   it->string = Qnil;
  6952   it->method = GET_FROM_BUFFER;
  6953 
  6954   get_overlay_strings_1 (it, charpos, true);
  6955 
  6956   CHECK_IT (it);
  6957 
  6958   /* Value is true if we found at least one overlay string.  */
  6959   return STRINGP (it->string);
  6960 }
  6961 
  6962 
  6963 
  6964 /***********************************************************************
  6965                       Saving and restoring state
  6966  ***********************************************************************/
  6967 
  6968 /* Save current settings of IT on IT->stack.  Called, for example,
  6969    before setting up IT for an overlay string, to be able to restore
  6970    IT's settings to what they were after the overlay string has been
  6971    processed.  If POSITION is non-NULL, it is the position to save on
  6972    the stack instead of IT->position.  */
  6973 
  6974 static void
  6975 push_it (struct it *it, struct text_pos *position)
  6976 {
  6977   struct iterator_stack_entry *p;
  6978 
  6979   eassert (it->sp < IT_STACK_SIZE);
  6980   p = it->stack + it->sp;
  6981 
  6982   p->stop_charpos = it->stop_charpos;
  6983   p->prev_stop = it->prev_stop;
  6984   p->base_level_stop = it->base_level_stop;
  6985   p->cmp_it = it->cmp_it;
  6986   eassert (it->face_id >= 0);
  6987   p->face_id = it->face_id;
  6988   p->string = it->string;
  6989   p->method = it->method;
  6990   p->from_overlay = it->from_overlay;
  6991   switch (p->method)
  6992     {
  6993     case GET_FROM_IMAGE:
  6994       p->u.image.object = it->object;
  6995       p->u.image.image_id = it->image_id;
  6996       p->u.image.slice = it->slice;
  6997       break;
  6998     case GET_FROM_STRETCH:
  6999       p->u.stretch.object = it->object;
  7000       break;
  7001     case GET_FROM_XWIDGET:
  7002       p->u.xwidget.object = it->object;
  7003       break;
  7004     case GET_FROM_BUFFER:
  7005     case GET_FROM_DISPLAY_VECTOR:
  7006     case GET_FROM_STRING:
  7007     case GET_FROM_C_STRING:
  7008       break;
  7009     default:
  7010       emacs_abort ();
  7011     }
  7012   p->position = position ? *position : it->position;
  7013   p->current = it->current;
  7014   p->end_charpos = it->end_charpos;
  7015   p->string_nchars = it->string_nchars;
  7016   p->area = it->area;
  7017   p->multibyte_p = it->multibyte_p;
  7018   p->avoid_cursor_p = it->avoid_cursor_p;
  7019   p->space_width = it->space_width;
  7020   p->font_height = it->font_height;
  7021   p->voffset = it->voffset;
  7022   p->string_from_display_prop_p = it->string_from_display_prop_p;
  7023   p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
  7024   p->display_ellipsis_p = false;
  7025   p->line_wrap = it->line_wrap;
  7026   p->bidi_p = it->bidi_p;
  7027   p->paragraph_embedding = it->paragraph_embedding;
  7028   p->from_disp_prop_p = it->from_disp_prop_p;
  7029   ++it->sp;
  7030 
  7031   /* Save the state of the bidi iterator as well. */
  7032   if (it->bidi_p)
  7033     bidi_push_it (&it->bidi_it);
  7034 }
  7035 
  7036 static void
  7037 iterate_out_of_display_property (struct it *it)
  7038 {
  7039   bool buffer_p = !STRINGP (it->string);
  7040   ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
  7041   ptrdiff_t bob = (buffer_p ? BEGV : 0);
  7042 
  7043   eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
  7044 
  7045   /* Maybe initialize paragraph direction.  If we are at the beginning
  7046      of a new paragraph, next_element_from_buffer may not have a
  7047      chance to do that.  */
  7048   if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
  7049     bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  7050   /* prev_stop can be zero, so check against BEGV as well.  */
  7051   while (it->bidi_it.charpos >= bob
  7052          && it->prev_stop <= it->bidi_it.charpos
  7053          && it->bidi_it.charpos < CHARPOS (it->position)
  7054          && it->bidi_it.charpos < eob)
  7055     bidi_move_to_visually_next (&it->bidi_it);
  7056   /* Record the stop_pos we just crossed, for when we cross it
  7057      back, maybe.  */
  7058   if (it->bidi_it.charpos > CHARPOS (it->position))
  7059     it->prev_stop = CHARPOS (it->position);
  7060   /* If we ended up not where pop_it put us, resync IT's
  7061      positional members with the bidi iterator. */
  7062   if (it->bidi_it.charpos != CHARPOS (it->position))
  7063     SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
  7064   if (buffer_p)
  7065     it->current.pos = it->position;
  7066   else
  7067     it->current.string_pos = it->position;
  7068 }
  7069 
  7070 /* Restore the IT->face_box_p flag, since it could have been
  7071    overwritten by the face of the object that we just finished
  7072    displaying.  Also, set the IT->start_of_box_run_p flag if the
  7073    change in faces requires that.  */
  7074 static void
  7075 restore_face_box_flags (struct it *it, int prev_face_id)
  7076 {
  7077   struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  7078 
  7079   if (face)
  7080     {
  7081       struct face *prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  7082 
  7083       if (!(it->start_of_box_run_p && prev_face && prev_face->box))
  7084         it->start_of_box_run_p = (face->box != FACE_NO_BOX
  7085                                   && (prev_face == NULL
  7086                                       || prev_face->box == FACE_NO_BOX));
  7087       it->face_box_p = face->box != FACE_NO_BOX;
  7088     }
  7089 }
  7090 
  7091 /* Restore IT's settings from IT->stack.  Called, for example, when no
  7092    more overlay strings must be processed, and we return to delivering
  7093    display elements from a buffer, or when the end of a string from a
  7094    `display' property is reached and we return to delivering display
  7095    elements from an overlay string, or from a buffer.  */
  7096 
  7097 static void
  7098 pop_it (struct it *it)
  7099 {
  7100   struct iterator_stack_entry *p;
  7101   bool from_display_prop = it->from_disp_prop_p;
  7102   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  7103   int prev_face_id = it->face_id;
  7104 
  7105   eassert (it->sp > 0);
  7106   --it->sp;
  7107   p = it->stack + it->sp;
  7108   it->stop_charpos = p->stop_charpos;
  7109   it->prev_stop = p->prev_stop;
  7110   it->base_level_stop = p->base_level_stop;
  7111   it->cmp_it = p->cmp_it;
  7112   it->face_id = p->face_id;
  7113   it->current = p->current;
  7114   it->position = p->position;
  7115   it->string = p->string;
  7116   it->from_overlay = p->from_overlay;
  7117   if (NILP (it->string))
  7118     SET_TEXT_POS (it->current.string_pos, -1, -1);
  7119   it->method = p->method;
  7120   switch (it->method)
  7121     {
  7122     case GET_FROM_IMAGE:
  7123       it->image_id = p->u.image.image_id;
  7124       it->object = p->u.image.object;
  7125       it->slice = p->u.image.slice;
  7126       break;
  7127     case GET_FROM_XWIDGET:
  7128       it->object = p->u.xwidget.object;
  7129       break;
  7130     case GET_FROM_STRETCH:
  7131       it->object = p->u.stretch.object;
  7132       break;
  7133     case GET_FROM_BUFFER:
  7134       {
  7135         restore_face_box_flags (it, prev_face_id);
  7136         it->object = it->w->contents;
  7137       }
  7138       break;
  7139     case GET_FROM_STRING:
  7140       {
  7141         restore_face_box_flags (it, prev_face_id);
  7142         it->object = it->string;
  7143       }
  7144       break;
  7145     case GET_FROM_DISPLAY_VECTOR:
  7146       if (it->s)
  7147         it->method = GET_FROM_C_STRING;
  7148       else if (STRINGP (it->string))
  7149         it->method = GET_FROM_STRING;
  7150       else
  7151         {
  7152           it->method = GET_FROM_BUFFER;
  7153           it->object = it->w->contents;
  7154         }
  7155       break;
  7156     case GET_FROM_C_STRING:
  7157       break;
  7158     default:
  7159       emacs_abort ();
  7160     }
  7161   it->end_charpos = p->end_charpos;
  7162   it->string_nchars = p->string_nchars;
  7163   it->area = p->area;
  7164   it->multibyte_p = p->multibyte_p;
  7165   it->avoid_cursor_p = p->avoid_cursor_p;
  7166   it->space_width = p->space_width;
  7167   it->font_height = p->font_height;
  7168   it->voffset = p->voffset;
  7169   it->string_from_display_prop_p = p->string_from_display_prop_p;
  7170   it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
  7171   it->line_wrap = p->line_wrap;
  7172   it->bidi_p = p->bidi_p;
  7173   it->paragraph_embedding = p->paragraph_embedding;
  7174   it->from_disp_prop_p = p->from_disp_prop_p;
  7175   if (it->bidi_p)
  7176     {
  7177       bidi_pop_it (&it->bidi_it);
  7178       /* Bidi-iterate until we get out of the portion of text, if any,
  7179          covered by a `display' text property or by an overlay with
  7180          `display' property.  (We cannot just jump there, because the
  7181          internal coherency of the bidi iterator state can not be
  7182          preserved across such jumps.)  We also must determine the
  7183          paragraph base direction if the overlay we just processed is
  7184          at the beginning of a new paragraph.  */
  7185       if (from_display_prop
  7186           && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
  7187         iterate_out_of_display_property (it);
  7188 
  7189       eassert ((BUFFERP (it->object)
  7190                 && IT_CHARPOS (*it) == it->bidi_it.charpos
  7191                 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
  7192                || (STRINGP (it->object)
  7193                    && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  7194                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
  7195                || (CONSP (it->object) && it->method == GET_FROM_STRETCH)
  7196                /* We could be in the middle of handling a list or a
  7197                   vector of several 'display' properties, in which
  7198                   case we should only verify the above conditions when
  7199                   we pop the iterator stack the last time, because
  7200                   higher stack levels cannot "iterate out of the
  7201                   display property".  */
  7202                || it->sp > 0);
  7203     }
  7204   /* If we move the iterator over text covered by a display property
  7205      to a new buffer position, any info about previously seen overlays
  7206      is no longer valid.  */
  7207   if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
  7208     it->ignore_overlay_strings_at_pos_p = false;
  7209 }
  7210 
  7211 
  7212 
  7213 /***********************************************************************
  7214                           Moving over lines
  7215  ***********************************************************************/
  7216 
  7217 /* Set IT's current position to the previous line start.  */
  7218 
  7219 static void
  7220 back_to_previous_line_start (struct it *it)
  7221 {
  7222   ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
  7223 
  7224   dec_both (&cp, &bp);
  7225   SET_WITH_NARROWED_BEGV (it, IT_CHARPOS (*it),
  7226                           find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it)),
  7227                           get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
  7228 }
  7229 
  7230 /* Find in the current buffer the first display or overlay string
  7231    between STARTPOS and ENDPOS that includes embedded newlines.
  7232    Consider only overlays that apply to window W.
  7233    Value is non-zero if such a display/overlay string is found.  */
  7234 static bool
  7235 strings_with_newlines (ptrdiff_t startpos, ptrdiff_t endpos, struct window *w)
  7236 {
  7237   struct itree_node *node;
  7238   /* Process overlays.  */
  7239   ITREE_FOREACH (node, current_buffer->overlays, startpos, endpos, DESCENDING)
  7240     {
  7241       Lisp_Object overlay = node->data;
  7242       eassert (OVERLAYP (overlay));
  7243 
  7244       /* Skip this overlay if it doesn't apply to our window.  */
  7245       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  7246       if (WINDOWP (window) && XWINDOW (window) != w)
  7247         continue;
  7248 
  7249       ptrdiff_t ostart = node->begin;
  7250       ptrdiff_t oend = node->end;
  7251 
  7252       /* Skip overlays that don't overlap the range.  */
  7253       if (!((startpos < oend && ostart < endpos)
  7254             || (ostart == oend
  7255                 && (startpos == oend || (endpos == ZV && oend == endpos)))))
  7256         continue;
  7257 
  7258       Lisp_Object str;
  7259       str = Foverlay_get (overlay, Qbefore_string);
  7260       if (STRINGP (str) && SCHARS (str)
  7261           && memchr (SDATA (str), '\n', SBYTES (str)))
  7262         return true;
  7263       str = Foverlay_get (overlay, Qafter_string);
  7264       if (STRINGP (str) && SCHARS (str)
  7265           && memchr (SDATA (str), '\n', SBYTES (str)))
  7266         return true;
  7267     }
  7268 
  7269   /* Check for 'display' properties whose values include strings.  */
  7270   Lisp_Object cpos = make_fixnum (startpos);
  7271   Lisp_Object limpos = make_fixnum (endpos);
  7272 
  7273   while ((cpos = Fnext_single_property_change (cpos, Qdisplay, Qnil, limpos),
  7274           !(NILP (cpos) || XFIXNAT (cpos) >= endpos)))
  7275     {
  7276       Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  7277       Lisp_Object string = string_from_display_spec (spec);
  7278       if (STRINGP (string)
  7279           && memchr (SDATA (string), '\n', SBYTES (string)))
  7280         return true;
  7281     }
  7282 
  7283   return false;
  7284 }
  7285 
  7286 
  7287 /* Move IT to the next line start.
  7288 
  7289    Value is true if a newline was found.  Set *SKIPPED_P to true if
  7290    we skipped over part of the text (as opposed to moving the iterator
  7291    continuously over the text).  Otherwise, don't change the value
  7292    of *SKIPPED_P.
  7293 
  7294    If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
  7295    iterator on the newline, if it was found.
  7296 
  7297    Newlines may come from buffer text, overlay strings, or strings
  7298    displayed via the `display' property.  That's the reason we can't
  7299    simply use find_newline_no_quit.
  7300 
  7301    Note that this function may not skip over invisible text that is so
  7302    because of text properties and immediately follows a newline.  If
  7303    it would, function reseat_at_next_visible_line_start, when called
  7304    from set_iterator_to_next, would effectively make invisible
  7305    characters following a newline part of the wrong glyph row, which
  7306    leads to wrong cursor motion.  */
  7307 
  7308 static bool
  7309 forward_to_next_line_start (struct it *it, bool *skipped_p,
  7310                             struct bidi_it *bidi_it_prev)
  7311 {
  7312   ptrdiff_t old_selective;
  7313   bool newline_found_p = false;
  7314   int n;
  7315   const int MAX_NEWLINE_DISTANCE = 500;
  7316 
  7317   /* If already on a newline, just consume it to avoid unintended
  7318      skipping over invisible text below.  */
  7319   if (it->what == IT_CHARACTER
  7320       && it->c == '\n'
  7321       && CHARPOS (it->position) == IT_CHARPOS (*it))
  7322     {
  7323       if (it->bidi_p && bidi_it_prev)
  7324         *bidi_it_prev = it->bidi_it;
  7325       set_iterator_to_next (it, false);
  7326       it->c = 0;
  7327       return true;
  7328     }
  7329 
  7330   /* Don't handle selective display in the following.  It's (a)
  7331      unnecessary because it's done by the caller, and (b) leads to an
  7332      infinite recursion because next_element_from_ellipsis indirectly
  7333      calls this function.  */
  7334   old_selective = it->selective;
  7335   it->selective = 0;
  7336 
  7337   /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
  7338      from buffer text, or till the end of the string if iterating a
  7339      string.  */
  7340   for (n = 0;
  7341        !newline_found_p && n < MAX_NEWLINE_DISTANCE;
  7342        n += !STRINGP (it->string))
  7343     {
  7344       if (!get_next_display_element (it))
  7345         return false;
  7346       newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
  7347       if (newline_found_p && it->bidi_p && bidi_it_prev)
  7348         *bidi_it_prev = it->bidi_it;
  7349       set_iterator_to_next (it, false);
  7350     }
  7351 
  7352   /* If we didn't find a newline near enough, see if we can use a
  7353      short-cut.  */
  7354   if (!newline_found_p)
  7355     {
  7356       ptrdiff_t bytepos, start = IT_CHARPOS (*it);
  7357       ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
  7358                                               1, &bytepos);
  7359       eassert (!STRINGP (it->string));
  7360 
  7361       /* it->stop_charpos >= limit means we already know there's no
  7362          stop position up until the newline at LIMIT, so there's no
  7363          need for any further checks.  */
  7364       bool no_strings_with_newlines = it->stop_charpos >= limit;
  7365 
  7366       if (!no_strings_with_newlines)
  7367         {
  7368           if (!(current_buffer->long_line_optimizations_p
  7369                 && it->line_wrap == TRUNCATE))
  7370             {
  7371               /* Quick-and-dirty check: if there isn't any `display'
  7372                  property in sight, and no overlays, we're done.  */
  7373               Lisp_Object pos =
  7374                 Fnext_single_property_change (make_fixnum (start),
  7375                                               Qdisplay, Qnil,
  7376                                               make_fixnum (limit));
  7377               no_strings_with_newlines =
  7378                 (NILP (pos) || XFIXNAT (pos) == limit) /* no 'display' props */
  7379                 && next_overlay_change (start) == ZV;  /* no overlays */
  7380             }
  7381           else
  7382             {
  7383               /* For buffers with very long and truncated lines we try
  7384                  harder, because it's worth our while to spend some
  7385                  time looking into the overlays and 'display' properties
  7386                  if we can then avoid iterating through all of them.  */
  7387               no_strings_with_newlines =
  7388                 !strings_with_newlines (start, limit, it->w);
  7389             }
  7390         }
  7391 
  7392       /* If there's no display or overlay strings with embedded
  7393          newlines until the position of the newline in buffer text, we
  7394          can just use that position.  */
  7395       if (no_strings_with_newlines)
  7396         {
  7397           if (!it->bidi_p || !bidi_it_prev)
  7398             {
  7399               /* The optimal case: just jump there.  */
  7400               IT_CHARPOS (*it) = limit;
  7401               IT_BYTEPOS (*it) = bytepos;
  7402             }
  7403           else
  7404             {
  7405               /* The less optimal case: need to bidi-walk there, but
  7406                  this is still cheaper that the full iteration using
  7407                  get_next_display_element and set_iterator_to_next.  */
  7408               struct bidi_it bprev;
  7409 
  7410               /* Help bidi.c avoid expensive searches for display
  7411                  properties and overlays, by telling it that there are
  7412                  none up to `limit'.  */
  7413               if (it->bidi_it.disp_pos < limit)
  7414                 {
  7415                   it->bidi_it.disp_pos = limit;
  7416                   it->bidi_it.disp_prop = 0;
  7417                 }
  7418               do {
  7419                 bprev = it->bidi_it;
  7420                 bidi_move_to_visually_next (&it->bidi_it);
  7421               } while (it->bidi_it.charpos != limit);
  7422               IT_CHARPOS (*it) = limit;
  7423               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7424               if (bidi_it_prev)
  7425                 *bidi_it_prev = bprev;
  7426             }
  7427           *skipped_p = newline_found_p = true;
  7428         }
  7429       else
  7430         {
  7431           /* The slow case.  */
  7432           while (!newline_found_p)
  7433             {
  7434               if (!get_next_display_element (it))
  7435                 break;
  7436               newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
  7437               if (newline_found_p && it->bidi_p && bidi_it_prev)
  7438                 *bidi_it_prev = it->bidi_it;
  7439               set_iterator_to_next (it, false);
  7440             }
  7441         }
  7442     }
  7443 
  7444   it->selective = old_selective;
  7445   return newline_found_p;
  7446 }
  7447 
  7448 
  7449 /* Set IT's current position to the previous visible line start.  Skip
  7450    invisible text that is so either due to text properties or due to
  7451    selective display.  Caution: this does not change IT->current_x and
  7452    IT->hpos.  */
  7453 
  7454 static void
  7455 back_to_previous_visible_line_start (struct it *it)
  7456 {
  7457   while (IT_CHARPOS (*it) > BEGV)
  7458     {
  7459       back_to_previous_line_start (it);
  7460 
  7461       if (IT_CHARPOS (*it) <= BEGV)
  7462         break;
  7463 
  7464       /* If selective > 0, then lines indented more than its value are
  7465          invisible.  */
  7466       if (it->selective > 0
  7467           && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7468                                 it->selective))
  7469         continue;
  7470 
  7471       /* Check the newline before point for invisibility.  */
  7472       {
  7473         Lisp_Object prop;
  7474         prop = Fget_char_property (make_fixnum (IT_CHARPOS (*it) - 1),
  7475                                    Qinvisible, it->window);
  7476         if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
  7477           continue;
  7478       }
  7479 
  7480       if (IT_CHARPOS (*it) <= BEGV)
  7481         break;
  7482 
  7483       {
  7484         struct it it2;
  7485         void *it2data = NULL;
  7486         ptrdiff_t pos;
  7487         ptrdiff_t beg, end;
  7488         Lisp_Object val, overlay;
  7489 
  7490         SAVE_IT (it2, *it, it2data);
  7491 
  7492         /* If newline is part of a composition, continue from start of composition */
  7493         if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
  7494             && beg < IT_CHARPOS (*it))
  7495           goto replaced;
  7496 
  7497         /* If newline is replaced by a display property, find start of overlay
  7498            or interval and continue search from that point.  */
  7499         pos = --IT_CHARPOS (it2);
  7500         --IT_BYTEPOS (it2);
  7501         it2.sp = 0;
  7502         bidi_unshelve_cache (NULL, false);
  7503         it2.string_from_display_prop_p = false;
  7504         it2.from_disp_prop_p = false;
  7505         if (handle_display_prop (&it2) == HANDLED_RETURN
  7506             && !NILP (val = get_char_property_and_overlay
  7507                       (make_fixnum (pos), Qdisplay, Qnil, &overlay))
  7508             && (OVERLAYP (overlay)
  7509                 ? (beg = OVERLAY_START (overlay))
  7510                 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
  7511           {
  7512             RESTORE_IT (it, it, it2data);
  7513             goto replaced;
  7514           }
  7515 
  7516         /* Newline is not replaced by anything -- so we are done.  */
  7517         RESTORE_IT (it, it, it2data);
  7518         break;
  7519 
  7520       replaced:
  7521         if (beg < BEGV)
  7522           beg = BEGV;
  7523         IT_CHARPOS (*it) = beg;
  7524         IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
  7525       }
  7526     }
  7527 
  7528   it->continuation_lines_width = 0;
  7529 
  7530   eassert (IT_CHARPOS (*it) >= BEGV);
  7531   eassert (it->medium_narrowing_begv > 0 /* long-line optimizations: all bets off */
  7532            || IT_CHARPOS (*it) == BEGV
  7533            || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7534   CHECK_IT (it);
  7535 }
  7536 
  7537 
  7538 /* Reseat iterator IT at the previous visible line start.  Skip
  7539    invisible text that is so either due to text properties or due to
  7540    selective display.  At the end, update IT's overlay information,
  7541    face information etc.  */
  7542 
  7543 void
  7544 reseat_at_previous_visible_line_start (struct it *it)
  7545 {
  7546   back_to_previous_visible_line_start (it);
  7547   reseat (it, it->current.pos, true);
  7548   CHECK_IT (it);
  7549 }
  7550 
  7551 
  7552 /* Reseat iterator IT on the next visible line start in the current
  7553    buffer.  ON_NEWLINE_P means position IT on the newline
  7554    preceding the line start.  Skip over invisible text that is so
  7555    because of selective display.  Compute faces, overlays etc at the
  7556    new position.  Note that this function does not skip over text that
  7557    is invisible because of text properties.  */
  7558 
  7559 static void
  7560 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
  7561 {
  7562   bool skipped_p = false;
  7563   struct bidi_it bidi_it_prev;
  7564   bool newline_found_p
  7565     = forward_to_next_line_start (it, &skipped_p,
  7566                                   on_newline_p ? &bidi_it_prev : NULL);
  7567 
  7568   /* Skip over lines that are invisible because they are indented
  7569      more than the value of IT->selective.  */
  7570   if (it->selective > 0)
  7571     while (IT_CHARPOS (*it) < ZV
  7572            && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7573                                  it->selective))
  7574       {
  7575         eassert (IT_BYTEPOS (*it) == BEGV
  7576                  || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7577         newline_found_p =
  7578           forward_to_next_line_start (it, &skipped_p,
  7579                                       on_newline_p ? &bidi_it_prev : NULL);
  7580       }
  7581 
  7582   /* Position on the newline if that's what's requested.  */
  7583   if (on_newline_p && newline_found_p)
  7584     {
  7585       if (STRINGP (it->string))
  7586         {
  7587           if (IT_STRING_CHARPOS (*it) > 0)
  7588             {
  7589               if (!it->bidi_p)
  7590                 {
  7591                   --IT_STRING_CHARPOS (*it);
  7592                   --IT_STRING_BYTEPOS (*it);
  7593                 }
  7594               else
  7595                 {
  7596                   /* We need to restore the bidi iterator to the state
  7597                      it had on the newline, and resync the IT's
  7598                      position with that.  */
  7599                   it->bidi_it = bidi_it_prev;
  7600                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  7601                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  7602                 }
  7603             }
  7604         }
  7605       else if (IT_CHARPOS (*it) > BEGV)
  7606         {
  7607           if (!it->bidi_p)
  7608             {
  7609               --IT_CHARPOS (*it);
  7610               --IT_BYTEPOS (*it);
  7611             }
  7612           else
  7613             {
  7614               /* We need to restore the bidi iterator to the state it
  7615                  had on the newline and resync IT with that.  */
  7616               it->bidi_it = bidi_it_prev;
  7617               IT_CHARPOS (*it) = it->bidi_it.charpos;
  7618               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7619             }
  7620           reseat (it, it->current.pos, false);
  7621         }
  7622     }
  7623   else if (skipped_p)
  7624     reseat (it, it->current.pos, false);
  7625 
  7626   CHECK_IT (it);
  7627 }
  7628 
  7629 
  7630 
  7631 /***********************************************************************
  7632                    Changing an iterator's position
  7633 ***********************************************************************/
  7634 
  7635 /* Change IT's current position to POS in current_buffer.
  7636    If FORCE_P, always check for text properties at the new position.
  7637    Otherwise, text properties are only looked up if POS >=
  7638    IT->check_charpos of a property.  */
  7639 
  7640 static void
  7641 reseat (struct it *it, struct text_pos pos, bool force_p)
  7642 {
  7643   ptrdiff_t original_pos = IT_CHARPOS (*it);
  7644 
  7645   reseat_1 (it, pos, false);
  7646 
  7647   if (current_buffer->long_line_optimizations_p)
  7648     {
  7649       if (!it->medium_narrowing_begv)
  7650         {
  7651           it->medium_narrowing_begv
  7652             = get_medium_narrowing_begv (it->w, window_point (it->w));
  7653           it->medium_narrowing_zv
  7654             = get_medium_narrowing_zv (it->w, window_point (it->w));
  7655           it->large_narrowing_begv
  7656             = get_large_narrowing_begv (window_point (it->w));
  7657           it->large_narrowing_zv
  7658             = get_large_narrowing_zv (window_point (it->w));
  7659         }
  7660       else if ((pos.charpos < it->medium_narrowing_begv
  7661                 || pos.charpos > it->medium_narrowing_zv)
  7662                 && (!redisplaying_p || it->line_wrap == TRUNCATE))
  7663         {
  7664           it->medium_narrowing_begv
  7665             = get_medium_narrowing_begv (it->w, pos.charpos);
  7666           it->medium_narrowing_zv
  7667             = get_medium_narrowing_zv (it->w, pos.charpos);
  7668           it->large_narrowing_begv
  7669             = get_large_narrowing_begv (window_point (it->w));
  7670           it->large_narrowing_zv
  7671             = get_large_narrowing_zv (window_point (it->w));
  7672         }
  7673     }
  7674 
  7675   /* Determine where to check text properties.  Avoid doing it
  7676      where possible because text property lookup is very expensive.  */
  7677   if (force_p
  7678       || CHARPOS (pos) > it->stop_charpos
  7679       || CHARPOS (pos) < original_pos)
  7680     {
  7681       if (it->bidi_p)
  7682         {
  7683           /* For bidi iteration, we need to prime prev_stop and
  7684              base_level_stop with our best estimations.  */
  7685           /* Implementation note: Of course, POS is not necessarily a
  7686              stop position, so assigning prev_pos to it is a lie; we
  7687              should have called compute_stop_backwards.  However, if
  7688              the current buffer does not include any R2L characters,
  7689              that call would be a waste of cycles, because the
  7690              iterator will never move back, and thus never cross this
  7691              "fake" stop position.  So we delay that backward search
  7692              until the time we really need it, in next_element_from_buffer.  */
  7693           if (CHARPOS (pos) != it->prev_stop)
  7694             it->prev_stop = CHARPOS (pos);
  7695           if (CHARPOS (pos) < it->base_level_stop)
  7696             it->base_level_stop = 0; /* meaning it's unknown */
  7697           handle_stop (it);
  7698         }
  7699       else
  7700         {
  7701           handle_stop (it);
  7702           it->prev_stop = it->base_level_stop = 0;
  7703         }
  7704 
  7705     }
  7706 
  7707   CHECK_IT (it);
  7708 }
  7709 
  7710 
  7711 /* Change IT's buffer position to POS.  SET_STOP_P means set
  7712    IT->stop_pos to POS, also.  */
  7713 
  7714 static void
  7715 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
  7716 {
  7717   /* Don't call this function when scanning a C string.  */
  7718   eassert (it->s == NULL);
  7719 
  7720   /* POS must be a reasonable value.  */
  7721   eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
  7722 
  7723   it->current.pos = it->position = pos;
  7724   it->end_charpos = ZV;
  7725   it->dpvec = NULL;
  7726   it->current.dpvec_index = -1;
  7727   it->current.overlay_string_index = -1;
  7728   IT_STRING_CHARPOS (*it) = -1;
  7729   IT_STRING_BYTEPOS (*it) = -1;
  7730   it->string = Qnil;
  7731   it->method = GET_FROM_BUFFER;
  7732   it->object = it->w->contents;
  7733   it->area = TEXT_AREA;
  7734   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  7735   it->sp = 0;
  7736   it->string_from_display_prop_p = false;
  7737   it->string_from_prefix_prop_p = false;
  7738 
  7739   it->from_disp_prop_p = false;
  7740   it->face_before_selective_p = false;
  7741   if (it->bidi_p)
  7742     {
  7743       bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7744                     &it->bidi_it);
  7745       bidi_unshelve_cache (NULL, false);
  7746       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7747       it->bidi_it.string.s = NULL;
  7748       it->bidi_it.string.lstring = Qnil;
  7749       it->bidi_it.string.bufpos = 0;
  7750       it->bidi_it.string.from_disp_str = false;
  7751       it->bidi_it.string.unibyte = false;
  7752       it->bidi_it.w = it->w;
  7753     }
  7754 
  7755   if (set_stop_p)
  7756     {
  7757       it->stop_charpos = CHARPOS (pos);
  7758       it->base_level_stop = CHARPOS (pos);
  7759     }
  7760   /* This make the information stored in it->cmp_it invalidate.  */
  7761   it->cmp_it.id = -1;
  7762   it->min_width_property = Qnil;
  7763 }
  7764 
  7765 
  7766 /* Set up IT for displaying a string, starting at CHARPOS in window W.
  7767    If S is non-null, it is a C string to iterate over.  Otherwise,
  7768    STRING gives a Lisp string to iterate over.
  7769 
  7770    If PRECISION > 0, don't return more then PRECISION number of
  7771    characters from the string.
  7772 
  7773    If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
  7774    characters have been returned.  FIELD_WIDTH < 0 means an infinite
  7775    field width.
  7776 
  7777    MULTIBYTE = 0 means disable processing of multibyte characters,
  7778    MULTIBYTE > 0 means enable it,
  7779    MULTIBYTE < 0 means use IT->multibyte_p.
  7780 
  7781    IT must be initialized via a prior call to init_iterator before
  7782    calling this function.  */
  7783 
  7784 static void
  7785 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
  7786                   ptrdiff_t charpos, ptrdiff_t precision, int field_width,
  7787                   int multibyte)
  7788 {
  7789   /* No text property checks performed by default, but see below.  */
  7790   it->stop_charpos = -1;
  7791 
  7792   /* Set iterator position and end position.  */
  7793   memset (&it->current, 0, sizeof it->current);
  7794   it->current.overlay_string_index = -1;
  7795   it->current.dpvec_index = -1;
  7796   eassert (charpos >= 0);
  7797 
  7798   /* If STRING is specified, use its multibyteness, otherwise use the
  7799      setting of MULTIBYTE, if specified.  */
  7800   if (multibyte >= 0)
  7801     it->multibyte_p = multibyte > 0;
  7802 
  7803   /* Bidirectional reordering of strings is controlled by the default
  7804      value of bidi-display-reordering.  Don't try to reorder while
  7805      loading loadup.el, as the necessary character property tables are
  7806      not yet available.  */
  7807   it->bidi_p =
  7808     !redisplay__inhibit_bidi
  7809     && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
  7810 
  7811   if (s == NULL)
  7812     {
  7813       eassert (STRINGP (string));
  7814       it->string = string;
  7815       it->s = NULL;
  7816       it->end_charpos = it->string_nchars = SCHARS (string);
  7817       it->method = GET_FROM_STRING;
  7818       it->current.string_pos = string_pos (charpos, string);
  7819 
  7820       if (it->bidi_p)
  7821         {
  7822           it->bidi_it.string.lstring = string;
  7823           it->bidi_it.string.s = NULL;
  7824           it->bidi_it.string.schars = it->end_charpos;
  7825           it->bidi_it.string.bufpos = 0;
  7826           it->bidi_it.string.from_disp_str = false;
  7827           it->bidi_it.string.unibyte = !it->multibyte_p;
  7828           it->bidi_it.w = it->w;
  7829           bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
  7830                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  7831         }
  7832     }
  7833   else
  7834     {
  7835       it->s = (const unsigned char *) s;
  7836       it->string = Qnil;
  7837 
  7838       /* Note that we use IT->current.pos, not it->current.string_pos,
  7839          for displaying C strings.  */
  7840       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  7841       if (it->multibyte_p)
  7842         {
  7843           it->current.pos = c_string_pos (charpos, s, true);
  7844           it->end_charpos = it->string_nchars = number_of_chars (s, true);
  7845         }
  7846       else
  7847         {
  7848           IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
  7849           it->end_charpos = it->string_nchars = strlen (s);
  7850         }
  7851 
  7852       if (it->bidi_p)
  7853         {
  7854           it->bidi_it.string.lstring = Qnil;
  7855           it->bidi_it.string.s = (const unsigned char *) s;
  7856           it->bidi_it.string.schars = it->end_charpos;
  7857           it->bidi_it.string.bufpos = 0;
  7858           it->bidi_it.string.from_disp_str = false;
  7859           it->bidi_it.string.unibyte = !it->multibyte_p;
  7860           it->bidi_it.w = it->w;
  7861           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7862                         &it->bidi_it);
  7863         }
  7864       it->method = GET_FROM_C_STRING;
  7865     }
  7866 
  7867   /* PRECISION > 0 means don't return more than PRECISION characters
  7868      from the string.  */
  7869   if (precision > 0 && it->end_charpos - charpos > precision)
  7870     {
  7871       it->end_charpos = it->string_nchars = charpos + precision;
  7872       if (it->bidi_p)
  7873         it->bidi_it.string.schars = it->end_charpos;
  7874     }
  7875 
  7876   /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
  7877      characters have been returned.  FIELD_WIDTH == 0 means don't pad,
  7878      FIELD_WIDTH < 0 means infinite field width.  This is useful for
  7879      padding with `-' at the end of a mode line.  */
  7880   if (field_width < 0)
  7881     field_width = DISP_INFINITY;
  7882   /* Implementation note: We deliberately don't enlarge
  7883      it->bidi_it.string.schars here to fit it->end_charpos, because
  7884      the bidi iterator cannot produce characters out of thin air.  */
  7885   if (field_width > it->end_charpos - charpos)
  7886     it->end_charpos = charpos + field_width;
  7887 
  7888   /* Use the standard display table for displaying strings.  */
  7889   if (DISP_TABLE_P (Vstandard_display_table))
  7890     it->dp = XCHAR_TABLE (Vstandard_display_table);
  7891 
  7892   it->stop_charpos = charpos;
  7893   it->prev_stop = charpos;
  7894   it->base_level_stop = 0;
  7895   if (it->bidi_p)
  7896     {
  7897       it->bidi_it.first_elt = true;
  7898       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7899       it->bidi_it.disp_pos = -1;
  7900     }
  7901   if (s == NULL && it->multibyte_p)
  7902     {
  7903       ptrdiff_t endpos = SCHARS (it->string);
  7904       if (endpos > it->end_charpos)
  7905         endpos = it->end_charpos;
  7906       composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
  7907                                     it->string, true);
  7908     }
  7909   CHECK_IT (it);
  7910 }
  7911 
  7912 
  7913 
  7914 /***********************************************************************
  7915                               Iteration
  7916 ***********************************************************************/
  7917 
  7918 /* Map enum it_method value to corresponding next_element_from_* function.  */
  7919 
  7920 typedef bool (*next_element_function) (struct it *);
  7921 
  7922 static next_element_function const get_next_element[NUM_IT_METHODS] =
  7923 {
  7924   next_element_from_buffer,
  7925   next_element_from_display_vector,
  7926   next_element_from_string,
  7927   next_element_from_c_string,
  7928   next_element_from_image,
  7929   next_element_from_stretch,
  7930   next_element_from_xwidget,
  7931 };
  7932 
  7933 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
  7934 
  7935 
  7936 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
  7937    (possibly with the following characters).
  7938 
  7939   Note: we pass -1 as the "resolved bidi level" when the iterator
  7940   doesn't have the bidi_p flag set, because in that case we really
  7941   don't know what is the directionality of the text, so we leave it to
  7942   the shaping engine to figure that out.  */
  7943 
  7944 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS)                 \
  7945   ((IT)->cmp_it.id >= 0                                                 \
  7946    || ((IT)->cmp_it.stop_pos == (CHARPOS)                               \
  7947        && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS,       \
  7948                                  END_CHARPOS, (IT)->w,                  \
  7949                                  (IT)->bidi_p                           \
  7950                                  ? (IT)->bidi_it.resolved_level         \
  7951                                  : -1,                                  \
  7952                                  FACE_FROM_ID_OR_NULL ((IT)->f,         \
  7953                                                        (IT)->face_id),  \
  7954                                  (IT)->string)))
  7955 
  7956 
  7957 /* Lookup the char-table Vglyphless_char_display for character C (-1
  7958    if we want information for no-font case), and return the display
  7959    method symbol.  By side-effect, update it->what and
  7960    it->glyphless_method.  This function is called from
  7961    get_next_display_element for each character element, and from
  7962    gui_produce_glyphs when no suitable font was found.  */
  7963 
  7964 Lisp_Object
  7965 lookup_glyphless_char_display (int c, struct it *it)
  7966 {
  7967   Lisp_Object glyphless_method = Qnil;
  7968 
  7969   if (CHAR_TABLE_P (Vglyphless_char_display)
  7970       && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
  7971     {
  7972       if (c >= 0)
  7973         glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
  7974       else
  7975         glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
  7976 
  7977       if (CONSP (glyphless_method))
  7978         glyphless_method = FRAME_WINDOW_P (it->f)
  7979           ? XCAR (glyphless_method)
  7980           : XCDR (glyphless_method);
  7981     }
  7982 
  7983  retry:
  7984   if (NILP (glyphless_method))
  7985     {
  7986       if (c >= 0)
  7987         /* The default is to display the character by a proper font.  */
  7988         return Qnil;
  7989       /* The default for the no-font case is to display an empty box.  */
  7990       glyphless_method = Qempty_box;
  7991     }
  7992   if (EQ (glyphless_method, Qzero_width))
  7993     {
  7994       if (c >= 0)
  7995         return glyphless_method;
  7996       /* This method can't be used for the no-font case.  */
  7997       glyphless_method = Qempty_box;
  7998     }
  7999   if (EQ (glyphless_method, Qthin_space))
  8000     it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
  8001   else if (EQ (glyphless_method, Qempty_box))
  8002     it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  8003   else if (EQ (glyphless_method, Qhex_code))
  8004     it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
  8005   else if (STRINGP (glyphless_method))
  8006     it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
  8007   else
  8008     {
  8009       /* Invalid value.  We use the default method.  */
  8010       glyphless_method = Qnil;
  8011       goto retry;
  8012     }
  8013   it->what = IT_GLYPHLESS;
  8014   return glyphless_method;
  8015 }
  8016 
  8017 /* Merge escape glyph face and cache the result.  */
  8018 
  8019 static struct frame *last_escape_glyph_frame = NULL;
  8020 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8021 static int last_escape_glyph_merged_face_id = 0;
  8022 
  8023 static int
  8024 merge_escape_glyph_face (struct it *it)
  8025 {
  8026   int face_id;
  8027 
  8028   if (it->f == last_escape_glyph_frame
  8029       && it->face_id == last_escape_glyph_face_id)
  8030     face_id = last_escape_glyph_merged_face_id;
  8031   else
  8032     {
  8033       /* Merge the `escape-glyph' face into the current face.  */
  8034       face_id = merge_faces (it->w, Qescape_glyph, 0, it->face_id);
  8035       last_escape_glyph_frame = it->f;
  8036       last_escape_glyph_face_id = it->face_id;
  8037       last_escape_glyph_merged_face_id = face_id;
  8038     }
  8039   return face_id;
  8040 }
  8041 
  8042 /* Likewise for glyphless glyph face.  */
  8043 
  8044 static struct frame *last_glyphless_glyph_frame = NULL;
  8045 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8046 static int last_glyphless_glyph_merged_face_id = 0;
  8047 
  8048 int
  8049 merge_glyphless_glyph_face (struct it *it)
  8050 {
  8051   int face_id;
  8052 
  8053   if (it->f == last_glyphless_glyph_frame
  8054       && it->face_id == last_glyphless_glyph_face_id)
  8055     face_id = last_glyphless_glyph_merged_face_id;
  8056   else
  8057     {
  8058       /* Merge the `glyphless-char' face into the current face.  */
  8059       face_id = merge_faces (it->w, Qglyphless_char, 0, it->face_id);
  8060       last_glyphless_glyph_frame = it->f;
  8061       last_glyphless_glyph_face_id = it->face_id;
  8062       last_glyphless_glyph_merged_face_id = face_id;
  8063     }
  8064   return face_id;
  8065 }
  8066 
  8067 /* Forget the `escape-glyph' and `glyphless-char' faces.  This should
  8068    be called before redisplaying windows, and when the frame's face
  8069    cache is freed.  */
  8070 void
  8071 forget_escape_and_glyphless_faces (void)
  8072 {
  8073   last_escape_glyph_frame = NULL;
  8074   last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8075   last_glyphless_glyph_frame = NULL;
  8076   last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8077 }
  8078 
  8079 /* Load IT's display element fields with information about the next
  8080    display element from the current position of IT.  Value is false if
  8081    end of buffer (or C string) is reached.  */
  8082 
  8083 static bool
  8084 get_next_display_element (struct it *it)
  8085 {
  8086   /* True means that we found a display element.  False means that
  8087      we hit the end of what we iterate over.  Performance note: the
  8088      function pointer `method' used here turns out to be faster than
  8089      using a sequence of if-statements.  */
  8090   bool success_p;
  8091 
  8092  get_next:
  8093   success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  8094 
  8095   if (it->what == IT_CHARACTER)
  8096     {
  8097       /* UAX#9, L4: "A character is depicted by a mirrored glyph if
  8098          and only if (a) the resolved directionality of that character
  8099          is R..."  */
  8100       /* FIXME: Do we need an exception for characters from display
  8101          tables?  */
  8102       if (it->bidi_p && it->bidi_it.type == STRONG_R
  8103           && !inhibit_bidi_mirroring)
  8104         it->c = bidi_mirror_char (it->c);
  8105       /* Map via display table or translate control characters.
  8106          IT->c, IT->len etc. have been set to the next character by
  8107          the function call above.  If we have a display table, and it
  8108          contains an entry for IT->c, translate it.  Don't do this if
  8109          IT->c itself comes from a display table, otherwise we could
  8110          end up in an infinite recursion.  (An alternative could be to
  8111          count the recursion depth of this function and signal an
  8112          error when a certain maximum depth is reached.)  Is it worth
  8113          it?  */
  8114       if (success_p && it->dpvec == NULL)
  8115         {
  8116           Lisp_Object dv;
  8117           struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
  8118           bool nonascii_space_p = false;
  8119           bool nonascii_hyphen_p = false;
  8120           int c = it->c;        /* This is the character to display.  */
  8121 
  8122           if (! it->multibyte_p && ! ASCII_CHAR_P (c))
  8123             {
  8124               eassert (SINGLE_BYTE_CHAR_P (c));
  8125               if (unibyte_display_via_language_environment)
  8126                 {
  8127                   c = DECODE_CHAR (unibyte, c);
  8128                   if (c < 0)
  8129                     c = BYTE8_TO_CHAR (it->c);
  8130                 }
  8131               else
  8132                 c = BYTE8_TO_CHAR (it->c);
  8133             }
  8134 
  8135           if (it->dp
  8136               && (dv = DISP_CHAR_VECTOR (it->dp, c),
  8137                   VECTORP (dv)))
  8138             {
  8139               struct Lisp_Vector *v = XVECTOR (dv);
  8140 
  8141               /* Return the first character from the display table
  8142                  entry, if not empty.  If empty, don't display the
  8143                  current character.  */
  8144               if (v->header.size)
  8145                 {
  8146                   it->dpvec_char_len = it->len;
  8147                   it->dpvec = v->contents;
  8148                   it->dpend = v->contents + v->header.size;
  8149                   it->current.dpvec_index = 0;
  8150                   it->dpvec_face_id = -1;
  8151                   it->saved_face_id = it->face_id;
  8152                   it->method = GET_FROM_DISPLAY_VECTOR;
  8153                   it->ellipsis_p = false;
  8154                 }
  8155               else
  8156                 {
  8157                   set_iterator_to_next (it, false);
  8158                 }
  8159               goto get_next;
  8160             }
  8161 
  8162           if (! NILP (lookup_glyphless_char_display (c, it)))
  8163             {
  8164               if (it->what == IT_GLYPHLESS)
  8165                 goto done;
  8166               /* Don't display this character.  */
  8167               set_iterator_to_next (it, false);
  8168               goto get_next;
  8169             }
  8170 
  8171           /* If `nobreak-char-display' is non-nil, we display
  8172              non-ASCII spaces and hyphens specially.  */
  8173           if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
  8174             {
  8175               if (blankp (c))
  8176                 nonascii_space_p = true;
  8177               else if (c == SOFT_HYPHEN || c == HYPHEN
  8178                        || c == NON_BREAKING_HYPHEN)
  8179                 nonascii_hyphen_p = true;
  8180             }
  8181 
  8182           /* Translate control characters into `\003' or `^C' form.
  8183              Control characters coming from a display table entry are
  8184              currently not translated because we use IT->dpvec to hold
  8185              the translation.  This could easily be changed but I
  8186              don't believe that it is worth doing.
  8187 
  8188              The characters handled by `nobreak-char-display' must be
  8189              translated too.
  8190 
  8191              Non-printable characters and raw-byte characters are also
  8192              translated to octal or hexadecimal form.  */
  8193           if (((c < ' ' || c == 127) /* ASCII control chars.  */
  8194                ? (it->area != TEXT_AREA
  8195                   /* In mode line, treat \n, \t like other crl chars.  */
  8196                   || (c != '\t'
  8197                       && it->glyph_row
  8198                       && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
  8199                   || (c != '\n' && c != '\t'))
  8200                : (nonascii_space_p
  8201                   || nonascii_hyphen_p
  8202                   || CHAR_BYTE8_P (c)
  8203                   || ! CHAR_PRINTABLE_P (c))))
  8204             {
  8205               /* C is a control character, non-ASCII space/hyphen,
  8206                  raw-byte, or a non-printable character which must be
  8207                  displayed either as '\003' or as `^C' where the '\\'
  8208                  and '^' can be defined in the display table.  Fill
  8209                  IT->ctl_chars with glyphs for what we have to
  8210                  display.  Then, set IT->dpvec to these glyphs.  */
  8211               Lisp_Object gc;
  8212               int ctl_len;
  8213               int face_id;
  8214               int lface_id = 0;
  8215               int escape_glyph;
  8216 
  8217               /* Handle control characters with ^.  */
  8218 
  8219               if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
  8220                 {
  8221                   int g;
  8222 
  8223                   g = '^';           /* default glyph for Control */
  8224                   /* Set IT->ctl_chars[0] to the glyph for `^'.  */
  8225                   if (it->dp
  8226                       && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8227                     {
  8228                       g = GLYPH_CODE_CHAR (gc);
  8229                       lface_id = GLYPH_CODE_FACE (gc);
  8230                     }
  8231 
  8232                   face_id = (lface_id
  8233                              ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8234                              : merge_escape_glyph_face (it));
  8235 
  8236                   XSETINT (it->ctl_chars[0], g);
  8237                   XSETINT (it->ctl_chars[1], c ^ 0100);
  8238                   ctl_len = 2;
  8239                   goto display_control;
  8240                 }
  8241 
  8242               /* Handle non-ascii space in the mode where it only gets
  8243                  highlighting.  */
  8244 
  8245               if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
  8246                 {
  8247                   /* Merge `nobreak-space' into the current face.  */
  8248                   face_id = merge_faces (it->w, Qnobreak_space, 0,
  8249                                          it->face_id);
  8250                   XSETINT (it->ctl_chars[0],
  8251                            nobreak_char_ascii_display ? ' ' : it->c);
  8252                   ctl_len = 1;
  8253                   goto display_control;
  8254                 }
  8255 
  8256               /* Handle non-ascii hyphens in the mode where it only
  8257                  gets highlighting.  */
  8258 
  8259               if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
  8260                 {
  8261                   /* Merge `nobreak-space' into the current face.  */
  8262                   face_id = merge_faces (it->w, Qnobreak_hyphen, 0,
  8263                                          it->face_id);
  8264                   XSETINT (it->ctl_chars[0],
  8265                            nobreak_char_ascii_display ? '-' : it->c);
  8266                   ctl_len = 1;
  8267                   goto display_control;
  8268                 }
  8269 
  8270               /* Handle sequences that start with the "escape glyph".  */
  8271 
  8272               /* the default escape glyph is \.  */
  8273               escape_glyph = '\\';
  8274 
  8275               if (it->dp
  8276                   && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8277                 {
  8278                   escape_glyph = GLYPH_CODE_CHAR (gc);
  8279                   lface_id = GLYPH_CODE_FACE (gc);
  8280                 }
  8281 
  8282               face_id = (lface_id
  8283                          ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8284                          : merge_escape_glyph_face (it));
  8285 
  8286               /* Draw non-ASCII space/hyphen with escape glyph: */
  8287 
  8288               if (nonascii_space_p || nonascii_hyphen_p)
  8289                 {
  8290                   XSETINT (it->ctl_chars[0], escape_glyph);
  8291                   XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
  8292                   ctl_len = 2;
  8293                   goto display_control;
  8294                 }
  8295 
  8296               {
  8297                 char str[10];
  8298                 int len, i;
  8299 
  8300                 if (CHAR_BYTE8_P (c))
  8301                   /* Display \200 or \x80 instead of \17777600.  */
  8302                   c = CHAR_TO_BYTE8 (c);
  8303                 const char *format_string = display_raw_bytes_as_hex
  8304                                             ? "x%02x"
  8305                                             : "%03o";
  8306                 len = sprintf (str, format_string, c + 0u);
  8307 
  8308                 XSETINT (it->ctl_chars[0], escape_glyph);
  8309                 for (i = 0; i < len; i++)
  8310                   XSETINT (it->ctl_chars[i + 1], str[i]);
  8311                 ctl_len = len + 1;
  8312               }
  8313 
  8314             display_control:
  8315               /* Set up IT->dpvec and return first character from it.  */
  8316               it->dpvec_char_len = it->len;
  8317               it->dpvec = it->ctl_chars;
  8318               it->dpend = it->dpvec + ctl_len;
  8319               it->current.dpvec_index = 0;
  8320               it->dpvec_face_id = face_id;
  8321               it->saved_face_id = it->face_id;
  8322               it->method = GET_FROM_DISPLAY_VECTOR;
  8323               it->ellipsis_p = false;
  8324               goto get_next;
  8325             }
  8326           it->char_to_display = c;
  8327         }
  8328       else if (success_p)
  8329         {
  8330           it->char_to_display = it->c;
  8331         }
  8332     }
  8333 
  8334 #ifdef HAVE_WINDOW_SYSTEM
  8335   /* Adjust face id for a multibyte character.  There are no multibyte
  8336      character in unibyte text.  */
  8337   if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
  8338       && it->multibyte_p
  8339       && success_p
  8340       && FRAME_WINDOW_P (it->f))
  8341     {
  8342       struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8343 
  8344       /* It shouldn't happen, ever, that FACE is NULL here, but
  8345          evidently some faulty fonts/fontsets can sometimes cause it.
  8346          In that case, we punt and consider the stuff undisplayable.  */
  8347       if (!face)
  8348         {
  8349           it->what = IT_GLYPHLESS;
  8350           it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  8351         }
  8352       else if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
  8353         {
  8354           /* Automatic composition with glyph-string.   */
  8355           Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
  8356 
  8357           it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
  8358         }
  8359       else
  8360         {
  8361           ptrdiff_t pos = (it->s ? -1
  8362                      : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
  8363                      : IT_CHARPOS (*it));
  8364           int c;
  8365 
  8366           if (it->what == IT_CHARACTER)
  8367             c = it->char_to_display;
  8368           else
  8369             {
  8370               struct composition *cmp = composition_table[it->cmp_it.id];
  8371               int i;
  8372 
  8373               c = ' ';
  8374               for (i = 0; i < cmp->glyph_len; i++)
  8375                 /* TAB in a composition means display glyphs with
  8376                    padding space on the left or right.  */
  8377                 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
  8378                   break;
  8379             }
  8380           it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
  8381         }
  8382     }
  8383 #endif  /* HAVE_WINDOW_SYSTEM */
  8384 
  8385  done:
  8386   /* Is this character the last one of a run of characters with
  8387      box?  If yes, set IT->end_of_box_run_p to true.  */
  8388   if (it->face_box_p
  8389       && it->s == NULL)
  8390     {
  8391       if (it->method == GET_FROM_STRING && it->sp)
  8392         {
  8393           int face_id = underlying_face_id (it);
  8394           struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
  8395 
  8396           if (face)
  8397             {
  8398               if (face->box == FACE_NO_BOX)
  8399                 {
  8400                   /* If the box comes from face properties in a
  8401                      display string, check faces in that string.  */
  8402                   int string_face_id = face_after_it_pos (it);
  8403                   if (FACE_FROM_ID (it->f, string_face_id)->box == FACE_NO_BOX)
  8404                     it->end_of_box_run_p = true;
  8405                 }
  8406               /* Otherwise, the box comes from the underlying face.
  8407                  If this is the last string character displayed, check
  8408                  the next buffer location.  */
  8409               else if (((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
  8410                         /* For a composition, see if the string ends
  8411                            at the last character included in the
  8412                            composition.  */
  8413                         || (it->what == IT_COMPOSITION
  8414                             && (IT_STRING_CHARPOS (*it) + it->cmp_it.nchars
  8415                                 >= SCHARS (it->string))))
  8416                        /* n_overlay_strings is unreliable unless
  8417                           overlay_string_index is non-negative.  */
  8418                        && ((it->current.overlay_string_index >= 0
  8419                             && (it->current.overlay_string_index
  8420                                 == it->n_overlay_strings - 1))
  8421                            /* A string from display property.  */
  8422                            || it->from_disp_prop_p))
  8423                 {
  8424                   ptrdiff_t ignore;
  8425                   int next_face_id;
  8426                   bool text_from_string = false;
  8427                   /* Normally, the next buffer location is stored in
  8428                      IT->current.pos...  */
  8429                   struct text_pos pos = it->current.pos;
  8430 
  8431                   /* ...but for a string from a display property, the
  8432                      next buffer position is stored in the 'position'
  8433                      member of the iteration stack slot below the
  8434                      current one, see handle_single_display_spec.  By
  8435                      contrast, it->current.pos was not yet updated to
  8436                      point to that buffer position; that will happen
  8437                      in pop_it, after we finish displaying the current
  8438                      string.  Note that we already checked above that
  8439                      it->sp is positive, so subtracting one from it is
  8440                      safe.  */
  8441                   if (it->from_disp_prop_p)
  8442                     {
  8443                       int stackp = it->sp - 1;
  8444 
  8445                       /* Find the stack level with data from buffer.  */
  8446                       while (stackp >= 0
  8447                              && STRINGP ((it->stack + stackp)->string))
  8448                         stackp--;
  8449                       if (stackp < 0)
  8450                         {
  8451                           /* If no stack slot was found for iterating
  8452                              a buffer, we are displaying text from a
  8453                              string, most probably the mode line or
  8454                              the header line, and that string has a
  8455                              display string on some of its
  8456                              characters.  */
  8457                           text_from_string = true;
  8458                           pos = it->stack[it->sp - 1].position;
  8459                         }
  8460                       else
  8461                         pos = (it->stack + stackp)->position;
  8462                     }
  8463                   else
  8464                     INC_TEXT_POS (pos, it->multibyte_p);
  8465 
  8466                   if (text_from_string)
  8467                     {
  8468                       Lisp_Object base_string = it->stack[it->sp - 1].string;
  8469 
  8470                       if (CHARPOS (pos) >= SCHARS (base_string) - 1)
  8471                         it->end_of_box_run_p = true;
  8472                       else
  8473                         {
  8474                           next_face_id
  8475                             = face_at_string_position (it->w, base_string,
  8476                                                        CHARPOS (pos), 0,
  8477                                                        &ignore, face_id,
  8478                                                        false, 0);
  8479                           if (FACE_FROM_ID (it->f, next_face_id)->box
  8480                               == FACE_NO_BOX)
  8481                             it->end_of_box_run_p = true;
  8482                         }
  8483                     }
  8484                   else if (CHARPOS (pos) >= ZV)
  8485                     it->end_of_box_run_p = true;
  8486                   else
  8487                     {
  8488                       next_face_id =
  8489                         face_at_buffer_position (it->w, CHARPOS (pos),
  8490                                                  &ignore,
  8491                                                  CHARPOS (pos)
  8492                                                  + TEXT_PROP_DISTANCE_LIMIT,
  8493                                                  false, -1, 0);
  8494                       if (FACE_FROM_ID (it->f, next_face_id)->box
  8495                           == FACE_NO_BOX)
  8496                         it->end_of_box_run_p = true;
  8497                     }
  8498                 }
  8499             }
  8500         }
  8501       /* next_element_from_display_vector sets this flag according to
  8502          faces of the display vector glyphs, see there.  */
  8503       else if (it->method != GET_FROM_DISPLAY_VECTOR)
  8504         {
  8505           int face_id = face_after_it_pos (it);
  8506           if (face_id != it->face_id
  8507               && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX)
  8508             it->end_of_box_run_p = true;
  8509         }
  8510     }
  8511   /* If we reached the end of the object we've been iterating (e.g., a
  8512      display string or an overlay string), and there's something on
  8513      IT->stack, proceed with what's on the stack.  It doesn't make
  8514      sense to return false if there's unprocessed stuff on the stack,
  8515      because otherwise that stuff will never be displayed.  */
  8516   if (!success_p && it->sp > 0)
  8517     {
  8518       set_iterator_to_next (it, false);
  8519       success_p = get_next_display_element (it);
  8520     }
  8521 
  8522   /* Value is false if end of buffer or string reached.  */
  8523   return success_p;
  8524 }
  8525 
  8526 
  8527 /* Move IT to the next display element.
  8528 
  8529    RESEAT_P means if called on a newline in buffer text,
  8530    skip to the next visible line start.
  8531 
  8532    Functions get_next_display_element and set_iterator_to_next are
  8533    separate because I find this arrangement easier to handle than a
  8534    get_next_display_element function that also increments IT's
  8535    position.  The way it is we can first look at an iterator's current
  8536    display element, decide whether it fits on a line, and if it does,
  8537    increment the iterator position.  The other way around we probably
  8538    would either need a flag indicating whether the iterator has to be
  8539    incremented the next time, or we would have to implement a
  8540    decrement position function which would not be easy to write.  */
  8541 
  8542 void
  8543 set_iterator_to_next (struct it *it, bool reseat_p)
  8544 {
  8545 
  8546   if (max_redisplay_ticks > 0)
  8547     update_redisplay_ticks (1, it->w);
  8548 
  8549   switch (it->method)
  8550     {
  8551     case GET_FROM_BUFFER:
  8552       /* The current display element of IT is a character from
  8553          current_buffer.  Advance in the buffer, and maybe skip over
  8554          invisible lines that are so because of selective display.  */
  8555       if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
  8556         reseat_at_next_visible_line_start (it, false);
  8557       else if (it->cmp_it.id >= 0)
  8558         {
  8559           /* We are currently getting glyphs from a composition.  */
  8560           if (! it->bidi_p)
  8561             {
  8562               IT_CHARPOS (*it) += it->cmp_it.nchars;
  8563               IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  8564             }
  8565           else
  8566             {
  8567               int i;
  8568 
  8569               /* Update IT's char/byte positions to point to the first
  8570                  character of the next grapheme cluster, or to the
  8571                  character visually after the current composition.  */
  8572               for (i = 0; i < it->cmp_it.nchars; i++)
  8573                 bidi_move_to_visually_next (&it->bidi_it);
  8574               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8575               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8576             }
  8577 
  8578           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8579               && it->cmp_it.to < it->cmp_it.nglyphs)
  8580             {
  8581               /* Composition created while scanning forward.  Proceed
  8582                  to the next grapheme cluster.  */
  8583               it->cmp_it.from = it->cmp_it.to;
  8584             }
  8585           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8586                    && it->cmp_it.from > 0)
  8587             {
  8588               /* Composition created while scanning backward.  Proceed
  8589                  to the previous grapheme cluster.  */
  8590               it->cmp_it.to = it->cmp_it.from;
  8591             }
  8592           else
  8593             {
  8594               /* No more grapheme clusters in this composition.
  8595                  Find the next stop position.  */
  8596               ptrdiff_t stop = it->end_charpos;
  8597 
  8598               if (it->bidi_it.scan_dir < 0)
  8599                 /* Now we are scanning backward and don't know
  8600                    where to stop.  */
  8601                 stop = -1;
  8602               composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8603                                             IT_BYTEPOS (*it), stop, Qnil, true);
  8604             }
  8605         }
  8606       else
  8607         {
  8608           eassert (it->len != 0);
  8609 
  8610           if (!it->bidi_p)
  8611             {
  8612               IT_BYTEPOS (*it) += it->len;
  8613               IT_CHARPOS (*it) += 1;
  8614             }
  8615           else
  8616             {
  8617               int prev_scan_dir = it->bidi_it.scan_dir;
  8618               /* If this is a new paragraph, determine its base
  8619                  direction (a.k.a. its base embedding level).  */
  8620               if (it->bidi_it.new_paragraph)
  8621                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  8622                                      false);
  8623               bidi_move_to_visually_next (&it->bidi_it);
  8624               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8625               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8626               if (prev_scan_dir != it->bidi_it.scan_dir)
  8627                 {
  8628                   /* As the scan direction was changed, we must
  8629                      re-compute the stop position for composition.  */
  8630                   ptrdiff_t stop = it->end_charpos;
  8631                   if (it->bidi_it.scan_dir < 0)
  8632                     stop = -1;
  8633                   composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8634                                                 IT_BYTEPOS (*it), stop, Qnil,
  8635                                                 true);
  8636                 }
  8637             }
  8638           eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
  8639         }
  8640       break;
  8641 
  8642     case GET_FROM_C_STRING:
  8643       /* Current display element of IT is from a C string.  */
  8644       if (!it->bidi_p
  8645           /* If the string position is beyond string's end, it means
  8646              next_element_from_c_string is padding the string with
  8647              blanks, in which case we bypass the bidi iterator,
  8648              because it cannot deal with such virtual characters.  */
  8649           || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
  8650         {
  8651           IT_BYTEPOS (*it) += it->len;
  8652           IT_CHARPOS (*it) += 1;
  8653         }
  8654       else
  8655         {
  8656           bidi_move_to_visually_next (&it->bidi_it);
  8657           IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8658           IT_CHARPOS (*it) = it->bidi_it.charpos;
  8659         }
  8660       break;
  8661 
  8662     case GET_FROM_DISPLAY_VECTOR:
  8663       /* Current display element of IT is from a display table entry.
  8664          Advance in the display table definition.  Reset it to null if
  8665          end reached, and continue with characters from buffers/
  8666          strings.  */
  8667       ++it->current.dpvec_index;
  8668 
  8669       /* Restore face of the iterator to what they were before the
  8670          display vector entry (these entries may contain faces).  */
  8671       it->face_id = it->saved_face_id;
  8672 
  8673       if (it->dpvec + it->current.dpvec_index >= it->dpend)
  8674         {
  8675           bool recheck_faces = it->ellipsis_p;
  8676 
  8677           if (it->s)
  8678             it->method = GET_FROM_C_STRING;
  8679           else if (STRINGP (it->string))
  8680             it->method = GET_FROM_STRING;
  8681           else
  8682             {
  8683               it->method = GET_FROM_BUFFER;
  8684               it->object = it->w->contents;
  8685             }
  8686 
  8687           it->dpvec = NULL;
  8688           it->current.dpvec_index = -1;
  8689 
  8690           /* Skip over characters which were displayed via IT->dpvec.  */
  8691           if (it->dpvec_char_len < 0)
  8692             reseat_at_next_visible_line_start (it, true);
  8693           else if (it->dpvec_char_len > 0)
  8694             {
  8695               it->len = it->dpvec_char_len;
  8696               set_iterator_to_next (it, reseat_p);
  8697             }
  8698 
  8699           /* Maybe recheck faces after display vector.  */
  8700           if (recheck_faces)
  8701             {
  8702               if (it->method == GET_FROM_STRING)
  8703                 it->stop_charpos = IT_STRING_CHARPOS (*it);
  8704               else
  8705                 it->stop_charpos = IT_CHARPOS (*it);
  8706             }
  8707         }
  8708       break;
  8709 
  8710     case GET_FROM_STRING:
  8711       /* Current display element is a character from a Lisp string.  */
  8712       eassert (it->s == NULL && STRINGP (it->string));
  8713       /* Don't advance past string end.  These conditions are true
  8714          when set_iterator_to_next is called at the end of
  8715          get_next_display_element, in which case the Lisp string is
  8716          already exhausted, and all we want is pop the iterator
  8717          stack.  */
  8718       if (it->current.overlay_string_index >= 0)
  8719         {
  8720           /* This is an overlay string, so there's no padding with
  8721              spaces, and the number of characters in the string is
  8722              where the string ends.  */
  8723           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8724             goto consider_string_end;
  8725         }
  8726       else
  8727         {
  8728           /* Not an overlay string.  There could be padding, so test
  8729              against it->end_charpos.  */
  8730           if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  8731             goto consider_string_end;
  8732         }
  8733       if (it->cmp_it.id >= 0)
  8734         {
  8735           /* We are delivering display elements from a composition.
  8736              Update the string position past the grapheme cluster
  8737              we've just processed.  */
  8738           if (! it->bidi_p)
  8739             {
  8740               IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  8741               IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  8742             }
  8743           else
  8744             {
  8745               int i;
  8746 
  8747               for (i = 0; i < it->cmp_it.nchars; i++)
  8748                 bidi_move_to_visually_next (&it->bidi_it);
  8749               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8750               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8751             }
  8752 
  8753           /* Did we exhaust all the grapheme clusters of this
  8754              composition?  */
  8755           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8756               && (it->cmp_it.to < it->cmp_it.nglyphs))
  8757             {
  8758               /* Not all the grapheme clusters were processed yet;
  8759                  advance to the next cluster.  */
  8760               it->cmp_it.from = it->cmp_it.to;
  8761             }
  8762           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8763                    && it->cmp_it.from > 0)
  8764             {
  8765               /* Likewise: advance to the next cluster, but going in
  8766                  the reverse direction.  */
  8767               it->cmp_it.to = it->cmp_it.from;
  8768             }
  8769           else
  8770             {
  8771               /* This composition was fully processed; find the next
  8772                  candidate place for checking for composed
  8773                  characters.  */
  8774               /* Always limit string searches to the string length;
  8775                  any padding spaces are not part of the string, and
  8776                  there cannot be any compositions in that padding.  */
  8777               ptrdiff_t stop = SCHARS (it->string);
  8778 
  8779               if (it->bidi_p && it->bidi_it.scan_dir < 0)
  8780                 stop = -1;
  8781               else if (it->end_charpos < stop)
  8782                 {
  8783                   /* Cf. PRECISION in reseat_to_string: we might be
  8784                      limited in how many of the string characters we
  8785                      need to deliver.  */
  8786                   stop = it->end_charpos;
  8787                 }
  8788               composition_compute_stop_pos (&it->cmp_it,
  8789                                             IT_STRING_CHARPOS (*it),
  8790                                             IT_STRING_BYTEPOS (*it), stop,
  8791                                             it->string, true);
  8792             }
  8793         }
  8794       else
  8795         {
  8796           if (!it->bidi_p
  8797               /* If the string position is beyond string's end, it
  8798                  means next_element_from_string is padding the string
  8799                  with blanks, in which case we bypass the bidi
  8800                  iterator, because it cannot deal with such virtual
  8801                  characters.  */
  8802               || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
  8803             {
  8804               IT_STRING_BYTEPOS (*it) += it->len;
  8805               IT_STRING_CHARPOS (*it) += 1;
  8806             }
  8807           else
  8808             {
  8809               int prev_scan_dir = it->bidi_it.scan_dir;
  8810 
  8811               bidi_move_to_visually_next (&it->bidi_it);
  8812               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8813               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8814               /* If the scan direction changes, we may need to update
  8815                  the place where to check for composed characters.  */
  8816               if (prev_scan_dir != it->bidi_it.scan_dir)
  8817                 {
  8818                   ptrdiff_t stop = SCHARS (it->string);
  8819 
  8820                   if (it->bidi_it.scan_dir < 0)
  8821                     stop = -1;
  8822                   else if (it->end_charpos < stop)
  8823                     stop = it->end_charpos;
  8824 
  8825                   composition_compute_stop_pos (&it->cmp_it,
  8826                                                 IT_STRING_CHARPOS (*it),
  8827                                                 IT_STRING_BYTEPOS (*it), stop,
  8828                                                 it->string, true);
  8829                 }
  8830             }
  8831         }
  8832 
  8833     consider_string_end:
  8834 
  8835       if (it->current.overlay_string_index >= 0)
  8836         {
  8837           /* IT->string is an overlay string.  Advance to the
  8838              next, if there is one.  */
  8839           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8840             {
  8841               it->ellipsis_p = false;
  8842               next_overlay_string (it);
  8843               if (it->ellipsis_p)
  8844                 setup_for_ellipsis (it, 0);
  8845             }
  8846         }
  8847       else
  8848         {
  8849           /* IT->string is not an overlay string.  If we reached
  8850              its end, and there is something on IT->stack, proceed
  8851              with what is on the stack.  This can be either another
  8852              string, this time an overlay string, or a buffer.  */
  8853           if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
  8854               && it->sp > 0)
  8855             {
  8856               pop_it (it);
  8857               if (it->method == GET_FROM_STRING)
  8858                 goto consider_string_end;
  8859             }
  8860         }
  8861       break;
  8862 
  8863     case GET_FROM_IMAGE:
  8864     case GET_FROM_STRETCH:
  8865     case GET_FROM_XWIDGET:
  8866 
  8867       /* The position etc with which we have to proceed are on
  8868          the stack.  The position may be at the end of a string,
  8869          if the `display' property takes up the whole string.  */
  8870       eassert (it->sp > 0);
  8871       pop_it (it);
  8872       if (it->method == GET_FROM_STRING)
  8873         goto consider_string_end;
  8874       break;
  8875 
  8876     default:
  8877       /* There are no other methods defined, so this should be a bug.  */
  8878       emacs_abort ();
  8879     }
  8880 
  8881   eassert (it->method != GET_FROM_STRING
  8882            || (STRINGP (it->string)
  8883                && IT_STRING_CHARPOS (*it) >= 0));
  8884 }
  8885 
  8886 /* Load IT's display element fields with information about the next
  8887    display element which comes from a display table entry or from the
  8888    result of translating a control character to one of the forms `^C'
  8889    or `\003'.
  8890 
  8891    IT->dpvec holds the glyphs to return as characters.
  8892    IT->saved_face_id holds the face id before the display vector--it
  8893    is restored into IT->face_id in set_iterator_to_next.  */
  8894 
  8895 static bool
  8896 next_element_from_display_vector (struct it *it)
  8897 {
  8898   Lisp_Object gc;
  8899   int prev_face_id = it->face_id;
  8900   int next_face_id;
  8901 
  8902   /* Precondition.  */
  8903   eassert (it->dpvec && it->current.dpvec_index >= 0);
  8904 
  8905   it->face_id = it->saved_face_id;
  8906 
  8907   /* KFS: This code used to check ip->dpvec[0] instead of the current element.
  8908      That seemed totally bogus - so I changed it...  */
  8909   if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
  8910       && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
  8911     {
  8912       struct face *this_face, *prev_face, *next_face;
  8913 
  8914       it->c = GLYPH_CODE_CHAR (gc);
  8915       it->len = CHAR_BYTES (it->c);
  8916 
  8917       /* The entry may contain a face id to use.  Such a face id is
  8918          the id of a Lisp face, not a realized face.  A face id of
  8919          zero means no face is specified.  */
  8920       if (it->dpvec_face_id >= 0)
  8921         it->face_id = it->dpvec_face_id;
  8922       else
  8923         {
  8924           int lface_id = GLYPH_CODE_FACE (gc);
  8925           if (lface_id > 0)
  8926             it->face_id = merge_faces (it->w, Qt, lface_id,
  8927                                        it->saved_face_id);
  8928         }
  8929 
  8930       /* Glyphs in the display vector could have the box face, so we
  8931          need to set the related flags in the iterator, as
  8932          appropriate.  */
  8933       this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8934       prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  8935 
  8936       /* Is this character the first character of a box-face run?  */
  8937       it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
  8938                                 && (!prev_face
  8939                                     || prev_face->box == FACE_NO_BOX));
  8940 
  8941       /* For the last character of the box-face run, we need to look
  8942          either at the next glyph from the display vector, or at the
  8943          face we saw before the display vector.  */
  8944       next_face_id = it->saved_face_id;
  8945       if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
  8946         {
  8947           if (it->dpvec_face_id >= 0)
  8948             next_face_id = it->dpvec_face_id;
  8949           else
  8950             {
  8951               Lisp_Object gc = it->dpvec[it->current.dpvec_index + 1];
  8952               int lface_id = GLYPH_CODE_P (gc) ? GLYPH_CODE_FACE (gc) : 0;
  8953 
  8954               if (lface_id > 0)
  8955                 next_face_id = merge_faces (it->w, Qt, lface_id,
  8956                                             it->saved_face_id);
  8957             }
  8958         }
  8959       next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
  8960       if (this_face && this_face->box != FACE_NO_BOX
  8961           && (!next_face || next_face->box == FACE_NO_BOX))
  8962         it->end_of_box_run_p = true;
  8963       it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
  8964     }
  8965   else
  8966     /* Display table entry is invalid.  Return a space.  */
  8967     it->c = ' ', it->len = 1;
  8968 
  8969   /* Don't change position and object of the iterator here.  They are
  8970      still the values of the character that had this display table
  8971      entry or was translated, and that's what we want.  */
  8972   it->what = IT_CHARACTER;
  8973   return true;
  8974 }
  8975 
  8976 /* Get the first element of string/buffer in the visual order, after
  8977    being reseated to a new position in a string or a buffer.  */
  8978 static void
  8979 get_visually_first_element (struct it *it)
  8980 {
  8981   bool string_p = STRINGP (it->string) || it->s;
  8982   ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
  8983   ptrdiff_t bob;
  8984   ptrdiff_t obegv = BEGV;
  8985 
  8986   SET_WITH_NARROWED_BEGV (it, bob,
  8987                           string_p ? 0 :
  8988                           IT_CHARPOS (*it) < BEGV ? obegv : BEGV,
  8989                           it->medium_narrowing_begv);
  8990 
  8991   if (STRINGP (it->string))
  8992     {
  8993       it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
  8994       it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
  8995     }
  8996   else
  8997     {
  8998       it->bidi_it.charpos = IT_CHARPOS (*it);
  8999       it->bidi_it.bytepos = IT_BYTEPOS (*it);
  9000     }
  9001 
  9002   if (it->bidi_it.charpos == eob)
  9003     {
  9004       /* Nothing to do, but reset the FIRST_ELT flag, like
  9005          bidi_paragraph_init does, because we are not going to
  9006          call it.  */
  9007       it->bidi_it.first_elt = false;
  9008     }
  9009   else if (it->bidi_it.charpos == bob
  9010            || (!string_p
  9011                && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
  9012                    || FETCH_BYTE (it->bidi_it.bytepos) == '\n')))
  9013     {
  9014       /* If we are at the beginning of a line/string, we can produce
  9015          the next element right away.  */
  9016       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9017       bidi_move_to_visually_next (&it->bidi_it);
  9018     }
  9019   else
  9020     {
  9021       ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  9022 
  9023       /* We need to prime the bidi iterator starting at the line's or
  9024          string's beginning, before we will be able to produce the
  9025          next element.  */
  9026       if (string_p)
  9027         it->bidi_it.charpos = it->bidi_it.bytepos = 0;
  9028       else
  9029         SET_WITH_NARROWED_BEGV (it, it->bidi_it.charpos,
  9030                                 find_newline_no_quit (IT_CHARPOS (*it),
  9031                                                       IT_BYTEPOS (*it), -1,
  9032                                                       &it->bidi_it.bytepos),
  9033                                 it->medium_narrowing_begv);
  9034       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9035       do
  9036         {
  9037           /* Now return to buffer/string position where we were asked
  9038              to get the next display element, and produce that.  */
  9039           bidi_move_to_visually_next (&it->bidi_it);
  9040         }
  9041       while (it->bidi_it.bytepos != orig_bytepos
  9042              && it->bidi_it.charpos < eob);
  9043     }
  9044 
  9045   /*  Adjust IT's position information to where we ended up.  */
  9046   if (STRINGP (it->string))
  9047     {
  9048       IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  9049       IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  9050     }
  9051   else
  9052     {
  9053       IT_CHARPOS (*it) = it->bidi_it.charpos;
  9054       IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  9055     }
  9056 
  9057   if (STRINGP (it->string) || !it->s)
  9058     {
  9059       ptrdiff_t stop, charpos, bytepos;
  9060 
  9061       if (STRINGP (it->string))
  9062         {
  9063           eassert (!it->s);
  9064           stop = SCHARS (it->string);
  9065           if (stop > it->end_charpos)
  9066             stop = it->end_charpos;
  9067           charpos = IT_STRING_CHARPOS (*it);
  9068           bytepos = IT_STRING_BYTEPOS (*it);
  9069         }
  9070       else
  9071         {
  9072           stop = it->end_charpos;
  9073           charpos = IT_CHARPOS (*it);
  9074           bytepos = IT_BYTEPOS (*it);
  9075         }
  9076       if (it->bidi_it.scan_dir < 0)
  9077         stop = -1;
  9078       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
  9079                                     it->string, true);
  9080     }
  9081 }
  9082 
  9083 /* Load IT with the next display element from Lisp string IT->string.
  9084    IT->current.string_pos is the current position within the string.
  9085    If IT->current.overlay_string_index >= 0, the Lisp string is an
  9086    overlay string.  */
  9087 
  9088 static bool
  9089 next_element_from_string (struct it *it)
  9090 {
  9091   struct text_pos position;
  9092 
  9093   eassert (STRINGP (it->string));
  9094   eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
  9095   eassert (IT_STRING_CHARPOS (*it) >= 0);
  9096   position = it->current.string_pos;
  9097 
  9098   /* With bidi reordering, the character to display might not be the
  9099      character at IT_STRING_CHARPOS.  BIDI_IT.FIRST_ELT means
  9100      that we were reseat()ed to a new string, whose paragraph
  9101      direction is not known.  */
  9102   if (it->bidi_p && it->bidi_it.first_elt)
  9103     {
  9104       get_visually_first_element (it);
  9105       SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
  9106     }
  9107 
  9108   /* Time to check for invisible text?  */
  9109   if (IT_STRING_CHARPOS (*it) < it->end_charpos)
  9110     {
  9111       if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
  9112         {
  9113           if (!(!it->bidi_p
  9114                 || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9115                 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
  9116             {
  9117               /* With bidi non-linear iteration, we could find
  9118                  ourselves far beyond the last computed stop_charpos,
  9119                  with several other stop positions in between that we
  9120                  missed.  Scan them all now, in buffer's logical
  9121                  order, until we find and handle the last stop_charpos
  9122                  that precedes our current position.  */
  9123               handle_stop_backwards (it, it->stop_charpos);
  9124               return GET_NEXT_DISPLAY_ELEMENT (it);
  9125             }
  9126           else
  9127             {
  9128               if (it->bidi_p)
  9129                 {
  9130                   /* Take note of the stop position we just moved
  9131                      across, for when we will move back across it.  */
  9132                   it->prev_stop = it->stop_charpos;
  9133                   /* If we are at base paragraph embedding level, take
  9134                      note of the last stop position seen at this
  9135                      level.  */
  9136                   if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9137                     it->base_level_stop = it->stop_charpos;
  9138                 }
  9139               handle_stop (it);
  9140 
  9141               /* Since a handler may have changed IT->method, we must
  9142                  recurse here.  */
  9143               return GET_NEXT_DISPLAY_ELEMENT (it);
  9144             }
  9145         }
  9146       else if (it->bidi_p
  9147                /* If we are before prev_stop, we may have overstepped
  9148                   on our way backwards a stop_pos, and if so, we need
  9149                   to handle that stop_pos.  */
  9150                && IT_STRING_CHARPOS (*it) < it->prev_stop
  9151                /* We can sometimes back up for reasons that have nothing
  9152                   to do with bidi reordering.  E.g., compositions.  The
  9153                   code below is only needed when we are above the base
  9154                   embedding level, so test for that explicitly.  */
  9155                && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9156         {
  9157           /* If we lost track of base_level_stop, we have no better
  9158              place for handle_stop_backwards to start from than string
  9159              beginning.  This happens, e.g., when we were reseated to
  9160              the previous screenful of text by vertical-motion.  */
  9161           if (it->base_level_stop <= 0
  9162               || IT_STRING_CHARPOS (*it) < it->base_level_stop)
  9163             it->base_level_stop = 0;
  9164           handle_stop_backwards (it, it->base_level_stop);
  9165           return GET_NEXT_DISPLAY_ELEMENT (it);
  9166         }
  9167     }
  9168 
  9169   if (it->current.overlay_string_index >= 0)
  9170     {
  9171       /* Get the next character from an overlay string.  In overlay
  9172          strings, there is no field width or padding with spaces to
  9173          do.  */
  9174       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  9175         {
  9176           it->what = IT_EOB;
  9177           return false;
  9178         }
  9179       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9180                                 IT_STRING_BYTEPOS (*it),
  9181                                 it->bidi_it.scan_dir < 0
  9182                                 ? -1
  9183                                 : SCHARS (it->string))
  9184                && next_element_from_composition (it))
  9185         {
  9186           return true;
  9187         }
  9188       else if (STRING_MULTIBYTE (it->string))
  9189         {
  9190           const unsigned char *s = (SDATA (it->string)
  9191                                     + IT_STRING_BYTEPOS (*it));
  9192           it->c = check_char_and_length (s, &it->len);
  9193         }
  9194       else
  9195         {
  9196           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9197           it->len = 1;
  9198         }
  9199     }
  9200   else
  9201     {
  9202       /* Get the next character from a Lisp string that is not an
  9203          overlay string.  Such strings come from the mode line, for
  9204          example.  We may have to pad with spaces, or truncate the
  9205          string.  See also next_element_from_c_string.  */
  9206       if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  9207         {
  9208           it->what = IT_EOB;
  9209           return false;
  9210         }
  9211       else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
  9212         {
  9213           /* Pad with spaces.  */
  9214           it->c = ' ', it->len = 1;
  9215           CHARPOS (position) = BYTEPOS (position) = -1;
  9216         }
  9217       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9218                                 IT_STRING_BYTEPOS (*it),
  9219                                 it->bidi_it.scan_dir < 0
  9220                                 ? -1
  9221                                 : it->string_nchars)
  9222                && next_element_from_composition (it))
  9223         {
  9224           return true;
  9225         }
  9226       else if (STRING_MULTIBYTE (it->string))
  9227         {
  9228           const unsigned char *s = (SDATA (it->string)
  9229                                     + IT_STRING_BYTEPOS (*it));
  9230           it->c = check_char_and_length (s, &it->len);
  9231         }
  9232       else
  9233         {
  9234           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9235           it->len = 1;
  9236         }
  9237     }
  9238 
  9239   /* Record what we have and where it came from.  */
  9240   it->what = IT_CHARACTER;
  9241   it->object = it->string;
  9242   it->position = position;
  9243   return true;
  9244 }
  9245 
  9246 
  9247 /* Load IT with next display element from C string IT->s.
  9248    IT->string_nchars is the maximum number of characters to return
  9249    from the string.  IT->end_charpos may be greater than
  9250    IT->string_nchars when this function is called, in which case we
  9251    may have to return padding spaces.  Value is false if end of string
  9252    reached, including padding spaces.  */
  9253 
  9254 static bool
  9255 next_element_from_c_string (struct it *it)
  9256 {
  9257   bool success_p = true;
  9258 
  9259   eassert (it->s);
  9260   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
  9261   it->what = IT_CHARACTER;
  9262   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
  9263   it->object = make_fixnum (0);
  9264 
  9265   /* With bidi reordering, the character to display might not be the
  9266      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9267      we were reseated to a new string, whose paragraph direction is
  9268      not known.  */
  9269   if (it->bidi_p && it->bidi_it.first_elt)
  9270     get_visually_first_element (it);
  9271 
  9272   /* IT's position can be greater than IT->string_nchars in case a
  9273      field width or precision has been specified when the iterator was
  9274      initialized.  */
  9275   if (IT_CHARPOS (*it) >= it->end_charpos)
  9276     {
  9277       /* End of the game.  */
  9278       it->what = IT_EOB;
  9279       success_p = false;
  9280     }
  9281   else if (IT_CHARPOS (*it) >= it->string_nchars)
  9282     {
  9283       /* Pad with spaces.  */
  9284       it->c = ' ', it->len = 1;
  9285       BYTEPOS (it->position) = CHARPOS (it->position) = -1;
  9286     }
  9287   else if (it->multibyte_p)
  9288     it->c = check_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
  9289   else
  9290     it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
  9291 
  9292   return success_p;
  9293 }
  9294 
  9295 
  9296 /* Set up IT to return characters from an ellipsis, if appropriate.
  9297    The definition of the ellipsis glyphs may come from a display table
  9298    entry.  This function fills IT with the first glyph from the
  9299    ellipsis if an ellipsis is to be displayed.  */
  9300 
  9301 static bool
  9302 next_element_from_ellipsis (struct it *it)
  9303 {
  9304   if (it->selective_display_ellipsis_p)
  9305     setup_for_ellipsis (it, it->len);
  9306   else
  9307     {
  9308       /* The face at the current position may be different from the
  9309          face we find after the invisible text.  Remember what it
  9310          was in IT->saved_face_id, and signal that it's there by
  9311          setting face_before_selective_p.  */
  9312       it->saved_face_id = it->face_id;
  9313       it->method = GET_FROM_BUFFER;
  9314       it->object = it->w->contents;
  9315       reseat_at_next_visible_line_start (it, true);
  9316       it->face_before_selective_p = true;
  9317     }
  9318 
  9319   return GET_NEXT_DISPLAY_ELEMENT (it);
  9320 }
  9321 
  9322 
  9323 /* Deliver an image display element.  The iterator IT is already
  9324    filled with image information (done in handle_display_prop).  Value
  9325    is always true.  */
  9326 
  9327 
  9328 static bool
  9329 next_element_from_image (struct it *it)
  9330 {
  9331   it->what = IT_IMAGE;
  9332   return true;
  9333 }
  9334 
  9335 static bool
  9336 next_element_from_xwidget (struct it *it)
  9337 {
  9338   it->what = IT_XWIDGET;
  9339   return true;
  9340 }
  9341 
  9342 
  9343 /* Fill iterator IT with next display element from a stretch glyph
  9344    property.  IT->object is the value of the text property.  Value is
  9345    always true.  */
  9346 
  9347 static bool
  9348 next_element_from_stretch (struct it *it)
  9349 {
  9350   it->what = IT_STRETCH;
  9351   return true;
  9352 }
  9353 
  9354 /* Scan backwards from IT's current position until we find a stop
  9355    position, or until BEGV.  This is called when we find ourself
  9356    before both the last known prev_stop and base_level_stop while
  9357    reordering bidirectional text.  */
  9358 
  9359 static void
  9360 compute_stop_pos_backwards (struct it *it)
  9361 {
  9362   const int SCAN_BACK_LIMIT = 1000;
  9363   struct text_pos pos;
  9364   struct display_pos save_current = it->current;
  9365   struct text_pos save_position = it->position;
  9366   ptrdiff_t charpos = IT_CHARPOS (*it);
  9367   ptrdiff_t where_we_are = charpos;
  9368   ptrdiff_t save_stop_pos = it->stop_charpos;
  9369   ptrdiff_t save_end_pos = it->end_charpos;
  9370 
  9371   eassert (NILP (it->string) && !it->s);
  9372   eassert (it->bidi_p);
  9373   it->bidi_p = false;
  9374   do
  9375     {
  9376       it->end_charpos = min (charpos + 1, ZV);
  9377       charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
  9378       SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
  9379       reseat_1 (it, pos, false);
  9380       compute_stop_pos (it);
  9381       /* We must advance forward, right?  */
  9382       if (it->stop_charpos <= charpos)
  9383         emacs_abort ();
  9384     }
  9385   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
  9386 
  9387   if (it->stop_charpos <= where_we_are)
  9388     it->prev_stop = it->stop_charpos;
  9389   else
  9390     it->prev_stop = BEGV;
  9391   it->bidi_p = true;
  9392   it->current = save_current;
  9393   it->position = save_position;
  9394   it->stop_charpos = save_stop_pos;
  9395   it->end_charpos = save_end_pos;
  9396 }
  9397 
  9398 /* Scan forward from CHARPOS in the current buffer/string, until we
  9399    find a stop position > current IT's position.  Then handle the stop
  9400    position before that.  This is called when we bump into a stop
  9401    position while reordering bidirectional text.  CHARPOS should be
  9402    the last previously processed stop_pos (or BEGV/0, if none were
  9403    processed yet) whose position is less than IT's current
  9404    position.  */
  9405 
  9406 static void
  9407 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  9408 {
  9409   bool bufp = !STRINGP (it->string);
  9410   ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
  9411   struct display_pos save_current = it->current;
  9412   struct text_pos save_position = it->position;
  9413   struct composition_it save_cmp_it = it->cmp_it;
  9414   struct text_pos pos1;
  9415   ptrdiff_t next_stop;
  9416 
  9417   /* Scan in strict logical order.  */
  9418   eassert (it->bidi_p);
  9419   it->bidi_p = false;
  9420   do
  9421     {
  9422       it->prev_stop = charpos;
  9423       if (bufp)
  9424         {
  9425           SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
  9426           reseat_1 (it, pos1, false);
  9427         }
  9428       else
  9429         it->current.string_pos = string_pos (charpos, it->string);
  9430       compute_stop_pos (it);
  9431       /* We must advance forward, right?  */
  9432       if (it->stop_charpos <= it->prev_stop)
  9433         emacs_abort ();
  9434       charpos = it->stop_charpos;
  9435     }
  9436   while (charpos <= where_we_are);
  9437 
  9438   it->bidi_p = true;
  9439   it->current = save_current;
  9440   it->position = save_position;
  9441   it->cmp_it = save_cmp_it;
  9442   next_stop = it->stop_charpos;
  9443   it->stop_charpos = it->prev_stop;
  9444   handle_stop (it);
  9445   it->stop_charpos = next_stop;
  9446 }
  9447 
  9448 /* Load IT with the next display element from current_buffer.  Value
  9449    is false if end of buffer reached.  IT->stop_charpos is the next
  9450    position at which to stop and check for text properties or buffer
  9451    end.  */
  9452 
  9453 static bool
  9454 next_element_from_buffer (struct it *it)
  9455 {
  9456   bool success_p = true;
  9457 
  9458   eassert (IT_CHARPOS (*it) >= BEGV);
  9459   eassert (NILP (it->string) && !it->s);
  9460   eassert (!it->bidi_p
  9461            || (NILP (it->bidi_it.string.lstring)
  9462                && it->bidi_it.string.s == NULL));
  9463 
  9464   /* With bidi reordering, the character to display might not be the
  9465      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9466      we were reseat()ed to a new buffer position, which is potentially
  9467      a different paragraph.  */
  9468   if (it->bidi_p && it->bidi_it.first_elt)
  9469     {
  9470       get_visually_first_element (it);
  9471       SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9472     }
  9473 
  9474   if (IT_CHARPOS (*it) >= it->stop_charpos)
  9475     {
  9476       if (IT_CHARPOS (*it) >= it->end_charpos)
  9477         {
  9478           bool overlay_strings_follow_p;
  9479 
  9480           /* End of the game, except when overlay strings follow that
  9481              haven't been returned yet.  */
  9482           if (it->overlay_strings_at_end_processed_p)
  9483             overlay_strings_follow_p = false;
  9484           else
  9485             {
  9486               it->overlay_strings_at_end_processed_p = true;
  9487               overlay_strings_follow_p = get_overlay_strings (it, 0);
  9488             }
  9489 
  9490           if (overlay_strings_follow_p)
  9491             success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  9492           else
  9493             {
  9494               it->what = IT_EOB;
  9495               it->position = it->current.pos;
  9496               success_p = false;
  9497             }
  9498         }
  9499       else if (!(!it->bidi_p
  9500                  || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9501                  || IT_CHARPOS (*it) == it->stop_charpos))
  9502         {
  9503           /* With bidi non-linear iteration, we could find ourselves
  9504              far beyond the last computed stop_charpos, with several
  9505              other stop positions in between that we missed.  Scan
  9506              them all now, in buffer's logical order, until we find
  9507              and handle the last stop_charpos that precedes our
  9508              current position.  */
  9509           handle_stop_backwards (it, it->stop_charpos);
  9510           it->ignore_overlay_strings_at_pos_p = false;
  9511           return GET_NEXT_DISPLAY_ELEMENT (it);
  9512         }
  9513       else
  9514         {
  9515           if (it->bidi_p)
  9516             {
  9517               /* Take note of the stop position we just moved across,
  9518                  for when we will move back across it.  */
  9519               it->prev_stop = it->stop_charpos;
  9520               /* If we are at base paragraph embedding level, take
  9521                  note of the last stop position seen at this
  9522                  level.  */
  9523               if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9524                 it->base_level_stop = it->stop_charpos;
  9525             }
  9526           handle_stop (it);
  9527           it->ignore_overlay_strings_at_pos_p = false;
  9528           return GET_NEXT_DISPLAY_ELEMENT (it);
  9529         }
  9530     }
  9531   else if (it->bidi_p
  9532            /* If we are before prev_stop, we may have overstepped on
  9533               our way backwards a stop_pos, and if so, we need to
  9534               handle that stop_pos.  */
  9535            && IT_CHARPOS (*it) < it->prev_stop
  9536            /* We can sometimes back up for reasons that have nothing
  9537               to do with bidi reordering.  E.g., compositions.  The
  9538               code below is only needed when we are above the base
  9539               embedding level, so test for that explicitly.  */
  9540            && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9541     {
  9542       if (it->base_level_stop <= 0
  9543           || IT_CHARPOS (*it) < it->base_level_stop)
  9544         {
  9545           /* If we lost track of base_level_stop, we need to find
  9546              prev_stop by looking backwards.  This happens, e.g., when
  9547              we were reseated to the previous screenful of text by
  9548              vertical-motion.  */
  9549           it->base_level_stop = BEGV;
  9550           compute_stop_pos_backwards (it);
  9551           handle_stop_backwards (it, it->prev_stop);
  9552         }
  9553       else
  9554         handle_stop_backwards (it, it->base_level_stop);
  9555       it->ignore_overlay_strings_at_pos_p = false;
  9556       return GET_NEXT_DISPLAY_ELEMENT (it);
  9557     }
  9558   else
  9559     {
  9560       /* No face changes, overlays etc. in sight, so just return a
  9561          character from current_buffer.  */
  9562       unsigned char *p;
  9563       ptrdiff_t stop;
  9564 
  9565       /* We moved to the next buffer position, so any info about
  9566          previously seen overlays is no longer valid.  */
  9567       it->ignore_overlay_strings_at_pos_p = false;
  9568 
  9569       if (composition_break_at_point
  9570           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  9571           && !NILP (Vauto_composition_mode))
  9572         {
  9573           /* Limit search for composable characters to point's position.  */
  9574           if (it->bidi_it.scan_dir < 0)
  9575             stop = (PT <= IT_CHARPOS (*it)) ? PT : -1;
  9576           else
  9577             stop = (IT_CHARPOS (*it) < PT
  9578                     && PT < it->end_charpos) ? PT : it->end_charpos;
  9579         }
  9580       else
  9581         stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
  9582       if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
  9583                            stop)
  9584           && next_element_from_composition (it))
  9585         {
  9586           return true;
  9587         }
  9588 
  9589       /* Get the next character, maybe multibyte.  */
  9590       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
  9591       if (it->multibyte_p && !ASCII_CHAR_P (*p))
  9592         it->c = string_char_and_length (p, &it->len);
  9593       else
  9594         it->c = *p, it->len = 1;
  9595 
  9596       /* Record what we have and where it came from.  */
  9597       it->what = IT_CHARACTER;
  9598       it->object = it->w->contents;
  9599       it->position = it->current.pos;
  9600 
  9601       /* Normally we return the character found above, except when we
  9602          really want to return an ellipsis for selective display.  */
  9603       if (it->selective)
  9604         {
  9605           if (it->c == '\n')
  9606             {
  9607               /* A value of selective > 0 means hide lines indented more
  9608                  than that number of columns.  */
  9609               if (it->selective > 0
  9610                   && IT_CHARPOS (*it) + 1 < ZV
  9611                   && indented_beyond_p (IT_CHARPOS (*it) + 1,
  9612                                         IT_BYTEPOS (*it) + 1,
  9613                                         it->selective))
  9614                 {
  9615                   success_p = next_element_from_ellipsis (it);
  9616                   it->dpvec_char_len = -1;
  9617                 }
  9618             }
  9619           else if (it->c == '\r' && it->selective == -1)
  9620             {
  9621               /* A value of selective == -1 means that everything from the
  9622                  CR to the end of the line is invisible, with maybe an
  9623                  ellipsis displayed for it.  */
  9624               success_p = next_element_from_ellipsis (it);
  9625               it->dpvec_char_len = -1;
  9626             }
  9627         }
  9628     }
  9629 
  9630   /* Value is false if end of buffer reached.  */
  9631   eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
  9632   return success_p;
  9633 }
  9634 
  9635 
  9636 /* Deliver a composition display element.  Unlike the other
  9637    next_element_from_XXX, this function is not registered in the array
  9638    get_next_element[].  It is called from next_element_from_buffer and
  9639    next_element_from_string when necessary.  */
  9640 
  9641 static bool
  9642 next_element_from_composition (struct it *it)
  9643 {
  9644   it->what = IT_COMPOSITION;
  9645   it->len = it->cmp_it.nbytes;
  9646   if (STRINGP (it->string))
  9647     {
  9648       if (it->c < 0)
  9649         {
  9650           IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  9651           IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  9652           return false;
  9653         }
  9654       it->position = it->current.string_pos;
  9655       it->object = it->string;
  9656       it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
  9657                                      IT_STRING_BYTEPOS (*it), it->string);
  9658     }
  9659   else
  9660     {
  9661       if (it->c < 0)
  9662         {
  9663           IT_CHARPOS (*it) += it->cmp_it.nchars;
  9664           IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  9665           if (it->bidi_p)
  9666             {
  9667               if (it->bidi_it.new_paragraph)
  9668                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  9669                                      false);
  9670               /* Resync the bidi iterator with IT's new position.
  9671                  FIXME: this doesn't support bidirectional text.  */
  9672               while (it->bidi_it.charpos < IT_CHARPOS (*it))
  9673                 bidi_move_to_visually_next (&it->bidi_it);
  9674             }
  9675           return false;
  9676         }
  9677       it->position = it->current.pos;
  9678       it->object = it->w->contents;
  9679       it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
  9680                                      IT_BYTEPOS (*it), Qnil);
  9681     }
  9682   return true;
  9683 }
  9684 
  9685 
  9686 
  9687 /***********************************************************************
  9688              Moving an iterator without producing glyphs
  9689  ***********************************************************************/
  9690 
  9691 /* Check if iterator is at a position corresponding to a valid buffer
  9692    position after some move_it_ call.  */
  9693 
  9694 #define IT_POS_VALID_AFTER_MOVE_P(it)                   \
  9695   ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
  9696 
  9697 
  9698 /* Move iterator IT to a specified buffer or X position within one
  9699    line on the display without producing glyphs.
  9700 
  9701    OP should be a bit mask including some or all of these bits:
  9702     MOVE_TO_X: Stop upon reaching x-position TO_X.
  9703     MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
  9704    Regardless of OP's value, stop upon reaching the end of the display line.
  9705 
  9706    TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
  9707    This means, in particular, that TO_X includes window's horizontal
  9708    scroll amount.
  9709 
  9710    The return value has several possible values that
  9711    say what condition caused the scan to stop:
  9712 
  9713    MOVE_POS_MATCH_OR_ZV
  9714      - when TO_POS or ZV was reached.
  9715 
  9716    MOVE_X_REACHED
  9717      -when TO_X was reached before TO_POS or ZV were reached.
  9718 
  9719    MOVE_LINE_CONTINUED
  9720      - when we reached the end of the display area and the line must
  9721      be continued.
  9722 
  9723    MOVE_LINE_TRUNCATED
  9724      - when we reached the end of the display area and the line is
  9725      truncated.
  9726 
  9727    MOVE_NEWLINE_OR_CR
  9728      - when we stopped at a line end, i.e. a newline or a CR and selective
  9729      display is on.  */
  9730 
  9731 static enum move_it_result
  9732 move_it_in_display_line_to (struct it *it,
  9733                             ptrdiff_t to_charpos, int to_x,
  9734                             enum move_operation_enum op)
  9735 {
  9736   enum move_it_result result = MOVE_UNDEFINED;
  9737   struct glyph_row *saved_glyph_row;
  9738   struct it wrap_it, atpos_it, atx_it, ppos_it;
  9739   void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
  9740   void *ppos_data = NULL;
  9741   bool may_wrap = false;
  9742   enum it_method prev_method = it->method;
  9743   ptrdiff_t closest_pos UNINIT;
  9744   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  9745   bool saw_smaller_pos = prev_pos < to_charpos;
  9746   bool line_number_pending = false;
  9747 
  9748   /* Don't produce glyphs in produce_glyphs.  */
  9749   saved_glyph_row = it->glyph_row;
  9750   it->glyph_row = NULL;
  9751 
  9752   /* Use wrap_it to save a copy of IT wherever a word wrap could
  9753      occur.  Use atpos_it to save a copy of IT at the desired buffer
  9754      position, if found, so that we can scan ahead and check if the
  9755      word later overshoots the window edge.  Use atx_it similarly, for
  9756      pixel positions.  */
  9757   wrap_it.sp = -1;
  9758   atpos_it.sp = -1;
  9759   atx_it.sp = -1;
  9760 
  9761   /* Use ppos_it under bidi reordering to save a copy of IT for the
  9762      initial position.  We restore that position in IT when we have
  9763      scanned the entire display line without finding a match for
  9764      TO_CHARPOS and all the character positions are greater than
  9765      TO_CHARPOS.  We then restart the scan from the initial position,
  9766      and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
  9767      the closest to TO_CHARPOS.  */
  9768   if (it->bidi_p)
  9769     {
  9770       if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
  9771         {
  9772           SAVE_IT (ppos_it, *it, ppos_data);
  9773           closest_pos = IT_CHARPOS (*it);
  9774         }
  9775       else
  9776         closest_pos = ZV;
  9777     }
  9778 
  9779 #define BUFFER_POS_REACHED_P()                                  \
  9780   ((op & MOVE_TO_POS) != 0                                      \
  9781    && BUFFERP (it->object)                                      \
  9782    && (IT_CHARPOS (*it) == to_charpos                           \
  9783        || ((!it->bidi_p                                         \
  9784             || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
  9785            && IT_CHARPOS (*it) > to_charpos)                    \
  9786        || (it->what == IT_COMPOSITION                           \
  9787            && ((IT_CHARPOS (*it) > to_charpos                   \
  9788                 && to_charpos >= it->cmp_it.charpos)            \
  9789                || (IT_CHARPOS (*it) < to_charpos                \
  9790                    && to_charpos <= it->cmp_it.charpos))))      \
  9791    && (it->method == GET_FROM_BUFFER                            \
  9792        || (it->method == GET_FROM_DISPLAY_VECTOR                \
  9793            && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
  9794 
  9795   if (it->hpos == 0)
  9796     {
  9797       /* If line numbers are being displayed, produce a line number.
  9798          But don't do that if we are to reach first_visible_x, because
  9799          line numbers are not relevant to stuff that is not visible on
  9800          display.  */
  9801       if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
  9802           && should_produce_line_number (it))
  9803         {
  9804           if (it->current_x == it->first_visible_x)
  9805             maybe_produce_line_number (it);
  9806           else
  9807             line_number_pending = true;
  9808         }
  9809       /* If there's a line-/wrap-prefix, handle it, if we didn't already.  */
  9810       if (it->area == TEXT_AREA && !it->string_from_prefix_prop_p)
  9811         handle_line_prefix (it);
  9812     }
  9813 
  9814   if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9815     SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9816 
  9817   while (true)
  9818     {
  9819       int x, i, ascent = 0, descent = 0;
  9820 
  9821 /* Utility macro to reset an iterator with x, ascent, and descent.  */
  9822 #define IT_RESET_X_ASCENT_DESCENT(IT)                   \
  9823   ((IT)->current_x = x, (IT)->max_ascent = ascent,      \
  9824    (IT)->max_descent = descent)
  9825 
  9826       /* Stop if we move beyond TO_CHARPOS (after an image or a
  9827          display string or stretch glyph).  */
  9828       if ((op & MOVE_TO_POS) != 0
  9829           && BUFFERP (it->object)
  9830           && it->method == GET_FROM_BUFFER
  9831           && (((!it->bidi_p
  9832                 /* When the iterator is at base embedding level, we
  9833                    are guaranteed that characters are delivered for
  9834                    display in strictly increasing order of their
  9835                    buffer positions.  */
  9836                 || BIDI_AT_BASE_LEVEL (it->bidi_it))
  9837                && IT_CHARPOS (*it) > to_charpos)
  9838               || (it->bidi_p
  9839                   && (prev_method == GET_FROM_IMAGE
  9840                       || prev_method == GET_FROM_STRETCH
  9841                       || prev_method == GET_FROM_STRING)
  9842                   /* Passed TO_CHARPOS from left to right.  */
  9843                   && ((prev_pos < to_charpos
  9844                        && IT_CHARPOS (*it) >= to_charpos)
  9845                       /* Passed TO_CHARPOS from right to left.  */
  9846                       || (prev_pos > to_charpos
  9847                           && IT_CHARPOS (*it) <= to_charpos)))))
  9848         {
  9849           if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9850             {
  9851               result = MOVE_POS_MATCH_OR_ZV;
  9852               break;
  9853             }
  9854           else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
  9855             /* If wrap_it is valid, the current position might be in a
  9856                word that is wrapped.  So, save the iterator in
  9857                atpos_it and continue to see if wrapping happens.  */
  9858             SAVE_IT (atpos_it, *it, atpos_data);
  9859         }
  9860 
  9861       /* Stop when ZV reached.
  9862          We used to stop here when TO_CHARPOS reached as well, but that is
  9863          too soon if this glyph does not fit on this line.  So we handle it
  9864          explicitly below.  */
  9865       if (!get_next_display_element (it))
  9866         {
  9867           result = MOVE_POS_MATCH_OR_ZV;
  9868           break;
  9869         }
  9870 
  9871       if (it->line_wrap == TRUNCATE)
  9872         {
  9873           /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
  9874              produced something that doesn't consume any screen estate
  9875              in the text area, so we don't want to exit the loop at
  9876              TO_CHARPOS, before we produce the glyph for that buffer
  9877              position.  This happens, e.g., when there's an overlay at
  9878              TO_CHARPOS that draws a fringe bitmap.  */
  9879           if (BUFFER_POS_REACHED_P ()
  9880               && (it->pixel_width > 0
  9881                   || IT_CHARPOS (*it) > to_charpos
  9882                   || it->area != TEXT_AREA))
  9883             {
  9884               result = MOVE_POS_MATCH_OR_ZV;
  9885               break;
  9886             }
  9887         }
  9888       else
  9889         {
  9890           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
  9891             {
  9892               bool next_may_wrap = may_wrap;
  9893               /* Can we wrap after this character?  */
  9894               if (char_can_wrap_after (it))
  9895                 next_may_wrap = true;
  9896               else
  9897                 next_may_wrap = false;
  9898               /* Can we wrap here? */
  9899               if (may_wrap && char_can_wrap_before (it))
  9900                 {
  9901                   /* We have reached a glyph that follows one or more
  9902                      whitespace characters or characters that allow
  9903                      wrapping after them.  If this character allows
  9904                      wrapping before it, save this position as a
  9905                      wrapping point.  */
  9906                   if (atpos_it.sp >= 0)
  9907                     {
  9908                       RESTORE_IT (it, &atpos_it, atpos_data);
  9909                       result = MOVE_POS_MATCH_OR_ZV;
  9910                       goto done;
  9911                     }
  9912                   if (atx_it.sp >= 0)
  9913                     {
  9914                       RESTORE_IT (it, &atx_it, atx_data);
  9915                       result = MOVE_X_REACHED;
  9916                       goto done;
  9917                     }
  9918                   /* Otherwise, we can wrap here.  */
  9919                   SAVE_IT (wrap_it, *it, wrap_data);
  9920                 }
  9921               /* Update may_wrap for the next iteration.  */
  9922               may_wrap = next_may_wrap;
  9923             }
  9924         }
  9925 
  9926       /* Remember the line height for the current line, in case
  9927          the next element doesn't fit on the line.  */
  9928       ascent = it->max_ascent;
  9929       descent = it->max_descent;
  9930 
  9931       /* The call to produce_glyphs will get the metrics of the
  9932          display element IT is loaded with.  Record the x-position
  9933          before this display element, in case it doesn't fit on the
  9934          line.  */
  9935       x = it->current_x;
  9936 
  9937       PRODUCE_GLYPHS (it);
  9938 
  9939       if (it->area != TEXT_AREA)
  9940         {
  9941           prev_method = it->method;
  9942           if (it->method == GET_FROM_BUFFER)
  9943             prev_pos = IT_CHARPOS (*it);
  9944           set_iterator_to_next (it, true);
  9945           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9946             SET_TEXT_POS (this_line_min_pos,
  9947                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9948           if (it->bidi_p
  9949               && (op & MOVE_TO_POS)
  9950               && IT_CHARPOS (*it) > to_charpos
  9951               && IT_CHARPOS (*it) < closest_pos)
  9952             closest_pos = IT_CHARPOS (*it);
  9953           continue;
  9954         }
  9955 
  9956       /* The number of glyphs we get back in IT->nglyphs will normally
  9957          be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
  9958          character on a terminal frame, or (iii) a line end.  For the
  9959          second case, IT->nglyphs - 1 padding glyphs will be present.
  9960          (On X frames, there is only one glyph produced for a
  9961          composite character.)
  9962 
  9963          The behavior implemented below means, for continuation lines,
  9964          that as many spaces of a TAB as fit on the current line are
  9965          displayed there.  For terminal frames, as many glyphs of a
  9966          multi-glyph character are displayed in the current line, too.
  9967          This is what the old redisplay code did, and we keep it that
  9968          way.  Under X, the whole shape of a complex character must
  9969          fit on the line or it will be completely displayed in the
  9970          next line.
  9971 
  9972          Note that both for tabs and padding glyphs, all glyphs have
  9973          the same width.  */
  9974       if (it->nglyphs)
  9975         {
  9976           /* More than one glyph or glyph doesn't fit on line.  All
  9977              glyphs have the same width.  */
  9978           int single_glyph_width = it->pixel_width / it->nglyphs;
  9979           int new_x;
  9980           int x_before_this_char = x;
  9981           int hpos_before_this_char = it->hpos;
  9982 
  9983           for (i = 0; i < it->nglyphs; ++i, x = new_x)
  9984             {
  9985               new_x = x + single_glyph_width;
  9986 
  9987               /* We want to leave anything reaching TO_X to the caller.  */
  9988               if ((op & MOVE_TO_X) && new_x > to_x)
  9989                 {
  9990                   if (BUFFER_POS_REACHED_P ())
  9991                     {
  9992                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9993                         goto buffer_pos_reached;
  9994                       if (atpos_it.sp < 0)
  9995                         {
  9996                           SAVE_IT (atpos_it, *it, atpos_data);
  9997                           IT_RESET_X_ASCENT_DESCENT (&atpos_it);
  9998                         }
  9999                     }
 10000                   else
 10001                     {
 10002                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10003                         {
 10004                           it->current_x = x;
 10005                           result = MOVE_X_REACHED;
 10006                           break;
 10007                         }
 10008                       if (atx_it.sp < 0)
 10009                         {
 10010                           SAVE_IT (atx_it, *it, atx_data);
 10011                           IT_RESET_X_ASCENT_DESCENT (&atx_it);
 10012                         }
 10013                     }
 10014                 }
 10015 
 10016               if (/* Lines are continued.  */
 10017                   it->line_wrap != TRUNCATE
 10018                   && (/* And glyph doesn't fit on the line.  */
 10019                       new_x > it->last_visible_x
 10020                       /* Or it fits exactly and we're on a window
 10021                          system frame.  */
 10022                       || (new_x == it->last_visible_x
 10023                           && FRAME_WINDOW_P (it->f)
 10024                           && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10025                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10026                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 10027                 {
 10028                   bool moved_forward = false;
 10029 
 10030                   if (/* IT->hpos == 0 means the very first glyph
 10031                          doesn't fit on the line, e.g. a wide image.  */
 10032                       it->hpos == 0
 10033                       || (new_x == it->last_visible_x
 10034                           && FRAME_WINDOW_P (it->f)))
 10035                     {
 10036                       ++it->hpos;
 10037                       it->current_x = new_x;
 10038 
 10039                       /* The character's last glyph just barely fits
 10040                          in this row.  */
 10041                       if (i == it->nglyphs - 1)
 10042                         {
 10043                           /* If this is the destination position,
 10044                              return a position *before* it in this row,
 10045                              now that we know it fits in this row.  */
 10046                           if (BUFFER_POS_REACHED_P ())
 10047                             {
 10048                               bool can_wrap = true;
 10049 
 10050                               /* If the previous character says we can
 10051                                  wrap after it, but the current
 10052                                  character says we can't wrap before
 10053                                  it, then we can't wrap here.  */
 10054                               if (it->line_wrap == WORD_WRAP
 10055                                   && wrap_it.sp >= 0
 10056                                   && may_wrap
 10057                                   && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10058                                 {
 10059                                   struct it tem_it;
 10060                                   void *tem_data = NULL;
 10061 
 10062                                   SAVE_IT (tem_it, *it, tem_data);
 10063                                   set_iterator_to_next (it, true);
 10064                                   if (get_next_display_element (it)
 10065                                       && !char_can_wrap_before (it))
 10066                                     can_wrap = false;
 10067                                   RESTORE_IT (it, &tem_it, tem_data);
 10068                                 }
 10069                               if (it->line_wrap != WORD_WRAP
 10070                                   || wrap_it.sp < 0
 10071                                   /* If we've just found whitespace
 10072                                      where we can wrap, effectively
 10073                                      ignore the previous wrap point --
 10074                                      it is no longer relevant, but we
 10075                                      won't have an opportunity to
 10076                                      update it, since we've reached
 10077                                      the edge of this screen line.  */
 10078                                   || (may_wrap && can_wrap
 10079                                       && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10080                                 {
 10081                                   it->hpos = hpos_before_this_char;
 10082                                   it->current_x = x_before_this_char;
 10083                                   result = MOVE_POS_MATCH_OR_ZV;
 10084                                   break;
 10085                                 }
 10086                               if (it->line_wrap == WORD_WRAP
 10087                                   && atpos_it.sp < 0)
 10088                                 {
 10089                                   SAVE_IT (atpos_it, *it, atpos_data);
 10090                                   atpos_it.current_x = x_before_this_char;
 10091                                   atpos_it.hpos = hpos_before_this_char;
 10092                                 }
 10093                             }
 10094 
 10095                           prev_method = it->method;
 10096                           if (it->method == GET_FROM_BUFFER)
 10097                             prev_pos = IT_CHARPOS (*it);
 10098                           set_iterator_to_next (it, true);
 10099                           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10100                             SET_TEXT_POS (this_line_min_pos,
 10101                                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10102                           /* On graphical terminals, newlines may
 10103                              "overflow" into the fringe if
 10104                              overflow-newline-into-fringe is non-nil.
 10105                              On text terminals, and on graphical
 10106                              terminals with no right margin, newlines
 10107                              may overflow into the last glyph on the
 10108                              display line.*/
 10109                           if (!FRAME_WINDOW_P (it->f)
 10110                               || ((it->bidi_p
 10111                                    && it->bidi_it.paragraph_dir == R2L)
 10112                                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10113                                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10114                               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10115                             {
 10116                               if (!get_next_display_element (it))
 10117                                 {
 10118                                   result = MOVE_POS_MATCH_OR_ZV;
 10119                                   break;
 10120                                 }
 10121                               moved_forward = true;
 10122                               if (BUFFER_POS_REACHED_P ())
 10123                                 {
 10124                                   if (ITERATOR_AT_END_OF_LINE_P (it))
 10125                                     result = MOVE_POS_MATCH_OR_ZV;
 10126                                   else
 10127                                     result = MOVE_LINE_CONTINUED;
 10128                                   break;
 10129                                 }
 10130                               if (ITERATOR_AT_END_OF_LINE_P (it)
 10131                                   && (it->line_wrap != WORD_WRAP
 10132                                       || wrap_it.sp < 0
 10133                                       || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10134                                 {
 10135                                   result = MOVE_NEWLINE_OR_CR;
 10136                                   break;
 10137                                 }
 10138                             }
 10139                         }
 10140                     }
 10141                   else
 10142                     IT_RESET_X_ASCENT_DESCENT (it);
 10143 
 10144                   /* If the screen line ends with whitespace (or
 10145                      wrap-able character), and we are under word-wrap,
 10146                      don't use wrap_it: it is no longer relevant, but
 10147                      we won't have an opportunity to update it, since
 10148                      we are done with this screen line.  */
 10149                   if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 10150                       /* If the character after the one which set the
 10151                          may_wrap flag says we can't wrap before it,
 10152                          we can't wrap here.  Therefore, wrap_it
 10153                          (previously found wrap-point) _is_ relevant
 10154                          in that case.  */
 10155                       && (!moved_forward || char_can_wrap_before (it)))
 10156                     {
 10157                       /* If we've found TO_X, go back there, as we now
 10158                          know the last word fits on this screen line.  */
 10159                       if ((op & MOVE_TO_X) && new_x == it->last_visible_x
 10160                           && atx_it.sp >= 0)
 10161                         {
 10162                           RESTORE_IT (it, &atx_it, atx_data);
 10163                           atpos_it.sp = -1;
 10164                           atx_it.sp = -1;
 10165                           result = MOVE_X_REACHED;
 10166                           break;
 10167                         }
 10168                     }
 10169                   else if (wrap_it.sp >= 0)
 10170                     {
 10171                       RESTORE_IT (it, &wrap_it, wrap_data);
 10172                       atpos_it.sp = -1;
 10173                       atx_it.sp = -1;
 10174                     }
 10175 
 10176                   move_trace ("move_it_in: continued at %td\n",
 10177                               IT_CHARPOS (*it));
 10178                   result = MOVE_LINE_CONTINUED;
 10179                   break;
 10180                 }
 10181 
 10182               if (BUFFER_POS_REACHED_P ())
 10183                 {
 10184                   if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10185                     goto buffer_pos_reached;
 10186                   if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
 10187                     {
 10188                       SAVE_IT (atpos_it, *it, atpos_data);
 10189                       IT_RESET_X_ASCENT_DESCENT (&atpos_it);
 10190                     }
 10191                 }
 10192 
 10193               if (new_x > it->first_visible_x)
 10194                 {
 10195                   /* If we have reached the visible portion of the
 10196                      screen line, produce the line number if needed.  */
 10197                   if (line_number_pending)
 10198                     {
 10199                       line_number_pending = false;
 10200                       it->current_x = it->first_visible_x;
 10201                       maybe_produce_line_number (it);
 10202                       it->current_x += new_x - it->first_visible_x;
 10203                     }
 10204                   /* Glyph is visible.  Increment number of glyphs that
 10205                      would be displayed.  */
 10206                   ++it->hpos;
 10207                 }
 10208             }
 10209 
 10210           if (result != MOVE_UNDEFINED)
 10211             break;
 10212         }
 10213       else if (BUFFER_POS_REACHED_P ())
 10214         {
 10215         buffer_pos_reached:
 10216           IT_RESET_X_ASCENT_DESCENT (it);
 10217           result = MOVE_POS_MATCH_OR_ZV;
 10218           break;
 10219         }
 10220       else if ((op & MOVE_TO_X) && it->current_x >= to_x)
 10221         {
 10222           /* Stop when TO_X specified and reached.  This check is
 10223              necessary here because of lines consisting of a line end,
 10224              only.  The line end will not produce any glyphs and we
 10225              would never get MOVE_X_REACHED.  */
 10226           eassert (it->nglyphs == 0);
 10227           result = MOVE_X_REACHED;
 10228           break;
 10229         }
 10230 
 10231       /* Is this a line end?  If yes, we're done.  */
 10232       if (ITERATOR_AT_END_OF_LINE_P (it))
 10233         {
 10234           /* If we are past TO_CHARPOS, but never saw any character
 10235              positions smaller than TO_CHARPOS, return
 10236              MOVE_POS_MATCH_OR_ZV, like the unidirectional display
 10237              did.  */
 10238           if (it->bidi_p && (op & MOVE_TO_POS) != 0)
 10239             {
 10240               if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
 10241                 {
 10242                   if (closest_pos < ZV)
 10243                     {
 10244                       RESTORE_IT (it, &ppos_it, ppos_data);
 10245                       /* Don't recurse if closest_pos is equal to
 10246                          to_charpos, since we have just tried that.  */
 10247                       if (closest_pos != to_charpos)
 10248                         move_it_in_display_line_to (it, closest_pos, -1,
 10249                                                     MOVE_TO_POS);
 10250                       result = MOVE_POS_MATCH_OR_ZV;
 10251                     }
 10252                   else
 10253                     goto buffer_pos_reached;
 10254                 }
 10255               else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
 10256                        && IT_CHARPOS (*it) > to_charpos)
 10257                 goto buffer_pos_reached;
 10258               else
 10259                 result = MOVE_NEWLINE_OR_CR;
 10260             }
 10261           else
 10262             result = MOVE_NEWLINE_OR_CR;
 10263           /* If lines are truncated, and the line we moved across is
 10264              completely hscrolled out of view, reset the line metrics
 10265              to those of the newline we've just processed, so that
 10266              glyphs not on display don't affect the line's height.  */
 10267           if (it->line_wrap == TRUNCATE
 10268               && it->current_x <= it->first_visible_x
 10269               && result == MOVE_NEWLINE_OR_CR
 10270               && it->char_to_display == '\n')
 10271             {
 10272               it->max_ascent = it->ascent;
 10273               it->max_descent = it->descent;
 10274             }
 10275           /* If we've processed the newline, make sure this flag is
 10276              reset, as it must only be set when the newline itself is
 10277              processed.  */
 10278           if (result == MOVE_NEWLINE_OR_CR)
 10279             it->constrain_row_ascent_descent_p = false;
 10280           break;
 10281         }
 10282 
 10283       prev_method = it->method;
 10284       if (it->method == GET_FROM_BUFFER)
 10285         prev_pos = IT_CHARPOS (*it);
 10286 
 10287       /* Detect overly-wide wrap-prefixes made of (space ...) display
 10288          properties.  When such a wrap prefix reaches past the right
 10289          margin of the window, we need to avoid the call to
 10290          set_iterator_to_next below, so that it->line_wrap is left at
 10291          its TRUNCATE value wisely set by handle_line_prefix.
 10292          Otherwise, set_iterator_to_next will pop the iterator stack,
 10293          restore it->line_wrap, and we might miss the opportunity to
 10294          exit the loop and return.  */
 10295       bool overwide_wrap_prefix =
 10296         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 10297         && it->sp > 0 && it->method == GET_FROM_STRETCH
 10298         && it->current_x >= it->last_visible_x
 10299         && it->continuation_lines_width > 0
 10300         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 10301       /* The current display element has been consumed.  Advance
 10302          to the next.  */
 10303       if (!overwide_wrap_prefix)
 10304         set_iterator_to_next (it, true);
 10305       if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10306         SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10307       if (IT_CHARPOS (*it) < to_charpos)
 10308         saw_smaller_pos = true;
 10309       if (it->bidi_p
 10310           && (op & MOVE_TO_POS)
 10311           && IT_CHARPOS (*it) >= to_charpos
 10312           && IT_CHARPOS (*it) < closest_pos)
 10313         closest_pos = IT_CHARPOS (*it);
 10314 
 10315       /* Stop if lines are truncated and IT's current x-position is
 10316          past the right edge of the window now.  */
 10317       if (it->line_wrap == TRUNCATE
 10318           && it->current_x >= it->last_visible_x)
 10319         {
 10320           if (!FRAME_WINDOW_P (it->f)
 10321               || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10322                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10323                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10324               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10325             {
 10326               bool at_eob_p = false;
 10327 
 10328               if ((at_eob_p = !get_next_display_element (it))
 10329                   || BUFFER_POS_REACHED_P ()
 10330                   /* If we are past TO_CHARPOS, but never saw any
 10331                      character positions smaller than TO_CHARPOS,
 10332                      return MOVE_POS_MATCH_OR_ZV, like the
 10333                      unidirectional display did.  */
 10334                   || (it->bidi_p && (op & MOVE_TO_POS) != 0
 10335                       && !saw_smaller_pos
 10336                       && IT_CHARPOS (*it) > to_charpos))
 10337                 {
 10338                   if (it->bidi_p
 10339                       && !BUFFER_POS_REACHED_P ()
 10340                       && !at_eob_p && closest_pos < ZV)
 10341                     {
 10342                       RESTORE_IT (it, &ppos_it, ppos_data);
 10343                       if (closest_pos != to_charpos)
 10344                         move_it_in_display_line_to (it, closest_pos, -1,
 10345                                                     MOVE_TO_POS);
 10346                     }
 10347                   result = MOVE_POS_MATCH_OR_ZV;
 10348                   break;
 10349                 }
 10350               if (ITERATOR_AT_END_OF_LINE_P (it))
 10351                 {
 10352                   result = MOVE_NEWLINE_OR_CR;
 10353                   break;
 10354                 }
 10355             }
 10356           else if (it->bidi_p && (op & MOVE_TO_POS) != 0
 10357                    && !saw_smaller_pos
 10358                    && IT_CHARPOS (*it) > to_charpos)
 10359             {
 10360               if (closest_pos < ZV)
 10361                 {
 10362                   RESTORE_IT (it, &ppos_it, ppos_data);
 10363                   if (closest_pos != to_charpos)
 10364                     move_it_in_display_line_to (it, closest_pos, -1,
 10365                                                 MOVE_TO_POS);
 10366                 }
 10367               result = MOVE_POS_MATCH_OR_ZV;
 10368               break;
 10369             }
 10370           result = MOVE_LINE_TRUNCATED;
 10371           break;
 10372         }
 10373 #undef IT_RESET_X_ASCENT_DESCENT
 10374     }
 10375 
 10376 #undef BUFFER_POS_REACHED_P
 10377 
 10378   /* If we scanned beyond TO_POS, restore the saved iterator either to
 10379      the wrap point (if found), or to atpos/atx location.  We decide which
 10380      data to use to restore the saved iterator state by their X coordinates,
 10381      since buffer positions might increase non-monotonically with screen
 10382      coordinates due to bidi reordering.  */
 10383   if (result == MOVE_LINE_CONTINUED
 10384       && it->line_wrap == WORD_WRAP
 10385       && wrap_it.sp >= 0
 10386       && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
 10387           || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
 10388     RESTORE_IT (it, &wrap_it, wrap_data);
 10389   else if (atpos_it.sp >= 0)
 10390     RESTORE_IT (it, &atpos_it, atpos_data);
 10391   else if (atx_it.sp >= 0)
 10392     RESTORE_IT (it, &atx_it, atx_data);
 10393 
 10394  done:
 10395 
 10396   if (atpos_data)
 10397     bidi_unshelve_cache (atpos_data, true);
 10398   if (atx_data)
 10399     bidi_unshelve_cache (atx_data, true);
 10400   if (wrap_data)
 10401     bidi_unshelve_cache (wrap_data, true);
 10402   if (ppos_data)
 10403     bidi_unshelve_cache (ppos_data, true);
 10404 
 10405   /* Restore the iterator settings altered at the beginning of this
 10406      function.  */
 10407   it->glyph_row = saved_glyph_row;
 10408   return result;
 10409 }
 10410 
 10411 /* For external use.  */
 10412 void
 10413 move_it_in_display_line (struct it *it,
 10414                          ptrdiff_t to_charpos, int to_x,
 10415                          enum move_operation_enum op)
 10416 {
 10417   if (it->line_wrap == WORD_WRAP
 10418       && (op & MOVE_TO_X))
 10419     {
 10420       struct it save_it;
 10421       void *save_data = NULL;
 10422       int skip;
 10423 
 10424       SAVE_IT (save_it, *it, save_data);
 10425       skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10426       /* When word-wrap is on, TO_X may lie past the end
 10427          of a wrapped line.  Then it->current is the
 10428          character on the next line, so backtrack to the
 10429          space before the wrap point.  */
 10430       if (skip == MOVE_LINE_CONTINUED)
 10431         {
 10432           int prev_x = max (it->current_x - 1, 0);
 10433           RESTORE_IT (it, &save_it, save_data);
 10434           move_it_in_display_line_to
 10435             (it, -1, prev_x, MOVE_TO_X);
 10436         }
 10437       else
 10438         bidi_unshelve_cache (save_data, true);
 10439     }
 10440   else
 10441     move_it_in_display_line_to (it, to_charpos, to_x, op);
 10442 }
 10443 
 10444 
 10445 /* Move IT forward until it satisfies one or more of the criteria in
 10446    TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
 10447 
 10448    OP is a bit-mask that specifies where to stop, and in particular,
 10449    which of those four position arguments makes a difference.  See the
 10450    description of enum move_operation_enum.
 10451 
 10452    If TO_CHARPOS is in invisible text, e.g. a truncated part of a
 10453    screen line, this function will set IT to the next position that is
 10454    displayed to the right of TO_CHARPOS on the screen.
 10455 
 10456    Return the maximum pixel length of any line scanned but never more
 10457    than it.last_visible_x.  */
 10458 
 10459 int
 10460 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
 10461 {
 10462   enum move_it_result skip, skip2 = MOVE_X_REACHED;
 10463   int line_height, line_start_x = 0, reached = 0;
 10464   int max_current_x = 0;
 10465   void *backup_data = NULL;
 10466   ptrdiff_t orig_charpos = -1;
 10467   enum it_method orig_method = NUM_IT_METHODS;
 10468 
 10469   for (;;)
 10470     {
 10471       orig_charpos = IT_CHARPOS (*it);
 10472       orig_method = it->method;
 10473       if (op & MOVE_TO_VPOS)
 10474         {
 10475           /* If no TO_CHARPOS and no TO_X specified, stop at the
 10476              start of the line TO_VPOS.  */
 10477           if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
 10478             {
 10479               if (it->vpos == to_vpos)
 10480                 {
 10481                   reached = 1;
 10482                   break;
 10483                 }
 10484               else
 10485                 skip = move_it_in_display_line_to (it, -1, -1, 0);
 10486             }
 10487           else
 10488             {
 10489               /* TO_VPOS >= 0 means stop at TO_X in the line at
 10490                  TO_VPOS, or at TO_POS, whichever comes first.  */
 10491               if (it->vpos == to_vpos)
 10492                 {
 10493                   reached = 2;
 10494                   break;
 10495                 }
 10496 
 10497               skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10498 
 10499               if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
 10500                 {
 10501                   reached = 3;
 10502                   break;
 10503                 }
 10504               else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
 10505                 {
 10506                   /* We have reached TO_X but not in the line we want.  */
 10507                   skip = move_it_in_display_line_to (it, to_charpos,
 10508                                                      -1, MOVE_TO_POS);
 10509                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10510                     {
 10511                       reached = 4;
 10512                       break;
 10513                     }
 10514                 }
 10515             }
 10516         }
 10517       else if (op & MOVE_TO_Y)
 10518         {
 10519           struct it it_backup;
 10520 
 10521           if (it->line_wrap == WORD_WRAP)
 10522             SAVE_IT (it_backup, *it, backup_data);
 10523 
 10524           /* TO_Y specified means stop at TO_X in the line containing
 10525              TO_Y---or at TO_CHARPOS if this is reached first.  The
 10526              problem is that we can't really tell whether the line
 10527              contains TO_Y before we have completely scanned it, and
 10528              this may skip past TO_X.  What we do is to first scan to
 10529              TO_X.
 10530 
 10531              If TO_X is not specified, use a TO_X of zero.  The reason
 10532              is to make the outcome of this function more predictable.
 10533              If we didn't use TO_X == 0, we would stop at the end of
 10534              the line which is probably not what a caller would expect
 10535              to happen.  */
 10536           skip = move_it_in_display_line_to
 10537             (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
 10538              (MOVE_TO_X | (op & MOVE_TO_POS)));
 10539 
 10540           /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
 10541           if (skip == MOVE_POS_MATCH_OR_ZV)
 10542             reached = 5;
 10543           else if (skip == MOVE_X_REACHED)
 10544             {
 10545               /* If TO_X was reached, we want to know whether TO_Y is
 10546                  in the line.  We know this is the case if the already
 10547                  scanned glyphs make the line tall enough.  Otherwise,
 10548                  we must check by scanning the rest of the line.  */
 10549               line_height = it->max_ascent + it->max_descent;
 10550               if (to_y >= it->current_y
 10551                   && to_y < it->current_y + line_height)
 10552                 {
 10553                   reached = 6;
 10554                   break;
 10555                 }
 10556               SAVE_IT (it_backup, *it, backup_data);
 10557               move_trace ("move_it: from %td\n", IT_CHARPOS (*it));
 10558               skip2 = move_it_in_display_line_to (it, to_charpos, -1,
 10559                                                   op & MOVE_TO_POS);
 10560               move_trace ("move_it: to %td\n", IT_CHARPOS (*it));
 10561               line_height = it->max_ascent + it->max_descent;
 10562               move_trace ("move_it: line_height = %d\n", line_height);
 10563 
 10564               if (to_y >= it->current_y
 10565                   && to_y < it->current_y + line_height)
 10566                 {
 10567                   /* If TO_Y is in this line and TO_X was reached
 10568                      above, we scanned too far.  We have to restore
 10569                      IT's settings to the ones before skipping.  But
 10570                      keep the more accurate values of max_ascent and
 10571                      max_descent we've found while skipping the rest
 10572                      of the line, for the sake of callers, such as
 10573                      pos_visible_p, that need to know the line
 10574                      height.  */
 10575                   int max_ascent = it->max_ascent;
 10576                   int max_descent = it->max_descent;
 10577 
 10578                   RESTORE_IT (it, &it_backup, backup_data);
 10579                   it->max_ascent = max_ascent;
 10580                   it->max_descent = max_descent;
 10581                   reached = 6;
 10582                 }
 10583               else
 10584                 {
 10585                   skip = skip2;
 10586                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10587                     {
 10588                       reached = 7;
 10589                       /* If the last move_it_in_display_line_to call
 10590                          took us away from TO_CHARPOS, back up to the
 10591                          previous position, as it is a better
 10592                          approximation of TO_CHARPOS.  (Note that we
 10593                          could have both positions after TO_CHARPOS or
 10594                          both positions before it, due to bidi
 10595                          reordering.)  */
 10596                       if (to_charpos > 0
 10597                           && IT_CHARPOS (*it) != to_charpos
 10598                           && ((IT_CHARPOS (it_backup) > to_charpos)
 10599                               == (IT_CHARPOS (*it) > to_charpos)))
 10600                         {
 10601                           int max_ascent = it->max_ascent;
 10602                           int max_descent = it->max_descent;
 10603 
 10604                           RESTORE_IT (it, &it_backup, backup_data);
 10605                           it->max_ascent = max_ascent;
 10606                           it->max_descent = max_descent;
 10607                         }
 10608                     }
 10609                 }
 10610             }
 10611           else
 10612             {
 10613               /* Check whether TO_Y is in this line.  */
 10614               line_height = it->max_ascent + it->max_descent;
 10615               move_trace ("move_it: line_height = %d\n", line_height);
 10616 
 10617               if (to_y >= it->current_y
 10618                   && to_y < it->current_y + line_height)
 10619                 {
 10620                   if (to_y > it->current_y)
 10621                     max_current_x = max (it->current_x, max_current_x);
 10622 
 10623                   /* When word-wrap is on, TO_X may lie past the end
 10624                      of a wrapped line.  Then it->current is the
 10625                      character on the next line, so backtrack to the
 10626                      space before the wrap point.  */
 10627                   if (skip == MOVE_LINE_CONTINUED
 10628                       && it->line_wrap == WORD_WRAP)
 10629                     {
 10630                       int prev_x = max (it->current_x - 1, 0);
 10631                       RESTORE_IT (it, &it_backup, backup_data);
 10632                       skip = move_it_in_display_line_to
 10633                         (it, -1, prev_x, MOVE_TO_X);
 10634                     }
 10635 
 10636                   reached = 6;
 10637                 }
 10638             }
 10639 
 10640           if (reached)
 10641             {
 10642               max_current_x = max (it->current_x, max_current_x);
 10643               break;
 10644             }
 10645         }
 10646       else if (BUFFERP (it->object)
 10647                && (it->method == GET_FROM_BUFFER
 10648                    || it->method == GET_FROM_STRETCH)
 10649                && IT_CHARPOS (*it) >= to_charpos
 10650                /* Under bidi iteration, a call to set_iterator_to_next
 10651                   can scan far beyond to_charpos if the initial
 10652                   portion of the next line needs to be reordered.  In
 10653                   that case, give move_it_in_display_line_to another
 10654                   chance below.  */
 10655                && !(it->bidi_p
 10656                     && it->bidi_it.scan_dir == -1))
 10657         skip = MOVE_POS_MATCH_OR_ZV;
 10658       else
 10659         skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
 10660 
 10661       switch (skip)
 10662         {
 10663         case MOVE_POS_MATCH_OR_ZV:
 10664           max_current_x = max (it->current_x, max_current_x);
 10665           reached = 8;
 10666           goto out;
 10667 
 10668         case MOVE_NEWLINE_OR_CR:
 10669           max_current_x = max (it->current_x, max_current_x);
 10670           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10671             it->override_ascent = -1;
 10672           set_iterator_to_next (it, true);
 10673           it->continuation_lines_width = 0;
 10674           break;
 10675 
 10676         case MOVE_LINE_TRUNCATED:
 10677           max_current_x = it->last_visible_x;
 10678           it->continuation_lines_width = 0;
 10679           reseat_at_next_visible_line_start (it, false);
 10680           if ((op & MOVE_TO_POS) != 0
 10681               && (IT_CHARPOS (*it) > to_charpos
 10682                   || (IT_CHARPOS (*it) == to_charpos
 10683                       /* Consider TO_CHARPOS as REACHED if we are at
 10684                          EOB that ends in something other than a newline.  */
 10685                       && to_charpos == ZV
 10686                       && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n')
 10687                       /* But if we have a display or an overlay string
 10688                          at EOB, keep going until we exhaust all the
 10689                          characters of the string(s).  */
 10690                       && (it->sp == 0
 10691                           || (STRINGP (it->string)
 10692                               && (it->current.overlay_string_index < 0
 10693                                   || (it->current.overlay_string_index >= 0
 10694                                       && it->current.overlay_string_index
 10695                                          >= it->n_overlay_strings - 1))
 10696                               && IT_STRING_CHARPOS (*it) >= it->end_charpos)))))
 10697             {
 10698               reached = 9;
 10699               goto out;
 10700             }
 10701           break;
 10702 
 10703         case MOVE_LINE_CONTINUED:
 10704           max_current_x = it->last_visible_x;
 10705           /* For continued lines ending in a tab, some of the glyphs
 10706              associated with the tab are displayed on the current
 10707              line.  Since it->current_x does not include these glyphs,
 10708              we use it->last_visible_x instead.  */
 10709           if (it->c == '\t')
 10710             {
 10711               it->continuation_lines_width += it->last_visible_x;
 10712               /* When moving by vpos, ensure that the iterator really
 10713                  advances to the next line (bug#847, bug#969).  Fixme:
 10714                  do we need to do this in other circumstances?  */
 10715               if (it->current_x != it->last_visible_x
 10716                   && (op & MOVE_TO_VPOS)
 10717                   && !(op & (MOVE_TO_X | MOVE_TO_POS)))
 10718                 {
 10719                   line_start_x = it->current_x + it->pixel_width
 10720                     - it->last_visible_x;
 10721                   if (FRAME_WINDOW_P (it->f))
 10722                     {
 10723                       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 10724                       struct font *face_font = face->font;
 10725 
 10726                       /* When display_line produces a continued line
 10727                          that ends in a TAB, it skips a tab stop that
 10728                          is closer than the font's space character
 10729                          width (see gui_produce_glyphs where it produces
 10730                          the stretch glyph which represents a TAB).
 10731                          We need to reproduce the same logic here.  */
 10732                       eassert (face_font);
 10733                       if (face_font)
 10734                         {
 10735                           if (line_start_x < face_font->space_width)
 10736                             line_start_x
 10737                               += it->tab_width * face_font->space_width;
 10738                         }
 10739                     }
 10740                   set_iterator_to_next (it, false);
 10741                 }
 10742             }
 10743           else
 10744             {
 10745               /* Make sure we do advance, otherwise we might infloop.
 10746                  This could happen when the first display element is
 10747                  wider than the window, or if we have a wrap-prefix
 10748                  that doesn't leave enough space after it to display
 10749                  even a single character.  We only do this for moving
 10750                  through buffer text, as with display/overlay strings
 10751                  we'd need to also compare it->object's, and this is
 10752                  unlikely to happen in that case anyway.  */
 10753               if (IT_CHARPOS (*it) == orig_charpos
 10754                   && it->method == orig_method
 10755                   && orig_method == GET_FROM_BUFFER)
 10756                 set_iterator_to_next (it, false);
 10757               it->continuation_lines_width += it->current_x;
 10758             }
 10759           break;
 10760 
 10761         default:
 10762           emacs_abort ();
 10763         }
 10764 
 10765       /* Reset/increment for the next run.  */
 10766       it->current_x = line_start_x;
 10767       line_start_x = 0;
 10768       it->hpos = 0;
 10769       it->line_number_produced_p = false;
 10770       it->current_y += it->max_ascent + it->max_descent;
 10771       ++it->vpos;
 10772       last_height = it->max_ascent + it->max_descent;
 10773       it->max_ascent = it->max_descent = 0;
 10774     }
 10775 
 10776  out:
 10777 
 10778   /* On text terminals, we may stop at the end of a line in the middle
 10779      of a multi-character glyph.  If the glyph itself is continued,
 10780      i.e. it is actually displayed on the next line, don't treat this
 10781      stopping point as valid; move to the next line instead (unless
 10782      that brings us offscreen).  */
 10783   if (!FRAME_WINDOW_P (it->f)
 10784       && op & MOVE_TO_POS
 10785       && IT_CHARPOS (*it) == to_charpos
 10786       && it->what == IT_CHARACTER
 10787       && it->nglyphs > 1
 10788       && it->line_wrap == WINDOW_WRAP
 10789       && it->current_x == it->last_visible_x - 1
 10790       && it->c != '\n'
 10791       && it->c != '\t'
 10792       && it->w->window_end_valid
 10793       && it->vpos < it->w->window_end_vpos)
 10794     {
 10795       it->continuation_lines_width += it->current_x;
 10796       it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
 10797       it->current_y += it->max_ascent + it->max_descent;
 10798       ++it->vpos;
 10799       last_height = it->max_ascent + it->max_descent;
 10800     }
 10801 
 10802   if (backup_data)
 10803     bidi_unshelve_cache (backup_data, true);
 10804 
 10805   move_trace ("move_it_to: reached %d\n", reached);
 10806 
 10807   return max_current_x;
 10808 }
 10809 
 10810 
 10811 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
 10812 
 10813    If DY > 0, move IT backward that many pixels.
 10814    DY = 0 means move IT backward to the preceding line start or to BEGV.
 10815    This function may move over less or more than DY pixels if
 10816    IT->current_y - DY ends up in the middle of a line; in this case
 10817    IT->current_y will be set to the top of the line either before or
 10818    after the exact pixel coordinate.  */
 10819 
 10820 void
 10821 move_it_vertically_backward (struct it *it, int dy)
 10822 {
 10823   int nlines, h;
 10824   struct it it2, it3;
 10825   void *it2data = NULL, *it3data = NULL;
 10826   ptrdiff_t start_pos;
 10827   int nchars_per_row
 10828     = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 10829   ptrdiff_t pos_limit;
 10830 
 10831  move_further_back:
 10832   eassert (dy >= 0);
 10833 
 10834   start_pos = IT_CHARPOS (*it);
 10835 
 10836   /* Estimate how many newlines we must move back.  */
 10837   nlines = max (1, dy / default_line_pixel_height (it->w));
 10838   if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 10839     pos_limit = BEGV;
 10840   else
 10841     pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
 10842 
 10843   /* Set the iterator's position that many lines back.  But don't go
 10844      back more than NLINES full screen lines -- this wins a day with
 10845      buffers which have very long lines.  */
 10846   while (nlines-- && IT_CHARPOS (*it) > pos_limit)
 10847     back_to_previous_visible_line_start (it);
 10848 
 10849   /* Reseat the iterator here.  When moving backward, we don't want
 10850      reseat to skip forward over invisible text, set up the iterator
 10851      to deliver from overlay strings at the new position etc.  So,
 10852      use reseat_1 here.  */
 10853   reseat_1 (it, it->current.pos, true);
 10854 
 10855   /* We are now surely at a line start.  */
 10856   it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
 10857                                    reordering is in effect.  */
 10858   it->continuation_lines_width = 0;
 10859 
 10860   /* Move forward and see what y-distance we moved.  First move to the
 10861      start of the next line so that we get its height.  We need this
 10862      height to be able to tell whether we reached the specified
 10863      y-distance.  */
 10864   SAVE_IT (it2, *it, it2data);
 10865   it2.max_ascent = it2.max_descent = 0;
 10866   do
 10867     {
 10868       move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
 10869                   MOVE_TO_POS | MOVE_TO_VPOS);
 10870     }
 10871   while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
 10872            /* If we are in a display string which starts at START_POS,
 10873               and that display string includes a newline, and we are
 10874               right after that newline (i.e. at the beginning of a
 10875               display line), exit the loop, because otherwise we will
 10876               infloop, since move_it_to will see that it is already at
 10877               START_POS and will not move.  */
 10878            || (it2.method == GET_FROM_STRING
 10879                && IT_CHARPOS (it2) == start_pos
 10880                && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
 10881   eassert (IT_CHARPOS (*it) >= BEGV);
 10882   SAVE_IT (it3, it2, it3data);
 10883 
 10884   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
 10885   eassert (IT_CHARPOS (*it) >= BEGV);
 10886   /* H is the actual vertical distance from the position in *IT
 10887      and the starting position.  */
 10888   h = it2.current_y - it->current_y;
 10889   /* NLINES is the distance in number of lines.  */
 10890   nlines = it2.vpos - it->vpos;
 10891 
 10892   /* Correct IT's y and vpos position
 10893      so that they are relative to the starting point.  */
 10894   it->vpos -= nlines;
 10895   it->current_y -= h;
 10896 
 10897   if (dy == 0)
 10898     {
 10899       /* DY == 0 means move to the start of the screen line.  The
 10900          value of nlines is > 0 if continuation lines were involved,
 10901          or if the original IT position was at start of a line.  */
 10902       RESTORE_IT (it, it, it2data);
 10903       if (nlines > 0)
 10904         move_it_by_lines (it, nlines);
 10905       /* The above code moves us to some position NLINES down,
 10906          usually to its first glyph (leftmost in an L2R line), but
 10907          that's not necessarily the start of the line, under bidi
 10908          reordering.  We want to get to the character position
 10909          that is immediately after the newline of the previous
 10910          line.  */
 10911       if (it->bidi_p
 10912           && !it->continuation_lines_width
 10913           && !STRINGP (it->string)
 10914           && IT_CHARPOS (*it) > BEGV
 10915           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10916         {
 10917           ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
 10918 
 10919           dec_both (&cp, &bp);
 10920           SET_WITH_NARROWED_BEGV (it, cp,
 10921                                   find_newline_no_quit (cp, bp, -1, NULL),
 10922                                   get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
 10923           move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
 10924         }
 10925       bidi_unshelve_cache (it3data, true);
 10926     }
 10927   else
 10928     {
 10929       /* The y-position we try to reach, relative to *IT.
 10930          Note that H has been subtracted in front of the if-statement.  */
 10931       int target_y = it->current_y + h - dy;
 10932       int y0 = it3.current_y;
 10933       int y1;
 10934       int line_height;
 10935 
 10936       RESTORE_IT (&it3, &it3, it3data);
 10937       y1 = line_bottom_y (&it3);
 10938       line_height = y1 - y0;
 10939       RESTORE_IT (it, it, it2data);
 10940       /* If we did not reach target_y, try to move further backward if
 10941          we can.  If we moved too far backward, try to move forward.  */
 10942       if (target_y < it->current_y
 10943           /* This is heuristic.  In a window that's 3 lines high, with
 10944              a line height of 13 pixels each, recentering with point
 10945              on the bottom line will try to move -39/2 = 19 pixels
 10946              backward.  Try to avoid moving into the first line.  */
 10947           && (it->current_y - target_y
 10948               > min (window_box_height (it->w), line_height * 2 / 3))
 10949           && IT_CHARPOS (*it) > BEGV)
 10950         {
 10951           move_trace ("  not far enough -> move_vert %d\n",
 10952                       target_y - it->current_y);
 10953           dy = it->current_y - target_y;
 10954           goto move_further_back;
 10955         }
 10956       else if (target_y >= it->current_y + line_height
 10957                && IT_CHARPOS (*it) < ZV)
 10958         {
 10959           /* Should move forward by at least one line, maybe more.
 10960 
 10961              Note: Calling move_it_by_lines can be expensive on
 10962              terminal frames, where compute_motion is used (via
 10963              vmotion) to do the job, when there are very long lines
 10964              and truncate-lines is nil.  That's the reason for
 10965              treating terminal frames specially here.  */
 10966 
 10967           if (!FRAME_WINDOW_P (it->f))
 10968             move_it_vertically (it, target_y - it->current_y);
 10969           else
 10970             {
 10971               struct text_pos last_pos;
 10972               int last_y, last_vpos;
 10973               do
 10974                 {
 10975                   last_pos = it->current.pos;
 10976                   last_y = it->current_y;
 10977                   last_vpos = it->vpos;
 10978                   move_it_by_lines (it, 1);
 10979                 }
 10980               while (target_y > it->current_y && IT_CHARPOS (*it) < ZV);
 10981               if (it->current_y > target_y)
 10982                 {
 10983                   reseat (it, last_pos, true);
 10984                   it->current_y = last_y;
 10985                   it->vpos = last_vpos;
 10986                 }
 10987             }
 10988         }
 10989     }
 10990 }
 10991 
 10992 
 10993 /* Move IT by a specified amount of pixel lines DY.  DY negative means
 10994    move backwards.  DY = 0 means move to start of screen line.  At the
 10995    end, IT will be on the start of a screen line.  */
 10996 
 10997 void
 10998 move_it_vertically (struct it *it, int dy)
 10999 {
 11000   if (dy <= 0)
 11001     move_it_vertically_backward (it, -dy);
 11002   else
 11003     {
 11004       move_trace ("move_it_v: from %td, %d\n", IT_CHARPOS (*it), dy);
 11005       move_it_to (it, ZV, -1, it->current_y + dy, -1,
 11006                   MOVE_TO_POS | MOVE_TO_Y);
 11007       move_trace ("move_it_v: to %td\n", IT_CHARPOS (*it));
 11008 
 11009       /* If buffer ends in ZV without a newline, move to the start of
 11010          the line to satisfy the post-condition.  */
 11011       if (IT_CHARPOS (*it) == ZV
 11012           && ZV > BEGV
 11013           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 11014         move_it_by_lines (it, 0);
 11015     }
 11016 }
 11017 
 11018 
 11019 /* Move iterator IT past the end of the text line it is in.  */
 11020 
 11021 void
 11022 move_it_past_eol (struct it *it)
 11023 {
 11024   enum move_it_result rc;
 11025 
 11026   rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
 11027   if (rc == MOVE_NEWLINE_OR_CR)
 11028     set_iterator_to_next (it, false);
 11029 }
 11030 
 11031 
 11032 /* Move IT by a specified number DVPOS of screen lines down.  DVPOS
 11033    negative means move up.  DVPOS == 0 means move to the start of the
 11034    screen line.
 11035 
 11036    Optimization idea: If we would know that IT->f doesn't use
 11037    a face with proportional font, we could be faster for
 11038    truncate-lines nil.  */
 11039 
 11040 void
 11041 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
 11042 {
 11043 
 11044   /* The commented-out optimization uses vmotion on terminals.  This
 11045      gives bad results, because elements like it->what, on which
 11046      callers such as pos_visible_p rely, aren't updated.  */
 11047   /* struct position pos;
 11048     if (!FRAME_WINDOW_P (it->f))
 11049     {
 11050       struct text_pos textpos;
 11051 
 11052       pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
 11053       SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
 11054       reseat (it, textpos, true);
 11055       it->vpos += pos.vpos;
 11056       it->current_y += pos.vpos;
 11057     }
 11058     else */
 11059 
 11060   if (dvpos == 0)
 11061     {
 11062       /* DVPOS == 0 means move to the start of the screen line.  */
 11063       move_it_vertically_backward (it, 0);
 11064       /* Let next call to line_bottom_y calculate real line height.  */
 11065       last_height = 0;
 11066     }
 11067   else if (dvpos > 0)
 11068     {
 11069       move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
 11070       if (!IT_POS_VALID_AFTER_MOVE_P (it))
 11071         {
 11072           /* Only move to the next buffer position if we ended up in a
 11073              string from display property, not in an overlay string
 11074              (before-string or after-string).  That is because the
 11075              latter don't conceal the underlying buffer position, so
 11076              we can ask to move the iterator to the exact position we
 11077              are interested in.  Note that, even if we are already at
 11078              IT_CHARPOS (*it), the call below is not a no-op, as it
 11079              will detect that we are at the end of the string, pop the
 11080              iterator, and compute it->current_x and it->hpos
 11081              correctly.  */
 11082           move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
 11083                       -1, -1, -1, MOVE_TO_POS);
 11084         }
 11085     }
 11086   else
 11087     {
 11088       struct it it2;
 11089       void *it2data = NULL;
 11090       ptrdiff_t start_charpos, orig_charpos, i;
 11091       int nchars_per_row
 11092         = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 11093       bool hit_pos_limit = false;
 11094       ptrdiff_t pos_limit;
 11095 
 11096       /* Start at the beginning of the screen line containing IT's
 11097          position.  This may actually move vertically backwards,
 11098          in case of overlays, so adjust dvpos accordingly.  */
 11099       dvpos += it->vpos;
 11100       orig_charpos = IT_CHARPOS (*it);
 11101       move_it_vertically_backward (it, 0);
 11102       dvpos -= it->vpos;
 11103 
 11104       /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
 11105          screen lines, and reseat the iterator there.  */
 11106       start_charpos = IT_CHARPOS (*it);
 11107       if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 11108         pos_limit = BEGV;
 11109       else
 11110         pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
 11111 
 11112       for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
 11113         back_to_previous_visible_line_start (it);
 11114       if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
 11115         hit_pos_limit = true;
 11116       reseat (it, it->current.pos, true);
 11117 
 11118       /* Move further back if we end up in a string or an image.  */
 11119       while (!IT_POS_VALID_AFTER_MOVE_P (it))
 11120         {
 11121           /* First try to move to start of display line.  */
 11122           dvpos += it->vpos;
 11123           move_it_vertically_backward (it, 0);
 11124           dvpos -= it->vpos;
 11125           if (IT_POS_VALID_AFTER_MOVE_P (it))
 11126             break;
 11127           /* If start of line is still in string or image,
 11128              move further back.  */
 11129           back_to_previous_visible_line_start (it);
 11130           reseat (it, it->current.pos, true);
 11131           dvpos--;
 11132         }
 11133 
 11134       it->current_x = it->hpos = 0;
 11135 
 11136       /* Above call may have moved too far if continuation lines
 11137          are involved.  Scan forward and see if it did.  */
 11138       SAVE_IT (it2, *it, it2data);
 11139       it2.vpos = it2.current_y = 0;
 11140       move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
 11141       it->vpos -= it2.vpos;
 11142       it->current_y -= it2.current_y;
 11143       it->current_x = it->hpos = 0;
 11144 
 11145       /* If we moved too far back, move IT some lines forward.  */
 11146       if (it2.vpos > -dvpos)
 11147         {
 11148           int delta = it2.vpos + dvpos;
 11149 
 11150           RESTORE_IT (&it2, &it2, it2data);
 11151           SAVE_IT (it2, *it, it2data);
 11152           move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
 11153           /* Move back again if we got too far ahead,
 11154              or didn't move at all.  */
 11155           if (it->vpos - it2.vpos > delta || IT_CHARPOS (*it) == orig_charpos)
 11156             RESTORE_IT (it, &it2, it2data);
 11157           else
 11158             bidi_unshelve_cache (it2data, true);
 11159         }
 11160       else if (hit_pos_limit && pos_limit > BEGV
 11161                && dvpos < 0 && it2.vpos < -dvpos)
 11162         {
 11163           /* If we hit the limit, but still didn't make it far enough
 11164              back, that means there's a display string with a newline
 11165              covering a large chunk of text, and that caused
 11166              back_to_previous_visible_line_start try to go too far.
 11167              Punish those who commit such atrocities by going back
 11168              until we've reached DVPOS, after lifting the limit, which
 11169              could make it slow for very long lines.  "If it hurts,
 11170              don't do that!"  */
 11171           dvpos += it2.vpos;
 11172           RESTORE_IT (it, it, it2data);
 11173           for (i = -dvpos; i > 0; --i)
 11174             {
 11175               back_to_previous_visible_line_start (it);
 11176               it->vpos--;
 11177             }
 11178           reseat_1 (it, it->current.pos, true);
 11179         }
 11180       else
 11181         RESTORE_IT (it, it, it2data);
 11182     }
 11183 }
 11184 
 11185 int
 11186 partial_line_height (struct it *it_origin)
 11187 {
 11188   /* In a buffer with very long and truncated lines, we ignore the
 11189      possibly-partial height of the last line in the window: it is too
 11190      expensive to compute that (since in most cases that involves
 11191      going all the way to ZV), and the effect of ignoring it is
 11192      relatively minor.  */
 11193   if (XBUFFER (it_origin->w->contents)->long_line_optimizations_p
 11194       && it_origin->line_wrap == TRUNCATE)
 11195     return 0;
 11196 
 11197   int partial_height;
 11198   void *it_data = NULL;
 11199   struct it it;
 11200   SAVE_IT (it, *it_origin, it_data);
 11201   move_it_to (&it, ZV, -1, it.last_visible_y, -1,
 11202               MOVE_TO_POS | MOVE_TO_Y);
 11203   if (it.what == IT_EOB)
 11204     {
 11205       int vis_height = it.last_visible_y - it.current_y;
 11206       int height = it.ascent + it.descent;
 11207       partial_height = (vis_height < height) ? vis_height : 0;
 11208     }
 11209   else
 11210     {
 11211       int last_line_y = it.current_y;
 11212       move_it_by_lines (&it, 1);
 11213       partial_height = (it.current_y > it.last_visible_y)
 11214         ? it.last_visible_y - last_line_y : 0;
 11215     }
 11216   RESTORE_IT (&it, &it, it_data);
 11217   return partial_height;
 11218 }
 11219 
 11220 /* Approximate move_it_in_display_line_to for very long and truncated
 11221    display lines, when moving horizontally.  This is used when the
 11222    buffer's long_line_optimizations_p flag is set.  It ignores various
 11223    complications, like different font sizes, invisible text, display
 11224    and overlay strings, and, to some degree, bidirectional text.  So
 11225    caveat emptor!
 11226 
 11227    Starting from IT's position, reseat IT after skipping NCHARS
 11228    characters or to the next newline/ZV, whichever comes first.  Return
 11229    what move_it_in_display_line_to would have returned in this case.  */
 11230 
 11231 static enum move_it_result
 11232 fast_move_it_horizontally (struct it *it, ptrdiff_t nchars)
 11233 {
 11234   ptrdiff_t nl_bytepos;
 11235   ptrdiff_t nl_pos = find_newline_no_quit (IT_CHARPOS (*it), IT_BYTEPOS (*it),
 11236                                            1, &nl_bytepos);
 11237   struct text_pos new_pos;
 11238   enum move_it_result move_result;
 11239 
 11240   if (nl_pos - IT_CHARPOS (*it) > nchars)
 11241     {
 11242       SET_TEXT_POS (new_pos,
 11243                     IT_CHARPOS (*it) + nchars,
 11244                     CHAR_TO_BYTE (IT_CHARPOS (*it) + nchars));
 11245       move_result = MOVE_X_REACHED;
 11246     }
 11247   else
 11248     {
 11249       if (nl_bytepos < ZV_BYTE
 11250           || (nl_bytepos > BEGV_BYTE
 11251               && FETCH_BYTE (nl_bytepos - 1) == '\n'))
 11252         {
 11253           nl_pos--;
 11254           nl_bytepos--;
 11255           move_result = MOVE_NEWLINE_OR_CR;
 11256         }
 11257       else
 11258         move_result = MOVE_POS_MATCH_OR_ZV;
 11259       SET_TEXT_POS (new_pos, nl_pos, nl_bytepos);
 11260     }
 11261   reseat (it, new_pos, false);
 11262   return move_result;
 11263 }
 11264 
 11265 /* Return true if IT points into the middle of a display vector.  */
 11266 
 11267 bool
 11268 in_display_vector_p (struct it *it)
 11269 {
 11270   return (it->method == GET_FROM_DISPLAY_VECTOR
 11271           && it->current.dpvec_index > 0
 11272           && it->dpvec + it->current.dpvec_index != it->dpend);
 11273 }
 11274 
 11275 /* This is like Fwindow_text_pixel_size but assumes that WINDOW's buffer
 11276    is the current buffer.  Fbuffer_text_pixel_size calls it after it has
 11277    set WINDOW's buffer to the buffer specified by its BUFFER_OR_NAME
 11278    argument.  */
 11279 static Lisp_Object
 11280 window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 11281                         Lisp_Object x_limit, Lisp_Object y_limit,
 11282                         Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11283 {
 11284   struct window *w = decode_live_window (window);
 11285   struct it it;
 11286   ptrdiff_t start, end, bpos;
 11287   struct text_pos startp;
 11288   void *itdata = NULL;
 11289   int c, max_x = 0, max_y = 0, x = 0, y = 0, vertical_offset = 0, doff = 0;
 11290 
 11291   if (NILP (from))
 11292     {
 11293       start = BEGV;
 11294       bpos = BEGV_BYTE;
 11295     }
 11296   else if (EQ (from, Qt))
 11297     {
 11298       start = BEGV;
 11299       bpos = BEGV_BYTE;
 11300       while (bpos < ZV_BYTE)
 11301         {
 11302           c = FETCH_BYTE (bpos);
 11303           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11304             break;
 11305           inc_both (&start, &bpos);
 11306         }
 11307       while (bpos > BEGV_BYTE)
 11308         {
 11309           dec_both (&start, &bpos);
 11310           c = FETCH_BYTE (bpos);
 11311           if (!(c == ' ' || c == '\t'))
 11312             break;
 11313         }
 11314     }
 11315   else if (CONSP (from))
 11316     {
 11317       start = clip_to_bounds (BEGV, fix_position (XCAR (from)), ZV);
 11318       bpos = CHAR_TO_BYTE (start);
 11319       CHECK_FIXNUM (XCDR (from));
 11320       vertical_offset = XFIXNUM (XCDR (from));
 11321     }
 11322   else
 11323     {
 11324       start = clip_to_bounds (BEGV, fix_position (from), ZV);
 11325       bpos = CHAR_TO_BYTE (start);
 11326     }
 11327 
 11328   SET_TEXT_POS (startp, start, bpos);
 11329 
 11330   if (NILP (to))
 11331     end = ZV;
 11332   else if (EQ (to, Qt))
 11333     {
 11334       end = ZV;
 11335       bpos = ZV_BYTE;
 11336       while (bpos > BEGV_BYTE)
 11337         {
 11338           dec_both (&end, &bpos);
 11339           c = FETCH_BYTE (bpos);
 11340           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11341             {
 11342               inc_both (&end, &bpos);
 11343               break;
 11344             }
 11345         }
 11346       while (bpos < ZV_BYTE)
 11347         {
 11348           c = fetch_char_advance (&end, &bpos);
 11349           if (!(c == ' ' || c == '\t'))
 11350             break;
 11351         }
 11352     }
 11353   else
 11354     end = clip_to_bounds (start, fix_position (to), ZV);
 11355 
 11356   if (RANGED_FIXNUMP (0, x_limit, INT_MAX))
 11357     max_x = XFIXNUM (x_limit);
 11358   else if (!NILP (x_limit))
 11359     max_x = INT_MAX;
 11360 
 11361   if (NILP (y_limit))
 11362     max_y = INT_MAX;
 11363   else if (RANGED_FIXNUMP (0, y_limit, INT_MAX))
 11364     max_y = XFIXNUM (y_limit);
 11365 
 11366   itdata = bidi_shelve_cache ();
 11367 
 11368   start_display (&it, w, startp);
 11369 
 11370   int start_y = it.current_y;
 11371 
 11372   /* It makes no sense to measure dimensions of region of text that
 11373      crosses the point where bidi reordering changes scan direction.
 11374      By using unidirectional movement here we at least support the use
 11375      case of measuring regions of text that have a uniformly R2L
 11376      directionality, and regions that begin and end in text of the
 11377      same directionality.  */
 11378   it.bidi_p = false;
 11379 
 11380   int start_x;
 11381   if (vertical_offset != 0)
 11382     {
 11383       int last_y;
 11384       it.current_y = 0;
 11385 
 11386       move_it_by_lines (&it, 0);
 11387 
 11388       /* `move_it_vertically_backward' is called by move_it_vertically
 11389          to move by a negative value (upwards), but it is not always
 11390          guaranteed to leave the iterator at or above the position
 11391          given by the offset, which this loop ensures.  */
 11392       if (vertical_offset < 0)
 11393         {
 11394           while (it.current_y > vertical_offset)
 11395             {
 11396               last_y = it.current_y;
 11397               move_it_vertically_backward (&it,
 11398                                            (abs (vertical_offset)
 11399                                             + it.current_y));
 11400 
 11401               if (it.current_y == last_y)
 11402                 break;
 11403             }
 11404         }
 11405       else
 11406         {
 11407           move_it_vertically (&it, vertical_offset);
 11408         }
 11409 
 11410       it.current_y = (WINDOW_TAB_LINE_HEIGHT (w)
 11411                       + WINDOW_HEADER_LINE_HEIGHT (w));
 11412       start = clip_to_bounds (BEGV, IT_CHARPOS (it), ZV);
 11413       start_y = it.current_y;
 11414       start_x = it.current_x;
 11415     }
 11416   else
 11417     {
 11418       /* Start at the beginning of the line containing FROM.  Otherwise
 11419          IT.current_x will be incorrectly set to zero at some arbitrary
 11420          non-zero X coordinate.  */
 11421       reseat_at_previous_visible_line_start (&it);
 11422       it.current_x = it.hpos = 0;
 11423       if (IT_CHARPOS (it) != start)
 11424         {
 11425           void *it1data = NULL;
 11426           struct it it1;
 11427 
 11428           SAVE_IT (it1, it, it1data);
 11429           move_it_to (&it, start, -1, -1, -1, MOVE_TO_POS);
 11430           /* We could have a display property at START, in which case
 11431              asking move_it_to to stop at START will overshoot and
 11432              stop at position after START.  So we try again, stopping
 11433              before START, and account for the width of the last
 11434              buffer position manually.  */
 11435           if (IT_CHARPOS (it) > start && start > BEGV)
 11436             {
 11437               ptrdiff_t it1pos = IT_CHARPOS (it1);
 11438               int it1_x = it1.current_x;
 11439 
 11440               RESTORE_IT (&it, &it1, it1data);
 11441               /* If START - 1 is the beginning of screen line,
 11442                  move_it_to will not move, so we need to use a
 11443                  lower-level move_it_in_display_line subroutine, and
 11444                  tell it to move just 1 pixel, so it stops at the next
 11445                  display element.  */
 11446               if (start - 1 > it1pos)
 11447                 move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11448               else
 11449                 move_it_in_display_line (&it, start, it1_x + 1,
 11450                                          MOVE_TO_POS | MOVE_TO_X);
 11451               move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11452               start_x = it.current_x;
 11453               /* If we didn't change our buffer position, the pixel
 11454                  width of what's here was not yet accounted for; do it
 11455                  manually.  */
 11456               if (IT_CHARPOS (it) == start - 1)
 11457                 start_x += it.pixel_width;
 11458             }
 11459           else
 11460             {
 11461               start_x = it.current_x;
 11462               bidi_unshelve_cache (it1data, true);
 11463             }
 11464         }
 11465       else
 11466         start_x = it.current_x;
 11467     }
 11468 
 11469   /* Now move to TO.  */
 11470   int move_op = MOVE_TO_POS | MOVE_TO_Y;
 11471   int to_x = -1;
 11472   it.current_y = start_y;
 11473   /* If FROM is on a newline, pretend that we start at the beginning
 11474      of the next line, because the newline takes no place on display.  */
 11475   if (FETCH_BYTE (start) == '\n')
 11476     it.current_x = 0;
 11477   if (!NILP (x_limit))
 11478     {
 11479       it.last_visible_x = max_x;
 11480       /* Actually, we never want move_it_to stop at to_x.  But to make
 11481          sure that move_it_in_display_line_to always moves far enough,
 11482          we set to_x to INT_MAX and specify MOVE_TO_X.  */
 11483       move_op |= MOVE_TO_X;
 11484       to_x = INT_MAX;
 11485     }
 11486 
 11487   void *it2data = NULL;
 11488   struct it it2;
 11489   SAVE_IT (it2, it, it2data);
 11490 
 11491   x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11492 
 11493   /* We could have a display property at END, in which case asking
 11494      move_it_to to stop at END will overshoot and stop at position
 11495      after END.  So we try again, stopping before END, and account for
 11496      the width of the last buffer position manually.  */
 11497   if (IT_CHARPOS (it) > end)
 11498     {
 11499       end--;
 11500       RESTORE_IT (&it, &it2, it2data);
 11501       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11502       /* Add the width of the thing at TO, but only if we didn't
 11503          overshoot it; if we did, it is already accounted for.  Also,
 11504          account for the height of the thing at TO.  */
 11505       if (IT_CHARPOS (it) == end)
 11506         {
 11507           x += it.pixel_width;
 11508 
 11509           /* DTRT if ignore_line_at_end is t.  */
 11510           if (!NILP (ignore_line_at_end))
 11511             doff = (max (it.max_ascent, it.ascent)
 11512                     + max (it.max_descent, it.descent));
 11513           else
 11514             {
 11515               it.max_ascent = max (it.max_ascent, it.ascent);
 11516               it.max_descent = max (it.max_descent, it.descent);
 11517             }
 11518         }
 11519     }
 11520   else
 11521     bidi_unshelve_cache (it2data, true);
 11522 
 11523   if (!NILP (x_limit))
 11524     {
 11525       /* Don't return more than X-LIMIT.  */
 11526       if (x > max_x)
 11527         x = max_x;
 11528     }
 11529 
 11530   /* If text spans more than one screen line, we don't need to adjust
 11531      the x-span for start_x, since the second and subsequent lines
 11532      will begin at zero X coordinate.  */
 11533   if (it.current_y > start_y)
 11534     start_x = 0;
 11535 
 11536   /* Subtract height of header-line and tab-line which was counted
 11537      automatically by start_display.  */
 11538   if (!NILP (ignore_line_at_end))
 11539     y = (it.current_y + doff
 11540          - WINDOW_TAB_LINE_HEIGHT (w)
 11541          - WINDOW_HEADER_LINE_HEIGHT (w));
 11542   else
 11543     y = (it.current_y + it.max_ascent + it.max_descent + doff
 11544          - WINDOW_TAB_LINE_HEIGHT (w) - WINDOW_HEADER_LINE_HEIGHT (w));
 11545 
 11546   /* Don't return more than Y-LIMIT.  */
 11547   if (y > max_y)
 11548     y = max_y;
 11549 
 11550   if ((EQ (mode_lines, Qtab_line) || EQ (mode_lines, Qt))
 11551       && window_wants_tab_line (w))
 11552     /* Add height of tab-line as requested.  */
 11553     {
 11554       Lisp_Object window_tab_line_format
 11555         = window_parameter (w, Qtab_line_format);
 11556 
 11557       y = y + display_mode_line (w, TAB_LINE_FACE_ID,
 11558                                  NILP (window_tab_line_format)
 11559                                  ? BVAR (current_buffer, tab_line_format)
 11560                                  : window_tab_line_format);
 11561     }
 11562 
 11563   if ((EQ (mode_lines, Qheader_line) || EQ (mode_lines, Qt))
 11564       && window_wants_header_line (w))
 11565     {
 11566       Lisp_Object window_header_line_format
 11567         = window_parameter (w, Qheader_line_format);
 11568 
 11569       y = y + display_mode_line (w, HEADER_LINE_FACE_ID,
 11570                                  NILP (window_header_line_format)
 11571                                  ? BVAR (current_buffer, header_line_format)
 11572                                  : window_header_line_format);
 11573     }
 11574 
 11575   if ((EQ (mode_lines, Qmode_line) || EQ (mode_lines, Qt))
 11576       && window_wants_mode_line (w))
 11577     {
 11578       Lisp_Object window_mode_line_format
 11579         = window_parameter (w, Qmode_line_format);
 11580 
 11581       y = y + display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
 11582                                  NILP (window_mode_line_format)
 11583                                  ? BVAR (current_buffer, mode_line_format)
 11584                                  : window_mode_line_format);
 11585     }
 11586 
 11587   bidi_unshelve_cache (itdata, false);
 11588 
 11589   return (!vertical_offset
 11590           ? Fcons (make_fixnum (x - start_x), make_fixnum (y))
 11591           : list3i (x - start_x, y, start));
 11592 }
 11593 
 11594 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
 11595        doc: /* Return the size of the text of WINDOW's buffer in pixels.
 11596 WINDOW must be a live window and defaults to the selected one.  The
 11597 return value is a cons of the maximum pixel-width of any text line and
 11598 the pixel-height of all the text lines in the accessible portion of
 11599 buffer text.
 11600 
 11601 If FROM is a cons cell, the return value includes, in addition to the
 11602 dimensions, also a third element that provides the buffer position
 11603 from which measuring of the text dimensions was actually started.
 11604 
 11605 This function exists to allow Lisp programs to adjust the dimensions
 11606 of WINDOW to the buffer text it needs to display.
 11607 
 11608 The optional argument FROM, if non-nil, specifies the first text
 11609 position to consider, and defaults to the minimum accessible position
 11610 of the buffer.  If FROM is a cons, its car specifies a buffer
 11611 position, and its cdr specifies the vertical offset in pixels from
 11612 that position to the first screen line to be measured.  If FROM is t,
 11613 it stands for the minimum accessible position that starts a non-empty
 11614 line.  TO, if non-nil, specifies the last text position and defaults
 11615 to the maximum accessible position of the buffer.  If TO is t, it
 11616 stands for the maximum accessible position that ends a non-empty line.
 11617 
 11618 The optional argument X-LIMIT, if non-nil, specifies the maximum X
 11619 coordinate beyond which the text should be ignored.  It is therefore
 11620 also the maximum width that the function can return.  X-LIMIT nil or
 11621 omitted means to use the pixel-width of WINDOW's body.  This default
 11622 means text of truncated lines wider than the window will be ignored;
 11623 specify a non-nil value for X-LIMIT if lines are truncated and you need
 11624 to account for the truncated text.
 11625 
 11626 Use nil for X-LIMIT if you want to know how high WINDOW should become in
 11627 order to fit all of its buffer's text with the width of WINDOW
 11628 unaltered.  Use the maximum width WINDOW may assume if you intend to
 11629 change WINDOW's width.  Use t for the maximum possible value.  Since
 11630 calculating the width of long lines can take some time, it's always a
 11631 good idea to make this argument as small as possible; in particular, if
 11632 the buffer contains long lines that shall be truncated anyway.
 11633 
 11634 The optional argument Y-LIMIT, if non-nil, specifies the maximum Y
 11635 coordinate beyond which the text is to be ignored; it is therefore
 11636 also the maximum height that the function can return (excluding the
 11637 height of the mode- or header-line, if any).  Y-LIMIT nil or omitted
 11638 means consider all of the accessible portion of buffer text up to the
 11639 position specified by TO.  Since calculating the text height of a
 11640 large buffer can take some time, it makes sense to specify this
 11641 argument if the size of the buffer is large or unknown.
 11642 
 11643 Optional argument MODE-LINES nil or omitted means do not include the
 11644 height of the mode-, tab- or header-line of WINDOW in the return value.
 11645 If it is the symbol `mode-line', `tab-line' or `header-line', include
 11646 only the height of that line, if present, in the return value.  If t,
 11647 include the height of any of these, if present, in the return value.
 11648 
 11649 IGNORE-LINE-AT-END, if non-nil, means to not add the height of the
 11650 screen line that includes TO to the returned height of the text.  */)
 11651   (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
 11652    Lisp_Object y_limit, Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11653 {
 11654   struct window *w = decode_live_window (window);
 11655   struct buffer *b = XBUFFER (w->contents);
 11656   struct buffer *old_b = NULL;
 11657   Lisp_Object value;
 11658 
 11659   if (b != current_buffer)
 11660     {
 11661       old_b = current_buffer;
 11662       set_buffer_internal_1 (b);
 11663     }
 11664 
 11665   value = window_text_pixel_size (window, from, to, x_limit, y_limit, mode_lines,
 11666                                   ignore_line_at_end);
 11667 
 11668   if (old_b)
 11669     set_buffer_internal_1 (old_b);
 11670 
 11671   return value;
 11672 }
 11673 
 11674 DEFUN ("buffer-text-pixel-size", Fbuffer_text_pixel_size, Sbuffer_text_pixel_size, 0, 4, 0,
 11675        doc: /* Return size of whole text of BUFFER-OR-NAME in WINDOW.
 11676 BUFFER-OR-NAME must specify a live buffer or the name of a live buffer
 11677 and defaults to the current buffer.  WINDOW must be a live window and
 11678 defaults to the selected one.  The return value is a cons of the maximum
 11679 pixel-width of any text line and the pixel-height of all the text lines
 11680 of the buffer specified by BUFFER-OR-NAME.
 11681 
 11682 The optional arguments X-LIMIT and Y-LIMIT have the same meaning as with
 11683 `window-text-pixel-size'.
 11684 
 11685 Do not use this function if the buffer specified by BUFFER-OR-NAME is
 11686 already displayed in WINDOW.  `window-text-pixel-size' is cheaper in
 11687 that case because it does not have to temporarily show that buffer in
 11688 WINDOW.  */)
 11689   (Lisp_Object buffer_or_name, Lisp_Object window, Lisp_Object x_limit,
 11690    Lisp_Object y_limit)
 11691 {
 11692   struct window *w = decode_live_window (window);
 11693   struct buffer *b = (NILP (buffer_or_name)
 11694                       ? current_buffer
 11695                       : XBUFFER (Fget_buffer (buffer_or_name)));
 11696   Lisp_Object buffer, value;
 11697   specpdl_ref count = SPECPDL_INDEX ();
 11698 
 11699   XSETBUFFER (buffer, b);
 11700 
 11701   /* The unwind form of with_echo_area_buffer is what we need here to
 11702      make WINDOW temporarily show our buffer.  */
 11703   /* FIXME: Can we move this into the `if (!EQ (buffer, w->contents))`?  */
 11704   record_unwind_protect (unwind_with_echo_area_buffer,
 11705                          with_echo_area_buffer_unwind_data (w));
 11706 
 11707   set_buffer_internal_1 (b);
 11708 
 11709   ptrdiff_t base_line_pos = w->base_line_pos;
 11710   int end_valid = w->window_end_valid;
 11711   if (!EQ (buffer, w->contents))
 11712     {
 11713       wset_buffer (w, buffer);
 11714       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 11715       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 11716     }
 11717 
 11718   value = window_text_pixel_size (window, Qnil, Qnil, x_limit, y_limit, Qnil,
 11719                                   Qnil);
 11720 
 11721   unbind_to (count, Qnil);
 11722 
 11723   /* Restore original values.  This is important if this function is
 11724      called from some ':eval' form in the middle of redisplay.  */
 11725   w->base_line_pos = base_line_pos;
 11726   w->window_end_valid = end_valid;
 11727 
 11728   return value;
 11729 }
 11730 
 11731 
 11732 DEFUN ("display--line-is-continued-p", Fdisplay__line_is_continued_p,
 11733        Sdisplay__line_is_continued_p, 0, 0, 0,
 11734        doc: /* Return non-nil if the current screen line is continued on display.  */)
 11735   (void)
 11736 {
 11737   struct buffer *oldb = current_buffer;
 11738   struct window *w = XWINDOW (selected_window);
 11739   enum move_it_result rc = MOVE_POS_MATCH_OR_ZV;
 11740 
 11741   set_buffer_internal_1 (XBUFFER (w->contents));
 11742 
 11743   if (PT < ZV)
 11744     {
 11745       struct text_pos startpos;
 11746       struct it it;
 11747       void *itdata;
 11748       /* Use a marker, since vertical-motion enters redisplay, which can
 11749          trigger fontifications, which in turn could modify buffer text.  */
 11750       Lisp_Object opoint = Fpoint_marker ();
 11751 
 11752       /* Make sure to start from the beginning of the current screen
 11753          line, so that move_it_in_display_line_to counts pixels correctly.  */
 11754       Fvertical_motion (make_fixnum (0), selected_window, Qnil);
 11755       SET_TEXT_POS (startpos, PT, PT_BYTE);
 11756       itdata = bidi_shelve_cache ();
 11757       start_display (&it, w, startpos);
 11758       /* If lines are truncated, no line is continued.  */
 11759       if (it.line_wrap != TRUNCATE)
 11760         {
 11761           it.glyph_row = NULL;
 11762           rc = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 11763         }
 11764       SET_PT_BOTH (marker_position (opoint), marker_byte_position (opoint));
 11765       bidi_unshelve_cache (itdata, false);
 11766     }
 11767   set_buffer_internal_1 (oldb);
 11768 
 11769   return rc == MOVE_LINE_CONTINUED ? Qt : Qnil;
 11770 }
 11771 
 11772 
 11773 /***********************************************************************
 11774                                Messages
 11775  ***********************************************************************/
 11776 
 11777 /* Return the number of arguments the format string FORMAT needs.  */
 11778 
 11779 static ptrdiff_t
 11780 format_nargs (char const *format)
 11781 {
 11782   ptrdiff_t nargs = 0;
 11783   for (char const *p = format; (p = strchr (p, '%')); p++)
 11784     if (p[1] == '%')
 11785       p++;
 11786     else
 11787       nargs++;
 11788   return nargs;
 11789 }
 11790 
 11791 /* Add a message with format string FORMAT and formatted arguments
 11792    to *Messages*.  */
 11793 
 11794 void
 11795 add_to_log (const char *format, ...)
 11796 {
 11797   va_list ap;
 11798   va_start (ap, format);
 11799   vadd_to_log (format, ap);
 11800   va_end (ap);
 11801 }
 11802 
 11803 void
 11804 vadd_to_log (char const *format, va_list ap)
 11805 {
 11806   ptrdiff_t form_nargs = format_nargs (format);
 11807   ptrdiff_t nargs = 1 + form_nargs;
 11808   Lisp_Object args[10];
 11809   eassert (nargs <= ARRAYELTS (args));
 11810   AUTO_STRING (args0, format);
 11811   args[0] = args0;
 11812   for (ptrdiff_t i = 1; i <= nargs; i++)
 11813     args[i] = va_arg (ap, Lisp_Object);
 11814   Lisp_Object msg = Qnil;
 11815   msg = Fformat_message (nargs, args);
 11816 
 11817   ptrdiff_t len = SBYTES (msg) + 1;
 11818   USE_SAFE_ALLOCA;
 11819   char *buffer = SAFE_ALLOCA (len);
 11820   memcpy (buffer, SDATA (msg), len);
 11821 
 11822   message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
 11823   SAFE_FREE ();
 11824 }
 11825 
 11826 
 11827 /* Output a newline in the *Messages* buffer if "needs" one.  */
 11828 
 11829 void
 11830 message_log_maybe_newline (void)
 11831 {
 11832   if (message_log_need_newline)
 11833     message_dolog ("", 0, true, false);
 11834 }
 11835 
 11836 
 11837 /* Add a string M of length NBYTES to the message log, optionally
 11838    terminated with a newline when NLFLAG is true.  MULTIBYTE, if
 11839    true, means interpret the contents of M as multibyte.  This
 11840    function calls low-level routines in order to bypass text property
 11841    hooks, etc. which might not be safe to run.
 11842 
 11843    This may GC (insert may run before/after change hooks),
 11844    so the buffer M must NOT point to a Lisp string.  */
 11845 
 11846 void
 11847 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
 11848 {
 11849   const unsigned char *msg = (const unsigned char *) m;
 11850 
 11851   if (!NILP (Vmemory_full))
 11852     return;
 11853 
 11854   if (!NILP (Vmessage_log_max))
 11855     {
 11856       struct buffer *oldbuf;
 11857       Lisp_Object oldpoint, oldbegv, oldzv;
 11858       int old_windows_or_buffers_changed = windows_or_buffers_changed;
 11859       ptrdiff_t point_at_end = 0;
 11860       ptrdiff_t zv_at_end = 0;
 11861       Lisp_Object old_deactivate_mark;
 11862 
 11863       old_deactivate_mark = Vdeactivate_mark;
 11864       oldbuf = current_buffer;
 11865 
 11866       /* Sanity check, in case the variable has been set to something
 11867          invalid.  */
 11868       if (! STRINGP (Vmessages_buffer_name))
 11869         Vmessages_buffer_name = build_string ("*Messages*");
 11870       /* Ensure the Messages buffer exists, and switch to it.
 11871          If we created it, set the major-mode.  */
 11872       bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
 11873       Fset_buffer (Fget_buffer_create (Vmessages_buffer_name, Qnil));
 11874       if (newbuffer
 11875           && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
 11876         call0 (intern ("messages-buffer-mode"));
 11877 
 11878       bset_undo_list (current_buffer, Qt);
 11879       bset_cache_long_scans (current_buffer, Qnil);
 11880 
 11881       oldpoint = message_dolog_marker1;
 11882       set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
 11883       oldbegv = message_dolog_marker2;
 11884       set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
 11885       oldzv = message_dolog_marker3;
 11886       set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
 11887 
 11888       if (PT == Z)
 11889         point_at_end = 1;
 11890       if (ZV == Z)
 11891         zv_at_end = 1;
 11892 
 11893       BEGV = BEG;
 11894       BEGV_BYTE = BEG_BYTE;
 11895       ZV = Z;
 11896       ZV_BYTE = Z_BYTE;
 11897       TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11898 
 11899       /* Insert the string--maybe converting multibyte to single byte
 11900          or vice versa, so that all the text fits the buffer.  */
 11901       if (multibyte
 11902           && NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11903         {
 11904           /* Convert a multibyte string to single-byte
 11905              for the *Message* buffer.  */
 11906           for (ptrdiff_t i = 0; i < nbytes; )
 11907             {
 11908               int char_bytes, c = check_char_and_length (msg + i, &char_bytes);
 11909               char work = CHAR_TO_BYTE8 (c);
 11910               insert_1_both (&work, 1, 1, true, false, false);
 11911               i += char_bytes;
 11912             }
 11913         }
 11914       else if (! multibyte
 11915                && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11916         {
 11917           /* Convert a single-byte string to multibyte
 11918              for the *Message* buffer.  */
 11919           for (ptrdiff_t i = 0; i < nbytes; i++)
 11920             {
 11921               int c = make_char_multibyte (msg[i]);
 11922               unsigned char str[MAX_MULTIBYTE_LENGTH];
 11923               int char_bytes = CHAR_STRING (c, str);
 11924               insert_1_both ((char *) str, 1, char_bytes, true, false, false);
 11925             }
 11926         }
 11927       else if (nbytes)
 11928         insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
 11929                        true, false, false);
 11930 
 11931       if (nlflag)
 11932         {
 11933           ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 11934           intmax_t dups;
 11935 
 11936           /* Since we call del_range_both passing false for PREPARE,
 11937              we aren't prepared to run modification hooks (we could
 11938              end up calling modification hooks from another buffer and
 11939              only with AFTER=t, Bug#21824).  */
 11940           specpdl_ref count = SPECPDL_INDEX ();
 11941           specbind (Qinhibit_modification_hooks, Qt);
 11942 
 11943           insert_1_both ("\n", 1, 1, true, false, false);
 11944 
 11945           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
 11946           this_bol = PT;
 11947           this_bol_byte = PT_BYTE;
 11948 
 11949           /* See if this line duplicates the previous one.
 11950              If so, combine duplicates.  */
 11951           if (this_bol > BEG)
 11952             {
 11953               scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
 11954               prev_bol = PT;
 11955               prev_bol_byte = PT_BYTE;
 11956 
 11957               dups = message_log_check_duplicate (prev_bol_byte,
 11958                                                   this_bol_byte);
 11959               if (dups)
 11960                 {
 11961                   del_range_both (prev_bol, prev_bol_byte,
 11962                                   this_bol, this_bol_byte, false);
 11963                   if (dups > 1)
 11964                     {
 11965                       char dupstr[sizeof " [ times]"
 11966                                   + INT_STRLEN_BOUND (dups)];
 11967 
 11968                       /* If you change this format, don't forget to also
 11969                          change message_log_check_duplicate.  */
 11970                       int duplen = sprintf (dupstr, " [%"PRIdMAX" times]",
 11971                                             dups);
 11972                       TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
 11973                       insert_1_both (dupstr, duplen, duplen,
 11974                                      true, false, true);
 11975                     }
 11976                 }
 11977             }
 11978 
 11979           /* If we have more than the desired maximum number of lines
 11980              in the *Messages* buffer now, delete the oldest ones.
 11981              This is safe because we don't have undo in this buffer.  */
 11982 
 11983           if (FIXNATP (Vmessage_log_max))
 11984             {
 11985               scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
 11986                             -XFIXNAT (Vmessage_log_max) - 1, false);
 11987               del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
 11988             }
 11989 
 11990           unbind_to (count, Qnil);
 11991         }
 11992       BEGV = marker_position (oldbegv);
 11993       BEGV_BYTE = marker_byte_position (oldbegv);
 11994 
 11995       if (zv_at_end)
 11996         {
 11997           ZV = Z;
 11998           ZV_BYTE = Z_BYTE;
 11999         }
 12000       else
 12001         {
 12002           ZV = marker_position (oldzv);
 12003           ZV_BYTE = marker_byte_position (oldzv);
 12004         }
 12005 
 12006       if (point_at_end)
 12007         TEMP_SET_PT_BOTH (Z, Z_BYTE);
 12008       else
 12009         /* We can't do Fgoto_char (oldpoint) because it will run some
 12010            Lisp code.  */
 12011         TEMP_SET_PT_BOTH (marker_position (oldpoint),
 12012                           marker_byte_position (oldpoint));
 12013 
 12014       unchain_marker (XMARKER (oldpoint));
 12015       unchain_marker (XMARKER (oldbegv));
 12016       unchain_marker (XMARKER (oldzv));
 12017 
 12018       /* We called insert_1_both above with its 5th argument (PREPARE)
 12019          false, which prevents insert_1_both from calling
 12020          prepare_to_modify_buffer, which in turns prevents us from
 12021          incrementing windows_or_buffers_changed even if *Messages* is
 12022          shown in some window.  So we must manually set
 12023          windows_or_buffers_changed here to make up for that.  */
 12024       windows_or_buffers_changed = old_windows_or_buffers_changed;
 12025       bset_redisplay (current_buffer);
 12026 
 12027       set_buffer_internal (oldbuf);
 12028 
 12029       message_log_need_newline = !nlflag;
 12030       Vdeactivate_mark = old_deactivate_mark;
 12031     }
 12032 }
 12033 
 12034 
 12035 /* We are at the end of the buffer after just having inserted a newline.
 12036    (Note: We depend on the fact we won't be crossing the gap.)
 12037    Check to see if the most recent message looks a lot like the previous one.
 12038    Return 0 if different, 1 if the new one should just replace it, or a
 12039    value N > 1 if we should also append " [N times]".  */
 12040 
 12041 static intmax_t
 12042 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
 12043 {
 12044   ptrdiff_t i;
 12045   ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
 12046   bool seen_dots = false;
 12047   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
 12048   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
 12049 
 12050   for (i = 0; i < len; i++)
 12051     {
 12052       if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
 12053         seen_dots = true;
 12054       if (p1[i] != p2[i])
 12055         return seen_dots;
 12056     }
 12057   p1 += len;
 12058   if (*p1 == '\n')
 12059     return 2;
 12060   if (*p1++ == ' ' && *p1++ == '[')
 12061     {
 12062       char *pend;
 12063       intmax_t n = strtoimax ((char *) p1, &pend, 10);
 12064       if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
 12065         return n + 1;
 12066     }
 12067   return 0;
 12068 }
 12069 
 12070 
 12071 /* Display an echo area message M with a specified length of NBYTES
 12072    bytes.  The string may include null characters.  If M is not a
 12073    string, clear out any existing message, and let the mini-buffer
 12074    text show through.
 12075 
 12076    This function cancels echoing.  */
 12077 
 12078 void
 12079 message3 (Lisp_Object m)
 12080 {
 12081   clear_message (true, true);
 12082   cancel_echoing ();
 12083 
 12084   /* First flush out any partial line written with print.  */
 12085   message_log_maybe_newline ();
 12086   if (STRINGP (m))
 12087     {
 12088       ptrdiff_t nbytes = SBYTES (m);
 12089       bool multibyte = STRING_MULTIBYTE (m);
 12090       char *buffer;
 12091       USE_SAFE_ALLOCA;
 12092       SAFE_ALLOCA_STRING (buffer, m);
 12093       message_dolog (buffer, nbytes, true, multibyte);
 12094       SAFE_FREE ();
 12095     }
 12096   if (! inhibit_message)
 12097     message3_nolog (m);
 12098 }
 12099 
 12100 /* Log the message M to stderr.  Log an empty line if M is not a string.  */
 12101 
 12102 static void
 12103 message_to_stderr (Lisp_Object m)
 12104 {
 12105   if (noninteractive_need_newline)
 12106     {
 12107       noninteractive_need_newline = false;
 12108       errputc ('\n');
 12109     }
 12110   if (STRINGP (m))
 12111     {
 12112       Lisp_Object coding_system = Vlocale_coding_system;
 12113       Lisp_Object s;
 12114 
 12115       if (!NILP (Vcoding_system_for_write))
 12116         coding_system = Vcoding_system_for_write;
 12117       if (!NILP (coding_system))
 12118         s = code_convert_string_norecord (m, coding_system, true);
 12119       else
 12120         s = m;
 12121 
 12122       errwrite (SDATA (s), SBYTES (s));
 12123     }
 12124   if (STRINGP (m) || !cursor_in_echo_area)
 12125     errputc ('\n');
 12126 }
 12127 
 12128 /* The non-logging version of message3.
 12129    This does not cancel echoing, because it is used for echoing.
 12130    Perhaps we need to make a separate function for echoing
 12131    and make this cancel echoing.  */
 12132 
 12133 void
 12134 message3_nolog (Lisp_Object m)
 12135 {
 12136   struct frame *sf = SELECTED_FRAME ();
 12137 
 12138   if (FRAME_INITIAL_P (sf))
 12139     message_to_stderr (m);
 12140   /* Error messages get reported properly by cmd_error, so this must be just an
 12141      informative message; if the frame hasn't really been initialized yet, just
 12142      toss it.  */
 12143   else if (INTERACTIVE && sf->glyphs_initialized_p)
 12144     {
 12145       /* Get the frame containing the mini-buffer
 12146          that the selected frame is using.  */
 12147       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 12148       Lisp_Object frame = XWINDOW (mini_window)->frame;
 12149       struct frame *f = XFRAME (frame);
 12150 
 12151       if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
 12152         Fmake_frame_visible (frame);
 12153 
 12154       if (STRINGP (m) && SCHARS (m) > 0)
 12155         {
 12156           set_message (m);
 12157           if (minibuffer_auto_raise)
 12158             Fraise_frame (frame);
 12159           /* Assume we are not echoing.
 12160              (If we are, echo_now will override this.)  */
 12161           echo_message_buffer = Qnil;
 12162         }
 12163       else
 12164         clear_message (true, true);
 12165 
 12166       do_pending_window_change (false);
 12167       echo_area_display (true);
 12168       do_pending_window_change (false);
 12169       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 12170         (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
 12171     }
 12172 }
 12173 
 12174 
 12175 /* Display a null-terminated echo area message M.  If M is 0, clear
 12176    out any existing message, and let the mini-buffer text show through.
 12177 
 12178    The buffer M must continue to exist until after the echo area gets
 12179    cleared or some other message gets displayed there.  Do not pass
 12180    text that is stored in a Lisp string.  Do not pass text in a buffer
 12181    that was alloca'd.  */
 12182 
 12183 void
 12184 message1 (const char *m)
 12185 {
 12186   message3 (m ? build_unibyte_string (m) : Qnil);
 12187 }
 12188 
 12189 
 12190 /* The non-logging counterpart of message1.  */
 12191 
 12192 void
 12193 message1_nolog (const char *m)
 12194 {
 12195   message3_nolog (m ? build_unibyte_string (m) : Qnil);
 12196 }
 12197 
 12198 /* Display a message M which contains a single %s
 12199    which gets replaced with STRING.  */
 12200 
 12201 void
 12202 message_with_string (const char *m, Lisp_Object string, bool log)
 12203 {
 12204   CHECK_STRING (string);
 12205 
 12206   bool need_message;
 12207   if (noninteractive)
 12208     need_message = !!m;
 12209   else if (!INTERACTIVE)
 12210     need_message = false;
 12211   else
 12212     {
 12213       /* The frame whose minibuffer we're going to display the message on.
 12214          It may be larger than the selected frame, so we need
 12215          to use its buffer, not the selected frame's buffer.  */
 12216       Lisp_Object mini_window;
 12217       struct frame *f, *sf = SELECTED_FRAME ();
 12218 
 12219       /* Get the frame containing the minibuffer
 12220          that the selected frame is using.  */
 12221       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12222       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12223 
 12224       /* Error messages get reported properly by cmd_error, so this must be
 12225          just an informative message; if the frame hasn't really been
 12226          initialized yet, just toss it.  */
 12227       need_message = f->glyphs_initialized_p;
 12228     }
 12229 
 12230   if (need_message)
 12231     {
 12232       AUTO_STRING (fmt, m);
 12233       Lisp_Object msg = CALLN (Fformat_message, fmt, string);
 12234 
 12235       if (noninteractive)
 12236         message_to_stderr (msg);
 12237       else
 12238         {
 12239           if (log)
 12240             message3 (msg);
 12241           else
 12242             message3_nolog (msg);
 12243 
 12244           /* Print should start at the beginning of the message
 12245              buffer next time.  */
 12246           message_buf_print = false;
 12247         }
 12248     }
 12249 }
 12250 
 12251 
 12252 /* Dump an informative message to the minibuf.  If M is 0, clear out
 12253    any existing message, and let the mini-buffer text show through.
 12254 
 12255    The message must be safe ASCII (because when Emacs is
 12256    non-interactive the message is sent straight to stderr without
 12257    encoding first) and the format must not contain ` or ' (because
 12258    this function does not account for `text-quoting-style').  If your
 12259    message and format do not fit into this category, convert your
 12260    arguments to Lisp objects and use Fmessage instead.  */
 12261 
 12262 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 12263 vmessage (const char *m, va_list ap)
 12264 {
 12265   if (noninteractive)
 12266     {
 12267       if (m)
 12268         {
 12269           if (noninteractive_need_newline)
 12270             putc ('\n', stderr);
 12271           noninteractive_need_newline = false;
 12272           vfprintf (stderr, m, ap);
 12273           if (!cursor_in_echo_area)
 12274             putc ('\n', stderr);
 12275           fflush (stderr);
 12276         }
 12277     }
 12278   else if (INTERACTIVE)
 12279     {
 12280       /* The frame whose mini-buffer we're going to display the message
 12281          on.  It may be larger than the selected frame, so we need to
 12282          use its buffer, not the selected frame's buffer.  */
 12283       Lisp_Object mini_window;
 12284       struct frame *f, *sf = SELECTED_FRAME ();
 12285 
 12286       /* Get the frame containing the mini-buffer
 12287          that the selected frame is using.  */
 12288       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12289       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12290 
 12291       /* Error messages get reported properly by cmd_error, so this must be
 12292          just an informative message; if the frame hasn't really been
 12293          initialized yet, just toss it.  */
 12294       if (f->glyphs_initialized_p)
 12295         {
 12296           if (m)
 12297             {
 12298               ptrdiff_t len;
 12299               ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
 12300               USE_SAFE_ALLOCA;
 12301               char *message_buf = SAFE_ALLOCA (maxsize + 1);
 12302 
 12303               len = doprnt (message_buf, maxsize, m, 0, ap);
 12304 
 12305               message3 (make_string (message_buf, len));
 12306               SAFE_FREE ();
 12307             }
 12308           else
 12309             message1 (0);
 12310 
 12311           /* Print should start at the beginning of the message
 12312              buffer next time.  */
 12313           message_buf_print = false;
 12314         }
 12315     }
 12316 }
 12317 
 12318 /* See vmessage for restrictions on the text of the message.  */
 12319 void
 12320 message (const char *m, ...)
 12321 {
 12322   va_list ap;
 12323   va_start (ap, m);
 12324   vmessage (m, ap);
 12325   va_end (ap);
 12326 }
 12327 
 12328 
 12329 /* Display the current message in the current mini-buffer.  This is
 12330    only called from error handlers in process.c, and is not time
 12331    critical.  */
 12332 
 12333 void
 12334 update_echo_area (void)
 12335 {
 12336   if (!NILP (echo_area_buffer[0]))
 12337     {
 12338       Lisp_Object string;
 12339       string = Fcurrent_message ();
 12340       message3 (string);
 12341     }
 12342 }
 12343 
 12344 
 12345 /* Make sure echo area buffers in `echo_buffers' are live.
 12346    If they aren't, make new ones.  */
 12347 
 12348 static void
 12349 ensure_echo_area_buffers (void)
 12350 {
 12351   for (int i = 0; i < 2; i++)
 12352     if (!BUFFERP (echo_buffer[i])
 12353         || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
 12354       {
 12355         Lisp_Object old_buffer = echo_buffer[i];
 12356         static char const name_fmt[] = " *Echo Area %d*";
 12357         char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
 12358         AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
 12359         echo_buffer[i] = Fget_buffer_create (lname, Qnil);
 12360         bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
 12361         /* to force word wrap in echo area -
 12362            it was decided to postpone this*/
 12363         /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
 12364 
 12365         for (int j = 0; j < 2; j++)
 12366           if (EQ (old_buffer, echo_area_buffer[j]))
 12367             echo_area_buffer[j] = echo_buffer[i];
 12368       }
 12369 }
 12370 
 12371 
 12372 /* Call FN with args A1..A2 with either the current or last displayed
 12373    echo_area_buffer as current buffer.
 12374 
 12375    WHICH zero means use the current message buffer
 12376    echo_area_buffer[0].  If that is nil, choose a suitable buffer
 12377    from echo_buffer[] and clear it.
 12378 
 12379    WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
 12380    suitable buffer from echo_buffer[] and clear it.
 12381 
 12382    If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
 12383    that the current message becomes the last displayed one, choose a
 12384    suitable buffer for echo_area_buffer[0], and clear it.
 12385 
 12386    Value is what FN returns.  */
 12387 
 12388 static bool
 12389 with_echo_area_buffer (struct window *w, int which,
 12390                        bool (*fn) (void *, Lisp_Object),
 12391                        void *a1, Lisp_Object a2)
 12392 {
 12393   Lisp_Object buffer;
 12394   bool this_one, the_other, clear_buffer_p, rc;
 12395   specpdl_ref count = SPECPDL_INDEX ();
 12396 
 12397   /* If buffers aren't live, make new ones.  */
 12398   ensure_echo_area_buffers ();
 12399 
 12400   clear_buffer_p = false;
 12401 
 12402   if (which == 0)
 12403     this_one = false, the_other = true;
 12404   else if (which > 0)
 12405     this_one = true, the_other = false;
 12406   else
 12407     {
 12408       this_one = false, the_other = true;
 12409       clear_buffer_p = true;
 12410 
 12411       /* We need a fresh one in case the current echo buffer equals
 12412          the one containing the last displayed echo area message.  */
 12413       if (!NILP (echo_area_buffer[this_one])
 12414           && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
 12415         echo_area_buffer[this_one] = Qnil;
 12416     }
 12417 
 12418   /* Choose a suitable buffer from echo_buffer[] if we don't
 12419      have one.  */
 12420   if (NILP (echo_area_buffer[this_one]))
 12421     {
 12422       echo_area_buffer[this_one]
 12423         = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
 12424            ? echo_buffer[the_other]
 12425            : echo_buffer[this_one]);
 12426       clear_buffer_p = true;
 12427     }
 12428 
 12429   buffer = echo_area_buffer[this_one];
 12430 
 12431   /* Don't get confused by reusing the buffer used for echoing
 12432      for a different purpose.  */
 12433   if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
 12434     cancel_echoing ();
 12435 
 12436   record_unwind_protect (unwind_with_echo_area_buffer,
 12437                          with_echo_area_buffer_unwind_data (w));
 12438 
 12439   /* Make the echo area buffer current.  Note that for display
 12440      purposes, it is not necessary that the displayed window's buffer
 12441      == current_buffer, except for text property lookup.  So, let's
 12442      only set that buffer temporarily here without doing a full
 12443      Fset_window_buffer.  We must also change w->pointm, though,
 12444      because otherwise an assertions in unshow_buffer fails, and Emacs
 12445      aborts.  */
 12446   set_buffer_internal_1 (XBUFFER (buffer));
 12447   if (w)
 12448     {
 12449       wset_buffer (w, buffer);
 12450       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 12451       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 12452     }
 12453 
 12454   bset_undo_list (current_buffer, Qt);
 12455   bset_read_only (current_buffer, Qnil);
 12456   specbind (Qinhibit_read_only, Qt);
 12457   specbind (Qinhibit_modification_hooks, Qt);
 12458 
 12459   if (clear_buffer_p && Z > BEG)
 12460     del_range (BEG, Z);
 12461 
 12462   eassert (BEGV >= BEG);
 12463   eassert (ZV <= Z && ZV >= BEGV);
 12464 
 12465   rc = fn (a1, a2);
 12466 
 12467   eassert (BEGV >= BEG);
 12468   eassert (ZV <= Z && ZV >= BEGV);
 12469 
 12470   unbind_to (count, Qnil);
 12471   return rc;
 12472 }
 12473 
 12474 
 12475 /* Save state that should be preserved around the call to the function
 12476    FN called in with_echo_area_buffer.  */
 12477 
 12478 static Lisp_Object
 12479 with_echo_area_buffer_unwind_data (struct window *w)
 12480 {
 12481   int i = 0;
 12482   Lisp_Object vector, tmp;
 12483 
 12484   /* Reduce consing by keeping one vector in
 12485      Vwith_echo_area_save_vector.  */
 12486   vector = Vwith_echo_area_save_vector;
 12487   Vwith_echo_area_save_vector = Qnil;
 12488 
 12489   if (NILP (vector))
 12490     vector = make_nil_vector (11);
 12491 
 12492   XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
 12493   ASET (vector, i, Vdeactivate_mark); ++i;
 12494   ASET (vector, i, make_fixnum (windows_or_buffers_changed)); ++i;
 12495 
 12496   if (w)
 12497     {
 12498       XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
 12499       ASET (vector, i, w->contents); ++i;
 12500       ASET (vector, i, make_fixnum (marker_position (w->pointm))); ++i;
 12501       ASET (vector, i, make_fixnum (marker_byte_position (w->pointm))); ++i;
 12502       ASET (vector, i, make_fixnum (marker_position (w->old_pointm))); ++i;
 12503       ASET (vector, i, make_fixnum (marker_byte_position (w->old_pointm))); ++i;
 12504       ASET (vector, i, make_fixnum (marker_position (w->start))); ++i;
 12505       ASET (vector, i, make_fixnum (marker_byte_position (w->start))); ++i;
 12506     }
 12507   else
 12508     {
 12509       int end = i + 8;
 12510       for (; i < end; ++i)
 12511         ASET (vector, i, Qnil);
 12512     }
 12513 
 12514   eassert (i == ASIZE (vector));
 12515   return vector;
 12516 }
 12517 
 12518 
 12519 /* Restore global state from VECTOR which was created by
 12520    with_echo_area_buffer_unwind_data.  */
 12521 
 12522 static void
 12523 unwind_with_echo_area_buffer (Lisp_Object vector)
 12524 {
 12525   set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
 12526   Vdeactivate_mark = AREF (vector, 1);
 12527   windows_or_buffers_changed = XFIXNAT (AREF (vector, 2));
 12528 
 12529   if (WINDOWP (AREF (vector, 3)))
 12530     {
 12531       struct window *w;
 12532       Lisp_Object buffer;
 12533 
 12534       w = XWINDOW (AREF (vector, 3));
 12535       buffer = AREF (vector, 4);
 12536 
 12537       wset_buffer (w, buffer);
 12538       set_marker_restricted_both (w->pointm, buffer,
 12539                                   XFIXNAT (AREF (vector, 5)),
 12540                                   XFIXNAT (AREF (vector, 6)));
 12541       set_marker_restricted_both (w->old_pointm, buffer,
 12542                                   XFIXNAT (AREF (vector, 7)),
 12543                                   XFIXNAT (AREF (vector, 8)));
 12544       set_marker_restricted_both (w->start, buffer,
 12545                                   XFIXNAT (AREF (vector, 9)),
 12546                                   XFIXNAT (AREF (vector, 10)));
 12547     }
 12548 
 12549   Vwith_echo_area_save_vector = vector;
 12550 }
 12551 
 12552 
 12553 /* Set up the echo area for use by print functions.  MULTIBYTE_P
 12554    means we will print multibyte.  */
 12555 
 12556 void
 12557 setup_echo_area_for_printing (bool multibyte_p)
 12558 {
 12559   /* If we can't find an echo area any more, exit.  */
 12560   if (! FRAME_LIVE_P (XFRAME (selected_frame)))
 12561     Fkill_emacs (Qnil, Qnil);
 12562 
 12563   ensure_echo_area_buffers ();
 12564 
 12565   if (!message_buf_print)
 12566     {
 12567       /* A message has been output since the last time we printed.
 12568          Choose a fresh echo area buffer.  */
 12569       if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12570         echo_area_buffer[0] = echo_buffer[1];
 12571       else
 12572         echo_area_buffer[0] = echo_buffer[0];
 12573 
 12574       /* Switch to that buffer and clear it.  */
 12575       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12576       bset_truncate_lines (current_buffer, Qnil);
 12577 
 12578       if (Z > BEG)
 12579         {
 12580           specpdl_ref count = SPECPDL_INDEX ();
 12581           specbind (Qinhibit_read_only, Qt);
 12582           /* Note that undo recording is always disabled.  */
 12583           del_range (BEG, Z);
 12584           unbind_to (count, Qnil);
 12585         }
 12586       TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 12587 
 12588       /* Set up the buffer for the multibyteness we need.  We always
 12589          set it to be multibyte, except when
 12590          unibyte-display-via-language-environment is non-nil and the
 12591          buffer from which we are called is unibyte, because in that
 12592          case unibyte characters should not be displayed as octal
 12593          escapes.  */
 12594       if (unibyte_display_via_language_environment
 12595           && !multibyte_p
 12596           && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12597         Fset_buffer_multibyte (Qnil);
 12598       else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12599         Fset_buffer_multibyte (Qt);
 12600 
 12601       /* Raise the frame containing the echo area.  */
 12602       if (minibuffer_auto_raise)
 12603         {
 12604           struct frame *sf = SELECTED_FRAME ();
 12605           Lisp_Object mini_window;
 12606           mini_window = FRAME_MINIBUF_WINDOW (sf);
 12607           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
 12608         }
 12609 
 12610       message_log_maybe_newline ();
 12611       message_buf_print = true;
 12612     }
 12613   else
 12614     {
 12615       if (NILP (echo_area_buffer[0]))
 12616         {
 12617           if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12618             echo_area_buffer[0] = echo_buffer[1];
 12619           else
 12620             echo_area_buffer[0] = echo_buffer[0];
 12621         }
 12622 
 12623       if (current_buffer != XBUFFER (echo_area_buffer[0]))
 12624         {
 12625           /* Someone switched buffers between print requests.  */
 12626           set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12627           bset_truncate_lines (current_buffer, Qnil);
 12628         }
 12629     }
 12630 }
 12631 
 12632 
 12633 /* Display an echo area message in window W.  Value is true if W's
 12634    height is changed.  If display_last_displayed_message_p,
 12635    display the message that was last displayed, otherwise
 12636    display the current message.  */
 12637 
 12638 static bool
 12639 display_echo_area (struct window *w)
 12640 {
 12641   bool no_message_p, window_height_changed_p;
 12642 
 12643   /* Temporarily disable garbage collections while displaying the echo
 12644      area.  This is done because a GC can print a message itself.
 12645      That message would modify the echo area buffer's contents while a
 12646      redisplay of the buffer is going on, and seriously confuse
 12647      redisplay.  */
 12648   specpdl_ref count = inhibit_garbage_collection ();
 12649 
 12650   /* If there is no message, we must call display_echo_area_1
 12651      nevertheless because it resizes the window.  But we will have to
 12652      reset the echo_area_buffer in question to nil at the end because
 12653      with_echo_area_buffer will set it to an empty buffer.  */
 12654   bool i = display_last_displayed_message_p;
 12655   /* According to the C standard, the integral value
 12656      of a "bool" is always 0 or 1, so this array access is safe here,
 12657      if oddly typed. */
 12658   no_message_p = NILP (echo_area_buffer[i]);
 12659 
 12660   window_height_changed_p
 12661     = with_echo_area_buffer (w, display_last_displayed_message_p,
 12662                              display_echo_area_1, w, Qnil);
 12663 
 12664   if (no_message_p)
 12665     echo_area_buffer[i] = Qnil;
 12666 
 12667   unbind_to (count, Qnil);
 12668   return window_height_changed_p;
 12669 }
 12670 
 12671 
 12672 /* Helper for display_echo_area.  Display the current buffer which
 12673    contains the current echo area message in window W, a mini-window,
 12674    a pointer to which is passed in A1.  A2 is currently not used.
 12675    Change the height of W so that all of the message is displayed.
 12676    Value is true if height of W was changed.  */
 12677 
 12678 static bool
 12679 display_echo_area_1 (void *a1, Lisp_Object a2)
 12680 {
 12681   struct window *w = a1;
 12682   Lisp_Object window;
 12683   struct text_pos start;
 12684 
 12685   /* We are about to enter redisplay without going through
 12686      redisplay_internal, so we need to forget these faces by hand
 12687      here.  */
 12688   forget_escape_and_glyphless_faces ();
 12689 
 12690   /* Do this before displaying, so that we have a large enough glyph
 12691      matrix for the display.  If we can't get enough space for the
 12692      whole text, display the last N lines.  That works by setting w->start.  */
 12693   bool window_height_changed_p = resize_mini_window (w, false);
 12694 
 12695   /* Use the starting position chosen by resize_mini_window.  */
 12696   SET_TEXT_POS_FROM_MARKER (start, w->start);
 12697 
 12698   /* Display.  */
 12699   clear_glyph_matrix (w->desired_matrix);
 12700   XSETWINDOW (window, w);
 12701   void *itdata = bidi_shelve_cache ();
 12702   try_window (window, start, 0);
 12703   bidi_unshelve_cache (itdata, false);
 12704 
 12705   return window_height_changed_p;
 12706 }
 12707 
 12708 
 12709 /* Resize the echo area window to exactly the size needed for the
 12710    currently displayed message, if there is one.  If a mini-buffer
 12711    is active, don't shrink it.  */
 12712 
 12713 void
 12714 resize_echo_area_exactly (void)
 12715 {
 12716   if (BUFFERP (echo_area_buffer[0])
 12717       && WINDOWP (echo_area_window))
 12718     {
 12719       struct window *w = XWINDOW (echo_area_window);
 12720       Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
 12721       bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
 12722                                               w, resize_exactly);
 12723       if (resized_p)
 12724         {
 12725           windows_or_buffers_changed = 42;
 12726           update_mode_lines = 30;
 12727           redisplay_internal ();
 12728         }
 12729     }
 12730 }
 12731 
 12732 
 12733 /* Callback function for with_echo_area_buffer, when used from
 12734    resize_echo_area_exactly.  A1 contains a pointer to the window to
 12735    resize, EXACTLY non-nil means resize the mini-window exactly to the
 12736    size of the text displayed.  Value is what resize_mini_window
 12737    returns.  */
 12738 
 12739 static bool
 12740 resize_mini_window_1 (void *a1, Lisp_Object exactly)
 12741 {
 12742   return resize_mini_window (a1, !NILP (exactly));
 12743 }
 12744 
 12745 
 12746 /* Resize mini-window W to fit the size of its contents.  EXACT_P
 12747    means size the window exactly to the size needed.  Otherwise, it's
 12748    only enlarged until W's buffer is empty.
 12749 
 12750    Set W->start to the right place to begin display.  If the whole
 12751    contents fit, start at the beginning.  Otherwise, start so as
 12752    to make the end of the contents appear.  This is particularly
 12753    important for y-or-n-p, but seems desirable generally.
 12754 
 12755    Value is true if the window height has been changed.  */
 12756 
 12757 bool
 12758 resize_mini_window (struct window *w, bool exact_p)
 12759 {
 12760   struct frame *f = XFRAME (w->frame);
 12761   int old_height = WINDOW_BOX_TEXT_HEIGHT (w);
 12762 
 12763   eassert (MINI_WINDOW_P (w));
 12764 
 12765   /* Don't resize windows while redisplaying a window; it would
 12766      confuse redisplay functions when the size of the window they are
 12767      displaying changes from under them.  Such a resizing can happen,
 12768      for instance, when which-func prints a long message while
 12769      we are running fontification-functions.  We're running these
 12770      functions with safe_call which binds inhibit-redisplay to t.  */
 12771   if (!NILP (Vinhibit_redisplay))
 12772     return false;
 12773 
 12774   /* By default, start display at the beginning.  */
 12775   if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12776     set_marker_both (w->start, w->contents,
 12777                      BUF_BEGV (XBUFFER (w->contents)),
 12778                      BUF_BEGV_BYTE (XBUFFER (w->contents)));
 12779 
 12780   /* Nil means don't try to resize.  */
 12781   if ((NILP (Vresize_mini_windows)
 12782        && (NILP (resize_mini_frames) || !FRAME_MINIBUF_ONLY_P (f)))
 12783       || (FRAME_X_P (f) && FRAME_OUTPUT_DATA (f) == NULL))
 12784     return false;
 12785 
 12786   if (FRAME_MINIBUF_ONLY_P (f))
 12787     {
 12788       if (!NILP (resize_mini_frames))
 12789         safe_call1 (Qwindow__resize_mini_frame, WINDOW_FRAME (w));
 12790     }
 12791   else
 12792     {
 12793       struct it it;
 12794       int unit = FRAME_LINE_HEIGHT (f);
 12795       int height, max_height;
 12796       struct text_pos start;
 12797       struct buffer *old_current_buffer = NULL;
 12798       int windows_height = FRAME_INNER_HEIGHT (f);
 12799 
 12800       if (current_buffer != XBUFFER (w->contents))
 12801         {
 12802           old_current_buffer = current_buffer;
 12803           set_buffer_internal (XBUFFER (w->contents));
 12804         }
 12805 
 12806       init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
 12807 
 12808       /* Compute the max. number of lines specified by the user.  */
 12809       if (FLOATP (Vmax_mini_window_height))
 12810         max_height = XFLOAT_DATA (Vmax_mini_window_height) * windows_height;
 12811       else if (FIXNUMP (Vmax_mini_window_height))
 12812         max_height = XFIXNUM (Vmax_mini_window_height) * unit;
 12813       else
 12814         max_height = windows_height / 4;
 12815 
 12816       /* Correct that max. height if it's bogus.  */
 12817       max_height = clip_to_bounds (unit, max_height, windows_height);
 12818 
 12819       /* Find out the height of the text in the window.  */
 12820       last_height = 0;
 12821       move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
 12822       /* If move_it_to moved to the next visible line after EOB,
 12823          account for the height of the last full line.  */
 12824       if (it.max_ascent == 0 && it.max_descent == 0)
 12825         {
 12826           height = it.current_y;
 12827           /* Don't add the last line's height if lines are truncated
 12828              and the text doesn't end in a newline.
 12829              FIXME: if the text ends in a newline from a display
 12830              property or an overlay string, they lose: the mini-window
 12831              might not show the last empty line.  */
 12832           if (!(it.line_wrap == TRUNCATE
 12833                 && it.current_x <= it.first_visible_x
 12834                 && ZV_BYTE > 1
 12835                 && FETCH_BYTE (ZV_BYTE - 1) != '\n'))
 12836             height += last_height;
 12837         }
 12838       else
 12839         height = it.current_y + it.max_ascent + it.max_descent;
 12840       height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
 12841 
 12842       /* Compute a suitable window start.  */
 12843       if (height > max_height)
 12844         {
 12845           height = (max_height / unit) * unit;
 12846           if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12847             {
 12848               init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
 12849               move_it_vertically_backward (&it, height - unit);
 12850               /* The following move is usually a no-op when the stuff
 12851                  displayed in the mini-window comes entirely from buffer
 12852                  text, but it is needed when some of it comes from overlay
 12853                  strings, especially when there's an after-string at ZV.
 12854                  This happens with some completion packages, like
 12855                  icomplete, ido-vertical, etc.  With those packages, if we
 12856                  don't force w->start to be at the beginning of a screen
 12857                  line, important parts of the stuff in the mini-window,
 12858                  such as user prompt, will be hidden from view.  */
 12859               move_it_by_lines (&it, 0);
 12860               start = it.current.pos;
 12861               /* Prevent redisplay_window from recentering, and thus from
 12862                  overriding the window-start point we computed here.  */
 12863               w->start_at_line_beg = false;
 12864               SET_MARKER_FROM_TEXT_POS (w->start, start);
 12865             }
 12866         }
 12867       else
 12868         {
 12869           SET_TEXT_POS (start, BEGV, BEGV_BYTE);
 12870           SET_MARKER_FROM_TEXT_POS (w->start, start);
 12871         }
 12872 
 12873       if (EQ (Vresize_mini_windows, Qgrow_only))
 12874         {
 12875           /* Let it grow only, until we display an empty message, in which
 12876              case the window shrinks again.  */
 12877           if (height > old_height)
 12878             grow_mini_window (w, height - old_height);
 12879           else if (height < old_height && (exact_p || BEGV == ZV))
 12880             shrink_mini_window (w);
 12881         }
 12882       else if (height != old_height)
 12883         /* Always resize to exact size needed.  */
 12884         grow_mini_window (w, height - old_height);
 12885 
 12886       if (old_current_buffer)
 12887         set_buffer_internal (old_current_buffer);
 12888     }
 12889 
 12890   return WINDOW_BOX_TEXT_HEIGHT (w) != old_height;
 12891 }
 12892 
 12893 
 12894 /* Value is the current message, a string, or nil if there is no
 12895    current message.  */
 12896 
 12897 Lisp_Object
 12898 current_message (void)
 12899 {
 12900   Lisp_Object msg;
 12901 
 12902   if (!BUFFERP (echo_area_buffer[0]))
 12903     msg = Qnil;
 12904   else
 12905     {
 12906       with_echo_area_buffer (0, 0, current_message_1, &msg, Qnil);
 12907       if (NILP (msg))
 12908         echo_area_buffer[0] = Qnil;
 12909     }
 12910 
 12911   return msg;
 12912 }
 12913 
 12914 
 12915 static bool
 12916 current_message_1 (void *a1, Lisp_Object a2)
 12917 {
 12918   Lisp_Object *msg = a1;
 12919 
 12920   if (Z > BEG)
 12921     *msg = make_buffer_string (BEG, Z, true);
 12922   else
 12923     *msg = Qnil;
 12924   return false;
 12925 }
 12926 
 12927 
 12928 /* Push the current message on Vmessage_stack for later restoration
 12929    by restore_message.  Value is true if the current message isn't
 12930    empty.  This is a relatively infrequent operation, so it's not
 12931    worth optimizing.  */
 12932 
 12933 bool
 12934 push_message (void)
 12935 {
 12936   Lisp_Object msg = current_message ();
 12937   Vmessage_stack = Fcons (msg, Vmessage_stack);
 12938   return STRINGP (msg);
 12939 }
 12940 
 12941 
 12942 /* Restore message display from the top of Vmessage_stack.  */
 12943 
 12944 void
 12945 restore_message (void)
 12946 {
 12947   eassert (CONSP (Vmessage_stack));
 12948   message3_nolog (XCAR (Vmessage_stack));
 12949 }
 12950 
 12951 
 12952 /* Handler for unwind-protect calling pop_message.  */
 12953 
 12954 void
 12955 pop_message_unwind (void)
 12956 {
 12957   /* Pop the top-most entry off Vmessage_stack.  */
 12958   eassert (CONSP (Vmessage_stack));
 12959   Vmessage_stack = XCDR (Vmessage_stack);
 12960 }
 12961 
 12962 
 12963 /* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
 12964    exits.  If the stack is not empty, we have a missing
 12965    pop_message_unwind somewhere.  */
 12966 
 12967 void
 12968 check_message_stack (void)
 12969 {
 12970   if (!NILP (Vmessage_stack))
 12971     emacs_abort ();
 12972 }
 12973 
 12974 void
 12975 clear_message_stack (void)
 12976 {
 12977   Vmessage_stack = Qnil;
 12978 }
 12979 
 12980 /* Truncate to NCHARS what will be displayed in the echo area the next
 12981    time we display it---but don't redisplay it now.  */
 12982 
 12983 void
 12984 truncate_echo_area (ptrdiff_t nchars)
 12985 {
 12986   if (nchars == 0)
 12987     echo_area_buffer[0] = Qnil;
 12988   else if (!noninteractive
 12989            && INTERACTIVE
 12990            && !NILP (echo_area_buffer[0]))
 12991     {
 12992       struct frame *sf = SELECTED_FRAME ();
 12993       /* Error messages get reported properly by cmd_error, so this must be
 12994          just an informative message; if the frame hasn't really been
 12995          initialized yet, just toss it.  */
 12996       if (sf->glyphs_initialized_p)
 12997         with_echo_area_buffer (0, 0, truncate_message_1,
 12998                                (void *) (intptr_t) nchars, Qnil);
 12999     }
 13000 }
 13001 
 13002 
 13003 /* Helper function for truncate_echo_area.  Truncate the current
 13004    message to at most NCHARS characters.  */
 13005 
 13006 static bool
 13007 truncate_message_1 (void *a1, Lisp_Object a2)
 13008 {
 13009   intptr_t nchars = (intptr_t) a1;
 13010   if (BEG + nchars < Z)
 13011     del_range (BEG + nchars, Z);
 13012   if (Z == BEG)
 13013     echo_area_buffer[0] = Qnil;
 13014   return false;
 13015 }
 13016 
 13017 extern intptr_t garbage_collection_inhibited;
 13018 
 13019 /* Set the current message to STRING.  */
 13020 
 13021 static void
 13022 set_message (Lisp_Object string)
 13023 {
 13024   Lisp_Object message = Qnil;
 13025 
 13026   eassert (STRINGP (string));
 13027 
 13028   if (FUNCTIONP (Vset_message_function)
 13029       /* FIXME: (bug#63253) We should really make the regexp engine re-entrant,
 13030          but in the mean time, let's ignore `set-message-function` when
 13031          called from `probably_quit`.  */
 13032       && !garbage_collection_inhibited)
 13033     {
 13034       specpdl_ref count = SPECPDL_INDEX ();
 13035       specbind (Qinhibit_quit, Qt);
 13036       message = safe_call1 (Vset_message_function, string);
 13037       unbind_to (count, Qnil);
 13038 
 13039       if (STRINGP (message))
 13040         {
 13041           string = message;
 13042           message = Qnil;
 13043         }
 13044     }
 13045 
 13046   if (NILP (message))
 13047     {
 13048       message_enable_multibyte = STRING_MULTIBYTE (string);
 13049 
 13050       with_echo_area_buffer (0, -1, set_message_1, 0, string);
 13051       message_buf_print = false;
 13052       help_echo_showing_p = false;
 13053     }
 13054 
 13055   if (STRINGP (Vdebug_on_message)
 13056       && STRINGP (string)
 13057       && fast_string_match (Vdebug_on_message, string) >= 0)
 13058     call_debugger (list2 (Qerror, string));
 13059 }
 13060 
 13061 
 13062 /* Helper function for set_message.  First argument is ignored and second
 13063    argument has the same meaning as for set_message.
 13064    This function is called with the echo area buffer being current.  */
 13065 
 13066 static bool
 13067 set_message_1 (void *a1, Lisp_Object string)
 13068 {
 13069   eassert (STRINGP (string));
 13070 
 13071   /* Change multibyteness of the echo buffer appropriately.  We always
 13072      set it to be multibyte, except when
 13073      unibyte-display-via-language-environment is non-nil and the
 13074      string to display is unibyte, because in that case unibyte
 13075      characters should not be displayed as octal escapes.  */
 13076   if (!message_enable_multibyte
 13077       && unibyte_display_via_language_environment
 13078       && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13079     Fset_buffer_multibyte (Qnil);
 13080   else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13081     Fset_buffer_multibyte (Qt);
 13082 
 13083   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
 13084   if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
 13085     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 13086 
 13087   /* Insert new message at BEG.  */
 13088   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 13089 
 13090   /* This function takes care of single/multibyte conversion.
 13091      We just have to ensure that the echo area buffer has the right
 13092      setting of enable_multibyte_characters.  */
 13093   insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
 13094 
 13095   return false;
 13096 }
 13097 
 13098 
 13099 /* Clear messages.  CURRENT_P means clear the current message.
 13100    LAST_DISPLAYED_P means clear the message last displayed.  */
 13101 
 13102 void
 13103 clear_message (bool current_p, bool last_displayed_p)
 13104 {
 13105   Lisp_Object preserve = Qnil;
 13106 
 13107   if (current_p)
 13108     {
 13109       if (FUNCTIONP (Vclear_message_function)
 13110           /* FIXME: (bug#63253) Same as for `set-message-function` above.  */
 13111           && !garbage_collection_inhibited)
 13112         {
 13113           specpdl_ref count = SPECPDL_INDEX ();
 13114           specbind (Qinhibit_quit, Qt);
 13115           preserve = safe_call (1, Vclear_message_function);
 13116           unbind_to (count, Qnil);
 13117         }
 13118 
 13119       if (!EQ (preserve, Qdont_clear_message))
 13120         {
 13121           echo_area_buffer[0] = Qnil;
 13122           message_cleared_p = true;
 13123         }
 13124     }
 13125 
 13126   if (last_displayed_p)
 13127     echo_area_buffer[1] = Qnil;
 13128 
 13129   message_buf_print = false;
 13130 }
 13131 
 13132 /* Clear garbaged frames.
 13133 
 13134    This function is used where the old redisplay called
 13135    redraw_garbaged_frames which in turn called redraw_frame which in
 13136    turn called clear_frame.  The call to clear_frame was a source of
 13137    flickering.  I believe a clear_frame is not necessary.  It should
 13138    suffice in the new redisplay to invalidate all current matrices,
 13139    and ensure a complete redisplay of all windows.  */
 13140 
 13141 static void
 13142 clear_garbaged_frames (void)
 13143 {
 13144   if (frame_garbaged)
 13145     {
 13146       Lisp_Object tail, frame;
 13147       struct frame *sf = SELECTED_FRAME ();
 13148 
 13149       FOR_EACH_FRAME (tail, frame)
 13150         {
 13151           struct frame *f = XFRAME (frame);
 13152 
 13153           if (FRAME_REDISPLAY_P (f) && FRAME_GARBAGED_P (f))
 13154             {
 13155               if (f->resized_p
 13156                   /* It makes no sense to redraw a non-selected TTY
 13157                      frame, since that will actually clear the
 13158                      selected frame, and might leave the selected
 13159                      frame with corrupted display, if it happens not
 13160                      to be marked garbaged.  */
 13161                   && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
 13162                 redraw_frame (f);
 13163               else
 13164                 clear_current_matrices (f);
 13165 
 13166 #ifdef HAVE_WINDOW_SYSTEM
 13167               if (FRAME_WINDOW_P (f)
 13168                   && FRAME_RIF (f)->clear_under_internal_border)
 13169                 FRAME_RIF (f)->clear_under_internal_border (f);
 13170 #endif
 13171               fset_redisplay (f);
 13172               f->garbaged = false;
 13173               f->resized_p = false;
 13174             }
 13175         }
 13176 
 13177       frame_garbaged = false;
 13178     }
 13179 }
 13180 
 13181 
 13182 /* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P, update
 13183    selected_frame.  */
 13184 
 13185 static void
 13186 echo_area_display (bool update_frame_p)
 13187 {
 13188   Lisp_Object mini_window;
 13189   struct window *w;
 13190   struct frame *f;
 13191   bool window_height_changed_p = false;
 13192   struct frame *sf = SELECTED_FRAME ();
 13193 
 13194   mini_window = FRAME_MINIBUF_WINDOW (sf);
 13195   if (NILP (mini_window))
 13196     return;
 13197 
 13198   w = XWINDOW (mini_window);
 13199   f = XFRAME (WINDOW_FRAME (w));
 13200 
 13201   /* Don't display if frame is invisible or not yet initialized.  */
 13202   if (!FRAME_REDISPLAY_P (f) || !f->glyphs_initialized_p)
 13203     return;
 13204 
 13205 #ifdef HAVE_WINDOW_SYSTEM
 13206   /* When Emacs starts, selected_frame may be the initial terminal
 13207      frame.  If we let this through, a message would be displayed on
 13208      the terminal.  */
 13209   if (FRAME_INITIAL_P (XFRAME (selected_frame)))
 13210     return;
 13211 #endif /* HAVE_WINDOW_SYSTEM */
 13212 
 13213   /* Redraw garbaged frames.  */
 13214   clear_garbaged_frames ();
 13215 
 13216   if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
 13217     {
 13218       echo_area_window = mini_window;
 13219       window_height_changed_p = display_echo_area (w);
 13220       w->must_be_updated_p = true;
 13221 
 13222       /* Update the display, unless called from redisplay_internal.
 13223          Also don't update the screen during redisplay itself.  The
 13224          update will happen at the end of redisplay, and an update
 13225          here could cause confusion.  */
 13226       if (update_frame_p && !redisplaying_p)
 13227         {
 13228           int n = 0;
 13229 
 13230           /* If the display update has been interrupted by pending
 13231              input, update mode lines in the frame.  Due to the
 13232              pending input, it might have been that redisplay hasn't
 13233              been called, so that mode lines above the echo area are
 13234              garbaged.  This looks odd, so we prevent it here.  */
 13235           if (!display_completed)
 13236             {
 13237               n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
 13238 
 13239 #ifdef HAVE_WINDOW_SYSTEM
 13240               if (FRAME_WINDOW_P (f)
 13241                   && FRAME_RIF (f)->clear_under_internal_border)
 13242                 FRAME_RIF (f)->clear_under_internal_border (f);
 13243 #endif
 13244             }
 13245 
 13246           if (window_height_changed_p
 13247               /* Don't do this if Emacs is shutting down.  Redisplay
 13248                  needs to run hooks.  */
 13249               && !NILP (Vrun_hooks))
 13250             {
 13251               /* Must update other windows.  Likewise as in other
 13252                  cases, don't let this update be interrupted by
 13253                  pending input.  */
 13254               specpdl_ref count = SPECPDL_INDEX ();
 13255               specbind (Qredisplay_dont_pause, Qt);
 13256               fset_redisplay (f);
 13257               redisplay_internal ();
 13258               unbind_to (count, Qnil);
 13259             }
 13260           else if (FRAME_WINDOW_P (f) && n == 0)
 13261             {
 13262               /* Window configuration is the same as before.
 13263                  Can do with a display update of the echo area,
 13264                  unless we displayed some mode lines.  */
 13265               update_single_window (w);
 13266               flush_frame (f);
 13267             }
 13268           else
 13269             update_frame (f, true, true);
 13270 
 13271           /* If cursor is in the echo area, make sure that the next
 13272              redisplay displays the minibuffer, so that the cursor will
 13273              be replaced with what the minibuffer wants.  */
 13274           if (cursor_in_echo_area)
 13275             wset_redisplay (XWINDOW (mini_window));
 13276         }
 13277     }
 13278   else if (!EQ (mini_window, selected_window))
 13279     wset_redisplay (XWINDOW (mini_window));
 13280 
 13281   /* Last displayed message is now the current message.  */
 13282   echo_area_buffer[1] = echo_area_buffer[0];
 13283   /* Inform read_char that we're not echoing.  */
 13284   echo_message_buffer = Qnil;
 13285 
 13286   /* Prevent redisplay optimization in redisplay_internal by resetting
 13287      this_line_start_pos.  This is done because the mini-buffer now
 13288      displays the message instead of its buffer text.  */
 13289   if (EQ (mini_window, selected_window))
 13290     CHARPOS (this_line_start_pos) = 0;
 13291 
 13292   if (window_height_changed_p)
 13293     {
 13294       fset_redisplay (f);
 13295 
 13296       /* If window configuration was changed, frames may have been
 13297          marked garbaged.  Clear them or we will experience
 13298          surprises wrt scrolling.
 13299          FIXME: How/why/when?  */
 13300       clear_garbaged_frames ();
 13301     }
 13302 }
 13303 
 13304 /* True if W's buffer was changed but not saved.  */
 13305 
 13306 static bool
 13307 window_buffer_changed (struct window *w)
 13308 {
 13309   struct buffer *b = XBUFFER (w->contents);
 13310 
 13311   eassert (BUFFER_LIVE_P (b));
 13312 
 13313   return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
 13314 }
 13315 
 13316 /* True if W has %c or %C in its mode line and mode line should be updated.  */
 13317 
 13318 static bool
 13319 mode_line_update_needed (struct window *w)
 13320 {
 13321   return (w->column_number_displayed != -1
 13322           && !(PT == w->last_point && !window_outdated (w))
 13323           && (w->column_number_displayed != current_column ()));
 13324 }
 13325 
 13326 /* True if window start of W is frozen and may not be changed during
 13327    redisplay.  */
 13328 
 13329 static bool
 13330 window_frozen_p (struct window *w)
 13331 {
 13332   if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
 13333     {
 13334       Lisp_Object window;
 13335 
 13336       XSETWINDOW (window, w);
 13337       if (MINI_WINDOW_P (w))
 13338         return false;
 13339       else if (EQ (window, selected_window))
 13340         return false;
 13341       else if (MINI_WINDOW_P (XWINDOW (selected_window))
 13342                && EQ (window, Vminibuf_scroll_window))
 13343         /* This special window can't be frozen too.  */
 13344         return false;
 13345       else
 13346         return true;
 13347     }
 13348   return false;
 13349 }
 13350 
 13351 /***********************************************************************
 13352                      Mode Lines and Frame Titles
 13353  ***********************************************************************/
 13354 
 13355 /* A buffer for constructing non-propertized mode-line strings and
 13356    frame titles in it; allocated from the heap in init_xdisp and
 13357    resized as needed in store_mode_line_noprop_char.  */
 13358 
 13359 static char *mode_line_noprop_buf;
 13360 
 13361 /* The buffer's end, and a current output position in it.  */
 13362 
 13363 static char *mode_line_noprop_buf_end;
 13364 static char *mode_line_noprop_ptr;
 13365 
 13366 #define MODE_LINE_NOPROP_LEN(start) \
 13367   ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
 13368 
 13369 static enum {
 13370   MODE_LINE_DISPLAY = 0,
 13371   MODE_LINE_TITLE,
 13372   MODE_LINE_NOPROP,
 13373   MODE_LINE_STRING
 13374 } mode_line_target;
 13375 
 13376 /* Alist that caches the results of :propertize.
 13377    Each element is (PROPERTIZED-STRING . PROPERTY-LIST).  */
 13378 static Lisp_Object mode_line_proptrans_alist;
 13379 
 13380 /* List of strings making up the mode-line.  */
 13381 static Lisp_Object mode_line_string_list;
 13382 
 13383 /* Base face property when building propertized mode line string.  */
 13384 static Lisp_Object mode_line_string_face;
 13385 static Lisp_Object mode_line_string_face_prop;
 13386 
 13387 
 13388 /* Unwind data for mode line strings */
 13389 
 13390 static Lisp_Object Vmode_line_unwind_vector;
 13391 
 13392 static Lisp_Object
 13393 format_mode_line_unwind_data (struct frame *target_frame,
 13394                               struct buffer *obuf,
 13395                               Lisp_Object owin,
 13396                               bool save_proptrans)
 13397 {
 13398   Lisp_Object vector, tmp;
 13399 
 13400   /* Reduce consing by keeping one vector in
 13401      Vwith_echo_area_save_vector.  */
 13402   vector = Vmode_line_unwind_vector;
 13403   Vmode_line_unwind_vector = Qnil;
 13404 
 13405   if (NILP (vector))
 13406     vector = make_nil_vector (12);
 13407 
 13408   ASET (vector, 0, make_fixnum (mode_line_target));
 13409   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
 13410   ASET (vector, 2, mode_line_string_list);
 13411   ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
 13412   ASET (vector, 4, mode_line_string_face);
 13413   ASET (vector, 5, mode_line_string_face_prop);
 13414 
 13415   if (obuf)
 13416     XSETBUFFER (tmp, obuf);
 13417   else
 13418     tmp = Qnil;
 13419   ASET (vector, 6, tmp);
 13420   ASET (vector, 7, owin);
 13421   if (target_frame)
 13422     {
 13423       Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
 13424       struct buffer *b = XBUFFER (buffer);
 13425       struct buffer *cb = current_buffer;
 13426 
 13427       /* Similarly to `with-selected-window', if the operation selects
 13428          a window on another frame, we must restore that frame's
 13429          selected window, and (for a tty) the top-frame.  */
 13430       ASET (vector, 8, target_frame->selected_window);
 13431       if (FRAME_TERMCAP_P (target_frame))
 13432         ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
 13433 
 13434       /* If we select a window on another frame, make sure that that
 13435          selection does not leave its buffer's point modified when
 13436          unwinding (Bug#32777).  */
 13437       ASET (vector, 10, buffer);
 13438       current_buffer = b;
 13439       ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
 13440       current_buffer = cb;
 13441     }
 13442 
 13443   return vector;
 13444 }
 13445 
 13446 static void
 13447 unwind_format_mode_line (Lisp_Object vector)
 13448 {
 13449   Lisp_Object old_window = AREF (vector, 7);
 13450   Lisp_Object target_frame_window = AREF (vector, 8);
 13451   Lisp_Object old_top_frame = AREF (vector, 9);
 13452 
 13453   mode_line_target = XFIXNUM (AREF (vector, 0));
 13454   mode_line_noprop_ptr = mode_line_noprop_buf + XFIXNUM (AREF (vector, 1));
 13455   mode_line_string_list = AREF (vector, 2);
 13456   if (! EQ (AREF (vector, 3), Qt))
 13457     mode_line_proptrans_alist = AREF (vector, 3);
 13458   mode_line_string_face = AREF (vector, 4);
 13459   mode_line_string_face_prop = AREF (vector, 5);
 13460 
 13461   /* Select window before buffer, since it may change the buffer.  */
 13462   if (WINDOW_LIVE_P (old_window))
 13463     {
 13464       /* If the operation that we are unwinding had selected a window
 13465          on a different frame, reset its frame-selected-window.  For a
 13466          text terminal, reset its top-frame if necessary.  */
 13467       if (WINDOW_LIVE_P (target_frame_window))
 13468         {
 13469           Lisp_Object frame
 13470             = WINDOW_FRAME (XWINDOW (target_frame_window));
 13471 
 13472           if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 13473             Fselect_window (target_frame_window, Qt);
 13474 
 13475           if (!NILP (old_top_frame) && !EQ (old_top_frame, frame)
 13476               /* This could've been destroyed during the formatting,
 13477                  possibly because the terminal was deleted.  */
 13478               && FRAME_LIVE_P (XFRAME (old_top_frame)))
 13479             Fselect_frame (old_top_frame, Qt);
 13480         }
 13481 
 13482       Fselect_window (old_window, Qt);
 13483 
 13484       /* Restore point of target_frame_window's buffer (Bug#32777).
 13485          But do this only after old_window has been reselected to
 13486          avoid that the window point of target_frame_window moves.  */
 13487       if (WINDOW_LIVE_P (target_frame_window))
 13488         {
 13489           Lisp_Object buffer = AREF (vector, 10);
 13490 
 13491           if (BUFFER_LIVE_P (XBUFFER (buffer)))
 13492             {
 13493               struct buffer *cb = current_buffer;
 13494 
 13495               current_buffer = XBUFFER (buffer);
 13496               set_point_from_marker (AREF (vector, 11));
 13497               ASET (vector, 11, Qnil);
 13498               current_buffer = cb;
 13499             }
 13500         }
 13501     }
 13502 
 13503   if (!NILP (AREF (vector, 6)))
 13504     {
 13505       set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
 13506       ASET (vector, 6, Qnil);
 13507     }
 13508 
 13509   Vmode_line_unwind_vector = vector;
 13510 }
 13511 
 13512 
 13513 /* Store a single character C for the frame title in mode_line_noprop_buf.
 13514    Re-allocate mode_line_noprop_buf if necessary.  */
 13515 
 13516 static void
 13517 store_mode_line_noprop_char (char c)
 13518 {
 13519   /* If output position has reached the end of the allocated buffer,
 13520      increase the buffer's size.  */
 13521   if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
 13522     {
 13523       ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
 13524       ptrdiff_t size = len;
 13525       mode_line_noprop_buf =
 13526         xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
 13527       mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 13528       mode_line_noprop_ptr = mode_line_noprop_buf + len;
 13529     }
 13530 
 13531   *mode_line_noprop_ptr++ = c;
 13532 }
 13533 
 13534 
 13535 /* Store part of a frame title in mode_line_noprop_buf, beginning at
 13536    mode_line_noprop_ptr.  STRING is the string to store.  Do not copy
 13537    characters that yield more columns than PRECISION; PRECISION <= 0
 13538    means copy the whole string.  Pad with spaces until FIELD_WIDTH
 13539    number of characters have been copied; FIELD_WIDTH <= 0 means don't
 13540    pad.  Called from display_mode_element when it is used to build a
 13541    frame title.  */
 13542 
 13543 static int
 13544 store_mode_line_noprop (const char *string, int field_width, int precision)
 13545 {
 13546   const unsigned char *str = (const unsigned char *) string;
 13547   int n = 0;
 13548   ptrdiff_t dummy, nbytes;
 13549 
 13550   /* Copy at most PRECISION chars from STR.  */
 13551   nbytes = strlen (string);
 13552   n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
 13553   while (nbytes--)
 13554     store_mode_line_noprop_char (*str++);
 13555 
 13556   /* Fill up with spaces until FIELD_WIDTH reached.  */
 13557   while (field_width > 0
 13558          && n < field_width)
 13559     {
 13560       store_mode_line_noprop_char (' ');
 13561       ++n;
 13562     }
 13563 
 13564   return n;
 13565 }
 13566 
 13567 /***********************************************************************
 13568                              Frame Titles
 13569  ***********************************************************************/
 13570 
 13571 #ifdef HAVE_WINDOW_SYSTEM
 13572 
 13573 /* Set the title of FRAME, if it has changed.  The title format is
 13574    Vicon_title_format if FRAME is iconified, otherwise it is
 13575    frame_title_format.  */
 13576 
 13577 void
 13578 gui_consider_frame_title (Lisp_Object frame)
 13579 {
 13580   struct frame *f = XFRAME (frame);
 13581   Lisp_Object format_data;
 13582 
 13583   if ((FRAME_WINDOW_P (f)
 13584        || FRAME_MINIBUF_ONLY_P (f)
 13585        || f->explicit_name)
 13586       && !FRAME_TOOLTIP_P (f))
 13587     {
 13588       /* Do we have more than one visible frame on this X display?  */
 13589       Lisp_Object tail, other_frame, fmt;
 13590       ptrdiff_t title_start;
 13591       char *title;
 13592       ptrdiff_t len;
 13593       struct it it;
 13594       specpdl_ref count = SPECPDL_INDEX ();
 13595 
 13596       FOR_EACH_FRAME (tail, other_frame)
 13597         {
 13598           struct frame *tf = XFRAME (other_frame);
 13599 
 13600           if (tf != f
 13601               && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
 13602               && !FRAME_MINIBUF_ONLY_P (tf)
 13603               && !FRAME_PARENT_FRAME (tf)
 13604               && !FRAME_TOOLTIP_P (tf)
 13605               && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
 13606             break;
 13607         }
 13608 
 13609       /* Set global variable indicating that multiple frames exist.  */
 13610       multiple_frames = CONSP (tail);
 13611 
 13612       /* select-frame calls resize_mini_window, which could resize the
 13613          mini-window and by that undo the effect of this redisplay
 13614          cycle wrt minibuffer and echo-area display.  Binding
 13615          inhibit-redisplay to t makes the call to resize_mini_window a
 13616          no-op, thus avoiding the adverse side effects.  */
 13617 
 13618       /* The following was moved before the record_unwind_protect form
 13619          below to inhibit redisplay also when restoring the selected
 13620          window/frame: This avoids that resize_mini_window sizes back
 13621          the minibuffer window of a temporarily selected frame.  See
 13622          Bug#34317.  */
 13623       specbind (Qinhibit_redisplay, Qt);
 13624 
 13625       /* Switch to the buffer of selected window of the frame.  Set up
 13626          mode_line_target so that display_mode_element will output
 13627          into mode_line_noprop_buf; then display the title.  Save the
 13628          original frame and selected window, and possibly the topmost
 13629          frame of the tty (for tty frames) into a vector; it will be
 13630          restored later.  */
 13631 
 13632       format_data = format_mode_line_unwind_data (f, current_buffer,
 13633                                                   selected_window,
 13634                                                   false);
 13635       record_unwind_protect (unwind_format_mode_line, format_data);
 13636 
 13637       Fselect_window (f->selected_window, Qt);
 13638       set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->contents));
 13639       fmt = (FRAME_ICONIFIED_P (f) && !EQ (Vicon_title_format, Qt)
 13640              ? Vicon_title_format : Vframe_title_format);
 13641 
 13642       mode_line_target = MODE_LINE_TITLE;
 13643       title_start = MODE_LINE_NOPROP_LEN (0);
 13644       init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
 13645                      NULL, DEFAULT_FACE_ID);
 13646       display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
 13647       len = MODE_LINE_NOPROP_LEN (title_start);
 13648       title = mode_line_noprop_buf + title_start;
 13649       /* Make sure that any raw bytes in the title are properly
 13650          represented by their multibyte sequences.  */
 13651       ptrdiff_t nchars = 0;
 13652       len = str_as_multibyte ((unsigned char *) title,
 13653                               mode_line_noprop_buf_end - title,
 13654                               len, &nchars);
 13655       unbind_to (count, Qnil);
 13656 
 13657       /* Set the title only if it's changed.  This avoids consing in
 13658          the common case where it hasn't.  (If it turns out that we've
 13659          already wasted too much time by walking through the list with
 13660          display_mode_element, then we might need to optimize at a
 13661          higher level than this.)  */
 13662       if ((! STRINGP (f->name)
 13663            || SBYTES (f->name) != len
 13664            || memcmp (title, SDATA (f->name), len) != 0)
 13665           && FRAME_TERMINAL (f)->implicit_set_name_hook)
 13666         {
 13667           Lisp_Object title_string = make_multibyte_string (title, nchars, len);
 13668           FRAME_TERMINAL (f)->implicit_set_name_hook (f, title_string, Qnil);
 13669         }
 13670     }
 13671 }
 13672 
 13673 #endif /* not HAVE_WINDOW_SYSTEM */
 13674 
 13675 
 13676 /***********************************************************************
 13677                               Menu Bars
 13678  ***********************************************************************/
 13679 
 13680 /* True if we will not redisplay all visible windows.  */
 13681 #define REDISPLAY_SOME_P()                              \
 13682   ((windows_or_buffers_changed == 0                     \
 13683     || windows_or_buffers_changed == REDISPLAY_SOME)    \
 13684    && (update_mode_lines == 0                           \
 13685        || update_mode_lines == REDISPLAY_SOME))
 13686 
 13687 static bool
 13688 needs_no_redisplay (struct window *w)
 13689 {
 13690   struct buffer *buffer = XBUFFER (w->contents);
 13691   struct frame *f = XFRAME (w->frame);
 13692   return (REDISPLAY_SOME_P ()
 13693           && !w->redisplay
 13694           && !w->update_mode_line
 13695           && !f->face_change
 13696           && !f->redisplay
 13697           && !buffer->text->redisplay
 13698           && window_point (w) == w->last_point);
 13699 }
 13700 
 13701 /* Prepare for redisplay by updating menu-bar item lists when
 13702    appropriate.  This can call eval.  */
 13703 
 13704 static void
 13705 prepare_menu_bars (void)
 13706 {
 13707   bool all_windows = windows_or_buffers_changed || update_mode_lines;
 13708   bool some_windows = REDISPLAY_SOME_P ();
 13709 
 13710   if (FUNCTIONP (Vpre_redisplay_function))
 13711     {
 13712       Lisp_Object windows = all_windows ? Qt : Qnil;
 13713       if (all_windows && some_windows)
 13714         {
 13715           Lisp_Object ws = window_list ();
 13716           for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
 13717             {
 13718               Lisp_Object this = XCAR (ws);
 13719               struct window *w = XWINDOW (this);
 13720               /* Cf. conditions for redisplaying a window at the
 13721                  beginning of redisplay_window.  */
 13722               if (!needs_no_redisplay (w))
 13723                 windows = Fcons (this, windows);
 13724             }
 13725         }
 13726       safe__call1 (true, Vpre_redisplay_function, windows);
 13727     }
 13728 
 13729   /* Update all frame titles based on their buffer names, etc.  We do
 13730      this before the menu bars so that the buffer-menu will show the
 13731      up-to-date frame titles.  */
 13732 #ifdef HAVE_WINDOW_SYSTEM
 13733   if (all_windows)
 13734     {
 13735       Lisp_Object tail, frame;
 13736 
 13737       FOR_EACH_FRAME (tail, frame)
 13738         {
 13739           struct frame *f = XFRAME (frame);
 13740           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13741           if (some_windows
 13742               && !f->redisplay
 13743               && !w->redisplay
 13744               && !XBUFFER (w->contents)->text->redisplay)
 13745             continue;
 13746 
 13747           if (!FRAME_TOOLTIP_P (f)
 13748               && (FRAME_ICONIFIED_P (f)
 13749                   || FRAME_VISIBLE_P (f) == 1
 13750                   /* Exclude TTY frames that are obscured because they
 13751                      are not the top frame on their console.  This is
 13752                      because gui_consider_frame_title actually switches
 13753                      to the frame, which for TTY frames means it is
 13754                      marked as garbaged, and will be completely
 13755                      redrawn on the next redisplay cycle.  This causes
 13756                      TTY frames to be completely redrawn, when there
 13757                      are more than one of them, even though nothing
 13758                      should be changed on display.  */
 13759                   || (FRAME_REDISPLAY_P (f) && FRAME_WINDOW_P (f))))
 13760             gui_consider_frame_title (frame);
 13761         }
 13762     }
 13763 #endif /* HAVE_WINDOW_SYSTEM */
 13764 
 13765   /* Update the menu bar item lists, if appropriate.  This has to be
 13766      done before any actual redisplay or generation of display lines.  */
 13767 
 13768   if (all_windows)
 13769     {
 13770       Lisp_Object tail, frame;
 13771       specpdl_ref count = SPECPDL_INDEX ();
 13772       /* True means that update_menu_bar has run its hooks
 13773          so any further calls to update_menu_bar shouldn't do so again.  */
 13774       bool menu_bar_hooks_run = false;
 13775 
 13776       record_unwind_save_match_data ();
 13777 
 13778       FOR_EACH_FRAME (tail, frame)
 13779         {
 13780           struct frame *f = XFRAME (frame);
 13781           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13782 
 13783           /* Ignore tooltip frame.  */
 13784           if (FRAME_TOOLTIP_P (f))
 13785             continue;
 13786 
 13787           if (some_windows
 13788               && !f->redisplay
 13789               && !w->redisplay
 13790               && !XBUFFER (w->contents)->text->redisplay)
 13791             continue;
 13792 
 13793           if (!FRAME_PARENT_FRAME (f))
 13794             menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
 13795 
 13796           update_tab_bar (f, false);
 13797 #ifdef HAVE_WINDOW_SYSTEM
 13798           update_tool_bar (f, false);
 13799 #endif
 13800         }
 13801 
 13802       unbind_to (count, Qnil);
 13803     }
 13804   else
 13805     {
 13806       struct frame *sf = SELECTED_FRAME ();
 13807 
 13808       if (!FRAME_PARENT_FRAME (sf))
 13809         update_menu_bar (sf, true, false);
 13810 
 13811       update_tab_bar (sf, true);
 13812 #ifdef HAVE_WINDOW_SYSTEM
 13813       update_tool_bar (sf, true);
 13814 #endif
 13815     }
 13816 }
 13817 
 13818 
 13819 /* Update the menu bar item list for frame F.  This has to be done
 13820    before we start to fill in any display lines, because it can call
 13821    eval.
 13822 
 13823    If SAVE_MATCH_DATA, we must save and restore it here.
 13824 
 13825    If HOOKS_RUN, a previous call to update_menu_bar
 13826    already ran the menu bar hooks for this redisplay, so there
 13827    is no need to run them again.  The return value is the
 13828    updated value of this flag, to pass to the next call.  */
 13829 
 13830 static bool
 13831 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
 13832 {
 13833   Lisp_Object window;
 13834   struct window *w;
 13835 
 13836   /* If called recursively during a menu update, do nothing.  This can
 13837      happen when, for instance, an activate-menubar-hook causes a
 13838      redisplay.  */
 13839   if (inhibit_menubar_update)
 13840     return hooks_run;
 13841 
 13842   window = FRAME_SELECTED_WINDOW (f);
 13843   w = XWINDOW (window);
 13844 
 13845   if (FRAME_WINDOW_P (f)
 13846       ?
 13847 #ifdef HAVE_EXT_MENU_BAR
 13848       FRAME_EXTERNAL_MENU_BAR (f)
 13849 #else
 13850       FRAME_MENU_BAR_LINES (f) > 0
 13851 #endif
 13852       : FRAME_MENU_BAR_LINES (f) > 0)
 13853     {
 13854       /* If the user has switched buffers or windows, we need to
 13855          recompute to reflect the new bindings.  But we'll
 13856          recompute when update_mode_lines is set too; that means
 13857          that people can use force-mode-line-update to request
 13858          that the menu bar be recomputed.  The adverse effect on
 13859          the rest of the redisplay algorithm is about the same as
 13860          windows_or_buffers_changed anyway.  */
 13861       if (windows_or_buffers_changed
 13862           /* This used to test w->update_mode_line, but we believe
 13863              there is no need to recompute the menu in that case.  */
 13864           || update_mode_lines
 13865           || window_buffer_changed (w))
 13866         {
 13867           struct buffer *prev = current_buffer;
 13868           specpdl_ref count = SPECPDL_INDEX ();
 13869 
 13870           specbind (Qinhibit_menubar_update, Qt);
 13871 
 13872           set_buffer_internal_1 (XBUFFER (w->contents));
 13873           if (save_match_data)
 13874             record_unwind_save_match_data ();
 13875           if (NILP (Voverriding_local_map_menu_flag))
 13876             {
 13877               specbind (Qoverriding_terminal_local_map, Qnil);
 13878               specbind (Qoverriding_local_map, Qnil);
 13879             }
 13880 
 13881           if (!hooks_run)
 13882             {
 13883               /* Run the Lucid hook.  */
 13884               safe_run_hooks (Qactivate_menubar_hook);
 13885 
 13886               /* If it has changed current-menubar from previous value,
 13887                  really recompute the menu-bar from the value.  */
 13888               safe_run_hooks (Qmenu_bar_update_hook);
 13889 
 13890               hooks_run = true;
 13891             }
 13892 
 13893           XSETFRAME (Vmenu_updating_frame, f);
 13894           fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
 13895 
 13896           /* Redisplay the menu bar in case we changed it.  */
 13897 #ifdef HAVE_EXT_MENU_BAR
 13898           if (FRAME_WINDOW_P (f))
 13899             {
 13900 #if defined (HAVE_NS)
 13901               /* All frames on Mac OS share the same menubar.  So only
 13902                  the selected frame should be allowed to set it.  */
 13903               if (f == SELECTED_FRAME ())
 13904 #endif
 13905                 set_frame_menubar (f, false);
 13906             }
 13907           else
 13908             /* On a terminal screen, the menu bar is an ordinary screen
 13909                line, and this makes it get updated.  */
 13910             w->update_mode_line = true;
 13911 #else /* ! (HAVE_EXT_MENU_BAR) */
 13912           /* In the non-toolkit version, the menu bar is an ordinary screen
 13913              line, and this makes it get updated.  */
 13914           w->update_mode_line = true;
 13915 #endif /* HAVE_EXT_MENU_BAR */
 13916 
 13917           unbind_to (count, Qnil);
 13918           set_buffer_internal_1 (prev);
 13919         }
 13920     }
 13921 
 13922   return hooks_run;
 13923 }
 13924 
 13925 
 13926 
 13927 /***********************************************************************
 13928                                Tab-bars
 13929  ***********************************************************************/
 13930 
 13931 /* Restore WINDOW as the selected window and its frame as the selected
 13932    frame.  If WINDOW is dead but the selected frame is live, make the
 13933    latter's selected window the selected window.  If both, WINDOW and
 13934    the selected frame, are dead, assign selected frame and window from
 13935    some arbitrary live frame.  Abort if no such frame can be found.  */
 13936 static void
 13937 restore_selected_window (Lisp_Object window)
 13938 {
 13939   if (WINDOW_LIVE_P (window))
 13940     /* If WINDOW is live, make it the selected window and its frame's
 13941        selected window and set the selected frame to its frame.  */
 13942     {
 13943       selected_window = window;
 13944       selected_frame = XWINDOW (window)->frame;
 13945       FRAME_SELECTED_WINDOW (XFRAME (selected_frame)) = window;
 13946     }
 13947   else if (FRAMEP (selected_frame) && FRAME_LIVE_P (XFRAME (selected_frame)))
 13948     /* If WINDOW is dead but the selected frame is still live, make the
 13949        latter's selected window the selected one.  */
 13950     selected_window = FRAME_SELECTED_WINDOW (XFRAME (selected_frame));
 13951   else
 13952     /* If WINDOW and the selected frame are dead, choose some live,
 13953        non-child and non-tooltip frame as the new selected frame and
 13954        make its selected window the selected window.  */
 13955     {
 13956       Lisp_Object tail;
 13957       Lisp_Object frame UNINIT;
 13958 
 13959       FOR_EACH_FRAME (tail, frame)
 13960         {
 13961           struct frame *f = XFRAME (frame);
 13962 
 13963           if (!FRAME_PARENT_FRAME (f) && !FRAME_TOOLTIP_P (f))
 13964             {
 13965               selected_frame = frame;
 13966               selected_window = FRAME_SELECTED_WINDOW (f);
 13967 
 13968               return;
 13969             }
 13970         }
 13971 
 13972       /* Abort if we cannot find a live frame.  */
 13973       emacs_abort ();
 13974     }
 13975 }
 13976 
 13977 /* Restore WINDOW, if live, as its frame's selected window.  */
 13978 static void
 13979 restore_frame_selected_window (Lisp_Object window)
 13980 {
 13981   if (WINDOW_LIVE_P (window))
 13982     /* If WINDOW is live, make it its frame's selected window.  If that
 13983        frame is the selected frame, make WINDOW the selected window as
 13984        well.  */
 13985     {
 13986       Lisp_Object frame = XWINDOW (window)->frame;
 13987 
 13988       FRAME_SELECTED_WINDOW (XFRAME (frame)) = window;
 13989       if (EQ (frame, selected_frame))
 13990         selected_window = window;
 13991     }
 13992 }
 13993 
 13994 /* Update the tab-bar item list for frame F.  This has to be done
 13995    before we start to fill in any display lines.  Called from
 13996    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 13997    and restore it here.  */
 13998 
 13999 static void
 14000 update_tab_bar (struct frame *f, bool save_match_data)
 14001 {
 14002   bool do_update = false;
 14003 
 14004 #ifdef HAVE_WINDOW_SYSTEM
 14005   if (FRAME_WINDOW_P (f) && WINDOWP (f->tab_bar_window)) {
 14006     if (WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)) > 0)
 14007       do_update = true;
 14008   }
 14009   else
 14010 #endif
 14011   if (FRAME_TAB_BAR_LINES (f) > 0)
 14012     do_update = true;
 14013 
 14014   if (do_update)
 14015     {
 14016       Lisp_Object window;
 14017       struct window *w;
 14018 
 14019       window = FRAME_SELECTED_WINDOW (f);
 14020       w = XWINDOW (window);
 14021 
 14022       /* If the user has switched buffers or windows, we need to
 14023          recompute to reflect the new bindings.  But we'll
 14024          recompute when update_mode_lines is set too; that means
 14025          that people can use force-mode-line-update to request
 14026          that the menu bar be recomputed.  The adverse effect on
 14027          the rest of the redisplay algorithm is about the same as
 14028          windows_or_buffers_changed anyway.  */
 14029       if (windows_or_buffers_changed
 14030           || w->update_mode_line
 14031           || update_mode_lines
 14032           || window_buffer_changed (w))
 14033         {
 14034           struct buffer *prev = current_buffer;
 14035           specpdl_ref count = SPECPDL_INDEX ();
 14036           Lisp_Object new_tab_bar;
 14037           int new_n_tab_bar;
 14038 
 14039           /* Set current_buffer to the buffer of the selected
 14040              window of the frame, so that we get the right local
 14041              keymaps.  */
 14042           set_buffer_internal_1 (XBUFFER (w->contents));
 14043 
 14044           /* Save match data, if we must.  */
 14045           if (save_match_data)
 14046             record_unwind_save_match_data ();
 14047 
 14048           /* Make sure that we don't accidentally use bogus keymaps.  */
 14049           if (NILP (Voverriding_local_map_menu_flag))
 14050             {
 14051               specbind (Qoverriding_terminal_local_map, Qnil);
 14052               specbind (Qoverriding_local_map, Qnil);
 14053             }
 14054 
 14055           /* We must temporarily set the selected frame to this frame
 14056              before calling tab_bar_items, because the calculation of
 14057              the tab-bar keymap uses the selected frame (see
 14058              `tab-bar-make-keymap' in tab-bar.el).  */
 14059           eassert (EQ (selected_window,
 14060                        /* Since we only explicitly preserve selected_frame,
 14061                           check that selected_window would be redundant.  */
 14062                        XFRAME (selected_frame)->selected_window));
 14063 #ifdef HAVE_WINDOW_SYSTEM
 14064           Lisp_Object frame;
 14065           record_unwind_protect (restore_selected_window, selected_window);
 14066           XSETFRAME (frame, f);
 14067           selected_frame = frame;
 14068           selected_window = FRAME_SELECTED_WINDOW (f);
 14069 #endif
 14070 
 14071           /* Build desired tab-bar items from keymaps.  */
 14072           new_tab_bar
 14073             = tab_bar_items (Fcopy_sequence (f->tab_bar_items),
 14074                               &new_n_tab_bar);
 14075 
 14076           /* Redisplay the tab-bar if we changed it.  */
 14077           if (new_n_tab_bar != f->n_tab_bar_items
 14078               || NILP (Fequal (new_tab_bar, f->tab_bar_items)))
 14079             {
 14080               /* Redisplay that happens asynchronously due to an expose event
 14081                  may access f->tab_bar_items.  Make sure we update both
 14082                  variables within BLOCK_INPUT so no such event interrupts.  */
 14083               block_input ();
 14084               fset_tab_bar_items (f, new_tab_bar);
 14085               f->n_tab_bar_items = new_n_tab_bar;
 14086               w->update_mode_line = true;
 14087               unblock_input ();
 14088             }
 14089 
 14090           unbind_to (count, Qnil);
 14091           set_buffer_internal_1 (prev);
 14092         }
 14093     }
 14094 }
 14095 
 14096 /* Redisplay the tab bar in the frame for window W.
 14097 
 14098    The tab bar of X frames that don't have X toolkit support is
 14099    displayed in a special window W->frame->tab_bar_window.
 14100 
 14101    The tab bar of terminal frames is treated specially as far as
 14102    glyph matrices are concerned.  Tab bar lines are not part of
 14103    windows, so the update is done directly on the frame matrix rows
 14104    for the tab bar.  */
 14105 
 14106 static void
 14107 display_tab_bar (struct window *w)
 14108 {
 14109   struct frame *f = XFRAME (WINDOW_FRAME (w));
 14110   struct it it;
 14111   Lisp_Object items;
 14112   int i;
 14113 
 14114   /* Don't do all this for graphical frames.  */
 14115 #ifdef HAVE_NTGUI
 14116   if (FRAME_W32_P (f))
 14117     return;
 14118 #endif
 14119 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14120   if (FRAME_X_P (f))
 14121     return;
 14122 #endif
 14123 
 14124 #ifdef HAVE_NS
 14125   if (FRAME_NS_P (f))
 14126     return;
 14127 #endif /* HAVE_NS */
 14128 
 14129 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14130   eassert (!FRAME_WINDOW_P (f));
 14131   init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14132                  + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14133                  TAB_BAR_FACE_ID);
 14134   it.first_visible_x = 0;
 14135   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14136 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 14137   if (FRAME_WINDOW_P (f))
 14138     {
 14139       /* Tab bar lines are displayed in the desired matrix of the
 14140          dummy window tab_bar_window.  */
 14141       struct window *tab_w;
 14142       tab_w = XWINDOW (f->tab_bar_window);
 14143       init_iterator (&it, tab_w, -1, -1, tab_w->desired_matrix->rows,
 14144                      TAB_BAR_FACE_ID);
 14145       it.first_visible_x = 0;
 14146       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14147     }
 14148   else
 14149 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 14150     {
 14151       /* This is a TTY frame, i.e. character hpos/vpos are used as
 14152          pixel x/y.  */
 14153       init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14154                      + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14155                      TAB_BAR_FACE_ID);
 14156       it.first_visible_x = 0;
 14157       it.last_visible_x = FRAME_COLS (f);
 14158     }
 14159 
 14160   /* FIXME: This should be controlled by a user option.  See the
 14161      comments in redisplay_tool_bar and display_mode_line about
 14162      this.  */
 14163   it.paragraph_embedding = L2R;
 14164 
 14165   /* Clear all rows of the tab bar.  */
 14166   for (i = 0; i < FRAME_TAB_BAR_LINES (f); ++i)
 14167     {
 14168       struct glyph_row *row = it.glyph_row + i;
 14169       clear_glyph_row (row);
 14170       row->enabled_p = true;
 14171       row->full_width_p = true;
 14172       row->reversed_p = false;
 14173     }
 14174 
 14175   /* Display all items of the tab bar.  */
 14176   items = it.f->tab_bar_items;
 14177   int j;
 14178   for (i = 0, j = 0; i < it.f->n_tab_bar_items; ++i, j += TAB_BAR_ITEM_NSLOTS)
 14179     {
 14180       Lisp_Object string = AREF (items, j + TAB_BAR_ITEM_CAPTION);
 14181 
 14182       /* Stop at nil string.  */
 14183       if (NILP (string))
 14184         break;
 14185 
 14186       if (it.current_x < it.last_visible_x)
 14187         display_string (NULL, string, Qnil, 0, 0, &it,
 14188                         SCHARS (string), 0, 0, STRING_MULTIBYTE (string));
 14189     }
 14190 
 14191   /* Fill out the line with spaces.  */
 14192   if (it.current_x < it.last_visible_x)
 14193     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 14194 
 14195   /* Compute the total height of the lines.  */
 14196   compute_line_metrics (&it);
 14197 }
 14198 
 14199 #ifdef HAVE_WINDOW_SYSTEM
 14200 
 14201 /* Set F->desired_tab_bar_string to a Lisp string representing frame
 14202    F's desired tab-bar contents.  F->tab_bar_items must have
 14203    been set up previously by calling prepare_menu_bars.  */
 14204 
 14205 static void
 14206 build_desired_tab_bar_string (struct frame *f)
 14207 {
 14208   int i;
 14209   Lisp_Object caption;
 14210 
 14211   caption = Qnil;
 14212 
 14213   /* Prepare F->desired_tab_bar_string.  Make a new string.  */
 14214   fset_desired_tab_bar_string (f, build_string (""));
 14215 
 14216   /* Put a `display' property on the string for the captions to display,
 14217      put a `menu_item' property on tab-bar items with a value that
 14218      is the index of the item in F's tab-bar item vector.  */
 14219   for (i = 0; i < f->n_tab_bar_items; ++i)
 14220     {
 14221 #define PROP(IDX) \
 14222   AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX))
 14223 
 14224       caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION));
 14225 
 14226       /* Put a `display' text property on the string for the caption to
 14227          display.  Put a `menu-item' property on the string that gives
 14228          the start of this item's properties in the tab-bar items
 14229          vector.  */
 14230       AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS));
 14231 
 14232       Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14233                             props, caption);
 14234 
 14235       f->desired_tab_bar_string =
 14236         concat2 (f->desired_tab_bar_string, caption);
 14237 
 14238 #undef PROP
 14239     }
 14240 }
 14241 
 14242 
 14243 /* Display one line of the tab-bar of frame IT->f.
 14244 
 14245    HEIGHT specifies the desired height of the tab-bar line.
 14246    If the actual height of the glyph row is less than HEIGHT, the
 14247    row's height is increased to HEIGHT, and the icons are centered
 14248    vertically in the new height.
 14249 
 14250    If HEIGHT is -1, we are counting needed tab-bar lines, so don't
 14251    count a final empty row in case the tab-bar width exactly matches
 14252    the window width.
 14253 */
 14254 
 14255 static void
 14256 display_tab_bar_line (struct it *it, int height)
 14257 {
 14258   struct glyph_row *row = it->glyph_row;
 14259   int max_x = it->last_visible_x;
 14260   struct glyph *last;
 14261 
 14262   /* Don't extend on a previously drawn tab bar items (Bug#16058).  */
 14263   clear_glyph_row (row);
 14264   row->enabled_p = true;
 14265   row->y = it->current_y;
 14266 
 14267   /* Note that this isn't made use of if the face hasn't a box,
 14268      so there's no need to check the face here.  */
 14269   it->start_of_box_run_p = true;
 14270 
 14271   bool enough = false;
 14272   while (it->current_x < max_x)
 14273     {
 14274       int x, n_glyphs_before, i, nglyphs;
 14275       struct it it_before;
 14276 
 14277       /* Get the next display element.  */
 14278       if (!get_next_display_element (it))
 14279         {
 14280           /* Don't count empty row if we are counting needed tab-bar lines.  */
 14281           if (height < 0 && !it->hpos)
 14282             return;
 14283           break;
 14284         }
 14285 
 14286       /* Produce glyphs.  */
 14287       n_glyphs_before = row->used[TEXT_AREA];
 14288       it_before = *it;
 14289 
 14290       PRODUCE_GLYPHS (it);
 14291 
 14292       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 14293       i = 0;
 14294       x = it_before.current_x;
 14295       while (i < nglyphs)
 14296         {
 14297           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 14298 
 14299           if (x + glyph->pixel_width > max_x)
 14300             {
 14301               /* Glyph doesn't fit on line.  Backtrack.  */
 14302               row->used[TEXT_AREA] = n_glyphs_before;
 14303               *it = it_before;
 14304               /* If this is the only glyph on this line, it will never fit on the
 14305                  tab-bar, so skip it.  But ensure there is at least one glyph,
 14306                  so we don't accidentally disable the tab-bar.  */
 14307               if (n_glyphs_before == 0
 14308                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 14309                 break;
 14310               goto out;
 14311             }
 14312 
 14313           ++it->hpos;
 14314           x += glyph->pixel_width;
 14315           ++i;
 14316         }
 14317 
 14318       enough = ITERATOR_AT_END_OF_LINE_P (it);
 14319       set_iterator_to_next (it, true);
 14320 
 14321       /* Stop at line end.  */
 14322       if (enough)
 14323         break;
 14324     }
 14325 
 14326  out:;
 14327 
 14328   row->displays_text_p = row->used[TEXT_AREA] != 0;
 14329 
 14330   /* Use default face for the border below the tab bar.
 14331 
 14332      FIXME: When auto-resize-tab-bars is grow-only, there is
 14333      no additional border below the possibly empty tab-bar lines.
 14334      So to make the extra empty lines look "normal", we have to
 14335      use the tab-bar face for the border too.  */
 14336   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14337       && !EQ (Vauto_resize_tab_bars, Qgrow_only))
 14338     it->face_id = DEFAULT_FACE_ID;
 14339 
 14340   extend_face_to_end_of_line (it);
 14341   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 14342   last->right_box_line_p = true;
 14343   if (last == row->glyphs[TEXT_AREA])
 14344     last->left_box_line_p = true;
 14345 
 14346   /* Make line the desired height and center it vertically.  */
 14347   if ((height -= it->max_ascent + it->max_descent) > 0)
 14348     {
 14349       /* Don't add more than one line height.  */
 14350       height %= FRAME_LINE_HEIGHT (it->f);
 14351       it->max_ascent += height / 2;
 14352       it->max_descent += (height + 1) / 2;
 14353     }
 14354 
 14355   compute_line_metrics (it);
 14356 
 14357   /* If line is empty, make it occupy the rest of the tab-bar.  */
 14358   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 14359     {
 14360       row->height = row->phys_height = it->last_visible_y - row->y;
 14361       row->visible_height = row->height;
 14362       row->ascent = row->phys_ascent = 0;
 14363       row->extra_line_spacing = 0;
 14364     }
 14365 
 14366   row->full_width_p = true;
 14367   row->continued_p = false;
 14368   row->truncated_on_left_p = false;
 14369   row->truncated_on_right_p = false;
 14370 
 14371   it->current_x = it->hpos = 0;
 14372   it->current_y += row->height;
 14373   ++it->vpos;
 14374   ++it->glyph_row;
 14375 }
 14376 
 14377 
 14378 /* Value is the number of pixels needed to make all tab-bar items of
 14379    frame F visible.  The actual number of glyph rows needed is
 14380    returned in *N_ROWS if non-NULL.  */
 14381 static int
 14382 tab_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 14383 {
 14384   struct window *w = XWINDOW (f->tab_bar_window);
 14385   struct it it;
 14386   /* tab_bar_height is called from redisplay_tab_bar after building
 14387      the desired matrix, so use (unused) mode-line row as temporary row to
 14388      avoid destroying the first tab-bar row.  */
 14389   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 14390 
 14391   /* Initialize an iterator for iteration over
 14392      F->desired_tab_bar_string in the tab-bar window of frame F.  */
 14393   init_iterator (&it, w, -1, -1, temp_row, TAB_BAR_FACE_ID);
 14394   temp_row->reversed_p = false;
 14395   it.first_visible_x = 0;
 14396   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14397   reseat_to_string (&it, NULL, f->desired_tab_bar_string,
 14398                     0, 0, 0, STRING_MULTIBYTE (f->desired_tab_bar_string));
 14399   it.paragraph_embedding = L2R;
 14400 
 14401   clear_glyph_row (temp_row);
 14402   while (!ITERATOR_AT_END_P (&it))
 14403     {
 14404       it.glyph_row = temp_row;
 14405       display_tab_bar_line (&it, -1);
 14406     }
 14407   clear_glyph_row (temp_row);
 14408 
 14409   /* f->n_tab_bar_rows == 0 means "unknown"; -1 means no tab-bar.  */
 14410   if (n_rows)
 14411     *n_rows = it.vpos > 0 ? it.vpos : -1;
 14412 
 14413   if (pixelwise)
 14414     return it.current_y;
 14415   else
 14416     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 14417 }
 14418 
 14419 DEFUN ("tab-bar-height", Ftab_bar_height, Stab_bar_height,
 14420        0, 2, 0,
 14421        doc: /* Return the number of lines occupied by the tab bar of FRAME.
 14422 If FRAME is nil or omitted, use the selected frame.  Optional argument
 14423 PIXELWISE non-nil means return the height of the tab bar in pixels.  */)
 14424   (Lisp_Object frame, Lisp_Object pixelwise)
 14425 {
 14426   int height = 0;
 14427 
 14428   struct frame *f = decode_any_frame (frame);
 14429 
 14430   if (WINDOWP (f->tab_bar_window)
 14431       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tab_bar_window)) > 0)
 14432     {
 14433       update_tab_bar (f, true);
 14434       if (f->n_tab_bar_items)
 14435         {
 14436           build_desired_tab_bar_string (f);
 14437           height = tab_bar_height (f, NULL, !NILP (pixelwise));
 14438         }
 14439     }
 14440 
 14441   return make_fixnum (height);
 14442 }
 14443 
 14444 
 14445 /* Display the tab-bar of frame F.  Value is true if tab-bar's
 14446    height should be changed.  */
 14447 static bool
 14448 redisplay_tab_bar (struct frame *f)
 14449 {
 14450   struct window *w;
 14451   struct it it;
 14452   struct glyph_row *row;
 14453 
 14454   f->tab_bar_redisplayed = true;
 14455 
 14456   /* If frame hasn't a tab-bar window or if it is zero-height, don't
 14457      do anything.  This means you must start with tab-bar-lines
 14458      non-zero to get the auto-sizing effect.  Or in other words, you
 14459      can turn off tab-bars by specifying tab-bar-lines zero.  */
 14460   if (!WINDOWP (f->tab_bar_window)
 14461       || (w = XWINDOW (f->tab_bar_window),
 14462           WINDOW_TOTAL_LINES (w) == 0))
 14463     {
 14464       /* Even if we do not display a tab bar initially, still pretend
 14465          that we have resized it.  This avoids that a later activation
 14466          of the tab bar resizes the frame, despite of the fact that the
 14467          setting of 'frame-inhibit-implied-resize' should inhibit it
 14468          (Bug#52986).  */
 14469       f->tab_bar_resized = true;
 14470 
 14471       return false;
 14472     }
 14473 
 14474   /* Build a string that represents the contents of the tab-bar.  */
 14475   build_desired_tab_bar_string (f);
 14476 
 14477   int new_nrows;
 14478   int new_height = tab_bar_height (f, &new_nrows, true);
 14479 
 14480   if (f->n_tab_bar_rows == 0)
 14481     {
 14482       f->n_tab_bar_rows = new_nrows;
 14483       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 14484         frame_default_tab_bar_height = new_height;
 14485     }
 14486 
 14487   /* If new_height or new_nrows indicate that we need to enlarge or
 14488      shrink the tab-bar window, we can return right away.  */
 14489   if (new_nrows > f->n_tab_bar_rows
 14490       || (EQ (Vauto_resize_tab_bars, Qgrow_only)
 14491           && !f->minimize_tab_bar_window_p
 14492           && new_height > WINDOW_PIXEL_HEIGHT (w))
 14493       || (! EQ (Vauto_resize_tab_bars, Qgrow_only)
 14494           && new_height < WINDOW_PIXEL_HEIGHT (w)))
 14495     {
 14496       if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14497         FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14498       if (new_nrows != f->n_tab_bar_rows)
 14499         f->n_tab_bar_rows = new_nrows;
 14500       clear_glyph_matrix (w->desired_matrix);
 14501       f->fonts_changed = true;
 14502       return true;
 14503     }
 14504 
 14505   /* Set up an iterator for the tab-bar window.  */
 14506   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TAB_BAR_FACE_ID);
 14507   it.first_visible_x = 0;
 14508   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14509   row = it.glyph_row;
 14510   row->reversed_p = false;
 14511   reseat_to_string (&it, NULL, f->desired_tab_bar_string, 0, 0, 0,
 14512                     STRING_MULTIBYTE (f->desired_tab_bar_string));
 14513   /* FIXME: This should be controlled by a user option.  But it
 14514      doesn't make sense to have an R2L tab bar if the menu bar cannot
 14515      be drawn also R2L, and making the menu bar R2L is tricky due
 14516      tabkit-specific code that implements it.  If an R2L tab bar is
 14517      ever supported, display_tab_bar_line should also be augmented to
 14518      call unproduce_glyphs like display_line and display_string
 14519      do.  */
 14520   it.paragraph_embedding = L2R;
 14521 
 14522   /* Display as many lines as needed to display all tab-bar items.  */
 14523 
 14524   if (f->n_tab_bar_rows > 0)
 14525     {
 14526       int border, rows, height, extra;
 14527 
 14528       if (TYPE_RANGED_FIXNUMP (int, Vtab_bar_border))
 14529         border = XFIXNUM (Vtab_bar_border);
 14530       else if (EQ (Vtab_bar_border, Qinternal_border_width))
 14531         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 14532       else if (EQ (Vtab_bar_border, Qborder_width))
 14533         border = f->border_width;
 14534       else
 14535         border = 0;
 14536       if (border < 0)
 14537         border = 0;
 14538 
 14539       rows = f->n_tab_bar_rows;
 14540       height = max (1, (it.last_visible_y - border) / rows);
 14541       extra = it.last_visible_y - border - height * rows;
 14542 
 14543       while (it.current_y < it.last_visible_y)
 14544         {
 14545           int h = 0;
 14546           if (extra > 0 && rows-- > 0)
 14547             {
 14548               h = (extra + rows - 1) / rows;
 14549               extra -= h;
 14550             }
 14551           display_tab_bar_line (&it, height + h);
 14552         }
 14553     }
 14554   else
 14555     {
 14556       while (it.current_y < it.last_visible_y)
 14557         display_tab_bar_line (&it, 0);
 14558     }
 14559 
 14560   /* It doesn't make much sense to try scrolling in the tab-bar
 14561      window, so don't do it.  */
 14562   w->desired_matrix->no_scrolling_p = true;
 14563   w->must_be_updated_p = true;
 14564 
 14565   if (!NILP (Vauto_resize_tab_bars))
 14566     {
 14567       bool change_height_p = false;
 14568 
 14569       /* If we couldn't display everything, change the tab-bar's
 14570          height if there is room for more.  */
 14571       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 14572         change_height_p = true;
 14573 
 14574       /* We subtract 1 because display_tab_bar_line advances the
 14575          glyph_row pointer before returning to its caller.  We want to
 14576          examine the last glyph row produced by
 14577          display_tab_bar_line.  */
 14578       row = it.glyph_row - 1;
 14579 
 14580       /* If there are blank lines at the end, except for a partially
 14581          visible blank line at the end that is smaller than
 14582          FRAME_LINE_HEIGHT, change the tab-bar's height.  */
 14583       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14584           && row->height >= FRAME_LINE_HEIGHT (f))
 14585         change_height_p = true;
 14586 
 14587       /* If row displays tab-bar items, but is partially visible,
 14588          change the tab-bar's height.  */
 14589       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14590           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 14591         change_height_p = true;
 14592 
 14593       /* Resize windows as needed by changing the `tab-bar-lines'
 14594          frame parameter.  */
 14595       if (change_height_p)
 14596         {
 14597           int nrows;
 14598           int new_height = tab_bar_height (f, &nrows, true);
 14599 
 14600           change_height_p = ((EQ (Vauto_resize_tab_bars, Qgrow_only)
 14601                               && !f->minimize_tab_bar_window_p)
 14602                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 14603                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 14604           f->minimize_tab_bar_window_p = false;
 14605 
 14606           if (change_height_p)
 14607             {
 14608               if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14609                 FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14610               frame_default_tab_bar_height = new_height;
 14611               clear_glyph_matrix (w->desired_matrix);
 14612               f->n_tab_bar_rows = nrows;
 14613               f->fonts_changed = true;
 14614 
 14615               return true;
 14616             }
 14617         }
 14618     }
 14619 
 14620   f->minimize_tab_bar_window_p = false;
 14621   return false;
 14622 }
 14623 
 14624 /* Get information about the tab-bar item which is displayed in GLYPH
 14625    on frame F.  Return in *PROP_IDX the index where tab-bar item
 14626    properties start in F->tab_bar_items.  Return in CLOSE_P an
 14627    indication whether the click was on the close-tab icon of the tab.
 14628    Value is false if GLYPH doesn't display a tab-bar item.  */
 14629 
 14630 static bool
 14631 tab_bar_item_info (struct frame *f, struct glyph *glyph,
 14632                    int *prop_idx, bool *close_p)
 14633 {
 14634   Lisp_Object prop;
 14635   ptrdiff_t charpos;
 14636 
 14637   /* This function can be called asynchronously, which means we must
 14638      exclude any possibility that Fget_text_property signals an
 14639      error.  */
 14640   charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos);
 14641   charpos = max (0, charpos);
 14642 
 14643   /* Get the text property `menu-item' at pos. The value of that
 14644      property is the start index of this item's properties in
 14645      F->tab_bar_items.  */
 14646   prop = Fget_text_property (make_fixnum (charpos),
 14647                              Qmenu_item, f->current_tab_bar_string);
 14648   if (! FIXNUMP (prop))
 14649     return false;
 14650 
 14651   *prop_idx = XFIXNUM (prop);
 14652 
 14653   if (close_p)
 14654     *close_p = !NILP (Fget_text_property (make_fixnum (charpos),
 14655                                           Qclose_tab,
 14656                                           f->current_tab_bar_string));
 14657 
 14658   return true;
 14659 }
 14660 
 14661 
 14662 /* Get information about the tab-bar item at position X/Y on frame F's
 14663    tab bar window.
 14664 
 14665    Set *GLYPH to a pointer to the glyph of the tab-bar item in the
 14666    current matrix of the tab-bar window of F, or NULL if not on a
 14667    tab-bar item.  Return in *PROP_IDX the index of the tab-bar item in
 14668    F->tab_bar_items.
 14669 
 14670    Place the window-relative vpos of Y in *VPOS, and the
 14671    window-relative hpos of X in *HPOS.  If CLOSE_P, set it to whether
 14672    or not the tab bar item represents a button that should close a
 14673    tab.
 14674 
 14675    Value is
 14676 
 14677    -1   if X/Y is not on a tab-bar item
 14678    0    if X/Y is on the same item that was highlighted before.
 14679    1    otherwise.  */
 14680 
 14681 static int
 14682 get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 14683                   int *hpos, int *vpos, int *prop_idx, bool *close_p)
 14684 {
 14685   struct window *w = XWINDOW (f->tab_bar_window);
 14686   int area;
 14687 
 14688   /* Find the glyph under X/Y.  */
 14689   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 14690   if (*glyph == NULL)
 14691     return -1;
 14692 
 14693   /* Get the start of this tab-bar item's properties in
 14694      f->tab_bar_items.  */
 14695   if (!tab_bar_item_info (f, *glyph, prop_idx, close_p))
 14696     return -1;
 14697 
 14698   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14699 }
 14700 
 14701 /* EXPORT:
 14702 
 14703    Like `get_tab_bar_item'.  However, don't return anything for GLYPH,
 14704    HPOS, or VPOS, and treat X and Y as relative to F itself, as
 14705    opposed to its tab bar window.  */
 14706 
 14707 int
 14708 get_tab_bar_item_kbd (struct frame *f, int x, int y, int *prop_idx,
 14709                       bool *close_p)
 14710 {
 14711   struct window *w;
 14712   int area, vpos, hpos;
 14713   struct glyph *glyph;
 14714 
 14715   w = XWINDOW (f->tab_bar_window);
 14716 
 14717   /* Convert X and Y to window coordinates.  */
 14718   frame_to_window_pixel_xy (w, &x, &y);
 14719 
 14720   /* Find the glyph under X/Y.  */
 14721   glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, 0,
 14722                             0, &area);
 14723   if (glyph == NULL)
 14724     return -1;
 14725 
 14726   /* Get the start of this tab-bar item's properties in
 14727      f->tab_bar_items.  */
 14728   if (!tab_bar_item_info (f, glyph, prop_idx, close_p))
 14729     return -1;
 14730 
 14731   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14732 }
 14733 
 14734 /* EXPORT:
 14735    Handle mouse button event on the tab-bar of frame F, at
 14736    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 14737    false for button release.  MODIFIERS is event modifiers for button
 14738    release.  */
 14739 
 14740 Lisp_Object
 14741 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14742                       int modifiers)
 14743 {
 14744   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14745   struct window *w = XWINDOW (f->tab_bar_window);
 14746   int hpos, vpos, prop_idx;
 14747   bool close_p;
 14748   struct glyph *glyph;
 14749   Lisp_Object enabled_p;
 14750   int ts;
 14751 
 14752   frame_to_window_pixel_xy (w, &x, &y);
 14753   ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14754   if (ts == -1)
 14755     return Fcons (Qtab_bar, Qnil);
 14756 
 14757   /* If item is disabled, do nothing.  */
 14758   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14759   if (NILP (enabled_p))
 14760     return Qnil;
 14761 
 14762   if (down_p)
 14763     {
 14764       /* Show the clicked button in pressed state.  */
 14765       if (!NILP (Vmouse_highlight))
 14766         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 14767       f->last_tab_bar_item = prop_idx; /* record the pressed tab */
 14768     }
 14769   else
 14770     {
 14771       /* Show item in released state.  */
 14772       if (!NILP (Vmouse_highlight))
 14773         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 14774       f->last_tab_bar_item = -1;
 14775     }
 14776 
 14777   Lisp_Object caption =
 14778     Fcopy_sequence (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION));
 14779 
 14780   AUTO_LIST2 (props, Qmenu_item,
 14781               list3 (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_KEY),
 14782                      AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_BINDING),
 14783                      close_p ? Qt : Qnil));
 14784 
 14785   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14786                         props, caption);
 14787 
 14788   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14789 }
 14790 
 14791 
 14792 /* Possibly highlight a tab-bar item on frame F when mouse moves to
 14793    tab-bar window-relative coordinates X/Y.  Called from
 14794    note_mouse_highlight.  */
 14795 
 14796 static void
 14797 note_tab_bar_highlight (struct frame *f, int x, int y)
 14798 {
 14799   Lisp_Object window = f->tab_bar_window;
 14800   struct window *w = XWINDOW (window);
 14801   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14802   int hpos, vpos;
 14803   struct glyph *glyph;
 14804   struct glyph_row *row;
 14805   int i;
 14806   Lisp_Object enabled_p;
 14807   int prop_idx;
 14808   bool close_p;
 14809   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 14810   int rc;
 14811 
 14812   /* Function note_mouse_highlight is called with negative X/Y
 14813      values when mouse moves outside of the frame.  */
 14814   if (x <= 0 || y <= 0)
 14815     {
 14816       clear_mouse_face (hlinfo);
 14817       return;
 14818     }
 14819 
 14820   rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14821   if (rc < 0)
 14822     {
 14823       /* Not on tab-bar item.  */
 14824       clear_mouse_face (hlinfo);
 14825       return;
 14826     }
 14827   else if (rc == 0)
 14828     /* On same tab-bar item as before.  */
 14829     goto set_help_echo;
 14830 
 14831   clear_mouse_face (hlinfo);
 14832 
 14833   bool mouse_down_p = false;
 14834   /* Mouse is down, but on different tab-bar item?  Or alternatively,
 14835      the mouse might've been pressed somewhere we don't know about,
 14836      and then have moved onto the tab bar.  In this case,
 14837      last_tab_bar_item is -1, so we DTRT and behave like other
 14838      programs by displaying the item as sunken. */
 14839   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 14840   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 14841                   && f == dpyinfo->last_mouse_frame);
 14842 
 14843   if (mouse_down_p && f->last_tab_bar_item != prop_idx
 14844       && f->last_tab_bar_item != -1)
 14845     return;
 14846   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 14847 
 14848   /* If tab-bar item is not enabled, don't highlight it.  */
 14849   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14850   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 14851     {
 14852       /* Compute the x-position of the glyph.  In front and past the
 14853          image is a space.  We include this in the highlighted area.  */
 14854       row = MATRIX_ROW (w->current_matrix, vpos);
 14855       for (i = x = 0; i < hpos; ++i)
 14856         x += row->glyphs[TEXT_AREA][i].pixel_width;
 14857 
 14858       /* Record this as the current active region.  */
 14859       hlinfo->mouse_face_beg_col = hpos;
 14860       hlinfo->mouse_face_beg_row = vpos;
 14861       hlinfo->mouse_face_beg_x = x;
 14862       hlinfo->mouse_face_past_end = false;
 14863 
 14864       hlinfo->mouse_face_end_col = hpos + 1;
 14865       hlinfo->mouse_face_end_row = vpos;
 14866       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 14867       hlinfo->mouse_face_window = window;
 14868       hlinfo->mouse_face_face_id = TAB_BAR_FACE_ID;
 14869 
 14870       /* Display it as active.  */
 14871       show_mouse_face (hlinfo, draw);
 14872     }
 14873 
 14874  set_help_echo:
 14875 
 14876   /* Set help_echo_string to a help string to display for this tab-bar item.
 14877      XTread_socket does the rest.  */
 14878   help_echo_object = help_echo_window = Qnil;
 14879   help_echo_pos = -1;
 14880   help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_HELP);
 14881   if (NILP (help_echo_string))
 14882     help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION);
 14883 }
 14884 
 14885 #endif /* HAVE_WINDOW_SYSTEM */
 14886 
 14887 /* Find the tab-bar item at X coordinate and return its information.  */
 14888 static Lisp_Object
 14889 tty_get_tab_bar_item (struct frame *f, int x, int *prop_idx, bool *close_p)
 14890 {
 14891   ptrdiff_t clen = 0;
 14892 
 14893   for (int i = 0; i < f->n_tab_bar_items; i++)
 14894     {
 14895       Lisp_Object caption = AREF (f->tab_bar_items, (i * TAB_BAR_ITEM_NSLOTS
 14896                                                      + TAB_BAR_ITEM_CAPTION));
 14897       if (NILP (caption))
 14898         return Qnil;
 14899       clen += SCHARS (caption);
 14900       if (x < clen)
 14901         {
 14902           *prop_idx = i;
 14903           *close_p = !NILP (Fget_text_property (make_fixnum (SCHARS (caption)
 14904                                                              - (clen - x)),
 14905                                                 Qclose_tab,
 14906                                                 caption));
 14907           return caption;
 14908         }
 14909     }
 14910   return Qnil;
 14911 }
 14912 
 14913 /* Handle a mouse click at X/Y on the tab bar of TTY frame F.  If the
 14914    click was on the tab bar and was handled, populate the EVENT
 14915    structure, store it in keyboard queue, and return true; otherwise
 14916    return false.  MODIFIERS are event modifiers for generating the tab
 14917    release event.  */
 14918 Lisp_Object
 14919 tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14920                           struct input_event *event)
 14921 {
 14922   /* Did they click on the tab bar?  */
 14923   if (y < FRAME_MENU_BAR_LINES (f)
 14924       || y >= FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f))
 14925     return Qnil;
 14926 
 14927   /* Find the tab-bar item where the X,Y coordinates belong.  */
 14928   int prop_idx;
 14929   bool close_p;
 14930   Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &close_p);
 14931 
 14932   if (NILP (caption))
 14933     return Qnil;
 14934 
 14935   if (NILP (AREF (f->tab_bar_items,
 14936                   prop_idx * TAB_BAR_ITEM_NSLOTS + TAB_BAR_ITEM_ENABLED_P)))
 14937     return Qnil;
 14938 
 14939   if (down_p)
 14940     f->last_tab_bar_item = prop_idx;
 14941   else
 14942     f->last_tab_bar_item = -1;
 14943 
 14944   caption = Fcopy_sequence (caption);
 14945 
 14946   AUTO_LIST2 (props, Qmenu_item,
 14947               list3 (AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14948                            + TAB_BAR_ITEM_KEY),
 14949                      AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14950                            + TAB_BAR_ITEM_BINDING),
 14951                      close_p ? Qt : Qnil));
 14952 
 14953   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14954                         props, caption);
 14955 
 14956   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14957 }
 14958 
 14959 
 14960 
 14961 /***********************************************************************
 14962                                Tool-bars
 14963  ***********************************************************************/
 14964 
 14965 #ifdef HAVE_WINDOW_SYSTEM
 14966 
 14967 /* Update the tool-bar item list for frame F.  This has to be done
 14968    before we start to fill in any display lines.  Called from
 14969    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 14970    and restore it here.  */
 14971 
 14972 static void
 14973 update_tool_bar (struct frame *f, bool save_match_data)
 14974 {
 14975 #ifdef HAVE_EXT_TOOL_BAR
 14976   bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
 14977 #else
 14978   bool do_update = (WINDOWP (f->tool_bar_window)
 14979                     && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
 14980 #endif
 14981 
 14982   if (do_update)
 14983     {
 14984       Lisp_Object window;
 14985       struct window *w;
 14986 
 14987       window = FRAME_SELECTED_WINDOW (f);
 14988       w = XWINDOW (window);
 14989 
 14990       /* If the user has switched buffers or windows, we need to
 14991          recompute to reflect the new bindings.  But we'll
 14992          recompute when update_mode_lines is set too; that means
 14993          that people can use force-mode-line-update to request
 14994          that the menu bar be recomputed.  The adverse effect on
 14995          the rest of the redisplay algorithm is about the same as
 14996          windows_or_buffers_changed anyway.  */
 14997       if (windows_or_buffers_changed
 14998           || w->update_mode_line
 14999           || update_mode_lines
 15000           || window_buffer_changed (w))
 15001         {
 15002           struct buffer *prev = current_buffer;
 15003           specpdl_ref count = SPECPDL_INDEX ();
 15004           Lisp_Object frame, new_tool_bar;
 15005           int new_n_tool_bar;
 15006 
 15007           /* Set current_buffer to the buffer of the selected
 15008              window of the frame, so that we get the right local
 15009              keymaps.  */
 15010           set_buffer_internal_1 (XBUFFER (w->contents));
 15011 
 15012           /* Save match data, if we must.  */
 15013           if (save_match_data)
 15014             record_unwind_save_match_data ();
 15015 
 15016           /* Make sure that we don't accidentally use bogus keymaps.  */
 15017           if (NILP (Voverriding_local_map_menu_flag))
 15018             {
 15019               specbind (Qoverriding_terminal_local_map, Qnil);
 15020               specbind (Qoverriding_local_map, Qnil);
 15021             }
 15022 
 15023           /* We must temporarily set the selected frame to this frame
 15024              before calling tool_bar_items, because the calculation of
 15025              the tool-bar keymap uses the selected frame (see
 15026              `tool-bar-make-keymap' in tool-bar.el).  */
 15027           eassert (EQ (selected_window,
 15028                        /* Since we only explicitly preserve selected_frame,
 15029                           check that selected_window would be redundant.  */
 15030                        XFRAME (selected_frame)->selected_window));
 15031           record_unwind_protect (restore_selected_window, selected_window);
 15032           XSETFRAME (frame, f);
 15033           selected_frame = frame;
 15034           selected_window = FRAME_SELECTED_WINDOW (f);
 15035 
 15036           /* Build desired tool-bar items from keymaps.  */
 15037           new_tool_bar
 15038             = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
 15039                               &new_n_tool_bar);
 15040 
 15041           /* Redisplay the tool-bar if we changed it.  */
 15042           if (new_n_tool_bar != f->n_tool_bar_items
 15043               || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
 15044             {
 15045               /* Redisplay that happens asynchronously due to an expose event
 15046                  may access f->tool_bar_items.  Make sure we update both
 15047                  variables within BLOCK_INPUT so no such event interrupts.  */
 15048               block_input ();
 15049               fset_tool_bar_items (f, new_tool_bar);
 15050               f->n_tool_bar_items = new_n_tool_bar;
 15051               w->update_mode_line = true;
 15052               unblock_input ();
 15053             }
 15054 
 15055           unbind_to (count, Qnil);
 15056           set_buffer_internal_1 (prev);
 15057         }
 15058     }
 15059 }
 15060 
 15061 #ifndef HAVE_EXT_TOOL_BAR
 15062 
 15063 /* Set F->desired_tool_bar_string to a Lisp string representing frame
 15064    F's desired tool-bar contents.  F->tool_bar_items must have
 15065    been set up previously by calling prepare_menu_bars.
 15066 
 15067    Also set F->tool_bar_wraps_p to whether or not the tool bar
 15068    contains explicit line breaking items.  */
 15069 
 15070 static void
 15071 build_desired_tool_bar_string (struct frame *f)
 15072 {
 15073   int i, size, size_needed;
 15074   Lisp_Object image, plist;
 15075 
 15076   image = plist = Qnil;
 15077 
 15078   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
 15079      Otherwise, make a new string.  */
 15080 
 15081   /* The size of the string we might be able to reuse.  */
 15082   size = (STRINGP (f->desired_tool_bar_string)
 15083           ? SCHARS (f->desired_tool_bar_string)
 15084           : 0);
 15085 
 15086   /* We need one space in the string for each image.  */
 15087   size_needed = f->n_tool_bar_items;
 15088 
 15089   /* Reuse f->desired_tool_bar_string, if possible.  */
 15090 
 15091   if (size < size_needed || NILP (f->desired_tool_bar_string))
 15092     /* Don't initialize the contents of this string yet, as they will
 15093        be set within the loop below.  */
 15094     fset_desired_tool_bar_string (f, make_uninit_string (size_needed));
 15095   else
 15096     {
 15097       AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
 15098       Fremove_text_properties (make_fixnum (0), make_fixnum (size),
 15099                                props, f->desired_tool_bar_string);
 15100     }
 15101 
 15102   f->tool_bar_wraps_p = false;
 15103 
 15104   /* Put a `display' property on the string for the images to display,
 15105      put a `menu_item' property on tool-bar items with a value that
 15106      is the index of the item in F's tool-bar item vector.  */
 15107   for (i = 0; i < f->n_tool_bar_items; ++i)
 15108     {
 15109 #define PROP(IDX) \
 15110   AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
 15111 
 15112       bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
 15113       bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
 15114       int hmargin, vmargin, relief, idx, end;
 15115 
 15116       if (!NILP (PROP (TOOL_BAR_ITEM_WRAP)))
 15117         {
 15118           /* This is a line wrap.  Instead of building a tool bar
 15119              item, display a new line character instead.  */
 15120           SSET (f->desired_tool_bar_string, i, '\n');
 15121 
 15122           /* Set F->tool_bar_wraps_p.  This tells redisplay_tool_bar
 15123              to allow individual rows to be different heights.  */
 15124           f->tool_bar_wraps_p = true;
 15125           continue;
 15126         }
 15127 
 15128       /* Replace this with a space character.  */
 15129       SSET (f->desired_tool_bar_string, i, ' ');
 15130 
 15131       /* If image is a vector, choose the image according to the
 15132          button state.  */
 15133       image = PROP (TOOL_BAR_ITEM_IMAGES);
 15134       if (VECTORP (image))
 15135         {
 15136           if (enabled_p)
 15137             idx = (selected_p
 15138                    ? TOOL_BAR_IMAGE_ENABLED_SELECTED
 15139                    : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
 15140           else
 15141             idx = (selected_p
 15142                    ? TOOL_BAR_IMAGE_DISABLED_SELECTED
 15143                    : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
 15144 
 15145           eassert (ASIZE (image) >= idx);
 15146           image = AREF (image, idx);
 15147         }
 15148       else
 15149         idx = -1;
 15150 
 15151       /* Ignore invalid image specifications.  */
 15152       if (!valid_image_p (image))
 15153         continue;
 15154 
 15155       /* Display the tool-bar button pressed, or depressed.  */
 15156       plist = Fcopy_sequence (XCDR (image));
 15157 
 15158       /* Compute margin and relief to draw.  */
 15159       relief = (tool_bar_button_relief >= 0
 15160                 ? min (tool_bar_button_relief,
 15161                        min (INT_MAX, MOST_POSITIVE_FIXNUM))
 15162                 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
 15163       hmargin = vmargin = relief;
 15164 
 15165       if (RANGED_FIXNUMP (1, Vtool_bar_button_margin,
 15166                            INT_MAX - max (hmargin, vmargin)))
 15167         {
 15168           hmargin += XFIXNAT (Vtool_bar_button_margin);
 15169           vmargin += XFIXNAT (Vtool_bar_button_margin);
 15170         }
 15171       else if (CONSP (Vtool_bar_button_margin))
 15172         {
 15173           if (RANGED_FIXNUMP (1, XCAR (Vtool_bar_button_margin),
 15174                                INT_MAX - hmargin))
 15175             hmargin += XFIXNAT (XCAR (Vtool_bar_button_margin));
 15176 
 15177           if (RANGED_FIXNUMP (1, XCDR (Vtool_bar_button_margin),
 15178                                INT_MAX - vmargin))
 15179             vmargin += XFIXNAT (XCDR (Vtool_bar_button_margin));
 15180         }
 15181 
 15182       if (auto_raise_tool_bar_buttons_p)
 15183         {
 15184           /* Add a `:relief' property to the image spec if the item is
 15185              selected.  */
 15186           if (selected_p)
 15187             {
 15188               plist = plist_put (plist, QCrelief, make_fixnum (-relief));
 15189               hmargin -= relief;
 15190               vmargin -= relief;
 15191             }
 15192         }
 15193       else
 15194         {
 15195           /* If image is selected, display it pressed, i.e. with a
 15196              negative relief.  If it's not selected, display it with a
 15197              raised relief.  */
 15198           plist = plist_put (plist, QCrelief,
 15199                              (selected_p
 15200                               ? make_fixnum (-relief)
 15201                               : make_fixnum (relief)));
 15202           hmargin -= relief;
 15203           vmargin -= relief;
 15204         }
 15205 
 15206       /* Put a margin around the image.  */
 15207       if (hmargin || vmargin)
 15208         {
 15209           if (hmargin == vmargin)
 15210             plist = plist_put (plist, QCmargin, make_fixnum (hmargin));
 15211           else
 15212             plist = plist_put (plist, QCmargin,
 15213                                Fcons (make_fixnum (hmargin),
 15214                                       make_fixnum (vmargin)));
 15215         }
 15216 
 15217       /* If button is not enabled, and we don't have special images
 15218          for the disabled state, make the image appear disabled by
 15219          applying an appropriate algorithm to it.  */
 15220       if (!enabled_p && idx < 0)
 15221         plist = plist_put (plist, QCconversion, Qdisabled);
 15222 
 15223       /* Put a `display' text property on the string for the image to
 15224          display.  Put a `menu-item' property on the string that gives
 15225          the start of this item's properties in the tool-bar items
 15226          vector.  */
 15227       image = Fcons (Qimage, plist);
 15228       AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
 15229                   make_fixnum (i * TOOL_BAR_ITEM_NSLOTS));
 15230 
 15231       /* Let the last image hide all remaining spaces in the tool bar
 15232          string.  The string can be longer than needed when we reuse a
 15233          previous string.  */
 15234       if (i + 1 == f->n_tool_bar_items)
 15235         end = SCHARS (f->desired_tool_bar_string);
 15236       else
 15237         end = i + 1;
 15238       Fadd_text_properties (make_fixnum (i), make_fixnum (end),
 15239                             props, f->desired_tool_bar_string);
 15240 #undef PROP
 15241     }
 15242 
 15243   /* Now replace each character between i and the end of the tool bar
 15244      string with spaces, to prevent stray newlines from accumulating
 15245      when the number of tool bar items decreases.  `size' is 0 if the
 15246      tool bar string is new, but in that case the string will have
 15247      been completely initialized anyway.  */
 15248 
 15249   for (; i < size; ++i)
 15250     /* Replace this with a space character.  */
 15251     SSET (f->desired_tool_bar_string, i, ' ');
 15252 }
 15253 
 15254 
 15255 /* Display one line of the tool-bar of frame IT->f.
 15256 
 15257    HEIGHT specifies the desired height of the tool-bar line.
 15258    If the actual height of the glyph row is less than HEIGHT, the
 15259    row's height is increased to HEIGHT, and the icons are centered
 15260    vertically in the new height.
 15261 
 15262    If HEIGHT is -1, we are counting needed tool-bar lines, so don't
 15263    count a final empty row in case the tool-bar width exactly matches
 15264    the window width.
 15265 
 15266    HEIGHT may also be -1 if there is an explicit line wrapping item
 15267    inside the tool bar; in that case, allow individual rows of the
 15268    tool bar to differ in height.  */
 15269 
 15270 static void
 15271 display_tool_bar_line (struct it *it, int height)
 15272 {
 15273   struct glyph_row *row = it->glyph_row;
 15274   int max_x = it->last_visible_x;
 15275   struct glyph *last;
 15276 
 15277   /* Don't extend on a previously drawn tool bar items (Bug#16058).  */
 15278   clear_glyph_row (row);
 15279   row->enabled_p = true;
 15280   row->y = it->current_y;
 15281 
 15282   /* Note that this isn't made use of if the face hasn't a box,
 15283      so there's no need to check the face here.  */
 15284   it->start_of_box_run_p = true;
 15285 
 15286   while (it->current_x < max_x)
 15287     {
 15288       int x, n_glyphs_before, i, nglyphs;
 15289       struct it it_before;
 15290 
 15291       /* Get the next display element.  */
 15292       if (!get_next_display_element (it))
 15293         {
 15294           /* Don't count empty row if we are counting needed tool-bar lines.  */
 15295           if (height < 0 && !it->hpos)
 15296             return;
 15297           break;
 15298         }
 15299 
 15300       /* Produce glyphs.  */
 15301       n_glyphs_before = row->used[TEXT_AREA];
 15302       it_before = *it;
 15303 
 15304       PRODUCE_GLYPHS (it);
 15305 
 15306       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 15307       i = 0;
 15308       x = it_before.current_x;
 15309       while (i < nglyphs)
 15310         {
 15311           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 15312 
 15313           if (x + glyph->pixel_width > max_x)
 15314             {
 15315               /* Glyph doesn't fit on line.  Backtrack.  */
 15316               row->used[TEXT_AREA] = n_glyphs_before;
 15317               *it = it_before;
 15318               /* If this is the only glyph on this line, it will never fit on the
 15319                  tool-bar, so skip it.  But ensure there is at least one glyph,
 15320                  so we don't accidentally disable the tool-bar.  */
 15321               if (n_glyphs_before == 0
 15322                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 15323                 break;
 15324               goto out;
 15325             }
 15326 
 15327           ++it->hpos;
 15328           x += glyph->pixel_width;
 15329           ++i;
 15330         }
 15331 
 15332       /* Stop at the end of the iterator, and move to the next line
 15333          upon a '\n' appearing in the tool bar string.  Tool bar
 15334          strings may contain multiple new line characters when
 15335          explicit wrap items are encountered.  */
 15336 
 15337       if (ITERATOR_AT_END_OF_LINE_P (it))
 15338         {
 15339           reseat_at_next_visible_line_start (it, false);
 15340           break;
 15341         }
 15342 
 15343       if (ITERATOR_AT_END_P (it))
 15344         break;
 15345 
 15346       set_iterator_to_next (it, true);
 15347     }
 15348 
 15349  out:;
 15350 
 15351   row->displays_text_p = row->used[TEXT_AREA] != 0;
 15352 
 15353   /* Use default face for the border below the tool bar.
 15354 
 15355      FIXME: When auto-resize-tool-bars is grow-only, there is
 15356      no additional border below the possibly empty tool-bar lines.
 15357      So to make the extra empty lines look "normal", we have to
 15358      use the tool-bar face for the border too.  */
 15359   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15360       && !EQ (Vauto_resize_tool_bars, Qgrow_only))
 15361     it->face_id = DEFAULT_FACE_ID;
 15362 
 15363   extend_face_to_end_of_line (it);
 15364   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 15365   last->right_box_line_p = true;
 15366   if (last == row->glyphs[TEXT_AREA])
 15367     last->left_box_line_p = true;
 15368 
 15369   /* Make line the desired height and center it vertically.  */
 15370   if (height != -1
 15371       && (height -= it->max_ascent + it->max_descent) > 0)
 15372     {
 15373       /* Don't add more than one line height.  */
 15374       height %= FRAME_LINE_HEIGHT (it->f);
 15375       it->max_ascent += height / 2;
 15376       it->max_descent += (height + 1) / 2;
 15377     }
 15378 
 15379   compute_line_metrics (it);
 15380 
 15381   /* If line is empty, make it occupy the rest of the tool-bar.  */
 15382   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 15383     {
 15384       row->height = row->phys_height = it->last_visible_y - row->y;
 15385       row->visible_height = row->height;
 15386       row->ascent = row->phys_ascent = 0;
 15387       row->extra_line_spacing = 0;
 15388     }
 15389 
 15390   row->full_width_p = true;
 15391   row->continued_p = false;
 15392   row->truncated_on_left_p = false;
 15393   row->truncated_on_right_p = false;
 15394 
 15395   it->current_x = it->hpos = 0;
 15396   it->current_y += row->height;
 15397   ++it->vpos;
 15398   ++it->glyph_row;
 15399 }
 15400 
 15401 
 15402 /* Value is the number of pixels needed to make all tool-bar items of
 15403    frame F visible.  The actual number of glyph rows needed is
 15404    returned in *N_ROWS if non-NULL.  */
 15405 
 15406 static int
 15407 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 15408 {
 15409   struct window *w = XWINDOW (f->tool_bar_window);
 15410   struct it it;
 15411   /* tool_bar_height is called from redisplay_tool_bar after building
 15412      the desired matrix, so use (unused) mode-line row as temporary row to
 15413      avoid destroying the first tool-bar row.  */
 15414   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 15415 
 15416   /* Initialize an iterator for iteration over
 15417      F->desired_tool_bar_string in the tool-bar window of frame F.  */
 15418   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
 15419   temp_row->reversed_p = false;
 15420   it.first_visible_x = 0;
 15421   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15422   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15423                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15424   it.paragraph_embedding = L2R;
 15425 
 15426   while (!ITERATOR_AT_END_P (&it))
 15427     {
 15428       clear_glyph_row (temp_row);
 15429       it.glyph_row = temp_row;
 15430       display_tool_bar_line (&it, -1);
 15431     }
 15432   clear_glyph_row (temp_row);
 15433 
 15434   /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar.  */
 15435   if (n_rows)
 15436     *n_rows = it.vpos > 0 ? it.vpos : -1;
 15437 
 15438   if (pixelwise)
 15439     return it.current_y;
 15440   else
 15441     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 15442 }
 15443 
 15444 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15445 
 15446 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
 15447        0, 2, 0,
 15448        doc: /* Return the number of lines occupied by the tool bar of FRAME.
 15449 If FRAME is nil or omitted, use the selected frame.  Optional argument
 15450 PIXELWISE non-nil means return the height of the tool bar in pixels.  */)
 15451   (Lisp_Object frame, Lisp_Object pixelwise)
 15452 {
 15453   int height = 0;
 15454 
 15455 #ifndef HAVE_EXT_TOOL_BAR
 15456   struct frame *f = decode_any_frame (frame);
 15457 
 15458   if (WINDOWP (f->tool_bar_window)
 15459       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
 15460     {
 15461       update_tool_bar (f, true);
 15462       if (f->n_tool_bar_items)
 15463         {
 15464           build_desired_tool_bar_string (f);
 15465           height = tool_bar_height (f, NULL, !NILP (pixelwise));
 15466         }
 15467     }
 15468 #endif
 15469 
 15470   return make_fixnum (height);
 15471 }
 15472 
 15473 #ifndef HAVE_EXT_TOOL_BAR
 15474 
 15475 /* Display the internal tool-bar of frame F.  Value is true if
 15476    tool-bar's height should be changed.  */
 15477 static bool
 15478 redisplay_tool_bar (struct frame *f)
 15479 {
 15480   struct window *w;
 15481   struct it it;
 15482   struct glyph_row *row;
 15483   bool change_height_p;
 15484 
 15485   change_height_p = false;
 15486   f->tool_bar_redisplayed = true;
 15487 
 15488   /* If frame hasn't a tool-bar window or if it is zero-height, don't
 15489      do anything.  This means you must start with tool-bar-lines
 15490      non-zero to get the auto-sizing effect.  Or in other words, you
 15491      can turn off tool-bars by specifying tool-bar-lines zero.  */
 15492   if (!WINDOWP (f->tool_bar_window)
 15493       || (w = XWINDOW (f->tool_bar_window),
 15494           WINDOW_TOTAL_LINES (w) == 0))
 15495     {
 15496       /* Even if we do not display a tool bar initially, still pretend
 15497          that we have resized it already.  This avoids that a later
 15498          activation of the tool bar resizes the frame, despite of the
 15499          fact that a setting of 'frame-inhibit-implied-resize' should
 15500          inhibit it (Bug#52986).  */
 15501       f->tool_bar_resized = true;
 15502 
 15503       return false;
 15504     }
 15505 
 15506   /* Set up an iterator for the tool-bar window.  */
 15507   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
 15508   it.first_visible_x = 0;
 15509   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15510   row = it.glyph_row;
 15511   row->reversed_p = false;
 15512 
 15513   /* Build a string that represents the contents of the tool-bar.  */
 15514   build_desired_tool_bar_string (f);
 15515   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15516                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15517   /* FIXME: This should be controlled by a user option.  But it
 15518      doesn't make sense to have an R2L tool bar if the menu bar cannot
 15519      be drawn also R2L, and making the menu bar R2L is tricky due to
 15520      toolkit-specific code that implements it.  If an R2L tool bar is
 15521      ever supported, display_tool_bar_line should also be augmented to
 15522      call unproduce_glyphs like display_line and display_string
 15523      do.  */
 15524   it.paragraph_embedding = L2R;
 15525 
 15526   if (f->n_tool_bar_rows == 0)
 15527     {
 15528       int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
 15529 
 15530       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 15531         {
 15532           if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15533             FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15534           frame_default_tool_bar_height = new_height;
 15535           /* Always do that now.  */
 15536           clear_glyph_matrix (w->desired_matrix);
 15537           f->fonts_changed = true;
 15538 
 15539           /* Kludge (this applies to the X Windows version as well as
 15540              Android): when the tool bar size changes,
 15541              adjust_window_size (presumably called by
 15542              change_tool_bar_height_hook) does not call through to
 15543              resize_frame_windows.  Pending further investigation,
 15544              just call it here as well.  */
 15545           resize_frame_windows (f, FRAME_INNER_HEIGHT (f), false);
 15546 
 15547           return true;
 15548         }
 15549     }
 15550 
 15551   /* Display as many lines as needed to display all tool-bar items.  */
 15552 
 15553   if (f->n_tool_bar_rows > 0)
 15554     {
 15555       int border, rows, height, extra;
 15556 
 15557       if (TYPE_RANGED_FIXNUMP (int, Vtool_bar_border))
 15558         border = XFIXNUM (Vtool_bar_border);
 15559       else if (EQ (Vtool_bar_border, Qinternal_border_width))
 15560         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 15561       else if (EQ (Vtool_bar_border, Qborder_width))
 15562         border = f->border_width;
 15563       else
 15564         border = 0;
 15565       if (border < 0)
 15566         border = 0;
 15567 
 15568       rows = f->n_tool_bar_rows;
 15569 
 15570       if (f->tool_bar_wraps_p)
 15571         {
 15572           /* If the tool bar contains explicit line wrapping items,
 15573              don't force each row to have a fixed height.  */
 15574 
 15575           while (!ITERATOR_AT_END_P (&it))
 15576             display_tool_bar_line (&it, -1);
 15577 
 15578           /* Because changes to individual tool bar items may now
 15579              change the height of the tool bar, adjust the height of
 15580              the tool bar window if it is different from the tool bar
 15581              height in any way.  */
 15582 
 15583           if (it.current_y != it.last_visible_y)
 15584             change_height_p = true;
 15585         }
 15586       else
 15587         {
 15588           height = max (1, (it.last_visible_y - border) / rows);
 15589           extra = it.last_visible_y - border - height * rows;
 15590 
 15591           while (it.current_y < it.last_visible_y)
 15592             {
 15593               int h = 0;
 15594               if (extra > 0 && rows-- > 0)
 15595                 {
 15596                   h = (extra + rows - 1) / rows;
 15597                   extra -= h;
 15598                 }
 15599 
 15600               display_tool_bar_line (&it, height + h);
 15601             }
 15602         }
 15603     }
 15604   else
 15605     {
 15606       while (it.current_y < it.last_visible_y)
 15607         display_tool_bar_line (&it, 0);
 15608     }
 15609 
 15610   /* It doesn't make much sense to try scrolling in the tool-bar
 15611      window, so don't do it.  */
 15612   w->desired_matrix->no_scrolling_p = true;
 15613   w->must_be_updated_p = true;
 15614 
 15615   if (!NILP (Vauto_resize_tool_bars))
 15616     {
 15617       /* If we couldn't display everything, change the tool-bar's
 15618          height if there is room for more.  */
 15619       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 15620         change_height_p = true;
 15621 
 15622       /* We subtract 1 because display_tool_bar_line advances the
 15623          glyph_row pointer before returning to its caller.  We want to
 15624          examine the last glyph row produced by
 15625          display_tool_bar_line.  */
 15626       row = it.glyph_row - 1;
 15627 
 15628       /* If there are blank lines at the end, except for a partially
 15629          visible blank line at the end that is smaller than
 15630          FRAME_LINE_HEIGHT, change the tool-bar's height.  */
 15631       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15632           && row->height >= FRAME_LINE_HEIGHT (f))
 15633         change_height_p = true;
 15634 
 15635       /* If row displays tool-bar items, but is partially visible,
 15636          change the tool-bar's height.  */
 15637       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15638           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 15639         change_height_p = true;
 15640 
 15641       /* Resize windows as needed by changing the `tool-bar-lines'
 15642          frame parameter.  */
 15643       if (change_height_p)
 15644         {
 15645           int nrows;
 15646           int new_height = tool_bar_height (f, &nrows, true);
 15647 
 15648           change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
 15649                               && !f->minimize_tool_bar_window_p)
 15650                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 15651                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 15652           f->minimize_tool_bar_window_p = false;
 15653 
 15654           if (change_height_p)
 15655             {
 15656               if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15657                 FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15658               frame_default_tool_bar_height = new_height;
 15659               clear_glyph_matrix (w->desired_matrix);
 15660               f->n_tool_bar_rows = nrows;
 15661               f->fonts_changed = true;
 15662 
 15663               return true;
 15664             }
 15665         }
 15666     }
 15667 
 15668   f->minimize_tool_bar_window_p = false;
 15669 
 15670   return false;
 15671 }
 15672 
 15673 /* Get information about the tool-bar item which is displayed in GLYPH
 15674    on frame F.  Return in *PROP_IDX the index where tool-bar item
 15675    properties start in F->tool_bar_items.  Value is false if
 15676    GLYPH doesn't display a tool-bar item.  */
 15677 
 15678 static bool
 15679 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
 15680 {
 15681   Lisp_Object prop;
 15682   ptrdiff_t charpos;
 15683 
 15684   /* This function can be called asynchronously, which means we must
 15685      exclude any possibility that Fget_text_property signals an
 15686      error.  */
 15687   charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
 15688   charpos = max (0, charpos);
 15689 
 15690   /* Get the text property `menu-item' at pos. The value of that
 15691      property is the start index of this item's properties in
 15692      F->tool_bar_items.  */
 15693   prop = Fget_text_property (make_fixnum (charpos),
 15694                              Qmenu_item, f->current_tool_bar_string);
 15695   if (! FIXNUMP (prop))
 15696     return false;
 15697   *prop_idx = XFIXNUM (prop);
 15698   return true;
 15699 }
 15700 
 15701 
 15702 /* Get information about the tool-bar item at position X/Y on frame F.
 15703    Return in *GLYPH a pointer to the glyph of the tool-bar item in
 15704    the current matrix of the tool-bar window of F, or NULL if not
 15705    on a tool-bar item.  Return in *PROP_IDX the index of the tool-bar
 15706    item in F->tool_bar_items.  Value is
 15707 
 15708    -1   if X/Y is not on a tool-bar item
 15709    0    if X/Y is on the same item that was highlighted before.
 15710    1    otherwise.  */
 15711 
 15712 static int
 15713 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 15714                    int *hpos, int *vpos, int *prop_idx)
 15715 {
 15716   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15717   struct window *w = XWINDOW (f->tool_bar_window);
 15718   int area;
 15719 
 15720   /* Find the glyph under X/Y.  */
 15721   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 15722   if (*glyph == NULL)
 15723     return -1;
 15724 
 15725   /* Get the start of this tool-bar item's properties in
 15726      f->tool_bar_items.  */
 15727   if (!tool_bar_item_info (f, *glyph, prop_idx))
 15728     return -1;
 15729 
 15730   /* Is mouse on the highlighted item?  */
 15731   if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
 15732       && *vpos >= hlinfo->mouse_face_beg_row
 15733       && *vpos <= hlinfo->mouse_face_end_row
 15734       && (*vpos > hlinfo->mouse_face_beg_row
 15735           || *hpos >= hlinfo->mouse_face_beg_col)
 15736       && (*vpos < hlinfo->mouse_face_end_row
 15737           || *hpos < hlinfo->mouse_face_end_col
 15738           || hlinfo->mouse_face_past_end))
 15739     return 0;
 15740 
 15741   return 1;
 15742 }
 15743 
 15744 
 15745 /* EXPORT:
 15746    Handle mouse button event on the tool-bar of frame F, at
 15747    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 15748    false for button release.  MODIFIERS is event modifiers for button
 15749    release.  DEVICE is the device the click came from, or Qt.  */
 15750 
 15751 void
 15752 handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p,
 15753                                    int modifiers, Lisp_Object device)
 15754 {
 15755   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15756   struct window *w = XWINDOW (f->tool_bar_window);
 15757   int hpos, vpos, prop_idx;
 15758   struct glyph *glyph;
 15759   Lisp_Object enabled_p;
 15760   int ts;
 15761 
 15762   /* If not on the highlighted tool-bar item, and mouse-highlight is
 15763      non-nil, return.  This is so we generate the tool-bar button
 15764      click only when the mouse button is released on the same item as
 15765      where it was pressed.  However, when mouse-highlight is disabled,
 15766      generate the click when the button is released regardless of the
 15767      highlight, since tool-bar items are not highlighted in that
 15768      case.  */
 15769   frame_to_window_pixel_xy (w, &x, &y);
 15770   ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15771   if (ts == -1
 15772       || (ts != 0 && !NILP (Vmouse_highlight)))
 15773     return;
 15774 
 15775   /* When mouse-highlight is off, generate the click for the item
 15776      where the button was pressed, disregarding where it was
 15777      released.  */
 15778   if (NILP (Vmouse_highlight) && !down_p)
 15779     prop_idx = f->last_tool_bar_item;
 15780 
 15781   /* If item is disabled, do nothing.  */
 15782   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15783   if (NILP (enabled_p))
 15784     return;
 15785 
 15786   if (down_p)
 15787     {
 15788       /* Show item in pressed state.  */
 15789       if (!NILP (Vmouse_highlight))
 15790         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 15791       f->last_tool_bar_item = prop_idx;
 15792     }
 15793   else
 15794     {
 15795       Lisp_Object key, frame;
 15796       struct input_event event;
 15797       EVENT_INIT (event);
 15798 
 15799       /* Show item in released state.  */
 15800       if (!NILP (Vmouse_highlight))
 15801         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 15802 
 15803       key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
 15804 
 15805       XSETFRAME (frame, f);
 15806       event.kind = TOOL_BAR_EVENT;
 15807       event.frame_or_window = frame;
 15808       event.arg = key;
 15809       event.modifiers = modifiers;
 15810       event.device = device;
 15811       kbd_buffer_store_event (&event);
 15812       f->last_tool_bar_item = -1;
 15813     }
 15814 }
 15815 
 15816 void
 15817 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
 15818                        int modifiers)
 15819 {
 15820   handle_tool_bar_click_with_device (f, x, y, down_p, modifiers, Qt);
 15821 }
 15822 
 15823 /* Possibly highlight a tool-bar item on frame F when mouse moves to
 15824    tool-bar window-relative coordinates X/Y.  Called from
 15825    note_mouse_highlight.  */
 15826 
 15827 static void
 15828 note_tool_bar_highlight (struct frame *f, int x, int y)
 15829 {
 15830   Lisp_Object window = f->tool_bar_window;
 15831   struct window *w = XWINDOW (window);
 15832   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 15833   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15834   int hpos, vpos;
 15835   struct glyph *glyph;
 15836   struct glyph_row *row;
 15837   int i;
 15838   Lisp_Object enabled_p;
 15839   int prop_idx;
 15840   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 15841   bool mouse_down_p;
 15842   int rc;
 15843 
 15844   /* Function note_mouse_highlight is called with negative X/Y
 15845      values when mouse moves outside of the frame.  */
 15846   if (x <= 0 || y <= 0)
 15847     {
 15848       clear_mouse_face (hlinfo);
 15849       return;
 15850     }
 15851 
 15852   rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15853   if (rc < 0)
 15854     {
 15855       /* Not on tool-bar item.  */
 15856       clear_mouse_face (hlinfo);
 15857       return;
 15858     }
 15859   else if (rc == 0)
 15860     /* On same tool-bar item as before.  */
 15861     goto set_help_echo;
 15862 
 15863   clear_mouse_face (hlinfo);
 15864 
 15865   /* Mouse is down, but on different tool-bar item?  */
 15866   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 15867                   && f == dpyinfo->last_mouse_frame);
 15868 
 15869   if (mouse_down_p && f->last_tool_bar_item != prop_idx)
 15870     return;
 15871 
 15872   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 15873 
 15874   /* If tool-bar item is not enabled, don't highlight it.  */
 15875   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15876   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 15877     {
 15878       /* Compute the x-position of the glyph.  In front and past the
 15879          image is a space.  We include this in the highlighted area.  */
 15880       row = MATRIX_ROW (w->current_matrix, vpos);
 15881       for (i = x = 0; i < hpos; ++i)
 15882         x += row->glyphs[TEXT_AREA][i].pixel_width;
 15883 
 15884       /* Record this as the current active region.  */
 15885       hlinfo->mouse_face_beg_col = hpos;
 15886       hlinfo->mouse_face_beg_row = vpos;
 15887       hlinfo->mouse_face_beg_x = x;
 15888       hlinfo->mouse_face_past_end = false;
 15889 
 15890       hlinfo->mouse_face_end_col = hpos + 1;
 15891       hlinfo->mouse_face_end_row = vpos;
 15892       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 15893       hlinfo->mouse_face_window = window;
 15894       hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
 15895 
 15896       /* Display it as active.  */
 15897       show_mouse_face (hlinfo, draw);
 15898     }
 15899 
 15900  set_help_echo:
 15901 
 15902   /* Set help_echo_string to a help string to display for this tool-bar item.
 15903      XTread_socket does the rest.  */
 15904   help_echo_object = help_echo_window = Qnil;
 15905   help_echo_pos = -1;
 15906   help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
 15907   if (NILP (help_echo_string))
 15908     help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
 15909 }
 15910 
 15911 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15912 
 15913 #endif /* HAVE_WINDOW_SYSTEM */
 15914 
 15915 
 15916 
 15917 /************************************************************************
 15918                          Horizontal scrolling
 15919  ************************************************************************/
 15920 
 15921 /* For all leaf windows in the window tree rooted at WINDOW, set their
 15922    hscroll value so that PT is (i) visible in the window, and (ii) so
 15923    that it is not within a certain margin at the window's left and
 15924    right border.  Value is true if any window's hscroll has been
 15925    changed.  */
 15926 
 15927 static bool
 15928 hscroll_window_tree (Lisp_Object window)
 15929 {
 15930   bool hscrolled_p = false;
 15931   bool hscroll_relative_p = FLOATP (Vhscroll_step);
 15932   int hscroll_step_abs = 0;
 15933   double hscroll_step_rel = 0;
 15934 
 15935   if (hscroll_relative_p)
 15936     {
 15937       hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
 15938       if (hscroll_step_rel < 0)
 15939         {
 15940           hscroll_relative_p = false;
 15941           hscroll_step_abs = 0;
 15942         }
 15943     }
 15944   else if (TYPE_RANGED_FIXNUMP (int, Vhscroll_step))
 15945     {
 15946       hscroll_step_abs = XFIXNUM (Vhscroll_step);
 15947       if (hscroll_step_abs < 0)
 15948         hscroll_step_abs = 0;
 15949     }
 15950   else
 15951     hscroll_step_abs = 0;
 15952 
 15953   while (WINDOWP (window))
 15954     {
 15955       struct window *w = XWINDOW (window);
 15956 
 15957       if (WINDOWP (w->contents))
 15958         hscrolled_p |= hscroll_window_tree (w->contents);
 15959       else if (w->cursor.vpos >= 0
 15960                /* Don't allow hscroll in mini-windows that display
 15961                   echo-area messages.  This is because desired_matrix
 15962                   of such windows was prepared while momentarily
 15963                   switched to an echo-area buffer, which is different
 15964                   from w->contents, and we simply cannot hscroll such
 15965                   windows safely.  */
 15966                && !(w == XWINDOW (echo_area_window)
 15967                     && !NILP (echo_area_buffer[0])))
 15968         {
 15969           int h_margin;
 15970           int text_area_width;
 15971           struct glyph_row *cursor_row;
 15972           struct glyph_row *bottom_row;
 15973 
 15974           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
 15975           if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
 15976             cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
 15977           else
 15978             cursor_row = bottom_row - 1;
 15979 
 15980           if (!cursor_row->enabled_p)
 15981             {
 15982               bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 15983               if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
 15984                 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 15985               else
 15986                 cursor_row = bottom_row - 1;
 15987             }
 15988           bool row_r2l_p = cursor_row->reversed_p;
 15989           bool hscl = hscrolling_current_line_p (w);
 15990           int x_offset = 0;
 15991           /* When line numbers are displayed, we need to account for
 15992              the horizontal space they consume.  */
 15993           if (!NILP (Vdisplay_line_numbers))
 15994             {
 15995               struct glyph *g;
 15996               if (!row_r2l_p)
 15997                 {
 15998                   for (g = cursor_row->glyphs[TEXT_AREA];
 15999                        g < cursor_row->glyphs[TEXT_AREA]
 16000                          + cursor_row->used[TEXT_AREA];
 16001                        g++)
 16002                     {
 16003                       if (!(NILP (g->object) && g->charpos < 0))
 16004                         break;
 16005                       x_offset += g->pixel_width;
 16006                     }
 16007                 }
 16008               else
 16009                 {
 16010                   for (g = cursor_row->glyphs[TEXT_AREA]
 16011                          + cursor_row->used[TEXT_AREA];
 16012                        g > cursor_row->glyphs[TEXT_AREA];
 16013                        g--)
 16014                     {
 16015                       if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
 16016                         break;
 16017                       x_offset += (g - 1)->pixel_width;
 16018                     }
 16019                 }
 16020             }
 16021           if (cursor_row->truncated_on_left_p)
 16022             {
 16023               /* On TTY frames, don't count the left truncation glyph.  */
 16024               struct frame *f = XFRAME (WINDOW_FRAME (w));
 16025               x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
 16026             }
 16027 
 16028           text_area_width = window_box_width (w, TEXT_AREA);
 16029 
 16030           /* Scroll when cursor is inside this scroll margin.  */
 16031           h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
 16032                       * WINDOW_FRAME_COLUMN_WIDTH (w));
 16033 
 16034           /* If the position of this window's point has explicitly
 16035              changed, no more suspend auto hscrolling.  */
 16036           if (w->suspend_auto_hscroll
 16037               && NILP (Fequal (Fwindow_point (window),
 16038                                Fwindow_old_point (window))))
 16039             {
 16040               w->suspend_auto_hscroll = false;
 16041               /* When hscrolling just the current line, and the rest
 16042                  of lines were temporarily hscrolled, but no longer
 16043                  are, force thorough redisplay of this window, to show
 16044                  the effect of disabling hscroll suspension immediately.  */
 16045               if (w->min_hscroll == 0 && w->hscroll > 0
 16046                   && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
 16047                          Qcurrent_line))
 16048                 SET_FRAME_GARBAGED (XFRAME (w->frame));
 16049             }
 16050 
 16051           /* Remember window point.  */
 16052           Fset_marker (w->old_pointm,
 16053                        ((w == XWINDOW (selected_window))
 16054                         ? make_fixnum (BUF_PT (XBUFFER (w->contents)))
 16055                         : Fmarker_position (w->pointm)),
 16056                        w->contents);
 16057 
 16058           if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
 16059               && !w->suspend_auto_hscroll
 16060               /* In some pathological cases, like restoring a window
 16061                  configuration into a frame that is much smaller than
 16062                  the one from which the configuration was saved, we
 16063                  get glyph rows whose start and end have zero buffer
 16064                  positions, which we cannot handle below.  Just skip
 16065                  such windows.  */
 16066               && (CHARPOS (cursor_row->start.pos)
 16067                   >= BUF_BEG (XBUFFER (w->contents)))
 16068               /* For left-to-right rows, hscroll when cursor is either
 16069                  (i) inside the right hscroll margin, or (ii) if it is
 16070                  inside the left margin and the window is already
 16071                  hscrolled.  */
 16072               && ((!row_r2l_p
 16073                    && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
 16074                        || (cursor_row->enabled_p
 16075                            && cursor_row->truncated_on_right_p
 16076                            && (w->cursor.x >= text_area_width - h_margin))))
 16077                   /* For right-to-left rows, the logic is similar,
 16078                      except that rules for scrolling to left and right
 16079                      are reversed.  E.g., if cursor.x <= h_margin, we
 16080                      need to hscroll "to the right" unconditionally,
 16081                      and that will scroll the screen to the left so as
 16082                      to reveal the next portion of the row.  */
 16083                   || (row_r2l_p
 16084                       && ((cursor_row->enabled_p
 16085                            /* FIXME: It is confusing to set the
 16086                               truncated_on_right_p flag when R2L rows
 16087                               are actually truncated on the left.  */
 16088                            && cursor_row->truncated_on_right_p
 16089                            && w->cursor.x <= h_margin)
 16090                           || (w->hscroll
 16091                               && (w->cursor.x >= (text_area_width - h_margin
 16092                                                   - x_offset)))))
 16093                   /* This last condition is needed when moving
 16094                      vertically from an hscrolled line to a short line
 16095                      that doesn't need to be hscrolled.  If we omit
 16096                      this condition, the line from which we move will
 16097                      remain hscrolled.  */
 16098                   || (hscl
 16099                       && w->hscroll != w->min_hscroll
 16100                       && !cursor_row->truncated_on_left_p)))
 16101             {
 16102               struct it it;
 16103               ptrdiff_t hscroll;
 16104               struct buffer *saved_current_buffer;
 16105               ptrdiff_t pt;
 16106               int wanted_x;
 16107 
 16108               /* Find point in a display of infinite width.  */
 16109               saved_current_buffer = current_buffer;
 16110               current_buffer = XBUFFER (w->contents);
 16111 
 16112               if (w == XWINDOW (selected_window))
 16113                 pt = PT;
 16114               else
 16115                 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
 16116 
 16117               /* Move iterator to pt starting at cursor_row->start in
 16118                  a line with infinite width.  */
 16119               init_to_row_start (&it, w, cursor_row);
 16120               if (hscl)
 16121                 it.first_visible_x = window_hscroll_limited (w, it.f)
 16122                                      * FRAME_COLUMN_WIDTH (it.f);
 16123               it.last_visible_x = DISP_INFINITY;
 16124 
 16125               ptrdiff_t nchars = pt - IT_CHARPOS (it);
 16126               if (current_buffer->long_line_optimizations_p
 16127                   && nchars > large_hscroll_threshold)
 16128                 {
 16129                   /* Special optimization for very long and truncated
 16130                      lines which need to be hscrolled far to the left:
 16131                      jump directly to the (approximate) first position
 16132                      that is visible, instead of slowly walking there.  */
 16133                   fast_move_it_horizontally (&it, nchars);
 16134                   it.current_x += nchars * FRAME_COLUMN_WIDTH (it.f);
 16135                 }
 16136               else
 16137                 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
 16138               /* If the line ends in an overlay string with a newline,
 16139                  we might infloop, because displaying the window will
 16140                  want to put the cursor after the overlay, i.e. at X
 16141                  coordinate of zero on the next screen line.  So we
 16142                  use the buffer position prior to the overlay string
 16143                  instead.  */
 16144               if (it.method == GET_FROM_STRING && pt > 1)
 16145                 {
 16146                   init_to_row_start (&it, w, cursor_row);
 16147                   if (hscl)
 16148                     it.first_visible_x = (window_hscroll_limited (w, it.f)
 16149                                           * FRAME_COLUMN_WIDTH (it.f));
 16150                   if (current_buffer->long_line_optimizations_p
 16151                       && nchars > large_hscroll_threshold)
 16152                     {
 16153                       fast_move_it_horizontally (&it, nchars - 1);
 16154                       it.current_x += (nchars - 1) * FRAME_COLUMN_WIDTH (it.f);
 16155                     }
 16156                   else
 16157                     move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
 16158                 }
 16159               current_buffer = saved_current_buffer;
 16160 
 16161               /* Position cursor in window.  */
 16162               if (!hscroll_relative_p && hscroll_step_abs == 0)
 16163                 hscroll = max (0, (it.current_x
 16164                                    - (ITERATOR_AT_END_OF_LINE_P (&it)
 16165                                       ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
 16166                                       : (text_area_width / 2))))
 16167                           / FRAME_COLUMN_WIDTH (it.f);
 16168               else if ((!row_r2l_p
 16169                         && w->cursor.x >= text_area_width - h_margin)
 16170                        || (row_r2l_p && w->cursor.x <= h_margin))
 16171                 {
 16172                   if (hscroll_relative_p)
 16173                     wanted_x = text_area_width * (1 - hscroll_step_rel)
 16174                                - h_margin;
 16175                   else
 16176                     wanted_x = text_area_width
 16177                                - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16178                                - h_margin;
 16179                   hscroll
 16180                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16181                 }
 16182               else
 16183                 {
 16184                   if (hscroll_relative_p)
 16185                     wanted_x =
 16186                       text_area_width * hscroll_step_rel + h_margin + x_offset;
 16187                   else
 16188                     wanted_x =
 16189                       hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16190                       + h_margin + x_offset;
 16191                   hscroll
 16192                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16193                 }
 16194               hscroll = max (hscroll, w->min_hscroll);
 16195 
 16196               /* Don't prevent redisplay optimizations if hscroll
 16197                  hasn't changed, as it will unnecessarily slow down
 16198                  redisplay.  */
 16199               if (w->hscroll != hscroll
 16200                   /* When hscrolling only the current line, we need to
 16201                      report hscroll even if its value is equal to the
 16202                      previous one, because the new line might need a
 16203                      different value.  */
 16204                   || (hscl && w->last_cursor_vpos != w->cursor.vpos))
 16205                 {
 16206                   struct buffer *b = XBUFFER (w->contents);
 16207                   b->prevent_redisplay_optimizations_p = true;
 16208                   w->hscroll = hscroll;
 16209                   hscrolled_p = true;
 16210                 }
 16211             }
 16212         }
 16213 
 16214       window = w->next;
 16215     }
 16216 
 16217   /* Value is true if hscroll of any leaf window has been changed.  */
 16218   return hscrolled_p;
 16219 }
 16220 
 16221 
 16222 /* Set hscroll so that cursor is visible and not inside horizontal
 16223    scroll margins for all windows in the tree rooted at WINDOW.  See
 16224    also hscroll_window_tree above.  Value is true if any window's
 16225    hscroll has been changed.  If it has, desired matrices on the frame
 16226    of WINDOW are cleared.  */
 16227 
 16228 static bool
 16229 hscroll_windows (Lisp_Object window)
 16230 {
 16231   bool hscrolled_p = hscroll_window_tree (window);
 16232   if (hscrolled_p)
 16233     clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
 16234   return hscrolled_p;
 16235 }
 16236 
 16237 
 16238 
 16239 /************************************************************************
 16240                                 Redisplay
 16241  ************************************************************************/
 16242 
 16243 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
 16244    This is sometimes handy to have in a debugger session.  */
 16245 
 16246 #ifdef GLYPH_DEBUG
 16247 
 16248 /* First and last unchanged row for try_window_id.  */
 16249 
 16250 static int debug_first_unchanged_at_end_vpos;
 16251 static int debug_last_unchanged_at_beg_vpos;
 16252 
 16253 /* Delta vpos and y.  */
 16254 
 16255 static int debug_dvpos, debug_dy;
 16256 
 16257 /* Delta in characters and bytes for try_window_id.  */
 16258 
 16259 static ptrdiff_t debug_delta, debug_delta_bytes;
 16260 
 16261 /* Values of window_end_pos and window_end_vpos at the end of
 16262    try_window_id.  */
 16263 
 16264 static ptrdiff_t debug_end_vpos;
 16265 
 16266 /* Append a string to W->desired_matrix->method.  FMT is a printf
 16267    format string.  If trace_redisplay_p is true also printf the
 16268    resulting string to stderr.  */
 16269 
 16270 static void debug_method_add (struct window *, char const *, ...)
 16271   ATTRIBUTE_FORMAT_PRINTF (2, 3);
 16272 
 16273 static void
 16274 debug_method_add (struct window *w, char const *fmt, ...)
 16275 {
 16276   void *ptr = w;
 16277   char *method = w->desired_matrix->method;
 16278   int len = strlen (method);
 16279   int size = sizeof w->desired_matrix->method;
 16280   int remaining = size - len - 1;
 16281   va_list ap;
 16282 
 16283   if (len && remaining)
 16284     {
 16285       method[len] = '|';
 16286       --remaining, ++len;
 16287     }
 16288 
 16289   va_start (ap, fmt);
 16290   vsnprintf (method + len, remaining + 1, fmt, ap);
 16291   va_end (ap);
 16292 
 16293   if (trace_redisplay_p)
 16294     fprintf (stderr, "%p (%s): %s\n",
 16295              ptr,
 16296              ((BUFFERP (w->contents)
 16297                && STRINGP (BVAR (XBUFFER (w->contents), name)))
 16298               ? SSDATA (BVAR (XBUFFER (w->contents), name))
 16299               : "no buffer"),
 16300              method + len);
 16301 }
 16302 
 16303 #endif /* GLYPH_DEBUG */
 16304 
 16305 
 16306 /* Value is true if all changes in window W, which displays
 16307    current_buffer, are in the text between START and END.  START is a
 16308    buffer position, END is given as a distance from Z.  Used in
 16309    redisplay_internal for display optimization.  */
 16310 
 16311 static bool
 16312 text_outside_line_unchanged_p (struct window *w,
 16313                                ptrdiff_t start, ptrdiff_t end)
 16314 {
 16315   bool unchanged_p = true;
 16316 
 16317   /* If text or overlays have changed, see where.  */
 16318   if (window_outdated (w))
 16319     {
 16320       /* Gap in the line?  */
 16321       if (GPT < start || Z - GPT < end)
 16322         unchanged_p = false;
 16323 
 16324       /* Changes start in front of the line, or end after it?  */
 16325       if (unchanged_p
 16326           && (BEG_UNCHANGED < start - 1
 16327               || END_UNCHANGED < end))
 16328         unchanged_p = false;
 16329 
 16330       /* If selective display, can't optimize if changes start at the
 16331          beginning of the line.  */
 16332       if (unchanged_p
 16333           && FIXNUMP (BVAR (current_buffer, selective_display))
 16334           && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
 16335           && (BEG_UNCHANGED < start || GPT <= start))
 16336         unchanged_p = false;
 16337 
 16338       /* If there are overlays at the start or end of the line, these
 16339          may have overlay strings with newlines in them.  A change at
 16340          START, for instance, may actually concern the display of such
 16341          overlay strings as well, and they are displayed on different
 16342          lines.  So, quickly rule out this case.  (For the future, it
 16343          might be desirable to implement something more telling than
 16344          just BEG/END_UNCHANGED.)  */
 16345       if (unchanged_p)
 16346         {
 16347           if (BEG + BEG_UNCHANGED == start
 16348               && overlay_touches_p (start))
 16349             unchanged_p = false;
 16350           if (END_UNCHANGED == end
 16351               && overlay_touches_p (Z - end))
 16352             unchanged_p = false;
 16353         }
 16354 
 16355       /* Under bidi reordering, adding or deleting a character in the
 16356          beginning of a paragraph, before the first strong directional
 16357          character, can change the base direction of the paragraph (unless
 16358          the buffer specifies a fixed paragraph direction), which will
 16359          require redisplaying the whole paragraph.  It might be worthwhile
 16360          to find the paragraph limits and widen the range of redisplayed
 16361          lines to that, but for now just give up this optimization.  */
 16362       if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 16363           && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 16364         unchanged_p = false;
 16365     }
 16366 
 16367   return unchanged_p;
 16368 }
 16369 
 16370 
 16371 /* Do a frame update, taking possible shortcuts into account.  This is
 16372    the main external entry point for redisplay.
 16373 
 16374    If the last redisplay displayed an echo area message and that message
 16375    is no longer requested, we clear the echo area or bring back the
 16376    mini-buffer if that is in use.  */
 16377 
 16378 void
 16379 redisplay (void)
 16380 {
 16381   redisplay_internal ();
 16382 }
 16383 
 16384 
 16385 static Lisp_Object
 16386 overlay_arrow_string_or_property (Lisp_Object var)
 16387 {
 16388   Lisp_Object val;
 16389 
 16390   if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
 16391     return val;
 16392 
 16393   return Voverlay_arrow_string;
 16394 }
 16395 
 16396 /* Return true if there are any overlay-arrows in current_buffer.  */
 16397 static bool
 16398 overlay_arrow_in_current_buffer_p (void)
 16399 {
 16400   Lisp_Object vlist;
 16401 
 16402   for (vlist = Voverlay_arrow_variable_list;
 16403        CONSP (vlist);
 16404        vlist = XCDR (vlist))
 16405     {
 16406       Lisp_Object var = XCAR (vlist);
 16407       Lisp_Object val;
 16408 
 16409       if (!SYMBOLP (var))
 16410         continue;
 16411       val = find_symbol_value (var);
 16412       if (MARKERP (val)
 16413           && current_buffer == XMARKER (val)->buffer)
 16414         return true;
 16415     }
 16416   return false;
 16417 }
 16418 
 16419 
 16420 /* Return true if any overlay_arrows have moved or overlay-arrow-string
 16421    has changed.
 16422    If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
 16423    buffers that are affected.  */
 16424 
 16425 static bool
 16426 overlay_arrows_changed_p (bool set_redisplay)
 16427 {
 16428   Lisp_Object vlist;
 16429   bool changed = false;
 16430 
 16431   for (vlist = Voverlay_arrow_variable_list;
 16432        CONSP (vlist);
 16433        vlist = XCDR (vlist))
 16434     {
 16435       Lisp_Object var = XCAR (vlist);
 16436       Lisp_Object val, pstr;
 16437 
 16438       if (!SYMBOLP (var))
 16439         continue;
 16440       val = find_symbol_value (var);
 16441       if (!MARKERP (val))
 16442         continue;
 16443       if (! EQ (Fmarker_position (val),
 16444                 /* FIXME: Don't we have a problem, using such a global
 16445                  * "last-position" if the variable is buffer-local?  */
 16446                 Fget (var, Qlast_arrow_position))
 16447           || ! (pstr = overlay_arrow_string_or_property (var),
 16448                 EQ (pstr, Fget (var, Qlast_arrow_string))))
 16449         {
 16450           struct buffer *buf = XMARKER (val)->buffer;
 16451 
 16452           if (set_redisplay)
 16453             {
 16454               if (buf)
 16455                 bset_redisplay (buf);
 16456               changed = true;
 16457             }
 16458           else
 16459             return true;
 16460         }
 16461     }
 16462   return changed;
 16463 }
 16464 
 16465 /* Mark overlay arrows to be updated on next redisplay.  */
 16466 
 16467 static void
 16468 update_overlay_arrows (int up_to_date)
 16469 {
 16470   Lisp_Object vlist;
 16471 
 16472   for (vlist = Voverlay_arrow_variable_list;
 16473        CONSP (vlist);
 16474        vlist = XCDR (vlist))
 16475     {
 16476       Lisp_Object var = XCAR (vlist);
 16477 
 16478       if (!SYMBOLP (var))
 16479         continue;
 16480 
 16481       if (up_to_date > 0)
 16482         {
 16483           Lisp_Object val = find_symbol_value (var);
 16484           if (!MARKERP (val))
 16485             continue;
 16486           Fput (var, Qlast_arrow_position, Fmarker_position (val));
 16487           Fput (var, Qlast_arrow_string,
 16488                 overlay_arrow_string_or_property (var));
 16489         }
 16490       else if (up_to_date < 0
 16491                || !NILP (Fget (var, Qlast_arrow_position)))
 16492         {
 16493           Fput (var, Qlast_arrow_position, Qt);
 16494           Fput (var, Qlast_arrow_string, Qt);
 16495         }
 16496     }
 16497 }
 16498 
 16499 
 16500 /* Return overlay arrow string to display at row.
 16501    Return integer (bitmap number) for arrow bitmap in left fringe.
 16502    Return nil if no overlay arrow.  */
 16503 
 16504 static Lisp_Object
 16505 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
 16506 {
 16507   Lisp_Object vlist;
 16508 
 16509   for (vlist = Voverlay_arrow_variable_list;
 16510        CONSP (vlist);
 16511        vlist = XCDR (vlist))
 16512     {
 16513       Lisp_Object var = XCAR (vlist);
 16514       Lisp_Object val;
 16515 
 16516       if (!SYMBOLP (var))
 16517         continue;
 16518 
 16519       val = find_symbol_value (var);
 16520 
 16521       if (MARKERP (val)
 16522           && current_buffer == XMARKER (val)->buffer
 16523           && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
 16524         {
 16525           if (FRAME_WINDOW_P (it->f)
 16526               /* FIXME: if ROW->reversed_p is set, this should test
 16527                  the right fringe, not the left one.  */
 16528               && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
 16529             {
 16530 #ifdef HAVE_WINDOW_SYSTEM
 16531               if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
 16532                 {
 16533                   int fringe_bitmap = lookup_fringe_bitmap (val);
 16534                   if (fringe_bitmap != 0)
 16535                     return make_fixnum (fringe_bitmap);
 16536                 }
 16537 #endif
 16538               return make_fixnum (-1); /* Use default arrow bitmap.  */
 16539             }
 16540           return overlay_arrow_string_or_property (var);
 16541         }
 16542     }
 16543 
 16544   return Qnil;
 16545 }
 16546 
 16547 /* Return true if point moved out of or into a composition.  Otherwise
 16548    return false.  PREV_BUF and PREV_PT are the last point buffer and
 16549    position.  BUF and PT are the current point buffer and position.  */
 16550 
 16551 static bool
 16552 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 16553                             struct buffer *buf, ptrdiff_t pt)
 16554 {
 16555   ptrdiff_t start, end;
 16556   Lisp_Object prop;
 16557   Lisp_Object buffer;
 16558 
 16559   XSETBUFFER (buffer, buf);
 16560   /* Check a composition at the last point if point moved within the
 16561      same buffer.  */
 16562   if (prev_buf == buf)
 16563     {
 16564       if (prev_pt == pt)
 16565         /* Point didn't move.  */
 16566         return false;
 16567 
 16568       if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
 16569           && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
 16570           && composition_valid_p (start, end, prop)
 16571           && start < prev_pt && end > prev_pt)
 16572         /* The last point was within the composition.  Return true iff
 16573             point moved out of the composition.  */
 16574         return (pt <= start || pt >= end);
 16575     }
 16576 
 16577   /* Check a composition at the current point.  */
 16578   return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
 16579           && find_composition (pt, -1, &start, &end, &prop, buffer)
 16580           && composition_valid_p (start, end, prop)
 16581           && start < pt && end > pt);
 16582 }
 16583 
 16584 /* Reconsider the clip changes of buffer which is displayed in W.  */
 16585 
 16586 static void
 16587 reconsider_clip_changes (struct window *w)
 16588 {
 16589   struct buffer *b = XBUFFER (w->contents);
 16590 
 16591   if (b->clip_changed
 16592       && w->window_end_valid
 16593       && w->current_matrix->buffer == b
 16594       && w->current_matrix->zv == BUF_ZV (b)
 16595       && w->current_matrix->begv == BUF_BEGV (b))
 16596     b->clip_changed = false;
 16597 
 16598   /* If display wasn't paused, and W is not a tool bar window, see if
 16599      point has been moved into or out of a composition.  In that case,
 16600      set b->clip_changed to force updating the screen.  If
 16601      b->clip_changed has already been set, skip this check.  */
 16602   if (!b->clip_changed && w->window_end_valid)
 16603     {
 16604       ptrdiff_t pt = (w == XWINDOW (selected_window)
 16605                       ? PT : marker_position (w->pointm));
 16606 
 16607       if ((w->current_matrix->buffer != b || pt != w->last_point)
 16608           && check_point_in_composition (w->current_matrix->buffer,
 16609                                          w->last_point, b, pt))
 16610         b->clip_changed = true;
 16611     }
 16612 }
 16613 
 16614 static void
 16615 propagate_buffer_redisplay (void)
 16616 { /* Resetting b->text->redisplay is problematic!
 16617      We can't just reset it in the case that some window that displays
 16618      it has not been redisplayed; and such a window can stay
 16619      unredisplayed for a long time if it's currently invisible.
 16620      But we do want to reset it at the end of redisplay otherwise
 16621      its displayed windows will keep being redisplayed over and over
 16622      again.
 16623      So we copy all b->text->redisplay flags up to their windows here,
 16624      such that mark_window_display_accurate can safely reset
 16625      b->text->redisplay.  */
 16626   Lisp_Object ws = window_list ();
 16627   for (; CONSP (ws); ws = XCDR (ws))
 16628     {
 16629       struct window *thisw = XWINDOW (XCAR (ws));
 16630       struct buffer *thisb = XBUFFER (thisw->contents);
 16631       if (thisb->text->redisplay)
 16632         thisw->redisplay = true;
 16633     }
 16634 }
 16635 
 16636 #define STOP_POLLING                                    \
 16637 do { if (! polling_stopped_here) stop_polling ();       \
 16638        polling_stopped_here = true; } while (false)
 16639 
 16640 #define RESUME_POLLING                                  \
 16641 do { if (polling_stopped_here) start_polling ();        \
 16642        polling_stopped_here = false; } while (false)
 16643 
 16644 /* Perhaps in the future avoid recentering windows if it
 16645    is not necessary; currently that causes some problems.  */
 16646 
 16647 static void
 16648 redisplay_internal (void)
 16649 {
 16650   struct window *w = XWINDOW (selected_window);
 16651   struct window *sw;
 16652   struct frame *fr;
 16653   bool pending;
 16654   bool must_finish = false, match_p;
 16655   struct text_pos tlbufpos, tlendpos;
 16656   int number_of_visible_frames;
 16657   struct frame *sf;
 16658   bool polling_stopped_here = false;
 16659   Lisp_Object tail, frame;
 16660 
 16661   /* Set a limit to the number of retries we perform due to horizontal
 16662      scrolling, this avoids getting stuck in an uninterruptible
 16663      infinite loop (Bug #24633).  */
 16664   enum { MAX_HSCROLL_RETRIES = 16 };
 16665   int hscroll_retries = 0;
 16666 
 16667   /* Limit the number of retries for when frame(s) become garbaged as
 16668      result of redisplaying them.  Some packages set various redisplay
 16669      hooks, such as window-scroll-functions, to run Lisp that always
 16670      calls APIs which cause the frame's garbaged flag to become set,
 16671      so we loop indefinitely.  */
 16672   enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
 16673   int garbaged_frame_retries = 0;
 16674 
 16675   /* False means that only the selected_window needs to be updated.
 16676      True means that other windows may need to be updated as well,
 16677      so we need to consult `needs_no_update` for all windows.  */
 16678   bool consider_all_windows_p;
 16679 
 16680   /* True means redisplay has to redisplay the miniwindow.  */
 16681   bool update_miniwindow_p = false;
 16682 
 16683   redisplay_trace ("redisplay_internal %d\n", redisplaying_p);
 16684 
 16685   /* No redisplay if running in batch mode or frame is not yet fully
 16686      initialized, or redisplay is explicitly turned off by setting
 16687      Vinhibit_redisplay.  */
 16688   if ((FRAME_INITIAL_P (SELECTED_FRAME ())
 16689        && redisplay_skip_initial_frame)
 16690       || !NILP (Vinhibit_redisplay))
 16691     return;
 16692 
 16693   /* Don't examine these until after testing Vinhibit_redisplay.
 16694      When Emacs is shutting down, perhaps because its connection to
 16695      X has dropped, we should not look at them at all.  */
 16696   fr = XFRAME (w->frame);
 16697   sf = SELECTED_FRAME ();
 16698 
 16699   if (!fr->glyphs_initialized_p)
 16700     return;
 16701 
 16702 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
 16703   if (popup_activated ())
 16704     return;
 16705 #endif
 16706 
 16707 #if defined (HAVE_HAIKU)
 16708   if (popup_activated_p)
 16709     return;
 16710 #endif
 16711 
 16712   /* I don't think this happens but let's be paranoid.  */
 16713   if (redisplaying_p)
 16714     return;
 16715 
 16716   /* Record a function that clears redisplaying_p
 16717      when we leave this function.  */
 16718   specpdl_ref count = SPECPDL_INDEX ();
 16719   record_unwind_protect_void (unwind_redisplay);
 16720   redisplaying_p = true;
 16721   block_buffer_flips ();
 16722   specbind (Qinhibit_free_realized_faces, Qnil);
 16723 
 16724   /* Record this function, so it appears on the profiler's backtraces.  */
 16725   record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
 16726 
 16727   FOR_EACH_FRAME (tail, frame)
 16728     XFRAME (frame)->already_hscrolled_p = false;
 16729 
 16730   reset_outermost_restrictions ();
 16731 
 16732  retry:
 16733   /* Remember the currently selected window.  */
 16734   sw = w;
 16735 
 16736   pending = false;
 16737   forget_escape_and_glyphless_faces ();
 16738 
 16739   inhibit_free_realized_faces = false;
 16740 
 16741   /* If face_change, init_iterator will free all realized faces, which
 16742      includes the faces referenced from current matrices.  So, we
 16743      can't reuse current matrices in this case.  */
 16744   if (face_change)
 16745     windows_or_buffers_changed = 47;
 16746 
 16747   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
 16748       && FRAME_TTY (sf)->previous_frame != sf)
 16749     {
 16750       /* Since frames on a single ASCII terminal share the same
 16751          display area, displaying a different frame means redisplay
 16752          the whole thing.  */
 16753       SET_FRAME_GARBAGED (sf);
 16754 #if !defined DOS_NT && !defined HAVE_ANDROID
 16755       set_tty_color_mode (FRAME_TTY (sf), sf);
 16756 #endif
 16757       FRAME_TTY (sf)->previous_frame = sf;
 16758     }
 16759 
 16760   /* Set the visible flags for all frames.  Do this before checking for
 16761      resized or garbaged frames; they want to know if their frames are
 16762      visible.  See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
 16763   number_of_visible_frames = 0;
 16764 
 16765   FOR_EACH_FRAME (tail, frame)
 16766     {
 16767       struct frame *f = XFRAME (frame);
 16768 
 16769       if (FRAME_REDISPLAY_P (f))
 16770         {
 16771           ++number_of_visible_frames;
 16772           /* Adjust matrices for visible frames only.  */
 16773           if (f->fonts_changed)
 16774             {
 16775               adjust_frame_glyphs (f);
 16776               /* Disable all redisplay optimizations for this frame.
 16777                  This is because adjust_frame_glyphs resets the
 16778                  enabled_p flag for all glyph rows of all windows, so
 16779                  many optimizations will fail anyway, and some might
 16780                  fail to test that flag and do bogus things as
 16781                  result.  */
 16782               SET_FRAME_GARBAGED (f);
 16783               f->fonts_changed = false;
 16784             }
 16785           /* If cursor type has been changed on the frame
 16786              other than selected, consider all frames.  */
 16787           if (f != sf && f->cursor_type_changed)
 16788             fset_redisplay (f);
 16789         }
 16790       clear_desired_matrices (f);
 16791     }
 16792 
 16793   /* Notice any pending interrupt request to change frame size.  */
 16794   do_pending_window_change (true);
 16795 
 16796   /* Clear frames marked as garbaged.  */
 16797   clear_garbaged_frames ();
 16798 
 16799   /* Build menubar and tool-bar items.  */
 16800   if (NILP (Vmemory_full))
 16801     prepare_menu_bars ();
 16802 
 16803   /* do_pending_window_change could change the selected_window due to
 16804      frame resizing which makes the selected window too small.
 16805      prepare_menu_bars may call lisp hooks and hence also change the
 16806      selected_window.  */
 16807   if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
 16808     sw = w;
 16809 
 16810   reconsider_clip_changes (w);
 16811 
 16812   /* In most cases selected window displays current buffer.  */
 16813   match_p = XBUFFER (w->contents) == current_buffer;
 16814   if (match_p)
 16815     {
 16816       /* Detect case that we need to write or remove a star in the mode line.  */
 16817       if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
 16818         w->update_mode_line = true;
 16819 
 16820       if (mode_line_update_needed (w))
 16821         w->update_mode_line = true;
 16822 
 16823       /* If reconsider_clip_changes above decided that the narrowing
 16824          in the current buffer changed, make sure all other windows
 16825          showing that buffer will be redisplayed.  */
 16826       if (current_buffer->clip_changed)
 16827         bset_update_mode_line (current_buffer);
 16828     }
 16829 
 16830   /* Normally the message* functions will have already displayed and
 16831      updated the echo area, but the frame may have been trashed, or
 16832      the update may have been preempted, so display the echo area
 16833      again here.  Checking message_cleared_p captures the case that
 16834      the echo area should be cleared.  */
 16835   if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
 16836       || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
 16837       || (message_cleared_p
 16838           && minibuf_level == 0
 16839           /* If the mini-window is currently selected, this means the
 16840              echo-area doesn't show through.  */
 16841           && !MINI_WINDOW_P (XWINDOW (selected_window))))
 16842     {
 16843       echo_area_display (false);
 16844 
 16845       if (message_cleared_p)
 16846         update_miniwindow_p = true;
 16847 
 16848       must_finish = true;
 16849 
 16850       /* If we don't display the current message, don't clear the
 16851          message_cleared_p flag, because, if we did, we wouldn't clear
 16852          the echo area in the next redisplay which doesn't preserve
 16853          the echo area.  */
 16854       if (!display_last_displayed_message_p)
 16855         message_cleared_p = false;
 16856     }
 16857   else if (EQ (selected_window, minibuf_window)
 16858            && (current_buffer->clip_changed || window_outdated (w))
 16859            && resize_mini_window (w, false))
 16860     {
 16861       /* Resized active mini-window to fit the size of what it is
 16862          showing if its contents might have changed.  */
 16863       must_finish = true;
 16864 
 16865       /* If window configuration was changed, frames may have been
 16866          marked garbaged.  Clear them or we will experience
 16867          surprises wrt scrolling.  */
 16868       clear_garbaged_frames ();
 16869     }
 16870 
 16871   if (!NILP (Vrun_hooks))
 16872     run_window_change_functions ();
 16873 
 16874   if (windows_or_buffers_changed && !update_mode_lines)
 16875     /* Code that sets windows_or_buffers_changed doesn't distinguish whether
 16876        only the windows's contents needs to be refreshed, or whether the
 16877        mode-lines also need a refresh.  */
 16878     update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
 16879                          ? REDISPLAY_SOME : 32);
 16880 
 16881   /* If specs for an arrow have changed, do thorough redisplay
 16882      to ensure we remove any arrow that should no longer exist.  */
 16883   /* Apparently, this is the only case where we update other windows,
 16884      without updating other mode-lines.  */
 16885   overlay_arrows_changed_p (true);
 16886 
 16887   consider_all_windows_p = (update_mode_lines
 16888                             || windows_or_buffers_changed);
 16889 
 16890 #define AINC(a,i)                                                       \
 16891   {                                                                     \
 16892     Lisp_Object entry = Fgethash (make_fixnum (i), a, make_fixnum (0)); \
 16893     if (FIXNUMP (entry))                                                \
 16894       Fputhash (make_fixnum (i), make_fixnum (1 + XFIXNUM (entry)), a); \
 16895   }
 16896 
 16897   AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
 16898   AINC (Vredisplay__mode_lines_cause, update_mode_lines);
 16899 
 16900   /* Optimize the case that only the line containing the cursor in the
 16901      selected window has changed.  Variables starting with this_ are
 16902      set in display_line and record information about the line
 16903      containing the cursor.  */
 16904   tlbufpos = this_line_start_pos;
 16905   tlendpos = this_line_end_pos;
 16906   if (!consider_all_windows_p
 16907       && CHARPOS (tlbufpos) > 0
 16908       && !w->update_mode_line
 16909       && !current_buffer->clip_changed
 16910       && !current_buffer->prevent_redisplay_optimizations_p
 16911       && FRAME_REDISPLAY_P (XFRAME (w->frame))
 16912       && !FRAME_OBSCURED_P (XFRAME (w->frame))
 16913       && !XFRAME (w->frame)->cursor_type_changed
 16914       && !XFRAME (w->frame)->face_change
 16915       /* Make sure recorded data applies to current buffer, etc.  */
 16916       && this_line_buffer == current_buffer
 16917       && match_p
 16918       && !w->force_start
 16919       && !w->optional_new_start
 16920       /* Point must be on the line that we have info recorded about.  */
 16921       && PT >= CHARPOS (tlbufpos)
 16922       && PT <= Z - CHARPOS (tlendpos)
 16923       /* FIXME: The following condition is only needed when
 16924          significant parts of the buffer are hidden (e.g., under
 16925          hs-minor-mode), but there doesn't seem to be a simple way of
 16926          detecting that, so we always disable the one-line redisplay
 16927          optimizations whenever display-line-numbers-mode is turned on
 16928          in the buffer.  */
 16929       && (NILP (Vdisplay_line_numbers)
 16930           || EQ (Vdisplay_line_numbers, Qvisual))
 16931       /* All text outside that line, including its final newline,
 16932          must be unchanged.  */
 16933       && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
 16934                                         CHARPOS (tlendpos)))
 16935     {
 16936       if (CHARPOS (tlbufpos) > BEGV
 16937           && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
 16938           && (CHARPOS (tlbufpos) == ZV
 16939               || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
 16940         /* Former continuation line has disappeared by becoming empty.  */
 16941         goto cancel;
 16942       else if (window_outdated (w) || MINI_WINDOW_P (w))
 16943         {
 16944           /* We have to handle the case of continuation around a
 16945              wide-column character (see the comment in indent.c around
 16946              line 1340).
 16947 
 16948              For instance, in the following case:
 16949 
 16950              --------  Insert  --------
 16951              K_A_N_\\   `a'    K_A_N_a\         `X_' are wide-column chars.
 16952              J_I_       ==>    J_I_             `^^' are cursors.
 16953              ^^                ^^
 16954              --------          --------
 16955 
 16956              As we have to redraw the line above, we cannot use this
 16957              optimization.  */
 16958 
 16959           struct it it;
 16960           int line_height_before = this_line_pixel_height;
 16961 
 16962           /* Note that start_display will handle the case that the
 16963              line starting at tlbufpos is a continuation line.  */
 16964           start_display (&it, w, tlbufpos);
 16965 
 16966           /* Implementation note: It this still necessary?  */
 16967           if (it.current_x != this_line_start_x)
 16968             goto cancel;
 16969 
 16970           /* Give up on this optimization if the line starts with a
 16971              string with display property that draws on the fringes,
 16972              as that might interfere with line-prefix display.  */
 16973           if (it.sp > 1
 16974               && it.method == GET_FROM_IMAGE && it.image_id == -1)
 16975             goto cancel;
 16976           redisplay_trace ("trying display optimization 1\n");
 16977           w->cursor.vpos = -1;
 16978           overlay_arrow_seen = false;
 16979           it.vpos = this_line_vpos;
 16980           it.current_y = this_line_y;
 16981           it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
 16982           display_line (&it, -1);
 16983 
 16984           /* If line contains point, is not continued,
 16985              and ends at same distance from eob as before, we win.  */
 16986           if (w->cursor.vpos >= 0
 16987               /* Line is not continued, otherwise this_line_start_pos
 16988                  would have been set to 0 in display_line.  */
 16989               && CHARPOS (this_line_start_pos)
 16990               /* Line ends as before.  */
 16991               && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
 16992               /* Line has same height as before.  Otherwise other lines
 16993                  would have to be shifted up or down.  */
 16994               && this_line_pixel_height == line_height_before
 16995               /* Cannot use this optimization if hscrolling current
 16996                  line and this line is the current one, because
 16997                  display_line above is not informed about the
 16998                  current-line's vpos, and cannot DTRT in that case.  */
 16999               && !hscrolling_current_line_p (w))
 17000             {
 17001               /* If this is not the window's last line, we must adjust
 17002                  the charstarts of the lines below.  */
 17003               if (it.current_y < it.last_visible_y)
 17004                 {
 17005                   struct glyph_row *row
 17006                     = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 17007                   ptrdiff_t delta, delta_bytes;
 17008 
 17009                   /* We used to distinguish between two cases here,
 17010                      conditioned by Z - CHARPOS (tlendpos) == ZV, for
 17011                      when the line ends in a newline or the end of the
 17012                      buffer's accessible portion.  But both cases did
 17013                      the same, so they were collapsed.  */
 17014                   delta = (Z
 17015                            - CHARPOS (tlendpos)
 17016                            - MATRIX_ROW_START_CHARPOS (row));
 17017                   delta_bytes = (Z_BYTE
 17018                                  - BYTEPOS (tlendpos)
 17019                                  - MATRIX_ROW_START_BYTEPOS (row));
 17020 
 17021                   increment_matrix_positions (w->current_matrix,
 17022                                               this_line_vpos + 1,
 17023                                               w->current_matrix->nrows,
 17024                                               delta, delta_bytes);
 17025                 }
 17026 
 17027               /* If this row displays text now but previously didn't,
 17028                  or vice versa, w->window_end_vpos may have to be
 17029                  adjusted.  */
 17030               if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
 17031                 {
 17032                   if (w->window_end_vpos < this_line_vpos)
 17033                     w->window_end_vpos = this_line_vpos;
 17034                 }
 17035               else if (w->window_end_vpos == this_line_vpos
 17036                        && this_line_vpos > 0)
 17037                 w->window_end_vpos = this_line_vpos - 1;
 17038               w->window_end_valid = false;
 17039 
 17040               /* Update hint: No need to try to scroll in update_window.  */
 17041               w->desired_matrix->no_scrolling_p = true;
 17042 
 17043 #ifdef GLYPH_DEBUG
 17044               *w->desired_matrix->method = 0;
 17045               debug_method_add (w, "optimization 1");
 17046 #endif
 17047 #ifdef HAVE_WINDOW_SYSTEM
 17048               update_window_fringes (w, false);
 17049 #endif
 17050               goto update;
 17051             }
 17052           else
 17053             goto cancel;
 17054         }
 17055       else if (/* Cursor position hasn't changed.  */
 17056                PT == w->last_point
 17057                /* Make sure the cursor was last displayed
 17058                   in this window.  Otherwise we have to reposition it.  */
 17059 
 17060                /* PXW: Must be converted to pixels, probably.  */
 17061                && 0 <= w->cursor.vpos
 17062                && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
 17063         {
 17064           if (!must_finish)
 17065             {
 17066               do_pending_window_change (true);
 17067               /* If selected_window changed, redisplay again.  */
 17068               if (WINDOWP (selected_window)
 17069                   && (w = XWINDOW (selected_window)) != sw)
 17070                 goto retry;
 17071 
 17072                 /* We used to always goto end_of_redisplay here, but this
 17073                  isn't enough if we have a blinking cursor.  */
 17074               if (w->cursor_off_p == w->last_cursor_off_p)
 17075                 goto end_of_redisplay;
 17076             }
 17077           goto update;
 17078         }
 17079       /* If highlighting the region, or if the cursor is in the echo area,
 17080          then we can't just move the cursor.  */
 17081       else if (NILP (Vshow_trailing_whitespace)
 17082                && !cursor_in_echo_area
 17083                && !composition_break_at_point)
 17084         {
 17085           struct it it;
 17086           struct glyph_row *row;
 17087 
 17088           /* Skip from tlbufpos to PT and see where it is.  Note that
 17089              PT may be in invisible text.  If so, we will end at the
 17090              next visible position.  */
 17091           init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
 17092                          NULL, DEFAULT_FACE_ID);
 17093           it.current_x = this_line_start_x;
 17094           it.current_y = this_line_y;
 17095           it.vpos = this_line_vpos;
 17096 
 17097           if (current_buffer->long_line_optimizations_p
 17098               && it.line_wrap == TRUNCATE
 17099               && PT - CHARPOS (tlbufpos) > large_hscroll_threshold)
 17100             {
 17101               /* When lines are very long and truncated, jumping to
 17102                  the next visible line is much faster than slowly
 17103                  iterating there.  */
 17104               reseat_at_next_visible_line_start (&it, false);
 17105               if (IT_CHARPOS (it) <= PT) /* point moved off this line */
 17106                 it.vpos = this_line_vpos + 1;
 17107             }
 17108           else
 17109             {
 17110               /* The call to move_it_to stops in front of PT, but
 17111                  moves over before-strings.  */
 17112               move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
 17113             }
 17114 
 17115           if (it.vpos == this_line_vpos
 17116               && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
 17117                   row->enabled_p))
 17118             {
 17119               eassert (this_line_vpos == it.vpos);
 17120               eassert (this_line_y == it.current_y);
 17121               set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 17122               if (cursor_row_fully_visible_p (w, false, true, false))
 17123                 {
 17124 #ifdef GLYPH_DEBUG
 17125                   *w->desired_matrix->method = 0;
 17126                   debug_method_add (w, "optimization 3");
 17127 #endif
 17128                   goto update;
 17129                 }
 17130               else
 17131                 goto cancel;
 17132             }
 17133           else
 17134             goto cancel;
 17135         }
 17136 
 17137     cancel:
 17138       /* Text changed drastically or point moved off of line.  */
 17139       SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
 17140     }
 17141 
 17142   CHARPOS (this_line_start_pos) = 0;
 17143   ++clear_face_cache_count;
 17144 #ifdef HAVE_WINDOW_SYSTEM
 17145   ++clear_image_cache_count;
 17146 #endif
 17147 
 17148   /* Build desired matrices, and update the display.  If
 17149      consider_all_windows_p, do it for all windows on all frames that
 17150      require redisplay, as specified by their 'redisplay' flag.
 17151      Otherwise do it for selected_window, only.  */
 17152 
 17153   if (consider_all_windows_p)
 17154     {
 17155       FOR_EACH_FRAME (tail, frame)
 17156         XFRAME (frame)->updated_p = false;
 17157 
 17158       propagate_buffer_redisplay ();
 17159 
 17160       FOR_EACH_FRAME (tail, frame)
 17161         {
 17162           struct frame *f = XFRAME (frame);
 17163 
 17164           /* We don't have to do anything for unselected terminal
 17165              frames.  */
 17166           if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
 17167               && !EQ (FRAME_TTY (f)->top_frame, frame))
 17168             continue;
 17169 
 17170         retry_frame:
 17171           if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
 17172             {
 17173               bool gcscrollbars
 17174                 /* Only GC scrollbars when we redisplay the whole frame.  */
 17175                 = f->redisplay || !REDISPLAY_SOME_P ();
 17176               bool f_redisplay_flag = f->redisplay;
 17177 
 17178               /* The X error handler may have deleted that frame
 17179                  before we went back to retry_frame.  This must come
 17180                  before any accesses to f->terminal.  */
 17181               if (!FRAME_LIVE_P (f))
 17182                 continue;
 17183 
 17184               /* Mark all the scroll bars to be removed; we'll redeem
 17185                  the ones we want when we redisplay their windows.  */
 17186               if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
 17187                 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
 17188 
 17189               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17190                 {
 17191                   /* Don't allow freeing images and faces for this
 17192                      frame as long as the frame's update wasn't
 17193                      completed.  This prevents crashes when some Lisp
 17194                      that runs from the various hooks or font-lock
 17195                      decides to clear the frame's image cache and face
 17196                      cache, when the images and faces in those caches
 17197                      are referenced by the desired matrix.  */
 17198                   f->inhibit_clear_image_cache = true;
 17199                   redisplay_windows (FRAME_ROOT_WINDOW (f));
 17200                 }
 17201               /* Remember that the invisible frames need to be redisplayed next
 17202                  time they're visible.  */
 17203               else if (!REDISPLAY_SOME_P ())
 17204                 f->redisplay = true;
 17205 
 17206               /* The X error handler may have deleted that frame.  */
 17207               if (!FRAME_LIVE_P (f))
 17208                 continue;
 17209 
 17210               /* Any scroll bars which redisplay_windows should have
 17211                  nuked should now go away.  */
 17212               if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
 17213                 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
 17214 
 17215               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17216                 {
 17217                   /* If fonts changed on visible frame, display again.  */
 17218                   if (f->fonts_changed)
 17219                     {
 17220                       adjust_frame_glyphs (f);
 17221                       /* Disable all redisplay optimizations for this
 17222                          frame.  For the reasons, see the comment near
 17223                          the previous call to adjust_frame_glyphs above.  */
 17224                       SET_FRAME_GARBAGED (f);
 17225                       f->fonts_changed = false;
 17226                       goto retry_frame;
 17227                     }
 17228 
 17229                   /* See if we have to hscroll.  */
 17230                   if (!f->already_hscrolled_p)
 17231                     {
 17232                       f->already_hscrolled_p = true;
 17233                       if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17234                           && hscroll_windows (f->root_window))
 17235                         {
 17236                           hscroll_retries++;
 17237                           goto retry_frame;
 17238                         }
 17239                     }
 17240 
 17241                   /* If the frame's redisplay flag was not set before
 17242                      we went about redisplaying its windows, but it is
 17243                      set now, that means we employed some redisplay
 17244                      optimizations inside redisplay_windows, and
 17245                      bypassed producing some screen lines.  But if
 17246                      f->redisplay is now set, it might mean the old
 17247                      faces are no longer valid (e.g., if redisplaying
 17248                      some window called some Lisp which defined a new
 17249                      face or redefined an existing face), so trying to
 17250                      use them in update_frame will segfault.
 17251                      Therefore, we must redisplay this frame.  */
 17252                   if (!f_redisplay_flag && f->redisplay)
 17253                     goto retry_frame;
 17254                   /* In some case (e.g., window resize), we notice
 17255                      only during window updating that the window
 17256                      content changed unpredictably (e.g., a GTK
 17257                      scrollbar moved, or some Lisp hook that winds up
 17258                      calling adjust_frame_glyphs) and that our
 17259                      previous estimation of the frame content was
 17260                      garbage.  We have to start over.  These cases
 17261                      should be rare, so going all the way back to the
 17262                      top of redisplay should be good enough.  */
 17263                   if (FRAME_GARBAGED_P (f)
 17264                       && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
 17265                     goto retry;
 17266 
 17267 #ifdef HAVE_WINDOW_SYSTEM
 17268                   if (FRAME_WINDOW_P (f)
 17269                       && FRAME_RIF (f)->clear_under_internal_border)
 17270                     FRAME_RIF (f)->clear_under_internal_border (f);
 17271 #endif
 17272                   /* Prevent various kinds of signals during display
 17273                      update.  stdio is not robust about handling
 17274                      signals, which can cause an apparent I/O error.  */
 17275                   if (interrupt_input)
 17276                     unrequest_sigio ();
 17277                   STOP_POLLING;
 17278 
 17279                   pending |= update_frame (f, false, false);
 17280                   /* On some platforms (at least MS-Windows), the
 17281                      scroll_run_hook called from scrolling_window
 17282                      called from update_frame could set the frame's
 17283                      garbaged flag, in which case we need to redisplay
 17284                      the frame.  Don't do that on TTY frames, since we
 17285                      need to keep the garbaged flag in that case when
 17286                      the frame has been resized.  */
 17287                   if (FRAME_GARBAGED_P (f))
 17288                     {
 17289                       fset_redisplay (f);
 17290                       f->garbaged = false;
 17291                       goto retry_frame;
 17292                     }
 17293                   f->cursor_type_changed = false;
 17294                   f->updated_p = true;
 17295                   f->inhibit_clear_image_cache = false;
 17296                 }
 17297             }
 17298         }
 17299 
 17300       eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
 17301 
 17302       if (!pending)
 17303         {
 17304           /* Do the mark_window_display_accurate after all windows have
 17305              been redisplayed because this call resets flags in buffers
 17306              which are needed for proper redisplay.  */
 17307           FOR_EACH_FRAME (tail, frame)
 17308             {
 17309               struct frame *f = XFRAME (frame);
 17310               if (f->updated_p)
 17311                 {
 17312                   f->redisplay = false;
 17313                   f->garbaged = false;
 17314                   mark_window_display_accurate (f->root_window, true);
 17315                   if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 17316                     FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
 17317                 }
 17318             }
 17319         }
 17320     }
 17321   else if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17322     {
 17323       sf->inhibit_clear_image_cache = true;
 17324       displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
 17325       /* Use list_of_error, not Qerror, so that
 17326          we catch only errors and don't run the debugger.  */
 17327       internal_condition_case_1 (redisplay_window_1, selected_window,
 17328                                  list_of_error,
 17329                                  redisplay_window_error);
 17330       if (update_miniwindow_p)
 17331         {
 17332           Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17333 
 17334           displayed_buffer = XBUFFER (XWINDOW (mini_window)->contents);
 17335           internal_condition_case_1 (redisplay_window_1, mini_window,
 17336                                      list_of_error,
 17337                                      redisplay_window_error);
 17338         }
 17339 
 17340       /* Compare desired and current matrices, perform output.  */
 17341 
 17342     update:
 17343       /* If fonts changed, display again.  Likewise if redisplay_window_1
 17344          above caused some change (e.g., a change in faces) that requires
 17345          considering the entire frame again.  */
 17346       if (sf->fonts_changed || sf->redisplay)
 17347         {
 17348           if (sf->redisplay)
 17349             {
 17350               /* Set this to force a more thorough redisplay.
 17351                  Otherwise, we might immediately loop back to the
 17352                  above "else-if" clause (since all the conditions that
 17353                  led here might still be true), and we will then
 17354                  infloop, because the selected-frame's redisplay flag
 17355                  is not (and cannot be) reset.  */
 17356               windows_or_buffers_changed = 50;
 17357             }
 17358           goto retry;
 17359         }
 17360 
 17361       /* Prevent freeing of realized faces, since desired matrices are
 17362          pending that reference the faces we computed and cached.  */
 17363       inhibit_free_realized_faces = true;
 17364 
 17365       /* Prevent various kinds of signals during display update.
 17366          stdio is not robust about handling signals,
 17367          which can cause an apparent I/O error.  */
 17368       if (interrupt_input)
 17369         unrequest_sigio ();
 17370       STOP_POLLING;
 17371 
 17372       if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17373         {
 17374           if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17375               && hscroll_windows (selected_window))
 17376             {
 17377               hscroll_retries++;
 17378               goto retry;
 17379             }
 17380 
 17381           XWINDOW (selected_window)->must_be_updated_p = true;
 17382           pending = update_frame (sf, false, false);
 17383           sf->cursor_type_changed = false;
 17384           sf->inhibit_clear_image_cache = false;
 17385         }
 17386 
 17387       /* We may have called echo_area_display at the top of this
 17388          function.  If the echo area is on another frame, that may
 17389          have put text on a frame other than the selected one, so the
 17390          above call to update_frame would not have caught it.  Catch
 17391          it here.  */
 17392       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17393       struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 17394 
 17395       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
 17396         {
 17397           XWINDOW (mini_window)->must_be_updated_p = true;
 17398           pending |= update_frame (mini_frame, false, false);
 17399           mini_frame->cursor_type_changed = false;
 17400           if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
 17401               && hscroll_windows (mini_window))
 17402             {
 17403               hscroll_retries++;
 17404               goto retry;
 17405             }
 17406         }
 17407     }
 17408 
 17409   /* If display was paused because of pending input, make sure we do a
 17410      thorough update the next time.  */
 17411   if (pending)
 17412     {
 17413       /* Prevent the optimization at the beginning of
 17414          redisplay_internal that tries a single-line update of the
 17415          line containing the cursor in the selected window.  */
 17416       CHARPOS (this_line_start_pos) = 0;
 17417 
 17418       /* Let the overlay arrow be updated the next time.  */
 17419       update_overlay_arrows (0);
 17420 
 17421       /* If we pause after scrolling, some rows in the current
 17422          matrices of some windows are not valid.  */
 17423       if (!WINDOW_FULL_WIDTH_P (w)
 17424           && !FRAME_WINDOW_P (XFRAME (w->frame)))
 17425         update_mode_lines = 36;
 17426     }
 17427   else
 17428     {
 17429       if (!consider_all_windows_p)
 17430         {
 17431           /* This has already been done above if
 17432              consider_all_windows_p is set.  */
 17433           if (XBUFFER (w->contents)->text->redisplay
 17434               && buffer_window_count (XBUFFER (w->contents)) > 1)
 17435             /* This can happen if b->text->redisplay was set during
 17436                jit-lock.  */
 17437             propagate_buffer_redisplay ();
 17438           mark_window_display_accurate_1 (w, true);
 17439 
 17440           /* Say overlay arrows are up to date.  */
 17441           update_overlay_arrows (1);
 17442 
 17443           if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
 17444             FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
 17445         }
 17446 
 17447       update_mode_lines = 0;
 17448       windows_or_buffers_changed = 0;
 17449     }
 17450 
 17451   /* Start SIGIO interrupts coming again.  Having them off during the
 17452      code above makes it less likely one will discard output, but not
 17453      impossible, since there might be stuff in the system buffer here.
 17454      But it is much hairier to try to do anything about that.  */
 17455   if (interrupt_input)
 17456     request_sigio ();
 17457   RESUME_POLLING;
 17458 
 17459   /* If a frame has become visible which was not before, redisplay
 17460      again, so that we display it.  Expose events for such a frame
 17461      (which it gets when becoming visible) don't call the parts of
 17462      redisplay constructing glyphs, so simply exposing a frame won't
 17463      display anything in this case.  So, we have to display these
 17464      frames here explicitly.  */
 17465   if (!pending)
 17466     {
 17467       int new_count = 0;
 17468 
 17469       FOR_EACH_FRAME (tail, frame)
 17470         {
 17471           if (FRAME_REDISPLAY_P (XFRAME (frame)))
 17472             new_count++;
 17473         }
 17474 
 17475       if (new_count != number_of_visible_frames)
 17476         windows_or_buffers_changed = 52;
 17477     }
 17478 
 17479   /* Change frame size now if a change is pending.  */
 17480   do_pending_window_change (true);
 17481 
 17482   /* If we just did a pending size change, or have additional
 17483      visible frames, or selected_window changed, redisplay again.  */
 17484   if ((windows_or_buffers_changed && !pending)
 17485       || (WINDOWP (selected_window)
 17486           && (w = XWINDOW (selected_window)) != sw))
 17487     goto retry;
 17488 
 17489   /* Clear the face and image caches.
 17490 
 17491      We used to do this only if consider_all_windows_p.  But the cache
 17492      needs to be cleared if a timer creates images in the current
 17493      buffer (e.g. the test case in Bug#6230).  */
 17494 
 17495   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
 17496     {
 17497       clear_face_cache (false);
 17498       clear_face_cache_count = 0;
 17499     }
 17500 
 17501 #ifdef HAVE_WINDOW_SYSTEM
 17502   if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
 17503     {
 17504       clear_image_caches (Qnil);
 17505       clear_image_cache_count = 0;
 17506     }
 17507 #endif /* HAVE_WINDOW_SYSTEM */
 17508 
 17509  end_of_redisplay:
 17510 #ifdef HAVE_NS
 17511   ns_set_doc_edited ();
 17512 #endif
 17513   if (interrupt_input && interrupts_deferred)
 17514     request_sigio ();
 17515 
 17516   /* We're done with this redisplay cycle, so reset the tick count in
 17517      preparation for the next redisplay cycle.  */
 17518   if (max_redisplay_ticks > 0)
 17519     update_redisplay_ticks (0, NULL);
 17520 
 17521   unbind_to (count, Qnil);
 17522   RESUME_POLLING;
 17523 }
 17524 
 17525 static void
 17526 unwind_redisplay_preserve_echo_area (void)
 17527 {
 17528   unblock_buffer_flips ();
 17529 }
 17530 
 17531 /* Redisplay, but leave alone any recent echo area message unless
 17532    another message has been requested in its place.
 17533 
 17534    This is useful in situations where you need to redisplay but no
 17535    user action has occurred, making it inappropriate for the message
 17536    area to be cleared.  See tracking_off and
 17537    wait_reading_process_output for examples of these situations.
 17538 
 17539    FROM_WHERE is an integer saying from where this function was
 17540    called.  This is useful for debugging.  */
 17541 
 17542 void
 17543 redisplay_preserve_echo_area (int from_where)
 17544 {
 17545   redisplay_trace ("redisplay_preserve_echo_area (%d)\n", from_where);
 17546 
 17547   block_input ();
 17548   specpdl_ref count = SPECPDL_INDEX ();
 17549   record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
 17550   block_buffer_flips ();
 17551   unblock_input ();
 17552 
 17553   if (!NILP (echo_area_buffer[1]))
 17554     {
 17555       /* We have a previously displayed message, but no current
 17556          message.  Redisplay the previous message.  */
 17557       display_last_displayed_message_p = true;
 17558       redisplay_internal ();
 17559       display_last_displayed_message_p = false;
 17560     }
 17561   else
 17562     redisplay_internal ();
 17563 
 17564   flush_frame (SELECTED_FRAME ());
 17565   unbind_to (count, Qnil);
 17566 }
 17567 
 17568 
 17569 /* Function registered with record_unwind_protect in redisplay_internal.  */
 17570 
 17571 static void
 17572 unwind_redisplay (void)
 17573 {
 17574   redisplaying_p = false;
 17575   unblock_buffer_flips ();
 17576 }
 17577 
 17578 /* Function registered with record_unwind_protect before calling
 17579    start_display outside of redisplay_internal.  */
 17580 void
 17581 unwind_display_working_on_window (void)
 17582 {
 17583   display_working_on_window_p = false;
 17584 }
 17585 
 17586 /* Mark the display of leaf window W as accurate or inaccurate.
 17587    If ACCURATE_P, mark display of W as accurate.
 17588    If !ACCURATE_P, arrange for W to be redisplayed the next
 17589    time redisplay_internal is called.  */
 17590 
 17591 static void
 17592 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 17593 {
 17594   struct buffer *b = XBUFFER (w->contents);
 17595 #ifdef HAVE_TEXT_CONVERSION
 17596   ptrdiff_t prev_point, prev_mark;
 17597 #endif /* HAVE_TEXT_CONVERSION */
 17598 
 17599   w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
 17600   w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
 17601   w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
 17602 
 17603   if (accurate_p)
 17604     {
 17605       b->clip_changed = false;
 17606       b->prevent_redisplay_optimizations_p = false;
 17607       eassert (buffer_window_count (b) > 0);
 17608       /* Resetting b->text->redisplay is problematic!
 17609          In order to make it safer to do it here, redisplay_internal must
 17610          have copied all b->text->redisplay to their respective windows.  */
 17611       b->text->redisplay = false;
 17612 
 17613       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
 17614       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
 17615       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
 17616       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 17617 
 17618       w->current_matrix->buffer = b;
 17619       w->current_matrix->begv = BUF_BEGV (b);
 17620       w->current_matrix->zv = BUF_ZV (b);
 17621       w->current_matrix->header_line_p = window_wants_header_line (w);
 17622       w->current_matrix->tab_line_p = window_wants_tab_line (w);
 17623 
 17624       w->last_cursor_vpos = w->cursor.vpos;
 17625       w->last_cursor_off_p = w->cursor_off_p;
 17626 
 17627 #ifdef HAVE_TEXT_CONVERSION
 17628       prev_point = w->last_point;
 17629       prev_mark = w->last_mark;
 17630 #endif /* HAVE_TEXT_CONVERSION */
 17631 
 17632       if (w == XWINDOW (selected_window))
 17633         w->last_point = BUF_PT (b);
 17634       else
 17635         w->last_point = marker_position (w->pointm);
 17636 
 17637       /* w->last_mark is recorded for text conversion purposes.
 17638          Input methods aren't interested in the value of the mark
 17639          if it is inactive, so set it to -1 if it's not.  */
 17640 
 17641       if (XMARKER (BVAR (b, mark))->buffer == b
 17642           && !NILP (BVAR (b, mark_active)))
 17643         w->last_mark = marker_position (BVAR (b, mark));
 17644       else
 17645         w->last_mark = -1;
 17646 
 17647 #ifdef HAVE_TEXT_CONVERSION
 17648       /* See the description of this field in struct window.  */
 17649       w->ephemeral_last_point = w->last_point;
 17650 
 17651       /* Point motion is only propagated to the input method for use
 17652          in text conversion during a redisplay.  While this can lead
 17653          to inconsistencies when point has moved but the change has
 17654          not yet been displayed, it leads to better results most of
 17655          the time, as point often changes within calls to
 17656          `save-excursion', and the only way to detect such calls is to
 17657          observe that the next redisplay never ends with those changes
 17658          applied.
 17659 
 17660          Changes to buffer text are immediately propagated to the
 17661          input method, and the position of point is also updated
 17662          during such a change, so the consequences are not that
 17663          severe.  */
 17664 
 17665       if ((prev_point != w->last_point
 17666            || prev_mark != w->last_mark)
 17667           && FRAME_WINDOW_P (WINDOW_XFRAME (w))
 17668           && w == XWINDOW (WINDOW_XFRAME (w)->selected_window))
 17669         report_point_change (WINDOW_XFRAME (w), w, b);
 17670 #endif /* HAVE_TEXT_CONVERSION */
 17671 
 17672       struct glyph_row *row;
 17673       /* These conditions should be consistent with CHECK_WINDOW_END.  */
 17674       if (w->window_end_vpos < w->current_matrix->nrows
 17675           && ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
 17676                !row->enabled_p
 17677                || MATRIX_ROW_DISPLAYS_TEXT_P (row)
 17678                || MATRIX_ROW_VPOS (row, w->current_matrix) == 0)))
 17679         w->window_end_valid = true;
 17680       w->update_mode_line = false;
 17681       w->preserve_vscroll_p = false;
 17682     }
 17683 
 17684   w->redisplay = !accurate_p;
 17685 }
 17686 
 17687 
 17688 /* Mark the display of windows in the window tree rooted at WINDOW as
 17689    accurate or inaccurate.  If ACCURATE_P, mark display of
 17690    windows as accurate.  If !ACCURATE_P, arrange for windows to
 17691    be redisplayed the next time redisplay_internal is called.  */
 17692 
 17693 void
 17694 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
 17695 {
 17696   struct window *w;
 17697 
 17698   for (; !NILP (window); window = w->next)
 17699     {
 17700       w = XWINDOW (window);
 17701       if (WINDOWP (w->contents))
 17702         mark_window_display_accurate (w->contents, accurate_p);
 17703       else
 17704         mark_window_display_accurate_1 (w, accurate_p);
 17705     }
 17706 
 17707   if (accurate_p)
 17708     update_overlay_arrows (1);
 17709   else
 17710     /* Force a thorough redisplay the next time by setting
 17711        last_arrow_position and last_arrow_string to t, which is
 17712        unequal to any useful value of Voverlay_arrow_...  */
 17713     update_overlay_arrows (-1);
 17714 }
 17715 
 17716 
 17717 /* Return value in display table DP (Lisp_Char_Table *) for character
 17718    C.  Since a display table doesn't have any parent, we don't have to
 17719    follow parent.  Do not call this function directly but use the
 17720    macro DISP_CHAR_VECTOR.  */
 17721 
 17722 Lisp_Object
 17723 disp_char_vector (struct Lisp_Char_Table *dp, int c)
 17724 {
 17725   Lisp_Object val;
 17726 
 17727   if (ASCII_CHAR_P (c))
 17728     {
 17729       val = dp->ascii;
 17730       if (SUB_CHAR_TABLE_P (val))
 17731         val = XSUB_CHAR_TABLE (val)->contents[c];
 17732     }
 17733   else
 17734     {
 17735       Lisp_Object table;
 17736 
 17737       XSETCHAR_TABLE (table, dp);
 17738       val = char_table_ref (table, c);
 17739     }
 17740   if (NILP (val))
 17741     val = dp->defalt;
 17742   return val;
 17743 }
 17744 
 17745 static int buffer_flip_blocked_depth;
 17746 
 17747 static void
 17748 block_buffer_flips (void)
 17749 {
 17750   eassert (buffer_flip_blocked_depth >= 0);
 17751   buffer_flip_blocked_depth++;
 17752 }
 17753 
 17754 static void
 17755 unblock_buffer_flips (void)
 17756 {
 17757   eassert (buffer_flip_blocked_depth > 0);
 17758   if (--buffer_flip_blocked_depth == 0)
 17759     {
 17760       Lisp_Object tail, frame;
 17761       block_input ();
 17762       FOR_EACH_FRAME (tail, frame)
 17763         {
 17764           struct frame *f = XFRAME (frame);
 17765           if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
 17766             (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
 17767         }
 17768       unblock_input ();
 17769     }
 17770 }
 17771 
 17772 bool
 17773 buffer_flipping_blocked_p (void)
 17774 {
 17775   return buffer_flip_blocked_depth > 0;
 17776 }
 17777 
 17778 
 17779 /***********************************************************************
 17780                            Window Redisplay
 17781  ***********************************************************************/
 17782 
 17783 /* Redisplay all leaf windows in the window tree rooted at WINDOW.  */
 17784 
 17785 static void
 17786 redisplay_windows (Lisp_Object window)
 17787 {
 17788   while (!NILP (window))
 17789     {
 17790       struct window *w = XWINDOW (window);
 17791 
 17792       if (WINDOWP (w->contents))
 17793         redisplay_windows (w->contents);
 17794       else if (BUFFERP (w->contents))
 17795         {
 17796           displayed_buffer = XBUFFER (w->contents);
 17797           /* Use list_of_error, not Qerror, so that
 17798              we catch only errors and don't run the debugger.  */
 17799           internal_condition_case_1 (redisplay_window_0, window,
 17800                                      list_of_error,
 17801                                      redisplay_window_error);
 17802         }
 17803 
 17804       window = w->next;
 17805     }
 17806 }
 17807 
 17808 static Lisp_Object
 17809 redisplay_window_error (Lisp_Object error_data)
 17810 {
 17811   displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
 17812 
 17813   /* When in redisplay, the error is captured and not shown.  Arrange
 17814      for it to be shown later.  */
 17815   if (max_redisplay_ticks > 0
 17816       && CONSP (error_data)
 17817       && EQ (XCAR (error_data), Qerror)
 17818       && CONSP (XCDR (error_data))
 17819       && STRINGP (XCAR (XCDR (error_data))))
 17820     Vdelayed_warnings_list = Fcons (list2 (XCAR (error_data),
 17821                                            XCAR (XCDR (error_data))),
 17822                                     Vdelayed_warnings_list);
 17823   return Qnil;
 17824 }
 17825 
 17826 static Lisp_Object
 17827 redisplay_window_0 (Lisp_Object window)
 17828 {
 17829   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17830     redisplay_window (window, false);
 17831   return Qnil;
 17832 }
 17833 
 17834 static Lisp_Object
 17835 redisplay_window_1 (Lisp_Object window)
 17836 {
 17837   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17838     redisplay_window (window, true);
 17839   return Qnil;
 17840 }
 17841 
 17842 
 17843 /***********************************************************************
 17844                       Aborting runaway redisplay
 17845  ***********************************************************************/
 17846 
 17847 /* Update the redisplay-tick count for window W, and signal an error
 17848    if the tick count is above some threshold, indicating that
 17849    redisplay of the window takes "too long".
 17850 
 17851    TICKS is the amount of ticks to add to the W's current count; zero
 17852    means to initialize the tick count to zero.
 17853 
 17854    W can be NULL if TICKS is zero: that means unconditionally
 17855    re-initialize the current tick count to zero.
 17856 
 17857    W can also be NULL if the caller doesn't know which window is being
 17858    processed by the display code.  In that case, if TICKS is non-zero,
 17859    we assume it's the last window that shows the current buffer.  */
 17860 void
 17861 update_redisplay_ticks (int ticks, struct window *w)
 17862 {
 17863   /* This keeps track of the window on which redisplay is working.  */
 17864   static struct window *cwindow;
 17865   static EMACS_INT window_ticks;
 17866 
 17867   /* We only initialize the count if this is a different window or
 17868      NULL.  Otherwise, this is a call from init_iterator for the same
 17869      window we tracked before, and we should keep the count.  */
 17870   if (!ticks && w != cwindow)
 17871     {
 17872       cwindow = w;
 17873       window_ticks = 0;
 17874     }
 17875   /* Some callers can be run in contexts unrelated to display code, so
 17876      don't abort them and don't update the tick count in those cases.  */
 17877   if ((!w && !redisplaying_p && !display_working_on_window_p)
 17878       /* We never disable redisplay of a mini-window, since that is
 17879          absolutely essential for communicating with Emacs.  */
 17880       || (w && MINI_WINDOW_P (w)))
 17881     return;
 17882 
 17883   if (ticks > 0)
 17884     window_ticks += ticks;
 17885   if (max_redisplay_ticks > 0 && window_ticks > max_redisplay_ticks)
 17886     {
 17887       /* In addition to a buffer, this could be a window (for non-leaf
 17888          windows, not expected here) or nil (for pseudo-windows like
 17889          the one used for the native tool bar).  */
 17890       Lisp_Object contents = w ? w->contents : Qnil;
 17891       char *bufname =
 17892         NILP (contents)
 17893         ? SSDATA (BVAR (current_buffer, name))
 17894         : (BUFFERP (contents)
 17895            ? SSDATA (BVAR (XBUFFER (contents), name))
 17896            : (char *) "<unknown>");
 17897 
 17898       windows_or_buffers_changed = 177;
 17899       /* scrolling_window depends too much on the glyph matrices being
 17900          correct, and we cannot guarantee that if we abort the
 17901          redisplay of this window.  */
 17902       if (w && w->desired_matrix)
 17903         w->desired_matrix->no_scrolling_p = true;
 17904       error ("Window showing buffer %s takes too long to redisplay", bufname);
 17905     }
 17906 }
 17907 
 17908 
 17909 
 17910 /* Set cursor position of W.  PT is assumed to be displayed in ROW.
 17911    DELTA and DELTA_BYTES are the numbers of characters and bytes by
 17912    which positions recorded in ROW differ from current buffer
 17913    positions.
 17914 
 17915    Return true iff cursor is on this row.  */
 17916 
 17917 static bool
 17918 set_cursor_from_row (struct window *w, struct glyph_row *row,
 17919                      struct glyph_matrix *matrix,
 17920                      ptrdiff_t delta, ptrdiff_t delta_bytes,
 17921                      int dy, int dvpos)
 17922 {
 17923   struct glyph *glyph = row->glyphs[TEXT_AREA];
 17924   struct glyph *end = glyph + row->used[TEXT_AREA];
 17925   struct glyph *cursor = NULL;
 17926   /* The last known character position in row.  */
 17927   ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 17928   int x = row->x;
 17929   ptrdiff_t pt_old = PT - delta;
 17930   ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 17931   ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 17932   struct glyph *glyph_before = glyph - 1, *glyph_after = end;
 17933   /* A glyph beyond the edge of TEXT_AREA which we should never
 17934      touch.  */
 17935   struct glyph *glyphs_end = end;
 17936   /* True means we've found a match for cursor position, but that
 17937      glyph has the avoid_cursor_p flag set.  */
 17938   bool match_with_avoid_cursor = false;
 17939   /* True means we've seen at least one glyph that came from a
 17940      display string.  */
 17941   bool string_seen = false;
 17942   /* Largest and smallest buffer positions seen so far during scan of
 17943      glyph row.  */
 17944   ptrdiff_t bpos_max = pos_before;
 17945   ptrdiff_t bpos_min = pos_after;
 17946   /* Last buffer position covered by an overlay string with an integer
 17947      `cursor' property.  */
 17948   ptrdiff_t bpos_covered = 0;
 17949   /* True means the display string on which to display the cursor
 17950      comes from a text property, not from an overlay.  */
 17951   bool string_from_text_prop = false;
 17952 
 17953   /* Don't even try doing anything if called for a mode-line or
 17954      header-line or tab-line row, since the rest of the code isn't
 17955      prepared to deal with such calamities.  */
 17956   eassert (!row->mode_line_p);
 17957   if (row->mode_line_p)
 17958     return false;
 17959 
 17960   /* Skip over glyphs not having an object at the start and the end of
 17961      the row.  These are special glyphs like truncation marks on
 17962      terminal frames.  */
 17963   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 17964     {
 17965       if (!row->reversed_p)
 17966         {
 17967           while (glyph < end
 17968                  && NILP (glyph->object)
 17969                  && glyph->charpos < 0)
 17970             {
 17971               x += glyph->pixel_width;
 17972               ++glyph;
 17973             }
 17974           while (end > glyph
 17975                  && NILP ((end - 1)->object)
 17976                  /* CHARPOS is zero for blanks and stretch glyphs
 17977                     inserted by extend_face_to_end_of_line.  */
 17978                  && (end - 1)->charpos <= 0)
 17979             --end;
 17980           glyph_before = glyph - 1;
 17981           glyph_after = end;
 17982         }
 17983       else
 17984         {
 17985           struct glyph *g;
 17986 
 17987           /* If the glyph row is reversed, we need to process it from back
 17988              to front, so swap the edge pointers.  */
 17989           glyphs_end = end = glyph - 1;
 17990           glyph += row->used[TEXT_AREA] - 1;
 17991 
 17992           while (glyph > end + 1
 17993                  && NILP (glyph->object)
 17994                  && glyph->charpos < 0)
 17995             --glyph;
 17996           if (NILP (glyph->object) && glyph->charpos < 0)
 17997             --glyph;
 17998           /* By default, in reversed rows we put the cursor on the
 17999              rightmost (first in the reading order) glyph.  */
 18000           for (x = 0, g = end + 1; g < glyph; g++)
 18001             x += g->pixel_width;
 18002           while (end < glyph
 18003                  && NILP ((end + 1)->object)
 18004                  && (end + 1)->charpos <= 0)
 18005             ++end;
 18006           glyph_before = glyph + 1;
 18007           glyph_after = end;
 18008         }
 18009     }
 18010   else if (row->reversed_p)
 18011     {
 18012       /* In R2L rows that don't display text, put the cursor on the
 18013          rightmost glyph.  Case in point: an empty last line that is
 18014          part of an R2L paragraph.  */
 18015       cursor = end - 1;
 18016       /* Avoid placing the cursor on the last glyph of the row, where
 18017          on terminal frames we hold the vertical border between
 18018          adjacent windows.  */
 18019       if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
 18020           && !WINDOW_RIGHTMOST_P (w)
 18021           && cursor == row->glyphs[LAST_AREA] - 1)
 18022         cursor--;
 18023       x = -1;   /* will be computed below, at label compute_x */
 18024     }
 18025 
 18026   /* Step 1: Try to find the glyph whose character position
 18027      corresponds to point.  If that's not possible, find 2 glyphs
 18028      whose character positions are the closest to point, one before
 18029      point, the other after it.  */
 18030   if (!row->reversed_p)
 18031     while (/* not marched to end of glyph row */
 18032            glyph < end
 18033            /* glyph was not inserted by redisplay for internal purposes */
 18034            && !NILP (glyph->object))
 18035       {
 18036         if (BUFFERP (glyph->object))
 18037           {
 18038             ptrdiff_t dpos = glyph->charpos - pt_old;
 18039 
 18040             if (glyph->charpos > bpos_max)
 18041               bpos_max = glyph->charpos;
 18042             if (glyph->charpos < bpos_min)
 18043               bpos_min = glyph->charpos;
 18044             if (!glyph->avoid_cursor_p)
 18045               {
 18046                 /* If we hit point, we've found the glyph on which to
 18047                    display the cursor.  */
 18048                 if (dpos == 0)
 18049                   {
 18050                     match_with_avoid_cursor = false;
 18051                     break;
 18052                   }
 18053                 /* See if we've found a better approximation to
 18054                    POS_BEFORE or to POS_AFTER.  */
 18055                 if (0 > dpos && dpos > pos_before - pt_old)
 18056                   {
 18057                     pos_before = glyph->charpos;
 18058                     glyph_before = glyph;
 18059                   }
 18060                 else if (0 < dpos && dpos < pos_after - pt_old)
 18061                   {
 18062                     pos_after = glyph->charpos;
 18063                     glyph_after = glyph;
 18064                   }
 18065               }
 18066             else if (dpos == 0)
 18067               match_with_avoid_cursor = true;
 18068           }
 18069         else if (STRINGP (glyph->object))
 18070           {
 18071             Lisp_Object chprop;
 18072             ptrdiff_t glyph_pos = glyph->charpos;
 18073 
 18074             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 18075                                          glyph->object);
 18076             if (!NILP (chprop))
 18077               {
 18078                 /* If the string came from a `display' text property,
 18079                    look up the buffer position of that property and
 18080                    use that position to update bpos_max, as if we
 18081                    actually saw such a position in one of the row's
 18082                    glyphs.  This helps with supporting integer values
 18083                    of `cursor' property on the display string in
 18084                    situations where most or all of the row's buffer
 18085                    text is completely covered by display properties,
 18086                    so that no glyph with valid buffer positions is
 18087                    ever seen in the row.  */
 18088                 ptrdiff_t prop_pos =
 18089                   string_buffer_position_lim (glyph->object, pos_before,
 18090                                               pos_after, false);
 18091 
 18092                 if (prop_pos >= pos_before)
 18093                   bpos_max = prop_pos;
 18094               }
 18095             if (FIXNUMP (chprop))
 18096               {
 18097                 bpos_covered = bpos_max + XFIXNUM (chprop);
 18098                 /* If the `cursor' property covers buffer positions up
 18099                    to and including point, we should display cursor on
 18100                    this glyph.  Note that, if a `cursor' property on one
 18101                    of the string's characters has an integer value, we
 18102                    will break out of the loop below _before_ we get to
 18103                    the position match above.  IOW, integer values of
 18104                    the `cursor' property override the "exact match for
 18105                    point" strategy of positioning the cursor.  */
 18106                 /* Implementation note: bpos_max == pt_old when, e.g.,
 18107                    we are in an empty line, where bpos_max is set to
 18108                    MATRIX_ROW_START_CHARPOS, see above.  */
 18109                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 18110                   {
 18111                     cursor = glyph;
 18112                     break;
 18113                   }
 18114               }
 18115 
 18116             string_seen = true;
 18117           }
 18118         x += glyph->pixel_width;
 18119         ++glyph;
 18120       }
 18121   else if (glyph > end) /* row is reversed */
 18122     while (!NILP (glyph->object))
 18123       {
 18124         if (BUFFERP (glyph->object))
 18125           {
 18126             ptrdiff_t dpos = glyph->charpos - pt_old;
 18127 
 18128             if (glyph->charpos > bpos_max)
 18129               bpos_max = glyph->charpos;
 18130             if (glyph->charpos < bpos_min)
 18131               bpos_min = glyph->charpos;
 18132             if (!glyph->avoid_cursor_p)
 18133               {
 18134                 if (dpos == 0)
 18135                   {
 18136                     match_with_avoid_cursor = false;
 18137                     break;
 18138                   }
 18139                 if (0 > dpos && dpos > pos_before - pt_old)
 18140                   {
 18141                     pos_before = glyph->charpos;
 18142                     glyph_before = glyph;
 18143                   }
 18144                 else if (0 < dpos && dpos < pos_after - pt_old)
 18145                   {
 18146                     pos_after = glyph->charpos;
 18147                     glyph_after = glyph;
 18148                   }
 18149               }
 18150             else if (dpos == 0)
 18151               match_with_avoid_cursor = true;
 18152           }
 18153         else if (STRINGP (glyph->object))
 18154           {
 18155             Lisp_Object chprop;
 18156             ptrdiff_t glyph_pos = glyph->charpos;
 18157 
 18158             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 18159                                          glyph->object);
 18160             if (!NILP (chprop))
 18161               {
 18162                 ptrdiff_t prop_pos =
 18163                   string_buffer_position_lim (glyph->object, pos_before,
 18164                                               pos_after, false);
 18165 
 18166                 if (prop_pos >= pos_before)
 18167                   bpos_max = prop_pos;
 18168               }
 18169             if (FIXNUMP (chprop))
 18170               {
 18171                 bpos_covered = bpos_max + XFIXNUM (chprop);
 18172                 /* If the `cursor' property covers buffer positions up
 18173                    to and including point, we should display cursor on
 18174                    this glyph.  */
 18175                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 18176                   {
 18177                     cursor = glyph;
 18178                     break;
 18179                   }
 18180               }
 18181             string_seen = true;
 18182           }
 18183         --glyph;
 18184         if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
 18185           {
 18186             x--;                /* can't use any pixel_width */
 18187             break;
 18188           }
 18189         x -= glyph->pixel_width;
 18190     }
 18191 
 18192   /* Step 2: If we didn't find an exact match for point, we need to
 18193      look for a proper place to put the cursor among glyphs between
 18194      GLYPH_BEFORE and GLYPH_AFTER.  */
 18195   if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18196         && BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18197       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
 18198     {
 18199       /* An empty line has a single glyph whose OBJECT is nil and
 18200          whose CHARPOS is the position of a newline on that line.
 18201          Note that on a TTY, there are more glyphs after that, which
 18202          were produced by extend_face_to_end_of_line, but their
 18203          CHARPOS is zero or negative.  */
 18204       bool empty_line_p =
 18205         ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18206          && NILP (glyph->object) && glyph->charpos > 0
 18207          /* On a TTY, continued and truncated rows also have a glyph at
 18208             their end whose OBJECT is nil and whose CHARPOS is
 18209             positive (the continuation and truncation glyphs), but such
 18210             rows are obviously not "empty".  */
 18211          && !(row->continued_p || row->truncated_on_right_p));
 18212 
 18213       if (row->ends_in_ellipsis_p && pos_after == last_pos)
 18214         {
 18215           ptrdiff_t ellipsis_pos;
 18216 
 18217           /* Scan back over the ellipsis glyphs.  */
 18218           if (!row->reversed_p)
 18219             {
 18220               ellipsis_pos = (glyph - 1)->charpos;
 18221               while (glyph > row->glyphs[TEXT_AREA]
 18222                      && (glyph - 1)->charpos == ellipsis_pos)
 18223                 glyph--, x -= glyph->pixel_width;
 18224               /* That loop always goes one position too far, including
 18225                  the glyph before the ellipsis.  So scan forward over
 18226                  that one.  */
 18227               x += glyph->pixel_width;
 18228               glyph++;
 18229             }
 18230           else  /* row is reversed */
 18231             {
 18232               ellipsis_pos = (glyph + 1)->charpos;
 18233               while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18234                      && (glyph + 1)->charpos == ellipsis_pos)
 18235                 glyph++, x += glyph->pixel_width;
 18236               x -= glyph->pixel_width;
 18237               glyph--;
 18238             }
 18239         }
 18240       else if (match_with_avoid_cursor)
 18241         {
 18242           cursor = glyph_after;
 18243           x = -1;
 18244         }
 18245       else if (string_seen)
 18246         {
 18247           int incr = row->reversed_p ? -1 : +1;
 18248 
 18249           /* Need to find the glyph that came out of a string which is
 18250              present at point.  That glyph is somewhere between
 18251              GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
 18252              positioned between POS_BEFORE and POS_AFTER in the
 18253              buffer.  */
 18254           struct glyph *start, *stop;
 18255           ptrdiff_t pos = pos_before;
 18256 
 18257           x = -1;
 18258 
 18259           /* If the row ends in a newline from a display string,
 18260              reordering could have moved the glyphs belonging to the
 18261              string out of the [GLYPH_BEFORE..GLYPH_AFTER] range.  So
 18262              in this case we extend the search to the last glyph in
 18263              the row that was not inserted by redisplay.  */
 18264           if (row->ends_in_newline_from_string_p)
 18265             {
 18266               glyph_after = end;
 18267               pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 18268             }
 18269 
 18270           /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
 18271              correspond to POS_BEFORE and POS_AFTER, respectively.  We
 18272              need START and STOP in the order that corresponds to the
 18273              row's direction as given by its reversed_p flag.  If the
 18274              directionality of characters between POS_BEFORE and
 18275              POS_AFTER is the opposite of the row's base direction,
 18276              these characters will have been reordered for display,
 18277              and we need to reverse START and STOP.  */
 18278           if (!row->reversed_p)
 18279             {
 18280               start = min (glyph_before, glyph_after);
 18281               stop = max (glyph_before, glyph_after);
 18282             }
 18283           else
 18284             {
 18285               start = max (glyph_before, glyph_after);
 18286               stop = min (glyph_before, glyph_after);
 18287             }
 18288           for (glyph = start + incr;
 18289                row->reversed_p ? glyph > stop : glyph < stop; )
 18290             {
 18291 
 18292               /* Any glyphs that come from the buffer are here because
 18293                  of bidi reordering.  Skip them, and only pay
 18294                  attention to glyphs that came from some string.  */
 18295               if (STRINGP (glyph->object))
 18296                 {
 18297                   Lisp_Object str;
 18298                   ptrdiff_t tem;
 18299                   /* If the display property covers the newline, we
 18300                      need to search for it one position farther.  */
 18301                   ptrdiff_t lim = pos_after
 18302                     + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
 18303 
 18304                   string_from_text_prop = false;
 18305                   str = glyph->object;
 18306                   tem = string_buffer_position_lim (str, pos, lim, false);
 18307                   if (tem == 0  /* from overlay */
 18308                       || pos <= tem)
 18309                     {
 18310                       /* If the string from which this glyph came is
 18311                          found in the buffer at point, or at position
 18312                          that is closer to point than pos_after, then
 18313                          we've found the glyph we've been looking for.
 18314                          If it comes from an overlay (tem == 0), and
 18315                          it has the `cursor' property on one of its
 18316                          glyphs, record that glyph as a candidate for
 18317                          displaying the cursor.  (As in the
 18318                          unidirectional version, we will display the
 18319                          cursor on the last candidate we find.)  */
 18320                       if (tem == 0
 18321                           || tem == pt_old
 18322                           || (tem - pt_old > 0 && tem < pos_after))
 18323                         {
 18324                           /* The glyphs from this string could have
 18325                              been reordered.  Find the one with the
 18326                              smallest string position.  Or there could
 18327                              be a character in the string with the
 18328                              `cursor' property, which means display
 18329                              cursor on that character's glyph.  */
 18330                           ptrdiff_t strpos = glyph->charpos;
 18331 
 18332                           if (tem)
 18333                             {
 18334                               cursor = glyph;
 18335                               string_from_text_prop = true;
 18336                             }
 18337                           for ( ;
 18338                                (row->reversed_p ? glyph > stop : glyph < stop)
 18339                                  && EQ (glyph->object, str);
 18340                                glyph += incr)
 18341                             {
 18342                               Lisp_Object cprop;
 18343                               ptrdiff_t gpos = glyph->charpos;
 18344 
 18345                               cprop = Fget_char_property (make_fixnum (gpos),
 18346                                                           Qcursor,
 18347                                                           glyph->object);
 18348                               if (!NILP (cprop))
 18349                                 {
 18350                                   cursor = glyph;
 18351                                   break;
 18352                                 }
 18353                               if (tem && glyph->charpos < strpos)
 18354                                 {
 18355                                   strpos = glyph->charpos;
 18356                                   cursor = glyph;
 18357                                 }
 18358                             }
 18359 
 18360                           if (tem == pt_old
 18361                               || (tem - pt_old > 0 && tem < pos_after))
 18362                             goto compute_x;
 18363                         }
 18364                       if (tem)
 18365                         pos = tem + 1; /* don't find previous instances */
 18366                     }
 18367                   /* This string is not what we want; skip all of the
 18368                      glyphs that came from it.  */
 18369                   while ((row->reversed_p ? glyph > stop : glyph < stop)
 18370                          && EQ (glyph->object, str))
 18371                     glyph += incr;
 18372                 }
 18373               else
 18374                 glyph += incr;
 18375             }
 18376 
 18377           /* If we reached the end of the line, and END was from a string,
 18378              the cursor is not on this line.  */
 18379           if (cursor == NULL
 18380               && (row->reversed_p ? glyph <= end : glyph >= end)
 18381               && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
 18382               && STRINGP (end->object)
 18383               && row->continued_p)
 18384             return false;
 18385         }
 18386       /* A truncated row may not include PT among its character positions.
 18387          Setting the cursor inside the scroll margin will trigger
 18388          recalculation of hscroll in hscroll_window_tree.  But if a
 18389          display string covers point, defer to the string-handling
 18390          code below to figure this out.  */
 18391       else if (row->truncated_on_left_p && pt_old < bpos_min)
 18392         {
 18393           cursor = glyph_before;
 18394           x = -1;
 18395         }
 18396       else if ((row->truncated_on_right_p && pt_old > bpos_max)
 18397                /* Zero-width characters produce no glyphs.  */
 18398                || (!empty_line_p
 18399                    && (row->reversed_p
 18400                        ? glyph_after > glyphs_end
 18401                        : glyph_after < glyphs_end)))
 18402         {
 18403           cursor = glyph_after;
 18404           x = -1;
 18405         }
 18406     }
 18407 
 18408  compute_x:
 18409   if (cursor != NULL)
 18410     glyph = cursor;
 18411   else if (glyph == glyphs_end
 18412            && pos_before == pos_after
 18413            && STRINGP ((row->reversed_p
 18414                         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18415                         : row->glyphs[TEXT_AREA])->object))
 18416     {
 18417       /* If all the glyphs of this row came from strings, put the
 18418          cursor on the first glyph of the row.  This avoids having the
 18419          cursor outside of the text area in this very rare and hard
 18420          use case.  */
 18421       glyph =
 18422         row->reversed_p
 18423         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18424         : row->glyphs[TEXT_AREA];
 18425     }
 18426   if (x < 0)
 18427     {
 18428       struct glyph *g;
 18429 
 18430       /* Need to compute x that corresponds to GLYPH.  */
 18431       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 18432         {
 18433           if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
 18434             emacs_abort ();
 18435           x += g->pixel_width;
 18436         }
 18437     }
 18438 
 18439   /* ROW could be part of a continued line, which, under bidi
 18440      reordering, might have other rows whose start and end charpos
 18441      occlude point.  Only set w->cursor if we found a better
 18442      approximation to the cursor position than we have from previously
 18443      examined candidate rows belonging to the same continued line.  */
 18444   if (/* We already have a candidate row.  */
 18445       w->cursor.vpos >= 0
 18446       /* That candidate is not the row we are processing.  */
 18447       && MATRIX_ROW (matrix, w->cursor.vpos) != row
 18448       /* Make sure cursor.vpos specifies a row whose start and end
 18449          charpos occlude point, and it is valid candidate for being a
 18450          cursor-row.  This is because some callers of this function
 18451          leave cursor.vpos at the row where the cursor was displayed
 18452          during the last redisplay cycle.  */
 18453       && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
 18454       && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18455       && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
 18456     {
 18457       struct glyph *g1
 18458         = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
 18459 
 18460       /* Don't consider glyphs that are outside TEXT_AREA.  */
 18461       if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
 18462         return false;
 18463       /* Keep the candidate whose buffer position is the closest to
 18464          point or has the `cursor' property.  */
 18465       if (/* Previous candidate is a glyph in TEXT_AREA of that row.  */
 18466           w->cursor.hpos >= 0
 18467           && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
 18468           && ((BUFFERP (g1->object)
 18469                && (g1->charpos == pt_old /* An exact match always wins.  */
 18470                    || (BUFFERP (glyph->object)
 18471                        && eabs (g1->charpos - pt_old)
 18472                        < eabs (glyph->charpos - pt_old))))
 18473               /* Previous candidate is a glyph from a string that has
 18474                  a non-nil `cursor' property.  */
 18475               || (STRINGP (g1->object)
 18476                   && (!NILP (Fget_char_property (make_fixnum (g1->charpos),
 18477                                                 Qcursor, g1->object))
 18478                       /* Previous candidate is from the same display
 18479                          string as this one, and the display string
 18480                          came from a text property.  */
 18481                       || (EQ (g1->object, glyph->object)
 18482                           && string_from_text_prop)
 18483                       /* this candidate is from newline and its
 18484                          position is not an exact match */
 18485                       || (NILP (glyph->object)
 18486                           && glyph->charpos != pt_old)))))
 18487         return false;
 18488       /* If this candidate gives an exact match, use that.  */
 18489       if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18490             /* If this candidate is a glyph created for the
 18491                terminating newline of a line, and point is on that
 18492                newline, it wins because it's an exact match.  */
 18493             || (!row->continued_p
 18494                 && NILP (glyph->object)
 18495                 && glyph->charpos == 0
 18496                 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
 18497           /* Otherwise, keep the candidate that comes from a row
 18498              spanning less buffer positions.  This may win when one or
 18499              both candidate positions are on glyphs that came from
 18500              display strings, for which we cannot compare buffer
 18501              positions.  */
 18502           && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18503              - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18504              < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
 18505         return false;
 18506     }
 18507   w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
 18508   w->cursor.x = x;
 18509   w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
 18510   w->cursor.y = row->y + dy;
 18511 
 18512   if (w == XWINDOW (selected_window))
 18513     {
 18514       if (!row->continued_p
 18515           && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 18516           && row->x == 0)
 18517         {
 18518           this_line_buffer = XBUFFER (w->contents);
 18519 
 18520           CHARPOS (this_line_start_pos)
 18521             = MATRIX_ROW_START_CHARPOS (row) + delta;
 18522           BYTEPOS (this_line_start_pos)
 18523             = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
 18524 
 18525           CHARPOS (this_line_end_pos)
 18526             = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
 18527           BYTEPOS (this_line_end_pos)
 18528             = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
 18529 
 18530           this_line_y = w->cursor.y;
 18531           this_line_pixel_height = row->height;
 18532           this_line_vpos = w->cursor.vpos;
 18533           this_line_start_x = row->x;
 18534         }
 18535       else
 18536         CHARPOS (this_line_start_pos) = 0;
 18537     }
 18538 
 18539   return true;
 18540 }
 18541 
 18542 
 18543 /* Run window scroll functions, if any, for WINDOW with new window
 18544    start STARTP.  Sets the window start of WINDOW to that position.
 18545 
 18546    We assume that the window's buffer is really current.  */
 18547 
 18548 static struct text_pos
 18549 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
 18550 {
 18551   struct window *w = XWINDOW (window);
 18552   SET_MARKER_FROM_TEXT_POS (w->start, startp);
 18553 
 18554   eassert (current_buffer == XBUFFER (w->contents));
 18555 
 18556   if (!NILP (Vwindow_scroll_functions))
 18557     {
 18558       specpdl_ref count = SPECPDL_INDEX ();
 18559       specbind (Qinhibit_quit, Qt);
 18560       safe_run_hooks_2
 18561         (Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
 18562       unbind_to (count, Qnil);
 18563       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18564       /* In case the hook functions switch buffers.  */
 18565       set_buffer_internal (XBUFFER (w->contents));
 18566     }
 18567 
 18568   return startp;
 18569 }
 18570 
 18571 
 18572 /* Make sure the line containing the cursor is fully visible.
 18573    A value of true means there is nothing to be done.
 18574    (Either the line is fully visible, or it cannot be made so,
 18575    or we cannot tell.)
 18576 
 18577    If FORCE_P, return false even if partial visible cursor row
 18578    is higher than window.
 18579 
 18580    If CURRENT_MATRIX_P, use the information from the
 18581    window's current glyph matrix; otherwise use the desired glyph
 18582    matrix.
 18583 
 18584    If JUST_TEST_USER_PREFERENCE_P, just test what the value of
 18585    make-cursor-row-fully-visible requires, don't test the actual
 18586    cursor position.  The assumption is that in that case the caller
 18587    performs the necessary testing of the cursor position.
 18588 
 18589    A value of false means the caller should do scrolling
 18590    as if point had gone off the screen.  */
 18591 
 18592 static bool
 18593 cursor_row_fully_visible_p (struct window *w, bool force_p,
 18594                             bool current_matrix_p,
 18595                             bool just_test_user_preference_p)
 18596 {
 18597   struct glyph_matrix *matrix;
 18598   struct glyph_row *row;
 18599   int window_height;
 18600   Lisp_Object mclfv_p =
 18601     buffer_local_value (Qmake_cursor_line_fully_visible, w->contents);
 18602 
 18603   /* If no local binding, use the global value.  */
 18604   if (BASE_EQ (mclfv_p, Qunbound))
 18605     mclfv_p = Vmake_cursor_line_fully_visible;
 18606   /* Follow mode sets the variable to a Lisp function in buffers that
 18607      are under Follow mode.  */
 18608   if (FUNCTIONP (mclfv_p))
 18609     {
 18610       Lisp_Object window;
 18611       XSETWINDOW (window, w);
 18612       /* Implementation note: if the function we call here signals an
 18613          error, we will NOT scroll when the cursor is partially-visible.  */
 18614       Lisp_Object val = safe_call1 (mclfv_p, window);
 18615       if (NILP (val))
 18616         return true;
 18617       else if (just_test_user_preference_p)
 18618         return false;
 18619     }
 18620   else if (NILP (mclfv_p))
 18621     return true;
 18622   else if (just_test_user_preference_p)
 18623     return false;
 18624 
 18625   /* It's not always possible to find the cursor, e.g, when a window
 18626      is full of overlay strings.  Don't do anything in that case.  */
 18627   if (w->cursor.vpos < 0)
 18628     return true;
 18629 
 18630   matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
 18631   row = MATRIX_ROW (matrix, w->cursor.vpos);
 18632 
 18633   /* If the cursor row is not partially visible, there's nothing to do.  */
 18634   if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
 18635     return true;
 18636 
 18637   /* If the row the cursor is in is taller than the window's height,
 18638      it's not clear what to do, so do nothing.  */
 18639   window_height = window_box_height (w);
 18640   if (row->height >= window_height)
 18641     {
 18642       if (!force_p || MINI_WINDOW_P (w)
 18643           || w->vscroll || w->cursor.vpos == 0)
 18644         return true;
 18645     }
 18646   return false;
 18647 }
 18648 
 18649 
 18650 /* Try scrolling PT into view in window WINDOW.  JUST_THIS_ONE_P
 18651    means only WINDOW is redisplayed in redisplay_internal.
 18652    TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
 18653    in redisplay_window to bring a partially visible line into view in
 18654    the case that only the cursor has moved.
 18655 
 18656    LAST_LINE_MISFIT should be true if we're scrolling because the
 18657    last screen line's vertical height extends past the end of the screen.
 18658 
 18659    Value is
 18660 
 18661    1    if scrolling succeeded
 18662 
 18663    0    if scrolling didn't find point.
 18664 
 18665    -1   if new fonts have been loaded so that we must interrupt
 18666    redisplay, adjust glyph matrices, and try again.  */
 18667 
 18668 enum
 18669 {
 18670   SCROLLING_SUCCESS = 1,
 18671   SCROLLING_FAILED = 0,
 18672   SCROLLING_NEED_LARGER_MATRICES = -1
 18673 };
 18674 
 18675 /* If scroll-conservatively is more than this, never recenter.
 18676 
 18677    If you change this, don't forget to update the doc string of
 18678    `scroll-conservatively' and the Emacs manual.  */
 18679 #define SCROLL_LIMIT 100
 18680 
 18681 static int
 18682 try_scrolling (Lisp_Object window, bool just_this_one_p,
 18683                intmax_t arg_scroll_conservatively, intmax_t scroll_step,
 18684                bool temp_scroll_step, bool last_line_misfit)
 18685 {
 18686   struct window *w = XWINDOW (window);
 18687   struct text_pos pos, startp;
 18688   struct it it;
 18689   int this_scroll_margin, scroll_max, rc, height;
 18690   int dy = 0, amount_to_scroll = 0;
 18691   bool scroll_down_p = false;
 18692   int extra_scroll_margin_lines = last_line_misfit;
 18693   Lisp_Object aggressive;
 18694   /* We will never try scrolling more than this number of lines.  */
 18695   int scroll_limit = SCROLL_LIMIT;
 18696   int frame_line_height = default_line_pixel_height (w);
 18697 
 18698 #ifdef GLYPH_DEBUG
 18699   debug_method_add (w, "try_scrolling");
 18700 #endif
 18701 
 18702   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18703 
 18704   this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18705 
 18706   /* Force arg_scroll_conservatively to have a reasonable value, to
 18707      avoid scrolling too far away with slow move_it_* functions.  Note
 18708      that the user can supply scroll-conservatively equal to
 18709      `most-positive-fixnum', which can be larger than INT_MAX.  */
 18710   if (arg_scroll_conservatively > scroll_limit)
 18711     {
 18712       arg_scroll_conservatively = scroll_limit + 1;
 18713       scroll_max = scroll_limit * frame_line_height;
 18714     }
 18715   else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
 18716     /* Compute how much we should try to scroll maximally to bring
 18717        point into view.  */
 18718     {
 18719       intmax_t scroll_lines_max
 18720         = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
 18721       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
 18722       scroll_max = scroll_lines * frame_line_height;
 18723     }
 18724   else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
 18725            || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
 18726     /* We're trying to scroll because of aggressive scrolling but no
 18727        scroll_step is set.  Choose an arbitrary one.  */
 18728     scroll_max = 10 * frame_line_height;
 18729   else
 18730     scroll_max = 0;
 18731 
 18732  too_near_end:
 18733 
 18734   /* Decide whether to scroll down.  */
 18735   if (PT > CHARPOS (startp))
 18736     {
 18737       int scroll_margin_y;
 18738 
 18739       /* Compute the pixel ypos of the scroll margin, then move IT to
 18740          either that ypos or PT, whichever comes first.  */
 18741       start_display (&it, w, startp);
 18742       scroll_margin_y = it.last_visible_y - partial_line_height (&it)
 18743         - this_scroll_margin
 18744         - frame_line_height * extra_scroll_margin_lines;
 18745       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 18746                   (MOVE_TO_POS | MOVE_TO_Y));
 18747 
 18748       if (PT > CHARPOS (it.current.pos))
 18749         {
 18750           int y0 = line_bottom_y (&it);
 18751           /* Compute how many pixels below window bottom to stop searching
 18752              for PT.  This avoids costly search for PT that is far away if
 18753              the user limited scrolling by a small number of lines, but
 18754              always finds PT if scroll_conservatively is set to a large
 18755              number, such as most-positive-fixnum.  */
 18756           int slack = max (scroll_max, 10 * frame_line_height);
 18757           int y_to_move = it.last_visible_y + slack;
 18758 
 18759           /* Compute the distance from the scroll margin to PT or to
 18760              the scroll limit, whichever comes first.  This should
 18761              include the height of the cursor line, to make that line
 18762              fully visible.  */
 18763           move_it_to (&it, PT, -1, y_to_move,
 18764                       -1, MOVE_TO_POS | MOVE_TO_Y);
 18765           dy = line_bottom_y (&it) - y0;
 18766 
 18767           if (dy > scroll_max)
 18768             return SCROLLING_FAILED;
 18769 
 18770           if (dy > 0)
 18771             scroll_down_p = true;
 18772         }
 18773       else if (PT == IT_CHARPOS (it)
 18774                && IT_CHARPOS (it) < ZV
 18775                && it.method == GET_FROM_STRING
 18776                && arg_scroll_conservatively > scroll_limit
 18777                && it.current_x == 0)
 18778         {
 18779           enum move_it_result skip;
 18780           int y1 = it.current_y;
 18781           int vpos;
 18782 
 18783           /* A before-string that includes newlines and is displayed
 18784              on the last visible screen line could fail us under
 18785              scroll-conservatively > 100, because we will be unable to
 18786              position the cursor on that last visible line.  Try to
 18787              recover by finding the first screen line that has some
 18788              glyphs coming from the buffer text.  */
 18789           do {
 18790             skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 18791             if (skip != MOVE_NEWLINE_OR_CR
 18792                 || IT_CHARPOS (it) != PT
 18793                 || it.method == GET_FROM_BUFFER)
 18794               break;
 18795             vpos = it.vpos;
 18796             move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
 18797           } while (it.vpos > vpos);
 18798 
 18799           dy = it.current_y - y1;
 18800 
 18801           if (dy > scroll_max)
 18802             return SCROLLING_FAILED;
 18803 
 18804           if (dy > 0)
 18805             scroll_down_p = true;
 18806         }
 18807     }
 18808 
 18809   if (scroll_down_p)
 18810     {
 18811       /* Point is in or below the bottom scroll margin, so move the
 18812          window start down.  If scrolling conservatively, move it just
 18813          enough down to make point visible.  If scroll_step is set,
 18814          move it down by scroll_step.  */
 18815       if (arg_scroll_conservatively)
 18816         amount_to_scroll
 18817           = min (max (dy, frame_line_height),
 18818                  frame_line_height * arg_scroll_conservatively);
 18819       else if (scroll_step || temp_scroll_step)
 18820         amount_to_scroll = scroll_max;
 18821       else
 18822         {
 18823           aggressive = BVAR (current_buffer, scroll_up_aggressively);
 18824           height = WINDOW_BOX_TEXT_HEIGHT (w);
 18825           if (NUMBERP (aggressive))
 18826             {
 18827               double float_amount = XFLOATINT (aggressive) * height;
 18828               int aggressive_scroll = float_amount;
 18829               if (aggressive_scroll == 0 && float_amount > 0)
 18830                 aggressive_scroll = 1;
 18831               /* Don't let point enter the scroll margin near top of
 18832                  the window.  This could happen if the value of
 18833                  scroll_up_aggressively is too large and there are
 18834                  non-zero margins, because scroll_up_aggressively
 18835                  means put point that fraction of window height
 18836                  _from_the_bottom_margin_.  */
 18837               if (aggressive_scroll + 2 * this_scroll_margin > height)
 18838                 aggressive_scroll = height - 2 * this_scroll_margin;
 18839               amount_to_scroll = dy + aggressive_scroll;
 18840             }
 18841         }
 18842 
 18843       if (amount_to_scroll <= 0)
 18844         return SCROLLING_FAILED;
 18845 
 18846       start_display (&it, w, startp);
 18847       if (arg_scroll_conservatively <= scroll_limit)
 18848         move_it_vertically (&it, amount_to_scroll);
 18849       else
 18850         {
 18851           /* Extra precision for users who set scroll-conservatively
 18852              to a large number: make sure the amount we scroll
 18853              the window start is never less than amount_to_scroll,
 18854              which was computed as distance from window bottom to
 18855              point.  This matters when lines at window top and lines
 18856              below window bottom have different height.  */
 18857           struct it it1;
 18858           void *it1data = NULL;
 18859           /* We use a temporary it1 because line_bottom_y can modify
 18860              its argument, if it moves one line down; see there.  */
 18861           int start_y;
 18862 
 18863           SAVE_IT (it1, it, it1data);
 18864           start_y = line_bottom_y (&it1);
 18865           do {
 18866             RESTORE_IT (&it, &it, it1data);
 18867             move_it_by_lines (&it, 1);
 18868             SAVE_IT (it1, it, it1data);
 18869           } while (IT_CHARPOS (it) < ZV
 18870                    && line_bottom_y (&it1) - start_y < amount_to_scroll);
 18871           bidi_unshelve_cache (it1data, true);
 18872         }
 18873 
 18874       /* If STARTP is unchanged, move it down another screen line.  */
 18875       if (IT_CHARPOS (it) == CHARPOS (startp))
 18876         move_it_by_lines (&it, 1);
 18877       startp = it.current.pos;
 18878     }
 18879   else
 18880     {
 18881       struct text_pos scroll_margin_pos = startp;
 18882       int y_offset = 0;
 18883 
 18884       /* See if point is inside the scroll margin at the top of the
 18885          window.  */
 18886       if (this_scroll_margin)
 18887         {
 18888           int y_start;
 18889 
 18890           start_display (&it, w, startp);
 18891           y_start = it.current_y;
 18892           move_it_vertically (&it, this_scroll_margin);
 18893           scroll_margin_pos = it.current.pos;
 18894           /* If we didn't move enough before hitting ZV, request
 18895              additional amount of scroll, to move point out of the
 18896              scroll margin.  */
 18897           if (IT_CHARPOS (it) == ZV
 18898               && it.current_y - y_start < this_scroll_margin)
 18899             y_offset = this_scroll_margin - (it.current_y - y_start);
 18900         }
 18901 
 18902       if (PT < CHARPOS (scroll_margin_pos))
 18903         {
 18904           /* Point is in the scroll margin at the top of the window or
 18905              above what is displayed in the window.  */
 18906           int y0, y_to_move;
 18907 
 18908           /* Compute the vertical distance from PT to the scroll
 18909              margin position.  Move as far as scroll_max allows, or
 18910              one screenful, or 10 screen lines, whichever is largest.
 18911              Give up if distance is greater than scroll_max or if we
 18912              didn't reach the scroll margin position.  */
 18913           SET_TEXT_POS (pos, PT, PT_BYTE);
 18914           start_display (&it, w, pos);
 18915           y0 = it.current_y;
 18916           y_to_move = max (it.last_visible_y,
 18917                            max (scroll_max, 10 * frame_line_height));
 18918           move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
 18919                       y_to_move, -1,
 18920                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18921           dy = it.current_y - y0;
 18922           if (dy > scroll_max
 18923               || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
 18924             return SCROLLING_FAILED;
 18925 
 18926           /* Additional scroll for when ZV was too close to point.  */
 18927           dy += y_offset;
 18928 
 18929           /* Compute new window start.  */
 18930           start_display (&it, w, startp);
 18931 
 18932           if (arg_scroll_conservatively)
 18933             amount_to_scroll
 18934               = min (max (dy, frame_line_height),
 18935                      frame_line_height * arg_scroll_conservatively);
 18936           else if (scroll_step || temp_scroll_step)
 18937             amount_to_scroll = scroll_max;
 18938           else
 18939             {
 18940               aggressive = BVAR (current_buffer, scroll_down_aggressively);
 18941               height = WINDOW_BOX_TEXT_HEIGHT (w);
 18942               if (NUMBERP (aggressive))
 18943                 {
 18944                   double float_amount = XFLOATINT (aggressive) * height;
 18945                   int aggressive_scroll = float_amount;
 18946                   if (aggressive_scroll == 0 && float_amount > 0)
 18947                     aggressive_scroll = 1;
 18948                   /* Don't let point enter the scroll margin near
 18949                      bottom of the window, if the value of
 18950                      scroll_down_aggressively happens to be too
 18951                      large.  */
 18952                   if (aggressive_scroll + 2 * this_scroll_margin > height)
 18953                     aggressive_scroll = height - 2 * this_scroll_margin;
 18954                   amount_to_scroll = dy + aggressive_scroll;
 18955                 }
 18956             }
 18957 
 18958           if (amount_to_scroll <= 0)
 18959             return SCROLLING_FAILED;
 18960 
 18961           move_it_vertically_backward (&it, amount_to_scroll);
 18962           startp = it.current.pos;
 18963         }
 18964     }
 18965 
 18966   /* Run window scroll functions.  */
 18967   startp = run_window_scroll_functions (window, startp);
 18968 
 18969   /* Display the window.  Give up if new fonts are loaded, or if point
 18970      doesn't appear.  */
 18971   if (!try_window (window, startp, 0))
 18972     rc = SCROLLING_NEED_LARGER_MATRICES;
 18973   else if (w->cursor.vpos < 0)
 18974     {
 18975       clear_glyph_matrix (w->desired_matrix);
 18976       rc = SCROLLING_FAILED;
 18977     }
 18978   else
 18979     {
 18980       /* Maybe forget recorded base line for line number display.  */
 18981       if (!just_this_one_p
 18982           || current_buffer->clip_changed
 18983           || BEG_UNCHANGED < CHARPOS (startp))
 18984         w->base_line_number = 0;
 18985 
 18986       /* If cursor ends up on a partially visible line,
 18987          treat that as being off the bottom of the screen.  */
 18988       if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
 18989                                         false, false)
 18990           /* It's possible that the cursor is on the first line of the
 18991              buffer, which is partially obscured due to a vscroll
 18992              (Bug#7537).  In that case, avoid looping forever. */
 18993           && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
 18994         {
 18995           clear_glyph_matrix (w->desired_matrix);
 18996           ++extra_scroll_margin_lines;
 18997           goto too_near_end;
 18998         }
 18999       rc = SCROLLING_SUCCESS;
 19000     }
 19001 
 19002   return rc;
 19003 }
 19004 
 19005 
 19006 /* Compute a suitable window start for window W if display of W starts
 19007    on a continuation line.  Value is true if a new window start
 19008    was computed.
 19009 
 19010    The new window start will be computed, based on W's width, starting
 19011    from the start of the continued line.  It is the start of the
 19012    screen line with the minimum distance from the old start W->start,
 19013    which is still before point (otherwise point will definitely not
 19014    be visible in the window).  */
 19015 
 19016 static bool
 19017 compute_window_start_on_continuation_line (struct window *w)
 19018 {
 19019   struct text_pos pos, start_pos, pos_before_pt;
 19020   bool window_start_changed_p = false;
 19021 
 19022   SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19023 
 19024   /* If window start is on a continuation line...  Window start may be
 19025      < BEGV in case there's invisible text at the start of the
 19026      buffer (M-x rmail, for example).  */
 19027   if (CHARPOS (start_pos) > BEGV
 19028       && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
 19029     {
 19030       struct it it;
 19031       struct glyph_row *row;
 19032 
 19033       /* Handle the case that the window start is out of range.  */
 19034       if (CHARPOS (start_pos) < BEGV)
 19035         SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
 19036       else if (CHARPOS (start_pos) > ZV)
 19037         SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
 19038 
 19039       /* Find the start of the continued line.  This should be fast
 19040          because find_newline is fast (newline cache).  */
 19041       row = w->desired_matrix->rows + window_wants_tab_line (w)
 19042                                     + window_wants_header_line (w);
 19043       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
 19044                      row, DEFAULT_FACE_ID);
 19045       reseat_at_previous_visible_line_start (&it);
 19046 
 19047       /* Give up (by not using the code in the block below) and say it
 19048          takes too much time to compute a new window start, if the
 19049          line start is "too far" away from the window start.  Also,
 19050          give up if the line start is after point, as in that case
 19051          point will not be visible with any window start we
 19052          compute.  */
 19053       if (IT_CHARPOS (it) <= PT
 19054           && (CHARPOS (start_pos) - IT_CHARPOS (it)
 19055               /* PXW: Do we need upper bounds here?  */
 19056               < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
 19057         {
 19058           int min_distance, distance;
 19059 
 19060           /* Move forward by display lines to find the new window
 19061              start.  If window width was enlarged, the new start can
 19062              be expected to be > the old start.  If window width was
 19063              decreased, the new window start will be < the old start.
 19064              So, we're looking for the display line start with the
 19065              minimum distance from the old window start.  */
 19066           pos_before_pt = pos = it.current.pos;
 19067           min_distance = DISP_INFINITY;
 19068           while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
 19069                  distance < min_distance)
 19070             {
 19071               min_distance = distance;
 19072               if (CHARPOS (pos) <= PT)
 19073                 pos_before_pt = pos;
 19074               pos = it.current.pos;
 19075               if (it.line_wrap == WORD_WRAP)
 19076                 {
 19077                   /* Under WORD_WRAP, move_it_by_lines is likely to
 19078                      overshoot and stop not at the first, but the
 19079                      second character from the left margin.  So in
 19080                      that case, we need a more tight control on the X
 19081                      coordinate of the iterator than move_it_by_lines
 19082                      promises in its contract.  The method is to first
 19083                      go to the last (rightmost) visible character of a
 19084                      line, then move to the leftmost character on the
 19085                      next line in a separate call.  */
 19086                   move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
 19087                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19088                   move_it_to (&it, ZV, 0,
 19089                               it.current_y + it.max_ascent + it.max_descent, -1,
 19090                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19091                 }
 19092               else
 19093                 move_it_by_lines (&it, 1);
 19094             }
 19095 
 19096           /* It makes very little sense to make the new window start
 19097              after point, as point won't be visible.  If that's what
 19098              the loop above finds, fall back on the candidate before
 19099              or at point that is closest to the old window start.  */
 19100           if (CHARPOS (pos) > PT)
 19101             pos = pos_before_pt;
 19102 
 19103           /* Set the window start there.  */
 19104           SET_MARKER_FROM_TEXT_POS (w->start, pos);
 19105           window_start_changed_p = true;
 19106         }
 19107     }
 19108 
 19109   return window_start_changed_p;
 19110 }
 19111 
 19112 
 19113 /* Try cursor movement in case text has not changed in window WINDOW,
 19114    with window start STARTP.  Value is
 19115 
 19116    CURSOR_MOVEMENT_SUCCESS if successful
 19117 
 19118    CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
 19119 
 19120    CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
 19121    display.  *SCROLL_STEP is set to true, under certain circumstances, if
 19122    we want to scroll as if scroll-step were set to 1.  See the code.
 19123 
 19124    CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
 19125    which case we have to abort this redisplay, and adjust matrices
 19126    first.  */
 19127 
 19128 enum
 19129 {
 19130   CURSOR_MOVEMENT_SUCCESS,
 19131   CURSOR_MOVEMENT_CANNOT_BE_USED,
 19132   CURSOR_MOVEMENT_MUST_SCROLL,
 19133   CURSOR_MOVEMENT_NEED_LARGER_MATRICES
 19134 };
 19135 
 19136 static int
 19137 try_cursor_movement (Lisp_Object window, struct text_pos startp,
 19138                      bool *scroll_step)
 19139 {
 19140   struct window *w = XWINDOW (window);
 19141   struct frame *f = XFRAME (w->frame);
 19142   int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
 19143 
 19144 #ifdef GLYPH_DEBUG
 19145   if (inhibit_try_cursor_movement)
 19146     return rc;
 19147 #endif
 19148 
 19149   /* Previously, there was a check for Lisp integer in the
 19150      if-statement below. Now, this field is converted to
 19151      ptrdiff_t, thus zero means invalid position in a buffer.  */
 19152   eassert (w->last_point > 0);
 19153   /* Likewise there was a check whether window_end_vpos is nil or larger
 19154      than the window.  Now window_end_vpos is int and so never nil, but
 19155      let's leave eassert to check whether it fits in the window.  */
 19156   eassert (!w->window_end_valid
 19157            || w->window_end_vpos < w->current_matrix->nrows);
 19158 
 19159   /* Handle case where text has not changed, only point, and it has
 19160      not moved off the frame.  */
 19161   if (/* Point may be in this window.  */
 19162       PT >= CHARPOS (startp)
 19163       /* Selective display hasn't changed.  */
 19164       && !current_buffer->clip_changed
 19165       /* Function force-mode-line-update is used to force a thorough
 19166          redisplay.  It sets either windows_or_buffers_changed or
 19167          update_mode_lines.  So don't take a shortcut here for these
 19168          cases.  */
 19169       && !update_mode_lines
 19170       && !windows_or_buffers_changed
 19171       && !f->cursor_type_changed
 19172       && NILP (Vshow_trailing_whitespace)
 19173       /* When display-line-numbers is in relative mode, moving point
 19174          requires to redraw the entire window.  */
 19175       && !EQ (Vdisplay_line_numbers, Qrelative)
 19176       && !EQ (Vdisplay_line_numbers, Qvisual)
 19177       /* When the current line number should be displayed in a
 19178          distinct face, moving point cannot be handled in optimized
 19179          way as below.  */
 19180       && !(!NILP (Vdisplay_line_numbers)
 19181            && NILP (Finternal_lisp_face_equal_p (Qline_number,
 19182                                                  Qline_number_current_line,
 19183                                                  w->frame)))
 19184       /* This code is not used for mini-buffer for the sake of the case
 19185          of redisplaying to replace an echo area message; since in
 19186          that case the mini-buffer contents per se are usually
 19187          unchanged.  This code is of no real use in the mini-buffer
 19188          since the handling of this_line_start_pos, etc., in redisplay
 19189          handles the same cases.  */
 19190       && !EQ (window, minibuf_window)
 19191       /* When overlay arrow is shown in current buffer, point movement
 19192          is no longer "simple", as it typically causes the overlay
 19193          arrow to move as well.  */
 19194       && !overlay_arrow_in_current_buffer_p ())
 19195     {
 19196       int this_scroll_margin, top_scroll_margin;
 19197       struct glyph_row *row = NULL;
 19198 
 19199 #ifdef GLYPH_DEBUG
 19200       debug_method_add (w, "cursor movement");
 19201 #endif
 19202 
 19203       this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 19204 
 19205       top_scroll_margin = this_scroll_margin;
 19206       if (window_wants_tab_line (w))
 19207         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 19208       if (window_wants_header_line (w))
 19209         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 19210 
 19211       /* Start with the row the cursor was displayed during the last
 19212          not paused redisplay.  Give up if that row is not valid.  */
 19213       if (w->last_cursor_vpos < 0
 19214           || w->last_cursor_vpos >= w->current_matrix->nrows)
 19215         rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19216       else
 19217         {
 19218           row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
 19219           /* Skip the tab-line and header-line rows, if any.  */
 19220           if (row->tab_line_p)
 19221             ++row;
 19222           if (row->mode_line_p)
 19223             ++row;
 19224           if (!row->enabled_p)
 19225             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19226         }
 19227 
 19228       if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
 19229         {
 19230           bool scroll_p = false, must_scroll = false;
 19231           int last_y = window_text_bottom_y (w) - this_scroll_margin;
 19232 
 19233           if (PT > w->last_point)
 19234             {
 19235               /* Point has moved forward.  */
 19236               while (MATRIX_ROW_END_CHARPOS (row) < PT
 19237                      && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19238                 {
 19239                   eassert (row->enabled_p);
 19240                   ++row;
 19241                 }
 19242 
 19243               /* If the end position of a row equals the start
 19244                  position of the next row, and PT is at that position,
 19245                  we would rather display cursor in the next line.  */
 19246               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19247                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19248                      && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
 19249                      && MATRIX_ROW_START_CHARPOS (row+1) >= PT
 19250                      && !cursor_row_p (row))
 19251                 ++row;
 19252 
 19253               /* If within the scroll margin, scroll.  Note that
 19254                  MATRIX_ROW_BOTTOM_Y gives the pixel position at which
 19255                  the next line would be drawn, and that
 19256                  this_scroll_margin can be zero.  */
 19257               if (MATRIX_ROW_BOTTOM_Y (row) > last_y
 19258                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19259                   /* Line is completely visible last line in window
 19260                      and PT is to be set in the next line.  */
 19261                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19262                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19263                       && !row->ends_at_zv_p
 19264                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19265                 scroll_p = true;
 19266             }
 19267           else if (PT < w->last_point)
 19268             {
 19269               /* Cursor has to be moved backward.  Note that PT >=
 19270                  CHARPOS (startp) because of the outer if-statement.  */
 19271               struct glyph_row *row0 = row;
 19272 
 19273               while (!row->mode_line_p
 19274                      && (MATRIX_ROW_START_CHARPOS (row) > PT
 19275                          || (MATRIX_ROW_START_CHARPOS (row) == PT
 19276                              && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
 19277                                  || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
 19278                                      row > w->current_matrix->rows
 19279                                      && (row-1)->ends_in_newline_from_string_p))))
 19280                      && (row->y > top_scroll_margin
 19281                          || CHARPOS (startp) == BEGV))
 19282                 {
 19283                   eassert (row->enabled_p);
 19284                   --row;
 19285                 }
 19286 
 19287               /* With bidi-reordered rows we can have buffer positions
 19288                  _decrease_ when going down by rows.  If we haven't
 19289                  found our row in the loop above, give it another try
 19290                  now going in the other direction from the original row.  */
 19291               if (!(MATRIX_ROW_START_CHARPOS (row) <= PT
 19292                     && PT <= MATRIX_ROW_END_CHARPOS (row))
 19293                   && row0->continued_p)
 19294                 {
 19295                   row = row0;
 19296                   while (MATRIX_ROW_START_CHARPOS (row) > PT
 19297                          && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19298                     {
 19299                       eassert (row->enabled_p);
 19300                       ++row;
 19301                     }
 19302                 }
 19303 
 19304               /* Consider the following case: Window starts at BEGV,
 19305                  there is invisible, intangible text at BEGV, so that
 19306                  display starts at some point START > BEGV.  It can
 19307                  happen that we are called with PT somewhere between
 19308                  BEGV and START.  Try to handle that case.  */
 19309               if (row < w->current_matrix->rows
 19310                   || row->mode_line_p)
 19311                 {
 19312                   row = w->current_matrix->rows;
 19313                   /* Skip the tab-line and header-line rows, if any.  */
 19314                   if (row->tab_line_p)
 19315                     ++row;
 19316                   if (row->mode_line_p)
 19317                     ++row;
 19318                 }
 19319 
 19320               /* Due to newlines in overlay strings, we may have to
 19321                  skip forward over overlay strings.  */
 19322               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19323                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19324                      && !cursor_row_p (row))
 19325                 ++row;
 19326 
 19327               /* If within the scroll margin, either the top one or
 19328                  the bottom one, scroll.  */
 19329               if ((row->y < top_scroll_margin
 19330                    && CHARPOS (startp) != BEGV)
 19331                   || MATRIX_ROW_BOTTOM_Y (row) > last_y
 19332                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19333                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19334                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19335                       && !row->ends_at_zv_p
 19336                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19337                 scroll_p = true;
 19338             }
 19339           else
 19340             {
 19341               /* Cursor did not move.  So don't scroll even if cursor line
 19342                  is partially visible, as it was so before.  */
 19343                  rc = CURSOR_MOVEMENT_SUCCESS;
 19344             }
 19345 
 19346           if ((PT < MATRIX_ROW_START_CHARPOS (row)
 19347                && (row == MATRIX_FIRST_TEXT_ROW (w->current_matrix)
 19348                    /* Don't give up if point is inside invisible text
 19349                       at the beginning of its glyph row.  */
 19350                    || (MATRIX_ROW_END_CHARPOS (row-1)
 19351                        == MATRIX_ROW_START_CHARPOS (row))))
 19352               || PT > MATRIX_ROW_END_CHARPOS (row))
 19353             {
 19354               /* if PT is not in the glyph row, give up.  */
 19355               rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19356               must_scroll = true;
 19357             }
 19358           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19359                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19360             {
 19361               struct glyph_row *row1;
 19362 
 19363               /* If rows are bidi-reordered and point moved, back up
 19364                  until we find a row that does not belong to a
 19365                  continuation line.  This is because we must consider
 19366                  all rows of a continued line as candidates for the
 19367                  new cursor positioning, since row start and end
 19368                  positions change non-linearly with vertical position
 19369                  in such rows.  */
 19370               /* FIXME: Revisit this when glyph ``spilling'' in
 19371                  continuation lines' rows is implemented for
 19372                  bidi-reordered rows.  */
 19373               for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 19374                    MATRIX_ROW_CONTINUATION_LINE_P (row);
 19375                    --row)
 19376                 {
 19377                   /* If we hit the beginning of the displayed portion
 19378                      without finding the first row of a continued
 19379                      line, give up.  */
 19380                   if (row <= row1)
 19381                     {
 19382                       rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19383                       break;
 19384                     }
 19385                   eassert (row->enabled_p);
 19386                 }
 19387             }
 19388           if (must_scroll)
 19389             ;
 19390           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19391               && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
 19392               /* Make sure this isn't a header line nor a tab-line by
 19393                  any chance, since then MATRIX_ROW_PARTIALLY_VISIBLE_P
 19394                  might yield true.  */
 19395               && !row->mode_line_p
 19396               && !cursor_row_fully_visible_p (w, true, true, true))
 19397             {
 19398               if (PT == MATRIX_ROW_END_CHARPOS (row)
 19399                   && !row->ends_at_zv_p
 19400                   && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 19401                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19402               else if (row->height > window_box_height (w))
 19403                 {
 19404                   /* If we end up in a partially visible line, let's
 19405                      make it fully visible, except when it's taller
 19406                      than the window, in which case we can't do much
 19407                      about it.  */
 19408                   *scroll_step = true;
 19409                   rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19410                 }
 19411               else
 19412                 {
 19413                   set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 19414                   if (!cursor_row_fully_visible_p (w, false, true, false))
 19415                     rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19416                   else
 19417                     rc = CURSOR_MOVEMENT_SUCCESS;
 19418                 }
 19419             }
 19420           else if (scroll_p)
 19421             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19422           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19423                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19424             {
 19425               /* With bidi-reordered rows, there could be more than
 19426                  one candidate row whose start and end positions
 19427                  occlude point.  We need to let set_cursor_from_row
 19428                  find the best candidate.  */
 19429               /* FIXME: Revisit this when glyph ``spilling'' in
 19430                  continuation lines' rows is implemented for
 19431                  bidi-reordered rows.  */
 19432               bool rv = false;
 19433               bool pt_invis = false;
 19434               Lisp_Object val = get_char_property_and_overlay (make_fixnum (PT),
 19435                                                                Qinvisible,
 19436                                                                Qnil, NULL);
 19437 
 19438               if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 19439                 pt_invis = true;
 19440 
 19441               do
 19442                 {
 19443                   bool at_zv_p = false, exact_match_p = false;
 19444 
 19445                   /* If point is in invisible text, we cannot assume
 19446                      it must be after row's start position, since the
 19447                      row could have invisible text at its beginning
 19448                      where point is located.  */
 19449                   if ((pt_invis || MATRIX_ROW_START_CHARPOS (row) <= PT)
 19450                       && PT <= MATRIX_ROW_END_CHARPOS (row)
 19451                       && cursor_row_p (row))
 19452                     rv |= set_cursor_from_row (w, row, w->current_matrix,
 19453                                                0, 0, 0, 0);
 19454                   /* As soon as we've found the exact match for point,
 19455                      or the first suitable row whose ends_at_zv_p flag
 19456                      is set, we are done.  */
 19457                   if (rv)
 19458                     {
 19459                       at_zv_p = MATRIX_ROW (w->current_matrix,
 19460                                             w->cursor.vpos)->ends_at_zv_p;
 19461                       if (!at_zv_p
 19462                           && w->cursor.hpos >= 0
 19463                           && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
 19464                                                                w->cursor.vpos))
 19465                         {
 19466                           struct glyph_row *candidate =
 19467                             MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 19468                           struct glyph *g =
 19469                             candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 19470                           ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
 19471 
 19472                           exact_match_p =
 19473                             (BUFFERP (g->object) && g->charpos == PT)
 19474                             || (NILP (g->object)
 19475                                 && (g->charpos == PT
 19476                                     || (g->charpos == 0 && endpos - 1 == PT)));
 19477                           /* Perhaps the point position is inside
 19478                              invisible text?  In that case, we trust
 19479                              'set_cursor_from_row' to do its job and
 19480                              find the best position for the cursor.  */
 19481                           if (!exact_match_p && pt_invis)
 19482                             exact_match_p = true;
 19483                         }
 19484                       if (at_zv_p || exact_match_p)
 19485                         {
 19486                           rc = CURSOR_MOVEMENT_SUCCESS;
 19487                           break;
 19488                         }
 19489                     }
 19490                   if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
 19491                     break;
 19492                   ++row;
 19493                 }
 19494               while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
 19495                        || row->continued_p)
 19496                       && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
 19497                      || (MATRIX_ROW_START_CHARPOS (row) == PT
 19498                          && MATRIX_ROW_BOTTOM_Y (row) < last_y));
 19499               /* If we didn't find any candidate rows, or exited the
 19500                  loop before all the candidates were examined, signal
 19501                  to the caller that this method failed.  */
 19502               if (rc != CURSOR_MOVEMENT_SUCCESS
 19503                   && !(rv
 19504                        && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 19505                        && !row->continued_p))
 19506                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19507               else if (rv)
 19508                 rc = CURSOR_MOVEMENT_SUCCESS;
 19509             }
 19510           else
 19511             {
 19512               do
 19513                 {
 19514                   if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
 19515                     {
 19516                       rc = CURSOR_MOVEMENT_SUCCESS;
 19517                       break;
 19518                     }
 19519                   ++row;
 19520                 }
 19521               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19522                      && MATRIX_ROW_START_CHARPOS (row) == PT
 19523                      && cursor_row_p (row));
 19524             }
 19525         }
 19526     }
 19527 
 19528   return rc;
 19529 }
 19530 
 19531 
 19532 void
 19533 set_vertical_scroll_bar (struct window *w)
 19534 {
 19535   ptrdiff_t start, end, whole;
 19536 
 19537   /* Calculate the start and end positions for the current window.
 19538      At some point, it would be nice to choose between scrollbars
 19539      which reflect the whole buffer size, with special markers
 19540      indicating narrowing, and scrollbars which reflect only the
 19541      visible region.
 19542 
 19543      Note that mini-buffers sometimes aren't displaying any text.  */
 19544   if (!MINI_WINDOW_P (w)
 19545       || (w == XWINDOW (minibuf_window)
 19546           && NILP (echo_area_buffer[0])))
 19547     {
 19548       struct buffer *buf = XBUFFER (w->contents);
 19549 
 19550       whole = BUF_ZV (buf) - BUF_BEGV (buf);
 19551       start = marker_position (w->start) - BUF_BEGV (buf);
 19552       end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
 19553 
 19554       /* If w->window_end_pos cannot be trusted, recompute it "the
 19555          hard way".  But don't bother to be too accurate when
 19556          long-line shortcuts are in effect.  */
 19557       if (!w->window_end_valid && !buf->long_line_optimizations_p)
 19558         {
 19559           struct it it;
 19560           struct text_pos start_pos;
 19561           struct buffer *obuf = current_buffer;
 19562           /* When we display the scroll bar of a mini-window,
 19563              current_buffer is not guaranteed to be the mini-window's
 19564              buffer, see the beginning of redisplay_window.  */
 19565           set_buffer_internal_1 (XBUFFER (w->contents));
 19566           SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19567           start_display (&it, w, start_pos);
 19568           move_it_to (&it, -1, it.last_visible_x, window_box_height (w), -1,
 19569                       MOVE_TO_X | MOVE_TO_Y);
 19570           end -= (BUF_Z (buf) - IT_CHARPOS (it)) - w->window_end_pos;
 19571           set_buffer_internal_1 (obuf);
 19572         }
 19573 
 19574       if (end < start)
 19575         end = start;
 19576       if (whole < (end - start))
 19577         whole = end - start;
 19578     }
 19579   else
 19580     start = end = whole = 0;
 19581 
 19582   /* Indicate what this scroll bar ought to be displaying now.  */
 19583   if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19584     (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19585       (w, end - start, whole, start);
 19586 }
 19587 
 19588 
 19589 void
 19590 set_horizontal_scroll_bar (struct window *w)
 19591 {
 19592   int start, end, whole, portion;
 19593 
 19594   if (!MINI_WINDOW_P (w) || EQ (w->horizontal_scroll_bar_type, Qbottom))
 19595     {
 19596       struct buffer *b = XBUFFER (w->contents);
 19597       struct buffer *old_buffer = NULL;
 19598       struct it it;
 19599       struct text_pos startp;
 19600 
 19601       if (b != current_buffer)
 19602         {
 19603           old_buffer = current_buffer;
 19604           set_buffer_internal (b);
 19605         }
 19606 
 19607       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19608       start_display (&it, w, startp);
 19609       it.last_visible_x = INT_MAX;
 19610       whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
 19611                           MOVE_TO_X | MOVE_TO_Y);
 19612       /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
 19613                           window_box_height (w), -1,
 19614                           MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 19615 
 19616       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
 19617       end = start + window_box_width (w, TEXT_AREA);
 19618       portion = end - start;
 19619       /* After enlarging a horizontally scrolled window such that it
 19620          gets at least as wide as the text it contains, make sure that
 19621          the thumb doesn't fill the entire scroll bar so we can still
 19622          drag it back to see the entire text.  */
 19623       whole = max (whole, end);
 19624 
 19625       if (it.bidi_p)
 19626         {
 19627           Lisp_Object pdir;
 19628 
 19629           pdir = Fcurrent_bidi_paragraph_direction (Qnil);
 19630           if (EQ (pdir, Qright_to_left))
 19631             {
 19632               start = whole - end;
 19633               end = start + portion;
 19634             }
 19635         }
 19636 
 19637       if (old_buffer)
 19638         set_buffer_internal (old_buffer);
 19639     }
 19640   else
 19641     start = end = whole = portion = 0;
 19642 
 19643   w->hscroll_whole = whole;
 19644 
 19645   /* Indicate what this scroll bar ought to be displaying now.  */
 19646   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19647     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19648       (w, portion, whole, start);
 19649 }
 19650 
 19651 /* Subroutine of redisplay_window, to determine whether a window-start
 19652    point STARTP of WINDOW should be rejected.  */
 19653 static bool
 19654 window_start_acceptable_p (Lisp_Object window, ptrdiff_t startp)
 19655 {
 19656   if (!make_window_start_visible)
 19657     return true;
 19658 
 19659   struct window *w = XWINDOW (window);
 19660   struct frame *f = XFRAME (w->frame);
 19661   Lisp_Object startpos = make_fixnum (startp);
 19662   Lisp_Object invprop, disp_spec;
 19663   struct text_pos ignored;
 19664 
 19665   /* Is STARTP in invisible text?  */
 19666   if ((invprop = Fget_char_property (startpos, Qinvisible, window)),
 19667       TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19668     return false;
 19669 
 19670   /* Is STARTP covered by a replacing 'display' property?  */
 19671   if (!NILP (disp_spec = Fget_char_property (startpos, Qdisplay, window))
 19672       && handle_display_spec (NULL, disp_spec, Qnil, Qnil, &ignored, startp,
 19673                               FRAME_WINDOW_P (f)) > 0)
 19674     return false;
 19675 
 19676   return true;
 19677 }
 19678 
 19679 DEFUN ("long-line-optimizations-p", Flong_line_optimizations_p, Slong_line_optimizations_p,
 19680        0, 0, 0,
 19681        doc: /* Return non-nil if long-line optimizations are in effect in current buffer.
 19682 See `long-line-threshold' and `large-hscroll-threshold' for what these
 19683 optimizations mean and when they are in effect.  */)
 19684   (void)
 19685 {
 19686   return current_buffer->long_line_optimizations_p ? Qt : Qnil;
 19687 }
 19688 
 19689 /* Redisplay leaf window WINDOW.  JUST_THIS_ONE_P means only
 19690    selected_window is redisplayed.
 19691 
 19692    We can return without actually redisplaying the window if fonts has been
 19693    changed on window's frame.  In that case, redisplay_internal will retry.
 19694 
 19695    As one of the important parts of redisplaying a window, we need to
 19696    decide whether the previous window-start position (stored in the
 19697    window's w->start marker position) is still valid, and if it isn't,
 19698    recompute it.  Some details about that:
 19699 
 19700     . The previous window-start could be in a continuation line, in
 19701       which case we need to recompute it when the window width
 19702       changes.  See compute_window_start_on_continuation_line and its
 19703       call below.
 19704 
 19705     . The text that changed since last redisplay could include the
 19706       previous window-start position.  In that case, we try to salvage
 19707       what we can from the current glyph matrix by calling
 19708       try_scrolling, which see.
 19709 
 19710     . Some Emacs command could force us to use a specific window-start
 19711       position by setting the window's force_start flag, or gently
 19712       propose doing that by setting the window's optional_new_start
 19713       flag.  In these cases, we try using the specified start point if
 19714       that succeeds (i.e. the window desired matrix is successfully
 19715       recomputed, and point location is within the window).  In case
 19716       of optional_new_start, we first check if the specified start
 19717       position is feasible, i.e. if it will allow point to be
 19718       displayed in the window.  If using the specified start point
 19719       fails, e.g., if new fonts are needed to be loaded, we abort the
 19720       redisplay cycle and leave it up to the next cycle to figure out
 19721       things.
 19722 
 19723     . Note that the window's force_start flag is sometimes set by
 19724       redisplay itself, when it decides that the previous window start
 19725       point is fine and should be kept.  Search for "goto force_start"
 19726       below to see the details.  Like the values of window-start
 19727       specified outside of redisplay, these internally-deduced values
 19728       are tested for feasibility, and ignored if found to be
 19729       unfeasible.
 19730 
 19731     . Note that the function try_window, used to completely redisplay
 19732       a window, accepts the window's start point as its argument.
 19733       This is used several times in the redisplay code to control
 19734       where the window start will be, according to user options such
 19735       as scroll-conservatively, and also to ensure the screen line
 19736       showing point will be fully (as opposed to partially) visible on
 19737       display.  */
 19738 
 19739 static void
 19740 redisplay_window (Lisp_Object window, bool just_this_one_p)
 19741 {
 19742   struct window *w = XWINDOW (window);
 19743   struct frame *f = XFRAME (w->frame);
 19744   struct buffer *buffer = XBUFFER (w->contents);
 19745   struct buffer *old = current_buffer;
 19746   struct text_pos lpoint, opoint, startp;
 19747   bool update_mode_line;
 19748   int tem;
 19749   struct it it;
 19750   /* Record it now because it's overwritten.  */
 19751   bool current_matrix_up_to_date_p = false;
 19752   bool used_current_matrix_p = false;
 19753   /* This is less strict than current_matrix_up_to_date_p.
 19754      It indicates that the buffer contents and narrowing are unchanged.  */
 19755   bool buffer_unchanged_p = false;
 19756   bool temp_scroll_step = false;
 19757   specpdl_ref count = SPECPDL_INDEX ();
 19758   int rc;
 19759   int centering_position = -1;
 19760   bool last_line_misfit = false;
 19761   ptrdiff_t beg_unchanged, end_unchanged;
 19762   int frame_line_height, margin;
 19763   bool use_desired_matrix;
 19764   void *itdata = NULL;
 19765 
 19766   SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19767   opoint = lpoint;
 19768 
 19769 #ifdef GLYPH_DEBUG
 19770   *w->desired_matrix->method = 0;
 19771 #endif
 19772 
 19773   if (!just_this_one_p && needs_no_redisplay (w))
 19774     return;
 19775 
 19776   /* Make sure that both W's markers are valid.  */
 19777   eassert (XMARKER (w->start)->buffer == buffer);
 19778   eassert (XMARKER (w->pointm)->buffer == buffer);
 19779 
 19780   reconsider_clip_changes (w);
 19781   frame_line_height = default_line_pixel_height (w);
 19782   margin = window_scroll_margin (w, MARGIN_IN_LINES);
 19783 
 19784 
 19785   /* Has the mode line to be updated?  */
 19786   update_mode_line = (w->update_mode_line
 19787                       || update_mode_lines
 19788                       || buffer->clip_changed
 19789                       || buffer->prevent_redisplay_optimizations_p);
 19790 
 19791   if (!just_this_one_p)
 19792     /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
 19793        cleverly elsewhere.  */
 19794     w->must_be_updated_p = true;
 19795 
 19796   if (MINI_WINDOW_P (w))
 19797     {
 19798       if (w == XWINDOW (echo_area_window)
 19799           && !NILP (echo_area_buffer[0]))
 19800         {
 19801           if (update_mode_line)
 19802             /* We may have to update a tty frame's menu bar or a
 19803                tool-bar.  Example `M-x C-h C-h C-g'.  */
 19804             goto finish_menu_bars;
 19805           else
 19806             /* We've already displayed the echo area glyphs in this window.  */
 19807             goto finish_scroll_bars;
 19808         }
 19809       else if ((w != XWINDOW (minibuf_window)
 19810                 || minibuf_level == 0)
 19811                /* When buffer is nonempty, redisplay window normally.  */
 19812                && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
 19813                /* Quail displays non-mini buffers in minibuffer window.
 19814                   In that case, redisplay the window normally.  */
 19815                && !NILP (Fmemq (w->contents, Vminibuffer_list)))
 19816         {
 19817           /* W is a mini-buffer window, but it's not active, so clear
 19818              it.  */
 19819           int yb = window_text_bottom_y (w);
 19820           struct glyph_row *row;
 19821           int y;
 19822 
 19823           for (y = 0, row = w->desired_matrix->rows;
 19824                y < yb;
 19825                y += row->height, ++row)
 19826             blank_row (w, row, y);
 19827           goto finish_scroll_bars;
 19828         }
 19829       else if (minibuf_level >= 1)
 19830         {
 19831           /* We could have a message produced by set-minibuffer-message
 19832              displayed in the mini-window as an overlay, so resize the
 19833              mini-window if needed.  */
 19834           resize_mini_window (w, false);
 19835         }
 19836 
 19837       clear_glyph_matrix (w->desired_matrix);
 19838     }
 19839 
 19840   /* Otherwise set up data on this window; select its buffer and point
 19841      value.  */
 19842   /* Really select the buffer, for the sake of buffer-local
 19843      variables.  */
 19844   set_buffer_internal_1 (XBUFFER (w->contents));
 19845 
 19846   current_matrix_up_to_date_p
 19847     = (w->window_end_valid
 19848        && !current_buffer->clip_changed
 19849        && !current_buffer->prevent_redisplay_optimizations_p
 19850        && !window_outdated (w)
 19851        && !composition_break_at_point
 19852        && !hscrolling_current_line_p (w));
 19853 
 19854   beg_unchanged = BEG_UNCHANGED;
 19855   end_unchanged = END_UNCHANGED;
 19856 
 19857   SET_TEXT_POS (opoint, PT, PT_BYTE);
 19858 
 19859   specbind (Qinhibit_point_motion_hooks, Qt);
 19860 
 19861   buffer_unchanged_p
 19862     = (w->window_end_valid
 19863        && !current_buffer->clip_changed
 19864        && !window_outdated (w));
 19865 
 19866   /* When windows_or_buffers_changed is non-zero, we can't rely
 19867      on the window end being valid, so set it to zero there.  */
 19868   if (windows_or_buffers_changed)
 19869     {
 19870       /* If window starts on a continuation line, maybe adjust the
 19871          window start in case the window's width changed.  */
 19872       if (XMARKER (w->start)->buffer == current_buffer)
 19873         compute_window_start_on_continuation_line (w);
 19874 
 19875       w->window_end_valid = false;
 19876       /* If so, we also can't rely on current matrix
 19877          and should not fool try_cursor_movement below.  */
 19878       current_matrix_up_to_date_p = false;
 19879     }
 19880 
 19881   /* Some sanity checks.  */
 19882   CHECK_WINDOW_END (w);
 19883   if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
 19884     emacs_abort ();
 19885   if (BYTEPOS (opoint) < CHARPOS (opoint))
 19886     emacs_abort ();
 19887 
 19888   if (mode_line_update_needed (w))
 19889     update_mode_line = true;
 19890 
 19891   /* Point refers normally to the selected window.  For any other
 19892      window, set up appropriate value.  */
 19893   if (!EQ (window, selected_window))
 19894     {
 19895       ptrdiff_t new_pt = marker_position (w->pointm);
 19896       ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
 19897 
 19898       if (new_pt < BEGV)
 19899         {
 19900           new_pt = BEGV;
 19901           new_pt_byte = BEGV_BYTE;
 19902           set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
 19903         }
 19904       else if (new_pt > (ZV - 1))
 19905         {
 19906           new_pt = ZV;
 19907           new_pt_byte = ZV_BYTE;
 19908           set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
 19909         }
 19910 
 19911       /* We don't use SET_PT so that the point-motion hooks don't run.  */
 19912       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
 19913     }
 19914 
 19915   /* If any of the character widths specified in the display table
 19916      have changed, invalidate the width run cache.  It's true that
 19917      this may be a bit late to catch such changes, but the rest of
 19918      redisplay goes (non-fatally) haywire when the display table is
 19919      changed, so why should we worry about doing any better?  */
 19920   if (current_buffer->width_run_cache
 19921       || (current_buffer->base_buffer
 19922           && current_buffer->base_buffer->width_run_cache))
 19923     {
 19924       struct Lisp_Char_Table *disptab = buffer_display_table ();
 19925 
 19926       if (! disptab_matches_widthtab
 19927           (disptab, XVECTOR (BVAR (current_buffer, width_table))))
 19928         {
 19929           struct buffer *buf = current_buffer;
 19930 
 19931           if (buf->base_buffer)
 19932             buf = buf->base_buffer;
 19933           invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
 19934           recompute_width_table (current_buffer, disptab);
 19935         }
 19936     }
 19937 
 19938   /* Check whether the buffer to be displayed contains long lines.  */
 19939   if (!NILP (Vlong_line_threshold)
 19940       && !current_buffer->long_line_optimizations_p
 19941       && (CHARS_MODIFF - UNCHANGED_MODIFIED > 8
 19942           || current_buffer->clip_changed))
 19943     {
 19944       ptrdiff_t cur, next, found, max = 0, threshold;
 19945       threshold = XFIXNUM (Vlong_line_threshold);
 19946       for (cur = BEGV; cur < ZV; cur = next)
 19947         {
 19948           next = find_newline1 (cur, CHAR_TO_BYTE (cur), 0, -1, 1,
 19949                                 &found, NULL, true);
 19950           if (next - cur > max) max = next - cur;
 19951           if (!found || max > threshold) break;
 19952         }
 19953       if (max > threshold)
 19954         current_buffer->long_line_optimizations_p = true;
 19955     }
 19956 
 19957   /* If window-start is screwed up, choose a new one.  */
 19958   if (XMARKER (w->start)->buffer != current_buffer)
 19959     goto recenter;
 19960 
 19961   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19962 
 19963   /* If someone specified a new starting point but did not insist,
 19964      check whether it can be used.  */
 19965   if ((w->optional_new_start || window_frozen_p (w))
 19966       && CHARPOS (startp) >= BEGV
 19967       && CHARPOS (startp) <= ZV)
 19968     {
 19969       ptrdiff_t it_charpos;
 19970 
 19971       w->optional_new_start = false;
 19972       if (!w->force_start)
 19973         {
 19974           start_display (&it, w, startp);
 19975           move_it_to (&it, PT, 0, it.last_visible_y, -1,
 19976                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19977           /* Record IT's position now, since line_bottom_y might
 19978              change that.  */
 19979           it_charpos = IT_CHARPOS (it);
 19980           /* Make sure we set the force_start flag only if the cursor
 19981              row will be fully visible.  Otherwise, the code under
 19982              force_start label below will try to move point back into
 19983              view, which is not what the code which sets
 19984              optional_new_start wants.  */
 19985           if (it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
 19986             {
 19987               if (it_charpos == PT)
 19988                 w->force_start = true;
 19989               /* IT may overshoot PT if text at PT is invisible.  */
 19990               else if (it_charpos > PT && CHARPOS (startp) <= PT)
 19991                 w->force_start = true;
 19992 #ifdef GLYPH_DEBUG
 19993               if (w->force_start)
 19994                 {
 19995                   if (window_frozen_p (w))
 19996                     debug_method_add (w, "set force_start from frozen window start");
 19997                   else
 19998                     debug_method_add (w, "set force_start from optional_new_start");
 19999                 }
 20000 #endif
 20001             }
 20002         }
 20003     }
 20004 
 20005  force_start:
 20006 
 20007   /* Handle case where place to start displaying has been specified,
 20008      unless the specified location is outside the accessible range.  */
 20009   if (w->force_start)
 20010     {
 20011       /* We set this later on if we have to adjust point.  */
 20012       int new_vpos = -1;
 20013 
 20014       w->force_start = false;
 20015 
 20016       /* The vscroll should be preserved in this case, since
 20017          `pixel-scroll-precision-mode' must continue working normally
 20018          when a mini-window is resized.  (bug#55312) */
 20019       if (!w->preserve_vscroll_p || !window_frozen_p (w))
 20020         w->vscroll = 0;
 20021 
 20022       w->preserve_vscroll_p = false;
 20023       w->window_end_valid = false;
 20024 
 20025       /* Forget any recorded base line for line number display.  */
 20026       if (!buffer_unchanged_p)
 20027         w->base_line_number = 0;
 20028 
 20029       /* Redisplay the mode line.  Select the buffer properly for that.
 20030          Also, run the hook window-scroll-functions
 20031          because we have scrolled.  */
 20032       /* Note, we do this after clearing force_start because
 20033          if there's an error, it is better to forget about force_start
 20034          than to get into an infinite loop calling the hook functions
 20035          and having them get more errors.  */
 20036       if (!update_mode_line
 20037           || ! NILP (Vwindow_scroll_functions))
 20038         {
 20039           update_mode_line = true;
 20040           w->update_mode_line = true;
 20041           startp = run_window_scroll_functions (window, startp);
 20042         }
 20043 
 20044       if (CHARPOS (startp) < BEGV)
 20045         SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
 20046       else if (CHARPOS (startp) > ZV)
 20047         SET_TEXT_POS (startp, ZV, ZV_BYTE);
 20048 
 20049       /* Reject the specified start location if it is invisible, and
 20050          the buffer wants it always visible.  */
 20051       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20052         goto ignore_start;
 20053 
 20054       /* Redisplay, then check if cursor has been set during the
 20055          redisplay.  Give up if new fonts were loaded.  */
 20056       /* We used to issue a CHECK_MARGINS argument to try_window here,
 20057          but this causes scrolling to fail when point begins inside
 20058          the scroll margin (bug#148) -- cyd  */
 20059       clear_glyph_matrix (w->desired_matrix);
 20060       if (!try_window (window, startp, 0))
 20061         {
 20062           w->force_start = true;
 20063           clear_glyph_matrix (w->desired_matrix);
 20064           goto need_larger_matrices;
 20065         }
 20066 
 20067       if (w->cursor.vpos < 0)
 20068         {
 20069           /* If point does not appear, try to move point so it does
 20070              appear.  The desired matrix has been built above, so we
 20071              can use it here.  First see if point is in invisible
 20072              text, and if so, move it to the first visible buffer
 20073              position past that.  */
 20074           struct glyph_row *r = NULL;
 20075           Lisp_Object invprop =
 20076             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20077                                            Qnil, NULL);
 20078 
 20079           if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 20080             {
 20081               ptrdiff_t alt_pt;
 20082               Lisp_Object invprop_end =
 20083                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20084                                                    Qnil, Qnil);
 20085 
 20086               if (FIXNATP (invprop_end))
 20087                 alt_pt = XFIXNAT (invprop_end);
 20088               else
 20089                 alt_pt = ZV;
 20090               r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
 20091                                       NULL, 0);
 20092             }
 20093           if (r)
 20094             new_vpos = MATRIX_ROW_BOTTOM_Y (r);
 20095           else  /* Give up and just move to the middle of the window.  */
 20096             new_vpos = window_box_height (w) / 2;
 20097         }
 20098 
 20099       if (!cursor_row_fully_visible_p (w, false, false, false))
 20100         {
 20101           /* Point does appear, but on a line partly visible at end of window.
 20102              Move it back to a fully-visible line.  */
 20103           new_vpos = window_box_height (w);
 20104           /* But if window_box_height suggests a Y coordinate that is
 20105              not less than we already have, that line will clearly not
 20106              be fully visible, so give up and scroll the display.
 20107              This can happen when the default face uses a font whose
 20108              dimensions are different from the frame's default
 20109              font.  */
 20110           if (new_vpos >= w->cursor.y)
 20111             {
 20112               w->cursor.vpos = -1;
 20113               clear_glyph_matrix (w->desired_matrix);
 20114               goto try_to_scroll;
 20115             }
 20116         }
 20117       else if (w->cursor.vpos >= 0)
 20118         {
 20119           /* Some people insist on not letting point enter the scroll
 20120              margin, even though this part handles windows that didn't
 20121              scroll at all.  */
 20122           int pixel_margin = margin * frame_line_height;
 20123           bool tab_line = window_wants_tab_line (w);
 20124           bool header_line = window_wants_header_line (w);
 20125 
 20126           /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
 20127              below, which finds the row to move point to, advances by
 20128              the Y coordinate of the _next_ row, see the definition of
 20129              MATRIX_ROW_BOTTOM_Y.  */
 20130           if (w->cursor.vpos < margin + tab_line + header_line)
 20131             {
 20132               w->cursor.vpos = -1;
 20133               clear_glyph_matrix (w->desired_matrix);
 20134               goto try_to_scroll;
 20135             }
 20136           else
 20137             {
 20138               int window_height = window_box_height (w);
 20139 
 20140               if (tab_line)
 20141                 window_height += CURRENT_TAB_LINE_HEIGHT (w);
 20142               if (header_line)
 20143                 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
 20144               if (w->cursor.y >= window_height - pixel_margin)
 20145                 {
 20146                   w->cursor.vpos = -1;
 20147                   clear_glyph_matrix (w->desired_matrix);
 20148                   goto try_to_scroll;
 20149                 }
 20150             }
 20151         }
 20152 
 20153       /* If we need to move point for either of the above reasons,
 20154          now actually do it.  */
 20155       if (new_vpos >= 0)
 20156         {
 20157           struct glyph_row *row;
 20158 
 20159           row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
 20160           while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos
 20161                  && !row->ends_at_zv_p)
 20162             ++row;
 20163 
 20164           TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
 20165                             MATRIX_ROW_START_BYTEPOS (row));
 20166 
 20167           if (w != XWINDOW (selected_window))
 20168             set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
 20169           else if (current_buffer == old)
 20170             SET_TEXT_POS (lpoint, PT, PT_BYTE);
 20171 
 20172           set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 20173 
 20174           /* Re-run pre-redisplay-function so it can update the region
 20175              according to the new position of point.  */
 20176           /* Other than the cursor, w's redisplay is done so we can set its
 20177              redisplay to false.  Also the buffer's redisplay can be set to
 20178              false, since propagate_buffer_redisplay should have already
 20179              propagated its info to `w' anyway.  */
 20180           w->redisplay = false;
 20181           XBUFFER (w->contents)->text->redisplay = false;
 20182           safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
 20183 
 20184           if (w->redisplay || XBUFFER (w->contents)->text->redisplay
 20185               || ((EQ (Vdisplay_line_numbers, Qrelative)
 20186                    || EQ (Vdisplay_line_numbers, Qvisual))
 20187                   && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
 20188             {
 20189               /* Either pre-redisplay-function made changes (e.g. move
 20190                  the region), or we moved point in a window that is
 20191                  under display-line-numbers = relative mode.  We need
 20192                  another round of redisplay.  */
 20193               clear_glyph_matrix (w->desired_matrix);
 20194               if (!try_window (window, startp, 0))
 20195                 goto need_larger_matrices;
 20196             }
 20197         }
 20198       if (w->cursor.vpos < 0
 20199           || !cursor_row_fully_visible_p (w, false, false, false))
 20200         {
 20201           clear_glyph_matrix (w->desired_matrix);
 20202           goto try_to_scroll;
 20203         }
 20204 
 20205 #ifdef GLYPH_DEBUG
 20206       debug_method_add (w, "forced window start");
 20207 #endif
 20208       goto done;
 20209     }
 20210 
 20211  ignore_start:
 20212 
 20213   /* Handle case where text has not changed, only point, and it has
 20214      not moved off the frame, and we are not retrying after hscroll.
 20215      (current_matrix_up_to_date_p is true when retrying.)  */
 20216   if (current_matrix_up_to_date_p
 20217       && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
 20218           rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
 20219     {
 20220       switch (rc)
 20221         {
 20222         case CURSOR_MOVEMENT_SUCCESS:
 20223           used_current_matrix_p = true;
 20224           goto done;
 20225 
 20226         case CURSOR_MOVEMENT_MUST_SCROLL:
 20227           goto try_to_scroll;
 20228 
 20229         default:
 20230           emacs_abort ();
 20231         }
 20232     }
 20233   /* If current starting point was originally the beginning of a line
 20234      but no longer is, or if the starting point is invisible but the
 20235      buffer wants it always visible, find a new starting point.  */
 20236   else if (w->start_at_line_beg
 20237            && ((CHARPOS (startp) > BEGV
 20238                 && FETCH_BYTE (BYTEPOS (startp) - 1) != '\n')
 20239                || (CHARPOS (startp) >= BEGV
 20240                    && CHARPOS (startp) <= ZV
 20241                    && !window_start_acceptable_p (window, CHARPOS (startp)))))
 20242     {
 20243 #ifdef GLYPH_DEBUG
 20244       debug_method_add (w, "recenter 1");
 20245 #endif
 20246       goto recenter;
 20247     }
 20248 
 20249   /* Try scrolling with try_window_id.  Value is > 0 if update has
 20250      been done, it is -1 if we know that the same window start will
 20251      not work.  It is 0 if unsuccessful for some other reason.  */
 20252   else if ((tem = try_window_id (w)) != 0)
 20253     {
 20254 #ifdef GLYPH_DEBUG
 20255       debug_method_add (w, "try_window_id %d", tem);
 20256 #endif
 20257 
 20258       if (f->fonts_changed)
 20259         goto need_larger_matrices;
 20260       if (tem > 0)
 20261         goto done;
 20262 
 20263       /* Otherwise try_window_id has returned -1 which means that we
 20264          don't want the alternative below this comment to execute.  */
 20265     }
 20266   else if (CHARPOS (startp) >= BEGV
 20267            && CHARPOS (startp) <= ZV
 20268            && PT >= CHARPOS (startp)
 20269            && (CHARPOS (startp) < ZV
 20270                /* Avoid starting at end of buffer.  */
 20271                || CHARPOS (startp) == BEGV
 20272                || !window_outdated (w)))
 20273     {
 20274       int d1, d2, d5, d6;
 20275       int rtop, rbot;
 20276 
 20277       /* If first window line is a continuation line, and window start
 20278          is inside the modified region, but the first change is before
 20279          current window start, we must select a new window start.
 20280 
 20281          However, if this is the result of a down-mouse event (e.g. by
 20282          extending the mouse-drag-overlay), we don't want to select a
 20283          new window start, since that would change the position under
 20284          the mouse, resulting in an unwanted mouse-movement rather
 20285          than a simple mouse-click.  */
 20286       if (!w->start_at_line_beg
 20287           && NILP (track_mouse)
 20288           && CHARPOS (startp) > BEGV
 20289           && CHARPOS (startp) > BEG + beg_unchanged
 20290           && CHARPOS (startp) <= Z - end_unchanged
 20291           /* Even if w->start_at_line_beg is nil, a new window may
 20292              start at a line_beg, since that's how set_buffer_window
 20293              sets it.  So, we need to check the return value of
 20294              compute_window_start_on_continuation_line.  (See also
 20295              bug#197).  */
 20296           && XMARKER (w->start)->buffer == current_buffer
 20297           && compute_window_start_on_continuation_line (w)
 20298           /* It doesn't make sense to force the window start like we
 20299              do at label force_start if it is already known that point
 20300              will not be fully visible in the resulting window, because
 20301              doing so will move point from its correct position
 20302              instead of scrolling the window to bring point into view.
 20303              See bug#9324.  */
 20304           && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
 20305           /* A very tall row could need more than the window height,
 20306              in which case we accept that it is partially visible.  */
 20307           && (rtop != 0) == (rbot != 0))
 20308         {
 20309           w->force_start = true;
 20310           SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20311 #ifdef GLYPH_DEBUG
 20312           debug_method_add (w, "recomputed window start in continuation line");
 20313 #endif
 20314           goto force_start;
 20315         }
 20316 
 20317       /* Don't use the same window-start if it is invisible or covered
 20318          by a replacing 'display' property and the buffer requested
 20319          the window-start to be always visible.  */
 20320       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20321         {
 20322 #ifdef GLYPH_DEBUG
 20323           debug_method_add (w, "recenter 2");
 20324 #endif
 20325           goto recenter;
 20326         }
 20327 
 20328 #ifdef GLYPH_DEBUG
 20329       debug_method_add (w, "same window start");
 20330 #endif
 20331 
 20332       /* Try to redisplay starting at same place as before.
 20333          If point has not moved off frame, accept the results.  */
 20334       if (!current_matrix_up_to_date_p
 20335           /* Don't use try_window_reusing_current_matrix in this case
 20336              because a window scroll function can have changed the
 20337              buffer.  */
 20338           || !NILP (Vwindow_scroll_functions)
 20339           || MINI_WINDOW_P (w)
 20340           || !(used_current_matrix_p
 20341                = try_window_reusing_current_matrix (w)))
 20342         {
 20343           IF_DEBUG (debug_method_add (w, "1"));
 20344           clear_glyph_matrix (w->desired_matrix);
 20345           if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
 20346             /* -1 means we need to scroll.
 20347                0 means we need new matrices, but fonts_changed
 20348                is set in that case, so we will detect it below.  */
 20349             goto try_to_scroll;
 20350         }
 20351 
 20352       if (f->fonts_changed)
 20353         goto need_larger_matrices;
 20354 
 20355       if (w->cursor.vpos >= 0)
 20356         {
 20357           if (!just_this_one_p
 20358               || current_buffer->clip_changed
 20359               || BEG_UNCHANGED < CHARPOS (startp))
 20360             /* Forget any recorded base line for line number display.  */
 20361             w->base_line_number = 0;
 20362 
 20363           if (!cursor_row_fully_visible_p (w, true, false, false))
 20364             {
 20365               clear_glyph_matrix (w->desired_matrix);
 20366               last_line_misfit = true;
 20367             }
 20368             /* Drop through and scroll.  */
 20369           else
 20370             goto done;
 20371         }
 20372       else
 20373         clear_glyph_matrix (w->desired_matrix);
 20374     }
 20375 
 20376  try_to_scroll:
 20377 
 20378   /* Redisplay the mode line.  Select the buffer properly for that.  */
 20379   if (!update_mode_line)
 20380     {
 20381       update_mode_line = true;
 20382       w->update_mode_line = true;
 20383     }
 20384 
 20385   /* Try to scroll by specified few lines.  */
 20386   if ((0 < scroll_conservatively
 20387        /* FIXME: the option is supposed to affect minibuffers, but we
 20388           test MINI_WINDOW_P, which can also catch uses of
 20389           mini-windows for displaying the echo area.  Do we need to
 20390           distinguish these two use cases?  */
 20391        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
 20392        || 0 < emacs_scroll_step
 20393        || temp_scroll_step
 20394        || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
 20395        || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
 20396       && CHARPOS (startp) >= BEGV
 20397       && CHARPOS (startp) <= ZV)
 20398     {
 20399       /* The function returns -1 if new fonts were loaded, 1 if
 20400          successful, 0 if not successful.  */
 20401       int ss = try_scrolling (window, just_this_one_p,
 20402                               ((scroll_minibuffer_conservatively
 20403                                 && MINI_WINDOW_P (w))
 20404                                ? SCROLL_LIMIT + 1
 20405                                : scroll_conservatively),
 20406                               emacs_scroll_step,
 20407                               temp_scroll_step, last_line_misfit);
 20408       switch (ss)
 20409         {
 20410         case SCROLLING_SUCCESS:
 20411           goto done;
 20412 
 20413         case SCROLLING_NEED_LARGER_MATRICES:
 20414           goto need_larger_matrices;
 20415 
 20416         case SCROLLING_FAILED:
 20417           break;
 20418 
 20419         default:
 20420           emacs_abort ();
 20421         }
 20422     }
 20423 
 20424   /* Finally, just choose a place to start which positions point
 20425      according to user preferences.  */
 20426 
 20427  recenter:
 20428 
 20429 #ifdef GLYPH_DEBUG
 20430   debug_method_add (w, "recenter");
 20431 #endif
 20432 
 20433   /* Forget any previously recorded base line for line number display.  */
 20434   if (!buffer_unchanged_p)
 20435     w->base_line_number = 0;
 20436 
 20437   /* Determine the window start relative to point.  */
 20438   init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20439   it.current_y = it.last_visible_y;
 20440   if (centering_position < 0)
 20441     {
 20442       ptrdiff_t margin_pos = CHARPOS (startp);
 20443       Lisp_Object aggressive;
 20444       bool scrolling_up;
 20445 
 20446       /* If there is a scroll margin at the top of the window, find
 20447          its character position.  */
 20448       if (margin
 20449           /* Cannot call start_display if startp is not in the
 20450              accessible region of the buffer.  This can happen when we
 20451              have just switched to a different buffer and/or changed
 20452              its restriction.  In that case, startp is initialized to
 20453              the character position 1 (BEGV) because we did not yet
 20454              have chance to display the buffer even once.  */
 20455           && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
 20456         {
 20457           struct it it1;
 20458           void *it1data = NULL;
 20459 
 20460           SAVE_IT (it1, it, it1data);
 20461           start_display (&it1, w, startp);
 20462           move_it_vertically (&it1, margin * frame_line_height);
 20463           margin_pos = IT_CHARPOS (it1);
 20464           RESTORE_IT (&it, &it, it1data);
 20465         }
 20466       scrolling_up = PT > margin_pos;
 20467       aggressive =
 20468         scrolling_up
 20469         ? BVAR (current_buffer, scroll_up_aggressively)
 20470         : BVAR (current_buffer, scroll_down_aggressively);
 20471 
 20472       if (!MINI_WINDOW_P (w)
 20473           && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
 20474         {
 20475           int pt_offset = 0;
 20476 
 20477           /* Setting scroll-conservatively overrides
 20478              scroll-*-aggressively.  */
 20479           if (!scroll_conservatively && NUMBERP (aggressive))
 20480             {
 20481               double float_amount = XFLOATINT (aggressive);
 20482 
 20483               pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
 20484               if (pt_offset == 0 && float_amount > 0)
 20485                 pt_offset = 1;
 20486               if (pt_offset && margin > 0)
 20487                 margin -= 1;
 20488             }
 20489           /* Compute how much to move the window start backward from
 20490              point so that point will be displayed where the user
 20491              wants it.  */
 20492           if (scrolling_up)
 20493             {
 20494               centering_position = it.last_visible_y;
 20495               if (pt_offset)
 20496                 centering_position -= pt_offset;
 20497               centering_position -=
 20498                 (frame_line_height * (1 + margin + last_line_misfit)
 20499                  + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 20500               /* Don't let point enter the scroll margin near top of
 20501                  the window.  */
 20502               if (centering_position < margin * frame_line_height)
 20503                 centering_position = margin * frame_line_height;
 20504             }
 20505           else
 20506             centering_position = margin * frame_line_height + pt_offset;
 20507         }
 20508       else
 20509         /* Set the window start half the height of the window backward
 20510            from point.  */
 20511         centering_position = window_box_height (w) / 2;
 20512     }
 20513   if (current_buffer->long_line_optimizations_p
 20514       && it.line_wrap == TRUNCATE)
 20515     {
 20516       /* For very long and truncated lines, go back using a simplified
 20517          method, which ignored any inaccuracies due to line-height
 20518          differences, display properties/overlays, etc.  */
 20519       int nlines = centering_position / frame_line_height;
 20520 
 20521       while (nlines-- && IT_CHARPOS (it) > BEGV)
 20522         back_to_previous_visible_line_start (&it);
 20523       reseat_1 (&it, it.current.pos, true);
 20524     }
 20525   else
 20526     move_it_vertically_backward (&it, centering_position);
 20527 
 20528   eassert (IT_CHARPOS (it) >= BEGV);
 20529 
 20530   /* The function move_it_vertically_backward may move over more
 20531      than the specified y-distance.  If it->w is small, e.g. a
 20532      mini-buffer window, we may end up in front of the window's
 20533      display area.  Start displaying at the start of the line
 20534      containing PT in this case.  */
 20535   if (it.current_y <= 0)
 20536     {
 20537       init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20538       move_it_vertically_backward (&it, 0);
 20539       it.current_y = 0;
 20540     }
 20541 
 20542   it.current_x = it.hpos = 0;
 20543 
 20544   /* Set the window start position here explicitly, to avoid an
 20545      infinite loop in case the functions in window-scroll-functions
 20546      get errors.  */
 20547   set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
 20548 
 20549   /* Run scroll hooks.  */
 20550   startp = run_window_scroll_functions (window, it.current.pos);
 20551 
 20552   /* We invoke try_window and try_window_reusing_current_matrix below,
 20553      and they manipulate the bidi cache.  Save and restore the cache
 20554      state of our iterator, so we could continue using it after that.  */
 20555   itdata = bidi_shelve_cache ();
 20556 
 20557   /* Redisplay the window.  */
 20558   use_desired_matrix = false;
 20559   if (!current_matrix_up_to_date_p
 20560       || windows_or_buffers_changed
 20561       || f->cursor_type_changed
 20562       /* Don't use try_window_reusing_current_matrix in this case
 20563          because it can have changed the buffer.  */
 20564       || !NILP (Vwindow_scroll_functions)
 20565       || !just_this_one_p
 20566       || MINI_WINDOW_P (w)
 20567       || !(used_current_matrix_p
 20568            = try_window_reusing_current_matrix (w)))
 20569     use_desired_matrix = (try_window (window, startp, 0) == 1);
 20570 
 20571   bidi_unshelve_cache (itdata, false);
 20572 
 20573   /* If new fonts have been loaded (due to fontsets), give up.  We
 20574      have to start a new redisplay since we need to re-adjust glyph
 20575      matrices.  */
 20576   if (f->fonts_changed)
 20577     goto need_larger_matrices;
 20578 
 20579   /* If cursor did not appear assume that the middle of the window is
 20580      in the first line of the window.  Do it again with the next line.
 20581      (Imagine a window of height 100, displaying two lines of height
 20582      60.  Moving back 50 from it->last_visible_y will end in the first
 20583      line.)  */
 20584   if (w->cursor.vpos < 0)
 20585     {
 20586       if (w->window_end_valid && PT >= Z - w->window_end_pos)
 20587         {
 20588           clear_glyph_matrix (w->desired_matrix);
 20589           move_it_by_lines (&it, 1);
 20590           try_window (window, it.current.pos, 0);
 20591         }
 20592       else if (PT < IT_CHARPOS (it))
 20593         {
 20594           clear_glyph_matrix (w->desired_matrix);
 20595           move_it_by_lines (&it, -1);
 20596           try_window (window, it.current.pos, 0);
 20597         }
 20598       else if (scroll_conservatively > SCROLL_LIMIT
 20599                && (it.method == GET_FROM_STRING
 20600                    || overlay_touches_p (IT_CHARPOS (it)))
 20601                && IT_CHARPOS (it) < ZV)
 20602         {
 20603           /* If the window starts with a before-string that spans more
 20604              than one screen line, using that position to display the
 20605              window might fail to bring point into the view, because
 20606              start_display will always start by displaying the string,
 20607              whereas the code above determines where to set w->start
 20608              by the buffer position of the place where it takes screen
 20609              coordinates.  Try to recover by finding the next screen
 20610              line that displays buffer text.  */
 20611           ptrdiff_t pos0 = IT_CHARPOS (it);
 20612 
 20613           clear_glyph_matrix (w->desired_matrix);
 20614           do {
 20615             move_it_by_lines (&it, 1);
 20616           } while (IT_CHARPOS (it) == pos0);
 20617           try_window (window, it.current.pos, 0);
 20618         }
 20619       else
 20620         {
 20621           /* Not much we can do about it.  */
 20622         }
 20623     }
 20624 
 20625   /* Consider the following case: Window starts at BEGV, there is
 20626      invisible, intangible text at BEGV, so that display starts at
 20627      some point START > BEGV.  It can happen that we are called with
 20628      PT somewhere between BEGV and START.  Try to handle that case,
 20629      and similar ones.  */
 20630   if (w->cursor.vpos < 0)
 20631     {
 20632       /* Prefer the desired matrix to the current matrix, if possible,
 20633          in the fallback calculations below.  This is because using
 20634          the current matrix might completely goof, e.g. if its first
 20635          row is after point.  */
 20636       struct glyph_matrix *matrix =
 20637         use_desired_matrix ? w->desired_matrix : w->current_matrix;
 20638       /* First, try locating the proper glyph row for PT.  */
 20639       struct glyph_row *row =
 20640         row_containing_pos (w, PT, matrix->rows, NULL, 0);
 20641 
 20642       /* Sometimes point is at the beginning of invisible text that is
 20643          before the 1st character displayed in the row.  In that case,
 20644          row_containing_pos fails to find the row, because no glyphs
 20645          with appropriate buffer positions are present in the row.
 20646          Therefore, we next try to find the row which shows the 1st
 20647          position after the invisible text.  */
 20648       if (!row)
 20649         {
 20650           Lisp_Object val =
 20651             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20652                                            Qnil, NULL);
 20653 
 20654           if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 20655             {
 20656               ptrdiff_t alt_pos;
 20657               Lisp_Object invis_end =
 20658                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20659                                                    Qnil, Qnil);
 20660 
 20661               if (FIXNATP (invis_end))
 20662                 alt_pos = XFIXNAT (invis_end);
 20663               else
 20664                 alt_pos = ZV;
 20665               row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
 20666             }
 20667         }
 20668       /* Finally, fall back on the first row of the window after the
 20669          tab-line and header line (if any).  This is slightly better
 20670          than not displaying the cursor at all.  */
 20671       if (!row)
 20672         {
 20673           row = matrix->rows;
 20674           /* Skip the tab-line and header-line rows, if any.  */
 20675           if (row->tab_line_p)
 20676             ++row;
 20677           if (row->mode_line_p)
 20678             ++row;
 20679         }
 20680       set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
 20681     }
 20682 
 20683   if (!cursor_row_fully_visible_p (w, false, false, false))
 20684     {
 20685       /* If vscroll is enabled, disable it and try again.  */
 20686       if (w->vscroll)
 20687         {
 20688           w->vscroll = 0;
 20689           clear_glyph_matrix (w->desired_matrix);
 20690           goto recenter;
 20691         }
 20692 
 20693       /* Users who set scroll-conservatively to a large number want
 20694          point just above/below the scroll margin.  If we ended up
 20695          with point's row partially visible, move the window start to
 20696          make that row fully visible and out of the margin.  */
 20697       if (scroll_conservatively > SCROLL_LIMIT)
 20698         {
 20699           int window_total_lines
 20700             = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
 20701           bool move_down = w->cursor.vpos >= window_total_lines / 2;
 20702 
 20703           move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
 20704           clear_glyph_matrix (w->desired_matrix);
 20705           if (1 == try_window (window, it.current.pos,
 20706                                TRY_WINDOW_CHECK_MARGINS))
 20707             goto done;
 20708         }
 20709 
 20710       /* If centering point failed to make the whole line visible,
 20711          put point at the top instead.  That has to make the whole line
 20712          visible, if it can be done.  */
 20713       if (centering_position == 0)
 20714         goto done;
 20715 
 20716       clear_glyph_matrix (w->desired_matrix);
 20717       centering_position = 0;
 20718       goto recenter;
 20719     }
 20720 
 20721  done:
 20722 
 20723   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20724   w->start_at_line_beg = (CHARPOS (startp) == BEGV
 20725                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
 20726 
 20727   /* Display the mode line, header line, and tab-line, if we must.  */
 20728   if ((update_mode_line
 20729        /* If window not full width, must redo its mode line
 20730           if (a) the window to its side is being redone and
 20731           (b) we do a frame-based redisplay.  This is a consequence
 20732           of how inverted lines are drawn in frame-based redisplay.  */
 20733        || (!just_this_one_p
 20734            && !FRAME_WINDOW_P (f)
 20735            && !WINDOW_FULL_WIDTH_P (w))
 20736        /* Line number to display.  */
 20737        || w->base_line_pos > 0
 20738        /* Column number is displayed and different from the one displayed.  */
 20739        || (w->column_number_displayed != -1
 20740            && (w->column_number_displayed != current_column ())))
 20741       /* This means that the window has a mode line.  */
 20742       && (window_wants_mode_line (w)
 20743           || window_wants_header_line (w)
 20744           || window_wants_tab_line (w)))
 20745     {
 20746       specpdl_ref count1 = SPECPDL_INDEX ();
 20747 
 20748       specbind (Qinhibit_quit, Qt);
 20749       display_mode_lines (w);
 20750       unbind_to (count1, Qnil);
 20751 
 20752       /* If mode line height has changed, arrange for a thorough
 20753          immediate redisplay using the correct mode line height.  */
 20754       if (window_wants_mode_line (w)
 20755           && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
 20756         {
 20757           f->fonts_changed = true;
 20758           w->mode_line_height = -1;
 20759           MATRIX_MODE_LINE_ROW (w->current_matrix)->height
 20760             = DESIRED_MODE_LINE_HEIGHT (w);
 20761         }
 20762 
 20763       /* If tab line height has changed, arrange for a thorough
 20764          immediate redisplay using the correct tab line height.  */
 20765       if (window_wants_tab_line (w)
 20766           && CURRENT_TAB_LINE_HEIGHT (w) != DESIRED_TAB_LINE_HEIGHT (w))
 20767         {
 20768           f->fonts_changed = true;
 20769           w->tab_line_height = -1;
 20770           MATRIX_TAB_LINE_ROW (w->current_matrix)->height
 20771             = DESIRED_TAB_LINE_HEIGHT (w);
 20772         }
 20773 
 20774       /* If header line height has changed, arrange for a thorough
 20775          immediate redisplay using the correct header line height.  */
 20776       if (window_wants_header_line (w)
 20777           && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
 20778         {
 20779           f->fonts_changed = true;
 20780           w->header_line_height = -1;
 20781           MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
 20782             = DESIRED_HEADER_LINE_HEIGHT (w);
 20783         }
 20784 
 20785       if (f->fonts_changed)
 20786         goto need_larger_matrices;
 20787     }
 20788 
 20789   if (!line_number_displayed && w->base_line_pos != -1)
 20790     {
 20791       w->base_line_pos = 0;
 20792       w->base_line_number = 0;
 20793     }
 20794 
 20795  finish_menu_bars:
 20796 
 20797   /* When we reach a frame's selected window, redo the frame's menu
 20798      bar, tool bar, tab-bar, and the frame's title.  */
 20799   if (update_mode_line
 20800       && EQ (FRAME_SELECTED_WINDOW (f), window))
 20801     {
 20802       bool redisplay_menu_p;
 20803 
 20804       if (FRAME_WINDOW_P (f))
 20805         {
 20806 #ifdef HAVE_EXT_MENU_BAR
 20807           redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
 20808 #else
 20809           redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20810 #endif
 20811         }
 20812       else
 20813         redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20814 
 20815       if (redisplay_menu_p)
 20816         display_menu_bar (w);
 20817 
 20818 #ifdef HAVE_WINDOW_SYSTEM
 20819       if (FRAME_WINDOW_P (f))
 20820         {
 20821           if (WINDOWP (f->tab_bar_window)
 20822               && (FRAME_TAB_BAR_LINES (f) > 0
 20823                   || !NILP (Vauto_resize_tab_bars))
 20824               && redisplay_tab_bar (f))
 20825             ignore_mouse_drag_p = true;
 20826 
 20827 #ifdef HAVE_EXT_TOOL_BAR
 20828           if (FRAME_EXTERNAL_TOOL_BAR (f))
 20829             update_frame_tool_bar (f);
 20830 #else
 20831           if (WINDOWP (f->tool_bar_window)
 20832               && (FRAME_TOOL_BAR_LINES (f) > 0
 20833                   || !NILP (Vauto_resize_tool_bars))
 20834               && redisplay_tool_bar (f))
 20835             ignore_mouse_drag_p = true;
 20836 #endif
 20837         }
 20838       else
 20839         {
 20840           if ((FRAME_TAB_BAR_LINES (f) > 0))
 20841             display_tab_bar (w);
 20842         }
 20843 
 20844       gui_consider_frame_title (w->frame);
 20845 #else
 20846       if ((FRAME_TAB_BAR_LINES (f) > 0))
 20847         display_tab_bar (w);
 20848 #endif
 20849     }
 20850 
 20851 #ifdef HAVE_WINDOW_SYSTEM
 20852   if (FRAME_WINDOW_P (f)
 20853       && update_window_fringes (w, (just_this_one_p
 20854                                     || (!used_current_matrix_p && !overlay_arrow_seen)
 20855                                     || w->pseudo_window_p)))
 20856     {
 20857       update_begin (f);
 20858       block_input ();
 20859       if (draw_window_fringes (w, true))
 20860         {
 20861           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 20862             gui_draw_right_divider (w);
 20863           else
 20864             gui_draw_vertical_border (w);
 20865         }
 20866       unblock_input ();
 20867       update_end (f);
 20868     }
 20869 
 20870   if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 20871     gui_draw_bottom_divider (w);
 20872 #endif /* HAVE_WINDOW_SYSTEM */
 20873 
 20874   /* We go to this label, with fonts_changed set, if it is
 20875      necessary to try again using larger glyph matrices.
 20876      We have to redeem the scroll bar even in this case,
 20877      because the loop in redisplay_internal expects that.  */
 20878  need_larger_matrices:
 20879   ;
 20880  finish_scroll_bars:
 20881 
 20882    if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20883     {
 20884       if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
 20885         /* Set the thumb's position and size.  */
 20886         set_vertical_scroll_bar (w);
 20887 
 20888       if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20889         /* Set the thumb's position and size.  */
 20890         set_horizontal_scroll_bar (w);
 20891 
 20892       /* Note that we actually used the scroll bar attached to this
 20893          window, so it shouldn't be deleted at the end of redisplay.  */
 20894       if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
 20895         (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
 20896     }
 20897 
 20898   /* Restore current_buffer and value of point in it.  The window
 20899      update may have changed the buffer, so first make sure `opoint'
 20900      is still valid (Bug#6177).  */
 20901   if (CHARPOS (opoint) < BEGV)
 20902     TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
 20903   else if (CHARPOS (opoint) > ZV)
 20904     TEMP_SET_PT_BOTH (Z, Z_BYTE);
 20905   else
 20906     TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
 20907 
 20908   set_buffer_internal_1 (old);
 20909   /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
 20910      shorter.  This can be caused by log truncation in *Messages*.  */
 20911   if (CHARPOS (lpoint) <= ZV)
 20912     TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 20913 
 20914   unbind_to (count, Qnil);
 20915 }
 20916 
 20917 
 20918 /* Build the complete desired matrix of WINDOW with a window start
 20919    buffer position POS.
 20920 
 20921    Value is 1 if successful.  It is zero if fonts were loaded during
 20922    redisplay or the dimensions of the desired matrix were found
 20923    insufficient, which makes re-adjusting glyph matrices necessary.
 20924    Value is -1 if point would appear in the scroll margins.  (We check
 20925    the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is unset in
 20926    FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is set in
 20927    FLAGS.)
 20928 
 20929    Note that 'x-show-tip' invokes this function in a special way, and
 20930    in that case the return value of zero doesn't necessarily mean the
 20931    glyph matrices need to be re-adjusted, if the entire text of the
 20932    tooltip was processed and has its glyphs in the matrix's glyph
 20933    rows, i.e. if the dimensions of the matrix were found insufficient
 20934    while producing empty glyph rows beyond ZV.  */
 20935 
 20936 int
 20937 try_window (Lisp_Object window, struct text_pos pos, int flags)
 20938 {
 20939   struct window *w = XWINDOW (window);
 20940   struct it it;
 20941   struct glyph_row *last_text_row = NULL;
 20942   struct frame *f = XFRAME (w->frame);
 20943   int cursor_vpos = w->cursor.vpos;
 20944 
 20945   /* Make POS the new window start.  */
 20946   set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
 20947 
 20948   /* Mark cursor position as unknown.  No overlay arrow seen.  */
 20949   w->cursor.vpos = -1;
 20950   overlay_arrow_seen = false;
 20951 
 20952   /* Initialize iterator and info to start at POS.  */
 20953   start_display (&it, w, pos);
 20954   it.glyph_row->reversed_p = false;
 20955 
 20956   /* Display all lines of W.  */
 20957   while (it.current_y < it.last_visible_y)
 20958     {
 20959       int last_row_scale = it.w->nrows_scale_factor;
 20960       int last_col_scale = it.w->ncols_scale_factor;
 20961       if (display_line (&it, cursor_vpos))
 20962         last_text_row = it.glyph_row - 1;
 20963       if (f->fonts_changed
 20964           && !((flags & TRY_WINDOW_IGNORE_FONTS_CHANGE)
 20965                /* If the matrix dimensions are insufficient, we _must_
 20966                   fail and let dispnew.c reallocate the matrix.  */
 20967                && last_row_scale == it.w->nrows_scale_factor
 20968                && last_col_scale == it.w->ncols_scale_factor))
 20969         return 0;
 20970     }
 20971 
 20972   /* Save the character position of 'it' before we call
 20973      'start_display' again.  */
 20974   ptrdiff_t it_charpos = IT_CHARPOS (it);
 20975 
 20976   /* Don't let the cursor end in the scroll margins.  However, when
 20977      the window is vscrolled, we leave it to vscroll to handle the
 20978      margins, see window_scroll_pixel_based.  */
 20979   if ((flags & TRY_WINDOW_CHECK_MARGINS)
 20980       && w->vscroll == 0
 20981       && !MINI_WINDOW_P (w))
 20982     {
 20983       int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 20984       int bot_scroll_margin = top_scroll_margin;
 20985       if (window_wants_header_line (w))
 20986         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 20987       if (window_wants_tab_line (w))
 20988         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 20989       start_display (&it, w, pos);
 20990 
 20991       if ((w->cursor.y >= 0
 20992            && w->cursor.y < top_scroll_margin
 20993            && CHARPOS (pos) > BEGV)
 20994           /* rms: considering make_cursor_line_fully_visible_p here
 20995              seems to give wrong results.  We don't want to recenter
 20996              when the last line is partly visible, we want to allow
 20997              that case to be handled in the usual way.  */
 20998           || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
 20999                             - bot_scroll_margin - 1))
 21000         {
 21001           w->cursor.vpos = -1;
 21002           clear_glyph_matrix (w->desired_matrix);
 21003           return -1;
 21004         }
 21005     }
 21006 
 21007   /* If bottom moved off end of frame, change mode line percentage.  */
 21008   if (w->window_end_pos <= 0 && Z != it_charpos)
 21009     w->update_mode_line = true;
 21010 
 21011   /* Set window_end_pos to the offset of the last character displayed
 21012      on the window from the end of current_buffer.  Set
 21013      window_end_vpos to its row number.  */
 21014   if (last_text_row)
 21015     {
 21016       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
 21017       adjust_window_ends (w, last_text_row, false);
 21018       eassert
 21019         (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
 21020                                                  w->window_end_vpos)));
 21021     }
 21022   else
 21023     {
 21024       w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21025       w->window_end_pos = Z - ZV;
 21026       w->window_end_vpos = 0;
 21027     }
 21028 
 21029   /* But that is not valid info until redisplay finishes.  */
 21030   w->window_end_valid = false;
 21031   return 1;
 21032 }
 21033 
 21034 
 21035 
 21036 /************************************************************************
 21037     Window redisplay reusing current matrix when buffer has not changed
 21038  ************************************************************************/
 21039 
 21040 /* Try redisplay of window W showing an unchanged buffer with a
 21041    different window start than the last time it was displayed by
 21042    reusing its current matrix.  Value is true if successful.
 21043    W->start is the new window start.  */
 21044 
 21045 static bool
 21046 try_window_reusing_current_matrix (struct window *w)
 21047 {
 21048   struct frame *f = XFRAME (w->frame);
 21049   struct glyph_row *bottom_row;
 21050   struct it it;
 21051   struct run run;
 21052   struct text_pos start, new_start;
 21053   int nrows_scrolled, i;
 21054   struct glyph_row *last_text_row;
 21055   struct glyph_row *last_reused_text_row;
 21056   struct glyph_row *start_row;
 21057   int start_vpos, min_y, max_y;
 21058 
 21059 #ifdef GLYPH_DEBUG
 21060   if (inhibit_try_window_reusing)
 21061     return false;
 21062 #endif
 21063 
 21064   if (/* This function doesn't handle terminal frames.  */
 21065       !FRAME_WINDOW_P (f)
 21066       /* Don't try to reuse the display if windows have been split
 21067          or such.  */
 21068       || windows_or_buffers_changed
 21069       || f->cursor_type_changed
 21070       /* This function cannot handle buffers where the overlay arrow
 21071          is shown on the fringes, because if the arrow position
 21072          changes, we cannot just reuse the current matrix.  */
 21073       || overlay_arrow_in_current_buffer_p ())
 21074     return false;
 21075 
 21076   /* Can't do this if showing trailing whitespace.  */
 21077   if (!NILP (Vshow_trailing_whitespace))
 21078     return false;
 21079 
 21080   /* If top-line visibility has changed, give up.  */
 21081   if (window_wants_tab_line (w)
 21082       != MATRIX_TAB_LINE_ROW (w->current_matrix)->mode_line_p)
 21083     return false;
 21084 
 21085   /* If top-line visibility has changed, give up.  */
 21086   if (window_wants_header_line (w)
 21087       != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
 21088     return false;
 21089 
 21090   /* Give up if old or new display is scrolled vertically.  We could
 21091      make this function handle this, but right now it doesn't.  */
 21092   start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21093   if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
 21094     return false;
 21095 
 21096   /* Clear the desired matrix for the display below.  */
 21097   clear_glyph_matrix (w->desired_matrix);
 21098 
 21099   /* Give up if line numbers are being displayed, because reusing the
 21100      current matrix might use the wrong width for line-number
 21101      display.  */
 21102   if (!NILP (Vdisplay_line_numbers))
 21103     return false;
 21104 
 21105   /* Can't scroll the display of w32 GUI frames when position of point
 21106      is indicated by the system caret, because scrolling the display
 21107      will then "copy" the pixels used by the caret.  */
 21108 #ifdef HAVE_NTGUI
 21109   if (w32_use_visible_system_caret)
 21110     return false;
 21111 #endif
 21112 
 21113   /* The variable new_start now holds the new window start.  The old
 21114      start `start' can be determined from the current matrix.  */
 21115   SET_TEXT_POS_FROM_MARKER (new_start, w->start);
 21116   start = start_row->minpos;
 21117   start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 21118 
 21119   if (CHARPOS (new_start) <= CHARPOS (start))
 21120     {
 21121       /* Don't use this method if the display starts with an ellipsis
 21122          displayed for invisible text.  It's not easy to handle that case
 21123          below, and it's certainly not worth the effort since this is
 21124          not a frequent case.  */
 21125       if (in_ellipses_for_invisible_text_p (&start_row->start, w))
 21126         return false;
 21127 
 21128       IF_DEBUG (debug_method_add (w, "twu1"));
 21129 
 21130       /* Display up to a row that can be reused.  The variable
 21131          last_text_row is set to the last row displayed that displays
 21132          text.  Note that it.vpos == 0 if or if not there is a
 21133          header-line; it's not the same as the MATRIX_ROW_VPOS!  */
 21134       start_display (&it, w, new_start);
 21135       w->cursor.vpos = -1;
 21136       last_text_row = last_reused_text_row = NULL;
 21137 
 21138       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21139         {
 21140           /* If we have reached into the characters in the START row,
 21141              that means the line boundaries have changed.  So we
 21142              can't start copying with the row START.  Maybe it will
 21143              work to start copying with the following row.  */
 21144           while (IT_CHARPOS (it) > CHARPOS (start))
 21145             {
 21146               /* Advance to the next row as the "start".  */
 21147               start_row++;
 21148               start = start_row->minpos;
 21149               /* If there are no more rows to try, or just one, give up.  */
 21150               if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
 21151                   || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
 21152                   || CHARPOS (start) == ZV)
 21153                 {
 21154                   clear_glyph_matrix (w->desired_matrix);
 21155                   return false;
 21156                 }
 21157 
 21158               start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 21159             }
 21160           /* If we have reached alignment, we can copy the rest of the
 21161              rows.  */
 21162           if (IT_CHARPOS (it) == CHARPOS (start)
 21163               /* Don't accept "alignment" inside a display vector,
 21164                  since start_row could have started in the middle of
 21165                  that same display vector (thus their character
 21166                  positions match), and we have no way of telling if
 21167                  that is the case.  */
 21168               && it.current.dpvec_index < 0)
 21169             break;
 21170 
 21171           it.glyph_row->reversed_p = false;
 21172           if (display_line (&it, -1))
 21173             last_text_row = it.glyph_row - 1;
 21174 
 21175         }
 21176 
 21177       /* A value of current_y < last_visible_y means that we stopped
 21178          at the previous window start, which in turn means that we
 21179          have at least one reusable row.  */
 21180       if (it.current_y < it.last_visible_y)
 21181         {
 21182           struct glyph_row *row;
 21183 
 21184           /* IT.vpos always starts from 0; it counts text lines.  */
 21185           nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
 21186 
 21187           /* Find PT if not already found in the lines displayed.  */
 21188           if (w->cursor.vpos < 0)
 21189             {
 21190               int dy = it.current_y - start_row->y;
 21191 
 21192               row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21193               row = row_containing_pos (w, PT, row, NULL, dy);
 21194               if (row)
 21195                 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
 21196                                      dy, nrows_scrolled);
 21197               else
 21198                 {
 21199                   clear_glyph_matrix (w->desired_matrix);
 21200                   return false;
 21201                 }
 21202             }
 21203 
 21204           /* Scroll the display.  Do it before the current matrix is
 21205              changed.  The problem here is that update has not yet
 21206              run, i.e. part of the current matrix is not up to date.
 21207              scroll_run_hook will clear the cursor, and use the
 21208              current matrix to get the height of the row the cursor is
 21209              in.  */
 21210           run.current_y = start_row->y;
 21211           run.desired_y = it.current_y;
 21212           run.height = it.last_visible_y - it.current_y;
 21213 
 21214           if (run.height > 0 && run.current_y != run.desired_y)
 21215             {
 21216 #ifdef HAVE_WINDOW_SYSTEM
 21217               update_begin (f);
 21218               gui_update_window_begin (w);
 21219               FRAME_RIF (f)->clear_window_mouse_face (w);
 21220               FRAME_RIF (f)->scroll_run_hook (w, &run);
 21221               gui_update_window_end (w, false, false);
 21222               update_end (f);
 21223 #endif
 21224             }
 21225 
 21226           /* Shift current matrix down by nrows_scrolled lines.  */
 21227           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21228           rotate_matrix (w->current_matrix,
 21229                          start_vpos,
 21230                          MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21231                          nrows_scrolled);
 21232 
 21233           /* Disable lines that must be updated.  */
 21234           for (i = 0; i < nrows_scrolled; ++i)
 21235             (start_row + i)->enabled_p = false;
 21236 
 21237           /* Re-compute Y positions.  */
 21238           min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21239           max_y = it.last_visible_y;
 21240           for (row = start_row + nrows_scrolled;
 21241                row < bottom_row;
 21242                ++row)
 21243             {
 21244               row->y = it.current_y;
 21245               row->visible_height = row->height;
 21246 
 21247               if (row->y < min_y)
 21248                 row->visible_height -= min_y - row->y;
 21249               if (row->y + row->height > max_y)
 21250                 row->visible_height -= row->y + row->height - max_y;
 21251               if (row->fringe_bitmap_periodic_p)
 21252                 row->redraw_fringe_bitmaps_p = true;
 21253 
 21254               it.current_y += row->height;
 21255 
 21256               if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21257                 last_reused_text_row = row;
 21258               if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
 21259                 break;
 21260             }
 21261 
 21262           /* Disable lines in the current matrix which are now
 21263              below the window.  */
 21264           for (++row; row < bottom_row; ++row)
 21265             row->enabled_p = row->mode_line_p = row->tab_line_p = false;
 21266         }
 21267 
 21268       /* Update window_end_pos etc.; last_reused_text_row is the last
 21269          reused row from the current matrix containing text, if any.
 21270          The value of last_text_row is the last displayed line
 21271          containing text.  */
 21272       if (last_reused_text_row)
 21273         adjust_window_ends (w, last_reused_text_row, true);
 21274       else if (last_text_row)
 21275         adjust_window_ends (w, last_text_row, false);
 21276       else
 21277         {
 21278           /* This window must be completely empty.  */
 21279           w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21280           w->window_end_pos = Z - ZV;
 21281           w->window_end_vpos = 0;
 21282         }
 21283       w->window_end_valid = false;
 21284 
 21285       /* Update hint: don't try scrolling again in update_window.  */
 21286       w->desired_matrix->no_scrolling_p = true;
 21287 
 21288 #ifdef GLYPH_DEBUG
 21289       debug_method_add (w, "try_window_reusing_current_matrix 1");
 21290 #endif
 21291       return true;
 21292     }
 21293   else if (CHARPOS (new_start) > CHARPOS (start))
 21294     {
 21295       struct glyph_row *pt_row, *row;
 21296       struct glyph_row *first_reusable_row;
 21297       struct glyph_row *first_row_to_display;
 21298       int dy;
 21299       int yb = window_text_bottom_y (w);
 21300 
 21301       /* Find the row starting at new_start, if there is one.  Don't
 21302          reuse a partially visible line at the end.  */
 21303       first_reusable_row = start_row;
 21304       while (first_reusable_row->enabled_p
 21305              && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
 21306              && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21307                  < CHARPOS (new_start)))
 21308         ++first_reusable_row;
 21309 
 21310       /* Give up if there is no row to reuse.  */
 21311       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
 21312           || !first_reusable_row->enabled_p
 21313           || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21314               != CHARPOS (new_start)))
 21315         return false;
 21316 
 21317       /* We can reuse fully visible rows beginning with
 21318          first_reusable_row to the end of the window.  Set
 21319          first_row_to_display to the first row that cannot be reused.
 21320          Set pt_row to the row containing point, if there is any.  */
 21321       pt_row = NULL;
 21322       for (first_row_to_display = first_reusable_row;
 21323            MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
 21324            ++first_row_to_display)
 21325         {
 21326           if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
 21327               && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21328                   || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21329                       && first_row_to_display->ends_at_zv_p
 21330                       && pt_row == NULL)))
 21331             pt_row = first_row_to_display;
 21332         }
 21333 
 21334       if (first_row_to_display->y >= yb)
 21335         return false;
 21336 
 21337       /* Start displaying at the start of first_row_to_display.  */
 21338       init_to_row_start (&it, w, first_row_to_display);
 21339 
 21340       nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
 21341                         - start_vpos);
 21342       it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
 21343                  - nrows_scrolled);
 21344       it.current_y = (first_row_to_display->y - first_reusable_row->y
 21345                       + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 21346 
 21347       /* Display lines beginning with first_row_to_display in the
 21348          desired matrix.  Set last_text_row to the last row displayed
 21349          that displays text.  */
 21350       it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
 21351       if (pt_row == NULL)
 21352         w->cursor.vpos = -1;
 21353       last_text_row = NULL;
 21354       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21355         if (display_line (&it, w->cursor.vpos))
 21356           last_text_row = it.glyph_row - 1;
 21357 
 21358       /* If point is in a reused row, adjust y and vpos of the cursor
 21359          position.  */
 21360       if (pt_row)
 21361         {
 21362           w->cursor.vpos -= nrows_scrolled;
 21363           w->cursor.y -= first_reusable_row->y - start_row->y;
 21364         }
 21365 
 21366       /* Give up if point isn't in a row displayed or reused.  (This
 21367          also handles the case where w->cursor.vpos < nrows_scrolled
 21368          after the calls to display_line, which can happen with scroll
 21369          margins.  See bug#1295.)  */
 21370       if (w->cursor.vpos < 0)
 21371         {
 21372           clear_glyph_matrix (w->desired_matrix);
 21373           return false;
 21374         }
 21375 
 21376       /* Scroll the display.  */
 21377       run.current_y = first_reusable_row->y;
 21378       run.desired_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21379       run.height = it.last_visible_y - run.current_y;
 21380       dy = run.current_y - run.desired_y;
 21381 
 21382       if (run.height)
 21383         {
 21384 #ifdef HAVE_WINDOW_SYSTEM
 21385           update_begin (f);
 21386           gui_update_window_begin (w);
 21387           FRAME_RIF (f)->clear_window_mouse_face (w);
 21388           FRAME_RIF (f)->scroll_run_hook (w, &run);
 21389           gui_update_window_end (w, false, false);
 21390           update_end (f);
 21391 #endif
 21392         }
 21393 
 21394       /* Adjust Y positions of reused rows.  */
 21395       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21396       min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21397       max_y = it.last_visible_y;
 21398       for (row = first_reusable_row; row < first_row_to_display; ++row)
 21399         {
 21400           row->y -= dy;
 21401           row->visible_height = row->height;
 21402           if (row->y < min_y)
 21403             row->visible_height -= min_y - row->y;
 21404           if (row->y + row->height > max_y)
 21405             row->visible_height -= row->y + row->height - max_y;
 21406           if (row->fringe_bitmap_periodic_p)
 21407             row->redraw_fringe_bitmaps_p = true;
 21408         }
 21409 
 21410       /* Scroll the current matrix.  */
 21411       eassert (nrows_scrolled > 0);
 21412       rotate_matrix (w->current_matrix,
 21413                      start_vpos,
 21414                      MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21415                      -nrows_scrolled);
 21416 
 21417       /* Disable rows not reused.  */
 21418       for (row -= nrows_scrolled; row < bottom_row; ++row)
 21419         row->enabled_p = false;
 21420 
 21421       /* Point may have moved to a different line, so we cannot assume that
 21422          the previous cursor position is valid; locate the correct row.  */
 21423       if (pt_row)
 21424         {
 21425           for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 21426                row < bottom_row
 21427                  && PT >= MATRIX_ROW_END_CHARPOS (row)
 21428                  && !row->ends_at_zv_p;
 21429                row++)
 21430             {
 21431               w->cursor.vpos++;
 21432               w->cursor.y = row->y;
 21433             }
 21434           if (row < bottom_row)
 21435             {
 21436               /* Can't simply scan the row for point with
 21437                  bidi-reordered glyph rows.  Let set_cursor_from_row
 21438                  figure out where to put the cursor, and if it fails,
 21439                  give up.  */
 21440               if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 21441                 {
 21442                   if (!set_cursor_from_row (w, row, w->current_matrix,
 21443                                             0, 0, 0, 0))
 21444                     {
 21445                       clear_glyph_matrix (w->desired_matrix);
 21446                       return false;
 21447                     }
 21448                 }
 21449               else
 21450                 {
 21451                   struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
 21452                   struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21453 
 21454                   for (; glyph < end
 21455                          && (!BUFFERP (glyph->object)
 21456                              || glyph->charpos < PT);
 21457                        glyph++)
 21458                     {
 21459                       w->cursor.hpos++;
 21460                       w->cursor.x += glyph->pixel_width;
 21461                     }
 21462                 }
 21463             }
 21464         }
 21465 
 21466       /* Adjust window end.  A null value of last_text_row means that
 21467          the window end is in reused rows which in turn means that
 21468          only its vpos can have changed.  */
 21469       if (last_text_row)
 21470         adjust_window_ends (w, last_text_row, false);
 21471       else
 21472         w->window_end_vpos -= nrows_scrolled;
 21473 
 21474       w->window_end_valid = false;
 21475       w->desired_matrix->no_scrolling_p = true;
 21476 
 21477 #ifdef GLYPH_DEBUG
 21478       debug_method_add (w, "try_window_reusing_current_matrix 2");
 21479 #endif
 21480       return true;
 21481     }
 21482 
 21483   return false;
 21484 }
 21485 
 21486 
 21487 
 21488 /************************************************************************
 21489    Window redisplay reusing current matrix when buffer has changed
 21490  ************************************************************************/
 21491 
 21492 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
 21493 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 21494                                                           ptrdiff_t *, ptrdiff_t *);
 21495 static struct glyph_row *
 21496 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
 21497                                struct glyph_row *);
 21498 
 21499 
 21500 /* Return the last row in MATRIX displaying text.  If row START is
 21501    non-null, start searching with that row.  IT gives the dimensions
 21502    of the display.  Value is null if matrix is empty; otherwise it is
 21503    a pointer to the row found.  */
 21504 
 21505 static struct glyph_row *
 21506 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
 21507                                struct glyph_row *start)
 21508 {
 21509   struct glyph_row *row, *row_found;
 21510 
 21511   /* Set row_found to the last row in IT->w's current matrix
 21512      displaying text.  The loop looks funny but think of partially
 21513      visible lines.  */
 21514   row_found = NULL;
 21515   row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
 21516   while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21517     {
 21518       eassert (row->enabled_p);
 21519       row_found = row;
 21520       if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
 21521         break;
 21522       ++row;
 21523     }
 21524 
 21525   return row_found;
 21526 }
 21527 
 21528 
 21529 /* Return the last row in the current matrix of W that is not affected
 21530    by changes at the start of current_buffer that occurred since W's
 21531    current matrix was built.  Value is null if no such row exists.
 21532 
 21533    BEG_UNCHANGED is the number of characters unchanged at the start of
 21534    current_buffer.  BEG + BEG_UNCHANGED is the buffer position of the
 21535    first changed character in current_buffer.  Characters at positions <
 21536    BEG + BEG_UNCHANGED are at the same buffer positions as they were
 21537    when the current matrix was built.  */
 21538 
 21539 static struct glyph_row *
 21540 find_last_unchanged_at_beg_row (struct window *w)
 21541 {
 21542   ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
 21543   struct glyph_row *row;
 21544   struct glyph_row *row_found = NULL;
 21545   int yb = window_text_bottom_y (w);
 21546 
 21547   /* Find the last row displaying unchanged text.  */
 21548   for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21549        MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21550          && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
 21551        ++row)
 21552     {
 21553       if (/* If row ends before first_changed_pos, it is unchanged,
 21554              except in some case.  */
 21555           MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
 21556           /* When row ends in ZV and we write at ZV it is not
 21557              unchanged.  */
 21558           && !row->ends_at_zv_p
 21559           /* When first_changed_pos is the end of a continued line,
 21560              row is not unchanged because it may be no longer
 21561              continued.  */
 21562           && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
 21563                && (row->continued_p
 21564                    || row->exact_window_width_line_p))
 21565           /* If ROW->end is beyond ZV, then ROW->end is outdated and
 21566              needs to be recomputed, so don't consider this row as
 21567              unchanged.  This happens when the last line was
 21568              bidi-reordered and was killed immediately before this
 21569              redisplay cycle.  In that case, ROW->end stores the
 21570              buffer position of the first visual-order character of
 21571              the killed text, which is now beyond ZV.  */
 21572           && CHARPOS (row->end.pos) <= ZV)
 21573         row_found = row;
 21574 
 21575       /* Stop if last visible row.  */
 21576       if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
 21577         break;
 21578     }
 21579 
 21580   return row_found;
 21581 }
 21582 
 21583 
 21584 /* Find the first glyph row in the current matrix of W that is not
 21585    affected by changes at the end of current_buffer since the
 21586    time W's current matrix was built.
 21587 
 21588    Return in *DELTA the number of chars by which buffer positions in
 21589    unchanged text at the end of current_buffer must be adjusted.
 21590 
 21591    Return in *DELTA_BYTES the corresponding number of bytes.
 21592 
 21593    Value is null if no such row exists, i.e. all rows are affected by
 21594    changes.  */
 21595 
 21596 static struct glyph_row *
 21597 find_first_unchanged_at_end_row (struct window *w,
 21598                                  ptrdiff_t *delta, ptrdiff_t *delta_bytes)
 21599 {
 21600   struct glyph_row *row;
 21601   struct glyph_row *row_found = NULL;
 21602 
 21603   *delta = *delta_bytes = 0;
 21604 
 21605   /* Display must not have been paused, otherwise the current matrix
 21606      is not up to date.  */
 21607   eassert (w->window_end_valid);
 21608 
 21609   /* A value of window_end_pos >= END_UNCHANGED means that the window
 21610      end is in the range of changed text.  If so, there is no
 21611      unchanged row at the end of W's current matrix.  */
 21612   if (w->window_end_pos >= END_UNCHANGED)
 21613     return NULL;
 21614 
 21615   /* Set row to the last row in W's current matrix displaying text.  */
 21616   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21617 
 21618   /* If matrix is entirely empty, no unchanged row exists.  */
 21619   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21620     {
 21621       /* The value of row is the last glyph row in the matrix having a
 21622          meaningful buffer position in it.  The end position of row
 21623          corresponds to window_end_pos.  This allows us to translate
 21624          buffer positions in the current matrix to current buffer
 21625          positions for characters not in changed text.  */
 21626       ptrdiff_t Z_old =
 21627         MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21628       ptrdiff_t Z_BYTE_old =
 21629         MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21630       ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
 21631       struct glyph_row *first_text_row
 21632         = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21633 
 21634       *delta = Z - Z_old;
 21635       *delta_bytes = Z_BYTE - Z_BYTE_old;
 21636 
 21637       /* Set last_unchanged_pos to the buffer position of the last
 21638          character in the buffer that has not been changed.  Z is the
 21639          index + 1 of the last character in current_buffer, i.e. by
 21640          subtracting END_UNCHANGED we get the index of the last
 21641          unchanged character, and we have to add BEG to get its buffer
 21642          position.  */
 21643       last_unchanged_pos = Z - END_UNCHANGED + BEG;
 21644       last_unchanged_pos_old = last_unchanged_pos - *delta;
 21645 
 21646       /* Search backward from ROW for a row displaying a line that
 21647          starts at a minimum position >= last_unchanged_pos_old.  */
 21648       for (; row > first_text_row; --row)
 21649         {
 21650           /* This used to abort, but it can happen.
 21651              It is ok to just stop the search instead here.  KFS.  */
 21652           if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21653             break;
 21654 
 21655           if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
 21656             row_found = row;
 21657         }
 21658     }
 21659 
 21660   eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
 21661 
 21662   return row_found;
 21663 }
 21664 
 21665 
 21666 /* Make sure that glyph rows in the current matrix of window W
 21667    reference the same glyph memory as corresponding rows in the
 21668    frame's frame matrix.  This function is called after scrolling W's
 21669    current matrix on a terminal frame in try_window_id and
 21670    try_window_reusing_current_matrix.  */
 21671 
 21672 static void
 21673 sync_frame_with_window_matrix_rows (struct window *w)
 21674 {
 21675   struct frame *f = XFRAME (w->frame);
 21676   struct glyph_row *window_row, *window_row_end, *frame_row;
 21677 
 21678   /* Preconditions: W must be a leaf window and full-width.  Its frame
 21679      must have a frame matrix.  */
 21680   eassert (BUFFERP (w->contents));
 21681   eassert (WINDOW_FULL_WIDTH_P (w));
 21682   eassert (!FRAME_WINDOW_P (f));
 21683 
 21684   /* If W is a full-width window, glyph pointers in W's current matrix
 21685      have, by definition, to be the same as glyph pointers in the
 21686      corresponding frame matrix.  Note that frame matrices have no
 21687      marginal areas (see build_frame_matrix).  */
 21688   window_row = w->current_matrix->rows;
 21689   window_row_end = window_row + w->current_matrix->nrows;
 21690   frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 21691   while (window_row < window_row_end)
 21692     {
 21693       struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
 21694       struct glyph *end = window_row->glyphs[LAST_AREA];
 21695 
 21696       frame_row->glyphs[LEFT_MARGIN_AREA] = start;
 21697       frame_row->glyphs[TEXT_AREA] = start;
 21698       frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
 21699       frame_row->glyphs[LAST_AREA] = end;
 21700 
 21701       /* Disable frame rows whose corresponding window rows have
 21702          been disabled in try_window_id.  */
 21703       if (!window_row->enabled_p)
 21704         frame_row->enabled_p = false;
 21705 
 21706       ++window_row, ++frame_row;
 21707     }
 21708 }
 21709 
 21710 
 21711 /* Find the glyph row in window W containing CHARPOS.  Consider all
 21712    rows between START and END (not inclusive).  END null means search
 21713    all rows to the end of the display area of W.  Value is the row
 21714    containing CHARPOS or null.  */
 21715 
 21716 struct glyph_row *
 21717 row_containing_pos (struct window *w, ptrdiff_t charpos,
 21718                     struct glyph_row *start, struct glyph_row *end, int dy)
 21719 {
 21720   struct glyph_row *row = start;
 21721   struct glyph_row *best_row = NULL;
 21722   ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
 21723   int last_y;
 21724 
 21725   /* If we happen to start on a header-line or a tab-line, skip that.  */
 21726   if (row->tab_line_p)
 21727     ++row;
 21728   if (row->mode_line_p)
 21729     ++row;
 21730 
 21731   if ((end && row >= end) || !row->enabled_p)
 21732     return NULL;
 21733 
 21734   last_y = window_text_bottom_y (w) - dy;
 21735 
 21736   while (true)
 21737     {
 21738       /* Give up if we have gone too far.  */
 21739       if ((end && row >= end) || !row->enabled_p)
 21740         return NULL;
 21741       /* This formerly returned if they were equal.
 21742          I think that both quantities are of a "last plus one" type;
 21743          if so, when they are equal, the row is within the screen. -- rms.  */
 21744       if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
 21745         return NULL;
 21746 
 21747       /* If it is in this row, return this row.  */
 21748       if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
 21749              || (MATRIX_ROW_END_CHARPOS (row) == charpos
 21750                  /* The end position of a row equals the start
 21751                     position of the next row.  If CHARPOS is there, we
 21752                     would rather consider it displayed in the next
 21753                     line, except when this line ends in ZV.  */
 21754                  && !row_for_charpos_p (row, charpos)))
 21755           && charpos >= MATRIX_ROW_START_CHARPOS (row))
 21756         {
 21757           struct glyph *g;
 21758 
 21759           if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21760               || (!best_row && !row->continued_p))
 21761             return row;
 21762           /* In bidi-reordered rows, there could be several rows whose
 21763              edges surround CHARPOS, all of these rows belonging to
 21764              the same continued line.  We need to find the row which
 21765              fits CHARPOS the best.  */
 21766           for (g = row->glyphs[TEXT_AREA];
 21767                g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21768                g++)
 21769             {
 21770               if (!STRINGP (g->object))
 21771                 {
 21772                   if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
 21773                     {
 21774                       mindif = eabs (g->charpos - charpos);
 21775                       best_row = row;
 21776                       /* Exact match always wins.  */
 21777                       if (mindif == 0)
 21778                         return best_row;
 21779                     }
 21780                 }
 21781             }
 21782         }
 21783       else if (best_row && !row->continued_p)
 21784         return best_row;
 21785       ++row;
 21786     }
 21787 }
 21788 
 21789 
 21790 /* Try to redisplay window W by reusing its existing display.  W's
 21791    current matrix must be up to date when this function is called,
 21792    i.e., window_end_valid must be true.
 21793 
 21794    Value is
 21795 
 21796    >= 1 if successful, i.e. display has been updated
 21797          specifically:
 21798          1 means the changes were in front of a newline that precedes
 21799            the window start, and the whole current matrix was reused
 21800          2 means the changes were after the last position displayed
 21801            in the window, and the whole current matrix was reused
 21802          3 means portions of the current matrix were reused, while
 21803            some of the screen lines were redrawn
 21804    -1   if redisplay with same window start is known not to succeed
 21805    0    if otherwise unsuccessful
 21806 
 21807    The following steps are performed:
 21808 
 21809    1. Find the last row in the current matrix of W that is not
 21810    affected by changes at the start of current_buffer.  If no such row
 21811    is found, give up.
 21812 
 21813    2. Find the first row in W's current matrix that is not affected by
 21814    changes at the end of current_buffer.  Maybe there is no such row.
 21815 
 21816    3. Display lines beginning with the row + 1 found in step 1 to the
 21817    row found in step 2 or, if step 2 didn't find a row, to the end of
 21818    the window.
 21819 
 21820    4. If cursor is not known to appear on the window, give up.
 21821 
 21822    5. If display stopped at the row found in step 2, scroll the
 21823    display and current matrix as needed.
 21824 
 21825    6. Maybe display some lines at the end of W, if we must.  This can
 21826    happen under various circumstances, like a partially visible line
 21827    becoming fully visible, or because newly displayed lines are displayed
 21828    in smaller font sizes.
 21829 
 21830    7. Update W's window end information.  */
 21831 
 21832 static int
 21833 try_window_id (struct window *w)
 21834 {
 21835   struct frame *f = XFRAME (w->frame);
 21836   struct glyph_matrix *current_matrix = w->current_matrix;
 21837   struct glyph_matrix *desired_matrix = w->desired_matrix;
 21838   struct glyph_row *last_unchanged_at_beg_row;
 21839   struct glyph_row *first_unchanged_at_end_row;
 21840   struct glyph_row *row;
 21841   struct glyph_row *bottom_row;
 21842   int bottom_vpos;
 21843   struct it it;
 21844   ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
 21845   int dvpos, dy;
 21846   struct text_pos start_pos;
 21847   struct run run;
 21848   int first_unchanged_at_end_vpos = 0;
 21849   struct glyph_row *last_text_row, *last_text_row_at_end;
 21850   struct text_pos start;
 21851   ptrdiff_t first_changed_charpos, last_changed_charpos;
 21852 
 21853 #ifdef GLYPH_DEBUG
 21854   if (inhibit_try_window_id)
 21855     return 0;
 21856 #endif
 21857 
 21858   /* This is handy for debugging.  */
 21859 #if false
 21860 #define GIVE_UP(X)                                              \
 21861   do {                                                          \
 21862     redisplay_trace ("try_window_id give up %d\n", X);          \
 21863     return 0;                                                   \
 21864   } while (false)
 21865 #else
 21866 #define GIVE_UP(X) return 0
 21867 #endif
 21868 
 21869   SET_TEXT_POS_FROM_MARKER (start, w->start);
 21870 
 21871   /* Don't use this for mini-windows because these can show
 21872      messages and mini-buffers, and we don't handle that here.  */
 21873   if (MINI_WINDOW_P (w))
 21874     GIVE_UP (1);
 21875 
 21876   /* This flag is used to prevent redisplay optimizations.  */
 21877   if (windows_or_buffers_changed || f->cursor_type_changed)
 21878     GIVE_UP (2);
 21879 
 21880   /* This function's optimizations cannot be used if overlays have
 21881      changed in the buffer displayed by the window, so give up if they
 21882      have.  */
 21883   if (w->last_overlay_modified != OVERLAY_MODIFF)
 21884     GIVE_UP (200);
 21885 
 21886   /* Verify that narrowing has not changed.
 21887      Also verify that we were not told to prevent redisplay optimizations.
 21888      It would be nice to further
 21889      reduce the number of cases where this prevents try_window_id.  */
 21890   if (current_buffer->clip_changed
 21891       || current_buffer->prevent_redisplay_optimizations_p)
 21892     GIVE_UP (3);
 21893 
 21894   /* Window must either use window-based redisplay or be full width.  */
 21895   if (!FRAME_WINDOW_P (f)
 21896       && (!FRAME_LINE_INS_DEL_OK (f)
 21897           || !WINDOW_FULL_WIDTH_P (w)))
 21898     GIVE_UP (4);
 21899 
 21900   /* Give up if point is known NOT to appear in W.  */
 21901   if (PT < CHARPOS (start))
 21902     GIVE_UP (5);
 21903 
 21904   /* Another way to prevent redisplay optimizations.  */
 21905   if (w->last_modified == 0)
 21906     GIVE_UP (6);
 21907 
 21908   /* Verify that window is not hscrolled.  */
 21909   if (w->hscroll != 0)
 21910     GIVE_UP (7);
 21911 
 21912   /* Verify that display wasn't paused.  */
 21913   if (!w->window_end_valid)
 21914     GIVE_UP (8);
 21915 
 21916   /* Likewise if highlighting trailing whitespace.  */
 21917   if (!NILP (Vshow_trailing_whitespace))
 21918     GIVE_UP (11);
 21919 
 21920   /* Can't use this if overlay arrow position and/or string have
 21921      changed.  */
 21922   if (overlay_arrows_changed_p (false))
 21923     GIVE_UP (12);
 21924 
 21925   /* When word-wrap is on, adding a space to the first word of a
 21926      wrapped line can change the wrap position, altering the line
 21927      above it.  It might be worthwhile to handle this more
 21928      intelligently, but for now just redisplay from scratch.  */
 21929   if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
 21930     GIVE_UP (21);
 21931 
 21932   /* Under bidi reordering, adding or deleting a character in the
 21933      beginning of a paragraph, before the first strong directional
 21934      character, can change the base direction of the paragraph (unless
 21935      the buffer specifies a fixed paragraph direction), which will
 21936      require redisplaying the whole paragraph.  It might be worthwhile
 21937      to find the paragraph limits and widen the range of redisplayed
 21938      lines to that, but for now just give up this optimization and
 21939      redisplay from scratch.  */
 21940   if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21941       && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 21942     GIVE_UP (22);
 21943 
 21944   /* Give up if the buffer has line-spacing set, as Lisp-level changes
 21945      to that variable require thorough redisplay.  */
 21946   if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
 21947     GIVE_UP (23);
 21948 
 21949   /* Give up if display-line-numbers is in relative mode, or when the
 21950      current line's number needs to be displayed in a distinct face.  */
 21951   if (EQ (Vdisplay_line_numbers, Qrelative)
 21952       || EQ (Vdisplay_line_numbers, Qvisual)
 21953       || (!NILP (Vdisplay_line_numbers)
 21954           && NILP (Finternal_lisp_face_equal_p (Qline_number,
 21955                                                 Qline_number_current_line,
 21956                                                 w->frame))))
 21957     GIVE_UP (24);
 21958 
 21959   /* composition-break-at-point is incompatible with the optimizations
 21960      in this function, because we need to recompose characters when
 21961      point moves off their positions.  */
 21962   if (composition_break_at_point)
 21963     GIVE_UP (27);
 21964 
 21965   /* Make sure beg_unchanged and end_unchanged are up to date.  Do it
 21966      only if buffer has really changed.  The reason is that the gap is
 21967      initially at Z for freshly visited files.  The code below would
 21968      set end_unchanged to 0 in that case.  */
 21969   if (MODIFF > SAVE_MODIFF
 21970       /* This seems to happen sometimes after saving a buffer.  */
 21971       || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
 21972     {
 21973       if (GPT - BEG < BEG_UNCHANGED)
 21974         BEG_UNCHANGED = GPT - BEG;
 21975       if (Z - GPT < END_UNCHANGED)
 21976         END_UNCHANGED = Z - GPT;
 21977     }
 21978 
 21979   /* The position of the first and last character that has been changed.  */
 21980   first_changed_charpos = BEG + BEG_UNCHANGED;
 21981   last_changed_charpos  = Z - END_UNCHANGED;
 21982 
 21983   /* If window starts after a line end, and the last change is in
 21984      front of that newline, then changes don't affect the display.
 21985      This case happens with stealth-fontification.  Note that although
 21986      the display is unchanged, glyph positions in the matrix have to
 21987      be adjusted, of course.  */
 21988   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21989   if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21990       && ((last_changed_charpos < CHARPOS (start)
 21991            && CHARPOS (start) == BEGV)
 21992           || (last_changed_charpos < CHARPOS (start) - 1
 21993               && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
 21994     {
 21995       ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 21996       struct glyph_row *r0;
 21997 
 21998       /* Compute how many chars/bytes have been added to or removed
 21999          from the buffer.  */
 22000       Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 22001       Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 22002       Z_delta = Z - Z_old;
 22003       Z_delta_bytes = Z_BYTE - Z_BYTE_old;
 22004 
 22005       /* Give up if PT is not in the window.  Note that it already has
 22006          been checked at the start of try_window_id that PT is not in
 22007          front of the window start.  */
 22008       if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
 22009         GIVE_UP (13);
 22010 
 22011       /* If window start is unchanged, we can reuse the whole matrix
 22012          as is, after adjusting glyph positions.  No need to compute
 22013          the window end again, since its offset from Z hasn't changed.  */
 22014       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22015       if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
 22016           && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
 22017           /* PT must not be in a partially visible line.  */
 22018           && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
 22019                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 22020         {
 22021           /* Adjust positions in the glyph matrix.  */
 22022           if (Z_delta || Z_delta_bytes)
 22023             {
 22024               struct glyph_row *r1
 22025                 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22026               increment_matrix_positions (w->current_matrix,
 22027                                           MATRIX_ROW_VPOS (r0, current_matrix),
 22028                                           MATRIX_ROW_VPOS (r1, current_matrix),
 22029                                           Z_delta, Z_delta_bytes);
 22030             }
 22031 
 22032           /* Set the cursor.  */
 22033           row = row_containing_pos (w, PT, r0, NULL, 0);
 22034           if (row)
 22035             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 22036           return 1;
 22037         }
 22038     }
 22039 
 22040   /* Handle the case that changes are all below what is displayed in
 22041      the window, and that PT is in the window.  This shortcut cannot
 22042      be taken if ZV is visible in the window, and text has been added
 22043      there that is visible in the window.  */
 22044   if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
 22045       /* ZV is not visible in the window, or there are no
 22046          changes at ZV, actually.  */
 22047       && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
 22048           || first_changed_charpos == last_changed_charpos))
 22049     {
 22050       struct glyph_row *r0;
 22051 
 22052       /* Give up if PT is not in the window.  Note that it already has
 22053          been checked at the start of try_window_id that PT is not in
 22054          front of the window start.  */
 22055       if (PT >= MATRIX_ROW_END_CHARPOS (row))
 22056         GIVE_UP (14);
 22057 
 22058       /* If window start is unchanged, we can reuse the whole matrix
 22059          as is, without changing glyph positions since no text has
 22060          been added/removed in front of the window end.  */
 22061       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22062       if (TEXT_POS_EQUAL_P (start, r0->minpos)
 22063           /* PT must not be in a partially visible line.  */
 22064           && !(PT >= MATRIX_ROW_START_CHARPOS (row)
 22065                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 22066         {
 22067           /* We have to compute the window end anew since text
 22068              could have been added/removed after it.  */
 22069           w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22070           w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22071 
 22072           /* Set the cursor.  */
 22073           row = row_containing_pos (w, PT, r0, NULL, 0);
 22074           if (row)
 22075             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 22076           return 2;
 22077         }
 22078     }
 22079 
 22080   /* Give up if window start is in the changed area.
 22081 
 22082      The condition used to read
 22083 
 22084      (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
 22085 
 22086      but why that was tested escapes me at the moment.  */
 22087   if (CHARPOS (start) >= first_changed_charpos
 22088       && CHARPOS (start) <= last_changed_charpos)
 22089     GIVE_UP (15);
 22090 
 22091   /* Check that window start agrees with the start of the first glyph
 22092      row in its current matrix.  Check this after we know the window
 22093      start is not in changed text, otherwise positions would not be
 22094      comparable.  */
 22095   row = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22096   if (!TEXT_POS_EQUAL_P (start, row->minpos))
 22097     GIVE_UP (16);
 22098 
 22099   /* Give up if the window ends in strings.  Overlay strings
 22100      at the end are difficult to handle, so don't try.  */
 22101   row = MATRIX_ROW (current_matrix, w->window_end_vpos);
 22102   if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
 22103     GIVE_UP (20);
 22104 
 22105   /* Can't let scroll_run_hook below run on w32 GUI frames when
 22106      position of point is indicated by the system caret, because
 22107      scrolling the display will then "copy" the pixels used by the
 22108      caret.  */
 22109 #ifdef HAVE_NTGUI
 22110   if (FRAME_W32_P (f) && w32_use_visible_system_caret)
 22111     GIVE_UP (25);
 22112 #endif
 22113 
 22114   /* Compute the position at which we have to start displaying new
 22115      lines.  Some of the lines at the top of the window might be
 22116      reusable because they are not displaying changed text.  Find the
 22117      last row in W's current matrix not affected by changes at the
 22118      start of current_buffer.  Value is null if changes start in the
 22119      first line of window.  */
 22120   last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
 22121   if (last_unchanged_at_beg_row)
 22122     {
 22123       /* Avoid starting to display in the middle of a character, a TAB
 22124          for instance.  This is easier than to set up the iterator
 22125          exactly, and it's not a frequent case, so the additional
 22126          effort wouldn't really pay off.  */
 22127       while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
 22128               || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
 22129              && last_unchanged_at_beg_row > w->current_matrix->rows)
 22130         --last_unchanged_at_beg_row;
 22131 
 22132       if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
 22133         GIVE_UP (17);
 22134 
 22135       if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
 22136         GIVE_UP (18);
 22137       /* Give up if the row starts with a display property that draws
 22138          on the fringes, since that could prevent correct display of
 22139          line-prefix and wrap-prefix.  */
 22140       if (it.sp > 1
 22141           && it.method == GET_FROM_IMAGE && it.image_id == -1)
 22142         GIVE_UP (26);
 22143       start_pos = it.current.pos;
 22144 
 22145       /* Start displaying new lines in the desired matrix at the same
 22146          vpos we would use in the current matrix, i.e. below
 22147          last_unchanged_at_beg_row.  */
 22148       it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
 22149                                      current_matrix);
 22150       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22151       it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
 22152 
 22153       eassert (it.hpos == 0 && it.current_x == 0);
 22154     }
 22155   else
 22156     {
 22157       /* There are no reusable lines at the start of the window.
 22158          Start displaying in the first text line.  */
 22159       start_display (&it, w, start);
 22160       it.vpos = it.first_vpos;
 22161       start_pos = it.current.pos;
 22162     }
 22163 
 22164   /* Find the first row that is not affected by changes at the end of
 22165      the buffer.  Value will be null if there is no unchanged row, in
 22166      which case we must redisplay to the end of the window.  delta
 22167      will be set to the value by which buffer positions beginning with
 22168      first_unchanged_at_end_row have to be adjusted due to text
 22169      changes.  */
 22170   first_unchanged_at_end_row
 22171     = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
 22172   IF_DEBUG (debug_delta = delta);
 22173   IF_DEBUG (debug_delta_bytes = delta_bytes);
 22174 
 22175   /* Set stop_pos to the buffer position up to which we will have to
 22176      display new lines.  If first_unchanged_at_end_row != NULL, this
 22177      is the buffer position of the start of the line displayed in that
 22178      row.  For first_unchanged_at_end_row == NULL, use 0 to indicate
 22179      that we don't stop at a buffer position.  */
 22180   stop_pos = 0;
 22181   if (first_unchanged_at_end_row)
 22182     {
 22183       eassert (last_unchanged_at_beg_row == NULL
 22184                || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
 22185 
 22186       /* If this is a continuation line, move forward to the next one
 22187          that isn't.  Changes in lines above affect this line.
 22188          Caution: this may move first_unchanged_at_end_row to a row
 22189          not displaying text.  */
 22190       while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
 22191              && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22192              && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22193                  < it.last_visible_y))
 22194         ++first_unchanged_at_end_row;
 22195 
 22196       if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22197           || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22198               >= it.last_visible_y))
 22199         first_unchanged_at_end_row = NULL;
 22200       else
 22201         {
 22202           stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
 22203                       + delta);
 22204           first_unchanged_at_end_vpos
 22205             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
 22206           eassert (stop_pos >= Z - END_UNCHANGED);
 22207         }
 22208     }
 22209   else if (last_unchanged_at_beg_row == NULL)
 22210     GIVE_UP (19);
 22211 
 22212 
 22213 #ifdef GLYPH_DEBUG
 22214 
 22215   /* Either there is no unchanged row at the end, or the one we have
 22216      now displays text.  This is a necessary condition for the window
 22217      end pos calculation at the end of this function.  */
 22218   eassert (first_unchanged_at_end_row == NULL
 22219            || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22220 
 22221   debug_last_unchanged_at_beg_vpos
 22222     = (last_unchanged_at_beg_row
 22223        ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
 22224        : -1);
 22225   debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
 22226 
 22227 #endif /* GLYPH_DEBUG */
 22228 
 22229 
 22230   /* Display new lines.  Set last_text_row to the last new line
 22231      displayed which has text on it, i.e. might end up as being the
 22232      line where the window_end_vpos is.  */
 22233   w->cursor.vpos = -1;
 22234   last_text_row = NULL;
 22235   overlay_arrow_seen = false;
 22236   if (it.current_y < it.last_visible_y
 22237       && !f->fonts_changed
 22238       && (first_unchanged_at_end_row == NULL
 22239           || IT_CHARPOS (it) < stop_pos))
 22240     it.glyph_row->reversed_p = false;
 22241   while (it.current_y < it.last_visible_y
 22242          && !f->fonts_changed
 22243          && (first_unchanged_at_end_row == NULL
 22244              || IT_CHARPOS (it) < stop_pos))
 22245     {
 22246       if (display_line (&it, -1))
 22247         last_text_row = it.glyph_row - 1;
 22248     }
 22249 
 22250   if (f->fonts_changed)
 22251     return -1;
 22252 
 22253   /* The redisplay iterations in display_line above could have
 22254      triggered font-lock, which could have done something that
 22255      invalidates IT->w window's end-point information, on which we
 22256      rely below.  E.g., one package, which will remain unnamed, used
 22257      to install a font-lock-fontify-region-function that called
 22258      bury-buffer, whose side effect is to switch the buffer displayed
 22259      by IT->w, and that predictably resets IT->w's window_end_valid
 22260      flag, which we already tested at the entry to this function.
 22261      Amply punish such packages/modes by giving up on this
 22262      optimization in those cases.  */
 22263   if (!w->window_end_valid)
 22264     {
 22265       clear_glyph_matrix (w->desired_matrix);
 22266       return -1;
 22267     }
 22268 
 22269   /* Compute differences in buffer positions, y-positions etc.  for
 22270      lines reused at the bottom of the window.  Compute what we can
 22271      scroll.  */
 22272   if (first_unchanged_at_end_row
 22273       /* No lines reused because we displayed everything up to the
 22274          bottom of the window.  */
 22275       && it.current_y < it.last_visible_y)
 22276     {
 22277       dvpos = (it.vpos
 22278                - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
 22279                                   current_matrix));
 22280       dy = it.current_y - first_unchanged_at_end_row->y;
 22281       run.current_y = first_unchanged_at_end_row->y;
 22282       run.desired_y = run.current_y + dy;
 22283       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
 22284     }
 22285   else
 22286     {
 22287       delta = delta_bytes = dvpos = dy
 22288         = run.current_y = run.desired_y = run.height = 0;
 22289       first_unchanged_at_end_row = NULL;
 22290     }
 22291   IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
 22292 
 22293 
 22294   /* Find the cursor if not already found.  We have to decide whether
 22295      PT will appear on this window (it sometimes doesn't, but this is
 22296      not a very frequent case.)  This decision has to be made before
 22297      the current matrix is altered.  A value of cursor.vpos < 0 means
 22298      that PT is either in one of the lines beginning at
 22299      first_unchanged_at_end_row or below the window.  Don't care for
 22300      lines that might be displayed later at the window end; as
 22301      mentioned, this is not a frequent case.  */
 22302   if (w->cursor.vpos < 0)
 22303     {
 22304       /* Cursor in unchanged rows at the top?  */
 22305       if (PT < CHARPOS (start_pos)
 22306           && last_unchanged_at_beg_row)
 22307         {
 22308           row = row_containing_pos (w, PT,
 22309                                     MATRIX_FIRST_TEXT_ROW (w->current_matrix),
 22310                                     last_unchanged_at_beg_row + 1, 0);
 22311           if (row)
 22312             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 22313         }
 22314 
 22315       /* Start from first_unchanged_at_end_row looking for PT.  */
 22316       else if (first_unchanged_at_end_row)
 22317         {
 22318           row = row_containing_pos (w, PT - delta,
 22319                                     first_unchanged_at_end_row, NULL, 0);
 22320           if (row)
 22321             set_cursor_from_row (w, row, w->current_matrix, delta,
 22322                                  delta_bytes, dy, dvpos);
 22323         }
 22324 
 22325       /* Give up if cursor was not found.  */
 22326       if (w->cursor.vpos < 0)
 22327         {
 22328           clear_glyph_matrix (w->desired_matrix);
 22329           return -1;
 22330         }
 22331     }
 22332 
 22333   /* Don't let the cursor end in the scroll margins.  */
 22334   {
 22335     int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 22336     int bot_scroll_margin = top_scroll_margin;
 22337     int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
 22338 
 22339     if (window_wants_header_line (w))
 22340       top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 22341     if (window_wants_tab_line (w))
 22342       top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 22343 
 22344     if ((w->cursor.y < top_scroll_margin
 22345          && CHARPOS (start) > BEGV)
 22346         /* Old redisplay didn't take scroll margin into account at the bottom,
 22347            but then global-hl-line-mode doesn't scroll.  KFS 2004-06-14 */
 22348         || (w->cursor.y
 22349             + (cursor_row_fully_visible_p (w, false, true, true)
 22350                ? 1
 22351                : cursor_height + bot_scroll_margin)) > it.last_visible_y)
 22352       {
 22353         w->cursor.vpos = -1;
 22354         clear_glyph_matrix (w->desired_matrix);
 22355         return -1;
 22356       }
 22357   }
 22358 
 22359   /* Scroll the display.  Do it before changing the current matrix so
 22360      that xterm.c doesn't get confused about where the cursor glyph is
 22361      found.  */
 22362   if (dy && run.height)
 22363     {
 22364       update_begin (f);
 22365 
 22366       if (FRAME_WINDOW_P (f))
 22367         {
 22368 #ifdef HAVE_WINDOW_SYSTEM
 22369           gui_update_window_begin (w);
 22370           FRAME_RIF (f)->clear_window_mouse_face (w);
 22371           FRAME_RIF (f)->scroll_run_hook (w, &run);
 22372           gui_update_window_end (w, false, false);
 22373 #endif
 22374         }
 22375       else
 22376         {
 22377           /* Terminal frame.  In this case, dvpos gives the number of
 22378              lines to scroll by; dvpos < 0 means scroll up.  */
 22379           int from_vpos
 22380             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
 22381           int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
 22382           int end = (WINDOW_TOP_EDGE_LINE (w)
 22383                      + window_wants_tab_line (w)
 22384                      + window_wants_header_line (w)
 22385                      + window_internal_height (w));
 22386 
 22387           gui_clear_window_mouse_face (w);
 22388 
 22389           /* Perform the operation on the screen.  */
 22390           if (dvpos > 0)
 22391             {
 22392               /* Scroll last_unchanged_at_beg_row to the end of the
 22393                  window down dvpos lines.  */
 22394               set_terminal_window (f, end);
 22395 
 22396               /* On dumb terminals delete dvpos lines at the end
 22397                  before inserting dvpos empty lines.  */
 22398               if (!FRAME_SCROLL_REGION_OK (f))
 22399                 ins_del_lines (f, end - dvpos, -dvpos);
 22400 
 22401               /* Insert dvpos empty lines in front of
 22402                  last_unchanged_at_beg_row.  */
 22403               ins_del_lines (f, from, dvpos);
 22404             }
 22405           else if (dvpos < 0)
 22406             {
 22407               /* Scroll up last_unchanged_at_beg_vpos to the end of
 22408                  the window to last_unchanged_at_beg_vpos - |dvpos|.  */
 22409               set_terminal_window (f, end);
 22410 
 22411               /* Delete dvpos lines in front of
 22412                  last_unchanged_at_beg_vpos.  ins_del_lines will set
 22413                  the cursor to the given vpos and emit |dvpos| delete
 22414                  line sequences.  */
 22415               ins_del_lines (f, from + dvpos, dvpos);
 22416 
 22417               /* On a dumb terminal insert dvpos empty lines at the
 22418                  end.  */
 22419               if (!FRAME_SCROLL_REGION_OK (f))
 22420                 ins_del_lines (f, end + dvpos, -dvpos);
 22421             }
 22422 
 22423           set_terminal_window (f, 0);
 22424         }
 22425 
 22426       update_end (f);
 22427     }
 22428 
 22429   /* Shift reused rows of the current matrix to the right position.
 22430      BOTTOM_ROW is the last + 1 row in the current matrix reserved for
 22431      text.  */
 22432   bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22433   bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
 22434   if (dvpos < 0)
 22435     {
 22436       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
 22437                      bottom_vpos, dvpos);
 22438       clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
 22439                                bottom_vpos);
 22440     }
 22441   else if (dvpos > 0)
 22442     {
 22443       rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
 22444                      bottom_vpos, dvpos);
 22445       clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
 22446                                first_unchanged_at_end_vpos + dvpos);
 22447     }
 22448 
 22449   /* For frame-based redisplay, make sure that current frame and window
 22450      matrix are in sync with respect to glyph memory.  */
 22451   if (!FRAME_WINDOW_P (f))
 22452     sync_frame_with_window_matrix_rows (w);
 22453 
 22454   /* Adjust buffer positions in reused rows.  */
 22455   if (delta || delta_bytes)
 22456     increment_matrix_positions (current_matrix,
 22457                                 first_unchanged_at_end_vpos + dvpos,
 22458                                 bottom_vpos, delta, delta_bytes);
 22459 
 22460   /* Adjust Y positions.  */
 22461   if (dy)
 22462     shift_glyph_matrix (w, current_matrix,
 22463                         first_unchanged_at_end_vpos + dvpos,
 22464                         bottom_vpos, dy);
 22465 
 22466   if (first_unchanged_at_end_row)
 22467     {
 22468       first_unchanged_at_end_row += dvpos;
 22469       if (first_unchanged_at_end_row->y >= it.last_visible_y
 22470           || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
 22471         first_unchanged_at_end_row = NULL;
 22472     }
 22473 
 22474   /* If scrolling up, there may be some lines to display at the end of
 22475      the window.  */
 22476   last_text_row_at_end = NULL;
 22477   if (dy < 0)
 22478     {
 22479       /* Scrolling up can leave for example a partially visible line
 22480          at the end of the window to be redisplayed.  */
 22481       /* Set last_row to the glyph row in the current matrix where the
 22482          window end line is found.  It has been moved up or down in
 22483          the matrix by dvpos.  */
 22484       int last_vpos = w->window_end_vpos + dvpos;
 22485       struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
 22486 
 22487       /* If last_row is the window end line, it should display text.  */
 22488       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
 22489 
 22490       /* If window end line was partially visible before, begin
 22491          displaying at that line.  Otherwise begin displaying with the
 22492          line following it.  */
 22493       if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
 22494         {
 22495           init_to_row_start (&it, w, last_row);
 22496           it.vpos = last_vpos;
 22497           it.current_y = last_row->y;
 22498         }
 22499       else
 22500         {
 22501           init_to_row_end (&it, w, last_row);
 22502           it.vpos = 1 + last_vpos;
 22503           it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
 22504           ++last_row;
 22505         }
 22506 
 22507       /* We may start in a continuation line.  If so, we have to
 22508          get the right continuation_lines_width and current_x.  */
 22509       it.continuation_lines_width = last_row->continuation_lines_width;
 22510       it.hpos = it.current_x = 0;
 22511 
 22512       /* Display the rest of the lines at the window end.  */
 22513       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22514       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 22515         {
 22516           /* Is it always sure that the display agrees with lines in
 22517              the current matrix?  I don't think so, so we mark rows
 22518              displayed invalid in the current matrix by setting their
 22519              enabled_p flag to false.  */
 22520           SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
 22521           if (display_line (&it, w->cursor.vpos))
 22522             last_text_row_at_end = it.glyph_row - 1;
 22523         }
 22524     }
 22525 
 22526   /* Update window_end_pos and window_end_vpos.  */
 22527   if (first_unchanged_at_end_row && !last_text_row_at_end)
 22528     {
 22529       /* Window end line if one of the preserved rows from the current
 22530          matrix.  Set row to the last row displaying text in current
 22531          matrix starting at first_unchanged_at_end_row, after
 22532          scrolling.  */
 22533       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22534       row = find_last_row_displaying_text (w->current_matrix, &it,
 22535                                            first_unchanged_at_end_row);
 22536       eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
 22537       adjust_window_ends (w, row, true);
 22538       eassert (w->window_end_bytepos >= 0);
 22539       IF_DEBUG (debug_method_add (w, "A"));
 22540     }
 22541   else if (last_text_row_at_end)
 22542     {
 22543       adjust_window_ends (w, last_text_row_at_end, false);
 22544       eassert (w->window_end_bytepos >= 0);
 22545       IF_DEBUG (debug_method_add (w, "B"));
 22546     }
 22547   else if (last_text_row)
 22548     {
 22549       /* We have displayed either to the end of the window or at the
 22550          end of the window, i.e. the last row with text is to be found
 22551          in the desired matrix.  */
 22552       adjust_window_ends (w, last_text_row, false);
 22553       eassert (w->window_end_bytepos >= 0);
 22554     }
 22555   else if (first_unchanged_at_end_row == NULL
 22556            && last_text_row == NULL
 22557            && last_text_row_at_end == NULL)
 22558     {
 22559       /* Displayed to end of window, but no line containing text was
 22560          displayed.  Lines were deleted at the end of the window.  */
 22561       int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
 22562       int vpos = w->window_end_vpos;
 22563       struct glyph_row *current_row = current_matrix->rows + vpos;
 22564       struct glyph_row *desired_row = desired_matrix->rows + vpos;
 22565 
 22566       for (row = NULL; !row; --vpos, --current_row, --desired_row)
 22567         {
 22568           eassert (first_vpos <= vpos);
 22569           if (desired_row->enabled_p)
 22570             {
 22571               if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
 22572                 row = desired_row;
 22573             }
 22574           else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
 22575             row  = current_row;
 22576         }
 22577 
 22578       w->window_end_vpos = vpos + 1;
 22579       w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22580       w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22581       eassert (w->window_end_bytepos >= 0);
 22582       IF_DEBUG (debug_method_add (w, "C"));
 22583     }
 22584   else
 22585     emacs_abort ();
 22586 
 22587   IF_DEBUG ((debug_end_pos = w->window_end_pos,
 22588              debug_end_vpos = w->window_end_vpos));
 22589 
 22590   /* Record that display has not been completed.  */
 22591   w->window_end_valid = false;
 22592   w->desired_matrix->no_scrolling_p = true;
 22593   return 3;
 22594 
 22595 #undef GIVE_UP
 22596 }
 22597 
 22598 
 22599 
 22600 /***********************************************************************
 22601                         More debugging support
 22602  ***********************************************************************/
 22603 
 22604 #ifdef GLYPH_DEBUG
 22605 
 22606 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
 22607 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
 22608 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
 22609 
 22610 
 22611 /* Dump the contents of glyph matrix MATRIX on stderr.
 22612 
 22613    GLYPHS 0 means don't show glyph contents.
 22614    GLYPHS 1 means show glyphs in short form
 22615    GLYPHS > 1 means show glyphs in long form.  */
 22616 
 22617 void
 22618 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
 22619 {
 22620   int i;
 22621   for (i = 0; i < matrix->nrows; ++i)
 22622     dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
 22623 }
 22624 
 22625 
 22626 /* Dump contents of glyph GLYPH to stderr.  ROW and AREA are
 22627    the glyph row and area where the glyph comes from.  */
 22628 
 22629 void
 22630 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
 22631 {
 22632   if (glyph->type == CHAR_GLYPH
 22633       || glyph->type == GLYPHLESS_GLYPH)
 22634     {
 22635       fprintf (stderr,
 22636                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22637                glyph - row->glyphs[TEXT_AREA],
 22638                (glyph->type == CHAR_GLYPH
 22639                 ? 'C'
 22640                 : 'G'),
 22641                glyph->charpos,
 22642                (BUFFERP (glyph->object)
 22643                 ? 'B'
 22644                 : (STRINGP (glyph->object)
 22645                    ? 'S'
 22646                    : (NILP (glyph->object)
 22647                       ? '0'
 22648                       : '-'))),
 22649                glyph->pixel_width,
 22650                glyph->u.ch,
 22651                (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
 22652                 ? (int) glyph->u.ch
 22653                 : '.'),
 22654                glyph->face_id,
 22655                glyph->left_box_line_p,
 22656                glyph->right_box_line_p);
 22657     }
 22658   else if (glyph->type == STRETCH_GLYPH)
 22659     {
 22660       fprintf (stderr,
 22661                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22662                glyph - row->glyphs[TEXT_AREA],
 22663                'S',
 22664                glyph->charpos,
 22665                (BUFFERP (glyph->object)
 22666                 ? 'B'
 22667                 : (STRINGP (glyph->object)
 22668                    ? 'S'
 22669                    : (NILP (glyph->object)
 22670                       ? '0'
 22671                       : '-'))),
 22672                glyph->pixel_width,
 22673                0u,
 22674                ' ',
 22675                glyph->face_id,
 22676                glyph->left_box_line_p,
 22677                glyph->right_box_line_p);
 22678     }
 22679   else if (glyph->type == IMAGE_GLYPH)
 22680     {
 22681       fprintf (stderr,
 22682                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22683                glyph - row->glyphs[TEXT_AREA],
 22684                'I',
 22685                glyph->charpos,
 22686                (BUFFERP (glyph->object)
 22687                 ? 'B'
 22688                 : (STRINGP (glyph->object)
 22689                    ? 'S'
 22690                    : (NILP (glyph->object)
 22691                       ? '0'
 22692                       : '-'))),
 22693                glyph->pixel_width,
 22694                (unsigned int) glyph->u.img_id,
 22695                '.',
 22696                glyph->face_id,
 22697                glyph->left_box_line_p,
 22698                glyph->right_box_line_p);
 22699     }
 22700   else if (glyph->type == COMPOSITE_GLYPH)
 22701     {
 22702       fprintf (stderr,
 22703                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x",
 22704                glyph - row->glyphs[TEXT_AREA],
 22705                '+',
 22706                glyph->charpos,
 22707                (BUFFERP (glyph->object)
 22708                 ? 'B'
 22709                 : (STRINGP (glyph->object)
 22710                    ? 'S'
 22711                    : (NILP (glyph->object)
 22712                       ? '0'
 22713                       : '-'))),
 22714                glyph->pixel_width,
 22715                (unsigned int) glyph->u.cmp.id);
 22716       if (glyph->u.cmp.automatic)
 22717         fprintf (stderr,
 22718                  "[%d-%d]",
 22719                  glyph->slice.cmp.from, glyph->slice.cmp.to);
 22720       fprintf (stderr, " . %4d %1.1d%1.1d\n",
 22721                glyph->face_id,
 22722                glyph->left_box_line_p,
 22723                glyph->right_box_line_p);
 22724     }
 22725   else if (glyph->type == XWIDGET_GLYPH)
 22726     {
 22727 #ifndef HAVE_XWIDGETS
 22728       eassume (false);
 22729 #else
 22730       fprintf (stderr,
 22731                "  %5td %4c %6td %c %3d %7p %c %4d %1.1d%1.1d\n",
 22732                glyph - row->glyphs[TEXT_AREA],
 22733                'X',
 22734                glyph->charpos,
 22735                (BUFFERP (glyph->object)
 22736                 ? 'B'
 22737                 : (STRINGP (glyph->object)
 22738                    ? 'S'
 22739                    : '-')),
 22740                glyph->pixel_width,
 22741                glyph->u.xwidget,
 22742                '.',
 22743                glyph->face_id,
 22744                glyph->left_box_line_p,
 22745                glyph->right_box_line_p);
 22746 #endif
 22747     }
 22748 }
 22749 
 22750 
 22751 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
 22752    GLYPHS 0 means don't show glyph contents.
 22753    GLYPHS 1 means show glyphs in short form
 22754    GLYPHS > 1 means show glyphs in long form.  */
 22755 
 22756 void
 22757 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
 22758 {
 22759   if (glyphs != 1)
 22760     {
 22761       fputs (("Row     Start       End Used oE><\\CTZFesm     X    Y    W    H    V    A    P\n"
 22762               "==============================================================================\n"),
 22763              stderr);
 22764       fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
 22765 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d  %4d %4d %4d %4d %4d %4d %4d\n",
 22766                vpos,
 22767                MATRIX_ROW_START_CHARPOS (row),
 22768                MATRIX_ROW_END_CHARPOS (row),
 22769                row->used[TEXT_AREA],
 22770                row->contains_overlapping_glyphs_p,
 22771                row->enabled_p,
 22772                row->truncated_on_left_p,
 22773                row->truncated_on_right_p,
 22774                row->continued_p,
 22775                MATRIX_ROW_CONTINUATION_LINE_P (row),
 22776                MATRIX_ROW_DISPLAYS_TEXT_P (row),
 22777                row->ends_at_zv_p,
 22778                row->fill_line_p,
 22779                row->ends_in_middle_of_char_p,
 22780                row->starts_in_middle_of_char_p,
 22781                row->mouse_face_p,
 22782                row->x,
 22783                row->y,
 22784                row->pixel_width,
 22785                row->height,
 22786                row->visible_height,
 22787                row->ascent,
 22788                row->phys_ascent);
 22789       /* The next 3 lines should align to "Start" in the header.  */
 22790       fprintf (stderr, "    %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
 22791                row->end.overlay_string_index,
 22792                row->continuation_lines_width);
 22793       fprintf (stderr, "    %9"pD"d %9"pD"d\n",
 22794                CHARPOS (row->start.string_pos),
 22795                CHARPOS (row->end.string_pos));
 22796       fprintf (stderr, "    %9d %9d\n", row->start.dpvec_index,
 22797                row->end.dpvec_index);
 22798     }
 22799 
 22800   if (glyphs > 1)
 22801     {
 22802       int area;
 22803 
 22804       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22805         {
 22806           struct glyph *glyph = row->glyphs[area];
 22807           struct glyph *glyph_end = glyph + row->used[area];
 22808 
 22809           /* Glyph for a line end in text.  */
 22810           if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
 22811             ++glyph_end;
 22812 
 22813           if (glyph < glyph_end)
 22814             fputs (" Glyph#  Type       Pos   O   W     Code      C Face LR\n",
 22815                    stderr);
 22816 
 22817           for (; glyph < glyph_end; ++glyph)
 22818             dump_glyph (row, glyph, area);
 22819         }
 22820     }
 22821   else if (glyphs == 1)
 22822     {
 22823       int area;
 22824       char s[SHRT_MAX + 4];
 22825 
 22826       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22827         {
 22828           int i;
 22829 
 22830           for (i = 0; i < row->used[area]; ++i)
 22831             {
 22832               struct glyph *glyph = row->glyphs[area] + i;
 22833               if (i == row->used[area] - 1
 22834                   && area == TEXT_AREA
 22835                   && NILP (glyph->object)
 22836                   && glyph->type == CHAR_GLYPH
 22837                   && glyph->u.ch == ' ')
 22838                 {
 22839                   strcpy (&s[i], "[\\n]");
 22840                   i += 4;
 22841                 }
 22842               else if (glyph->type == CHAR_GLYPH
 22843                        && glyph->u.ch < 0x80
 22844                        && glyph->u.ch >= ' ')
 22845                 s[i] = glyph->u.ch;
 22846               else
 22847                 s[i] = '.';
 22848             }
 22849 
 22850           s[i] = '\0';
 22851           fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
 22852         }
 22853     }
 22854 }
 22855 
 22856 
 22857 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 22858        Sdump_glyph_matrix, 0, 1, "p",
 22859        doc: /* Dump the current matrix of the selected window to stderr.
 22860 Shows contents of glyph row structures.  With non-nil
 22861 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
 22862 glyphs in short form, otherwise show glyphs in long form.
 22863 
 22864 Interactively, no argument means show glyphs in short form;
 22865 with numeric argument, its value is passed as the GLYPHS flag.  */)
 22866   (Lisp_Object glyphs)
 22867 {
 22868   struct window *w = XWINDOW (selected_window);
 22869   struct buffer *buffer = XBUFFER (w->contents);
 22870 
 22871   fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
 22872            BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
 22873   fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
 22874            w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
 22875   fputs ("=============================================\n", stderr);
 22876   dump_glyph_matrix (w->current_matrix,
 22877                      TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 0);
 22878   return Qnil;
 22879 }
 22880 
 22881 
 22882 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
 22883        Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
 22884 Only text-mode frames have frame glyph matrices.  */)
 22885   (void)
 22886 {
 22887   struct frame *f = XFRAME (selected_frame);
 22888 
 22889   if (f->current_matrix)
 22890     dump_glyph_matrix (f->current_matrix, 1);
 22891   else
 22892     fputs ("*** This frame doesn't have a frame glyph matrix ***\n", stderr);
 22893   return Qnil;
 22894 }
 22895 
 22896 
 22897 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
 22898        doc: /* Dump glyph row ROW to stderr.
 22899 Interactively, ROW is the prefix numeric argument and defaults to
 22900 the row which displays point.
 22901 Optional argument GLYPHS 0 means don't dump glyphs.
 22902 GLYPHS 1 means dump glyphs in short form.
 22903 GLYPHS > 1 or omitted means dump glyphs in long form.  */)
 22904   (Lisp_Object row, Lisp_Object glyphs)
 22905 {
 22906   struct glyph_matrix *matrix;
 22907   EMACS_INT vpos;
 22908 
 22909   if (NILP (row))
 22910     {
 22911       int d1, d2, d3, d4, d5, ypos;
 22912       bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
 22913                                       &d1, &d2, &d3, &d4, &d5, &ypos);
 22914       if (visible_p)
 22915         vpos = ypos;
 22916       else
 22917         vpos = 0;
 22918     }
 22919   else
 22920     {
 22921       CHECK_FIXNUM (row);
 22922       vpos = XFIXNUM (row);
 22923     }
 22924   matrix = XWINDOW (selected_window)->current_matrix;
 22925   if (vpos >= 0 && vpos < matrix->nrows)
 22926     dump_glyph_row (MATRIX_ROW (matrix, vpos),
 22927                     vpos,
 22928                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22929   return Qnil;
 22930 }
 22931 
 22932 
 22933 DEFUN ("dump-tab-bar-row", Fdump_tab_bar_row, Sdump_tab_bar_row, 1, 2, "P",
 22934        doc: /* Dump glyph row ROW of the tab-bar of the current frame to stderr.
 22935 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22936 GLYPHS 0 means don't dump glyphs.
 22937 GLYPHS 1 means dump glyphs in short form.
 22938 GLYPHS > 1 or omitted means dump glyphs in long form.
 22939 
 22940 If there's no tab-bar, or if the tab-bar is not drawn by Emacs,
 22941 do nothing.  */)
 22942   (Lisp_Object row, Lisp_Object glyphs)
 22943 {
 22944 #if defined (HAVE_WINDOW_SYSTEM)
 22945   struct frame *sf = SELECTED_FRAME ();
 22946   struct glyph_matrix *m = WINDOWP (sf->tab_bar_window)
 22947     ? XWINDOW (sf->tab_bar_window)->current_matrix
 22948     : sf->current_matrix;
 22949   EMACS_INT vpos;
 22950 
 22951   if (NILP (row))
 22952     vpos = WINDOWP (sf->tab_bar_window) ? 0 :
 22953       FRAME_MENU_BAR_LINES (sf) > 0 ? 1 : 0;
 22954   else
 22955     {
 22956       CHECK_FIXNUM (row);
 22957       vpos = XFIXNUM (row);
 22958     }
 22959   if (vpos >= 0 && vpos < m->nrows)
 22960     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22961                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22962 #endif
 22963   return Qnil;
 22964 }
 22965 
 22966 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
 22967        doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
 22968 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22969 GLYPHS 0 means don't dump glyphs.
 22970 GLYPHS 1 means dump glyphs in short form.
 22971 GLYPHS > 1 or omitted means dump glyphs in long form.
 22972 
 22973 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
 22974 do nothing.  */)
 22975   (Lisp_Object row, Lisp_Object glyphs)
 22976 {
 22977 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 22978   struct frame *sf = SELECTED_FRAME ();
 22979   struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 22980   EMACS_INT vpos;
 22981 
 22982   if (NILP (row))
 22983     vpos = 0;
 22984   else
 22985     {
 22986       CHECK_FIXNUM (row);
 22987       vpos = XFIXNUM (row);
 22988     }
 22989   if (vpos >= 0 && vpos < m->nrows)
 22990     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22991                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22992 #endif
 22993   return Qnil;
 22994 }
 22995 
 22996 
 22997 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
 22998        doc: /* Toggle tracing of redisplay.
 22999 With ARG, turn tracing on if and only if ARG is positive.  */)
 23000   (Lisp_Object arg)
 23001 {
 23002   if (NILP (arg))
 23003     trace_redisplay_p = !trace_redisplay_p;
 23004   else
 23005     {
 23006       arg = Fprefix_numeric_value (arg);
 23007       trace_redisplay_p = XFIXNUM (arg) > 0;
 23008     }
 23009 
 23010   return Qnil;
 23011 }
 23012 
 23013 
 23014 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
 23015        doc: /* Like `format', but print result to stderr.
 23016 usage: (trace-to-stderr STRING &rest OBJECTS)  */)
 23017   (ptrdiff_t nargs, Lisp_Object *args)
 23018 {
 23019   Lisp_Object s = Fformat (nargs, args);
 23020   fwrite (SDATA (s), 1, SBYTES (s), stderr);
 23021   return Qnil;
 23022 }
 23023 
 23024 #endif /* GLYPH_DEBUG */
 23025 
 23026 
 23027 
 23028 /***********************************************************************
 23029                      Building Desired Matrix Rows
 23030  ***********************************************************************/
 23031 
 23032 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
 23033    Used for non-window-redisplay windows, and for windows without left
 23034    fringe.  */
 23035 
 23036 static struct glyph_row *
 23037 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 23038 {
 23039   struct frame *f = XFRAME (WINDOW_FRAME (w));
 23040   struct buffer *buffer = XBUFFER (w->contents);
 23041   struct buffer *old = current_buffer;
 23042   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
 23043   ptrdiff_t arrow_len = SBYTES (overlay_arrow_string), char_num = 0;
 23044   const unsigned char *arrow_end = arrow_string + arrow_len;
 23045   const unsigned char *p;
 23046   struct it it;
 23047   bool multibyte_p;
 23048   int n_glyphs_before;
 23049 
 23050   set_buffer_temp (buffer);
 23051   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
 23052   scratch_glyph_row.reversed_p = false;
 23053   it.glyph_row->used[TEXT_AREA] = 0;
 23054   SET_TEXT_POS (it.position, 0, 0);
 23055 
 23056   multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
 23057   p = arrow_string;
 23058   while (p < arrow_end)
 23059     {
 23060       Lisp_Object face, ilisp;
 23061 
 23062       /* Get the next character.  */
 23063       if (multibyte_p)
 23064         it.c = it.char_to_display = check_char_and_length (p, &it.len);
 23065       else
 23066         {
 23067           it.c = it.char_to_display = *p, it.len = 1;
 23068           if (! ASCII_CHAR_P (it.c))
 23069             it.char_to_display = BYTE8_TO_CHAR (it.c);
 23070         }
 23071       p += it.len;
 23072 
 23073       /* Get its face.  */
 23074       ilisp = make_fixnum (char_num++);
 23075       face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
 23076       it.face_id = compute_char_face (f, it.char_to_display, face);
 23077 
 23078       /* Compute its width, get its glyphs.  */
 23079       n_glyphs_before = it.glyph_row->used[TEXT_AREA];
 23080       SET_TEXT_POS (it.position, -1, -1);
 23081       PRODUCE_GLYPHS (&it);
 23082 
 23083       /* If this character doesn't fit any more in the line, we have
 23084          to remove some glyphs.  */
 23085       if (it.current_x > it.last_visible_x)
 23086         {
 23087           it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
 23088           break;
 23089         }
 23090     }
 23091 
 23092   set_buffer_temp (old);
 23093   return it.glyph_row;
 23094 }
 23095 
 23096 
 23097 /* Insert truncation glyphs at the start of IT->glyph_row.  Which
 23098    glyphs to insert is determined by produce_special_glyphs.  */
 23099 
 23100 static void
 23101 insert_left_trunc_glyphs (struct it *it)
 23102 {
 23103   struct it truncate_it;
 23104   struct glyph *from, *end, *to, *toend;
 23105 
 23106   eassert (!FRAME_WINDOW_P (it->f)
 23107            || (!it->glyph_row->reversed_p
 23108                && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23109            || (it->glyph_row->reversed_p
 23110                && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
 23111 
 23112   /* Get the truncation glyphs.  */
 23113   truncate_it = *it;
 23114   truncate_it.current_x = 0;
 23115   truncate_it.face_id = DEFAULT_FACE_ID;
 23116   truncate_it.glyph_row = &scratch_glyph_row;
 23117   truncate_it.area = TEXT_AREA;
 23118   truncate_it.glyph_row->used[TEXT_AREA] = 0;
 23119   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
 23120   truncate_it.object = Qnil;
 23121   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 23122 
 23123   /* Overwrite glyphs from IT with truncation glyphs.  */
 23124   if (!it->glyph_row->reversed_p)
 23125     {
 23126       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 23127 
 23128       from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23129       end = from + tused;
 23130       to = it->glyph_row->glyphs[TEXT_AREA];
 23131       toend = to + it->glyph_row->used[TEXT_AREA];
 23132       if (FRAME_WINDOW_P (it->f))
 23133         {
 23134           /* On GUI frames, when variable-size fonts are displayed,
 23135              the truncation glyphs may need more pixels than the row's
 23136              glyphs they overwrite.  We overwrite more glyphs to free
 23137              enough screen real estate, and enlarge the stretch glyph
 23138              on the right (see display_line), if there is one, to
 23139              preserve the screen position of the truncation glyphs on
 23140              the right.  */
 23141           int w = 0;
 23142           struct glyph *g = to;
 23143           short used;
 23144 
 23145           /* The first glyph could be partially visible, in which case
 23146              it->glyph_row->x will be negative.  But we want the left
 23147              truncation glyphs to be aligned at the left margin of the
 23148              window, so we override the x coordinate at which the row
 23149              will begin.  */
 23150           it->glyph_row->x = 0;
 23151           while (g < toend && w < it->truncation_pixel_width)
 23152             {
 23153               w += g->pixel_width;
 23154               ++g;
 23155             }
 23156           if (g - to - tused > 0)
 23157             {
 23158               memmove (to + tused, g, (toend - g) * sizeof(*g));
 23159               it->glyph_row->used[TEXT_AREA] -= g - to - tused;
 23160             }
 23161           used = it->glyph_row->used[TEXT_AREA];
 23162           if (it->glyph_row->truncated_on_right_p
 23163               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
 23164               && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
 23165               == STRETCH_GLYPH)
 23166             {
 23167               int extra = w - it->truncation_pixel_width;
 23168 
 23169               it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
 23170             }
 23171         }
 23172 
 23173       while (from < end)
 23174         *to++ = *from++;
 23175 
 23176       /* There may be padding glyphs left over.  Overwrite them too.  */
 23177       if (!FRAME_WINDOW_P (it->f))
 23178         {
 23179           while (to < toend && CHAR_GLYPH_PADDING_P (*to))
 23180             {
 23181               from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23182               while (from < end)
 23183                 *to++ = *from++;
 23184             }
 23185         }
 23186 
 23187       if (to > toend)
 23188         it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
 23189     }
 23190   else
 23191     {
 23192       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 23193 
 23194       /* In R2L rows, overwrite the last (rightmost) glyphs, and do
 23195          that back to front.  */
 23196       end = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23197       from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23198       toend = it->glyph_row->glyphs[TEXT_AREA];
 23199       to = toend + it->glyph_row->used[TEXT_AREA] - 1;
 23200       if (FRAME_WINDOW_P (it->f))
 23201         {
 23202           int w = 0;
 23203           struct glyph *g = to;
 23204 
 23205           while (g >= toend && w < it->truncation_pixel_width)
 23206             {
 23207               w += g->pixel_width;
 23208               --g;
 23209             }
 23210           if (to - g - tused > 0)
 23211             to = g + tused;
 23212           if (it->glyph_row->truncated_on_right_p
 23213               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
 23214               && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
 23215             {
 23216               int extra = w - it->truncation_pixel_width;
 23217 
 23218               it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
 23219             }
 23220         }
 23221 
 23222       while (from >= end && to >= toend)
 23223         *to-- = *from--;
 23224       if (!FRAME_WINDOW_P (it->f))
 23225         {
 23226           while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
 23227             {
 23228               from =
 23229                 truncate_it.glyph_row->glyphs[TEXT_AREA]
 23230                 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23231               while (from >= end && to >= toend)
 23232                 *to-- = *from--;
 23233             }
 23234         }
 23235       if (from >= end)
 23236         {
 23237           /* Need to free some room before prepending additional
 23238              glyphs.  */
 23239           int move_by = from - end + 1;
 23240           struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
 23241           struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
 23242 
 23243           for ( ; g >= g0; g--)
 23244             g[move_by] = *g;
 23245           while (from >= end)
 23246             *to-- = *from--;
 23247           it->glyph_row->used[TEXT_AREA] += move_by;
 23248         }
 23249     }
 23250 }
 23251 
 23252 /* Compute the hash code for ROW.  */
 23253 unsigned
 23254 row_hash (struct glyph_row *row)
 23255 {
 23256   int area, k;
 23257   unsigned hashval = 0;
 23258 
 23259   for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 23260     for (k = 0; k < row->used[area]; ++k)
 23261       hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
 23262                   + row->glyphs[area][k].u.val
 23263                   + row->glyphs[area][k].face_id
 23264                   + row->glyphs[area][k].padding_p
 23265                   + (row->glyphs[area][k].type << 2));
 23266 
 23267   return hashval;
 23268 }
 23269 
 23270 /* Compute the pixel height and width of IT->glyph_row.
 23271 
 23272    Most of the time, ascent and height of a display line will be equal
 23273    to the max_ascent and max_height values of the display iterator
 23274    structure.  This is not the case if
 23275 
 23276    1. We hit ZV without displaying anything.  In this case, max_ascent
 23277    and max_height will be zero.
 23278 
 23279    2. We have some glyphs that don't contribute to the line height.
 23280    (The glyph row flag contributes_to_line_height_p is for future
 23281    pixmap extensions).
 23282 
 23283    The first case is easily covered by using default values because in
 23284    these cases, the line height does not really matter, except that it
 23285    must not be zero.  */
 23286 
 23287 static void
 23288 compute_line_metrics (struct it *it)
 23289 {
 23290   struct glyph_row *row = it->glyph_row;
 23291 
 23292   if (FRAME_WINDOW_P (it->f))
 23293     {
 23294       int i, min_y, max_y;
 23295 
 23296       /* The line may consist of one space only, that was added to
 23297          place the cursor on it.  If so, the row's height hasn't been
 23298          computed yet.  */
 23299       if (row->height == 0)
 23300         {
 23301           if (it->max_ascent + it->max_descent == 0)
 23302             it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
 23303           row->ascent = it->max_ascent;
 23304           row->height = it->max_ascent + it->max_descent;
 23305           row->phys_ascent = it->max_phys_ascent;
 23306           row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 23307           row->extra_line_spacing = it->max_extra_line_spacing;
 23308         }
 23309 
 23310       /* Compute the width of this line.  */
 23311       row->pixel_width = row->x;
 23312       for (i = 0; i < row->used[TEXT_AREA]; ++i)
 23313         row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
 23314 
 23315       eassert (row->pixel_width >= 0);
 23316       eassert (row->ascent >= 0 && row->height > 0);
 23317 
 23318       row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
 23319                             || MATRIX_ROW_OVERLAPS_PRED_P (row));
 23320 
 23321       /* If first line's physical ascent is larger than its logical
 23322          ascent, use the physical ascent, and make the row taller.
 23323          This makes accented characters fully visible.  */
 23324       if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
 23325           && row->phys_ascent > row->ascent)
 23326         {
 23327           row->height += row->phys_ascent - row->ascent;
 23328           row->ascent = row->phys_ascent;
 23329         }
 23330 
 23331       /* Compute how much of the line is visible.  */
 23332       row->visible_height = row->height;
 23333 
 23334       min_y = WINDOW_TAB_LINE_HEIGHT (it->w) + WINDOW_HEADER_LINE_HEIGHT (it->w);
 23335       max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
 23336 
 23337       if (row->y < min_y)
 23338         row->visible_height -= min_y - row->y;
 23339       if (row->y + row->height > max_y)
 23340         row->visible_height -= row->y + row->height - max_y;
 23341     }
 23342   else
 23343     {
 23344       row->pixel_width = row->used[TEXT_AREA];
 23345       if (row->continued_p)
 23346         row->pixel_width -= it->continuation_pixel_width;
 23347       else if (row->truncated_on_right_p)
 23348         row->pixel_width -= it->truncation_pixel_width;
 23349       row->ascent = row->phys_ascent = 0;
 23350       row->height = row->phys_height = row->visible_height = 1;
 23351       row->extra_line_spacing = 0;
 23352     }
 23353 
 23354   /* Compute a hash code for this row.  */
 23355   row->hash = row_hash (row);
 23356 
 23357   it->max_ascent = it->max_descent = 0;
 23358   it->max_phys_ascent = it->max_phys_descent = 0;
 23359 }
 23360 
 23361 
 23362 static void
 23363 clear_position (struct it *it)
 23364 {
 23365   it->position.charpos = 0;
 23366   it->position.bytepos = 0;
 23367 }
 23368 
 23369 /* Append one space to the glyph row of iterator IT if doing a
 23370    window-based redisplay.  The space has the same face as
 23371    IT->face_id.  Value is true if a space was added.
 23372 
 23373    This function is called to make sure that there is always one glyph
 23374    at the end of a glyph row that the cursor can be set on under
 23375    window-systems.  (If there weren't such a glyph we would not know
 23376    how wide and tall a box cursor should be displayed).
 23377 
 23378    At the same time this space let's a nicely handle clearing to the
 23379    end of the line if the row ends in italic text.  */
 23380 
 23381 static bool
 23382 append_space_for_newline (struct it *it, bool default_face_p)
 23383 {
 23384   int n = it->glyph_row->used[TEXT_AREA];
 23385 
 23386   if (it->glyph_row->glyphs[TEXT_AREA] + n
 23387       < it->glyph_row->glyphs[1 + TEXT_AREA])
 23388     {
 23389       /* Save some values that must not be changed.
 23390          Must save IT->c and IT->len because otherwise
 23391          ITERATOR_AT_END_P wouldn't work anymore after
 23392          append_space_for_newline has been called.  */
 23393       enum display_element_type saved_what = it->what;
 23394       int saved_c = it->c, saved_len = it->len;
 23395       int saved_char_to_display = it->char_to_display;
 23396       int saved_x = it->current_x;
 23397       const int saved_face_id = it->face_id;
 23398       bool saved_box_end = it->end_of_box_run_p;
 23399       struct text_pos saved_pos = it->position;
 23400       Lisp_Object saved_object = it->object;
 23401       struct face *face;
 23402 
 23403       it->what = IT_CHARACTER;
 23404       clear_position (it);
 23405       it->object = Qnil;
 23406       it->len = 1;
 23407 
 23408       int char_width = 1;
 23409 
 23410       if (default_face_p
 23411 #ifdef HAVE_WINDOW_SYSTEM
 23412           || FRAME_WINDOW_P (it->f)
 23413 #endif
 23414           )
 23415         {
 23416           const int local_default_face_id =
 23417             lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
 23418 
 23419 #ifdef HAVE_WINDOW_SYSTEM
 23420           if (FRAME_WINDOW_P (it->f))
 23421             {
 23422               struct face *default_face
 23423                 = FACE_FROM_ID (it->f, local_default_face_id);
 23424               struct font *font = (default_face->font
 23425                                    ? default_face->font
 23426                                    : FRAME_FONT (it->f));
 23427               char_width = (font->average_width
 23428                             ? font->average_width
 23429                             : font->space_width);
 23430             }
 23431 #endif
 23432           if (default_face_p)
 23433             it->face_id = local_default_face_id;
 23434         }
 23435       /* Corner case for when display-fill-column-indicator-mode
 23436          is active and the extra character should be added in the
 23437          same place than the line.  */
 23438 
 23439       const int indicator_column =
 23440         fill_column_indicator_column (it, char_width);
 23441       int saved_end_of_box_run = it->end_of_box_run_p;
 23442       bool should_keep_end_of_box_run = false;
 23443 
 23444       if (it->current_x == indicator_column)
 23445         {
 23446           it->c = it->char_to_display
 23447             = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23448           it->face_id
 23449             = merge_faces (it->w, Qfill_column_indicator,
 23450                            0, saved_face_id);
 23451           face = FACE_FROM_ID (it->f, it->face_id);
 23452         }
 23453       else
 23454         {
 23455           it->c = it->char_to_display = ' ';
 23456           /* If the default face was remapped, be sure to use the
 23457              remapped face for the appended newline.  */
 23458 
 23459           face = FACE_FROM_ID (it->f, it->face_id);
 23460           it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
 23461           /* In R2L rows, we will prepend a stretch glyph that will
 23462              have the end_of_box_run_p flag set for it, so there's no
 23463              need for the appended newline glyph to have that flag
 23464              set.  */
 23465           if (!(it->glyph_row->reversed_p
 23466                 /* But if the appended newline glyph goes all the way to
 23467                    the end of the row, there will be no stretch glyph,
 23468                    so leave the box flag set.  */
 23469                 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x))
 23470             should_keep_end_of_box_run = true;
 23471         }
 23472       PRODUCE_GLYPHS (it);
 23473       /* Restore the end_of_box_run_p flag which was reset by
 23474          PRODUCE_GLYPHS.  */
 23475       if (should_keep_end_of_box_run)
 23476         it->end_of_box_run_p = saved_end_of_box_run;
 23477 #ifdef HAVE_WINDOW_SYSTEM
 23478       if (FRAME_WINDOW_P (it->f))
 23479         {
 23480           /* Make sure this space glyph has the right ascent and
 23481              descent values, or else cursor at end of line will look
 23482              funny, and height of empty lines will be incorrect.  */
 23483           struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
 23484           struct font *font = face->font ? face->font : FRAME_FONT (it->f);
 23485           if (n == 0)
 23486             {
 23487               Lisp_Object height, total_height;
 23488               int extra_line_spacing = it->extra_line_spacing;
 23489               int boff = font->baseline_offset;
 23490 
 23491               if (font->vertical_centering)
 23492                 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 23493 
 23494               it->object = saved_object; /* get_it_property needs this */
 23495               normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 23496               /* Must do a subset of line height processing from
 23497                  gui_produce_glyph for newline characters.  */
 23498               height = get_it_property (it, Qline_height);
 23499               if (CONSP (height)
 23500                   && CONSP (XCDR (height))
 23501                   && NILP (XCDR (XCDR (height))))
 23502                 {
 23503                   total_height = XCAR (XCDR (height));
 23504                   height = XCAR (height);
 23505                 }
 23506               else
 23507                 total_height = Qnil;
 23508               height = calc_line_height_property (it, height, font, boff, true);
 23509 
 23510               if (it->override_ascent >= 0)
 23511                 {
 23512                   it->ascent = it->override_ascent;
 23513                   it->descent = it->override_descent;
 23514                   boff = it->override_boff;
 23515                 }
 23516               if (EQ (height, Qt))
 23517                 extra_line_spacing = 0;
 23518               else
 23519                 {
 23520                   Lisp_Object spacing;
 23521 
 23522                   it->phys_ascent = it->ascent;
 23523                   it->phys_descent = it->descent;
 23524                   if (!NILP (height)
 23525                       && XFIXNUM (height) > it->ascent + it->descent)
 23526                     it->ascent = XFIXNUM (height) - it->descent;
 23527 
 23528                   if (!NILP (total_height))
 23529                     spacing = calc_line_height_property (it, total_height, font,
 23530                                                          boff, false);
 23531                   else
 23532                     {
 23533                       spacing = get_it_property (it, Qline_spacing);
 23534                       spacing = calc_line_height_property (it, spacing, font,
 23535                                                            boff, false);
 23536                     }
 23537                   if (FIXNUMP (spacing))
 23538                     {
 23539                       extra_line_spacing = XFIXNUM (spacing);
 23540                       if (!NILP (total_height))
 23541                         extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 23542                     }
 23543                 }
 23544               if (extra_line_spacing > 0)
 23545                 {
 23546                   it->descent += extra_line_spacing;
 23547                   if (extra_line_spacing > it->max_extra_line_spacing)
 23548                     it->max_extra_line_spacing = extra_line_spacing;
 23549                 }
 23550               it->max_ascent = it->ascent;
 23551               it->max_descent = it->descent;
 23552               /* Make sure compute_line_metrics recomputes the row height.  */
 23553               it->glyph_row->height = 0;
 23554             }
 23555 
 23556           g->ascent = it->max_ascent;
 23557           g->descent = it->max_descent;
 23558         }
 23559 #endif /* HAVE_WINDOW_SYSTEM  */
 23560       it->override_ascent = -1;
 23561       it->constrain_row_ascent_descent_p = false;
 23562       it->current_x = saved_x;
 23563       it->object = saved_object;
 23564       it->position = saved_pos;
 23565       it->what = saved_what;
 23566       it->face_id = saved_face_id;
 23567       it->len = saved_len;
 23568       it->c = saved_c;
 23569       it->char_to_display = saved_char_to_display;
 23570       it->end_of_box_run_p = saved_box_end;
 23571       return true;
 23572     }
 23573 
 23574   return false;
 23575 }
 23576 
 23577 
 23578 /* Extend the face of the last glyph in the text area of IT->glyph_row
 23579    to the end of the display line.  Called from display_line.  If the
 23580    glyph row is empty, add a space glyph to it so that we know the
 23581    face to draw.  Set the glyph row flag fill_line_p.  If the glyph
 23582    row is R2L, prepend a stretch glyph to cover the empty space to the
 23583    left of the leftmost glyph.  */
 23584 
 23585 static void
 23586 extend_face_to_end_of_line (struct it *it)
 23587 {
 23588   struct frame *f = it->f;
 23589 
 23590   /* If line is already filled, do nothing.  Non window-system frames
 23591      get a grace of one more ``pixel'' because their characters are
 23592      1-``pixel'' wide, so they hit the equality too early.  This grace
 23593      is needed only for R2L rows that are not continued, to produce
 23594      one extra blank where we could display the cursor.  */
 23595   if ((it->current_x >= it->last_visible_x
 23596        + (!FRAME_WINDOW_P (f)
 23597           && it->glyph_row->reversed_p
 23598           && !it->glyph_row->continued_p))
 23599       /* If the window has display margins, we will need to extend
 23600          their face even if the text area is filled.  */
 23601       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23602            || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
 23603     return;
 23604 
 23605   specpdl_ref count = SPECPDL_INDEX ();
 23606 
 23607   /* Don't allow the user to quit out of face-merging code, in case
 23608      this is called when redisplaying a non-selected window, with
 23609      point temporarily moved to window-point.  */
 23610   specbind (Qinhibit_quit, Qt);
 23611   /* The default face, possibly remapped. */
 23612   struct face *default_face =
 23613     FACE_FROM_ID_OR_NULL (f, lookup_basic_face (it->w, f, DEFAULT_FACE_ID));
 23614   if (!default_face)
 23615     return;
 23616 
 23617   const int extend_face_id =
 23618     (it->face_id == default_face->id || it->s != NULL)
 23619     ? it->face_id
 23620     : (it->glyph_row->ends_at_zv_p
 23621        ? default_face->id
 23622        : face_at_pos (it, LFACE_EXTEND_INDEX));
 23623   unbind_to (count, Qnil);
 23624 
 23625   /* Face extension extends the background and box of IT->extend_face_id
 23626      to the end of the line.  If the background equals the background
 23627      of the frame, we don't have to do anything.  */
 23628   struct face *face = FACE_FROM_ID (f, (it->face_before_selective_p
 23629                                         ? it->saved_face_id
 23630                                         : extend_face_id));
 23631 
 23632   if (FRAME_WINDOW_P (f)
 23633       && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
 23634       && face->box == FACE_NO_BOX
 23635       && face->underline == FACE_NO_UNDERLINE
 23636       && !face->overline_p
 23637       && !face->strike_through_p
 23638       && face->background == FRAME_BACKGROUND_PIXEL (f)
 23639 #ifdef HAVE_WINDOW_SYSTEM
 23640       && !face->stipple
 23641 #endif
 23642       && !it->glyph_row->reversed_p
 23643       && !display_fill_column_indicator)
 23644     return;
 23645 
 23646   /* Set the glyph row flag indicating that the face of the last glyph
 23647      in the text area has to be drawn to the end of the text area.  */
 23648   it->glyph_row->fill_line_p = true;
 23649 
 23650   const int orig_face_id = it->face_id;
 23651   /* If current character of IT is not ASCII, make sure we have the
 23652      ASCII face.  This will be automatically undone the next time
 23653      get_next_display_element returns a multibyte character.  Note
 23654      that the character will always be single byte in unibyte
 23655      text.  */
 23656   if (!ASCII_CHAR_P (it->c))
 23657     it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
 23658 
 23659 
 23660 #ifdef HAVE_WINDOW_SYSTEM
 23661   if (FRAME_WINDOW_P (f))
 23662     {
 23663       /* If the row is empty, add a space with the current face of IT,
 23664          so that we know which face to draw.  */
 23665       if (it->glyph_row->used[TEXT_AREA] == 0)
 23666         {
 23667           it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
 23668           it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
 23669           it->glyph_row->used[TEXT_AREA] = 1;
 23670         }
 23671       /* Mode line and the header line don't have margins, and
 23672          likewise the frame's tool-bar window, if there is any.  */
 23673       if (!(it->glyph_row->mode_line_p
 23674             || (WINDOWP (f->tab_bar_window)
 23675                 && it->w == XWINDOW (f->tab_bar_window))
 23676 #ifndef HAVE_EXT_TOOL_BAR
 23677             || (WINDOWP (f->tool_bar_window)
 23678                 && it->w == XWINDOW (f->tool_bar_window))
 23679 #endif
 23680             ))
 23681         {
 23682           if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23683               && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
 23684             {
 23685               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 23686               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
 23687                 default_face->id;
 23688               it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 23689             }
 23690           if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23691               && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
 23692             {
 23693               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 23694               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
 23695                 default_face->id;
 23696               it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 23697             }
 23698 
 23699           struct font *font = (default_face->font
 23700                                ? default_face->font
 23701                                : FRAME_FONT (f));
 23702 
 23703           const int char_width = (font->average_width
 23704                                   ? font->average_width
 23705                                   : font->space_width);
 23706 
 23707           const int indicator_column =
 23708             fill_column_indicator_column (it, char_width);
 23709 
 23710           const char saved_char = it->char_to_display;
 23711           const struct text_pos saved_pos = it->position;
 23712           const bool saved_avoid_cursor = it->avoid_cursor_p;
 23713           const bool saved_box_start = it->start_of_box_run_p;
 23714           Lisp_Object save_object = it->object;
 23715           const int saved_face_id = it->face_id;
 23716 
 23717           it->face_id = extend_face_id;
 23718           it->avoid_cursor_p = true;
 23719           it->object = Qnil;
 23720 
 23721           const int stretch_height = it->ascent + it->descent;
 23722           const int stretch_ascent =
 23723             (stretch_height * FONT_BASE (font)) / FONT_HEIGHT (font);
 23724 
 23725           if (indicator_column >= 0
 23726               && indicator_column > it->current_x
 23727               && indicator_column < it->last_visible_x)
 23728             {
 23729 
 23730               /* Here we subtract char_width because we want the
 23731                  column indicator in the column INDICATOR_COLUMN,
 23732                  not after it.  */
 23733               const int stretch_width =
 23734                 indicator_column - it->current_x - char_width;
 23735 
 23736               clear_position (it);
 23737 
 23738               /* Only generate a stretch glyph if there is distance
 23739                  between current_x and the indicator position.  */
 23740               if (stretch_width > 0)
 23741                 {
 23742                   append_stretch_glyph (it, Qnil, stretch_width,
 23743                                         stretch_height, stretch_ascent);
 23744                 }
 23745 
 23746               /* Generate the glyph indicator only if
 23747                  append_space_for_newline didn't already.  */
 23748               if (it->current_x < indicator_column)
 23749                 {
 23750                   const int save_face_id = it->face_id;
 23751                   const int save_ascent = it->ascent;
 23752                   const int save_descent = it->descent;
 23753                   it->char_to_display
 23754                     = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23755                   it->face_id
 23756                     = merge_faces (it->w, Qfill_column_indicator,
 23757                                    0, extend_face_id);
 23758                   PRODUCE_GLYPHS (it);
 23759                   it->face_id = save_face_id;
 23760                   it->ascent = save_ascent;
 23761                   it->descent = save_descent;
 23762                 }
 23763             }
 23764 
 23765           /* Fill space until window edge with the merged face.  Do that
 23766              only for L2R rows, as R2L rows are handled specially below.  */
 23767           if (!it->glyph_row->reversed_p)
 23768             {
 23769               const int stretch_width = it->last_visible_x - it->current_x;
 23770 
 23771               if (stretch_width > 0)
 23772                 {
 23773                   clear_position (it);
 23774                   append_stretch_glyph (it, Qnil, stretch_width,
 23775                                         stretch_height, stretch_ascent);
 23776                 }
 23777             }
 23778 
 23779           it->char_to_display = saved_char;
 23780           it->position = saved_pos;
 23781           it->avoid_cursor_p = saved_avoid_cursor;
 23782           it->start_of_box_run_p = saved_box_start;
 23783           it->object = save_object;
 23784           it->face_id = saved_face_id;
 23785         }
 23786       if (it->glyph_row->reversed_p)
 23787         {
 23788           /* Prepend a stretch glyph to the row, such that the
 23789              rightmost glyph will be drawn flushed all the way to the
 23790              right margin of the window.  The stretch glyph that will
 23791              occupy the empty space, if any, to the left of the
 23792              glyph.  */
 23793           struct font *font = face->font ? face->font : FRAME_FONT (f);
 23794           struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
 23795           struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
 23796           struct glyph *g;
 23797           int row_width, stretch_ascent, stretch_width;
 23798           struct text_pos saved_pos;
 23799           int saved_face_id;
 23800           bool saved_avoid_cursor, saved_box_start;
 23801 
 23802           for (row_width = 0, g = row_start; g < row_end; g++)
 23803             row_width += g->pixel_width;
 23804 
 23805           /* FIXME: There are various minor display glitches in R2L
 23806              rows when only one of the fringes is missing.  The
 23807              strange condition below produces the least bad effect.  */
 23808           if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23809               == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
 23810               || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
 23811             stretch_width = window_box_width (it->w, TEXT_AREA);
 23812           else
 23813             stretch_width = it->last_visible_x - it->first_visible_x;
 23814           stretch_width -= row_width;
 23815 
 23816           if (stretch_width > 0)
 23817             {
 23818               stretch_ascent =
 23819                 (((it->ascent + it->descent)
 23820                   * FONT_BASE (font)) / FONT_HEIGHT (font));
 23821               saved_pos = it->position;
 23822               clear_position (it);
 23823               saved_avoid_cursor = it->avoid_cursor_p;
 23824               it->avoid_cursor_p = true;
 23825               saved_face_id = it->face_id;
 23826               saved_box_start = it->start_of_box_run_p;
 23827               /* The last row's stretch glyph should get the default
 23828                  face, to avoid painting the rest of the window with
 23829                  the region face, if the region ends at ZV.  */
 23830               it->face_id = (it->glyph_row->ends_at_zv_p ?
 23831                              default_face->id : face->id);
 23832 
 23833               it->start_of_box_run_p = false;
 23834               append_stretch_glyph (it, Qnil, stretch_width,
 23835                                     it->ascent + it->descent, stretch_ascent);
 23836               it->position = saved_pos;
 23837               it->avoid_cursor_p = saved_avoid_cursor;
 23838               it->face_id = saved_face_id;
 23839               it->start_of_box_run_p = saved_box_start;
 23840             }
 23841           /* If stretch_width comes out negative, it means that the
 23842              last glyph is only partially visible.  In R2L rows, we
 23843              want the leftmost glyph to be partially visible, so we
 23844              need to give the row the corresponding left offset.  */
 23845           if (stretch_width < 0)
 23846             it->glyph_row->x = stretch_width;
 23847         }
 23848       it->face_id = orig_face_id;
 23849     }
 23850   else
 23851 #endif  /* HAVE_WINDOW_SYSTEM */
 23852     {
 23853       /* Save some values that must not be changed.  */
 23854       int saved_x = it->current_x;
 23855       struct text_pos saved_pos = it->position;
 23856       Lisp_Object saved_object = it->object;;
 23857       enum display_element_type saved_what = it->what;
 23858 
 23859       it->what = IT_CHARACTER;
 23860       clear_position (it);
 23861       it->object = Qnil;
 23862       it->c = it->char_to_display = ' ';
 23863       it->len = 1;
 23864 
 23865       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23866           && (it->glyph_row->used[LEFT_MARGIN_AREA]
 23867               < WINDOW_LEFT_MARGIN_WIDTH (it->w))
 23868           && !it->glyph_row->mode_line_p
 23869           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23870         {
 23871           struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
 23872           struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
 23873 
 23874           for (it->current_x = 0; g < e; g++)
 23875             it->current_x += g->pixel_width;
 23876 
 23877           it->area = LEFT_MARGIN_AREA;
 23878           it->face_id = default_face->id;
 23879           while (it->glyph_row->used[LEFT_MARGIN_AREA]
 23880                  < WINDOW_LEFT_MARGIN_WIDTH (it->w)
 23881                  && g < it->glyph_row->glyphs[TEXT_AREA])
 23882             {
 23883               PRODUCE_GLYPHS (it);
 23884               /* term.c:produce_glyphs advances it->current_x only for
 23885                  TEXT_AREA.  */
 23886               it->current_x += it->pixel_width;
 23887               g++;
 23888             }
 23889 
 23890           it->current_x = saved_x;
 23891           it->area = TEXT_AREA;
 23892         }
 23893 
 23894       /* The last row's blank glyphs should get the default face, to
 23895          avoid painting the rest of the window with the region face,
 23896          if the region ends at ZV.  */
 23897       it->face_id = (it->glyph_row->ends_at_zv_p ?
 23898                      default_face->id : face->id);
 23899 
 23900       /* Make sure our idea of current_x is in sync with the glyphs
 23901          actually in the glyph row.  They might differ because
 23902          append_space_for_newline can insert one glyph without
 23903          updating current_x.  */
 23904       it->current_x = it->glyph_row->used[TEXT_AREA];
 23905 
 23906       /* The above assignment causes the code below to use a
 23907          non-standard semantics of it->current_x: it is measured
 23908          relative to the beginning of the text-area, thus disregarding
 23909          the window's hscroll.  That is why we need to correct the
 23910          indicator column for the hscroll, otherwise the indicator
 23911          will not move together with the text as result of horizontal
 23912          scrolling.  */
 23913       const int indicator_column =
 23914         fill_column_indicator_column (it, 1) - it->first_visible_x;
 23915 
 23916       /* Display fill-column indicator if needed.  */
 23917       while (it->current_x <= it->last_visible_x)
 23918         {
 23919           if (it->current_x != indicator_column)
 23920             PRODUCE_GLYPHS (it);
 23921           else
 23922             {
 23923               int saved_face_id = it->face_id;
 23924               it->face_id
 23925                 = merge_faces (it->w, Qfill_column_indicator, 0, extend_face_id);
 23926               it->c = it->char_to_display
 23927                 = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23928 
 23929               PRODUCE_GLYPHS (it);
 23930 
 23931               it->face_id = saved_face_id;
 23932               it->c = it->char_to_display = ' ';
 23933             }
 23934         }
 23935 
 23936       if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23937           && (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23938               < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
 23939           && !it->glyph_row->mode_line_p
 23940           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23941         {
 23942           struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
 23943           struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
 23944 
 23945           for ( ; g < e; g++)
 23946             it->current_x += g->pixel_width;
 23947 
 23948           it->area = RIGHT_MARGIN_AREA;
 23949           it->face_id = default_face->id;
 23950           while (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23951                  < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
 23952                  && g < it->glyph_row->glyphs[LAST_AREA])
 23953             {
 23954               PRODUCE_GLYPHS (it);
 23955               it->current_x += it->pixel_width;
 23956               g++;
 23957             }
 23958 
 23959           it->area = TEXT_AREA;
 23960         }
 23961 
 23962       /* Don't count these blanks really.  It would let us insert a left
 23963          truncation glyph below and make us set the cursor on them, maybe.  */
 23964       it->current_x = saved_x;
 23965       it->object = saved_object;
 23966       it->position = saved_pos;
 23967       it->what = saved_what;
 23968       it->face_id = orig_face_id;
 23969     }
 23970 }
 23971 
 23972 
 23973 /* Value is true if text starting at CHARPOS in current_buffer is
 23974    trailing whitespace.  */
 23975 
 23976 static bool
 23977 trailing_whitespace_p (ptrdiff_t charpos)
 23978 {
 23979   ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
 23980   int c = 0;
 23981 
 23982   while (bytepos < ZV_BYTE
 23983          && (c = FETCH_BYTE (bytepos),
 23984              c == ' ' || c == '\t'))
 23985     ++bytepos;
 23986 
 23987   if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
 23988     {
 23989       if (bytepos != PT_BYTE)
 23990         return true;
 23991     }
 23992   return false;
 23993 }
 23994 
 23995 
 23996 /* Highlight trailing whitespace, if any, in row at IT.  */
 23997 
 23998 static void
 23999 highlight_trailing_whitespace (struct it *it)
 24000 {
 24001   struct glyph_row *row = it->glyph_row;
 24002   int used = row->used[TEXT_AREA];
 24003 
 24004   if (used)
 24005     {
 24006       struct glyph *start = row->glyphs[TEXT_AREA];
 24007       struct glyph *glyph = start + used - 1;
 24008 
 24009       if (row->reversed_p)
 24010         {
 24011           /* Right-to-left rows need to be processed in the opposite
 24012              direction, so swap the edge pointers. */
 24013           glyph = start;
 24014           start = row->glyphs[TEXT_AREA] + used - 1;
 24015         }
 24016 
 24017       /* Skip over glyphs inserted to display the cursor at the
 24018          end of a line, for extending the face of the last glyph
 24019          to the end of the line on terminals, and for truncation
 24020          and continuation glyphs.  */
 24021       if (!row->reversed_p)
 24022         {
 24023           while (glyph >= start
 24024                  && (glyph->type == CHAR_GLYPH
 24025                      || glyph->type == STRETCH_GLYPH)
 24026                  && NILP (glyph->object))
 24027             --glyph;
 24028         }
 24029       else
 24030         {
 24031           while (glyph <= start
 24032                  && (glyph->type == CHAR_GLYPH || glyph->type == STRETCH_GLYPH)
 24033                  && NILP (glyph->object))
 24034             ++glyph;
 24035         }
 24036 
 24037       /* If last glyph is a space or stretch, and it's trailing
 24038          whitespace, set the face of all trailing whitespace glyphs in
 24039          IT->glyph_row to `trailing-whitespace'.  */
 24040       if ((row->reversed_p ? glyph <= start : glyph >= start)
 24041           && BUFFERP (glyph->object)
 24042           && (glyph->type == STRETCH_GLYPH
 24043               || (glyph->type == CHAR_GLYPH
 24044                   && glyph->u.ch == ' '))
 24045           && trailing_whitespace_p (glyph->charpos))
 24046         {
 24047           int face_id = lookup_named_face (it->w, it->f, Qtrailing_whitespace, false);
 24048           if (face_id < 0)
 24049             return;
 24050 
 24051           if (!row->reversed_p)
 24052             {
 24053               while (glyph >= start
 24054                      && BUFFERP (glyph->object)
 24055                      && (glyph->type == STRETCH_GLYPH
 24056                          || (glyph->type == CHAR_GLYPH
 24057                              && glyph->u.ch == ' ')))
 24058                 (glyph--)->face_id = face_id;
 24059             }
 24060           else
 24061             {
 24062               while (glyph <= start
 24063                      && BUFFERP (glyph->object)
 24064                      && (glyph->type == STRETCH_GLYPH
 24065                          || (glyph->type == CHAR_GLYPH
 24066                              && glyph->u.ch == ' ')))
 24067                 (glyph++)->face_id = face_id;
 24068             }
 24069         }
 24070     }
 24071 }
 24072 
 24073 
 24074 /* Value is true if glyph row ROW should be
 24075    considered to hold the buffer position CHARPOS.  */
 24076 
 24077 static bool
 24078 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
 24079 {
 24080   bool result = true;
 24081 
 24082   if (charpos == CHARPOS (row->end.pos)
 24083       || charpos == MATRIX_ROW_END_CHARPOS (row))
 24084     {
 24085       /* Suppose the row ends on a string.
 24086          Unless the row is continued, that means it ends on a newline
 24087          in the string.  If it's anything other than a display string
 24088          (e.g., a before-string from an overlay), we don't want the
 24089          cursor there.  (This heuristic seems to give the optimal
 24090          behavior for the various types of multi-line strings.)
 24091          One exception: if the string has `cursor' property on one of
 24092          its characters, we _do_ want the cursor there.  */
 24093       if (CHARPOS (row->end.string_pos) >= 0)
 24094         {
 24095           if (row->continued_p)
 24096             result = true;
 24097           else
 24098             {
 24099               /* Check for `display' property.  */
 24100               struct glyph *beg = row->glyphs[TEXT_AREA];
 24101               struct glyph *end = beg + row->used[TEXT_AREA] - 1;
 24102               struct glyph *glyph;
 24103 
 24104               result = false;
 24105               for (glyph = end; glyph >= beg; --glyph)
 24106                 if (STRINGP (glyph->object))
 24107                   {
 24108                     Lisp_Object prop
 24109                       = Fget_char_property (make_fixnum (charpos),
 24110                                             Qdisplay, Qnil);
 24111                     result =
 24112                       (!NILP (prop)
 24113                        && display_prop_string_p (prop, glyph->object));
 24114                     /* If there's a `cursor' property on one of the
 24115                        string's characters, this row is a cursor row,
 24116                        even though this is not a display string.  */
 24117                     if (!result)
 24118                       {
 24119                         Lisp_Object s = glyph->object;
 24120 
 24121                         for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
 24122                           {
 24123                             ptrdiff_t gpos = glyph->charpos;
 24124 
 24125                             if (!NILP (Fget_char_property (make_fixnum (gpos),
 24126                                                            Qcursor, s)))
 24127                               {
 24128                                 result = true;
 24129                                 break;
 24130                               }
 24131                           }
 24132                       }
 24133                     break;
 24134                   }
 24135             }
 24136         }
 24137       else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 24138         {
 24139           /* If the row ends in middle of a real character,
 24140              and the line is continued, we want the cursor here.
 24141              That's because CHARPOS (ROW->end.pos) would equal
 24142              PT if PT is before the character.  */
 24143           if (!row->ends_in_ellipsis_p)
 24144             result = row->continued_p;
 24145           else
 24146           /* If the row ends in an ellipsis, then
 24147              CHARPOS (ROW->end.pos) will equal point after the
 24148              invisible text.  We want that position to be displayed
 24149              after the ellipsis.  */
 24150             result = false;
 24151         }
 24152       /* If the row ends at ZV, display the cursor at the end of that
 24153          row instead of at the start of the row below.  */
 24154       else
 24155         result = row->ends_at_zv_p;
 24156     }
 24157 
 24158   return result;
 24159 }
 24160 
 24161 /* Value is true if glyph row ROW should be
 24162    used to hold the cursor.  */
 24163 
 24164 static bool
 24165 cursor_row_p (struct glyph_row *row)
 24166 {
 24167   return row_for_charpos_p (row, PT);
 24168 }
 24169 
 24170 
 24171 
 24172 /* Push the property PROP so that it will be rendered at the current
 24173    position in IT.  Return true if PROP was successfully pushed, false
 24174    otherwise.  Called from handle_line_prefix to handle the
 24175    `line-prefix' and `wrap-prefix' properties.  */
 24176 
 24177 static bool
 24178 push_prefix_prop (struct it *it, Lisp_Object prop)
 24179 {
 24180   struct text_pos pos =
 24181     STRINGP (it->string) ? it->current.string_pos : it->current.pos;
 24182 
 24183   eassert (it->method == GET_FROM_BUFFER
 24184            || it->method == GET_FROM_DISPLAY_VECTOR
 24185            || it->method == GET_FROM_STRING
 24186            || it->method == GET_FROM_IMAGE);
 24187 
 24188   /* We need to save the current buffer/string position, so it will be
 24189      restored by pop_it, because iterate_out_of_display_property
 24190      depends on that being set correctly, but some situations leave
 24191      it->position not yet set when this function is called.  */
 24192   push_it (it, &pos);
 24193 
 24194   if (STRINGP (prop))
 24195     {
 24196       if (SCHARS (prop) == 0)
 24197         {
 24198           pop_it (it);
 24199           return false;
 24200         }
 24201 
 24202       it->string = prop;
 24203       it->string_from_prefix_prop_p = true;
 24204       it->multibyte_p = STRING_MULTIBYTE (it->string);
 24205       it->current.overlay_string_index = -1;
 24206       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
 24207       it->end_charpos = it->string_nchars = SCHARS (it->string);
 24208       it->method = GET_FROM_STRING;
 24209       it->stop_charpos = 0;
 24210       it->prev_stop = 0;
 24211       it->base_level_stop = 0;
 24212       it->cmp_it.id = -1;
 24213 
 24214       /* Force paragraph direction to be that of the parent
 24215          buffer/string.  */
 24216       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 24217         it->paragraph_embedding = it->bidi_it.paragraph_dir;
 24218       else
 24219         it->paragraph_embedding = L2R;
 24220 
 24221       /* Set up the bidi iterator for this display string.  */
 24222       if (it->bidi_p)
 24223         {
 24224           it->bidi_it.string.lstring = it->string;
 24225           it->bidi_it.string.s = NULL;
 24226           it->bidi_it.string.schars = it->end_charpos;
 24227           it->bidi_it.string.bufpos = IT_CHARPOS (*it);
 24228           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
 24229           it->bidi_it.string.unibyte = !it->multibyte_p;
 24230           it->bidi_it.w = it->w;
 24231           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
 24232         }
 24233     }
 24234   else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
 24235     {
 24236       it->method = GET_FROM_STRETCH;
 24237       it->object = prop;
 24238     }
 24239 #ifdef HAVE_WINDOW_SYSTEM
 24240   else if (IMAGEP (prop))
 24241     {
 24242       it->what = IT_IMAGE;
 24243       it->image_id = lookup_image (it->f, prop, it->face_id);
 24244       it->method = GET_FROM_IMAGE;
 24245     }
 24246 #endif /* HAVE_WINDOW_SYSTEM */
 24247   else
 24248     {
 24249       pop_it (it);              /* bogus display property, give up */
 24250       return false;
 24251     }
 24252 
 24253   return true;
 24254 }
 24255 
 24256 /* Return the character-property PROP at the current position in IT.  */
 24257 
 24258 static Lisp_Object
 24259 get_it_property (struct it *it, Lisp_Object prop)
 24260 {
 24261   Lisp_Object position, object = it->object;
 24262 
 24263   if (STRINGP (object))
 24264     position = make_fixnum (IT_STRING_CHARPOS (*it));
 24265   else if (BUFFERP (object))
 24266     {
 24267       position = make_fixnum (IT_CHARPOS (*it));
 24268       object = it->window;
 24269     }
 24270   else
 24271     return Qnil;
 24272 
 24273   return Fget_char_property (position, prop, object);
 24274 }
 24275 
 24276 /* Return the line-prefix/wrap-prefix property, checking both the
 24277    current IT->OBJECT and the underlying buffer text.  */
 24278 
 24279 static Lisp_Object
 24280 get_line_prefix_it_property (struct it *it, Lisp_Object prop)
 24281 {
 24282   Lisp_Object prefix = get_it_property (it, prop);
 24283 
 24284   /* If we are looking at a display or overlay string, check also the
 24285      underlying buffer text.  */
 24286   if (NILP (prefix) && it->sp > 0 && STRINGP (it->object))
 24287     return Fget_char_property (make_fixnum (IT_CHARPOS (*it)), prop,
 24288                                it->w->contents);
 24289   return prefix;
 24290 }
 24291 
 24292 /* See if there's a line- or wrap-prefix, and if so, push it on IT.  */
 24293 
 24294 static void
 24295 handle_line_prefix (struct it *it)
 24296 {
 24297   Lisp_Object prefix;
 24298 
 24299   if (it->continuation_lines_width > 0)
 24300     {
 24301       prefix = get_line_prefix_it_property (it, Qwrap_prefix);
 24302       if (NILP (prefix))
 24303         prefix = Vwrap_prefix;
 24304     }
 24305   else
 24306     {
 24307       prefix = get_line_prefix_it_property (it, Qline_prefix);
 24308       if (NILP (prefix))
 24309         prefix = Vline_prefix;
 24310     }
 24311   if (! NILP (prefix) && push_prefix_prop (it, prefix))
 24312     {
 24313       /* If the prefix is wider than the window, and we try to wrap
 24314          it, it would acquire its own wrap prefix, and so on till the
 24315          iterator stack overflows.  So, don't wrap the prefix.  */
 24316       it->line_wrap = TRUNCATE;
 24317       it->avoid_cursor_p = true;
 24318     }
 24319 }
 24320 
 24321 
 24322 
 24323 /* Remove N glyphs at the start of a reversed IT->glyph_row.  Called
 24324    only for R2L lines from display_line and display_string, when they
 24325    decide that too many glyphs were produced by PRODUCE_GLYPHS, and
 24326    the line/string needs to be continued on the next glyph row.  */
 24327 static void
 24328 unproduce_glyphs (struct it *it, int n)
 24329 {
 24330   struct glyph *glyph, *end;
 24331 
 24332   eassert (it->glyph_row);
 24333   eassert (it->glyph_row->reversed_p);
 24334   eassert (it->area == TEXT_AREA);
 24335   eassert (n <= it->glyph_row->used[TEXT_AREA]);
 24336 
 24337   if (n > it->glyph_row->used[TEXT_AREA])
 24338     n = it->glyph_row->used[TEXT_AREA];
 24339   glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
 24340   end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
 24341   for ( ; glyph < end; glyph++)
 24342     glyph[-n] = *glyph;
 24343 }
 24344 
 24345 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
 24346    and ROW->maxpos.  */
 24347 static void
 24348 find_row_edges (struct it *it, struct glyph_row *row,
 24349                 ptrdiff_t min_pos, ptrdiff_t min_bpos,
 24350                 ptrdiff_t max_pos, ptrdiff_t max_bpos)
 24351 {
 24352   /* FIXME: Revisit this when glyph ``spilling'' in continuation
 24353      lines' rows is implemented for bidi-reordered rows.  */
 24354 
 24355   /* ROW->minpos is the value of min_pos, the minimal buffer position
 24356      we have in ROW, or ROW->start.pos if that is smaller.  */
 24357   if (min_pos <= ZV && min_pos < row->start.pos.charpos)
 24358     SET_TEXT_POS (row->minpos, min_pos, min_bpos);
 24359   else
 24360     /* We didn't find buffer positions smaller than ROW->start, or
 24361        didn't find _any_ valid buffer positions in any of the glyphs,
 24362        so we must trust the iterator's computed positions.  */
 24363       row->minpos = row->start.pos;
 24364   if (max_pos <= 0)
 24365     {
 24366       max_pos = CHARPOS (it->current.pos);
 24367       max_bpos = BYTEPOS (it->current.pos);
 24368     }
 24369 
 24370   /* Here are the various use-cases for ending the row, and the
 24371      corresponding values for ROW->maxpos:
 24372 
 24373      Line ends in a newline from buffer       eol_pos + 1
 24374      Line is continued from buffer            max_pos + 1
 24375      Line is truncated on right               it->current.pos
 24376      Line ends in a newline from string       max_pos + 1(*)
 24377       (*) + 1 only when line ends in a forward scan
 24378      Line is continued from string            max_pos
 24379      Line is continued from display vector    max_pos
 24380      Line is entirely from a string           min_pos == max_pos
 24381      Line is entirely from a display vector   min_pos == max_pos
 24382      Line that ends at ZV                     ZV
 24383 
 24384      If you discover other use-cases, please add them here as
 24385      appropriate.  */
 24386   if (row->ends_at_zv_p)
 24387     row->maxpos = it->current.pos;
 24388   else if (row->used[TEXT_AREA])
 24389     {
 24390       bool seen_this_string = false;
 24391       struct glyph_row *r1 = row - 1;
 24392 
 24393       /* Did we see the same display string on the previous row?  */
 24394       if (STRINGP (it->object)
 24395           /* this is not the first row */
 24396           && row > it->w->desired_matrix->rows
 24397           /* previous row is not the header line or tab-line */
 24398           && !r1->mode_line_p
 24399           /* previous row also ends in a newline from a string */
 24400           && r1->ends_in_newline_from_string_p)
 24401         {
 24402           struct glyph *start, *end;
 24403 
 24404           /* Search for the last glyph of the previous row that came
 24405              from buffer or string.  Depending on whether the row is
 24406              L2R or R2L, we need to process it front to back or the
 24407              other way round.  */
 24408           if (!r1->reversed_p)
 24409             {
 24410               start = r1->glyphs[TEXT_AREA];
 24411               end = start + r1->used[TEXT_AREA];
 24412               /* Glyphs inserted by redisplay have nil as their object.  */
 24413               while (end > start
 24414                      && NILP ((end - 1)->object)
 24415                      && (end - 1)->charpos <= 0)
 24416                 --end;
 24417               if (end > start)
 24418                 {
 24419                   if (EQ ((end - 1)->object, it->object))
 24420                     seen_this_string = true;
 24421                 }
 24422               else
 24423                 /* If all the glyphs of the previous row were inserted
 24424                    by redisplay, it means the previous row was
 24425                    produced from a single newline, which is only
 24426                    possible if that newline came from the same string
 24427                    as the one which produced this ROW.  */
 24428                 seen_this_string = true;
 24429             }
 24430           else
 24431             {
 24432               end = r1->glyphs[TEXT_AREA] - 1;
 24433               start = end + r1->used[TEXT_AREA];
 24434               while (end < start
 24435                      && NILP ((end + 1)->object)
 24436                      && (end + 1)->charpos <= 0)
 24437                 ++end;
 24438               if (end < start)
 24439                 {
 24440                   if (EQ ((end + 1)->object, it->object))
 24441                     seen_this_string = true;
 24442                 }
 24443               else
 24444                 seen_this_string = true;
 24445             }
 24446         }
 24447       /* Take note of each display string that covers a newline only
 24448          once, the first time we see it.  This is for when a display
 24449          string includes more than one newline in it.  */
 24450       if (row->ends_in_newline_from_string_p && !seen_this_string)
 24451         {
 24452           /* If we were scanning the buffer forward when we displayed
 24453              the string, we want to account for at least one buffer
 24454              position that belongs to this row (position covered by
 24455              the display string), so that cursor positioning will
 24456              consider this row as a candidate when point is at the end
 24457              of the visual line represented by this row.  This is not
 24458              required when scanning back, because max_pos will already
 24459              have a much larger value.  */
 24460           if (CHARPOS (row->end.pos) > max_pos)
 24461             inc_both (&max_pos, &max_bpos);
 24462           SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24463         }
 24464       else if (CHARPOS (it->eol_pos) > 0)
 24465         SET_TEXT_POS (row->maxpos,
 24466                       CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
 24467       else if (row->continued_p)
 24468         {
 24469           /* If max_pos is different from IT's current position, it
 24470              means IT->method does not belong to the display element
 24471              at max_pos.  However, it also means that the display
 24472              element at max_pos was displayed in its entirety on this
 24473              line, which is equivalent to saying that the next line
 24474              starts at the next buffer position.  */
 24475           if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
 24476             SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24477           else
 24478             {
 24479               inc_both (&max_pos, &max_bpos);
 24480               SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24481             }
 24482         }
 24483       else if (row->truncated_on_right_p)
 24484         /* display_line already called reseat_at_next_visible_line_start,
 24485            which puts the iterator at the beginning of the next line, in
 24486            the logical order. */
 24487         row->maxpos = it->current.pos;
 24488       else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
 24489         /* A line that is entirely from a string/image/stretch...  */
 24490         row->maxpos = row->minpos;
 24491       else
 24492         emacs_abort ();
 24493     }
 24494   else
 24495     row->maxpos = it->current.pos;
 24496 }
 24497 
 24498 /* Like display_count_lines, but capable of counting outside of the
 24499    current narrowed region.  */
 24500 static ptrdiff_t
 24501 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
 24502                                ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
 24503 {
 24504   if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
 24505     return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24506 
 24507   ptrdiff_t val;
 24508   specpdl_ref pdl_count = SPECPDL_INDEX ();
 24509   record_unwind_protect (save_restriction_restore, save_restriction_save ());
 24510   labeled_restrictions_remove_in_current_buffer ();
 24511   Fwiden ();
 24512   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24513   unbind_to (pdl_count, Qnil);
 24514   return val;
 24515 }
 24516 
 24517 /* Count the number of screen lines in window IT->w between character
 24518    position IT_CHARPOS(*IT) and the line showing that window's point.  */
 24519 static ptrdiff_t
 24520 display_count_lines_visually (struct it *it)
 24521 {
 24522   struct it tem_it;
 24523   ptrdiff_t to;
 24524   struct text_pos from;
 24525 
 24526   /* If we already calculated a relative line number, use that.  This
 24527      trick relies on the fact that visual lines (a.k.a. "glyph rows")
 24528      are laid out sequentially, one by one, for each sequence of calls
 24529      to display_line or other similar function that follows a call to
 24530      init_iterator.  */
 24531   if (it->lnum_bytepos > 0)
 24532     return it->lnum + 1;
 24533   else
 24534     {
 24535       specpdl_ref count = SPECPDL_INDEX ();
 24536 
 24537       if (IT_CHARPOS (*it) <= PT)
 24538         {
 24539           from = it->current.pos;
 24540           to = PT;
 24541         }
 24542       else
 24543         {
 24544           SET_TEXT_POS (from, PT, PT_BYTE);
 24545           to = IT_CHARPOS (*it);
 24546         }
 24547       /* Need to disable visual mode temporarily, since otherwise the
 24548          call to move_it_to below and inside start_display will cause
 24549          infinite recursion.  */
 24550       specbind (Qdisplay_line_numbers, Qrelative);
 24551       start_display (&tem_it, it->w, from);
 24552       /* Some redisplay optimizations could invoke us very far from
 24553          PT, which will make the caller painfully slow.  There should
 24554          be no need to go too far beyond the window's bottom, as any
 24555          such optimization will fail to show point anyway.  */
 24556       move_it_to (&tem_it, to, -1,
 24557                   tem_it.last_visible_y
 24558                   + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
 24559                   -1, MOVE_TO_POS | MOVE_TO_Y);
 24560       unbind_to (count, Qnil);
 24561       return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
 24562     }
 24563 }
 24564 
 24565 /* Produce the line-number glyphs for the current glyph_row.  If
 24566    IT->glyph_row is non-NULL, populate the row with the produced
 24567    glyphs.  */
 24568 static void
 24569 maybe_produce_line_number (struct it *it)
 24570 {
 24571   ptrdiff_t last_line = it->lnum;
 24572   ptrdiff_t start_from, bytepos;
 24573   ptrdiff_t this_line;
 24574   bool first_time = false;
 24575   ptrdiff_t beg_byte;
 24576   ptrdiff_t z_byte;
 24577   bool line_numbers_wide;
 24578   void *itdata = bidi_shelve_cache ();
 24579 
 24580   if (display_line_numbers_offset
 24581       && !display_line_numbers_widen
 24582       && !EQ (Vdisplay_line_numbers, Qvisual)
 24583       && !EQ (Vdisplay_line_numbers, Qrelative))
 24584     line_numbers_wide = true;
 24585   else
 24586     line_numbers_wide = display_line_numbers_widen;
 24587 
 24588   beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
 24589   z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
 24590 
 24591   if (EQ (Vdisplay_line_numbers, Qvisual))
 24592     this_line = display_count_lines_visually (it);
 24593   else
 24594     {
 24595       if (!last_line)
 24596         {
 24597           /* If possible, reuse data cached by line-number-mode.  */
 24598           if (it->w->base_line_number > 0
 24599               && it->w->base_line_pos > 0
 24600               && it->w->base_line_pos <= IT_CHARPOS (*it)
 24601               /* line-number-mode always displays narrowed line
 24602                  numbers, so we cannot use its data if the user wants
 24603                  line numbers that disregard narrowing, or if the
 24604                  buffer's narrowing has just changed.  */
 24605               && !(line_numbers_wide
 24606                    && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 24607               && !current_buffer->clip_changed)
 24608             {
 24609               start_from = CHAR_TO_BYTE (it->w->base_line_pos);
 24610               last_line = it->w->base_line_number - 1;
 24611             }
 24612           else
 24613             start_from = beg_byte;
 24614           if (!it->lnum_bytepos)
 24615             first_time = true;
 24616         }
 24617       else
 24618         start_from = it->lnum_bytepos;
 24619 
 24620       /* Paranoia: what if someone changes the narrowing since the
 24621          last time display_line was called?  Shouldn't really happen,
 24622          but who knows what some crazy Lisp invoked by :eval could do?  */
 24623       if (!(beg_byte <= start_from && start_from <= z_byte))
 24624         {
 24625           last_line = 0;
 24626           start_from = beg_byte;
 24627         }
 24628 
 24629       this_line =
 24630         last_line + display_count_lines_logically (start_from,
 24631                                                    IT_BYTEPOS (*it),
 24632                                                    IT_CHARPOS (*it), &bytepos);
 24633       eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
 24634       eassert (bytepos == IT_BYTEPOS (*it));
 24635     }
 24636 
 24637   /* Record the line number information.  */
 24638   if (this_line != last_line || !it->lnum_bytepos)
 24639     {
 24640       it->lnum = this_line;
 24641       it->lnum_bytepos = IT_BYTEPOS (*it);
 24642     }
 24643 
 24644   /* Produce the glyphs for the line number.  */
 24645   struct it tem_it;
 24646   char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
 24647   bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
 24648   ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
 24649   int lnum_face_id = merge_faces (it->w, Qline_number, 0, DEFAULT_FACE_ID);
 24650   int current_lnum_face_id
 24651     = merge_faces (it->w, Qline_number_current_line, 0, DEFAULT_FACE_ID);
 24652   /* From here onwards, we must prevent freeing realized faces, because
 24653      we are using the above 2 face IDs for the glyphs we produce.  */
 24654   bool save_free_realized_faces = inhibit_free_realized_faces;
 24655   inhibit_free_realized_faces = true;
 24656   /* Compute point's line number if needed.  */
 24657   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24658        || EQ (Vdisplay_line_numbers, Qvisual)
 24659        || lnum_face_id != current_lnum_face_id)
 24660       && !it->pt_lnum)
 24661     {
 24662       ptrdiff_t ignored;
 24663       if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
 24664         it->pt_lnum =
 24665           this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
 24666                                                      PT, &ignored);
 24667       else
 24668         it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
 24669                                                      &ignored);
 24670     }
 24671   /* Compute the required width if needed.  */
 24672   if (!it->lnum_width)
 24673     {
 24674       if (FIXNATP (Vdisplay_line_numbers_width))
 24675         it->lnum_width = XFIXNAT (Vdisplay_line_numbers_width);
 24676 
 24677       /* Max line number to be displayed cannot be more than the one
 24678          corresponding to the last row of the desired matrix.  */
 24679       ptrdiff_t max_lnum;
 24680 
 24681       if (NILP (Vdisplay_line_numbers_current_absolute)
 24682           && (EQ (Vdisplay_line_numbers, Qrelative)
 24683               || EQ (Vdisplay_line_numbers, Qvisual)))
 24684         /* We subtract one more because the current line is always
 24685            zero in this mode.  */
 24686         max_lnum = it->w->desired_matrix->nrows - 2;
 24687       else if (EQ (Vdisplay_line_numbers, Qvisual))
 24688         max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
 24689       else
 24690         max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
 24691       max_lnum = max (1, max_lnum);
 24692       it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
 24693       eassert (it->lnum_width > 0);
 24694     }
 24695   if (EQ (Vdisplay_line_numbers, Qrelative))
 24696     lnum_offset = it->pt_lnum;
 24697   else if (EQ (Vdisplay_line_numbers, Qvisual))
 24698     lnum_offset = 0;
 24699   else if (display_line_numbers_offset)
 24700     lnum_offset -= display_line_numbers_offset;
 24701 
 24702   /* Under 'relative', display the absolute line number for the
 24703      current line, unless the user requests otherwise.  */
 24704   ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
 24705   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24706        || EQ (Vdisplay_line_numbers, Qvisual))
 24707       && lnum_to_display == 0
 24708       && !NILP (Vdisplay_line_numbers_current_absolute))
 24709     lnum_to_display = it->pt_lnum + 1;
 24710   /* In L2R rows we need to append the blank separator, in R2L
 24711      rows we need to prepend it.  But this function is usually
 24712      called when no display elements were produced from the
 24713      following line, so the paragraph direction might be unknown.
 24714      Therefore we cheat and add 2 blanks, one on either side.  */
 24715   pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
 24716   strcat (lnum_buf, " ");
 24717 
 24718   /* Setup for producing the glyphs.  */
 24719   init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
 24720                  /* FIXME: Use specialized face.  */
 24721                  DEFAULT_FACE_ID);
 24722   scratch_glyph_row.reversed_p = false;
 24723   scratch_glyph_row.used[TEXT_AREA] = 0;
 24724   SET_TEXT_POS (tem_it.position, 0, 0);
 24725   tem_it.avoid_cursor_p = true;
 24726   tem_it.bidi_p = true;
 24727   tem_it.bidi_it.type = WEAK_EN;
 24728   /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
 24729      1 level in R2L paragraphs.  Emulate that, assuming we are in
 24730      an L2R paragraph.  */
 24731   tem_it.bidi_it.resolved_level = 2;
 24732 
 24733   /* We must leave space for 2 glyphs for continuation and truncation,
 24734      and at least one glyph for buffer text.  */
 24735   int width_limit =
 24736     tem_it.last_visible_x - tem_it.first_visible_x
 24737     - 3 * FRAME_COLUMN_WIDTH (it->f);
 24738 
 24739   tem_it.face_id = lnum_face_id;
 24740   /* Avoid displaying any face other than line-number on
 24741      empty lines beyond EOB.  */
 24742   if (lnum_face_id != current_lnum_face_id
 24743       && (EQ (Vdisplay_line_numbers, Qvisual)
 24744           ? this_line == 0
 24745           : this_line == it->pt_lnum)
 24746       && it->what != IT_EOB)
 24747     tem_it.face_id = current_lnum_face_id;
 24748   else if (!beyond_zv)
 24749     {
 24750       if (display_line_numbers_major_tick > 0
 24751           && (lnum_to_display % display_line_numbers_major_tick == 0))
 24752         tem_it.face_id = merge_faces (it->w, Qline_number_major_tick,
 24753                                       0, DEFAULT_FACE_ID);
 24754       else if (display_line_numbers_minor_tick > 0
 24755                && (lnum_to_display % display_line_numbers_minor_tick == 0))
 24756         tem_it.face_id = merge_faces (it->w, Qline_number_minor_tick,
 24757                                       0, DEFAULT_FACE_ID);
 24758     }
 24759 
 24760   /* Produce glyphs for the line number in a scratch glyph_row.  */
 24761   for (const char *p = lnum_buf; *p; p++)
 24762     {
 24763       /* For continuation lines and lines after ZV, instead of a line
 24764          number, produce a blank prefix of the same width.  */
 24765       if (beyond_zv
 24766           /* Don't display the same line number more than once.  */
 24767           || (!EQ (Vdisplay_line_numbers, Qvisual)
 24768               && (it->continuation_lines_width > 0
 24769                   || (this_line == last_line && !first_time))))
 24770         tem_it.c = tem_it.char_to_display = ' ';
 24771       else
 24772         tem_it.c = tem_it.char_to_display = *p;
 24773       tem_it.len = 1;
 24774       /* Make sure these glyphs will have a "position" of -1.  */
 24775       SET_TEXT_POS (tem_it.position, -1, -1);
 24776       PRODUCE_GLYPHS (&tem_it);
 24777 
 24778       /* Stop producing glyphs, and refrain from producing the line
 24779          number, if we don't have enough space on this line.  */
 24780       if (tem_it.current_x >= width_limit)
 24781         {
 24782           it->lnum_width = 0;
 24783           it->lnum_pixel_width = 0;
 24784           bidi_unshelve_cache (itdata, false);
 24785           inhibit_free_realized_faces = save_free_realized_faces;
 24786           return;
 24787         }
 24788     }
 24789 
 24790   inhibit_free_realized_faces = save_free_realized_faces;
 24791 
 24792   /* Record the width in pixels we need for the line number display.  */
 24793   it->lnum_pixel_width = tem_it.current_x;
 24794   /* Copy the produced glyphs into IT's glyph_row.  */
 24795   struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
 24796   struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
 24797   struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
 24798   short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
 24799 
 24800   eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
 24801 
 24802   for ( ; g < e; g++)
 24803     {
 24804       it->current_x += g->pixel_width;
 24805       /* The following is important when this function is called
 24806          from move_it_in_display_line_to: HPOS is incremented only
 24807          when we are in the visible portion of the glyph row.  */
 24808       if (it->current_x > it->first_visible_x)
 24809         it->hpos++;
 24810       if (p)
 24811         {
 24812           *p++ = *g;
 24813           (*u)++;
 24814         }
 24815     }
 24816 
 24817   /* Update IT's metrics due to glyphs produced for line numbers.
 24818      Don't do that for rows beyond ZV, to avoid displaying a cursor of
 24819      different dimensions there.  */
 24820   if (!beyond_zv)
 24821     {
 24822       if (it->glyph_row)
 24823         {
 24824           struct glyph_row *row = it->glyph_row;
 24825 
 24826           it->max_ascent = max (row->ascent, tem_it.max_ascent);
 24827           it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
 24828           it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
 24829           it->max_phys_descent = max (row->phys_height - row->phys_ascent,
 24830                                       tem_it.max_phys_descent);
 24831         }
 24832       else
 24833         {
 24834           it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
 24835           it->max_descent = max (it->max_descent, tem_it.max_descent);
 24836           it->max_phys_ascent = max (it->max_phys_ascent,
 24837                                      tem_it.max_phys_ascent);
 24838           it->max_phys_descent = max (it->max_phys_descent,
 24839                                       tem_it.max_phys_descent);
 24840         }
 24841     }
 24842 
 24843   it->line_number_produced_p = true;
 24844 
 24845   bidi_unshelve_cache (itdata, false);
 24846 }
 24847 
 24848 /* Return true if this glyph row needs a line number to be produced
 24849    for it.  */
 24850 static bool
 24851 should_produce_line_number (struct it *it)
 24852 {
 24853   if (NILP (Vdisplay_line_numbers))
 24854     return false;
 24855 
 24856   /* Don't display line numbers in minibuffer windows.  */
 24857   if (MINI_WINDOW_P (it->w))
 24858     return false;
 24859 
 24860 #ifdef HAVE_WINDOW_SYSTEM
 24861   /* Don't display line number in tooltip frames.  */
 24862   if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
 24863     return false;
 24864 #endif
 24865 
 24866   /* If the character at current position has a non-nil special
 24867      property, disable line numbers for this row.  This is for
 24868      packages such as company-mode, which need this for their tricky
 24869      layout, where line numbers get in the way.  */
 24870   Lisp_Object val = Fget_char_property (make_fixnum (IT_CHARPOS (*it)),
 24871                                         Qdisplay_line_numbers_disable,
 24872                                         it->window);
 24873   /* For ZV, we need to also look in empty overlays at that point,
 24874      because get-char-property always returns nil for ZV, except if
 24875      the property is in 'default-text-properties'.  */
 24876   if (NILP (val) && IT_CHARPOS (*it) >= ZV)
 24877     val = disable_line_numbers_overlay_at_eob ();
 24878   return NILP (val) ? true : false;
 24879 }
 24880 
 24881 /* Return true if ROW has no glyphs except those inserted by the
 24882    display engine.  This is needed for indicate-empty-lines and
 24883    similar features when the glyph row starts with glyphs which didn't
 24884    come from buffer or string.  */
 24885 static bool
 24886 row_text_area_empty (struct glyph_row *row)
 24887 {
 24888   if (!row->reversed_p)
 24889     {
 24890       for (struct glyph *g = row->glyphs[TEXT_AREA];
 24891            g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 24892            g++)
 24893         if (!NILP (g->object) || g->charpos > 0)
 24894           return false;
 24895     }
 24896   else
 24897     {
 24898       for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 24899            g > row->glyphs[TEXT_AREA];
 24900            g--)
 24901         if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
 24902           return false;
 24903     }
 24904 
 24905   return true;
 24906 }
 24907 
 24908 /* Construct the glyph row IT->glyph_row in the desired matrix of
 24909    IT->w from text at the current position of IT.  See dispextern.h
 24910    for an overview of struct it.  Value is true if
 24911    IT->glyph_row displays text, as opposed to a line displaying ZV
 24912    only.  CURSOR_VPOS is the window-relative vertical position of
 24913    the glyph row displaying the cursor, or -1 if unknown.  */
 24914 
 24915 static bool
 24916 display_line (struct it *it, int cursor_vpos)
 24917 {
 24918   struct glyph_row *row = it->glyph_row;
 24919   Lisp_Object overlay_arrow_string;
 24920   struct it wrap_it;
 24921   void *wrap_data = NULL;
 24922   bool may_wrap = false;
 24923   int wrap_x UNINIT;
 24924   int wrap_row_used = -1;
 24925   int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
 24926   int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
 24927   int wrap_row_extra_line_spacing UNINIT;
 24928   ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
 24929   ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
 24930   int cvpos;
 24931   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 24932   ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
 24933   bool pending_handle_line_prefix = false;
 24934   int tab_line = window_wants_tab_line (it->w);
 24935   int header_line = window_wants_header_line (it->w);
 24936   bool hscroll_this_line = (cursor_vpos >= 0
 24937                             && it->vpos == cursor_vpos - tab_line - header_line
 24938                             && hscrolling_current_line_p (it->w));
 24939   int first_visible_x = it->first_visible_x;
 24940   int last_visible_x = it->last_visible_x;
 24941   int x_incr = 0;
 24942 
 24943   /* We always start displaying at hpos zero even if hscrolled.  */
 24944   eassert (it->hpos == 0 && it->current_x == 0);
 24945 
 24946   if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
 24947       >= it->w->desired_matrix->nrows)
 24948     {
 24949       it->w->nrows_scale_factor++;
 24950       it->f->fonts_changed = true;
 24951       return false;
 24952     }
 24953 
 24954   /* Clear the result glyph row and enable it.  */
 24955   prepare_desired_row (it->w, row, false);
 24956 
 24957   row->y = it->current_y;
 24958   row->start = it->start;
 24959   row->continuation_lines_width = it->continuation_lines_width;
 24960   row->displays_text_p = true;
 24961   row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
 24962   it->starts_in_middle_of_char_p = false;
 24963   it->stretch_adjust = 0;
 24964   it->line_number_produced_p = false;
 24965 
 24966   /* If we are going to display the cursor's line, account for the
 24967      hscroll of that line.  We subtract the window's min_hscroll,
 24968      because that was already accounted for in init_iterator.  */
 24969   if (hscroll_this_line)
 24970     x_incr =
 24971       (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
 24972       * FRAME_COLUMN_WIDTH (it->f);
 24973 
 24974   bool line_number_needed = should_produce_line_number (it);
 24975 
 24976   /* Move over display elements that are not visible because we are
 24977      hscrolled.  This may stop at an x-position < first_visible_x
 24978      if the first glyph is partially visible or if we hit a line end.  */
 24979   if (it->current_x < it->first_visible_x + x_incr)
 24980     {
 24981       enum move_it_result move_result;
 24982 
 24983       this_line_min_pos = row->start.pos;
 24984       if (hscroll_this_line)
 24985         {
 24986           it->first_visible_x += x_incr;
 24987           it->last_visible_x  += x_incr;
 24988         }
 24989       if (current_buffer->long_line_optimizations_p
 24990           && it->line_wrap == TRUNCATE
 24991           && window_hscroll_limited (it->w, it->f) > large_hscroll_threshold)
 24992         {
 24993           /* Special optimization for very long and truncated lines
 24994              which are hscrolled far to the left: jump directly to the
 24995              (approximate) position that is visible, instead of slowly
 24996              walking there.  */
 24997           ptrdiff_t chars_to_skip =
 24998             it->first_visible_x / FRAME_COLUMN_WIDTH (it->f);
 24999           move_result = fast_move_it_horizontally (it, chars_to_skip);
 25000 
 25001           if (move_result == MOVE_X_REACHED)
 25002             it->current_x = it->first_visible_x;
 25003           else  /* use arbitrary value < first_visible_x */
 25004             it->current_x = it->first_visible_x - FRAME_COLUMN_WIDTH (it->f);
 25005         }
 25006       else
 25007         move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
 25008                                                   MOVE_TO_POS | MOVE_TO_X);
 25009       /* If we are under a large hscroll, move_it_in_display_line_to
 25010          could hit the end of the line without reaching
 25011          first_visible_x.  Pretend that we did reach it.  This is
 25012          especially important on a TTY, where we will call
 25013          extend_face_to_end_of_line, which needs to know how many
 25014          blank glyphs to produce.  */
 25015       if (it->current_x < it->first_visible_x
 25016           && (move_result == MOVE_NEWLINE_OR_CR
 25017               || move_result == MOVE_POS_MATCH_OR_ZV))
 25018         it->current_x = it->first_visible_x;
 25019 
 25020       /* In case move_it_in_display_line_to above "produced" the line
 25021          number.  */
 25022       it->line_number_produced_p = false;
 25023 
 25024       /* Record the smallest positions seen while we moved over
 25025          display elements that are not visible.  This is needed by
 25026          redisplay_internal for optimizing the case where the cursor
 25027          stays inside the same line.  The rest of this function only
 25028          considers positions that are actually displayed, so
 25029          RECORD_MAX_MIN_POS will not otherwise record positions that
 25030          are hscrolled to the left of the left edge of the window.  */
 25031       min_pos = CHARPOS (this_line_min_pos);
 25032       min_bpos = BYTEPOS (this_line_min_pos);
 25033 
 25034       /* Produce line number, if needed.  */
 25035       if (line_number_needed)
 25036         maybe_produce_line_number (it);
 25037     }
 25038   else if (it->area == TEXT_AREA)
 25039     {
 25040       /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25041       if (line_number_needed)
 25042         maybe_produce_line_number (it);
 25043 
 25044       /* We only do this when not calling move_it_in_display_line_to
 25045          above, because that function calls itself handle_line_prefix.  */
 25046       handle_line_prefix (it);
 25047     }
 25048   else
 25049     {
 25050       /* Line-prefix and wrap-prefix are always displayed in the text
 25051          area.  But if this is the first call to display_line after
 25052          init_iterator, the iterator might have been set up to write
 25053          into a marginal area, e.g. if the line begins with some
 25054          display property that writes to the margins.  So we need to
 25055          wait with the call to handle_line_prefix until whatever
 25056          writes to the margin has done its job.  */
 25057       pending_handle_line_prefix = true;
 25058     }
 25059 
 25060   /* Get the initial row height.  This is either the height of the
 25061      text hscrolled, if there is any, or zero.  */
 25062   row->ascent = it->max_ascent;
 25063   row->height = it->max_ascent + it->max_descent;
 25064   row->phys_ascent = it->max_phys_ascent;
 25065   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 25066   row->extra_line_spacing = it->max_extra_line_spacing;
 25067 
 25068 /* Utility macro to record max and min buffer positions seen until now.  */
 25069 #define RECORD_MAX_MIN_POS(IT)                                  \
 25070   do                                                            \
 25071     {                                                           \
 25072       bool composition_p                                        \
 25073         = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
 25074       ptrdiff_t current_pos =                                   \
 25075         composition_p ? (IT)->cmp_it.charpos                    \
 25076                       : IT_CHARPOS (*(IT));                     \
 25077       ptrdiff_t current_bpos =                                  \
 25078         composition_p ? CHAR_TO_BYTE (current_pos)              \
 25079                       : IT_BYTEPOS (*(IT));                     \
 25080       if (current_pos < min_pos)                                \
 25081         {                                                       \
 25082           min_pos = current_pos;                                \
 25083           min_bpos = current_bpos;                              \
 25084         }                                                       \
 25085       if (IT_CHARPOS (*it) > max_pos)                           \
 25086         {                                                       \
 25087           max_pos = IT_CHARPOS (*it);                           \
 25088           max_bpos = IT_BYTEPOS (*it);                          \
 25089         }                                                       \
 25090     }                                                           \
 25091   while (false)
 25092 
 25093   /* Loop generating characters.  The loop is left with IT on the next
 25094      character to display.  */
 25095   while (true)
 25096     {
 25097       int n_glyphs_before, hpos_before, x_before;
 25098       int x, nglyphs;
 25099       int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
 25100 
 25101       /* Retrieve the next thing to display.  Value is false if end of
 25102          buffer reached.  */
 25103       if (!get_next_display_element (it))
 25104         {
 25105           bool row_has_glyphs = false;
 25106           /* Maybe add a space at the end of this line that is used to
 25107              display the cursor there under X.  Set the charpos of the
 25108              first glyph of blank lines not corresponding to any text
 25109              to -1.  */
 25110           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25111             row->exact_window_width_line_p = true;
 25112           else if ((append_space_for_newline (it, true)
 25113                     && row->used[TEXT_AREA] == 1)
 25114                    || row->used[TEXT_AREA] == 0
 25115                    || (row_has_glyphs = row_text_area_empty (row)))
 25116             {
 25117               row->glyphs[TEXT_AREA]->charpos = -1;
 25118               /* Don't reset the displays_text_p flag if we are
 25119                  displaying line numbers or line-prefix.  */
 25120               if (!row_has_glyphs)
 25121                 row->displays_text_p = false;
 25122 
 25123               if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
 25124                   && (!MINI_WINDOW_P (it->w)))
 25125                 row->indicate_empty_line_p = true;
 25126             }
 25127 
 25128           it->continuation_lines_width = 0;
 25129           /* Reset those iterator values set from display property
 25130              values.  This is for the case when the display property
 25131              ends at ZV, and is not a replacing property, so pop_it is
 25132              not called.  */
 25133           it->font_height = Qnil;
 25134           it->voffset = 0;
 25135           row->ends_at_zv_p = true;
 25136           /* A row that displays right-to-left text must always have
 25137              its last face extended all the way to the end of line,
 25138              even if this row ends in ZV, because we still write to
 25139              the screen left to right.  We also need to extend the
 25140              last face if the default face is remapped to some
 25141              different face, otherwise the functions that clear
 25142              portions of the screen will clear with the default face's
 25143              background color.  */
 25144           if (row->reversed_p
 25145               || lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
 25146               != DEFAULT_FACE_ID)
 25147             extend_face_to_end_of_line (it);
 25148           break;
 25149         }
 25150 
 25151       /* Now, get the metrics of what we want to display.  This also
 25152          generates glyphs in `row' (which is IT->glyph_row).  */
 25153       n_glyphs_before = row->used[TEXT_AREA];
 25154       x = it->current_x;
 25155 
 25156       /* Remember the line height so far in case the next element doesn't
 25157          fit on the line.  */
 25158       if (it->line_wrap != TRUNCATE)
 25159         {
 25160           ascent = it->max_ascent;
 25161           descent = it->max_descent;
 25162           phys_ascent = it->max_phys_ascent;
 25163           phys_descent = it->max_phys_descent;
 25164 
 25165           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
 25166             {
 25167               bool next_may_wrap = may_wrap;
 25168               /* Can we wrap after this character?  */
 25169               if (char_can_wrap_after (it))
 25170                 next_may_wrap = true;
 25171               else
 25172                 next_may_wrap = false;
 25173               /* Can we wrap here? */
 25174               if (may_wrap && char_can_wrap_before (it))
 25175                 {
 25176                   SAVE_IT (wrap_it, *it, wrap_data);
 25177                   wrap_x = x;
 25178                   wrap_row_used = row->used[TEXT_AREA];
 25179                   wrap_row_ascent = row->ascent;
 25180                   wrap_row_height = row->height;
 25181                   wrap_row_phys_ascent = row->phys_ascent;
 25182                   wrap_row_phys_height = row->phys_height;
 25183                   wrap_row_extra_line_spacing = row->extra_line_spacing;
 25184                   wrap_row_min_pos = min_pos;
 25185                   wrap_row_min_bpos = min_bpos;
 25186                   wrap_row_max_pos = max_pos;
 25187                   wrap_row_max_bpos = max_bpos;
 25188                 }
 25189               /* Update may_wrap for the next iteration.  */
 25190               may_wrap = next_may_wrap;
 25191             }
 25192         }
 25193 
 25194       PRODUCE_GLYPHS (it);
 25195 
 25196       /* If this display element was in marginal areas, continue with
 25197          the next one.  */
 25198       if (it->area != TEXT_AREA)
 25199         {
 25200           row->ascent = max (row->ascent, it->max_ascent);
 25201           row->height = max (row->height, it->max_ascent + it->max_descent);
 25202           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25203           row->phys_height = max (row->phys_height,
 25204                                   it->max_phys_ascent + it->max_phys_descent);
 25205           row->extra_line_spacing = max (row->extra_line_spacing,
 25206                                          it->max_extra_line_spacing);
 25207           set_iterator_to_next (it, true);
 25208           /* If we didn't handle the line/wrap prefix above, and the
 25209              call to set_iterator_to_next just switched to TEXT_AREA,
 25210              process the prefix now.  */
 25211           if (it->area == TEXT_AREA && pending_handle_line_prefix)
 25212             {
 25213               /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25214               if (line_number_needed)
 25215                 maybe_produce_line_number (it);
 25216 
 25217               pending_handle_line_prefix = false;
 25218               handle_line_prefix (it);
 25219             }
 25220           continue;
 25221         }
 25222 
 25223       /* Does the display element fit on the line?  If we truncate
 25224          lines, we should draw past the right edge of the window.  If
 25225          we don't truncate, we want to stop so that we can display the
 25226          continuation glyph before the right margin.  If lines are
 25227          continued, there are two possible strategies for characters
 25228          resulting in more than 1 glyph (e.g. tabs): Display as many
 25229          glyphs as possible in this line and leave the rest for the
 25230          continuation line, or display the whole element in the next
 25231          line.  Original redisplay did the former, so we do it also.  */
 25232       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 25233       hpos_before = it->hpos;
 25234       x_before = x;
 25235 
 25236       if (/* Not a newline.  */
 25237           nglyphs > 0
 25238           /* Glyphs produced fit entirely in the line.  */
 25239           && it->current_x < it->last_visible_x)
 25240         {
 25241           it->hpos += nglyphs;
 25242           row->ascent = max (row->ascent, it->max_ascent);
 25243           row->height = max (row->height, it->max_ascent + it->max_descent);
 25244           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25245           row->phys_height = max (row->phys_height,
 25246                                   it->max_phys_ascent + it->max_phys_descent);
 25247           row->extra_line_spacing = max (row->extra_line_spacing,
 25248                                          it->max_extra_line_spacing);
 25249           if (it->current_x - it->pixel_width < it->first_visible_x
 25250               /* When line numbers are displayed, row->x should not be
 25251                  offset, as the first glyph after the line number can
 25252                  never be partially visible.  */
 25253               && !line_number_needed
 25254               /* In R2L rows, we arrange in extend_face_to_end_of_line
 25255                  to add a right offset to the line, by a suitable
 25256                  change to the stretch glyph that is the leftmost
 25257                  glyph of the line.  */
 25258               && !row->reversed_p)
 25259             row->x = x - it->first_visible_x;
 25260           /* Record the maximum and minimum buffer positions seen so
 25261              far in glyphs that will be displayed by this row.  */
 25262           if (it->bidi_p)
 25263             RECORD_MAX_MIN_POS (it);
 25264         }
 25265       else
 25266         {
 25267           int i, new_x;
 25268           struct glyph *glyph;
 25269 
 25270           for (i = 0; i < nglyphs; ++i, x = new_x)
 25271             {
 25272               /* Identify the glyphs added by the last call to
 25273                  PRODUCE_GLYPHS.  In R2L rows, they are prepended to
 25274                  the previous glyphs.  */
 25275               if (!row->reversed_p)
 25276                 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 25277               else
 25278                 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
 25279               new_x = x + glyph->pixel_width;
 25280 
 25281               if (/* Lines are continued.  */
 25282                   it->line_wrap != TRUNCATE
 25283                   && (/* Glyph doesn't fit on the line.  */
 25284                       new_x > it->last_visible_x
 25285                       /* Or it fits exactly on a window system frame.  */
 25286                       || (new_x == it->last_visible_x
 25287                           && FRAME_WINDOW_P (it->f)
 25288                           && (row->reversed_p
 25289                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25290                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 25291                 {
 25292                   /* End of a continued line.  */
 25293 
 25294                   if (it->hpos == 0
 25295                       || (new_x == it->last_visible_x
 25296                           && FRAME_WINDOW_P (it->f)
 25297                           && (row->reversed_p
 25298                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25299                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
 25300                     {
 25301                       /* Current glyph is the only one on the line or
 25302                          fits exactly on the line.  We must continue
 25303                          the line because we can't draw the cursor
 25304                          after the glyph.  */
 25305                       row->continued_p = true;
 25306                       it->current_x = new_x;
 25307                       it->continuation_lines_width += new_x;
 25308                       ++it->hpos;
 25309                       if (i == nglyphs - 1)
 25310                         {
 25311                           /* If line-wrap is on, check if a previous
 25312                              wrap point was found.  */
 25313                           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 25314                               && wrap_row_used > 0 /* Found.  */
 25315                               /* Even if there is a previous wrap
 25316                                  point, continue the line here as
 25317                                  usual, if (i) the previous character
 25318                                  allows wrapping after it, AND (ii)
 25319                                  the current character allows wrapping
 25320                                  before it.  Because this is a valid
 25321                                  break point, we can just continue to
 25322                                  the next line at here, there is no
 25323                                  need to wrap early at the previous
 25324                                  wrap point.  */
 25325                               && (!may_wrap || !char_can_wrap_before (it)))
 25326                             goto back_to_wrap;
 25327 
 25328                           /* Record the maximum and minimum buffer
 25329                              positions seen so far in glyphs that will be
 25330                              displayed by this row.  */
 25331                           if (it->bidi_p)
 25332                             RECORD_MAX_MIN_POS (it);
 25333                           set_iterator_to_next (it, true);
 25334                           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25335                             {
 25336                               if (!get_next_display_element (it))
 25337                                 {
 25338                                   row->exact_window_width_line_p = true;
 25339                                   it->continuation_lines_width = 0;
 25340                                   it->font_height = Qnil;
 25341                                   it->voffset = 0;
 25342                                   row->continued_p = false;
 25343                                   row->ends_at_zv_p = true;
 25344                                 }
 25345                               else if (ITERATOR_AT_END_OF_LINE_P (it))
 25346                                 {
 25347                                   row->continued_p = false;
 25348                                   row->exact_window_width_line_p = true;
 25349                                 }
 25350                               /* If line-wrap is on, check if a
 25351                                  previous wrap point was found.  */
 25352                               else if (wrap_row_used > 0
 25353                                        /* Even if there is a previous
 25354                                           wrap point, continue the
 25355                                           line here as usual, if (i)
 25356                                           the previous character was a
 25357                                           space or tab AND (ii) the
 25358                                           current character is not,
 25359                                           AND (iii) the current
 25360                                           character allows wrapping
 25361                                           before it.  */
 25362                                        && (!may_wrap || !char_can_wrap_before (it)))
 25363                                 goto back_to_wrap;
 25364 
 25365                             }
 25366                         }
 25367                       else if (it->bidi_p)
 25368                         RECORD_MAX_MIN_POS (it);
 25369                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25370                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25371                         extend_face_to_end_of_line (it);
 25372                     }
 25373                   else if (CHAR_GLYPH_PADDING_P (*glyph)
 25374                            && !FRAME_WINDOW_P (it->f))
 25375                     {
 25376                       /* A padding glyph that doesn't fit on this line.
 25377                          This means the whole character doesn't fit
 25378                          on the line.  */
 25379                       if (row->reversed_p)
 25380                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25381                                                - n_glyphs_before);
 25382                       row->used[TEXT_AREA] = n_glyphs_before;
 25383 
 25384                       /* Fill the rest of the row with continuation
 25385                          glyphs like in 20.x.  */
 25386                       while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
 25387                              < row->glyphs[1 + TEXT_AREA])
 25388                         produce_special_glyphs (it, IT_CONTINUATION);
 25389 
 25390                       row->continued_p = true;
 25391                       it->current_x = x_before;
 25392                       it->continuation_lines_width += x_before;
 25393 
 25394                       /* Restore the height to what it was before the
 25395                          element not fitting on the line.  */
 25396                       it->max_ascent = ascent;
 25397                       it->max_descent = descent;
 25398                       it->max_phys_ascent = phys_ascent;
 25399                       it->max_phys_descent = phys_descent;
 25400                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25401                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25402                         extend_face_to_end_of_line (it);
 25403                     }
 25404                   else if (wrap_row_used > 0)
 25405                     {
 25406                     back_to_wrap:
 25407                       if (row->reversed_p)
 25408                         unproduce_glyphs (it,
 25409                                           row->used[TEXT_AREA] - wrap_row_used);
 25410                       RESTORE_IT (it, &wrap_it, wrap_data);
 25411                       it->continuation_lines_width += wrap_x;
 25412                       row->used[TEXT_AREA] = wrap_row_used;
 25413                       row->ascent = wrap_row_ascent;
 25414                       row->height = wrap_row_height;
 25415                       row->phys_ascent = wrap_row_phys_ascent;
 25416                       row->phys_height = wrap_row_phys_height;
 25417                       row->extra_line_spacing = wrap_row_extra_line_spacing;
 25418                       min_pos = wrap_row_min_pos;
 25419                       min_bpos = wrap_row_min_bpos;
 25420                       max_pos = wrap_row_max_pos;
 25421                       max_bpos = wrap_row_max_bpos;
 25422                       row->continued_p = true;
 25423                       row->ends_at_zv_p = false;
 25424                       row->exact_window_width_line_p = false;
 25425 
 25426                       /* Make sure that a non-default face is extended
 25427                          up to the right margin of the window.  */
 25428                       extend_face_to_end_of_line (it);
 25429                     }
 25430                   else if ((it->what == IT_CHARACTER
 25431                             || it->what == IT_STRETCH
 25432                             || it->what == IT_COMPOSITION)
 25433                            && it->c == '\t' && FRAME_WINDOW_P (it->f))
 25434                     {
 25435                       /* A TAB that extends past the right edge of the
 25436                          window.  This produces a single glyph on
 25437                          window system frames.  We leave the glyph in
 25438                          this row and let it fill the row, but don't
 25439                          consume the TAB.  */
 25440                       if ((row->reversed_p
 25441                            ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25442                            : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25443                         produce_special_glyphs (it, IT_CONTINUATION);
 25444                       it->continuation_lines_width += it->last_visible_x;
 25445                       row->ends_in_middle_of_char_p = true;
 25446                       row->continued_p = true;
 25447                       glyph->pixel_width = it->last_visible_x - x;
 25448                       it->starts_in_middle_of_char_p = true;
 25449                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25450                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25451                         extend_face_to_end_of_line (it);
 25452                     }
 25453                   else
 25454                     {
 25455                       /* Something other than a TAB that draws past
 25456                          the right edge of the window.  Restore
 25457                          positions to values before the element.  */
 25458                       if (row->reversed_p)
 25459                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25460                                                - (n_glyphs_before + i));
 25461                       row->used[TEXT_AREA] = n_glyphs_before + i;
 25462 
 25463                       /* Display continuation glyphs.  */
 25464                       it->current_x = x_before;
 25465                       it->continuation_lines_width += x;
 25466                       if (!FRAME_WINDOW_P (it->f)
 25467                           || (row->reversed_p
 25468                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25469                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25470                         produce_special_glyphs (it, IT_CONTINUATION);
 25471                       row->continued_p = true;
 25472 
 25473                       extend_face_to_end_of_line (it);
 25474 
 25475                       if (nglyphs > 1 && i > 0)
 25476                         {
 25477                           row->ends_in_middle_of_char_p = true;
 25478                           it->starts_in_middle_of_char_p = true;
 25479                         }
 25480 
 25481                       /* Restore the height to what it was before the
 25482                          element not fitting on the line.  */
 25483                       it->max_ascent = ascent;
 25484                       it->max_descent = descent;
 25485                       it->max_phys_ascent = phys_ascent;
 25486                       it->max_phys_descent = phys_descent;
 25487                     }
 25488 
 25489                   break;
 25490                 }
 25491               else if (new_x > it->first_visible_x)
 25492                 {
 25493                   /* Increment number of glyphs actually displayed.  */
 25494                   ++it->hpos;
 25495 
 25496                   /* Record the maximum and minimum buffer positions
 25497                      seen so far in glyphs that will be displayed by
 25498                      this row.  */
 25499                   if (it->bidi_p)
 25500                     RECORD_MAX_MIN_POS (it);
 25501 
 25502                   if (x < it->first_visible_x && !row->reversed_p
 25503                       && !line_number_needed)
 25504                     /* Glyph is partially visible, i.e. row starts at
 25505                        negative X position.  Don't do that in R2L
 25506                        rows, where we arrange to add a right offset to
 25507                        the line in extend_face_to_end_of_line, by a
 25508                        suitable change to the stretch glyph that is
 25509                        the leftmost glyph of the line.  */
 25510                     row->x = x - it->first_visible_x;
 25511                   /* When the last glyph of an R2L row only fits
 25512                      partially on the line, we need to set row->x to a
 25513                      negative offset, so that the leftmost glyph is
 25514                      the one that is partially visible.  But if we are
 25515                      going to produce the truncation glyph, this will
 25516                      be taken care of in produce_special_glyphs.  */
 25517                   if (row->reversed_p
 25518                       && new_x > it->last_visible_x
 25519                       && !line_number_needed
 25520                       && !(it->line_wrap == TRUNCATE
 25521                            && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
 25522                     {
 25523                       eassert (FRAME_WINDOW_P (it->f));
 25524                       row->x = it->last_visible_x - new_x;
 25525                     }
 25526                 }
 25527               else
 25528                 {
 25529                   /* Glyph is completely off the left margin of the
 25530                      window.  This should not happen because of the
 25531                      move_it_in_display_line at the start of this
 25532                      function, unless the text display area of the
 25533                      window is empty.  */
 25534                   eassert (it->first_visible_x <= it->last_visible_x);
 25535                 }
 25536             }
 25537           /* Even if this display element produced no glyphs at all,
 25538              we want to record its position.  */
 25539           if (it->bidi_p && nglyphs == 0)
 25540             RECORD_MAX_MIN_POS (it);
 25541 
 25542           row->ascent = max (row->ascent, it->max_ascent);
 25543           row->height = max (row->height, it->max_ascent + it->max_descent);
 25544           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25545           row->phys_height = max (row->phys_height,
 25546                                   it->max_phys_ascent + it->max_phys_descent);
 25547           row->extra_line_spacing = max (row->extra_line_spacing,
 25548                                          it->max_extra_line_spacing);
 25549 
 25550           /* End of this display line if row is continued.  */
 25551           if (row->continued_p || row->ends_at_zv_p)
 25552             break;
 25553         }
 25554 
 25555     at_end_of_line:
 25556       /* Is this a line end?  If yes, we're also done, after making
 25557          sure that a non-default face is extended up to the right
 25558          margin of the window.  */
 25559       if (ITERATOR_AT_END_OF_LINE_P (it))
 25560         {
 25561           int used_before = row->used[TEXT_AREA];
 25562 
 25563           row->ends_in_newline_from_string_p = STRINGP (it->object);
 25564 
 25565           /* Add a space at the end of the line that is used to
 25566              display the cursor there.  */
 25567           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25568             append_space_for_newline (it, false);
 25569 
 25570           /* Extend the face to the end of the line.  */
 25571           extend_face_to_end_of_line (it);
 25572 
 25573           /* Make sure we have the position.  */
 25574           if (used_before == 0)
 25575             row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
 25576 
 25577           /* Record the position of the newline, for use in
 25578              find_row_edges.  */
 25579           it->eol_pos = it->current.pos;
 25580 
 25581           /* Consume the line end.  This skips over invisible lines.  */
 25582           set_iterator_to_next (it, true);
 25583           it->continuation_lines_width = 0;
 25584           break;
 25585         }
 25586 
 25587       /* Detect overly-wide wrap-prefixes made of (space ...) display
 25588          properties.  When such a wrap prefix reaches past the right
 25589          margin of the window, we need to avoid the call to
 25590          set_iterator_to_next below, so that it->line_wrap is left at
 25591          its TRUNCATE value wisely set by handle_line_prefix.
 25592          Otherwise, set_iterator_to_next will pop the iterator stack,
 25593          restore it->line_wrap, and redisplay might infloop.  */
 25594       bool overwide_wrap_prefix =
 25595         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 25596         && it->sp > 0 && it->method == GET_FROM_STRETCH
 25597         && it->current_x >= it->last_visible_x
 25598         && it->continuation_lines_width > 0
 25599         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 25600 
 25601       /* Proceed with next display element.  Note that this skips
 25602          over lines invisible because of selective display.  */
 25603       if (!overwide_wrap_prefix)
 25604         set_iterator_to_next (it, true);
 25605 
 25606       /* If we truncate lines, we are done when the last displayed
 25607          glyphs reach past the right margin of the window.  */
 25608       if (it->line_wrap == TRUNCATE
 25609           && ((FRAME_WINDOW_P (it->f)
 25610                /* Images are preprocessed in produce_image_glyph such
 25611                   that they are cropped at the right edge of the
 25612                   window, so an image glyph will always end exactly at
 25613                   last_visible_x, even if there's no right fringe.  */
 25614                && ((row->reversed_p
 25615                     ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25616                     : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
 25617                    || it->what == IT_IMAGE))
 25618               ? (it->current_x >= it->last_visible_x)
 25619               : (it->current_x > it->last_visible_x)))
 25620         {
 25621           /* Maybe add truncation glyphs.  */
 25622           if (!FRAME_WINDOW_P (it->f)
 25623               || (row->reversed_p
 25624                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25625                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25626             {
 25627               int i, n;
 25628 
 25629               if (!row->reversed_p)
 25630                 {
 25631                   for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
 25632                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25633                       break;
 25634                 }
 25635               else
 25636                 {
 25637                   for (i = 0; i < row->used[TEXT_AREA]; i++)
 25638                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25639                       break;
 25640                   /* Remove any padding glyphs at the front of ROW, to
 25641                      make room for the truncation glyphs we will be
 25642                      adding below.  The loop below always inserts at
 25643                      least one truncation glyph, so also remove the
 25644                      last glyph added to ROW.  */
 25645                   unproduce_glyphs (it, i + 1);
 25646                   /* Adjust i for the loop below.  */
 25647                   i = row->used[TEXT_AREA] - (i + 1);
 25648                 }
 25649 
 25650               /* produce_special_glyphs overwrites the last glyph, so
 25651                  we don't want that if we want to keep that last
 25652                  glyph, which means it's an image.  */
 25653               if (it->current_x > it->last_visible_x)
 25654                 {
 25655                   it->current_x = x_before;
 25656                   if (!FRAME_WINDOW_P (it->f))
 25657                     {
 25658                       for (n = row->used[TEXT_AREA]; i < n; ++i)
 25659                         {
 25660                           row->used[TEXT_AREA] = i;
 25661                           produce_special_glyphs (it, IT_TRUNCATION);
 25662                         }
 25663                     }
 25664                   else
 25665                     {
 25666                       row->used[TEXT_AREA] = i;
 25667                       produce_special_glyphs (it, IT_TRUNCATION);
 25668                     }
 25669                   it->hpos = hpos_before;
 25670                 }
 25671             }
 25672           else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25673             {
 25674               /* Don't truncate if we can overflow newline into fringe.  */
 25675               if (!get_next_display_element (it))
 25676                 {
 25677                   it->continuation_lines_width = 0;
 25678                   it->font_height = Qnil;
 25679                   it->voffset = 0;
 25680                   row->ends_at_zv_p = true;
 25681                   row->exact_window_width_line_p = true;
 25682                   break;
 25683                 }
 25684               if (ITERATOR_AT_END_OF_LINE_P (it))
 25685                 {
 25686                   row->exact_window_width_line_p = true;
 25687                   goto at_end_of_line;
 25688                 }
 25689               it->current_x = x_before;
 25690               it->hpos = hpos_before;
 25691             }
 25692 
 25693           row->truncated_on_right_p = true;
 25694           it->continuation_lines_width = 0;
 25695           reseat_at_next_visible_line_start (it, false);
 25696           /* We insist below that IT's position be at ZV because in
 25697              bidi-reordered lines the character at visible line start
 25698              might not be the character that follows the newline in
 25699              the logical order.  */
 25700           if (IT_BYTEPOS (*it) > BEG_BYTE)
 25701             row->ends_at_zv_p =
 25702               IT_BYTEPOS (*it) >= ZV_BYTE
 25703               && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n');
 25704           else
 25705             row->ends_at_zv_p = false;
 25706           break;
 25707         }
 25708     }
 25709 
 25710   if (wrap_data)
 25711     bidi_unshelve_cache (wrap_data, true);
 25712 
 25713   /* If line is not empty and hscrolled, maybe insert truncation glyphs
 25714      at the left window margin.  */
 25715   if (it->first_visible_x
 25716       && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
 25717     {
 25718       if (!FRAME_WINDOW_P (it->f)
 25719           || (((row->reversed_p
 25720                 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 25721                 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 25722               /* Don't let insert_left_trunc_glyphs overwrite the
 25723                  first glyph of the row if it is an image.  */
 25724               && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
 25725         insert_left_trunc_glyphs (it);
 25726       row->truncated_on_left_p = true;
 25727     }
 25728 
 25729   /* Remember the position at which this line ends.
 25730 
 25731      BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
 25732      cannot be before the call to find_row_edges below, since that is
 25733      where these positions are determined. */
 25734   row->end = it->current;
 25735   if (!it->bidi_p)
 25736     {
 25737       row->minpos = row->start.pos;
 25738       row->maxpos = row->end.pos;
 25739     }
 25740   else
 25741     {
 25742       /* ROW->minpos and ROW->maxpos must be the smallest and
 25743          `1 + the largest' buffer positions in ROW.  But if ROW was
 25744          bidi-reordered, these two positions can be anywhere in the
 25745          row, so we must determine them now.  */
 25746       find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
 25747     }
 25748 
 25749   /* If the start of this line is the overlay arrow-position, then
 25750      mark this glyph row as the one containing the overlay arrow.
 25751      This is clearly a mess with variable size fonts.  It would be
 25752      better to let it be displayed like cursors under X.  */
 25753   if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
 25754       && (overlay_arrow_string = overlay_arrow_at_row (it, row),
 25755           !NILP (overlay_arrow_string)))
 25756     {
 25757       /* Overlay arrow in window redisplay is a fringe bitmap.  */
 25758       if (STRINGP (overlay_arrow_string))
 25759         {
 25760           struct glyph_row *arrow_row
 25761             = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
 25762           struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
 25763           struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
 25764           struct glyph *p = row->glyphs[TEXT_AREA];
 25765           struct glyph *p2, *end;
 25766 
 25767           /* Copy the arrow glyphs.  */
 25768           while (glyph < arrow_end)
 25769             *p++ = *glyph++;
 25770 
 25771           /* Throw away padding glyphs.  */
 25772           p2 = p;
 25773           end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 25774           while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
 25775             ++p2;
 25776           if (p2 > p)
 25777             {
 25778               while (p2 < end)
 25779                 *p++ = *p2++;
 25780               row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
 25781             }
 25782         }
 25783       else
 25784         {
 25785           eassert (FIXNUMP (overlay_arrow_string));
 25786           row->overlay_arrow_bitmap = XFIXNUM (overlay_arrow_string);
 25787         }
 25788       overlay_arrow_seen = true;
 25789     }
 25790 
 25791   /* Highlight trailing whitespace.  */
 25792   if (!NILP (Vshow_trailing_whitespace))
 25793     highlight_trailing_whitespace (it);
 25794 
 25795   /* Compute pixel dimensions of this line.  */
 25796   compute_line_metrics (it);
 25797 
 25798   /* Implementation note: No changes in the glyphs of ROW or in their
 25799      faces can be done past this point, because compute_line_metrics
 25800      computes ROW's hash value and stores it within the glyph_row
 25801      structure.  */
 25802 
 25803   /* Record whether this row ends inside an ellipsis.  */
 25804   row->ends_in_ellipsis_p
 25805     = (it->method == GET_FROM_DISPLAY_VECTOR
 25806        && it->ellipsis_p);
 25807 
 25808   /* Save fringe bitmaps in this row.  */
 25809   row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
 25810   row->left_user_fringe_face_id = it->left_user_fringe_face_id;
 25811   row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
 25812   row->right_user_fringe_face_id = it->right_user_fringe_face_id;
 25813 
 25814   it->left_user_fringe_bitmap = 0;
 25815   it->left_user_fringe_face_id = 0;
 25816   it->right_user_fringe_bitmap = 0;
 25817   it->right_user_fringe_face_id = 0;
 25818 
 25819   /* When they turn off tooltip-mode on a GUI frame, we call 'message'
 25820      with message-truncate-lines bound to non-nil, which produces
 25821      truncation bitmaps on the fringe.  Force redrawing of the fringes
 25822      in that case, to make sure the fringe bitmaps are removed when a
 25823      shorter message is displayed.  */
 25824   if (MINI_WINDOW_P (it->w) && it->line_wrap == TRUNCATE
 25825       && FRAME_WINDOW_P (it->f) && !cursor_in_echo_area)
 25826     row->redraw_fringe_bitmaps_p = true;
 25827 
 25828   /* Maybe set the cursor.  */
 25829   cvpos = it->w->cursor.vpos;
 25830   if ((cvpos < 0
 25831        /* In bidi-reordered rows, keep checking for proper cursor
 25832           position even if one has been found already, because buffer
 25833           positions in such rows change non-linearly with ROW->VPOS,
 25834           when a line is continued.  One exception: when we are at ZV,
 25835           display cursor on the first suitable glyph row, since all
 25836           the empty rows after that also have their position set to ZV.  */
 25837        /* FIXME: Revisit this when glyph ``spilling'' in continuation
 25838           lines' rows is implemented for bidi-reordered rows.  */
 25839        || (it->bidi_p
 25840            && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
 25841       && PT >= MATRIX_ROW_START_CHARPOS (row)
 25842       && PT <= MATRIX_ROW_END_CHARPOS (row)
 25843       && cursor_row_p (row))
 25844     set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
 25845 
 25846   /* Prepare for the next line.  This line starts horizontally at (X
 25847      HPOS) = (0 0).  Vertical positions are incremented.  As a
 25848      convenience for the caller, IT->glyph_row is set to the next
 25849      row to be used.  */
 25850   it->current_x = it->hpos = 0;
 25851   it->current_y += row->height;
 25852   /* Restore the first and last visible X if we adjusted them for
 25853      current-line hscrolling.  */
 25854   if (hscroll_this_line)
 25855     {
 25856       it->first_visible_x = first_visible_x;
 25857       it->last_visible_x  = last_visible_x;
 25858     }
 25859   SET_TEXT_POS (it->eol_pos, 0, 0);
 25860   ++it->vpos;
 25861   ++it->glyph_row;
 25862   /* The next row should by default use the same value of the
 25863      reversed_p flag as this one.  set_iterator_to_next decides when
 25864      it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
 25865      the flag accordingly.  */
 25866   if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
 25867     it->glyph_row->reversed_p = row->reversed_p;
 25868   it->start = row->end;
 25869   return MATRIX_ROW_DISPLAYS_TEXT_P (row);
 25870 
 25871 #undef RECORD_MAX_MIN_POS
 25872 }
 25873 
 25874 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
 25875        Scurrent_bidi_paragraph_direction, 0, 1, 0,
 25876        doc: /* Return paragraph direction at point in BUFFER.
 25877 Value is either `left-to-right' or `right-to-left'.
 25878 If BUFFER is omitted or nil, it defaults to the current buffer.
 25879 
 25880 Paragraph direction determines how the text in the paragraph is displayed.
 25881 In left-to-right paragraphs, text begins at the left margin of the window
 25882 and the reading direction is generally left to right.  In right-to-left
 25883 paragraphs, text begins at the right margin and is read from right to left.
 25884 
 25885 See also `bidi-paragraph-direction'.  */)
 25886   (Lisp_Object buffer)
 25887 {
 25888   struct buffer *buf = current_buffer;
 25889   struct buffer *old = buf;
 25890 
 25891   if (! NILP (buffer))
 25892     {
 25893       CHECK_BUFFER (buffer);
 25894       buf = XBUFFER (buffer);
 25895     }
 25896 
 25897   if (NILP (BVAR (buf, bidi_display_reordering))
 25898       || NILP (BVAR (buf, enable_multibyte_characters))
 25899       /* When we are loading loadup.el, the character property tables
 25900          needed for bidi iteration are not yet available.  */
 25901       || redisplay__inhibit_bidi)
 25902     return Qleft_to_right;
 25903   else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
 25904     return BVAR (buf, bidi_paragraph_direction);
 25905   else
 25906     {
 25907       /* Determine the direction from buffer text.  We could try to
 25908          use current_matrix if it is up to date, but this seems fast
 25909          enough as it is.  */
 25910       struct bidi_it itb;
 25911       ptrdiff_t pos = BUF_PT (buf);
 25912       ptrdiff_t bytepos = BUF_PT_BYTE (buf);
 25913       int c;
 25914       void *itb_data = bidi_shelve_cache ();
 25915 
 25916       set_buffer_temp (buf);
 25917       /* bidi_paragraph_init finds the base direction of the paragraph
 25918          by searching forward from paragraph start.  We need the base
 25919          direction of the current or _previous_ paragraph, so we need
 25920          to make sure we are within that paragraph.  To that end, find
 25921          the previous non-empty line.  */
 25922       if (pos >= ZV && pos > BEGV)
 25923         dec_both (&pos, &bytepos);
 25924       AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
 25925       if (fast_looking_at (trailing_white_space,
 25926                            pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
 25927         {
 25928           while ((c = FETCH_BYTE (bytepos)) == '\n'
 25929                  || c == ' ' || c == '\t' || c == '\f')
 25930             {
 25931               if (bytepos <= BEGV_BYTE)
 25932                 break;
 25933               bytepos--;
 25934               pos--;
 25935             }
 25936           while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
 25937             bytepos--;
 25938         }
 25939       bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
 25940       itb.paragraph_dir = NEUTRAL_DIR;
 25941       itb.string.s = NULL;
 25942       itb.string.lstring = Qnil;
 25943       itb.string.bufpos = 0;
 25944       itb.string.from_disp_str = false;
 25945       itb.string.unibyte = false;
 25946       /* We have no window to use here for ignoring window-specific
 25947          overlays.  Using NULL for window pointer will cause
 25948          compute_display_string_pos to use the current buffer.  */
 25949       itb.w = NULL;
 25950       bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
 25951       bidi_unshelve_cache (itb_data, false);
 25952       set_buffer_temp (old);
 25953       switch (itb.paragraph_dir)
 25954         {
 25955         case L2R:
 25956           return Qleft_to_right;
 25957           break;
 25958         case R2L:
 25959           return Qright_to_left;
 25960           break;
 25961         default:
 25962           emacs_abort ();
 25963         }
 25964     }
 25965 }
 25966 
 25967 DEFUN ("bidi-find-overridden-directionality",
 25968        Fbidi_find_overridden_directionality,
 25969        Sbidi_find_overridden_directionality, 3, 4, 0,
 25970        doc: /* Return position between FROM and TO where directionality was overridden.
 25971 
 25972 This function returns the first character position in the specified
 25973 region of OBJECT where characters have their bidirectional
 25974 properties affected in a way that might make its text look confusingly
 25975 on display.  For example, characters whose `bidi-class' property is `L',
 25976 could be forced to display as `R' by a directional override, and
 25977 likewise characters whose `bidi-class' is `R' or `AL' that are
 25978 forced to display as `L'.
 25979 
 25980 If no such character is found, the function returns nil.
 25981 
 25982 OBJECT is a Lisp string or buffer to search for overridden
 25983 directionality, and defaults to the current buffer if nil.
 25984 OBJECT can also be a window, in which case the function will search
 25985 the buffer displayed in that window.  Passing the window instead of
 25986 a buffer is preferable when the buffer is displayed in some window,
 25987 because this function will then be able to correctly account for
 25988 window-specific overlays, which can affect the results.
 25989 
 25990 Optional argument BASE-DIR specifies the base paragraph directory
 25991 of the text.  It should be a symbol, either `left-to-right'
 25992 or `right-to-left', and defaults to `left-to-right'.
 25993 
 25994 Strong directional characters `L', `R', and `AL' can have their
 25995 intrinsic directionality overridden by directional override control
 25996 characters RLO (u+202E) and LRO (u+202D).  They can also have their
 25997 directionality affected by other formatting control characters: LRE
 25998 (u+202A), RLE (u+202B), LRI (u+2066), and RLI (u+2067).  See the
 25999 function `get-char-code-property' for a way to inquire about the
 26000 `bidi-class' property of a character.  Characters whose intrinsic
 26001 directionality is weak or neutral, such as numbers or punctuation
 26002 characters, can be forced to display in a very different place with
 26003 respect of its surrounding characters, so as to make the surrounding
 26004 text confuse the user regarding what the text says.
 26005 
 26006 Also see the `highlight-confusing-reorderings' function, which can be
 26007 useful in similar circumstances as this function.  */)
 26008   (Lisp_Object from, Lisp_Object to, Lisp_Object object, Lisp_Object base_dir)
 26009 {
 26010   struct buffer *buf = current_buffer;
 26011   struct buffer *old = buf;
 26012   struct window *w = NULL;
 26013   bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
 26014   struct bidi_it itb;
 26015   ptrdiff_t from_pos, to_pos, from_bpos;
 26016   void *itb_data;
 26017 
 26018   if (!NILP (object))
 26019     {
 26020       if (BUFFERP (object))
 26021         buf = XBUFFER (object);
 26022       else if (WINDOWP (object))
 26023         {
 26024           w = decode_live_window (object);
 26025           buf = XBUFFER (w->contents);
 26026           frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
 26027         }
 26028       else
 26029         CHECK_STRING (object);
 26030     }
 26031 
 26032   if (STRINGP (object))
 26033     {
 26034       /* Characters in unibyte strings are always treated by bidi.c as
 26035          strong LTR.  */
 26036       if (!STRING_MULTIBYTE (object)
 26037           /* When we are loading loadup.el, the character property
 26038              tables needed for bidi iteration are not yet
 26039              available.  */
 26040           || redisplay__inhibit_bidi)
 26041         return Qnil;
 26042 
 26043       validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
 26044       if (from_pos >= SCHARS (object))
 26045         return Qnil;
 26046 
 26047       /* Set up the bidi iterator.  */
 26048       itb_data = bidi_shelve_cache ();
 26049       itb.paragraph_dir = NEUTRAL_DIR;
 26050       itb.string.lstring = object;
 26051       itb.string.s = NULL;
 26052       itb.string.schars = SCHARS (object);
 26053       itb.string.bufpos = 0;
 26054       itb.string.from_disp_str = false;
 26055       itb.string.unibyte = false;
 26056       itb.w = w;
 26057       bidi_init_it (0, 0, frame_window_p, &itb);
 26058     }
 26059   else
 26060     {
 26061       /* Nothing this fancy can happen in unibyte buffers, or in a
 26062          buffer that disabled reordering, or if FROM is at EOB.  */
 26063       if (NILP (BVAR (buf, bidi_display_reordering))
 26064           || NILP (BVAR (buf, enable_multibyte_characters))
 26065           /* When we are loading loadup.el, the character property
 26066              tables needed for bidi iteration are not yet
 26067              available.  */
 26068           || redisplay__inhibit_bidi)
 26069         return Qnil;
 26070 
 26071       set_buffer_temp (buf);
 26072       validate_region (&from, &to);
 26073       from_pos = XFIXNUM (from);
 26074       to_pos = XFIXNUM (to);
 26075       if (from_pos >= ZV)
 26076         return Qnil;
 26077 
 26078       /* Set up the bidi iterator.  */
 26079       itb_data = bidi_shelve_cache ();
 26080       from_bpos = CHAR_TO_BYTE (from_pos);
 26081       if (from_pos == BEGV)
 26082         {
 26083           itb.charpos = BEGV;
 26084           itb.bytepos = BEGV_BYTE;
 26085         }
 26086       else if (FETCH_BYTE (from_bpos - 1) == '\n')
 26087         {
 26088           itb.charpos = from_pos;
 26089           itb.bytepos = from_bpos;
 26090         }
 26091       else
 26092         itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
 26093                                             -1, &itb.bytepos);
 26094       itb.paragraph_dir = NEUTRAL_DIR;
 26095       itb.string.s = NULL;
 26096       itb.string.lstring = Qnil;
 26097       itb.string.bufpos = 0;
 26098       itb.string.from_disp_str = false;
 26099       itb.string.unibyte = false;
 26100       itb.w = w;
 26101       bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
 26102     }
 26103 
 26104   ptrdiff_t found;
 26105   bidi_dir_t bdir = EQ (base_dir, Qright_to_left) ? R2L : L2R;
 26106   do {
 26107     bidi_paragraph_init (bdir, &itb, false);
 26108     while ((found = bidi_find_first_overridden (&itb)) < from_pos)
 26109       ;
 26110   } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
 26111 
 26112   bidi_unshelve_cache (itb_data, false);
 26113   set_buffer_temp (old);
 26114 
 26115   return (from_pos <= found && found < to_pos) ? make_fixnum (found) : Qnil;
 26116 }
 26117 
 26118 DEFUN ("move-point-visually", Fmove_point_visually,
 26119        Smove_point_visually, 1, 1, 0,
 26120        doc: /* Move point in the visual order in the specified DIRECTION.
 26121 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
 26122 left.
 26123 
 26124 Value is the new character position of point.  */)
 26125   (Lisp_Object direction)
 26126 {
 26127   struct window *w = XWINDOW (selected_window);
 26128   struct buffer *b = XBUFFER (w->contents);
 26129   struct glyph_row *row;
 26130   int dir;
 26131   Lisp_Object paragraph_dir;
 26132 
 26133 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)          \
 26134   (!(ROW)->continued_p                          \
 26135    && NILP ((GLYPH)->object)                    \
 26136    && (GLYPH)->type == CHAR_GLYPH               \
 26137    && (GLYPH)->u.ch == ' '                      \
 26138    && (GLYPH)->charpos >= 0                     \
 26139    && !(GLYPH)->avoid_cursor_p)
 26140 
 26141   CHECK_FIXNUM (direction);
 26142   dir = XFIXNUM (direction);
 26143   if (dir > 0)
 26144     dir = 1;
 26145   else
 26146     dir = -1;
 26147 
 26148   /* If current matrix is up-to-date, we can use the information
 26149      recorded in the glyphs, at least as long as the goal is on the
 26150      screen.  */
 26151   if (w->window_end_valid
 26152       && !windows_or_buffers_changed
 26153       && b
 26154       && !b->clip_changed
 26155       && !b->prevent_redisplay_optimizations_p
 26156       && !window_outdated (w)
 26157       /* We rely below on the cursor coordinates to be up to date, but
 26158          we cannot trust them if some command moved point since the
 26159          last complete redisplay.  */
 26160       && w->last_point == BUF_PT (b)
 26161       && w->cursor.vpos >= 0
 26162       && w->cursor.vpos < w->current_matrix->nrows
 26163       && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
 26164     {
 26165       struct glyph *g = row->glyphs[TEXT_AREA];
 26166       struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
 26167       struct glyph *gpt = g + w->cursor.hpos;
 26168 
 26169       for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
 26170         {
 26171           if (BUFFERP (g->object) && g->charpos != PT)
 26172             {
 26173               SET_PT (g->charpos);
 26174               w->cursor.vpos = -1;
 26175               return make_fixnum (PT);
 26176             }
 26177           else if (!NILP (g->object) && !EQ (g->object, gpt->object))
 26178             {
 26179               ptrdiff_t new_pos;
 26180 
 26181               if (BUFFERP (gpt->object))
 26182                 {
 26183                   new_pos = PT;
 26184                   if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
 26185                     new_pos += (row->reversed_p ? -dir : dir);
 26186                   else
 26187                     new_pos -= (row->reversed_p ? -dir : dir);
 26188                   new_pos = clip_to_bounds (BEGV, new_pos, ZV);
 26189                   /* If we didn't move, we've hit BEGV or ZV, so we
 26190                      need to signal a suitable error.  */
 26191                   if (new_pos == PT)
 26192                     break;
 26193                 }
 26194               else if (BUFFERP (g->object))
 26195                 new_pos = g->charpos;
 26196               else
 26197                 break;
 26198               SET_PT (new_pos);
 26199               w->cursor.vpos = -1;
 26200               return make_fixnum (PT);
 26201             }
 26202           else if (ROW_GLYPH_NEWLINE_P (row, g))
 26203             {
 26204               /* Glyphs inserted at the end of a non-empty line for
 26205                  positioning the cursor have zero charpos, so we must
 26206                  deduce the value of point by other means.  */
 26207               if (g->charpos > 0)
 26208                 SET_PT (g->charpos);
 26209               else if (row->ends_at_zv_p && PT != ZV)
 26210                 SET_PT (ZV);
 26211               else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
 26212                 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26213               else
 26214                 break;
 26215               w->cursor.vpos = -1;
 26216               return make_fixnum (PT);
 26217             }
 26218         }
 26219       if (g == e || NILP (g->object))
 26220         {
 26221           if (row->truncated_on_left_p || row->truncated_on_right_p)
 26222             goto simulate_display;
 26223           if (!row->reversed_p)
 26224             row += dir;
 26225           else
 26226             row -= dir;
 26227           if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
 26228                 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
 26229             goto simulate_display;
 26230 
 26231           if (dir > 0)
 26232             {
 26233               if (row->reversed_p && !row->continued_p)
 26234                 {
 26235                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26236                   w->cursor.vpos = -1;
 26237                   return make_fixnum (PT);
 26238                 }
 26239               g = row->glyphs[TEXT_AREA];
 26240               e = g + row->used[TEXT_AREA];
 26241               for ( ; g < e; g++)
 26242                 {
 26243                   if (BUFFERP (g->object)
 26244                       /* Empty lines have only one glyph, which stands
 26245                          for the newline, and whose charpos is the
 26246                          buffer position of the newline.  */
 26247                       || ROW_GLYPH_NEWLINE_P (row, g)
 26248                       /* When the buffer ends in a newline, the line at
 26249                          EOB also has one glyph, but its charpos is -1.  */
 26250                       || (row->ends_at_zv_p
 26251                           && !row->reversed_p
 26252                           && NILP (g->object)
 26253                           && g->type == CHAR_GLYPH
 26254                           && g->u.ch == ' '))
 26255                     {
 26256                       if (g->charpos > 0)
 26257                         SET_PT (g->charpos);
 26258                       else if (!row->reversed_p
 26259                                && row->ends_at_zv_p
 26260                                && PT != ZV)
 26261                         SET_PT (ZV);
 26262                       else
 26263                         continue;
 26264                       w->cursor.vpos = -1;
 26265                       return make_fixnum (PT);
 26266                     }
 26267                 }
 26268             }
 26269           else
 26270             {
 26271               if (!row->reversed_p && !row->continued_p)
 26272                 {
 26273                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26274                   w->cursor.vpos = -1;
 26275                   return make_fixnum (PT);
 26276                 }
 26277               e = row->glyphs[TEXT_AREA];
 26278               g = e + row->used[TEXT_AREA] - 1;
 26279               for ( ; g >= e; g--)
 26280                 {
 26281                   if (BUFFERP (g->object)
 26282                       || (ROW_GLYPH_NEWLINE_P (row, g)
 26283                           && g->charpos > 0)
 26284                       /* Empty R2L lines on GUI frames have the buffer
 26285                          position of the newline stored in the stretch
 26286                          glyph.  */
 26287                       || g->type == STRETCH_GLYPH
 26288                       || (row->ends_at_zv_p
 26289                           && row->reversed_p
 26290                           && NILP (g->object)
 26291                           && g->type == CHAR_GLYPH
 26292                           && g->u.ch == ' '))
 26293                     {
 26294                       if (g->charpos > 0)
 26295                         SET_PT (g->charpos);
 26296                       else if (row->reversed_p
 26297                                && row->ends_at_zv_p
 26298                                && PT != ZV)
 26299                         SET_PT (ZV);
 26300                       else
 26301                         continue;
 26302                       w->cursor.vpos = -1;
 26303                       return make_fixnum (PT);
 26304                     }
 26305                 }
 26306             }
 26307         }
 26308     }
 26309 
 26310  simulate_display:
 26311 
 26312   /* If we wind up here, we failed to move by using the glyphs, so we
 26313      need to simulate display instead.  */
 26314 
 26315   if (b)
 26316     paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
 26317   else
 26318     paragraph_dir = Qleft_to_right;
 26319   if (EQ (paragraph_dir, Qright_to_left))
 26320     dir = -dir;
 26321   if (PT <= BEGV && dir < 0)
 26322     xsignal0 (Qbeginning_of_buffer);
 26323   else if (PT >= ZV && dir > 0)
 26324     xsignal0 (Qend_of_buffer);
 26325   else
 26326     {
 26327       struct text_pos pt;
 26328       struct it it;
 26329       int pt_x, target_x, pixel_width, pt_vpos;
 26330       bool at_eol_p;
 26331       bool overshoot_expected = false;
 26332       bool target_is_eol_p = false;
 26333       void *itdata = bidi_shelve_cache ();
 26334 
 26335       /* Setup the arena.  */
 26336       SET_TEXT_POS (pt, PT, PT_BYTE);
 26337       start_display (&it, w, pt);
 26338       /* When lines are truncated, we could be called with point
 26339          outside of the windows edges, in which case move_it_*
 26340          functions either prematurely stop at window's edge or jump to
 26341          the next screen line, whereas we rely below on our ability to
 26342          reach point, in order to start from its X coordinate.  So we
 26343          need to disregard the window's horizontal extent in that case.  */
 26344       if (it.line_wrap == TRUNCATE)
 26345         it.last_visible_x = DISP_INFINITY;
 26346 
 26347       if (it.cmp_it.id < 0
 26348           && it.method == GET_FROM_STRING
 26349           && it.area == TEXT_AREA
 26350           && it.string_from_display_prop_p
 26351           && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
 26352         overshoot_expected = true;
 26353 
 26354       /* Find the X coordinate of point.  We start from the beginning
 26355          of this or previous line to make sure we are before point in
 26356          the logical order (since the move_it_* functions can only
 26357          move forward).  */
 26358     reseat:
 26359       reseat_at_previous_visible_line_start (&it);
 26360       it.current_x = it.hpos = it.current_y = it.vpos = 0;
 26361       if (IT_CHARPOS (it) != PT)
 26362         {
 26363           move_it_to (&it, overshoot_expected ? PT - 1 : PT,
 26364                       -1, -1, -1, MOVE_TO_POS);
 26365           /* If we missed point because the character there is
 26366              displayed out of a display vector that has more than one
 26367              glyph, retry expecting overshoot.  */
 26368           if (it.method == GET_FROM_DISPLAY_VECTOR
 26369               && it.current.dpvec_index > 0
 26370               && !overshoot_expected)
 26371             {
 26372               overshoot_expected = true;
 26373               goto reseat;
 26374             }
 26375           else if (IT_CHARPOS (it) != PT && !overshoot_expected)
 26376             move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
 26377         }
 26378       pt_x = it.current_x;
 26379       pt_vpos = it.vpos;
 26380       if (dir > 0 || overshoot_expected)
 26381         {
 26382           struct glyph_row *row = it.glyph_row;
 26383 
 26384           /* When point is at beginning of line, we don't have
 26385              information about the glyph there loaded into struct
 26386              it.  Calling get_next_display_element fixes that.  */
 26387           if (pt_x == 0)
 26388             get_next_display_element (&it);
 26389           at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26390           it.glyph_row = NULL;
 26391           PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
 26392           it.glyph_row = row;
 26393           /* PRODUCE_GLYPHS advances it.current_x, so we must restore
 26394              it, lest it will become out of sync with it's buffer
 26395              position.  */
 26396           it.current_x = pt_x;
 26397         }
 26398       else
 26399         at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26400       pixel_width = it.pixel_width;
 26401       if (overshoot_expected && at_eol_p)
 26402         pixel_width = 0;
 26403       else if (pixel_width <= 0)
 26404         pixel_width = 1;
 26405 
 26406       /* If there's a display string (or something similar) at point,
 26407          we are actually at the glyph to the left of point, so we need
 26408          to correct the X coordinate.  */
 26409       if (overshoot_expected)
 26410         {
 26411           if (it.bidi_p)
 26412             pt_x += pixel_width * it.bidi_it.scan_dir;
 26413           else
 26414             pt_x += pixel_width;
 26415         }
 26416 
 26417       /* Compute target X coordinate, either to the left or to the
 26418          right of point.  On TTY frames, all characters have the same
 26419          pixel width of 1, so we can use that.  On GUI frames we don't
 26420          have an easy way of getting at the pixel width of the
 26421          character to the left of point, so we use a different method
 26422          of getting to that place.  */
 26423       if (dir > 0)
 26424         target_x = pt_x + pixel_width;
 26425       else
 26426         target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
 26427 
 26428       /* Target X coordinate could be one line above or below the line
 26429          of point, in which case we need to adjust the target X
 26430          coordinate.  Also, if moving to the left, we need to begin at
 26431          the left edge of the point's screen line.  */
 26432       if (dir < 0)
 26433         {
 26434           if (pt_x > 0)
 26435             {
 26436               start_display (&it, w, pt);
 26437               if (it.line_wrap == TRUNCATE)
 26438                 it.last_visible_x = DISP_INFINITY;
 26439               reseat_at_previous_visible_line_start (&it);
 26440               it.current_x = it.current_y = it.hpos = 0;
 26441               if (pt_vpos != 0)
 26442                 move_it_by_lines (&it, pt_vpos);
 26443             }
 26444           else
 26445             {
 26446               move_it_by_lines (&it, -1);
 26447               target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
 26448               target_is_eol_p = true;
 26449               /* Under word-wrap, we don't know the x coordinate of
 26450                  the last character displayed on the previous line,
 26451                  which immediately precedes the wrap point.  To find
 26452                  out its x coordinate, we try moving to the right
 26453                  margin of the window, which will stop at the wrap
 26454                  point, and then reset target_x to point at the
 26455                  character that precedes the wrap point.  This is not
 26456                  needed on GUI frames, because (see below) there we
 26457                  move from the left margin one grapheme cluster at a
 26458                  time, and stop when we hit the wrap point.  */
 26459               if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
 26460                 {
 26461                   void *it_data = NULL;
 26462                   struct it it2;
 26463 
 26464                   SAVE_IT (it2, it, it_data);
 26465                   move_it_in_display_line_to (&it, ZV, target_x,
 26466                                               MOVE_TO_POS | MOVE_TO_X);
 26467                   /* If we arrived at target_x, that _is_ the last
 26468                      character on the previous line.  */
 26469                   if (it.current_x != target_x)
 26470                     target_x = it.current_x - 1;
 26471                   RESTORE_IT (&it, &it2, it_data);
 26472                 }
 26473             }
 26474         }
 26475       else
 26476         {
 26477           if (at_eol_p
 26478               || (target_x >= it.last_visible_x
 26479                   && it.line_wrap != TRUNCATE))
 26480             {
 26481               if (pt_x > 0)
 26482                 move_it_by_lines (&it, 0);
 26483               move_it_by_lines (&it, 1);
 26484               target_x = 0;
 26485             }
 26486         }
 26487 
 26488       /* Move to the target X coordinate.  */
 26489       /* On GUI frames, as we don't know the X coordinate of the
 26490          character to the left of point, moving point to the left
 26491          requires walking, one grapheme cluster at a time, until we
 26492          find ourself at a place immediately to the left of the
 26493          character at point.  */
 26494       if (FRAME_WINDOW_P (it.f) && dir < 0)
 26495         {
 26496           struct text_pos new_pos;
 26497           enum move_it_result rc = MOVE_X_REACHED;
 26498 
 26499           if (it.current_x == 0)
 26500             get_next_display_element (&it);
 26501           if (it.what == IT_COMPOSITION)
 26502             {
 26503               new_pos.charpos = it.cmp_it.charpos;
 26504               new_pos.bytepos = -1;
 26505             }
 26506           else
 26507             new_pos = it.current.pos;
 26508 
 26509           while (it.current_x + it.pixel_width <= target_x
 26510                  && (rc == MOVE_X_REACHED
 26511                      /* Under word-wrap, move_it_in_display_line_to
 26512                         stops at correct coordinates, but sometimes
 26513                         returns MOVE_POS_MATCH_OR_ZV.  */
 26514                      || (it.line_wrap == WORD_WRAP
 26515                          && rc == MOVE_POS_MATCH_OR_ZV)))
 26516             {
 26517               int new_x = it.current_x + it.pixel_width;
 26518 
 26519               /* For composed characters, we want the position of the
 26520                  first character in the grapheme cluster (usually, the
 26521                  composition's base character), whereas it.current
 26522                  might give us the position of the _last_ one, e.g. if
 26523                  the composition is rendered in reverse due to bidi
 26524                  reordering.  */
 26525               if (it.what == IT_COMPOSITION)
 26526                 {
 26527                   new_pos.charpos = it.cmp_it.charpos;
 26528                   new_pos.bytepos = -1;
 26529                 }
 26530               else
 26531                 new_pos = it.current.pos;
 26532               if (new_x == it.current_x)
 26533                 new_x++;
 26534               rc = move_it_in_display_line_to (&it, ZV, new_x,
 26535                                                MOVE_TO_POS | MOVE_TO_X);
 26536               if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
 26537                 break;
 26538             }
 26539           /* The previous position we saw in the loop is the one we
 26540              want.  */
 26541           if (new_pos.bytepos == -1)
 26542             new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
 26543           it.current.pos = new_pos;
 26544         }
 26545       else if (it.current_x != target_x)
 26546         move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
 26547 
 26548       /* If we ended up in a display string that covers point, move to
 26549          buffer position to the right in the visual order.  */
 26550       if (dir > 0)
 26551         {
 26552           while (IT_CHARPOS (it) == PT)
 26553             {
 26554               set_iterator_to_next (&it, false);
 26555               if (!get_next_display_element (&it))
 26556                 break;
 26557             }
 26558         }
 26559 
 26560       /* Move point to that position.  */
 26561       SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
 26562       bidi_unshelve_cache (itdata, false);
 26563     }
 26564 
 26565   return make_fixnum (PT);
 26566 
 26567 #undef ROW_GLYPH_NEWLINE_P
 26568 }
 26569 
 26570 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
 26571        Sbidi_resolved_levels, 0, 1, 0,
 26572        doc: /* Return the resolved bidirectional levels of characters at VPOS.
 26573 
 26574 The resolved levels are produced by the Emacs bidi reordering engine
 26575 that implements the UBA, the Unicode Bidirectional Algorithm.  Please
 26576 read the Unicode Standard Annex 9 (UAX#9) for background information
 26577 about these levels.
 26578 
 26579 VPOS is the zero-based number of the current window's screen line
 26580 for which to produce the resolved levels.  If VPOS is nil or omitted,
 26581 it defaults to the screen line of point.  If the window displays a
 26582 header line, VPOS of zero will report on the header line, and first
 26583 line of text in the window will have VPOS of 1.
 26584 
 26585 Value is an array of resolved levels, indexed by glyph number.
 26586 Glyphs are numbered from zero starting from the beginning of the
 26587 screen line, i.e. the left edge of the window for left-to-right lines
 26588 and from the right edge for right-to-left lines.  The resolved levels
 26589 are produced only for the window's text area; text in display margins
 26590 is not included.
 26591 
 26592 If the selected window's display is not up-to-date, or if the specified
 26593 screen line does not display text, this function returns nil.  It is
 26594 highly recommended to bind this function to some simple key, like F8,
 26595 in order to avoid these problems.
 26596 
 26597 This function exists mainly for testing the correctness of the
 26598 Emacs UBA implementation, in particular with the test suite.  */)
 26599   (Lisp_Object vpos)
 26600 {
 26601   struct window *w = XWINDOW (selected_window);
 26602   struct buffer *b = XBUFFER (w->contents);
 26603   int nrow;
 26604   struct glyph_row *row;
 26605 
 26606   if (NILP (vpos))
 26607     {
 26608       int d1, d2, d3, d4, d5;
 26609 
 26610       pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
 26611     }
 26612   else
 26613     {
 26614       CHECK_FIXNUM (vpos);
 26615       nrow = XFIXNUM (vpos);
 26616     }
 26617 
 26618   /* We require up-to-date glyph matrix for this window.  */
 26619   if (w->window_end_valid
 26620       && !windows_or_buffers_changed
 26621       && b
 26622       && !b->clip_changed
 26623       && !b->prevent_redisplay_optimizations_p
 26624       && !window_outdated (w)
 26625       && nrow >= 0
 26626       && nrow < w->current_matrix->nrows
 26627       && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
 26628       && MATRIX_ROW_DISPLAYS_TEXT_P (row))
 26629     {
 26630       struct glyph *g, *e, *g1;
 26631       int nglyphs, i;
 26632       Lisp_Object levels;
 26633 
 26634       if (!row->reversed_p)     /* Left-to-right glyph row.  */
 26635         {
 26636           g = g1 = row->glyphs[TEXT_AREA];
 26637           e = g + row->used[TEXT_AREA];
 26638 
 26639           /* Skip over glyphs at the start of the row that was
 26640              generated by redisplay for its own needs.  */
 26641           while (g < e
 26642                  && NILP (g->object)
 26643                  && g->charpos < 0)
 26644             g++;
 26645           g1 = g;
 26646 
 26647           /* Count the "interesting" glyphs in this row.  */
 26648           for (nglyphs = 0; g < e && !NILP (g->object); g++)
 26649             nglyphs++;
 26650 
 26651           /* Create and fill the array.  */
 26652           levels = make_uninit_vector (nglyphs);
 26653           for (i = 0; g1 < g; i++, g1++)
 26654             ASET (levels, i, make_fixnum (g1->resolved_level));
 26655         }
 26656       else                      /* Right-to-left glyph row.  */
 26657         {
 26658           g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 26659           e = row->glyphs[TEXT_AREA] - 1;
 26660           while (g > e
 26661                  && NILP (g->object)
 26662                  && g->charpos < 0)
 26663             g--;
 26664           g1 = g;
 26665           for (nglyphs = 0; g > e && !NILP (g->object); g--)
 26666             nglyphs++;
 26667           levels = make_uninit_vector (nglyphs);
 26668           for (i = 0; g1 > g; i++, g1--)
 26669             ASET (levels, i, make_fixnum (g1->resolved_level));
 26670         }
 26671       return levels;
 26672     }
 26673   else
 26674     return Qnil;
 26675 }
 26676 
 26677 
 26678 
 26679 /***********************************************************************
 26680                                Menu Bar
 26681  ***********************************************************************/
 26682 
 26683 /* Redisplay the menu bar in the frame for window W.
 26684 
 26685    The menu bar of X frames that don't have X toolkit support is
 26686    displayed in a special window W->frame->menu_bar_window.
 26687 
 26688    The menu bar of terminal frames is treated specially as far as
 26689    glyph matrices are concerned.  Menu bar lines are not part of
 26690    windows, so the update is done directly on the frame matrix rows
 26691    for the menu bar.  */
 26692 
 26693 static void
 26694 display_menu_bar (struct window *w)
 26695 {
 26696   struct frame *f = XFRAME (WINDOW_FRAME (w));
 26697   struct it it;
 26698   Lisp_Object items;
 26699   int i;
 26700 
 26701   /* Don't do all this for graphical frames.  */
 26702 #ifdef HAVE_NTGUI
 26703   if (FRAME_W32_P (f))
 26704     return;
 26705 #endif
 26706 #if defined (HAVE_PGTK)
 26707   if (FRAME_PGTK_P (f))
 26708     return;
 26709 #endif
 26710 
 26711 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26712   if (FRAME_X_P (f))
 26713     return;
 26714 #endif
 26715 
 26716 #ifdef HAVE_NS
 26717   if (FRAME_NS_P (f))
 26718     return;
 26719 #endif /* HAVE_NS */
 26720 
 26721 #ifdef HAVE_HAIKU
 26722   if (FRAME_HAIKU_P (f))
 26723     return;
 26724 #endif /* HAVE_HAIKU */
 26725 
 26726 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26727   eassert (!FRAME_WINDOW_P (f));
 26728   init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
 26729   it.first_visible_x = 0;
 26730   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26731 #elif defined (HAVE_X_WINDOWS) || defined (HAVE_ANDROID)
 26732   struct window *menu_window = NULL;
 26733   struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
 26734 
 26735   if (FRAME_WINDOW_P (f))
 26736     {
 26737       /* Menu bar lines are displayed in the desired matrix of the
 26738          dummy window menu_bar_window.  */
 26739       menu_window = XWINDOW (f->menu_bar_window);
 26740       init_iterator (&it, menu_window, -1, -1,
 26741                      menu_window->desired_matrix->rows,
 26742                      MENU_FACE_ID);
 26743     }
 26744   else
 26745 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 26746     {
 26747       /* This is a TTY frame, i.e. character hpos/vpos are used as
 26748          pixel x/y.  */
 26749       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
 26750                      MENU_FACE_ID);
 26751       it.first_visible_x = 0;
 26752       it.last_visible_x = FRAME_COLS (f);
 26753     }
 26754 
 26755   /* FIXME: This should be controlled by a user option.  See the
 26756      comments in redisplay_tool_bar and display_mode_line about
 26757      this.  */
 26758   it.paragraph_embedding = L2R;
 26759 
 26760   /* Clear all rows of the menu bar.  */
 26761   for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
 26762     {
 26763       struct glyph_row *row = it.glyph_row + i;
 26764       clear_glyph_row (row);
 26765       row->enabled_p = true;
 26766       row->full_width_p = true;
 26767       row->reversed_p = false;
 26768     }
 26769 
 26770   /* Display all items of the menu bar.  */
 26771   items = FRAME_MENU_BAR_ITEMS (it.f);
 26772   for (i = 0; i < ASIZE (items); i += 4)
 26773     {
 26774       Lisp_Object string;
 26775 
 26776       /* Stop at nil string.  */
 26777       string = AREF (items, i + 1);
 26778       if (NILP (string))
 26779         break;
 26780 
 26781       /* Remember where item was displayed.  */
 26782       ASET (items, i + 3, make_fixnum (it.hpos));
 26783 
 26784       /* Display the item, pad with one space.  */
 26785       if (it.current_x < it.last_visible_x)
 26786         display_string (NULL, string, Qnil, 0, 0, &it,
 26787                         SCHARS (string) + 1, 0, 0, STRING_MULTIBYTE (string));
 26788     }
 26789 
 26790   /* Fill out the line with spaces.  */
 26791   if (it.current_x < it.last_visible_x)
 26792     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 26793 
 26794   /* Compute the total height of the lines.  */
 26795   compute_line_metrics (&it);
 26796   it.glyph_row->full_width_p = true;
 26797   it.glyph_row->continued_p = false;
 26798   it.glyph_row->truncated_on_left_p = false;
 26799   it.glyph_row->truncated_on_right_p = false;
 26800 
 26801   /* This will break the moment someone tries to add another window
 26802      system that uses the no toolkit menu bar.  Oh well.  At least
 26803      there will be an error, meaning he will correct the ifdef inside
 26804      which `face' is defined.  */
 26805 #if defined HAVE_WINDOW_SYSTEM && !defined HAVE_EXT_MENU_BAR
 26806   /* Make a 3D menu bar have a shadow at its right end.  */
 26807   extend_face_to_end_of_line (&it);
 26808   if (face->box != FACE_NO_BOX)
 26809     {
 26810       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 26811                             + it.glyph_row->used[TEXT_AREA] - 1);
 26812       int box_thickness = face->box_vertical_line_width;
 26813       last->right_box_line_p = true;
 26814       /* Add back the space for the right box line we subtracted in
 26815          init_iterator, since the right_box_line_p flag will make the
 26816          glyph wider.  We actually add only as much space as is
 26817          available for the last glyph of the menu bar and whatever
 26818          space is left beyond it, since that glyph could be only
 26819          partially visible.  */
 26820       if (box_thickness > 0)
 26821         last->pixel_width += max (0, (box_thickness
 26822                                       - (it.current_x - it.last_visible_x)));
 26823     }
 26824 
 26825   /* With the non-toolkit version, modify the menu bar window height
 26826      accordingly.  */
 26827   if (FRAME_WINDOW_P (it.f) && menu_window)
 26828     {
 26829       struct glyph_row *row;
 26830       int delta_height;
 26831 
 26832       row = it.glyph_row;
 26833       delta_height
 26834         = ((row->y + row->height)
 26835            - WINDOW_BOX_HEIGHT_NO_MODE_LINE (menu_window));
 26836 
 26837       if (delta_height != 0)
 26838         {
 26839           FRAME_MENU_BAR_HEIGHT (it.f) += delta_height;
 26840           adjust_frame_size (it.f, -1, -1, 3, false, Qmenu_bar_lines);
 26841         }
 26842     }
 26843 #endif
 26844 }
 26845 
 26846 /* This code is never used on Android where there are only GUI and
 26847    initial frames.  */
 26848 
 26849 #ifndef HAVE_ANDROID
 26850 
 26851 /* Deep copy of a glyph row, including the glyphs.  */
 26852 static void
 26853 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
 26854 {
 26855   struct glyph *pointers[1 + LAST_AREA];
 26856   int to_used = to->used[TEXT_AREA];
 26857 
 26858   /* Save glyph pointers of TO.  */
 26859   memcpy (pointers, to->glyphs, sizeof to->glyphs);
 26860 
 26861   /* Do a structure assignment.  */
 26862   *to = *from;
 26863 
 26864   /* Restore original glyph pointers of TO.  */
 26865   memcpy (to->glyphs, pointers, sizeof to->glyphs);
 26866 
 26867   /* Copy the glyphs.  */
 26868   memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
 26869           min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
 26870 
 26871   /* If we filled only part of the TO row, fill the rest with
 26872      space_glyph (which will display as empty space).  */
 26873   if (to_used > from->used[TEXT_AREA])
 26874     fill_up_frame_row_with_spaces (to, to_used);
 26875 }
 26876 
 26877 /* Display one menu item on a TTY, by overwriting the glyphs in the
 26878    frame F's desired glyph matrix with glyphs produced from the menu
 26879    item text.  Called from term.c to display TTY drop-down menus one
 26880    item at a time.
 26881 
 26882    ITEM_TEXT is the menu item text as a C string.
 26883 
 26884    FACE_ID is the face ID to be used for this menu item.  FACE_ID
 26885    could specify one of 3 faces: a face for an enabled item, a face
 26886    for a disabled item, or a face for a selected item.
 26887 
 26888    X and Y are coordinates of the first glyph in the frame's desired
 26889    matrix to be overwritten by the menu item.  Since this is a TTY, Y
 26890    is the zero-based number of the glyph row and X is the zero-based
 26891    glyph number in the row, starting from left, where to start
 26892    displaying the item.
 26893 
 26894    SUBMENU means this menu item drops down a submenu, which
 26895    should be indicated by displaying a proper visual cue after the
 26896    item text.  */
 26897 
 26898 void
 26899 display_tty_menu_item (const char *item_text, int width, int face_id,
 26900                        int x, int y, bool submenu)
 26901 {
 26902   struct it it;
 26903   struct frame *f = SELECTED_FRAME ();
 26904   struct window *w = XWINDOW (f->selected_window);
 26905   struct glyph_row *row;
 26906   size_t item_len = strlen (item_text);
 26907 
 26908   eassert (FRAME_TERMCAP_P (f));
 26909 
 26910   /* Don't write beyond the matrix's last row.  This can happen for
 26911      TTY screens that are not high enough to show the entire menu.
 26912      (This is actually a bit of defensive programming, as
 26913      tty_menu_display already limits the number of menu items to one
 26914      less than the number of screen lines.)  */
 26915   if (y >= f->desired_matrix->nrows)
 26916     return;
 26917 
 26918   init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
 26919   it.first_visible_x = 0;
 26920   it.last_visible_x = FRAME_COLS (f) - 1;
 26921   row = it.glyph_row;
 26922   /* Start with the row contents from the current matrix.  */
 26923   deep_copy_glyph_row (row, f->current_matrix->rows + y);
 26924   bool saved_width = row->full_width_p;
 26925   row->full_width_p = true;
 26926   bool saved_reversed = row->reversed_p;
 26927   row->reversed_p = false;
 26928   row->enabled_p = true;
 26929 
 26930   /* Arrange for the menu item glyphs to start at (X,Y) and have the
 26931      desired face.  */
 26932   eassert (x < f->desired_matrix->matrix_w);
 26933   it.current_x = it.hpos = x;
 26934   it.current_y = it.vpos = y;
 26935   int saved_used = row->used[TEXT_AREA];
 26936   bool saved_truncated = row->truncated_on_right_p;
 26937   row->used[TEXT_AREA] = x;
 26938   it.face_id = face_id;
 26939   it.line_wrap = TRUNCATE;
 26940 
 26941   /* FIXME: This should be controlled by a user option.  See the
 26942      comments in redisplay_tool_bar and display_mode_line about this.
 26943      Also, if paragraph_embedding could ever be R2L, changes will be
 26944      needed to avoid shifting to the right the row characters in
 26945      term.c:append_glyph.  */
 26946   it.paragraph_embedding = L2R;
 26947 
 26948   /* Pad with a space on the left.  */
 26949   display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
 26950   width--;
 26951   /* Display the menu item, pad with spaces to WIDTH.  */
 26952   if (submenu)
 26953     {
 26954       display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26955                       item_len, 0, FRAME_COLS (f) - 1, -1);
 26956       width -= item_len;
 26957       /* Indicate with " >" that there's a submenu.  */
 26958       display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
 26959                       FRAME_COLS (f) - 1, -1);
 26960     }
 26961   else
 26962     display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26963                     width, 0, FRAME_COLS (f) - 1, -1);
 26964 
 26965   row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
 26966   row->truncated_on_right_p = saved_truncated;
 26967   row->hash = row_hash (row);
 26968   row->full_width_p = saved_width;
 26969   row->reversed_p = saved_reversed;
 26970 }
 26971 
 26972 #endif
 26973 
 26974 
 26975 /***********************************************************************
 26976                               Mode Line
 26977  ***********************************************************************/
 26978 
 26979 /* Redisplay mode lines in the window tree whose root is WINDOW.
 26980    If FORCE, redisplay mode lines unconditionally.
 26981    Otherwise, redisplay only mode lines that are garbaged.  Value is
 26982    the number of windows whose mode lines were redisplayed.  */
 26983 
 26984 static int
 26985 redisplay_mode_lines (Lisp_Object window, bool force)
 26986 {
 26987   int nwindows = 0;
 26988 
 26989   while (!NILP (window))
 26990     {
 26991       struct window *w = XWINDOW (window);
 26992 
 26993       if (WINDOWP (w->contents))
 26994         nwindows += redisplay_mode_lines (w->contents, force);
 26995       else if (force
 26996                || FRAME_GARBAGED_P (XFRAME (w->frame))
 26997                || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
 26998         {
 26999           struct text_pos lpoint;
 27000           struct buffer *old = current_buffer;
 27001 
 27002           /* Set the window's buffer for the mode line display.  */
 27003           SET_TEXT_POS (lpoint, PT, PT_BYTE);
 27004           set_buffer_internal_1 (XBUFFER (w->contents));
 27005 
 27006           /* Point refers normally to the selected window.  For any
 27007              other window, set up appropriate value.  */
 27008           if (!EQ (window, selected_window))
 27009             {
 27010               struct text_pos pt;
 27011 
 27012               CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
 27013               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
 27014             }
 27015 
 27016           /* Display mode lines.  */
 27017           clear_glyph_matrix (w->desired_matrix);
 27018           if (display_mode_lines (w))
 27019             ++nwindows;
 27020 
 27021           /* Restore old settings.  */
 27022           set_buffer_internal_1 (old);
 27023           TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 27024         }
 27025 
 27026       window = w->next;
 27027     }
 27028 
 27029   return nwindows;
 27030 }
 27031 
 27032 
 27033 /* Display the mode line, the header line, and the tab-line of window
 27034    W.  Value is the sum number of mode lines, header lines, and tab
 27035    lines actually displayed.  */
 27036 
 27037 static int
 27038 display_mode_lines (struct window *w)
 27039 {
 27040   Lisp_Object old_selected_window = selected_window;
 27041   Lisp_Object new_frame = w->frame;
 27042   specpdl_ref count = SPECPDL_INDEX ();
 27043   int n = 0;
 27044 
 27045   record_unwind_protect (restore_selected_window, selected_window);
 27046   record_unwind_protect
 27047     (restore_frame_selected_window, XFRAME (new_frame)->selected_window);
 27048 
 27049   if (window_wants_mode_line (w))
 27050     {
 27051       Lisp_Object window;
 27052       Lisp_Object default_help
 27053         = buffer_local_value (Qmode_line_default_help_echo, w->contents);
 27054 
 27055       /* Set up mode line help echo.  Do this before selecting w so it
 27056          can reasonably tell whether a mouse click will select w.  */
 27057       XSETWINDOW (window, w);
 27058       if (FUNCTIONP (default_help))
 27059         wset_mode_line_help_echo (w, safe_call1 (default_help, window));
 27060       else if (STRINGP (default_help))
 27061         wset_mode_line_help_echo (w, default_help);
 27062       else
 27063         wset_mode_line_help_echo (w, Qnil);
 27064     }
 27065 
 27066   selected_frame = new_frame;
 27067   /* FIXME: If we were to allow the mode-line's computation changing the buffer
 27068      or window's point, then we'd need select_window_1 here as well.  */
 27069   XSETWINDOW (selected_window, w);
 27070   XFRAME (new_frame)->selected_window = selected_window;
 27071 
 27072   /* These will be set while the mode line specs are processed.  */
 27073   line_number_displayed = false;
 27074   w->column_number_displayed = -1;
 27075 
 27076   if (window_wants_mode_line (w))
 27077     {
 27078       Lisp_Object window_mode_line_format
 27079         = window_parameter (w, Qmode_line_format);
 27080       struct window *sel_w = XWINDOW (old_selected_window);
 27081 
 27082       /* Select mode line face based on the real selected window.  */
 27083       display_mode_line (w,
 27084                          CURRENT_MODE_LINE_ACTIVE_FACE_ID_3 (sel_w, sel_w, w),
 27085                          NILP (window_mode_line_format)
 27086                          ? BVAR (current_buffer, mode_line_format)
 27087                          : window_mode_line_format);
 27088       ++n;
 27089     }
 27090 
 27091   if (window_wants_tab_line (w))
 27092     {
 27093       Lisp_Object window_tab_line_format
 27094         = window_parameter (w, Qtab_line_format);
 27095 
 27096       display_mode_line (w, TAB_LINE_FACE_ID,
 27097                          NILP (window_tab_line_format)
 27098                          ? BVAR (current_buffer, tab_line_format)
 27099                          : window_tab_line_format);
 27100       ++n;
 27101     }
 27102 
 27103   if (window_wants_header_line (w))
 27104     {
 27105       Lisp_Object window_header_line_format
 27106         = window_parameter (w, Qheader_line_format);
 27107 
 27108       display_mode_line (w, HEADER_LINE_FACE_ID,
 27109                          NILP (window_header_line_format)
 27110                          ? BVAR (current_buffer, header_line_format)
 27111                          : window_header_line_format);
 27112       ++n;
 27113     }
 27114 
 27115   unbind_to (count, Qnil);
 27116 
 27117   if (n > 0)
 27118     w->must_be_updated_p = true;
 27119   return n;
 27120 }
 27121 
 27122 
 27123 /* Display mode or header/tab line of window W.  FACE_ID specifies
 27124    which line to display; it is either MODE_LINE_ACTIVE_FACE_ID,
 27125    HEADER_LINE_FACE_ID or TAB_LINE_FACE_ID.  FORMAT is the
 27126    mode/header/tab line format to display.  Value is the pixel height
 27127    of the mode/header/tab line displayed.  */
 27128 
 27129 static int
 27130 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 27131 {
 27132   struct it it;
 27133   struct face *face;
 27134   specpdl_ref count = SPECPDL_INDEX ();
 27135 
 27136   init_iterator (&it, w, -1, -1, NULL, face_id);
 27137   /* Don't extend on a previously drawn mode-line.
 27138      This may happen if called from pos_visible_p.  */
 27139   it.glyph_row->enabled_p = false;
 27140   prepare_desired_row (w, it.glyph_row, true);
 27141 
 27142   it.glyph_row->mode_line_p = true;
 27143   if (face_id == TAB_LINE_FACE_ID)
 27144     {
 27145       it.glyph_row->tab_line_p = true;
 27146       w->desired_matrix->tab_line_p = true;
 27147     }
 27148   else if (face_id == HEADER_LINE_FACE_ID)
 27149     w->desired_matrix->header_line_p = true;
 27150 
 27151   /* FIXME: This should be controlled by a user option.  But
 27152      supporting such an option is not trivial, since the mode line is
 27153      made up of many separate strings.  */
 27154   it.paragraph_embedding = L2R;
 27155 
 27156   record_unwind_protect (unwind_format_mode_line,
 27157                          format_mode_line_unwind_data (NULL, NULL,
 27158                                                        Qnil, false));
 27159 
 27160   /* Temporarily make frame's keyboard the current kboard so that
 27161      kboard-local variables in the mode_line_format will get the right
 27162      values.  */
 27163   push_kboard (FRAME_KBOARD (it.f));
 27164   record_unwind_save_match_data ();
 27165 
 27166   if (NILP (Vmode_line_compact)
 27167       || face_id == HEADER_LINE_FACE_ID || face_id == TAB_LINE_FACE_ID)
 27168     {
 27169       mode_line_target = MODE_LINE_DISPLAY;
 27170       display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27171     }
 27172   else
 27173     {
 27174       Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
 27175       if (EQ (Vmode_line_compact, Qlong)
 27176           && WINDOW_TOTAL_COLS (w) >= SCHARS (mode_string))
 27177         {
 27178           /* The window is wide enough; just display the mode line we
 27179              just computed. */
 27180           display_string (NULL, mode_string, Qnil,
 27181                           0, 0, &it, 0, 0, 0,
 27182                           STRING_MULTIBYTE (mode_string));
 27183         }
 27184       else
 27185         {
 27186           /* Compress the mode line. */
 27187           ptrdiff_t i = 0, i_byte = 0, start = 0;
 27188           int prev = 0;
 27189 
 27190           while (i < SCHARS (mode_string))
 27191             {
 27192               int c = fetch_string_char_advance (mode_string, &i, &i_byte);
 27193               if (c == ' ' && prev == ' ')
 27194                 {
 27195                   display_string (NULL,
 27196                                   Fsubstring (mode_string, make_fixnum (start),
 27197                                               make_fixnum (i - 1)),
 27198                                   Qnil, 0, 0, &it, 0, 0, 0,
 27199                                   STRING_MULTIBYTE (mode_string));
 27200                   /* Skip past the rest of the space characters. */
 27201                   while (c == ' ' && i < SCHARS (mode_string))
 27202                       c = fetch_string_char_advance (mode_string, &i, &i_byte);
 27203                   start = i - 1;
 27204                 }
 27205               prev = c;
 27206             }
 27207 
 27208           /* Display the final bit. */
 27209           if (start < i)
 27210             display_string (NULL,
 27211                             Fsubstring (mode_string, make_fixnum (start),
 27212                                         make_fixnum (i)),
 27213                             Qnil, 0, 0, &it, 0, 0, 0,
 27214                             STRING_MULTIBYTE (mode_string));
 27215         }
 27216     }
 27217   pop_kboard ();
 27218 
 27219   unbind_to (count, Qnil);
 27220 
 27221   /* Fill up with spaces.  */
 27222   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
 27223 
 27224   compute_line_metrics (&it);
 27225   it.glyph_row->full_width_p = true;
 27226   it.glyph_row->continued_p = false;
 27227   it.glyph_row->truncated_on_left_p = false;
 27228   it.glyph_row->truncated_on_right_p = false;
 27229 
 27230   /* Make a 3D mode-line have a shadow at its right end.  */
 27231   face = FACE_FROM_ID (it.f, face_id);
 27232   extend_face_to_end_of_line (&it);
 27233   if (face->box != FACE_NO_BOX)
 27234     {
 27235       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 27236                             + it.glyph_row->used[TEXT_AREA] - 1);
 27237       int box_thickness = face->box_vertical_line_width;
 27238       last->right_box_line_p = true;
 27239       /* Add back the space for the right box line we subtracted in
 27240          init_iterator, since the right_box_line_p flag will make the
 27241          glyph wider.  We actually add only as much space as is
 27242          available for the last glyph of the modeline and whatever
 27243          space is left beyond it, since that glyph could be only
 27244          partially visible */
 27245       if (box_thickness > 0)
 27246         last->pixel_width += max (0, (box_thickness
 27247                                       - (it.current_x - it.last_visible_x)));
 27248     }
 27249 
 27250   return it.glyph_row->height;
 27251 }
 27252 
 27253 /* Move element ELT in LIST to the front of LIST.
 27254    Return the updated list.  */
 27255 
 27256 static Lisp_Object
 27257 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
 27258 {
 27259   register Lisp_Object tail, prev;
 27260   register Lisp_Object tem;
 27261 
 27262   tail = list;
 27263   prev = Qnil;
 27264   while (CONSP (tail))
 27265     {
 27266       tem = XCAR (tail);
 27267 
 27268       if (EQ (elt, tem))
 27269         {
 27270           /* Splice out the link TAIL.  */
 27271           if (NILP (prev))
 27272             list = XCDR (tail);
 27273           else
 27274             Fsetcdr (prev, XCDR (tail));
 27275 
 27276           /* Now make it the first.  */
 27277           Fsetcdr (tail, list);
 27278           return tail;
 27279         }
 27280       else
 27281         prev = tail;
 27282       tail = XCDR (tail);
 27283       maybe_quit ();
 27284     }
 27285 
 27286   /* Not found--return unchanged LIST.  */
 27287   return list;
 27288 }
 27289 
 27290 /* Subroutine to call Fset_text_properties through
 27291    internal_condition_case_n.  ARGS are the arguments of
 27292    Fset_text_properties, in order.  */
 27293 
 27294 static Lisp_Object
 27295 safe_set_text_properties (ptrdiff_t nargs, Lisp_Object *args)
 27296 {
 27297   eassert (nargs == 4);
 27298   return Fset_text_properties (args[0], args[1], args[2], args[3]);
 27299 }
 27300 
 27301 /* Contribute ELT to the mode line for window IT->w.  How it
 27302    translates into text depends on its data type.
 27303 
 27304    IT describes the display environment in which we display, as usual.
 27305 
 27306    DEPTH is the depth in recursion.  It is used to prevent
 27307    infinite recursion here.
 27308 
 27309    FIELD_WIDTH is the number of characters the display of ELT should
 27310    occupy in the mode line, and PRECISION is the maximum number of
 27311    characters to display from ELT's representation.  See
 27312    display_string for details.
 27313 
 27314    Returns the hpos of the end of the text generated by ELT.
 27315 
 27316    PROPS is a property list to add to any string we encounter.
 27317 
 27318    If RISKY, remove (disregard) any properties in any string
 27319    we encounter, and ignore :eval and :propertize.
 27320 
 27321    The global variable `mode_line_target' determines whether the
 27322    output is passed to `store_mode_line_noprop',
 27323    `store_mode_line_string', or `display_string'.  */
 27324 
 27325 static int
 27326 display_mode_element (struct it *it, int depth, int field_width, int precision,
 27327                       Lisp_Object elt, Lisp_Object props, bool risky)
 27328 {
 27329   int n = 0, field, prec;
 27330   bool literal = false;
 27331 
 27332  tail_recurse:
 27333   if (depth > 100)
 27334     elt = build_string ("*too-deep*");
 27335 
 27336   depth++;
 27337 
 27338   switch (XTYPE (elt))
 27339     {
 27340     case Lisp_String:
 27341       {
 27342         /* A string: output it and check for %-constructs within it.  */
 27343         unsigned char c;
 27344         ptrdiff_t offset = 0;
 27345 
 27346         if (SCHARS (elt) > 0
 27347             && (!NILP (props) || risky))
 27348           {
 27349             Lisp_Object oprops, aelt;
 27350             oprops = Ftext_properties_at (make_fixnum (0), elt);
 27351 
 27352             /* If the starting string's properties are not what
 27353                we want, translate the string.  Also, if the string
 27354                is risky, do that anyway.  */
 27355 
 27356             if (NILP (Fequal (props, oprops)) || risky)
 27357               {
 27358                 /* If the starting string has properties,
 27359                    merge the specified ones onto the existing ones.  */
 27360                 if (! NILP (oprops) && !risky)
 27361                   {
 27362                     Lisp_Object tem;
 27363 
 27364                     oprops = Fcopy_sequence (oprops);
 27365                     tem = props;
 27366                     while (CONSP (tem) && CONSP (XCDR (tem)))
 27367                       {
 27368                         oprops = plist_put (oprops, XCAR (tem),
 27369                                             XCAR (XCDR (tem)));
 27370                         tem = XCDR (XCDR (tem));
 27371                       }
 27372                     props = oprops;
 27373                   }
 27374 
 27375                 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
 27376                 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
 27377                   {
 27378                     /* AELT is what we want.  Move it to the front
 27379                        without consing.  */
 27380                     elt = XCAR (aelt);
 27381                     mode_line_proptrans_alist
 27382                       = move_elt_to_front (aelt, mode_line_proptrans_alist);
 27383                   }
 27384                 else
 27385                   {
 27386                     Lisp_Object tem;
 27387 
 27388                     /* If AELT has the wrong props, it is useless.
 27389                        so get rid of it.  */
 27390                     if (! NILP (aelt))
 27391                       mode_line_proptrans_alist
 27392                         = Fdelq (aelt, mode_line_proptrans_alist);
 27393 
 27394                     elt = Fcopy_sequence (elt);
 27395                     /* PROPS might cause set-text-properties to signal
 27396                        an error, so we call it via internal_condition_case_n,
 27397                        to avoid an infloop in redisplay due to the error.  */
 27398                     internal_condition_case_n (safe_set_text_properties,
 27399                                                4,
 27400                                                ((Lisp_Object [])
 27401                                                {make_fixnum (0),
 27402                                                    Flength (elt),
 27403                                                    props,
 27404                                                    elt}),
 27405                                                Qt, safe_eval_handler);
 27406                     /* Add this item to mode_line_proptrans_alist.  */
 27407                     mode_line_proptrans_alist
 27408                       = Fcons (Fcons (elt, props),
 27409                                mode_line_proptrans_alist);
 27410                     /* Truncate mode_line_proptrans_alist
 27411                        to at most 50 elements.  */
 27412                     tem = Fnthcdr (make_fixnum (50),
 27413                                    mode_line_proptrans_alist);
 27414                     if (! NILP (tem))
 27415                       XSETCDR (tem, Qnil);
 27416                   }
 27417               }
 27418           }
 27419 
 27420         offset = 0;
 27421 
 27422         if (literal)
 27423           {
 27424             prec = precision - n;
 27425             switch (mode_line_target)
 27426               {
 27427               case MODE_LINE_NOPROP:
 27428               case MODE_LINE_TITLE:
 27429                 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
 27430                 break;
 27431               case MODE_LINE_STRING:
 27432                 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
 27433                 break;
 27434               case MODE_LINE_DISPLAY:
 27435                 n += display_string (NULL, elt, Qnil, 0, 0, it,
 27436                                      0, prec, 0, STRING_MULTIBYTE (elt));
 27437                 break;
 27438               }
 27439 
 27440             break;
 27441           }
 27442 
 27443         /* Handle the non-literal case.  */
 27444 
 27445         while ((precision <= 0 || n < precision)
 27446                && SREF (elt, offset) != 0
 27447                && (mode_line_target != MODE_LINE_DISPLAY
 27448                    || it->current_x < it->last_visible_x))
 27449           {
 27450             ptrdiff_t last_offset = offset;
 27451 
 27452             /* Advance to end of string or next format specifier.  */
 27453             while ((c = SREF (elt, offset++)) != '\0' && c != '%')
 27454               ;
 27455 
 27456             if (offset - 1 != last_offset)
 27457               {
 27458                 ptrdiff_t nchars, nbytes;
 27459 
 27460                 /* Output to end of string or up to '%'.  Field width
 27461                    is length of string.  Don't output more than
 27462                    PRECISION allows us.  */
 27463                 offset--;
 27464 
 27465                 prec = c_string_width (SDATA (elt) + last_offset,
 27466                                        offset - last_offset, precision - n,
 27467                                        &nchars, &nbytes);
 27468 
 27469                 switch (mode_line_target)
 27470                   {
 27471                   case MODE_LINE_NOPROP:
 27472                   case MODE_LINE_TITLE:
 27473                     n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
 27474                     break;
 27475                   case MODE_LINE_STRING:
 27476                     {
 27477                       ptrdiff_t bytepos = last_offset;
 27478                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27479                       ptrdiff_t endpos = (precision <= 0
 27480                                           ? string_byte_to_char (elt, offset)
 27481                                           : charpos + nchars);
 27482                       Lisp_Object mode_string
 27483                         = Fsubstring (elt, make_fixnum (charpos),
 27484                                       make_fixnum (endpos));
 27485                       n += store_mode_line_string (NULL, mode_string, false,
 27486                                                    0, 0, Qnil);
 27487                     }
 27488                     break;
 27489                   case MODE_LINE_DISPLAY:
 27490                     {
 27491                       ptrdiff_t bytepos = last_offset;
 27492                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27493 
 27494                       if (precision <= 0)
 27495                         nchars = string_byte_to_char (elt, offset) - charpos;
 27496                       n += display_string (NULL, elt, Qnil, 0, charpos,
 27497                                            it, 0, nchars, 0,
 27498                                            STRING_MULTIBYTE (elt));
 27499                     }
 27500                     break;
 27501                   }
 27502               }
 27503             else /* c == '%' */
 27504               {
 27505                 ptrdiff_t percent_position = offset;
 27506 
 27507                 /* Get the specified minimum width.  Zero means
 27508                    don't pad.  */
 27509                 field = 0;
 27510                 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
 27511                   field = field * 10 + c - '0';
 27512 
 27513                 /* Don't pad beyond the total padding allowed.  */
 27514                 if (field_width - n > 0 && field > field_width - n)
 27515                   field = field_width - n;
 27516 
 27517                 /* Note that either PRECISION <= 0 or N < PRECISION.  */
 27518                 prec = precision - n;
 27519 
 27520                 if (c == 'M')
 27521                   n += display_mode_element (it, depth, field, prec,
 27522                                              Vglobal_mode_string, props,
 27523                                              risky);
 27524                 else if (c != 0)
 27525                   {
 27526                     bool multibyte;
 27527                     ptrdiff_t bytepos, charpos;
 27528                     const char *spec;
 27529                     Lisp_Object string;
 27530 
 27531                     bytepos = percent_position;
 27532                     charpos = (STRING_MULTIBYTE (elt)
 27533                                ? string_byte_to_char (elt, bytepos)
 27534                                : bytepos);
 27535                     spec = decode_mode_spec (it->w, c, field, &string);
 27536                     eassert (NILP (string) || STRINGP (string));
 27537                     multibyte = !NILP (string) && STRING_MULTIBYTE (string);
 27538                     /* Non-ASCII characters in SPEC should cause mode-line
 27539                        element be displayed as a multibyte string.  */
 27540                     ptrdiff_t nbytes = strlen (spec);
 27541                     ptrdiff_t nchars, mb_nbytes;
 27542                     parse_str_as_multibyte ((const unsigned char *)spec, nbytes,
 27543                                             &nchars, &mb_nbytes);
 27544                     if (!(nbytes == nchars || nbytes != mb_nbytes))
 27545                       multibyte = true;
 27546 
 27547                     switch (mode_line_target)
 27548                       {
 27549                       case MODE_LINE_NOPROP:
 27550                       case MODE_LINE_TITLE:
 27551                         n += store_mode_line_noprop (spec, field, prec);
 27552                         break;
 27553                       case MODE_LINE_STRING:
 27554                         {
 27555                           Lisp_Object tem = build_string (spec);
 27556                           props = Ftext_properties_at (make_fixnum (charpos), elt);
 27557                           /* Should only keep face property in props */
 27558                           n += store_mode_line_string (NULL, tem, false,
 27559                                                        field, prec, props);
 27560                         }
 27561                         break;
 27562                       case MODE_LINE_DISPLAY:
 27563                         {
 27564                           int nglyphs_before, nwritten;
 27565 
 27566                           nglyphs_before = it->glyph_row->used[TEXT_AREA];
 27567                           nwritten = display_string (spec, string, elt,
 27568                                                      charpos, 0, it,
 27569                                                      field, prec, 0,
 27570                                                      multibyte);
 27571 
 27572                           /* Assign to the glyphs written above the
 27573                              string where the `%x' came from, position
 27574                              of the `%'.  */
 27575                           if (nwritten > 0)
 27576                             {
 27577                               struct glyph *glyph
 27578                                 = (it->glyph_row->glyphs[TEXT_AREA]
 27579                                    + nglyphs_before);
 27580                               int i;
 27581 
 27582                               for (i = 0; i < nwritten; ++i)
 27583                                 {
 27584                                   glyph[i].object = elt;
 27585                                   glyph[i].charpos = charpos;
 27586                                 }
 27587 
 27588                               n += nwritten;
 27589                             }
 27590                         }
 27591                         break;
 27592                       }
 27593                   }
 27594                 else /* c == 0 */
 27595                   break;
 27596               }
 27597           }
 27598       }
 27599       break;
 27600 
 27601     case Lisp_Symbol:
 27602       /* A symbol: process the value of the symbol recursively
 27603          as if it appeared here directly.  Avoid error if symbol void.
 27604          Special case: if value of symbol is a string, output the string
 27605          literally.  */
 27606       {
 27607         register Lisp_Object tem;
 27608 
 27609         /* If the variable is not marked as risky to set
 27610            then its contents are risky to use.  */
 27611         if (NILP (Fget (elt, Qrisky_local_variable)))
 27612           risky = true;
 27613 
 27614         tem = Fboundp (elt);
 27615         if (!NILP (tem))
 27616           {
 27617             tem = Fsymbol_value (elt);
 27618             /* If value is a string, output that string literally:
 27619                don't check for % within it.  */
 27620             if (STRINGP (tem))
 27621               literal = true;
 27622 
 27623             if (!EQ (tem, elt))
 27624               {
 27625                 /* Give up right away for nil or t.  */
 27626                 elt = tem;
 27627                 goto tail_recurse;
 27628               }
 27629           }
 27630       }
 27631       break;
 27632 
 27633     case Lisp_Cons:
 27634       {
 27635         register Lisp_Object car, tem;
 27636 
 27637         /* A cons cell: five distinct cases.
 27638            If first element is :eval or :propertize, do something special.
 27639            If first element is a string or a cons, process all the elements
 27640            and effectively concatenate them.
 27641            If first element is a negative number, truncate displaying cdr to
 27642            at most that many characters.  If positive, pad (with spaces)
 27643            to at least that many characters.
 27644            If first element is a symbol, process the cadr or caddr recursively
 27645            according to whether the symbol's value is non-nil or nil.  */
 27646         car = XCAR (elt);
 27647         if (EQ (car, QCeval))
 27648           {
 27649             /* An element of the form (:eval FORM) means evaluate FORM
 27650                and use the result as mode line elements.  */
 27651 
 27652             if (risky)
 27653               break;
 27654 
 27655             if (CONSP (XCDR (elt)))
 27656               {
 27657                 Lisp_Object spec;
 27658                 spec = safe__eval (true, XCAR (XCDR (elt)));
 27659                 /* The :eval form could delete the frame stored in the
 27660                    iterator, which will cause a crash if we try to
 27661                    access faces and other fields (e.g., FRAME_KBOARD)
 27662                    on that frame.  This is a nonsensical thing to do,
 27663                    and signaling an error from redisplay might be
 27664                    dangerous, but we cannot continue with an invalid frame.  */
 27665                 if (!FRAME_LIVE_P (it->f))
 27666                   signal_error (":eval deleted the frame being displayed", elt);
 27667                 n += display_mode_element (it, depth, field_width - n,
 27668                                            precision - n, spec, props,
 27669                                            risky);
 27670               }
 27671           }
 27672         else if (EQ (car, QCpropertize))
 27673           {
 27674             /* An element of the form (:propertize ELT PROPS...)
 27675                means display ELT but applying properties PROPS.  */
 27676 
 27677             if (risky)
 27678               break;
 27679 
 27680             if (CONSP (XCDR (elt)))
 27681               n += display_mode_element (it, depth, field_width - n,
 27682                                          precision - n, XCAR (XCDR (elt)),
 27683                                          XCDR (XCDR (elt)), risky);
 27684           }
 27685         else if (SYMBOLP (car))
 27686           {
 27687             tem = Fboundp (car);
 27688             elt = XCDR (elt);
 27689             if (!CONSP (elt))
 27690               goto invalid;
 27691             /* elt is now the cdr, and we know it is a cons cell.
 27692                Use its car if CAR has a non-nil value.  */
 27693             if (!NILP (tem))
 27694               {
 27695                 tem = Fsymbol_value (car);
 27696                 if (!NILP (tem))
 27697                   {
 27698                     elt = XCAR (elt);
 27699                     goto tail_recurse;
 27700                   }
 27701               }
 27702             /* Symbol's value is nil (or symbol is unbound)
 27703                Get the cddr of the original list
 27704                and if possible find the caddr and use that.  */
 27705             elt = XCDR (elt);
 27706             if (NILP (elt))
 27707               break;
 27708             else if (!CONSP (elt))
 27709               goto invalid;
 27710             elt = XCAR (elt);
 27711             goto tail_recurse;
 27712           }
 27713         else if (FIXNUMP (car))
 27714           {
 27715             register int lim = XFIXNUM (car);
 27716             elt = XCDR (elt);
 27717             if (lim < 0)
 27718               {
 27719                 /* Negative int means reduce maximum width.  */
 27720                 if (precision <= 0)
 27721                   precision = -lim;
 27722                 else
 27723                   precision = min (precision, -lim);
 27724               }
 27725             else if (lim > 0)
 27726               {
 27727                 /* Padding specified.  Don't let it be more than
 27728                    current maximum.  */
 27729                 if (precision > 0)
 27730                   lim = min (precision, lim);
 27731 
 27732                 /* If that's more padding than already wanted, queue it.
 27733                    But don't reduce padding already specified even if
 27734                    that is beyond the current truncation point.  */
 27735                 field_width = max (lim, field_width);
 27736               }
 27737             goto tail_recurse;
 27738           }
 27739         else if (STRINGP (car) || CONSP (car))
 27740           FOR_EACH_TAIL_SAFE (elt)
 27741             {
 27742               if (0 < precision && precision <= n)
 27743                 break;
 27744               n += display_mode_element (it, depth,
 27745                                          /* Pad after only the last
 27746                                             list element.  */
 27747                                          (! CONSP (XCDR (elt))
 27748                                           ? field_width - n
 27749                                           : 0),
 27750                                          precision - n, XCAR (elt),
 27751                                          props, risky);
 27752             }
 27753       }
 27754       break;
 27755 
 27756     default:
 27757     invalid:
 27758       elt = build_string ("*invalid*");
 27759       goto tail_recurse;
 27760     }
 27761 
 27762   /* Pad to FIELD_WIDTH.  */
 27763   if (field_width > 0 && n < field_width)
 27764     {
 27765       switch (mode_line_target)
 27766         {
 27767         case MODE_LINE_NOPROP:
 27768         case MODE_LINE_TITLE:
 27769           n += store_mode_line_noprop ("", field_width - n, 0);
 27770           break;
 27771         case MODE_LINE_STRING:
 27772           n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
 27773                                        Qnil);
 27774           break;
 27775         case MODE_LINE_DISPLAY:
 27776           n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
 27777                                0, 0, 0);
 27778           break;
 27779         }
 27780     }
 27781 
 27782   return n;
 27783 }
 27784 
 27785 /* Store a mode-line string element in mode_line_string_list.
 27786 
 27787    If STRING is non-null, display that C string.  Otherwise, the Lisp
 27788    string LISP_STRING is displayed.
 27789 
 27790    FIELD_WIDTH is the minimum number of output glyphs to produce.
 27791    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 27792    with spaces.  FIELD_WIDTH <= 0 means don't pad.
 27793 
 27794    PRECISION is the maximum number of characters to output from
 27795    STRING.  PRECISION <= 0  means don't truncate the string.
 27796 
 27797    If COPY_STRING, make a copy of LISP_STRING before adding
 27798    properties to the string.
 27799 
 27800    PROPS are the properties to add to the string.
 27801    The mode_line_string_face face property is always added to the string.
 27802  */
 27803 
 27804 static int
 27805 store_mode_line_string (const char *string, Lisp_Object lisp_string,
 27806                         bool copy_string,
 27807                         int field_width, int precision, Lisp_Object props)
 27808 {
 27809   ptrdiff_t len;
 27810   int n = 0;
 27811 
 27812   if (string != NULL)
 27813     {
 27814       len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
 27815       lisp_string = make_string (string, len);
 27816       if (NILP (props))
 27817         props = mode_line_string_face_prop;
 27818       else if (!NILP (mode_line_string_face))
 27819         {
 27820           Lisp_Object face = plist_get (props, Qface);
 27821           props = Fcopy_sequence (props);
 27822           if (NILP (face))
 27823             face = mode_line_string_face;
 27824           else
 27825             face = list2 (face, mode_line_string_face);
 27826           props = plist_put (props, Qface, face);
 27827         }
 27828       Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27829                             props, lisp_string);
 27830     }
 27831   else
 27832     {
 27833       len = SCHARS (lisp_string);
 27834       if (precision > 0 && len > precision)
 27835         {
 27836           len = precision;
 27837           lisp_string = Fsubstring (lisp_string, make_fixnum (0), make_fixnum (len));
 27838           precision = -1;
 27839         }
 27840       if (!NILP (mode_line_string_face))
 27841         {
 27842           Lisp_Object face;
 27843           if (NILP (props))
 27844             props = Ftext_properties_at (make_fixnum (0), lisp_string);
 27845           face = plist_get (props, Qface);
 27846           if (NILP (face))
 27847             face = mode_line_string_face;
 27848           else
 27849             face = list2 (face, mode_line_string_face);
 27850           props = list2 (Qface, face);
 27851           if (copy_string)
 27852             lisp_string = Fcopy_sequence (lisp_string);
 27853         }
 27854       if (!NILP (props))
 27855         Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27856                               props, lisp_string);
 27857     }
 27858 
 27859   if (len > 0)
 27860     {
 27861       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27862       n += len;
 27863     }
 27864 
 27865   if (field_width > len)
 27866     {
 27867       field_width -= len;
 27868       lisp_string = Fmake_string (make_fixnum (field_width), make_fixnum (' '),
 27869                                   Qnil);
 27870       if (!NILP (props))
 27871         Fadd_text_properties (make_fixnum (0), make_fixnum (field_width),
 27872                               props, lisp_string);
 27873       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27874       n += field_width;
 27875     }
 27876 
 27877   return n;
 27878 }
 27879 
 27880 
 27881 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
 27882        1, 4, 0,
 27883        doc: /* Format a string out of a mode line format specification.
 27884 First arg FORMAT specifies the mode line format (see `mode-line-format'
 27885 for details) to use.
 27886 
 27887 By default, the format is evaluated for the currently selected window.
 27888 
 27889 Optional second arg FACE specifies the face property to put on all
 27890 characters for which no face is specified.  The value nil means the
 27891 default face.  The value t means whatever face the window's mode line
 27892 currently uses (either `mode-line' or `mode-line-inactive',
 27893 depending on whether the window is the selected window or not).
 27894 An integer value means the value string has no text
 27895 properties.
 27896 
 27897 Optional third and fourth args WINDOW and BUFFER specify the window
 27898 and buffer to use as the context for the formatting (defaults
 27899 are the selected window and the WINDOW's buffer).  */)
 27900      (Lisp_Object format, Lisp_Object face,
 27901       Lisp_Object window, Lisp_Object buffer)
 27902 {
 27903   struct it it;
 27904   int len;
 27905   struct window *w;
 27906   struct buffer *old_buffer = NULL;
 27907   int face_id;
 27908   bool no_props = FIXNUMP (face);
 27909   specpdl_ref count = SPECPDL_INDEX ();
 27910   Lisp_Object str;
 27911   int string_start = 0;
 27912 
 27913   w = decode_any_window (window);
 27914   XSETWINDOW (window, w);
 27915 
 27916   if (NILP (buffer))
 27917     buffer = w->contents;
 27918   CHECK_BUFFER (buffer);
 27919   if (!BUFFER_LIVE_P (XBUFFER (buffer)))
 27920     error ("Attempt to format a mode line for a dead buffer");
 27921 
 27922   /* Make formatting the modeline a non-op when noninteractive, otherwise
 27923      there will be problems later caused by a partially initialized frame.  */
 27924   if (NILP (format) || noninteractive)
 27925     return empty_unibyte_string;
 27926 
 27927   if (no_props)
 27928     face = Qnil;
 27929 
 27930   face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
 27931     : EQ (face, Qt) ? (EQ (window, selected_window)
 27932                        ? MODE_LINE_ACTIVE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
 27933     : EQ (face, Qmode_line_active) ? MODE_LINE_ACTIVE_FACE_ID
 27934     : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
 27935     : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
 27936     : EQ (face, Qtab_line) ? TAB_LINE_FACE_ID
 27937     : EQ (face, Qtab_bar) ? TAB_BAR_FACE_ID
 27938     : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
 27939     : DEFAULT_FACE_ID;
 27940 
 27941   old_buffer = current_buffer;
 27942 
 27943   /* Save things including mode_line_proptrans_alist,
 27944      and set that to nil so that we don't alter the outer value.  */
 27945   record_unwind_protect (unwind_format_mode_line,
 27946                          format_mode_line_unwind_data
 27947                            (XFRAME (WINDOW_FRAME (w)),
 27948                             old_buffer, selected_window, true));
 27949   mode_line_proptrans_alist = Qnil;
 27950 
 27951   Fselect_window (window, Qt);
 27952   set_buffer_internal_1 (XBUFFER (buffer));
 27953 
 27954   init_iterator (&it, w, -1, -1, NULL, face_id);
 27955 
 27956   if (no_props)
 27957     {
 27958       mode_line_target = MODE_LINE_NOPROP;
 27959       mode_line_string_face_prop = Qnil;
 27960       mode_line_string_list = Qnil;
 27961       string_start = MODE_LINE_NOPROP_LEN (0);
 27962     }
 27963   else
 27964     {
 27965       mode_line_target = MODE_LINE_STRING;
 27966       mode_line_string_list = Qnil;
 27967       mode_line_string_face = face;
 27968       mode_line_string_face_prop
 27969         = NILP (face) ? Qnil : list2 (Qface, face);
 27970     }
 27971 
 27972   push_kboard (FRAME_KBOARD (it.f));
 27973   display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27974   pop_kboard ();
 27975 
 27976   if (no_props)
 27977     {
 27978       len = MODE_LINE_NOPROP_LEN (string_start);
 27979       str = make_string (mode_line_noprop_buf + string_start, len);
 27980     }
 27981   else
 27982     {
 27983       mode_line_string_list = Fnreverse (mode_line_string_list);
 27984       str = Fmapconcat (Qidentity, mode_line_string_list,
 27985                         empty_unibyte_string);
 27986     }
 27987 
 27988   return unbind_to (count, str);
 27989 }
 27990 
 27991 /* Write a null-terminated, right justified decimal representation of
 27992    the positive integer D to BUF using a minimal field width WIDTH.  */
 27993 
 27994 static void
 27995 pint2str (register char *buf, register int width, register ptrdiff_t d)
 27996 {
 27997   register char *p = buf;
 27998 
 27999   if (d <= 0)
 28000     *p++ = '0';
 28001   else
 28002     {
 28003       while (d > 0)
 28004         {
 28005           *p++ = d % 10 + '0';
 28006           d /= 10;
 28007         }
 28008     }
 28009 
 28010   for (width -= (int) (p - buf); width > 0; --width)
 28011     *p++ = ' ';
 28012   *p-- = '\0';
 28013   while (p > buf)
 28014     {
 28015       d = *buf;
 28016       *buf++ = *p;
 28017       *p-- = d;
 28018     }
 28019 }
 28020 
 28021 /* Write a null-terminated, right justified decimal and "human
 28022    readable" representation of the nonnegative integer D to BUF using
 28023    a minimal field width WIDTH.  D should be smaller than 999.5e24. */
 28024 
 28025 static const char power_letter[] =
 28026   {
 28027     0,   /* no letter */
 28028     'k', /* kilo */
 28029     'M', /* mega */
 28030     'G', /* giga */
 28031     'T', /* tera */
 28032     'P', /* peta */
 28033     'E', /* exa */
 28034     'Z', /* zetta */
 28035     'Y', /* yotta */
 28036     'R', /* ronna */
 28037     'Q'  /* quetta */
 28038   };
 28039 
 28040 static void
 28041 pint2hrstr (char *buf, int width, ptrdiff_t d)
 28042 {
 28043   /* We aim to represent the nonnegative integer D as
 28044      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 28045   ptrdiff_t quotient = d;
 28046   int remainder = 0;
 28047   /* -1 means: do not use TENTHS. */
 28048   int tenths = -1;
 28049   int exponent = 0;
 28050 
 28051   /* Length of QUOTIENT.TENTHS as a string. */
 28052   int length;
 28053 
 28054   char * psuffix;
 28055   char * p;
 28056 
 28057   if (quotient >= 1000)
 28058     {
 28059       /* Scale to the appropriate EXPONENT. */
 28060       do
 28061         {
 28062           remainder = quotient % 1000;
 28063           quotient /= 1000;
 28064           exponent++;
 28065         }
 28066       while (quotient >= 1000);
 28067 
 28068       /* Round to nearest and decide whether to use TENTHS or not. */
 28069       if (quotient <= 9)
 28070         {
 28071           tenths = remainder / 100;
 28072           if (remainder % 100 >= 50)
 28073             {
 28074               if (tenths < 9)
 28075                 tenths++;
 28076               else
 28077                 {
 28078                   quotient++;
 28079                   if (quotient == 10)
 28080                     tenths = -1;
 28081                   else
 28082                     tenths = 0;
 28083                 }
 28084             }
 28085         }
 28086       else
 28087         if (remainder >= 500)
 28088           {
 28089             if (quotient < 999)
 28090               quotient++;
 28091             else
 28092               {
 28093                 quotient = 1;
 28094                 exponent++;
 28095                 tenths = 0;
 28096               }
 28097           }
 28098     }
 28099 
 28100   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
 28101   if (tenths == -1 && quotient <= 99)
 28102     if (quotient <= 9)
 28103       length = 1;
 28104     else
 28105       length = 2;
 28106   else
 28107     length = 3;
 28108   p = psuffix = buf + max (width, length);
 28109 
 28110   /* Print EXPONENT. */
 28111   *psuffix++ = power_letter[exponent];
 28112   *psuffix = '\0';
 28113 
 28114   /* Print TENTHS. */
 28115   if (tenths >= 0)
 28116     {
 28117       *--p = '0' + tenths;
 28118       *--p = '.';
 28119     }
 28120 
 28121   /* Print QUOTIENT. */
 28122   do
 28123     {
 28124       int digit = quotient % 10;
 28125       *--p =  '0' + digit;
 28126     }
 28127   while ((quotient /= 10) != 0);
 28128 
 28129   /* Print leading spaces. */
 28130   while (buf < p)
 28131     *--p = ' ';
 28132 }
 28133 
 28134 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
 28135    If EOL_FLAG, set also a mnemonic character for end-of-line
 28136    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
 28137 
 28138 static unsigned char invalid_eol_type[] = "(*invalid*)";
 28139 
 28140 static char *
 28141 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
 28142 {
 28143   Lisp_Object val;
 28144   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 28145   const unsigned char *eol_str;
 28146   int eol_str_len;
 28147   /* The EOL conversion we are using.  */
 28148   Lisp_Object eoltype;
 28149 
 28150   val = CODING_SYSTEM_SPEC (coding_system);
 28151   eoltype = Qnil;
 28152 
 28153   if (!VECTORP (val))           /* Not yet decided.  */
 28154     {
 28155       *buf++ = multibyte ? '-' : ' ';
 28156       if (eol_flag)
 28157         eoltype = eol_mnemonic_undecided;
 28158       /* Don't mention EOL conversion if it isn't decided.  */
 28159     }
 28160   else
 28161     {
 28162       Lisp_Object attrs;
 28163       Lisp_Object eolvalue;
 28164 
 28165       attrs = AREF (val, 0);
 28166       eolvalue = AREF (val, 2);
 28167 
 28168       if (multibyte)
 28169         buf += CHAR_STRING (XFIXNAT (CODING_ATTR_MNEMONIC (attrs)),
 28170                             (unsigned char *) buf);
 28171       else
 28172         *buf++ = ' ';
 28173 
 28174       if (eol_flag)
 28175         {
 28176           /* The EOL conversion that is normal on this system.  */
 28177 
 28178           if (NILP (eolvalue))  /* Not yet decided.  */
 28179             eoltype = eol_mnemonic_undecided;
 28180           else if (VECTORP (eolvalue)) /* Not yet decided.  */
 28181             eoltype = eol_mnemonic_undecided;
 28182           else                  /* eolvalue is Qunix, Qdos, or Qmac.  */
 28183             eoltype = (EQ (eolvalue, Qunix)
 28184                        ? eol_mnemonic_unix
 28185                        : EQ (eolvalue, Qdos)
 28186                        ? eol_mnemonic_dos : eol_mnemonic_mac);
 28187         }
 28188     }
 28189 
 28190   if (eol_flag)
 28191     {
 28192       /* Mention the EOL conversion if it is not the usual one.  */
 28193       if (STRINGP (eoltype))
 28194         {
 28195           eol_str = SDATA (eoltype);
 28196           eol_str_len = SBYTES (eoltype);
 28197         }
 28198       else if (CHARACTERP (eoltype))
 28199         {
 28200           int c = XFIXNAT (eoltype);
 28201           return buf + CHAR_STRING (c, (unsigned char *) buf);
 28202         }
 28203       else
 28204         {
 28205           eol_str = invalid_eol_type;
 28206           eol_str_len = sizeof (invalid_eol_type) - 1;
 28207         }
 28208       memcpy (buf, eol_str, eol_str_len);
 28209       buf += eol_str_len;
 28210     }
 28211 
 28212   return buf;
 28213 }
 28214 
 28215 /* Return the approximate percentage N is of D (rounding upward), or 99,
 28216    whichever is less.  Assume 0 < D and 0 <= N <= D * INT_MAX / 100.  */
 28217 
 28218 static int
 28219 percent99 (ptrdiff_t n, ptrdiff_t d)
 28220 {
 28221   int percent = (d - 1 + 100.0 * n) / d;
 28222   return min (percent, 99);
 28223 }
 28224 
 28225 /* Return a string for the output of a mode line %-spec for window W,
 28226    generated by character C.  FIELD_WIDTH > 0 means pad the string
 28227    returned with spaces to that value.  Set *STRING to be a Lisp
 28228    string if the resulting string is taken from that Lisp string;
 28229    otherwise, set *STRING to Qnil.
 28230 
 28231    Note we operate on the current buffer for most purposes.  */
 28232 
 28233 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 28234 
 28235 static const char *
 28236 decode_mode_spec (struct window *w, register int c, int field_width,
 28237                   Lisp_Object *string)
 28238 {
 28239   Lisp_Object obj;
 28240   struct frame *f = XFRAME (WINDOW_FRAME (w));
 28241   char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
 28242   /* We are going to use f->decode_mode_spec_buffer as the buffer to
 28243      produce strings from numerical values, so limit preposterously
 28244      large values of FIELD_WIDTH to avoid overrunning the buffer's
 28245      end.  The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
 28246      bytes plus the terminating null.  */
 28247   int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
 28248   struct buffer *b = current_buffer;
 28249 
 28250   obj = Qnil;
 28251   *string = Qnil;
 28252 
 28253   switch (c)
 28254     {
 28255     case '*':
 28256       if (!NILP (BVAR (b, read_only)))
 28257         return "%";
 28258       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28259         return "*";
 28260       return "-";
 28261 
 28262     case '+':
 28263       /* This differs from %* only for a modified read-only buffer.  */
 28264       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28265         return "*";
 28266       if (!NILP (BVAR (b, read_only)))
 28267         return "%";
 28268       return "-";
 28269 
 28270     case '&':
 28271       /* This differs from %* in ignoring read-only-ness.  */
 28272       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28273         return "*";
 28274       return "-";
 28275 
 28276     case '%':
 28277       return "%";
 28278 
 28279     case '[':
 28280       {
 28281         int i;
 28282         char *p;
 28283 
 28284         if (command_loop_level > 5)
 28285           return "[[[... ";
 28286         p = decode_mode_spec_buf;
 28287         for (i = 0; i < command_loop_level; i++)
 28288           *p++ = '[';
 28289         *p = 0;
 28290         return decode_mode_spec_buf;
 28291       }
 28292 
 28293     case ']':
 28294       {
 28295         int i;
 28296         char *p;
 28297 
 28298         if (command_loop_level > 5)
 28299           return " ...]]]";
 28300         p = decode_mode_spec_buf;
 28301         for (i = 0; i < command_loop_level; i++)
 28302           *p++ = ']';
 28303         *p = 0;
 28304         return decode_mode_spec_buf;
 28305       }
 28306 
 28307     case '-':
 28308       {
 28309         register int i;
 28310 
 28311         /* Let lots_of_dashes be a string of infinite length.  */
 28312         if (mode_line_target == MODE_LINE_NOPROP
 28313             || mode_line_target == MODE_LINE_STRING)
 28314           return "--";
 28315         if (field_width <= 0
 28316             || field_width > sizeof (lots_of_dashes))
 28317           {
 28318             for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
 28319               decode_mode_spec_buf[i] = '-';
 28320             decode_mode_spec_buf[i] = '\0';
 28321             return decode_mode_spec_buf;
 28322           }
 28323         else
 28324           return lots_of_dashes;
 28325       }
 28326 
 28327     case 'b':
 28328       obj = BVAR (b, name);
 28329       break;
 28330 
 28331     case 'c':
 28332     case 'C':
 28333       /* %c, %C, and %l are ignored in `frame-title-format'.
 28334          (In redisplay_internal, the frame title is drawn _before_ the
 28335          windows are updated, so the stuff which depends on actual
 28336          window contents (such as %l) may fail to render properly, or
 28337          even crash emacs.)  */
 28338       if (mode_line_target == MODE_LINE_TITLE)
 28339         return "";
 28340       else
 28341         {
 28342           ptrdiff_t col = current_column ();
 28343           int disp_col = (c == 'C') ? col + 1 : col;
 28344           w->column_number_displayed = col;
 28345           pint2str (decode_mode_spec_buf, width, disp_col);
 28346           return decode_mode_spec_buf;
 28347         }
 28348 
 28349     case 'e':
 28350 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
 28351       {
 28352         if (NILP (Vmemory_full))
 28353           return "";
 28354         else
 28355           return "!MEM FULL! ";
 28356       }
 28357 #else
 28358       return "";
 28359 #endif
 28360 
 28361     case 'F':
 28362       /* %F displays the frame name.  */
 28363       if (!NILP (f->title))
 28364         return SSDATA (f->title);
 28365       if (f->explicit_name || ! FRAME_WINDOW_P (f))
 28366         return SSDATA (f->name);
 28367       return "Emacs";
 28368 
 28369     case 'f':
 28370       obj = BVAR (b, filename);
 28371       break;
 28372 
 28373     case 'i':
 28374       {
 28375         ptrdiff_t size = ZV - BEGV;
 28376         pint2str (decode_mode_spec_buf, width, size);
 28377         return decode_mode_spec_buf;
 28378       }
 28379 
 28380     case 'I':
 28381       {
 28382         ptrdiff_t size = ZV - BEGV;
 28383         pint2hrstr (decode_mode_spec_buf, width, size);
 28384         return decode_mode_spec_buf;
 28385       }
 28386 
 28387     case 'l':
 28388       {
 28389         ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 28390         ptrdiff_t topline, nlines, height;
 28391         ptrdiff_t junk;
 28392 
 28393         /* %c, %C, and %l are ignored in `frame-title-format'.  */
 28394         if (mode_line_target == MODE_LINE_TITLE)
 28395           return "";
 28396 
 28397         startpos = marker_position (w->start);
 28398         startpos_byte = marker_byte_position (w->start);
 28399         height = WINDOW_TOTAL_LINES (w);
 28400         /* We cannot cope with w->start being outside of the
 28401            accessible portion of the buffer; in particular,
 28402            display_count_lines call below might infloop if called with
 28403            startpos_byte outside of the [BEGV_BYTE..ZV_BYTE] region.
 28404            Such w->start means we were called in some "creative" way
 28405            when the buffer's restriction was changed, but the window
 28406            wasn't yet redisplayed after that.  If that happens, we
 28407            need to determine a new base line.  */
 28408         if (!(BUF_BEGV_BYTE (b) <= startpos_byte
 28409               && startpos_byte <= BUF_ZV_BYTE (b)))
 28410           {
 28411             startpos = BUF_BEGV (b);
 28412             startpos_byte = BUF_BEGV_BYTE (b);
 28413             w->base_line_pos = 0;
 28414             w->base_line_number = 0;
 28415           }
 28416 
 28417         /* If we decided that this buffer isn't suitable for line numbers,
 28418            don't forget that too fast.  */
 28419         if (w->base_line_pos == -1)
 28420           goto no_value;
 28421 
 28422         /* If the buffer is very big, don't waste time.  */
 28423         if (FIXNUMP (Vline_number_display_limit)
 28424             && BUF_ZV (b) - BUF_BEGV (b) > XFIXNUM (Vline_number_display_limit))
 28425           {
 28426             w->base_line_pos = 0;
 28427             w->base_line_number = 0;
 28428             goto no_value;
 28429           }
 28430 
 28431         if (w->base_line_number > 0
 28432             && w->base_line_pos > 0
 28433             && w->base_line_pos <= startpos)
 28434           {
 28435             line = w->base_line_number;
 28436             linepos = w->base_line_pos;
 28437             linepos_byte = buf_charpos_to_bytepos (b, linepos);
 28438           }
 28439         else
 28440           {
 28441             line = 1;
 28442             linepos = BUF_BEGV (b);
 28443             linepos_byte = BUF_BEGV_BYTE (b);
 28444           }
 28445 
 28446         /* Count lines from base line to window start position.  */
 28447         nlines = display_count_lines (linepos_byte,
 28448                                       startpos_byte,
 28449                                       startpos, &junk);
 28450 
 28451         topline = nlines + line;
 28452 
 28453         /* Determine a new base line, if the old one is too close
 28454            or too far away, or if we did not have one.
 28455            "Too close" means it's plausible a scroll-down would
 28456            go back past it.  */
 28457         if (startpos == BUF_BEGV (b))
 28458           {
 28459             w->base_line_number = topline;
 28460             w->base_line_pos = BUF_BEGV (b);
 28461           }
 28462         else if (nlines < height + 25 || nlines > height * 3 + 50
 28463                  || linepos == BUF_BEGV (b))
 28464           {
 28465             ptrdiff_t limit = BUF_BEGV (b);
 28466             ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 28467             ptrdiff_t position;
 28468             ptrdiff_t distance
 28469               = (line_number_display_limit_width < 0 ? 0
 28470                  : ckd_mul (&distance, line_number_display_limit_width,
 28471                             height * 2 + 30)
 28472                  ? PTRDIFF_MAX : distance);
 28473 
 28474             if (startpos - distance > limit)
 28475               {
 28476                 limit = startpos - distance;
 28477                 limit_byte = CHAR_TO_BYTE (limit);
 28478               }
 28479 
 28480             nlines = display_count_lines (startpos_byte,
 28481                                           limit_byte,
 28482                                           - (height * 2 + 30),
 28483                                           &position);
 28484             /* If we couldn't find the lines we wanted within
 28485                line_number_display_limit_width chars per line,
 28486                give up on line numbers for this window.  */
 28487             if (position == limit_byte && limit == startpos - distance)
 28488               {
 28489                 w->base_line_pos = -1;
 28490                 w->base_line_number = 0;
 28491                 goto no_value;
 28492               }
 28493 
 28494             w->base_line_number = topline - nlines;
 28495             w->base_line_pos = BYTE_TO_CHAR (position);
 28496           }
 28497 
 28498         /* Now count lines from the start pos to point.  */
 28499         nlines = display_count_lines (startpos_byte,
 28500                                       PT_BYTE, PT, &junk);
 28501 
 28502         /* Record that we did display the line number.  */
 28503         line_number_displayed = true;
 28504 
 28505         /* Make the string to show.  */
 28506         pint2str (decode_mode_spec_buf, width, topline + nlines);
 28507         return decode_mode_spec_buf;
 28508     no_value:
 28509         {
 28510           char *p = decode_mode_spec_buf;
 28511           int pad = width - 2;
 28512           while (pad-- > 0)
 28513             *p++ = ' ';
 28514           *p++ = '?';
 28515           *p++ = '?';
 28516           *p = '\0';
 28517           return decode_mode_spec_buf;
 28518         }
 28519       }
 28520       break;
 28521 
 28522     case 'm':
 28523       obj = BVAR (b, mode_name);
 28524       break;
 28525 
 28526     case 'n':
 28527       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
 28528         return " Narrow";
 28529       break;
 28530 
 28531       /* Display the "degree of travel" of the window through the buffer.  */
 28532     case 'o':
 28533       {
 28534         ptrdiff_t toppos = marker_position (w->start);
 28535         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28536         ptrdiff_t begv = BUF_BEGV (b);
 28537         ptrdiff_t zv = BUF_ZV (b);
 28538 
 28539         if (zv <= botpos)
 28540           return toppos <= begv ? "All" : "Bottom";
 28541         else if (toppos <= begv)
 28542           return "Top";
 28543         else
 28544           {
 28545           sprintf (decode_mode_spec_buf, "%2d%%",
 28546                    percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
 28547           return decode_mode_spec_buf;
 28548           }
 28549       }
 28550 
 28551       /* Display percentage of buffer above the top of the screen.  */
 28552     case 'p':
 28553       {
 28554         ptrdiff_t pos = marker_position (w->start);
 28555         ptrdiff_t begv = BUF_BEGV (b);
 28556         ptrdiff_t zv = BUF_ZV (b);
 28557 
 28558         if (w->window_end_pos <= BUF_Z (b) - zv)
 28559           return pos <= begv ? "All" : "Bottom";
 28560         else if (pos <= begv)
 28561           return "Top";
 28562         else
 28563           {
 28564             sprintf (decode_mode_spec_buf, "%2d%%",
 28565                      percent99 (pos - begv, zv - begv));
 28566             return decode_mode_spec_buf;
 28567           }
 28568       }
 28569 
 28570       /* Display percentage of size above the bottom of the screen.  */
 28571     case 'P':
 28572       {
 28573         ptrdiff_t toppos = marker_position (w->start);
 28574         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28575         ptrdiff_t begv = BUF_BEGV (b);
 28576         ptrdiff_t zv = BUF_ZV (b);
 28577 
 28578         if (zv <= botpos)
 28579           return toppos <= begv ? "All" : "Bottom";
 28580         else
 28581           {
 28582             sprintf (decode_mode_spec_buf,
 28583                      &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
 28584                      percent99 (botpos - begv, zv - begv));
 28585             return decode_mode_spec_buf;
 28586           }
 28587       }
 28588 
 28589       /* Display percentage offsets of top and bottom of the window,
 28590          using "All" (but not "Top" or "Bottom") where appropriate.  */
 28591     case 'q':
 28592       {
 28593         ptrdiff_t toppos = marker_position (w->start);
 28594         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28595         ptrdiff_t begv = BUF_BEGV (b);
 28596         ptrdiff_t zv = BUF_ZV (b);
 28597         int top_perc, bot_perc;
 28598 
 28599         if ((toppos <= begv) && (zv <= botpos))
 28600           return "All   ";
 28601 
 28602         top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
 28603         bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
 28604 
 28605         if (top_perc == bot_perc)
 28606           sprintf (decode_mode_spec_buf, "%d%%", top_perc);
 28607         else
 28608           sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
 28609 
 28610         return decode_mode_spec_buf;
 28611       }
 28612 
 28613     case 's':
 28614       /* status of process */
 28615       obj = Fget_buffer_process (Fcurrent_buffer ());
 28616       if (NILP (obj))
 28617         return "no process";
 28618 #ifndef MSDOS
 28619       obj = Fsymbol_name (Fprocess_status (obj));
 28620 #endif
 28621       break;
 28622 
 28623     case '@':
 28624       {
 28625         specpdl_ref count = inhibit_garbage_collection ();
 28626         Lisp_Object curdir = BVAR (current_buffer, directory);
 28627         Lisp_Object val = Qnil;
 28628 
 28629         if (STRINGP (curdir))
 28630           val = safe_call1 (intern ("file-remote-p"), curdir);
 28631 
 28632         val = unbind_to (count, val);
 28633 
 28634         if (NILP (val))
 28635           return "-";
 28636         else
 28637           return "@";
 28638       }
 28639 
 28640     case 'z':
 28641       /* coding-system (not including end-of-line format) */
 28642     case 'Z':
 28643       /* coding-system (including end-of-line type) */
 28644       {
 28645         bool eol_flag = (c == 'Z');
 28646         char *p = decode_mode_spec_buf;
 28647 
 28648         if (! FRAME_WINDOW_P (f))
 28649           {
 28650             /* No need to mention EOL here--the terminal never needs
 28651                to do EOL conversion.  */
 28652             p = decode_mode_spec_coding (CODING_ID_NAME
 28653                                          (FRAME_KEYBOARD_CODING (f)->id),
 28654                                          p, false);
 28655             p = decode_mode_spec_coding (CODING_ID_NAME
 28656                                          (FRAME_TERMINAL_CODING (f)->id),
 28657                                          p, false);
 28658           }
 28659         p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
 28660                                      p, eol_flag);
 28661 
 28662 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
 28663 #ifdef subprocesses
 28664         obj = Fget_buffer_process (Fcurrent_buffer ());
 28665         if (PROCESSP (obj))
 28666           {
 28667             p = decode_mode_spec_coding
 28668               (XPROCESS (obj)->decode_coding_system, p, eol_flag);
 28669             p = decode_mode_spec_coding
 28670               (XPROCESS (obj)->encode_coding_system, p, eol_flag);
 28671           }
 28672 #endif /* subprocesses */
 28673 #endif /* false */
 28674         *p = 0;
 28675         return decode_mode_spec_buf;
 28676       }
 28677     }
 28678 
 28679   if (STRINGP (obj))
 28680     {
 28681       *string = obj;
 28682       return SSDATA (obj);
 28683     }
 28684   else
 28685     return "";
 28686 }
 28687 
 28688 /* Return the number of lines between start_byte and end_byte in the
 28689    current buffer. */
 28690 
 28691 ptrdiff_t
 28692 count_lines (ptrdiff_t start_byte, ptrdiff_t end_byte)
 28693 {
 28694   ptrdiff_t ignored;
 28695   return display_count_lines (start_byte, end_byte, ZV, &ignored);
 28696 }
 28697 
 28698 /* Count up to COUNT lines starting from START_BYTE.  COUNT negative
 28699    means count lines back from START_BYTE.  But don't go beyond
 28700    LIMIT_BYTE.  Return the number of lines thus found (always
 28701    nonnegative).
 28702 
 28703    Set *BYTE_POS_PTR to the byte position where we stopped.  This is
 28704    either the position COUNT lines after/before START_BYTE, if we
 28705    found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
 28706    COUNT lines.  */
 28707 
 28708 static ptrdiff_t
 28709 display_count_lines (ptrdiff_t start_byte,
 28710                      ptrdiff_t limit_byte, ptrdiff_t count,
 28711                      ptrdiff_t *byte_pos_ptr)
 28712 {
 28713   register unsigned char *cursor;
 28714   unsigned char *base;
 28715 
 28716   register ptrdiff_t ceiling;
 28717   register unsigned char *ceiling_addr;
 28718   ptrdiff_t orig_count = count;
 28719 
 28720   /* If we are not in selective display mode,
 28721      check only for newlines.  */
 28722   bool selective_display
 28723     = (!NILP (BVAR (current_buffer, selective_display))
 28724        && !FIXNUMP (BVAR (current_buffer, selective_display)));
 28725 
 28726   if (count > 0)
 28727     {
 28728       while (start_byte < limit_byte)
 28729         {
 28730           ceiling =  BUFFER_CEILING_OF (start_byte);
 28731           ceiling = min (limit_byte - 1, ceiling);
 28732           ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
 28733           base = (cursor = BYTE_POS_ADDR (start_byte));
 28734 
 28735           do
 28736             {
 28737               if (selective_display)
 28738                 {
 28739                   while (*cursor != '\n' && *cursor != 015
 28740                          && ++cursor != ceiling_addr)
 28741                     continue;
 28742                   if (cursor == ceiling_addr)
 28743                     break;
 28744                 }
 28745               else
 28746                 {
 28747                   cursor = memchr (cursor, '\n', ceiling_addr - cursor);
 28748                   if (! cursor)
 28749                     break;
 28750                 }
 28751 
 28752               cursor++;
 28753 
 28754               if (--count == 0)
 28755                 {
 28756                   start_byte += cursor - base;
 28757                   *byte_pos_ptr = start_byte;
 28758                   return orig_count;
 28759                 }
 28760             }
 28761           while (cursor < ceiling_addr);
 28762 
 28763           start_byte += ceiling_addr - base;
 28764         }
 28765     }
 28766   else
 28767     {
 28768       while (start_byte > limit_byte)
 28769         {
 28770           ceiling = BUFFER_FLOOR_OF (start_byte - 1);
 28771           ceiling = max (limit_byte, ceiling);
 28772           ceiling_addr = BYTE_POS_ADDR (ceiling);
 28773           base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
 28774           while (true)
 28775             {
 28776               if (selective_display)
 28777                 {
 28778                   while (--cursor >= ceiling_addr
 28779                          && *cursor != '\n' && *cursor != 015)
 28780                     continue;
 28781                   if (cursor < ceiling_addr)
 28782                     break;
 28783                 }
 28784               else
 28785                 {
 28786                   cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
 28787                   if (! cursor)
 28788                     break;
 28789                 }
 28790 
 28791               if (++count == 0)
 28792                 {
 28793                   start_byte += cursor - base + 1;
 28794                   *byte_pos_ptr = start_byte;
 28795                   /* When scanning backwards, we should
 28796                      not count the newline posterior to which we stop.  */
 28797                   return - orig_count - 1;
 28798                 }
 28799             }
 28800           start_byte += ceiling_addr - base;
 28801         }
 28802     }
 28803 
 28804   *byte_pos_ptr = limit_byte;
 28805 
 28806   if (count < 0)
 28807     return - orig_count + count;
 28808   return orig_count - count;
 28809 
 28810 }
 28811 
 28812 
 28813 
 28814 /***********************************************************************
 28815                          Displaying strings
 28816  ***********************************************************************/
 28817 
 28818 /* Display a NUL-terminated string, starting with index START.
 28819 
 28820    If STRING is non-null, display that C string.  Otherwise, the Lisp
 28821    string LISP_STRING is displayed.  There's a case that STRING is
 28822    non-null and LISP_STRING is not nil.  It means STRING is a string
 28823    data of LISP_STRING.  In that case, we display LISP_STRING while
 28824    ignoring its text properties.
 28825 
 28826    If FACE_STRING is not nil, FACE_STRING_POS is a position in
 28827    FACE_STRING.  Display STRING or LISP_STRING with the face at
 28828    FACE_STRING_POS in FACE_STRING:
 28829 
 28830    Display the string in the environment given by IT, but use the
 28831    standard display table, temporarily.
 28832 
 28833    FIELD_WIDTH is the minimum number of output glyphs to produce.
 28834    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 28835    with spaces.  If STRING has more characters, more than FIELD_WIDTH
 28836    glyphs will be produced.  FIELD_WIDTH <= 0 means don't pad.
 28837 
 28838    PRECISION is the maximum number of characters to output from
 28839    STRING.  PRECISION < 0  means don't truncate the string.
 28840 
 28841    This is roughly equivalent to printf format specifiers:
 28842 
 28843    FIELD_WIDTH  PRECISION       PRINTF
 28844    ----------------------------------------
 28845    -1           -1              %s
 28846    -1           10              %.10s
 28847    10           -1              %10s
 28848    20           10              %20.10s
 28849 
 28850    MULTIBYTE zero means do not display multibyte chars, > 0 means do
 28851    display them, and < 0 means obey the current buffer's value of
 28852    enable_multibyte_characters.
 28853 
 28854    Value is the number of columns displayed.  */
 28855 
 28856 static int
 28857 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 28858                 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
 28859                 int field_width, int precision, int max_x, int multibyte)
 28860 {
 28861   int hpos_at_start = it->hpos;
 28862   int saved_face_id = it->face_id;
 28863   struct glyph_row *row = it->glyph_row;
 28864   ptrdiff_t it_charpos;
 28865 
 28866   /* Initialize the iterator IT for iteration over STRING beginning
 28867      with index START.  */
 28868   reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string,
 28869                     start, precision, field_width, multibyte);
 28870 
 28871   if (string && STRINGP (lisp_string))
 28872     /* LISP_STRING is the one returned by decode_mode_spec.  We should
 28873        ignore its text properties.  */
 28874     it->stop_charpos = it->end_charpos;
 28875 
 28876   /* If displaying STRING, set up the face of the iterator from
 28877      FACE_STRING, if that's given.  */
 28878   if (STRINGP (face_string))
 28879     {
 28880       ptrdiff_t endptr;
 28881       struct face *face;
 28882 
 28883       it->face_id
 28884         = face_at_string_position (it->w, face_string, face_string_pos,
 28885                                    0, &endptr, it->base_face_id, false, 0);
 28886       face = FACE_FROM_ID (it->f, it->face_id);
 28887       it->face_box_p = face->box != FACE_NO_BOX;
 28888 
 28889       /* If we have a display spec, but there's no Lisp string being
 28890          displayed, then check whether we've got one from the
 28891          :propertize being passed in and use that.  */
 28892       if (NILP (lisp_string))
 28893         {
 28894           Lisp_Object display = Fget_text_property (make_fixnum (0), Qdisplay,
 28895                                                     face_string);
 28896           if (!NILP (display))
 28897             {
 28898               Lisp_Object min_width = plist_get (display, Qmin_width);
 28899               if (!NILP (min_width))
 28900                 display_min_width (it, 0, face_string, min_width);
 28901             }
 28902         }
 28903     }
 28904 
 28905   /* Set max_x to the maximum allowed X position.  Don't let it go
 28906      beyond the right edge of the window.  */
 28907   if (max_x <= 0)
 28908     max_x = it->last_visible_x;
 28909   else
 28910     max_x = min (max_x, it->last_visible_x);
 28911 
 28912   /* Skip over display elements that are not visible because IT->w is
 28913      hscrolled.  */
 28914   if (it->current_x < it->first_visible_x)
 28915     move_it_in_display_line_to (it, 100000, it->first_visible_x,
 28916                                 MOVE_TO_POS | MOVE_TO_X);
 28917 
 28918   row->ascent = it->max_ascent;
 28919   row->height = it->max_ascent + it->max_descent;
 28920   row->phys_ascent = it->max_phys_ascent;
 28921   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 28922   row->extra_line_spacing = it->max_extra_line_spacing;
 28923 
 28924   if (STRINGP (it->string))
 28925     it_charpos = IT_STRING_CHARPOS (*it);
 28926   else
 28927     it_charpos = IT_CHARPOS (*it);
 28928 
 28929   /* This condition is for the case that we are called with current_x
 28930      past last_visible_x.  */
 28931   while (it->current_x < max_x)
 28932     {
 28933       int x_before, x, n_glyphs_before, i, nglyphs;
 28934 
 28935       /* Get the next display element.  */
 28936       if (!get_next_display_element (it))
 28937         break;
 28938 
 28939       /* Produce glyphs.  */
 28940       x_before = it->current_x;
 28941       n_glyphs_before = row->used[TEXT_AREA];
 28942       PRODUCE_GLYPHS (it);
 28943 
 28944       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 28945       i = 0;
 28946       x = x_before;
 28947       while (i < nglyphs)
 28948         {
 28949           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 28950 
 28951           if (it->line_wrap != TRUNCATE
 28952               && x + glyph->pixel_width > max_x)
 28953             {
 28954               /* End of continued line or max_x reached.  */
 28955               if (CHAR_GLYPH_PADDING_P (*glyph))
 28956                 {
 28957                   /* A wide character is unbreakable.  */
 28958                   if (row->reversed_p)
 28959                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28960                                       - n_glyphs_before);
 28961                   row->used[TEXT_AREA] = n_glyphs_before;
 28962                   it->current_x = x_before;
 28963                 }
 28964               else
 28965                 {
 28966                   if (row->reversed_p)
 28967                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28968                                       - (n_glyphs_before + i));
 28969                   row->used[TEXT_AREA] = n_glyphs_before + i;
 28970                   it->current_x = x;
 28971                 }
 28972               break;
 28973             }
 28974           else if (x + glyph->pixel_width >= it->first_visible_x)
 28975             {
 28976               /* Glyph is at least partially visible.  */
 28977               ++it->hpos;
 28978               if (x < it->first_visible_x)
 28979                 row->x = x - it->first_visible_x;
 28980             }
 28981           else
 28982             {
 28983               /* Glyph is off the left margin of the display area.
 28984                  Should not happen.  */
 28985               emacs_abort ();
 28986             }
 28987 
 28988           row->ascent = max (row->ascent, it->max_ascent);
 28989           row->height = max (row->height, it->max_ascent + it->max_descent);
 28990           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 28991           row->phys_height = max (row->phys_height,
 28992                                   it->max_phys_ascent + it->max_phys_descent);
 28993           row->extra_line_spacing = max (row->extra_line_spacing,
 28994                                          it->max_extra_line_spacing);
 28995           x += glyph->pixel_width;
 28996           ++i;
 28997         }
 28998 
 28999       /* Stop if max_x reached.  */
 29000       if (i < nglyphs)
 29001         break;
 29002 
 29003       /* Stop at line ends.  */
 29004       if (ITERATOR_AT_END_OF_LINE_P (it))
 29005         {
 29006           it->continuation_lines_width = 0;
 29007           break;
 29008         }
 29009 
 29010       set_iterator_to_next (it, true);
 29011       if (STRINGP (it->string))
 29012         it_charpos = IT_STRING_CHARPOS (*it);
 29013       else
 29014         it_charpos = IT_CHARPOS (*it);
 29015 
 29016       /* Stop if truncating at the right edge.  */
 29017       if (it->line_wrap == TRUNCATE
 29018           && it->current_x >= it->last_visible_x)
 29019         {
 29020           /* Add truncation mark, but don't do it if the line is
 29021              truncated at a padding space.  */
 29022           /* Need to do the below for the last string character as
 29023              well, since it could be a double-width character, in
 29024              which case the previous character ends before
 29025              last_visible_x.  Thus, comparison with <=, not <.  */
 29026           if (it_charpos <= it->string_nchars)
 29027             {
 29028               if (!FRAME_WINDOW_P (it->f))
 29029                 {
 29030                   int ii, n;
 29031 
 29032                   if (it->current_x > it->last_visible_x)
 29033                     {
 29034                       /* This flag is true if we are displaying mode
 29035                          line, false for header-line or tab-line.  */
 29036                       bool mode_line_p = false;
 29037 
 29038                       /* ROW->mode_line_p is true if we display mode
 29039                          line or header-line or tab-line.  */
 29040                       if (row->mode_line_p)
 29041                         {
 29042                           struct window *w = it->w;
 29043                           if (row == MATRIX_MODE_LINE_ROW (w->desired_matrix))
 29044                             mode_line_p = true;
 29045                         }
 29046                       if (!row->reversed_p)
 29047                         {
 29048                           for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
 29049                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 29050                               break;
 29051                         }
 29052                       else
 29053                         {
 29054                           for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
 29055                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 29056                               break;
 29057                           unproduce_glyphs (it, ii + 1);
 29058                           ii = row->used[TEXT_AREA] - (ii + 1);
 29059                         }
 29060                       for (n = row->used[TEXT_AREA]; ii < n; ++ii)
 29061                         {
 29062                           row->used[TEXT_AREA] = ii;
 29063                           if (row->mode_line_p)
 29064                             pad_mode_line (it, mode_line_p);
 29065                           else
 29066                             produce_special_glyphs (it, IT_TRUNCATION);
 29067                         }
 29068                     }
 29069                   produce_special_glyphs (it, IT_TRUNCATION);
 29070                 }
 29071               row->truncated_on_right_p = true;
 29072             }
 29073           break;
 29074         }
 29075     }
 29076 
 29077   /* Maybe insert a truncation at the left.  */
 29078   if (it->first_visible_x
 29079       && it_charpos > 0)
 29080     {
 29081       if (!FRAME_WINDOW_P (it->f)
 29082           || (row->reversed_p
 29083               ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29084               : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 29085         insert_left_trunc_glyphs (it);
 29086       row->truncated_on_left_p = true;
 29087     }
 29088 
 29089   it->face_id = saved_face_id;
 29090 
 29091   /* Value is number of columns displayed.  */
 29092   return it->hpos - hpos_at_start;
 29093 }
 29094 
 29095 
 29096 
 29097 /* This is like a combination of memq and assq.  Return 1/2 if PROPVAL
 29098    appears as an element of LIST or as the car of an element of LIST.
 29099    If PROPVAL is a list, compare each element against LIST in that
 29100    way, and return 1/2 if any element of PROPVAL is found in LIST.
 29101    Otherwise return 0.  This function cannot quit.
 29102    The return value is 2 if the text is invisible but with an ellipsis
 29103    and 1 if it's invisible and without an ellipsis.  */
 29104 
 29105 int
 29106 invisible_prop (Lisp_Object propval, Lisp_Object list)
 29107 {
 29108   Lisp_Object tail, proptail;
 29109 
 29110   for (tail = list; CONSP (tail); tail = XCDR (tail))
 29111     {
 29112       register Lisp_Object tem;
 29113       tem = XCAR (tail);
 29114       if (EQ (propval, tem))
 29115         return 1;
 29116       if (CONSP (tem) && EQ (propval, XCAR (tem)))
 29117         return NILP (XCDR (tem)) ? 1 : 2;
 29118     }
 29119 
 29120   if (CONSP (propval))
 29121     {
 29122       for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
 29123         {
 29124           Lisp_Object propelt;
 29125           propelt = XCAR (proptail);
 29126           for (tail = list; CONSP (tail); tail = XCDR (tail))
 29127             {
 29128               register Lisp_Object tem;
 29129               tem = XCAR (tail);
 29130               if (EQ (propelt, tem))
 29131                 return 1;
 29132               if (CONSP (tem) && EQ (propelt, XCAR (tem)))
 29133                 return NILP (XCDR (tem)) ? 1 : 2;
 29134             }
 29135         }
 29136     }
 29137 
 29138   return 0;
 29139 }
 29140 
 29141 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
 29142        doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
 29143 POS should be a marker or a buffer position; the value of the `invisible'
 29144 property at that position in the current buffer is examined.
 29145 POS can also be the actual value of the `invisible' text or overlay
 29146 property of the text of interest, in which case the value itself is
 29147 examined.
 29148 
 29149 The non-nil value returned can be t for currently invisible text that is
 29150 entirely hidden on display, or some other non-nil, non-t value if the
 29151 text is replaced by an ellipsis.
 29152 
 29153 Note that whether text with `invisible' property is actually hidden on
 29154 display may depend on `buffer-invisibility-spec', which see.  */)
 29155   (Lisp_Object pos)
 29156 {
 29157   Lisp_Object prop
 29158     = (FIXNATP (pos) || MARKERP (pos)
 29159        ? Fget_char_property (pos, Qinvisible, Qnil)
 29160        : pos);
 29161   int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
 29162   return (invis == 0 ? Qnil
 29163           : invis == 1 ? Qt
 29164           : make_fixnum (invis));
 29165 }
 29166 
 29167 /* Calculate a width or height in pixels from a specification using
 29168    the following elements:
 29169 
 29170    SPEC ::=
 29171      NUM      - a (fractional) multiple of the default font width/height
 29172      (NUM)    - specifies exactly NUM pixels
 29173      UNIT     - a fixed number of pixels, see below.
 29174      ELEMENT  - size of a display element in pixels, see below.
 29175      (NUM . SPEC) - equals NUM * SPEC
 29176      (+ SPEC SPEC ...)  - add pixel values
 29177      (- SPEC SPEC ...)  - subtract pixel values
 29178      (- SPEC)           - negate pixel value
 29179 
 29180    NUM ::=
 29181      INT or FLOAT   - a number constant
 29182      SYMBOL         - use symbol's (buffer local) variable binding.
 29183 
 29184    UNIT ::=
 29185      in       - pixels per inch  *)
 29186      mm       - pixels per 1/1000 meter  *)
 29187      cm       - pixels per 1/100 meter   *)
 29188      width    - width of current font in pixels.
 29189      height   - height of current font in pixels.
 29190 
 29191      *) using the ratio(s) defined in display-pixels-per-inch.
 29192 
 29193    ELEMENT ::=
 29194 
 29195      left-fringe          - left fringe width in pixels
 29196      right-fringe         - right fringe width in pixels
 29197 
 29198      left-margin          - left margin width in pixels
 29199      right-margin         - right margin width in pixels
 29200 
 29201      scroll-bar           - scroll-bar area width in pixels
 29202 
 29203    Examples:
 29204 
 29205    Pixels corresponding to 5 inches:
 29206      (5 . in)
 29207 
 29208    Total width of non-text areas on left side of window (if scroll-bar is on left):
 29209      '(space :width (+ left-fringe left-margin scroll-bar))
 29210 
 29211    Align to first text column (in header line):
 29212      '(space :align-to 0)
 29213 
 29214    Align to middle of text area minus half the width of variable `my-image'
 29215    containing a loaded image:
 29216      '(space :align-to (0.5 . (- text my-image)))
 29217 
 29218    Width of left margin minus width of 1 character in the default font:
 29219      '(space :width (- left-margin 1))
 29220 
 29221    Width of left margin minus width of 2 characters in the current font:
 29222      '(space :width (- left-margin (2 . width)))
 29223 
 29224    Center 1 character over left-margin (in header line):
 29225      '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
 29226 
 29227    Different ways to express width of left fringe plus left margin minus one pixel:
 29228      '(space :width (- (+ left-fringe left-margin) (1)))
 29229      '(space :width (+ left-fringe left-margin (- (1))))
 29230      '(space :width (+ left-fringe left-margin (-1)))
 29231 
 29232    If ALIGN_TO is NULL, returns the result in *RES.  If ALIGN_TO is
 29233    non-NULL, the value of *ALIGN_TO is a window-relative pixel
 29234    coordinate, and *RES is the additional pixel width from that point
 29235    till the end of the stretch glyph.
 29236 
 29237    WIDTH_P non-zero means take the width dimension or X coordinate of
 29238    the object specified by PROP, WIDTH_P zero means take the height
 29239    dimension or the Y coordinate.  (Therefore, if ALIGN_TO is
 29240    non-NULL, WIDTH_P should be non-zero.)
 29241 
 29242    FONT is the font of the face of the surrounding text.
 29243 
 29244    The return value is non-zero if width or height were successfully
 29245    calculated, i.e. if PROP is a valid spec.  */
 29246 
 29247 static bool
 29248 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 29249                             struct font *font, bool width_p, int *align_to)
 29250 {
 29251   /* Don't adjust for line number if we didn't yet produce it for this
 29252      screen line.  This is for when this function is called from
 29253      move_it_in_display_line_to that was called by display_line to get
 29254      past the glyphs hscrolled off the left side of the window.  */
 29255   int lnum_pixel_width = it->line_number_produced_p ? it->lnum_pixel_width : 0;
 29256   double pixels;
 29257 
 29258 # define OK_PIXELS(val) (*res = (val), true)
 29259 # define OK_ALIGN_TO(val) (*align_to = (val), true)
 29260 
 29261   if (NILP (prop))
 29262     return OK_PIXELS (0);
 29263 
 29264   eassert (FRAME_LIVE_P (it->f));
 29265 
 29266   if (SYMBOLP (prop))
 29267     {
 29268       if (SCHARS (SYMBOL_NAME (prop)) == 2)
 29269         {
 29270           char *unit = SSDATA (SYMBOL_NAME (prop));
 29271 
 29272           /* The UNIT expression, e.g. as part of (NUM . UNIT).  */
 29273           if (unit[0] == 'i' && unit[1] == 'n')
 29274             pixels = 1.0;
 29275           else if (unit[0] == 'm' && unit[1] == 'm')
 29276             pixels = 25.4;
 29277           else if (unit[0] == 'c' && unit[1] == 'm')
 29278             pixels = 2.54;
 29279           else
 29280             pixels = 0;
 29281           if (pixels > 0)
 29282             {
 29283               double ppi = (width_p ? FRAME_RES_X (it->f)
 29284                             : FRAME_RES_Y (it->f));
 29285 
 29286               if (ppi > 0)
 29287                 return OK_PIXELS (ppi / pixels);
 29288               return false;
 29289             }
 29290         }
 29291 
 29292 #ifdef HAVE_WINDOW_SYSTEM
 29293       /* 'height': the height of FONT.  */
 29294       if (EQ (prop, Qheight))
 29295         return OK_PIXELS (font
 29296                           ? normal_char_height (font, -1)
 29297                           : FRAME_LINE_HEIGHT (it->f));
 29298       /* 'width': the width of FONT.  */
 29299       if (EQ (prop, Qwidth))
 29300         return OK_PIXELS (font
 29301                           ? (font->average_width
 29302                              ? font->average_width
 29303                              : font->space_width)
 29304                           : FRAME_COLUMN_WIDTH (it->f));
 29305 #else
 29306       if (EQ (prop, Qheight) || EQ (prop, Qwidth))
 29307         return OK_PIXELS (1);
 29308 #endif
 29309 
 29310       /* 'text': the width or height of the text area.  */
 29311       if (EQ (prop, Qtext))
 29312           return OK_PIXELS (width_p
 29313                             ? (window_box_width (it->w, TEXT_AREA)
 29314                                - lnum_pixel_width)
 29315                             : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
 29316 
 29317       /* ':align_to'.  First time we compute the value, window
 29318          elements are interpreted as the position of the element's
 29319          left edge.  */
 29320       if (align_to && *align_to < 0)
 29321         {
 29322           *res = 0;
 29323           /* 'left': left edge of the text area.  */
 29324           if (EQ (prop, Qleft))
 29325             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29326                                 + lnum_pixel_width);
 29327           /* 'right': right edge of the text area.  */
 29328           if (EQ (prop, Qright))
 29329             return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
 29330           /* 'center': the center of the text area.  */
 29331           if (EQ (prop, Qcenter))
 29332             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29333                                 + lnum_pixel_width
 29334                                 + window_box_width (it->w, TEXT_AREA) / 2);
 29335           /* 'left-fringe': left edge of the left fringe.  */
 29336           if (EQ (prop, Qleft_fringe))
 29337             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29338                                 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
 29339                                 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
 29340           /* 'right-fringe': left edge of the right fringe.  */
 29341           if (EQ (prop, Qright_fringe))
 29342             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29343                                 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29344                                 : window_box_right_offset (it->w, TEXT_AREA));
 29345           /* 'left-margin': left edge of the left display margin.  */
 29346           if (EQ (prop, Qleft_margin))
 29347             return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
 29348           /* 'right-margin': left edge of the right display margin.  */
 29349           if (EQ (prop, Qright_margin))
 29350             return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
 29351           /* 'scroll-bar': left edge of the vertical scroll bar.  */
 29352           if (EQ (prop, Qscroll_bar))
 29353             return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
 29354                                 ? 0
 29355                                 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29356                                    + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29357                                       ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29358                                       : 0)));
 29359         }
 29360       else
 29361         {
 29362           /* Otherwise, the elements stand for their width.  */
 29363           if (EQ (prop, Qleft_fringe))
 29364             return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
 29365           if (EQ (prop, Qright_fringe))
 29366             return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
 29367           if (EQ (prop, Qleft_margin))
 29368             return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
 29369           if (EQ (prop, Qright_margin))
 29370             return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
 29371           if (EQ (prop, Qscroll_bar))
 29372             return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
 29373         }
 29374 
 29375       prop = buffer_local_value (prop, it->w->contents);
 29376       if (BASE_EQ (prop, Qunbound))
 29377         prop = Qnil;
 29378     }
 29379 
 29380   if (NUMBERP (prop))
 29381     {
 29382       int base_unit = (width_p
 29383                        ? FRAME_COLUMN_WIDTH (it->f)
 29384                        : FRAME_LINE_HEIGHT (it->f));
 29385       if (width_p && align_to && *align_to < 0)
 29386         return OK_PIXELS (XFLOATINT (prop) * base_unit + lnum_pixel_width);
 29387       return OK_PIXELS (XFLOATINT (prop) * base_unit);
 29388     }
 29389 
 29390   if (CONSP (prop))
 29391     {
 29392       Lisp_Object car = XCAR (prop);
 29393       Lisp_Object cdr = XCDR (prop);
 29394 
 29395       if (SYMBOLP (car))
 29396         {
 29397 #ifdef HAVE_WINDOW_SYSTEM
 29398           /* '(image PROPS...)': width or height of the specified image.  */
 29399           if (FRAME_WINDOW_P (it->f)
 29400               && valid_image_p (prop))
 29401             {
 29402               ptrdiff_t id = lookup_image (it->f, prop, it->face_id);
 29403               struct image *img = IMAGE_FROM_ID (it->f, id);
 29404 
 29405               return OK_PIXELS (width_p ? img->width : img->height);
 29406             }
 29407           /* '(xwidget PROPS...)': dimensions of the specified xwidget.  */
 29408           if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
 29409             {
 29410               /* TODO: Don't return dummy size.  */
 29411               return OK_PIXELS (100);
 29412             }
 29413 #endif
 29414           /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
 29415              recursively calculated values.  */
 29416           if (EQ (car, Qplus) || EQ (car, Qminus))
 29417             {
 29418               bool first = true;
 29419               double px;
 29420 
 29421               pixels = 0;
 29422               while (CONSP (cdr))
 29423                 {
 29424                   if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
 29425                                                    font, width_p, align_to))
 29426                     return false;
 29427                   if (first)
 29428                     pixels = (EQ (car, Qplus) ? px : -px), first = false;
 29429                   else
 29430                     pixels += px;
 29431                   cdr = XCDR (cdr);
 29432                 }
 29433               if (EQ (car, Qminus))
 29434                 pixels = -pixels;
 29435               return OK_PIXELS (pixels);
 29436             }
 29437 
 29438           car = buffer_local_value (car, it->w->contents);
 29439           if (BASE_EQ (car, Qunbound))
 29440             car = Qnil;
 29441         }
 29442 
 29443       /* '(NUM)': absolute number of pixels.  */
 29444       if (NUMBERP (car))
 29445         {
 29446           double fact;
 29447           int offset =
 29448             width_p && align_to && *align_to < 0 ? lnum_pixel_width : 0;
 29449           pixels = XFLOATINT (car);
 29450           if (NILP (cdr))
 29451             return OK_PIXELS (pixels + offset);
 29452           if (calc_pixel_width_or_height (&fact, it, cdr,
 29453                                           font, width_p, align_to))
 29454             return OK_PIXELS (pixels * fact + offset);
 29455           return false;
 29456         }
 29457 
 29458       return false;
 29459     }
 29460 
 29461   return false;
 29462 }
 29463 
 29464 void
 29465 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
 29466 {
 29467 #ifdef HAVE_WINDOW_SYSTEM
 29468   normal_char_ascent_descent (font, -1, ascent, descent);
 29469 #else
 29470   *ascent = 1;
 29471   *descent = 0;
 29472 #endif
 29473 }
 29474 
 29475 
 29476 /***********************************************************************
 29477                              Glyph Display
 29478  ***********************************************************************/
 29479 
 29480 #ifdef HAVE_WINDOW_SYSTEM
 29481 
 29482 #ifdef GLYPH_DEBUG
 29483 
 29484 extern void dump_glyph_string (struct glyph_string *) EXTERNALLY_VISIBLE;
 29485 void
 29486 dump_glyph_string (struct glyph_string *s)
 29487 {
 29488   fputs ("glyph string\n", stderr);
 29489   fprintf (stderr, "  x, y, w, h = %d, %d, %d, %d\n",
 29490            s->x, s->y, s->width, s->height);
 29491   fprintf (stderr, "  ybase = %d\n", s->ybase);
 29492   fprintf (stderr, "  hl = %u\n", s->hl);
 29493   fprintf (stderr, "  left overhang = %d, right = %d\n",
 29494            s->left_overhang, s->right_overhang);
 29495   fprintf (stderr, "  nchars = %d\n", s->nchars);
 29496   fprintf (stderr, "  extends to end of line = %d\n",
 29497            s->extends_to_end_of_line_p);
 29498   fprintf (stderr, "  font height = %d\n", FONT_HEIGHT (s->font));
 29499   fprintf (stderr, "  bg width = %d\n", s->background_width);
 29500 }
 29501 
 29502 #endif /* GLYPH_DEBUG */
 29503 
 29504 /* Initialize glyph string S.  CHAR2B is a suitably allocated vector
 29505    of 2-byte unsigned integers for S; it can't be allocated in
 29506    init_glyph_string because it must be allocated via `alloca'.  W
 29507    is the window on which S is drawn.  ROW and AREA are the glyph row
 29508    and area within the row from which S is constructed.  START is the
 29509    index of the first glyph structure covered by S.  HL is a
 29510    face-override for drawing S.  */
 29511 
 29512 #ifdef HAVE_NTGUI
 29513 /* We set inhibit-quit here due to paranoia: get_frame_dc acquires the
 29514    critical section, and we cannot QUIT while we hold the critical
 29515    section.  If any of the code run by callers of ALLOCATE_HDC happens
 29516    to call Lisp (might be possible due to all the hooks lying around),
 29517    we must prevent it from quitting.  */
 29518 # define ALLOCATE_HDC(hdc, f)                   \
 29519   Lisp_Object prev_quit = Vinhibit_quit;        \
 29520   Vinhibit_quit = Qt;                           \
 29521   HDC hdc = get_frame_dc ((f))
 29522 # define RELEASE_HDC(hdc, f)                    \
 29523   release_frame_dc ((f), (hdc));                \
 29524   Vinhibit_quit = prev_quit
 29525 #else
 29526 # define ALLOCATE_HDC(hdc, f)
 29527 # define RELEASE_HDC(hdc, f)
 29528 #endif
 29529 
 29530 static void
 29531 init_glyph_string (struct glyph_string *s,
 29532 #ifdef HAVE_NTGUI
 29533                    HDC hdc,
 29534 #endif
 29535                    unsigned *char2b, struct window *w, struct glyph_row *row,
 29536                    enum glyph_row_area area, int start, enum draw_glyphs_face hl)
 29537 {
 29538   memset (s, 0, sizeof *s);
 29539   s->w = w;
 29540   s->f = XFRAME (w->frame);
 29541 #ifdef HAVE_NTGUI
 29542   s->hdc = hdc;
 29543 #endif
 29544   s->char2b = char2b;
 29545   s->hl = hl;
 29546   s->row = row;
 29547   s->area = area;
 29548   s->first_glyph = row->glyphs[area] + start;
 29549   s->height = row->height;
 29550   s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
 29551   s->ybase = s->y + row->ascent;
 29552 }
 29553 
 29554 
 29555 /* Append the list of glyph strings with head H and tail T to the list
 29556    with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the result.  */
 29557 
 29558 static void
 29559 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29560                            struct glyph_string *h, struct glyph_string *t)
 29561 {
 29562   if (h)
 29563     {
 29564       if (*head)
 29565         (*tail)->next = h;
 29566       else
 29567         *head = h;
 29568       h->prev = *tail;
 29569       *tail = t;
 29570     }
 29571 }
 29572 
 29573 
 29574 /* Prepend the list of glyph strings with head H and tail T to the
 29575    list with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the
 29576    result.  */
 29577 
 29578 static void
 29579 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29580                             struct glyph_string *h, struct glyph_string *t)
 29581 {
 29582   if (h)
 29583     {
 29584       if (*head)
 29585         (*head)->prev = t;
 29586       else
 29587         *tail = t;
 29588       t->next = *head;
 29589       *head = h;
 29590     }
 29591 }
 29592 
 29593 
 29594 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
 29595    Set *HEAD and *TAIL to the resulting list.  */
 29596 
 29597 static void
 29598 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
 29599                      struct glyph_string *s)
 29600 {
 29601   s->next = s->prev = NULL;
 29602   append_glyph_string_lists (head, tail, s, s);
 29603 }
 29604 
 29605 
 29606 /* Get face and two-byte form of character C in face FACE_ID on frame F.
 29607    The encoding of C is returned in *CHAR2B.  DISPLAY_P means
 29608    make sure that X resources for the face returned are allocated.
 29609    Value is a pointer to a realized face that is ready for display if
 29610    DISPLAY_P.  */
 29611 
 29612 static struct face *
 29613 get_char_face_and_encoding (struct frame *f, int c, int face_id,
 29614                             unsigned *char2b, bool display_p)
 29615 {
 29616   struct face *face = FACE_FROM_ID (f, face_id);
 29617   unsigned code = 0;
 29618 
 29619   if (face->font)
 29620     {
 29621       code = face->font->driver->encode_char (face->font, c);
 29622 
 29623       if (code == FONT_INVALID_CODE)
 29624         code = 0;
 29625     }
 29626   /* Ensure that the code is only 2 bytes wide.  */
 29627   *char2b = code & 0xFFFF;
 29628 
 29629   /* Make sure X resources of the face are allocated.  */
 29630 #ifdef HAVE_X_WINDOWS
 29631   if (display_p)
 29632 #endif
 29633     {
 29634       eassert (face != NULL);
 29635       prepare_face_for_display (f, face);
 29636     }
 29637 
 29638   return face;
 29639 }
 29640 
 29641 
 29642 /* Get face and two-byte form of character glyph GLYPH on frame F.
 29643    The encoding of GLYPH->u.ch is returned in *CHAR2B.  Value is
 29644    a pointer to a realized face that is ready for display.  */
 29645 
 29646 static struct face *
 29647 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
 29648                              unsigned *char2b)
 29649 {
 29650   struct face *face;
 29651   unsigned code = 0;
 29652 
 29653   eassert (glyph->type == CHAR_GLYPH);
 29654   face = FACE_FROM_ID (f, glyph->face_id);
 29655 
 29656   /* Make sure X resources of the face are allocated.  */
 29657   prepare_face_for_display (f, face);
 29658 
 29659   if (face->font)
 29660     {
 29661       if (CHAR_BYTE8_P (glyph->u.ch))
 29662         code = CHAR_TO_BYTE8 (glyph->u.ch);
 29663       else
 29664         code = face->font->driver->encode_char (face->font, glyph->u.ch);
 29665 
 29666       if (code == FONT_INVALID_CODE)
 29667         code = 0;
 29668     }
 29669 
 29670   /* Ensure that the code is only 2 bytes wide.  */
 29671   *char2b = code & 0xFFFF;
 29672   return face;
 29673 }
 29674 
 29675 
 29676 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
 29677    Return true iff FONT has a glyph for C.  */
 29678 
 29679 static bool
 29680 get_char_glyph_code (int c, struct font *font, unsigned *char2b)
 29681 {
 29682   unsigned code;
 29683 
 29684   if (CHAR_BYTE8_P (c))
 29685     code = CHAR_TO_BYTE8 (c);
 29686   else
 29687     code = font->driver->encode_char (font, c);
 29688 
 29689   if (code == FONT_INVALID_CODE)
 29690     return false;
 29691 
 29692   /* Ensure that the code is only 2 bytes wide.  */
 29693   *char2b = code & 0xFFFF;
 29694   return true;
 29695 }
 29696 
 29697 
 29698 /* Fill glyph string S with composition components specified by S->cmp.
 29699 
 29700    BASE_FACE is the base face of the composition.
 29701    S->cmp_from is the index of the first component for S.
 29702 
 29703    OVERLAPS non-zero means S should draw the foreground only, and use
 29704    its physical height for clipping.  See also draw_glyphs.
 29705 
 29706    Value is the index of a component not in S.  */
 29707 
 29708 static int
 29709 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
 29710                              int overlaps)
 29711 {
 29712   int i;
 29713   /* For all glyphs of this composition, starting at the offset
 29714      S->cmp_from, until we reach the end of the definition or encounter a
 29715      glyph that requires the different face, add it to S.  */
 29716   struct face *face;
 29717 
 29718   eassert (s);
 29719 
 29720   s->for_overlaps = overlaps;
 29721   s->face = NULL;
 29722   s->font = NULL;
 29723   for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
 29724     {
 29725       int c = COMPOSITION_GLYPH (s->cmp, i);
 29726 
 29727       /* TAB in a composition means display glyphs with padding space
 29728          on the left or right.  */
 29729       if (c != '\t')
 29730         {
 29731           int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
 29732                                        -1, Qnil);
 29733 
 29734           face = get_char_face_and_encoding (s->f, c, face_id,
 29735                                              s->char2b + i, true);
 29736           if (face)
 29737             {
 29738               if (! s->face)
 29739                 {
 29740                   s->face = face;
 29741                   s->font = s->face->font;
 29742                 }
 29743               else if (s->face != face)
 29744                 break;
 29745             }
 29746         }
 29747       ++s->nchars;
 29748     }
 29749   s->cmp_to = i;
 29750 
 29751   if (s->face == NULL)
 29752     {
 29753       s->face = base_face->ascii_face;
 29754       s->font = s->face->font;
 29755     }
 29756 
 29757   if (s->hl == DRAW_MOUSE_FACE
 29758       || (s->hl == DRAW_CURSOR
 29759           && MATRIX_ROW (s->w->current_matrix,
 29760                          s->w->phys_cursor.vpos)->mouse_face_p
 29761           && cursor_in_mouse_face_p (s->w)))
 29762     {
 29763       int c = COMPOSITION_GLYPH (s->cmp, 0);
 29764       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29765       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29766       if (!s->face)
 29767         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29768 
 29769       s->face = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face, c, -1, Qnil));
 29770       prepare_face_for_display (s->f, s->face);
 29771     }
 29772 
 29773   /* All glyph strings for the same composition has the same width,
 29774      i.e. the width set for the first component of the composition.  */
 29775   s->width = s->first_glyph->pixel_width;
 29776 
 29777   /* If the specified font could not be loaded, use the frame's
 29778      default font, but record the fact that we couldn't load it in
 29779      the glyph string so that we can draw rectangles for the
 29780      characters of the glyph string.  */
 29781   if (s->font == NULL)
 29782     {
 29783       s->font_not_found_p = true;
 29784       s->font = FRAME_FONT (s->f);
 29785     }
 29786 
 29787   /* Adjust base line for subscript/superscript text.  */
 29788   s->ybase += s->first_glyph->voffset;
 29789 
 29790   return s->cmp_to;
 29791 }
 29792 
 29793 static int
 29794 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
 29795                            int start, int end, int overlaps)
 29796 {
 29797   struct glyph *glyph, *last;
 29798   int voffset;
 29799   Lisp_Object lgstring;
 29800   int i;
 29801   bool glyph_not_available_p;
 29802 
 29803   s->for_overlaps = overlaps;
 29804   glyph = s->row->glyphs[s->area] + start;
 29805   last = s->row->glyphs[s->area] + end;
 29806   voffset = glyph->voffset;
 29807   glyph_not_available_p = glyph->glyph_not_available_p;
 29808   s->cmp_id = glyph->u.cmp.id;
 29809   s->cmp_from = glyph->slice.cmp.from;
 29810   s->cmp_to = glyph->slice.cmp.to + 1;
 29811   if (s->hl == DRAW_MOUSE_FACE
 29812       || (s->hl == DRAW_CURSOR
 29813           && MATRIX_ROW (s->w->current_matrix,
 29814                          s->w->phys_cursor.vpos)->mouse_face_p
 29815           && cursor_in_mouse_face_p (s->w)))
 29816     {
 29817       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29818       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29819       if (!s->face)
 29820         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29821       prepare_face_for_display (s->f, s->face);
 29822     }
 29823   else
 29824     s->face = FACE_FROM_ID (s->f, face_id);
 29825   lgstring = composition_gstring_from_id (s->cmp_id);
 29826   s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
 29827   /* The width of a composition glyph string is the sum of the
 29828      composition's glyph widths.  */
 29829   s->width = s->first_glyph->pixel_width;
 29830   glyph++;
 29831   while (glyph < last
 29832          && glyph->u.cmp.automatic
 29833          && glyph->u.cmp.id == s->cmp_id
 29834          && glyph->face_id == face_id
 29835          && s->cmp_to == glyph->slice.cmp.from
 29836          && glyph->glyph_not_available_p == glyph_not_available_p)
 29837     {
 29838       s->width += glyph->pixel_width;
 29839       s->cmp_to = (glyph++)->slice.cmp.to + 1;
 29840     }
 29841 
 29842   for (i = s->cmp_from; i < s->cmp_to; i++)
 29843     {
 29844       Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
 29845       unsigned code = LGLYPH_CODE (lglyph);
 29846 
 29847       /* Ensure that the code is only 2 bytes wide.  */
 29848       s->char2b[i] = code & 0xFFFF;
 29849     }
 29850 
 29851   /* If the specified font could not be loaded, record that fact in
 29852      S->font_not_found_p so that we can draw rectangles for the
 29853      characters of the glyph string.  */
 29854   if (glyph_not_available_p)
 29855     s->font_not_found_p = true;
 29856 
 29857   /* Adjust base line for subscript/superscript text.  */
 29858   s->ybase += voffset;
 29859 
 29860   return glyph - s->row->glyphs[s->area];
 29861 }
 29862 
 29863 
 29864 /* Fill glyph string S from a sequence glyphs for glyphless characters.
 29865    See the comment of fill_glyph_string for arguments.
 29866    Value is the index of the first glyph not in S.  */
 29867 
 29868 
 29869 static int
 29870 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
 29871                              int start, int end, int overlaps)
 29872 {
 29873   struct glyph *glyph, *last;
 29874   int voffset;
 29875 
 29876   eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
 29877   s->for_overlaps = overlaps;
 29878   glyph = s->row->glyphs[s->area] + start;
 29879   last = s->row->glyphs[s->area] + end;
 29880   voffset = glyph->voffset;
 29881   s->face = FACE_FROM_ID (s->f, face_id);
 29882   s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
 29883   if (s->hl == DRAW_MOUSE_FACE
 29884       || (s->hl == DRAW_CURSOR
 29885           && MATRIX_ROW (s->w->current_matrix,
 29886                          s->w->phys_cursor.vpos)->mouse_face_p
 29887           && cursor_in_mouse_face_p (s->w)))
 29888     {
 29889       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29890       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29891       if (!s->face)
 29892         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29893       prepare_face_for_display (s->f, s->face);
 29894     }
 29895   s->nchars = 1;
 29896   s->width = glyph->pixel_width;
 29897   glyph++;
 29898   while (glyph < last
 29899          && glyph->type == GLYPHLESS_GLYPH
 29900          && glyph->voffset == voffset
 29901          && glyph->face_id == face_id)
 29902     {
 29903       s->nchars++;
 29904       s->width += glyph->pixel_width;
 29905       glyph++;
 29906     }
 29907   s->ybase += voffset;
 29908   return glyph - s->row->glyphs[s->area];
 29909 }
 29910 
 29911 
 29912 /* Fill glyph string S from a sequence of character glyphs.
 29913 
 29914    FACE_ID is the face id of the string.  START is the index of the
 29915    first glyph to consider, END is the index of the last + 1.
 29916    OVERLAPS non-zero means S should draw the foreground only, and use
 29917    its physical height for clipping.  See also draw_glyphs.
 29918 
 29919    Value is the index of the first glyph not in S.  */
 29920 
 29921 static int
 29922 fill_glyph_string (struct glyph_string *s, int face_id,
 29923                    int start, int end, int overlaps)
 29924 {
 29925   struct glyph *glyph, *last;
 29926   int voffset;
 29927   bool glyph_not_available_p;
 29928 
 29929   eassert (s->f == XFRAME (s->w->frame));
 29930   eassert (s->nchars == 0);
 29931   eassert (start >= 0 && end > start);
 29932 
 29933   s->for_overlaps = overlaps;
 29934   glyph = s->row->glyphs[s->area] + start;
 29935   last = s->row->glyphs[s->area] + end;
 29936   voffset = glyph->voffset;
 29937   s->padding_p = glyph->padding_p;
 29938   glyph_not_available_p = glyph->glyph_not_available_p;
 29939 
 29940   while (glyph < last
 29941          && glyph->type == CHAR_GLYPH
 29942          && glyph->voffset == voffset
 29943          /* Same face id implies same font, nowadays.  */
 29944          && glyph->face_id == face_id
 29945          && glyph->glyph_not_available_p == glyph_not_available_p)
 29946     {
 29947       s->face = get_glyph_face_and_encoding (s->f, glyph,
 29948                                              s->char2b + s->nchars);
 29949       ++s->nchars;
 29950       eassert (s->nchars <= end - start);
 29951       s->width += glyph->pixel_width;
 29952       if (glyph++->padding_p != s->padding_p)
 29953         break;
 29954     }
 29955 
 29956   s->font = s->face->font;
 29957 
 29958   if (s->hl == DRAW_MOUSE_FACE
 29959       || (s->hl == DRAW_CURSOR
 29960           && MATRIX_ROW (s->w->current_matrix,
 29961                          s->w->phys_cursor.vpos)->mouse_face_p
 29962           && cursor_in_mouse_face_p (s->w)))
 29963     {
 29964       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29965       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29966       if (!s->face)
 29967         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29968       s->face
 29969         = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face,
 29970                                              s->first_glyph->u.ch, -1, Qnil));
 29971       prepare_face_for_display (s->f, s->face);
 29972     }
 29973 
 29974   /* If the specified font could not be loaded, use the frame's font,
 29975      but record the fact that we couldn't load it in
 29976      S->font_not_found_p so that we can draw rectangles for the
 29977      characters of the glyph string.  */
 29978   if (s->font == NULL || glyph_not_available_p)
 29979     {
 29980       s->font_not_found_p = true;
 29981       s->font = FRAME_FONT (s->f);
 29982     }
 29983 
 29984   /* Adjust base line for subscript/superscript text.  */
 29985   s->ybase += voffset;
 29986 
 29987   eassert (s->face && s->face->gc);
 29988   return glyph - s->row->glyphs[s->area];
 29989 }
 29990 
 29991 
 29992 /* Fill glyph string S from image glyph S->first_glyph.  */
 29993 
 29994 static void
 29995 fill_image_glyph_string (struct glyph_string *s)
 29996 {
 29997   eassert (s->first_glyph->type == IMAGE_GLYPH);
 29998   s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
 29999   eassert (s->img);
 30000   s->slice = s->first_glyph->slice.img;
 30001   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 30002   s->font = s->face->font;
 30003   if (s->hl == DRAW_MOUSE_FACE
 30004       || (s->hl == DRAW_CURSOR
 30005           && MATRIX_ROW (s->w->current_matrix,
 30006                          s->w->phys_cursor.vpos)->mouse_face_p
 30007           && cursor_in_mouse_face_p (s->w)))
 30008     {
 30009       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30010       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30011       if (!s->face)
 30012         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30013       prepare_face_for_display (s->f, s->face);
 30014     }
 30015   s->width = s->first_glyph->pixel_width;
 30016 
 30017   /* Adjust base line for subscript/superscript text.  */
 30018   s->ybase += s->first_glyph->voffset;
 30019 }
 30020 
 30021 
 30022 #ifdef HAVE_XWIDGETS
 30023 static void
 30024 fill_xwidget_glyph_string (struct glyph_string *s)
 30025 {
 30026   eassert (s->first_glyph->type == XWIDGET_GLYPH);
 30027   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 30028   s->font = s->face->font;
 30029   if (s->hl == DRAW_MOUSE_FACE
 30030       || (s->hl == DRAW_CURSOR
 30031           && MATRIX_ROW (s->w->current_matrix,
 30032                          s->w->phys_cursor.vpos)->mouse_face_p
 30033           && cursor_in_mouse_face_p (s->w)))
 30034     {
 30035       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30036       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30037       if (!s->face)
 30038         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30039       prepare_face_for_display (s->f, s->face);
 30040     }
 30041   s->width = s->first_glyph->pixel_width;
 30042   s->ybase += s->first_glyph->voffset;
 30043   s->xwidget = xwidget_from_id (s->first_glyph->u.xwidget);
 30044 }
 30045 #endif
 30046 /* Fill glyph string S from a sequence of stretch glyphs.
 30047 
 30048    START is the index of the first glyph to consider,
 30049    END is the index of the last + 1.
 30050 
 30051    Value is the index of the first glyph not in S.  */
 30052 
 30053 static int
 30054 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
 30055 {
 30056   struct glyph *glyph, *last;
 30057   int voffset, face_id;
 30058 
 30059   eassert (s->first_glyph->type == STRETCH_GLYPH);
 30060 
 30061   glyph = s->row->glyphs[s->area] + start;
 30062   last = s->row->glyphs[s->area] + end;
 30063   face_id = glyph->face_id;
 30064   s->face = FACE_FROM_ID (s->f, face_id);
 30065   s->font = s->face->font;
 30066   if (s->hl == DRAW_MOUSE_FACE
 30067       || (s->hl == DRAW_CURSOR
 30068           && MATRIX_ROW (s->w->current_matrix,
 30069                          s->w->phys_cursor.vpos)->mouse_face_p
 30070           && cursor_in_mouse_face_p (s->w)))
 30071     {
 30072       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30073       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30074       if (!s->face)
 30075         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30076       prepare_face_for_display (s->f, s->face);
 30077     }
 30078   s->width = glyph->pixel_width;
 30079   s->nchars = 1;
 30080   voffset = glyph->voffset;
 30081 
 30082   for (++glyph;
 30083        (glyph < last
 30084         && glyph->type == STRETCH_GLYPH
 30085         && glyph->voffset == voffset
 30086         && glyph->face_id == face_id);
 30087        ++glyph)
 30088     s->width += glyph->pixel_width;
 30089 
 30090   /* Adjust base line for subscript/superscript text.  */
 30091   s->ybase += voffset;
 30092 
 30093   /* The case that face->gc == 0 is handled when drawing the glyph
 30094      string by calling prepare_face_for_display.  */
 30095   eassert (s->face);
 30096   return glyph - s->row->glyphs[s->area];
 30097 }
 30098 
 30099 static struct font_metrics *
 30100 get_per_char_metric (struct font *font, const unsigned *char2b)
 30101 {
 30102   static struct font_metrics metrics;
 30103 
 30104   if (! font)
 30105     return NULL;
 30106   if (*char2b == FONT_INVALID_CODE)
 30107     return NULL;
 30108 
 30109   font->driver->text_extents (font, char2b, 1, &metrics);
 30110   return &metrics;
 30111 }
 30112 
 30113 /* A subroutine that computes "normal" values of ASCENT and DESCENT
 30114    for FONT.  Values are taken from font-global ones, except for fonts
 30115    that claim preposterously large values, but whose glyphs actually
 30116    have reasonable dimensions.  C is the character to use for metrics
 30117    if the font-global values are too large; if C is negative, the
 30118    function selects a default character.  */
 30119 static void
 30120 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
 30121 {
 30122   *ascent = FONT_BASE (font);
 30123   *descent = FONT_DESCENT (font);
 30124 
 30125   if (FONT_TOO_HIGH (font))
 30126     {
 30127       unsigned char2b;
 30128 
 30129       /* Get metrics of C, defaulting to a reasonably sized ASCII
 30130          character.  */
 30131       if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
 30132         {
 30133           struct font_metrics *pcm = get_per_char_metric (font, &char2b);
 30134           eassume (pcm);
 30135 
 30136           if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
 30137             {
 30138               /* We add 1 pixel to character dimensions as heuristics
 30139                  that produces nicer display, e.g. when the face has
 30140                  the box attribute.  */
 30141               *ascent = pcm->ascent + 1;
 30142               *descent = pcm->descent + 1;
 30143             }
 30144         }
 30145     }
 30146 }
 30147 
 30148 /* A subroutine that computes a reasonable "normal character height"
 30149    for fonts that claim preposterously large vertical dimensions, but
 30150    whose glyphs are actually reasonably sized.  C is the character
 30151    whose metrics to use for those fonts, or -1 for default
 30152    character.  */
 30153 static int
 30154 normal_char_height (struct font *font, int c)
 30155 {
 30156   int ascent, descent;
 30157 
 30158   normal_char_ascent_descent (font, c, &ascent, &descent);
 30159 
 30160   return ascent + descent;
 30161 }
 30162 
 30163 /* EXPORT for RIF:
 30164    Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
 30165    frame F.  Overhangs of glyphs other than type CHAR_GLYPH are
 30166    assumed to be zero.  */
 30167 
 30168 void
 30169 gui_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
 30170 {
 30171   *left = *right = 0;
 30172 
 30173   if (glyph->type == CHAR_GLYPH)
 30174     {
 30175       unsigned char2b;
 30176       struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
 30177       if (face->font)
 30178         {
 30179           struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
 30180           if (pcm)
 30181             {
 30182               if (pcm->rbearing > pcm->width)
 30183                 *right = pcm->rbearing - pcm->width;
 30184               if (pcm->lbearing < 0)
 30185                 *left = -pcm->lbearing;
 30186             }
 30187         }
 30188     }
 30189   else if (glyph->type == COMPOSITE_GLYPH)
 30190     {
 30191       if (! glyph->u.cmp.automatic)
 30192         {
 30193           struct composition *cmp = composition_table[glyph->u.cmp.id];
 30194 
 30195           if (cmp->rbearing > cmp->pixel_width)
 30196             *right = cmp->rbearing - cmp->pixel_width;
 30197           if (cmp->lbearing < 0)
 30198             *left = - cmp->lbearing;
 30199         }
 30200       else
 30201         {
 30202           Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
 30203           struct font_metrics metrics;
 30204 
 30205           composition_gstring_width (gstring, glyph->slice.cmp.from,
 30206                                      glyph->slice.cmp.to + 1, &metrics);
 30207           if (metrics.rbearing > metrics.width)
 30208             *right = metrics.rbearing - metrics.width;
 30209           if (metrics.lbearing < 0)
 30210             *left = - metrics.lbearing;
 30211         }
 30212     }
 30213 }
 30214 
 30215 
 30216 /* Return the index of the first glyph preceding glyph string S that
 30217    is overwritten by S because of S's left overhang.  Value is -1
 30218    if no glyphs are overwritten.  */
 30219 
 30220 static int
 30221 left_overwritten (struct glyph_string *s)
 30222 {
 30223   int k;
 30224 
 30225   if (s->left_overhang)
 30226     {
 30227       int x = 0, i;
 30228       struct glyph *glyphs = s->row->glyphs[s->area];
 30229       int first = s->first_glyph - glyphs;
 30230 
 30231       for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
 30232         x -= glyphs[i].pixel_width;
 30233 
 30234       k = i + 1;
 30235     }
 30236   else
 30237     k = -1;
 30238 
 30239   return k;
 30240 }
 30241 
 30242 
 30243 /* Return the index of the first glyph preceding glyph string S that
 30244    is overwriting S because of its right overhang.  Value is -1 if no
 30245    glyph in front of S overwrites S.  */
 30246 
 30247 static int
 30248 left_overwriting (struct glyph_string *s)
 30249 {
 30250   int i, k, x;
 30251   struct glyph *glyphs = s->row->glyphs[s->area];
 30252   int first = s->first_glyph - glyphs;
 30253 
 30254   k = -1;
 30255   x = 0;
 30256   for (i = first - 1; i >= 0; --i)
 30257     {
 30258       int left, right;
 30259       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30260       if (x + right > 0)
 30261         k = i;
 30262       x -= glyphs[i].pixel_width;
 30263     }
 30264 
 30265   return k;
 30266 }
 30267 
 30268 
 30269 /* Return the index of the last glyph following glyph string S that is
 30270    overwritten by S because of S's right overhang.  Value is -1 if
 30271    no such glyph is found.  */
 30272 
 30273 static int
 30274 right_overwritten (struct glyph_string *s)
 30275 {
 30276   int k = -1;
 30277 
 30278   if (s->right_overhang)
 30279     {
 30280       int x = 0, i;
 30281       struct glyph *glyphs = s->row->glyphs[s->area];
 30282       int first = (s->first_glyph - glyphs
 30283                    + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30284       int end = s->row->used[s->area];
 30285 
 30286       for (i = first; i < end && s->right_overhang > x; ++i)
 30287         x += glyphs[i].pixel_width;
 30288 
 30289       k = i;
 30290     }
 30291 
 30292   return k;
 30293 }
 30294 
 30295 
 30296 /* Return the index of the last glyph following glyph string S that
 30297    overwrites S because of its left overhang.  Value is negative
 30298    if no such glyph is found.  */
 30299 
 30300 static int
 30301 right_overwriting (struct glyph_string *s)
 30302 {
 30303   int i, k, x;
 30304   int end = s->row->used[s->area];
 30305   struct glyph *glyphs = s->row->glyphs[s->area];
 30306   int first = (s->first_glyph - glyphs
 30307                + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30308 
 30309   k = -1;
 30310   x = 0;
 30311   for (i = first; i < end; ++i)
 30312     {
 30313       int left, right;
 30314       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30315       if (x - left < 0)
 30316         k = i;
 30317       x += glyphs[i].pixel_width;
 30318     }
 30319 
 30320   return k;
 30321 }
 30322 
 30323 
 30324 /* Set background width of glyph string S.  START is the index of the
 30325    first glyph following S.  LAST_X is the right-most x-position + 1
 30326    in the drawing area.
 30327 
 30328    If S->hl is DRAW_CURSOR, S->f is a window system frame, and the
 30329    cursor in S's window is currently inside mouse face, also update
 30330    S->width to take into account potentially differing :box
 30331    properties between the original face and the mouse face.  */
 30332 
 30333 static void
 30334 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
 30335 {
 30336   /* If the face of this glyph string has to be drawn to the end of
 30337      the drawing area, set S->extends_to_end_of_line_p.  */
 30338 
 30339   if (start == s->row->used[s->area]
 30340       && ((s->row->fill_line_p
 30341            && (s->hl == DRAW_NORMAL_TEXT
 30342                || s->hl == DRAW_IMAGE_RAISED
 30343                || s->hl == DRAW_IMAGE_SUNKEN))
 30344           || s->hl == DRAW_MOUSE_FACE))
 30345     s->extends_to_end_of_line_p = true;
 30346 
 30347   /* If S extends its face to the end of the line, set its
 30348      background_width to the distance to the right edge of the drawing
 30349      area.  */
 30350   if (s->extends_to_end_of_line_p)
 30351     s->background_width = last_x - s->x + 1;
 30352   else
 30353     {
 30354       s->background_width = s->width;
 30355 #ifdef HAVE_WINDOW_SYSTEM
 30356       if (FRAME_WINDOW_P (s->f)
 30357           && s->hl == DRAW_CURSOR
 30358           && MATRIX_ROW (s->w->current_matrix,
 30359                          s->w->phys_cursor.vpos)->mouse_face_p
 30360           && cursor_in_mouse_face_p (s->w))
 30361         {
 30362           /* Adjust the background width of the glyph string, because
 30363              if the glyph's face has the :box attribute, its
 30364              pixel_width might be different when it's displayed in the
 30365              mouse-face, if that also has the :box attribute.  */
 30366           struct glyph *g = s->first_glyph;
 30367           struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
 30368           s->background_width +=
 30369             adjust_glyph_width_for_mouse_face (g, s->row, s->w,
 30370                                                regular_face, s->face);
 30371           /* S->width is probably worth adjusting here as well.  */
 30372           s->width = s->background_width;
 30373         }
 30374 #endif
 30375     }
 30376 }
 30377 
 30378 
 30379 /* Return glyph string that shares background with glyph string S and
 30380    whose `background_width' member has been set.  */
 30381 
 30382 static struct glyph_string *
 30383 glyph_string_containing_background_width (struct glyph_string *s)
 30384 {
 30385   if (s->cmp)
 30386     while (s->cmp_from)
 30387       s = s->prev;
 30388 
 30389   return s;
 30390 }
 30391 
 30392 
 30393 /* Compute overhangs and x-positions for glyph string S and its
 30394    predecessors, or successors.  X is the starting x-position for S.
 30395    BACKWARD_P means process predecessors.  */
 30396 
 30397 static void
 30398 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
 30399 {
 30400   if (backward_p)
 30401     {
 30402       while (s)
 30403         {
 30404           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30405             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30406           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30407             x -= s->width;
 30408           s->x = x;
 30409           s = s->prev;
 30410         }
 30411     }
 30412   else
 30413     {
 30414       while (s)
 30415         {
 30416           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30417             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30418           s->x = x;
 30419           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30420             x += s->width;
 30421           s = s->next;
 30422         }
 30423     }
 30424 }
 30425 
 30426 
 30427 
 30428 /* The following macros are only called from draw_glyphs below.
 30429    They reference the following parameters of that function directly:
 30430      `w', `row', `area', and `overlap_p'
 30431    as well as the following local variables:
 30432      `s', `f', and `hdc' (in W32)  */
 30433 
 30434 #ifdef HAVE_NTGUI
 30435 /* On W32, silently add local `hdc' variable to argument list of
 30436    init_glyph_string.  */
 30437 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30438   init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
 30439 #else
 30440 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30441   init_glyph_string (s, char2b, w, row, area, start, hl)
 30442 #endif
 30443 
 30444 /* Add a glyph string for a stretch glyph to the list of strings
 30445    between HEAD and TAIL.  START is the index of the stretch glyph in
 30446    row area AREA of glyph row ROW.  END is the index of the last glyph
 30447    in that glyph row area.  X is the current output position assigned
 30448    to the new glyph string constructed.  HL overrides that face of the
 30449    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30450    is the right-most x-position of the drawing area.  */
 30451 
 30452 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
 30453    and below -- keep them on one line.  */
 30454 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30455      do                                                                     \
 30456        {                                                                    \
 30457          s = alloca (sizeof *s);                                            \
 30458          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);              \
 30459          START = fill_stretch_glyph_string (s, START, END);                 \
 30460          append_glyph_string (&HEAD, &TAIL, s);                             \
 30461          s->x = (X);                                                        \
 30462        }                                                                    \
 30463      while (false)
 30464 
 30465 
 30466 /* Add a glyph string for an image glyph to the list of strings
 30467    between HEAD and TAIL.  START is the index of the image glyph in
 30468    row area AREA of glyph row ROW.  END is the index of the last glyph
 30469    in that glyph row area.  X is the current output position assigned
 30470    to the new glyph string constructed.  HL overrides that face of the
 30471    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30472    is the right-most x-position of the drawing area.  */
 30473 
 30474 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30475      do                                                                 \
 30476        {                                                                \
 30477          s = alloca (sizeof *s);                                        \
 30478          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30479          fill_image_glyph_string (s);                                   \
 30480          append_glyph_string (&HEAD, &TAIL, s);                         \
 30481          ++START;                                                       \
 30482          s->x = (X);                                                    \
 30483        }                                                                \
 30484      while (false)
 30485 
 30486 #ifndef HAVE_XWIDGETS
 30487 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30488      eassume (false)
 30489 #else
 30490 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30491      do                                                                 \
 30492        {                                                                \
 30493          s = alloca (sizeof *s);                                        \
 30494          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30495          fill_xwidget_glyph_string (s);                                 \
 30496          append_glyph_string (&(HEAD), &(TAIL), s);                     \
 30497          ++(START);                                                     \
 30498          s->x = (X);                                                    \
 30499        }                                                                \
 30500      while (false)
 30501 #endif
 30502 
 30503 /* Add a glyph string for a sequence of character glyphs to the list
 30504    of strings between HEAD and TAIL.  START is the index of the first
 30505    glyph in row area AREA of glyph row ROW that is part of the new
 30506    glyph string.  END is the index of the last glyph in that glyph row
 30507    area.  X is the current output position assigned to the new glyph
 30508    string constructed.  HL overrides that face of the glyph; e.g. it
 30509    is DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the
 30510    right-most x-position of the drawing area.  */
 30511 
 30512 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30513      do                                                                    \
 30514        {                                                                   \
 30515          int face_id;                                                      \
 30516          unsigned *char2b;                                         \
 30517                                                                            \
 30518          face_id = (row)->glyphs[area][START].face_id;                     \
 30519                                                                            \
 30520          s = alloca (sizeof *s);                                           \
 30521          SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
 30522          INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
 30523          append_glyph_string (&HEAD, &TAIL, s);                            \
 30524          s->x = (X);                                                       \
 30525          START = fill_glyph_string (s, face_id, START, END, overlaps);     \
 30526        }                                                                   \
 30527      while (false)
 30528 
 30529 
 30530 /* Add a glyph string for a composite sequence to the list of strings
 30531    between HEAD and TAIL.  START is the index of the first glyph in
 30532    row area AREA of glyph row ROW that is part of the new glyph
 30533    string.  END is the index of the last glyph in that glyph row area.
 30534    X is the current output position assigned to the new glyph string
 30535    constructed.  HL overrides that face of the glyph; e.g. it is
 30536    DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the right-most
 30537    x-position of the drawing area.  */
 30538 
 30539 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30540   do {                                                                      \
 30541     int face_id = (row)->glyphs[area][START].face_id;                       \
 30542     struct face *base_face = FACE_FROM_ID (f, face_id);             \
 30543     ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id;                 \
 30544     struct composition *cmp = composition_table[cmp_id];                    \
 30545     unsigned *char2b;                                                       \
 30546     struct glyph_string *first_s = NULL;                                    \
 30547     int n;                                                                  \
 30548                                                                             \
 30549     SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                               \
 30550                                                                             \
 30551     /* Make glyph_strings for each glyph sequence that is drawable by       \
 30552        the same face, and append them to HEAD/TAIL.  */                     \
 30553     for (n = 0; n < cmp->glyph_len;)                                        \
 30554       {                                                                     \
 30555         s = alloca (sizeof *s);                                             \
 30556         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);             \
 30557         append_glyph_string (&(HEAD), &(TAIL), s);                          \
 30558         s->cmp = cmp;                                                       \
 30559         s->cmp_from = n;                                                    \
 30560         s->x = (X);                                                         \
 30561         if (n == 0)                                                         \
 30562           first_s = s;                                                      \
 30563         n = fill_composite_glyph_string (s, base_face, overlaps);           \
 30564       }                                                                     \
 30565                                                                             \
 30566     ++START;                                                                \
 30567     s = first_s;                                                            \
 30568   } while (false)
 30569 
 30570 
 30571 /* Add a glyph string for a glyph-string sequence to the list of strings
 30572    between HEAD and TAIL.  */
 30573 
 30574 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30575   do {                                                                    \
 30576     int face_id;                                                          \
 30577     unsigned *char2b;                                                     \
 30578     Lisp_Object gstring;                                                  \
 30579                                                                           \
 30580     face_id = (row)->glyphs[area][START].face_id;                         \
 30581     gstring = (composition_gstring_from_id                                \
 30582                ((row)->glyphs[area][START].u.cmp.id));                    \
 30583     s = alloca (sizeof *s);                                               \
 30584     SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));               \
 30585     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);               \
 30586     append_glyph_string (&(HEAD), &(TAIL), s);                            \
 30587     s->x = (X);                                                           \
 30588     START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
 30589   } while (false)
 30590 
 30591 
 30592 /* Add a glyph string for a sequence of glyphless character's glyphs
 30593    to the list of strings between HEAD and TAIL.  The meanings of
 30594    arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS.  */
 30595 
 30596 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30597   do                                                                        \
 30598     {                                                                       \
 30599       int face_id;                                                          \
 30600                                                                             \
 30601       face_id = (row)->glyphs[area][START].face_id;                         \
 30602                                                                             \
 30603       s = alloca (sizeof *s);                                               \
 30604       INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);                 \
 30605       append_glyph_string (&HEAD, &TAIL, s);                                \
 30606       s->x = (X);                                                           \
 30607       START = fill_glyphless_glyph_string (s, face_id, START, END,          \
 30608                                            overlaps);                       \
 30609     }                                                                       \
 30610   while (false)
 30611 
 30612 
 30613 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
 30614    of AREA of glyph row ROW on window W between indices START and END.
 30615    HL overrides the face for drawing glyph strings, e.g. it is
 30616    DRAW_CURSOR to draw a cursor.  X and LAST_X are start and end
 30617    x-positions of the drawing area.
 30618 
 30619    This is an ugly monster macro construct because we must use alloca
 30620    to allocate glyph strings (because draw_glyphs can be called
 30621    asynchronously).  */
 30622 
 30623 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30624   do                                                                    \
 30625     {                                                                   \
 30626       HEAD = TAIL = NULL;                                               \
 30627       while (START < END)                                               \
 30628         {                                                               \
 30629           struct glyph *first_glyph = (row)->glyphs[area] + START;      \
 30630           switch (first_glyph->type)                                    \
 30631             {                                                           \
 30632             case CHAR_GLYPH:                                            \
 30633               BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL,         \
 30634                                         HL, X, LAST_X);                 \
 30635               break;                                                    \
 30636                                                                         \
 30637             case COMPOSITE_GLYPH:                                       \
 30638               if (first_glyph->u.cmp.automatic)                         \
 30639                 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30640                                             HL, X, LAST_X);             \
 30641               else                                                      \
 30642                 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL,   \
 30643                                               HL, X, LAST_X);           \
 30644               break;                                                    \
 30645                                                                         \
 30646             case STRETCH_GLYPH:                                         \
 30647               BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30648                                           HL, X, LAST_X);               \
 30649               break;                                                    \
 30650                                                                         \
 30651             case IMAGE_GLYPH:                                           \
 30652               BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
 30653                                         HL, X, LAST_X);                 \
 30654               break;
 30655 
 30656 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30657             case XWIDGET_GLYPH:                                         \
 30658               BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30659                                           HL, X, LAST_X);               \
 30660               break;
 30661 
 30662 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30663             case GLYPHLESS_GLYPH:                                       \
 30664               BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30665                                             HL, X, LAST_X);             \
 30666               break;                                                    \
 30667                                                                         \
 30668             default:                                                    \
 30669               emacs_abort ();                                           \
 30670             }                                                           \
 30671                                                                         \
 30672           if (s)                                                        \
 30673             {                                                           \
 30674               set_glyph_string_background_width (s, START, LAST_X);     \
 30675               (X) += s->width;                                          \
 30676             }                                                           \
 30677         }                                                               \
 30678     } while (false)
 30679 
 30680 
 30681 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)      \
 30682     BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)        \
 30683     BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)       \
 30684     BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
 30685 
 30686 
 30687 /* Draw glyphs between START and END in AREA of ROW on window W,
 30688    starting at x-position X.  X is relative to AREA in W.  HL is a
 30689    face-override with the following meaning:
 30690 
 30691    DRAW_NORMAL_TEXT     draw normally
 30692    DRAW_CURSOR          draw in cursor face
 30693    DRAW_MOUSE_FACE      draw in mouse face.
 30694    DRAW_INVERSE_VIDEO   draw in mode line face
 30695    DRAW_IMAGE_SUNKEN    draw an image with a sunken relief around it
 30696    DRAW_IMAGE_RAISED    draw an image with a raised relief around it
 30697 
 30698    If OVERLAPS is non-zero, draw only the foreground of characters and
 30699    clip to the physical height of ROW.  Non-zero value also defines
 30700    the overlapping part to be drawn:
 30701 
 30702    OVERLAPS_PRED                overlap with preceding rows
 30703    OVERLAPS_SUCC                overlap with succeeding rows
 30704    OVERLAPS_BOTH                overlap with both preceding/succeeding rows
 30705    OVERLAPS_ERASED_CURSOR       overlap with erased cursor area
 30706 
 30707    Value is the x-position reached, relative to AREA of W.  */
 30708 
 30709 static int
 30710 draw_glyphs (struct window *w, int x, struct glyph_row *row,
 30711              enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
 30712              enum draw_glyphs_face hl, int overlaps)
 30713 {
 30714   struct glyph_string *head, *tail;
 30715   struct glyph_string *s;
 30716   struct glyph_string *clip_head = NULL, *clip_tail = NULL;
 30717   int i, j, x_reached, last_x, area_left = 0;
 30718   struct frame *f = XFRAME (WINDOW_FRAME (w));
 30719 
 30720   ALLOCATE_HDC (hdc, f);
 30721 
 30722   /* Let's rather be paranoid than getting a SEGV.  */
 30723   end = min (end, row->used[area]);
 30724   start = clip_to_bounds (0, start, end);
 30725 
 30726   /* Translate X to frame coordinates.  Set last_x to the right
 30727      end of the drawing area.  */
 30728   if (row->full_width_p)
 30729     {
 30730       /* X is relative to the left edge of W, without scroll bars
 30731          or fringes.  */
 30732       area_left = WINDOW_LEFT_EDGE_X (w);
 30733       last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
 30734                 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 30735     }
 30736   else
 30737     {
 30738       area_left = window_box_left (w, area);
 30739       last_x = area_left + window_box_width (w, area);
 30740     }
 30741   x += area_left;
 30742 
 30743   /* Build a doubly-linked list of glyph_string structures between
 30744      head and tail from what we have to draw.  Note that the macro
 30745      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
 30746      the reason we use a separate variable `i'.  */
 30747   i = start;
 30748   USE_SAFE_ALLOCA;
 30749   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
 30750   if (tail)
 30751     {
 30752       s = glyph_string_containing_background_width (tail);
 30753       x_reached = s->x + s->background_width;
 30754     }
 30755   else
 30756     x_reached = x;
 30757 
 30758   /* If there are any glyphs with lbearing < 0 or rbearing > width in
 30759      the row, redraw some glyphs in front or following the glyph
 30760      strings built above.  */
 30761   if (head && !overlaps && row->contains_overlapping_glyphs_p)
 30762     {
 30763       struct glyph_string *h, *t;
 30764       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 30765       int mouse_beg_col UNINIT, mouse_end_col UNINIT;
 30766       bool check_mouse_face = false;
 30767       int dummy_x = 0;
 30768 
 30769       /* If mouse highlighting is on, we may need to draw adjacent
 30770          glyphs using mouse-face highlighting.  */
 30771       if (area == TEXT_AREA && row->mouse_face_p
 30772           && hlinfo->mouse_face_beg_row >= 0
 30773           && hlinfo->mouse_face_end_row >= 0)
 30774         {
 30775           ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
 30776 
 30777           if (row_vpos >= hlinfo->mouse_face_beg_row
 30778               && row_vpos <= hlinfo->mouse_face_end_row)
 30779             {
 30780               check_mouse_face = true;
 30781               mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
 30782                 ? hlinfo->mouse_face_beg_col : 0;
 30783               mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
 30784                 ? hlinfo->mouse_face_end_col
 30785                 : row->used[TEXT_AREA];
 30786             }
 30787         }
 30788 
 30789       /* Compute overhangs for all glyph strings.  */
 30790       if (FRAME_RIF (f)->compute_glyph_string_overhangs)
 30791         for (s = head; s; s = s->next)
 30792           FRAME_RIF (f)->compute_glyph_string_overhangs (s);
 30793 
 30794       /* Prepend glyph strings for glyphs in front of the first glyph
 30795          string that are overwritten because of the first glyph
 30796          string's left overhang.  The background of all strings
 30797          prepended must be drawn because the first glyph string
 30798          draws over it.  */
 30799       i = left_overwritten (head);
 30800       if (i >= 0)
 30801         {
 30802           enum draw_glyphs_face overlap_hl;
 30803 
 30804           /* If this row contains mouse highlighting, attempt to draw
 30805              the overlapped glyphs with the correct highlight.  This
 30806              code fails if the overlap encompasses more than one glyph
 30807              and mouse-highlight spans only some of these glyphs.
 30808              However, making it work perfectly involves a lot more
 30809              code, and I don't know if the pathological case occurs in
 30810              practice, so we'll stick to this for now.  --- cyd  */
 30811           if (check_mouse_face
 30812               && mouse_beg_col < start && mouse_end_col > i)
 30813             overlap_hl = DRAW_MOUSE_FACE;
 30814           else
 30815             overlap_hl = DRAW_NORMAL_TEXT;
 30816 
 30817           if (hl != overlap_hl)
 30818             clip_head = head;
 30819           j = i;
 30820           BUILD_GLYPH_STRINGS (j, start, h, t,
 30821                                overlap_hl, dummy_x, last_x);
 30822           start = i;
 30823           compute_overhangs_and_x (t, head->x, true);
 30824           prepend_glyph_string_lists (&head, &tail, h, t);
 30825           if (clip_head == NULL)
 30826             clip_head = head;
 30827         }
 30828 
 30829       /* Prepend glyph strings for glyphs in front of the first glyph
 30830          string that overwrite that glyph string because of their
 30831          right overhang.  For these strings, only the foreground must
 30832          be drawn, because it draws over the glyph string at `head'.
 30833          The background must not be drawn because this would overwrite
 30834          right overhangs of preceding glyphs for which no glyph
 30835          strings exist.  */
 30836       i = left_overwriting (head);
 30837       if (i >= 0)
 30838         {
 30839           enum draw_glyphs_face overlap_hl;
 30840 
 30841           if (check_mouse_face
 30842               && mouse_beg_col < start && mouse_end_col > i)
 30843             overlap_hl = DRAW_MOUSE_FACE;
 30844           else
 30845             overlap_hl = DRAW_NORMAL_TEXT;
 30846 
 30847           if (hl == overlap_hl || clip_head == NULL)
 30848             clip_head = head;
 30849           BUILD_GLYPH_STRINGS (i, start, h, t,
 30850                                overlap_hl, dummy_x, last_x);
 30851           for (s = h; s; s = s->next)
 30852             s->background_filled_p = true;
 30853           compute_overhangs_and_x (t, head->x, true);
 30854           prepend_glyph_string_lists (&head, &tail, h, t);
 30855         }
 30856 
 30857       /* Append glyphs strings for glyphs following the last glyph
 30858          string tail that are overwritten by tail.  The background of
 30859          these strings has to be drawn because tail's foreground draws
 30860          over it.  */
 30861       i = right_overwritten (tail);
 30862       if (i >= 0)
 30863         {
 30864           enum draw_glyphs_face overlap_hl;
 30865 
 30866           if (check_mouse_face
 30867               && mouse_beg_col < i && mouse_end_col > end)
 30868             overlap_hl = DRAW_MOUSE_FACE;
 30869           else
 30870             overlap_hl = DRAW_NORMAL_TEXT;
 30871 
 30872           if (hl != overlap_hl)
 30873             clip_tail = tail;
 30874           BUILD_GLYPH_STRINGS (end, i, h, t,
 30875                                overlap_hl, x, last_x);
 30876           /* Because BUILD_GLYPH_STRINGS updates the first argument,
 30877              we don't have `end = i;' here.  */
 30878           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30879           append_glyph_string_lists (&head, &tail, h, t);
 30880           if (clip_tail == NULL)
 30881             clip_tail = tail;
 30882         }
 30883 
 30884       /* Append glyph strings for glyphs following the last glyph
 30885          string tail that overwrite tail.  The foreground of such
 30886          glyphs has to be drawn because it writes into the background
 30887          of tail.  The background must not be drawn because it could
 30888          paint over the foreground of following glyphs.  */
 30889       i = right_overwriting (tail);
 30890       if (i >= 0)
 30891         {
 30892           enum draw_glyphs_face overlap_hl;
 30893           if (check_mouse_face
 30894               && mouse_beg_col < i && mouse_end_col > end)
 30895             overlap_hl = DRAW_MOUSE_FACE;
 30896           else
 30897             overlap_hl = DRAW_NORMAL_TEXT;
 30898 
 30899           if (hl == overlap_hl || clip_tail == NULL)
 30900             clip_tail = tail;
 30901           i++;                  /* We must include the Ith glyph.  */
 30902           BUILD_GLYPH_STRINGS (end, i, h, t,
 30903                                overlap_hl, x, last_x);
 30904           for (s = h; s; s = s->next)
 30905             s->background_filled_p = true;
 30906           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30907           append_glyph_string_lists (&head, &tail, h, t);
 30908         }
 30909       tail = glyph_string_containing_background_width (tail);
 30910       if (clip_tail)
 30911         clip_tail = glyph_string_containing_background_width (clip_tail);
 30912       if (clip_head || clip_tail)
 30913         for (s = head; s; s = s->next)
 30914           {
 30915             s->clip_head = clip_head;
 30916             s->clip_tail = clip_tail;
 30917           }
 30918     }
 30919 
 30920   /* Draw all strings.  */
 30921   for (s = head; s; s = s->next)
 30922     FRAME_RIF (f)->draw_glyph_string (s);
 30923 
 30924   /* When focus a sole frame and move horizontally, this clears on_p
 30925      causing a failure to erase prev cursor position. */
 30926   if (area == TEXT_AREA
 30927       && !row->full_width_p
 30928       /* When drawing overlapping rows, only the glyph strings'
 30929          foreground is drawn, which doesn't erase a cursor
 30930          completely. */
 30931       && !overlaps)
 30932     {
 30933       int x0 = clip_head ? clip_head->x : (head ? head->x : x);
 30934       int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
 30935                 : (tail ? tail->x + tail->background_width : x));
 30936       x0 -= area_left;
 30937       x1 -= area_left;
 30938 
 30939       notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
 30940                                  row->y, MATRIX_ROW_BOTTOM_Y (row));
 30941     }
 30942 
 30943   /* Value is the x-position up to which drawn, relative to AREA of W.
 30944      This doesn't include parts drawn because of overhangs.  */
 30945   if (row->full_width_p)
 30946     x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
 30947   else
 30948     x_reached -= area_left;
 30949 
 30950   RELEASE_HDC (hdc, f);
 30951 
 30952   SAFE_FREE ();
 30953   return x_reached;
 30954 }
 30955 
 30956 /* Find the first glyph in the run of underlined glyphs preceding the
 30957    beginning of glyph string S, and return its font (which could be
 30958    NULL).  This is needed because that font determines the underline
 30959    position and thickness for the entire run of the underlined glyphs.
 30960    This function is called from the draw_glyph_string method of GUI
 30961    frame's redisplay interface (RIF) when it needs to draw in an
 30962    underlined face.  */
 30963 struct font *
 30964 font_for_underline_metrics (struct glyph_string *s)
 30965 {
 30966   struct glyph *g0 = s->row->glyphs[s->area], *g;
 30967 
 30968   for (g = s->first_glyph - 1; g >= g0; g--)
 30969     {
 30970       struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
 30971       if (!(prev_face && prev_face->underline != FACE_NO_UNDERLINE))
 30972         break;
 30973     }
 30974 
 30975   /* If preceding glyphs are not underlined, use the font of S.  */
 30976   if (g == s->first_glyph - 1)
 30977     return s->font;
 30978   else
 30979     {
 30980       /* Otherwise use the font of the last glyph we saw in the above
 30981          loop whose face had the underline_p flag set.  */
 30982       return FACE_FROM_ID (s->f, g[1].face_id)->font;
 30983     }
 30984 }
 30985 
 30986 /* Expand row matrix if too narrow.  Don't expand if area
 30987    is not present.  */
 30988 
 30989 #define IT_EXPAND_MATRIX_WIDTH(it, area)                \
 30990   {                                                     \
 30991     if (!it->f->fonts_changed                           \
 30992         && (it->glyph_row->glyphs[area]                 \
 30993             < it->glyph_row->glyphs[area + 1]))         \
 30994       {                                                 \
 30995         it->w->ncols_scale_factor++;                    \
 30996         it->f->fonts_changed = true;                    \
 30997       }                                                 \
 30998   }
 30999 
 31000 /* Store one glyph for IT->char_to_display in IT->glyph_row.
 31001    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 31002 
 31003 static void
 31004 append_glyph (struct it *it)
 31005 {
 31006   struct glyph *glyph;
 31007   enum glyph_row_area area = it->area;
 31008 
 31009   eassert (it->glyph_row);
 31010   eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
 31011 
 31012   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31013   if (glyph < it->glyph_row->glyphs[area + 1])
 31014     {
 31015       /* If the glyph row is reversed, we need to prepend the glyph
 31016          rather than append it.  */
 31017       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31018         {
 31019           struct glyph *g;
 31020 
 31021           /* Make room for the additional glyph.  */
 31022           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31023             g[1] = *g;
 31024           glyph = it->glyph_row->glyphs[area];
 31025         }
 31026       glyph->charpos = CHARPOS (it->position);
 31027       glyph->object = it->object;
 31028       if (it->pixel_width > 0)
 31029         {
 31030           eassert (it->pixel_width <= SHRT_MAX);
 31031           glyph->pixel_width = it->pixel_width;
 31032           glyph->padding_p = false;
 31033         }
 31034       else
 31035         {
 31036           /* Assure at least 1-pixel width.  Otherwise, cursor can't
 31037              be displayed correctly.  */
 31038           glyph->pixel_width = 1;
 31039           glyph->padding_p = true;
 31040         }
 31041       glyph->ascent = it->ascent;
 31042       glyph->descent = it->descent;
 31043       glyph->voffset = it->voffset;
 31044       glyph->type = CHAR_GLYPH;
 31045       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31046       glyph->multibyte_p = it->multibyte_p;
 31047       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31048         {
 31049           /* In R2L rows, the left and the right box edges need to be
 31050              drawn in reverse direction.  */
 31051           glyph->right_box_line_p = it->start_of_box_run_p;
 31052           glyph->left_box_line_p = it->end_of_box_run_p;
 31053         }
 31054       else
 31055         {
 31056           glyph->left_box_line_p = it->start_of_box_run_p;
 31057           glyph->right_box_line_p = it->end_of_box_run_p;
 31058         }
 31059       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31060                                       || it->phys_descent > it->descent);
 31061       glyph->glyph_not_available_p = it->glyph_not_available_p;
 31062       glyph->face_id = it->face_id;
 31063       glyph->u.ch = it->char_to_display;
 31064       glyph->slice.img = null_glyph_slice;
 31065       glyph->font_type = FONT_TYPE_UNKNOWN;
 31066       if (it->bidi_p)
 31067         {
 31068           glyph->resolved_level = it->bidi_it.resolved_level;
 31069           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31070           glyph->bidi_type = it->bidi_it.type;
 31071         }
 31072       else
 31073         {
 31074           glyph->resolved_level = 0;
 31075           glyph->bidi_type = UNKNOWN_BT;
 31076         }
 31077       ++it->glyph_row->used[area];
 31078     }
 31079   else
 31080     IT_EXPAND_MATRIX_WIDTH (it, area);
 31081 }
 31082 
 31083 /* Store one glyph for the composition IT->cmp_it.id in IT->glyph_row.
 31084    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 31085 
 31086 static void
 31087 append_composite_glyph (struct it *it)
 31088 {
 31089   struct glyph *glyph;
 31090   enum glyph_row_area area = it->area;
 31091 
 31092   eassert (it->glyph_row);
 31093 
 31094   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31095   if (glyph < it->glyph_row->glyphs[area + 1])
 31096     {
 31097       /* If the glyph row is reversed, we need to prepend the glyph
 31098          rather than append it.  */
 31099       if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
 31100         {
 31101           struct glyph *g;
 31102 
 31103           /* Make room for the new glyph.  */
 31104           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31105             g[1] = *g;
 31106           glyph = it->glyph_row->glyphs[it->area];
 31107         }
 31108       glyph->charpos = it->cmp_it.charpos;
 31109       glyph->object = it->object;
 31110       eassert (it->pixel_width <= SHRT_MAX);
 31111       glyph->pixel_width = it->pixel_width;
 31112       glyph->ascent = it->ascent;
 31113       glyph->descent = it->descent;
 31114       glyph->voffset = it->voffset;
 31115       glyph->type = COMPOSITE_GLYPH;
 31116       if (it->cmp_it.ch < 0)
 31117         {
 31118           glyph->u.cmp.automatic = false;
 31119           glyph->u.cmp.id = it->cmp_it.id;
 31120           glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
 31121         }
 31122       else
 31123         {
 31124           glyph->u.cmp.automatic = true;
 31125           glyph->u.cmp.id = it->cmp_it.id;
 31126           glyph->slice.cmp.from = it->cmp_it.from;
 31127           glyph->slice.cmp.to = it->cmp_it.to - 1;
 31128         }
 31129       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31130       glyph->multibyte_p = it->multibyte_p;
 31131       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31132         {
 31133           /* In R2L rows, the left and the right box edges need to be
 31134              drawn in reverse direction.  */
 31135           glyph->right_box_line_p = it->start_of_box_run_p;
 31136           glyph->left_box_line_p = it->end_of_box_run_p;
 31137         }
 31138       else
 31139         {
 31140           glyph->left_box_line_p = it->start_of_box_run_p;
 31141           glyph->right_box_line_p = it->end_of_box_run_p;
 31142         }
 31143       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31144                                       || it->phys_descent > it->descent);
 31145       glyph->padding_p = false;
 31146       glyph->glyph_not_available_p = it->glyph_not_available_p;
 31147       glyph->face_id = it->face_id;
 31148       glyph->font_type = FONT_TYPE_UNKNOWN;
 31149       if (it->bidi_p)
 31150         {
 31151           glyph->resolved_level = it->bidi_it.resolved_level;
 31152           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31153           glyph->bidi_type = it->bidi_it.type;
 31154         }
 31155       ++it->glyph_row->used[area];
 31156     }
 31157   else
 31158     IT_EXPAND_MATRIX_WIDTH (it, area);
 31159 }
 31160 
 31161 
 31162 /* Change IT->ascent and IT->height according to the setting of
 31163    IT->voffset.  */
 31164 
 31165 static void
 31166 take_vertical_position_into_account (struct it *it)
 31167 {
 31168   if (it->voffset)
 31169     {
 31170       if (it->voffset < 0)
 31171         /* Increase the ascent so that we can display the text higher
 31172            in the line.  */
 31173         it->ascent -= it->voffset;
 31174       else
 31175         /* Increase the descent so that we can display the text lower
 31176            in the line.  */
 31177         it->descent += it->voffset;
 31178     }
 31179 }
 31180 
 31181 
 31182 /* Produce glyphs/get display metrics for the image IT is loaded with.
 31183    See the description of struct display_iterator in dispextern.h for
 31184    an overview of struct display_iterator.  */
 31185 
 31186 static void
 31187 produce_image_glyph (struct it *it)
 31188 {
 31189   struct image *img;
 31190   struct face *face;
 31191   int glyph_ascent, crop;
 31192   struct glyph_slice slice;
 31193 
 31194   eassert (it->what == IT_IMAGE);
 31195 
 31196   face = FACE_FROM_ID (it->f, it->face_id);
 31197   /* Make sure X resources of the face is loaded.  */
 31198   prepare_face_for_display (it->f, face);
 31199 
 31200   if (it->image_id < 0)
 31201     {
 31202       /* Fringe bitmap.  */
 31203       it->ascent = it->phys_ascent = 0;
 31204       it->descent = it->phys_descent = 0;
 31205       it->pixel_width = 0;
 31206       it->nglyphs = 0;
 31207       return;
 31208     }
 31209 
 31210   img = IMAGE_FROM_ID (it->f, it->image_id);
 31211   /* Make sure X resources of the image is loaded.  */
 31212   prepare_image_for_display (it->f, img);
 31213 
 31214   slice.x = slice.y = 0;
 31215   slice.width = img->width;
 31216   slice.height = img->height;
 31217 
 31218   if (FIXNUMP (it->slice.x))
 31219     slice.x = XFIXNUM (it->slice.x);
 31220   else if (FLOATP (it->slice.x))
 31221     slice.x = XFLOAT_DATA (it->slice.x) * img->width;
 31222 
 31223   if (FIXNUMP (it->slice.y))
 31224     slice.y = XFIXNUM (it->slice.y);
 31225   else if (FLOATP (it->slice.y))
 31226     slice.y = XFLOAT_DATA (it->slice.y) * img->height;
 31227 
 31228   if (FIXNUMP (it->slice.width))
 31229     slice.width = XFIXNUM (it->slice.width);
 31230   else if (FLOATP (it->slice.width))
 31231     slice.width = XFLOAT_DATA (it->slice.width) * img->width;
 31232 
 31233   if (FIXNUMP (it->slice.height))
 31234     slice.height = XFIXNUM (it->slice.height);
 31235   else if (FLOATP (it->slice.height))
 31236     slice.height = XFLOAT_DATA (it->slice.height) * img->height;
 31237 
 31238   if (slice.x >= img->width)
 31239     slice.x = img->width;
 31240   if (slice.y >= img->height)
 31241     slice.y = img->height;
 31242   if (slice.x + slice.width >= img->width)
 31243     slice.width = img->width - slice.x;
 31244   if (slice.y + slice.height > img->height)
 31245     slice.height = img->height - slice.y;
 31246 
 31247   if (slice.width == 0 || slice.height == 0)
 31248     return;
 31249 
 31250   it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
 31251 
 31252   it->descent = slice.height - glyph_ascent;
 31253   if (slice.y == 0)
 31254     it->descent += img->vmargin;
 31255   if (slice.y + slice.height == img->height)
 31256     it->descent += img->vmargin;
 31257   it->phys_descent = it->descent;
 31258 
 31259   it->pixel_width = slice.width;
 31260   if (slice.x == 0)
 31261     it->pixel_width += img->hmargin;
 31262   if (slice.x + slice.width == img->width)
 31263     it->pixel_width += img->hmargin;
 31264 
 31265   /* It's quite possible for images to have an ascent greater than
 31266      their height, so don't get confused in that case.  */
 31267   if (it->descent < 0)
 31268     it->descent = 0;
 31269 
 31270   it->nglyphs = 1;
 31271 
 31272   if (face->box != FACE_NO_BOX)
 31273     {
 31274       /* If you change the logic here, please change it in
 31275          get_cursor_offset_for_mouse_face as well. */
 31276       if (face->box_horizontal_line_width > 0)
 31277         {
 31278           if (slice.y == 0)
 31279             it->ascent += face->box_horizontal_line_width;
 31280           if (slice.y + slice.height == img->height)
 31281             it->descent += face->box_horizontal_line_width;
 31282         }
 31283 
 31284       if (face->box_vertical_line_width > 0)
 31285         {
 31286           if (it->start_of_box_run_p && slice.x == 0)
 31287             it->pixel_width += face->box_vertical_line_width;
 31288           if (it->end_of_box_run_p && slice.x + slice.width == img->width)
 31289             it->pixel_width += face->box_vertical_line_width;
 31290         }
 31291     }
 31292 
 31293   take_vertical_position_into_account (it);
 31294 
 31295   /* Automatically crop wide image glyphs at right edge so we can
 31296      draw the cursor on same display row.  */
 31297   if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
 31298       && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31299     {
 31300       it->pixel_width -= crop;
 31301       slice.width -= crop;
 31302     }
 31303 
 31304   if (it->glyph_row)
 31305     {
 31306       struct glyph *glyph;
 31307       enum glyph_row_area area = it->area;
 31308 
 31309       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31310       if (it->glyph_row->reversed_p)
 31311         {
 31312           struct glyph *g;
 31313 
 31314           /* Make room for the new glyph.  */
 31315           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31316             g[1] = *g;
 31317           glyph = it->glyph_row->glyphs[it->area];
 31318         }
 31319       if (glyph < it->glyph_row->glyphs[area + 1])
 31320         {
 31321           glyph->charpos = CHARPOS (it->position);
 31322           glyph->object = it->object;
 31323           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31324           glyph->ascent = glyph_ascent;
 31325           glyph->descent = it->descent;
 31326           glyph->voffset = it->voffset;
 31327           glyph->type = IMAGE_GLYPH;
 31328           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31329           glyph->multibyte_p = it->multibyte_p;
 31330           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31331             {
 31332               /* In R2L rows, the left and the right box edges need to be
 31333                  drawn in reverse direction.  */
 31334               glyph->right_box_line_p = it->start_of_box_run_p;
 31335               glyph->left_box_line_p = it->end_of_box_run_p;
 31336             }
 31337           else
 31338             {
 31339               glyph->left_box_line_p = it->start_of_box_run_p;
 31340               glyph->right_box_line_p = it->end_of_box_run_p;
 31341             }
 31342           glyph->overlaps_vertically_p = false;
 31343           glyph->padding_p = false;
 31344           glyph->glyph_not_available_p = false;
 31345           glyph->face_id = it->face_id;
 31346           glyph->u.img_id = img->id;
 31347           glyph->slice.img = slice;
 31348           glyph->font_type = FONT_TYPE_UNKNOWN;
 31349           if (it->bidi_p)
 31350             {
 31351               glyph->resolved_level = it->bidi_it.resolved_level;
 31352               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31353               glyph->bidi_type = it->bidi_it.type;
 31354             }
 31355           ++it->glyph_row->used[area];
 31356         }
 31357       else
 31358         IT_EXPAND_MATRIX_WIDTH (it, area);
 31359     }
 31360 }
 31361 
 31362 static void
 31363 produce_xwidget_glyph (struct it *it)
 31364 {
 31365 #ifdef HAVE_XWIDGETS
 31366   struct xwidget *xw;
 31367   int glyph_ascent, crop;
 31368   eassert (it->what == IT_XWIDGET);
 31369 
 31370   struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31371   /* Make sure X resources of the face is loaded.  */
 31372   prepare_face_for_display (it->f, face);
 31373 
 31374   xw = it->xwidget;
 31375   it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
 31376   it->descent = xw->height/2;
 31377   it->phys_descent = it->descent;
 31378   it->pixel_width = xw->width;
 31379   /* It's quite possible for images to have an ascent greater than
 31380      their height, so don't get confused in that case.  */
 31381   if (it->descent < 0)
 31382     it->descent = 0;
 31383 
 31384   it->nglyphs = 1;
 31385 
 31386   if (face->box != FACE_NO_BOX)
 31387     {
 31388       if (face->box_horizontal_line_width > 0)
 31389         {
 31390           it->ascent += face->box_horizontal_line_width;
 31391           it->descent += face->box_horizontal_line_width;
 31392         }
 31393 
 31394       if (face->box_vertical_line_width > 0)
 31395         {
 31396           if (it->start_of_box_run_p)
 31397             it->pixel_width += face->box_vertical_line_width;
 31398           it->pixel_width += face->box_vertical_line_width;
 31399         }
 31400     }
 31401 
 31402   take_vertical_position_into_account (it);
 31403 
 31404   /* Automatically crop wide image glyphs at right edge so we can
 31405      draw the cursor on same display row.  */
 31406   crop = it->pixel_width - (it->last_visible_x - it->current_x);
 31407   if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31408     it->pixel_width -= crop;
 31409 
 31410   if (it->glyph_row)
 31411     {
 31412       enum glyph_row_area area = it->area;
 31413       struct glyph *glyph
 31414         = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31415 
 31416       if (it->glyph_row->reversed_p)
 31417         {
 31418           struct glyph *g;
 31419 
 31420           /* Make room for the new glyph.  */
 31421           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31422             g[1] = *g;
 31423           glyph = it->glyph_row->glyphs[it->area];
 31424         }
 31425       if (glyph < it->glyph_row->glyphs[area + 1])
 31426         {
 31427           glyph->charpos = CHARPOS (it->position);
 31428           glyph->object = it->object;
 31429           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31430           glyph->ascent = glyph_ascent;
 31431           glyph->descent = it->descent;
 31432           glyph->voffset = it->voffset;
 31433           glyph->type = XWIDGET_GLYPH;
 31434           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31435           glyph->multibyte_p = it->multibyte_p;
 31436           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31437             {
 31438               /* In R2L rows, the left and the right box edges need to be
 31439                  drawn in reverse direction.  */
 31440               glyph->right_box_line_p = it->start_of_box_run_p;
 31441               glyph->left_box_line_p = it->end_of_box_run_p;
 31442             }
 31443           else
 31444             {
 31445               glyph->left_box_line_p = it->start_of_box_run_p;
 31446               glyph->right_box_line_p = it->end_of_box_run_p;
 31447             }
 31448           glyph->overlaps_vertically_p = 0;
 31449           glyph->padding_p = 0;
 31450           glyph->glyph_not_available_p = 0;
 31451           glyph->face_id = it->face_id;
 31452           glyph->u.xwidget = it->xwidget->xwidget_id;
 31453           glyph->font_type = FONT_TYPE_UNKNOWN;
 31454           if (it->bidi_p)
 31455             {
 31456               glyph->resolved_level = it->bidi_it.resolved_level;
 31457               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31458               glyph->bidi_type = it->bidi_it.type;
 31459             }
 31460           ++it->glyph_row->used[area];
 31461         }
 31462       else
 31463         IT_EXPAND_MATRIX_WIDTH (it, area);
 31464     }
 31465 #endif
 31466 }
 31467 
 31468 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
 31469    of the glyph, WIDTH and HEIGHT are the width and height of the
 31470    stretch.  ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT).  */
 31471 
 31472 static void
 31473 append_stretch_glyph (struct it *it, Lisp_Object object,
 31474                       int width, int height, int ascent)
 31475 {
 31476   struct glyph *glyph;
 31477   enum glyph_row_area area = it->area;
 31478 
 31479   eassert (ascent >= 0 && ascent <= height);
 31480 
 31481   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31482   if (glyph < it->glyph_row->glyphs[area + 1])
 31483     {
 31484       /* If the glyph row is reversed, we need to prepend the glyph
 31485          rather than append it.  */
 31486       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31487         {
 31488           struct glyph *g;
 31489 
 31490           /* Make room for the additional glyph.  */
 31491           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31492             g[1] = *g;
 31493           glyph = it->glyph_row->glyphs[area];
 31494 
 31495           /* Decrease the width of the first glyph of the row that
 31496              begins before first_visible_x (e.g., due to hscroll).
 31497              This is so the overall width of the row becomes smaller
 31498              by the scroll amount, and the stretch glyph appended by
 31499              extend_face_to_end_of_line will be wider, to shift the
 31500              row glyphs to the right.  (In L2R rows, the corresponding
 31501              left-shift effect is accomplished by setting row->x to a
 31502              negative value, which won't work with R2L rows.)
 31503 
 31504              This must leave us with a positive value of WIDTH, since
 31505              otherwise the call to move_it_in_display_line_to at the
 31506              beginning of display_line would have got past the entire
 31507              first glyph, and then it->current_x would have been
 31508              greater or equal to it->first_visible_x.  */
 31509           if (it->current_x < it->first_visible_x)
 31510             width -= it->first_visible_x - it->current_x;
 31511           eassert (width > 0);
 31512         }
 31513       glyph->charpos = CHARPOS (it->position);
 31514       glyph->object = object;
 31515       /* FIXME: It would be better to use TYPE_MAX here, but
 31516          __typeof__ is not portable enough...  */
 31517       glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
 31518       glyph->ascent = ascent;
 31519       glyph->descent = height - ascent;
 31520       glyph->voffset = it->voffset;
 31521       glyph->type = STRETCH_GLYPH;
 31522       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31523       glyph->multibyte_p = it->multibyte_p;
 31524       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31525         {
 31526           /* In R2L rows, the left and the right box edges need to be
 31527              drawn in reverse direction.  */
 31528           glyph->right_box_line_p = it->start_of_box_run_p;
 31529           glyph->left_box_line_p = it->end_of_box_run_p;
 31530         }
 31531       else
 31532         {
 31533           glyph->left_box_line_p = it->start_of_box_run_p;
 31534           glyph->right_box_line_p = it->end_of_box_run_p;
 31535         }
 31536       glyph->overlaps_vertically_p = false;
 31537       glyph->padding_p = false;
 31538       glyph->glyph_not_available_p = false;
 31539       glyph->face_id = it->face_id;
 31540       glyph->u.stretch.ascent = ascent;
 31541       glyph->u.stretch.height = height;
 31542       glyph->slice.img = null_glyph_slice;
 31543       glyph->font_type = FONT_TYPE_UNKNOWN;
 31544       if (it->bidi_p)
 31545         {
 31546           glyph->resolved_level = it->bidi_it.resolved_level;
 31547           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31548           glyph->bidi_type = it->bidi_it.type;
 31549         }
 31550       else
 31551         {
 31552           glyph->resolved_level = 0;
 31553           glyph->bidi_type = UNKNOWN_BT;
 31554         }
 31555       ++it->glyph_row->used[area];
 31556     }
 31557   else
 31558     IT_EXPAND_MATRIX_WIDTH (it, area);
 31559 }
 31560 
 31561 #endif  /* HAVE_WINDOW_SYSTEM */
 31562 
 31563 /* Produce a stretch glyph for iterator IT.  IT->object is the value
 31564    of the display property.  The value must be a list of the form
 31565    `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
 31566    being recognized:
 31567 
 31568    1. `:width WIDTH' specifies that the space should be WIDTH *
 31569    canonical char width wide.  WIDTH may be an integer or floating
 31570    point number.
 31571 
 31572    2. `:relative-width FACTOR' specifies that the width of the stretch
 31573    should be computed from the width of the first character having the
 31574    `display' property, and should be FACTOR times that width.
 31575 
 31576    3. `:align-to HPOS' specifies that the space should be wide enough
 31577    to reach HPOS, a value in canonical character units.
 31578 
 31579    Exactly one of the above pairs must be present.
 31580 
 31581    4. `:height HEIGHT' specifies that the height of the stretch produced
 31582    should be HEIGHT, measured in canonical character units.
 31583 
 31584    5. `:relative-height FACTOR' specifies that the height of the
 31585    stretch should be FACTOR times the height of the characters having
 31586    the display property.
 31587 
 31588    Either none or exactly one of 4 or 5 must be present.
 31589 
 31590    6. `:ascent ASCENT'  specifies that ASCENT percent of the height
 31591    of the stretch should be used for the ascent of the stretch.
 31592    ASCENT must be in the range 0 <= ASCENT <= 100.  */
 31593 
 31594 void
 31595 produce_stretch_glyph (struct it *it)
 31596 {
 31597   /* (space :width WIDTH :height HEIGHT ...)  */
 31598   Lisp_Object prop, plist;
 31599   int width = 0, height = 0, align_to = -1;
 31600   bool zero_width_ok_p = false;
 31601   double tem;
 31602   struct font *font = NULL;
 31603 
 31604 #ifdef HAVE_WINDOW_SYSTEM
 31605   int ascent = 0;
 31606   bool zero_height_ok_p = false;
 31607   struct face *face = NULL;     /* shut up GCC's -Wmaybe-uninitialized */
 31608 
 31609   if (FRAME_WINDOW_P (it->f))
 31610     {
 31611       face = FACE_FROM_ID (it->f, it->face_id);
 31612       font = face->font ? face->font : FRAME_FONT (it->f);
 31613       prepare_face_for_display (it->f, face);
 31614     }
 31615 #endif
 31616 
 31617   /* List should start with `space'.  */
 31618   eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
 31619   plist = XCDR (it->object);
 31620 
 31621   /* Compute the width of the stretch.  */
 31622   if ((prop = plist_get (plist, QCwidth), !NILP (prop))
 31623       && calc_pixel_width_or_height (&tem, it, prop, font, true, NULL))
 31624     {
 31625       /* Absolute width `:width WIDTH' specified and valid.  */
 31626       zero_width_ok_p = true;
 31627       width = (int)tem;
 31628     }
 31629   else if (prop = plist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
 31630     {
 31631       /* Relative width `:relative-width FACTOR' specified and valid.
 31632          Compute the width of the characters having this `display'
 31633          property.  */
 31634       struct it it2;
 31635       Lisp_Object object =
 31636         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31637       unsigned char *p = (STRINGP (object)
 31638                           ? SDATA (object) + IT_STRING_BYTEPOS (*it)
 31639                           : BYTE_POS_ADDR (IT_BYTEPOS (*it)));
 31640       bool multibyte_p =
 31641         STRINGP (object) ? STRING_MULTIBYTE (object) : it->multibyte_p;
 31642 
 31643       it2 = *it;
 31644       if (multibyte_p)
 31645         {
 31646           it2.c = it2.char_to_display = string_char_and_length (p, &it2.len);
 31647 #ifdef HAVE_WINDOW_SYSTEM
 31648           if (FRAME_WINDOW_P (it->f) && ! ASCII_CHAR_P (it2.c))
 31649             it2.face_id = FACE_FOR_CHAR (it->f, face, it2.c,
 31650                                          IT_CHARPOS (*it),
 31651                                          STRINGP (object)? object : Qnil);
 31652 #endif
 31653         }
 31654       else
 31655         {
 31656           it2.c = it2.char_to_display = *p, it2.len = 1;
 31657           if (! ASCII_CHAR_P (it2.c))
 31658             it2.char_to_display = BYTE8_TO_CHAR (it2.c);
 31659         }
 31660 
 31661       it2.glyph_row = NULL;
 31662       it2.what = IT_CHARACTER;
 31663       PRODUCE_GLYPHS (&it2);
 31664       width = NUMVAL (prop) * it2.pixel_width;
 31665     }
 31666   else if ((prop = plist_get (plist, QCalign_to), !NILP (prop))
 31667            && calc_pixel_width_or_height (&tem, it, prop, font, true,
 31668                                           &align_to))
 31669     {
 31670       int x = it->current_x + it->continuation_lines_width;
 31671       int x0 = x;
 31672       /* Adjust for line numbers, if needed.   */
 31673       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31674         {
 31675           x -= it->lnum_pixel_width;
 31676           /* Restore the original width, if required.  */
 31677           if (x + it->stretch_adjust >= it->first_visible_x)
 31678             x += it->stretch_adjust;
 31679         }
 31680 
 31681       if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
 31682         align_to = (align_to < 0
 31683                     ? 0
 31684                     : align_to - window_box_left_offset (it->w, TEXT_AREA));
 31685       else if (align_to < 0)
 31686         align_to = window_box_left_offset (it->w, TEXT_AREA);
 31687       width = max (0, (int)tem + align_to - x);
 31688 
 31689       int next_x = x + width;
 31690       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31691         {
 31692           /* If the line is hscrolled, and the stretch starts before
 31693              the first visible pixel, simulate negative row->x.  */
 31694           if (x < it->first_visible_x)
 31695             {
 31696               next_x -= it->first_visible_x - x;
 31697               it->stretch_adjust = it->first_visible_x - x;
 31698             }
 31699           else
 31700             next_x -= it->stretch_adjust;
 31701         }
 31702       width = next_x - x0;
 31703       zero_width_ok_p = true;
 31704     }
 31705   else
 31706     /* Nothing specified -> width defaults to canonical char width.  */
 31707     width = FRAME_COLUMN_WIDTH (it->f);
 31708 
 31709   if (width <= 0 && (width < 0 || !zero_width_ok_p))
 31710     width = 1;
 31711 
 31712 #ifdef HAVE_WINDOW_SYSTEM
 31713   /* Compute height.  */
 31714   if (FRAME_WINDOW_P (it->f))
 31715     {
 31716       int default_height = normal_char_height (font, ' ');
 31717 
 31718       if ((prop = plist_get (plist, QCheight), !NILP (prop))
 31719           && calc_pixel_width_or_height (&tem, it, prop, font, false, NULL))
 31720         {
 31721           height = (int)tem;
 31722           zero_height_ok_p = true;
 31723         }
 31724       else if (prop = plist_get (plist, QCrelative_height),
 31725                NUMVAL (prop) > 0)
 31726         height = default_height * NUMVAL (prop);
 31727       else
 31728         height = default_height;
 31729 
 31730       if (height <= 0 && (height < 0 || !zero_height_ok_p))
 31731         height = 1;
 31732 
 31733       /* Compute percentage of height used for ascent.  If
 31734          `:ascent ASCENT' is present and valid, use that.  Otherwise,
 31735          derive the ascent from the font in use.  */
 31736       if (prop = plist_get (plist, QCascent),
 31737           NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
 31738         ascent = height * NUMVAL (prop) / 100.0;
 31739       else if (!NILP (prop)
 31740                && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
 31741         ascent = min (max (0, (int)tem), height);
 31742       else
 31743         ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
 31744     }
 31745   else
 31746 #endif  /* HAVE_WINDOW_SYSTEM */
 31747     height = 1;
 31748 
 31749   if (width > 0
 31750       && it->area == TEXT_AREA && it->line_wrap != TRUNCATE
 31751       && it->current_x + width > it->last_visible_x)
 31752     {
 31753       width = it->last_visible_x - it->current_x;
 31754 #ifdef HAVE_WINDOW_SYSTEM
 31755       /* Subtract one more pixel from the stretch width, but only on
 31756          GUI frames, since on a TTY each glyph is one "pixel" wide.  */
 31757       width -= FRAME_WINDOW_P (it->f);
 31758 #endif
 31759     }
 31760 
 31761   if (width > 0 && height > 0 && it->glyph_row)
 31762     {
 31763       Lisp_Object o_object = it->object;
 31764       Lisp_Object object =
 31765         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31766       int n = width;
 31767 
 31768       if (!STRINGP (object))
 31769         object = it->w->contents;
 31770 #ifdef HAVE_WINDOW_SYSTEM
 31771       if (FRAME_WINDOW_P (it->f))
 31772         append_stretch_glyph (it, object, width, height, ascent);
 31773       else
 31774 #endif
 31775         {
 31776           it->object = object;
 31777           it->char_to_display = ' ';
 31778           it->pixel_width = it->len = 1;
 31779           while (n--)
 31780             tty_append_glyph (it);
 31781           it->object = o_object;
 31782         }
 31783     }
 31784 
 31785   it->pixel_width = width;
 31786 #ifdef HAVE_WINDOW_SYSTEM
 31787   if (FRAME_WINDOW_P (it->f))
 31788     {
 31789       it->ascent = it->phys_ascent = ascent;
 31790       it->descent = it->phys_descent = height - it->ascent;
 31791       it->nglyphs = width > 0 && height > 0;
 31792       take_vertical_position_into_account (it);
 31793     }
 31794   else
 31795 #endif
 31796     it->nglyphs = width;
 31797 }
 31798 
 31799 /* Get information about special display element WHAT in an
 31800    environment described by IT.  WHAT is one of IT_TRUNCATION or
 31801    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
 31802    non-null glyph_row member.  This function ensures that fields like
 31803    face_id, c, len of IT are left untouched.  */
 31804 
 31805 static void
 31806 produce_special_glyphs (struct it *it, enum display_element_type what)
 31807 {
 31808   struct it temp_it;
 31809   Lisp_Object gc;
 31810   GLYPH glyph;
 31811 
 31812   temp_it = *it;
 31813   temp_it.object = Qnil;
 31814   memset (&temp_it.current, 0, sizeof temp_it.current);
 31815 
 31816   if (what == IT_CONTINUATION)
 31817     {
 31818       /* Continuation glyph.  For R2L lines, we mirror it by hand.  */
 31819       if (it->bidi_it.paragraph_dir == R2L)
 31820         SET_GLYPH_FROM_CHAR (glyph, '/');
 31821       else
 31822         SET_GLYPH_FROM_CHAR (glyph, '\\');
 31823       if (it->dp
 31824           && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31825         {
 31826           /* FIXME: Should we mirror GC for R2L lines?  */
 31827           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31828           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31829         }
 31830     }
 31831   else if (what == IT_TRUNCATION)
 31832     {
 31833       /* Truncation glyph.  */
 31834       SET_GLYPH_FROM_CHAR (glyph, '$');
 31835       if (it->dp
 31836           && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31837         {
 31838           /* FIXME: Should we mirror GC for R2L lines?  */
 31839           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31840           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31841         }
 31842     }
 31843   else
 31844     emacs_abort ();
 31845 
 31846 #ifdef HAVE_WINDOW_SYSTEM
 31847   /* On a GUI frame, when the right fringe (left fringe for R2L rows)
 31848      is turned off, we precede the truncation/continuation glyphs by a
 31849      stretch glyph whose width is computed such that these special
 31850      glyphs are aligned at the window margin, even when very different
 31851      fonts are used in different glyph rows.  */
 31852   if (FRAME_WINDOW_P (temp_it.f)
 31853       /* init_iterator calls this with it->glyph_row == NULL, and it
 31854          wants only the pixel width of the truncation/continuation
 31855          glyphs.  */
 31856       && temp_it.glyph_row
 31857       /* insert_left_trunc_glyphs calls us at the beginning of the
 31858          row, and it has its own calculation of the stretch glyph
 31859          width.  */
 31860       && temp_it.glyph_row->used[TEXT_AREA] > 0
 31861       && (temp_it.glyph_row->reversed_p
 31862           ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
 31863           : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
 31864     {
 31865       int stretch_width = temp_it.last_visible_x - temp_it.current_x;
 31866 
 31867       if (stretch_width > 0)
 31868         {
 31869           struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
 31870           struct font *font =
 31871             face->font ? face->font : FRAME_FONT (temp_it.f);
 31872           int stretch_ascent =
 31873             (((temp_it.ascent + temp_it.descent)
 31874               * FONT_BASE (font)) / FONT_HEIGHT (font));
 31875 
 31876           append_stretch_glyph (&temp_it, Qnil, stretch_width,
 31877                                 temp_it.ascent + temp_it.descent,
 31878                                 stretch_ascent);
 31879         }
 31880     }
 31881 #endif
 31882 
 31883   temp_it.dp = NULL;
 31884   temp_it.what = IT_CHARACTER;
 31885   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31886   temp_it.face_id = GLYPH_FACE (glyph);
 31887   temp_it.len = CHAR_BYTES (temp_it.c);
 31888 
 31889   PRODUCE_GLYPHS (&temp_it);
 31890   it->pixel_width = temp_it.pixel_width;
 31891   it->nglyphs = temp_it.nglyphs;
 31892 }
 31893 
 31894 /* Produce padding glyphs for mode/header/tab-line whose text needs to
 31895    be truncated.  This is used when the last visible character leaves
 31896    one or more columns till the window edge, but the next character is
 31897    wider than that number of columns, and therefore cannot fit on the
 31898    line.  We then replace these columns with the appropriate padding
 31899    character: '-' for the mode line and SPC for the other two.  That's
 31900    because these lines should not show the usual truncation glyphs
 31901    there.  This function is only used on TTY frames.  */
 31902 static void
 31903 pad_mode_line (struct it *it, bool mode_line_p)
 31904 {
 31905   struct it temp_it;
 31906   GLYPH glyph;
 31907 
 31908   eassert (!FRAME_WINDOW_P (it->f));
 31909   temp_it = *it;
 31910   temp_it.object = Qnil;
 31911   memset (&temp_it.current, 0, sizeof temp_it.current);
 31912 
 31913   SET_GLYPH (glyph, mode_line_p ? '-' : ' ', it->base_face_id);
 31914 
 31915   temp_it.dp = NULL;
 31916   temp_it.what = IT_CHARACTER;
 31917   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31918   temp_it.face_id = GLYPH_FACE (glyph);
 31919   temp_it.len = CHAR_BYTES (temp_it.c);
 31920 
 31921   PRODUCE_GLYPHS (&temp_it);
 31922   it->pixel_width = temp_it.pixel_width;
 31923   it->nglyphs = temp_it.nglyphs;
 31924 }
 31925 
 31926 #ifdef HAVE_WINDOW_SYSTEM
 31927 
 31928 /* Calculate line-height and line-spacing properties.
 31929    An integer value specifies explicit pixel value.
 31930    A float value specifies relative value to current face height.
 31931    A cons (float . face-name) specifies relative value to
 31932    height of specified face font.
 31933 
 31934    Returns height in pixels, or nil.  */
 31935 
 31936 static Lisp_Object
 31937 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
 31938                            int boff, bool override)
 31939 {
 31940   Lisp_Object face_name = Qnil;
 31941   int ascent, descent, height;
 31942 
 31943   if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
 31944     return val;
 31945 
 31946   if (CONSP (val))
 31947     {
 31948       face_name = XCAR (val);
 31949       val = XCDR (val);
 31950       if (!NUMBERP (val))
 31951         val = make_fixnum (1);
 31952       if (NILP (face_name))
 31953         {
 31954           height = it->ascent + it->descent;
 31955           goto scale;
 31956         }
 31957     }
 31958 
 31959   if (NILP (face_name))
 31960     {
 31961       font = FRAME_FONT (it->f);
 31962       boff = FRAME_BASELINE_OFFSET (it->f);
 31963     }
 31964   else if (EQ (face_name, Qt))
 31965     {
 31966       override = false;
 31967     }
 31968   else
 31969     {
 31970       int face_id;
 31971       struct face *face;
 31972 
 31973       face_id = lookup_named_face (it->w, it->f, face_name, false);
 31974       face = FACE_FROM_ID_OR_NULL (it->f, face_id);
 31975       if (face == NULL || ((font = face->font) == NULL))
 31976         return make_fixnum (-1);
 31977       boff = font->baseline_offset;
 31978       if (font->vertical_centering)
 31979         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31980     }
 31981 
 31982   normal_char_ascent_descent (font, -1, &ascent, &descent);
 31983 
 31984   if (override)
 31985     {
 31986       it->override_ascent = ascent;
 31987       it->override_descent = descent;
 31988       it->override_boff = boff;
 31989     }
 31990 
 31991   height = ascent + descent;
 31992 
 31993  scale:
 31994   /* FIXME: Check for overflow in multiplication or conversion.  */
 31995   if (FLOATP (val))
 31996     height = (int)(XFLOAT_DATA (val) * height);
 31997   else if (INTEGERP (val))
 31998     {
 31999       intmax_t v;
 32000       if (integer_to_intmax (val, &v))
 32001         height *= v;
 32002     }
 32003 
 32004   return make_fixnum (height);
 32005 }
 32006 
 32007 
 32008 /* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
 32009    is a face ID to be used for the glyph.  FOR_NO_FONT is true if
 32010    and only if this is for a character for which no font was found.
 32011 
 32012    If the display method (it->glyphless_method) is
 32013    GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
 32014    length of the acronym or the hexadecimal string, UPPER_XOFF and
 32015    UPPER_YOFF are pixel offsets for the upper part of the string,
 32016    LOWER_XOFF and LOWER_YOFF are for the lower part.
 32017 
 32018    For the other display methods, LEN through LOWER_YOFF are zero.  */
 32019 
 32020 static void
 32021 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
 32022                         short upper_xoff, short upper_yoff,
 32023                         short lower_xoff, short lower_yoff)
 32024 {
 32025   struct glyph *glyph;
 32026   enum glyph_row_area area = it->area;
 32027 
 32028   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 32029   if (glyph < it->glyph_row->glyphs[area + 1])
 32030     {
 32031       /* If the glyph row is reversed, we need to prepend the glyph
 32032          rather than append it.  */
 32033       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 32034         {
 32035           struct glyph *g;
 32036 
 32037           /* Make room for the additional glyph.  */
 32038           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 32039             g[1] = *g;
 32040           glyph = it->glyph_row->glyphs[area];
 32041         }
 32042       glyph->charpos = CHARPOS (it->position);
 32043       glyph->object = it->object;
 32044       eassert (it->pixel_width <= SHRT_MAX);
 32045       glyph->pixel_width = it->pixel_width;
 32046       glyph->ascent = it->ascent;
 32047       glyph->descent = it->descent;
 32048       glyph->voffset = it->voffset;
 32049       glyph->type = GLYPHLESS_GLYPH;
 32050       glyph->u.glyphless.method = it->glyphless_method;
 32051       glyph->u.glyphless.for_no_font = for_no_font;
 32052       glyph->u.glyphless.len = len;
 32053       glyph->u.glyphless.ch = it->c;
 32054       glyph->slice.glyphless.upper_xoff = upper_xoff;
 32055       glyph->slice.glyphless.upper_yoff = upper_yoff;
 32056       glyph->slice.glyphless.lower_xoff = lower_xoff;
 32057       glyph->slice.glyphless.lower_yoff = lower_yoff;
 32058       glyph->avoid_cursor_p = it->avoid_cursor_p;
 32059       glyph->multibyte_p = it->multibyte_p;
 32060       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 32061         {
 32062           /* In R2L rows, the left and the right box edges need to be
 32063              drawn in reverse direction.  */
 32064           glyph->right_box_line_p = it->start_of_box_run_p;
 32065           glyph->left_box_line_p = it->end_of_box_run_p;
 32066         }
 32067       else
 32068         {
 32069           glyph->left_box_line_p = it->start_of_box_run_p;
 32070           glyph->right_box_line_p = it->end_of_box_run_p;
 32071         }
 32072       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 32073                                       || it->phys_descent > it->descent);
 32074       glyph->padding_p = false;
 32075       glyph->glyph_not_available_p = false;
 32076       glyph->face_id = face_id;
 32077       glyph->font_type = FONT_TYPE_UNKNOWN;
 32078       if (it->bidi_p)
 32079         {
 32080           glyph->resolved_level = it->bidi_it.resolved_level;
 32081           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 32082           glyph->bidi_type = it->bidi_it.type;
 32083         }
 32084       ++it->glyph_row->used[area];
 32085     }
 32086   else
 32087     IT_EXPAND_MATRIX_WIDTH (it, area);
 32088 }
 32089 
 32090 
 32091 /* Produce a glyph for a glyphless character for iterator IT.
 32092    IT->glyphless_method specifies which method to use for displaying
 32093    the character.  See the description of enum
 32094    glyphless_display_method in dispextern.h for the detail.
 32095 
 32096    FOR_NO_FONT is true if and only if this is for a character for
 32097    which no font was found.  ACRONYM, if non-nil, is an acronym string
 32098    for the character.  */
 32099 
 32100 static void
 32101 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
 32102 {
 32103   int face_id;
 32104   struct face *face;
 32105   struct font *font;
 32106   int base_width, base_height, width, height;
 32107   short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
 32108   int len;
 32109 
 32110   /* Get the metrics of the base font.  We always refer to the current
 32111      ASCII face, but if some faulty setup of fontsets causes that to
 32112      be NULL, we fall back to the frame's default font.  */
 32113   face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
 32114   if (face)
 32115     face = face->ascii_face;
 32116   font = (face && face->font) ? face->font : FRAME_FONT (it->f);
 32117   normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 32118   it->ascent += font->baseline_offset;
 32119   it->descent -= font->baseline_offset;
 32120   base_height = it->ascent + it->descent;
 32121   base_width = font->average_width;
 32122 
 32123   face_id = merge_glyphless_glyph_face (it);
 32124 
 32125   if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
 32126     {
 32127       it->pixel_width = THIN_SPACE_WIDTH;
 32128       len = 0;
 32129       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 32130     }
 32131   else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
 32132     {
 32133       width = CHARACTER_WIDTH (it->c);
 32134       if (width == 0)
 32135         width = 1;
 32136       else if (width > 4)
 32137         width = 4;
 32138       it->pixel_width = base_width * width;
 32139       len = 0;
 32140       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 32141     }
 32142   else
 32143     {
 32144       char buf[7];
 32145       const char *str;
 32146       unsigned int code[6];
 32147       int upper_len;
 32148       int ascent, descent;
 32149       struct font_metrics metrics_upper, metrics_lower;
 32150 
 32151       face = FACE_FROM_ID (it->f, face_id);
 32152       font = face->font ? face->font : FRAME_FONT (it->f);
 32153       prepare_face_for_display (it->f, face);
 32154 
 32155       if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
 32156         {
 32157           if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
 32158             acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
 32159           if (CONSP (acronym))
 32160             acronym = XCAR (acronym);
 32161           str = STRINGP (acronym) ? SSDATA (acronym) : "";
 32162         }
 32163       else
 32164         {
 32165           eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
 32166           sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
 32167           str = buf;
 32168         }
 32169       for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
 32170         code[len] = font->driver->encode_char (font, str[len]);
 32171       upper_len = (len + 1) / 2;
 32172       font->driver->text_extents (font, code, upper_len,
 32173                                   &metrics_upper);
 32174       font->driver->text_extents (font, code + upper_len, len - upper_len,
 32175                                   &metrics_lower);
 32176 
 32177 
 32178 
 32179       /* +4 is for vertical bars of a box plus 1-pixel spaces at both side.  */
 32180       width = max (metrics_upper.width, metrics_lower.width) + 4;
 32181       upper_xoff = lower_xoff = 2; /* the typical case */
 32182       if (base_width >= width)
 32183         {
 32184           /* Align the upper to the left, the lower to the right.  */
 32185           it->pixel_width = base_width;
 32186           lower_xoff = base_width - 2 - metrics_lower.width;
 32187         }
 32188       else
 32189         {
 32190           /* Center the shorter one.  */
 32191           it->pixel_width = width;
 32192           if (metrics_upper.width >= metrics_lower.width)
 32193             lower_xoff = (width - metrics_lower.width) / 2;
 32194           else
 32195             upper_xoff = (width - metrics_upper.width) / 2;
 32196         }
 32197 
 32198       /* +5 is for horizontal bars of a box plus 1-pixel spaces at
 32199          top, bottom, and between upper and lower strings.  */
 32200       height = (metrics_upper.ascent + metrics_upper.descent
 32201                 + metrics_lower.ascent + metrics_lower.descent) + 5;
 32202       /* Center vertically.
 32203          H:base_height, D:base_descent
 32204          h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
 32205 
 32206          ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
 32207          descent = D - H/2 + h/2;
 32208          lower_yoff = descent - 2 - ld;
 32209          upper_yoff = lower_yoff - la - 1 - ud;  */
 32210       ascent = - (it->descent - (base_height + height + 1) / 2);
 32211       descent = it->descent - (base_height - height) / 2;
 32212       lower_yoff = descent - 2 - metrics_lower.descent;
 32213       upper_yoff = (lower_yoff - metrics_lower.ascent - 1
 32214                     - metrics_upper.descent);
 32215       /* Don't make the height shorter than the base height.  */
 32216       if (height > base_height)
 32217         {
 32218           it->ascent = ascent;
 32219           it->descent = descent;
 32220         }
 32221     }
 32222 
 32223   it->phys_ascent = it->ascent;
 32224   it->phys_descent = it->descent;
 32225   if (it->glyph_row)
 32226     append_glyphless_glyph (it, face_id, for_no_font, len,
 32227                             upper_xoff, upper_yoff,
 32228                             lower_xoff, lower_yoff);
 32229   it->nglyphs = 1;
 32230   take_vertical_position_into_account (it);
 32231 }
 32232 
 32233 
 32234 /* If face has a box, add the box thickness to the character
 32235    height.  If character has a box line to the left and/or
 32236    right, add the box line width to the character's width.  */
 32237 #define IT_APPLY_FACE_BOX(it, face)                             \
 32238   do {                                                          \
 32239     if (face->box != FACE_NO_BOX)                               \
 32240       {                                                         \
 32241         int thick = face->box_horizontal_line_width;            \
 32242         if (thick > 0)                                          \
 32243           {                                                     \
 32244             it->ascent += thick;                                \
 32245             it->descent += thick;                               \
 32246           }                                                     \
 32247                                                                 \
 32248         thick = face->box_vertical_line_width;                  \
 32249         if (thick > 0)                                          \
 32250           {                                                     \
 32251             if (it->start_of_box_run_p)                         \
 32252               it->pixel_width += thick;                         \
 32253             if (it->end_of_box_run_p)                           \
 32254               it->pixel_width += thick;                         \
 32255           }                                                     \
 32256       }                                                         \
 32257     } while (false)
 32258 
 32259 /* RIF:
 32260    Produce glyphs/get display metrics for the display element IT is
 32261    loaded with.  See the description of struct it in dispextern.h
 32262    for an overview of struct it.  */
 32263 
 32264 void
 32265 gui_produce_glyphs (struct it *it)
 32266 {
 32267   int extra_line_spacing = it->extra_line_spacing;
 32268 
 32269   it->glyph_not_available_p = false;
 32270 
 32271   if (it->what == IT_CHARACTER)
 32272     {
 32273       unsigned char2b;
 32274       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32275       struct font *font = face->font;
 32276       struct font_metrics *pcm = NULL;
 32277       int boff;                 /* Baseline offset.  */
 32278 
 32279       if (font == NULL)
 32280         {
 32281           /* When no suitable font is found, display this character by
 32282              the method specified in the first extra slot of
 32283              Vglyphless_char_display.  */
 32284           Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 32285 
 32286           eassert (it->what == IT_GLYPHLESS);
 32287           produce_glyphless_glyph (it, true,
 32288                                    STRINGP (acronym) ? acronym : Qnil);
 32289           goto done;
 32290         }
 32291 
 32292       boff = font->baseline_offset;
 32293       if (font->vertical_centering)
 32294         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32295 
 32296       if (it->char_to_display != '\n' && it->char_to_display != '\t')
 32297         {
 32298           it->nglyphs = 1;
 32299 
 32300           if (it->override_ascent >= 0)
 32301             {
 32302               it->ascent = it->override_ascent;
 32303               it->descent = it->override_descent;
 32304               boff = it->override_boff;
 32305             }
 32306           else
 32307             {
 32308               it->ascent = FONT_BASE (font) + boff;
 32309               it->descent = FONT_DESCENT (font) - boff;
 32310             }
 32311 
 32312           if (get_char_glyph_code (it->char_to_display, font, &char2b))
 32313             {
 32314               pcm = get_per_char_metric (font, &char2b);
 32315               if (pcm->width == 0
 32316                   && pcm->rbearing == 0 && pcm->lbearing == 0)
 32317                 pcm = NULL;
 32318             }
 32319 
 32320           if (pcm)
 32321             {
 32322               it->phys_ascent = pcm->ascent + boff;
 32323               it->phys_descent = pcm->descent - boff;
 32324               it->pixel_width = pcm->width;
 32325               /* Don't use font-global values for ascent and descent
 32326                  if they result in an exceedingly large line height.  */
 32327               if (it->override_ascent < 0)
 32328                 {
 32329                   if (FONT_TOO_HIGH (font))
 32330                     {
 32331                       it->ascent = it->phys_ascent;
 32332                       it->descent = it->phys_descent;
 32333                       /* These limitations are enforced by an
 32334                          assertion near the end of this function.  */
 32335                       if (it->ascent < 0)
 32336                         it->ascent = 0;
 32337                       if (it->descent < 0)
 32338                         it->descent = 0;
 32339                     }
 32340                 }
 32341             }
 32342           else
 32343             {
 32344               it->glyph_not_available_p = true;
 32345               it->phys_ascent = it->ascent;
 32346               it->phys_descent = it->descent;
 32347               it->pixel_width = font->space_width;
 32348             }
 32349 
 32350           if (it->constrain_row_ascent_descent_p)
 32351             {
 32352               if (it->descent > it->max_descent)
 32353                 {
 32354                   it->ascent += it->descent - it->max_descent;
 32355                   it->descent = it->max_descent;
 32356                 }
 32357               if (it->ascent > it->max_ascent)
 32358                 {
 32359                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32360                   it->ascent = it->max_ascent;
 32361                 }
 32362               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32363               it->phys_descent = min (it->phys_descent, it->descent);
 32364               extra_line_spacing = 0;
 32365             }
 32366 
 32367           /* If this is a space inside a region of text with
 32368              `space-width' property, change its width.  */
 32369           bool stretched_p
 32370             = it->char_to_display == ' ' && !NILP (it->space_width);
 32371           if (stretched_p)
 32372             it->pixel_width *= XFLOATINT (it->space_width);
 32373 
 32374           IT_APPLY_FACE_BOX(it, face);
 32375 
 32376           /* If face has an overline, add the height of the overline
 32377              (1 pixel) and a 1 pixel margin to the character height.  */
 32378           if (face->overline_p)
 32379             it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32380 
 32381           if (it->constrain_row_ascent_descent_p)
 32382             {
 32383               if (it->ascent > it->max_ascent)
 32384                 it->ascent = it->max_ascent;
 32385               if (it->descent > it->max_descent)
 32386                 it->descent = it->max_descent;
 32387             }
 32388 
 32389           take_vertical_position_into_account (it);
 32390 
 32391           /* If we have to actually produce glyphs, do it.  */
 32392           if (it->glyph_row)
 32393             {
 32394               if (stretched_p)
 32395                 {
 32396                   /* Translate a space with a `space-width' property
 32397                      into a stretch glyph.  */
 32398                   int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
 32399                                 / FONT_HEIGHT (font));
 32400                   append_stretch_glyph (it, it->object, it->pixel_width,
 32401                                         it->ascent + it->descent, ascent);
 32402                 }
 32403               else
 32404                 append_glyph (it);
 32405 
 32406               /* If characters with lbearing or rbearing are displayed
 32407                  in this line, record that fact in a flag of the
 32408                  glyph row.  This is used to optimize X output code.  */
 32409               if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
 32410                 it->glyph_row->contains_overlapping_glyphs_p = true;
 32411             }
 32412           if (! stretched_p && it->pixel_width == 0)
 32413             /* We assure that all visible glyphs have at least 1-pixel
 32414                width.  */
 32415             it->pixel_width = 1;
 32416         }
 32417       else if (it->char_to_display == '\n')
 32418         {
 32419           /* A newline has no width, but we need the height of the
 32420              line.  But if previous part of the line sets a height,
 32421              don't increase that height.  */
 32422 
 32423           Lisp_Object height;
 32424           Lisp_Object total_height = Qnil;
 32425 
 32426           it->override_ascent = -1;
 32427           it->pixel_width = 0;
 32428           it->nglyphs = 0;
 32429 
 32430           height = get_it_property (it, Qline_height);
 32431           /* Split (line-height total-height) list.  */
 32432           if (CONSP (height)
 32433               && CONSP (XCDR (height))
 32434               && NILP (XCDR (XCDR (height))))
 32435             {
 32436               total_height = XCAR (XCDR (height));
 32437               height = XCAR (height);
 32438             }
 32439           height = calc_line_height_property (it, height, font, boff, true);
 32440 
 32441           if (it->override_ascent >= 0)
 32442             {
 32443               it->ascent = it->override_ascent;
 32444               it->descent = it->override_descent;
 32445               boff = it->override_boff;
 32446             }
 32447           else
 32448             {
 32449               if (FONT_TOO_HIGH (font))
 32450                 {
 32451                   it->ascent = font->pixel_size + boff - 1;
 32452                   it->descent = -boff + 1;
 32453                   if (it->descent < 0)
 32454                     it->descent = 0;
 32455                 }
 32456               else
 32457                 {
 32458                   it->ascent = FONT_BASE (font) + boff;
 32459                   it->descent = FONT_DESCENT (font) - boff;
 32460                 }
 32461             }
 32462 
 32463           if (EQ (height, Qt))
 32464             {
 32465               if (it->descent > it->max_descent)
 32466                 {
 32467                   it->ascent += it->descent - it->max_descent;
 32468                   it->descent = it->max_descent;
 32469                 }
 32470               if (it->ascent > it->max_ascent)
 32471                 {
 32472                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32473                   it->ascent = it->max_ascent;
 32474                 }
 32475               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32476               it->phys_descent = min (it->phys_descent, it->descent);
 32477               it->constrain_row_ascent_descent_p = true;
 32478               extra_line_spacing = 0;
 32479             }
 32480           else
 32481             {
 32482               Lisp_Object spacing;
 32483 
 32484               it->phys_ascent = it->ascent;
 32485               it->phys_descent = it->descent;
 32486 
 32487               if ((it->max_ascent > 0 || it->max_descent > 0)
 32488                   && face->box != FACE_NO_BOX
 32489                   && face->box_horizontal_line_width > 0)
 32490                 {
 32491                   it->ascent += face->box_horizontal_line_width;
 32492                   it->descent += face->box_horizontal_line_width;
 32493                 }
 32494               if (!NILP (height)
 32495                   && XFIXNUM (height) > it->ascent + it->descent)
 32496                 it->ascent = XFIXNUM (height) - it->descent;
 32497 
 32498               if (!NILP (total_height))
 32499                 spacing = calc_line_height_property (it, total_height, font,
 32500                                                      boff, false);
 32501               else
 32502                 {
 32503                   spacing = get_it_property (it, Qline_spacing);
 32504                   spacing = calc_line_height_property (it, spacing, font,
 32505                                                        boff, false);
 32506                 }
 32507               if (FIXNUMP (spacing))
 32508                 {
 32509                   extra_line_spacing = XFIXNUM (spacing);
 32510                   if (!NILP (total_height))
 32511                     extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 32512                 }
 32513             }
 32514         }
 32515       else                    /* i.e. (it->char_to_display == '\t') */
 32516         {
 32517           if (font->space_width > 0)
 32518             {
 32519               int tab_width = it->tab_width * font->space_width;
 32520               int x = it->current_x + it->continuation_lines_width;
 32521               int x0 = x;
 32522               /* Adjust for line numbers, if needed.   */
 32523               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32524                 {
 32525                   x -= it->lnum_pixel_width;
 32526                   /* Restore the original TAB width, if required.  */
 32527                   if (x + it->stretch_adjust >= it->first_visible_x)
 32528                     x += it->stretch_adjust;
 32529                 }
 32530 
 32531               int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
 32532 
 32533               /* If the distance from the current position to the next tab
 32534                  stop is less than a space character width, use the
 32535                  tab stop after that.  */
 32536               if (next_tab_x - x < font->space_width)
 32537                 next_tab_x += tab_width;
 32538               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32539                 {
 32540                   next_tab_x += it->lnum_pixel_width;
 32541                   /* If the line is hscrolled, and the TAB starts before
 32542                      the first visible pixel, simulate negative row->x.  */
 32543                   if (x < it->first_visible_x)
 32544                     {
 32545                       next_tab_x -= it->first_visible_x - x;
 32546                       it->stretch_adjust = it->first_visible_x - x;
 32547                     }
 32548                   else
 32549                     next_tab_x -= it->stretch_adjust;
 32550                 }
 32551 
 32552               it->pixel_width = next_tab_x - x0;
 32553               it->nglyphs = 1;
 32554               if (FONT_TOO_HIGH (font))
 32555                 {
 32556                   if (get_char_glyph_code (' ', font, &char2b))
 32557                     {
 32558                       pcm = get_per_char_metric (font, &char2b);
 32559                       if (pcm->width == 0
 32560                           && pcm->rbearing == 0 && pcm->lbearing == 0)
 32561                         pcm = NULL;
 32562                     }
 32563 
 32564                   if (pcm)
 32565                     {
 32566                       it->ascent = pcm->ascent + boff;
 32567                       it->descent = pcm->descent - boff;
 32568                     }
 32569                   else
 32570                     {
 32571                       it->ascent = font->pixel_size + boff - 1;
 32572                       it->descent = -boff + 1;
 32573                     }
 32574                   if (it->ascent < 0)
 32575                     it->ascent = 0;
 32576                   if (it->descent < 0)
 32577                     it->descent = 0;
 32578                 }
 32579               else
 32580                 {
 32581                   it->ascent = FONT_BASE (font) + boff;
 32582                   it->descent = FONT_DESCENT (font) - boff;
 32583                 }
 32584               it->phys_ascent = it->ascent;
 32585               it->phys_descent = it->descent;
 32586 
 32587               if (it->glyph_row)
 32588                 {
 32589                   append_stretch_glyph (it, it->object, it->pixel_width,
 32590                                         it->ascent + it->descent, it->ascent);
 32591                 }
 32592             }
 32593           else
 32594             {
 32595               it->pixel_width = 0;
 32596               it->nglyphs = 1;
 32597             }
 32598         }
 32599 
 32600       if (FONT_TOO_HIGH (font))
 32601         {
 32602           int font_ascent, font_descent;
 32603 
 32604           /* For very large fonts, where we ignore the declared font
 32605              dimensions, and go by per-character metrics instead,
 32606              don't let the row ascent and descent values (and the row
 32607              height computed from them) be smaller than the "normal"
 32608              character metrics.  This avoids unpleasant effects
 32609              whereby lines on display would change their height
 32610              depending on which characters are shown.  */
 32611           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32612           it->max_ascent = max (it->max_ascent, font_ascent);
 32613           it->max_descent = max (it->max_descent, font_descent);
 32614         }
 32615 
 32616       if (it->ascent < 0)
 32617         it->ascent = 0;
 32618       if (it->descent < 0)
 32619         it->descent = 0;
 32620     }
 32621   else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
 32622     {
 32623       /* A static composition.
 32624 
 32625          Note: A composition is represented as one glyph in the
 32626          glyph matrix.  There are no padding glyphs.
 32627 
 32628          Important note: pixel_width, ascent, and descent are the
 32629          values of what is drawn by draw_glyphs (i.e. the values of
 32630          the overall glyphs composed).  */
 32631       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32632       int boff;                 /* baseline offset */
 32633       struct composition *cmp = composition_table[it->cmp_it.id];
 32634       int glyph_len = cmp->glyph_len;
 32635       struct font *font = face->font;
 32636 
 32637       it->nglyphs = 1;
 32638 
 32639       /* If we have not yet calculated pixel size data of glyphs of
 32640          the composition for the current face font, calculate them
 32641          now.  Theoretically, we have to check all fonts for the
 32642          glyphs, but that requires much time and memory space.  So,
 32643          here we check only the font of the first glyph.  This may
 32644          lead to incorrect display, but it's very rare, and C-l
 32645          (recenter-top-bottom) can correct the display anyway.  */
 32646       if (! cmp->font || cmp->font != font)
 32647         {
 32648           /* Ascent and descent of the font of the first character
 32649              of this composition (adjusted by baseline offset).
 32650              Ascent and descent of overall glyphs should not be less
 32651              than these, respectively.  */
 32652           int font_ascent, font_descent, font_height;
 32653           /* Bounding box of the overall glyphs.  */
 32654           int leftmost, rightmost, lowest, highest;
 32655           int lbearing, rbearing;
 32656           int i, width, ascent, descent;
 32657           int c;
 32658           unsigned char2b;
 32659           struct font_metrics *pcm;
 32660           ptrdiff_t pos;
 32661 
 32662           eassume (0 < glyph_len); /* See Bug#8512.  */
 32663           do
 32664             c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
 32665           while (c == '\t' && 0 < --glyph_len);
 32666 
 32667           bool right_padded = glyph_len < cmp->glyph_len;
 32668           for (i = 0; i < glyph_len; i++)
 32669             {
 32670               c = COMPOSITION_GLYPH (cmp, i);
 32671               if (c != '\t')
 32672                 break;
 32673               cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32674             }
 32675           bool left_padded = i > 0;
 32676 
 32677           pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
 32678                  : IT_CHARPOS (*it));
 32679           /* If no suitable font is found, use the default font.  */
 32680           bool font_not_found_p = font == NULL;
 32681           if (font_not_found_p)
 32682             {
 32683               face = face->ascii_face;
 32684               font = face->font;
 32685             }
 32686           boff = font->baseline_offset;
 32687           if (font->vertical_centering)
 32688             boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32689           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32690           font_ascent +=  boff;
 32691           font_descent -= boff;
 32692           font_height = font_ascent + font_descent;
 32693 
 32694           cmp->font = font;
 32695 
 32696           pcm = NULL;
 32697           if (! font_not_found_p)
 32698             {
 32699               get_char_face_and_encoding (it->f, c, it->face_id,
 32700                                           &char2b, false);
 32701               pcm = get_per_char_metric (font, &char2b);
 32702             }
 32703 
 32704           /* Initialize the bounding box.  */
 32705           if (pcm)
 32706             {
 32707               width = cmp->glyph_len > 0 ? pcm->width : 0;
 32708               ascent = pcm->ascent;
 32709               descent = pcm->descent;
 32710               lbearing = pcm->lbearing;
 32711               rbearing = pcm->rbearing;
 32712             }
 32713           else
 32714             {
 32715               width = cmp->glyph_len > 0 ? font->space_width : 0;
 32716               ascent = FONT_BASE (font);
 32717               descent = FONT_DESCENT (font);
 32718               lbearing = 0;
 32719               rbearing = width;
 32720             }
 32721 
 32722           rightmost = width;
 32723           leftmost = 0;
 32724           lowest = - descent + boff;
 32725           highest = ascent + boff;
 32726 
 32727           if (! font_not_found_p
 32728               && font->default_ascent
 32729               && CHAR_TABLE_P (Vuse_default_ascent)
 32730               && !NILP (Faref (Vuse_default_ascent,
 32731                                make_fixnum (it->char_to_display))))
 32732             highest = font->default_ascent + boff;
 32733 
 32734           /* Draw the first glyph at the normal position.  It may be
 32735              shifted to right later if some other glyphs are drawn
 32736              at the left.  */
 32737           cmp->offsets[i * 2] = 0;
 32738           cmp->offsets[i * 2 + 1] = boff;
 32739           cmp->lbearing = lbearing;
 32740           cmp->rbearing = rbearing;
 32741 
 32742           /* Set cmp->offsets for the remaining glyphs.  */
 32743           for (i++; i < glyph_len; i++)
 32744             {
 32745               int left, right, btm, top;
 32746               int ch = COMPOSITION_GLYPH (cmp, i);
 32747               int face_id;
 32748               struct face *this_face;
 32749 
 32750               if (ch == '\t')
 32751                 ch = ' ';
 32752               face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
 32753               this_face = FACE_FROM_ID (it->f, face_id);
 32754               font = this_face->font;
 32755 
 32756               if (font == NULL)
 32757                 pcm = NULL;
 32758               else
 32759                 {
 32760                   get_char_face_and_encoding (it->f, ch, face_id,
 32761                                               &char2b, false);
 32762                   pcm = get_per_char_metric (font, &char2b);
 32763                 }
 32764               if (! pcm)
 32765                 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32766               else
 32767                 {
 32768                   width = pcm->width;
 32769                   ascent = pcm->ascent;
 32770                   descent = pcm->descent;
 32771                   lbearing = pcm->lbearing;
 32772                   rbearing = pcm->rbearing;
 32773                   if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
 32774                     {
 32775                       /* Relative composition with or without
 32776                          alternate chars.  */
 32777                       left = (leftmost + rightmost - width) / 2;
 32778                       btm = - descent + boff;
 32779                       if (font->relative_compose
 32780                           && (! CHAR_TABLE_P (Vignore_relative_composition)
 32781                               || NILP (Faref (Vignore_relative_composition,
 32782                                               make_fixnum (ch)))))
 32783                         {
 32784 
 32785                           if (- descent >= font->relative_compose)
 32786                             /* One extra pixel between two glyphs.  */
 32787                             btm = highest + 1;
 32788                           else if (ascent <= 0)
 32789                             /* One extra pixel between two glyphs.  */
 32790                             btm = lowest - 1 - ascent - descent;
 32791                         }
 32792                     }
 32793                   else
 32794                     {
 32795                       /* A composition rule is specified by an integer
 32796                          value that encodes global and new reference
 32797                          points (GREF and NREF).  GREF and NREF are
 32798                          specified by numbers as below:
 32799 
 32800                          0---1---2 -- ascent
 32801                          |       |
 32802                          |       |
 32803                          |       |
 32804                          9--10--11 -- center
 32805                          |       |
 32806                          ---3---4---5--- baseline
 32807                          |       |
 32808                          6---7---8 -- descent
 32809                       */
 32810                       int rule = COMPOSITION_RULE (cmp, i);
 32811                       int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
 32812 
 32813                       COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
 32814                       grefx = gref % 3, nrefx = nref % 3;
 32815                       grefy = gref / 3, nrefy = nref / 3;
 32816                       if (xoff)
 32817                         xoff = font_height * (xoff - 128) / 256;
 32818                       if (yoff)
 32819                         yoff = font_height * (yoff - 128) / 256;
 32820 
 32821                       left = (leftmost
 32822                               + grefx * (rightmost - leftmost) / 2
 32823                               - nrefx * width / 2
 32824                               + xoff);
 32825 
 32826                       btm = ((grefy == 0 ? highest
 32827                               : grefy == 1 ? 0
 32828                               : grefy == 2 ? lowest
 32829                               : (highest + lowest) / 2)
 32830                              - (nrefy == 0 ? ascent + descent
 32831                                 : nrefy == 1 ? descent - boff
 32832                                 : nrefy == 2 ? 0
 32833                                 : (ascent + descent) / 2)
 32834                              + yoff);
 32835                     }
 32836 
 32837                   cmp->offsets[i * 2] = left;
 32838                   cmp->offsets[i * 2 + 1] = btm + descent;
 32839 
 32840                   /* Update the bounding box of the overall glyphs. */
 32841                   if (width > 0)
 32842                     {
 32843                       right = left + width;
 32844                       if (left < leftmost)
 32845                         leftmost = left;
 32846                       if (right > rightmost)
 32847                         rightmost = right;
 32848                     }
 32849                   top = btm + descent + ascent;
 32850                   if (top > highest)
 32851                     highest = top;
 32852                   if (btm < lowest)
 32853                     lowest = btm;
 32854 
 32855                   if (cmp->lbearing > left + lbearing)
 32856                     cmp->lbearing = left + lbearing;
 32857                   if (cmp->rbearing < left + rbearing)
 32858                     cmp->rbearing = left + rbearing;
 32859                 }
 32860             }
 32861 
 32862           /* If there are glyphs whose x-offsets are negative,
 32863              shift all glyphs to the right and make all x-offsets
 32864              non-negative.  */
 32865           if (leftmost < 0)
 32866             {
 32867               for (i = 0; i < cmp->glyph_len; i++)
 32868                 cmp->offsets[i * 2] -= leftmost;
 32869               rightmost -= leftmost;
 32870               cmp->lbearing -= leftmost;
 32871               cmp->rbearing -= leftmost;
 32872             }
 32873 
 32874           if (left_padded && cmp->lbearing < 0)
 32875             {
 32876               for (i = 0; i < cmp->glyph_len; i++)
 32877                 cmp->offsets[i * 2] -= cmp->lbearing;
 32878               rightmost -= cmp->lbearing;
 32879               cmp->rbearing -= cmp->lbearing;
 32880               cmp->lbearing = 0;
 32881             }
 32882           if (right_padded && rightmost < cmp->rbearing)
 32883             {
 32884               rightmost = cmp->rbearing;
 32885             }
 32886 
 32887           cmp->pixel_width = rightmost;
 32888           cmp->ascent = highest;
 32889           cmp->descent = - lowest;
 32890           if (cmp->ascent < font_ascent)
 32891             cmp->ascent = font_ascent;
 32892           if (cmp->descent < font_descent)
 32893             cmp->descent = font_descent;
 32894         }
 32895 
 32896       if (it->glyph_row
 32897           && (cmp->lbearing < 0
 32898               || cmp->rbearing > cmp->pixel_width))
 32899         it->glyph_row->contains_overlapping_glyphs_p = true;
 32900 
 32901       it->pixel_width = cmp->pixel_width;
 32902       it->ascent = it->phys_ascent = cmp->ascent;
 32903       it->descent = it->phys_descent = cmp->descent;
 32904       IT_APPLY_FACE_BOX(it, face);
 32905 
 32906       /* If face has an overline, add the height of the overline
 32907          (1 pixel) and a 1 pixel margin to the character height.  */
 32908       if (face->overline_p)
 32909         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32910 
 32911       take_vertical_position_into_account (it);
 32912       if (it->ascent < 0)
 32913         it->ascent = 0;
 32914       if (it->descent < 0)
 32915         it->descent = 0;
 32916 
 32917       if (it->glyph_row && cmp->glyph_len > 0)
 32918         append_composite_glyph (it);
 32919     }
 32920   else if (it->what == IT_COMPOSITION)
 32921     {
 32922       /* A dynamic (automatic) composition.  */
 32923       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32924       Lisp_Object gstring;
 32925       struct font_metrics metrics;
 32926 
 32927       it->nglyphs = 1;
 32928 
 32929       gstring = composition_gstring_from_id (it->cmp_it.id);
 32930       it->pixel_width
 32931         = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
 32932                                      &metrics);
 32933       if (it->pixel_width == 0)
 32934         {
 32935           it->glyph_not_available_p = true;
 32936           it->phys_ascent = it->ascent;
 32937           it->phys_descent = it->descent;
 32938           it->pixel_width = face->font->space_width;
 32939         }
 32940       else
 32941         {
 32942           if (it->glyph_row
 32943               && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
 32944             it->glyph_row->contains_overlapping_glyphs_p = true;
 32945           it->ascent = it->phys_ascent = metrics.ascent;
 32946           it->descent = it->phys_descent = metrics.descent;
 32947         }
 32948       IT_APPLY_FACE_BOX(it, face);
 32949 
 32950       /* If face has an overline, add the height of the overline
 32951          (1 pixel) and a 1 pixel margin to the character height.  */
 32952       if (face->overline_p)
 32953         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32954       take_vertical_position_into_account (it);
 32955       if (it->ascent < 0)
 32956         it->ascent = 0;
 32957       if (it->descent < 0)
 32958         it->descent = 0;
 32959 
 32960       if (it->glyph_row)
 32961         append_composite_glyph (it);
 32962     }
 32963   else if (it->what == IT_GLYPHLESS)
 32964     produce_glyphless_glyph (it, false, Qnil);
 32965   else if (it->what == IT_IMAGE)
 32966     produce_image_glyph (it);
 32967   else if (it->what == IT_STRETCH)
 32968     produce_stretch_glyph (it);
 32969   else if (it->what == IT_XWIDGET)
 32970     produce_xwidget_glyph (it);
 32971 
 32972  done:
 32973   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
 32974      because this isn't true for images with `:ascent 100'.  */
 32975   eassert (it->ascent >= 0 && it->descent >= 0);
 32976   if (it->area == TEXT_AREA)
 32977     it->current_x += it->pixel_width;
 32978 
 32979   if (extra_line_spacing > 0)
 32980     {
 32981       it->descent += extra_line_spacing;
 32982       if (extra_line_spacing > it->max_extra_line_spacing)
 32983         it->max_extra_line_spacing = extra_line_spacing;
 32984     }
 32985 
 32986   it->max_ascent = max (it->max_ascent, it->ascent);
 32987   it->max_descent = max (it->max_descent, it->descent);
 32988   it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
 32989   it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
 32990 }
 32991 
 32992 /* EXPORT for RIF:
 32993    Output LEN glyphs starting at START at the nominal cursor position.
 32994    Advance the nominal cursor over the text.  UPDATED_ROW is the glyph row
 32995    being updated, and UPDATED_AREA is the area of that row being updated.  */
 32996 
 32997 void
 32998 gui_write_glyphs (struct window *w, struct glyph_row *updated_row,
 32999                 struct glyph *start, enum glyph_row_area updated_area, int len)
 33000 {
 33001   int x, hpos, chpos = w->phys_cursor.hpos;
 33002 
 33003   eassert (updated_row);
 33004   /* When the window is hscrolled, cursor hpos can legitimately be out
 33005      of bounds, but we draw the cursor at the corresponding window
 33006      margin in that case.  */
 33007   if (!updated_row->reversed_p && chpos < 0)
 33008     chpos = 0;
 33009   if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
 33010     chpos = updated_row->used[TEXT_AREA] - 1;
 33011 
 33012   block_input ();
 33013 
 33014   /* Write glyphs.  */
 33015 
 33016   hpos = start - updated_row->glyphs[updated_area];
 33017   x = draw_glyphs (w, w->output_cursor.x,
 33018                    updated_row, updated_area,
 33019                    hpos, hpos + len,
 33020                    DRAW_NORMAL_TEXT, 0);
 33021 
 33022   /* Invalidate old phys cursor if the glyph at its hpos is redrawn.  */
 33023   if (updated_area == TEXT_AREA
 33024       && w->phys_cursor_on_p
 33025       && w->phys_cursor.vpos == w->output_cursor.vpos
 33026       && chpos >= hpos
 33027       && chpos < hpos + len)
 33028     w->phys_cursor_on_p = false;
 33029 
 33030   unblock_input ();
 33031 
 33032   /* Advance the output cursor.  */
 33033   w->output_cursor.hpos += len;
 33034   w->output_cursor.x = x;
 33035 }
 33036 
 33037 
 33038 /* EXPORT for RIF:
 33039    Insert LEN glyphs from START at the nominal cursor position.  */
 33040 
 33041 void
 33042 gui_insert_glyphs (struct window *w, struct glyph_row *updated_row,
 33043                  struct glyph *start, enum glyph_row_area updated_area, int len)
 33044 {
 33045   struct frame *f;
 33046   int line_height, shift_by_width, shifted_region_width;
 33047   struct glyph_row *row;
 33048   struct glyph *glyph;
 33049   int frame_x, frame_y;
 33050   ptrdiff_t hpos;
 33051 
 33052   eassert (updated_row);
 33053   block_input ();
 33054   f = XFRAME (WINDOW_FRAME (w));
 33055 
 33056   /* Get the height of the line we are in.  */
 33057   row = updated_row;
 33058   line_height = row->height;
 33059 
 33060   /* Get the width of the glyphs to insert.  */
 33061   shift_by_width = 0;
 33062   for (glyph = start; glyph < start + len; ++glyph)
 33063     shift_by_width += glyph->pixel_width;
 33064 
 33065   /* Get the width of the region to shift right.  */
 33066   shifted_region_width = (window_box_width (w, updated_area)
 33067                           - w->output_cursor.x
 33068                           - shift_by_width);
 33069 
 33070   /* Shift right.  */
 33071   frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
 33072   frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
 33073 
 33074   FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
 33075                                           line_height, shift_by_width);
 33076 
 33077   /* Write the glyphs.  */
 33078   hpos = start - row->glyphs[updated_area];
 33079   draw_glyphs (w, w->output_cursor.x, row, updated_area,
 33080                hpos, hpos + len,
 33081                DRAW_NORMAL_TEXT, 0);
 33082 
 33083   /* Advance the output cursor.  */
 33084   w->output_cursor.hpos += len;
 33085   w->output_cursor.x += shift_by_width;
 33086   unblock_input ();
 33087 }
 33088 
 33089 
 33090 /* EXPORT for RIF:
 33091    Erase the current text line from the nominal cursor position
 33092    (inclusive) to pixel column TO_X (exclusive).  The idea is that
 33093    everything from TO_X onward is already erased.
 33094 
 33095    TO_X is a pixel position relative to UPDATED_AREA of currently
 33096    updated window W.  TO_X == -1 means clear to the end of this area.  */
 33097 
 33098 void
 33099 gui_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
 33100                        enum glyph_row_area updated_area, int to_x)
 33101 {
 33102   struct frame *f;
 33103   int max_x, min_y, max_y;
 33104   int from_x, from_y, to_y;
 33105   struct face *face;
 33106 
 33107   eassert (updated_row);
 33108   f = XFRAME (w->frame);
 33109   face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
 33110 
 33111   if (updated_row->full_width_p)
 33112     max_x = (WINDOW_PIXEL_WIDTH (w)
 33113              - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 33114   else
 33115     max_x = window_box_width (w, updated_area);
 33116   max_y = window_text_bottom_y (w);
 33117 
 33118   /* TO_X == 0 means don't do anything.  TO_X < 0 means clear to end
 33119      of window.  For TO_X > 0, truncate to end of drawing area.  */
 33120   if (to_x == 0)
 33121     return;
 33122   else if (to_x < 0)
 33123     to_x = max_x;
 33124   else
 33125     to_x = min (to_x, max_x);
 33126 
 33127   to_y = min (max_y, w->output_cursor.y + updated_row->height);
 33128 
 33129   /* Notice if the cursor will be cleared by this operation.  */
 33130   if (!updated_row->full_width_p)
 33131     notice_overwritten_cursor (w, updated_area,
 33132                                w->output_cursor.x, -1,
 33133                                updated_row->y,
 33134                                MATRIX_ROW_BOTTOM_Y (updated_row));
 33135 
 33136   from_x = w->output_cursor.x;
 33137 
 33138   /* Translate to frame coordinates.  */
 33139   if (updated_row->full_width_p)
 33140     {
 33141       from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
 33142       to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
 33143     }
 33144   else
 33145     {
 33146       int area_left = window_box_left (w, updated_area);
 33147       from_x += area_left;
 33148       to_x += area_left;
 33149     }
 33150 
 33151   min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 33152   from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
 33153   to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
 33154 
 33155   /* Prevent inadvertently clearing to end of the X window.  */
 33156   if (to_x > from_x && to_y > from_y)
 33157     {
 33158       block_input ();
 33159       FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
 33160                                        to_x - from_x, to_y - from_y);
 33161 
 33162       if (face && !updated_row->stipple_p)
 33163         updated_row->stipple_p = face->stipple;
 33164       unblock_input ();
 33165     }
 33166 }
 33167 
 33168 #endif /* HAVE_WINDOW_SYSTEM */
 33169 
 33170 
 33171 
 33172 /***********************************************************************
 33173                              Cursor types
 33174  ***********************************************************************/
 33175 
 33176 /* Value is the internal representation of the specified cursor type
 33177    ARG.  If type is BAR_CURSOR, return in *WIDTH the specified width
 33178    of the bar cursor.  */
 33179 
 33180 static enum text_cursor_kinds
 33181 get_specified_cursor_type (Lisp_Object arg, int *width)
 33182 {
 33183   enum text_cursor_kinds type;
 33184 
 33185   if (NILP (arg))
 33186     return NO_CURSOR;
 33187 
 33188   if (EQ (arg, Qbox))
 33189     return FILLED_BOX_CURSOR;
 33190 
 33191   if (EQ (arg, Qhollow))
 33192     return HOLLOW_BOX_CURSOR;
 33193 
 33194   if (EQ (arg, Qbar))
 33195     {
 33196       *width = 2;
 33197       return BAR_CURSOR;
 33198     }
 33199 
 33200   if (EQ (arg, Qhbar))
 33201     {
 33202       *width = 2;
 33203       return HBAR_CURSOR;
 33204     }
 33205 
 33206   if (CONSP (arg)
 33207       && RANGED_FIXNUMP (0, XCDR (arg), INT_MAX))
 33208     {
 33209       *width = XFIXNUM (XCDR (arg));
 33210 
 33211       if (EQ (XCAR (arg), Qbox))
 33212           return FILLED_BOX_CURSOR;
 33213       else if (EQ (XCAR (arg), Qbar))
 33214           return BAR_CURSOR;
 33215       else if (EQ (XCAR (arg), Qhbar))
 33216           return HBAR_CURSOR;
 33217     }
 33218 
 33219   /* Treat anything unknown as "hollow box cursor".
 33220      It was bad to signal an error; people have trouble fixing
 33221      .Xdefaults with Emacs, when it has something bad in it.  */
 33222   type = HOLLOW_BOX_CURSOR;
 33223 
 33224   return type;
 33225 }
 33226 
 33227 /* Set the default cursor types for specified frame.  */
 33228 void
 33229 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
 33230 {
 33231   int width = 1;
 33232   Lisp_Object tem;
 33233 
 33234   FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
 33235   FRAME_CURSOR_WIDTH (f) = width;
 33236 
 33237   /* By default, set up the blink-off state depending on the on-state.  */
 33238 
 33239   tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
 33240   if (!NILP (tem))
 33241     {
 33242       FRAME_BLINK_OFF_CURSOR (f)
 33243         = get_specified_cursor_type (XCDR (tem), &width);
 33244       FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
 33245     }
 33246   else
 33247     FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
 33248 
 33249   /* Make sure the cursor gets redrawn.  */
 33250   f->cursor_type_changed = true;
 33251 }
 33252 
 33253 
 33254 #ifdef HAVE_WINDOW_SYSTEM
 33255 
 33256 /* Return the cursor we want to be displayed in window W.  Return
 33257    width of bar/hbar cursor through WIDTH arg.  Return with
 33258    ACTIVE_CURSOR arg set to true if cursor in window W is `active'
 33259    (i.e. if the `system caret' should track this cursor).
 33260 
 33261    In a mini-buffer window, we want the cursor only to appear if we
 33262    are reading input from this window.  For the selected window, we
 33263    want the cursor type given by the frame parameter or buffer local
 33264    setting of cursor-type.  If explicitly marked off, draw no cursor.
 33265    In all other cases, we want a hollow box cursor.  */
 33266 
 33267 static enum text_cursor_kinds
 33268 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 33269                         bool *active_cursor)
 33270 {
 33271   struct frame *f = XFRAME (w->frame);
 33272   struct buffer *b = XBUFFER (w->contents);
 33273   int cursor_type = DEFAULT_CURSOR;
 33274   Lisp_Object alt_cursor;
 33275   bool non_selected = false;
 33276 
 33277   *active_cursor = true;
 33278 
 33279   /* Echo area */
 33280   if (cursor_in_echo_area
 33281       && FRAME_HAS_MINIBUF_P (f)
 33282       && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
 33283     {
 33284       if (w == XWINDOW (echo_area_window))
 33285         {
 33286           if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
 33287             {
 33288               *width = FRAME_CURSOR_WIDTH (f);
 33289               return FRAME_DESIRED_CURSOR (f);
 33290             }
 33291           else
 33292             return get_specified_cursor_type (BVAR (b, cursor_type), width);
 33293         }
 33294 
 33295       *active_cursor = false;
 33296       non_selected = true;
 33297     }
 33298 
 33299   /* Detect a nonselected window or nonselected frame.  */
 33300   else if (w != XWINDOW (f->selected_window)
 33301            || f != FRAME_DISPLAY_INFO (f)->highlight_frame)
 33302     {
 33303       *active_cursor = false;
 33304 
 33305       if (MINI_WINDOW_P (w) &&
 33306           (minibuf_level == 0
 33307            || is_minibuffer (0, w->contents)))
 33308         return NO_CURSOR;
 33309 
 33310       non_selected = true;
 33311     }
 33312 
 33313   /* Never display a cursor in a window in which cursor-type is nil.  */
 33314   if (NILP (BVAR (b, cursor_type)))
 33315     return NO_CURSOR;
 33316 
 33317   /* Get the normal cursor type for this window.  */
 33318   if (EQ (BVAR (b, cursor_type), Qt))
 33319     {
 33320       cursor_type = FRAME_DESIRED_CURSOR (f);
 33321       *width = FRAME_CURSOR_WIDTH (f);
 33322     }
 33323   else
 33324     cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
 33325 
 33326   /* Use cursor-in-non-selected-windows instead
 33327      for non-selected window or frame.  */
 33328   if (non_selected)
 33329     {
 33330       alt_cursor = BVAR (b, cursor_in_non_selected_windows);
 33331       if (!EQ (Qt, alt_cursor))
 33332         return get_specified_cursor_type (alt_cursor, width);
 33333       /* t means modify the normal cursor type.  */
 33334       if (cursor_type == FILLED_BOX_CURSOR)
 33335         cursor_type = HOLLOW_BOX_CURSOR;
 33336       else if (cursor_type == BAR_CURSOR && *width > 1)
 33337         --*width;
 33338       return cursor_type;
 33339     }
 33340 
 33341   /* Use normal cursor if not blinked off.  */
 33342   if (!w->cursor_off_p)
 33343     {
 33344       if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
 33345         return NO_CURSOR;
 33346       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 33347         {
 33348           if (cursor_type == FILLED_BOX_CURSOR)
 33349             {
 33350               /* Using a block cursor on large images can be very
 33351                  annoying.  So use a hollow cursor for "large" images.
 33352                  If image is not transparent (no mask), also use
 33353                  hollow cursor.  */
 33354               struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 33355               if (img != NULL && IMAGEP (img->spec))
 33356                 {
 33357                   /* Interpret "large" as >SIZExSIZE and >NxN where
 33358                      SIZE is the value from cursor-type of the form
 33359                      (box . SIZE), where N = size of default frame
 33360                      font size.  So, setting cursor-type to (box . 32)
 33361                      should cover most of the "tiny" icons people may
 33362                      use.  */
 33363                   if (!img->mask
 33364                       || (CONSP (BVAR (b, cursor_type))
 33365                           && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 33366                           && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 33367                     cursor_type = HOLLOW_BOX_CURSOR;
 33368                 }
 33369             }
 33370           else if (cursor_type != NO_CURSOR)
 33371             {
 33372               /* Display current only supports BOX and HOLLOW cursors for images.
 33373                  So for now, unconditionally use a HOLLOW cursor when cursor is
 33374                  not a solid box cursor.  */
 33375               cursor_type = HOLLOW_BOX_CURSOR;
 33376             }
 33377       }
 33378       return cursor_type;
 33379     }
 33380 
 33381   /* Cursor is blinked off, so determine how to "toggle" it.  */
 33382 
 33383   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
 33384   if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
 33385     return get_specified_cursor_type (XCDR (alt_cursor), width);
 33386 
 33387   /* Then see if frame has specified a specific blink off cursor type.  */
 33388   if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
 33389     {
 33390       *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
 33391       return FRAME_BLINK_OFF_CURSOR (f);
 33392     }
 33393 
 33394 #if false
 33395   /* Some people liked having a permanently visible blinking cursor,
 33396      while others had very strong opinions against it.  So it was
 33397      decided to remove it.  KFS 2003-09-03 */
 33398 
 33399   /* Finally perform built-in cursor blinking:
 33400        filled box      <->   hollow box
 33401        wide [h]bar     <->   narrow [h]bar
 33402        narrow [h]bar   <->   no cursor
 33403        other type      <->   no cursor  */
 33404 
 33405   if (cursor_type == FILLED_BOX_CURSOR)
 33406     return HOLLOW_BOX_CURSOR;
 33407 
 33408   if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
 33409     {
 33410       *width = 1;
 33411       return cursor_type;
 33412     }
 33413 #endif
 33414 
 33415   return NO_CURSOR;
 33416 }
 33417 
 33418 
 33419 /* Notice when the text cursor of window W has been completely
 33420    overwritten by a drawing operation that outputs glyphs in AREA
 33421    starting at X0 and ending at X1 in the line starting at Y0 and
 33422    ending at Y1.  X coordinates are area-relative.  X1 < 0 means all
 33423    the rest of the line after X0 has been written.  Y coordinates
 33424    are window-relative.  */
 33425 
 33426 static void
 33427 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
 33428                            int x0, int x1, int y0, int y1)
 33429 {
 33430   int cx0, cx1, cy0, cy1;
 33431   struct glyph_row *row;
 33432 
 33433   if (!w->phys_cursor_on_p)
 33434     return;
 33435   if (area != TEXT_AREA)
 33436     return;
 33437 
 33438   if (w->phys_cursor.vpos < 0
 33439       || w->phys_cursor.vpos >= w->current_matrix->nrows
 33440       || (row = w->current_matrix->rows + w->phys_cursor.vpos,
 33441           !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
 33442     return;
 33443 
 33444   if (row->cursor_in_fringe_p)
 33445     {
 33446       row->cursor_in_fringe_p = false;
 33447       draw_fringe_bitmap (w, row, row->reversed_p);
 33448       w->phys_cursor_on_p = false;
 33449       return;
 33450     }
 33451 
 33452   cx0 = w->phys_cursor.x;
 33453   cx1 = cx0 + w->phys_cursor_width;
 33454   if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
 33455     return;
 33456 
 33457   /* The cursor image will be completely removed from the
 33458      screen if the output area intersects the cursor area in
 33459      y-direction.  When we draw in [y0 y1[, and some part of
 33460      the cursor is at y < y0, that part must have been drawn
 33461      before.  When scrolling, the cursor is erased before
 33462      actually scrolling, so we don't come here.  When not
 33463      scrolling, the rows above the old cursor row must have
 33464      changed, and in this case these rows must have written
 33465      over the cursor image.
 33466 
 33467      Likewise if part of the cursor is below y1, with the
 33468      exception of the cursor being in the first blank row at
 33469      the buffer and window end because update_text_area
 33470      doesn't draw that row.  (Except when it does, but
 33471      that's handled in update_text_area.)  */
 33472 
 33473   cy0 = w->phys_cursor.y;
 33474   cy1 = cy0 + w->phys_cursor_height;
 33475   if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
 33476     return;
 33477 
 33478   w->phys_cursor_on_p = false;
 33479 }
 33480 
 33481 #endif /* HAVE_WINDOW_SYSTEM */
 33482 
 33483 
 33484 /************************************************************************
 33485                               Mouse Face
 33486  ************************************************************************/
 33487 
 33488 #ifdef HAVE_WINDOW_SYSTEM
 33489 
 33490 /* EXPORT for RIF:
 33491    Fix the display of area AREA of overlapping row ROW in window W
 33492    with respect to the overlapping part OVERLAPS.  */
 33493 
 33494 void
 33495 gui_fix_overlapping_area (struct window *w, struct glyph_row *row,
 33496                         enum glyph_row_area area, int overlaps)
 33497 {
 33498   int i, x;
 33499 
 33500   block_input ();
 33501 
 33502   x = 0;
 33503   for (i = 0; i < row->used[area];)
 33504     {
 33505       if (row->glyphs[area][i].overlaps_vertically_p)
 33506         {
 33507           int start = i, start_x = x;
 33508 
 33509           do
 33510             {
 33511               x += row->glyphs[area][i].pixel_width;
 33512               ++i;
 33513             }
 33514           while (i < row->used[area]
 33515                  && row->glyphs[area][i].overlaps_vertically_p);
 33516 
 33517           draw_glyphs (w, start_x, row, area,
 33518                        start, i,
 33519                        DRAW_NORMAL_TEXT, overlaps);
 33520         }
 33521       else
 33522         {
 33523           x += row->glyphs[area][i].pixel_width;
 33524           ++i;
 33525         }
 33526     }
 33527 
 33528   unblock_input ();
 33529 }
 33530 
 33531 
 33532 /* EXPORT:
 33533    Draw the cursor glyph of window W in glyph row ROW.  See the
 33534    comment of draw_glyphs for the meaning of HL.  */
 33535 
 33536 void
 33537 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
 33538                         enum draw_glyphs_face hl)
 33539 {
 33540   /* If cursor hpos is out of bounds, don't draw garbage.  This can
 33541      happen in mini-buffer windows when switching between echo area
 33542      glyphs and mini-buffer.  */
 33543   if ((row->reversed_p
 33544        ? (w->phys_cursor.hpos >= 0)
 33545        : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
 33546     {
 33547       bool on_p = w->phys_cursor_on_p;
 33548       int x1;
 33549       int hpos = w->phys_cursor.hpos;
 33550 
 33551       /* When the window is hscrolled, cursor hpos can legitimately be
 33552          out of bounds, but we draw the cursor at the corresponding
 33553          window margin in that case.  */
 33554       if (!row->reversed_p && hpos < 0)
 33555         hpos = 0;
 33556       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33557         hpos = row->used[TEXT_AREA] - 1;
 33558 
 33559       x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
 33560                         hl, 0);
 33561       w->phys_cursor_on_p = on_p;
 33562 
 33563       if (hl == DRAW_CURSOR)
 33564         w->phys_cursor_width = x1 - w->phys_cursor.x;
 33565       /* When we erase the cursor, and ROW is overlapped by other
 33566          rows, make sure that these overlapping parts of other rows
 33567          are redrawn.  */
 33568       else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
 33569         {
 33570           w->phys_cursor_width = x1 - w->phys_cursor.x;
 33571 
 33572           if (row > w->current_matrix->rows
 33573               && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
 33574             gui_fix_overlapping_area (w, row - 1, TEXT_AREA,
 33575                                     OVERLAPS_ERASED_CURSOR);
 33576 
 33577           if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
 33578               && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
 33579             gui_fix_overlapping_area (w, row + 1, TEXT_AREA,
 33580                                     OVERLAPS_ERASED_CURSOR);
 33581         }
 33582     }
 33583 }
 33584 
 33585 
 33586 /* Erase the image of a cursor of window W from the screen.  */
 33587 
 33588 void
 33589 erase_phys_cursor (struct window *w)
 33590 {
 33591   struct frame *f = XFRAME (w->frame);
 33592   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 33593   int hpos = w->phys_cursor.hpos;
 33594   int vpos = w->phys_cursor.vpos;
 33595   bool mouse_face_here_p = false;
 33596   struct glyph_matrix *active_glyphs = w->current_matrix;
 33597   struct glyph_row *cursor_row;
 33598   struct glyph *cursor_glyph;
 33599   enum draw_glyphs_face hl;
 33600 
 33601   /* No cursor displayed or row invalidated => nothing to do on the
 33602      screen.  */
 33603   if (w->phys_cursor_type == NO_CURSOR)
 33604     goto mark_cursor_off;
 33605 
 33606   /* VPOS >= active_glyphs->nrows means that window has been resized.
 33607      Don't bother to erase the cursor.  */
 33608   if (vpos >= active_glyphs->nrows)
 33609     goto mark_cursor_off;
 33610 
 33611   /* If row containing cursor is marked invalid, there is nothing we
 33612      can do.  */
 33613   cursor_row = MATRIX_ROW (active_glyphs, vpos);
 33614   if (!cursor_row->enabled_p)
 33615     goto mark_cursor_off;
 33616 
 33617   /* If line spacing is > 0, old cursor may only be partially visible in
 33618      window after split-window.  So adjust visible height.  */
 33619   cursor_row->visible_height = min (cursor_row->visible_height,
 33620                                     window_text_bottom_y (w) - cursor_row->y);
 33621 
 33622   /* If row is completely invisible, don't attempt to delete a cursor which
 33623      isn't there.  This can happen if cursor is at top of a window, and
 33624      we switch to a buffer with a header line in that window.  */
 33625   if (cursor_row->visible_height <= 0)
 33626     goto mark_cursor_off;
 33627 
 33628   /* If cursor is in the fringe, erase by drawing actual bitmap there.  */
 33629   if (cursor_row->cursor_in_fringe_p)
 33630     {
 33631       cursor_row->cursor_in_fringe_p = false;
 33632       draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
 33633       goto mark_cursor_off;
 33634     }
 33635 
 33636   /* This can happen when the new row is shorter than the old one.
 33637      In this case, either draw_glyphs or clear_end_of_line
 33638      should have cleared the cursor.  Note that we wouldn't be
 33639      able to erase the cursor in this case because we don't have a
 33640      cursor glyph at hand.  */
 33641   if ((cursor_row->reversed_p
 33642        ? (w->phys_cursor.hpos < 0)
 33643        : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
 33644     goto mark_cursor_off;
 33645 
 33646   /* When the window is hscrolled, cursor hpos can legitimately be out
 33647      of bounds, but we draw the cursor at the corresponding window
 33648      margin in that case.  */
 33649   if (!cursor_row->reversed_p && hpos < 0)
 33650     hpos = 0;
 33651   if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
 33652     hpos = cursor_row->used[TEXT_AREA] - 1;
 33653 
 33654   /* If the cursor is in the mouse face area, redisplay that when
 33655      we clear the cursor.  */
 33656   if (! NILP (hlinfo->mouse_face_window)
 33657       && coords_in_mouse_face_p (w, hpos, vpos)
 33658       /* Don't redraw the cursor's spot in mouse face if it is at the
 33659          end of a line (on a newline).  The cursor appears there, but
 33660          mouse highlighting does not.  */
 33661       && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
 33662     mouse_face_here_p = true;
 33663 
 33664 #ifdef HAVE_WINDOW_SYSTEM
 33665   /* Since erasing the phys cursor will probably lead to corruption of
 33666      the mouse face display if the glyph's pixel_width is not kept up
 33667      to date with the :box property of the mouse face, just redraw the
 33668      mouse face.  */
 33669   if (FRAME_WINDOW_P (WINDOW_XFRAME (w)) && mouse_face_here_p)
 33670     {
 33671       w->phys_cursor_on_p = false;
 33672       w->phys_cursor_type = NO_CURSOR;
 33673       show_mouse_face (MOUSE_HL_INFO (WINDOW_XFRAME (w)), DRAW_MOUSE_FACE);
 33674       return;
 33675     }
 33676 #endif
 33677 
 33678   /* Maybe clear the display under the cursor.  */
 33679   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
 33680     {
 33681       int x, y;
 33682       int tab_line_height = WINDOW_TAB_LINE_HEIGHT (w);
 33683       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
 33684       int width;
 33685 
 33686       cursor_glyph = get_phys_cursor_glyph (w);
 33687       if (cursor_glyph == NULL)
 33688         goto mark_cursor_off;
 33689 
 33690       width = cursor_glyph->pixel_width;
 33691       x = w->phys_cursor.x;
 33692       if (x < 0)
 33693         {
 33694           width += x;
 33695           x = 0;
 33696         }
 33697       width = min (width, window_box_width (w, TEXT_AREA) - x);
 33698       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (tab_line_height, max (header_line_height, cursor_row->y)));
 33699       x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 33700 
 33701       if (width > 0)
 33702         FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
 33703     }
 33704 
 33705   /* Erase the cursor by redrawing the character underneath it.  */
 33706   if (mouse_face_here_p)
 33707     hl = DRAW_MOUSE_FACE;
 33708   else
 33709     hl = DRAW_NORMAL_TEXT;
 33710   draw_phys_cursor_glyph (w, cursor_row, hl);
 33711 
 33712  mark_cursor_off:
 33713   w->phys_cursor_on_p = false;
 33714   w->phys_cursor_type = NO_CURSOR;
 33715 }
 33716 
 33717 
 33718 /* Display or clear cursor of window W.  If !ON, clear the cursor.
 33719    If ON, display the cursor; where to put the cursor is specified by
 33720    HPOS, VPOS, X and Y.  */
 33721 
 33722 void
 33723 display_and_set_cursor (struct window *w, bool on,
 33724                         int hpos, int vpos, int x, int y)
 33725 {
 33726   struct frame *f = XFRAME (w->frame);
 33727   int new_cursor_type;
 33728   int new_cursor_width UNINIT;
 33729   bool active_cursor;
 33730   struct glyph_row *glyph_row;
 33731   struct glyph *glyph;
 33732 
 33733   /* This is pointless on invisible frames, and dangerous on garbaged
 33734      windows and frames; in the latter case, the frame or window may
 33735      be in the midst of changing its size, and x and y may be off the
 33736      window.  */
 33737   if (! FRAME_REDISPLAY_P (f)
 33738       || vpos >= w->current_matrix->nrows
 33739       || hpos >= w->current_matrix->matrix_w)
 33740     return;
 33741 
 33742   /* If cursor is off and we want it off, return quickly.  */
 33743   if (!on && !w->phys_cursor_on_p)
 33744     return;
 33745 
 33746   glyph_row = MATRIX_ROW (w->current_matrix, vpos);
 33747   /* If cursor row is not enabled, we don't really know where to
 33748      display the cursor.  */
 33749   if (!glyph_row->enabled_p)
 33750     {
 33751       w->phys_cursor_on_p = false;
 33752       return;
 33753     }
 33754 
 33755   /* A frame might be marked garbaged even though its cursor position
 33756      is correct, and will not change upon subsequent redisplay.  This
 33757      happens in some rare situations, like toggling the sort order in
 33758      Dired windows.  We've already established that VPOS is valid, so
 33759      it shouldn't do any harm to record the cursor position, as we are
 33760      going to return without acting on it anyway.  Otherwise, expose
 33761      events might come in and call update_window_cursor, which will
 33762      blindly use outdated values in w->phys_cursor.  */
 33763   if (FRAME_GARBAGED_P (f))
 33764     {
 33765       if (on)
 33766         {
 33767           w->phys_cursor.x = x;
 33768           w->phys_cursor.y = glyph_row->y;
 33769           w->phys_cursor.hpos = hpos;
 33770           w->phys_cursor.vpos = vpos;
 33771         }
 33772       return;
 33773     }
 33774 
 33775   glyph = NULL;
 33776   if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
 33777     glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
 33778 
 33779   eassert (input_blocked_p ());
 33780 
 33781   /* Set new_cursor_type to the cursor we want to be displayed.  */
 33782   new_cursor_type = get_window_cursor_type (w, glyph,
 33783                                             &new_cursor_width, &active_cursor);
 33784 
 33785   /* If cursor is currently being shown and we don't want it to be or
 33786      it is in the wrong place, or the cursor type is not what we want,
 33787      erase it.  */
 33788   if (w->phys_cursor_on_p
 33789       && (!on
 33790           || w->phys_cursor.x != x
 33791           || w->phys_cursor.y != y
 33792           /* HPOS can be negative in R2L rows whose
 33793              exact_window_width_line_p flag is set (i.e. their newline
 33794              would "overflow into the fringe").  */
 33795           || hpos < 0
 33796           || new_cursor_type != w->phys_cursor_type
 33797           || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
 33798               && new_cursor_width != w->phys_cursor_width)))
 33799     erase_phys_cursor (w);
 33800 
 33801   /* Don't check phys_cursor_on_p here because that flag is only set
 33802      to false in some cases where we know that the cursor has been
 33803      completely erased, to avoid the extra work of erasing the cursor
 33804      twice.  In other words, phys_cursor_on_p can be true and the cursor
 33805      still not be visible, or it has only been partly erased.  */
 33806 
 33807   if (on)
 33808     {
 33809       w->phys_cursor_ascent = glyph_row->ascent;
 33810       w->phys_cursor_height = glyph_row->height;
 33811 
 33812       /* Set phys_cursor_.* before x_draw_.* is called because some
 33813          of them may need the information.  */
 33814       w->phys_cursor.x = x;
 33815       w->phys_cursor.y = glyph_row->y;
 33816       w->phys_cursor.hpos = hpos;
 33817       w->phys_cursor.vpos = vpos;
 33818     }
 33819 
 33820   /* If make_cursor_line_fully_visible is nil and the row is in fact
 33821      vscrolled out of the window, then glyph_row->y +
 33822      glyph_row->height will be less than or equal to 0.  Eschew
 33823      displaying the cursor in that case.  */
 33824 
 33825   if (MATRIX_ROW_BOTTOM_Y (glyph_row) > 0)
 33826     FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
 33827                                        new_cursor_type, new_cursor_width,
 33828                                        on, active_cursor);
 33829 }
 33830 
 33831 
 33832 /* Switch the display of W's cursor on or off, according to the value
 33833    of ON.  */
 33834 
 33835 static void
 33836 update_window_cursor (struct window *w, bool on)
 33837 {
 33838   /* Don't update cursor in windows whose frame is in the process
 33839      of being deleted.  */
 33840   if (w->current_matrix)
 33841     {
 33842       int hpos = w->phys_cursor.hpos;
 33843       int vpos = w->phys_cursor.vpos;
 33844       struct glyph_row *row;
 33845 
 33846       if (vpos >= w->current_matrix->nrows
 33847           || hpos >= w->current_matrix->matrix_w)
 33848         return;
 33849 
 33850       row = MATRIX_ROW (w->current_matrix, vpos);
 33851 
 33852       /* When the window is hscrolled, cursor hpos can legitimately be
 33853          out of bounds, but we draw the cursor at the corresponding
 33854          window margin in that case.  */
 33855       if (!row->reversed_p && hpos < 0)
 33856         hpos = 0;
 33857       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33858         hpos = row->used[TEXT_AREA] - 1;
 33859 
 33860       block_input ();
 33861       display_and_set_cursor (w, on, hpos, vpos,
 33862                               w->phys_cursor.x, w->phys_cursor.y);
 33863       unblock_input ();
 33864     }
 33865 }
 33866 
 33867 
 33868 /* Call update_window_cursor with parameter ON_P on all leaf windows
 33869    in the window tree rooted at W.  */
 33870 
 33871 static void
 33872 update_cursor_in_window_tree (struct window *w, bool on_p)
 33873 {
 33874   while (w)
 33875     {
 33876       if (WINDOWP (w->contents))
 33877         update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
 33878       else
 33879         update_window_cursor (w, on_p);
 33880 
 33881       w = NILP (w->next) ? 0 : XWINDOW (w->next);
 33882     }
 33883 }
 33884 
 33885 
 33886 /* EXPORT:
 33887    Display the cursor on window W, or clear it, according to ON_P.
 33888    Don't change the cursor's position.  */
 33889 
 33890 void
 33891 gui_update_cursor (struct frame *f, bool on_p)
 33892 {
 33893   update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
 33894 }
 33895 
 33896 
 33897 /* EXPORT:
 33898    Clear the cursor of window W to background color, and mark the
 33899    cursor as not shown.  This is used when the text where the cursor
 33900    is about to be rewritten.  */
 33901 
 33902 void
 33903 gui_clear_cursor (struct window *w)
 33904 {
 33905   if (FRAME_REDISPLAY_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
 33906     update_window_cursor (w, false);
 33907 }
 33908 
 33909 #endif /* HAVE_WINDOW_SYSTEM */
 33910 
 33911 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
 33912    and MSDOS.  */
 33913 static void
 33914 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
 33915                           int start_hpos, int end_hpos,
 33916                           enum draw_glyphs_face draw)
 33917 {
 33918 #ifdef HAVE_WINDOW_SYSTEM
 33919   if (FRAME_WINDOW_P (XFRAME (w->frame)))
 33920     {
 33921       draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
 33922       return;
 33923     }
 33924 #endif
 33925 
 33926 #ifndef HAVE_ANDROID
 33927   tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
 33928 #endif
 33929 }
 33930 
 33931 /* Display the active region described by mouse_face_* according to DRAW.  */
 33932 
 33933 static void
 33934 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 33935 {
 33936   /* Don't bother doing anything if the mouse-face window is not set
 33937      up.  */
 33938   if (!WINDOWP (hlinfo->mouse_face_window))
 33939     return;
 33940 
 33941   struct window *w = XWINDOW (hlinfo->mouse_face_window);
 33942   struct frame *f = XFRAME (WINDOW_FRAME (w));
 33943 
 33944   /* Don't bother doing anything if we are on a wrong frame.  */
 33945   if (f != hlinfo->mouse_face_mouse_frame)
 33946     return;
 33947 
 33948   if (/* If window is in the process of being destroyed, don't bother
 33949          to do anything.  */
 33950       w->current_matrix != NULL
 33951       /* Don't update mouse highlight if hidden.  */
 33952       && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
 33953       /* Recognize when we are called to operate on rows that don't exist
 33954          anymore.  This can happen when a window is split.  */
 33955       && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
 33956     {
 33957       bool phys_cursor_on_p = w->phys_cursor_on_p;
 33958 #ifdef HAVE_WINDOW_SYSTEM
 33959       int mouse_off = 0;
 33960 #endif
 33961       struct glyph_row *row, *first, *last;
 33962 
 33963       first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
 33964       last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
 33965 
 33966       for (row = first; row <= last && row->enabled_p; ++row)
 33967         {
 33968           int start_hpos, end_hpos, start_x;
 33969 
 33970           /* For all but the first row, the highlight starts at column 0.  */
 33971           if (row == first)
 33972             {
 33973               /* R2L rows have BEG and END in reversed order, but the
 33974                  screen drawing geometry is always left to right.  So
 33975                  we need to mirror the beginning and end of the
 33976                  highlighted area in R2L rows.  */
 33977               if (!row->reversed_p)
 33978                 {
 33979                   start_hpos = hlinfo->mouse_face_beg_col;
 33980                   start_x = hlinfo->mouse_face_beg_x;
 33981                 }
 33982               else if (row == last)
 33983                 {
 33984                   start_hpos = hlinfo->mouse_face_end_col;
 33985                   start_x = hlinfo->mouse_face_end_x;
 33986                 }
 33987               else
 33988                 {
 33989                   start_hpos = 0;
 33990                   start_x = 0;
 33991                 }
 33992             }
 33993           else if (row->reversed_p && row == last)
 33994             {
 33995               start_hpos = hlinfo->mouse_face_end_col;
 33996               start_x = hlinfo->mouse_face_end_x;
 33997             }
 33998           else
 33999             {
 34000               start_hpos = 0;
 34001               start_x = 0;
 34002             }
 34003 
 34004           if (row == last)
 34005             {
 34006               if (!row->reversed_p)
 34007                 end_hpos = hlinfo->mouse_face_end_col;
 34008               else if (row == first)
 34009                 end_hpos = hlinfo->mouse_face_beg_col;
 34010               else
 34011                 {
 34012                   end_hpos = row->used[TEXT_AREA];
 34013                   if (draw == DRAW_NORMAL_TEXT)
 34014                     row->fill_line_p = true; /* Clear to end of line.  */
 34015                 }
 34016             }
 34017           else if (row->reversed_p && row == first)
 34018             end_hpos = hlinfo->mouse_face_beg_col;
 34019           else
 34020             {
 34021               end_hpos = row->used[TEXT_AREA];
 34022               if (draw == DRAW_NORMAL_TEXT)
 34023                 row->fill_line_p = true; /* Clear to end of line.  */
 34024             }
 34025 
 34026           if (end_hpos > start_hpos)
 34027             {
 34028               draw_row_with_mouse_face (w, start_x, row,
 34029                                         start_hpos, end_hpos, draw);
 34030 
 34031               row->mouse_face_p
 34032                 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
 34033             }
 34034 #ifdef HAVE_WINDOW_SYSTEM
 34035           /* Compute the cursor offset due to mouse-highlight.  */
 34036           if ((MATRIX_ROW_VPOS (row, w->current_matrix) == w->phys_cursor.vpos)
 34037               /* But not when highlighting a pseudo window, such as
 34038                  the toolbar, which can't have a cursor anyway.  */
 34039               && !w->pseudo_window_p
 34040               && draw == DRAW_MOUSE_FACE)
 34041             get_cursor_offset_for_mouse_face (w, row, &mouse_off);
 34042 #endif
 34043         }
 34044 
 34045       /* When we've written over the cursor, arrange for it to
 34046          be displayed again.  */
 34047       if (FRAME_WINDOW_P (f)
 34048           && phys_cursor_on_p && !w->phys_cursor_on_p)
 34049         {
 34050 #ifdef HAVE_WINDOW_SYSTEM
 34051           int hpos = w->phys_cursor.hpos;
 34052           int old_phys_cursor_x = w->phys_cursor.x;
 34053 
 34054           /* When the window is hscrolled, cursor hpos can legitimately be
 34055              out of bounds, but we draw the cursor at the corresponding
 34056              window margin in that case.  */
 34057           if (!row->reversed_p && hpos < 0)
 34058             hpos = 0;
 34059           if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 34060             hpos = row->used[TEXT_AREA] - 1;
 34061 
 34062           block_input ();
 34063           display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
 34064                                   w->phys_cursor.x + mouse_off,
 34065                                   w->phys_cursor.y);
 34066           /* Restore the original cursor coordinates, perhaps modified
 34067              to account for mouse-highlight.  */
 34068           w->phys_cursor.x = old_phys_cursor_x;
 34069           unblock_input ();
 34070 #endif  /* HAVE_WINDOW_SYSTEM */
 34071         }
 34072     }
 34073 
 34074 #ifdef HAVE_WINDOW_SYSTEM
 34075   /* Change the mouse cursor.  */
 34076   if (FRAME_WINDOW_P (f) && NILP (track_mouse))
 34077     {
 34078       if (draw == DRAW_NORMAL_TEXT
 34079 #ifndef HAVE_EXT_TOOL_BAR
 34080           && !EQ (hlinfo->mouse_face_window, f->tool_bar_window)
 34081 #endif
 34082           && !EQ (hlinfo->mouse_face_window, f->tab_bar_window))
 34083         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->text_cursor);
 34084       else
 34085       if (draw == DRAW_MOUSE_FACE)
 34086         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->hand_cursor);
 34087       else
 34088         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->nontext_cursor);
 34089     }
 34090 #endif  /* HAVE_WINDOW_SYSTEM */
 34091 }
 34092 
 34093 /* EXPORT:
 34094    Clear out the mouse-highlighted active region.
 34095    Redraw it un-highlighted first.  Value is true if mouse
 34096    face was actually drawn unhighlighted.  */
 34097 
 34098 bool
 34099 clear_mouse_face (Mouse_HLInfo *hlinfo)
 34100 {
 34101   bool cleared
 34102     = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
 34103   if (cleared)
 34104     show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
 34105   hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
 34106   hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
 34107   hlinfo->mouse_face_window = Qnil;
 34108   hlinfo->mouse_face_overlay = Qnil;
 34109   return cleared;
 34110 }
 34111 
 34112 /* Return true if the coordinates HPOS and VPOS on windows W are
 34113    within the mouse face on that window.  */
 34114 static bool
 34115 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
 34116 {
 34117   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 34118 
 34119   /* Quickly resolve the easy cases.  */
 34120   if (!(WINDOWP (hlinfo->mouse_face_window)
 34121         && XWINDOW (hlinfo->mouse_face_window) == w))
 34122     return false;
 34123   if (vpos < hlinfo->mouse_face_beg_row
 34124       || vpos > hlinfo->mouse_face_end_row)
 34125     return false;
 34126   if (vpos > hlinfo->mouse_face_beg_row
 34127       && vpos < hlinfo->mouse_face_end_row)
 34128     return true;
 34129 
 34130   if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
 34131     {
 34132       if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 34133         {
 34134           if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
 34135             return true;
 34136         }
 34137       else if ((vpos == hlinfo->mouse_face_beg_row
 34138                 && hpos >= hlinfo->mouse_face_beg_col)
 34139                || (vpos == hlinfo->mouse_face_end_row
 34140                    && hpos < hlinfo->mouse_face_end_col))
 34141         return true;
 34142     }
 34143   else
 34144     {
 34145        if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 34146         {
 34147           if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
 34148             return true;
 34149         }
 34150       else if ((vpos == hlinfo->mouse_face_beg_row
 34151                 && hpos <= hlinfo->mouse_face_beg_col)
 34152                || (vpos == hlinfo->mouse_face_end_row
 34153                    && hpos > hlinfo->mouse_face_end_col))
 34154         return true;
 34155     }
 34156   return false;
 34157 }
 34158 
 34159 
 34160 /* EXPORT:
 34161    True if physical cursor of window W is within mouse face.  */
 34162 
 34163 bool
 34164 cursor_in_mouse_face_p (struct window *w)
 34165 {
 34166   int vpos = w->phys_cursor.vpos;
 34167 
 34168   /* If the cursor is outside the matrix glyph rows, it cannot be
 34169      within the mouse face.  */
 34170   if (!(0 <= vpos && vpos < w->current_matrix->nrows))
 34171     return false;
 34172 
 34173   int hpos = w->phys_cursor.hpos;
 34174   struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
 34175 
 34176   /* When the window is hscrolled, cursor hpos can legitimately be out
 34177      of bounds, but we draw the cursor at the corresponding window
 34178      margin in that case.  */
 34179   if (!row->reversed_p && hpos < 0)
 34180     hpos = 0;
 34181   if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 34182     hpos = row->used[TEXT_AREA] - 1;
 34183 
 34184   return coords_in_mouse_face_p (w, hpos, vpos);
 34185 }
 34186 
 34187 
 34188 
 34189 /* Find the glyph rows START_ROW and END_ROW of window W that display
 34190    characters between buffer positions START_CHARPOS and END_CHARPOS
 34191    (excluding END_CHARPOS).  DISP_STRING is a display string that
 34192    covers these buffer positions.  This is similar to
 34193    row_containing_pos, but is more accurate when bidi reordering makes
 34194    buffer positions change non-linearly with glyph rows.  */
 34195 static void
 34196 rows_from_pos_range (struct window *w,
 34197                      ptrdiff_t start_charpos, ptrdiff_t end_charpos,
 34198                      Lisp_Object disp_string,
 34199                      struct glyph_row **start, struct glyph_row **end)
 34200 {
 34201   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34202   int last_y = window_text_bottom_y (w);
 34203   struct glyph_row *row;
 34204 
 34205   *start = NULL;
 34206   *end = NULL;
 34207 
 34208   while (!first->enabled_p
 34209          && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
 34210     first++;
 34211 
 34212   /* Find the START row.  */
 34213   for (row = first;
 34214        row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
 34215        row++)
 34216     {
 34217       /* A row can potentially be the START row if the range of the
 34218          characters it displays intersects the range
 34219          [START_CHARPOS..END_CHARPOS).  */
 34220       if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
 34221               && end_charpos < MATRIX_ROW_START_CHARPOS (row))
 34222              /* See the commentary in row_containing_pos, for the
 34223                 explanation of the complicated way to check whether
 34224                 some position is beyond the end of the characters
 34225                 displayed by a row.  */
 34226              || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
 34227                   || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
 34228                       && !row->ends_at_zv_p
 34229                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 34230                  && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
 34231                      || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
 34232                          && !row->ends_at_zv_p
 34233                          && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
 34234         {
 34235           /* Found a candidate row.  Now make sure at least one of the
 34236              glyphs it displays has a charpos from the range
 34237              [START_CHARPOS..END_CHARPOS).
 34238 
 34239              This is not obvious because bidi reordering could make
 34240              buffer positions of a row be 1,2,3,102,101,100, and if we
 34241              want to highlight characters in [50..60), we don't want
 34242              this row, even though [50..60) does intersect [1..103),
 34243              the range of character positions given by the row's start
 34244              and end positions.  */
 34245           struct glyph *g = row->glyphs[TEXT_AREA];
 34246           struct glyph *e = g + row->used[TEXT_AREA];
 34247 
 34248           while (g < e)
 34249             {
 34250               if (((BUFFERP (g->object) || NILP (g->object))
 34251                    && start_charpos <= g->charpos && g->charpos < end_charpos)
 34252                   /* A glyph that comes from DISP_STRING is by
 34253                      definition to be highlighted.  */
 34254                   || EQ (g->object, disp_string))
 34255                 *start = row;
 34256               g++;
 34257             }
 34258           if (*start)
 34259             break;
 34260         }
 34261     }
 34262 
 34263   /* Find the END row.  */
 34264   if (!*start
 34265       /* If the last row is partially visible, start looking for END
 34266          from that row, instead of starting from FIRST.  */
 34267       && !(row->enabled_p
 34268            && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
 34269     row = first;
 34270   for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
 34271     {
 34272       struct glyph_row *next = row + 1;
 34273       ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
 34274 
 34275       if (!next->enabled_p
 34276           || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
 34277           /* The first row >= START whose range of displayed characters
 34278              does NOT intersect the range [START_CHARPOS..END_CHARPOS]
 34279              is the row END + 1.  */
 34280           || (start_charpos < next_start
 34281               && end_charpos < next_start)
 34282           || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
 34283                || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
 34284                    && !next->ends_at_zv_p
 34285                    && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
 34286               && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
 34287                   || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
 34288                       && !next->ends_at_zv_p
 34289                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
 34290         {
 34291           *end = row;
 34292           break;
 34293         }
 34294       else
 34295         {
 34296           /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
 34297              but none of the characters it displays are in the range, it is
 34298              also END + 1. */
 34299           struct glyph *g = next->glyphs[TEXT_AREA];
 34300           struct glyph *s = g;
 34301           struct glyph *e = g + next->used[TEXT_AREA];
 34302 
 34303           while (g < e)
 34304             {
 34305               if (((BUFFERP (g->object) || NILP (g->object))
 34306                    && ((start_charpos <= g->charpos && g->charpos < end_charpos)
 34307                        /* If the buffer position of the first glyph in
 34308                           the row is equal to END_CHARPOS, it means
 34309                           the last character to be highlighted is the
 34310                           newline of ROW, and we must consider NEXT as
 34311                           END, not END+1.  */
 34312                        || (((!next->reversed_p && g == s)
 34313                             || (next->reversed_p && g == e - 1))
 34314                            && (g->charpos == end_charpos
 34315                                /* Special case for when NEXT is an
 34316                                   empty line at ZV.  */
 34317                                || (g->charpos == -1
 34318                                    && !row->ends_at_zv_p
 34319                                    && next_start == end_charpos)))))
 34320                   /* A glyph that comes from DISP_STRING is by
 34321                      definition to be highlighted.  */
 34322                   || EQ (g->object, disp_string))
 34323                 break;
 34324               g++;
 34325             }
 34326           if (g == e)
 34327             {
 34328               *end = row;
 34329               break;
 34330             }
 34331           /* The first row that ends at ZV must be the last to be
 34332              highlighted.  */
 34333           else if (next->ends_at_zv_p)
 34334             {
 34335               *end = next;
 34336               break;
 34337             }
 34338         }
 34339     }
 34340 }
 34341 
 34342 /* This function sets the mouse_face_* elements of HLINFO, assuming
 34343    the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
 34344    window WINDOW.  START_CHARPOS and END_CHARPOS are buffer positions
 34345    for the overlay or run of text properties specifying the mouse
 34346    face.  BEFORE_STRING and AFTER_STRING, if non-nil, are a
 34347    before-string and after-string that must also be highlighted.
 34348    DISP_STRING, if non-nil, is a display string that may cover some
 34349    or all of the highlighted text.  */
 34350 
 34351 static void
 34352 mouse_face_from_buffer_pos (Lisp_Object window,
 34353                             Mouse_HLInfo *hlinfo,
 34354                             ptrdiff_t mouse_charpos,
 34355                             ptrdiff_t start_charpos,
 34356                             ptrdiff_t end_charpos,
 34357                             Lisp_Object before_string,
 34358                             Lisp_Object after_string,
 34359                             Lisp_Object disp_string)
 34360 {
 34361   struct window *w = XWINDOW (window);
 34362   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34363   struct glyph_row *r1, *r2;
 34364   struct glyph *glyph, *end;
 34365   ptrdiff_t ignore, pos;
 34366   int x;
 34367 
 34368   eassert (NILP (disp_string) || STRINGP (disp_string));
 34369   eassert (NILP (before_string) || STRINGP (before_string));
 34370   eassert (NILP (after_string) || STRINGP (after_string));
 34371 
 34372   /* Find the rows corresponding to START_CHARPOS and END_CHARPOS.  */
 34373   rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
 34374   if (r1 == NULL)
 34375     r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34376   /* If the before-string or display-string contains newlines,
 34377      rows_from_pos_range skips to its last row.  Move back.  */
 34378   if (!NILP (before_string) || !NILP (disp_string))
 34379     {
 34380       struct glyph_row *prev;
 34381       while ((prev = r1 - 1, prev >= first)
 34382              && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
 34383              && prev->used[TEXT_AREA] > 0)
 34384         {
 34385           struct glyph *beg = prev->glyphs[TEXT_AREA];
 34386           glyph = beg + prev->used[TEXT_AREA];
 34387           while (--glyph >= beg && NILP (glyph->object));
 34388           if (glyph < beg
 34389               || !(EQ (glyph->object, before_string)
 34390                    || EQ (glyph->object, disp_string)))
 34391             break;
 34392           r1 = prev;
 34393         }
 34394     }
 34395   if (r2 == NULL)
 34396     {
 34397       r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34398       hlinfo->mouse_face_past_end = true;
 34399     }
 34400   else if (!NILP (after_string))
 34401     {
 34402       /* If the after-string has newlines, advance to its last row.  */
 34403       struct glyph_row *next;
 34404       struct glyph_row *last
 34405         = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34406 
 34407       for (next = r2 + 1;
 34408            next <= last
 34409              && next->used[TEXT_AREA] > 0
 34410              && EQ (next->glyphs[TEXT_AREA]->object, after_string);
 34411            ++next)
 34412         r2 = next;
 34413     }
 34414   /* The rest of the display engine assumes that mouse_face_beg_row is
 34415      either above mouse_face_end_row or identical to it.  But with
 34416      bidi-reordered continued lines, the row for START_CHARPOS could
 34417      be below the row for END_CHARPOS.  If so, swap the rows and store
 34418      them in correct order.  */
 34419   if (r1->y > r2->y)
 34420     {
 34421       struct glyph_row *tem = r2;
 34422 
 34423       r2 = r1;
 34424       r1 = tem;
 34425     }
 34426 
 34427   hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
 34428   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
 34429 
 34430   /* For a bidi-reordered row, the positions of BEFORE_STRING,
 34431      AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
 34432      could be anywhere in the row and in any order.  The strategy
 34433      below is to find the leftmost and the rightmost glyph that
 34434      belongs to either of these 3 strings, or whose position is
 34435      between START_CHARPOS and END_CHARPOS, and highlight all the
 34436      glyphs between those two.  This may cover more than just the text
 34437      between START_CHARPOS and END_CHARPOS if the range of characters
 34438      strides the bidi level boundary, e.g. if the beginning is in R2L
 34439      text while the end is in L2R text or vice versa.  */
 34440   if (!r1->reversed_p)
 34441     {
 34442       /* This row is in a left to right paragraph.  Scan it left to
 34443          right.  */
 34444       glyph = r1->glyphs[TEXT_AREA];
 34445       end = glyph + r1->used[TEXT_AREA];
 34446       x = r1->x;
 34447 
 34448       /* Skip truncation glyphs at the start of the glyph row.  */
 34449       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34450         for (; glyph < end
 34451                && NILP (glyph->object)
 34452                && glyph->charpos < 0;
 34453              ++glyph)
 34454           x += glyph->pixel_width;
 34455 
 34456       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34457          or DISP_STRING, and the first glyph from buffer whose
 34458          position is between START_CHARPOS and END_CHARPOS.  */
 34459       for (; glyph < end
 34460              && !NILP (glyph->object)
 34461              && !EQ (glyph->object, disp_string)
 34462              && !(BUFFERP (glyph->object)
 34463                   && (glyph->charpos >= start_charpos
 34464                       && glyph->charpos < end_charpos));
 34465            ++glyph)
 34466         {
 34467           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34468              are present at buffer positions between START_CHARPOS and
 34469              END_CHARPOS, or if they come from an overlay.  */
 34470           if (EQ (glyph->object, before_string))
 34471             {
 34472               pos = string_buffer_position (before_string,
 34473                                             start_charpos);
 34474               /* If pos == 0, it means before_string came from an
 34475                  overlay, not from a buffer position.  */
 34476               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34477                 break;
 34478             }
 34479           else if (EQ (glyph->object, after_string))
 34480             {
 34481               pos = string_buffer_position (after_string, end_charpos);
 34482               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34483                 break;
 34484             }
 34485           x += glyph->pixel_width;
 34486         }
 34487       hlinfo->mouse_face_beg_x = x;
 34488       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34489     }
 34490   else
 34491     {
 34492       /* This row is in a right to left paragraph.  Scan it right to
 34493          left.  */
 34494       struct glyph *g;
 34495 
 34496       end = r1->glyphs[TEXT_AREA] - 1;
 34497       glyph = end + r1->used[TEXT_AREA];
 34498 
 34499       /* Skip truncation glyphs at the start of the glyph row.  */
 34500       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34501         for (; glyph > end
 34502                && NILP (glyph->object)
 34503                && glyph->charpos < 0;
 34504              --glyph)
 34505           ;
 34506 
 34507       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34508          or DISP_STRING, and the first glyph from buffer whose
 34509          position is between START_CHARPOS and END_CHARPOS.  */
 34510       for (; glyph > end
 34511              && !NILP (glyph->object)
 34512              && !EQ (glyph->object, disp_string)
 34513              && !(BUFFERP (glyph->object)
 34514                   && (glyph->charpos >= start_charpos
 34515                       && glyph->charpos < end_charpos));
 34516            --glyph)
 34517         {
 34518           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34519              are present at buffer positions between START_CHARPOS and
 34520              END_CHARPOS, or if they come from an overlay.  */
 34521           if (EQ (glyph->object, before_string))
 34522             {
 34523               pos = string_buffer_position (before_string, start_charpos);
 34524               /* If pos == 0, it means before_string came from an
 34525                  overlay, not from a buffer position.  */
 34526               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34527                 break;
 34528             }
 34529           else if (EQ (glyph->object, after_string))
 34530             {
 34531               pos = string_buffer_position (after_string, end_charpos);
 34532               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34533                 break;
 34534             }
 34535         }
 34536 
 34537       glyph++; /* first glyph to the right of the highlighted area */
 34538       for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
 34539         x += g->pixel_width;
 34540       hlinfo->mouse_face_beg_x = x;
 34541       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34542     }
 34543 
 34544   /* If the highlight ends in a different row, compute GLYPH and END
 34545      for the end row.  Otherwise, reuse the values computed above for
 34546      the row where the highlight begins.  */
 34547   if (r2 != r1)
 34548     {
 34549       if (!r2->reversed_p)
 34550         {
 34551           glyph = r2->glyphs[TEXT_AREA];
 34552           end = glyph + r2->used[TEXT_AREA];
 34553           x = r2->x;
 34554         }
 34555       else
 34556         {
 34557           end = r2->glyphs[TEXT_AREA] - 1;
 34558           glyph = end + r2->used[TEXT_AREA];
 34559         }
 34560     }
 34561 
 34562   if (!r2->reversed_p)
 34563     {
 34564       /* Skip truncation and continuation glyphs near the end of the
 34565          row, and also blanks and stretch glyphs inserted by
 34566          extend_face_to_end_of_line.  */
 34567       while (end > glyph
 34568              && NILP ((end - 1)->object))
 34569         --end;
 34570       /* Scan the rest of the glyph row from the end, looking for the
 34571          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34572          DISP_STRING, or whose position is between START_CHARPOS
 34573          and END_CHARPOS */
 34574       for (--end;
 34575              end > glyph
 34576              && !NILP (end->object)
 34577              && !EQ (end->object, disp_string)
 34578              && !(BUFFERP (end->object)
 34579                   && (end->charpos >= start_charpos
 34580                       && end->charpos < end_charpos));
 34581            --end)
 34582         {
 34583           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34584              are present at buffer positions between START_CHARPOS and
 34585              END_CHARPOS, or if they come from an overlay.  */
 34586           if (EQ (end->object, before_string))
 34587             {
 34588               pos = string_buffer_position (before_string, start_charpos);
 34589               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34590                 break;
 34591             }
 34592           else if (EQ (end->object, after_string))
 34593             {
 34594               pos = string_buffer_position (after_string, end_charpos);
 34595               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34596                 break;
 34597             }
 34598         }
 34599       /* Find the X coordinate of the last glyph to be highlighted.  */
 34600       for (; glyph <= end; ++glyph)
 34601         x += glyph->pixel_width;
 34602 
 34603       hlinfo->mouse_face_end_x = x;
 34604       hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
 34605     }
 34606   else
 34607     {
 34608       /* Skip truncation and continuation glyphs near the end of the
 34609          row, and also blanks and stretch glyphs inserted by
 34610          extend_face_to_end_of_line.  */
 34611       x = r2->x;
 34612       end++;
 34613       while (end < glyph
 34614              && NILP (end->object))
 34615         {
 34616           x += end->pixel_width;
 34617           ++end;
 34618         }
 34619       /* Scan the rest of the glyph row from the end, looking for the
 34620          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34621          DISP_STRING, or whose position is between START_CHARPOS
 34622          and END_CHARPOS */
 34623       for ( ;
 34624              end < glyph
 34625              && !NILP (end->object)
 34626              && !EQ (end->object, disp_string)
 34627              && !(BUFFERP (end->object)
 34628                   && (end->charpos >= start_charpos
 34629                       && end->charpos < end_charpos));
 34630            ++end)
 34631         {
 34632           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34633              are present at buffer positions between START_CHARPOS and
 34634              END_CHARPOS, or if they come from an overlay.  */
 34635           if (EQ (end->object, before_string))
 34636             {
 34637               pos = string_buffer_position (before_string, start_charpos);
 34638               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34639                 break;
 34640             }
 34641           else if (EQ (end->object, after_string))
 34642             {
 34643               pos = string_buffer_position (after_string, end_charpos);
 34644               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34645                 break;
 34646             }
 34647           x += end->pixel_width;
 34648         }
 34649       /* If we exited the above loop because we arrived at the last
 34650          glyph of the row, and its buffer position is still not in
 34651          range, it means the last character in range is the preceding
 34652          newline.  Bump the end column and x values to get past the
 34653          last glyph.  */
 34654       if (end == glyph
 34655           && BUFFERP (end->object)
 34656           && (end->charpos < start_charpos
 34657               || end->charpos >= end_charpos))
 34658         {
 34659           x += end->pixel_width;
 34660           ++end;
 34661         }
 34662       hlinfo->mouse_face_end_x = x;
 34663       hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
 34664     }
 34665 
 34666   hlinfo->mouse_face_window = window;
 34667   hlinfo->mouse_face_face_id
 34668     = face_at_buffer_position (w, mouse_charpos, &ignore,
 34669                                mouse_charpos + 1,
 34670                                !hlinfo->mouse_face_hidden, -1, 0);
 34671   show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 34672 }
 34673 
 34674 /* The following function is not used anymore (replaced with
 34675    mouse_face_from_string_pos), but I leave it here for the time
 34676    being, in case someone would.  */
 34677 
 34678 #if false       /* not used */
 34679 
 34680 /* Find the position of the glyph for position POS in OBJECT in
 34681    window W's current matrix, and return in *X, *Y the pixel
 34682    coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
 34683 
 34684    RIGHT_P means return the position of the right edge of the glyph.
 34685    !RIGHT_P means return the left edge position.
 34686 
 34687    If no glyph for POS exists in the matrix, return the position of
 34688    the glyph with the next smaller position that is in the matrix, if
 34689    RIGHT_P is false.  If RIGHT_P, and no glyph for POS
 34690    exists in the matrix, return the position of the glyph with the
 34691    next larger position in OBJECT.
 34692 
 34693    Value is true if a glyph was found.  */
 34694 
 34695 static bool
 34696 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
 34697                       int *hpos, int *vpos, int *x, int *y, bool right_p)
 34698 {
 34699   int yb = window_text_bottom_y (w);
 34700   struct glyph_row *r;
 34701   struct glyph *best_glyph = NULL;
 34702   struct glyph_row *best_row = NULL;
 34703   int best_x = 0;
 34704 
 34705   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34706        r->enabled_p && r->y < yb;
 34707        ++r)
 34708     {
 34709       struct glyph *g = r->glyphs[TEXT_AREA];
 34710       struct glyph *e = g + r->used[TEXT_AREA];
 34711       int gx;
 34712 
 34713       for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34714         if (EQ (g->object, object))
 34715           {
 34716             if (g->charpos == pos)
 34717               {
 34718                 best_glyph = g;
 34719                 best_x = gx;
 34720                 best_row = r;
 34721                 goto found;
 34722               }
 34723             else if (best_glyph == NULL
 34724                      || ((eabs (g->charpos - pos)
 34725                          < eabs (best_glyph->charpos - pos))
 34726                          && (right_p
 34727                              ? g->charpos < pos
 34728                              : g->charpos > pos)))
 34729               {
 34730                 best_glyph = g;
 34731                 best_x = gx;
 34732                 best_row = r;
 34733               }
 34734           }
 34735     }
 34736 
 34737  found:
 34738 
 34739   if (best_glyph)
 34740     {
 34741       *x = best_x;
 34742       *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
 34743 
 34744       if (right_p)
 34745         {
 34746           *x += best_glyph->pixel_width;
 34747           ++*hpos;
 34748         }
 34749 
 34750       *y = best_row->y;
 34751       *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
 34752     }
 34753 
 34754   return best_glyph != NULL;
 34755 }
 34756 #endif  /* not used */
 34757 
 34758 /* Find the positions of the first and the last glyphs in window W's
 34759    current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
 34760    (assumed to be a string), and return in HLINFO's mouse_face_*
 34761    members the pixel and column/row coordinates of those glyphs.  */
 34762 
 34763 static void
 34764 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
 34765                             Lisp_Object object,
 34766                             ptrdiff_t startpos, ptrdiff_t endpos)
 34767 {
 34768   int yb = window_text_bottom_y (w);
 34769   struct glyph_row *r;
 34770   struct glyph *g, *e;
 34771   int gx;
 34772   bool found = false;
 34773 
 34774   /* Find the glyph row with at least one position in the range
 34775      [STARTPOS..ENDPOS), and the first glyph in that row whose
 34776      position belongs to that range.  */
 34777   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34778        r->enabled_p && r->y < yb;
 34779        ++r)
 34780     {
 34781       if (!r->reversed_p)
 34782         {
 34783           g = r->glyphs[TEXT_AREA];
 34784           e = g + r->used[TEXT_AREA];
 34785           for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34786             if (EQ (g->object, object)
 34787                 && startpos <= g->charpos && g->charpos < endpos)
 34788               {
 34789                 hlinfo->mouse_face_beg_row
 34790                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34791                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34792                 hlinfo->mouse_face_beg_x = gx;
 34793                 found = true;
 34794                 break;
 34795               }
 34796         }
 34797       else
 34798         {
 34799           struct glyph *g1;
 34800 
 34801           e = r->glyphs[TEXT_AREA];
 34802           g = e + r->used[TEXT_AREA];
 34803           for ( ; g > e; --g)
 34804             if (EQ ((g-1)->object, object)
 34805                 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
 34806               {
 34807                 hlinfo->mouse_face_beg_row
 34808                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34809                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34810                 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
 34811                   gx += g1->pixel_width;
 34812                 hlinfo->mouse_face_beg_x = gx;
 34813                 found = true;
 34814                 break;
 34815               }
 34816         }
 34817       if (found)
 34818         break;
 34819     }
 34820 
 34821   if (!found)
 34822     return;
 34823 
 34824   /* Starting with the next row, look for the first row which does NOT
 34825      include any glyphs whose positions are in the range.  */
 34826   for (++r; r->enabled_p && r->y < yb; ++r)
 34827     {
 34828       g = r->glyphs[TEXT_AREA];
 34829       e = g + r->used[TEXT_AREA];
 34830       found = false;
 34831       for ( ; g < e; ++g)
 34832         if (EQ (g->object, object)
 34833             && startpos <= g->charpos && g->charpos < endpos)
 34834           {
 34835             found = true;
 34836             break;
 34837           }
 34838       if (!found)
 34839         break;
 34840     }
 34841 
 34842   /* The highlighted region ends on the previous row.  */
 34843   r--;
 34844 
 34845   /* Set the end row.  */
 34846   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
 34847 
 34848   /* Compute and set the end column and the end column's horizontal
 34849      pixel coordinate.  */
 34850   if (!r->reversed_p)
 34851     {
 34852       g = r->glyphs[TEXT_AREA];
 34853       e = g + r->used[TEXT_AREA];
 34854       for ( ; e > g; --e)
 34855         if (EQ ((e-1)->object, object)
 34856             && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
 34857           break;
 34858       hlinfo->mouse_face_end_col = e - g;
 34859 
 34860       for (gx = r->x; g < e; ++g)
 34861         gx += g->pixel_width;
 34862       hlinfo->mouse_face_end_x = gx;
 34863     }
 34864   else
 34865     {
 34866       e = r->glyphs[TEXT_AREA];
 34867       g = e + r->used[TEXT_AREA];
 34868       for (gx = r->x ; e < g; ++e)
 34869         {
 34870           if (EQ (e->object, object)
 34871               && startpos <= e->charpos && e->charpos < endpos)
 34872             break;
 34873           gx += e->pixel_width;
 34874         }
 34875       hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
 34876       hlinfo->mouse_face_end_x = gx;
 34877     }
 34878 }
 34879 
 34880 #ifdef HAVE_WINDOW_SYSTEM
 34881 
 34882 /* See if position X, Y is within a hot-spot of an image.  */
 34883 
 34884 static bool
 34885 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
 34886 {
 34887   if (!CONSP (hot_spot))
 34888     return false;
 34889 
 34890   if (EQ (XCAR (hot_spot), Qrect))
 34891     {
 34892       /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1))  */
 34893       Lisp_Object rect = XCDR (hot_spot);
 34894       Lisp_Object tem;
 34895       if (!CONSP (rect))
 34896         return false;
 34897       if (!CONSP (XCAR (rect)))
 34898         return false;
 34899       if (!CONSP (XCDR (rect)))
 34900         return false;
 34901       if (!(tem = XCAR (XCAR (rect)), FIXNUMP (tem) && x >= XFIXNUM (tem)))
 34902         return false;
 34903       if (!(tem = XCDR (XCAR (rect)), FIXNUMP (tem) && y >= XFIXNUM (tem)))
 34904         return false;
 34905       if (!(tem = XCAR (XCDR (rect)), FIXNUMP (tem) && x <= XFIXNUM (tem)))
 34906         return false;
 34907       if (!(tem = XCDR (XCDR (rect)), FIXNUMP (tem) && y <= XFIXNUM (tem)))
 34908         return false;
 34909       return true;
 34910     }
 34911   else if (EQ (XCAR (hot_spot), Qcircle))
 34912     {
 34913       /* CDR is (Center . Radius) = ((x0 . y0) . r) */
 34914       Lisp_Object circ = XCDR (hot_spot);
 34915       Lisp_Object lr, lx0, ly0;
 34916       if (CONSP (circ)
 34917           && CONSP (XCAR (circ))
 34918           && (lr = XCDR (circ), NUMBERP (lr))
 34919           && (lx0 = XCAR (XCAR (circ)), FIXNUMP (lx0))
 34920           && (ly0 = XCDR (XCAR (circ)), FIXNUMP (ly0)))
 34921         {
 34922           double r = XFLOATINT (lr);
 34923           double dx = XFIXNUM (lx0) - x;
 34924           double dy = XFIXNUM (ly0) - y;
 34925           return (dx * dx + dy * dy <= r * r);
 34926         }
 34927     }
 34928   else if (EQ (XCAR (hot_spot), Qpoly))
 34929     {
 34930       /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
 34931       if (VECTORP (XCDR (hot_spot)))
 34932         {
 34933           struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
 34934           Lisp_Object *poly = v->contents;
 34935           ptrdiff_t n = v->header.size;
 34936           ptrdiff_t i;
 34937           bool inside = false;
 34938           Lisp_Object lx, ly;
 34939           int x0, y0;
 34940 
 34941           /* Need an even number of coordinates, and at least 3 edges.  */
 34942           if (n < 6 || n & 1)
 34943             return false;
 34944 
 34945           /* Count edge segments intersecting line from (X,Y) to (X,infinity).
 34946              If count is odd, we are inside polygon.  Pixels on edges
 34947              may or may not be included depending on actual geometry of the
 34948              polygon.  */
 34949           if ((lx = poly[n-2], !FIXNUMP (lx))
 34950               || (ly = poly[n-1], !FIXNUMP (lx)))
 34951             return false;
 34952           x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34953           for (i = 0; i < n; i += 2)
 34954             {
 34955               int x1 = x0, y1 = y0;
 34956               if ((lx = poly[i], !FIXNUMP (lx))
 34957                   || (ly = poly[i+1], !FIXNUMP (ly)))
 34958                 return false;
 34959               x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34960 
 34961               /* Does this segment cross the X line?  */
 34962               if (x0 >= x)
 34963                 {
 34964                   if (x1 >= x)
 34965                     continue;
 34966                 }
 34967               else if (x1 < x)
 34968                 continue;
 34969               if (y > y0 && y > y1)
 34970                 continue;
 34971               if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
 34972                 inside = !inside;
 34973             }
 34974           return inside;
 34975         }
 34976     }
 34977   return false;
 34978 }
 34979 
 34980 Lisp_Object
 34981 find_hot_spot (Lisp_Object map, int x, int y)
 34982 {
 34983   while (CONSP (map))
 34984     {
 34985       if (CONSP (XCAR (map))
 34986           && on_hot_spot_p (XCAR (XCAR (map)), x, y))
 34987         return XCAR (map);
 34988       map = XCDR (map);
 34989     }
 34990 
 34991   return Qnil;
 34992 }
 34993 
 34994 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
 34995        3, 3, 0,
 34996        doc: /* Lookup in image map MAP coordinates X and Y.
 34997 An image map is an alist where each element has the format (AREA ID PLIST).
 34998 An AREA is specified as either a rectangle, a circle, or a polygon:
 34999 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
 35000 pixel coordinates of the upper left and bottom right corners.
 35001 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
 35002 and the radius of the circle; r may be a float or integer.
 35003 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
 35004 vector describes one corner in the polygon.
 35005 Returns the alist element for the first matching AREA in MAP.  */)
 35006   (Lisp_Object map, Lisp_Object x, Lisp_Object y)
 35007 {
 35008   if (NILP (map))
 35009     return Qnil;
 35010 
 35011   CHECK_FIXNUM (x);
 35012   CHECK_FIXNUM (y);
 35013 
 35014   return find_hot_spot (map,
 35015                         clip_to_bounds (INT_MIN, XFIXNUM (x), INT_MAX),
 35016                         clip_to_bounds (INT_MIN, XFIXNUM (y), INT_MAX));
 35017 }
 35018 #endif  /* HAVE_WINDOW_SYSTEM */
 35019 
 35020 
 35021 /* Display frame CURSOR, optionally using shape defined by POINTER.  */
 35022 static void
 35023 define_frame_cursor1 (struct frame *f, Emacs_Cursor cursor, Lisp_Object pointer)
 35024 {
 35025 #ifdef HAVE_WINDOW_SYSTEM
 35026   if (!FRAME_WINDOW_P (f))
 35027     return;
 35028 
 35029   /* Do not change cursor shape while dragging mouse.  */
 35030   if (EQ (track_mouse, Qdragging) || EQ (track_mouse, Qdropping)
 35031       || EQ (track_mouse, Qdrag_source))
 35032     return;
 35033 
 35034   if (!NILP (pointer))
 35035     {
 35036       if (EQ (pointer, Qarrow))
 35037         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35038       else if (EQ (pointer, Qhand))
 35039         cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35040       else if (EQ (pointer, Qtext))
 35041         cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35042       else if (EQ (pointer, intern ("hdrag")))
 35043         cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35044       else if (EQ (pointer, intern ("nhdrag")))
 35045         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35046 # ifdef HAVE_X_WINDOWS
 35047       else if (EQ (pointer, intern ("vdrag")))
 35048         cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
 35049 # endif
 35050       else if (EQ (pointer, intern ("hourglass")))
 35051         cursor = FRAME_OUTPUT_DATA (f)->hourglass_cursor;
 35052       else if (EQ (pointer, Qmodeline))
 35053         cursor = FRAME_OUTPUT_DATA (f)->modeline_cursor;
 35054       else
 35055         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35056     }
 35057 
 35058   if (cursor != No_Cursor)
 35059     FRAME_RIF (f)->define_frame_cursor (f, cursor);
 35060 #endif
 35061 }
 35062 
 35063 /* Take proper action when mouse has moved to the mode or header line
 35064    or marginal area AREA of window W, x-position X and y-position Y.
 35065    X is relative to the start of the text display area of W, so the
 35066    width of bitmap areas and scroll bars must be subtracted to get a
 35067    position relative to the start of the mode line.  */
 35068 
 35069 static void
 35070 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
 35071                                     enum window_part area)
 35072 {
 35073   struct window *w = XWINDOW (window);
 35074   struct frame *f = XFRAME (w->frame);
 35075   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35076   Emacs_Cursor cursor = No_Cursor;
 35077   Lisp_Object pointer = Qnil;
 35078   int dx, dy, width, height;
 35079   ptrdiff_t charpos;
 35080   Lisp_Object string, object = Qnil;
 35081   Lisp_Object pos UNINIT;
 35082   Lisp_Object mouse_face;
 35083   int original_x_pixel = x;
 35084   struct glyph * glyph = NULL, * row_start_glyph = NULL;
 35085   struct glyph_row *row UNINIT;
 35086 
 35087   if (area == ON_MODE_LINE || area == ON_HEADER_LINE || area == ON_TAB_LINE)
 35088     {
 35089       int x0;
 35090       struct glyph *end;
 35091 
 35092       /* Kludge alert: mode_line_string takes X/Y in pixels, but
 35093          returns them in row/column units!  */
 35094       string = mode_line_string (w, area, &x, &y, &charpos,
 35095                                  &object, &dx, &dy, &width, &height);
 35096 
 35097       row = (area == ON_MODE_LINE
 35098              ? MATRIX_MODE_LINE_ROW (w->current_matrix)
 35099              : (area == ON_TAB_LINE
 35100                 ? MATRIX_TAB_LINE_ROW (w->current_matrix)
 35101                 : MATRIX_HEADER_LINE_ROW (w->current_matrix)));
 35102 
 35103       /* Find the glyph under the mouse pointer.  */
 35104       if (row->mode_line_p && row->enabled_p)
 35105         {
 35106           glyph = row_start_glyph = row->glyphs[TEXT_AREA];
 35107           end = glyph + row->used[TEXT_AREA];
 35108 
 35109           for (x0 = original_x_pixel;
 35110                glyph < end && x0 >= glyph->pixel_width;
 35111                ++glyph)
 35112             x0 -= glyph->pixel_width;
 35113 
 35114           if (glyph >= end)
 35115             glyph = NULL;
 35116         }
 35117     }
 35118   else
 35119     {
 35120       x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
 35121       /* Kludge alert: marginal_area_string takes X/Y in pixels, but
 35122          returns them in row/column units!  */
 35123       string = marginal_area_string (w, area, &x, &y, &charpos,
 35124                                      &object, &dx, &dy, &width, &height);
 35125     }
 35126 
 35127   Lisp_Object help = Qnil;
 35128 
 35129 #ifdef HAVE_WINDOW_SYSTEM
 35130   if (IMAGEP (object))
 35131     {
 35132       Lisp_Object image_map, hotspot;
 35133       if ((image_map = plist_get (XCDR (object), QCmap),
 35134            !NILP (image_map))
 35135           && (hotspot = find_hot_spot (image_map, dx, dy),
 35136               CONSP (hotspot))
 35137           && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35138         {
 35139           Lisp_Object plist;
 35140 
 35141           /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
 35142              If so, we could look for mouse-enter, mouse-leave
 35143              properties in PLIST (and do something...).  */
 35144           hotspot = XCDR (hotspot);
 35145           if (CONSP (hotspot)
 35146               && (plist = XCAR (hotspot), CONSP (plist)))
 35147             {
 35148               pointer = plist_get (plist, Qpointer);
 35149               if (NILP (pointer))
 35150                 pointer = Qhand;
 35151               help = plist_get (plist, Qhelp_echo);
 35152               if (!NILP (help))
 35153                 {
 35154                   help_echo_string = help;
 35155                   XSETWINDOW (help_echo_window, w);
 35156                   help_echo_object = w->contents;
 35157                   help_echo_pos = charpos;
 35158                 }
 35159             }
 35160         }
 35161       if (NILP (pointer))
 35162         pointer = plist_get (XCDR (object), QCpointer);
 35163     }
 35164 #endif  /* HAVE_WINDOW_SYSTEM */
 35165 
 35166   /* CHARPOS can be beyond the last position of STRING due, e.g., to
 35167      min-width 'display' property.  Fix that, to let all the calls to
 35168      get-text-property below do their thing.  */
 35169   if (STRINGP (string))
 35170     pos = make_fixnum (min (charpos, SCHARS (string) - 1));
 35171 
 35172   /* Set the help text and mouse pointer.  If the mouse is on a part
 35173      of the mode line without any text (e.g. past the right edge of
 35174      the mode line text), use that windows's mode line help echo if it
 35175      has been set.  */
 35176   if (STRINGP (string) || area == ON_MODE_LINE || area == ON_HEADER_LINE
 35177       || area == ON_TAB_LINE)
 35178     {
 35179       /* Arrange to display the help by setting the global variables
 35180          help_echo_string, help_echo_object, and help_echo_pos.  */
 35181       if (NILP (help))
 35182         {
 35183           if (STRINGP (string))
 35184             help = Fget_text_property (pos, Qhelp_echo, string);
 35185 
 35186           if (!NILP (help))
 35187             {
 35188               help_echo_string = help;
 35189               XSETWINDOW (help_echo_window, w);
 35190               help_echo_object = string;
 35191               help_echo_pos = charpos;
 35192             }
 35193           else if (area == ON_MODE_LINE
 35194                    && !NILP (w->mode_line_help_echo))
 35195             {
 35196               help_echo_string =  w->mode_line_help_echo;
 35197               XSETWINDOW (help_echo_window, w);
 35198               help_echo_object = Qnil;
 35199               help_echo_pos = -1;
 35200             }
 35201         }
 35202 
 35203 #ifdef HAVE_WINDOW_SYSTEM
 35204       /* Change the mouse pointer according to what is under it.  */
 35205       if (FRAME_WINDOW_P (f))
 35206         {
 35207           bool draggable = (! WINDOW_BOTTOMMOST_P (w)
 35208                             || minibuf_level
 35209                             || NILP (Vresize_mini_windows));
 35210 
 35211           if (STRINGP (string))
 35212             {
 35213               cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35214 
 35215               if (NILP (pointer))
 35216                 pointer = Fget_text_property (pos, Qpointer, string);
 35217 
 35218               /* Change the mouse pointer according to what is under X/Y.  */
 35219               if (NILP (pointer)
 35220                   && (area == ON_MODE_LINE || area == ON_HEADER_LINE
 35221                       || area == ON_TAB_LINE))
 35222                 {
 35223                   Lisp_Object map;
 35224 
 35225                   map = Fget_text_property (pos, Qlocal_map, string);
 35226                   if (!KEYMAPP (map))
 35227                     map = Fget_text_property (pos, Qkeymap, string);
 35228                   if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
 35229                     cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35230                 }
 35231             }
 35232           else if (draggable && area == ON_MODE_LINE)
 35233             cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35234           else if ((area == ON_MODE_LINE
 35235                     && WINDOW_BOTTOMMOST_P (w)
 35236                     && !FRAME_HAS_MINIBUF_P (f)
 35237                     && !NILP (Fframe_parameter
 35238                               (w->frame, Qdrag_with_mode_line)))
 35239                    || (((area == ON_HEADER_LINE
 35240                          && !NILP (Fframe_parameter
 35241                                    (w->frame, Qdrag_with_header_line)))
 35242                         || (area == ON_TAB_LINE
 35243                             && !NILP (Fframe_parameter
 35244                                       (w->frame, Qdrag_with_tab_line))))
 35245                        && WINDOW_TOPMOST_P (w)))
 35246             cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35247           else
 35248             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35249         }
 35250 #endif
 35251     }
 35252 
 35253   /* Change the mouse face according to what is under X/Y.  */
 35254   bool mouse_face_shown = false;
 35255 
 35256   if (STRINGP (string))
 35257     {
 35258       mouse_face = Fget_text_property (pos, Qmouse_face, string);
 35259       if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
 35260           && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)
 35261               || (area == ON_TAB_LINE))
 35262           && glyph)
 35263         {
 35264           Lisp_Object b, e;
 35265 
 35266           struct glyph * tmp_glyph;
 35267 
 35268           int gpos;
 35269           int gseq_length;
 35270           int total_pixel_width;
 35271           ptrdiff_t begpos, endpos, ignore;
 35272 
 35273           int vpos, hpos;
 35274 
 35275           b = Fprevious_single_property_change (make_fixnum (charpos + 1),
 35276                                                 Qmouse_face, string, Qnil);
 35277           if (NILP (b))
 35278             begpos = 0;
 35279           else
 35280             begpos = XFIXNUM (b);
 35281 
 35282           e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
 35283           if (NILP (e))
 35284             endpos = SCHARS (string);
 35285           else
 35286             endpos = XFIXNUM (e);
 35287 
 35288           /* Calculate the glyph position GPOS of GLYPH in the
 35289              displayed string, relative to the beginning of the
 35290              highlighted part of the string.
 35291 
 35292              Note: GPOS is different from CHARPOS.  CHARPOS is the
 35293              position of GLYPH in the internal string object.  A mode
 35294              line string format has structures which are converted to
 35295              a flattened string by the Emacs Lisp interpreter.  The
 35296              internal string is an element of those structures.  The
 35297              displayed string is the flattened string.  */
 35298           tmp_glyph = row_start_glyph;
 35299           while (tmp_glyph < glyph
 35300                  && (!(EQ (tmp_glyph->object, glyph->object)
 35301                        && begpos <= tmp_glyph->charpos
 35302                        && tmp_glyph->charpos < endpos)))
 35303             tmp_glyph++;
 35304           gpos = glyph - tmp_glyph;
 35305 
 35306           /* Calculate the length GSEQ_LENGTH of the glyph sequence of
 35307              the highlighted part of the displayed string to which
 35308              GLYPH belongs.  Note: GSEQ_LENGTH is different from
 35309              SCHARS (STRING), because the latter returns the length of
 35310              the internal string.  */
 35311           for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 35312                tmp_glyph > glyph
 35313                  && (!(EQ (tmp_glyph->object, glyph->object)
 35314                        && begpos <= tmp_glyph->charpos
 35315                        && tmp_glyph->charpos < endpos));
 35316                tmp_glyph--)
 35317             ;
 35318           gseq_length = gpos + (tmp_glyph - glyph) + 1;
 35319 
 35320           /* Calculate the total pixel width of all the glyphs between
 35321              the beginning of the highlighted area and GLYPH.  */
 35322           total_pixel_width = 0;
 35323           for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
 35324             total_pixel_width += tmp_glyph->pixel_width;
 35325 
 35326           /* Pre calculation of re-rendering position.  Note: X is in
 35327              column units here, after the call to mode_line_string or
 35328              marginal_area_string.  */
 35329           hpos = x - gpos;
 35330           vpos = (area == ON_MODE_LINE
 35331                   ? (w->current_matrix)->nrows - 1
 35332                   : (area == ON_TAB_LINE
 35333                      ? 0
 35334                      : (w->current_matrix->tab_line_p
 35335                         ? 1
 35336                         : 0)));
 35337 
 35338           /* If GLYPH's position is included in the region that is
 35339              already drawn in mouse face, we have nothing to do.  */
 35340           if ( EQ (window, hlinfo->mouse_face_window)
 35341                && (!row->reversed_p
 35342                    ? (hlinfo->mouse_face_beg_col <= hpos
 35343                       && hpos < hlinfo->mouse_face_end_col)
 35344                    /* In R2L rows we swap BEG and END, see below.  */
 35345                    : (hlinfo->mouse_face_end_col <= hpos
 35346                       && hpos < hlinfo->mouse_face_beg_col))
 35347                && hlinfo->mouse_face_beg_row == vpos )
 35348             return;
 35349 
 35350           if (clear_mouse_face (hlinfo))
 35351             cursor = No_Cursor;
 35352 
 35353           if (!row->reversed_p)
 35354             {
 35355               hlinfo->mouse_face_beg_col = hpos;
 35356               hlinfo->mouse_face_beg_x   = original_x_pixel
 35357                                             - (total_pixel_width + dx);
 35358               hlinfo->mouse_face_end_col = hpos + gseq_length;
 35359               hlinfo->mouse_face_end_x   = 0;
 35360             }
 35361           else
 35362             {
 35363               /* In R2L rows, show_mouse_face expects BEG and END
 35364                  coordinates to be swapped.  */
 35365               hlinfo->mouse_face_end_col = hpos;
 35366               hlinfo->mouse_face_end_x   = original_x_pixel
 35367                                             - (total_pixel_width + dx);
 35368               hlinfo->mouse_face_beg_col = hpos + gseq_length;
 35369               hlinfo->mouse_face_beg_x   = 0;
 35370             }
 35371 
 35372           hlinfo->mouse_face_beg_row  = vpos;
 35373           hlinfo->mouse_face_end_row  = hlinfo->mouse_face_beg_row;
 35374           hlinfo->mouse_face_past_end = false;
 35375           hlinfo->mouse_face_window   = window;
 35376 
 35377           hlinfo->mouse_face_face_id =
 35378             face_at_string_position (w, string, charpos, 0, &ignore,
 35379                                      glyph->face_id, true, 0);
 35380 
 35381           show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35382           mouse_face_shown = true;
 35383 
 35384           if (NILP (pointer))
 35385             pointer = Qhand;
 35386         }
 35387     }
 35388 
 35389   /* If mouse-face doesn't need to be shown, clear any existing
 35390      mouse-face.  */
 35391   if ((area == ON_MODE_LINE || area == ON_HEADER_LINE
 35392        || area == ON_TAB_LINE) && !mouse_face_shown)
 35393     clear_mouse_face (hlinfo);
 35394 
 35395   define_frame_cursor1 (f, cursor, pointer);
 35396 }
 35397 
 35398 
 35399 /* EXPORT:
 35400    Take proper action when the mouse has moved to position X, Y on
 35401    frame F with regards to highlighting portions of display that have
 35402    mouse-face properties.  Also de-highlight portions of display where
 35403    the mouse was before, set the mouse pointer shape as appropriate
 35404    for the mouse coordinates, and activate help echo (tooltips).
 35405    X and Y can be negative or out of range.  */
 35406 
 35407 void
 35408 note_mouse_highlight (struct frame *f, int x, int y)
 35409 {
 35410   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35411   enum window_part part = ON_NOTHING;
 35412   Lisp_Object window;
 35413   struct window *w;
 35414   Emacs_Cursor cursor = No_Cursor;
 35415   Lisp_Object pointer = Qnil;  /* Takes precedence over cursor!  */
 35416   struct buffer *b;
 35417 
 35418   /* When a menu is active, don't highlight because this looks odd.  */
 35419 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS) || defined (MSDOS) \
 35420   || defined (HAVE_ANDROID)
 35421   if (popup_activated ())
 35422     return;
 35423 #endif
 35424 
 35425 #if defined (HAVE_HAIKU)
 35426   if (popup_activated_p)
 35427     return;
 35428 #endif
 35429 
 35430   if (!f->glyphs_initialized_p
 35431       || f->pointer_invisible)
 35432     return;
 35433 
 35434   hlinfo->mouse_face_mouse_x = x;
 35435   hlinfo->mouse_face_mouse_y = y;
 35436   hlinfo->mouse_face_mouse_frame = f;
 35437 
 35438   if (hlinfo->mouse_face_defer)
 35439     return;
 35440 
 35441   /* Which window is that in?  */
 35442   window = window_from_coordinates (f, x, y, &part, true, true);
 35443 
 35444   /* If displaying active text in another window, clear that.  */
 35445   if (! EQ (window, hlinfo->mouse_face_window)
 35446       /* Also clear if we move out of text area in same window.  */
 35447       || (!NILP (hlinfo->mouse_face_window)
 35448           && !NILP (window)
 35449           && part != ON_TEXT
 35450           && part != ON_MODE_LINE
 35451           && part != ON_HEADER_LINE
 35452           && part != ON_TAB_LINE))
 35453     clear_mouse_face (hlinfo);
 35454 
 35455   /* Reset help_echo_string.  It will get recomputed below.  */
 35456   help_echo_string = Qnil;
 35457 
 35458   /* Handle tab-bar highlight on mouse-capable TTY frames.  */
 35459   if (!FRAME_WINDOW_P (f)
 35460       && (y >= FRAME_MENU_BAR_LINES (f)
 35461           && y < FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f)))
 35462     {
 35463       int prop_idx;
 35464       bool ignore;
 35465       Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &ignore);
 35466 
 35467       if (!NILP (caption))
 35468         {
 35469           help_echo_object = help_echo_window = Qnil;
 35470           help_echo_pos = -1;
 35471           help_echo_string = AREF (f->tab_bar_items,
 35472                                    prop_idx * TAB_BAR_ITEM_NSLOTS
 35473                                    + TAB_BAR_ITEM_HELP);
 35474           if (NILP (help_echo_string))
 35475             help_echo_string = caption;
 35476         }
 35477     }
 35478 
 35479 #ifdef HAVE_WINDOW_SYSTEM
 35480   /* If the cursor is on the internal border of FRAME and FRAME's
 35481      internal border is draggable, provide some visual feedback.  */
 35482   if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
 35483       && !NILP (get_frame_param (f, Qdrag_internal_border)))
 35484     {
 35485       enum internal_border_part part = frame_internal_border_part (f, x, y);
 35486 
 35487       switch (part)
 35488         {
 35489         case INTERNAL_BORDER_NONE:
 35490           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35491             /* Reset cursor.  */
 35492             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35493           break;
 35494         case INTERNAL_BORDER_LEFT_EDGE:
 35495           cursor = FRAME_OUTPUT_DATA (f)->left_edge_cursor;
 35496           break;
 35497         case INTERNAL_BORDER_TOP_LEFT_CORNER:
 35498           cursor = FRAME_OUTPUT_DATA (f)->top_left_corner_cursor;
 35499           break;
 35500         case INTERNAL_BORDER_TOP_EDGE:
 35501           cursor = FRAME_OUTPUT_DATA (f)->top_edge_cursor;
 35502           break;
 35503         case INTERNAL_BORDER_TOP_RIGHT_CORNER:
 35504           cursor = FRAME_OUTPUT_DATA (f)->top_right_corner_cursor;
 35505           break;
 35506         case INTERNAL_BORDER_RIGHT_EDGE:
 35507           cursor = FRAME_OUTPUT_DATA (f)->right_edge_cursor;
 35508           break;
 35509         case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
 35510           cursor = FRAME_OUTPUT_DATA (f)->bottom_right_corner_cursor;
 35511           break;
 35512         case INTERNAL_BORDER_BOTTOM_EDGE:
 35513           cursor = FRAME_OUTPUT_DATA (f)->bottom_edge_cursor;
 35514           break;
 35515         case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
 35516           cursor = FRAME_OUTPUT_DATA (f)->bottom_left_corner_cursor;
 35517           break;
 35518         default:
 35519           /* This should not happen.  */
 35520           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35521             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35522         }
 35523 
 35524       if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35525         {
 35526           /* Do we really want a help echo here?  */
 35527           help_echo_string = build_string ("drag-mouse-1: resize frame");
 35528           goto set_cursor;
 35529         }
 35530     }
 35531 #endif /* HAVE_WINDOW_SYSTEM */
 35532 
 35533   /* Not on a window -> return.  */
 35534   if (!WINDOWP (window))
 35535     return;
 35536 
 35537   /* Convert to window-relative pixel coordinates.  */
 35538   w = XWINDOW (window);
 35539   frame_to_window_pixel_xy (w, &x, &y);
 35540 
 35541 #if defined (HAVE_WINDOW_SYSTEM)
 35542   /* Handle tab-bar window differently since it doesn't display a
 35543      buffer.  */
 35544   if (EQ (window, f->tab_bar_window))
 35545     {
 35546       note_tab_bar_highlight (f, x, y);
 35547       if (tab_bar__dragging_in_progress)
 35548         {
 35549           cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35550           goto set_cursor;
 35551         }
 35552       else
 35553         return;
 35554     }
 35555   else
 35556     {
 35557       /* The mouse might have pressed into the tab bar, but might
 35558          also have been released outside the tab bar, so
 35559          f->last_tab_bar_item must be reset, in order to make sure the
 35560          item can be still highlighted again in the future.  */
 35561       f->last_tab_bar_item = -1;
 35562     }
 35563 #endif
 35564 
 35565 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 35566   /* Handle tool-bar window differently since it doesn't display a
 35567      buffer.  */
 35568   if (EQ (window, f->tool_bar_window))
 35569     {
 35570       note_tool_bar_highlight (f, x, y);
 35571       return;
 35572     }
 35573 #endif
 35574 
 35575   /* Mouse is on the mode, header line or margin?  */
 35576   if (part == ON_MODE_LINE || part == ON_HEADER_LINE || part == ON_TAB_LINE
 35577       || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35578     {
 35579       note_mode_line_or_margin_highlight (window, x, y, part);
 35580 
 35581 #ifdef HAVE_WINDOW_SYSTEM
 35582       if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35583         {
 35584           cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35585           /* Show non-text cursor (Bug#16647).  */
 35586           goto set_cursor;
 35587         }
 35588       else
 35589 #endif
 35590         return;
 35591     }
 35592 
 35593 #ifdef HAVE_WINDOW_SYSTEM
 35594   if (part == ON_VERTICAL_BORDER)
 35595     {
 35596       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35597       help_echo_string = build_string ("drag-mouse-1: resize");
 35598       goto set_cursor;
 35599     }
 35600   else if (part == ON_RIGHT_DIVIDER)
 35601     {
 35602       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35603       help_echo_string = build_string ("drag-mouse-1: resize");
 35604       goto set_cursor;
 35605     }
 35606   else if (part == ON_BOTTOM_DIVIDER)
 35607     if (! WINDOW_BOTTOMMOST_P (w)
 35608         || minibuf_level
 35609         || NILP (Vresize_mini_windows))
 35610       {
 35611         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35612         help_echo_string = build_string ("drag-mouse-1: resize");
 35613         goto set_cursor;
 35614       }
 35615     else
 35616       cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35617   else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
 35618            || part == ON_VERTICAL_SCROLL_BAR
 35619            || part == ON_HORIZONTAL_SCROLL_BAR)
 35620     cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35621   else
 35622     cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35623 #endif
 35624 
 35625   /* Are we in a window whose display is up to date?
 35626      And verify the buffer's text has not changed.  */
 35627   b = XBUFFER (w->contents);
 35628   if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
 35629     {
 35630       int hpos, vpos, dx, dy, area = LAST_AREA;
 35631       ptrdiff_t pos;
 35632       struct glyph *glyph;
 35633       Lisp_Object object;
 35634       Lisp_Object mouse_face = Qnil, position;
 35635       Lisp_Object *overlay_vec = NULL;
 35636       ptrdiff_t i, noverlays;
 35637       struct buffer *obuf;
 35638       ptrdiff_t obegv, ozv;
 35639       bool same_region;
 35640 
 35641       /* Find the glyph under X/Y.  */
 35642       glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
 35643 
 35644 #ifdef HAVE_WINDOW_SYSTEM
 35645       /* Look for :pointer property on image.  */
 35646       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 35647         {
 35648           struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 35649           if (img != NULL && IMAGEP (img->spec))
 35650             {
 35651               Lisp_Object image_map, hotspot;
 35652               if ((image_map = plist_get (XCDR (img->spec), QCmap),
 35653                    !NILP (image_map))
 35654                   && (hotspot = find_hot_spot (image_map,
 35655                                                glyph->slice.img.x + dx,
 35656                                                glyph->slice.img.y + dy),
 35657                       CONSP (hotspot))
 35658                   && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35659                 {
 35660                   Lisp_Object plist;
 35661 
 35662                   /* Could check XCAR (hotspot) to see if we enter/leave
 35663                      this hot-spot.
 35664                      If so, we could look for mouse-enter, mouse-leave
 35665                      properties in PLIST (and do something...).  */
 35666                   hotspot = XCDR (hotspot);
 35667                   if (CONSP (hotspot)
 35668                       && (plist = XCAR (hotspot), CONSP (plist)))
 35669                     {
 35670                       pointer = plist_get (plist, Qpointer);
 35671                       if (NILP (pointer))
 35672                         pointer = Qhand;
 35673                       help_echo_string = plist_get (plist, Qhelp_echo);
 35674                       if (!NILP (help_echo_string))
 35675                         {
 35676                           help_echo_window = window;
 35677                           help_echo_object = glyph->object;
 35678                           help_echo_pos = glyph->charpos;
 35679                         }
 35680                     }
 35681                 }
 35682               if (NILP (pointer))
 35683                 pointer = plist_get (XCDR (img->spec), QCpointer);
 35684             }
 35685         }
 35686 #endif  /* HAVE_WINDOW_SYSTEM */
 35687 
 35688       /* Clear mouse face if X/Y not over text.  */
 35689       if (glyph == NULL
 35690           || area != TEXT_AREA
 35691           || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
 35692           /* Glyph's OBJECT is nil for glyphs inserted by the
 35693              display engine for its internal purposes, like truncation
 35694              and continuation glyphs and blanks beyond the end of
 35695              line's text on text terminals.  If we are over such a
 35696              glyph, we are not over any text.  */
 35697           || NILP (glyph->object)
 35698           /* R2L rows have a stretch glyph at their front, which
 35699              stands for no text, whereas L2R rows have no glyphs at
 35700              all beyond the end of text.  Treat such stretch glyphs
 35701              like we do with NULL glyphs in L2R rows.  */
 35702           || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
 35703               && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
 35704               && glyph->type == STRETCH_GLYPH
 35705               && glyph->avoid_cursor_p))
 35706         {
 35707           if (clear_mouse_face (hlinfo))
 35708             cursor = No_Cursor;
 35709           if (FRAME_WINDOW_P (f) && NILP (pointer))
 35710             {
 35711 #ifdef HAVE_WINDOW_SYSTEM
 35712               if (area != TEXT_AREA)
 35713                 cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35714               else
 35715                 pointer = Vvoid_text_area_pointer;
 35716 #endif
 35717             }
 35718           goto set_cursor;
 35719         }
 35720 
 35721       pos = glyph->charpos;
 35722       object = glyph->object;
 35723       if (!STRINGP (object) && !BUFFERP (object))
 35724         goto set_cursor;
 35725 
 35726       /* If we get an out-of-range value, return now; avoid an error.  */
 35727       if (BUFFERP (object) && pos > BUF_Z (b))
 35728         goto set_cursor;
 35729 
 35730       /* Make the window's buffer temporarily current for
 35731          overlays_at and compute_char_face.  */
 35732       obuf = current_buffer;
 35733       current_buffer = b;
 35734       obegv = BEGV;
 35735       ozv = ZV;
 35736       BEGV = BEG;
 35737       ZV = Z;
 35738 
 35739       /* Is this char mouse-active or does it have help-echo?  */
 35740       position = make_fixnum (pos);
 35741 
 35742       USE_SAFE_ALLOCA;
 35743 
 35744       if (BUFFERP (object))
 35745         {
 35746           /* Put all the overlays we want in a vector in overlay_vec.  */
 35747           GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL);
 35748           /* Sort overlays into increasing priority order.  */
 35749           noverlays = sort_overlays (overlay_vec, noverlays, w);
 35750         }
 35751       else
 35752         noverlays = 0;
 35753 
 35754       if (NILP (Vmouse_highlight))
 35755         {
 35756           clear_mouse_face (hlinfo);
 35757           goto check_help_echo;
 35758         }
 35759 
 35760       same_region = coords_in_mouse_face_p (w, hpos, vpos);
 35761 
 35762       if (same_region)
 35763         cursor = No_Cursor;
 35764 
 35765       /* Check mouse-face highlighting.  */
 35766       if (! same_region
 35767           /* If there exists an overlay with mouse-face overlapping
 35768              the one we are currently highlighting, we have to check
 35769              if we enter the overlapping overlay, and then highlight
 35770              only that.  Skip the check when mouse-face highlighting
 35771              is currently hidden to avoid Bug#30519.  */
 35772           || (!hlinfo->mouse_face_hidden
 35773               && OVERLAYP (hlinfo->mouse_face_overlay)
 35774               /* It's possible the overlay was deleted (Bug#35273).  */
 35775               && OVERLAY_BUFFER (hlinfo->mouse_face_overlay)
 35776               && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
 35777         {
 35778           /* Find the highest priority overlay with a mouse-face.  */
 35779           Lisp_Object overlay = Qnil;
 35780           for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
 35781             {
 35782               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
 35783               if (!NILP (mouse_face))
 35784                 overlay = overlay_vec[i];
 35785             }
 35786 
 35787           /* If we're highlighting the same overlay as before, there's
 35788              no need to do that again.  */
 35789           if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
 35790             goto check_help_echo;
 35791 
 35792           /* Clear the display of the old active region, if any.  */
 35793           if (clear_mouse_face (hlinfo))
 35794             cursor = No_Cursor;
 35795 
 35796           /* Record the overlay, if any, to be highlighted.  */
 35797           hlinfo->mouse_face_overlay = overlay;
 35798 
 35799           /* If no overlay applies, get a text property.  */
 35800           if (NILP (overlay))
 35801             mouse_face = Fget_text_property (position, Qmouse_face, object);
 35802 
 35803           /* Next, compute the bounds of the mouse highlighting and
 35804              display it.  */
 35805           if (!NILP (mouse_face) && STRINGP (object))
 35806             {
 35807               /* The mouse-highlighting comes from a display string
 35808                  with a mouse-face.  */
 35809               Lisp_Object s, e;
 35810               ptrdiff_t ignore;
 35811 
 35812               s = Fprevious_single_property_change
 35813                 (make_fixnum (pos + 1), Qmouse_face, object, Qnil);
 35814               e = Fnext_single_property_change
 35815                 (position, Qmouse_face, object, Qnil);
 35816               if (NILP (s))
 35817                 s = make_fixnum (0);
 35818               if (NILP (e))
 35819                 e = make_fixnum (SCHARS (object));
 35820               mouse_face_from_string_pos (w, hlinfo, object,
 35821                                           XFIXNUM (s), XFIXNUM (e));
 35822               hlinfo->mouse_face_past_end = false;
 35823               hlinfo->mouse_face_window = window;
 35824               hlinfo->mouse_face_face_id
 35825                 = face_at_string_position (w, object, pos, 0, &ignore,
 35826                                            glyph->face_id, true, 0);
 35827               show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35828               cursor = No_Cursor;
 35829             }
 35830           else
 35831             {
 35832               /* The mouse-highlighting, if any, comes from an overlay
 35833                  or text property in the buffer.  */
 35834               Lisp_Object buffer UNINIT;
 35835               Lisp_Object disp_string UNINIT;
 35836 
 35837               if (STRINGP (object))
 35838                 {
 35839                   /* If we are on a display string with no mouse-face,
 35840                      check if the text under it has one.  */
 35841                   struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 35842                   ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35843                   pos = string_buffer_position (object, start);
 35844                   if (pos > 0)
 35845                     {
 35846                       mouse_face = get_char_property_and_overlay
 35847                         (make_fixnum (pos), Qmouse_face, w->contents, &overlay);
 35848                       buffer = w->contents;
 35849                       disp_string = object;
 35850                     }
 35851                 }
 35852               else
 35853                 {
 35854                   buffer = object;
 35855                   disp_string = Qnil;
 35856                 }
 35857 
 35858               if (!NILP (mouse_face))
 35859                 {
 35860                   Lisp_Object before, after;
 35861                   Lisp_Object before_string, after_string;
 35862                   /* To correctly find the limits of mouse highlight
 35863                      in a bidi-reordered buffer, we must not use the
 35864                      optimization of limiting the search in
 35865                      previous-single-property-change and
 35866                      next-single-property-change, because
 35867                      rows_from_pos_range needs the real start and end
 35868                      positions to DTRT in this case.  That's because
 35869                      the first row visible in a window does not
 35870                      necessarily display the character whose position
 35871                      is the smallest.  */
 35872                   Lisp_Object lim1
 35873                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35874                     ? Fmarker_position (w->start)
 35875                     : Qnil;
 35876                   Lisp_Object lim2
 35877                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35878                     ? make_fixnum (BUF_Z (XBUFFER (buffer))
 35879                                    - w->window_end_pos)
 35880                     : Qnil;
 35881 
 35882                   if (NILP (overlay))
 35883                     {
 35884                       /* Handle the text property case.  */
 35885                       before = Fprevious_single_property_change
 35886                         (make_fixnum (pos + 1), Qmouse_face, buffer, lim1);
 35887                       after = Fnext_single_property_change
 35888                         (make_fixnum (pos), Qmouse_face, buffer, lim2);
 35889                       before_string = after_string = Qnil;
 35890                     }
 35891                   else
 35892                     {
 35893                       /* Handle the overlay case.  */
 35894                       before = Foverlay_start (overlay);
 35895                       after = Foverlay_end (overlay);
 35896                       before_string = Foverlay_get (overlay, Qbefore_string);
 35897                       after_string = Foverlay_get (overlay, Qafter_string);
 35898 
 35899                       if (!STRINGP (before_string)) before_string = Qnil;
 35900                       if (!STRINGP (after_string))  after_string = Qnil;
 35901                     }
 35902 
 35903                   mouse_face_from_buffer_pos (window, hlinfo, pos,
 35904                                               NILP (before)
 35905                                               ? 1
 35906                                               : XFIXNAT (before),
 35907                                               NILP (after)
 35908                                               ? BUF_Z (XBUFFER (buffer))
 35909                                               : XFIXNAT (after),
 35910                                               before_string, after_string,
 35911                                               disp_string);
 35912                   cursor = No_Cursor;
 35913                 }
 35914             }
 35915         }
 35916 
 35917     check_help_echo:
 35918 
 35919       /* Look for a `help-echo' property.  */
 35920       if (NILP (help_echo_string)) {
 35921         Lisp_Object help, overlay;
 35922 
 35923         /* Check overlays first.  */
 35924         help = overlay = Qnil;
 35925         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
 35926           {
 35927             overlay = overlay_vec[i];
 35928             help = Foverlay_get (overlay, Qhelp_echo);
 35929           }
 35930 
 35931         if (!NILP (help))
 35932           {
 35933             help_echo_string = help;
 35934             help_echo_window = window;
 35935             help_echo_object = overlay;
 35936             help_echo_pos = pos;
 35937           }
 35938         else
 35939           {
 35940             Lisp_Object obj = glyph->object;
 35941             ptrdiff_t charpos = glyph->charpos;
 35942 
 35943             /* Try text properties.  */
 35944             if (STRINGP (obj)
 35945                 && charpos >= 0
 35946                 && charpos < SCHARS (obj))
 35947               {
 35948                 help = Fget_text_property (make_fixnum (charpos),
 35949                                            Qhelp_echo, obj);
 35950                 if (NILP (help))
 35951                   {
 35952                     /* If the string itself doesn't specify a help-echo,
 35953                        see if the buffer text ``under'' it does.  */
 35954                     struct glyph_row *r
 35955                       = MATRIX_ROW (w->current_matrix, vpos);
 35956                     ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35957                     ptrdiff_t p = string_buffer_position (obj, start);
 35958                     if (p > 0)
 35959                       {
 35960                         help = Fget_char_property (make_fixnum (p),
 35961                                                    Qhelp_echo, w->contents);
 35962                         if (!NILP (help))
 35963                           {
 35964                             charpos = p;
 35965                             obj = w->contents;
 35966                           }
 35967                       }
 35968                   }
 35969               }
 35970             else if (BUFFERP (obj)
 35971                      && charpos >= BEGV
 35972                      && charpos < ZV)
 35973               help = Fget_text_property (make_fixnum (charpos), Qhelp_echo,
 35974                                          obj);
 35975 
 35976             if (!NILP (help))
 35977               {
 35978                 help_echo_string = help;
 35979                 help_echo_window = window;
 35980                 help_echo_object = obj;
 35981                 help_echo_pos = charpos;
 35982               }
 35983           }
 35984       }
 35985 
 35986 #ifdef HAVE_WINDOW_SYSTEM
 35987       /* Look for a `pointer' property.  */
 35988       if (FRAME_WINDOW_P (f) && NILP (pointer))
 35989         {
 35990           /* Check overlays first.  */
 35991           for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
 35992             pointer = Foverlay_get (overlay_vec[i], Qpointer);
 35993 
 35994           if (NILP (pointer))
 35995             {
 35996               Lisp_Object obj = glyph->object;
 35997               ptrdiff_t charpos = glyph->charpos;
 35998 
 35999               /* Try text properties.  */
 36000               if (STRINGP (obj)
 36001                   && charpos >= 0
 36002                   && charpos < SCHARS (obj))
 36003                 {
 36004                   pointer = Fget_text_property (make_fixnum (charpos),
 36005                                                 Qpointer, obj);
 36006                   if (NILP (pointer))
 36007                     {
 36008                       /* If the string itself doesn't specify a pointer,
 36009                          see if the buffer text ``under'' it does.  */
 36010                       struct glyph_row *r
 36011                         = MATRIX_ROW (w->current_matrix, vpos);
 36012                       ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 36013                       ptrdiff_t p = string_buffer_position (obj, start);
 36014                       if (p > 0)
 36015                         pointer = Fget_char_property (make_fixnum (p),
 36016                                                       Qpointer, w->contents);
 36017                     }
 36018                 }
 36019               else if (BUFFERP (obj)
 36020                        && charpos >= BEGV
 36021                        && charpos < ZV)
 36022                 pointer = Fget_text_property (make_fixnum (charpos),
 36023                                               Qpointer, obj);
 36024             }
 36025         }
 36026 #endif  /* HAVE_WINDOW_SYSTEM */
 36027 
 36028       BEGV = obegv;
 36029       ZV = ozv;
 36030       current_buffer = obuf;
 36031       SAFE_FREE ();
 36032     }
 36033 
 36034  set_cursor:
 36035   define_frame_cursor1 (f, cursor, pointer);
 36036 }
 36037 
 36038 
 36039 /* EXPORT for RIF:
 36040    Clear any mouse-face on window W.  This function is part of the
 36041    redisplay interface, and is called from try_window_id and similar
 36042    functions to ensure the mouse-highlight is off.  */
 36043 
 36044 void
 36045 gui_clear_window_mouse_face (struct window *w)
 36046 {
 36047   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 36048   Lisp_Object window;
 36049 
 36050   block_input ();
 36051   XSETWINDOW (window, w);
 36052   if (EQ (window, hlinfo->mouse_face_window))
 36053     clear_mouse_face (hlinfo);
 36054   unblock_input ();
 36055 }
 36056 
 36057 
 36058 /* EXPORT:
 36059    Just discard the mouse face information for frame F, if any.
 36060    This is used when the size of F is changed.  */
 36061 
 36062 void
 36063 cancel_mouse_face (struct frame *f)
 36064 {
 36065   Lisp_Object window;
 36066   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36067 
 36068   window = hlinfo->mouse_face_window;
 36069   if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
 36070     reset_mouse_highlight (hlinfo);
 36071 }
 36072 
 36073 
 36074 
 36075 /***********************************************************************
 36076                            Exposure Events
 36077  ***********************************************************************/
 36078 
 36079 #ifdef HAVE_WINDOW_SYSTEM
 36080 
 36081 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
 36082    which intersects rectangle R.  R is in window-relative coordinates.  */
 36083 
 36084 static void
 36085 expose_area (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r,
 36086              enum glyph_row_area area)
 36087 {
 36088   struct glyph *first = row->glyphs[area];
 36089   struct glyph *end = row->glyphs[area] + row->used[area];
 36090   struct glyph *last;
 36091   int first_x, start_x, x;
 36092 
 36093   if (area == TEXT_AREA && row->fill_line_p)
 36094     /* If row extends face to end of line write the whole line.  */
 36095     draw_glyphs (w, row->x, row, area,
 36096                  0, row->used[area],
 36097                  DRAW_NORMAL_TEXT, 0);
 36098   else
 36099     {
 36100       /* Set START_X to the window-relative start position for drawing glyphs of
 36101          AREA.  The first glyph of the text area can be partially visible.
 36102          The first glyphs of other areas cannot.  */
 36103       start_x = window_box_left_offset (w, area);
 36104       x = start_x;
 36105       if (area == TEXT_AREA)
 36106         x += row->x;
 36107 
 36108       /* Find the first glyph that must be redrawn.  */
 36109       while (first < end
 36110              && x + first->pixel_width < r->x)
 36111         {
 36112           x += first->pixel_width;
 36113           ++first;
 36114         }
 36115 
 36116       /* Find the last one.  */
 36117       last = first;
 36118       first_x = x;
 36119       /* Use a signed int intermediate value to avoid catastrophic
 36120          failures due to comparison between signed and unsigned, when
 36121          x is negative (can happen for wide images that are hscrolled).  */
 36122       int r_end = r->x + r->width;
 36123       while (last < end && x < r_end)
 36124         {
 36125           x += last->pixel_width;
 36126           ++last;
 36127         }
 36128 
 36129       /* Repaint.  */
 36130       if (last > first)
 36131         draw_glyphs (w, first_x - start_x, row, area,
 36132                      first - row->glyphs[area], last - row->glyphs[area],
 36133                      DRAW_NORMAL_TEXT, 0);
 36134     }
 36135 }
 36136 
 36137 
 36138 /* Redraw the parts of the glyph row ROW on window W intersecting
 36139    rectangle R.  R is in window-relative coordinates.  Value is
 36140    true if mouse-face was overwritten.  */
 36141 
 36142 static bool
 36143 expose_line (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r)
 36144 {
 36145   eassert (row->enabled_p);
 36146 
 36147   if (row->mode_line_p || w->pseudo_window_p)
 36148     draw_glyphs (w, 0, row, TEXT_AREA,
 36149                  0, row->used[TEXT_AREA],
 36150                  DRAW_NORMAL_TEXT, 0);
 36151   else
 36152     {
 36153       if (row->used[LEFT_MARGIN_AREA])
 36154         expose_area (w, row, r, LEFT_MARGIN_AREA);
 36155       if (row->used[TEXT_AREA])
 36156         expose_area (w, row, r, TEXT_AREA);
 36157       if (row->used[RIGHT_MARGIN_AREA])
 36158         expose_area (w, row, r, RIGHT_MARGIN_AREA);
 36159       draw_row_fringe_bitmaps (w, row);
 36160     }
 36161 
 36162   return row->mouse_face_p;
 36163 }
 36164 
 36165 
 36166 /* Redraw those parts of glyphs rows during expose event handling that
 36167    overlap other rows.  Redrawing of an exposed line writes over parts
 36168    of lines overlapping that exposed line; this function fixes that.
 36169 
 36170    W is the window being exposed.  FIRST_OVERLAPPING_ROW is the first
 36171    row in W's current matrix that is exposed and overlaps other rows.
 36172    LAST_OVERLAPPING_ROW is the last such row.  */
 36173 
 36174 static void
 36175 expose_overlaps (struct window *w,
 36176                  struct glyph_row *first_overlapping_row,
 36177                  struct glyph_row *last_overlapping_row,
 36178                  const Emacs_Rectangle *r)
 36179 {
 36180   struct glyph_row *row;
 36181 
 36182   for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
 36183     if (row->overlapping_p)
 36184       {
 36185         eassert (row->enabled_p && !row->mode_line_p);
 36186 
 36187         row->clip = r;
 36188         if (row->used[LEFT_MARGIN_AREA])
 36189           gui_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
 36190 
 36191         if (row->used[TEXT_AREA])
 36192           gui_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
 36193 
 36194         if (row->used[RIGHT_MARGIN_AREA])
 36195           gui_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
 36196         row->clip = NULL;
 36197       }
 36198 }
 36199 
 36200 
 36201 /* Return true if W's cursor intersects rectangle R.  */
 36202 
 36203 static bool
 36204 phys_cursor_in_rect_p (struct window *w, const Emacs_Rectangle *r)
 36205 {
 36206   Emacs_Rectangle cr, result;
 36207   struct glyph *cursor_glyph;
 36208   struct glyph_row *row;
 36209 
 36210   if (w->phys_cursor.vpos >= 0
 36211       && w->phys_cursor.vpos < w->current_matrix->nrows
 36212       && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
 36213           row->enabled_p)
 36214       && row->cursor_in_fringe_p)
 36215     {
 36216       /* Cursor is in the fringe.  */
 36217       cr.x = window_box_right_offset (w,
 36218                                       (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
 36219                                        ? RIGHT_MARGIN_AREA
 36220                                        : TEXT_AREA));
 36221       cr.y = row->y;
 36222       cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
 36223       cr.height = row->height;
 36224       return gui_intersect_rectangles (&cr, r, &result);
 36225     }
 36226 
 36227   cursor_glyph = get_phys_cursor_glyph (w);
 36228   if (cursor_glyph)
 36229     {
 36230       /* r is relative to W's box, but w->phys_cursor.x is relative
 36231          to left edge of W's TEXT area.  Adjust it.  */
 36232       cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
 36233       cr.y = w->phys_cursor.y;
 36234       cr.width = cursor_glyph->pixel_width;
 36235       cr.height = w->phys_cursor_height;
 36236       /* ++KFS: W32 version used W32-specific IntersectRect here, but
 36237          I assume the effect is the same -- and this is portable.  */
 36238       return gui_intersect_rectangles (&cr, r, &result);
 36239     }
 36240   /* If we don't understand the format, pretend we're not in the hot-spot.  */
 36241   return false;
 36242 }
 36243 
 36244 
 36245 /* EXPORT:
 36246    Draw a vertical window border to the right of window W if W doesn't
 36247    have vertical scroll bars.  */
 36248 
 36249 void
 36250 gui_draw_vertical_border (struct window *w)
 36251 {
 36252   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36253 
 36254   /* We could do better, if we knew what type of scroll-bar the adjacent
 36255      windows (on either side) have...  But we don't :-(
 36256      However, I think this works ok.  ++KFS 2003-04-25 */
 36257 
 36258   /* Redraw borders between horizontally adjacent windows.  Don't
 36259      do it for frames with vertical scroll bars because either the
 36260      right scroll bar of a window, or the left scroll bar of its
 36261      neighbor will suffice as a border.  */
 36262   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
 36263     return;
 36264 
 36265   /* Note: It is necessary to redraw both the left and the right
 36266      borders, for when only this single window W is being
 36267      redisplayed.  */
 36268   if (!WINDOW_RIGHTMOST_P (w)
 36269       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
 36270     {
 36271       int x0, x1, y0, y1;
 36272 
 36273       window_box_edges (w, &x0, &y0, &x1, &y1);
 36274       y1 -= 1;
 36275 
 36276       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36277         x1 -= 1;
 36278 
 36279       FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
 36280     }
 36281 
 36282   if (!WINDOW_LEFTMOST_P (w)
 36283       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
 36284     {
 36285       int x0, x1, y0, y1;
 36286 
 36287       window_box_edges (w, &x0, &y0, &x1, &y1);
 36288       y1 -= 1;
 36289 
 36290       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36291         x0 -= 1;
 36292 
 36293       FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
 36294     }
 36295 }
 36296 
 36297 
 36298 /* Draw window dividers for window W.  */
 36299 
 36300 void
 36301 gui_draw_right_divider (struct window *w)
 36302 {
 36303   struct frame *f = WINDOW_XFRAME (w);
 36304 
 36305   if (w->mini || w->pseudo_window_p)
 36306     return;
 36307   else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36308     {
 36309       int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36310       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36311       int y0 = WINDOW_TOP_EDGE_Y (w);
 36312       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36313 
 36314       /* If W is horizontally combined and has a right sibling, don't
 36315          draw over any bottom divider.  */
 36316       if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
 36317           && !NILP (w->parent)
 36318           && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
 36319           && !NILP (w->next))
 36320         y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36321 
 36322       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36323     }
 36324 }
 36325 
 36326 static void
 36327 gui_draw_bottom_divider (struct window *w)
 36328 {
 36329   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36330 
 36331   if (w->mini || w->pseudo_window_p)
 36332     return;
 36333   else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36334     {
 36335       int x0 = WINDOW_LEFT_EDGE_X (w);
 36336       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36337       int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36338       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36339       struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
 36340 
 36341       /* If W is vertically combined and has a sibling below, don't draw
 36342          over any right divider.  */
 36343       if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
 36344           && p
 36345           && ((WINDOW_VERTICAL_COMBINATION_P (p)
 36346                && !NILP (w->next))
 36347               || (WINDOW_HORIZONTAL_COMBINATION_P (p)
 36348                   && NILP (w->next)
 36349                   && !NILP (p->parent)
 36350                   && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
 36351                   && !NILP (XWINDOW (p->parent)->next))))
 36352         x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36353 
 36354       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36355     }
 36356 }
 36357 
 36358 /* Redraw the part of window W intersection rectangle FR.  Pixel
 36359    coordinates in FR are frame-relative.  Call this function with
 36360    input blocked.  Value is true if the exposure overwrites
 36361    mouse-face.  */
 36362 
 36363 static bool
 36364 expose_window (struct window *w, const Emacs_Rectangle *fr)
 36365 {
 36366   struct frame *f = XFRAME (w->frame);
 36367   Emacs_Rectangle wr, r;
 36368   bool mouse_face_overwritten_p = false;
 36369 
 36370   /* If window is not yet fully initialized, do nothing.  This can
 36371      happen when toolkit scroll bars are used and a window is split.
 36372      Reconfiguring the scroll bar will generate an expose for a newly
 36373      created window.  */
 36374   if (w->current_matrix == NULL)
 36375     return false;
 36376 
 36377   /* When we're currently updating the window, display and current
 36378      matrix usually don't agree.  Arrange for a thorough display
 36379      later.  */
 36380   if (w->must_be_updated_p)
 36381     {
 36382       SET_FRAME_GARBAGED (f);
 36383       return false;
 36384     }
 36385 
 36386   /* Frame-relative pixel rectangle of W.  */
 36387   wr.x = WINDOW_LEFT_EDGE_X (w);
 36388   wr.y = WINDOW_TOP_EDGE_Y (w);
 36389   wr.width = WINDOW_PIXEL_WIDTH (w);
 36390   wr.height = WINDOW_PIXEL_HEIGHT (w);
 36391 
 36392   if (gui_intersect_rectangles (fr, &wr, &r))
 36393     {
 36394       int yb = window_text_bottom_y (w);
 36395       struct glyph_row *row;
 36396       struct glyph_row *first_overlapping_row, *last_overlapping_row;
 36397 
 36398       redisplay_trace ("expose_window (%d, %d, %u, %u)\n",
 36399                        r.x, r.y, r.width, r.height);
 36400 
 36401       /* Convert to window coordinates.  */
 36402       r.x -= WINDOW_LEFT_EDGE_X (w);
 36403       r.y -= WINDOW_TOP_EDGE_Y (w);
 36404 
 36405       /* Turn off the cursor.  */
 36406       bool cursor_cleared_p = (!w->pseudo_window_p
 36407                                && phys_cursor_in_rect_p (w, &r));
 36408       if (cursor_cleared_p)
 36409         gui_clear_cursor (w);
 36410 
 36411       /* If the row containing the cursor extends face to end of line,
 36412          then expose_area might overwrite the cursor outside the
 36413          rectangle and thus notice_overwritten_cursor might clear
 36414          w->phys_cursor_on_p.  We remember the original value and
 36415          check later if it is changed.  */
 36416       bool phys_cursor_on_p = w->phys_cursor_on_p;
 36417 
 36418       /* Use a signed int intermediate value to avoid catastrophic
 36419          failures due to comparison between signed and unsigned, when
 36420          y0 or y1 is negative (can happen for tall images).  */
 36421       int r_bottom = r.y + r.height;
 36422 
 36423       /* We must temporarily switch to the window's buffer, in case
 36424          the fringe face has been remapped in that buffer's
 36425          face-remapping-alist, so that draw_row_fringe_bitmaps,
 36426          called from expose_line, will use the right face.  */
 36427       bool buffer_changed = false;
 36428       struct buffer *oldbuf = current_buffer;
 36429       if (!w->pseudo_window_p)
 36430         {
 36431           set_buffer_internal_1 (XBUFFER (w->contents));
 36432           buffer_changed = true;
 36433         }
 36434 
 36435       /* Update lines intersecting rectangle R.  */
 36436       first_overlapping_row = last_overlapping_row = NULL;
 36437       for (row = w->current_matrix->rows;
 36438            row->enabled_p;
 36439            ++row)
 36440         {
 36441           int y0 = row->y;
 36442           int y1 = MATRIX_ROW_BOTTOM_Y (row);
 36443 
 36444           if ((y0 >= r.y && y0 < r_bottom)
 36445               || (y1 > r.y && y1 < r_bottom)
 36446               || (r.y >= y0 && r.y < y1)
 36447               || (r_bottom > y0 && r_bottom < y1))
 36448             {
 36449               /* A header line may be overlapping, but there is no need
 36450                  to fix overlapping areas for them.  KFS 2005-02-12 */
 36451               if (row->overlapping_p && !row->mode_line_p)
 36452                 {
 36453                   if (first_overlapping_row == NULL)
 36454                     first_overlapping_row = row;
 36455                   last_overlapping_row = row;
 36456                 }
 36457 
 36458               row->clip = fr;
 36459               if (expose_line (w, row, &r))
 36460                 mouse_face_overwritten_p = true;
 36461               row->clip = NULL;
 36462             }
 36463           else if (row->overlapping_p)
 36464             {
 36465               /* We must redraw a row overlapping the exposed area.  */
 36466               if (y0 < r.y
 36467                   ? y0 + row->phys_height > r.y
 36468                   : y0 + row->ascent - row->phys_ascent < r.y +r.height)
 36469                 {
 36470                   if (first_overlapping_row == NULL)
 36471                     first_overlapping_row = row;
 36472                   last_overlapping_row = row;
 36473                 }
 36474             }
 36475 
 36476           if (y1 >= yb)
 36477             break;
 36478         }
 36479 
 36480       if (buffer_changed)
 36481         set_buffer_internal_1 (oldbuf);
 36482 
 36483       /* Display the mode line if there is one.  */
 36484       if (window_wants_mode_line (w)
 36485           && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
 36486               row->enabled_p)
 36487           && row->y < r_bottom)
 36488         {
 36489           if (expose_line (w, row, &r))
 36490             mouse_face_overwritten_p = true;
 36491         }
 36492 
 36493       if (!w->pseudo_window_p)
 36494         {
 36495           /* Fix the display of overlapping rows.  */
 36496           if (first_overlapping_row)
 36497             expose_overlaps (w, first_overlapping_row, last_overlapping_row,
 36498                              fr);
 36499 
 36500           /* Draw border between windows.  */
 36501           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36502             gui_draw_right_divider (w);
 36503           else
 36504             gui_draw_vertical_border (w);
 36505 
 36506           if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36507             gui_draw_bottom_divider (w);
 36508 
 36509           /* Turn the cursor on again.  */
 36510           if (cursor_cleared_p
 36511               || (phys_cursor_on_p && !w->phys_cursor_on_p))
 36512             update_window_cursor (w, true);
 36513         }
 36514     }
 36515 
 36516   return mouse_face_overwritten_p;
 36517 }
 36518 
 36519 
 36520 
 36521 /* Redraw (parts) of all windows in the window tree rooted at W that
 36522    intersect R.  R contains frame pixel coordinates.  Value is
 36523    true if the exposure overwrites mouse-face.  */
 36524 
 36525 static bool
 36526 expose_window_tree (struct window *w, const Emacs_Rectangle *r)
 36527 {
 36528   struct frame *f = XFRAME (w->frame);
 36529   bool mouse_face_overwritten_p = false;
 36530 
 36531   while (w && !FRAME_GARBAGED_P (f))
 36532     {
 36533       mouse_face_overwritten_p
 36534         |= (WINDOWP (w->contents)
 36535             ? expose_window_tree (XWINDOW (w->contents), r)
 36536             : expose_window (w, r));
 36537 
 36538       w = NILP (w->next) ? NULL : XWINDOW (w->next);
 36539     }
 36540 
 36541   return mouse_face_overwritten_p;
 36542 }
 36543 
 36544 
 36545 /* EXPORT:
 36546    Redisplay an exposed area of frame F.  X and Y are the upper-left
 36547    corner of the exposed rectangle.  W and H are width and height of
 36548    the exposed area.  All are pixel values.  W or H zero means redraw
 36549    the entire frame.  */
 36550 
 36551 void
 36552 expose_frame (struct frame *f, int x, int y, int w, int h)
 36553 {
 36554   Emacs_Rectangle r;
 36555   bool mouse_face_overwritten_p = false;
 36556 
 36557   if (FRAME_GARBAGED_P (f))
 36558     {
 36559       redisplay_trace ("expose_frame garbaged\n");
 36560       return;
 36561     }
 36562 
 36563   /* If basic faces haven't been realized yet, there is no point in
 36564      trying to redraw anything.  This can happen when we get an expose
 36565      event while Emacs is starting, e.g. by moving another window.  */
 36566   if (FRAME_FACE_CACHE (f) == NULL
 36567       || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
 36568     {
 36569       redisplay_trace ("expose_frame no faces\n");
 36570       return;
 36571     }
 36572 
 36573   if (w == 0 || h == 0)
 36574     {
 36575       r.x = r.y = 0;
 36576       r.width = FRAME_TEXT_WIDTH (f);
 36577       r.height = FRAME_TEXT_HEIGHT (f);
 36578     }
 36579   else
 36580     {
 36581       r.x = x;
 36582       r.y = y;
 36583       r.width = w;
 36584       r.height = h;
 36585     }
 36586 
 36587   redisplay_trace ("expose_frame (%d, %d, %u, %u)\n",
 36588                    r.x, r.y, r.width, r.height);
 36589   mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
 36590 
 36591   if (WINDOWP (f->tab_bar_window))
 36592     mouse_face_overwritten_p
 36593       |= expose_window (XWINDOW (f->tab_bar_window), &r);
 36594 
 36595 #ifndef HAVE_EXT_TOOL_BAR
 36596   if (WINDOWP (f->tool_bar_window))
 36597     mouse_face_overwritten_p
 36598       |= expose_window (XWINDOW (f->tool_bar_window), &r);
 36599 #endif
 36600 
 36601 #if defined HAVE_WINDOW_SYSTEM && !defined HAVE_EXT_MENU_BAR
 36602   if (WINDOWP (f->menu_bar_window))
 36603     mouse_face_overwritten_p
 36604       |= expose_window (XWINDOW (f->menu_bar_window), &r);
 36605 #endif
 36606 
 36607   /* Some window managers support a focus-follows-mouse style with
 36608      delayed raising of frames.  Imagine a partially obscured frame,
 36609      and moving the mouse into partially obscured mouse-face on that
 36610      frame.  The visible part of the mouse-face will be highlighted,
 36611      then the WM raises the obscured frame.  With at least one WM, KDE
 36612      2.1, Emacs is not getting any event for the raising of the frame
 36613      (even tried with SubstructureRedirectMask), only Expose events.
 36614      These expose events will draw text normally, i.e. not
 36615      highlighted.  Which means we must redo the highlight here.
 36616      Subsume it under ``we love X''.  --gerd 2001-08-15  */
 36617   /* Included in Windows version because Windows most likely does not
 36618      do the right thing if any third party tool offers
 36619      focus-follows-mouse with delayed raise.  --jason 2001-10-12  */
 36620   if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
 36621     {
 36622       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36623       if (f == hlinfo->mouse_face_mouse_frame)
 36624         {
 36625           int mouse_x = hlinfo->mouse_face_mouse_x;
 36626           int mouse_y = hlinfo->mouse_face_mouse_y;
 36627           clear_mouse_face (hlinfo);
 36628           note_mouse_highlight (f, mouse_x, mouse_y);
 36629         }
 36630     }
 36631 }
 36632 
 36633 
 36634 /* EXPORT:
 36635    Determine the intersection of two rectangles R1 and R2.  Return
 36636    the intersection in *RESULT.  Value is true if RESULT is not
 36637    empty.  */
 36638 
 36639 bool
 36640 gui_intersect_rectangles (const Emacs_Rectangle *r1, const Emacs_Rectangle *r2,
 36641                           Emacs_Rectangle *result)
 36642 {
 36643   const Emacs_Rectangle *left, *right;
 36644   const Emacs_Rectangle *upper, *lower;
 36645   bool intersection_p = false;
 36646 
 36647   /* Rearrange so that R1 is the left-most rectangle.  */
 36648   if (r1->x < r2->x)
 36649     left = r1, right = r2;
 36650   else
 36651     left = r2, right = r1;
 36652 
 36653   /* X0 of the intersection is right.x0, if this is inside R1,
 36654      otherwise there is no intersection.  */
 36655   if (right->x <= left->x + left->width)
 36656     {
 36657       result->x = right->x;
 36658 
 36659       /* The right end of the intersection is the minimum of
 36660          the right ends of left and right.  */
 36661       result->width = (min (left->x + left->width, right->x + right->width)
 36662                        - result->x);
 36663 
 36664       /* Same game for Y.  */
 36665       if (r1->y < r2->y)
 36666         upper = r1, lower = r2;
 36667       else
 36668         upper = r2, lower = r1;
 36669 
 36670       /* The upper end of the intersection is lower.y0, if this is inside
 36671          of upper.  Otherwise, there is no intersection.  */
 36672       if (lower->y <= upper->y + upper->height)
 36673         {
 36674           result->y = lower->y;
 36675 
 36676           /* The lower end of the intersection is the minimum of the lower
 36677              ends of upper and lower.  */
 36678           result->height = (min (lower->y + lower->height,
 36679                                  upper->y + upper->height)
 36680                             - result->y);
 36681           intersection_p = true;
 36682         }
 36683     }
 36684 
 36685   return intersection_p;
 36686 }
 36687 
 36688 /* EXPORT:
 36689    Determine the union of the rectangles A and B.  Return the smallest
 36690    rectangle encompassing both the bounds of A and B in *RESULT.  It
 36691    is safe for all three arguments to point to each other.  */
 36692 
 36693 void
 36694 gui_union_rectangles (const Emacs_Rectangle *a, const Emacs_Rectangle *b,
 36695                       Emacs_Rectangle *result)
 36696 {
 36697   struct gui_box a_box, b_box, result_box;
 36698 
 36699   /* Handle special cases where one of the rectangles is empty.  */
 36700 
 36701   if (!a->width || !a->height)
 36702     {
 36703       *result = *b;
 36704       return;
 36705     }
 36706   else if (!b->width || !b->height)
 36707     {
 36708       *result = *a;
 36709       return;
 36710     }
 36711 
 36712   /* Convert A and B to boxes.  */
 36713   a_box.x1 = a->x;
 36714   a_box.y1 = a->y;
 36715   a_box.x2 = a->x + a->width;
 36716   a_box.y2 = a->y + a->height;
 36717 
 36718   b_box.x1 = b->x;
 36719   b_box.y1 = b->y;
 36720   b_box.x2 = b->x + b->width;
 36721   b_box.y2 = b->y + b->height;
 36722 
 36723   /* Compute the union of the boxes.  */
 36724   result_box.x1 = min (a_box.x1, b_box.x1);
 36725   result_box.y1 = min (a_box.y1, b_box.y1);
 36726   result_box.x2 = max (a_box.x2, b_box.x2);
 36727   result_box.y2 = max (a_box.y2, b_box.y2);
 36728 
 36729   /* Convert result_box to an XRectangle and put the result in
 36730      RESULT.  */
 36731   result->x = result_box.x1;
 36732   result->y = result_box.y1;
 36733   result->width = result_box.x2 - result_box.x1;
 36734   result->height = result_box.y2 - result_box.y1;
 36735 }
 36736 
 36737 #endif /* HAVE_WINDOW_SYSTEM */
 36738 
 36739 
 36740 /***********************************************************************
 36741                             Initialization
 36742  ***********************************************************************/
 36743 
 36744 void
 36745 syms_of_xdisp (void)
 36746 {
 36747   Vwith_echo_area_save_vector = Qnil;
 36748   staticpro (&Vwith_echo_area_save_vector);
 36749 
 36750   Vmessage_stack = Qnil;
 36751   staticpro (&Vmessage_stack);
 36752 
 36753   /* Non-nil means don't actually do any redisplay.  */
 36754   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
 36755 
 36756   DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
 36757 
 36758   DEFVAR_BOOL ("scroll-minibuffer-conservatively",
 36759                scroll_minibuffer_conservatively,
 36760                doc: /* Non-nil means scroll conservatively in minibuffer windows.
 36761 When the value is nil, scrolling in minibuffer windows obeys the
 36762 settings of `scroll-conservatively'.  */);
 36763   scroll_minibuffer_conservatively = true; /* bug#44070 */
 36764 
 36765   DEFVAR_BOOL ("inhibit-message", inhibit_message,
 36766               doc:  /* Non-nil means calls to `message' are not displayed.
 36767 They are still logged to the *Messages* buffer.
 36768 
 36769 Do NOT set this globally to a non-nil value, as doing that will
 36770 disable messages everywhere, including in I-search and other
 36771 places where they are necessary.  This variable is intended to
 36772 be let-bound around code that needs to disable messages temporarily. */);
 36773   inhibit_message = false;
 36774 
 36775   message_dolog_marker1 = Fmake_marker ();
 36776   staticpro (&message_dolog_marker1);
 36777   message_dolog_marker2 = Fmake_marker ();
 36778   staticpro (&message_dolog_marker2);
 36779   message_dolog_marker3 = Fmake_marker ();
 36780   staticpro (&message_dolog_marker3);
 36781 
 36782   defsubr (&Sset_buffer_redisplay);
 36783 #ifdef GLYPH_DEBUG
 36784   defsubr (&Sdump_frame_glyph_matrix);
 36785   defsubr (&Sdump_glyph_matrix);
 36786   defsubr (&Sdump_glyph_row);
 36787   defsubr (&Sdump_tab_bar_row);
 36788   defsubr (&Sdump_tool_bar_row);
 36789   defsubr (&Strace_redisplay);
 36790   defsubr (&Strace_to_stderr);
 36791 #endif
 36792 #ifdef HAVE_WINDOW_SYSTEM
 36793   defsubr (&Stab_bar_height);
 36794   defsubr (&Stool_bar_height);
 36795   defsubr (&Slookup_image_map);
 36796 #endif
 36797   defsubr (&Sline_pixel_height);
 36798   defsubr (&Sformat_mode_line);
 36799   defsubr (&Sinvisible_p);
 36800   defsubr (&Scurrent_bidi_paragraph_direction);
 36801   defsubr (&Swindow_text_pixel_size);
 36802   defsubr (&Sbuffer_text_pixel_size);
 36803   defsubr (&Smove_point_visually);
 36804   defsubr (&Sbidi_find_overridden_directionality);
 36805   defsubr (&Sdisplay__line_is_continued_p);
 36806   defsubr (&Sget_display_property);
 36807   defsubr (&Slong_line_optimizations_p);
 36808 
 36809   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
 36810   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
 36811   DEFSYM (Qoverriding_local_map, "overriding-local-map");
 36812   DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
 36813   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
 36814   DEFSYM (Qeval, "eval");
 36815   DEFSYM (QCdata, ":data");
 36816 
 36817   /* Names of text properties relevant for redisplay.  */
 36818   DEFSYM (Qdisplay, "display");
 36819   DEFSYM (Qspace_width, "space-width");
 36820   DEFSYM (Qraise, "raise");
 36821   DEFSYM (Qslice, "slice");
 36822   DEFSYM (Qspace, "space");
 36823   DEFSYM (Qmargin, "margin");
 36824   DEFSYM (Qpointer, "pointer");
 36825   DEFSYM (Qleft_margin, "left-margin");
 36826   DEFSYM (Qright_margin, "right-margin");
 36827   DEFSYM (Qcenter, "center");
 36828   DEFSYM (Qline_height, "line-height");
 36829   DEFSYM (QCalign_to, ":align-to");
 36830   DEFSYM (QCrelative_width, ":relative-width");
 36831   DEFSYM (QCrelative_height, ":relative-height");
 36832   DEFSYM (QCeval, ":eval");
 36833   DEFSYM (QCpropertize, ":propertize");
 36834   DEFSYM (QCfile, ":file");
 36835   DEFSYM (Qfontified, "fontified");
 36836   DEFSYM (Qfontification_functions, "fontification-functions");
 36837   DEFSYM (Qlong_line_optimizations_in_fontification_functions,
 36838           "long-line-optimizations-in-fontification-functions");
 36839 
 36840   /* Name of the symbol which disables Lisp evaluation in 'display'
 36841      properties.  This is used by enriched.el.  */
 36842   DEFSYM (Qdisable_eval, "disable-eval");
 36843 
 36844   /* Name of the face used to highlight trailing whitespace.  */
 36845   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
 36846 
 36847   /* Names of the faces used to display line numbers.  */
 36848   DEFSYM (Qline_number, "line-number");
 36849   DEFSYM (Qline_number_current_line, "line-number-current-line");
 36850   DEFSYM (Qline_number_major_tick, "line-number-major-tick");
 36851   DEFSYM (Qline_number_minor_tick, "line-number-minor-tick");
 36852   /* Name of a text property which disables line-number display.  */
 36853   DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
 36854 
 36855   /* Name of the face used to display fill column indicator character.  */
 36856   DEFSYM (Qfill_column_indicator, "fill-column-indicator");
 36857 
 36858   /* Name and number of the face used to highlight escape glyphs.  */
 36859   DEFSYM (Qescape_glyph, "escape-glyph");
 36860 
 36861   /* Name and number of the face used to highlight non-breaking
 36862      spaces/hyphens.  */
 36863   DEFSYM (Qnobreak_space, "nobreak-space");
 36864   DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
 36865 
 36866   /* The symbol 'image' which is the car of the lists used to represent
 36867      images in Lisp.  Also a tool bar style.  */
 36868   DEFSYM (Qimage, "image");
 36869 
 36870   /* Tool bar styles.  */
 36871   DEFSYM (Qtext, "text");
 36872   DEFSYM (Qboth, "both");
 36873   DEFSYM (Qboth_horiz, "both-horiz");
 36874   DEFSYM (Qtext_image_horiz, "text-image-horiz");
 36875 
 36876   /* The image map types.  */
 36877   DEFSYM (QCmap, ":map");
 36878   DEFSYM (QCpointer, ":pointer");
 36879   DEFSYM (Qrect, "rect");
 36880   DEFSYM (Qcircle, "circle");
 36881   DEFSYM (Qpoly, "poly");
 36882 
 36883   DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
 36884 
 36885   DEFSYM (Qgrow_only, "grow-only");
 36886   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
 36887   DEFSYM (Qposition, "position");
 36888   DEFSYM (Qbuffer_position, "buffer-position");
 36889   DEFSYM (Qobject, "object");
 36890 
 36891   /* Cursor shapes.  */
 36892   DEFSYM (Qbar, "bar");
 36893   DEFSYM (Qhbar, "hbar");
 36894   DEFSYM (Qbox, "box");
 36895   DEFSYM (Qhollow, "hollow");
 36896 
 36897   /* Pointer shapes.  */
 36898   DEFSYM (Qhand, "hand");
 36899   DEFSYM (Qarrow, "arrow");
 36900   /* also Qtext */
 36901 
 36902   DEFSYM (Qdragging, "dragging");
 36903   DEFSYM (Qdropping, "dropping");
 36904   DEFSYM (Qdrag_source, "drag-source");
 36905 
 36906   DEFSYM (Qdrag_with_mode_line, "drag-with-mode-line");
 36907   DEFSYM (Qdrag_with_header_line, "drag-with-header-line");
 36908   DEFSYM (Qdrag_with_tab_line, "drag-with-tab-line");
 36909 
 36910   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 36911 
 36912   list_of_error = list1 (Qerror);
 36913   staticpro (&list_of_error);
 36914 
 36915   /* Values of those variables at last redisplay are stored as
 36916      properties on 'overlay-arrow-position' symbol.  However, if
 36917      Voverlay_arrow_position is a marker, last-arrow-position is its
 36918      numerical position.  */
 36919   DEFSYM (Qlast_arrow_position, "last-arrow-position");
 36920   DEFSYM (Qlast_arrow_string, "last-arrow-string");
 36921 
 36922   /* Alternative overlay-arrow-string and overlay-arrow-bitmap
 36923      properties on a symbol in overlay-arrow-variable-list.  */
 36924   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
 36925   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 36926 
 36927   echo_buffer[0] = echo_buffer[1] = Qnil;
 36928   staticpro (&echo_buffer[0]);
 36929   staticpro (&echo_buffer[1]);
 36930 
 36931   echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
 36932   staticpro (&echo_area_buffer[0]);
 36933   staticpro (&echo_area_buffer[1]);
 36934 
 36935   DEFVAR_LISP ("messages-buffer-name", Vmessages_buffer_name,
 36936     doc: /* The name of the buffer where messages are logged.
 36937 This is normally \"\*Messages*\", but can be rebound by packages that
 36938 wish to redirect messages to a different buffer.  (If the buffer
 36939 doesn't exist, it will be created and put into
 36940 `messages-buffer-mode'.)  */);
 36941   Vmessages_buffer_name = build_string ("*Messages*");
 36942 
 36943   mode_line_proptrans_alist = Qnil;
 36944   staticpro (&mode_line_proptrans_alist);
 36945   mode_line_string_list = Qnil;
 36946   staticpro (&mode_line_string_list);
 36947   mode_line_string_face = Qnil;
 36948   staticpro (&mode_line_string_face);
 36949   mode_line_string_face_prop = Qnil;
 36950   staticpro (&mode_line_string_face_prop);
 36951   Vmode_line_unwind_vector = Qnil;
 36952   staticpro (&Vmode_line_unwind_vector);
 36953 
 36954   DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
 36955 
 36956   help_echo_string = Qnil;
 36957   staticpro (&help_echo_string);
 36958   help_echo_object = Qnil;
 36959   staticpro (&help_echo_object);
 36960   help_echo_window = Qnil;
 36961   staticpro (&help_echo_window);
 36962   previous_help_echo_string = Qnil;
 36963   staticpro (&previous_help_echo_string);
 36964   help_echo_pos = -1;
 36965 
 36966   DEFSYM (Qright_to_left, "right-to-left");
 36967   DEFSYM (Qleft_to_right, "left-to-right");
 36968   defsubr (&Sbidi_resolved_levels);
 36969 
 36970 #ifdef HAVE_WINDOW_SYSTEM
 36971   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
 36972     doc: /* Non-nil means draw block cursor as wide as the glyph under it.
 36973 For example, if a block cursor is over a tab, it will be drawn as
 36974 wide as that tab on the display.  */);
 36975   x_stretch_cursor_p = 0;
 36976 #endif
 36977 
 36978   DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
 36979     doc: /* Non-nil means highlight trailing whitespace.
 36980 The face used for trailing whitespace is `trailing-whitespace'.  */);
 36981   Vshow_trailing_whitespace = Qnil;
 36982 
 36983   DEFVAR_LISP ("mode-line-compact", Vmode_line_compact,
 36984     doc: /* Non-nil means that mode lines should be compact.
 36985 This means that repeating spaces will be replaced with a single space.
 36986 If this variable is `long', only mode lines that are wider than the
 36987 currently selected window are compressed. */);
 36988   Vmode_line_compact = Qnil;
 36989   DEFSYM (Qlong, "long");
 36990 
 36991   DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
 36992     doc: /* Control highlighting of non-ASCII space and hyphen chars.
 36993 If the value is t, Emacs highlights non-ASCII chars which have the
 36994 same appearance as an ASCII space or hyphen, using the `nobreak-space'
 36995 or `nobreak-hyphen' face respectively.
 36996 
 36997 All of the non-ASCII characters in the Unicode horizontal whitespace
 36998 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36999 U+2011 (non-breaking hyphen) are affected.
 37000 
 37001 Any other non-nil value means to display these characters as an escape
 37002 glyph followed by an ordinary space or hyphen.
 37003 
 37004 A value of nil means no special handling of these characters.  */);
 37005   Vnobreak_char_display = Qt;
 37006 
 37007   DEFVAR_BOOL ("nobreak-char-ascii-display", nobreak_char_ascii_display,
 37008     doc: /* Control display of non-ASCII space and hyphen chars.
 37009 If the value of this variable is nil, the default, Emacs displays
 37010 non-ASCII chars which have the same appearance as an ASCII space
 37011 or hyphen as themselves, with the `nobreak-space' or `nobreak-hyphen'
 37012 face, respectively.
 37013 
 37014 If the value is t, these characters are displayed as their ASCII
 37015 counterparts: whitespace characters as ASCII space, hyphen characters
 37016 as ASCII hyphen (a.k.a. \"dash\"), using the `nobreak-space' or
 37017 the `nobreak-hyphen' face.
 37018 
 37019 This variable has effect only if `nobreak-char-display' is t;
 37020 otherwise it is ignored.
 37021 
 37022 All of the non-ASCII characters in the Unicode horizontal whitespace
 37023 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 37024 U+2011 (non-breaking hyphen) are affected.  */);
 37025   nobreak_char_ascii_display = false;
 37026 
 37027   DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
 37028     doc: /* The pointer shape to show in void text areas.
 37029 A value of nil means to show the text pointer.  Other options are
 37030 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
 37031 `hourglass'.  */);
 37032   Vvoid_text_area_pointer = Qarrow;
 37033 
 37034   DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
 37035     doc: /* Non-nil means don't actually do any redisplay.
 37036 This is used for internal purposes.  */);
 37037   Vinhibit_redisplay = Qnil;
 37038 
 37039   DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
 37040     doc: /* String (or mode line construct) included (normally) in `mode-line-misc-info'.  */);
 37041   Vglobal_mode_string = Qnil;
 37042 
 37043   DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
 37044     doc: /* Marker for where to display an arrow on top of the buffer text.
 37045 This must be the beginning of a line in order to work.
 37046 See also `overlay-arrow-string'.  */);
 37047   Voverlay_arrow_position = Qnil;
 37048 
 37049   DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
 37050     doc: /* String to display as an arrow in non-window frames.
 37051 See also `overlay-arrow-position'.  */);
 37052   Voverlay_arrow_string = build_pure_c_string ("=>");
 37053 
 37054   DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
 37055     doc: /* List of variables (symbols) which hold markers for overlay arrows.
 37056 The symbols on this list are examined during redisplay to determine
 37057 where to display overlay arrows.  */);
 37058   Voverlay_arrow_variable_list
 37059     = list1 (intern_c_string ("overlay-arrow-position"));
 37060 
 37061   DEFVAR_INT ("scroll-step", emacs_scroll_step,
 37062     doc: /* The number of lines to try scrolling a window by when point moves out.
 37063 If that fails to bring point back on frame, point is centered instead.
 37064 If this is zero, point is always centered after it moves off frame.
 37065 If you want scrolling to always be a line at a time, you should set
 37066 `scroll-conservatively' to a large value rather than set this to 1.  */);
 37067 
 37068   DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
 37069     doc: /* Scroll up to this many lines, to bring point back on screen.
 37070 If point moves off-screen, redisplay will scroll by up to
 37071 `scroll-conservatively' lines in order to bring point just barely
 37072 onto the screen again.  If that cannot be done, then redisplay
 37073 recenters point as usual.
 37074 
 37075 If the value is greater than 100, redisplay will never recenter point,
 37076 but will always scroll just enough text to bring point into view, even
 37077 if you move far away.
 37078 
 37079 A value of zero means always recenter point if it moves off screen.  */);
 37080   scroll_conservatively = 0;
 37081 
 37082   DEFVAR_INT ("scroll-margin", scroll_margin,
 37083     doc: /* Number of lines of margin at the top and bottom of a window.
 37084 Trigger automatic scrolling whenever point gets within this many lines
 37085 of the top or bottom of the window (see info node `Auto Scrolling').  */);
 37086   scroll_margin = 0;
 37087 
 37088   DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
 37089     doc: /* Maximum effective value of `scroll-margin'.
 37090 Given as a fraction of the current window's lines.  The value should
 37091 be a floating point number between 0.0 and 0.5.  The effective maximum
 37092 is limited to (/ (1- window-lines) 2).  Non-float values for this
 37093 variable are ignored and the default 0.25 is used instead.  */);
 37094   Vmaximum_scroll_margin = make_float (0.25);
 37095 
 37096   DEFVAR_LISP ("display-pixels-per-inch",  Vdisplay_pixels_per_inch,
 37097     doc: /* Pixels per inch value for non-window system displays.
 37098 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI).  */);
 37099   Vdisplay_pixels_per_inch = make_float (72.0);
 37100 
 37101 #ifdef GLYPH_DEBUG
 37102   DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask.  */);
 37103 #endif
 37104 
 37105   DEFVAR_LISP ("truncate-partial-width-windows",
 37106                Vtruncate_partial_width_windows,
 37107     doc: /* Non-nil means truncate lines in windows narrower than the frame.
 37108 For an integer value, truncate lines in each window narrower than the
 37109 full frame width, provided the total window width in column units is less
 37110 than that integer; otherwise, respect the value of `truncate-lines'.
 37111 The total width of the window is as returned by `window-total-width', it
 37112 includes the fringes, the continuation and truncation glyphs, the
 37113 display margins (if any), and the scroll bar
 37114 
 37115 For any other non-nil value, truncate lines in all windows that do
 37116 not span the full frame width.
 37117 
 37118 A value of nil means to respect the value of `truncate-lines'.
 37119 
 37120 If `word-wrap' is enabled, you might want to reduce the value of this.
 37121 
 37122 Don't set this to a non-nil value when `visual-line-mode' is
 37123 turned on, as it could produce confusing results.  */);
 37124   Vtruncate_partial_width_windows = make_fixnum (50);
 37125 
 37126   DEFVAR_BOOL("word-wrap-by-category", word_wrap_by_category, doc: /*
 37127     Non-nil means also wrap after characters of a certain category.
 37128 Normally when `word-wrap' is on, Emacs only breaks lines after
 37129 whitespace characters.  When this option is turned on, Emacs also
 37130 breaks lines after characters that have the "|" category (defined in
 37131 characters.el).  This is useful for allowing breaking after CJK
 37132 characters and improves the word-wrapping for CJK text mixed with
 37133 Latin text.
 37134 
 37135 If this variable is set using Customize, Emacs automatically loads
 37136 kinsoku.el.  When kinsoku.el is loaded, Emacs respects kinsoku rules
 37137 when breaking lines.  That means characters with the ">" category
 37138 don't appear at the beginning of a line (e.g., FULLWIDTH COMMA), and
 37139 characters with the "<" category don't appear at the end of a line
 37140 (e.g., LEFT DOUBLE ANGLE BRACKET).  */);
 37141   word_wrap_by_category = false;
 37142 
 37143   DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
 37144     doc: /* Maximum buffer size for which line number should be displayed.
 37145 If the buffer is bigger than this, the line number does not appear
 37146 in the mode line.  A value of nil means no limit.  */);
 37147   Vline_number_display_limit = Qnil;
 37148 
 37149   DEFVAR_INT ("line-number-display-limit-width",
 37150               line_number_display_limit_width,
 37151     doc: /* Maximum line width (in characters) for line number display.
 37152 If the average length of the lines near point is bigger than this, then the
 37153 line number may be omitted from the mode line.  */);
 37154   line_number_display_limit_width = 200;
 37155 
 37156   DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
 37157     doc: /* Non-nil means highlight active region even in nonselected windows.
 37158 When nil (the default), the active region is only highlighted when
 37159 the window is selected.  */);
 37160   highlight_nonselected_windows = false;
 37161 
 37162   DEFVAR_BOOL ("multiple-frames", multiple_frames,
 37163     doc: /* Non-nil if more than one frame is visible on this display.
 37164 Minibuffer-only frames don't count, but iconified frames do.
 37165 This variable is not guaranteed to be accurate except while processing
 37166 `frame-title-format' and `icon-title-format'.  */);
 37167 
 37168   DEFVAR_LISP ("frame-title-format", Vframe_title_format,
 37169     doc: /* Template for displaying the title bar of visible frames.
 37170 \(Assuming the window manager supports this feature.)
 37171 
 37172 This variable has the same structure as `mode-line-format', except that
 37173 the %c, %C, and %l constructs are ignored.  It is used only on frames for
 37174 which no explicit name has been set (see `modify-frame-parameters').  */);
 37175 
 37176   DEFVAR_LISP ("icon-title-format", Vicon_title_format,
 37177     doc: /* Template for displaying the title bar of an iconified frame.
 37178 \(Assuming the window manager supports this feature.)
 37179 If the value is a string, it should have the same structure
 37180 as `mode-line-format' (which see), and is used only on frames
 37181 for which no explicit name has been set \(see `modify-frame-parameters').
 37182 If the value is t, that means use `frame-title-format' for
 37183 iconified frames.  */);
 37184   /* Do not nest calls to pure_list.  This works around a bug in
 37185      Oracle Developer Studio 12.6.  */
 37186   Lisp_Object icon_title_name_format
 37187     = pure_list (empty_unibyte_string,
 37188                  build_pure_c_string ("%b - GNU Emacs at "),
 37189                  intern_c_string ("system-name"));
 37190   Vicon_title_format
 37191     = Vframe_title_format
 37192     = pure_list (intern_c_string ("multiple-frames"),
 37193                  build_pure_c_string ("%b"),
 37194                  icon_title_name_format);
 37195 
 37196   DEFVAR_LISP ("message-log-max", Vmessage_log_max,
 37197     doc: /* Maximum number of lines to keep in the message log buffer.
 37198 If nil, disable message logging.  If t, log messages but don't truncate
 37199 the buffer when it becomes large.  */);
 37200   Vmessage_log_max = make_fixnum (1000);
 37201 
 37202   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
 37203     doc: /* List of functions to call before redisplaying a window with scrolling.
 37204 Each function is called with two arguments, the window and its new
 37205 display-start position.
 37206 These functions are called whenever the `window-start' marker is modified,
 37207 either to point into another buffer (e.g. via `set-window-buffer') or another
 37208 place in the same buffer.
 37209 When each function is called, the `window-start' marker of its window
 37210 argument has been already set to the new value, and the buffer which that
 37211 window will display is set to be the current buffer.
 37212 Note that the value of `window-end' is not valid when these functions are
 37213 called.
 37214 
 37215 Warning: Do not use this feature to alter the way the window
 37216 is scrolled.  It is not designed for that, and such use probably won't
 37217 work.  */);
 37218   Vwindow_scroll_functions = Qnil;
 37219 
 37220   DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
 37221      doc: /* Non-nil means autoselect window with mouse pointer.
 37222 If nil, do not autoselect windows.
 37223 A positive number means delay autoselection by that many seconds: a
 37224 window is autoselected only after the mouse has remained in that
 37225 window for the duration of the delay.
 37226 A negative number has a similar effect, but causes windows to be
 37227 autoselected only after the mouse has stopped moving.  (Because of
 37228 the way Emacs compares mouse events, you will occasionally wait twice
 37229 that time before the window gets selected.)
 37230 Any other value means to autoselect window instantaneously when the
 37231 mouse pointer enters it.
 37232 
 37233 Autoselection selects the minibuffer only if it is active, and never
 37234 unselects the minibuffer if it is active.
 37235 
 37236 If you want to use the mouse to autoselect a window on another frame,
 37237 make sure that (1) your window manager has focus follow the mouse and
 37238 (2) the value of the option `focus-follows-mouse' matches the policy
 37239 of your window manager.  */);
 37240   Vmouse_autoselect_window = Qnil;
 37241 
 37242   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
 37243     doc: /* Non-nil means automatically resize tab-bars.
 37244 This dynamically changes the tab-bar's height to the minimum height
 37245 that is needed to make all tab-bar items visible.
 37246 If value is `grow-only', the tab-bar's height is only increased
 37247 automatically; to decrease the tab-bar height, use \\[recenter],
 37248 after setting `recenter-redisplay' to the value of t.  */);
 37249   Vauto_resize_tab_bars = Qt;
 37250 
 37251   DEFVAR_BOOL ("auto-raise-tab-bar-buttons", auto_raise_tab_bar_buttons_p,
 37252     doc: /* Non-nil means raise tab-bar buttons when the mouse moves over them.  */);
 37253   auto_raise_tab_bar_buttons_p = true;
 37254 
 37255   DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
 37256     doc: /* Non-nil means automatically resize tool-bars.
 37257 This dynamically changes the tool-bar's height to the minimum height
 37258 that is needed to make all tool-bar items visible.
 37259 If value is `grow-only', the tool-bar's height is only increased
 37260 automatically; to decrease the tool-bar height, use \\[recenter],
 37261 after setting `recenter-redisplay' to the value of t.  */);
 37262   Vauto_resize_tool_bars = Qt;
 37263 
 37264   DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
 37265     doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them.  */);
 37266   auto_raise_tool_bar_buttons_p = true;
 37267 
 37268   DEFVAR_LISP ("make-cursor-line-fully-visible", Vmake_cursor_line_fully_visible,
 37269     doc: /* Whether to scroll the window if the cursor line is not fully visible.
 37270 If the value is non-nil, Emacs scrolls or recenters the window to make
 37271 the cursor line fully visible.  The value could also be a function, which
 37272 is called with a single argument, the window to be scrolled, and should
 37273 return non-nil if the partially-visible cursor requires scrolling the
 37274 window, nil if it's okay to leave the cursor partially-visible.  */);
 37275   Vmake_cursor_line_fully_visible = Qt;
 37276   DEFSYM (Qmake_cursor_line_fully_visible, "make-cursor-line-fully-visible");
 37277 
 37278   DEFVAR_BOOL ("make-window-start-visible", make_window_start_visible,
 37279     doc: /* Whether to ensure `window-start' position is never invisible.  */);
 37280   make_window_start_visible = false;
 37281   DEFSYM (Qmake_window_start_visible, "make-window-start-visible");
 37282   Fmake_variable_buffer_local (Qmake_window_start_visible);
 37283 
 37284   DEFSYM (Qclose_tab, "close-tab");
 37285   DEFVAR_LISP ("tab-bar-border", Vtab_bar_border,
 37286     doc: /* Border below tab-bar in pixels.
 37287 If an integer, use it as the height of the border.
 37288 If it is one of `internal-border-width' or `border-width', use the
 37289 value of the corresponding frame parameter.
 37290 Otherwise, no border is added below the tab-bar.  */);
 37291   Vtab_bar_border = Qinternal_border_width;
 37292 
 37293   DEFVAR_LISP ("tab-bar-button-margin", Vtab_bar_button_margin,
 37294     doc: /* Margin around tab-bar buttons in pixels.
 37295 If an integer, use that for both horizontal and vertical margins.
 37296 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 37297 HORZ specifying the horizontal margin, and VERT specifying the
 37298 vertical margin.  */);
 37299   Vtab_bar_button_margin = make_fixnum (DEFAULT_TAB_BAR_BUTTON_MARGIN);
 37300 
 37301   DEFVAR_INT ("tab-bar-button-relief", tab_bar_button_relief,
 37302     doc: /* Relief thickness of tab-bar buttons.  */);
 37303   tab_bar_button_relief = DEFAULT_TAB_BAR_BUTTON_RELIEF;
 37304 
 37305   DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
 37306     doc: /* Border below tool-bar in pixels.
 37307 If an integer, use it as the height of the border.
 37308 If it is one of `internal-border-width' or `border-width', use the
 37309 value of the corresponding frame parameter.
 37310 Otherwise, no border is added below the tool-bar.  */);
 37311   Vtool_bar_border = Qinternal_border_width;
 37312 
 37313   DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
 37314     doc: /* Margin around tool-bar buttons in pixels.
 37315 If an integer, use that for both horizontal and vertical margins.
 37316 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 37317 HORZ specifying the horizontal margin, and VERT specifying the
 37318 vertical margin.  */);
 37319   Vtool_bar_button_margin = make_fixnum (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
 37320 
 37321   DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
 37322     doc: /* Relief thickness of tool-bar buttons.  */);
 37323   tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
 37324 
 37325   DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
 37326     doc: /* Tool bar style to use.
 37327 It can be one of
 37328  image            - show images only
 37329  text             - show text only
 37330  both             - show both, text below image
 37331  both-horiz       - show text to the right of the image
 37332  text-image-horiz - show text to the left of the image
 37333  any other        - use system default or image if no system default.
 37334 
 37335 This variable only affects the GTK+ toolkit version of Emacs.  */);
 37336   Vtool_bar_style = Qnil;
 37337 
 37338   DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
 37339     doc: /* Maximum number of characters a label can have to be shown.
 37340 The tool bar style must also show labels for this to have any effect, see
 37341 `tool-bar-style'.  */);
 37342   tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
 37343 
 37344   DEFVAR_LISP ("fontification-functions", Vfontification_functions,
 37345     doc: /* List of functions to call to fontify regions of text.
 37346 Each function is called with one argument POS.  Functions must
 37347 fontify a region starting at POS in the current buffer, and give
 37348 fontified regions the property `fontified' with a non-nil value.
 37349 
 37350 Note that, when `long-line-optimizations-p' is non-nil in the buffer,
 37351 these functions are called as if they were in a `with-restriction' form,
 37352 with a `long-line-optimizations-in-fontification-functions' label and
 37353 with the buffer narrowed to a portion around POS whose size is
 37354 specified by `long-line-optimizations-region-size'.  */);
 37355   Vfontification_functions = Qnil;
 37356   Fmake_variable_buffer_local (Qfontification_functions);
 37357 
 37358   DEFVAR_BOOL ("unibyte-display-via-language-environment",
 37359                unibyte_display_via_language_environment,
 37360     doc: /* Non-nil means display unibyte text according to language environment.
 37361 Specifically, this means that raw bytes in the range 160-255 decimal
 37362 are displayed by converting them to the equivalent multibyte characters
 37363 according to the current language environment.  As a result, they are
 37364 displayed according to the current fontset.
 37365 
 37366 Note that this variable affects only how these bytes are displayed,
 37367 but does not change the fact they are interpreted as raw bytes.  */);
 37368   unibyte_display_via_language_environment = false;
 37369 
 37370   DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
 37371     doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
 37372 If a float, it specifies the maximum height in units of the
 37373 mini-window frame's height.
 37374 If an integer, it specifies the maximum height in units of the
 37375 mini-window frame's default font's height.  */);
 37376   Vmax_mini_window_height = make_float (0.25);
 37377 
 37378   DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
 37379     doc: /* How to resize mini-windows (the minibuffer and the echo area).
 37380 A value of nil means don't automatically resize mini-windows.
 37381 A value of t means resize them to fit the text displayed in them.
 37382 A value of `grow-only', the default, means let mini-windows grow only;
 37383 they return to their normal size when the minibuffer is closed, or the
 37384 echo area becomes empty.
 37385 
 37386 This variable does not affect resizing of the minibuffer window of
 37387 minibuffer-only frames.  These are handled by `resize-mini-frames'
 37388 only.  */);
 37389   /* Contrary to the doc string, we initialize this to nil, so that
 37390      loading loadup.el won't try to resize windows before loading
 37391      window.el, where some functions we need to call for this live.
 37392      We assign the 'grow-only' value right after loading window.el
 37393      during loadup.  */
 37394   Vresize_mini_windows = Qnil;
 37395 
 37396   DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
 37397     doc: /* Alist specifying how to blink the cursor off.
 37398 Each element has the form (ON-STATE . OFF-STATE).  Whenever the
 37399 `cursor-type' frame-parameter or variable equals ON-STATE,
 37400 comparing using `equal', Emacs uses OFF-STATE to specify
 37401 how to blink it off.  ON-STATE and OFF-STATE are values for
 37402 the `cursor-type' frame parameter.
 37403 
 37404 If a frame's ON-STATE has no entry in this list,
 37405 the frame's other specifications determine how to blink the cursor off.  */);
 37406   Vblink_cursor_alist = Qnil;
 37407 
 37408   DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
 37409     doc: /* Allow or disallow automatic horizontal scrolling of windows.
 37410 The value `current-line' means the line displaying point in each window
 37411 is automatically scrolled horizontally to make point visible.
 37412 Any other non-nil value means all the lines in a window are automatically
 37413 scrolled horizontally to make point visible.  */);
 37414   automatic_hscrolling = Qt;
 37415   DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
 37416   DEFSYM (Qcurrent_line, "current-line");
 37417 
 37418   DEFVAR_INT ("hscroll-margin", hscroll_margin,
 37419     doc: /* How many columns away from the window edge point is allowed to get
 37420 before automatic hscrolling will horizontally scroll the window.  */);
 37421   hscroll_margin = 5;
 37422 
 37423   DEFVAR_LISP ("hscroll-step", Vhscroll_step,
 37424     doc: /* How many columns to scroll the window when point gets too close to the edge.
 37425 When point is less than `hscroll-margin' columns from the window
 37426 edge, automatic hscrolling will scroll the window by the amount of columns
 37427 determined by this variable.  If its value is a positive integer, scroll that
 37428 many columns.  If it's a positive floating-point number, it specifies the
 37429 fraction of the window's width to scroll.  If it's nil or zero, point will be
 37430 centered horizontally after the scroll.  Any other value, including negative
 37431 numbers, are treated as if the value were zero.
 37432 
 37433 Automatic hscrolling always moves point outside the scroll margin, so if
 37434 point was more than scroll step columns inside the margin, the window will
 37435 scroll more than the value given by the scroll step.
 37436 
 37437 Note that the lower bound for automatic hscrolling specified by `scroll-left'
 37438 and `scroll-right' overrides this variable's effect.  */);
 37439   Vhscroll_step = make_fixnum (0);
 37440 
 37441   DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
 37442     doc: /* If non-nil, messages are truncated when displaying the echo area.
 37443 Bind this around calls to `message' to let it take effect.  */);
 37444   message_truncate_lines = false;
 37445 
 37446   DEFVAR_LISP ("menu-bar-update-hook",  Vmenu_bar_update_hook,
 37447     doc: /* Normal hook run to update the menu bar definitions.
 37448 Redisplay runs this hook before it redisplays the menu bar.
 37449 This is used to update menus such as Buffers, whose contents depend on
 37450 various data.  */);
 37451   Vmenu_bar_update_hook = Qnil;
 37452 
 37453   DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
 37454                doc: /* Frame for which we are updating a menu.
 37455 The enable predicate for a menu binding should check this variable.  */);
 37456   Vmenu_updating_frame = Qnil;
 37457 
 37458   DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
 37459     doc: /* Non-nil means don't update menu bars.  Internal use only.  */);
 37460   inhibit_menubar_update = false;
 37461 
 37462   DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
 37463     doc: /* Prefix prepended to all continuation lines at display time.
 37464 The value may be a string, an image, or a stretch-glyph; it is
 37465 interpreted in the same way as the value of a `display' text property.
 37466 
 37467 This variable is overridden by any `wrap-prefix' text or overlay
 37468 property.
 37469 
 37470 To add a prefix to non-continuation lines, use `line-prefix'.  */);
 37471   Vwrap_prefix = Qnil;
 37472   DEFSYM (Qwrap_prefix, "wrap-prefix");
 37473   Fmake_variable_buffer_local (Qwrap_prefix);
 37474 
 37475   DEFVAR_LISP ("line-prefix", Vline_prefix,
 37476     doc: /* Prefix prepended to all non-continuation lines at display time.
 37477 The value may be a string, an image, or a stretch-glyph; it is
 37478 interpreted in the same way as the value of a `display' text property.
 37479 
 37480 This variable is overridden by any `line-prefix' text or overlay
 37481 property.
 37482 
 37483 To add a prefix to continuation lines, use `wrap-prefix'.  */);
 37484   Vline_prefix = Qnil;
 37485   DEFSYM (Qline_prefix, "line-prefix");
 37486   Fmake_variable_buffer_local (Qline_prefix);
 37487 
 37488   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
 37489     doc: /* Non-nil means display line numbers.
 37490 
 37491 If the value is t, display the absolute number of each line of a buffer
 37492 shown in a window.  Absolute line numbers count from the beginning of
 37493 the current narrowing, or from buffer beginning.  The variable
 37494 `display-line-numbers-offset', if non-zero, is a signed offset added
 37495 to each absolute line number; it also forces line numbers to be counted
 37496 from the beginning of the buffer, as if `display-line-numbers-widen'
 37497 were non-nil.  It has no effect when line numbers are not absolute.
 37498 
 37499 If the value is `relative', display for each line not containing the
 37500 window's point its relative number instead, i.e. the number of the line
 37501 relative to the line showing the window's point.
 37502 
 37503 In either case, line numbers are displayed at the beginning of each
 37504 non-continuation line that displays buffer text, i.e. after each newline
 37505 character that comes from the buffer.  The value `visual' is like
 37506 `relative' but counts screen lines instead of buffer lines.  In practice
 37507 this means that continuation lines count as well when calculating the
 37508 relative number of a line.
 37509 
 37510 Lisp programs can disable display of a line number of a particular
 37511 buffer line by putting the `display-line-numbers-disable' text property
 37512 or overlay property on the first visible character of that line.  */);
 37513   Vdisplay_line_numbers = Qnil;
 37514   DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
 37515   Fmake_variable_buffer_local (Qdisplay_line_numbers);
 37516   DEFSYM (Qrelative, "relative");
 37517   DEFSYM (Qvisual, "visual");
 37518 
 37519   DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
 37520     doc: /* Minimum width of space reserved for line number display.
 37521 A positive number means reserve that many columns for line numbers,
 37522 even if the actual number needs less space.
 37523 The default value of nil means compute the space dynamically.
 37524 Any other value is treated as nil.  */);
 37525   Vdisplay_line_numbers_width = Qnil;
 37526   DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
 37527   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 37528 
 37529   DEFVAR_LISP ("display-line-numbers-current-absolute",
 37530                Vdisplay_line_numbers_current_absolute,
 37531     doc: /* Non-nil means display absolute number of current line.
 37532 This variable has effect only when `display-line-numbers' is
 37533 either `relative' or `visual'.  */);
 37534   Vdisplay_line_numbers_current_absolute = Qt;
 37535 
 37536   DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
 37537     doc: /* Non-nil means display line numbers disregarding any narrowing.  */);
 37538   display_line_numbers_widen = false;
 37539   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
 37540   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 37541 
 37542   DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
 37543     doc: /* A signed integer added to each absolute line number.
 37544 When this variable is non-zero, line numbers are always counted from
 37545 the beginning of the buffer even if `display-line-numbers-widen' is nil.
 37546 It has no effect when set to 0, or when line numbers are not absolute.  */);
 37547   display_line_numbers_offset = 0;
 37548   DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
 37549   Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
 37550 
 37551   DEFVAR_BOOL ("display-fill-column-indicator", display_fill_column_indicator,
 37552     doc: /* Non-nil means display the fill column indicator.
 37553 If you set this non-nil, make sure `display-fill-column-indicator-character'
 37554 is also non-nil.
 37555 See Info node `Displaying Boundaries' for details.  */);
 37556   display_fill_column_indicator = false;
 37557   DEFSYM (Qdisplay_fill_column_indicator, "display-fill-column-indicator");
 37558   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator);
 37559 
 37560   DEFVAR_LISP ("display-fill-column-indicator-column", Vdisplay_fill_column_indicator_column,
 37561     doc: /* Column for indicator when `display-fill-column-indicator' is non-nil.
 37562 The default value is t which means that the indicator
 37563 will use the `fill-column' variable.  If it is set to an integer the
 37564 indicator will be drawn in that column.
 37565 See Info node `Displaying Boundaries' for details.  */);
 37566   Vdisplay_fill_column_indicator_column = Qt;
 37567   DEFSYM (Qdisplay_fill_column_indicator_column, "display-fill-column-indicator-column");
 37568   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_column);
 37569 
 37570   DEFVAR_LISP ("display-fill-column-indicator-character", Vdisplay_fill_column_indicator_character,
 37571     doc: /* Character to draw the indicator when `display-fill-column-indicator' is non-nil.
 37572 A good candidate is U+2502, and an alternative is (ascii 124) if the
 37573 font of `fill-column-indicator' face does not support Unicode characters.
 37574 See Info node `Displaying Boundaries' for details.  */);
 37575   Vdisplay_fill_column_indicator_character = Qnil;
 37576   DEFSYM (Qdisplay_fill_column_indicator_character, "display-fill-column-indicator-character");
 37577   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_character);
 37578 
 37579   DEFVAR_INT ("display-line-numbers-major-tick", display_line_numbers_major_tick,
 37580     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37581 The line number is shown with the `line-number-major-tick' face.
 37582 Otherwise, no special highlighting is done every Nth line.
 37583 Note that major ticks take precedence over minor ticks.  */);
 37584   display_line_numbers_major_tick = 0;
 37585 
 37586   DEFVAR_INT ("display-line-numbers-minor-tick", display_line_numbers_minor_tick,
 37587     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37588 The line number is shown with the `line-number-minor-tick' face.
 37589 Otherwise, no special highlighting is done every Nth line.
 37590 Note that major ticks take precedence over minor ticks.  */);
 37591   display_line_numbers_minor_tick = 0;
 37592 
 37593   DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
 37594     doc: /* Non-nil means don't eval Lisp during redisplay.  */);
 37595   inhibit_eval_during_redisplay = false;
 37596 
 37597   DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
 37598     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
 37599   inhibit_free_realized_faces = false;
 37600 
 37601   DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
 37602     doc: /* Non-nil means don't mirror characters even when bidi context requires that.
 37603 Intended for use during debugging and for testing bidi display;
 37604 see biditest.el in the test suite.  */);
 37605   inhibit_bidi_mirroring = false;
 37606 
 37607   DEFVAR_BOOL ("bidi-inhibit-bpa", bidi_inhibit_bpa,
 37608     doc: /* Non-nil means inhibit the Bidirectional Parentheses Algorithm.
 37609 Disabling the BPA makes redisplay faster, but might produce incorrect
 37610 display reordering of bidirectional text with embedded parentheses and
 37611 other bracket characters whose `paired-bracket' Unicode property is
 37612 non-nil, see `get-char-code-property'.  */);
 37613   bidi_inhibit_bpa = false;
 37614 
 37615 #ifdef GLYPH_DEBUG
 37616   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
 37617                doc: /* Inhibit try_window_id display optimization.  */);
 37618   inhibit_try_window_id = false;
 37619 
 37620   DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
 37621                doc: /* Inhibit try_window_reusing display optimization.  */);
 37622   inhibit_try_window_reusing = false;
 37623 
 37624   DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
 37625                doc: /* Inhibit try_cursor_movement display optimization.  */);
 37626   inhibit_try_cursor_movement = false;
 37627 #endif /* GLYPH_DEBUG */
 37628 
 37629   DEFVAR_INT ("overline-margin", overline_margin,
 37630                doc: /* Space between overline and text, in pixels.
 37631 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
 37632 margin to the character height.  */);
 37633   overline_margin = 2;
 37634 
 37635   DEFVAR_INT ("underline-minimum-offset",
 37636                underline_minimum_offset,
 37637      doc: /* Minimum distance between baseline and underline.
 37638 This can improve legibility of underlined text at small font sizes,
 37639 particularly when using variable `x-use-underline-position-properties'
 37640 with fonts that specify an UNDERLINE_POSITION relatively close to the
 37641 baseline.  The default value is 1.  */);
 37642   underline_minimum_offset = 1;
 37643   DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 37644 
 37645   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 37646                doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
 37647 This feature only works when on a window system that can change
 37648 cursor shapes.  */);
 37649   display_hourglass_p = true;
 37650 
 37651   DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
 37652                doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy.  */);
 37653   Vhourglass_delay = make_fixnum (DEFAULT_HOURGLASS_DELAY);
 37654 
 37655 #ifdef HAVE_WINDOW_SYSTEM
 37656   hourglass_atimer = NULL;
 37657   hourglass_shown_p = false;
 37658 #endif /* HAVE_WINDOW_SYSTEM */
 37659 
 37660   /* Name of the face used to display glyphless characters.  */
 37661   DEFSYM (Qglyphless_char, "glyphless-char");
 37662 
 37663   /* Method symbols for Vglyphless_char_display.  */
 37664   DEFSYM (Qhex_code, "hex-code");
 37665   DEFSYM (Qempty_box, "empty-box");
 37666   DEFSYM (Qthin_space, "thin-space");
 37667   DEFSYM (Qzero_width, "zero-width");
 37668 
 37669   DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
 37670                doc: /* Function run just before redisplay.
 37671 It is called with one argument, which is the set of windows that are to
 37672 be redisplayed.  This set can be nil (meaning, only the selected window),
 37673 or t (meaning all windows).  */);
 37674   Vpre_redisplay_function = intern ("ignore");
 37675 
 37676   /* Symbol for the purpose of Vglyphless_char_display.  */
 37677   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
 37678   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_fixnum (1));
 37679 
 37680   DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
 37681                doc: /* Char-table defining glyphless characters.
 37682 Each element, if non-nil, should be one of the following:
 37683   an ASCII acronym string: display this string in a box
 37684   `hex-code':   display the hexadecimal code of a character in a box
 37685   `empty-box':  display as an empty box
 37686   `thin-space': display as 1-pixel width space
 37687   `zero-width': don't display
 37688 Any other value is interpreted as `empty-box'.
 37689 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
 37690 display method for graphical terminals and text terminals respectively.
 37691 GRAPHICAL and TEXT should each have one of the values listed above.
 37692 
 37693 The char-table has one extra slot to control the display of characters
 37694 for which no font is found on graphical terminals, and characters that
 37695 cannot be displayed by text-mode terminals.  Its value should be an
 37696 ASCII acronym string, `hex-code', `empty-box', or `thin-space'.  It
 37697 could also be a cons cell of any two of these, to specify separate
 37698 values for graphical and text terminals.  The default is `empty-box'.
 37699 
 37700 With the obvious exception of `zero-width', all the other representations
 37701 are displayed using the face `glyphless-char'.
 37702 
 37703 If a character has a non-nil entry in an active display table, the
 37704 display table takes effect; in this case, Emacs does not consult
 37705 `glyphless-char-display' at all.  */);
 37706   Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
 37707   Fset_char_table_extra_slot (Vglyphless_char_display, make_fixnum (0),
 37708                               Qempty_box);
 37709 
 37710   DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
 37711                doc: /* If non-nil, debug if a message matching this regexp is displayed.  */);
 37712   Vdebug_on_message = Qnil;
 37713 
 37714   DEFVAR_LISP ("set-message-function", Vset_message_function,
 37715                doc: /* If non-nil, function to handle display of echo-area messages.
 37716 The function is called with one argument that is the text of a message.
 37717 If this function returns nil, the message is displayed in the echo area
 37718 as usual.  If the function returns a string, the returned string is
 37719 displayed in the echo area.  If this function returns any other non-nil
 37720 value, this means that the message was already handled, and the original
 37721 message text will not be displayed in the echo area.
 37722 
 37723 Also see `clear-message-function' (which can be used to clear the
 37724 message displayed by this function), and `command-error-function'
 37725 (which controls how error messages are displayed).  */);
 37726   Vset_message_function = Qnil;
 37727 
 37728   DEFSYM (Qdont_clear_message, "dont-clear-message");
 37729   DEFVAR_LISP ("clear-message-function", Vclear_message_function,
 37730                doc: /* If non-nil, function to clear echo-area messages.
 37731 Usually this function is called when the next input event arrives.
 37732 It is expected to clear the message displayed by its counterpart
 37733 function specified by `set-message-function'.
 37734 
 37735 The function is called without arguments.
 37736 
 37737 If this function returns a value that isn't `dont-clear-message', the
 37738 message is cleared from the echo area as usual.  If this function
 37739 returns `dont-clear-message', this means that the message was already
 37740 handled, and the original message text will not be cleared from the
 37741 echo area.  */);
 37742   Vclear_message_function = Qnil;
 37743 
 37744   DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
 37745                doc: /* Code of the cause for redisplaying all windows.
 37746 Internal use only.  */);
 37747   Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
 37748 
 37749   DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
 37750                doc: /* Code of the cause for redisplaying mode lines.
 37751 Internal use only.  */);
 37752   Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
 37753 
 37754   DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
 37755      doc: /* Non-nil means it is not safe to attempt bidi reordering for display.  */);
 37756   /* Initialize to t, since we need to disable reordering until
 37757      loadup.el successfully loads charprop.el.  */
 37758   redisplay__inhibit_bidi = true;
 37759 
 37760   DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
 37761     doc: /* Non-nil means display raw bytes in hexadecimal format.
 37762 The default is to use octal format (\\200) whereas hexadecimal (\\x80)
 37763 may be more familiar to users.  */);
 37764   display_raw_bytes_as_hex = false;
 37765 
 37766   DEFVAR_BOOL ("mouse-fine-grained-tracking", mouse_fine_grained_tracking,
 37767     doc: /* Non-nil for pixelwise mouse-movement.
 37768 When nil, mouse-movement events will not be generated as long as the
 37769 mouse stays within the extent of a single glyph (except for images).
 37770 When nil and `mouse-prefer-closest-glyph' is non-nil, mouse-movement
 37771 events will instead not be generated as long as the mouse stays within
 37772 the extent of a single left/right half glyph (except for images).  */);
 37773   mouse_fine_grained_tracking = false;
 37774 
 37775   DEFVAR_BOOL ("tab-bar--dragging-in-progress", tab_bar__dragging_in_progress,
 37776     doc: /* Non-nil when maybe dragging tab bar item.  */);
 37777   tab_bar__dragging_in_progress = false;
 37778 
 37779   DEFVAR_BOOL ("redisplay-skip-initial-frame", redisplay_skip_initial_frame,
 37780     doc: /* Non-nil means skip redisplay of the initial frame.
 37781 The initial frame is the text-mode frame used by Emacs internally during
 37782 the early stages of startup.  That frame is not displayed anywhere, so
 37783 skipping it is best except in special circumstances such as running
 37784 redisplay tests in batch mode.   */);
 37785   redisplay_skip_initial_frame = true;
 37786 
 37787   DEFVAR_BOOL ("redisplay-skip-fontification-on-input",
 37788                redisplay_skip_fontification_on_input,
 37789     doc: /* Skip `fontification_functions` when there is input pending.
 37790 If non-nil and there was input pending at the beginning of the command,
 37791 the `fontification_functions` hook is not run.  This usually does not
 37792 affect the display because redisplay is completely skipped anyway if input
 37793 was pending, but it can make scrolling smoother by avoiding
 37794 unnecessary fontification.
 37795 It is similar to `fast-but-imprecise-scrolling' with similar tradeoffs,
 37796 but with the advantage that it should only affect the behavior when Emacs
 37797 has trouble keeping up with the incoming input rate.  */);
 37798   redisplay_skip_fontification_on_input = false;
 37799 
 37800   DEFVAR_BOOL ("redisplay-adhoc-scroll-in-resize-mini-windows",
 37801                redisplay_adhoc_scroll_in_resize_mini_windows,
 37802     doc: /* If nil always use normal scrolling in minibuffer windows.
 37803 Otherwise, use custom-tailored code after resizing minibuffer windows to try
 37804 and display the most important part of the minibuffer.   */);
 37805   /* See bug#43519 for some discussion around this.  */
 37806   redisplay_adhoc_scroll_in_resize_mini_windows = true;
 37807 
 37808   DEFVAR_BOOL ("composition-break-at-point", composition_break_at_point,
 37809     doc: /* If non-nil, prevent auto-composition of characters around point.
 37810 This makes it easier to edit character sequences that are
 37811 composed on display.  */);
 37812   composition_break_at_point = false;
 37813 
 37814   DEFVAR_INT ("max-redisplay-ticks", max_redisplay_ticks,
 37815     doc: /* Maximum number of redisplay ticks before aborting redisplay of a window.
 37816 
 37817 This allows to abort the display of a window if the amount of low-level
 37818 redisplay operations exceeds the value of this variable.  When display of
 37819 a window is aborted due to this reason, the buffer shown in that window
 37820 will not have its windows redisplayed until the buffer is modified or until
 37821 you type \\[recenter-top-bottom] with one of its windows selected.
 37822 You can also decide to kill the buffer and visit it in some
 37823 other way, like under `so-long-mode' or literally.
 37824 
 37825 The default value is zero, which disables this feature.
 37826 The recommended non-zero value is between 100000 and 1000000,
 37827 depending on your patience and the speed of your system.  */);
 37828   max_redisplay_ticks = 0;
 37829 }
 37830 
 37831 
 37832 /* Initialize this module when Emacs starts.  */
 37833 
 37834 void
 37835 init_xdisp (void)
 37836 {
 37837   CHARPOS (this_line_start_pos) = 0;
 37838 
 37839   echo_area_window = minibuf_window;
 37840 
 37841   if (!noninteractive)
 37842     {
 37843       struct window *m = XWINDOW (minibuf_window);
 37844       Lisp_Object frame = m->frame;
 37845       struct frame *f = XFRAME (frame);
 37846       Lisp_Object root = FRAME_ROOT_WINDOW (f);
 37847       struct window *r = XWINDOW (root);
 37848       int i;
 37849 
 37850       r->top_line = FRAME_TOP_MARGIN (f);
 37851       r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
 37852       r->total_cols = FRAME_COLS (f);
 37853       r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
 37854       r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_MARGINS (f);
 37855       r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
 37856 
 37857       m->top_line = FRAME_TOTAL_LINES (f) - 1;
 37858       m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
 37859       m->total_cols = FRAME_COLS (f);
 37860       m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
 37861       m->total_lines = 1;
 37862       m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
 37863 
 37864       scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
 37865       scratch_glyph_row.glyphs[TEXT_AREA + 1]
 37866         = scratch_glyphs + MAX_SCRATCH_GLYPHS;
 37867 
 37868       /* The default ellipsis glyphs `...'.  */
 37869       for (i = 0; i < 3; ++i)
 37870         default_invis_vector[i] = make_fixnum ('.');
 37871     }
 37872 
 37873   {
 37874     /* Allocate the buffer for frame titles.
 37875        Also used for `format-mode-line'.  */
 37876     int size = 100;
 37877     mode_line_noprop_buf = xmalloc (size);
 37878     mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 37879     mode_line_noprop_ptr = mode_line_noprop_buf;
 37880     mode_line_target = MODE_LINE_DISPLAY;
 37881   }
 37882 
 37883   help_echo_showing_p = false;
 37884 }
 37885 
 37886 #ifdef HAVE_WINDOW_SYSTEM
 37887 
 37888 /* Platform-independent portion of hourglass implementation.  */
 37889 
 37890 /* Timer function of hourglass_atimer.  */
 37891 
 37892 static void
 37893 show_hourglass (struct atimer *timer)
 37894 {
 37895   /* The timer implementation will cancel this timer automatically
 37896      after this function has run.  Set hourglass_atimer to null
 37897      so that we know the timer doesn't have to be canceled.  */
 37898   hourglass_atimer = NULL;
 37899 
 37900   if (!hourglass_shown_p)
 37901     {
 37902       Lisp_Object tail, frame;
 37903 
 37904       block_input ();
 37905 
 37906       FOR_EACH_FRAME (tail, frame)
 37907         {
 37908           struct frame *f = XFRAME (frame);
 37909 
 37910           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37911               && FRAME_RIF (f)->show_hourglass)
 37912             FRAME_RIF (f)->show_hourglass (f);
 37913         }
 37914 
 37915       hourglass_shown_p = true;
 37916       unblock_input ();
 37917     }
 37918 }
 37919 
 37920 /* Cancel a currently active hourglass timer, and start a new one.  */
 37921 
 37922 void
 37923 start_hourglass (void)
 37924 {
 37925   struct timespec delay;
 37926 
 37927   cancel_hourglass ();
 37928 
 37929   if (FIXNUMP (Vhourglass_delay)
 37930       && XFIXNUM (Vhourglass_delay) > 0)
 37931     delay = make_timespec (min (XFIXNUM (Vhourglass_delay),
 37932                                   TYPE_MAXIMUM (time_t)),
 37933                            0);
 37934   else if (FLOATP (Vhourglass_delay)
 37935            && XFLOAT_DATA (Vhourglass_delay) > 0)
 37936     delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
 37937   else
 37938     delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
 37939 
 37940   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
 37941                                    show_hourglass, NULL);
 37942 }
 37943 
 37944 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
 37945    shown.  */
 37946 
 37947 void
 37948 cancel_hourglass (void)
 37949 {
 37950   if (hourglass_atimer)
 37951     {
 37952       cancel_atimer (hourglass_atimer);
 37953       hourglass_atimer = NULL;
 37954     }
 37955 
 37956   if (hourglass_shown_p)
 37957     {
 37958       Lisp_Object tail, frame;
 37959 
 37960       block_input ();
 37961 
 37962       FOR_EACH_FRAME (tail, frame)
 37963         {
 37964           struct frame *f = XFRAME (frame);
 37965 
 37966           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37967               && FRAME_RIF (f)->hide_hourglass)
 37968             FRAME_RIF (f)->hide_hourglass (f);
 37969 #ifdef HAVE_NTGUI
 37970           /* No cursors on non GUI frames - restore to stock arrow cursor.  */
 37971           else if (!FRAME_W32_P (f))
 37972             w32_arrow_cursor ();
 37973 #endif
 37974         }
 37975 
 37976       hourglass_shown_p = false;
 37977       unblock_input ();
 37978     }
 37979 }
 37980 
 37981 /* Return a correction to be applied to G->pixel_width when it is
 37982    displayed in MOUSE_FACE.  This is needed for the first and the last
 37983    glyphs of text inside a face with :box when it is displayed with
 37984    MOUSE_FACE that has a different or no :box attribute.
 37985    ORIGINAL_FACE is the face G was originally drawn in, and MOUSE_FACE
 37986    is the face it will be drawn in now.  ROW is the G's glyph row and
 37987    W is its window.  */
 37988 static int
 37989 adjust_glyph_width_for_mouse_face (struct glyph *g, struct glyph_row *row,
 37990                                    struct window *w,
 37991                                    struct face *original_face,
 37992                                    struct face *mouse_face)
 37993 {
 37994   int sum = 0;
 37995 
 37996   bool do_left_box_p = g->left_box_line_p;
 37997   bool do_right_box_p = g->right_box_line_p;
 37998 
 37999   /* This is required because we test some parameters of the image
 38000      slice before applying the box in produce_image_glyph.  */
 38001   if (g->type == IMAGE_GLYPH)
 38002     {
 38003       if (!row->reversed_p)
 38004         {
 38005           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 38006                                              g->u.img_id);
 38007           do_left_box_p = g->left_box_line_p &&
 38008             g->slice.img.x == 0;
 38009           do_right_box_p = g->right_box_line_p &&
 38010             g->slice.img.x + g->slice.img.width == img->width;
 38011         }
 38012       else
 38013         {
 38014           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 38015                                              g->u.img_id);
 38016           do_left_box_p = g->left_box_line_p &&
 38017             g->slice.img.x + g->slice.img.width == img->width;
 38018           do_right_box_p = g->right_box_line_p &&
 38019             g->slice.img.x == 0;
 38020         }
 38021     }
 38022 
 38023   /* If the glyph has a left box line, subtract it from the offset.  */
 38024   if (do_left_box_p)
 38025     sum -= max (0, original_face->box_vertical_line_width);
 38026   /* Likewise with the right box line, as there may be a
 38027      box there as well.  */
 38028   if (do_right_box_p)
 38029     sum -= max (0, original_face->box_vertical_line_width);
 38030   /* Now add the line widths from the new face.  */
 38031   if (g->left_box_line_p)
 38032     sum += max (0, mouse_face->box_vertical_line_width);
 38033   if (g->right_box_line_p)
 38034     sum += max (0, mouse_face->box_vertical_line_width);
 38035 
 38036   return sum;
 38037 }
 38038 
 38039 /* Get the offset due to mouse-highlight to apply before drawing
 38040    phys_cursor, and return it in OFFSET.  ROW should be the row that
 38041    is under mouse face and contains the phys cursor.
 38042 
 38043    This is required because the produce_XXX_glyph series of functions
 38044    add the width of the various vertical box lines to the total width
 38045    of the glyphs, but that must be updated when the row is put under
 38046    mouse face, which can have different box dimensions.  */
 38047 static void
 38048 get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
 38049                                   int *offset)
 38050 {
 38051   int sum = 0;
 38052   /* Return because the mode line can't possibly have a cursor. */
 38053   if (row->mode_line_p)
 38054     return;
 38055 
 38056   block_input ();
 38057 
 38058   struct frame *f = WINDOW_XFRAME (w);
 38059   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 38060   struct glyph *start, *end;
 38061   struct face *mouse_face = FACE_FROM_ID (f, hlinfo->mouse_face_face_id);
 38062   int hpos = w->phys_cursor.hpos;
 38063   end = &row->glyphs[TEXT_AREA][hpos];
 38064 
 38065   if (!row->reversed_p)
 38066     {
 38067       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 38068           hlinfo->mouse_face_beg_row)
 38069         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_beg_col];
 38070       else
 38071         start = row->glyphs[TEXT_AREA];
 38072     }
 38073   else
 38074     {
 38075       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 38076           hlinfo->mouse_face_end_row)
 38077         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_end_col];
 38078       else
 38079         start = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
 38080     }
 38081 
 38082   /* Calculate the offset by which to correct phys_cursor x if we are
 38083      drawing the cursor inside mouse-face highlighted text.  */
 38084 
 38085   for ( ; row->reversed_p ? start > end : start < end;
 38086           row->reversed_p ? --start : ++start)
 38087     sum += adjust_glyph_width_for_mouse_face (start, row, w,
 38088                                               FACE_FROM_ID (f, start->face_id),
 38089                                               mouse_face);
 38090 
 38091   if (row->reversed_p)
 38092     sum = -sum;
 38093 
 38094   *offset = sum;
 38095 
 38096   unblock_input ();
 38097 }
 38098 #endif /* HAVE_WINDOW_SYSTEM */

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