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 (it->f, it->face_id);
  8343 
  8344       if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
  8345         {
  8346           /* Automatic composition with glyph-string.   */
  8347           Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
  8348 
  8349           it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
  8350         }
  8351       else
  8352         {
  8353           ptrdiff_t pos = (it->s ? -1
  8354                      : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
  8355                      : IT_CHARPOS (*it));
  8356           int c;
  8357 
  8358           if (it->what == IT_CHARACTER)
  8359             c = it->char_to_display;
  8360           else
  8361             {
  8362               struct composition *cmp = composition_table[it->cmp_it.id];
  8363               int i;
  8364 
  8365               c = ' ';
  8366               for (i = 0; i < cmp->glyph_len; i++)
  8367                 /* TAB in a composition means display glyphs with
  8368                    padding space on the left or right.  */
  8369                 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
  8370                   break;
  8371             }
  8372           it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
  8373         }
  8374     }
  8375 #endif  /* HAVE_WINDOW_SYSTEM */
  8376 
  8377  done:
  8378   /* Is this character the last one of a run of characters with
  8379      box?  If yes, set IT->end_of_box_run_p to true.  */
  8380   if (it->face_box_p
  8381       && it->s == NULL)
  8382     {
  8383       if (it->method == GET_FROM_STRING && it->sp)
  8384         {
  8385           int face_id = underlying_face_id (it);
  8386           struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
  8387 
  8388           if (face)
  8389             {
  8390               if (face->box == FACE_NO_BOX)
  8391                 {
  8392                   /* If the box comes from face properties in a
  8393                      display string, check faces in that string.  */
  8394                   int string_face_id = face_after_it_pos (it);
  8395                   if (FACE_FROM_ID (it->f, string_face_id)->box == FACE_NO_BOX)
  8396                     it->end_of_box_run_p = true;
  8397                 }
  8398               /* Otherwise, the box comes from the underlying face.
  8399                  If this is the last string character displayed, check
  8400                  the next buffer location.  */
  8401               else if (((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
  8402                         /* For a composition, see if the string ends
  8403                            at the last character included in the
  8404                            composition.  */
  8405                         || (it->what == IT_COMPOSITION
  8406                             && (IT_STRING_CHARPOS (*it) + it->cmp_it.nchars
  8407                                 >= SCHARS (it->string))))
  8408                        /* n_overlay_strings is unreliable unless
  8409                           overlay_string_index is non-negative.  */
  8410                        && ((it->current.overlay_string_index >= 0
  8411                             && (it->current.overlay_string_index
  8412                                 == it->n_overlay_strings - 1))
  8413                            /* A string from display property.  */
  8414                            || it->from_disp_prop_p))
  8415                 {
  8416                   ptrdiff_t ignore;
  8417                   int next_face_id;
  8418                   bool text_from_string = false;
  8419                   /* Normally, the next buffer location is stored in
  8420                      IT->current.pos...  */
  8421                   struct text_pos pos = it->current.pos;
  8422 
  8423                   /* ...but for a string from a display property, the
  8424                      next buffer position is stored in the 'position'
  8425                      member of the iteration stack slot below the
  8426                      current one, see handle_single_display_spec.  By
  8427                      contrast, it->current.pos was not yet updated to
  8428                      point to that buffer position; that will happen
  8429                      in pop_it, after we finish displaying the current
  8430                      string.  Note that we already checked above that
  8431                      it->sp is positive, so subtracting one from it is
  8432                      safe.  */
  8433                   if (it->from_disp_prop_p)
  8434                     {
  8435                       int stackp = it->sp - 1;
  8436 
  8437                       /* Find the stack level with data from buffer.  */
  8438                       while (stackp >= 0
  8439                              && STRINGP ((it->stack + stackp)->string))
  8440                         stackp--;
  8441                       if (stackp < 0)
  8442                         {
  8443                           /* If no stack slot was found for iterating
  8444                              a buffer, we are displaying text from a
  8445                              string, most probably the mode line or
  8446                              the header line, and that string has a
  8447                              display string on some of its
  8448                              characters.  */
  8449                           text_from_string = true;
  8450                           pos = it->stack[it->sp - 1].position;
  8451                         }
  8452                       else
  8453                         pos = (it->stack + stackp)->position;
  8454                     }
  8455                   else
  8456                     INC_TEXT_POS (pos, it->multibyte_p);
  8457 
  8458                   if (text_from_string)
  8459                     {
  8460                       Lisp_Object base_string = it->stack[it->sp - 1].string;
  8461 
  8462                       if (CHARPOS (pos) >= SCHARS (base_string) - 1)
  8463                         it->end_of_box_run_p = true;
  8464                       else
  8465                         {
  8466                           next_face_id
  8467                             = face_at_string_position (it->w, base_string,
  8468                                                        CHARPOS (pos), 0,
  8469                                                        &ignore, face_id,
  8470                                                        false, 0);
  8471                           if (FACE_FROM_ID (it->f, next_face_id)->box
  8472                               == FACE_NO_BOX)
  8473                             it->end_of_box_run_p = true;
  8474                         }
  8475                     }
  8476                   else if (CHARPOS (pos) >= ZV)
  8477                     it->end_of_box_run_p = true;
  8478                   else
  8479                     {
  8480                       next_face_id =
  8481                         face_at_buffer_position (it->w, CHARPOS (pos),
  8482                                                  &ignore,
  8483                                                  CHARPOS (pos)
  8484                                                  + TEXT_PROP_DISTANCE_LIMIT,
  8485                                                  false, -1, 0);
  8486                       if (FACE_FROM_ID (it->f, next_face_id)->box
  8487                           == FACE_NO_BOX)
  8488                         it->end_of_box_run_p = true;
  8489                     }
  8490                 }
  8491             }
  8492         }
  8493       /* next_element_from_display_vector sets this flag according to
  8494          faces of the display vector glyphs, see there.  */
  8495       else if (it->method != GET_FROM_DISPLAY_VECTOR)
  8496         {
  8497           int face_id = face_after_it_pos (it);
  8498           if (face_id != it->face_id
  8499               && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX)
  8500             it->end_of_box_run_p = true;
  8501         }
  8502     }
  8503   /* If we reached the end of the object we've been iterating (e.g., a
  8504      display string or an overlay string), and there's something on
  8505      IT->stack, proceed with what's on the stack.  It doesn't make
  8506      sense to return false if there's unprocessed stuff on the stack,
  8507      because otherwise that stuff will never be displayed.  */
  8508   if (!success_p && it->sp > 0)
  8509     {
  8510       set_iterator_to_next (it, false);
  8511       success_p = get_next_display_element (it);
  8512     }
  8513 
  8514   /* Value is false if end of buffer or string reached.  */
  8515   return success_p;
  8516 }
  8517 
  8518 
  8519 /* Move IT to the next display element.
  8520 
  8521    RESEAT_P means if called on a newline in buffer text,
  8522    skip to the next visible line start.
  8523 
  8524    Functions get_next_display_element and set_iterator_to_next are
  8525    separate because I find this arrangement easier to handle than a
  8526    get_next_display_element function that also increments IT's
  8527    position.  The way it is we can first look at an iterator's current
  8528    display element, decide whether it fits on a line, and if it does,
  8529    increment the iterator position.  The other way around we probably
  8530    would either need a flag indicating whether the iterator has to be
  8531    incremented the next time, or we would have to implement a
  8532    decrement position function which would not be easy to write.  */
  8533 
  8534 void
  8535 set_iterator_to_next (struct it *it, bool reseat_p)
  8536 {
  8537 
  8538   if (max_redisplay_ticks > 0)
  8539     update_redisplay_ticks (1, it->w);
  8540 
  8541   switch (it->method)
  8542     {
  8543     case GET_FROM_BUFFER:
  8544       /* The current display element of IT is a character from
  8545          current_buffer.  Advance in the buffer, and maybe skip over
  8546          invisible lines that are so because of selective display.  */
  8547       if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
  8548         reseat_at_next_visible_line_start (it, false);
  8549       else if (it->cmp_it.id >= 0)
  8550         {
  8551           /* We are currently getting glyphs from a composition.  */
  8552           if (! it->bidi_p)
  8553             {
  8554               IT_CHARPOS (*it) += it->cmp_it.nchars;
  8555               IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  8556             }
  8557           else
  8558             {
  8559               int i;
  8560 
  8561               /* Update IT's char/byte positions to point to the first
  8562                  character of the next grapheme cluster, or to the
  8563                  character visually after the current composition.  */
  8564               for (i = 0; i < it->cmp_it.nchars; i++)
  8565                 bidi_move_to_visually_next (&it->bidi_it);
  8566               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8567               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8568             }
  8569 
  8570           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8571               && it->cmp_it.to < it->cmp_it.nglyphs)
  8572             {
  8573               /* Composition created while scanning forward.  Proceed
  8574                  to the next grapheme cluster.  */
  8575               it->cmp_it.from = it->cmp_it.to;
  8576             }
  8577           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8578                    && it->cmp_it.from > 0)
  8579             {
  8580               /* Composition created while scanning backward.  Proceed
  8581                  to the previous grapheme cluster.  */
  8582               it->cmp_it.to = it->cmp_it.from;
  8583             }
  8584           else
  8585             {
  8586               /* No more grapheme clusters in this composition.
  8587                  Find the next stop position.  */
  8588               ptrdiff_t stop = it->end_charpos;
  8589 
  8590               if (it->bidi_it.scan_dir < 0)
  8591                 /* Now we are scanning backward and don't know
  8592                    where to stop.  */
  8593                 stop = -1;
  8594               composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8595                                             IT_BYTEPOS (*it), stop, Qnil, true);
  8596             }
  8597         }
  8598       else
  8599         {
  8600           eassert (it->len != 0);
  8601 
  8602           if (!it->bidi_p)
  8603             {
  8604               IT_BYTEPOS (*it) += it->len;
  8605               IT_CHARPOS (*it) += 1;
  8606             }
  8607           else
  8608             {
  8609               int prev_scan_dir = it->bidi_it.scan_dir;
  8610               /* If this is a new paragraph, determine its base
  8611                  direction (a.k.a. its base embedding level).  */
  8612               if (it->bidi_it.new_paragraph)
  8613                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  8614                                      false);
  8615               bidi_move_to_visually_next (&it->bidi_it);
  8616               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8617               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8618               if (prev_scan_dir != it->bidi_it.scan_dir)
  8619                 {
  8620                   /* As the scan direction was changed, we must
  8621                      re-compute the stop position for composition.  */
  8622                   ptrdiff_t stop = it->end_charpos;
  8623                   if (it->bidi_it.scan_dir < 0)
  8624                     stop = -1;
  8625                   composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8626                                                 IT_BYTEPOS (*it), stop, Qnil,
  8627                                                 true);
  8628                 }
  8629             }
  8630           eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
  8631         }
  8632       break;
  8633 
  8634     case GET_FROM_C_STRING:
  8635       /* Current display element of IT is from a C string.  */
  8636       if (!it->bidi_p
  8637           /* If the string position is beyond string's end, it means
  8638              next_element_from_c_string is padding the string with
  8639              blanks, in which case we bypass the bidi iterator,
  8640              because it cannot deal with such virtual characters.  */
  8641           || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
  8642         {
  8643           IT_BYTEPOS (*it) += it->len;
  8644           IT_CHARPOS (*it) += 1;
  8645         }
  8646       else
  8647         {
  8648           bidi_move_to_visually_next (&it->bidi_it);
  8649           IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8650           IT_CHARPOS (*it) = it->bidi_it.charpos;
  8651         }
  8652       break;
  8653 
  8654     case GET_FROM_DISPLAY_VECTOR:
  8655       /* Current display element of IT is from a display table entry.
  8656          Advance in the display table definition.  Reset it to null if
  8657          end reached, and continue with characters from buffers/
  8658          strings.  */
  8659       ++it->current.dpvec_index;
  8660 
  8661       /* Restore face of the iterator to what they were before the
  8662          display vector entry (these entries may contain faces).  */
  8663       it->face_id = it->saved_face_id;
  8664 
  8665       if (it->dpvec + it->current.dpvec_index >= it->dpend)
  8666         {
  8667           bool recheck_faces = it->ellipsis_p;
  8668 
  8669           if (it->s)
  8670             it->method = GET_FROM_C_STRING;
  8671           else if (STRINGP (it->string))
  8672             it->method = GET_FROM_STRING;
  8673           else
  8674             {
  8675               it->method = GET_FROM_BUFFER;
  8676               it->object = it->w->contents;
  8677             }
  8678 
  8679           it->dpvec = NULL;
  8680           it->current.dpvec_index = -1;
  8681 
  8682           /* Skip over characters which were displayed via IT->dpvec.  */
  8683           if (it->dpvec_char_len < 0)
  8684             reseat_at_next_visible_line_start (it, true);
  8685           else if (it->dpvec_char_len > 0)
  8686             {
  8687               it->len = it->dpvec_char_len;
  8688               set_iterator_to_next (it, reseat_p);
  8689             }
  8690 
  8691           /* Maybe recheck faces after display vector.  */
  8692           if (recheck_faces)
  8693             {
  8694               if (it->method == GET_FROM_STRING)
  8695                 it->stop_charpos = IT_STRING_CHARPOS (*it);
  8696               else
  8697                 it->stop_charpos = IT_CHARPOS (*it);
  8698             }
  8699         }
  8700       break;
  8701 
  8702     case GET_FROM_STRING:
  8703       /* Current display element is a character from a Lisp string.  */
  8704       eassert (it->s == NULL && STRINGP (it->string));
  8705       /* Don't advance past string end.  These conditions are true
  8706          when set_iterator_to_next is called at the end of
  8707          get_next_display_element, in which case the Lisp string is
  8708          already exhausted, and all we want is pop the iterator
  8709          stack.  */
  8710       if (it->current.overlay_string_index >= 0)
  8711         {
  8712           /* This is an overlay string, so there's no padding with
  8713              spaces, and the number of characters in the string is
  8714              where the string ends.  */
  8715           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8716             goto consider_string_end;
  8717         }
  8718       else
  8719         {
  8720           /* Not an overlay string.  There could be padding, so test
  8721              against it->end_charpos.  */
  8722           if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  8723             goto consider_string_end;
  8724         }
  8725       if (it->cmp_it.id >= 0)
  8726         {
  8727           /* We are delivering display elements from a composition.
  8728              Update the string position past the grapheme cluster
  8729              we've just processed.  */
  8730           if (! it->bidi_p)
  8731             {
  8732               IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  8733               IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  8734             }
  8735           else
  8736             {
  8737               int i;
  8738 
  8739               for (i = 0; i < it->cmp_it.nchars; i++)
  8740                 bidi_move_to_visually_next (&it->bidi_it);
  8741               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8742               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8743             }
  8744 
  8745           /* Did we exhaust all the grapheme clusters of this
  8746              composition?  */
  8747           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8748               && (it->cmp_it.to < it->cmp_it.nglyphs))
  8749             {
  8750               /* Not all the grapheme clusters were processed yet;
  8751                  advance to the next cluster.  */
  8752               it->cmp_it.from = it->cmp_it.to;
  8753             }
  8754           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8755                    && it->cmp_it.from > 0)
  8756             {
  8757               /* Likewise: advance to the next cluster, but going in
  8758                  the reverse direction.  */
  8759               it->cmp_it.to = it->cmp_it.from;
  8760             }
  8761           else
  8762             {
  8763               /* This composition was fully processed; find the next
  8764                  candidate place for checking for composed
  8765                  characters.  */
  8766               /* Always limit string searches to the string length;
  8767                  any padding spaces are not part of the string, and
  8768                  there cannot be any compositions in that padding.  */
  8769               ptrdiff_t stop = SCHARS (it->string);
  8770 
  8771               if (it->bidi_p && it->bidi_it.scan_dir < 0)
  8772                 stop = -1;
  8773               else if (it->end_charpos < stop)
  8774                 {
  8775                   /* Cf. PRECISION in reseat_to_string: we might be
  8776                      limited in how many of the string characters we
  8777                      need to deliver.  */
  8778                   stop = it->end_charpos;
  8779                 }
  8780               composition_compute_stop_pos (&it->cmp_it,
  8781                                             IT_STRING_CHARPOS (*it),
  8782                                             IT_STRING_BYTEPOS (*it), stop,
  8783                                             it->string, true);
  8784             }
  8785         }
  8786       else
  8787         {
  8788           if (!it->bidi_p
  8789               /* If the string position is beyond string's end, it
  8790                  means next_element_from_string is padding the string
  8791                  with blanks, in which case we bypass the bidi
  8792                  iterator, because it cannot deal with such virtual
  8793                  characters.  */
  8794               || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
  8795             {
  8796               IT_STRING_BYTEPOS (*it) += it->len;
  8797               IT_STRING_CHARPOS (*it) += 1;
  8798             }
  8799           else
  8800             {
  8801               int prev_scan_dir = it->bidi_it.scan_dir;
  8802 
  8803               bidi_move_to_visually_next (&it->bidi_it);
  8804               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8805               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8806               /* If the scan direction changes, we may need to update
  8807                  the place where to check for composed characters.  */
  8808               if (prev_scan_dir != it->bidi_it.scan_dir)
  8809                 {
  8810                   ptrdiff_t stop = SCHARS (it->string);
  8811 
  8812                   if (it->bidi_it.scan_dir < 0)
  8813                     stop = -1;
  8814                   else if (it->end_charpos < stop)
  8815                     stop = it->end_charpos;
  8816 
  8817                   composition_compute_stop_pos (&it->cmp_it,
  8818                                                 IT_STRING_CHARPOS (*it),
  8819                                                 IT_STRING_BYTEPOS (*it), stop,
  8820                                                 it->string, true);
  8821                 }
  8822             }
  8823         }
  8824 
  8825     consider_string_end:
  8826 
  8827       if (it->current.overlay_string_index >= 0)
  8828         {
  8829           /* IT->string is an overlay string.  Advance to the
  8830              next, if there is one.  */
  8831           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8832             {
  8833               it->ellipsis_p = false;
  8834               next_overlay_string (it);
  8835               if (it->ellipsis_p)
  8836                 setup_for_ellipsis (it, 0);
  8837             }
  8838         }
  8839       else
  8840         {
  8841           /* IT->string is not an overlay string.  If we reached
  8842              its end, and there is something on IT->stack, proceed
  8843              with what is on the stack.  This can be either another
  8844              string, this time an overlay string, or a buffer.  */
  8845           if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
  8846               && it->sp > 0)
  8847             {
  8848               pop_it (it);
  8849               if (it->method == GET_FROM_STRING)
  8850                 goto consider_string_end;
  8851             }
  8852         }
  8853       break;
  8854 
  8855     case GET_FROM_IMAGE:
  8856     case GET_FROM_STRETCH:
  8857     case GET_FROM_XWIDGET:
  8858 
  8859       /* The position etc with which we have to proceed are on
  8860          the stack.  The position may be at the end of a string,
  8861          if the `display' property takes up the whole string.  */
  8862       eassert (it->sp > 0);
  8863       pop_it (it);
  8864       if (it->method == GET_FROM_STRING)
  8865         goto consider_string_end;
  8866       break;
  8867 
  8868     default:
  8869       /* There are no other methods defined, so this should be a bug.  */
  8870       emacs_abort ();
  8871     }
  8872 
  8873   eassert (it->method != GET_FROM_STRING
  8874            || (STRINGP (it->string)
  8875                && IT_STRING_CHARPOS (*it) >= 0));
  8876 }
  8877 
  8878 /* Load IT's display element fields with information about the next
  8879    display element which comes from a display table entry or from the
  8880    result of translating a control character to one of the forms `^C'
  8881    or `\003'.
  8882 
  8883    IT->dpvec holds the glyphs to return as characters.
  8884    IT->saved_face_id holds the face id before the display vector--it
  8885    is restored into IT->face_id in set_iterator_to_next.  */
  8886 
  8887 static bool
  8888 next_element_from_display_vector (struct it *it)
  8889 {
  8890   Lisp_Object gc;
  8891   int prev_face_id = it->face_id;
  8892   int next_face_id;
  8893 
  8894   /* Precondition.  */
  8895   eassert (it->dpvec && it->current.dpvec_index >= 0);
  8896 
  8897   it->face_id = it->saved_face_id;
  8898 
  8899   /* KFS: This code used to check ip->dpvec[0] instead of the current element.
  8900      That seemed totally bogus - so I changed it...  */
  8901   if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
  8902       && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
  8903     {
  8904       struct face *this_face, *prev_face, *next_face;
  8905 
  8906       it->c = GLYPH_CODE_CHAR (gc);
  8907       it->len = CHAR_BYTES (it->c);
  8908 
  8909       /* The entry may contain a face id to use.  Such a face id is
  8910          the id of a Lisp face, not a realized face.  A face id of
  8911          zero means no face is specified.  */
  8912       if (it->dpvec_face_id >= 0)
  8913         it->face_id = it->dpvec_face_id;
  8914       else
  8915         {
  8916           int lface_id = GLYPH_CODE_FACE (gc);
  8917           if (lface_id > 0)
  8918             it->face_id = merge_faces (it->w, Qt, lface_id,
  8919                                        it->saved_face_id);
  8920         }
  8921 
  8922       /* Glyphs in the display vector could have the box face, so we
  8923          need to set the related flags in the iterator, as
  8924          appropriate.  */
  8925       this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8926       prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  8927 
  8928       /* Is this character the first character of a box-face run?  */
  8929       it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
  8930                                 && (!prev_face
  8931                                     || prev_face->box == FACE_NO_BOX));
  8932 
  8933       /* For the last character of the box-face run, we need to look
  8934          either at the next glyph from the display vector, or at the
  8935          face we saw before the display vector.  */
  8936       next_face_id = it->saved_face_id;
  8937       if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
  8938         {
  8939           if (it->dpvec_face_id >= 0)
  8940             next_face_id = it->dpvec_face_id;
  8941           else
  8942             {
  8943               Lisp_Object gc = it->dpvec[it->current.dpvec_index + 1];
  8944               int lface_id = GLYPH_CODE_P (gc) ? GLYPH_CODE_FACE (gc) : 0;
  8945 
  8946               if (lface_id > 0)
  8947                 next_face_id = merge_faces (it->w, Qt, lface_id,
  8948                                             it->saved_face_id);
  8949             }
  8950         }
  8951       next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
  8952       if (this_face && this_face->box != FACE_NO_BOX
  8953           && (!next_face || next_face->box == FACE_NO_BOX))
  8954         it->end_of_box_run_p = true;
  8955       it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
  8956     }
  8957   else
  8958     /* Display table entry is invalid.  Return a space.  */
  8959     it->c = ' ', it->len = 1;
  8960 
  8961   /* Don't change position and object of the iterator here.  They are
  8962      still the values of the character that had this display table
  8963      entry or was translated, and that's what we want.  */
  8964   it->what = IT_CHARACTER;
  8965   return true;
  8966 }
  8967 
  8968 /* Get the first element of string/buffer in the visual order, after
  8969    being reseated to a new position in a string or a buffer.  */
  8970 static void
  8971 get_visually_first_element (struct it *it)
  8972 {
  8973   bool string_p = STRINGP (it->string) || it->s;
  8974   ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
  8975   ptrdiff_t bob;
  8976   ptrdiff_t obegv = BEGV;
  8977 
  8978   SET_WITH_NARROWED_BEGV (it, bob,
  8979                           string_p ? 0 :
  8980                           IT_CHARPOS (*it) < BEGV ? obegv : BEGV,
  8981                           it->medium_narrowing_begv);
  8982 
  8983   if (STRINGP (it->string))
  8984     {
  8985       it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
  8986       it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
  8987     }
  8988   else
  8989     {
  8990       it->bidi_it.charpos = IT_CHARPOS (*it);
  8991       it->bidi_it.bytepos = IT_BYTEPOS (*it);
  8992     }
  8993 
  8994   if (it->bidi_it.charpos == eob)
  8995     {
  8996       /* Nothing to do, but reset the FIRST_ELT flag, like
  8997          bidi_paragraph_init does, because we are not going to
  8998          call it.  */
  8999       it->bidi_it.first_elt = false;
  9000     }
  9001   else if (it->bidi_it.charpos == bob
  9002            || (!string_p
  9003                && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
  9004                    || FETCH_BYTE (it->bidi_it.bytepos) == '\n')))
  9005     {
  9006       /* If we are at the beginning of a line/string, we can produce
  9007          the next element right away.  */
  9008       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9009       bidi_move_to_visually_next (&it->bidi_it);
  9010     }
  9011   else
  9012     {
  9013       ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  9014 
  9015       /* We need to prime the bidi iterator starting at the line's or
  9016          string's beginning, before we will be able to produce the
  9017          next element.  */
  9018       if (string_p)
  9019         it->bidi_it.charpos = it->bidi_it.bytepos = 0;
  9020       else
  9021         SET_WITH_NARROWED_BEGV (it, it->bidi_it.charpos,
  9022                                 find_newline_no_quit (IT_CHARPOS (*it),
  9023                                                       IT_BYTEPOS (*it), -1,
  9024                                                       &it->bidi_it.bytepos),
  9025                                 it->medium_narrowing_begv);
  9026       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9027       do
  9028         {
  9029           /* Now return to buffer/string position where we were asked
  9030              to get the next display element, and produce that.  */
  9031           bidi_move_to_visually_next (&it->bidi_it);
  9032         }
  9033       while (it->bidi_it.bytepos != orig_bytepos
  9034              && it->bidi_it.charpos < eob);
  9035     }
  9036 
  9037   /*  Adjust IT's position information to where we ended up.  */
  9038   if (STRINGP (it->string))
  9039     {
  9040       IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  9041       IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  9042     }
  9043   else
  9044     {
  9045       IT_CHARPOS (*it) = it->bidi_it.charpos;
  9046       IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  9047     }
  9048 
  9049   if (STRINGP (it->string) || !it->s)
  9050     {
  9051       ptrdiff_t stop, charpos, bytepos;
  9052 
  9053       if (STRINGP (it->string))
  9054         {
  9055           eassert (!it->s);
  9056           stop = SCHARS (it->string);
  9057           if (stop > it->end_charpos)
  9058             stop = it->end_charpos;
  9059           charpos = IT_STRING_CHARPOS (*it);
  9060           bytepos = IT_STRING_BYTEPOS (*it);
  9061         }
  9062       else
  9063         {
  9064           stop = it->end_charpos;
  9065           charpos = IT_CHARPOS (*it);
  9066           bytepos = IT_BYTEPOS (*it);
  9067         }
  9068       if (it->bidi_it.scan_dir < 0)
  9069         stop = -1;
  9070       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
  9071                                     it->string, true);
  9072     }
  9073 }
  9074 
  9075 /* Load IT with the next display element from Lisp string IT->string.
  9076    IT->current.string_pos is the current position within the string.
  9077    If IT->current.overlay_string_index >= 0, the Lisp string is an
  9078    overlay string.  */
  9079 
  9080 static bool
  9081 next_element_from_string (struct it *it)
  9082 {
  9083   struct text_pos position;
  9084 
  9085   eassert (STRINGP (it->string));
  9086   eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
  9087   eassert (IT_STRING_CHARPOS (*it) >= 0);
  9088   position = it->current.string_pos;
  9089 
  9090   /* With bidi reordering, the character to display might not be the
  9091      character at IT_STRING_CHARPOS.  BIDI_IT.FIRST_ELT means
  9092      that we were reseat()ed to a new string, whose paragraph
  9093      direction is not known.  */
  9094   if (it->bidi_p && it->bidi_it.first_elt)
  9095     {
  9096       get_visually_first_element (it);
  9097       SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
  9098     }
  9099 
  9100   /* Time to check for invisible text?  */
  9101   if (IT_STRING_CHARPOS (*it) < it->end_charpos)
  9102     {
  9103       if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
  9104         {
  9105           if (!(!it->bidi_p
  9106                 || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9107                 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
  9108             {
  9109               /* With bidi non-linear iteration, we could find
  9110                  ourselves far beyond the last computed stop_charpos,
  9111                  with several other stop positions in between that we
  9112                  missed.  Scan them all now, in buffer's logical
  9113                  order, until we find and handle the last stop_charpos
  9114                  that precedes our current position.  */
  9115               handle_stop_backwards (it, it->stop_charpos);
  9116               return GET_NEXT_DISPLAY_ELEMENT (it);
  9117             }
  9118           else
  9119             {
  9120               if (it->bidi_p)
  9121                 {
  9122                   /* Take note of the stop position we just moved
  9123                      across, for when we will move back across it.  */
  9124                   it->prev_stop = it->stop_charpos;
  9125                   /* If we are at base paragraph embedding level, take
  9126                      note of the last stop position seen at this
  9127                      level.  */
  9128                   if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9129                     it->base_level_stop = it->stop_charpos;
  9130                 }
  9131               handle_stop (it);
  9132 
  9133               /* Since a handler may have changed IT->method, we must
  9134                  recurse here.  */
  9135               return GET_NEXT_DISPLAY_ELEMENT (it);
  9136             }
  9137         }
  9138       else if (it->bidi_p
  9139                /* If we are before prev_stop, we may have overstepped
  9140                   on our way backwards a stop_pos, and if so, we need
  9141                   to handle that stop_pos.  */
  9142                && IT_STRING_CHARPOS (*it) < it->prev_stop
  9143                /* We can sometimes back up for reasons that have nothing
  9144                   to do with bidi reordering.  E.g., compositions.  The
  9145                   code below is only needed when we are above the base
  9146                   embedding level, so test for that explicitly.  */
  9147                && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9148         {
  9149           /* If we lost track of base_level_stop, we have no better
  9150              place for handle_stop_backwards to start from than string
  9151              beginning.  This happens, e.g., when we were reseated to
  9152              the previous screenful of text by vertical-motion.  */
  9153           if (it->base_level_stop <= 0
  9154               || IT_STRING_CHARPOS (*it) < it->base_level_stop)
  9155             it->base_level_stop = 0;
  9156           handle_stop_backwards (it, it->base_level_stop);
  9157           return GET_NEXT_DISPLAY_ELEMENT (it);
  9158         }
  9159     }
  9160 
  9161   if (it->current.overlay_string_index >= 0)
  9162     {
  9163       /* Get the next character from an overlay string.  In overlay
  9164          strings, there is no field width or padding with spaces to
  9165          do.  */
  9166       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  9167         {
  9168           it->what = IT_EOB;
  9169           return false;
  9170         }
  9171       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9172                                 IT_STRING_BYTEPOS (*it),
  9173                                 it->bidi_it.scan_dir < 0
  9174                                 ? -1
  9175                                 : SCHARS (it->string))
  9176                && next_element_from_composition (it))
  9177         {
  9178           return true;
  9179         }
  9180       else if (STRING_MULTIBYTE (it->string))
  9181         {
  9182           const unsigned char *s = (SDATA (it->string)
  9183                                     + IT_STRING_BYTEPOS (*it));
  9184           it->c = check_char_and_length (s, &it->len);
  9185         }
  9186       else
  9187         {
  9188           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9189           it->len = 1;
  9190         }
  9191     }
  9192   else
  9193     {
  9194       /* Get the next character from a Lisp string that is not an
  9195          overlay string.  Such strings come from the mode line, for
  9196          example.  We may have to pad with spaces, or truncate the
  9197          string.  See also next_element_from_c_string.  */
  9198       if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  9199         {
  9200           it->what = IT_EOB;
  9201           return false;
  9202         }
  9203       else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
  9204         {
  9205           /* Pad with spaces.  */
  9206           it->c = ' ', it->len = 1;
  9207           CHARPOS (position) = BYTEPOS (position) = -1;
  9208         }
  9209       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9210                                 IT_STRING_BYTEPOS (*it),
  9211                                 it->bidi_it.scan_dir < 0
  9212                                 ? -1
  9213                                 : it->string_nchars)
  9214                && next_element_from_composition (it))
  9215         {
  9216           return true;
  9217         }
  9218       else if (STRING_MULTIBYTE (it->string))
  9219         {
  9220           const unsigned char *s = (SDATA (it->string)
  9221                                     + IT_STRING_BYTEPOS (*it));
  9222           it->c = check_char_and_length (s, &it->len);
  9223         }
  9224       else
  9225         {
  9226           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9227           it->len = 1;
  9228         }
  9229     }
  9230 
  9231   /* Record what we have and where it came from.  */
  9232   it->what = IT_CHARACTER;
  9233   it->object = it->string;
  9234   it->position = position;
  9235   return true;
  9236 }
  9237 
  9238 
  9239 /* Load IT with next display element from C string IT->s.
  9240    IT->string_nchars is the maximum number of characters to return
  9241    from the string.  IT->end_charpos may be greater than
  9242    IT->string_nchars when this function is called, in which case we
  9243    may have to return padding spaces.  Value is false if end of string
  9244    reached, including padding spaces.  */
  9245 
  9246 static bool
  9247 next_element_from_c_string (struct it *it)
  9248 {
  9249   bool success_p = true;
  9250 
  9251   eassert (it->s);
  9252   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
  9253   it->what = IT_CHARACTER;
  9254   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
  9255   it->object = make_fixnum (0);
  9256 
  9257   /* With bidi reordering, the character to display might not be the
  9258      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9259      we were reseated to a new string, whose paragraph direction is
  9260      not known.  */
  9261   if (it->bidi_p && it->bidi_it.first_elt)
  9262     get_visually_first_element (it);
  9263 
  9264   /* IT's position can be greater than IT->string_nchars in case a
  9265      field width or precision has been specified when the iterator was
  9266      initialized.  */
  9267   if (IT_CHARPOS (*it) >= it->end_charpos)
  9268     {
  9269       /* End of the game.  */
  9270       it->what = IT_EOB;
  9271       success_p = false;
  9272     }
  9273   else if (IT_CHARPOS (*it) >= it->string_nchars)
  9274     {
  9275       /* Pad with spaces.  */
  9276       it->c = ' ', it->len = 1;
  9277       BYTEPOS (it->position) = CHARPOS (it->position) = -1;
  9278     }
  9279   else if (it->multibyte_p)
  9280     it->c = check_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
  9281   else
  9282     it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
  9283 
  9284   return success_p;
  9285 }
  9286 
  9287 
  9288 /* Set up IT to return characters from an ellipsis, if appropriate.
  9289    The definition of the ellipsis glyphs may come from a display table
  9290    entry.  This function fills IT with the first glyph from the
  9291    ellipsis if an ellipsis is to be displayed.  */
  9292 
  9293 static bool
  9294 next_element_from_ellipsis (struct it *it)
  9295 {
  9296   if (it->selective_display_ellipsis_p)
  9297     setup_for_ellipsis (it, it->len);
  9298   else
  9299     {
  9300       /* The face at the current position may be different from the
  9301          face we find after the invisible text.  Remember what it
  9302          was in IT->saved_face_id, and signal that it's there by
  9303          setting face_before_selective_p.  */
  9304       it->saved_face_id = it->face_id;
  9305       it->method = GET_FROM_BUFFER;
  9306       it->object = it->w->contents;
  9307       reseat_at_next_visible_line_start (it, true);
  9308       it->face_before_selective_p = true;
  9309     }
  9310 
  9311   return GET_NEXT_DISPLAY_ELEMENT (it);
  9312 }
  9313 
  9314 
  9315 /* Deliver an image display element.  The iterator IT is already
  9316    filled with image information (done in handle_display_prop).  Value
  9317    is always true.  */
  9318 
  9319 
  9320 static bool
  9321 next_element_from_image (struct it *it)
  9322 {
  9323   it->what = IT_IMAGE;
  9324   return true;
  9325 }
  9326 
  9327 static bool
  9328 next_element_from_xwidget (struct it *it)
  9329 {
  9330   it->what = IT_XWIDGET;
  9331   return true;
  9332 }
  9333 
  9334 
  9335 /* Fill iterator IT with next display element from a stretch glyph
  9336    property.  IT->object is the value of the text property.  Value is
  9337    always true.  */
  9338 
  9339 static bool
  9340 next_element_from_stretch (struct it *it)
  9341 {
  9342   it->what = IT_STRETCH;
  9343   return true;
  9344 }
  9345 
  9346 /* Scan backwards from IT's current position until we find a stop
  9347    position, or until BEGV.  This is called when we find ourself
  9348    before both the last known prev_stop and base_level_stop while
  9349    reordering bidirectional text.  */
  9350 
  9351 static void
  9352 compute_stop_pos_backwards (struct it *it)
  9353 {
  9354   const int SCAN_BACK_LIMIT = 1000;
  9355   struct text_pos pos;
  9356   struct display_pos save_current = it->current;
  9357   struct text_pos save_position = it->position;
  9358   ptrdiff_t charpos = IT_CHARPOS (*it);
  9359   ptrdiff_t where_we_are = charpos;
  9360   ptrdiff_t save_stop_pos = it->stop_charpos;
  9361   ptrdiff_t save_end_pos = it->end_charpos;
  9362 
  9363   eassert (NILP (it->string) && !it->s);
  9364   eassert (it->bidi_p);
  9365   it->bidi_p = false;
  9366   do
  9367     {
  9368       it->end_charpos = min (charpos + 1, ZV);
  9369       charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
  9370       SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
  9371       reseat_1 (it, pos, false);
  9372       compute_stop_pos (it);
  9373       /* We must advance forward, right?  */
  9374       if (it->stop_charpos <= charpos)
  9375         emacs_abort ();
  9376     }
  9377   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
  9378 
  9379   if (it->stop_charpos <= where_we_are)
  9380     it->prev_stop = it->stop_charpos;
  9381   else
  9382     it->prev_stop = BEGV;
  9383   it->bidi_p = true;
  9384   it->current = save_current;
  9385   it->position = save_position;
  9386   it->stop_charpos = save_stop_pos;
  9387   it->end_charpos = save_end_pos;
  9388 }
  9389 
  9390 /* Scan forward from CHARPOS in the current buffer/string, until we
  9391    find a stop position > current IT's position.  Then handle the stop
  9392    position before that.  This is called when we bump into a stop
  9393    position while reordering bidirectional text.  CHARPOS should be
  9394    the last previously processed stop_pos (or BEGV/0, if none were
  9395    processed yet) whose position is less than IT's current
  9396    position.  */
  9397 
  9398 static void
  9399 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  9400 {
  9401   bool bufp = !STRINGP (it->string);
  9402   ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
  9403   struct display_pos save_current = it->current;
  9404   struct text_pos save_position = it->position;
  9405   struct composition_it save_cmp_it = it->cmp_it;
  9406   struct text_pos pos1;
  9407   ptrdiff_t next_stop;
  9408 
  9409   /* Scan in strict logical order.  */
  9410   eassert (it->bidi_p);
  9411   it->bidi_p = false;
  9412   do
  9413     {
  9414       it->prev_stop = charpos;
  9415       if (bufp)
  9416         {
  9417           SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
  9418           reseat_1 (it, pos1, false);
  9419         }
  9420       else
  9421         it->current.string_pos = string_pos (charpos, it->string);
  9422       compute_stop_pos (it);
  9423       /* We must advance forward, right?  */
  9424       if (it->stop_charpos <= it->prev_stop)
  9425         emacs_abort ();
  9426       charpos = it->stop_charpos;
  9427     }
  9428   while (charpos <= where_we_are);
  9429 
  9430   it->bidi_p = true;
  9431   it->current = save_current;
  9432   it->position = save_position;
  9433   it->cmp_it = save_cmp_it;
  9434   next_stop = it->stop_charpos;
  9435   it->stop_charpos = it->prev_stop;
  9436   handle_stop (it);
  9437   it->stop_charpos = next_stop;
  9438 }
  9439 
  9440 /* Load IT with the next display element from current_buffer.  Value
  9441    is false if end of buffer reached.  IT->stop_charpos is the next
  9442    position at which to stop and check for text properties or buffer
  9443    end.  */
  9444 
  9445 static bool
  9446 next_element_from_buffer (struct it *it)
  9447 {
  9448   bool success_p = true;
  9449 
  9450   eassert (IT_CHARPOS (*it) >= BEGV);
  9451   eassert (NILP (it->string) && !it->s);
  9452   eassert (!it->bidi_p
  9453            || (NILP (it->bidi_it.string.lstring)
  9454                && it->bidi_it.string.s == NULL));
  9455 
  9456   /* With bidi reordering, the character to display might not be the
  9457      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9458      we were reseat()ed to a new buffer position, which is potentially
  9459      a different paragraph.  */
  9460   if (it->bidi_p && it->bidi_it.first_elt)
  9461     {
  9462       get_visually_first_element (it);
  9463       SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9464     }
  9465 
  9466   if (IT_CHARPOS (*it) >= it->stop_charpos)
  9467     {
  9468       if (IT_CHARPOS (*it) >= it->end_charpos)
  9469         {
  9470           bool overlay_strings_follow_p;
  9471 
  9472           /* End of the game, except when overlay strings follow that
  9473              haven't been returned yet.  */
  9474           if (it->overlay_strings_at_end_processed_p)
  9475             overlay_strings_follow_p = false;
  9476           else
  9477             {
  9478               it->overlay_strings_at_end_processed_p = true;
  9479               overlay_strings_follow_p = get_overlay_strings (it, 0);
  9480             }
  9481 
  9482           if (overlay_strings_follow_p)
  9483             success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  9484           else
  9485             {
  9486               it->what = IT_EOB;
  9487               it->position = it->current.pos;
  9488               success_p = false;
  9489             }
  9490         }
  9491       else if (!(!it->bidi_p
  9492                  || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9493                  || IT_CHARPOS (*it) == it->stop_charpos))
  9494         {
  9495           /* With bidi non-linear iteration, we could find ourselves
  9496              far beyond the last computed stop_charpos, with several
  9497              other stop positions in between that we missed.  Scan
  9498              them all now, in buffer's logical order, until we find
  9499              and handle the last stop_charpos that precedes our
  9500              current position.  */
  9501           handle_stop_backwards (it, it->stop_charpos);
  9502           it->ignore_overlay_strings_at_pos_p = false;
  9503           return GET_NEXT_DISPLAY_ELEMENT (it);
  9504         }
  9505       else
  9506         {
  9507           if (it->bidi_p)
  9508             {
  9509               /* Take note of the stop position we just moved across,
  9510                  for when we will move back across it.  */
  9511               it->prev_stop = it->stop_charpos;
  9512               /* If we are at base paragraph embedding level, take
  9513                  note of the last stop position seen at this
  9514                  level.  */
  9515               if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9516                 it->base_level_stop = it->stop_charpos;
  9517             }
  9518           handle_stop (it);
  9519           it->ignore_overlay_strings_at_pos_p = false;
  9520           return GET_NEXT_DISPLAY_ELEMENT (it);
  9521         }
  9522     }
  9523   else if (it->bidi_p
  9524            /* If we are before prev_stop, we may have overstepped on
  9525               our way backwards a stop_pos, and if so, we need to
  9526               handle that stop_pos.  */
  9527            && IT_CHARPOS (*it) < it->prev_stop
  9528            /* We can sometimes back up for reasons that have nothing
  9529               to do with bidi reordering.  E.g., compositions.  The
  9530               code below is only needed when we are above the base
  9531               embedding level, so test for that explicitly.  */
  9532            && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9533     {
  9534       if (it->base_level_stop <= 0
  9535           || IT_CHARPOS (*it) < it->base_level_stop)
  9536         {
  9537           /* If we lost track of base_level_stop, we need to find
  9538              prev_stop by looking backwards.  This happens, e.g., when
  9539              we were reseated to the previous screenful of text by
  9540              vertical-motion.  */
  9541           it->base_level_stop = BEGV;
  9542           compute_stop_pos_backwards (it);
  9543           handle_stop_backwards (it, it->prev_stop);
  9544         }
  9545       else
  9546         handle_stop_backwards (it, it->base_level_stop);
  9547       it->ignore_overlay_strings_at_pos_p = false;
  9548       return GET_NEXT_DISPLAY_ELEMENT (it);
  9549     }
  9550   else
  9551     {
  9552       /* No face changes, overlays etc. in sight, so just return a
  9553          character from current_buffer.  */
  9554       unsigned char *p;
  9555       ptrdiff_t stop;
  9556 
  9557       /* We moved to the next buffer position, so any info about
  9558          previously seen overlays is no longer valid.  */
  9559       it->ignore_overlay_strings_at_pos_p = false;
  9560 
  9561       if (composition_break_at_point
  9562           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  9563           && !NILP (Vauto_composition_mode))
  9564         {
  9565           /* Limit search for composable characters to point's position.  */
  9566           if (it->bidi_it.scan_dir < 0)
  9567             stop = (PT <= IT_CHARPOS (*it)) ? PT : -1;
  9568           else
  9569             stop = (IT_CHARPOS (*it) < PT
  9570                     && PT < it->end_charpos) ? PT : it->end_charpos;
  9571         }
  9572       else
  9573         stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
  9574       if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
  9575                            stop)
  9576           && next_element_from_composition (it))
  9577         {
  9578           return true;
  9579         }
  9580 
  9581       /* Get the next character, maybe multibyte.  */
  9582       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
  9583       if (it->multibyte_p && !ASCII_CHAR_P (*p))
  9584         it->c = string_char_and_length (p, &it->len);
  9585       else
  9586         it->c = *p, it->len = 1;
  9587 
  9588       /* Record what we have and where it came from.  */
  9589       it->what = IT_CHARACTER;
  9590       it->object = it->w->contents;
  9591       it->position = it->current.pos;
  9592 
  9593       /* Normally we return the character found above, except when we
  9594          really want to return an ellipsis for selective display.  */
  9595       if (it->selective)
  9596         {
  9597           if (it->c == '\n')
  9598             {
  9599               /* A value of selective > 0 means hide lines indented more
  9600                  than that number of columns.  */
  9601               if (it->selective > 0
  9602                   && IT_CHARPOS (*it) + 1 < ZV
  9603                   && indented_beyond_p (IT_CHARPOS (*it) + 1,
  9604                                         IT_BYTEPOS (*it) + 1,
  9605                                         it->selective))
  9606                 {
  9607                   success_p = next_element_from_ellipsis (it);
  9608                   it->dpvec_char_len = -1;
  9609                 }
  9610             }
  9611           else if (it->c == '\r' && it->selective == -1)
  9612             {
  9613               /* A value of selective == -1 means that everything from the
  9614                  CR to the end of the line is invisible, with maybe an
  9615                  ellipsis displayed for it.  */
  9616               success_p = next_element_from_ellipsis (it);
  9617               it->dpvec_char_len = -1;
  9618             }
  9619         }
  9620     }
  9621 
  9622   /* Value is false if end of buffer reached.  */
  9623   eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
  9624   return success_p;
  9625 }
  9626 
  9627 
  9628 /* Deliver a composition display element.  Unlike the other
  9629    next_element_from_XXX, this function is not registered in the array
  9630    get_next_element[].  It is called from next_element_from_buffer and
  9631    next_element_from_string when necessary.  */
  9632 
  9633 static bool
  9634 next_element_from_composition (struct it *it)
  9635 {
  9636   it->what = IT_COMPOSITION;
  9637   it->len = it->cmp_it.nbytes;
  9638   if (STRINGP (it->string))
  9639     {
  9640       if (it->c < 0)
  9641         {
  9642           IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  9643           IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  9644           return false;
  9645         }
  9646       it->position = it->current.string_pos;
  9647       it->object = it->string;
  9648       it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
  9649                                      IT_STRING_BYTEPOS (*it), it->string);
  9650     }
  9651   else
  9652     {
  9653       if (it->c < 0)
  9654         {
  9655           IT_CHARPOS (*it) += it->cmp_it.nchars;
  9656           IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  9657           if (it->bidi_p)
  9658             {
  9659               if (it->bidi_it.new_paragraph)
  9660                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  9661                                      false);
  9662               /* Resync the bidi iterator with IT's new position.
  9663                  FIXME: this doesn't support bidirectional text.  */
  9664               while (it->bidi_it.charpos < IT_CHARPOS (*it))
  9665                 bidi_move_to_visually_next (&it->bidi_it);
  9666             }
  9667           return false;
  9668         }
  9669       it->position = it->current.pos;
  9670       it->object = it->w->contents;
  9671       it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
  9672                                      IT_BYTEPOS (*it), Qnil);
  9673     }
  9674   return true;
  9675 }
  9676 
  9677 
  9678 
  9679 /***********************************************************************
  9680              Moving an iterator without producing glyphs
  9681  ***********************************************************************/
  9682 
  9683 /* Check if iterator is at a position corresponding to a valid buffer
  9684    position after some move_it_ call.  */
  9685 
  9686 #define IT_POS_VALID_AFTER_MOVE_P(it)                   \
  9687   ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
  9688 
  9689 
  9690 /* Move iterator IT to a specified buffer or X position within one
  9691    line on the display without producing glyphs.
  9692 
  9693    OP should be a bit mask including some or all of these bits:
  9694     MOVE_TO_X: Stop upon reaching x-position TO_X.
  9695     MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
  9696    Regardless of OP's value, stop upon reaching the end of the display line.
  9697 
  9698    TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
  9699    This means, in particular, that TO_X includes window's horizontal
  9700    scroll amount.
  9701 
  9702    The return value has several possible values that
  9703    say what condition caused the scan to stop:
  9704 
  9705    MOVE_POS_MATCH_OR_ZV
  9706      - when TO_POS or ZV was reached.
  9707 
  9708    MOVE_X_REACHED
  9709      -when TO_X was reached before TO_POS or ZV were reached.
  9710 
  9711    MOVE_LINE_CONTINUED
  9712      - when we reached the end of the display area and the line must
  9713      be continued.
  9714 
  9715    MOVE_LINE_TRUNCATED
  9716      - when we reached the end of the display area and the line is
  9717      truncated.
  9718 
  9719    MOVE_NEWLINE_OR_CR
  9720      - when we stopped at a line end, i.e. a newline or a CR and selective
  9721      display is on.  */
  9722 
  9723 static enum move_it_result
  9724 move_it_in_display_line_to (struct it *it,
  9725                             ptrdiff_t to_charpos, int to_x,
  9726                             enum move_operation_enum op)
  9727 {
  9728   enum move_it_result result = MOVE_UNDEFINED;
  9729   struct glyph_row *saved_glyph_row;
  9730   struct it wrap_it, atpos_it, atx_it, ppos_it;
  9731   void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
  9732   void *ppos_data = NULL;
  9733   bool may_wrap = false;
  9734   enum it_method prev_method = it->method;
  9735   ptrdiff_t closest_pos UNINIT;
  9736   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  9737   bool saw_smaller_pos = prev_pos < to_charpos;
  9738   bool line_number_pending = false;
  9739 
  9740   /* Don't produce glyphs in produce_glyphs.  */
  9741   saved_glyph_row = it->glyph_row;
  9742   it->glyph_row = NULL;
  9743 
  9744   /* Use wrap_it to save a copy of IT wherever a word wrap could
  9745      occur.  Use atpos_it to save a copy of IT at the desired buffer
  9746      position, if found, so that we can scan ahead and check if the
  9747      word later overshoots the window edge.  Use atx_it similarly, for
  9748      pixel positions.  */
  9749   wrap_it.sp = -1;
  9750   atpos_it.sp = -1;
  9751   atx_it.sp = -1;
  9752 
  9753   /* Use ppos_it under bidi reordering to save a copy of IT for the
  9754      initial position.  We restore that position in IT when we have
  9755      scanned the entire display line without finding a match for
  9756      TO_CHARPOS and all the character positions are greater than
  9757      TO_CHARPOS.  We then restart the scan from the initial position,
  9758      and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
  9759      the closest to TO_CHARPOS.  */
  9760   if (it->bidi_p)
  9761     {
  9762       if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
  9763         {
  9764           SAVE_IT (ppos_it, *it, ppos_data);
  9765           closest_pos = IT_CHARPOS (*it);
  9766         }
  9767       else
  9768         closest_pos = ZV;
  9769     }
  9770 
  9771 #define BUFFER_POS_REACHED_P()                                  \
  9772   ((op & MOVE_TO_POS) != 0                                      \
  9773    && BUFFERP (it->object)                                      \
  9774    && (IT_CHARPOS (*it) == to_charpos                           \
  9775        || ((!it->bidi_p                                         \
  9776             || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
  9777            && IT_CHARPOS (*it) > to_charpos)                    \
  9778        || (it->what == IT_COMPOSITION                           \
  9779            && ((IT_CHARPOS (*it) > to_charpos                   \
  9780                 && to_charpos >= it->cmp_it.charpos)            \
  9781                || (IT_CHARPOS (*it) < to_charpos                \
  9782                    && to_charpos <= it->cmp_it.charpos))))      \
  9783    && (it->method == GET_FROM_BUFFER                            \
  9784        || (it->method == GET_FROM_DISPLAY_VECTOR                \
  9785            && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
  9786 
  9787   if (it->hpos == 0)
  9788     {
  9789       /* If line numbers are being displayed, produce a line number.
  9790          But don't do that if we are to reach first_visible_x, because
  9791          line numbers are not relevant to stuff that is not visible on
  9792          display.  */
  9793       if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
  9794           && should_produce_line_number (it))
  9795         {
  9796           if (it->current_x == it->first_visible_x)
  9797             maybe_produce_line_number (it);
  9798           else
  9799             line_number_pending = true;
  9800         }
  9801       /* If there's a line-/wrap-prefix, handle it, if we didn't already.  */
  9802       if (it->area == TEXT_AREA && !it->string_from_prefix_prop_p)
  9803         handle_line_prefix (it);
  9804     }
  9805 
  9806   if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9807     SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9808 
  9809   while (true)
  9810     {
  9811       int x, i, ascent = 0, descent = 0;
  9812 
  9813 /* Utility macro to reset an iterator with x, ascent, and descent.  */
  9814 #define IT_RESET_X_ASCENT_DESCENT(IT)                   \
  9815   ((IT)->current_x = x, (IT)->max_ascent = ascent,      \
  9816    (IT)->max_descent = descent)
  9817 
  9818       /* Stop if we move beyond TO_CHARPOS (after an image or a
  9819          display string or stretch glyph).  */
  9820       if ((op & MOVE_TO_POS) != 0
  9821           && BUFFERP (it->object)
  9822           && it->method == GET_FROM_BUFFER
  9823           && (((!it->bidi_p
  9824                 /* When the iterator is at base embedding level, we
  9825                    are guaranteed that characters are delivered for
  9826                    display in strictly increasing order of their
  9827                    buffer positions.  */
  9828                 || BIDI_AT_BASE_LEVEL (it->bidi_it))
  9829                && IT_CHARPOS (*it) > to_charpos)
  9830               || (it->bidi_p
  9831                   && (prev_method == GET_FROM_IMAGE
  9832                       || prev_method == GET_FROM_STRETCH
  9833                       || prev_method == GET_FROM_STRING)
  9834                   /* Passed TO_CHARPOS from left to right.  */
  9835                   && ((prev_pos < to_charpos
  9836                        && IT_CHARPOS (*it) >= to_charpos)
  9837                       /* Passed TO_CHARPOS from right to left.  */
  9838                       || (prev_pos > to_charpos
  9839                           && IT_CHARPOS (*it) <= to_charpos)))))
  9840         {
  9841           if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9842             {
  9843               result = MOVE_POS_MATCH_OR_ZV;
  9844               break;
  9845             }
  9846           else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
  9847             /* If wrap_it is valid, the current position might be in a
  9848                word that is wrapped.  So, save the iterator in
  9849                atpos_it and continue to see if wrapping happens.  */
  9850             SAVE_IT (atpos_it, *it, atpos_data);
  9851         }
  9852 
  9853       /* Stop when ZV reached.
  9854          We used to stop here when TO_CHARPOS reached as well, but that is
  9855          too soon if this glyph does not fit on this line.  So we handle it
  9856          explicitly below.  */
  9857       if (!get_next_display_element (it))
  9858         {
  9859           result = MOVE_POS_MATCH_OR_ZV;
  9860           break;
  9861         }
  9862 
  9863       if (it->line_wrap == TRUNCATE)
  9864         {
  9865           /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
  9866              produced something that doesn't consume any screen estate
  9867              in the text area, so we don't want to exit the loop at
  9868              TO_CHARPOS, before we produce the glyph for that buffer
  9869              position.  This happens, e.g., when there's an overlay at
  9870              TO_CHARPOS that draws a fringe bitmap.  */
  9871           if (BUFFER_POS_REACHED_P ()
  9872               && (it->pixel_width > 0
  9873                   || IT_CHARPOS (*it) > to_charpos
  9874                   || it->area != TEXT_AREA))
  9875             {
  9876               result = MOVE_POS_MATCH_OR_ZV;
  9877               break;
  9878             }
  9879         }
  9880       else
  9881         {
  9882           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
  9883             {
  9884               bool next_may_wrap = may_wrap;
  9885               /* Can we wrap after this character?  */
  9886               if (char_can_wrap_after (it))
  9887                 next_may_wrap = true;
  9888               else
  9889                 next_may_wrap = false;
  9890               /* Can we wrap here? */
  9891               if (may_wrap && char_can_wrap_before (it))
  9892                 {
  9893                   /* We have reached a glyph that follows one or more
  9894                      whitespace characters or characters that allow
  9895                      wrapping after them.  If this character allows
  9896                      wrapping before it, save this position as a
  9897                      wrapping point.  */
  9898                   if (atpos_it.sp >= 0)
  9899                     {
  9900                       RESTORE_IT (it, &atpos_it, atpos_data);
  9901                       result = MOVE_POS_MATCH_OR_ZV;
  9902                       goto done;
  9903                     }
  9904                   if (atx_it.sp >= 0)
  9905                     {
  9906                       RESTORE_IT (it, &atx_it, atx_data);
  9907                       result = MOVE_X_REACHED;
  9908                       goto done;
  9909                     }
  9910                   /* Otherwise, we can wrap here.  */
  9911                   SAVE_IT (wrap_it, *it, wrap_data);
  9912                 }
  9913               /* Update may_wrap for the next iteration.  */
  9914               may_wrap = next_may_wrap;
  9915             }
  9916         }
  9917 
  9918       /* Remember the line height for the current line, in case
  9919          the next element doesn't fit on the line.  */
  9920       ascent = it->max_ascent;
  9921       descent = it->max_descent;
  9922 
  9923       /* The call to produce_glyphs will get the metrics of the
  9924          display element IT is loaded with.  Record the x-position
  9925          before this display element, in case it doesn't fit on the
  9926          line.  */
  9927       x = it->current_x;
  9928 
  9929       PRODUCE_GLYPHS (it);
  9930 
  9931       if (it->area != TEXT_AREA)
  9932         {
  9933           prev_method = it->method;
  9934           if (it->method == GET_FROM_BUFFER)
  9935             prev_pos = IT_CHARPOS (*it);
  9936           set_iterator_to_next (it, true);
  9937           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9938             SET_TEXT_POS (this_line_min_pos,
  9939                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9940           if (it->bidi_p
  9941               && (op & MOVE_TO_POS)
  9942               && IT_CHARPOS (*it) > to_charpos
  9943               && IT_CHARPOS (*it) < closest_pos)
  9944             closest_pos = IT_CHARPOS (*it);
  9945           continue;
  9946         }
  9947 
  9948       /* The number of glyphs we get back in IT->nglyphs will normally
  9949          be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
  9950          character on a terminal frame, or (iii) a line end.  For the
  9951          second case, IT->nglyphs - 1 padding glyphs will be present.
  9952          (On X frames, there is only one glyph produced for a
  9953          composite character.)
  9954 
  9955          The behavior implemented below means, for continuation lines,
  9956          that as many spaces of a TAB as fit on the current line are
  9957          displayed there.  For terminal frames, as many glyphs of a
  9958          multi-glyph character are displayed in the current line, too.
  9959          This is what the old redisplay code did, and we keep it that
  9960          way.  Under X, the whole shape of a complex character must
  9961          fit on the line or it will be completely displayed in the
  9962          next line.
  9963 
  9964          Note that both for tabs and padding glyphs, all glyphs have
  9965          the same width.  */
  9966       if (it->nglyphs)
  9967         {
  9968           /* More than one glyph or glyph doesn't fit on line.  All
  9969              glyphs have the same width.  */
  9970           int single_glyph_width = it->pixel_width / it->nglyphs;
  9971           int new_x;
  9972           int x_before_this_char = x;
  9973           int hpos_before_this_char = it->hpos;
  9974 
  9975           for (i = 0; i < it->nglyphs; ++i, x = new_x)
  9976             {
  9977               new_x = x + single_glyph_width;
  9978 
  9979               /* We want to leave anything reaching TO_X to the caller.  */
  9980               if ((op & MOVE_TO_X) && new_x > to_x)
  9981                 {
  9982                   if (BUFFER_POS_REACHED_P ())
  9983                     {
  9984                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9985                         goto buffer_pos_reached;
  9986                       if (atpos_it.sp < 0)
  9987                         {
  9988                           SAVE_IT (atpos_it, *it, atpos_data);
  9989                           IT_RESET_X_ASCENT_DESCENT (&atpos_it);
  9990                         }
  9991                     }
  9992                   else
  9993                     {
  9994                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9995                         {
  9996                           it->current_x = x;
  9997                           result = MOVE_X_REACHED;
  9998                           break;
  9999                         }
 10000                       if (atx_it.sp < 0)
 10001                         {
 10002                           SAVE_IT (atx_it, *it, atx_data);
 10003                           IT_RESET_X_ASCENT_DESCENT (&atx_it);
 10004                         }
 10005                     }
 10006                 }
 10007 
 10008               if (/* Lines are continued.  */
 10009                   it->line_wrap != TRUNCATE
 10010                   && (/* And glyph doesn't fit on the line.  */
 10011                       new_x > it->last_visible_x
 10012                       /* Or it fits exactly and we're on a window
 10013                          system frame.  */
 10014                       || (new_x == it->last_visible_x
 10015                           && FRAME_WINDOW_P (it->f)
 10016                           && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10017                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10018                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 10019                 {
 10020                   bool moved_forward = false;
 10021 
 10022                   if (/* IT->hpos == 0 means the very first glyph
 10023                          doesn't fit on the line, e.g. a wide image.  */
 10024                       it->hpos == 0
 10025                       || (new_x == it->last_visible_x
 10026                           && FRAME_WINDOW_P (it->f)))
 10027                     {
 10028                       ++it->hpos;
 10029                       it->current_x = new_x;
 10030 
 10031                       /* The character's last glyph just barely fits
 10032                          in this row.  */
 10033                       if (i == it->nglyphs - 1)
 10034                         {
 10035                           /* If this is the destination position,
 10036                              return a position *before* it in this row,
 10037                              now that we know it fits in this row.  */
 10038                           if (BUFFER_POS_REACHED_P ())
 10039                             {
 10040                               bool can_wrap = true;
 10041 
 10042                               /* If the previous character says we can
 10043                                  wrap after it, but the current
 10044                                  character says we can't wrap before
 10045                                  it, then we can't wrap here.  */
 10046                               if (it->line_wrap == WORD_WRAP
 10047                                   && wrap_it.sp >= 0
 10048                                   && may_wrap
 10049                                   && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10050                                 {
 10051                                   struct it tem_it;
 10052                                   void *tem_data = NULL;
 10053 
 10054                                   SAVE_IT (tem_it, *it, tem_data);
 10055                                   set_iterator_to_next (it, true);
 10056                                   if (get_next_display_element (it)
 10057                                       && !char_can_wrap_before (it))
 10058                                     can_wrap = false;
 10059                                   RESTORE_IT (it, &tem_it, tem_data);
 10060                                 }
 10061                               if (it->line_wrap != WORD_WRAP
 10062                                   || wrap_it.sp < 0
 10063                                   /* If we've just found whitespace
 10064                                      where we can wrap, effectively
 10065                                      ignore the previous wrap point --
 10066                                      it is no longer relevant, but we
 10067                                      won't have an opportunity to
 10068                                      update it, since we've reached
 10069                                      the edge of this screen line.  */
 10070                                   || (may_wrap && can_wrap
 10071                                       && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10072                                 {
 10073                                   it->hpos = hpos_before_this_char;
 10074                                   it->current_x = x_before_this_char;
 10075                                   result = MOVE_POS_MATCH_OR_ZV;
 10076                                   break;
 10077                                 }
 10078                               if (it->line_wrap == WORD_WRAP
 10079                                   && atpos_it.sp < 0)
 10080                                 {
 10081                                   SAVE_IT (atpos_it, *it, atpos_data);
 10082                                   atpos_it.current_x = x_before_this_char;
 10083                                   atpos_it.hpos = hpos_before_this_char;
 10084                                 }
 10085                             }
 10086 
 10087                           prev_method = it->method;
 10088                           if (it->method == GET_FROM_BUFFER)
 10089                             prev_pos = IT_CHARPOS (*it);
 10090                           set_iterator_to_next (it, true);
 10091                           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10092                             SET_TEXT_POS (this_line_min_pos,
 10093                                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10094                           /* On graphical terminals, newlines may
 10095                              "overflow" into the fringe if
 10096                              overflow-newline-into-fringe is non-nil.
 10097                              On text terminals, and on graphical
 10098                              terminals with no right margin, newlines
 10099                              may overflow into the last glyph on the
 10100                              display line.*/
 10101                           if (!FRAME_WINDOW_P (it->f)
 10102                               || ((it->bidi_p
 10103                                    && it->bidi_it.paragraph_dir == R2L)
 10104                                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10105                                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10106                               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10107                             {
 10108                               if (!get_next_display_element (it))
 10109                                 {
 10110                                   result = MOVE_POS_MATCH_OR_ZV;
 10111                                   break;
 10112                                 }
 10113                               moved_forward = true;
 10114                               if (BUFFER_POS_REACHED_P ())
 10115                                 {
 10116                                   if (ITERATOR_AT_END_OF_LINE_P (it))
 10117                                     result = MOVE_POS_MATCH_OR_ZV;
 10118                                   else
 10119                                     result = MOVE_LINE_CONTINUED;
 10120                                   break;
 10121                                 }
 10122                               if (ITERATOR_AT_END_OF_LINE_P (it)
 10123                                   && (it->line_wrap != WORD_WRAP
 10124                                       || wrap_it.sp < 0
 10125                                       || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10126                                 {
 10127                                   result = MOVE_NEWLINE_OR_CR;
 10128                                   break;
 10129                                 }
 10130                             }
 10131                         }
 10132                     }
 10133                   else
 10134                     IT_RESET_X_ASCENT_DESCENT (it);
 10135 
 10136                   /* If the screen line ends with whitespace (or
 10137                      wrap-able character), and we are under word-wrap,
 10138                      don't use wrap_it: it is no longer relevant, but
 10139                      we won't have an opportunity to update it, since
 10140                      we are done with this screen line.  */
 10141                   if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 10142                       /* If the character after the one which set the
 10143                          may_wrap flag says we can't wrap before it,
 10144                          we can't wrap here.  Therefore, wrap_it
 10145                          (previously found wrap-point) _is_ relevant
 10146                          in that case.  */
 10147                       && (!moved_forward || char_can_wrap_before (it)))
 10148                     {
 10149                       /* If we've found TO_X, go back there, as we now
 10150                          know the last word fits on this screen line.  */
 10151                       if ((op & MOVE_TO_X) && new_x == it->last_visible_x
 10152                           && atx_it.sp >= 0)
 10153                         {
 10154                           RESTORE_IT (it, &atx_it, atx_data);
 10155                           atpos_it.sp = -1;
 10156                           atx_it.sp = -1;
 10157                           result = MOVE_X_REACHED;
 10158                           break;
 10159                         }
 10160                     }
 10161                   else if (wrap_it.sp >= 0)
 10162                     {
 10163                       RESTORE_IT (it, &wrap_it, wrap_data);
 10164                       atpos_it.sp = -1;
 10165                       atx_it.sp = -1;
 10166                     }
 10167 
 10168                   move_trace ("move_it_in: continued at %td\n",
 10169                               IT_CHARPOS (*it));
 10170                   result = MOVE_LINE_CONTINUED;
 10171                   break;
 10172                 }
 10173 
 10174               if (BUFFER_POS_REACHED_P ())
 10175                 {
 10176                   if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10177                     goto buffer_pos_reached;
 10178                   if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
 10179                     {
 10180                       SAVE_IT (atpos_it, *it, atpos_data);
 10181                       IT_RESET_X_ASCENT_DESCENT (&atpos_it);
 10182                     }
 10183                 }
 10184 
 10185               if (new_x > it->first_visible_x)
 10186                 {
 10187                   /* If we have reached the visible portion of the
 10188                      screen line, produce the line number if needed.  */
 10189                   if (line_number_pending)
 10190                     {
 10191                       line_number_pending = false;
 10192                       it->current_x = it->first_visible_x;
 10193                       maybe_produce_line_number (it);
 10194                       it->current_x += new_x - it->first_visible_x;
 10195                     }
 10196                   /* Glyph is visible.  Increment number of glyphs that
 10197                      would be displayed.  */
 10198                   ++it->hpos;
 10199                 }
 10200             }
 10201 
 10202           if (result != MOVE_UNDEFINED)
 10203             break;
 10204         }
 10205       else if (BUFFER_POS_REACHED_P ())
 10206         {
 10207         buffer_pos_reached:
 10208           IT_RESET_X_ASCENT_DESCENT (it);
 10209           result = MOVE_POS_MATCH_OR_ZV;
 10210           break;
 10211         }
 10212       else if ((op & MOVE_TO_X) && it->current_x >= to_x)
 10213         {
 10214           /* Stop when TO_X specified and reached.  This check is
 10215              necessary here because of lines consisting of a line end,
 10216              only.  The line end will not produce any glyphs and we
 10217              would never get MOVE_X_REACHED.  */
 10218           eassert (it->nglyphs == 0);
 10219           result = MOVE_X_REACHED;
 10220           break;
 10221         }
 10222 
 10223       /* Is this a line end?  If yes, we're done.  */
 10224       if (ITERATOR_AT_END_OF_LINE_P (it))
 10225         {
 10226           /* If we are past TO_CHARPOS, but never saw any character
 10227              positions smaller than TO_CHARPOS, return
 10228              MOVE_POS_MATCH_OR_ZV, like the unidirectional display
 10229              did.  */
 10230           if (it->bidi_p && (op & MOVE_TO_POS) != 0)
 10231             {
 10232               if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
 10233                 {
 10234                   if (closest_pos < ZV)
 10235                     {
 10236                       RESTORE_IT (it, &ppos_it, ppos_data);
 10237                       /* Don't recurse if closest_pos is equal to
 10238                          to_charpos, since we have just tried that.  */
 10239                       if (closest_pos != to_charpos)
 10240                         move_it_in_display_line_to (it, closest_pos, -1,
 10241                                                     MOVE_TO_POS);
 10242                       result = MOVE_POS_MATCH_OR_ZV;
 10243                     }
 10244                   else
 10245                     goto buffer_pos_reached;
 10246                 }
 10247               else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
 10248                        && IT_CHARPOS (*it) > to_charpos)
 10249                 goto buffer_pos_reached;
 10250               else
 10251                 result = MOVE_NEWLINE_OR_CR;
 10252             }
 10253           else
 10254             result = MOVE_NEWLINE_OR_CR;
 10255           /* If lines are truncated, and the line we moved across is
 10256              completely hscrolled out of view, reset the line metrics
 10257              to those of the newline we've just processed, so that
 10258              glyphs not on display don't affect the line's height.  */
 10259           if (it->line_wrap == TRUNCATE
 10260               && it->current_x <= it->first_visible_x
 10261               && result == MOVE_NEWLINE_OR_CR
 10262               && it->char_to_display == '\n')
 10263             {
 10264               it->max_ascent = it->ascent;
 10265               it->max_descent = it->descent;
 10266             }
 10267           /* If we've processed the newline, make sure this flag is
 10268              reset, as it must only be set when the newline itself is
 10269              processed.  */
 10270           if (result == MOVE_NEWLINE_OR_CR)
 10271             it->constrain_row_ascent_descent_p = false;
 10272           break;
 10273         }
 10274 
 10275       prev_method = it->method;
 10276       if (it->method == GET_FROM_BUFFER)
 10277         prev_pos = IT_CHARPOS (*it);
 10278 
 10279       /* Detect overly-wide wrap-prefixes made of (space ...) display
 10280          properties.  When such a wrap prefix reaches past the right
 10281          margin of the window, we need to avoid the call to
 10282          set_iterator_to_next below, so that it->line_wrap is left at
 10283          its TRUNCATE value wisely set by handle_line_prefix.
 10284          Otherwise, set_iterator_to_next will pop the iterator stack,
 10285          restore it->line_wrap, and we might miss the opportunity to
 10286          exit the loop and return.  */
 10287       bool overwide_wrap_prefix =
 10288         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 10289         && it->sp > 0 && it->method == GET_FROM_STRETCH
 10290         && it->current_x >= it->last_visible_x
 10291         && it->continuation_lines_width > 0
 10292         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 10293       /* The current display element has been consumed.  Advance
 10294          to the next.  */
 10295       if (!overwide_wrap_prefix)
 10296         set_iterator_to_next (it, true);
 10297       if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10298         SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10299       if (IT_CHARPOS (*it) < to_charpos)
 10300         saw_smaller_pos = true;
 10301       if (it->bidi_p
 10302           && (op & MOVE_TO_POS)
 10303           && IT_CHARPOS (*it) >= to_charpos
 10304           && IT_CHARPOS (*it) < closest_pos)
 10305         closest_pos = IT_CHARPOS (*it);
 10306 
 10307       /* Stop if lines are truncated and IT's current x-position is
 10308          past the right edge of the window now.  */
 10309       if (it->line_wrap == TRUNCATE
 10310           && it->current_x >= it->last_visible_x)
 10311         {
 10312           if (!FRAME_WINDOW_P (it->f)
 10313               || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10314                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10315                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10316               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10317             {
 10318               bool at_eob_p = false;
 10319 
 10320               if ((at_eob_p = !get_next_display_element (it))
 10321                   || BUFFER_POS_REACHED_P ()
 10322                   /* If we are past TO_CHARPOS, but never saw any
 10323                      character positions smaller than TO_CHARPOS,
 10324                      return MOVE_POS_MATCH_OR_ZV, like the
 10325                      unidirectional display did.  */
 10326                   || (it->bidi_p && (op & MOVE_TO_POS) != 0
 10327                       && !saw_smaller_pos
 10328                       && IT_CHARPOS (*it) > to_charpos))
 10329                 {
 10330                   if (it->bidi_p
 10331                       && !BUFFER_POS_REACHED_P ()
 10332                       && !at_eob_p && closest_pos < ZV)
 10333                     {
 10334                       RESTORE_IT (it, &ppos_it, ppos_data);
 10335                       if (closest_pos != to_charpos)
 10336                         move_it_in_display_line_to (it, closest_pos, -1,
 10337                                                     MOVE_TO_POS);
 10338                     }
 10339                   result = MOVE_POS_MATCH_OR_ZV;
 10340                   break;
 10341                 }
 10342               if (ITERATOR_AT_END_OF_LINE_P (it))
 10343                 {
 10344                   result = MOVE_NEWLINE_OR_CR;
 10345                   break;
 10346                 }
 10347             }
 10348           else if (it->bidi_p && (op & MOVE_TO_POS) != 0
 10349                    && !saw_smaller_pos
 10350                    && IT_CHARPOS (*it) > to_charpos)
 10351             {
 10352               if (closest_pos < ZV)
 10353                 {
 10354                   RESTORE_IT (it, &ppos_it, ppos_data);
 10355                   if (closest_pos != to_charpos)
 10356                     move_it_in_display_line_to (it, closest_pos, -1,
 10357                                                 MOVE_TO_POS);
 10358                 }
 10359               result = MOVE_POS_MATCH_OR_ZV;
 10360               break;
 10361             }
 10362           result = MOVE_LINE_TRUNCATED;
 10363           break;
 10364         }
 10365 #undef IT_RESET_X_ASCENT_DESCENT
 10366     }
 10367 
 10368 #undef BUFFER_POS_REACHED_P
 10369 
 10370   /* If we scanned beyond TO_POS, restore the saved iterator either to
 10371      the wrap point (if found), or to atpos/atx location.  We decide which
 10372      data to use to restore the saved iterator state by their X coordinates,
 10373      since buffer positions might increase non-monotonically with screen
 10374      coordinates due to bidi reordering.  */
 10375   if (result == MOVE_LINE_CONTINUED
 10376       && it->line_wrap == WORD_WRAP
 10377       && wrap_it.sp >= 0
 10378       && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
 10379           || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
 10380     RESTORE_IT (it, &wrap_it, wrap_data);
 10381   else if (atpos_it.sp >= 0)
 10382     RESTORE_IT (it, &atpos_it, atpos_data);
 10383   else if (atx_it.sp >= 0)
 10384     RESTORE_IT (it, &atx_it, atx_data);
 10385 
 10386  done:
 10387 
 10388   if (atpos_data)
 10389     bidi_unshelve_cache (atpos_data, true);
 10390   if (atx_data)
 10391     bidi_unshelve_cache (atx_data, true);
 10392   if (wrap_data)
 10393     bidi_unshelve_cache (wrap_data, true);
 10394   if (ppos_data)
 10395     bidi_unshelve_cache (ppos_data, true);
 10396 
 10397   /* Restore the iterator settings altered at the beginning of this
 10398      function.  */
 10399   it->glyph_row = saved_glyph_row;
 10400   return result;
 10401 }
 10402 
 10403 /* For external use.  */
 10404 void
 10405 move_it_in_display_line (struct it *it,
 10406                          ptrdiff_t to_charpos, int to_x,
 10407                          enum move_operation_enum op)
 10408 {
 10409   if (it->line_wrap == WORD_WRAP
 10410       && (op & MOVE_TO_X))
 10411     {
 10412       struct it save_it;
 10413       void *save_data = NULL;
 10414       int skip;
 10415 
 10416       SAVE_IT (save_it, *it, save_data);
 10417       skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10418       /* When word-wrap is on, TO_X may lie past the end
 10419          of a wrapped line.  Then it->current is the
 10420          character on the next line, so backtrack to the
 10421          space before the wrap point.  */
 10422       if (skip == MOVE_LINE_CONTINUED)
 10423         {
 10424           int prev_x = max (it->current_x - 1, 0);
 10425           RESTORE_IT (it, &save_it, save_data);
 10426           move_it_in_display_line_to
 10427             (it, -1, prev_x, MOVE_TO_X);
 10428         }
 10429       else
 10430         bidi_unshelve_cache (save_data, true);
 10431     }
 10432   else
 10433     move_it_in_display_line_to (it, to_charpos, to_x, op);
 10434 }
 10435 
 10436 
 10437 /* Move IT forward until it satisfies one or more of the criteria in
 10438    TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
 10439 
 10440    OP is a bit-mask that specifies where to stop, and in particular,
 10441    which of those four position arguments makes a difference.  See the
 10442    description of enum move_operation_enum.
 10443 
 10444    If TO_CHARPOS is in invisible text, e.g. a truncated part of a
 10445    screen line, this function will set IT to the next position that is
 10446    displayed to the right of TO_CHARPOS on the screen.
 10447 
 10448    Return the maximum pixel length of any line scanned but never more
 10449    than it.last_visible_x.  */
 10450 
 10451 int
 10452 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
 10453 {
 10454   enum move_it_result skip, skip2 = MOVE_X_REACHED;
 10455   int line_height, line_start_x = 0, reached = 0;
 10456   int max_current_x = 0;
 10457   void *backup_data = NULL;
 10458   ptrdiff_t orig_charpos = -1;
 10459   enum it_method orig_method = NUM_IT_METHODS;
 10460 
 10461   for (;;)
 10462     {
 10463       orig_charpos = IT_CHARPOS (*it);
 10464       orig_method = it->method;
 10465       if (op & MOVE_TO_VPOS)
 10466         {
 10467           /* If no TO_CHARPOS and no TO_X specified, stop at the
 10468              start of the line TO_VPOS.  */
 10469           if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
 10470             {
 10471               if (it->vpos == to_vpos)
 10472                 {
 10473                   reached = 1;
 10474                   break;
 10475                 }
 10476               else
 10477                 skip = move_it_in_display_line_to (it, -1, -1, 0);
 10478             }
 10479           else
 10480             {
 10481               /* TO_VPOS >= 0 means stop at TO_X in the line at
 10482                  TO_VPOS, or at TO_POS, whichever comes first.  */
 10483               if (it->vpos == to_vpos)
 10484                 {
 10485                   reached = 2;
 10486                   break;
 10487                 }
 10488 
 10489               skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10490 
 10491               if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
 10492                 {
 10493                   reached = 3;
 10494                   break;
 10495                 }
 10496               else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
 10497                 {
 10498                   /* We have reached TO_X but not in the line we want.  */
 10499                   skip = move_it_in_display_line_to (it, to_charpos,
 10500                                                      -1, MOVE_TO_POS);
 10501                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10502                     {
 10503                       reached = 4;
 10504                       break;
 10505                     }
 10506                 }
 10507             }
 10508         }
 10509       else if (op & MOVE_TO_Y)
 10510         {
 10511           struct it it_backup;
 10512 
 10513           if (it->line_wrap == WORD_WRAP)
 10514             SAVE_IT (it_backup, *it, backup_data);
 10515 
 10516           /* TO_Y specified means stop at TO_X in the line containing
 10517              TO_Y---or at TO_CHARPOS if this is reached first.  The
 10518              problem is that we can't really tell whether the line
 10519              contains TO_Y before we have completely scanned it, and
 10520              this may skip past TO_X.  What we do is to first scan to
 10521              TO_X.
 10522 
 10523              If TO_X is not specified, use a TO_X of zero.  The reason
 10524              is to make the outcome of this function more predictable.
 10525              If we didn't use TO_X == 0, we would stop at the end of
 10526              the line which is probably not what a caller would expect
 10527              to happen.  */
 10528           skip = move_it_in_display_line_to
 10529             (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
 10530              (MOVE_TO_X | (op & MOVE_TO_POS)));
 10531 
 10532           /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
 10533           if (skip == MOVE_POS_MATCH_OR_ZV)
 10534             reached = 5;
 10535           else if (skip == MOVE_X_REACHED)
 10536             {
 10537               /* If TO_X was reached, we want to know whether TO_Y is
 10538                  in the line.  We know this is the case if the already
 10539                  scanned glyphs make the line tall enough.  Otherwise,
 10540                  we must check by scanning the rest of the line.  */
 10541               line_height = it->max_ascent + it->max_descent;
 10542               if (to_y >= it->current_y
 10543                   && to_y < it->current_y + line_height)
 10544                 {
 10545                   reached = 6;
 10546                   break;
 10547                 }
 10548               SAVE_IT (it_backup, *it, backup_data);
 10549               move_trace ("move_it: from %td\n", IT_CHARPOS (*it));
 10550               skip2 = move_it_in_display_line_to (it, to_charpos, -1,
 10551                                                   op & MOVE_TO_POS);
 10552               move_trace ("move_it: to %td\n", IT_CHARPOS (*it));
 10553               line_height = it->max_ascent + it->max_descent;
 10554               move_trace ("move_it: line_height = %d\n", line_height);
 10555 
 10556               if (to_y >= it->current_y
 10557                   && to_y < it->current_y + line_height)
 10558                 {
 10559                   /* If TO_Y is in this line and TO_X was reached
 10560                      above, we scanned too far.  We have to restore
 10561                      IT's settings to the ones before skipping.  But
 10562                      keep the more accurate values of max_ascent and
 10563                      max_descent we've found while skipping the rest
 10564                      of the line, for the sake of callers, such as
 10565                      pos_visible_p, that need to know the line
 10566                      height.  */
 10567                   int max_ascent = it->max_ascent;
 10568                   int max_descent = it->max_descent;
 10569 
 10570                   RESTORE_IT (it, &it_backup, backup_data);
 10571                   it->max_ascent = max_ascent;
 10572                   it->max_descent = max_descent;
 10573                   reached = 6;
 10574                 }
 10575               else
 10576                 {
 10577                   skip = skip2;
 10578                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10579                     {
 10580                       reached = 7;
 10581                       /* If the last move_it_in_display_line_to call
 10582                          took us away from TO_CHARPOS, back up to the
 10583                          previous position, as it is a better
 10584                          approximation of TO_CHARPOS.  (Note that we
 10585                          could have both positions after TO_CHARPOS or
 10586                          both positions before it, due to bidi
 10587                          reordering.)  */
 10588                       if (to_charpos > 0
 10589                           && IT_CHARPOS (*it) != to_charpos
 10590                           && ((IT_CHARPOS (it_backup) > to_charpos)
 10591                               == (IT_CHARPOS (*it) > to_charpos)))
 10592                         {
 10593                           int max_ascent = it->max_ascent;
 10594                           int max_descent = it->max_descent;
 10595 
 10596                           RESTORE_IT (it, &it_backup, backup_data);
 10597                           it->max_ascent = max_ascent;
 10598                           it->max_descent = max_descent;
 10599                         }
 10600                     }
 10601                 }
 10602             }
 10603           else
 10604             {
 10605               /* Check whether TO_Y is in this line.  */
 10606               line_height = it->max_ascent + it->max_descent;
 10607               move_trace ("move_it: line_height = %d\n", line_height);
 10608 
 10609               if (to_y >= it->current_y
 10610                   && to_y < it->current_y + line_height)
 10611                 {
 10612                   if (to_y > it->current_y)
 10613                     max_current_x = max (it->current_x, max_current_x);
 10614 
 10615                   /* When word-wrap is on, TO_X may lie past the end
 10616                      of a wrapped line.  Then it->current is the
 10617                      character on the next line, so backtrack to the
 10618                      space before the wrap point.  */
 10619                   if (skip == MOVE_LINE_CONTINUED
 10620                       && it->line_wrap == WORD_WRAP)
 10621                     {
 10622                       int prev_x = max (it->current_x - 1, 0);
 10623                       RESTORE_IT (it, &it_backup, backup_data);
 10624                       skip = move_it_in_display_line_to
 10625                         (it, -1, prev_x, MOVE_TO_X);
 10626                     }
 10627 
 10628                   reached = 6;
 10629                 }
 10630             }
 10631 
 10632           if (reached)
 10633             {
 10634               max_current_x = max (it->current_x, max_current_x);
 10635               break;
 10636             }
 10637         }
 10638       else if (BUFFERP (it->object)
 10639                && (it->method == GET_FROM_BUFFER
 10640                    || it->method == GET_FROM_STRETCH)
 10641                && IT_CHARPOS (*it) >= to_charpos
 10642                /* Under bidi iteration, a call to set_iterator_to_next
 10643                   can scan far beyond to_charpos if the initial
 10644                   portion of the next line needs to be reordered.  In
 10645                   that case, give move_it_in_display_line_to another
 10646                   chance below.  */
 10647                && !(it->bidi_p
 10648                     && it->bidi_it.scan_dir == -1))
 10649         skip = MOVE_POS_MATCH_OR_ZV;
 10650       else
 10651         skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
 10652 
 10653       switch (skip)
 10654         {
 10655         case MOVE_POS_MATCH_OR_ZV:
 10656           max_current_x = max (it->current_x, max_current_x);
 10657           reached = 8;
 10658           goto out;
 10659 
 10660         case MOVE_NEWLINE_OR_CR:
 10661           max_current_x = max (it->current_x, max_current_x);
 10662           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10663             it->override_ascent = -1;
 10664           set_iterator_to_next (it, true);
 10665           it->continuation_lines_width = 0;
 10666           break;
 10667 
 10668         case MOVE_LINE_TRUNCATED:
 10669           max_current_x = it->last_visible_x;
 10670           it->continuation_lines_width = 0;
 10671           reseat_at_next_visible_line_start (it, false);
 10672           if ((op & MOVE_TO_POS) != 0
 10673               && (IT_CHARPOS (*it) > to_charpos
 10674                   || (IT_CHARPOS (*it) == to_charpos
 10675                       /* Consider TO_CHARPOS as REACHED if we are at
 10676                          EOB that ends in something other than a newline.  */
 10677                       && to_charpos == ZV
 10678                       && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n')
 10679                       /* But if we have a display or an overlay string
 10680                          at EOB, keep going until we exhaust all the
 10681                          characters of the string(s).  */
 10682                       && (it->sp == 0
 10683                           || (STRINGP (it->string)
 10684                               && (it->current.overlay_string_index < 0
 10685                                   || (it->current.overlay_string_index >= 0
 10686                                       && it->current.overlay_string_index
 10687                                          >= it->n_overlay_strings - 1))
 10688                               && IT_STRING_CHARPOS (*it) >= it->end_charpos)))))
 10689             {
 10690               reached = 9;
 10691               goto out;
 10692             }
 10693           break;
 10694 
 10695         case MOVE_LINE_CONTINUED:
 10696           max_current_x = it->last_visible_x;
 10697           /* For continued lines ending in a tab, some of the glyphs
 10698              associated with the tab are displayed on the current
 10699              line.  Since it->current_x does not include these glyphs,
 10700              we use it->last_visible_x instead.  */
 10701           if (it->c == '\t')
 10702             {
 10703               it->continuation_lines_width += it->last_visible_x;
 10704               /* When moving by vpos, ensure that the iterator really
 10705                  advances to the next line (bug#847, bug#969).  Fixme:
 10706                  do we need to do this in other circumstances?  */
 10707               if (it->current_x != it->last_visible_x
 10708                   && (op & MOVE_TO_VPOS)
 10709                   && !(op & (MOVE_TO_X | MOVE_TO_POS)))
 10710                 {
 10711                   line_start_x = it->current_x + it->pixel_width
 10712                     - it->last_visible_x;
 10713                   if (FRAME_WINDOW_P (it->f))
 10714                     {
 10715                       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 10716                       struct font *face_font = face->font;
 10717 
 10718                       /* When display_line produces a continued line
 10719                          that ends in a TAB, it skips a tab stop that
 10720                          is closer than the font's space character
 10721                          width (see gui_produce_glyphs where it produces
 10722                          the stretch glyph which represents a TAB).
 10723                          We need to reproduce the same logic here.  */
 10724                       eassert (face_font);
 10725                       if (face_font)
 10726                         {
 10727                           if (line_start_x < face_font->space_width)
 10728                             line_start_x
 10729                               += it->tab_width * face_font->space_width;
 10730                         }
 10731                     }
 10732                   set_iterator_to_next (it, false);
 10733                 }
 10734             }
 10735           else
 10736             {
 10737               /* Make sure we do advance, otherwise we might infloop.
 10738                  This could happen when the first display element is
 10739                  wider than the window, or if we have a wrap-prefix
 10740                  that doesn't leave enough space after it to display
 10741                  even a single character.  We only do this for moving
 10742                  through buffer text, as with display/overlay strings
 10743                  we'd need to also compare it->object's, and this is
 10744                  unlikely to happen in that case anyway.  */
 10745               if (IT_CHARPOS (*it) == orig_charpos
 10746                   && it->method == orig_method
 10747                   && orig_method == GET_FROM_BUFFER)
 10748                 set_iterator_to_next (it, false);
 10749               it->continuation_lines_width += it->current_x;
 10750             }
 10751           break;
 10752 
 10753         default:
 10754           emacs_abort ();
 10755         }
 10756 
 10757       /* Reset/increment for the next run.  */
 10758       it->current_x = line_start_x;
 10759       line_start_x = 0;
 10760       it->hpos = 0;
 10761       it->line_number_produced_p = false;
 10762       it->current_y += it->max_ascent + it->max_descent;
 10763       ++it->vpos;
 10764       last_height = it->max_ascent + it->max_descent;
 10765       it->max_ascent = it->max_descent = 0;
 10766     }
 10767 
 10768  out:
 10769 
 10770   /* On text terminals, we may stop at the end of a line in the middle
 10771      of a multi-character glyph.  If the glyph itself is continued,
 10772      i.e. it is actually displayed on the next line, don't treat this
 10773      stopping point as valid; move to the next line instead (unless
 10774      that brings us offscreen).  */
 10775   if (!FRAME_WINDOW_P (it->f)
 10776       && op & MOVE_TO_POS
 10777       && IT_CHARPOS (*it) == to_charpos
 10778       && it->what == IT_CHARACTER
 10779       && it->nglyphs > 1
 10780       && it->line_wrap == WINDOW_WRAP
 10781       && it->current_x == it->last_visible_x - 1
 10782       && it->c != '\n'
 10783       && it->c != '\t'
 10784       && it->w->window_end_valid
 10785       && it->vpos < it->w->window_end_vpos)
 10786     {
 10787       it->continuation_lines_width += it->current_x;
 10788       it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
 10789       it->current_y += it->max_ascent + it->max_descent;
 10790       ++it->vpos;
 10791       last_height = it->max_ascent + it->max_descent;
 10792     }
 10793 
 10794   if (backup_data)
 10795     bidi_unshelve_cache (backup_data, true);
 10796 
 10797   move_trace ("move_it_to: reached %d\n", reached);
 10798 
 10799   return max_current_x;
 10800 }
 10801 
 10802 
 10803 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
 10804 
 10805    If DY > 0, move IT backward that many pixels.
 10806    DY = 0 means move IT backward to the preceding line start or to BEGV.
 10807    This function may move over less or more than DY pixels if
 10808    IT->current_y - DY ends up in the middle of a line; in this case
 10809    IT->current_y will be set to the top of the line either before or
 10810    after the exact pixel coordinate.  */
 10811 
 10812 void
 10813 move_it_vertically_backward (struct it *it, int dy)
 10814 {
 10815   int nlines, h;
 10816   struct it it2, it3;
 10817   void *it2data = NULL, *it3data = NULL;
 10818   ptrdiff_t start_pos;
 10819   int nchars_per_row
 10820     = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 10821   ptrdiff_t pos_limit;
 10822 
 10823  move_further_back:
 10824   eassert (dy >= 0);
 10825 
 10826   start_pos = IT_CHARPOS (*it);
 10827 
 10828   /* Estimate how many newlines we must move back.  */
 10829   nlines = max (1, dy / default_line_pixel_height (it->w));
 10830   if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 10831     pos_limit = BEGV;
 10832   else
 10833     pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
 10834 
 10835   /* Set the iterator's position that many lines back.  But don't go
 10836      back more than NLINES full screen lines -- this wins a day with
 10837      buffers which have very long lines.  */
 10838   while (nlines-- && IT_CHARPOS (*it) > pos_limit)
 10839     back_to_previous_visible_line_start (it);
 10840 
 10841   /* Reseat the iterator here.  When moving backward, we don't want
 10842      reseat to skip forward over invisible text, set up the iterator
 10843      to deliver from overlay strings at the new position etc.  So,
 10844      use reseat_1 here.  */
 10845   reseat_1 (it, it->current.pos, true);
 10846 
 10847   /* We are now surely at a line start.  */
 10848   it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
 10849                                    reordering is in effect.  */
 10850   it->continuation_lines_width = 0;
 10851 
 10852   /* Move forward and see what y-distance we moved.  First move to the
 10853      start of the next line so that we get its height.  We need this
 10854      height to be able to tell whether we reached the specified
 10855      y-distance.  */
 10856   SAVE_IT (it2, *it, it2data);
 10857   it2.max_ascent = it2.max_descent = 0;
 10858   do
 10859     {
 10860       move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
 10861                   MOVE_TO_POS | MOVE_TO_VPOS);
 10862     }
 10863   while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
 10864            /* If we are in a display string which starts at START_POS,
 10865               and that display string includes a newline, and we are
 10866               right after that newline (i.e. at the beginning of a
 10867               display line), exit the loop, because otherwise we will
 10868               infloop, since move_it_to will see that it is already at
 10869               START_POS and will not move.  */
 10870            || (it2.method == GET_FROM_STRING
 10871                && IT_CHARPOS (it2) == start_pos
 10872                && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
 10873   eassert (IT_CHARPOS (*it) >= BEGV);
 10874   SAVE_IT (it3, it2, it3data);
 10875 
 10876   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
 10877   eassert (IT_CHARPOS (*it) >= BEGV);
 10878   /* H is the actual vertical distance from the position in *IT
 10879      and the starting position.  */
 10880   h = it2.current_y - it->current_y;
 10881   /* NLINES is the distance in number of lines.  */
 10882   nlines = it2.vpos - it->vpos;
 10883 
 10884   /* Correct IT's y and vpos position
 10885      so that they are relative to the starting point.  */
 10886   it->vpos -= nlines;
 10887   it->current_y -= h;
 10888 
 10889   if (dy == 0)
 10890     {
 10891       /* DY == 0 means move to the start of the screen line.  The
 10892          value of nlines is > 0 if continuation lines were involved,
 10893          or if the original IT position was at start of a line.  */
 10894       RESTORE_IT (it, it, it2data);
 10895       if (nlines > 0)
 10896         move_it_by_lines (it, nlines);
 10897       /* The above code moves us to some position NLINES down,
 10898          usually to its first glyph (leftmost in an L2R line), but
 10899          that's not necessarily the start of the line, under bidi
 10900          reordering.  We want to get to the character position
 10901          that is immediately after the newline of the previous
 10902          line.  */
 10903       if (it->bidi_p
 10904           && !it->continuation_lines_width
 10905           && !STRINGP (it->string)
 10906           && IT_CHARPOS (*it) > BEGV
 10907           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10908         {
 10909           ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
 10910 
 10911           dec_both (&cp, &bp);
 10912           SET_WITH_NARROWED_BEGV (it, cp,
 10913                                   find_newline_no_quit (cp, bp, -1, NULL),
 10914                                   get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
 10915           move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
 10916         }
 10917       bidi_unshelve_cache (it3data, true);
 10918     }
 10919   else
 10920     {
 10921       /* The y-position we try to reach, relative to *IT.
 10922          Note that H has been subtracted in front of the if-statement.  */
 10923       int target_y = it->current_y + h - dy;
 10924       int y0 = it3.current_y;
 10925       int y1;
 10926       int line_height;
 10927 
 10928       RESTORE_IT (&it3, &it3, it3data);
 10929       y1 = line_bottom_y (&it3);
 10930       line_height = y1 - y0;
 10931       RESTORE_IT (it, it, it2data);
 10932       /* If we did not reach target_y, try to move further backward if
 10933          we can.  If we moved too far backward, try to move forward.  */
 10934       if (target_y < it->current_y
 10935           /* This is heuristic.  In a window that's 3 lines high, with
 10936              a line height of 13 pixels each, recentering with point
 10937              on the bottom line will try to move -39/2 = 19 pixels
 10938              backward.  Try to avoid moving into the first line.  */
 10939           && (it->current_y - target_y
 10940               > min (window_box_height (it->w), line_height * 2 / 3))
 10941           && IT_CHARPOS (*it) > BEGV)
 10942         {
 10943           move_trace ("  not far enough -> move_vert %d\n",
 10944                       target_y - it->current_y);
 10945           dy = it->current_y - target_y;
 10946           goto move_further_back;
 10947         }
 10948       else if (target_y >= it->current_y + line_height
 10949                && IT_CHARPOS (*it) < ZV)
 10950         {
 10951           /* Should move forward by at least one line, maybe more.
 10952 
 10953              Note: Calling move_it_by_lines can be expensive on
 10954              terminal frames, where compute_motion is used (via
 10955              vmotion) to do the job, when there are very long lines
 10956              and truncate-lines is nil.  That's the reason for
 10957              treating terminal frames specially here.  */
 10958 
 10959           if (!FRAME_WINDOW_P (it->f))
 10960             move_it_vertically (it, target_y - it->current_y);
 10961           else
 10962             {
 10963               struct text_pos last_pos;
 10964               int last_y, last_vpos;
 10965               do
 10966                 {
 10967                   last_pos = it->current.pos;
 10968                   last_y = it->current_y;
 10969                   last_vpos = it->vpos;
 10970                   move_it_by_lines (it, 1);
 10971                 }
 10972               while (target_y > it->current_y && IT_CHARPOS (*it) < ZV);
 10973               if (it->current_y > target_y)
 10974                 {
 10975                   reseat (it, last_pos, true);
 10976                   it->current_y = last_y;
 10977                   it->vpos = last_vpos;
 10978                 }
 10979             }
 10980         }
 10981     }
 10982 }
 10983 
 10984 
 10985 /* Move IT by a specified amount of pixel lines DY.  DY negative means
 10986    move backwards.  DY = 0 means move to start of screen line.  At the
 10987    end, IT will be on the start of a screen line.  */
 10988 
 10989 void
 10990 move_it_vertically (struct it *it, int dy)
 10991 {
 10992   if (dy <= 0)
 10993     move_it_vertically_backward (it, -dy);
 10994   else
 10995     {
 10996       move_trace ("move_it_v: from %td, %d\n", IT_CHARPOS (*it), dy);
 10997       move_it_to (it, ZV, -1, it->current_y + dy, -1,
 10998                   MOVE_TO_POS | MOVE_TO_Y);
 10999       move_trace ("move_it_v: to %td\n", IT_CHARPOS (*it));
 11000 
 11001       /* If buffer ends in ZV without a newline, move to the start of
 11002          the line to satisfy the post-condition.  */
 11003       if (IT_CHARPOS (*it) == ZV
 11004           && ZV > BEGV
 11005           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 11006         move_it_by_lines (it, 0);
 11007     }
 11008 }
 11009 
 11010 
 11011 /* Move iterator IT past the end of the text line it is in.  */
 11012 
 11013 void
 11014 move_it_past_eol (struct it *it)
 11015 {
 11016   enum move_it_result rc;
 11017 
 11018   rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
 11019   if (rc == MOVE_NEWLINE_OR_CR)
 11020     set_iterator_to_next (it, false);
 11021 }
 11022 
 11023 
 11024 /* Move IT by a specified number DVPOS of screen lines down.  DVPOS
 11025    negative means move up.  DVPOS == 0 means move to the start of the
 11026    screen line.
 11027 
 11028    Optimization idea: If we would know that IT->f doesn't use
 11029    a face with proportional font, we could be faster for
 11030    truncate-lines nil.  */
 11031 
 11032 void
 11033 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
 11034 {
 11035 
 11036   /* The commented-out optimization uses vmotion on terminals.  This
 11037      gives bad results, because elements like it->what, on which
 11038      callers such as pos_visible_p rely, aren't updated.  */
 11039   /* struct position pos;
 11040     if (!FRAME_WINDOW_P (it->f))
 11041     {
 11042       struct text_pos textpos;
 11043 
 11044       pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
 11045       SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
 11046       reseat (it, textpos, true);
 11047       it->vpos += pos.vpos;
 11048       it->current_y += pos.vpos;
 11049     }
 11050     else */
 11051 
 11052   if (dvpos == 0)
 11053     {
 11054       /* DVPOS == 0 means move to the start of the screen line.  */
 11055       move_it_vertically_backward (it, 0);
 11056       /* Let next call to line_bottom_y calculate real line height.  */
 11057       last_height = 0;
 11058     }
 11059   else if (dvpos > 0)
 11060     {
 11061       move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
 11062       if (!IT_POS_VALID_AFTER_MOVE_P (it))
 11063         {
 11064           /* Only move to the next buffer position if we ended up in a
 11065              string from display property, not in an overlay string
 11066              (before-string or after-string).  That is because the
 11067              latter don't conceal the underlying buffer position, so
 11068              we can ask to move the iterator to the exact position we
 11069              are interested in.  Note that, even if we are already at
 11070              IT_CHARPOS (*it), the call below is not a no-op, as it
 11071              will detect that we are at the end of the string, pop the
 11072              iterator, and compute it->current_x and it->hpos
 11073              correctly.  */
 11074           move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
 11075                       -1, -1, -1, MOVE_TO_POS);
 11076         }
 11077     }
 11078   else
 11079     {
 11080       struct it it2;
 11081       void *it2data = NULL;
 11082       ptrdiff_t start_charpos, orig_charpos, i;
 11083       int nchars_per_row
 11084         = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 11085       bool hit_pos_limit = false;
 11086       ptrdiff_t pos_limit;
 11087 
 11088       /* Start at the beginning of the screen line containing IT's
 11089          position.  This may actually move vertically backwards,
 11090          in case of overlays, so adjust dvpos accordingly.  */
 11091       dvpos += it->vpos;
 11092       orig_charpos = IT_CHARPOS (*it);
 11093       move_it_vertically_backward (it, 0);
 11094       dvpos -= it->vpos;
 11095 
 11096       /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
 11097          screen lines, and reseat the iterator there.  */
 11098       start_charpos = IT_CHARPOS (*it);
 11099       if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 11100         pos_limit = BEGV;
 11101       else
 11102         pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
 11103 
 11104       for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
 11105         back_to_previous_visible_line_start (it);
 11106       if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
 11107         hit_pos_limit = true;
 11108       reseat (it, it->current.pos, true);
 11109 
 11110       /* Move further back if we end up in a string or an image.  */
 11111       while (!IT_POS_VALID_AFTER_MOVE_P (it))
 11112         {
 11113           /* First try to move to start of display line.  */
 11114           dvpos += it->vpos;
 11115           move_it_vertically_backward (it, 0);
 11116           dvpos -= it->vpos;
 11117           if (IT_POS_VALID_AFTER_MOVE_P (it))
 11118             break;
 11119           /* If start of line is still in string or image,
 11120              move further back.  */
 11121           back_to_previous_visible_line_start (it);
 11122           reseat (it, it->current.pos, true);
 11123           dvpos--;
 11124         }
 11125 
 11126       it->current_x = it->hpos = 0;
 11127 
 11128       /* Above call may have moved too far if continuation lines
 11129          are involved.  Scan forward and see if it did.  */
 11130       SAVE_IT (it2, *it, it2data);
 11131       it2.vpos = it2.current_y = 0;
 11132       move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
 11133       it->vpos -= it2.vpos;
 11134       it->current_y -= it2.current_y;
 11135       it->current_x = it->hpos = 0;
 11136 
 11137       /* If we moved too far back, move IT some lines forward.  */
 11138       if (it2.vpos > -dvpos)
 11139         {
 11140           int delta = it2.vpos + dvpos;
 11141 
 11142           RESTORE_IT (&it2, &it2, it2data);
 11143           SAVE_IT (it2, *it, it2data);
 11144           move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
 11145           /* Move back again if we got too far ahead,
 11146              or didn't move at all.  */
 11147           if (it->vpos - it2.vpos > delta || IT_CHARPOS (*it) == orig_charpos)
 11148             RESTORE_IT (it, &it2, it2data);
 11149           else
 11150             bidi_unshelve_cache (it2data, true);
 11151         }
 11152       else if (hit_pos_limit && pos_limit > BEGV
 11153                && dvpos < 0 && it2.vpos < -dvpos)
 11154         {
 11155           /* If we hit the limit, but still didn't make it far enough
 11156              back, that means there's a display string with a newline
 11157              covering a large chunk of text, and that caused
 11158              back_to_previous_visible_line_start try to go too far.
 11159              Punish those who commit such atrocities by going back
 11160              until we've reached DVPOS, after lifting the limit, which
 11161              could make it slow for very long lines.  "If it hurts,
 11162              don't do that!"  */
 11163           dvpos += it2.vpos;
 11164           RESTORE_IT (it, it, it2data);
 11165           for (i = -dvpos; i > 0; --i)
 11166             {
 11167               back_to_previous_visible_line_start (it);
 11168               it->vpos--;
 11169             }
 11170           reseat_1 (it, it->current.pos, true);
 11171         }
 11172       else
 11173         RESTORE_IT (it, it, it2data);
 11174     }
 11175 }
 11176 
 11177 int
 11178 partial_line_height (struct it *it_origin)
 11179 {
 11180   /* In a buffer with very long and truncated lines, we ignore the
 11181      possibly-partial height of the last line in the window: it is too
 11182      expensive to compute that (since in most cases that involves
 11183      going all the way to ZV), and the effect of ignoring it is
 11184      relatively minor.  */
 11185   if (XBUFFER (it_origin->w->contents)->long_line_optimizations_p
 11186       && it_origin->line_wrap == TRUNCATE)
 11187     return 0;
 11188 
 11189   int partial_height;
 11190   void *it_data = NULL;
 11191   struct it it;
 11192   SAVE_IT (it, *it_origin, it_data);
 11193   move_it_to (&it, ZV, -1, it.last_visible_y, -1,
 11194               MOVE_TO_POS | MOVE_TO_Y);
 11195   if (it.what == IT_EOB)
 11196     {
 11197       int vis_height = it.last_visible_y - it.current_y;
 11198       int height = it.ascent + it.descent;
 11199       partial_height = (vis_height < height) ? vis_height : 0;
 11200     }
 11201   else
 11202     {
 11203       int last_line_y = it.current_y;
 11204       move_it_by_lines (&it, 1);
 11205       partial_height = (it.current_y > it.last_visible_y)
 11206         ? it.last_visible_y - last_line_y : 0;
 11207     }
 11208   RESTORE_IT (&it, &it, it_data);
 11209   return partial_height;
 11210 }
 11211 
 11212 /* Approximate move_it_in_display_line_to for very long and truncated
 11213    display lines, when moving horizontally.  This is used when the
 11214    buffer's long_line_optimizations_p flag is set.  It ignores various
 11215    complications, like different font sizes, invisible text, display
 11216    and overlay strings, and, to some degree, bidirectional text.  So
 11217    caveat emptor!
 11218 
 11219    Starting from IT's position, reseat IT after skipping NCHARS
 11220    characters or to the next newline/ZV, whichever comes first.  Return
 11221    what move_it_in_display_line_to would have returned in this case.  */
 11222 
 11223 static enum move_it_result
 11224 fast_move_it_horizontally (struct it *it, ptrdiff_t nchars)
 11225 {
 11226   ptrdiff_t nl_bytepos;
 11227   ptrdiff_t nl_pos = find_newline_no_quit (IT_CHARPOS (*it), IT_BYTEPOS (*it),
 11228                                            1, &nl_bytepos);
 11229   struct text_pos new_pos;
 11230   enum move_it_result move_result;
 11231 
 11232   if (nl_pos - IT_CHARPOS (*it) > nchars)
 11233     {
 11234       SET_TEXT_POS (new_pos,
 11235                     IT_CHARPOS (*it) + nchars,
 11236                     CHAR_TO_BYTE (IT_CHARPOS (*it) + nchars));
 11237       move_result = MOVE_X_REACHED;
 11238     }
 11239   else
 11240     {
 11241       if (nl_bytepos < ZV_BYTE
 11242           || (nl_bytepos > BEGV_BYTE
 11243               && FETCH_BYTE (nl_bytepos - 1) == '\n'))
 11244         {
 11245           nl_pos--;
 11246           nl_bytepos--;
 11247           move_result = MOVE_NEWLINE_OR_CR;
 11248         }
 11249       else
 11250         move_result = MOVE_POS_MATCH_OR_ZV;
 11251       SET_TEXT_POS (new_pos, nl_pos, nl_bytepos);
 11252     }
 11253   reseat (it, new_pos, false);
 11254   return move_result;
 11255 }
 11256 
 11257 /* Return true if IT points into the middle of a display vector.  */
 11258 
 11259 bool
 11260 in_display_vector_p (struct it *it)
 11261 {
 11262   return (it->method == GET_FROM_DISPLAY_VECTOR
 11263           && it->current.dpvec_index > 0
 11264           && it->dpvec + it->current.dpvec_index != it->dpend);
 11265 }
 11266 
 11267 /* This is like Fwindow_text_pixel_size but assumes that WINDOW's buffer
 11268    is the current buffer.  Fbuffer_text_pixel_size calls it after it has
 11269    set WINDOW's buffer to the buffer specified by its BUFFER_OR_NAME
 11270    argument.  */
 11271 static Lisp_Object
 11272 window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 11273                         Lisp_Object x_limit, Lisp_Object y_limit,
 11274                         Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11275 {
 11276   struct window *w = decode_live_window (window);
 11277   struct it it;
 11278   ptrdiff_t start, end, bpos;
 11279   struct text_pos startp;
 11280   void *itdata = NULL;
 11281   int c, max_x = 0, max_y = 0, x = 0, y = 0, vertical_offset = 0, doff = 0;
 11282 
 11283   if (NILP (from))
 11284     {
 11285       start = BEGV;
 11286       bpos = BEGV_BYTE;
 11287     }
 11288   else if (EQ (from, Qt))
 11289     {
 11290       start = BEGV;
 11291       bpos = BEGV_BYTE;
 11292       while (bpos < ZV_BYTE)
 11293         {
 11294           c = FETCH_BYTE (bpos);
 11295           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11296             break;
 11297           inc_both (&start, &bpos);
 11298         }
 11299       while (bpos > BEGV_BYTE)
 11300         {
 11301           dec_both (&start, &bpos);
 11302           c = FETCH_BYTE (bpos);
 11303           if (!(c == ' ' || c == '\t'))
 11304             break;
 11305         }
 11306     }
 11307   else if (CONSP (from))
 11308     {
 11309       start = clip_to_bounds (BEGV, fix_position (XCAR (from)), ZV);
 11310       bpos = CHAR_TO_BYTE (start);
 11311       CHECK_FIXNUM (XCDR (from));
 11312       vertical_offset = XFIXNUM (XCDR (from));
 11313     }
 11314   else
 11315     {
 11316       start = clip_to_bounds (BEGV, fix_position (from), ZV);
 11317       bpos = CHAR_TO_BYTE (start);
 11318     }
 11319 
 11320   SET_TEXT_POS (startp, start, bpos);
 11321 
 11322   if (NILP (to))
 11323     end = ZV;
 11324   else if (EQ (to, Qt))
 11325     {
 11326       end = ZV;
 11327       bpos = ZV_BYTE;
 11328       while (bpos > BEGV_BYTE)
 11329         {
 11330           dec_both (&end, &bpos);
 11331           c = FETCH_BYTE (bpos);
 11332           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11333             {
 11334               inc_both (&end, &bpos);
 11335               break;
 11336             }
 11337         }
 11338       while (bpos < ZV_BYTE)
 11339         {
 11340           c = fetch_char_advance (&end, &bpos);
 11341           if (!(c == ' ' || c == '\t'))
 11342             break;
 11343         }
 11344     }
 11345   else
 11346     end = clip_to_bounds (start, fix_position (to), ZV);
 11347 
 11348   if (RANGED_FIXNUMP (0, x_limit, INT_MAX))
 11349     max_x = XFIXNUM (x_limit);
 11350   else if (!NILP (x_limit))
 11351     max_x = INT_MAX;
 11352 
 11353   if (NILP (y_limit))
 11354     max_y = INT_MAX;
 11355   else if (RANGED_FIXNUMP (0, y_limit, INT_MAX))
 11356     max_y = XFIXNUM (y_limit);
 11357 
 11358   itdata = bidi_shelve_cache ();
 11359 
 11360   start_display (&it, w, startp);
 11361 
 11362   int start_y = it.current_y;
 11363 
 11364   /* It makes no sense to measure dimensions of region of text that
 11365      crosses the point where bidi reordering changes scan direction.
 11366      By using unidirectional movement here we at least support the use
 11367      case of measuring regions of text that have a uniformly R2L
 11368      directionality, and regions that begin and end in text of the
 11369      same directionality.  */
 11370   it.bidi_p = false;
 11371 
 11372   int start_x;
 11373   if (vertical_offset != 0)
 11374     {
 11375       int last_y;
 11376       it.current_y = 0;
 11377 
 11378       move_it_by_lines (&it, 0);
 11379 
 11380       /* `move_it_vertically_backward' is called by move_it_vertically
 11381          to move by a negative value (upwards), but it is not always
 11382          guaranteed to leave the iterator at or above the position
 11383          given by the offset, which this loop ensures.  */
 11384       if (vertical_offset < 0)
 11385         {
 11386           while (it.current_y > vertical_offset)
 11387             {
 11388               last_y = it.current_y;
 11389               move_it_vertically_backward (&it,
 11390                                            (abs (vertical_offset)
 11391                                             + it.current_y));
 11392 
 11393               if (it.current_y == last_y)
 11394                 break;
 11395             }
 11396         }
 11397       else
 11398         {
 11399           move_it_vertically (&it, vertical_offset);
 11400         }
 11401 
 11402       it.current_y = (WINDOW_TAB_LINE_HEIGHT (w)
 11403                       + WINDOW_HEADER_LINE_HEIGHT (w));
 11404       start = clip_to_bounds (BEGV, IT_CHARPOS (it), ZV);
 11405       start_y = it.current_y;
 11406       start_x = it.current_x;
 11407     }
 11408   else
 11409     {
 11410       /* Start at the beginning of the line containing FROM.  Otherwise
 11411          IT.current_x will be incorrectly set to zero at some arbitrary
 11412          non-zero X coordinate.  */
 11413       reseat_at_previous_visible_line_start (&it);
 11414       it.current_x = it.hpos = 0;
 11415       if (IT_CHARPOS (it) != start)
 11416         {
 11417           void *it1data = NULL;
 11418           struct it it1;
 11419 
 11420           SAVE_IT (it1, it, it1data);
 11421           move_it_to (&it, start, -1, -1, -1, MOVE_TO_POS);
 11422           /* We could have a display property at START, in which case
 11423              asking move_it_to to stop at START will overshoot and
 11424              stop at position after START.  So we try again, stopping
 11425              before START, and account for the width of the last
 11426              buffer position manually.  */
 11427           if (IT_CHARPOS (it) > start && start > BEGV)
 11428             {
 11429               ptrdiff_t it1pos = IT_CHARPOS (it1);
 11430               int it1_x = it1.current_x;
 11431 
 11432               RESTORE_IT (&it, &it1, it1data);
 11433               /* If START - 1 is the beginning of screen line,
 11434                  move_it_to will not move, so we need to use a
 11435                  lower-level move_it_in_display_line subroutine, and
 11436                  tell it to move just 1 pixel, so it stops at the next
 11437                  display element.  */
 11438               if (start - 1 > it1pos)
 11439                 move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11440               else
 11441                 move_it_in_display_line (&it, start, it1_x + 1,
 11442                                          MOVE_TO_POS | MOVE_TO_X);
 11443               move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11444               start_x = it.current_x;
 11445               /* If we didn't change our buffer position, the pixel
 11446                  width of what's here was not yet accounted for; do it
 11447                  manually.  */
 11448               if (IT_CHARPOS (it) == start - 1)
 11449                 start_x += it.pixel_width;
 11450             }
 11451           else
 11452             {
 11453               start_x = it.current_x;
 11454               bidi_unshelve_cache (it1data, true);
 11455             }
 11456         }
 11457       else
 11458         start_x = it.current_x;
 11459     }
 11460 
 11461   /* Now move to TO.  */
 11462   int move_op = MOVE_TO_POS | MOVE_TO_Y;
 11463   int to_x = -1;
 11464   it.current_y = start_y;
 11465   /* If FROM is on a newline, pretend that we start at the beginning
 11466      of the next line, because the newline takes no place on display.  */
 11467   if (FETCH_BYTE (start) == '\n')
 11468     it.current_x = 0;
 11469   if (!NILP (x_limit))
 11470     {
 11471       it.last_visible_x = max_x;
 11472       /* Actually, we never want move_it_to stop at to_x.  But to make
 11473          sure that move_it_in_display_line_to always moves far enough,
 11474          we set to_x to INT_MAX and specify MOVE_TO_X.  */
 11475       move_op |= MOVE_TO_X;
 11476       to_x = INT_MAX;
 11477     }
 11478 
 11479   void *it2data = NULL;
 11480   struct it it2;
 11481   SAVE_IT (it2, it, it2data);
 11482 
 11483   x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11484 
 11485   /* We could have a display property at END, in which case asking
 11486      move_it_to to stop at END will overshoot and stop at position
 11487      after END.  So we try again, stopping before END, and account for
 11488      the width of the last buffer position manually.  */
 11489   if (IT_CHARPOS (it) > end)
 11490     {
 11491       end--;
 11492       RESTORE_IT (&it, &it2, it2data);
 11493       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11494       /* Add the width of the thing at TO, but only if we didn't
 11495          overshoot it; if we did, it is already accounted for.  Also,
 11496          account for the height of the thing at TO.  */
 11497       if (IT_CHARPOS (it) == end)
 11498         {
 11499           x += it.pixel_width;
 11500 
 11501           /* DTRT if ignore_line_at_end is t.  */
 11502           if (!NILP (ignore_line_at_end))
 11503             doff = (max (it.max_ascent, it.ascent)
 11504                     + max (it.max_descent, it.descent));
 11505           else
 11506             {
 11507               it.max_ascent = max (it.max_ascent, it.ascent);
 11508               it.max_descent = max (it.max_descent, it.descent);
 11509             }
 11510         }
 11511     }
 11512   else
 11513     bidi_unshelve_cache (it2data, true);
 11514 
 11515   if (!NILP (x_limit))
 11516     {
 11517       /* Don't return more than X-LIMIT.  */
 11518       if (x > max_x)
 11519         x = max_x;
 11520     }
 11521 
 11522   /* If text spans more than one screen line, we don't need to adjust
 11523      the x-span for start_x, since the second and subsequent lines
 11524      will begin at zero X coordinate.  */
 11525   if (it.current_y > start_y)
 11526     start_x = 0;
 11527 
 11528   /* Subtract height of header-line and tab-line which was counted
 11529      automatically by start_display.  */
 11530   if (!NILP (ignore_line_at_end))
 11531     y = (it.current_y + doff
 11532          - WINDOW_TAB_LINE_HEIGHT (w)
 11533          - WINDOW_HEADER_LINE_HEIGHT (w));
 11534   else
 11535     y = (it.current_y + it.max_ascent + it.max_descent + doff
 11536          - WINDOW_TAB_LINE_HEIGHT (w) - WINDOW_HEADER_LINE_HEIGHT (w));
 11537 
 11538   /* Don't return more than Y-LIMIT.  */
 11539   if (y > max_y)
 11540     y = max_y;
 11541 
 11542   if ((EQ (mode_lines, Qtab_line) || EQ (mode_lines, Qt))
 11543       && window_wants_tab_line (w))
 11544     /* Add height of tab-line as requested.  */
 11545     {
 11546       Lisp_Object window_tab_line_format
 11547         = window_parameter (w, Qtab_line_format);
 11548 
 11549       y = y + display_mode_line (w, TAB_LINE_FACE_ID,
 11550                                  NILP (window_tab_line_format)
 11551                                  ? BVAR (current_buffer, tab_line_format)
 11552                                  : window_tab_line_format);
 11553     }
 11554 
 11555   if ((EQ (mode_lines, Qheader_line) || EQ (mode_lines, Qt))
 11556       && window_wants_header_line (w))
 11557     {
 11558       Lisp_Object window_header_line_format
 11559         = window_parameter (w, Qheader_line_format);
 11560 
 11561       y = y + display_mode_line (w, HEADER_LINE_FACE_ID,
 11562                                  NILP (window_header_line_format)
 11563                                  ? BVAR (current_buffer, header_line_format)
 11564                                  : window_header_line_format);
 11565     }
 11566 
 11567   if ((EQ (mode_lines, Qmode_line) || EQ (mode_lines, Qt))
 11568       && window_wants_mode_line (w))
 11569     {
 11570       Lisp_Object window_mode_line_format
 11571         = window_parameter (w, Qmode_line_format);
 11572 
 11573       y = y + display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
 11574                                  NILP (window_mode_line_format)
 11575                                  ? BVAR (current_buffer, mode_line_format)
 11576                                  : window_mode_line_format);
 11577     }
 11578 
 11579   bidi_unshelve_cache (itdata, false);
 11580 
 11581   return (!vertical_offset
 11582           ? Fcons (make_fixnum (x - start_x), make_fixnum (y))
 11583           : list3i (x - start_x, y, start));
 11584 }
 11585 
 11586 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
 11587        doc: /* Return the size of the text of WINDOW's buffer in pixels.
 11588 WINDOW must be a live window and defaults to the selected one.  The
 11589 return value is a cons of the maximum pixel-width of any text line and
 11590 the pixel-height of all the text lines in the accessible portion of
 11591 buffer text.
 11592 
 11593 If FROM is a cons cell, the return value includes, in addition to the
 11594 dimensions, also a third element that provides the buffer position
 11595 from which measuring of the text dimensions was actually started.
 11596 
 11597 This function exists to allow Lisp programs to adjust the dimensions
 11598 of WINDOW to the buffer text it needs to display.
 11599 
 11600 The optional argument FROM, if non-nil, specifies the first text
 11601 position to consider, and defaults to the minimum accessible position
 11602 of the buffer.  If FROM is a cons, its car specifies a buffer
 11603 position, and its cdr specifies the vertical offset in pixels from
 11604 that position to the first screen line to be measured.  If FROM is t,
 11605 it stands for the minimum accessible position that starts a non-empty
 11606 line.  TO, if non-nil, specifies the last text position and defaults
 11607 to the maximum accessible position of the buffer.  If TO is t, it
 11608 stands for the maximum accessible position that ends a non-empty line.
 11609 
 11610 The optional argument X-LIMIT, if non-nil, specifies the maximum X
 11611 coordinate beyond which the text should be ignored.  It is therefore
 11612 also the maximum width that the function can return.  X-LIMIT nil or
 11613 omitted means to use the pixel-width of WINDOW's body.  This default
 11614 means text of truncated lines wider than the window will be ignored;
 11615 specify a non-nil value for X-LIMIT if lines are truncated and you need
 11616 to account for the truncated text.
 11617 
 11618 Use nil for X-LIMIT if you want to know how high WINDOW should become in
 11619 order to fit all of its buffer's text with the width of WINDOW
 11620 unaltered.  Use the maximum width WINDOW may assume if you intend to
 11621 change WINDOW's width.  Use t for the maximum possible value.  Since
 11622 calculating the width of long lines can take some time, it's always a
 11623 good idea to make this argument as small as possible; in particular, if
 11624 the buffer contains long lines that shall be truncated anyway.
 11625 
 11626 The optional argument Y-LIMIT, if non-nil, specifies the maximum Y
 11627 coordinate beyond which the text is to be ignored; it is therefore
 11628 also the maximum height that the function can return (excluding the
 11629 height of the mode- or header-line, if any).  Y-LIMIT nil or omitted
 11630 means consider all of the accessible portion of buffer text up to the
 11631 position specified by TO.  Since calculating the text height of a
 11632 large buffer can take some time, it makes sense to specify this
 11633 argument if the size of the buffer is large or unknown.
 11634 
 11635 Optional argument MODE-LINES nil or omitted means do not include the
 11636 height of the mode-, tab- or header-line of WINDOW in the return value.
 11637 If it is the symbol `mode-line', `tab-line' or `header-line', include
 11638 only the height of that line, if present, in the return value.  If t,
 11639 include the height of any of these, if present, in the return value.
 11640 
 11641 IGNORE-LINE-AT-END, if non-nil, means to not add the height of the
 11642 screen line that includes TO to the returned height of the text.  */)
 11643   (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
 11644    Lisp_Object y_limit, Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11645 {
 11646   struct window *w = decode_live_window (window);
 11647   struct buffer *b = XBUFFER (w->contents);
 11648   struct buffer *old_b = NULL;
 11649   Lisp_Object value;
 11650 
 11651   if (b != current_buffer)
 11652     {
 11653       old_b = current_buffer;
 11654       set_buffer_internal_1 (b);
 11655     }
 11656 
 11657   value = window_text_pixel_size (window, from, to, x_limit, y_limit, mode_lines,
 11658                                   ignore_line_at_end);
 11659 
 11660   if (old_b)
 11661     set_buffer_internal_1 (old_b);
 11662 
 11663   return value;
 11664 }
 11665 
 11666 DEFUN ("buffer-text-pixel-size", Fbuffer_text_pixel_size, Sbuffer_text_pixel_size, 0, 4, 0,
 11667        doc: /* Return size of whole text of BUFFER-OR-NAME in WINDOW.
 11668 BUFFER-OR-NAME must specify a live buffer or the name of a live buffer
 11669 and defaults to the current buffer.  WINDOW must be a live window and
 11670 defaults to the selected one.  The return value is a cons of the maximum
 11671 pixel-width of any text line and the pixel-height of all the text lines
 11672 of the buffer specified by BUFFER-OR-NAME.
 11673 
 11674 The optional arguments X-LIMIT and Y-LIMIT have the same meaning as with
 11675 `window-text-pixel-size'.
 11676 
 11677 Do not use this function if the buffer specified by BUFFER-OR-NAME is
 11678 already displayed in WINDOW.  `window-text-pixel-size' is cheaper in
 11679 that case because it does not have to temporarily show that buffer in
 11680 WINDOW.  */)
 11681   (Lisp_Object buffer_or_name, Lisp_Object window, Lisp_Object x_limit,
 11682    Lisp_Object y_limit)
 11683 {
 11684   struct window *w = decode_live_window (window);
 11685   struct buffer *b = (NILP (buffer_or_name)
 11686                       ? current_buffer
 11687                       : XBUFFER (Fget_buffer (buffer_or_name)));
 11688   Lisp_Object buffer, value;
 11689   specpdl_ref count = SPECPDL_INDEX ();
 11690 
 11691   XSETBUFFER (buffer, b);
 11692 
 11693   /* The unwind form of with_echo_area_buffer is what we need here to
 11694      make WINDOW temporarily show our buffer.  */
 11695   /* FIXME: Can we move this into the `if (!EQ (buffer, w->contents))`?  */
 11696   record_unwind_protect (unwind_with_echo_area_buffer,
 11697                          with_echo_area_buffer_unwind_data (w));
 11698 
 11699   set_buffer_internal_1 (b);
 11700 
 11701   ptrdiff_t base_line_pos = w->base_line_pos;
 11702   int end_valid = w->window_end_valid;
 11703   if (!EQ (buffer, w->contents))
 11704     {
 11705       wset_buffer (w, buffer);
 11706       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 11707       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 11708     }
 11709 
 11710   value = window_text_pixel_size (window, Qnil, Qnil, x_limit, y_limit, Qnil,
 11711                                   Qnil);
 11712 
 11713   unbind_to (count, Qnil);
 11714 
 11715   /* Restore original values.  This is important if this function is
 11716      called from some ':eval' form in the middle of redisplay.  */
 11717   w->base_line_pos = base_line_pos;
 11718   w->window_end_valid = end_valid;
 11719 
 11720   return value;
 11721 }
 11722 
 11723 
 11724 DEFUN ("display--line-is-continued-p", Fdisplay__line_is_continued_p,
 11725        Sdisplay__line_is_continued_p, 0, 0, 0,
 11726        doc: /* Return non-nil if the current screen line is continued on display.  */)
 11727   (void)
 11728 {
 11729   struct buffer *oldb = current_buffer;
 11730   struct window *w = XWINDOW (selected_window);
 11731   enum move_it_result rc = MOVE_POS_MATCH_OR_ZV;
 11732 
 11733   set_buffer_internal_1 (XBUFFER (w->contents));
 11734 
 11735   if (PT < ZV)
 11736     {
 11737       struct text_pos startpos;
 11738       struct it it;
 11739       void *itdata;
 11740       /* Use a marker, since vertical-motion enters redisplay, which can
 11741          trigger fontifications, which in turn could modify buffer text.  */
 11742       Lisp_Object opoint = Fpoint_marker ();
 11743 
 11744       /* Make sure to start from the beginning of the current screen
 11745          line, so that move_it_in_display_line_to counts pixels correctly.  */
 11746       Fvertical_motion (make_fixnum (0), selected_window, Qnil);
 11747       SET_TEXT_POS (startpos, PT, PT_BYTE);
 11748       itdata = bidi_shelve_cache ();
 11749       start_display (&it, w, startpos);
 11750       /* If lines are truncated, no line is continued.  */
 11751       if (it.line_wrap != TRUNCATE)
 11752         {
 11753           it.glyph_row = NULL;
 11754           rc = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 11755         }
 11756       SET_PT_BOTH (marker_position (opoint), marker_byte_position (opoint));
 11757       bidi_unshelve_cache (itdata, false);
 11758     }
 11759   set_buffer_internal_1 (oldb);
 11760 
 11761   return rc == MOVE_LINE_CONTINUED ? Qt : Qnil;
 11762 }
 11763 
 11764 
 11765 /***********************************************************************
 11766                                Messages
 11767  ***********************************************************************/
 11768 
 11769 /* Return the number of arguments the format string FORMAT needs.  */
 11770 
 11771 static ptrdiff_t
 11772 format_nargs (char const *format)
 11773 {
 11774   ptrdiff_t nargs = 0;
 11775   for (char const *p = format; (p = strchr (p, '%')); p++)
 11776     if (p[1] == '%')
 11777       p++;
 11778     else
 11779       nargs++;
 11780   return nargs;
 11781 }
 11782 
 11783 /* Add a message with format string FORMAT and formatted arguments
 11784    to *Messages*.  */
 11785 
 11786 void
 11787 add_to_log (const char *format, ...)
 11788 {
 11789   va_list ap;
 11790   va_start (ap, format);
 11791   vadd_to_log (format, ap);
 11792   va_end (ap);
 11793 }
 11794 
 11795 void
 11796 vadd_to_log (char const *format, va_list ap)
 11797 {
 11798   ptrdiff_t form_nargs = format_nargs (format);
 11799   ptrdiff_t nargs = 1 + form_nargs;
 11800   Lisp_Object args[10];
 11801   eassert (nargs <= ARRAYELTS (args));
 11802   AUTO_STRING (args0, format);
 11803   args[0] = args0;
 11804   for (ptrdiff_t i = 1; i <= nargs; i++)
 11805     args[i] = va_arg (ap, Lisp_Object);
 11806   Lisp_Object msg = Qnil;
 11807   msg = Fformat_message (nargs, args);
 11808 
 11809   ptrdiff_t len = SBYTES (msg) + 1;
 11810   USE_SAFE_ALLOCA;
 11811   char *buffer = SAFE_ALLOCA (len);
 11812   memcpy (buffer, SDATA (msg), len);
 11813 
 11814   message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
 11815   SAFE_FREE ();
 11816 }
 11817 
 11818 
 11819 /* Output a newline in the *Messages* buffer if "needs" one.  */
 11820 
 11821 void
 11822 message_log_maybe_newline (void)
 11823 {
 11824   if (message_log_need_newline)
 11825     message_dolog ("", 0, true, false);
 11826 }
 11827 
 11828 
 11829 /* Add a string M of length NBYTES to the message log, optionally
 11830    terminated with a newline when NLFLAG is true.  MULTIBYTE, if
 11831    true, means interpret the contents of M as multibyte.  This
 11832    function calls low-level routines in order to bypass text property
 11833    hooks, etc. which might not be safe to run.
 11834 
 11835    This may GC (insert may run before/after change hooks),
 11836    so the buffer M must NOT point to a Lisp string.  */
 11837 
 11838 void
 11839 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
 11840 {
 11841   const unsigned char *msg = (const unsigned char *) m;
 11842 
 11843   if (!NILP (Vmemory_full))
 11844     return;
 11845 
 11846   if (!NILP (Vmessage_log_max))
 11847     {
 11848       struct buffer *oldbuf;
 11849       Lisp_Object oldpoint, oldbegv, oldzv;
 11850       int old_windows_or_buffers_changed = windows_or_buffers_changed;
 11851       ptrdiff_t point_at_end = 0;
 11852       ptrdiff_t zv_at_end = 0;
 11853       Lisp_Object old_deactivate_mark;
 11854 
 11855       old_deactivate_mark = Vdeactivate_mark;
 11856       oldbuf = current_buffer;
 11857 
 11858       /* Sanity check, in case the variable has been set to something
 11859          invalid.  */
 11860       if (! STRINGP (Vmessages_buffer_name))
 11861         Vmessages_buffer_name = build_string ("*Messages*");
 11862       /* Ensure the Messages buffer exists, and switch to it.
 11863          If we created it, set the major-mode.  */
 11864       bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
 11865       Fset_buffer (Fget_buffer_create (Vmessages_buffer_name, Qnil));
 11866       if (newbuffer
 11867           && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
 11868         call0 (intern ("messages-buffer-mode"));
 11869 
 11870       bset_undo_list (current_buffer, Qt);
 11871       bset_cache_long_scans (current_buffer, Qnil);
 11872 
 11873       oldpoint = message_dolog_marker1;
 11874       set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
 11875       oldbegv = message_dolog_marker2;
 11876       set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
 11877       oldzv = message_dolog_marker3;
 11878       set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
 11879 
 11880       if (PT == Z)
 11881         point_at_end = 1;
 11882       if (ZV == Z)
 11883         zv_at_end = 1;
 11884 
 11885       BEGV = BEG;
 11886       BEGV_BYTE = BEG_BYTE;
 11887       ZV = Z;
 11888       ZV_BYTE = Z_BYTE;
 11889       TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11890 
 11891       /* Insert the string--maybe converting multibyte to single byte
 11892          or vice versa, so that all the text fits the buffer.  */
 11893       if (multibyte
 11894           && NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11895         {
 11896           /* Convert a multibyte string to single-byte
 11897              for the *Message* buffer.  */
 11898           for (ptrdiff_t i = 0; i < nbytes; )
 11899             {
 11900               int char_bytes, c = check_char_and_length (msg + i, &char_bytes);
 11901               char work = CHAR_TO_BYTE8 (c);
 11902               insert_1_both (&work, 1, 1, true, false, false);
 11903               i += char_bytes;
 11904             }
 11905         }
 11906       else if (! multibyte
 11907                && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11908         {
 11909           /* Convert a single-byte string to multibyte
 11910              for the *Message* buffer.  */
 11911           for (ptrdiff_t i = 0; i < nbytes; i++)
 11912             {
 11913               int c = make_char_multibyte (msg[i]);
 11914               unsigned char str[MAX_MULTIBYTE_LENGTH];
 11915               int char_bytes = CHAR_STRING (c, str);
 11916               insert_1_both ((char *) str, 1, char_bytes, true, false, false);
 11917             }
 11918         }
 11919       else if (nbytes)
 11920         insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
 11921                        true, false, false);
 11922 
 11923       if (nlflag)
 11924         {
 11925           ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 11926           intmax_t dups;
 11927 
 11928           /* Since we call del_range_both passing false for PREPARE,
 11929              we aren't prepared to run modification hooks (we could
 11930              end up calling modification hooks from another buffer and
 11931              only with AFTER=t, Bug#21824).  */
 11932           specpdl_ref count = SPECPDL_INDEX ();
 11933           specbind (Qinhibit_modification_hooks, Qt);
 11934 
 11935           insert_1_both ("\n", 1, 1, true, false, false);
 11936 
 11937           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
 11938           this_bol = PT;
 11939           this_bol_byte = PT_BYTE;
 11940 
 11941           /* See if this line duplicates the previous one.
 11942              If so, combine duplicates.  */
 11943           if (this_bol > BEG)
 11944             {
 11945               scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
 11946               prev_bol = PT;
 11947               prev_bol_byte = PT_BYTE;
 11948 
 11949               dups = message_log_check_duplicate (prev_bol_byte,
 11950                                                   this_bol_byte);
 11951               if (dups)
 11952                 {
 11953                   del_range_both (prev_bol, prev_bol_byte,
 11954                                   this_bol, this_bol_byte, false);
 11955                   if (dups > 1)
 11956                     {
 11957                       char dupstr[sizeof " [ times]"
 11958                                   + INT_STRLEN_BOUND (dups)];
 11959 
 11960                       /* If you change this format, don't forget to also
 11961                          change message_log_check_duplicate.  */
 11962                       int duplen = sprintf (dupstr, " [%"PRIdMAX" times]",
 11963                                             dups);
 11964                       TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
 11965                       insert_1_both (dupstr, duplen, duplen,
 11966                                      true, false, true);
 11967                     }
 11968                 }
 11969             }
 11970 
 11971           /* If we have more than the desired maximum number of lines
 11972              in the *Messages* buffer now, delete the oldest ones.
 11973              This is safe because we don't have undo in this buffer.  */
 11974 
 11975           if (FIXNATP (Vmessage_log_max))
 11976             {
 11977               scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
 11978                             -XFIXNAT (Vmessage_log_max) - 1, false);
 11979               del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
 11980             }
 11981 
 11982           unbind_to (count, Qnil);
 11983         }
 11984       BEGV = marker_position (oldbegv);
 11985       BEGV_BYTE = marker_byte_position (oldbegv);
 11986 
 11987       if (zv_at_end)
 11988         {
 11989           ZV = Z;
 11990           ZV_BYTE = Z_BYTE;
 11991         }
 11992       else
 11993         {
 11994           ZV = marker_position (oldzv);
 11995           ZV_BYTE = marker_byte_position (oldzv);
 11996         }
 11997 
 11998       if (point_at_end)
 11999         TEMP_SET_PT_BOTH (Z, Z_BYTE);
 12000       else
 12001         /* We can't do Fgoto_char (oldpoint) because it will run some
 12002            Lisp code.  */
 12003         TEMP_SET_PT_BOTH (marker_position (oldpoint),
 12004                           marker_byte_position (oldpoint));
 12005 
 12006       unchain_marker (XMARKER (oldpoint));
 12007       unchain_marker (XMARKER (oldbegv));
 12008       unchain_marker (XMARKER (oldzv));
 12009 
 12010       /* We called insert_1_both above with its 5th argument (PREPARE)
 12011          false, which prevents insert_1_both from calling
 12012          prepare_to_modify_buffer, which in turns prevents us from
 12013          incrementing windows_or_buffers_changed even if *Messages* is
 12014          shown in some window.  So we must manually set
 12015          windows_or_buffers_changed here to make up for that.  */
 12016       windows_or_buffers_changed = old_windows_or_buffers_changed;
 12017       bset_redisplay (current_buffer);
 12018 
 12019       set_buffer_internal (oldbuf);
 12020 
 12021       message_log_need_newline = !nlflag;
 12022       Vdeactivate_mark = old_deactivate_mark;
 12023     }
 12024 }
 12025 
 12026 
 12027 /* We are at the end of the buffer after just having inserted a newline.
 12028    (Note: We depend on the fact we won't be crossing the gap.)
 12029    Check to see if the most recent message looks a lot like the previous one.
 12030    Return 0 if different, 1 if the new one should just replace it, or a
 12031    value N > 1 if we should also append " [N times]".  */
 12032 
 12033 static intmax_t
 12034 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
 12035 {
 12036   ptrdiff_t i;
 12037   ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
 12038   bool seen_dots = false;
 12039   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
 12040   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
 12041 
 12042   for (i = 0; i < len; i++)
 12043     {
 12044       if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
 12045         seen_dots = true;
 12046       if (p1[i] != p2[i])
 12047         return seen_dots;
 12048     }
 12049   p1 += len;
 12050   if (*p1 == '\n')
 12051     return 2;
 12052   if (*p1++ == ' ' && *p1++ == '[')
 12053     {
 12054       char *pend;
 12055       intmax_t n = strtoimax ((char *) p1, &pend, 10);
 12056       if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
 12057         return n + 1;
 12058     }
 12059   return 0;
 12060 }
 12061 
 12062 
 12063 /* Display an echo area message M with a specified length of NBYTES
 12064    bytes.  The string may include null characters.  If M is not a
 12065    string, clear out any existing message, and let the mini-buffer
 12066    text show through.
 12067 
 12068    This function cancels echoing.  */
 12069 
 12070 void
 12071 message3 (Lisp_Object m)
 12072 {
 12073   clear_message (true, true);
 12074   cancel_echoing ();
 12075 
 12076   /* First flush out any partial line written with print.  */
 12077   message_log_maybe_newline ();
 12078   if (STRINGP (m))
 12079     {
 12080       ptrdiff_t nbytes = SBYTES (m);
 12081       bool multibyte = STRING_MULTIBYTE (m);
 12082       char *buffer;
 12083       USE_SAFE_ALLOCA;
 12084       SAFE_ALLOCA_STRING (buffer, m);
 12085       message_dolog (buffer, nbytes, true, multibyte);
 12086       SAFE_FREE ();
 12087     }
 12088   if (! inhibit_message)
 12089     message3_nolog (m);
 12090 }
 12091 
 12092 /* Log the message M to stderr.  Log an empty line if M is not a string.  */
 12093 
 12094 static void
 12095 message_to_stderr (Lisp_Object m)
 12096 {
 12097   if (noninteractive_need_newline)
 12098     {
 12099       noninteractive_need_newline = false;
 12100       errputc ('\n');
 12101     }
 12102   if (STRINGP (m))
 12103     {
 12104       Lisp_Object coding_system = Vlocale_coding_system;
 12105       Lisp_Object s;
 12106 
 12107       if (!NILP (Vcoding_system_for_write))
 12108         coding_system = Vcoding_system_for_write;
 12109       if (!NILP (coding_system))
 12110         s = code_convert_string_norecord (m, coding_system, true);
 12111       else
 12112         s = m;
 12113 
 12114       errwrite (SDATA (s), SBYTES (s));
 12115     }
 12116   if (STRINGP (m) || !cursor_in_echo_area)
 12117     errputc ('\n');
 12118 }
 12119 
 12120 /* The non-logging version of message3.
 12121    This does not cancel echoing, because it is used for echoing.
 12122    Perhaps we need to make a separate function for echoing
 12123    and make this cancel echoing.  */
 12124 
 12125 void
 12126 message3_nolog (Lisp_Object m)
 12127 {
 12128   struct frame *sf = SELECTED_FRAME ();
 12129 
 12130   if (FRAME_INITIAL_P (sf))
 12131     message_to_stderr (m);
 12132   /* Error messages get reported properly by cmd_error, so this must be just an
 12133      informative message; if the frame hasn't really been initialized yet, just
 12134      toss it.  */
 12135   else if (INTERACTIVE && sf->glyphs_initialized_p)
 12136     {
 12137       /* Get the frame containing the mini-buffer
 12138          that the selected frame is using.  */
 12139       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 12140       Lisp_Object frame = XWINDOW (mini_window)->frame;
 12141       struct frame *f = XFRAME (frame);
 12142 
 12143       if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
 12144         Fmake_frame_visible (frame);
 12145 
 12146       if (STRINGP (m) && SCHARS (m) > 0)
 12147         {
 12148           set_message (m);
 12149           if (minibuffer_auto_raise)
 12150             Fraise_frame (frame);
 12151           /* Assume we are not echoing.
 12152              (If we are, echo_now will override this.)  */
 12153           echo_message_buffer = Qnil;
 12154         }
 12155       else
 12156         clear_message (true, true);
 12157 
 12158       do_pending_window_change (false);
 12159       echo_area_display (true);
 12160       do_pending_window_change (false);
 12161       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 12162         (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
 12163     }
 12164 }
 12165 
 12166 
 12167 /* Display a null-terminated echo area message M.  If M is 0, clear
 12168    out any existing message, and let the mini-buffer text show through.
 12169 
 12170    The buffer M must continue to exist until after the echo area gets
 12171    cleared or some other message gets displayed there.  Do not pass
 12172    text that is stored in a Lisp string.  Do not pass text in a buffer
 12173    that was alloca'd.  */
 12174 
 12175 void
 12176 message1 (const char *m)
 12177 {
 12178   message3 (m ? build_unibyte_string (m) : Qnil);
 12179 }
 12180 
 12181 
 12182 /* The non-logging counterpart of message1.  */
 12183 
 12184 void
 12185 message1_nolog (const char *m)
 12186 {
 12187   message3_nolog (m ? build_unibyte_string (m) : Qnil);
 12188 }
 12189 
 12190 /* Display a message M which contains a single %s
 12191    which gets replaced with STRING.  */
 12192 
 12193 void
 12194 message_with_string (const char *m, Lisp_Object string, bool log)
 12195 {
 12196   CHECK_STRING (string);
 12197 
 12198   bool need_message;
 12199   if (noninteractive)
 12200     need_message = !!m;
 12201   else if (!INTERACTIVE)
 12202     need_message = false;
 12203   else
 12204     {
 12205       /* The frame whose minibuffer we're going to display the message on.
 12206          It may be larger than the selected frame, so we need
 12207          to use its buffer, not the selected frame's buffer.  */
 12208       Lisp_Object mini_window;
 12209       struct frame *f, *sf = SELECTED_FRAME ();
 12210 
 12211       /* Get the frame containing the minibuffer
 12212          that the selected frame is using.  */
 12213       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12214       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12215 
 12216       /* Error messages get reported properly by cmd_error, so this must be
 12217          just an informative message; if the frame hasn't really been
 12218          initialized yet, just toss it.  */
 12219       need_message = f->glyphs_initialized_p;
 12220     }
 12221 
 12222   if (need_message)
 12223     {
 12224       AUTO_STRING (fmt, m);
 12225       Lisp_Object msg = CALLN (Fformat_message, fmt, string);
 12226 
 12227       if (noninteractive)
 12228         message_to_stderr (msg);
 12229       else
 12230         {
 12231           if (log)
 12232             message3 (msg);
 12233           else
 12234             message3_nolog (msg);
 12235 
 12236           /* Print should start at the beginning of the message
 12237              buffer next time.  */
 12238           message_buf_print = false;
 12239         }
 12240     }
 12241 }
 12242 
 12243 
 12244 /* Dump an informative message to the minibuf.  If M is 0, clear out
 12245    any existing message, and let the mini-buffer text show through.
 12246 
 12247    The message must be safe ASCII (because when Emacs is
 12248    non-interactive the message is sent straight to stderr without
 12249    encoding first) and the format must not contain ` or ' (because
 12250    this function does not account for `text-quoting-style').  If your
 12251    message and format do not fit into this category, convert your
 12252    arguments to Lisp objects and use Fmessage instead.  */
 12253 
 12254 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 12255 vmessage (const char *m, va_list ap)
 12256 {
 12257   if (noninteractive)
 12258     {
 12259       if (m)
 12260         {
 12261           if (noninteractive_need_newline)
 12262             putc ('\n', stderr);
 12263           noninteractive_need_newline = false;
 12264           vfprintf (stderr, m, ap);
 12265           if (!cursor_in_echo_area)
 12266             putc ('\n', stderr);
 12267           fflush (stderr);
 12268         }
 12269     }
 12270   else if (INTERACTIVE)
 12271     {
 12272       /* The frame whose mini-buffer we're going to display the message
 12273          on.  It may be larger than the selected frame, so we need to
 12274          use its buffer, not the selected frame's buffer.  */
 12275       Lisp_Object mini_window;
 12276       struct frame *f, *sf = SELECTED_FRAME ();
 12277 
 12278       /* Get the frame containing the mini-buffer
 12279          that the selected frame is using.  */
 12280       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12281       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12282 
 12283       /* Error messages get reported properly by cmd_error, so this must be
 12284          just an informative message; if the frame hasn't really been
 12285          initialized yet, just toss it.  */
 12286       if (f->glyphs_initialized_p)
 12287         {
 12288           if (m)
 12289             {
 12290               ptrdiff_t len;
 12291               ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
 12292               USE_SAFE_ALLOCA;
 12293               char *message_buf = SAFE_ALLOCA (maxsize + 1);
 12294 
 12295               len = doprnt (message_buf, maxsize, m, 0, ap);
 12296 
 12297               message3 (make_string (message_buf, len));
 12298               SAFE_FREE ();
 12299             }
 12300           else
 12301             message1 (0);
 12302 
 12303           /* Print should start at the beginning of the message
 12304              buffer next time.  */
 12305           message_buf_print = false;
 12306         }
 12307     }
 12308 }
 12309 
 12310 /* See vmessage for restrictions on the text of the message.  */
 12311 void
 12312 message (const char *m, ...)
 12313 {
 12314   va_list ap;
 12315   va_start (ap, m);
 12316   vmessage (m, ap);
 12317   va_end (ap);
 12318 }
 12319 
 12320 
 12321 /* Display the current message in the current mini-buffer.  This is
 12322    only called from error handlers in process.c, and is not time
 12323    critical.  */
 12324 
 12325 void
 12326 update_echo_area (void)
 12327 {
 12328   if (!NILP (echo_area_buffer[0]))
 12329     {
 12330       Lisp_Object string;
 12331       string = Fcurrent_message ();
 12332       message3 (string);
 12333     }
 12334 }
 12335 
 12336 
 12337 /* Make sure echo area buffers in `echo_buffers' are live.
 12338    If they aren't, make new ones.  */
 12339 
 12340 static void
 12341 ensure_echo_area_buffers (void)
 12342 {
 12343   for (int i = 0; i < 2; i++)
 12344     if (!BUFFERP (echo_buffer[i])
 12345         || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
 12346       {
 12347         Lisp_Object old_buffer = echo_buffer[i];
 12348         static char const name_fmt[] = " *Echo Area %d*";
 12349         char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
 12350         AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
 12351         echo_buffer[i] = Fget_buffer_create (lname, Qnil);
 12352         bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
 12353         /* to force word wrap in echo area -
 12354            it was decided to postpone this*/
 12355         /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
 12356 
 12357         for (int j = 0; j < 2; j++)
 12358           if (EQ (old_buffer, echo_area_buffer[j]))
 12359             echo_area_buffer[j] = echo_buffer[i];
 12360       }
 12361 }
 12362 
 12363 
 12364 /* Call FN with args A1..A2 with either the current or last displayed
 12365    echo_area_buffer as current buffer.
 12366 
 12367    WHICH zero means use the current message buffer
 12368    echo_area_buffer[0].  If that is nil, choose a suitable buffer
 12369    from echo_buffer[] and clear it.
 12370 
 12371    WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
 12372    suitable buffer from echo_buffer[] and clear it.
 12373 
 12374    If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
 12375    that the current message becomes the last displayed one, choose a
 12376    suitable buffer for echo_area_buffer[0], and clear it.
 12377 
 12378    Value is what FN returns.  */
 12379 
 12380 static bool
 12381 with_echo_area_buffer (struct window *w, int which,
 12382                        bool (*fn) (void *, Lisp_Object),
 12383                        void *a1, Lisp_Object a2)
 12384 {
 12385   Lisp_Object buffer;
 12386   bool this_one, the_other, clear_buffer_p, rc;
 12387   specpdl_ref count = SPECPDL_INDEX ();
 12388 
 12389   /* If buffers aren't live, make new ones.  */
 12390   ensure_echo_area_buffers ();
 12391 
 12392   clear_buffer_p = false;
 12393 
 12394   if (which == 0)
 12395     this_one = false, the_other = true;
 12396   else if (which > 0)
 12397     this_one = true, the_other = false;
 12398   else
 12399     {
 12400       this_one = false, the_other = true;
 12401       clear_buffer_p = true;
 12402 
 12403       /* We need a fresh one in case the current echo buffer equals
 12404          the one containing the last displayed echo area message.  */
 12405       if (!NILP (echo_area_buffer[this_one])
 12406           && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
 12407         echo_area_buffer[this_one] = Qnil;
 12408     }
 12409 
 12410   /* Choose a suitable buffer from echo_buffer[] if we don't
 12411      have one.  */
 12412   if (NILP (echo_area_buffer[this_one]))
 12413     {
 12414       echo_area_buffer[this_one]
 12415         = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
 12416            ? echo_buffer[the_other]
 12417            : echo_buffer[this_one]);
 12418       clear_buffer_p = true;
 12419     }
 12420 
 12421   buffer = echo_area_buffer[this_one];
 12422 
 12423   /* Don't get confused by reusing the buffer used for echoing
 12424      for a different purpose.  */
 12425   if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
 12426     cancel_echoing ();
 12427 
 12428   record_unwind_protect (unwind_with_echo_area_buffer,
 12429                          with_echo_area_buffer_unwind_data (w));
 12430 
 12431   /* Make the echo area buffer current.  Note that for display
 12432      purposes, it is not necessary that the displayed window's buffer
 12433      == current_buffer, except for text property lookup.  So, let's
 12434      only set that buffer temporarily here without doing a full
 12435      Fset_window_buffer.  We must also change w->pointm, though,
 12436      because otherwise an assertions in unshow_buffer fails, and Emacs
 12437      aborts.  */
 12438   set_buffer_internal_1 (XBUFFER (buffer));
 12439   if (w)
 12440     {
 12441       wset_buffer (w, buffer);
 12442       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 12443       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 12444     }
 12445 
 12446   bset_undo_list (current_buffer, Qt);
 12447   bset_read_only (current_buffer, Qnil);
 12448   specbind (Qinhibit_read_only, Qt);
 12449   specbind (Qinhibit_modification_hooks, Qt);
 12450 
 12451   if (clear_buffer_p && Z > BEG)
 12452     del_range (BEG, Z);
 12453 
 12454   eassert (BEGV >= BEG);
 12455   eassert (ZV <= Z && ZV >= BEGV);
 12456 
 12457   rc = fn (a1, a2);
 12458 
 12459   eassert (BEGV >= BEG);
 12460   eassert (ZV <= Z && ZV >= BEGV);
 12461 
 12462   unbind_to (count, Qnil);
 12463   return rc;
 12464 }
 12465 
 12466 
 12467 /* Save state that should be preserved around the call to the function
 12468    FN called in with_echo_area_buffer.  */
 12469 
 12470 static Lisp_Object
 12471 with_echo_area_buffer_unwind_data (struct window *w)
 12472 {
 12473   int i = 0;
 12474   Lisp_Object vector, tmp;
 12475 
 12476   /* Reduce consing by keeping one vector in
 12477      Vwith_echo_area_save_vector.  */
 12478   vector = Vwith_echo_area_save_vector;
 12479   Vwith_echo_area_save_vector = Qnil;
 12480 
 12481   if (NILP (vector))
 12482     vector = make_nil_vector (11);
 12483 
 12484   XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
 12485   ASET (vector, i, Vdeactivate_mark); ++i;
 12486   ASET (vector, i, make_fixnum (windows_or_buffers_changed)); ++i;
 12487 
 12488   if (w)
 12489     {
 12490       XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
 12491       ASET (vector, i, w->contents); ++i;
 12492       ASET (vector, i, make_fixnum (marker_position (w->pointm))); ++i;
 12493       ASET (vector, i, make_fixnum (marker_byte_position (w->pointm))); ++i;
 12494       ASET (vector, i, make_fixnum (marker_position (w->old_pointm))); ++i;
 12495       ASET (vector, i, make_fixnum (marker_byte_position (w->old_pointm))); ++i;
 12496       ASET (vector, i, make_fixnum (marker_position (w->start))); ++i;
 12497       ASET (vector, i, make_fixnum (marker_byte_position (w->start))); ++i;
 12498     }
 12499   else
 12500     {
 12501       int end = i + 8;
 12502       for (; i < end; ++i)
 12503         ASET (vector, i, Qnil);
 12504     }
 12505 
 12506   eassert (i == ASIZE (vector));
 12507   return vector;
 12508 }
 12509 
 12510 
 12511 /* Restore global state from VECTOR which was created by
 12512    with_echo_area_buffer_unwind_data.  */
 12513 
 12514 static void
 12515 unwind_with_echo_area_buffer (Lisp_Object vector)
 12516 {
 12517   set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
 12518   Vdeactivate_mark = AREF (vector, 1);
 12519   windows_or_buffers_changed = XFIXNAT (AREF (vector, 2));
 12520 
 12521   if (WINDOWP (AREF (vector, 3)))
 12522     {
 12523       struct window *w;
 12524       Lisp_Object buffer;
 12525 
 12526       w = XWINDOW (AREF (vector, 3));
 12527       buffer = AREF (vector, 4);
 12528 
 12529       wset_buffer (w, buffer);
 12530       set_marker_restricted_both (w->pointm, buffer,
 12531                                   XFIXNAT (AREF (vector, 5)),
 12532                                   XFIXNAT (AREF (vector, 6)));
 12533       set_marker_restricted_both (w->old_pointm, buffer,
 12534                                   XFIXNAT (AREF (vector, 7)),
 12535                                   XFIXNAT (AREF (vector, 8)));
 12536       set_marker_restricted_both (w->start, buffer,
 12537                                   XFIXNAT (AREF (vector, 9)),
 12538                                   XFIXNAT (AREF (vector, 10)));
 12539     }
 12540 
 12541   Vwith_echo_area_save_vector = vector;
 12542 }
 12543 
 12544 
 12545 /* Set up the echo area for use by print functions.  MULTIBYTE_P
 12546    means we will print multibyte.  */
 12547 
 12548 void
 12549 setup_echo_area_for_printing (bool multibyte_p)
 12550 {
 12551   /* If we can't find an echo area any more, exit.  */
 12552   if (! FRAME_LIVE_P (XFRAME (selected_frame)))
 12553     Fkill_emacs (Qnil, Qnil);
 12554 
 12555   ensure_echo_area_buffers ();
 12556 
 12557   if (!message_buf_print)
 12558     {
 12559       /* A message has been output since the last time we printed.
 12560          Choose a fresh echo area buffer.  */
 12561       if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12562         echo_area_buffer[0] = echo_buffer[1];
 12563       else
 12564         echo_area_buffer[0] = echo_buffer[0];
 12565 
 12566       /* Switch to that buffer and clear it.  */
 12567       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12568       bset_truncate_lines (current_buffer, Qnil);
 12569 
 12570       if (Z > BEG)
 12571         {
 12572           specpdl_ref count = SPECPDL_INDEX ();
 12573           specbind (Qinhibit_read_only, Qt);
 12574           /* Note that undo recording is always disabled.  */
 12575           del_range (BEG, Z);
 12576           unbind_to (count, Qnil);
 12577         }
 12578       TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 12579 
 12580       /* Set up the buffer for the multibyteness we need.  We always
 12581          set it to be multibyte, except when
 12582          unibyte-display-via-language-environment is non-nil and the
 12583          buffer from which we are called is unibyte, because in that
 12584          case unibyte characters should not be displayed as octal
 12585          escapes.  */
 12586       if (unibyte_display_via_language_environment
 12587           && !multibyte_p
 12588           && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12589         Fset_buffer_multibyte (Qnil);
 12590       else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12591         Fset_buffer_multibyte (Qt);
 12592 
 12593       /* Raise the frame containing the echo area.  */
 12594       if (minibuffer_auto_raise)
 12595         {
 12596           struct frame *sf = SELECTED_FRAME ();
 12597           Lisp_Object mini_window;
 12598           mini_window = FRAME_MINIBUF_WINDOW (sf);
 12599           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
 12600         }
 12601 
 12602       message_log_maybe_newline ();
 12603       message_buf_print = true;
 12604     }
 12605   else
 12606     {
 12607       if (NILP (echo_area_buffer[0]))
 12608         {
 12609           if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12610             echo_area_buffer[0] = echo_buffer[1];
 12611           else
 12612             echo_area_buffer[0] = echo_buffer[0];
 12613         }
 12614 
 12615       if (current_buffer != XBUFFER (echo_area_buffer[0]))
 12616         {
 12617           /* Someone switched buffers between print requests.  */
 12618           set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12619           bset_truncate_lines (current_buffer, Qnil);
 12620         }
 12621     }
 12622 }
 12623 
 12624 
 12625 /* Display an echo area message in window W.  Value is true if W's
 12626    height is changed.  If display_last_displayed_message_p,
 12627    display the message that was last displayed, otherwise
 12628    display the current message.  */
 12629 
 12630 static bool
 12631 display_echo_area (struct window *w)
 12632 {
 12633   bool no_message_p, window_height_changed_p;
 12634 
 12635   /* Temporarily disable garbage collections while displaying the echo
 12636      area.  This is done because a GC can print a message itself.
 12637      That message would modify the echo area buffer's contents while a
 12638      redisplay of the buffer is going on, and seriously confuse
 12639      redisplay.  */
 12640   specpdl_ref count = inhibit_garbage_collection ();
 12641 
 12642   /* If there is no message, we must call display_echo_area_1
 12643      nevertheless because it resizes the window.  But we will have to
 12644      reset the echo_area_buffer in question to nil at the end because
 12645      with_echo_area_buffer will set it to an empty buffer.  */
 12646   bool i = display_last_displayed_message_p;
 12647   /* According to the C standard, the integral value
 12648      of a "bool" is always 0 or 1, so this array access is safe here,
 12649      if oddly typed. */
 12650   no_message_p = NILP (echo_area_buffer[i]);
 12651 
 12652   window_height_changed_p
 12653     = with_echo_area_buffer (w, display_last_displayed_message_p,
 12654                              display_echo_area_1, w, Qnil);
 12655 
 12656   if (no_message_p)
 12657     echo_area_buffer[i] = Qnil;
 12658 
 12659   unbind_to (count, Qnil);
 12660   return window_height_changed_p;
 12661 }
 12662 
 12663 
 12664 /* Helper for display_echo_area.  Display the current buffer which
 12665    contains the current echo area message in window W, a mini-window,
 12666    a pointer to which is passed in A1.  A2 is currently not used.
 12667    Change the height of W so that all of the message is displayed.
 12668    Value is true if height of W was changed.  */
 12669 
 12670 static bool
 12671 display_echo_area_1 (void *a1, Lisp_Object a2)
 12672 {
 12673   struct window *w = a1;
 12674   Lisp_Object window;
 12675   struct text_pos start;
 12676 
 12677   /* We are about to enter redisplay without going through
 12678      redisplay_internal, so we need to forget these faces by hand
 12679      here.  */
 12680   forget_escape_and_glyphless_faces ();
 12681 
 12682   /* Do this before displaying, so that we have a large enough glyph
 12683      matrix for the display.  If we can't get enough space for the
 12684      whole text, display the last N lines.  That works by setting w->start.  */
 12685   bool window_height_changed_p = resize_mini_window (w, false);
 12686 
 12687   /* Use the starting position chosen by resize_mini_window.  */
 12688   SET_TEXT_POS_FROM_MARKER (start, w->start);
 12689 
 12690   /* Display.  */
 12691   clear_glyph_matrix (w->desired_matrix);
 12692   XSETWINDOW (window, w);
 12693   void *itdata = bidi_shelve_cache ();
 12694   try_window (window, start, 0);
 12695   bidi_unshelve_cache (itdata, false);
 12696 
 12697   return window_height_changed_p;
 12698 }
 12699 
 12700 
 12701 /* Resize the echo area window to exactly the size needed for the
 12702    currently displayed message, if there is one.  If a mini-buffer
 12703    is active, don't shrink it.  */
 12704 
 12705 void
 12706 resize_echo_area_exactly (void)
 12707 {
 12708   if (BUFFERP (echo_area_buffer[0])
 12709       && WINDOWP (echo_area_window))
 12710     {
 12711       struct window *w = XWINDOW (echo_area_window);
 12712       Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
 12713       bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
 12714                                               w, resize_exactly);
 12715       if (resized_p)
 12716         {
 12717           windows_or_buffers_changed = 42;
 12718           update_mode_lines = 30;
 12719           redisplay_internal ();
 12720         }
 12721     }
 12722 }
 12723 
 12724 
 12725 /* Callback function for with_echo_area_buffer, when used from
 12726    resize_echo_area_exactly.  A1 contains a pointer to the window to
 12727    resize, EXACTLY non-nil means resize the mini-window exactly to the
 12728    size of the text displayed.  Value is what resize_mini_window
 12729    returns.  */
 12730 
 12731 static bool
 12732 resize_mini_window_1 (void *a1, Lisp_Object exactly)
 12733 {
 12734   return resize_mini_window (a1, !NILP (exactly));
 12735 }
 12736 
 12737 
 12738 /* Resize mini-window W to fit the size of its contents.  EXACT_P
 12739    means size the window exactly to the size needed.  Otherwise, it's
 12740    only enlarged until W's buffer is empty.
 12741 
 12742    Set W->start to the right place to begin display.  If the whole
 12743    contents fit, start at the beginning.  Otherwise, start so as
 12744    to make the end of the contents appear.  This is particularly
 12745    important for y-or-n-p, but seems desirable generally.
 12746 
 12747    Value is true if the window height has been changed.  */
 12748 
 12749 bool
 12750 resize_mini_window (struct window *w, bool exact_p)
 12751 {
 12752   struct frame *f = XFRAME (w->frame);
 12753   int old_height = WINDOW_BOX_TEXT_HEIGHT (w);
 12754 
 12755   eassert (MINI_WINDOW_P (w));
 12756 
 12757   /* Don't resize windows while redisplaying a window; it would
 12758      confuse redisplay functions when the size of the window they are
 12759      displaying changes from under them.  Such a resizing can happen,
 12760      for instance, when which-func prints a long message while
 12761      we are running fontification-functions.  We're running these
 12762      functions with safe_call which binds inhibit-redisplay to t.  */
 12763   if (!NILP (Vinhibit_redisplay))
 12764     return false;
 12765 
 12766   /* By default, start display at the beginning.  */
 12767   if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12768     set_marker_both (w->start, w->contents,
 12769                      BUF_BEGV (XBUFFER (w->contents)),
 12770                      BUF_BEGV_BYTE (XBUFFER (w->contents)));
 12771 
 12772   /* Nil means don't try to resize.  */
 12773   if ((NILP (Vresize_mini_windows)
 12774        && (NILP (resize_mini_frames) || !FRAME_MINIBUF_ONLY_P (f)))
 12775       || (FRAME_X_P (f) && FRAME_OUTPUT_DATA (f) == NULL))
 12776     return false;
 12777 
 12778   if (FRAME_MINIBUF_ONLY_P (f))
 12779     {
 12780       if (!NILP (resize_mini_frames))
 12781         safe_call1 (Qwindow__resize_mini_frame, WINDOW_FRAME (w));
 12782     }
 12783   else
 12784     {
 12785       struct it it;
 12786       int unit = FRAME_LINE_HEIGHT (f);
 12787       int height, max_height;
 12788       struct text_pos start;
 12789       struct buffer *old_current_buffer = NULL;
 12790       int windows_height = FRAME_INNER_HEIGHT (f);
 12791 
 12792       if (current_buffer != XBUFFER (w->contents))
 12793         {
 12794           old_current_buffer = current_buffer;
 12795           set_buffer_internal (XBUFFER (w->contents));
 12796         }
 12797 
 12798       init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
 12799 
 12800       /* Compute the max. number of lines specified by the user.  */
 12801       if (FLOATP (Vmax_mini_window_height))
 12802         max_height = XFLOAT_DATA (Vmax_mini_window_height) * windows_height;
 12803       else if (FIXNUMP (Vmax_mini_window_height))
 12804         max_height = XFIXNUM (Vmax_mini_window_height) * unit;
 12805       else
 12806         max_height = windows_height / 4;
 12807 
 12808       /* Correct that max. height if it's bogus.  */
 12809       max_height = clip_to_bounds (unit, max_height, windows_height);
 12810 
 12811       /* Find out the height of the text in the window.  */
 12812       last_height = 0;
 12813       move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
 12814       /* If move_it_to moved to the next visible line after EOB,
 12815          account for the height of the last full line.  */
 12816       if (it.max_ascent == 0 && it.max_descent == 0)
 12817         {
 12818           height = it.current_y;
 12819           /* Don't add the last line's height if lines are truncated
 12820              and the text doesn't end in a newline.
 12821              FIXME: if the text ends in a newline from a display
 12822              property or an overlay string, they lose: the mini-window
 12823              might not show the last empty line.  */
 12824           if (!(it.line_wrap == TRUNCATE
 12825                 && it.current_x <= it.first_visible_x
 12826                 && ZV_BYTE > 1
 12827                 && FETCH_BYTE (ZV_BYTE - 1) != '\n'))
 12828             height += last_height;
 12829         }
 12830       else
 12831         height = it.current_y + it.max_ascent + it.max_descent;
 12832       height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
 12833 
 12834       /* Compute a suitable window start.  */
 12835       if (height > max_height)
 12836         {
 12837           height = (max_height / unit) * unit;
 12838           if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12839             {
 12840               init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
 12841               move_it_vertically_backward (&it, height - unit);
 12842               /* The following move is usually a no-op when the stuff
 12843                  displayed in the mini-window comes entirely from buffer
 12844                  text, but it is needed when some of it comes from overlay
 12845                  strings, especially when there's an after-string at ZV.
 12846                  This happens with some completion packages, like
 12847                  icomplete, ido-vertical, etc.  With those packages, if we
 12848                  don't force w->start to be at the beginning of a screen
 12849                  line, important parts of the stuff in the mini-window,
 12850                  such as user prompt, will be hidden from view.  */
 12851               move_it_by_lines (&it, 0);
 12852               start = it.current.pos;
 12853               /* Prevent redisplay_window from recentering, and thus from
 12854                  overriding the window-start point we computed here.  */
 12855               w->start_at_line_beg = false;
 12856               SET_MARKER_FROM_TEXT_POS (w->start, start);
 12857             }
 12858         }
 12859       else
 12860         {
 12861           SET_TEXT_POS (start, BEGV, BEGV_BYTE);
 12862           SET_MARKER_FROM_TEXT_POS (w->start, start);
 12863         }
 12864 
 12865       if (EQ (Vresize_mini_windows, Qgrow_only))
 12866         {
 12867           /* Let it grow only, until we display an empty message, in which
 12868              case the window shrinks again.  */
 12869           if (height > old_height)
 12870             grow_mini_window (w, height - old_height);
 12871           else if (height < old_height && (exact_p || BEGV == ZV))
 12872             shrink_mini_window (w);
 12873         }
 12874       else if (height != old_height)
 12875         /* Always resize to exact size needed.  */
 12876         grow_mini_window (w, height - old_height);
 12877 
 12878       if (old_current_buffer)
 12879         set_buffer_internal (old_current_buffer);
 12880     }
 12881 
 12882   return WINDOW_BOX_TEXT_HEIGHT (w) != old_height;
 12883 }
 12884 
 12885 
 12886 /* Value is the current message, a string, or nil if there is no
 12887    current message.  */
 12888 
 12889 Lisp_Object
 12890 current_message (void)
 12891 {
 12892   Lisp_Object msg;
 12893 
 12894   if (!BUFFERP (echo_area_buffer[0]))
 12895     msg = Qnil;
 12896   else
 12897     {
 12898       with_echo_area_buffer (0, 0, current_message_1, &msg, Qnil);
 12899       if (NILP (msg))
 12900         echo_area_buffer[0] = Qnil;
 12901     }
 12902 
 12903   return msg;
 12904 }
 12905 
 12906 
 12907 static bool
 12908 current_message_1 (void *a1, Lisp_Object a2)
 12909 {
 12910   Lisp_Object *msg = a1;
 12911 
 12912   if (Z > BEG)
 12913     *msg = make_buffer_string (BEG, Z, true);
 12914   else
 12915     *msg = Qnil;
 12916   return false;
 12917 }
 12918 
 12919 
 12920 /* Push the current message on Vmessage_stack for later restoration
 12921    by restore_message.  Value is true if the current message isn't
 12922    empty.  This is a relatively infrequent operation, so it's not
 12923    worth optimizing.  */
 12924 
 12925 bool
 12926 push_message (void)
 12927 {
 12928   Lisp_Object msg = current_message ();
 12929   Vmessage_stack = Fcons (msg, Vmessage_stack);
 12930   return STRINGP (msg);
 12931 }
 12932 
 12933 
 12934 /* Restore message display from the top of Vmessage_stack.  */
 12935 
 12936 void
 12937 restore_message (void)
 12938 {
 12939   eassert (CONSP (Vmessage_stack));
 12940   message3_nolog (XCAR (Vmessage_stack));
 12941 }
 12942 
 12943 
 12944 /* Handler for unwind-protect calling pop_message.  */
 12945 
 12946 void
 12947 pop_message_unwind (void)
 12948 {
 12949   /* Pop the top-most entry off Vmessage_stack.  */
 12950   eassert (CONSP (Vmessage_stack));
 12951   Vmessage_stack = XCDR (Vmessage_stack);
 12952 }
 12953 
 12954 
 12955 /* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
 12956    exits.  If the stack is not empty, we have a missing
 12957    pop_message_unwind somewhere.  */
 12958 
 12959 void
 12960 check_message_stack (void)
 12961 {
 12962   if (!NILP (Vmessage_stack))
 12963     emacs_abort ();
 12964 }
 12965 
 12966 void
 12967 clear_message_stack (void)
 12968 {
 12969   Vmessage_stack = Qnil;
 12970 }
 12971 
 12972 /* Truncate to NCHARS what will be displayed in the echo area the next
 12973    time we display it---but don't redisplay it now.  */
 12974 
 12975 void
 12976 truncate_echo_area (ptrdiff_t nchars)
 12977 {
 12978   if (nchars == 0)
 12979     echo_area_buffer[0] = Qnil;
 12980   else if (!noninteractive
 12981            && INTERACTIVE
 12982            && !NILP (echo_area_buffer[0]))
 12983     {
 12984       struct frame *sf = SELECTED_FRAME ();
 12985       /* Error messages get reported properly by cmd_error, so this must be
 12986          just an informative message; if the frame hasn't really been
 12987          initialized yet, just toss it.  */
 12988       if (sf->glyphs_initialized_p)
 12989         with_echo_area_buffer (0, 0, truncate_message_1,
 12990                                (void *) (intptr_t) nchars, Qnil);
 12991     }
 12992 }
 12993 
 12994 
 12995 /* Helper function for truncate_echo_area.  Truncate the current
 12996    message to at most NCHARS characters.  */
 12997 
 12998 static bool
 12999 truncate_message_1 (void *a1, Lisp_Object a2)
 13000 {
 13001   intptr_t nchars = (intptr_t) a1;
 13002   if (BEG + nchars < Z)
 13003     del_range (BEG + nchars, Z);
 13004   if (Z == BEG)
 13005     echo_area_buffer[0] = Qnil;
 13006   return false;
 13007 }
 13008 
 13009 extern intptr_t garbage_collection_inhibited;
 13010 
 13011 /* Set the current message to STRING.  */
 13012 
 13013 static void
 13014 set_message (Lisp_Object string)
 13015 {
 13016   Lisp_Object message = Qnil;
 13017 
 13018   eassert (STRINGP (string));
 13019 
 13020   if (FUNCTIONP (Vset_message_function)
 13021       /* FIXME: (bug#63253) We should really make the regexp engine re-entrant,
 13022          but in the mean time, let's ignore `set-message-function` when
 13023          called from `probably_quit`.  */
 13024       && !garbage_collection_inhibited)
 13025     {
 13026       specpdl_ref count = SPECPDL_INDEX ();
 13027       specbind (Qinhibit_quit, Qt);
 13028       message = safe_call1 (Vset_message_function, string);
 13029       unbind_to (count, Qnil);
 13030 
 13031       if (STRINGP (message))
 13032         {
 13033           string = message;
 13034           message = Qnil;
 13035         }
 13036     }
 13037 
 13038   if (NILP (message))
 13039     {
 13040       message_enable_multibyte = STRING_MULTIBYTE (string);
 13041 
 13042       with_echo_area_buffer (0, -1, set_message_1, 0, string);
 13043       message_buf_print = false;
 13044       help_echo_showing_p = false;
 13045     }
 13046 
 13047   if (STRINGP (Vdebug_on_message)
 13048       && STRINGP (string)
 13049       && fast_string_match (Vdebug_on_message, string) >= 0)
 13050     call_debugger (list2 (Qerror, string));
 13051 }
 13052 
 13053 
 13054 /* Helper function for set_message.  First argument is ignored and second
 13055    argument has the same meaning as for set_message.
 13056    This function is called with the echo area buffer being current.  */
 13057 
 13058 static bool
 13059 set_message_1 (void *a1, Lisp_Object string)
 13060 {
 13061   eassert (STRINGP (string));
 13062 
 13063   /* Change multibyteness of the echo buffer appropriately.  We always
 13064      set it to be multibyte, except when
 13065      unibyte-display-via-language-environment is non-nil and the
 13066      string to display is unibyte, because in that case unibyte
 13067      characters should not be displayed as octal escapes.  */
 13068   if (!message_enable_multibyte
 13069       && unibyte_display_via_language_environment
 13070       && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13071     Fset_buffer_multibyte (Qnil);
 13072   else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13073     Fset_buffer_multibyte (Qt);
 13074 
 13075   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
 13076   if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
 13077     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 13078 
 13079   /* Insert new message at BEG.  */
 13080   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 13081 
 13082   /* This function takes care of single/multibyte conversion.
 13083      We just have to ensure that the echo area buffer has the right
 13084      setting of enable_multibyte_characters.  */
 13085   insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
 13086 
 13087   return false;
 13088 }
 13089 
 13090 
 13091 /* Clear messages.  CURRENT_P means clear the current message.
 13092    LAST_DISPLAYED_P means clear the message last displayed.  */
 13093 
 13094 void
 13095 clear_message (bool current_p, bool last_displayed_p)
 13096 {
 13097   Lisp_Object preserve = Qnil;
 13098 
 13099   if (current_p)
 13100     {
 13101       if (FUNCTIONP (Vclear_message_function)
 13102           /* FIXME: (bug#63253) Same as for `set-message-function` above.  */
 13103           && !garbage_collection_inhibited)
 13104         {
 13105           specpdl_ref count = SPECPDL_INDEX ();
 13106           specbind (Qinhibit_quit, Qt);
 13107           preserve = safe_call (1, Vclear_message_function);
 13108           unbind_to (count, Qnil);
 13109         }
 13110 
 13111       if (!EQ (preserve, Qdont_clear_message))
 13112         {
 13113           echo_area_buffer[0] = Qnil;
 13114           message_cleared_p = true;
 13115         }
 13116     }
 13117 
 13118   if (last_displayed_p)
 13119     echo_area_buffer[1] = Qnil;
 13120 
 13121   message_buf_print = false;
 13122 }
 13123 
 13124 /* Clear garbaged frames.
 13125 
 13126    This function is used where the old redisplay called
 13127    redraw_garbaged_frames which in turn called redraw_frame which in
 13128    turn called clear_frame.  The call to clear_frame was a source of
 13129    flickering.  I believe a clear_frame is not necessary.  It should
 13130    suffice in the new redisplay to invalidate all current matrices,
 13131    and ensure a complete redisplay of all windows.  */
 13132 
 13133 static void
 13134 clear_garbaged_frames (void)
 13135 {
 13136   if (frame_garbaged)
 13137     {
 13138       Lisp_Object tail, frame;
 13139       struct frame *sf = SELECTED_FRAME ();
 13140 
 13141       FOR_EACH_FRAME (tail, frame)
 13142         {
 13143           struct frame *f = XFRAME (frame);
 13144 
 13145           if (FRAME_REDISPLAY_P (f) && FRAME_GARBAGED_P (f))
 13146             {
 13147               if (f->resized_p
 13148                   /* It makes no sense to redraw a non-selected TTY
 13149                      frame, since that will actually clear the
 13150                      selected frame, and might leave the selected
 13151                      frame with corrupted display, if it happens not
 13152                      to be marked garbaged.  */
 13153                   && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
 13154                 redraw_frame (f);
 13155               else
 13156                 clear_current_matrices (f);
 13157 
 13158 #ifdef HAVE_WINDOW_SYSTEM
 13159               if (FRAME_WINDOW_P (f)
 13160                   && FRAME_RIF (f)->clear_under_internal_border)
 13161                 FRAME_RIF (f)->clear_under_internal_border (f);
 13162 #endif
 13163               fset_redisplay (f);
 13164               f->garbaged = false;
 13165               f->resized_p = false;
 13166             }
 13167         }
 13168 
 13169       frame_garbaged = false;
 13170     }
 13171 }
 13172 
 13173 
 13174 /* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P, update
 13175    selected_frame.  */
 13176 
 13177 static void
 13178 echo_area_display (bool update_frame_p)
 13179 {
 13180   Lisp_Object mini_window;
 13181   struct window *w;
 13182   struct frame *f;
 13183   bool window_height_changed_p = false;
 13184   struct frame *sf = SELECTED_FRAME ();
 13185 
 13186   mini_window = FRAME_MINIBUF_WINDOW (sf);
 13187   if (NILP (mini_window))
 13188     return;
 13189 
 13190   w = XWINDOW (mini_window);
 13191   f = XFRAME (WINDOW_FRAME (w));
 13192 
 13193   /* Don't display if frame is invisible or not yet initialized.  */
 13194   if (!FRAME_REDISPLAY_P (f) || !f->glyphs_initialized_p)
 13195     return;
 13196 
 13197 #ifdef HAVE_WINDOW_SYSTEM
 13198   /* When Emacs starts, selected_frame may be the initial terminal
 13199      frame.  If we let this through, a message would be displayed on
 13200      the terminal.  */
 13201   if (FRAME_INITIAL_P (XFRAME (selected_frame)))
 13202     return;
 13203 #endif /* HAVE_WINDOW_SYSTEM */
 13204 
 13205   /* Redraw garbaged frames.  */
 13206   clear_garbaged_frames ();
 13207 
 13208   if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
 13209     {
 13210       echo_area_window = mini_window;
 13211       window_height_changed_p = display_echo_area (w);
 13212       w->must_be_updated_p = true;
 13213 
 13214       /* Update the display, unless called from redisplay_internal.
 13215          Also don't update the screen during redisplay itself.  The
 13216          update will happen at the end of redisplay, and an update
 13217          here could cause confusion.  */
 13218       if (update_frame_p && !redisplaying_p)
 13219         {
 13220           int n = 0;
 13221 
 13222           /* If the display update has been interrupted by pending
 13223              input, update mode lines in the frame.  Due to the
 13224              pending input, it might have been that redisplay hasn't
 13225              been called, so that mode lines above the echo area are
 13226              garbaged.  This looks odd, so we prevent it here.  */
 13227           if (!display_completed)
 13228             {
 13229               n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
 13230 
 13231 #ifdef HAVE_WINDOW_SYSTEM
 13232               if (FRAME_WINDOW_P (f)
 13233                   && FRAME_RIF (f)->clear_under_internal_border)
 13234                 FRAME_RIF (f)->clear_under_internal_border (f);
 13235 #endif
 13236             }
 13237 
 13238           if (window_height_changed_p
 13239               /* Don't do this if Emacs is shutting down.  Redisplay
 13240                  needs to run hooks.  */
 13241               && !NILP (Vrun_hooks))
 13242             {
 13243               /* Must update other windows.  Likewise as in other
 13244                  cases, don't let this update be interrupted by
 13245                  pending input.  */
 13246               specpdl_ref count = SPECPDL_INDEX ();
 13247               specbind (Qredisplay_dont_pause, Qt);
 13248               fset_redisplay (f);
 13249               redisplay_internal ();
 13250               unbind_to (count, Qnil);
 13251             }
 13252           else if (FRAME_WINDOW_P (f) && n == 0)
 13253             {
 13254               /* Window configuration is the same as before.
 13255                  Can do with a display update of the echo area,
 13256                  unless we displayed some mode lines.  */
 13257               update_single_window (w);
 13258               flush_frame (f);
 13259             }
 13260           else
 13261             update_frame (f, true, true);
 13262 
 13263           /* If cursor is in the echo area, make sure that the next
 13264              redisplay displays the minibuffer, so that the cursor will
 13265              be replaced with what the minibuffer wants.  */
 13266           if (cursor_in_echo_area)
 13267             wset_redisplay (XWINDOW (mini_window));
 13268         }
 13269     }
 13270   else if (!EQ (mini_window, selected_window))
 13271     wset_redisplay (XWINDOW (mini_window));
 13272 
 13273   /* Last displayed message is now the current message.  */
 13274   echo_area_buffer[1] = echo_area_buffer[0];
 13275   /* Inform read_char that we're not echoing.  */
 13276   echo_message_buffer = Qnil;
 13277 
 13278   /* Prevent redisplay optimization in redisplay_internal by resetting
 13279      this_line_start_pos.  This is done because the mini-buffer now
 13280      displays the message instead of its buffer text.  */
 13281   if (EQ (mini_window, selected_window))
 13282     CHARPOS (this_line_start_pos) = 0;
 13283 
 13284   if (window_height_changed_p)
 13285     {
 13286       fset_redisplay (f);
 13287 
 13288       /* If window configuration was changed, frames may have been
 13289          marked garbaged.  Clear them or we will experience
 13290          surprises wrt scrolling.
 13291          FIXME: How/why/when?  */
 13292       clear_garbaged_frames ();
 13293     }
 13294 }
 13295 
 13296 /* True if W's buffer was changed but not saved.  */
 13297 
 13298 static bool
 13299 window_buffer_changed (struct window *w)
 13300 {
 13301   struct buffer *b = XBUFFER (w->contents);
 13302 
 13303   eassert (BUFFER_LIVE_P (b));
 13304 
 13305   return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
 13306 }
 13307 
 13308 /* True if W has %c or %C in its mode line and mode line should be updated.  */
 13309 
 13310 static bool
 13311 mode_line_update_needed (struct window *w)
 13312 {
 13313   return (w->column_number_displayed != -1
 13314           && !(PT == w->last_point && !window_outdated (w))
 13315           && (w->column_number_displayed != current_column ()));
 13316 }
 13317 
 13318 /* True if window start of W is frozen and may not be changed during
 13319    redisplay.  */
 13320 
 13321 static bool
 13322 window_frozen_p (struct window *w)
 13323 {
 13324   if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
 13325     {
 13326       Lisp_Object window;
 13327 
 13328       XSETWINDOW (window, w);
 13329       if (MINI_WINDOW_P (w))
 13330         return false;
 13331       else if (EQ (window, selected_window))
 13332         return false;
 13333       else if (MINI_WINDOW_P (XWINDOW (selected_window))
 13334                && EQ (window, Vminibuf_scroll_window))
 13335         /* This special window can't be frozen too.  */
 13336         return false;
 13337       else
 13338         return true;
 13339     }
 13340   return false;
 13341 }
 13342 
 13343 /***********************************************************************
 13344                      Mode Lines and Frame Titles
 13345  ***********************************************************************/
 13346 
 13347 /* A buffer for constructing non-propertized mode-line strings and
 13348    frame titles in it; allocated from the heap in init_xdisp and
 13349    resized as needed in store_mode_line_noprop_char.  */
 13350 
 13351 static char *mode_line_noprop_buf;
 13352 
 13353 /* The buffer's end, and a current output position in it.  */
 13354 
 13355 static char *mode_line_noprop_buf_end;
 13356 static char *mode_line_noprop_ptr;
 13357 
 13358 #define MODE_LINE_NOPROP_LEN(start) \
 13359   ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
 13360 
 13361 static enum {
 13362   MODE_LINE_DISPLAY = 0,
 13363   MODE_LINE_TITLE,
 13364   MODE_LINE_NOPROP,
 13365   MODE_LINE_STRING
 13366 } mode_line_target;
 13367 
 13368 /* Alist that caches the results of :propertize.
 13369    Each element is (PROPERTIZED-STRING . PROPERTY-LIST).  */
 13370 static Lisp_Object mode_line_proptrans_alist;
 13371 
 13372 /* List of strings making up the mode-line.  */
 13373 static Lisp_Object mode_line_string_list;
 13374 
 13375 /* Base face property when building propertized mode line string.  */
 13376 static Lisp_Object mode_line_string_face;
 13377 static Lisp_Object mode_line_string_face_prop;
 13378 
 13379 
 13380 /* Unwind data for mode line strings */
 13381 
 13382 static Lisp_Object Vmode_line_unwind_vector;
 13383 
 13384 static Lisp_Object
 13385 format_mode_line_unwind_data (struct frame *target_frame,
 13386                               struct buffer *obuf,
 13387                               Lisp_Object owin,
 13388                               bool save_proptrans)
 13389 {
 13390   Lisp_Object vector, tmp;
 13391 
 13392   /* Reduce consing by keeping one vector in
 13393      Vwith_echo_area_save_vector.  */
 13394   vector = Vmode_line_unwind_vector;
 13395   Vmode_line_unwind_vector = Qnil;
 13396 
 13397   if (NILP (vector))
 13398     vector = make_nil_vector (12);
 13399 
 13400   ASET (vector, 0, make_fixnum (mode_line_target));
 13401   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
 13402   ASET (vector, 2, mode_line_string_list);
 13403   ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
 13404   ASET (vector, 4, mode_line_string_face);
 13405   ASET (vector, 5, mode_line_string_face_prop);
 13406 
 13407   if (obuf)
 13408     XSETBUFFER (tmp, obuf);
 13409   else
 13410     tmp = Qnil;
 13411   ASET (vector, 6, tmp);
 13412   ASET (vector, 7, owin);
 13413   if (target_frame)
 13414     {
 13415       Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
 13416       struct buffer *b = XBUFFER (buffer);
 13417       struct buffer *cb = current_buffer;
 13418 
 13419       /* Similarly to `with-selected-window', if the operation selects
 13420          a window on another frame, we must restore that frame's
 13421          selected window, and (for a tty) the top-frame.  */
 13422       ASET (vector, 8, target_frame->selected_window);
 13423       if (FRAME_TERMCAP_P (target_frame))
 13424         ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
 13425 
 13426       /* If we select a window on another frame, make sure that that
 13427          selection does not leave its buffer's point modified when
 13428          unwinding (Bug#32777).  */
 13429       ASET (vector, 10, buffer);
 13430       current_buffer = b;
 13431       ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
 13432       current_buffer = cb;
 13433     }
 13434 
 13435   return vector;
 13436 }
 13437 
 13438 static void
 13439 unwind_format_mode_line (Lisp_Object vector)
 13440 {
 13441   Lisp_Object old_window = AREF (vector, 7);
 13442   Lisp_Object target_frame_window = AREF (vector, 8);
 13443   Lisp_Object old_top_frame = AREF (vector, 9);
 13444 
 13445   mode_line_target = XFIXNUM (AREF (vector, 0));
 13446   mode_line_noprop_ptr = mode_line_noprop_buf + XFIXNUM (AREF (vector, 1));
 13447   mode_line_string_list = AREF (vector, 2);
 13448   if (! EQ (AREF (vector, 3), Qt))
 13449     mode_line_proptrans_alist = AREF (vector, 3);
 13450   mode_line_string_face = AREF (vector, 4);
 13451   mode_line_string_face_prop = AREF (vector, 5);
 13452 
 13453   /* Select window before buffer, since it may change the buffer.  */
 13454   if (WINDOW_LIVE_P (old_window))
 13455     {
 13456       /* If the operation that we are unwinding had selected a window
 13457          on a different frame, reset its frame-selected-window.  For a
 13458          text terminal, reset its top-frame if necessary.  */
 13459       if (WINDOW_LIVE_P (target_frame_window))
 13460         {
 13461           Lisp_Object frame
 13462             = WINDOW_FRAME (XWINDOW (target_frame_window));
 13463 
 13464           if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 13465             Fselect_window (target_frame_window, Qt);
 13466 
 13467           if (!NILP (old_top_frame) && !EQ (old_top_frame, frame)
 13468               /* This could've been destroyed during the formatting,
 13469                  possibly because the terminal was deleted.  */
 13470               && FRAME_LIVE_P (XFRAME (old_top_frame)))
 13471             Fselect_frame (old_top_frame, Qt);
 13472         }
 13473 
 13474       Fselect_window (old_window, Qt);
 13475 
 13476       /* Restore point of target_frame_window's buffer (Bug#32777).
 13477          But do this only after old_window has been reselected to
 13478          avoid that the window point of target_frame_window moves.  */
 13479       if (WINDOW_LIVE_P (target_frame_window))
 13480         {
 13481           Lisp_Object buffer = AREF (vector, 10);
 13482 
 13483           if (BUFFER_LIVE_P (XBUFFER (buffer)))
 13484             {
 13485               struct buffer *cb = current_buffer;
 13486 
 13487               current_buffer = XBUFFER (buffer);
 13488               set_point_from_marker (AREF (vector, 11));
 13489               ASET (vector, 11, Qnil);
 13490               current_buffer = cb;
 13491             }
 13492         }
 13493     }
 13494 
 13495   if (!NILP (AREF (vector, 6)))
 13496     {
 13497       set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
 13498       ASET (vector, 6, Qnil);
 13499     }
 13500 
 13501   Vmode_line_unwind_vector = vector;
 13502 }
 13503 
 13504 
 13505 /* Store a single character C for the frame title in mode_line_noprop_buf.
 13506    Re-allocate mode_line_noprop_buf if necessary.  */
 13507 
 13508 static void
 13509 store_mode_line_noprop_char (char c)
 13510 {
 13511   /* If output position has reached the end of the allocated buffer,
 13512      increase the buffer's size.  */
 13513   if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
 13514     {
 13515       ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
 13516       ptrdiff_t size = len;
 13517       mode_line_noprop_buf =
 13518         xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
 13519       mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 13520       mode_line_noprop_ptr = mode_line_noprop_buf + len;
 13521     }
 13522 
 13523   *mode_line_noprop_ptr++ = c;
 13524 }
 13525 
 13526 
 13527 /* Store part of a frame title in mode_line_noprop_buf, beginning at
 13528    mode_line_noprop_ptr.  STRING is the string to store.  Do not copy
 13529    characters that yield more columns than PRECISION; PRECISION <= 0
 13530    means copy the whole string.  Pad with spaces until FIELD_WIDTH
 13531    number of characters have been copied; FIELD_WIDTH <= 0 means don't
 13532    pad.  Called from display_mode_element when it is used to build a
 13533    frame title.  */
 13534 
 13535 static int
 13536 store_mode_line_noprop (const char *string, int field_width, int precision)
 13537 {
 13538   const unsigned char *str = (const unsigned char *) string;
 13539   int n = 0;
 13540   ptrdiff_t dummy, nbytes;
 13541 
 13542   /* Copy at most PRECISION chars from STR.  */
 13543   nbytes = strlen (string);
 13544   n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
 13545   while (nbytes--)
 13546     store_mode_line_noprop_char (*str++);
 13547 
 13548   /* Fill up with spaces until FIELD_WIDTH reached.  */
 13549   while (field_width > 0
 13550          && n < field_width)
 13551     {
 13552       store_mode_line_noprop_char (' ');
 13553       ++n;
 13554     }
 13555 
 13556   return n;
 13557 }
 13558 
 13559 /***********************************************************************
 13560                              Frame Titles
 13561  ***********************************************************************/
 13562 
 13563 #ifdef HAVE_WINDOW_SYSTEM
 13564 
 13565 /* Set the title of FRAME, if it has changed.  The title format is
 13566    Vicon_title_format if FRAME is iconified, otherwise it is
 13567    frame_title_format.  */
 13568 
 13569 void
 13570 gui_consider_frame_title (Lisp_Object frame)
 13571 {
 13572   struct frame *f = XFRAME (frame);
 13573   Lisp_Object format_data;
 13574 
 13575   if ((FRAME_WINDOW_P (f)
 13576        || FRAME_MINIBUF_ONLY_P (f)
 13577        || f->explicit_name)
 13578       && !FRAME_TOOLTIP_P (f))
 13579     {
 13580       /* Do we have more than one visible frame on this X display?  */
 13581       Lisp_Object tail, other_frame, fmt;
 13582       ptrdiff_t title_start;
 13583       char *title;
 13584       ptrdiff_t len;
 13585       struct it it;
 13586       specpdl_ref count = SPECPDL_INDEX ();
 13587 
 13588       FOR_EACH_FRAME (tail, other_frame)
 13589         {
 13590           struct frame *tf = XFRAME (other_frame);
 13591 
 13592           if (tf != f
 13593               && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
 13594               && !FRAME_MINIBUF_ONLY_P (tf)
 13595               && !FRAME_PARENT_FRAME (tf)
 13596               && !FRAME_TOOLTIP_P (tf)
 13597               && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
 13598             break;
 13599         }
 13600 
 13601       /* Set global variable indicating that multiple frames exist.  */
 13602       multiple_frames = CONSP (tail);
 13603 
 13604       /* select-frame calls resize_mini_window, which could resize the
 13605          mini-window and by that undo the effect of this redisplay
 13606          cycle wrt minibuffer and echo-area display.  Binding
 13607          inhibit-redisplay to t makes the call to resize_mini_window a
 13608          no-op, thus avoiding the adverse side effects.  */
 13609 
 13610       /* The following was moved before the record_unwind_protect form
 13611          below to inhibit redisplay also when restoring the selected
 13612          window/frame: This avoids that resize_mini_window sizes back
 13613          the minibuffer window of a temporarily selected frame.  See
 13614          Bug#34317.  */
 13615       specbind (Qinhibit_redisplay, Qt);
 13616 
 13617       /* Switch to the buffer of selected window of the frame.  Set up
 13618          mode_line_target so that display_mode_element will output
 13619          into mode_line_noprop_buf; then display the title.  Save the
 13620          original frame and selected window, and possibly the topmost
 13621          frame of the tty (for tty frames) into a vector; it will be
 13622          restored later.  */
 13623 
 13624       format_data = format_mode_line_unwind_data (f, current_buffer,
 13625                                                   selected_window,
 13626                                                   false);
 13627       record_unwind_protect (unwind_format_mode_line, format_data);
 13628 
 13629       Fselect_window (f->selected_window, Qt);
 13630       set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->contents));
 13631       fmt = (FRAME_ICONIFIED_P (f) && !EQ (Vicon_title_format, Qt)
 13632              ? Vicon_title_format : Vframe_title_format);
 13633 
 13634       mode_line_target = MODE_LINE_TITLE;
 13635       title_start = MODE_LINE_NOPROP_LEN (0);
 13636       init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
 13637                      NULL, DEFAULT_FACE_ID);
 13638       display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
 13639       len = MODE_LINE_NOPROP_LEN (title_start);
 13640       title = mode_line_noprop_buf + title_start;
 13641       /* Make sure that any raw bytes in the title are properly
 13642          represented by their multibyte sequences.  */
 13643       ptrdiff_t nchars = 0;
 13644       len = str_as_multibyte ((unsigned char *) title,
 13645                               mode_line_noprop_buf_end - title,
 13646                               len, &nchars);
 13647       unbind_to (count, Qnil);
 13648 
 13649       /* Set the title only if it's changed.  This avoids consing in
 13650          the common case where it hasn't.  (If it turns out that we've
 13651          already wasted too much time by walking through the list with
 13652          display_mode_element, then we might need to optimize at a
 13653          higher level than this.)  */
 13654       if ((! STRINGP (f->name)
 13655            || SBYTES (f->name) != len
 13656            || memcmp (title, SDATA (f->name), len) != 0)
 13657           && FRAME_TERMINAL (f)->implicit_set_name_hook)
 13658         {
 13659           Lisp_Object title_string = make_multibyte_string (title, nchars, len);
 13660           FRAME_TERMINAL (f)->implicit_set_name_hook (f, title_string, Qnil);
 13661         }
 13662     }
 13663 }
 13664 
 13665 #endif /* not HAVE_WINDOW_SYSTEM */
 13666 
 13667 
 13668 /***********************************************************************
 13669                               Menu Bars
 13670  ***********************************************************************/
 13671 
 13672 /* True if we will not redisplay all visible windows.  */
 13673 #define REDISPLAY_SOME_P()                              \
 13674   ((windows_or_buffers_changed == 0                     \
 13675     || windows_or_buffers_changed == REDISPLAY_SOME)    \
 13676    && (update_mode_lines == 0                           \
 13677        || update_mode_lines == REDISPLAY_SOME))
 13678 
 13679 static bool
 13680 needs_no_redisplay (struct window *w)
 13681 {
 13682   struct buffer *buffer = XBUFFER (w->contents);
 13683   struct frame *f = XFRAME (w->frame);
 13684   return (REDISPLAY_SOME_P ()
 13685           && !w->redisplay
 13686           && !w->update_mode_line
 13687           && !f->face_change
 13688           && !f->redisplay
 13689           && !buffer->text->redisplay
 13690           && window_point (w) == w->last_point);
 13691 }
 13692 
 13693 /* Prepare for redisplay by updating menu-bar item lists when
 13694    appropriate.  This can call eval.  */
 13695 
 13696 static void
 13697 prepare_menu_bars (void)
 13698 {
 13699   bool all_windows = windows_or_buffers_changed || update_mode_lines;
 13700   bool some_windows = REDISPLAY_SOME_P ();
 13701 
 13702   if (FUNCTIONP (Vpre_redisplay_function))
 13703     {
 13704       Lisp_Object windows = all_windows ? Qt : Qnil;
 13705       if (all_windows && some_windows)
 13706         {
 13707           Lisp_Object ws = window_list ();
 13708           for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
 13709             {
 13710               Lisp_Object this = XCAR (ws);
 13711               struct window *w = XWINDOW (this);
 13712               /* Cf. conditions for redisplaying a window at the
 13713                  beginning of redisplay_window.  */
 13714               if (!needs_no_redisplay (w))
 13715                 windows = Fcons (this, windows);
 13716             }
 13717         }
 13718       safe__call1 (true, Vpre_redisplay_function, windows);
 13719     }
 13720 
 13721   /* Update all frame titles based on their buffer names, etc.  We do
 13722      this before the menu bars so that the buffer-menu will show the
 13723      up-to-date frame titles.  */
 13724 #ifdef HAVE_WINDOW_SYSTEM
 13725   if (all_windows)
 13726     {
 13727       Lisp_Object tail, frame;
 13728 
 13729       FOR_EACH_FRAME (tail, frame)
 13730         {
 13731           struct frame *f = XFRAME (frame);
 13732           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13733           if (some_windows
 13734               && !f->redisplay
 13735               && !w->redisplay
 13736               && !XBUFFER (w->contents)->text->redisplay)
 13737             continue;
 13738 
 13739           if (!FRAME_TOOLTIP_P (f)
 13740               && (FRAME_ICONIFIED_P (f)
 13741                   || FRAME_VISIBLE_P (f) == 1
 13742                   /* Exclude TTY frames that are obscured because they
 13743                      are not the top frame on their console.  This is
 13744                      because gui_consider_frame_title actually switches
 13745                      to the frame, which for TTY frames means it is
 13746                      marked as garbaged, and will be completely
 13747                      redrawn on the next redisplay cycle.  This causes
 13748                      TTY frames to be completely redrawn, when there
 13749                      are more than one of them, even though nothing
 13750                      should be changed on display.  */
 13751                   || (FRAME_REDISPLAY_P (f) && FRAME_WINDOW_P (f))))
 13752             gui_consider_frame_title (frame);
 13753         }
 13754     }
 13755 #endif /* HAVE_WINDOW_SYSTEM */
 13756 
 13757   /* Update the menu bar item lists, if appropriate.  This has to be
 13758      done before any actual redisplay or generation of display lines.  */
 13759 
 13760   if (all_windows)
 13761     {
 13762       Lisp_Object tail, frame;
 13763       specpdl_ref count = SPECPDL_INDEX ();
 13764       /* True means that update_menu_bar has run its hooks
 13765          so any further calls to update_menu_bar shouldn't do so again.  */
 13766       bool menu_bar_hooks_run = false;
 13767 
 13768       record_unwind_save_match_data ();
 13769 
 13770       FOR_EACH_FRAME (tail, frame)
 13771         {
 13772           struct frame *f = XFRAME (frame);
 13773           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13774 
 13775           /* Ignore tooltip frame.  */
 13776           if (FRAME_TOOLTIP_P (f))
 13777             continue;
 13778 
 13779           if (some_windows
 13780               && !f->redisplay
 13781               && !w->redisplay
 13782               && !XBUFFER (w->contents)->text->redisplay)
 13783             continue;
 13784 
 13785           if (!FRAME_PARENT_FRAME (f))
 13786             menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
 13787 
 13788           update_tab_bar (f, false);
 13789 #ifdef HAVE_WINDOW_SYSTEM
 13790           update_tool_bar (f, false);
 13791 #endif
 13792         }
 13793 
 13794       unbind_to (count, Qnil);
 13795     }
 13796   else
 13797     {
 13798       struct frame *sf = SELECTED_FRAME ();
 13799 
 13800       if (!FRAME_PARENT_FRAME (sf))
 13801         update_menu_bar (sf, true, false);
 13802 
 13803       update_tab_bar (sf, true);
 13804 #ifdef HAVE_WINDOW_SYSTEM
 13805       update_tool_bar (sf, true);
 13806 #endif
 13807     }
 13808 }
 13809 
 13810 
 13811 /* Update the menu bar item list for frame F.  This has to be done
 13812    before we start to fill in any display lines, because it can call
 13813    eval.
 13814 
 13815    If SAVE_MATCH_DATA, we must save and restore it here.
 13816 
 13817    If HOOKS_RUN, a previous call to update_menu_bar
 13818    already ran the menu bar hooks for this redisplay, so there
 13819    is no need to run them again.  The return value is the
 13820    updated value of this flag, to pass to the next call.  */
 13821 
 13822 static bool
 13823 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
 13824 {
 13825   Lisp_Object window;
 13826   struct window *w;
 13827 
 13828   /* If called recursively during a menu update, do nothing.  This can
 13829      happen when, for instance, an activate-menubar-hook causes a
 13830      redisplay.  */
 13831   if (inhibit_menubar_update)
 13832     return hooks_run;
 13833 
 13834   window = FRAME_SELECTED_WINDOW (f);
 13835   w = XWINDOW (window);
 13836 
 13837   if (FRAME_WINDOW_P (f)
 13838       ?
 13839 #ifdef HAVE_EXT_MENU_BAR
 13840       FRAME_EXTERNAL_MENU_BAR (f)
 13841 #else
 13842       FRAME_MENU_BAR_LINES (f) > 0
 13843 #endif
 13844       : FRAME_MENU_BAR_LINES (f) > 0)
 13845     {
 13846       /* If the user has switched buffers or windows, we need to
 13847          recompute to reflect the new bindings.  But we'll
 13848          recompute when update_mode_lines is set too; that means
 13849          that people can use force-mode-line-update to request
 13850          that the menu bar be recomputed.  The adverse effect on
 13851          the rest of the redisplay algorithm is about the same as
 13852          windows_or_buffers_changed anyway.  */
 13853       if (windows_or_buffers_changed
 13854           /* This used to test w->update_mode_line, but we believe
 13855              there is no need to recompute the menu in that case.  */
 13856           || update_mode_lines
 13857           || window_buffer_changed (w))
 13858         {
 13859           struct buffer *prev = current_buffer;
 13860           specpdl_ref count = SPECPDL_INDEX ();
 13861 
 13862           specbind (Qinhibit_menubar_update, Qt);
 13863 
 13864           set_buffer_internal_1 (XBUFFER (w->contents));
 13865           if (save_match_data)
 13866             record_unwind_save_match_data ();
 13867           if (NILP (Voverriding_local_map_menu_flag))
 13868             {
 13869               specbind (Qoverriding_terminal_local_map, Qnil);
 13870               specbind (Qoverriding_local_map, Qnil);
 13871             }
 13872 
 13873           if (!hooks_run)
 13874             {
 13875               /* Run the Lucid hook.  */
 13876               safe_run_hooks (Qactivate_menubar_hook);
 13877 
 13878               /* If it has changed current-menubar from previous value,
 13879                  really recompute the menu-bar from the value.  */
 13880               safe_run_hooks (Qmenu_bar_update_hook);
 13881 
 13882               hooks_run = true;
 13883             }
 13884 
 13885           XSETFRAME (Vmenu_updating_frame, f);
 13886           fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
 13887 
 13888           /* Redisplay the menu bar in case we changed it.  */
 13889 #ifdef HAVE_EXT_MENU_BAR
 13890           if (FRAME_WINDOW_P (f))
 13891             {
 13892 #if defined (HAVE_NS)
 13893               /* All frames on Mac OS share the same menubar.  So only
 13894                  the selected frame should be allowed to set it.  */
 13895               if (f == SELECTED_FRAME ())
 13896 #endif
 13897                 set_frame_menubar (f, false);
 13898             }
 13899           else
 13900             /* On a terminal screen, the menu bar is an ordinary screen
 13901                line, and this makes it get updated.  */
 13902             w->update_mode_line = true;
 13903 #else /* ! (HAVE_EXT_MENU_BAR) */
 13904           /* In the non-toolkit version, the menu bar is an ordinary screen
 13905              line, and this makes it get updated.  */
 13906           w->update_mode_line = true;
 13907 #endif /* HAVE_EXT_MENU_BAR */
 13908 
 13909           unbind_to (count, Qnil);
 13910           set_buffer_internal_1 (prev);
 13911         }
 13912     }
 13913 
 13914   return hooks_run;
 13915 }
 13916 
 13917 
 13918 
 13919 /***********************************************************************
 13920                                Tab-bars
 13921  ***********************************************************************/
 13922 
 13923 /* Restore WINDOW as the selected window and its frame as the selected
 13924    frame.  If WINDOW is dead but the selected frame is live, make the
 13925    latter's selected window the selected window.  If both, WINDOW and
 13926    the selected frame, are dead, assign selected frame and window from
 13927    some arbitrary live frame.  Abort if no such frame can be found.  */
 13928 static void
 13929 restore_selected_window (Lisp_Object window)
 13930 {
 13931   if (WINDOW_LIVE_P (window))
 13932     /* If WINDOW is live, make it the selected window and its frame's
 13933        selected window and set the selected frame to its frame.  */
 13934     {
 13935       selected_window = window;
 13936       selected_frame = XWINDOW (window)->frame;
 13937       FRAME_SELECTED_WINDOW (XFRAME (selected_frame)) = window;
 13938     }
 13939   else if (FRAMEP (selected_frame) && FRAME_LIVE_P (XFRAME (selected_frame)))
 13940     /* If WINDOW is dead but the selected frame is still live, make the
 13941        latter's selected window the selected one.  */
 13942     selected_window = FRAME_SELECTED_WINDOW (XFRAME (selected_frame));
 13943   else
 13944     /* If WINDOW and the selected frame are dead, choose some live,
 13945        non-child and non-tooltip frame as the new selected frame and
 13946        make its selected window the selected window.  */
 13947     {
 13948       Lisp_Object tail;
 13949       Lisp_Object frame UNINIT;
 13950 
 13951       FOR_EACH_FRAME (tail, frame)
 13952         {
 13953           struct frame *f = XFRAME (frame);
 13954 
 13955           if (!FRAME_PARENT_FRAME (f) && !FRAME_TOOLTIP_P (f))
 13956             {
 13957               selected_frame = frame;
 13958               selected_window = FRAME_SELECTED_WINDOW (f);
 13959 
 13960               return;
 13961             }
 13962         }
 13963 
 13964       /* Abort if we cannot find a live frame.  */
 13965       emacs_abort ();
 13966     }
 13967 }
 13968 
 13969 /* Restore WINDOW, if live, as its frame's selected window.  */
 13970 static void
 13971 restore_frame_selected_window (Lisp_Object window)
 13972 {
 13973   if (WINDOW_LIVE_P (window))
 13974     /* If WINDOW is live, make it its frame's selected window.  If that
 13975        frame is the selected frame, make WINDOW the selected window as
 13976        well.  */
 13977     {
 13978       Lisp_Object frame = XWINDOW (window)->frame;
 13979 
 13980       FRAME_SELECTED_WINDOW (XFRAME (frame)) = window;
 13981       if (EQ (frame, selected_frame))
 13982         selected_window = window;
 13983     }
 13984 }
 13985 
 13986 /* Update the tab-bar item list for frame F.  This has to be done
 13987    before we start to fill in any display lines.  Called from
 13988    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 13989    and restore it here.  */
 13990 
 13991 static void
 13992 update_tab_bar (struct frame *f, bool save_match_data)
 13993 {
 13994   bool do_update = false;
 13995 
 13996 #ifdef HAVE_WINDOW_SYSTEM
 13997   if (FRAME_WINDOW_P (f) && WINDOWP (f->tab_bar_window)) {
 13998     if (WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)) > 0)
 13999       do_update = true;
 14000   }
 14001   else
 14002 #endif
 14003   if (FRAME_TAB_BAR_LINES (f) > 0)
 14004     do_update = true;
 14005 
 14006   if (do_update)
 14007     {
 14008       Lisp_Object window;
 14009       struct window *w;
 14010 
 14011       window = FRAME_SELECTED_WINDOW (f);
 14012       w = XWINDOW (window);
 14013 
 14014       /* If the user has switched buffers or windows, we need to
 14015          recompute to reflect the new bindings.  But we'll
 14016          recompute when update_mode_lines is set too; that means
 14017          that people can use force-mode-line-update to request
 14018          that the menu bar be recomputed.  The adverse effect on
 14019          the rest of the redisplay algorithm is about the same as
 14020          windows_or_buffers_changed anyway.  */
 14021       if (windows_or_buffers_changed
 14022           || w->update_mode_line
 14023           || update_mode_lines
 14024           || window_buffer_changed (w))
 14025         {
 14026           struct buffer *prev = current_buffer;
 14027           specpdl_ref count = SPECPDL_INDEX ();
 14028           Lisp_Object new_tab_bar;
 14029           int new_n_tab_bar;
 14030 
 14031           /* Set current_buffer to the buffer of the selected
 14032              window of the frame, so that we get the right local
 14033              keymaps.  */
 14034           set_buffer_internal_1 (XBUFFER (w->contents));
 14035 
 14036           /* Save match data, if we must.  */
 14037           if (save_match_data)
 14038             record_unwind_save_match_data ();
 14039 
 14040           /* Make sure that we don't accidentally use bogus keymaps.  */
 14041           if (NILP (Voverriding_local_map_menu_flag))
 14042             {
 14043               specbind (Qoverriding_terminal_local_map, Qnil);
 14044               specbind (Qoverriding_local_map, Qnil);
 14045             }
 14046 
 14047           /* We must temporarily set the selected frame to this frame
 14048              before calling tab_bar_items, because the calculation of
 14049              the tab-bar keymap uses the selected frame (see
 14050              `tab-bar-make-keymap' in tab-bar.el).  */
 14051           eassert (EQ (selected_window,
 14052                        /* Since we only explicitly preserve selected_frame,
 14053                           check that selected_window would be redundant.  */
 14054                        XFRAME (selected_frame)->selected_window));
 14055 #ifdef HAVE_WINDOW_SYSTEM
 14056           Lisp_Object frame;
 14057           record_unwind_protect (restore_selected_window, selected_window);
 14058           XSETFRAME (frame, f);
 14059           selected_frame = frame;
 14060           selected_window = FRAME_SELECTED_WINDOW (f);
 14061 #endif
 14062 
 14063           /* Build desired tab-bar items from keymaps.  */
 14064           new_tab_bar
 14065             = tab_bar_items (Fcopy_sequence (f->tab_bar_items),
 14066                               &new_n_tab_bar);
 14067 
 14068           /* Redisplay the tab-bar if we changed it.  */
 14069           if (new_n_tab_bar != f->n_tab_bar_items
 14070               || NILP (Fequal (new_tab_bar, f->tab_bar_items)))
 14071             {
 14072               /* Redisplay that happens asynchronously due to an expose event
 14073                  may access f->tab_bar_items.  Make sure we update both
 14074                  variables within BLOCK_INPUT so no such event interrupts.  */
 14075               block_input ();
 14076               fset_tab_bar_items (f, new_tab_bar);
 14077               f->n_tab_bar_items = new_n_tab_bar;
 14078               w->update_mode_line = true;
 14079               unblock_input ();
 14080             }
 14081 
 14082           unbind_to (count, Qnil);
 14083           set_buffer_internal_1 (prev);
 14084         }
 14085     }
 14086 }
 14087 
 14088 /* Redisplay the tab bar in the frame for window W.
 14089 
 14090    The tab bar of X frames that don't have X toolkit support is
 14091    displayed in a special window W->frame->tab_bar_window.
 14092 
 14093    The tab bar of terminal frames is treated specially as far as
 14094    glyph matrices are concerned.  Tab bar lines are not part of
 14095    windows, so the update is done directly on the frame matrix rows
 14096    for the tab bar.  */
 14097 
 14098 static void
 14099 display_tab_bar (struct window *w)
 14100 {
 14101   struct frame *f = XFRAME (WINDOW_FRAME (w));
 14102   struct it it;
 14103   Lisp_Object items;
 14104   int i;
 14105 
 14106   /* Don't do all this for graphical frames.  */
 14107 #ifdef HAVE_NTGUI
 14108   if (FRAME_W32_P (f))
 14109     return;
 14110 #endif
 14111 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14112   if (FRAME_X_P (f))
 14113     return;
 14114 #endif
 14115 
 14116 #ifdef HAVE_NS
 14117   if (FRAME_NS_P (f))
 14118     return;
 14119 #endif /* HAVE_NS */
 14120 
 14121 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14122   eassert (!FRAME_WINDOW_P (f));
 14123   init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14124                  + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14125                  TAB_BAR_FACE_ID);
 14126   it.first_visible_x = 0;
 14127   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14128 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 14129   if (FRAME_WINDOW_P (f))
 14130     {
 14131       /* Tab bar lines are displayed in the desired matrix of the
 14132          dummy window tab_bar_window.  */
 14133       struct window *tab_w;
 14134       tab_w = XWINDOW (f->tab_bar_window);
 14135       init_iterator (&it, tab_w, -1, -1, tab_w->desired_matrix->rows,
 14136                      TAB_BAR_FACE_ID);
 14137       it.first_visible_x = 0;
 14138       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14139     }
 14140   else
 14141 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 14142     {
 14143       /* This is a TTY frame, i.e. character hpos/vpos are used as
 14144          pixel x/y.  */
 14145       init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14146                      + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14147                      TAB_BAR_FACE_ID);
 14148       it.first_visible_x = 0;
 14149       it.last_visible_x = FRAME_COLS (f);
 14150     }
 14151 
 14152   /* FIXME: This should be controlled by a user option.  See the
 14153      comments in redisplay_tool_bar and display_mode_line about
 14154      this.  */
 14155   it.paragraph_embedding = L2R;
 14156 
 14157   /* Clear all rows of the tab bar.  */
 14158   for (i = 0; i < FRAME_TAB_BAR_LINES (f); ++i)
 14159     {
 14160       struct glyph_row *row = it.glyph_row + i;
 14161       clear_glyph_row (row);
 14162       row->enabled_p = true;
 14163       row->full_width_p = true;
 14164       row->reversed_p = false;
 14165     }
 14166 
 14167   /* Display all items of the tab bar.  */
 14168   items = it.f->tab_bar_items;
 14169   int j;
 14170   for (i = 0, j = 0; i < it.f->n_tab_bar_items; ++i, j += TAB_BAR_ITEM_NSLOTS)
 14171     {
 14172       Lisp_Object string = AREF (items, j + TAB_BAR_ITEM_CAPTION);
 14173 
 14174       /* Stop at nil string.  */
 14175       if (NILP (string))
 14176         break;
 14177 
 14178       if (it.current_x < it.last_visible_x)
 14179         display_string (NULL, string, Qnil, 0, 0, &it,
 14180                         SCHARS (string), 0, 0, STRING_MULTIBYTE (string));
 14181     }
 14182 
 14183   /* Fill out the line with spaces.  */
 14184   if (it.current_x < it.last_visible_x)
 14185     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 14186 
 14187   /* Compute the total height of the lines.  */
 14188   compute_line_metrics (&it);
 14189 }
 14190 
 14191 #ifdef HAVE_WINDOW_SYSTEM
 14192 
 14193 /* Set F->desired_tab_bar_string to a Lisp string representing frame
 14194    F's desired tab-bar contents.  F->tab_bar_items must have
 14195    been set up previously by calling prepare_menu_bars.  */
 14196 
 14197 static void
 14198 build_desired_tab_bar_string (struct frame *f)
 14199 {
 14200   int i;
 14201   Lisp_Object caption;
 14202 
 14203   caption = Qnil;
 14204 
 14205   /* Prepare F->desired_tab_bar_string.  Make a new string.  */
 14206   fset_desired_tab_bar_string (f, build_string (""));
 14207 
 14208   /* Put a `display' property on the string for the captions to display,
 14209      put a `menu_item' property on tab-bar items with a value that
 14210      is the index of the item in F's tab-bar item vector.  */
 14211   for (i = 0; i < f->n_tab_bar_items; ++i)
 14212     {
 14213 #define PROP(IDX) \
 14214   AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX))
 14215 
 14216       caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION));
 14217 
 14218       /* Put a `display' text property on the string for the caption to
 14219          display.  Put a `menu-item' property on the string that gives
 14220          the start of this item's properties in the tab-bar items
 14221          vector.  */
 14222       AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS));
 14223 
 14224       Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14225                             props, caption);
 14226 
 14227       f->desired_tab_bar_string =
 14228         concat2 (f->desired_tab_bar_string, caption);
 14229 
 14230 #undef PROP
 14231     }
 14232 }
 14233 
 14234 
 14235 /* Display one line of the tab-bar of frame IT->f.
 14236 
 14237    HEIGHT specifies the desired height of the tab-bar line.
 14238    If the actual height of the glyph row is less than HEIGHT, the
 14239    row's height is increased to HEIGHT, and the icons are centered
 14240    vertically in the new height.
 14241 
 14242    If HEIGHT is -1, we are counting needed tab-bar lines, so don't
 14243    count a final empty row in case the tab-bar width exactly matches
 14244    the window width.
 14245 */
 14246 
 14247 static void
 14248 display_tab_bar_line (struct it *it, int height)
 14249 {
 14250   struct glyph_row *row = it->glyph_row;
 14251   int max_x = it->last_visible_x;
 14252   struct glyph *last;
 14253 
 14254   /* Don't extend on a previously drawn tab bar items (Bug#16058).  */
 14255   clear_glyph_row (row);
 14256   row->enabled_p = true;
 14257   row->y = it->current_y;
 14258 
 14259   /* Note that this isn't made use of if the face hasn't a box,
 14260      so there's no need to check the face here.  */
 14261   it->start_of_box_run_p = true;
 14262 
 14263   bool enough = false;
 14264   while (it->current_x < max_x)
 14265     {
 14266       int x, n_glyphs_before, i, nglyphs;
 14267       struct it it_before;
 14268 
 14269       /* Get the next display element.  */
 14270       if (!get_next_display_element (it))
 14271         {
 14272           /* Don't count empty row if we are counting needed tab-bar lines.  */
 14273           if (height < 0 && !it->hpos)
 14274             return;
 14275           break;
 14276         }
 14277 
 14278       /* Produce glyphs.  */
 14279       n_glyphs_before = row->used[TEXT_AREA];
 14280       it_before = *it;
 14281 
 14282       PRODUCE_GLYPHS (it);
 14283 
 14284       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 14285       i = 0;
 14286       x = it_before.current_x;
 14287       while (i < nglyphs)
 14288         {
 14289           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 14290 
 14291           if (x + glyph->pixel_width > max_x)
 14292             {
 14293               /* Glyph doesn't fit on line.  Backtrack.  */
 14294               row->used[TEXT_AREA] = n_glyphs_before;
 14295               *it = it_before;
 14296               /* If this is the only glyph on this line, it will never fit on the
 14297                  tab-bar, so skip it.  But ensure there is at least one glyph,
 14298                  so we don't accidentally disable the tab-bar.  */
 14299               if (n_glyphs_before == 0
 14300                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 14301                 break;
 14302               goto out;
 14303             }
 14304 
 14305           ++it->hpos;
 14306           x += glyph->pixel_width;
 14307           ++i;
 14308         }
 14309 
 14310       enough = ITERATOR_AT_END_OF_LINE_P (it);
 14311       set_iterator_to_next (it, true);
 14312 
 14313       /* Stop at line end.  */
 14314       if (enough)
 14315         break;
 14316     }
 14317 
 14318  out:;
 14319 
 14320   row->displays_text_p = row->used[TEXT_AREA] != 0;
 14321 
 14322   /* Use default face for the border below the tab bar.
 14323 
 14324      FIXME: When auto-resize-tab-bars is grow-only, there is
 14325      no additional border below the possibly empty tab-bar lines.
 14326      So to make the extra empty lines look "normal", we have to
 14327      use the tab-bar face for the border too.  */
 14328   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14329       && !EQ (Vauto_resize_tab_bars, Qgrow_only))
 14330     it->face_id = DEFAULT_FACE_ID;
 14331 
 14332   extend_face_to_end_of_line (it);
 14333   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 14334   last->right_box_line_p = true;
 14335   if (last == row->glyphs[TEXT_AREA])
 14336     last->left_box_line_p = true;
 14337 
 14338   /* Make line the desired height and center it vertically.  */
 14339   if ((height -= it->max_ascent + it->max_descent) > 0)
 14340     {
 14341       /* Don't add more than one line height.  */
 14342       height %= FRAME_LINE_HEIGHT (it->f);
 14343       it->max_ascent += height / 2;
 14344       it->max_descent += (height + 1) / 2;
 14345     }
 14346 
 14347   compute_line_metrics (it);
 14348 
 14349   /* If line is empty, make it occupy the rest of the tab-bar.  */
 14350   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 14351     {
 14352       row->height = row->phys_height = it->last_visible_y - row->y;
 14353       row->visible_height = row->height;
 14354       row->ascent = row->phys_ascent = 0;
 14355       row->extra_line_spacing = 0;
 14356     }
 14357 
 14358   row->full_width_p = true;
 14359   row->continued_p = false;
 14360   row->truncated_on_left_p = false;
 14361   row->truncated_on_right_p = false;
 14362 
 14363   it->current_x = it->hpos = 0;
 14364   it->current_y += row->height;
 14365   ++it->vpos;
 14366   ++it->glyph_row;
 14367 }
 14368 
 14369 
 14370 /* Value is the number of pixels needed to make all tab-bar items of
 14371    frame F visible.  The actual number of glyph rows needed is
 14372    returned in *N_ROWS if non-NULL.  */
 14373 static int
 14374 tab_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 14375 {
 14376   struct window *w = XWINDOW (f->tab_bar_window);
 14377   struct it it;
 14378   /* tab_bar_height is called from redisplay_tab_bar after building
 14379      the desired matrix, so use (unused) mode-line row as temporary row to
 14380      avoid destroying the first tab-bar row.  */
 14381   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 14382 
 14383   /* Initialize an iterator for iteration over
 14384      F->desired_tab_bar_string in the tab-bar window of frame F.  */
 14385   init_iterator (&it, w, -1, -1, temp_row, TAB_BAR_FACE_ID);
 14386   temp_row->reversed_p = false;
 14387   it.first_visible_x = 0;
 14388   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14389   reseat_to_string (&it, NULL, f->desired_tab_bar_string,
 14390                     0, 0, 0, STRING_MULTIBYTE (f->desired_tab_bar_string));
 14391   it.paragraph_embedding = L2R;
 14392 
 14393   clear_glyph_row (temp_row);
 14394   while (!ITERATOR_AT_END_P (&it))
 14395     {
 14396       it.glyph_row = temp_row;
 14397       display_tab_bar_line (&it, -1);
 14398     }
 14399   clear_glyph_row (temp_row);
 14400 
 14401   /* f->n_tab_bar_rows == 0 means "unknown"; -1 means no tab-bar.  */
 14402   if (n_rows)
 14403     *n_rows = it.vpos > 0 ? it.vpos : -1;
 14404 
 14405   if (pixelwise)
 14406     return it.current_y;
 14407   else
 14408     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 14409 }
 14410 
 14411 DEFUN ("tab-bar-height", Ftab_bar_height, Stab_bar_height,
 14412        0, 2, 0,
 14413        doc: /* Return the number of lines occupied by the tab bar of FRAME.
 14414 If FRAME is nil or omitted, use the selected frame.  Optional argument
 14415 PIXELWISE non-nil means return the height of the tab bar in pixels.  */)
 14416   (Lisp_Object frame, Lisp_Object pixelwise)
 14417 {
 14418   int height = 0;
 14419 
 14420   struct frame *f = decode_any_frame (frame);
 14421 
 14422   if (WINDOWP (f->tab_bar_window)
 14423       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tab_bar_window)) > 0)
 14424     {
 14425       update_tab_bar (f, true);
 14426       if (f->n_tab_bar_items)
 14427         {
 14428           build_desired_tab_bar_string (f);
 14429           height = tab_bar_height (f, NULL, !NILP (pixelwise));
 14430         }
 14431     }
 14432 
 14433   return make_fixnum (height);
 14434 }
 14435 
 14436 
 14437 /* Display the tab-bar of frame F.  Value is true if tab-bar's
 14438    height should be changed.  */
 14439 static bool
 14440 redisplay_tab_bar (struct frame *f)
 14441 {
 14442   struct window *w;
 14443   struct it it;
 14444   struct glyph_row *row;
 14445 
 14446   f->tab_bar_redisplayed = true;
 14447 
 14448   /* If frame hasn't a tab-bar window or if it is zero-height, don't
 14449      do anything.  This means you must start with tab-bar-lines
 14450      non-zero to get the auto-sizing effect.  Or in other words, you
 14451      can turn off tab-bars by specifying tab-bar-lines zero.  */
 14452   if (!WINDOWP (f->tab_bar_window)
 14453       || (w = XWINDOW (f->tab_bar_window),
 14454           WINDOW_TOTAL_LINES (w) == 0))
 14455     {
 14456       /* Even if we do not display a tab bar initially, still pretend
 14457          that we have resized it.  This avoids that a later activation
 14458          of the tab bar resizes the frame, despite of the fact that the
 14459          setting of 'frame-inhibit-implied-resize' should inhibit it
 14460          (Bug#52986).  */
 14461       f->tab_bar_resized = true;
 14462 
 14463       return false;
 14464     }
 14465 
 14466   /* Build a string that represents the contents of the tab-bar.  */
 14467   build_desired_tab_bar_string (f);
 14468 
 14469   int new_nrows;
 14470   int new_height = tab_bar_height (f, &new_nrows, true);
 14471 
 14472   if (f->n_tab_bar_rows == 0)
 14473     {
 14474       f->n_tab_bar_rows = new_nrows;
 14475       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 14476         frame_default_tab_bar_height = new_height;
 14477     }
 14478 
 14479   /* If new_height or new_nrows indicate that we need to enlarge or
 14480      shrink the tab-bar window, we can return right away.  */
 14481   if (new_nrows > f->n_tab_bar_rows
 14482       || (EQ (Vauto_resize_tab_bars, Qgrow_only)
 14483           && !f->minimize_tab_bar_window_p
 14484           && new_height > WINDOW_PIXEL_HEIGHT (w))
 14485       || (! EQ (Vauto_resize_tab_bars, Qgrow_only)
 14486           && new_height < WINDOW_PIXEL_HEIGHT (w)))
 14487     {
 14488       if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14489         FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14490       if (new_nrows != f->n_tab_bar_rows)
 14491         f->n_tab_bar_rows = new_nrows;
 14492       clear_glyph_matrix (w->desired_matrix);
 14493       f->fonts_changed = true;
 14494       return true;
 14495     }
 14496 
 14497   /* Set up an iterator for the tab-bar window.  */
 14498   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TAB_BAR_FACE_ID);
 14499   it.first_visible_x = 0;
 14500   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14501   row = it.glyph_row;
 14502   row->reversed_p = false;
 14503   reseat_to_string (&it, NULL, f->desired_tab_bar_string, 0, 0, 0,
 14504                     STRING_MULTIBYTE (f->desired_tab_bar_string));
 14505   /* FIXME: This should be controlled by a user option.  But it
 14506      doesn't make sense to have an R2L tab bar if the menu bar cannot
 14507      be drawn also R2L, and making the menu bar R2L is tricky due
 14508      tabkit-specific code that implements it.  If an R2L tab bar is
 14509      ever supported, display_tab_bar_line should also be augmented to
 14510      call unproduce_glyphs like display_line and display_string
 14511      do.  */
 14512   it.paragraph_embedding = L2R;
 14513 
 14514   /* Display as many lines as needed to display all tab-bar items.  */
 14515 
 14516   if (f->n_tab_bar_rows > 0)
 14517     {
 14518       int border, rows, height, extra;
 14519 
 14520       if (TYPE_RANGED_FIXNUMP (int, Vtab_bar_border))
 14521         border = XFIXNUM (Vtab_bar_border);
 14522       else if (EQ (Vtab_bar_border, Qinternal_border_width))
 14523         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 14524       else if (EQ (Vtab_bar_border, Qborder_width))
 14525         border = f->border_width;
 14526       else
 14527         border = 0;
 14528       if (border < 0)
 14529         border = 0;
 14530 
 14531       rows = f->n_tab_bar_rows;
 14532       height = max (1, (it.last_visible_y - border) / rows);
 14533       extra = it.last_visible_y - border - height * rows;
 14534 
 14535       while (it.current_y < it.last_visible_y)
 14536         {
 14537           int h = 0;
 14538           if (extra > 0 && rows-- > 0)
 14539             {
 14540               h = (extra + rows - 1) / rows;
 14541               extra -= h;
 14542             }
 14543           display_tab_bar_line (&it, height + h);
 14544         }
 14545     }
 14546   else
 14547     {
 14548       while (it.current_y < it.last_visible_y)
 14549         display_tab_bar_line (&it, 0);
 14550     }
 14551 
 14552   /* It doesn't make much sense to try scrolling in the tab-bar
 14553      window, so don't do it.  */
 14554   w->desired_matrix->no_scrolling_p = true;
 14555   w->must_be_updated_p = true;
 14556 
 14557   if (!NILP (Vauto_resize_tab_bars))
 14558     {
 14559       bool change_height_p = false;
 14560 
 14561       /* If we couldn't display everything, change the tab-bar's
 14562          height if there is room for more.  */
 14563       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 14564         change_height_p = true;
 14565 
 14566       /* We subtract 1 because display_tab_bar_line advances the
 14567          glyph_row pointer before returning to its caller.  We want to
 14568          examine the last glyph row produced by
 14569          display_tab_bar_line.  */
 14570       row = it.glyph_row - 1;
 14571 
 14572       /* If there are blank lines at the end, except for a partially
 14573          visible blank line at the end that is smaller than
 14574          FRAME_LINE_HEIGHT, change the tab-bar's height.  */
 14575       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14576           && row->height >= FRAME_LINE_HEIGHT (f))
 14577         change_height_p = true;
 14578 
 14579       /* If row displays tab-bar items, but is partially visible,
 14580          change the tab-bar's height.  */
 14581       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14582           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 14583         change_height_p = true;
 14584 
 14585       /* Resize windows as needed by changing the `tab-bar-lines'
 14586          frame parameter.  */
 14587       if (change_height_p)
 14588         {
 14589           int nrows;
 14590           int new_height = tab_bar_height (f, &nrows, true);
 14591 
 14592           change_height_p = ((EQ (Vauto_resize_tab_bars, Qgrow_only)
 14593                               && !f->minimize_tab_bar_window_p)
 14594                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 14595                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 14596           f->minimize_tab_bar_window_p = false;
 14597 
 14598           if (change_height_p)
 14599             {
 14600               if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14601                 FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14602               frame_default_tab_bar_height = new_height;
 14603               clear_glyph_matrix (w->desired_matrix);
 14604               f->n_tab_bar_rows = nrows;
 14605               f->fonts_changed = true;
 14606 
 14607               return true;
 14608             }
 14609         }
 14610     }
 14611 
 14612   f->minimize_tab_bar_window_p = false;
 14613   return false;
 14614 }
 14615 
 14616 /* Get information about the tab-bar item which is displayed in GLYPH
 14617    on frame F.  Return in *PROP_IDX the index where tab-bar item
 14618    properties start in F->tab_bar_items.  Return in CLOSE_P an
 14619    indication whether the click was on the close-tab icon of the tab.
 14620    Value is false if GLYPH doesn't display a tab-bar item.  */
 14621 
 14622 static bool
 14623 tab_bar_item_info (struct frame *f, struct glyph *glyph,
 14624                    int *prop_idx, bool *close_p)
 14625 {
 14626   Lisp_Object prop;
 14627   ptrdiff_t charpos;
 14628 
 14629   /* This function can be called asynchronously, which means we must
 14630      exclude any possibility that Fget_text_property signals an
 14631      error.  */
 14632   charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos);
 14633   charpos = max (0, charpos);
 14634 
 14635   /* Get the text property `menu-item' at pos. The value of that
 14636      property is the start index of this item's properties in
 14637      F->tab_bar_items.  */
 14638   prop = Fget_text_property (make_fixnum (charpos),
 14639                              Qmenu_item, f->current_tab_bar_string);
 14640   if (! FIXNUMP (prop))
 14641     return false;
 14642 
 14643   *prop_idx = XFIXNUM (prop);
 14644 
 14645   if (close_p)
 14646     *close_p = !NILP (Fget_text_property (make_fixnum (charpos),
 14647                                           Qclose_tab,
 14648                                           f->current_tab_bar_string));
 14649 
 14650   return true;
 14651 }
 14652 
 14653 
 14654 /* Get information about the tab-bar item at position X/Y on frame F's
 14655    tab bar window.
 14656 
 14657    Set *GLYPH to a pointer to the glyph of the tab-bar item in the
 14658    current matrix of the tab-bar window of F, or NULL if not on a
 14659    tab-bar item.  Return in *PROP_IDX the index of the tab-bar item in
 14660    F->tab_bar_items.
 14661 
 14662    Place the window-relative vpos of Y in *VPOS, and the
 14663    window-relative hpos of X in *HPOS.  If CLOSE_P, set it to whether
 14664    or not the tab bar item represents a button that should close a
 14665    tab.
 14666 
 14667    Value is
 14668 
 14669    -1   if X/Y is not on a tab-bar item
 14670    0    if X/Y is on the same item that was highlighted before.
 14671    1    otherwise.  */
 14672 
 14673 static int
 14674 get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 14675                   int *hpos, int *vpos, int *prop_idx, bool *close_p)
 14676 {
 14677   struct window *w = XWINDOW (f->tab_bar_window);
 14678   int area;
 14679 
 14680   /* Find the glyph under X/Y.  */
 14681   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 14682   if (*glyph == NULL)
 14683     return -1;
 14684 
 14685   /* Get the start of this tab-bar item's properties in
 14686      f->tab_bar_items.  */
 14687   if (!tab_bar_item_info (f, *glyph, prop_idx, close_p))
 14688     return -1;
 14689 
 14690   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14691 }
 14692 
 14693 /* EXPORT:
 14694 
 14695    Like `get_tab_bar_item'.  However, don't return anything for GLYPH,
 14696    HPOS, or VPOS, and treat X and Y as relative to F itself, as
 14697    opposed to its tab bar window.  */
 14698 
 14699 int
 14700 get_tab_bar_item_kbd (struct frame *f, int x, int y, int *prop_idx,
 14701                       bool *close_p)
 14702 {
 14703   struct window *w;
 14704   int area, vpos, hpos;
 14705   struct glyph *glyph;
 14706 
 14707   w = XWINDOW (f->tab_bar_window);
 14708 
 14709   /* Convert X and Y to window coordinates.  */
 14710   frame_to_window_pixel_xy (w, &x, &y);
 14711 
 14712   /* Find the glyph under X/Y.  */
 14713   glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, 0,
 14714                             0, &area);
 14715   if (glyph == NULL)
 14716     return -1;
 14717 
 14718   /* Get the start of this tab-bar item's properties in
 14719      f->tab_bar_items.  */
 14720   if (!tab_bar_item_info (f, glyph, prop_idx, close_p))
 14721     return -1;
 14722 
 14723   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14724 }
 14725 
 14726 /* EXPORT:
 14727    Handle mouse button event on the tab-bar of frame F, at
 14728    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 14729    false for button release.  MODIFIERS is event modifiers for button
 14730    release.  */
 14731 
 14732 Lisp_Object
 14733 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14734                       int modifiers)
 14735 {
 14736   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14737   struct window *w = XWINDOW (f->tab_bar_window);
 14738   int hpos, vpos, prop_idx;
 14739   bool close_p;
 14740   struct glyph *glyph;
 14741   Lisp_Object enabled_p;
 14742   int ts;
 14743 
 14744   frame_to_window_pixel_xy (w, &x, &y);
 14745   ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14746   if (ts == -1)
 14747     return Fcons (Qtab_bar, Qnil);
 14748 
 14749   /* If item is disabled, do nothing.  */
 14750   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14751   if (NILP (enabled_p))
 14752     return Qnil;
 14753 
 14754   if (down_p)
 14755     {
 14756       /* Show the clicked button in pressed state.  */
 14757       if (!NILP (Vmouse_highlight))
 14758         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 14759       f->last_tab_bar_item = prop_idx; /* record the pressed tab */
 14760     }
 14761   else
 14762     {
 14763       /* Show item in released state.  */
 14764       if (!NILP (Vmouse_highlight))
 14765         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 14766       f->last_tab_bar_item = -1;
 14767     }
 14768 
 14769   Lisp_Object caption =
 14770     Fcopy_sequence (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION));
 14771 
 14772   AUTO_LIST2 (props, Qmenu_item,
 14773               list3 (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_KEY),
 14774                      AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_BINDING),
 14775                      close_p ? Qt : Qnil));
 14776 
 14777   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14778                         props, caption);
 14779 
 14780   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14781 }
 14782 
 14783 
 14784 /* Possibly highlight a tab-bar item on frame F when mouse moves to
 14785    tab-bar window-relative coordinates X/Y.  Called from
 14786    note_mouse_highlight.  */
 14787 
 14788 static void
 14789 note_tab_bar_highlight (struct frame *f, int x, int y)
 14790 {
 14791   Lisp_Object window = f->tab_bar_window;
 14792   struct window *w = XWINDOW (window);
 14793   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14794   int hpos, vpos;
 14795   struct glyph *glyph;
 14796   struct glyph_row *row;
 14797   int i;
 14798   Lisp_Object enabled_p;
 14799   int prop_idx;
 14800   bool close_p;
 14801   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 14802   int rc;
 14803 
 14804   /* Function note_mouse_highlight is called with negative X/Y
 14805      values when mouse moves outside of the frame.  */
 14806   if (x <= 0 || y <= 0)
 14807     {
 14808       clear_mouse_face (hlinfo);
 14809       return;
 14810     }
 14811 
 14812   rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14813   if (rc < 0)
 14814     {
 14815       /* Not on tab-bar item.  */
 14816       clear_mouse_face (hlinfo);
 14817       return;
 14818     }
 14819   else if (rc == 0)
 14820     /* On same tab-bar item as before.  */
 14821     goto set_help_echo;
 14822 
 14823   clear_mouse_face (hlinfo);
 14824 
 14825   bool mouse_down_p = false;
 14826   /* Mouse is down, but on different tab-bar item?  Or alternatively,
 14827      the mouse might've been pressed somewhere we don't know about,
 14828      and then have moved onto the tab bar.  In this case,
 14829      last_tab_bar_item is -1, so we DTRT and behave like other
 14830      programs by displaying the item as sunken. */
 14831   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 14832   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 14833                   && f == dpyinfo->last_mouse_frame);
 14834 
 14835   if (mouse_down_p && f->last_tab_bar_item != prop_idx
 14836       && f->last_tab_bar_item != -1)
 14837     return;
 14838   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 14839 
 14840   /* If tab-bar item is not enabled, don't highlight it.  */
 14841   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14842   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 14843     {
 14844       /* Compute the x-position of the glyph.  In front and past the
 14845          image is a space.  We include this in the highlighted area.  */
 14846       row = MATRIX_ROW (w->current_matrix, vpos);
 14847       for (i = x = 0; i < hpos; ++i)
 14848         x += row->glyphs[TEXT_AREA][i].pixel_width;
 14849 
 14850       /* Record this as the current active region.  */
 14851       hlinfo->mouse_face_beg_col = hpos;
 14852       hlinfo->mouse_face_beg_row = vpos;
 14853       hlinfo->mouse_face_beg_x = x;
 14854       hlinfo->mouse_face_past_end = false;
 14855 
 14856       hlinfo->mouse_face_end_col = hpos + 1;
 14857       hlinfo->mouse_face_end_row = vpos;
 14858       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 14859       hlinfo->mouse_face_window = window;
 14860       hlinfo->mouse_face_face_id = TAB_BAR_FACE_ID;
 14861 
 14862       /* Display it as active.  */
 14863       show_mouse_face (hlinfo, draw);
 14864     }
 14865 
 14866  set_help_echo:
 14867 
 14868   /* Set help_echo_string to a help string to display for this tab-bar item.
 14869      XTread_socket does the rest.  */
 14870   help_echo_object = help_echo_window = Qnil;
 14871   help_echo_pos = -1;
 14872   help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_HELP);
 14873   if (NILP (help_echo_string))
 14874     help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION);
 14875 }
 14876 
 14877 #endif /* HAVE_WINDOW_SYSTEM */
 14878 
 14879 /* Find the tab-bar item at X coordinate and return its information.  */
 14880 static Lisp_Object
 14881 tty_get_tab_bar_item (struct frame *f, int x, int *prop_idx, bool *close_p)
 14882 {
 14883   ptrdiff_t clen = 0;
 14884 
 14885   for (int i = 0; i < f->n_tab_bar_items; i++)
 14886     {
 14887       Lisp_Object caption = AREF (f->tab_bar_items, (i * TAB_BAR_ITEM_NSLOTS
 14888                                                      + TAB_BAR_ITEM_CAPTION));
 14889       if (NILP (caption))
 14890         return Qnil;
 14891       clen += SCHARS (caption);
 14892       if (x < clen)
 14893         {
 14894           *prop_idx = i;
 14895           *close_p = !NILP (Fget_text_property (make_fixnum (SCHARS (caption)
 14896                                                              - (clen - x)),
 14897                                                 Qclose_tab,
 14898                                                 caption));
 14899           return caption;
 14900         }
 14901     }
 14902   return Qnil;
 14903 }
 14904 
 14905 /* Handle a mouse click at X/Y on the tab bar of TTY frame F.  If the
 14906    click was on the tab bar and was handled, populate the EVENT
 14907    structure, store it in keyboard queue, and return true; otherwise
 14908    return false.  MODIFIERS are event modifiers for generating the tab
 14909    release event.  */
 14910 Lisp_Object
 14911 tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14912                           struct input_event *event)
 14913 {
 14914   /* Did they click on the tab bar?  */
 14915   if (y < FRAME_MENU_BAR_LINES (f)
 14916       || y >= FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f))
 14917     return Qnil;
 14918 
 14919   /* Find the tab-bar item where the X,Y coordinates belong.  */
 14920   int prop_idx;
 14921   bool close_p;
 14922   Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &close_p);
 14923 
 14924   if (NILP (caption))
 14925     return Qnil;
 14926 
 14927   if (NILP (AREF (f->tab_bar_items,
 14928                   prop_idx * TAB_BAR_ITEM_NSLOTS + TAB_BAR_ITEM_ENABLED_P)))
 14929     return Qnil;
 14930 
 14931   if (down_p)
 14932     f->last_tab_bar_item = prop_idx;
 14933   else
 14934     f->last_tab_bar_item = -1;
 14935 
 14936   caption = Fcopy_sequence (caption);
 14937 
 14938   AUTO_LIST2 (props, Qmenu_item,
 14939               list3 (AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14940                            + TAB_BAR_ITEM_KEY),
 14941                      AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14942                            + TAB_BAR_ITEM_BINDING),
 14943                      close_p ? Qt : Qnil));
 14944 
 14945   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14946                         props, caption);
 14947 
 14948   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14949 }
 14950 
 14951 
 14952 
 14953 /***********************************************************************
 14954                                Tool-bars
 14955  ***********************************************************************/
 14956 
 14957 #ifdef HAVE_WINDOW_SYSTEM
 14958 
 14959 /* Update the tool-bar item list for frame F.  This has to be done
 14960    before we start to fill in any display lines.  Called from
 14961    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 14962    and restore it here.  */
 14963 
 14964 static void
 14965 update_tool_bar (struct frame *f, bool save_match_data)
 14966 {
 14967 #ifdef HAVE_EXT_TOOL_BAR
 14968   bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
 14969 #else
 14970   bool do_update = (WINDOWP (f->tool_bar_window)
 14971                     && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
 14972 #endif
 14973 
 14974   if (do_update)
 14975     {
 14976       Lisp_Object window;
 14977       struct window *w;
 14978 
 14979       window = FRAME_SELECTED_WINDOW (f);
 14980       w = XWINDOW (window);
 14981 
 14982       /* If the user has switched buffers or windows, we need to
 14983          recompute to reflect the new bindings.  But we'll
 14984          recompute when update_mode_lines is set too; that means
 14985          that people can use force-mode-line-update to request
 14986          that the menu bar be recomputed.  The adverse effect on
 14987          the rest of the redisplay algorithm is about the same as
 14988          windows_or_buffers_changed anyway.  */
 14989       if (windows_or_buffers_changed
 14990           || w->update_mode_line
 14991           || update_mode_lines
 14992           || window_buffer_changed (w))
 14993         {
 14994           struct buffer *prev = current_buffer;
 14995           specpdl_ref count = SPECPDL_INDEX ();
 14996           Lisp_Object frame, new_tool_bar;
 14997           int new_n_tool_bar;
 14998 
 14999           /* Set current_buffer to the buffer of the selected
 15000              window of the frame, so that we get the right local
 15001              keymaps.  */
 15002           set_buffer_internal_1 (XBUFFER (w->contents));
 15003 
 15004           /* Save match data, if we must.  */
 15005           if (save_match_data)
 15006             record_unwind_save_match_data ();
 15007 
 15008           /* Make sure that we don't accidentally use bogus keymaps.  */
 15009           if (NILP (Voverriding_local_map_menu_flag))
 15010             {
 15011               specbind (Qoverriding_terminal_local_map, Qnil);
 15012               specbind (Qoverriding_local_map, Qnil);
 15013             }
 15014 
 15015           /* We must temporarily set the selected frame to this frame
 15016              before calling tool_bar_items, because the calculation of
 15017              the tool-bar keymap uses the selected frame (see
 15018              `tool-bar-make-keymap' in tool-bar.el).  */
 15019           eassert (EQ (selected_window,
 15020                        /* Since we only explicitly preserve selected_frame,
 15021                           check that selected_window would be redundant.  */
 15022                        XFRAME (selected_frame)->selected_window));
 15023           record_unwind_protect (restore_selected_window, selected_window);
 15024           XSETFRAME (frame, f);
 15025           selected_frame = frame;
 15026           selected_window = FRAME_SELECTED_WINDOW (f);
 15027 
 15028           /* Build desired tool-bar items from keymaps.  */
 15029           new_tool_bar
 15030             = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
 15031                               &new_n_tool_bar);
 15032 
 15033           /* Redisplay the tool-bar if we changed it.  */
 15034           if (new_n_tool_bar != f->n_tool_bar_items
 15035               || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
 15036             {
 15037               /* Redisplay that happens asynchronously due to an expose event
 15038                  may access f->tool_bar_items.  Make sure we update both
 15039                  variables within BLOCK_INPUT so no such event interrupts.  */
 15040               block_input ();
 15041               fset_tool_bar_items (f, new_tool_bar);
 15042               f->n_tool_bar_items = new_n_tool_bar;
 15043               w->update_mode_line = true;
 15044               unblock_input ();
 15045             }
 15046 
 15047           unbind_to (count, Qnil);
 15048           set_buffer_internal_1 (prev);
 15049         }
 15050     }
 15051 }
 15052 
 15053 #ifndef HAVE_EXT_TOOL_BAR
 15054 
 15055 /* Set F->desired_tool_bar_string to a Lisp string representing frame
 15056    F's desired tool-bar contents.  F->tool_bar_items must have
 15057    been set up previously by calling prepare_menu_bars.
 15058 
 15059    Also set F->tool_bar_wraps_p to whether or not the tool bar
 15060    contains explicit line breaking items.  */
 15061 
 15062 static void
 15063 build_desired_tool_bar_string (struct frame *f)
 15064 {
 15065   int i, size, size_needed;
 15066   Lisp_Object image, plist;
 15067 
 15068   image = plist = Qnil;
 15069 
 15070   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
 15071      Otherwise, make a new string.  */
 15072 
 15073   /* The size of the string we might be able to reuse.  */
 15074   size = (STRINGP (f->desired_tool_bar_string)
 15075           ? SCHARS (f->desired_tool_bar_string)
 15076           : 0);
 15077 
 15078   /* We need one space in the string for each image.  */
 15079   size_needed = f->n_tool_bar_items;
 15080 
 15081   /* Reuse f->desired_tool_bar_string, if possible.  */
 15082 
 15083   if (size < size_needed || NILP (f->desired_tool_bar_string))
 15084     /* Don't initialize the contents of this string yet, as they will
 15085        be set within the loop below.  */
 15086     fset_desired_tool_bar_string (f, make_uninit_string (size_needed));
 15087   else
 15088     {
 15089       AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
 15090       Fremove_text_properties (make_fixnum (0), make_fixnum (size),
 15091                                props, f->desired_tool_bar_string);
 15092     }
 15093 
 15094   f->tool_bar_wraps_p = false;
 15095 
 15096   /* Put a `display' property on the string for the images to display,
 15097      put a `menu_item' property on tool-bar items with a value that
 15098      is the index of the item in F's tool-bar item vector.  */
 15099   for (i = 0; i < f->n_tool_bar_items; ++i)
 15100     {
 15101 #define PROP(IDX) \
 15102   AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
 15103 
 15104       bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
 15105       bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
 15106       int hmargin, vmargin, relief, idx, end;
 15107 
 15108       if (!NILP (PROP (TOOL_BAR_ITEM_WRAP)))
 15109         {
 15110           /* This is a line wrap.  Instead of building a tool bar
 15111              item, display a new line character instead.  */
 15112           SSET (f->desired_tool_bar_string, i, '\n');
 15113 
 15114           /* Set F->tool_bar_wraps_p.  This tells redisplay_tool_bar
 15115              to allow individual rows to be different heights.  */
 15116           f->tool_bar_wraps_p = true;
 15117           continue;
 15118         }
 15119 
 15120       /* Replace this with a space character.  */
 15121       SSET (f->desired_tool_bar_string, i, ' ');
 15122 
 15123       /* If image is a vector, choose the image according to the
 15124          button state.  */
 15125       image = PROP (TOOL_BAR_ITEM_IMAGES);
 15126       if (VECTORP (image))
 15127         {
 15128           if (enabled_p)
 15129             idx = (selected_p
 15130                    ? TOOL_BAR_IMAGE_ENABLED_SELECTED
 15131                    : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
 15132           else
 15133             idx = (selected_p
 15134                    ? TOOL_BAR_IMAGE_DISABLED_SELECTED
 15135                    : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
 15136 
 15137           eassert (ASIZE (image) >= idx);
 15138           image = AREF (image, idx);
 15139         }
 15140       else
 15141         idx = -1;
 15142 
 15143       /* Ignore invalid image specifications.  */
 15144       if (!valid_image_p (image))
 15145         continue;
 15146 
 15147       /* Display the tool-bar button pressed, or depressed.  */
 15148       plist = Fcopy_sequence (XCDR (image));
 15149 
 15150       /* Compute margin and relief to draw.  */
 15151       relief = (tool_bar_button_relief >= 0
 15152                 ? min (tool_bar_button_relief,
 15153                        min (INT_MAX, MOST_POSITIVE_FIXNUM))
 15154                 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
 15155       hmargin = vmargin = relief;
 15156 
 15157       if (RANGED_FIXNUMP (1, Vtool_bar_button_margin,
 15158                            INT_MAX - max (hmargin, vmargin)))
 15159         {
 15160           hmargin += XFIXNAT (Vtool_bar_button_margin);
 15161           vmargin += XFIXNAT (Vtool_bar_button_margin);
 15162         }
 15163       else if (CONSP (Vtool_bar_button_margin))
 15164         {
 15165           if (RANGED_FIXNUMP (1, XCAR (Vtool_bar_button_margin),
 15166                                INT_MAX - hmargin))
 15167             hmargin += XFIXNAT (XCAR (Vtool_bar_button_margin));
 15168 
 15169           if (RANGED_FIXNUMP (1, XCDR (Vtool_bar_button_margin),
 15170                                INT_MAX - vmargin))
 15171             vmargin += XFIXNAT (XCDR (Vtool_bar_button_margin));
 15172         }
 15173 
 15174       if (auto_raise_tool_bar_buttons_p)
 15175         {
 15176           /* Add a `:relief' property to the image spec if the item is
 15177              selected.  */
 15178           if (selected_p)
 15179             {
 15180               plist = plist_put (plist, QCrelief, make_fixnum (-relief));
 15181               hmargin -= relief;
 15182               vmargin -= relief;
 15183             }
 15184         }
 15185       else
 15186         {
 15187           /* If image is selected, display it pressed, i.e. with a
 15188              negative relief.  If it's not selected, display it with a
 15189              raised relief.  */
 15190           plist = plist_put (plist, QCrelief,
 15191                              (selected_p
 15192                               ? make_fixnum (-relief)
 15193                               : make_fixnum (relief)));
 15194           hmargin -= relief;
 15195           vmargin -= relief;
 15196         }
 15197 
 15198       /* Put a margin around the image.  */
 15199       if (hmargin || vmargin)
 15200         {
 15201           if (hmargin == vmargin)
 15202             plist = plist_put (plist, QCmargin, make_fixnum (hmargin));
 15203           else
 15204             plist = plist_put (plist, QCmargin,
 15205                                Fcons (make_fixnum (hmargin),
 15206                                       make_fixnum (vmargin)));
 15207         }
 15208 
 15209       /* If button is not enabled, and we don't have special images
 15210          for the disabled state, make the image appear disabled by
 15211          applying an appropriate algorithm to it.  */
 15212       if (!enabled_p && idx < 0)
 15213         plist = plist_put (plist, QCconversion, Qdisabled);
 15214 
 15215       /* Put a `display' text property on the string for the image to
 15216          display.  Put a `menu-item' property on the string that gives
 15217          the start of this item's properties in the tool-bar items
 15218          vector.  */
 15219       image = Fcons (Qimage, plist);
 15220       AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
 15221                   make_fixnum (i * TOOL_BAR_ITEM_NSLOTS));
 15222 
 15223       /* Let the last image hide all remaining spaces in the tool bar
 15224          string.  The string can be longer than needed when we reuse a
 15225          previous string.  */
 15226       if (i + 1 == f->n_tool_bar_items)
 15227         end = SCHARS (f->desired_tool_bar_string);
 15228       else
 15229         end = i + 1;
 15230       Fadd_text_properties (make_fixnum (i), make_fixnum (end),
 15231                             props, f->desired_tool_bar_string);
 15232 #undef PROP
 15233     }
 15234 
 15235   /* Now replace each character between i and the end of the tool bar
 15236      string with spaces, to prevent stray newlines from accumulating
 15237      when the number of tool bar items decreases.  `size' is 0 if the
 15238      tool bar string is new, but in that case the string will have
 15239      been completely initialized anyway.  */
 15240 
 15241   for (; i < size; ++i)
 15242     /* Replace this with a space character.  */
 15243     SSET (f->desired_tool_bar_string, i, ' ');
 15244 }
 15245 
 15246 
 15247 /* Display one line of the tool-bar of frame IT->f.
 15248 
 15249    HEIGHT specifies the desired height of the tool-bar line.
 15250    If the actual height of the glyph row is less than HEIGHT, the
 15251    row's height is increased to HEIGHT, and the icons are centered
 15252    vertically in the new height.
 15253 
 15254    If HEIGHT is -1, we are counting needed tool-bar lines, so don't
 15255    count a final empty row in case the tool-bar width exactly matches
 15256    the window width.
 15257 
 15258    HEIGHT may also be -1 if there is an explicit line wrapping item
 15259    inside the tool bar; in that case, allow individual rows of the
 15260    tool bar to differ in height.  */
 15261 
 15262 static void
 15263 display_tool_bar_line (struct it *it, int height)
 15264 {
 15265   struct glyph_row *row = it->glyph_row;
 15266   int max_x = it->last_visible_x;
 15267   struct glyph *last;
 15268 
 15269   /* Don't extend on a previously drawn tool bar items (Bug#16058).  */
 15270   clear_glyph_row (row);
 15271   row->enabled_p = true;
 15272   row->y = it->current_y;
 15273 
 15274   /* Note that this isn't made use of if the face hasn't a box,
 15275      so there's no need to check the face here.  */
 15276   it->start_of_box_run_p = true;
 15277 
 15278   while (it->current_x < max_x)
 15279     {
 15280       int x, n_glyphs_before, i, nglyphs;
 15281       struct it it_before;
 15282 
 15283       /* Get the next display element.  */
 15284       if (!get_next_display_element (it))
 15285         {
 15286           /* Don't count empty row if we are counting needed tool-bar lines.  */
 15287           if (height < 0 && !it->hpos)
 15288             return;
 15289           break;
 15290         }
 15291 
 15292       /* Produce glyphs.  */
 15293       n_glyphs_before = row->used[TEXT_AREA];
 15294       it_before = *it;
 15295 
 15296       PRODUCE_GLYPHS (it);
 15297 
 15298       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 15299       i = 0;
 15300       x = it_before.current_x;
 15301       while (i < nglyphs)
 15302         {
 15303           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 15304 
 15305           if (x + glyph->pixel_width > max_x)
 15306             {
 15307               /* Glyph doesn't fit on line.  Backtrack.  */
 15308               row->used[TEXT_AREA] = n_glyphs_before;
 15309               *it = it_before;
 15310               /* If this is the only glyph on this line, it will never fit on the
 15311                  tool-bar, so skip it.  But ensure there is at least one glyph,
 15312                  so we don't accidentally disable the tool-bar.  */
 15313               if (n_glyphs_before == 0
 15314                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 15315                 break;
 15316               goto out;
 15317             }
 15318 
 15319           ++it->hpos;
 15320           x += glyph->pixel_width;
 15321           ++i;
 15322         }
 15323 
 15324       /* Stop at the end of the iterator, and move to the next line
 15325          upon a '\n' appearing in the tool bar string.  Tool bar
 15326          strings may contain multiple new line characters when
 15327          explicit wrap items are encountered.  */
 15328 
 15329       if (ITERATOR_AT_END_OF_LINE_P (it))
 15330         {
 15331           reseat_at_next_visible_line_start (it, false);
 15332           break;
 15333         }
 15334 
 15335       if (ITERATOR_AT_END_P (it))
 15336         break;
 15337 
 15338       set_iterator_to_next (it, true);
 15339     }
 15340 
 15341  out:;
 15342 
 15343   row->displays_text_p = row->used[TEXT_AREA] != 0;
 15344 
 15345   /* Use default face for the border below the tool bar.
 15346 
 15347      FIXME: When auto-resize-tool-bars is grow-only, there is
 15348      no additional border below the possibly empty tool-bar lines.
 15349      So to make the extra empty lines look "normal", we have to
 15350      use the tool-bar face for the border too.  */
 15351   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15352       && !EQ (Vauto_resize_tool_bars, Qgrow_only))
 15353     it->face_id = DEFAULT_FACE_ID;
 15354 
 15355   extend_face_to_end_of_line (it);
 15356   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 15357   last->right_box_line_p = true;
 15358   if (last == row->glyphs[TEXT_AREA])
 15359     last->left_box_line_p = true;
 15360 
 15361   /* Make line the desired height and center it vertically.  */
 15362   if (height != -1
 15363       && (height -= it->max_ascent + it->max_descent) > 0)
 15364     {
 15365       /* Don't add more than one line height.  */
 15366       height %= FRAME_LINE_HEIGHT (it->f);
 15367       it->max_ascent += height / 2;
 15368       it->max_descent += (height + 1) / 2;
 15369     }
 15370 
 15371   compute_line_metrics (it);
 15372 
 15373   /* If line is empty, make it occupy the rest of the tool-bar.  */
 15374   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 15375     {
 15376       row->height = row->phys_height = it->last_visible_y - row->y;
 15377       row->visible_height = row->height;
 15378       row->ascent = row->phys_ascent = 0;
 15379       row->extra_line_spacing = 0;
 15380     }
 15381 
 15382   row->full_width_p = true;
 15383   row->continued_p = false;
 15384   row->truncated_on_left_p = false;
 15385   row->truncated_on_right_p = false;
 15386 
 15387   it->current_x = it->hpos = 0;
 15388   it->current_y += row->height;
 15389   ++it->vpos;
 15390   ++it->glyph_row;
 15391 }
 15392 
 15393 
 15394 /* Value is the number of pixels needed to make all tool-bar items of
 15395    frame F visible.  The actual number of glyph rows needed is
 15396    returned in *N_ROWS if non-NULL.  */
 15397 
 15398 static int
 15399 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 15400 {
 15401   struct window *w = XWINDOW (f->tool_bar_window);
 15402   struct it it;
 15403   /* tool_bar_height is called from redisplay_tool_bar after building
 15404      the desired matrix, so use (unused) mode-line row as temporary row to
 15405      avoid destroying the first tool-bar row.  */
 15406   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 15407 
 15408   /* Initialize an iterator for iteration over
 15409      F->desired_tool_bar_string in the tool-bar window of frame F.  */
 15410   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
 15411   temp_row->reversed_p = false;
 15412   it.first_visible_x = 0;
 15413   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15414   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15415                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15416   it.paragraph_embedding = L2R;
 15417 
 15418   while (!ITERATOR_AT_END_P (&it))
 15419     {
 15420       clear_glyph_row (temp_row);
 15421       it.glyph_row = temp_row;
 15422       display_tool_bar_line (&it, -1);
 15423     }
 15424   clear_glyph_row (temp_row);
 15425 
 15426   /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar.  */
 15427   if (n_rows)
 15428     *n_rows = it.vpos > 0 ? it.vpos : -1;
 15429 
 15430   if (pixelwise)
 15431     return it.current_y;
 15432   else
 15433     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 15434 }
 15435 
 15436 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15437 
 15438 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
 15439        0, 2, 0,
 15440        doc: /* Return the number of lines occupied by the tool bar of FRAME.
 15441 If FRAME is nil or omitted, use the selected frame.  Optional argument
 15442 PIXELWISE non-nil means return the height of the tool bar in pixels.  */)
 15443   (Lisp_Object frame, Lisp_Object pixelwise)
 15444 {
 15445   int height = 0;
 15446 
 15447 #ifndef HAVE_EXT_TOOL_BAR
 15448   struct frame *f = decode_any_frame (frame);
 15449 
 15450   if (WINDOWP (f->tool_bar_window)
 15451       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
 15452     {
 15453       update_tool_bar (f, true);
 15454       if (f->n_tool_bar_items)
 15455         {
 15456           build_desired_tool_bar_string (f);
 15457           height = tool_bar_height (f, NULL, !NILP (pixelwise));
 15458         }
 15459     }
 15460 #endif
 15461 
 15462   return make_fixnum (height);
 15463 }
 15464 
 15465 #ifndef HAVE_EXT_TOOL_BAR
 15466 
 15467 /* Display the internal tool-bar of frame F.  Value is true if
 15468    tool-bar's height should be changed.  */
 15469 static bool
 15470 redisplay_tool_bar (struct frame *f)
 15471 {
 15472   struct window *w;
 15473   struct it it;
 15474   struct glyph_row *row;
 15475   bool change_height_p;
 15476 
 15477   change_height_p = false;
 15478   f->tool_bar_redisplayed = true;
 15479 
 15480   /* If frame hasn't a tool-bar window or if it is zero-height, don't
 15481      do anything.  This means you must start with tool-bar-lines
 15482      non-zero to get the auto-sizing effect.  Or in other words, you
 15483      can turn off tool-bars by specifying tool-bar-lines zero.  */
 15484   if (!WINDOWP (f->tool_bar_window)
 15485       || (w = XWINDOW (f->tool_bar_window),
 15486           WINDOW_TOTAL_LINES (w) == 0))
 15487     {
 15488       /* Even if we do not display a tool bar initially, still pretend
 15489          that we have resized it already.  This avoids that a later
 15490          activation of the tool bar resizes the frame, despite of the
 15491          fact that a setting of 'frame-inhibit-implied-resize' should
 15492          inhibit it (Bug#52986).  */
 15493       f->tool_bar_resized = true;
 15494 
 15495       return false;
 15496     }
 15497 
 15498   /* Set up an iterator for the tool-bar window.  */
 15499   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
 15500   it.first_visible_x = 0;
 15501   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15502   row = it.glyph_row;
 15503   row->reversed_p = false;
 15504 
 15505   /* Build a string that represents the contents of the tool-bar.  */
 15506   build_desired_tool_bar_string (f);
 15507   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15508                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15509   /* FIXME: This should be controlled by a user option.  But it
 15510      doesn't make sense to have an R2L tool bar if the menu bar cannot
 15511      be drawn also R2L, and making the menu bar R2L is tricky due to
 15512      toolkit-specific code that implements it.  If an R2L tool bar is
 15513      ever supported, display_tool_bar_line should also be augmented to
 15514      call unproduce_glyphs like display_line and display_string
 15515      do.  */
 15516   it.paragraph_embedding = L2R;
 15517 
 15518   if (f->n_tool_bar_rows == 0)
 15519     {
 15520       int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
 15521 
 15522       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 15523         {
 15524           if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15525             FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15526           frame_default_tool_bar_height = new_height;
 15527           /* Always do that now.  */
 15528           clear_glyph_matrix (w->desired_matrix);
 15529           f->fonts_changed = true;
 15530 
 15531           /* Kludge (this applies to the X Windows version as well as
 15532              Android): when the tool bar size changes,
 15533              adjust_window_size (presumably called by
 15534              change_tool_bar_height_hook) does not call through to
 15535              resize_frame_windows.  Pending further investigation,
 15536              just call it here as well.  */
 15537           resize_frame_windows (f, FRAME_INNER_HEIGHT (f), false);
 15538 
 15539           return true;
 15540         }
 15541     }
 15542 
 15543   /* Display as many lines as needed to display all tool-bar items.  */
 15544 
 15545   if (f->n_tool_bar_rows > 0)
 15546     {
 15547       int border, rows, height, extra;
 15548 
 15549       if (TYPE_RANGED_FIXNUMP (int, Vtool_bar_border))
 15550         border = XFIXNUM (Vtool_bar_border);
 15551       else if (EQ (Vtool_bar_border, Qinternal_border_width))
 15552         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 15553       else if (EQ (Vtool_bar_border, Qborder_width))
 15554         border = f->border_width;
 15555       else
 15556         border = 0;
 15557       if (border < 0)
 15558         border = 0;
 15559 
 15560       rows = f->n_tool_bar_rows;
 15561 
 15562       if (f->tool_bar_wraps_p)
 15563         {
 15564           /* If the tool bar contains explicit line wrapping items,
 15565              don't force each row to have a fixed height.  */
 15566 
 15567           while (!ITERATOR_AT_END_P (&it))
 15568             display_tool_bar_line (&it, -1);
 15569 
 15570           /* Because changes to individual tool bar items may now
 15571              change the height of the tool bar, adjust the height of
 15572              the tool bar window if it is different from the tool bar
 15573              height in any way.  */
 15574 
 15575           if (it.current_y != it.last_visible_y)
 15576             change_height_p = true;
 15577         }
 15578       else
 15579         {
 15580           height = max (1, (it.last_visible_y - border) / rows);
 15581           extra = it.last_visible_y - border - height * rows;
 15582 
 15583           while (it.current_y < it.last_visible_y)
 15584             {
 15585               int h = 0;
 15586               if (extra > 0 && rows-- > 0)
 15587                 {
 15588                   h = (extra + rows - 1) / rows;
 15589                   extra -= h;
 15590                 }
 15591           
 15592               display_tool_bar_line (&it, height + h);
 15593             }
 15594         }
 15595     }
 15596   else
 15597     {
 15598       while (it.current_y < it.last_visible_y)
 15599         display_tool_bar_line (&it, 0);
 15600     }
 15601 
 15602   /* It doesn't make much sense to try scrolling in the tool-bar
 15603      window, so don't do it.  */
 15604   w->desired_matrix->no_scrolling_p = true;
 15605   w->must_be_updated_p = true;
 15606 
 15607   if (!NILP (Vauto_resize_tool_bars))
 15608     {
 15609       /* If we couldn't display everything, change the tool-bar's
 15610          height if there is room for more.  */
 15611       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 15612         change_height_p = true;
 15613 
 15614       /* We subtract 1 because display_tool_bar_line advances the
 15615          glyph_row pointer before returning to its caller.  We want to
 15616          examine the last glyph row produced by
 15617          display_tool_bar_line.  */
 15618       row = it.glyph_row - 1;
 15619 
 15620       /* If there are blank lines at the end, except for a partially
 15621          visible blank line at the end that is smaller than
 15622          FRAME_LINE_HEIGHT, change the tool-bar's height.  */
 15623       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15624           && row->height >= FRAME_LINE_HEIGHT (f))
 15625         change_height_p = true;
 15626 
 15627       /* If row displays tool-bar items, but is partially visible,
 15628          change the tool-bar's height.  */
 15629       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15630           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 15631         change_height_p = true;
 15632 
 15633       /* Resize windows as needed by changing the `tool-bar-lines'
 15634          frame parameter.  */
 15635       if (change_height_p)
 15636         {
 15637           int nrows;
 15638           int new_height = tool_bar_height (f, &nrows, true);
 15639 
 15640           change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
 15641                               && !f->minimize_tool_bar_window_p)
 15642                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 15643                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 15644           f->minimize_tool_bar_window_p = false;
 15645 
 15646           if (change_height_p)
 15647             {
 15648               if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15649                 FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15650               frame_default_tool_bar_height = new_height;
 15651               clear_glyph_matrix (w->desired_matrix);
 15652               f->n_tool_bar_rows = nrows;
 15653               f->fonts_changed = true;
 15654 
 15655               return true;
 15656             }
 15657         }
 15658     }
 15659 
 15660   f->minimize_tool_bar_window_p = false;
 15661 
 15662   return false;
 15663 }
 15664 
 15665 /* Get information about the tool-bar item which is displayed in GLYPH
 15666    on frame F.  Return in *PROP_IDX the index where tool-bar item
 15667    properties start in F->tool_bar_items.  Value is false if
 15668    GLYPH doesn't display a tool-bar item.  */
 15669 
 15670 static bool
 15671 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
 15672 {
 15673   Lisp_Object prop;
 15674   ptrdiff_t charpos;
 15675 
 15676   /* This function can be called asynchronously, which means we must
 15677      exclude any possibility that Fget_text_property signals an
 15678      error.  */
 15679   charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
 15680   charpos = max (0, charpos);
 15681 
 15682   /* Get the text property `menu-item' at pos. The value of that
 15683      property is the start index of this item's properties in
 15684      F->tool_bar_items.  */
 15685   prop = Fget_text_property (make_fixnum (charpos),
 15686                              Qmenu_item, f->current_tool_bar_string);
 15687   if (! FIXNUMP (prop))
 15688     return false;
 15689   *prop_idx = XFIXNUM (prop);
 15690   return true;
 15691 }
 15692 
 15693 
 15694 /* Get information about the tool-bar item at position X/Y on frame F.
 15695    Return in *GLYPH a pointer to the glyph of the tool-bar item in
 15696    the current matrix of the tool-bar window of F, or NULL if not
 15697    on a tool-bar item.  Return in *PROP_IDX the index of the tool-bar
 15698    item in F->tool_bar_items.  Value is
 15699 
 15700    -1   if X/Y is not on a tool-bar item
 15701    0    if X/Y is on the same item that was highlighted before.
 15702    1    otherwise.  */
 15703 
 15704 static int
 15705 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 15706                    int *hpos, int *vpos, int *prop_idx)
 15707 {
 15708   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15709   struct window *w = XWINDOW (f->tool_bar_window);
 15710   int area;
 15711 
 15712   /* Find the glyph under X/Y.  */
 15713   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 15714   if (*glyph == NULL)
 15715     return -1;
 15716 
 15717   /* Get the start of this tool-bar item's properties in
 15718      f->tool_bar_items.  */
 15719   if (!tool_bar_item_info (f, *glyph, prop_idx))
 15720     return -1;
 15721 
 15722   /* Is mouse on the highlighted item?  */
 15723   if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
 15724       && *vpos >= hlinfo->mouse_face_beg_row
 15725       && *vpos <= hlinfo->mouse_face_end_row
 15726       && (*vpos > hlinfo->mouse_face_beg_row
 15727           || *hpos >= hlinfo->mouse_face_beg_col)
 15728       && (*vpos < hlinfo->mouse_face_end_row
 15729           || *hpos < hlinfo->mouse_face_end_col
 15730           || hlinfo->mouse_face_past_end))
 15731     return 0;
 15732 
 15733   return 1;
 15734 }
 15735 
 15736 
 15737 /* EXPORT:
 15738    Handle mouse button event on the tool-bar of frame F, at
 15739    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 15740    false for button release.  MODIFIERS is event modifiers for button
 15741    release.  DEVICE is the device the click came from, or Qt.  */
 15742 
 15743 void
 15744 handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p,
 15745                                    int modifiers, Lisp_Object device)
 15746 {
 15747   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15748   struct window *w = XWINDOW (f->tool_bar_window);
 15749   int hpos, vpos, prop_idx;
 15750   struct glyph *glyph;
 15751   Lisp_Object enabled_p;
 15752   int ts;
 15753 
 15754   /* If not on the highlighted tool-bar item, and mouse-highlight is
 15755      non-nil, return.  This is so we generate the tool-bar button
 15756      click only when the mouse button is released on the same item as
 15757      where it was pressed.  However, when mouse-highlight is disabled,
 15758      generate the click when the button is released regardless of the
 15759      highlight, since tool-bar items are not highlighted in that
 15760      case.  */
 15761   frame_to_window_pixel_xy (w, &x, &y);
 15762   ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15763   if (ts == -1
 15764       || (ts != 0 && !NILP (Vmouse_highlight)))
 15765     return;
 15766 
 15767   /* When mouse-highlight is off, generate the click for the item
 15768      where the button was pressed, disregarding where it was
 15769      released.  */
 15770   if (NILP (Vmouse_highlight) && !down_p)
 15771     prop_idx = f->last_tool_bar_item;
 15772 
 15773   /* If item is disabled, do nothing.  */
 15774   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15775   if (NILP (enabled_p))
 15776     return;
 15777 
 15778   if (down_p)
 15779     {
 15780       /* Show item in pressed state.  */
 15781       if (!NILP (Vmouse_highlight))
 15782         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 15783       f->last_tool_bar_item = prop_idx;
 15784     }
 15785   else
 15786     {
 15787       Lisp_Object key, frame;
 15788       struct input_event event;
 15789       EVENT_INIT (event);
 15790 
 15791       /* Show item in released state.  */
 15792       if (!NILP (Vmouse_highlight))
 15793         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 15794 
 15795       key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
 15796 
 15797       XSETFRAME (frame, f);
 15798       event.kind = TOOL_BAR_EVENT;
 15799       event.frame_or_window = frame;
 15800       event.arg = key;
 15801       event.modifiers = modifiers;
 15802       event.device = device;
 15803       kbd_buffer_store_event (&event);
 15804       f->last_tool_bar_item = -1;
 15805     }
 15806 }
 15807 
 15808 void
 15809 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
 15810                        int modifiers)
 15811 {
 15812   handle_tool_bar_click_with_device (f, x, y, down_p, modifiers, Qt);
 15813 }
 15814 
 15815 /* Possibly highlight a tool-bar item on frame F when mouse moves to
 15816    tool-bar window-relative coordinates X/Y.  Called from
 15817    note_mouse_highlight.  */
 15818 
 15819 static void
 15820 note_tool_bar_highlight (struct frame *f, int x, int y)
 15821 {
 15822   Lisp_Object window = f->tool_bar_window;
 15823   struct window *w = XWINDOW (window);
 15824   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 15825   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15826   int hpos, vpos;
 15827   struct glyph *glyph;
 15828   struct glyph_row *row;
 15829   int i;
 15830   Lisp_Object enabled_p;
 15831   int prop_idx;
 15832   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 15833   bool mouse_down_p;
 15834   int rc;
 15835 
 15836   /* Function note_mouse_highlight is called with negative X/Y
 15837      values when mouse moves outside of the frame.  */
 15838   if (x <= 0 || y <= 0)
 15839     {
 15840       clear_mouse_face (hlinfo);
 15841       return;
 15842     }
 15843 
 15844   rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15845   if (rc < 0)
 15846     {
 15847       /* Not on tool-bar item.  */
 15848       clear_mouse_face (hlinfo);
 15849       return;
 15850     }
 15851   else if (rc == 0)
 15852     /* On same tool-bar item as before.  */
 15853     goto set_help_echo;
 15854 
 15855   clear_mouse_face (hlinfo);
 15856 
 15857   /* Mouse is down, but on different tool-bar item?  */
 15858   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 15859                   && f == dpyinfo->last_mouse_frame);
 15860 
 15861   if (mouse_down_p && f->last_tool_bar_item != prop_idx)
 15862     return;
 15863 
 15864   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 15865 
 15866   /* If tool-bar item is not enabled, don't highlight it.  */
 15867   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15868   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 15869     {
 15870       /* Compute the x-position of the glyph.  In front and past the
 15871          image is a space.  We include this in the highlighted area.  */
 15872       row = MATRIX_ROW (w->current_matrix, vpos);
 15873       for (i = x = 0; i < hpos; ++i)
 15874         x += row->glyphs[TEXT_AREA][i].pixel_width;
 15875 
 15876       /* Record this as the current active region.  */
 15877       hlinfo->mouse_face_beg_col = hpos;
 15878       hlinfo->mouse_face_beg_row = vpos;
 15879       hlinfo->mouse_face_beg_x = x;
 15880       hlinfo->mouse_face_past_end = false;
 15881 
 15882       hlinfo->mouse_face_end_col = hpos + 1;
 15883       hlinfo->mouse_face_end_row = vpos;
 15884       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 15885       hlinfo->mouse_face_window = window;
 15886       hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
 15887 
 15888       /* Display it as active.  */
 15889       show_mouse_face (hlinfo, draw);
 15890     }
 15891 
 15892  set_help_echo:
 15893 
 15894   /* Set help_echo_string to a help string to display for this tool-bar item.
 15895      XTread_socket does the rest.  */
 15896   help_echo_object = help_echo_window = Qnil;
 15897   help_echo_pos = -1;
 15898   help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
 15899   if (NILP (help_echo_string))
 15900     help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
 15901 }
 15902 
 15903 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15904 
 15905 #endif /* HAVE_WINDOW_SYSTEM */
 15906 
 15907 
 15908 
 15909 /************************************************************************
 15910                          Horizontal scrolling
 15911  ************************************************************************/
 15912 
 15913 /* For all leaf windows in the window tree rooted at WINDOW, set their
 15914    hscroll value so that PT is (i) visible in the window, and (ii) so
 15915    that it is not within a certain margin at the window's left and
 15916    right border.  Value is true if any window's hscroll has been
 15917    changed.  */
 15918 
 15919 static bool
 15920 hscroll_window_tree (Lisp_Object window)
 15921 {
 15922   bool hscrolled_p = false;
 15923   bool hscroll_relative_p = FLOATP (Vhscroll_step);
 15924   int hscroll_step_abs = 0;
 15925   double hscroll_step_rel = 0;
 15926 
 15927   if (hscroll_relative_p)
 15928     {
 15929       hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
 15930       if (hscroll_step_rel < 0)
 15931         {
 15932           hscroll_relative_p = false;
 15933           hscroll_step_abs = 0;
 15934         }
 15935     }
 15936   else if (TYPE_RANGED_FIXNUMP (int, Vhscroll_step))
 15937     {
 15938       hscroll_step_abs = XFIXNUM (Vhscroll_step);
 15939       if (hscroll_step_abs < 0)
 15940         hscroll_step_abs = 0;
 15941     }
 15942   else
 15943     hscroll_step_abs = 0;
 15944 
 15945   while (WINDOWP (window))
 15946     {
 15947       struct window *w = XWINDOW (window);
 15948 
 15949       if (WINDOWP (w->contents))
 15950         hscrolled_p |= hscroll_window_tree (w->contents);
 15951       else if (w->cursor.vpos >= 0
 15952                /* Don't allow hscroll in mini-windows that display
 15953                   echo-area messages.  This is because desired_matrix
 15954                   of such windows was prepared while momentarily
 15955                   switched to an echo-area buffer, which is different
 15956                   from w->contents, and we simply cannot hscroll such
 15957                   windows safely.  */
 15958                && !(w == XWINDOW (echo_area_window)
 15959                     && !NILP (echo_area_buffer[0])))
 15960         {
 15961           int h_margin;
 15962           int text_area_width;
 15963           struct glyph_row *cursor_row;
 15964           struct glyph_row *bottom_row;
 15965 
 15966           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
 15967           if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
 15968             cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
 15969           else
 15970             cursor_row = bottom_row - 1;
 15971 
 15972           if (!cursor_row->enabled_p)
 15973             {
 15974               bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 15975               if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
 15976                 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 15977               else
 15978                 cursor_row = bottom_row - 1;
 15979             }
 15980           bool row_r2l_p = cursor_row->reversed_p;
 15981           bool hscl = hscrolling_current_line_p (w);
 15982           int x_offset = 0;
 15983           /* When line numbers are displayed, we need to account for
 15984              the horizontal space they consume.  */
 15985           if (!NILP (Vdisplay_line_numbers))
 15986             {
 15987               struct glyph *g;
 15988               if (!row_r2l_p)
 15989                 {
 15990                   for (g = cursor_row->glyphs[TEXT_AREA];
 15991                        g < cursor_row->glyphs[TEXT_AREA]
 15992                          + cursor_row->used[TEXT_AREA];
 15993                        g++)
 15994                     {
 15995                       if (!(NILP (g->object) && g->charpos < 0))
 15996                         break;
 15997                       x_offset += g->pixel_width;
 15998                     }
 15999                 }
 16000               else
 16001                 {
 16002                   for (g = cursor_row->glyphs[TEXT_AREA]
 16003                          + cursor_row->used[TEXT_AREA];
 16004                        g > cursor_row->glyphs[TEXT_AREA];
 16005                        g--)
 16006                     {
 16007                       if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
 16008                         break;
 16009                       x_offset += (g - 1)->pixel_width;
 16010                     }
 16011                 }
 16012             }
 16013           if (cursor_row->truncated_on_left_p)
 16014             {
 16015               /* On TTY frames, don't count the left truncation glyph.  */
 16016               struct frame *f = XFRAME (WINDOW_FRAME (w));
 16017               x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
 16018             }
 16019 
 16020           text_area_width = window_box_width (w, TEXT_AREA);
 16021 
 16022           /* Scroll when cursor is inside this scroll margin.  */
 16023           h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
 16024                       * WINDOW_FRAME_COLUMN_WIDTH (w));
 16025 
 16026           /* If the position of this window's point has explicitly
 16027              changed, no more suspend auto hscrolling.  */
 16028           if (w->suspend_auto_hscroll
 16029               && NILP (Fequal (Fwindow_point (window),
 16030                                Fwindow_old_point (window))))
 16031             {
 16032               w->suspend_auto_hscroll = false;
 16033               /* When hscrolling just the current line, and the rest
 16034                  of lines were temporarily hscrolled, but no longer
 16035                  are, force thorough redisplay of this window, to show
 16036                  the effect of disabling hscroll suspension immediately.  */
 16037               if (w->min_hscroll == 0 && w->hscroll > 0
 16038                   && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
 16039                          Qcurrent_line))
 16040                 SET_FRAME_GARBAGED (XFRAME (w->frame));
 16041             }
 16042 
 16043           /* Remember window point.  */
 16044           Fset_marker (w->old_pointm,
 16045                        ((w == XWINDOW (selected_window))
 16046                         ? make_fixnum (BUF_PT (XBUFFER (w->contents)))
 16047                         : Fmarker_position (w->pointm)),
 16048                        w->contents);
 16049 
 16050           if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
 16051               && !w->suspend_auto_hscroll
 16052               /* In some pathological cases, like restoring a window
 16053                  configuration into a frame that is much smaller than
 16054                  the one from which the configuration was saved, we
 16055                  get glyph rows whose start and end have zero buffer
 16056                  positions, which we cannot handle below.  Just skip
 16057                  such windows.  */
 16058               && (CHARPOS (cursor_row->start.pos)
 16059                   >= BUF_BEG (XBUFFER (w->contents)))
 16060               /* For left-to-right rows, hscroll when cursor is either
 16061                  (i) inside the right hscroll margin, or (ii) if it is
 16062                  inside the left margin and the window is already
 16063                  hscrolled.  */
 16064               && ((!row_r2l_p
 16065                    && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
 16066                        || (cursor_row->enabled_p
 16067                            && cursor_row->truncated_on_right_p
 16068                            && (w->cursor.x >= text_area_width - h_margin))))
 16069                   /* For right-to-left rows, the logic is similar,
 16070                      except that rules for scrolling to left and right
 16071                      are reversed.  E.g., if cursor.x <= h_margin, we
 16072                      need to hscroll "to the right" unconditionally,
 16073                      and that will scroll the screen to the left so as
 16074                      to reveal the next portion of the row.  */
 16075                   || (row_r2l_p
 16076                       && ((cursor_row->enabled_p
 16077                            /* FIXME: It is confusing to set the
 16078                               truncated_on_right_p flag when R2L rows
 16079                               are actually truncated on the left.  */
 16080                            && cursor_row->truncated_on_right_p
 16081                            && w->cursor.x <= h_margin)
 16082                           || (w->hscroll
 16083                               && (w->cursor.x >= (text_area_width - h_margin
 16084                                                   - x_offset)))))
 16085                   /* This last condition is needed when moving
 16086                      vertically from an hscrolled line to a short line
 16087                      that doesn't need to be hscrolled.  If we omit
 16088                      this condition, the line from which we move will
 16089                      remain hscrolled.  */
 16090                   || (hscl
 16091                       && w->hscroll != w->min_hscroll
 16092                       && !cursor_row->truncated_on_left_p)))
 16093             {
 16094               struct it it;
 16095               ptrdiff_t hscroll;
 16096               struct buffer *saved_current_buffer;
 16097               ptrdiff_t pt;
 16098               int wanted_x;
 16099 
 16100               /* Find point in a display of infinite width.  */
 16101               saved_current_buffer = current_buffer;
 16102               current_buffer = XBUFFER (w->contents);
 16103 
 16104               if (w == XWINDOW (selected_window))
 16105                 pt = PT;
 16106               else
 16107                 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
 16108 
 16109               /* Move iterator to pt starting at cursor_row->start in
 16110                  a line with infinite width.  */
 16111               init_to_row_start (&it, w, cursor_row);
 16112               if (hscl)
 16113                 it.first_visible_x = window_hscroll_limited (w, it.f)
 16114                                      * FRAME_COLUMN_WIDTH (it.f);
 16115               it.last_visible_x = DISP_INFINITY;
 16116 
 16117               ptrdiff_t nchars = pt - IT_CHARPOS (it);
 16118               if (current_buffer->long_line_optimizations_p
 16119                   && nchars > large_hscroll_threshold)
 16120                 {
 16121                   /* Special optimization for very long and truncated
 16122                      lines which need to be hscrolled far to the left:
 16123                      jump directly to the (approximate) first position
 16124                      that is visible, instead of slowly walking there.  */
 16125                   fast_move_it_horizontally (&it, nchars);
 16126                   it.current_x += nchars * FRAME_COLUMN_WIDTH (it.f);
 16127                 }
 16128               else
 16129                 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
 16130               /* If the line ends in an overlay string with a newline,
 16131                  we might infloop, because displaying the window will
 16132                  want to put the cursor after the overlay, i.e. at X
 16133                  coordinate of zero on the next screen line.  So we
 16134                  use the buffer position prior to the overlay string
 16135                  instead.  */
 16136               if (it.method == GET_FROM_STRING && pt > 1)
 16137                 {
 16138                   init_to_row_start (&it, w, cursor_row);
 16139                   if (hscl)
 16140                     it.first_visible_x = (window_hscroll_limited (w, it.f)
 16141                                           * FRAME_COLUMN_WIDTH (it.f));
 16142                   if (current_buffer->long_line_optimizations_p
 16143                       && nchars > large_hscroll_threshold)
 16144                     {
 16145                       fast_move_it_horizontally (&it, nchars - 1);
 16146                       it.current_x += (nchars - 1) * FRAME_COLUMN_WIDTH (it.f);
 16147                     }
 16148                   else
 16149                     move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
 16150                 }
 16151               current_buffer = saved_current_buffer;
 16152 
 16153               /* Position cursor in window.  */
 16154               if (!hscroll_relative_p && hscroll_step_abs == 0)
 16155                 hscroll = max (0, (it.current_x
 16156                                    - (ITERATOR_AT_END_OF_LINE_P (&it)
 16157                                       ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
 16158                                       : (text_area_width / 2))))
 16159                           / FRAME_COLUMN_WIDTH (it.f);
 16160               else if ((!row_r2l_p
 16161                         && w->cursor.x >= text_area_width - h_margin)
 16162                        || (row_r2l_p && w->cursor.x <= h_margin))
 16163                 {
 16164                   if (hscroll_relative_p)
 16165                     wanted_x = text_area_width * (1 - hscroll_step_rel)
 16166                                - h_margin;
 16167                   else
 16168                     wanted_x = text_area_width
 16169                                - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16170                                - h_margin;
 16171                   hscroll
 16172                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16173                 }
 16174               else
 16175                 {
 16176                   if (hscroll_relative_p)
 16177                     wanted_x =
 16178                       text_area_width * hscroll_step_rel + h_margin + x_offset;
 16179                   else
 16180                     wanted_x =
 16181                       hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16182                       + h_margin + x_offset;
 16183                   hscroll
 16184                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16185                 }
 16186               hscroll = max (hscroll, w->min_hscroll);
 16187 
 16188               /* Don't prevent redisplay optimizations if hscroll
 16189                  hasn't changed, as it will unnecessarily slow down
 16190                  redisplay.  */
 16191               if (w->hscroll != hscroll
 16192                   /* When hscrolling only the current line, we need to
 16193                      report hscroll even if its value is equal to the
 16194                      previous one, because the new line might need a
 16195                      different value.  */
 16196                   || (hscl && w->last_cursor_vpos != w->cursor.vpos))
 16197                 {
 16198                   struct buffer *b = XBUFFER (w->contents);
 16199                   b->prevent_redisplay_optimizations_p = true;
 16200                   w->hscroll = hscroll;
 16201                   hscrolled_p = true;
 16202                 }
 16203             }
 16204         }
 16205 
 16206       window = w->next;
 16207     }
 16208 
 16209   /* Value is true if hscroll of any leaf window has been changed.  */
 16210   return hscrolled_p;
 16211 }
 16212 
 16213 
 16214 /* Set hscroll so that cursor is visible and not inside horizontal
 16215    scroll margins for all windows in the tree rooted at WINDOW.  See
 16216    also hscroll_window_tree above.  Value is true if any window's
 16217    hscroll has been changed.  If it has, desired matrices on the frame
 16218    of WINDOW are cleared.  */
 16219 
 16220 static bool
 16221 hscroll_windows (Lisp_Object window)
 16222 {
 16223   bool hscrolled_p = hscroll_window_tree (window);
 16224   if (hscrolled_p)
 16225     clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
 16226   return hscrolled_p;
 16227 }
 16228 
 16229 
 16230 
 16231 /************************************************************************
 16232                                 Redisplay
 16233  ************************************************************************/
 16234 
 16235 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
 16236    This is sometimes handy to have in a debugger session.  */
 16237 
 16238 #ifdef GLYPH_DEBUG
 16239 
 16240 /* First and last unchanged row for try_window_id.  */
 16241 
 16242 static int debug_first_unchanged_at_end_vpos;
 16243 static int debug_last_unchanged_at_beg_vpos;
 16244 
 16245 /* Delta vpos and y.  */
 16246 
 16247 static int debug_dvpos, debug_dy;
 16248 
 16249 /* Delta in characters and bytes for try_window_id.  */
 16250 
 16251 static ptrdiff_t debug_delta, debug_delta_bytes;
 16252 
 16253 /* Values of window_end_pos and window_end_vpos at the end of
 16254    try_window_id.  */
 16255 
 16256 static ptrdiff_t debug_end_vpos;
 16257 
 16258 /* Append a string to W->desired_matrix->method.  FMT is a printf
 16259    format string.  If trace_redisplay_p is true also printf the
 16260    resulting string to stderr.  */
 16261 
 16262 static void debug_method_add (struct window *, char const *, ...)
 16263   ATTRIBUTE_FORMAT_PRINTF (2, 3);
 16264 
 16265 static void
 16266 debug_method_add (struct window *w, char const *fmt, ...)
 16267 {
 16268   void *ptr = w;
 16269   char *method = w->desired_matrix->method;
 16270   int len = strlen (method);
 16271   int size = sizeof w->desired_matrix->method;
 16272   int remaining = size - len - 1;
 16273   va_list ap;
 16274 
 16275   if (len && remaining)
 16276     {
 16277       method[len] = '|';
 16278       --remaining, ++len;
 16279     }
 16280 
 16281   va_start (ap, fmt);
 16282   vsnprintf (method + len, remaining + 1, fmt, ap);
 16283   va_end (ap);
 16284 
 16285   if (trace_redisplay_p)
 16286     fprintf (stderr, "%p (%s): %s\n",
 16287              ptr,
 16288              ((BUFFERP (w->contents)
 16289                && STRINGP (BVAR (XBUFFER (w->contents), name)))
 16290               ? SSDATA (BVAR (XBUFFER (w->contents), name))
 16291               : "no buffer"),
 16292              method + len);
 16293 }
 16294 
 16295 #endif /* GLYPH_DEBUG */
 16296 
 16297 
 16298 /* Value is true if all changes in window W, which displays
 16299    current_buffer, are in the text between START and END.  START is a
 16300    buffer position, END is given as a distance from Z.  Used in
 16301    redisplay_internal for display optimization.  */
 16302 
 16303 static bool
 16304 text_outside_line_unchanged_p (struct window *w,
 16305                                ptrdiff_t start, ptrdiff_t end)
 16306 {
 16307   bool unchanged_p = true;
 16308 
 16309   /* If text or overlays have changed, see where.  */
 16310   if (window_outdated (w))
 16311     {
 16312       /* Gap in the line?  */
 16313       if (GPT < start || Z - GPT < end)
 16314         unchanged_p = false;
 16315 
 16316       /* Changes start in front of the line, or end after it?  */
 16317       if (unchanged_p
 16318           && (BEG_UNCHANGED < start - 1
 16319               || END_UNCHANGED < end))
 16320         unchanged_p = false;
 16321 
 16322       /* If selective display, can't optimize if changes start at the
 16323          beginning of the line.  */
 16324       if (unchanged_p
 16325           && FIXNUMP (BVAR (current_buffer, selective_display))
 16326           && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
 16327           && (BEG_UNCHANGED < start || GPT <= start))
 16328         unchanged_p = false;
 16329 
 16330       /* If there are overlays at the start or end of the line, these
 16331          may have overlay strings with newlines in them.  A change at
 16332          START, for instance, may actually concern the display of such
 16333          overlay strings as well, and they are displayed on different
 16334          lines.  So, quickly rule out this case.  (For the future, it
 16335          might be desirable to implement something more telling than
 16336          just BEG/END_UNCHANGED.)  */
 16337       if (unchanged_p)
 16338         {
 16339           if (BEG + BEG_UNCHANGED == start
 16340               && overlay_touches_p (start))
 16341             unchanged_p = false;
 16342           if (END_UNCHANGED == end
 16343               && overlay_touches_p (Z - end))
 16344             unchanged_p = false;
 16345         }
 16346 
 16347       /* Under bidi reordering, adding or deleting a character in the
 16348          beginning of a paragraph, before the first strong directional
 16349          character, can change the base direction of the paragraph (unless
 16350          the buffer specifies a fixed paragraph direction), which will
 16351          require redisplaying the whole paragraph.  It might be worthwhile
 16352          to find the paragraph limits and widen the range of redisplayed
 16353          lines to that, but for now just give up this optimization.  */
 16354       if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 16355           && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 16356         unchanged_p = false;
 16357     }
 16358 
 16359   return unchanged_p;
 16360 }
 16361 
 16362 
 16363 /* Do a frame update, taking possible shortcuts into account.  This is
 16364    the main external entry point for redisplay.
 16365 
 16366    If the last redisplay displayed an echo area message and that message
 16367    is no longer requested, we clear the echo area or bring back the
 16368    mini-buffer if that is in use.  */
 16369 
 16370 void
 16371 redisplay (void)
 16372 {
 16373   redisplay_internal ();
 16374 }
 16375 
 16376 
 16377 static Lisp_Object
 16378 overlay_arrow_string_or_property (Lisp_Object var)
 16379 {
 16380   Lisp_Object val;
 16381 
 16382   if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
 16383     return val;
 16384 
 16385   return Voverlay_arrow_string;
 16386 }
 16387 
 16388 /* Return true if there are any overlay-arrows in current_buffer.  */
 16389 static bool
 16390 overlay_arrow_in_current_buffer_p (void)
 16391 {
 16392   Lisp_Object vlist;
 16393 
 16394   for (vlist = Voverlay_arrow_variable_list;
 16395        CONSP (vlist);
 16396        vlist = XCDR (vlist))
 16397     {
 16398       Lisp_Object var = XCAR (vlist);
 16399       Lisp_Object val;
 16400 
 16401       if (!SYMBOLP (var))
 16402         continue;
 16403       val = find_symbol_value (var);
 16404       if (MARKERP (val)
 16405           && current_buffer == XMARKER (val)->buffer)
 16406         return true;
 16407     }
 16408   return false;
 16409 }
 16410 
 16411 
 16412 /* Return true if any overlay_arrows have moved or overlay-arrow-string
 16413    has changed.
 16414    If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
 16415    buffers that are affected.  */
 16416 
 16417 static bool
 16418 overlay_arrows_changed_p (bool set_redisplay)
 16419 {
 16420   Lisp_Object vlist;
 16421   bool changed = false;
 16422 
 16423   for (vlist = Voverlay_arrow_variable_list;
 16424        CONSP (vlist);
 16425        vlist = XCDR (vlist))
 16426     {
 16427       Lisp_Object var = XCAR (vlist);
 16428       Lisp_Object val, pstr;
 16429 
 16430       if (!SYMBOLP (var))
 16431         continue;
 16432       val = find_symbol_value (var);
 16433       if (!MARKERP (val))
 16434         continue;
 16435       if (! EQ (Fmarker_position (val),
 16436                 /* FIXME: Don't we have a problem, using such a global
 16437                  * "last-position" if the variable is buffer-local?  */
 16438                 Fget (var, Qlast_arrow_position))
 16439           || ! (pstr = overlay_arrow_string_or_property (var),
 16440                 EQ (pstr, Fget (var, Qlast_arrow_string))))
 16441         {
 16442           struct buffer *buf = XMARKER (val)->buffer;
 16443 
 16444           if (set_redisplay)
 16445             {
 16446               if (buf)
 16447                 bset_redisplay (buf);
 16448               changed = true;
 16449             }
 16450           else
 16451             return true;
 16452         }
 16453     }
 16454   return changed;
 16455 }
 16456 
 16457 /* Mark overlay arrows to be updated on next redisplay.  */
 16458 
 16459 static void
 16460 update_overlay_arrows (int up_to_date)
 16461 {
 16462   Lisp_Object vlist;
 16463 
 16464   for (vlist = Voverlay_arrow_variable_list;
 16465        CONSP (vlist);
 16466        vlist = XCDR (vlist))
 16467     {
 16468       Lisp_Object var = XCAR (vlist);
 16469 
 16470       if (!SYMBOLP (var))
 16471         continue;
 16472 
 16473       if (up_to_date > 0)
 16474         {
 16475           Lisp_Object val = find_symbol_value (var);
 16476           if (!MARKERP (val))
 16477             continue;
 16478           Fput (var, Qlast_arrow_position, Fmarker_position (val));
 16479           Fput (var, Qlast_arrow_string,
 16480                 overlay_arrow_string_or_property (var));
 16481         }
 16482       else if (up_to_date < 0
 16483                || !NILP (Fget (var, Qlast_arrow_position)))
 16484         {
 16485           Fput (var, Qlast_arrow_position, Qt);
 16486           Fput (var, Qlast_arrow_string, Qt);
 16487         }
 16488     }
 16489 }
 16490 
 16491 
 16492 /* Return overlay arrow string to display at row.
 16493    Return integer (bitmap number) for arrow bitmap in left fringe.
 16494    Return nil if no overlay arrow.  */
 16495 
 16496 static Lisp_Object
 16497 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
 16498 {
 16499   Lisp_Object vlist;
 16500 
 16501   for (vlist = Voverlay_arrow_variable_list;
 16502        CONSP (vlist);
 16503        vlist = XCDR (vlist))
 16504     {
 16505       Lisp_Object var = XCAR (vlist);
 16506       Lisp_Object val;
 16507 
 16508       if (!SYMBOLP (var))
 16509         continue;
 16510 
 16511       val = find_symbol_value (var);
 16512 
 16513       if (MARKERP (val)
 16514           && current_buffer == XMARKER (val)->buffer
 16515           && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
 16516         {
 16517           if (FRAME_WINDOW_P (it->f)
 16518               /* FIXME: if ROW->reversed_p is set, this should test
 16519                  the right fringe, not the left one.  */
 16520               && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
 16521             {
 16522 #ifdef HAVE_WINDOW_SYSTEM
 16523               if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
 16524                 {
 16525                   int fringe_bitmap = lookup_fringe_bitmap (val);
 16526                   if (fringe_bitmap != 0)
 16527                     return make_fixnum (fringe_bitmap);
 16528                 }
 16529 #endif
 16530               return make_fixnum (-1); /* Use default arrow bitmap.  */
 16531             }
 16532           return overlay_arrow_string_or_property (var);
 16533         }
 16534     }
 16535 
 16536   return Qnil;
 16537 }
 16538 
 16539 /* Return true if point moved out of or into a composition.  Otherwise
 16540    return false.  PREV_BUF and PREV_PT are the last point buffer and
 16541    position.  BUF and PT are the current point buffer and position.  */
 16542 
 16543 static bool
 16544 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 16545                             struct buffer *buf, ptrdiff_t pt)
 16546 {
 16547   ptrdiff_t start, end;
 16548   Lisp_Object prop;
 16549   Lisp_Object buffer;
 16550 
 16551   XSETBUFFER (buffer, buf);
 16552   /* Check a composition at the last point if point moved within the
 16553      same buffer.  */
 16554   if (prev_buf == buf)
 16555     {
 16556       if (prev_pt == pt)
 16557         /* Point didn't move.  */
 16558         return false;
 16559 
 16560       if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
 16561           && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
 16562           && composition_valid_p (start, end, prop)
 16563           && start < prev_pt && end > prev_pt)
 16564         /* The last point was within the composition.  Return true iff
 16565             point moved out of the composition.  */
 16566         return (pt <= start || pt >= end);
 16567     }
 16568 
 16569   /* Check a composition at the current point.  */
 16570   return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
 16571           && find_composition (pt, -1, &start, &end, &prop, buffer)
 16572           && composition_valid_p (start, end, prop)
 16573           && start < pt && end > pt);
 16574 }
 16575 
 16576 /* Reconsider the clip changes of buffer which is displayed in W.  */
 16577 
 16578 static void
 16579 reconsider_clip_changes (struct window *w)
 16580 {
 16581   struct buffer *b = XBUFFER (w->contents);
 16582 
 16583   if (b->clip_changed
 16584       && w->window_end_valid
 16585       && w->current_matrix->buffer == b
 16586       && w->current_matrix->zv == BUF_ZV (b)
 16587       && w->current_matrix->begv == BUF_BEGV (b))
 16588     b->clip_changed = false;
 16589 
 16590   /* If display wasn't paused, and W is not a tool bar window, see if
 16591      point has been moved into or out of a composition.  In that case,
 16592      set b->clip_changed to force updating the screen.  If
 16593      b->clip_changed has already been set, skip this check.  */
 16594   if (!b->clip_changed && w->window_end_valid)
 16595     {
 16596       ptrdiff_t pt = (w == XWINDOW (selected_window)
 16597                       ? PT : marker_position (w->pointm));
 16598 
 16599       if ((w->current_matrix->buffer != b || pt != w->last_point)
 16600           && check_point_in_composition (w->current_matrix->buffer,
 16601                                          w->last_point, b, pt))
 16602         b->clip_changed = true;
 16603     }
 16604 }
 16605 
 16606 static void
 16607 propagate_buffer_redisplay (void)
 16608 { /* Resetting b->text->redisplay is problematic!
 16609      We can't just reset it in the case that some window that displays
 16610      it has not been redisplayed; and such a window can stay
 16611      unredisplayed for a long time if it's currently invisible.
 16612      But we do want to reset it at the end of redisplay otherwise
 16613      its displayed windows will keep being redisplayed over and over
 16614      again.
 16615      So we copy all b->text->redisplay flags up to their windows here,
 16616      such that mark_window_display_accurate can safely reset
 16617      b->text->redisplay.  */
 16618   Lisp_Object ws = window_list ();
 16619   for (; CONSP (ws); ws = XCDR (ws))
 16620     {
 16621       struct window *thisw = XWINDOW (XCAR (ws));
 16622       struct buffer *thisb = XBUFFER (thisw->contents);
 16623       if (thisb->text->redisplay)
 16624         thisw->redisplay = true;
 16625     }
 16626 }
 16627 
 16628 #define STOP_POLLING                                    \
 16629 do { if (! polling_stopped_here) stop_polling ();       \
 16630        polling_stopped_here = true; } while (false)
 16631 
 16632 #define RESUME_POLLING                                  \
 16633 do { if (polling_stopped_here) start_polling ();        \
 16634        polling_stopped_here = false; } while (false)
 16635 
 16636 /* Perhaps in the future avoid recentering windows if it
 16637    is not necessary; currently that causes some problems.  */
 16638 
 16639 static void
 16640 redisplay_internal (void)
 16641 {
 16642   struct window *w = XWINDOW (selected_window);
 16643   struct window *sw;
 16644   struct frame *fr;
 16645   bool pending;
 16646   bool must_finish = false, match_p;
 16647   struct text_pos tlbufpos, tlendpos;
 16648   int number_of_visible_frames;
 16649   struct frame *sf;
 16650   bool polling_stopped_here = false;
 16651   Lisp_Object tail, frame;
 16652 
 16653   /* Set a limit to the number of retries we perform due to horizontal
 16654      scrolling, this avoids getting stuck in an uninterruptible
 16655      infinite loop (Bug #24633).  */
 16656   enum { MAX_HSCROLL_RETRIES = 16 };
 16657   int hscroll_retries = 0;
 16658 
 16659   /* Limit the number of retries for when frame(s) become garbaged as
 16660      result of redisplaying them.  Some packages set various redisplay
 16661      hooks, such as window-scroll-functions, to run Lisp that always
 16662      calls APIs which cause the frame's garbaged flag to become set,
 16663      so we loop indefinitely.  */
 16664   enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
 16665   int garbaged_frame_retries = 0;
 16666 
 16667   /* False means that only the selected_window needs to be updated.
 16668      True means that other windows may need to be updated as well,
 16669      so we need to consult `needs_no_update` for all windows.  */
 16670   bool consider_all_windows_p;
 16671 
 16672   /* True means redisplay has to redisplay the miniwindow.  */
 16673   bool update_miniwindow_p = false;
 16674 
 16675   redisplay_trace ("redisplay_internal %d\n", redisplaying_p);
 16676 
 16677   /* No redisplay if running in batch mode or frame is not yet fully
 16678      initialized, or redisplay is explicitly turned off by setting
 16679      Vinhibit_redisplay.  */
 16680   if ((FRAME_INITIAL_P (SELECTED_FRAME ())
 16681        && redisplay_skip_initial_frame)
 16682       || !NILP (Vinhibit_redisplay))
 16683     return;
 16684 
 16685   /* Don't examine these until after testing Vinhibit_redisplay.
 16686      When Emacs is shutting down, perhaps because its connection to
 16687      X has dropped, we should not look at them at all.  */
 16688   fr = XFRAME (w->frame);
 16689   sf = SELECTED_FRAME ();
 16690 
 16691   if (!fr->glyphs_initialized_p)
 16692     return;
 16693 
 16694 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
 16695   if (popup_activated ())
 16696     return;
 16697 #endif
 16698 
 16699 #if defined (HAVE_HAIKU)
 16700   if (popup_activated_p)
 16701     return;
 16702 #endif
 16703 
 16704   /* I don't think this happens but let's be paranoid.  */
 16705   if (redisplaying_p)
 16706     return;
 16707 
 16708   /* Record a function that clears redisplaying_p
 16709      when we leave this function.  */
 16710   specpdl_ref count = SPECPDL_INDEX ();
 16711   record_unwind_protect_void (unwind_redisplay);
 16712   redisplaying_p = true;
 16713   block_buffer_flips ();
 16714   specbind (Qinhibit_free_realized_faces, Qnil);
 16715 
 16716   /* Record this function, so it appears on the profiler's backtraces.  */
 16717   record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
 16718 
 16719   FOR_EACH_FRAME (tail, frame)
 16720     XFRAME (frame)->already_hscrolled_p = false;
 16721 
 16722   reset_outermost_restrictions ();
 16723 
 16724  retry:
 16725   /* Remember the currently selected window.  */
 16726   sw = w;
 16727 
 16728   pending = false;
 16729   forget_escape_and_glyphless_faces ();
 16730 
 16731   inhibit_free_realized_faces = false;
 16732 
 16733   /* If face_change, init_iterator will free all realized faces, which
 16734      includes the faces referenced from current matrices.  So, we
 16735      can't reuse current matrices in this case.  */
 16736   if (face_change)
 16737     windows_or_buffers_changed = 47;
 16738 
 16739   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
 16740       && FRAME_TTY (sf)->previous_frame != sf)
 16741     {
 16742       /* Since frames on a single ASCII terminal share the same
 16743          display area, displaying a different frame means redisplay
 16744          the whole thing.  */
 16745       SET_FRAME_GARBAGED (sf);
 16746 #if !defined DOS_NT && !defined HAVE_ANDROID
 16747       set_tty_color_mode (FRAME_TTY (sf), sf);
 16748 #endif
 16749       FRAME_TTY (sf)->previous_frame = sf;
 16750     }
 16751 
 16752   /* Set the visible flags for all frames.  Do this before checking for
 16753      resized or garbaged frames; they want to know if their frames are
 16754      visible.  See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
 16755   number_of_visible_frames = 0;
 16756 
 16757   FOR_EACH_FRAME (tail, frame)
 16758     {
 16759       struct frame *f = XFRAME (frame);
 16760 
 16761       if (FRAME_REDISPLAY_P (f))
 16762         {
 16763           ++number_of_visible_frames;
 16764           /* Adjust matrices for visible frames only.  */
 16765           if (f->fonts_changed)
 16766             {
 16767               adjust_frame_glyphs (f);
 16768               /* Disable all redisplay optimizations for this frame.
 16769                  This is because adjust_frame_glyphs resets the
 16770                  enabled_p flag for all glyph rows of all windows, so
 16771                  many optimizations will fail anyway, and some might
 16772                  fail to test that flag and do bogus things as
 16773                  result.  */
 16774               SET_FRAME_GARBAGED (f);
 16775               f->fonts_changed = false;
 16776             }
 16777           /* If cursor type has been changed on the frame
 16778              other than selected, consider all frames.  */
 16779           if (f != sf && f->cursor_type_changed)
 16780             fset_redisplay (f);
 16781         }
 16782       clear_desired_matrices (f);
 16783     }
 16784 
 16785   /* Notice any pending interrupt request to change frame size.  */
 16786   do_pending_window_change (true);
 16787 
 16788   /* Clear frames marked as garbaged.  */
 16789   clear_garbaged_frames ();
 16790 
 16791   /* Build menubar and tool-bar items.  */
 16792   if (NILP (Vmemory_full))
 16793     prepare_menu_bars ();
 16794 
 16795   /* do_pending_window_change could change the selected_window due to
 16796      frame resizing which makes the selected window too small.
 16797      prepare_menu_bars may call lisp hooks and hence also change the
 16798      selected_window.  */
 16799   if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
 16800     sw = w;
 16801 
 16802   reconsider_clip_changes (w);
 16803 
 16804   /* In most cases selected window displays current buffer.  */
 16805   match_p = XBUFFER (w->contents) == current_buffer;
 16806   if (match_p)
 16807     {
 16808       /* Detect case that we need to write or remove a star in the mode line.  */
 16809       if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
 16810         w->update_mode_line = true;
 16811 
 16812       if (mode_line_update_needed (w))
 16813         w->update_mode_line = true;
 16814 
 16815       /* If reconsider_clip_changes above decided that the narrowing
 16816          in the current buffer changed, make sure all other windows
 16817          showing that buffer will be redisplayed.  */
 16818       if (current_buffer->clip_changed)
 16819         bset_update_mode_line (current_buffer);
 16820     }
 16821 
 16822   /* Normally the message* functions will have already displayed and
 16823      updated the echo area, but the frame may have been trashed, or
 16824      the update may have been preempted, so display the echo area
 16825      again here.  Checking message_cleared_p captures the case that
 16826      the echo area should be cleared.  */
 16827   if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
 16828       || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
 16829       || (message_cleared_p
 16830           && minibuf_level == 0
 16831           /* If the mini-window is currently selected, this means the
 16832              echo-area doesn't show through.  */
 16833           && !MINI_WINDOW_P (XWINDOW (selected_window))))
 16834     {
 16835       echo_area_display (false);
 16836 
 16837       if (message_cleared_p)
 16838         update_miniwindow_p = true;
 16839 
 16840       must_finish = true;
 16841 
 16842       /* If we don't display the current message, don't clear the
 16843          message_cleared_p flag, because, if we did, we wouldn't clear
 16844          the echo area in the next redisplay which doesn't preserve
 16845          the echo area.  */
 16846       if (!display_last_displayed_message_p)
 16847         message_cleared_p = false;
 16848     }
 16849   else if (EQ (selected_window, minibuf_window)
 16850            && (current_buffer->clip_changed || window_outdated (w))
 16851            && resize_mini_window (w, false))
 16852     {
 16853       /* Resized active mini-window to fit the size of what it is
 16854          showing if its contents might have changed.  */
 16855       must_finish = true;
 16856 
 16857       /* If window configuration was changed, frames may have been
 16858          marked garbaged.  Clear them or we will experience
 16859          surprises wrt scrolling.  */
 16860       clear_garbaged_frames ();
 16861     }
 16862 
 16863   if (!NILP (Vrun_hooks))
 16864     run_window_change_functions ();
 16865 
 16866   if (windows_or_buffers_changed && !update_mode_lines)
 16867     /* Code that sets windows_or_buffers_changed doesn't distinguish whether
 16868        only the windows's contents needs to be refreshed, or whether the
 16869        mode-lines also need a refresh.  */
 16870     update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
 16871                          ? REDISPLAY_SOME : 32);
 16872 
 16873   /* If specs for an arrow have changed, do thorough redisplay
 16874      to ensure we remove any arrow that should no longer exist.  */
 16875   /* Apparently, this is the only case where we update other windows,
 16876      without updating other mode-lines.  */
 16877   overlay_arrows_changed_p (true);
 16878 
 16879   consider_all_windows_p = (update_mode_lines
 16880                             || windows_or_buffers_changed);
 16881 
 16882 #define AINC(a,i)                                                       \
 16883   {                                                                     \
 16884     Lisp_Object entry = Fgethash (make_fixnum (i), a, make_fixnum (0)); \
 16885     if (FIXNUMP (entry))                                                \
 16886       Fputhash (make_fixnum (i), make_fixnum (1 + XFIXNUM (entry)), a); \
 16887   }
 16888 
 16889   AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
 16890   AINC (Vredisplay__mode_lines_cause, update_mode_lines);
 16891 
 16892   /* Optimize the case that only the line containing the cursor in the
 16893      selected window has changed.  Variables starting with this_ are
 16894      set in display_line and record information about the line
 16895      containing the cursor.  */
 16896   tlbufpos = this_line_start_pos;
 16897   tlendpos = this_line_end_pos;
 16898   if (!consider_all_windows_p
 16899       && CHARPOS (tlbufpos) > 0
 16900       && !w->update_mode_line
 16901       && !current_buffer->clip_changed
 16902       && !current_buffer->prevent_redisplay_optimizations_p
 16903       && FRAME_REDISPLAY_P (XFRAME (w->frame))
 16904       && !FRAME_OBSCURED_P (XFRAME (w->frame))
 16905       && !XFRAME (w->frame)->cursor_type_changed
 16906       && !XFRAME (w->frame)->face_change
 16907       /* Make sure recorded data applies to current buffer, etc.  */
 16908       && this_line_buffer == current_buffer
 16909       && match_p
 16910       && !w->force_start
 16911       && !w->optional_new_start
 16912       /* Point must be on the line that we have info recorded about.  */
 16913       && PT >= CHARPOS (tlbufpos)
 16914       && PT <= Z - CHARPOS (tlendpos)
 16915       /* FIXME: The following condition is only needed when
 16916          significant parts of the buffer are hidden (e.g., under
 16917          hs-minor-mode), but there doesn't seem to be a simple way of
 16918          detecting that, so we always disable the one-line redisplay
 16919          optimizations whenever display-line-numbers-mode is turned on
 16920          in the buffer.  */
 16921       && (NILP (Vdisplay_line_numbers)
 16922           || EQ (Vdisplay_line_numbers, Qvisual))
 16923       /* All text outside that line, including its final newline,
 16924          must be unchanged.  */
 16925       && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
 16926                                         CHARPOS (tlendpos)))
 16927     {
 16928       if (CHARPOS (tlbufpos) > BEGV
 16929           && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
 16930           && (CHARPOS (tlbufpos) == ZV
 16931               || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
 16932         /* Former continuation line has disappeared by becoming empty.  */
 16933         goto cancel;
 16934       else if (window_outdated (w) || MINI_WINDOW_P (w))
 16935         {
 16936           /* We have to handle the case of continuation around a
 16937              wide-column character (see the comment in indent.c around
 16938              line 1340).
 16939 
 16940              For instance, in the following case:
 16941 
 16942              --------  Insert  --------
 16943              K_A_N_\\   `a'    K_A_N_a\         `X_' are wide-column chars.
 16944              J_I_       ==>    J_I_             `^^' are cursors.
 16945              ^^                ^^
 16946              --------          --------
 16947 
 16948              As we have to redraw the line above, we cannot use this
 16949              optimization.  */
 16950 
 16951           struct it it;
 16952           int line_height_before = this_line_pixel_height;
 16953 
 16954           /* Note that start_display will handle the case that the
 16955              line starting at tlbufpos is a continuation line.  */
 16956           start_display (&it, w, tlbufpos);
 16957 
 16958           /* Implementation note: It this still necessary?  */
 16959           if (it.current_x != this_line_start_x)
 16960             goto cancel;
 16961 
 16962           /* Give up on this optimization if the line starts with a
 16963              string with display property that draws on the fringes,
 16964              as that might interfere with line-prefix display.  */
 16965           if (it.sp > 1
 16966               && it.method == GET_FROM_IMAGE && it.image_id == -1)
 16967             goto cancel;
 16968           redisplay_trace ("trying display optimization 1\n");
 16969           w->cursor.vpos = -1;
 16970           overlay_arrow_seen = false;
 16971           it.vpos = this_line_vpos;
 16972           it.current_y = this_line_y;
 16973           it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
 16974           display_line (&it, -1);
 16975 
 16976           /* If line contains point, is not continued,
 16977              and ends at same distance from eob as before, we win.  */
 16978           if (w->cursor.vpos >= 0
 16979               /* Line is not continued, otherwise this_line_start_pos
 16980                  would have been set to 0 in display_line.  */
 16981               && CHARPOS (this_line_start_pos)
 16982               /* Line ends as before.  */
 16983               && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
 16984               /* Line has same height as before.  Otherwise other lines
 16985                  would have to be shifted up or down.  */
 16986               && this_line_pixel_height == line_height_before
 16987               /* Cannot use this optimization if hscrolling current
 16988                  line and this line is the current one, because
 16989                  display_line above is not informed about the
 16990                  current-line's vpos, and cannot DTRT in that case.  */
 16991               && !hscrolling_current_line_p (w))
 16992             {
 16993               /* If this is not the window's last line, we must adjust
 16994                  the charstarts of the lines below.  */
 16995               if (it.current_y < it.last_visible_y)
 16996                 {
 16997                   struct glyph_row *row
 16998                     = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 16999                   ptrdiff_t delta, delta_bytes;
 17000 
 17001                   /* We used to distinguish between two cases here,
 17002                      conditioned by Z - CHARPOS (tlendpos) == ZV, for
 17003                      when the line ends in a newline or the end of the
 17004                      buffer's accessible portion.  But both cases did
 17005                      the same, so they were collapsed.  */
 17006                   delta = (Z
 17007                            - CHARPOS (tlendpos)
 17008                            - MATRIX_ROW_START_CHARPOS (row));
 17009                   delta_bytes = (Z_BYTE
 17010                                  - BYTEPOS (tlendpos)
 17011                                  - MATRIX_ROW_START_BYTEPOS (row));
 17012 
 17013                   increment_matrix_positions (w->current_matrix,
 17014                                               this_line_vpos + 1,
 17015                                               w->current_matrix->nrows,
 17016                                               delta, delta_bytes);
 17017                 }
 17018 
 17019               /* If this row displays text now but previously didn't,
 17020                  or vice versa, w->window_end_vpos may have to be
 17021                  adjusted.  */
 17022               if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
 17023                 {
 17024                   if (w->window_end_vpos < this_line_vpos)
 17025                     w->window_end_vpos = this_line_vpos;
 17026                 }
 17027               else if (w->window_end_vpos == this_line_vpos
 17028                        && this_line_vpos > 0)
 17029                 w->window_end_vpos = this_line_vpos - 1;
 17030               w->window_end_valid = false;
 17031 
 17032               /* Update hint: No need to try to scroll in update_window.  */
 17033               w->desired_matrix->no_scrolling_p = true;
 17034 
 17035 #ifdef GLYPH_DEBUG
 17036               *w->desired_matrix->method = 0;
 17037               debug_method_add (w, "optimization 1");
 17038 #endif
 17039 #ifdef HAVE_WINDOW_SYSTEM
 17040               update_window_fringes (w, false);
 17041 #endif
 17042               goto update;
 17043             }
 17044           else
 17045             goto cancel;
 17046         }
 17047       else if (/* Cursor position hasn't changed.  */
 17048                PT == w->last_point
 17049                /* Make sure the cursor was last displayed
 17050                   in this window.  Otherwise we have to reposition it.  */
 17051 
 17052                /* PXW: Must be converted to pixels, probably.  */
 17053                && 0 <= w->cursor.vpos
 17054                && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
 17055         {
 17056           if (!must_finish)
 17057             {
 17058               do_pending_window_change (true);
 17059               /* If selected_window changed, redisplay again.  */
 17060               if (WINDOWP (selected_window)
 17061                   && (w = XWINDOW (selected_window)) != sw)
 17062                 goto retry;
 17063 
 17064                 /* We used to always goto end_of_redisplay here, but this
 17065                  isn't enough if we have a blinking cursor.  */
 17066               if (w->cursor_off_p == w->last_cursor_off_p)
 17067                 goto end_of_redisplay;
 17068             }
 17069           goto update;
 17070         }
 17071       /* If highlighting the region, or if the cursor is in the echo area,
 17072          then we can't just move the cursor.  */
 17073       else if (NILP (Vshow_trailing_whitespace)
 17074                && !cursor_in_echo_area
 17075                && !composition_break_at_point)
 17076         {
 17077           struct it it;
 17078           struct glyph_row *row;
 17079 
 17080           /* Skip from tlbufpos to PT and see where it is.  Note that
 17081              PT may be in invisible text.  If so, we will end at the
 17082              next visible position.  */
 17083           init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
 17084                          NULL, DEFAULT_FACE_ID);
 17085           it.current_x = this_line_start_x;
 17086           it.current_y = this_line_y;
 17087           it.vpos = this_line_vpos;
 17088 
 17089           if (current_buffer->long_line_optimizations_p
 17090               && it.line_wrap == TRUNCATE
 17091               && PT - CHARPOS (tlbufpos) > large_hscroll_threshold)
 17092             {
 17093               /* When lines are very long and truncated, jumping to
 17094                  the next visible line is much faster than slowly
 17095                  iterating there.  */
 17096               reseat_at_next_visible_line_start (&it, false);
 17097               if (IT_CHARPOS (it) <= PT) /* point moved off this line */
 17098                 it.vpos = this_line_vpos + 1;
 17099             }
 17100           else
 17101             {
 17102               /* The call to move_it_to stops in front of PT, but
 17103                  moves over before-strings.  */
 17104               move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
 17105             }
 17106 
 17107           if (it.vpos == this_line_vpos
 17108               && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
 17109                   row->enabled_p))
 17110             {
 17111               eassert (this_line_vpos == it.vpos);
 17112               eassert (this_line_y == it.current_y);
 17113               set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 17114               if (cursor_row_fully_visible_p (w, false, true, false))
 17115                 {
 17116 #ifdef GLYPH_DEBUG
 17117                   *w->desired_matrix->method = 0;
 17118                   debug_method_add (w, "optimization 3");
 17119 #endif
 17120                   goto update;
 17121                 }
 17122               else
 17123                 goto cancel;
 17124             }
 17125           else
 17126             goto cancel;
 17127         }
 17128 
 17129     cancel:
 17130       /* Text changed drastically or point moved off of line.  */
 17131       SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
 17132     }
 17133 
 17134   CHARPOS (this_line_start_pos) = 0;
 17135   ++clear_face_cache_count;
 17136 #ifdef HAVE_WINDOW_SYSTEM
 17137   ++clear_image_cache_count;
 17138 #endif
 17139 
 17140   /* Build desired matrices, and update the display.  If
 17141      consider_all_windows_p, do it for all windows on all frames that
 17142      require redisplay, as specified by their 'redisplay' flag.
 17143      Otherwise do it for selected_window, only.  */
 17144 
 17145   if (consider_all_windows_p)
 17146     {
 17147       FOR_EACH_FRAME (tail, frame)
 17148         XFRAME (frame)->updated_p = false;
 17149 
 17150       propagate_buffer_redisplay ();
 17151 
 17152       FOR_EACH_FRAME (tail, frame)
 17153         {
 17154           struct frame *f = XFRAME (frame);
 17155 
 17156           /* We don't have to do anything for unselected terminal
 17157              frames.  */
 17158           if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
 17159               && !EQ (FRAME_TTY (f)->top_frame, frame))
 17160             continue;
 17161 
 17162         retry_frame:
 17163           if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
 17164             {
 17165               bool gcscrollbars
 17166                 /* Only GC scrollbars when we redisplay the whole frame.  */
 17167                 = f->redisplay || !REDISPLAY_SOME_P ();
 17168               bool f_redisplay_flag = f->redisplay;
 17169 
 17170               /* The X error handler may have deleted that frame
 17171                  before we went back to retry_frame.  This must come
 17172                  before any accesses to f->terminal.  */
 17173               if (!FRAME_LIVE_P (f))
 17174                 continue;
 17175 
 17176               /* Mark all the scroll bars to be removed; we'll redeem
 17177                  the ones we want when we redisplay their windows.  */
 17178               if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
 17179                 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
 17180 
 17181               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17182                 {
 17183                   /* Don't allow freeing images and faces for this
 17184                      frame as long as the frame's update wasn't
 17185                      completed.  This prevents crashes when some Lisp
 17186                      that runs from the various hooks or font-lock
 17187                      decides to clear the frame's image cache and face
 17188                      cache, when the images and faces in those caches
 17189                      are referenced by the desired matrix.  */
 17190                   f->inhibit_clear_image_cache = true;
 17191                   redisplay_windows (FRAME_ROOT_WINDOW (f));
 17192                 }
 17193               /* Remember that the invisible frames need to be redisplayed next
 17194                  time they're visible.  */
 17195               else if (!REDISPLAY_SOME_P ())
 17196                 f->redisplay = true;
 17197 
 17198               /* The X error handler may have deleted that frame.  */
 17199               if (!FRAME_LIVE_P (f))
 17200                 continue;
 17201 
 17202               /* Any scroll bars which redisplay_windows should have
 17203                  nuked should now go away.  */
 17204               if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
 17205                 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
 17206 
 17207               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17208                 {
 17209                   /* If fonts changed on visible frame, display again.  */
 17210                   if (f->fonts_changed)
 17211                     {
 17212                       adjust_frame_glyphs (f);
 17213                       /* Disable all redisplay optimizations for this
 17214                          frame.  For the reasons, see the comment near
 17215                          the previous call to adjust_frame_glyphs above.  */
 17216                       SET_FRAME_GARBAGED (f);
 17217                       f->fonts_changed = false;
 17218                       goto retry_frame;
 17219                     }
 17220 
 17221                   /* See if we have to hscroll.  */
 17222                   if (!f->already_hscrolled_p)
 17223                     {
 17224                       f->already_hscrolled_p = true;
 17225                       if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17226                           && hscroll_windows (f->root_window))
 17227                         {
 17228                           hscroll_retries++;
 17229                           goto retry_frame;
 17230                         }
 17231                     }
 17232 
 17233                   /* If the frame's redisplay flag was not set before
 17234                      we went about redisplaying its windows, but it is
 17235                      set now, that means we employed some redisplay
 17236                      optimizations inside redisplay_windows, and
 17237                      bypassed producing some screen lines.  But if
 17238                      f->redisplay is now set, it might mean the old
 17239                      faces are no longer valid (e.g., if redisplaying
 17240                      some window called some Lisp which defined a new
 17241                      face or redefined an existing face), so trying to
 17242                      use them in update_frame will segfault.
 17243                      Therefore, we must redisplay this frame.  */
 17244                   if (!f_redisplay_flag && f->redisplay)
 17245                     goto retry_frame;
 17246                   /* In some case (e.g., window resize), we notice
 17247                      only during window updating that the window
 17248                      content changed unpredictably (e.g., a GTK
 17249                      scrollbar moved, or some Lisp hook that winds up
 17250                      calling adjust_frame_glyphs) and that our
 17251                      previous estimation of the frame content was
 17252                      garbage.  We have to start over.  These cases
 17253                      should be rare, so going all the way back to the
 17254                      top of redisplay should be good enough.  */
 17255                   if (FRAME_GARBAGED_P (f)
 17256                       && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
 17257                     goto retry;
 17258 
 17259 #ifdef HAVE_WINDOW_SYSTEM
 17260                   if (FRAME_WINDOW_P (f)
 17261                       && FRAME_RIF (f)->clear_under_internal_border)
 17262                     FRAME_RIF (f)->clear_under_internal_border (f);
 17263 #endif
 17264                   /* Prevent various kinds of signals during display
 17265                      update.  stdio is not robust about handling
 17266                      signals, which can cause an apparent I/O error.  */
 17267                   if (interrupt_input)
 17268                     unrequest_sigio ();
 17269                   STOP_POLLING;
 17270 
 17271                   pending |= update_frame (f, false, false);
 17272                   /* On some platforms (at least MS-Windows), the
 17273                      scroll_run_hook called from scrolling_window
 17274                      called from update_frame could set the frame's
 17275                      garbaged flag, in which case we need to redisplay
 17276                      the frame.  Don't do that on TTY frames, since we
 17277                      need to keep the garbaged flag in that case when
 17278                      the frame has been resized.  */
 17279                   if (FRAME_GARBAGED_P (f))
 17280                     {
 17281                       fset_redisplay (f);
 17282                       f->garbaged = false;
 17283                       goto retry_frame;
 17284                     }
 17285                   f->cursor_type_changed = false;
 17286                   f->updated_p = true;
 17287                   f->inhibit_clear_image_cache = false;
 17288                 }
 17289             }
 17290         }
 17291 
 17292       eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
 17293 
 17294       if (!pending)
 17295         {
 17296           /* Do the mark_window_display_accurate after all windows have
 17297              been redisplayed because this call resets flags in buffers
 17298              which are needed for proper redisplay.  */
 17299           FOR_EACH_FRAME (tail, frame)
 17300             {
 17301               struct frame *f = XFRAME (frame);
 17302               if (f->updated_p)
 17303                 {
 17304                   f->redisplay = false;
 17305                   f->garbaged = false;
 17306                   mark_window_display_accurate (f->root_window, true);
 17307                   if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 17308                     FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
 17309                 }
 17310             }
 17311         }
 17312     }
 17313   else if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17314     {
 17315       sf->inhibit_clear_image_cache = true;
 17316       displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
 17317       /* Use list_of_error, not Qerror, so that
 17318          we catch only errors and don't run the debugger.  */
 17319       internal_condition_case_1 (redisplay_window_1, selected_window,
 17320                                  list_of_error,
 17321                                  redisplay_window_error);
 17322       if (update_miniwindow_p)
 17323         {
 17324           Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17325 
 17326           displayed_buffer = XBUFFER (XWINDOW (mini_window)->contents);
 17327           internal_condition_case_1 (redisplay_window_1, mini_window,
 17328                                      list_of_error,
 17329                                      redisplay_window_error);
 17330         }
 17331 
 17332       /* Compare desired and current matrices, perform output.  */
 17333 
 17334     update:
 17335       /* If fonts changed, display again.  Likewise if redisplay_window_1
 17336          above caused some change (e.g., a change in faces) that requires
 17337          considering the entire frame again.  */
 17338       if (sf->fonts_changed || sf->redisplay)
 17339         {
 17340           if (sf->redisplay)
 17341             {
 17342               /* Set this to force a more thorough redisplay.
 17343                  Otherwise, we might immediately loop back to the
 17344                  above "else-if" clause (since all the conditions that
 17345                  led here might still be true), and we will then
 17346                  infloop, because the selected-frame's redisplay flag
 17347                  is not (and cannot be) reset.  */
 17348               windows_or_buffers_changed = 50;
 17349             }
 17350           goto retry;
 17351         }
 17352 
 17353       /* Prevent freeing of realized faces, since desired matrices are
 17354          pending that reference the faces we computed and cached.  */
 17355       inhibit_free_realized_faces = true;
 17356 
 17357       /* Prevent various kinds of signals during display update.
 17358          stdio is not robust about handling signals,
 17359          which can cause an apparent I/O error.  */
 17360       if (interrupt_input)
 17361         unrequest_sigio ();
 17362       STOP_POLLING;
 17363 
 17364       if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17365         {
 17366           if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17367               && hscroll_windows (selected_window))
 17368             {
 17369               hscroll_retries++;
 17370               goto retry;
 17371             }
 17372 
 17373           XWINDOW (selected_window)->must_be_updated_p = true;
 17374           pending = update_frame (sf, false, false);
 17375           sf->cursor_type_changed = false;
 17376           sf->inhibit_clear_image_cache = false;
 17377         }
 17378 
 17379       /* We may have called echo_area_display at the top of this
 17380          function.  If the echo area is on another frame, that may
 17381          have put text on a frame other than the selected one, so the
 17382          above call to update_frame would not have caught it.  Catch
 17383          it here.  */
 17384       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17385       struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 17386 
 17387       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
 17388         {
 17389           XWINDOW (mini_window)->must_be_updated_p = true;
 17390           pending |= update_frame (mini_frame, false, false);
 17391           mini_frame->cursor_type_changed = false;
 17392           if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
 17393               && hscroll_windows (mini_window))
 17394             {
 17395               hscroll_retries++;
 17396               goto retry;
 17397             }
 17398         }
 17399     }
 17400 
 17401   /* If display was paused because of pending input, make sure we do a
 17402      thorough update the next time.  */
 17403   if (pending)
 17404     {
 17405       /* Prevent the optimization at the beginning of
 17406          redisplay_internal that tries a single-line update of the
 17407          line containing the cursor in the selected window.  */
 17408       CHARPOS (this_line_start_pos) = 0;
 17409 
 17410       /* Let the overlay arrow be updated the next time.  */
 17411       update_overlay_arrows (0);
 17412 
 17413       /* If we pause after scrolling, some rows in the current
 17414          matrices of some windows are not valid.  */
 17415       if (!WINDOW_FULL_WIDTH_P (w)
 17416           && !FRAME_WINDOW_P (XFRAME (w->frame)))
 17417         update_mode_lines = 36;
 17418     }
 17419   else
 17420     {
 17421       if (!consider_all_windows_p)
 17422         {
 17423           /* This has already been done above if
 17424              consider_all_windows_p is set.  */
 17425           if (XBUFFER (w->contents)->text->redisplay
 17426               && buffer_window_count (XBUFFER (w->contents)) > 1)
 17427             /* This can happen if b->text->redisplay was set during
 17428                jit-lock.  */
 17429             propagate_buffer_redisplay ();
 17430           mark_window_display_accurate_1 (w, true);
 17431 
 17432           /* Say overlay arrows are up to date.  */
 17433           update_overlay_arrows (1);
 17434 
 17435           if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
 17436             FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
 17437         }
 17438 
 17439       update_mode_lines = 0;
 17440       windows_or_buffers_changed = 0;
 17441     }
 17442 
 17443   /* Start SIGIO interrupts coming again.  Having them off during the
 17444      code above makes it less likely one will discard output, but not
 17445      impossible, since there might be stuff in the system buffer here.
 17446      But it is much hairier to try to do anything about that.  */
 17447   if (interrupt_input)
 17448     request_sigio ();
 17449   RESUME_POLLING;
 17450 
 17451   /* If a frame has become visible which was not before, redisplay
 17452      again, so that we display it.  Expose events for such a frame
 17453      (which it gets when becoming visible) don't call the parts of
 17454      redisplay constructing glyphs, so simply exposing a frame won't
 17455      display anything in this case.  So, we have to display these
 17456      frames here explicitly.  */
 17457   if (!pending)
 17458     {
 17459       int new_count = 0;
 17460 
 17461       FOR_EACH_FRAME (tail, frame)
 17462         {
 17463           if (FRAME_REDISPLAY_P (XFRAME (frame)))
 17464             new_count++;
 17465         }
 17466 
 17467       if (new_count != number_of_visible_frames)
 17468         windows_or_buffers_changed = 52;
 17469     }
 17470 
 17471   /* Change frame size now if a change is pending.  */
 17472   do_pending_window_change (true);
 17473 
 17474   /* If we just did a pending size change, or have additional
 17475      visible frames, or selected_window changed, redisplay again.  */
 17476   if ((windows_or_buffers_changed && !pending)
 17477       || (WINDOWP (selected_window)
 17478           && (w = XWINDOW (selected_window)) != sw))
 17479     goto retry;
 17480 
 17481   /* Clear the face and image caches.
 17482 
 17483      We used to do this only if consider_all_windows_p.  But the cache
 17484      needs to be cleared if a timer creates images in the current
 17485      buffer (e.g. the test case in Bug#6230).  */
 17486 
 17487   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
 17488     {
 17489       clear_face_cache (false);
 17490       clear_face_cache_count = 0;
 17491     }
 17492 
 17493 #ifdef HAVE_WINDOW_SYSTEM
 17494   if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
 17495     {
 17496       clear_image_caches (Qnil);
 17497       clear_image_cache_count = 0;
 17498     }
 17499 #endif /* HAVE_WINDOW_SYSTEM */
 17500 
 17501  end_of_redisplay:
 17502 #ifdef HAVE_NS
 17503   ns_set_doc_edited ();
 17504 #endif
 17505   if (interrupt_input && interrupts_deferred)
 17506     request_sigio ();
 17507 
 17508   /* We're done with this redisplay cycle, so reset the tick count in
 17509      preparation for the next redisplay cycle.  */
 17510   if (max_redisplay_ticks > 0)
 17511     update_redisplay_ticks (0, NULL);
 17512 
 17513   unbind_to (count, Qnil);
 17514   RESUME_POLLING;
 17515 }
 17516 
 17517 static void
 17518 unwind_redisplay_preserve_echo_area (void)
 17519 {
 17520   unblock_buffer_flips ();
 17521 }
 17522 
 17523 /* Redisplay, but leave alone any recent echo area message unless
 17524    another message has been requested in its place.
 17525 
 17526    This is useful in situations where you need to redisplay but no
 17527    user action has occurred, making it inappropriate for the message
 17528    area to be cleared.  See tracking_off and
 17529    wait_reading_process_output for examples of these situations.
 17530 
 17531    FROM_WHERE is an integer saying from where this function was
 17532    called.  This is useful for debugging.  */
 17533 
 17534 void
 17535 redisplay_preserve_echo_area (int from_where)
 17536 {
 17537   redisplay_trace ("redisplay_preserve_echo_area (%d)\n", from_where);
 17538 
 17539   block_input ();
 17540   specpdl_ref count = SPECPDL_INDEX ();
 17541   record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
 17542   block_buffer_flips ();
 17543   unblock_input ();
 17544 
 17545   if (!NILP (echo_area_buffer[1]))
 17546     {
 17547       /* We have a previously displayed message, but no current
 17548          message.  Redisplay the previous message.  */
 17549       display_last_displayed_message_p = true;
 17550       redisplay_internal ();
 17551       display_last_displayed_message_p = false;
 17552     }
 17553   else
 17554     redisplay_internal ();
 17555 
 17556   flush_frame (SELECTED_FRAME ());
 17557   unbind_to (count, Qnil);
 17558 }
 17559 
 17560 
 17561 /* Function registered with record_unwind_protect in redisplay_internal.  */
 17562 
 17563 static void
 17564 unwind_redisplay (void)
 17565 {
 17566   redisplaying_p = false;
 17567   unblock_buffer_flips ();
 17568 }
 17569 
 17570 /* Function registered with record_unwind_protect before calling
 17571    start_display outside of redisplay_internal.  */
 17572 void
 17573 unwind_display_working_on_window (void)
 17574 {
 17575   display_working_on_window_p = false;
 17576 }
 17577 
 17578 /* Mark the display of leaf window W as accurate or inaccurate.
 17579    If ACCURATE_P, mark display of W as accurate.
 17580    If !ACCURATE_P, arrange for W to be redisplayed the next
 17581    time redisplay_internal is called.  */
 17582 
 17583 static void
 17584 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 17585 {
 17586   struct buffer *b = XBUFFER (w->contents);
 17587 #ifdef HAVE_TEXT_CONVERSION
 17588   ptrdiff_t prev_point, prev_mark;
 17589 #endif /* HAVE_TEXT_CONVERSION */
 17590 
 17591   w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
 17592   w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
 17593   w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
 17594 
 17595   if (accurate_p)
 17596     {
 17597       b->clip_changed = false;
 17598       b->prevent_redisplay_optimizations_p = false;
 17599       eassert (buffer_window_count (b) > 0);
 17600       /* Resetting b->text->redisplay is problematic!
 17601          In order to make it safer to do it here, redisplay_internal must
 17602          have copied all b->text->redisplay to their respective windows.  */
 17603       b->text->redisplay = false;
 17604 
 17605       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
 17606       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
 17607       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
 17608       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 17609 
 17610       w->current_matrix->buffer = b;
 17611       w->current_matrix->begv = BUF_BEGV (b);
 17612       w->current_matrix->zv = BUF_ZV (b);
 17613       w->current_matrix->header_line_p = window_wants_header_line (w);
 17614       w->current_matrix->tab_line_p = window_wants_tab_line (w);
 17615 
 17616       w->last_cursor_vpos = w->cursor.vpos;
 17617       w->last_cursor_off_p = w->cursor_off_p;
 17618 
 17619 #ifdef HAVE_TEXT_CONVERSION
 17620       prev_point = w->last_point;
 17621       prev_mark = w->last_mark;
 17622 #endif /* HAVE_TEXT_CONVERSION */
 17623 
 17624       if (w == XWINDOW (selected_window))
 17625         w->last_point = BUF_PT (b);
 17626       else
 17627         w->last_point = marker_position (w->pointm);
 17628 
 17629       /* w->last_mark is recorded for text conversion purposes.
 17630          Input methods aren't interested in the value of the mark
 17631          if it is inactive, so set it to -1 if it's not.  */
 17632 
 17633       if (XMARKER (BVAR (b, mark))->buffer == b
 17634           && !NILP (BVAR (b, mark_active)))
 17635         w->last_mark = marker_position (BVAR (b, mark));
 17636       else
 17637         w->last_mark = -1;
 17638 
 17639 #ifdef HAVE_TEXT_CONVERSION
 17640       /* See the description of this field in struct window.  */
 17641       w->ephemeral_last_point = w->last_point;
 17642 
 17643       /* Point motion is only propagated to the input method for use
 17644          in text conversion during a redisplay.  While this can lead
 17645          to inconsistencies when point has moved but the change has
 17646          not yet been displayed, it leads to better results most of
 17647          the time, as point often changes within calls to
 17648          `save-excursion', and the only way to detect such calls is to
 17649          observe that the next redisplay never ends with those changes
 17650          applied.
 17651 
 17652          Changes to buffer text are immediately propagated to the
 17653          input method, and the position of point is also updated
 17654          during such a change, so the consequences are not that
 17655          severe.  */
 17656 
 17657       if ((prev_point != w->last_point
 17658            || prev_mark != w->last_mark)
 17659           && FRAME_WINDOW_P (WINDOW_XFRAME (w))
 17660           && w == XWINDOW (WINDOW_XFRAME (w)->selected_window))
 17661         report_point_change (WINDOW_XFRAME (w), w, b);
 17662 #endif /* HAVE_TEXT_CONVERSION */
 17663 
 17664       struct glyph_row *row;
 17665       /* These conditions should be consistent with CHECK_WINDOW_END.  */
 17666       if (w->window_end_vpos < w->current_matrix->nrows
 17667           && ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
 17668                !row->enabled_p
 17669                || MATRIX_ROW_DISPLAYS_TEXT_P (row)
 17670                || MATRIX_ROW_VPOS (row, w->current_matrix) == 0)))
 17671         w->window_end_valid = true;
 17672       w->update_mode_line = false;
 17673       w->preserve_vscroll_p = false;
 17674     }
 17675 
 17676   w->redisplay = !accurate_p;
 17677 }
 17678 
 17679 
 17680 /* Mark the display of windows in the window tree rooted at WINDOW as
 17681    accurate or inaccurate.  If ACCURATE_P, mark display of
 17682    windows as accurate.  If !ACCURATE_P, arrange for windows to
 17683    be redisplayed the next time redisplay_internal is called.  */
 17684 
 17685 void
 17686 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
 17687 {
 17688   struct window *w;
 17689 
 17690   for (; !NILP (window); window = w->next)
 17691     {
 17692       w = XWINDOW (window);
 17693       if (WINDOWP (w->contents))
 17694         mark_window_display_accurate (w->contents, accurate_p);
 17695       else
 17696         mark_window_display_accurate_1 (w, accurate_p);
 17697     }
 17698 
 17699   if (accurate_p)
 17700     update_overlay_arrows (1);
 17701   else
 17702     /* Force a thorough redisplay the next time by setting
 17703        last_arrow_position and last_arrow_string to t, which is
 17704        unequal to any useful value of Voverlay_arrow_...  */
 17705     update_overlay_arrows (-1);
 17706 }
 17707 
 17708 
 17709 /* Return value in display table DP (Lisp_Char_Table *) for character
 17710    C.  Since a display table doesn't have any parent, we don't have to
 17711    follow parent.  Do not call this function directly but use the
 17712    macro DISP_CHAR_VECTOR.  */
 17713 
 17714 Lisp_Object
 17715 disp_char_vector (struct Lisp_Char_Table *dp, int c)
 17716 {
 17717   Lisp_Object val;
 17718 
 17719   if (ASCII_CHAR_P (c))
 17720     {
 17721       val = dp->ascii;
 17722       if (SUB_CHAR_TABLE_P (val))
 17723         val = XSUB_CHAR_TABLE (val)->contents[c];
 17724     }
 17725   else
 17726     {
 17727       Lisp_Object table;
 17728 
 17729       XSETCHAR_TABLE (table, dp);
 17730       val = char_table_ref (table, c);
 17731     }
 17732   if (NILP (val))
 17733     val = dp->defalt;
 17734   return val;
 17735 }
 17736 
 17737 static int buffer_flip_blocked_depth;
 17738 
 17739 static void
 17740 block_buffer_flips (void)
 17741 {
 17742   eassert (buffer_flip_blocked_depth >= 0);
 17743   buffer_flip_blocked_depth++;
 17744 }
 17745 
 17746 static void
 17747 unblock_buffer_flips (void)
 17748 {
 17749   eassert (buffer_flip_blocked_depth > 0);
 17750   if (--buffer_flip_blocked_depth == 0)
 17751     {
 17752       Lisp_Object tail, frame;
 17753       block_input ();
 17754       FOR_EACH_FRAME (tail, frame)
 17755         {
 17756           struct frame *f = XFRAME (frame);
 17757           if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
 17758             (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
 17759         }
 17760       unblock_input ();
 17761     }
 17762 }
 17763 
 17764 bool
 17765 buffer_flipping_blocked_p (void)
 17766 {
 17767   return buffer_flip_blocked_depth > 0;
 17768 }
 17769 
 17770 
 17771 /***********************************************************************
 17772                            Window Redisplay
 17773  ***********************************************************************/
 17774 
 17775 /* Redisplay all leaf windows in the window tree rooted at WINDOW.  */
 17776 
 17777 static void
 17778 redisplay_windows (Lisp_Object window)
 17779 {
 17780   while (!NILP (window))
 17781     {
 17782       struct window *w = XWINDOW (window);
 17783 
 17784       if (WINDOWP (w->contents))
 17785         redisplay_windows (w->contents);
 17786       else if (BUFFERP (w->contents))
 17787         {
 17788           displayed_buffer = XBUFFER (w->contents);
 17789           /* Use list_of_error, not Qerror, so that
 17790              we catch only errors and don't run the debugger.  */
 17791           internal_condition_case_1 (redisplay_window_0, window,
 17792                                      list_of_error,
 17793                                      redisplay_window_error);
 17794         }
 17795 
 17796       window = w->next;
 17797     }
 17798 }
 17799 
 17800 static Lisp_Object
 17801 redisplay_window_error (Lisp_Object error_data)
 17802 {
 17803   displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
 17804 
 17805   /* When in redisplay, the error is captured and not shown.  Arrange
 17806      for it to be shown later.  */
 17807   if (max_redisplay_ticks > 0
 17808       && CONSP (error_data)
 17809       && EQ (XCAR (error_data), Qerror)
 17810       && CONSP (XCDR (error_data))
 17811       && STRINGP (XCAR (XCDR (error_data))))
 17812     Vdelayed_warnings_list = Fcons (list2 (XCAR (error_data),
 17813                                            XCAR (XCDR (error_data))),
 17814                                     Vdelayed_warnings_list);
 17815   return Qnil;
 17816 }
 17817 
 17818 static Lisp_Object
 17819 redisplay_window_0 (Lisp_Object window)
 17820 {
 17821   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17822     redisplay_window (window, false);
 17823   return Qnil;
 17824 }
 17825 
 17826 static Lisp_Object
 17827 redisplay_window_1 (Lisp_Object window)
 17828 {
 17829   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17830     redisplay_window (window, true);
 17831   return Qnil;
 17832 }
 17833 
 17834 
 17835 /***********************************************************************
 17836                       Aborting runaway redisplay
 17837  ***********************************************************************/
 17838 
 17839 /* Update the redisplay-tick count for window W, and signal an error
 17840    if the tick count is above some threshold, indicating that
 17841    redisplay of the window takes "too long".
 17842 
 17843    TICKS is the amount of ticks to add to the W's current count; zero
 17844    means to initialize the tick count to zero.
 17845 
 17846    W can be NULL if TICKS is zero: that means unconditionally
 17847    re-initialize the current tick count to zero.
 17848 
 17849    W can also be NULL if the caller doesn't know which window is being
 17850    processed by the display code.  In that case, if TICKS is non-zero,
 17851    we assume it's the last window that shows the current buffer.  */
 17852 void
 17853 update_redisplay_ticks (int ticks, struct window *w)
 17854 {
 17855   /* This keeps track of the window on which redisplay is working.  */
 17856   static struct window *cwindow;
 17857   static EMACS_INT window_ticks;
 17858 
 17859   /* We only initialize the count if this is a different window or
 17860      NULL.  Otherwise, this is a call from init_iterator for the same
 17861      window we tracked before, and we should keep the count.  */
 17862   if (!ticks && w != cwindow)
 17863     {
 17864       cwindow = w;
 17865       window_ticks = 0;
 17866     }
 17867   /* Some callers can be run in contexts unrelated to display code, so
 17868      don't abort them and don't update the tick count in those cases.  */
 17869   if ((!w && !redisplaying_p && !display_working_on_window_p)
 17870       /* We never disable redisplay of a mini-window, since that is
 17871          absolutely essential for communicating with Emacs.  */
 17872       || (w && MINI_WINDOW_P (w)))
 17873     return;
 17874 
 17875   if (ticks > 0)
 17876     window_ticks += ticks;
 17877   if (max_redisplay_ticks > 0 && window_ticks > max_redisplay_ticks)
 17878     {
 17879       /* In addition to a buffer, this could be a window (for non-leaf
 17880          windows, not expected here) or nil (for pseudo-windows like
 17881          the one used for the native tool bar).  */
 17882       Lisp_Object contents = w ? w->contents : Qnil;
 17883       char *bufname =
 17884         NILP (contents)
 17885         ? SSDATA (BVAR (current_buffer, name))
 17886         : (BUFFERP (contents)
 17887            ? SSDATA (BVAR (XBUFFER (contents), name))
 17888            : (char *) "<unknown>");
 17889 
 17890       windows_or_buffers_changed = 177;
 17891       /* scrolling_window depends too much on the glyph matrices being
 17892          correct, and we cannot guarantee that if we abort the
 17893          redisplay of this window.  */
 17894       if (w && w->desired_matrix)
 17895         w->desired_matrix->no_scrolling_p = true;
 17896       error ("Window showing buffer %s takes too long to redisplay", bufname);
 17897     }
 17898 }
 17899 
 17900 
 17901 
 17902 /* Set cursor position of W.  PT is assumed to be displayed in ROW.
 17903    DELTA and DELTA_BYTES are the numbers of characters and bytes by
 17904    which positions recorded in ROW differ from current buffer
 17905    positions.
 17906 
 17907    Return true iff cursor is on this row.  */
 17908 
 17909 static bool
 17910 set_cursor_from_row (struct window *w, struct glyph_row *row,
 17911                      struct glyph_matrix *matrix,
 17912                      ptrdiff_t delta, ptrdiff_t delta_bytes,
 17913                      int dy, int dvpos)
 17914 {
 17915   struct glyph *glyph = row->glyphs[TEXT_AREA];
 17916   struct glyph *end = glyph + row->used[TEXT_AREA];
 17917   struct glyph *cursor = NULL;
 17918   /* The last known character position in row.  */
 17919   ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 17920   int x = row->x;
 17921   ptrdiff_t pt_old = PT - delta;
 17922   ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 17923   ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 17924   struct glyph *glyph_before = glyph - 1, *glyph_after = end;
 17925   /* A glyph beyond the edge of TEXT_AREA which we should never
 17926      touch.  */
 17927   struct glyph *glyphs_end = end;
 17928   /* True means we've found a match for cursor position, but that
 17929      glyph has the avoid_cursor_p flag set.  */
 17930   bool match_with_avoid_cursor = false;
 17931   /* True means we've seen at least one glyph that came from a
 17932      display string.  */
 17933   bool string_seen = false;
 17934   /* Largest and smallest buffer positions seen so far during scan of
 17935      glyph row.  */
 17936   ptrdiff_t bpos_max = pos_before;
 17937   ptrdiff_t bpos_min = pos_after;
 17938   /* Last buffer position covered by an overlay string with an integer
 17939      `cursor' property.  */
 17940   ptrdiff_t bpos_covered = 0;
 17941   /* True means the display string on which to display the cursor
 17942      comes from a text property, not from an overlay.  */
 17943   bool string_from_text_prop = false;
 17944 
 17945   /* Don't even try doing anything if called for a mode-line or
 17946      header-line or tab-line row, since the rest of the code isn't
 17947      prepared to deal with such calamities.  */
 17948   eassert (!row->mode_line_p);
 17949   if (row->mode_line_p)
 17950     return false;
 17951 
 17952   /* Skip over glyphs not having an object at the start and the end of
 17953      the row.  These are special glyphs like truncation marks on
 17954      terminal frames.  */
 17955   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 17956     {
 17957       if (!row->reversed_p)
 17958         {
 17959           while (glyph < end
 17960                  && NILP (glyph->object)
 17961                  && glyph->charpos < 0)
 17962             {
 17963               x += glyph->pixel_width;
 17964               ++glyph;
 17965             }
 17966           while (end > glyph
 17967                  && NILP ((end - 1)->object)
 17968                  /* CHARPOS is zero for blanks and stretch glyphs
 17969                     inserted by extend_face_to_end_of_line.  */
 17970                  && (end - 1)->charpos <= 0)
 17971             --end;
 17972           glyph_before = glyph - 1;
 17973           glyph_after = end;
 17974         }
 17975       else
 17976         {
 17977           struct glyph *g;
 17978 
 17979           /* If the glyph row is reversed, we need to process it from back
 17980              to front, so swap the edge pointers.  */
 17981           glyphs_end = end = glyph - 1;
 17982           glyph += row->used[TEXT_AREA] - 1;
 17983 
 17984           while (glyph > end + 1
 17985                  && NILP (glyph->object)
 17986                  && glyph->charpos < 0)
 17987             --glyph;
 17988           if (NILP (glyph->object) && glyph->charpos < 0)
 17989             --glyph;
 17990           /* By default, in reversed rows we put the cursor on the
 17991              rightmost (first in the reading order) glyph.  */
 17992           for (x = 0, g = end + 1; g < glyph; g++)
 17993             x += g->pixel_width;
 17994           while (end < glyph
 17995                  && NILP ((end + 1)->object)
 17996                  && (end + 1)->charpos <= 0)
 17997             ++end;
 17998           glyph_before = glyph + 1;
 17999           glyph_after = end;
 18000         }
 18001     }
 18002   else if (row->reversed_p)
 18003     {
 18004       /* In R2L rows that don't display text, put the cursor on the
 18005          rightmost glyph.  Case in point: an empty last line that is
 18006          part of an R2L paragraph.  */
 18007       cursor = end - 1;
 18008       /* Avoid placing the cursor on the last glyph of the row, where
 18009          on terminal frames we hold the vertical border between
 18010          adjacent windows.  */
 18011       if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
 18012           && !WINDOW_RIGHTMOST_P (w)
 18013           && cursor == row->glyphs[LAST_AREA] - 1)
 18014         cursor--;
 18015       x = -1;   /* will be computed below, at label compute_x */
 18016     }
 18017 
 18018   /* Step 1: Try to find the glyph whose character position
 18019      corresponds to point.  If that's not possible, find 2 glyphs
 18020      whose character positions are the closest to point, one before
 18021      point, the other after it.  */
 18022   if (!row->reversed_p)
 18023     while (/* not marched to end of glyph row */
 18024            glyph < end
 18025            /* glyph was not inserted by redisplay for internal purposes */
 18026            && !NILP (glyph->object))
 18027       {
 18028         if (BUFFERP (glyph->object))
 18029           {
 18030             ptrdiff_t dpos = glyph->charpos - pt_old;
 18031 
 18032             if (glyph->charpos > bpos_max)
 18033               bpos_max = glyph->charpos;
 18034             if (glyph->charpos < bpos_min)
 18035               bpos_min = glyph->charpos;
 18036             if (!glyph->avoid_cursor_p)
 18037               {
 18038                 /* If we hit point, we've found the glyph on which to
 18039                    display the cursor.  */
 18040                 if (dpos == 0)
 18041                   {
 18042                     match_with_avoid_cursor = false;
 18043                     break;
 18044                   }
 18045                 /* See if we've found a better approximation to
 18046                    POS_BEFORE or to POS_AFTER.  */
 18047                 if (0 > dpos && dpos > pos_before - pt_old)
 18048                   {
 18049                     pos_before = glyph->charpos;
 18050                     glyph_before = glyph;
 18051                   }
 18052                 else if (0 < dpos && dpos < pos_after - pt_old)
 18053                   {
 18054                     pos_after = glyph->charpos;
 18055                     glyph_after = glyph;
 18056                   }
 18057               }
 18058             else if (dpos == 0)
 18059               match_with_avoid_cursor = true;
 18060           }
 18061         else if (STRINGP (glyph->object))
 18062           {
 18063             Lisp_Object chprop;
 18064             ptrdiff_t glyph_pos = glyph->charpos;
 18065 
 18066             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 18067                                          glyph->object);
 18068             if (!NILP (chprop))
 18069               {
 18070                 /* If the string came from a `display' text property,
 18071                    look up the buffer position of that property and
 18072                    use that position to update bpos_max, as if we
 18073                    actually saw such a position in one of the row's
 18074                    glyphs.  This helps with supporting integer values
 18075                    of `cursor' property on the display string in
 18076                    situations where most or all of the row's buffer
 18077                    text is completely covered by display properties,
 18078                    so that no glyph with valid buffer positions is
 18079                    ever seen in the row.  */
 18080                 ptrdiff_t prop_pos =
 18081                   string_buffer_position_lim (glyph->object, pos_before,
 18082                                               pos_after, false);
 18083 
 18084                 if (prop_pos >= pos_before)
 18085                   bpos_max = prop_pos;
 18086               }
 18087             if (FIXNUMP (chprop))
 18088               {
 18089                 bpos_covered = bpos_max + XFIXNUM (chprop);
 18090                 /* If the `cursor' property covers buffer positions up
 18091                    to and including point, we should display cursor on
 18092                    this glyph.  Note that, if a `cursor' property on one
 18093                    of the string's characters has an integer value, we
 18094                    will break out of the loop below _before_ we get to
 18095                    the position match above.  IOW, integer values of
 18096                    the `cursor' property override the "exact match for
 18097                    point" strategy of positioning the cursor.  */
 18098                 /* Implementation note: bpos_max == pt_old when, e.g.,
 18099                    we are in an empty line, where bpos_max is set to
 18100                    MATRIX_ROW_START_CHARPOS, see above.  */
 18101                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 18102                   {
 18103                     cursor = glyph;
 18104                     break;
 18105                   }
 18106               }
 18107 
 18108             string_seen = true;
 18109           }
 18110         x += glyph->pixel_width;
 18111         ++glyph;
 18112       }
 18113   else if (glyph > end) /* row is reversed */
 18114     while (!NILP (glyph->object))
 18115       {
 18116         if (BUFFERP (glyph->object))
 18117           {
 18118             ptrdiff_t dpos = glyph->charpos - pt_old;
 18119 
 18120             if (glyph->charpos > bpos_max)
 18121               bpos_max = glyph->charpos;
 18122             if (glyph->charpos < bpos_min)
 18123               bpos_min = glyph->charpos;
 18124             if (!glyph->avoid_cursor_p)
 18125               {
 18126                 if (dpos == 0)
 18127                   {
 18128                     match_with_avoid_cursor = false;
 18129                     break;
 18130                   }
 18131                 if (0 > dpos && dpos > pos_before - pt_old)
 18132                   {
 18133                     pos_before = glyph->charpos;
 18134                     glyph_before = glyph;
 18135                   }
 18136                 else if (0 < dpos && dpos < pos_after - pt_old)
 18137                   {
 18138                     pos_after = glyph->charpos;
 18139                     glyph_after = glyph;
 18140                   }
 18141               }
 18142             else if (dpos == 0)
 18143               match_with_avoid_cursor = true;
 18144           }
 18145         else if (STRINGP (glyph->object))
 18146           {
 18147             Lisp_Object chprop;
 18148             ptrdiff_t glyph_pos = glyph->charpos;
 18149 
 18150             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 18151                                          glyph->object);
 18152             if (!NILP (chprop))
 18153               {
 18154                 ptrdiff_t prop_pos =
 18155                   string_buffer_position_lim (glyph->object, pos_before,
 18156                                               pos_after, false);
 18157 
 18158                 if (prop_pos >= pos_before)
 18159                   bpos_max = prop_pos;
 18160               }
 18161             if (FIXNUMP (chprop))
 18162               {
 18163                 bpos_covered = bpos_max + XFIXNUM (chprop);
 18164                 /* If the `cursor' property covers buffer positions up
 18165                    to and including point, we should display cursor on
 18166                    this glyph.  */
 18167                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 18168                   {
 18169                     cursor = glyph;
 18170                     break;
 18171                   }
 18172               }
 18173             string_seen = true;
 18174           }
 18175         --glyph;
 18176         if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
 18177           {
 18178             x--;                /* can't use any pixel_width */
 18179             break;
 18180           }
 18181         x -= glyph->pixel_width;
 18182     }
 18183 
 18184   /* Step 2: If we didn't find an exact match for point, we need to
 18185      look for a proper place to put the cursor among glyphs between
 18186      GLYPH_BEFORE and GLYPH_AFTER.  */
 18187   if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18188         && BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18189       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
 18190     {
 18191       /* An empty line has a single glyph whose OBJECT is nil and
 18192          whose CHARPOS is the position of a newline on that line.
 18193          Note that on a TTY, there are more glyphs after that, which
 18194          were produced by extend_face_to_end_of_line, but their
 18195          CHARPOS is zero or negative.  */
 18196       bool empty_line_p =
 18197         ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18198          && NILP (glyph->object) && glyph->charpos > 0
 18199          /* On a TTY, continued and truncated rows also have a glyph at
 18200             their end whose OBJECT is nil and whose CHARPOS is
 18201             positive (the continuation and truncation glyphs), but such
 18202             rows are obviously not "empty".  */
 18203          && !(row->continued_p || row->truncated_on_right_p));
 18204 
 18205       if (row->ends_in_ellipsis_p && pos_after == last_pos)
 18206         {
 18207           ptrdiff_t ellipsis_pos;
 18208 
 18209           /* Scan back over the ellipsis glyphs.  */
 18210           if (!row->reversed_p)
 18211             {
 18212               ellipsis_pos = (glyph - 1)->charpos;
 18213               while (glyph > row->glyphs[TEXT_AREA]
 18214                      && (glyph - 1)->charpos == ellipsis_pos)
 18215                 glyph--, x -= glyph->pixel_width;
 18216               /* That loop always goes one position too far, including
 18217                  the glyph before the ellipsis.  So scan forward over
 18218                  that one.  */
 18219               x += glyph->pixel_width;
 18220               glyph++;
 18221             }
 18222           else  /* row is reversed */
 18223             {
 18224               ellipsis_pos = (glyph + 1)->charpos;
 18225               while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18226                      && (glyph + 1)->charpos == ellipsis_pos)
 18227                 glyph++, x += glyph->pixel_width;
 18228               x -= glyph->pixel_width;
 18229               glyph--;
 18230             }
 18231         }
 18232       else if (match_with_avoid_cursor)
 18233         {
 18234           cursor = glyph_after;
 18235           x = -1;
 18236         }
 18237       else if (string_seen)
 18238         {
 18239           int incr = row->reversed_p ? -1 : +1;
 18240 
 18241           /* Need to find the glyph that came out of a string which is
 18242              present at point.  That glyph is somewhere between
 18243              GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
 18244              positioned between POS_BEFORE and POS_AFTER in the
 18245              buffer.  */
 18246           struct glyph *start, *stop;
 18247           ptrdiff_t pos = pos_before;
 18248 
 18249           x = -1;
 18250 
 18251           /* If the row ends in a newline from a display string,
 18252              reordering could have moved the glyphs belonging to the
 18253              string out of the [GLYPH_BEFORE..GLYPH_AFTER] range.  So
 18254              in this case we extend the search to the last glyph in
 18255              the row that was not inserted by redisplay.  */
 18256           if (row->ends_in_newline_from_string_p)
 18257             {
 18258               glyph_after = end;
 18259               pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 18260             }
 18261 
 18262           /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
 18263              correspond to POS_BEFORE and POS_AFTER, respectively.  We
 18264              need START and STOP in the order that corresponds to the
 18265              row's direction as given by its reversed_p flag.  If the
 18266              directionality of characters between POS_BEFORE and
 18267              POS_AFTER is the opposite of the row's base direction,
 18268              these characters will have been reordered for display,
 18269              and we need to reverse START and STOP.  */
 18270           if (!row->reversed_p)
 18271             {
 18272               start = min (glyph_before, glyph_after);
 18273               stop = max (glyph_before, glyph_after);
 18274             }
 18275           else
 18276             {
 18277               start = max (glyph_before, glyph_after);
 18278               stop = min (glyph_before, glyph_after);
 18279             }
 18280           for (glyph = start + incr;
 18281                row->reversed_p ? glyph > stop : glyph < stop; )
 18282             {
 18283 
 18284               /* Any glyphs that come from the buffer are here because
 18285                  of bidi reordering.  Skip them, and only pay
 18286                  attention to glyphs that came from some string.  */
 18287               if (STRINGP (glyph->object))
 18288                 {
 18289                   Lisp_Object str;
 18290                   ptrdiff_t tem;
 18291                   /* If the display property covers the newline, we
 18292                      need to search for it one position farther.  */
 18293                   ptrdiff_t lim = pos_after
 18294                     + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
 18295 
 18296                   string_from_text_prop = false;
 18297                   str = glyph->object;
 18298                   tem = string_buffer_position_lim (str, pos, lim, false);
 18299                   if (tem == 0  /* from overlay */
 18300                       || pos <= tem)
 18301                     {
 18302                       /* If the string from which this glyph came is
 18303                          found in the buffer at point, or at position
 18304                          that is closer to point than pos_after, then
 18305                          we've found the glyph we've been looking for.
 18306                          If it comes from an overlay (tem == 0), and
 18307                          it has the `cursor' property on one of its
 18308                          glyphs, record that glyph as a candidate for
 18309                          displaying the cursor.  (As in the
 18310                          unidirectional version, we will display the
 18311                          cursor on the last candidate we find.)  */
 18312                       if (tem == 0
 18313                           || tem == pt_old
 18314                           || (tem - pt_old > 0 && tem < pos_after))
 18315                         {
 18316                           /* The glyphs from this string could have
 18317                              been reordered.  Find the one with the
 18318                              smallest string position.  Or there could
 18319                              be a character in the string with the
 18320                              `cursor' property, which means display
 18321                              cursor on that character's glyph.  */
 18322                           ptrdiff_t strpos = glyph->charpos;
 18323 
 18324                           if (tem)
 18325                             {
 18326                               cursor = glyph;
 18327                               string_from_text_prop = true;
 18328                             }
 18329                           for ( ;
 18330                                (row->reversed_p ? glyph > stop : glyph < stop)
 18331                                  && EQ (glyph->object, str);
 18332                                glyph += incr)
 18333                             {
 18334                               Lisp_Object cprop;
 18335                               ptrdiff_t gpos = glyph->charpos;
 18336 
 18337                               cprop = Fget_char_property (make_fixnum (gpos),
 18338                                                           Qcursor,
 18339                                                           glyph->object);
 18340                               if (!NILP (cprop))
 18341                                 {
 18342                                   cursor = glyph;
 18343                                   break;
 18344                                 }
 18345                               if (tem && glyph->charpos < strpos)
 18346                                 {
 18347                                   strpos = glyph->charpos;
 18348                                   cursor = glyph;
 18349                                 }
 18350                             }
 18351 
 18352                           if (tem == pt_old
 18353                               || (tem - pt_old > 0 && tem < pos_after))
 18354                             goto compute_x;
 18355                         }
 18356                       if (tem)
 18357                         pos = tem + 1; /* don't find previous instances */
 18358                     }
 18359                   /* This string is not what we want; skip all of the
 18360                      glyphs that came from it.  */
 18361                   while ((row->reversed_p ? glyph > stop : glyph < stop)
 18362                          && EQ (glyph->object, str))
 18363                     glyph += incr;
 18364                 }
 18365               else
 18366                 glyph += incr;
 18367             }
 18368 
 18369           /* If we reached the end of the line, and END was from a string,
 18370              the cursor is not on this line.  */
 18371           if (cursor == NULL
 18372               && (row->reversed_p ? glyph <= end : glyph >= end)
 18373               && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
 18374               && STRINGP (end->object)
 18375               && row->continued_p)
 18376             return false;
 18377         }
 18378       /* A truncated row may not include PT among its character positions.
 18379          Setting the cursor inside the scroll margin will trigger
 18380          recalculation of hscroll in hscroll_window_tree.  But if a
 18381          display string covers point, defer to the string-handling
 18382          code below to figure this out.  */
 18383       else if (row->truncated_on_left_p && pt_old < bpos_min)
 18384         {
 18385           cursor = glyph_before;
 18386           x = -1;
 18387         }
 18388       else if ((row->truncated_on_right_p && pt_old > bpos_max)
 18389                /* Zero-width characters produce no glyphs.  */
 18390                || (!empty_line_p
 18391                    && (row->reversed_p
 18392                        ? glyph_after > glyphs_end
 18393                        : glyph_after < glyphs_end)))
 18394         {
 18395           cursor = glyph_after;
 18396           x = -1;
 18397         }
 18398     }
 18399 
 18400  compute_x:
 18401   if (cursor != NULL)
 18402     glyph = cursor;
 18403   else if (glyph == glyphs_end
 18404            && pos_before == pos_after
 18405            && STRINGP ((row->reversed_p
 18406                         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18407                         : row->glyphs[TEXT_AREA])->object))
 18408     {
 18409       /* If all the glyphs of this row came from strings, put the
 18410          cursor on the first glyph of the row.  This avoids having the
 18411          cursor outside of the text area in this very rare and hard
 18412          use case.  */
 18413       glyph =
 18414         row->reversed_p
 18415         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18416         : row->glyphs[TEXT_AREA];
 18417     }
 18418   if (x < 0)
 18419     {
 18420       struct glyph *g;
 18421 
 18422       /* Need to compute x that corresponds to GLYPH.  */
 18423       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 18424         {
 18425           if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
 18426             emacs_abort ();
 18427           x += g->pixel_width;
 18428         }
 18429     }
 18430 
 18431   /* ROW could be part of a continued line, which, under bidi
 18432      reordering, might have other rows whose start and end charpos
 18433      occlude point.  Only set w->cursor if we found a better
 18434      approximation to the cursor position than we have from previously
 18435      examined candidate rows belonging to the same continued line.  */
 18436   if (/* We already have a candidate row.  */
 18437       w->cursor.vpos >= 0
 18438       /* That candidate is not the row we are processing.  */
 18439       && MATRIX_ROW (matrix, w->cursor.vpos) != row
 18440       /* Make sure cursor.vpos specifies a row whose start and end
 18441          charpos occlude point, and it is valid candidate for being a
 18442          cursor-row.  This is because some callers of this function
 18443          leave cursor.vpos at the row where the cursor was displayed
 18444          during the last redisplay cycle.  */
 18445       && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
 18446       && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18447       && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
 18448     {
 18449       struct glyph *g1
 18450         = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
 18451 
 18452       /* Don't consider glyphs that are outside TEXT_AREA.  */
 18453       if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
 18454         return false;
 18455       /* Keep the candidate whose buffer position is the closest to
 18456          point or has the `cursor' property.  */
 18457       if (/* Previous candidate is a glyph in TEXT_AREA of that row.  */
 18458           w->cursor.hpos >= 0
 18459           && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
 18460           && ((BUFFERP (g1->object)
 18461                && (g1->charpos == pt_old /* An exact match always wins.  */
 18462                    || (BUFFERP (glyph->object)
 18463                        && eabs (g1->charpos - pt_old)
 18464                        < eabs (glyph->charpos - pt_old))))
 18465               /* Previous candidate is a glyph from a string that has
 18466                  a non-nil `cursor' property.  */
 18467               || (STRINGP (g1->object)
 18468                   && (!NILP (Fget_char_property (make_fixnum (g1->charpos),
 18469                                                 Qcursor, g1->object))
 18470                       /* Previous candidate is from the same display
 18471                          string as this one, and the display string
 18472                          came from a text property.  */
 18473                       || (EQ (g1->object, glyph->object)
 18474                           && string_from_text_prop)
 18475                       /* this candidate is from newline and its
 18476                          position is not an exact match */
 18477                       || (NILP (glyph->object)
 18478                           && glyph->charpos != pt_old)))))
 18479         return false;
 18480       /* If this candidate gives an exact match, use that.  */
 18481       if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18482             /* If this candidate is a glyph created for the
 18483                terminating newline of a line, and point is on that
 18484                newline, it wins because it's an exact match.  */
 18485             || (!row->continued_p
 18486                 && NILP (glyph->object)
 18487                 && glyph->charpos == 0
 18488                 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
 18489           /* Otherwise, keep the candidate that comes from a row
 18490              spanning less buffer positions.  This may win when one or
 18491              both candidate positions are on glyphs that came from
 18492              display strings, for which we cannot compare buffer
 18493              positions.  */
 18494           && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18495              - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18496              < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
 18497         return false;
 18498     }
 18499   w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
 18500   w->cursor.x = x;
 18501   w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
 18502   w->cursor.y = row->y + dy;
 18503 
 18504   if (w == XWINDOW (selected_window))
 18505     {
 18506       if (!row->continued_p
 18507           && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 18508           && row->x == 0)
 18509         {
 18510           this_line_buffer = XBUFFER (w->contents);
 18511 
 18512           CHARPOS (this_line_start_pos)
 18513             = MATRIX_ROW_START_CHARPOS (row) + delta;
 18514           BYTEPOS (this_line_start_pos)
 18515             = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
 18516 
 18517           CHARPOS (this_line_end_pos)
 18518             = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
 18519           BYTEPOS (this_line_end_pos)
 18520             = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
 18521 
 18522           this_line_y = w->cursor.y;
 18523           this_line_pixel_height = row->height;
 18524           this_line_vpos = w->cursor.vpos;
 18525           this_line_start_x = row->x;
 18526         }
 18527       else
 18528         CHARPOS (this_line_start_pos) = 0;
 18529     }
 18530 
 18531   return true;
 18532 }
 18533 
 18534 
 18535 /* Run window scroll functions, if any, for WINDOW with new window
 18536    start STARTP.  Sets the window start of WINDOW to that position.
 18537 
 18538    We assume that the window's buffer is really current.  */
 18539 
 18540 static struct text_pos
 18541 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
 18542 {
 18543   struct window *w = XWINDOW (window);
 18544   SET_MARKER_FROM_TEXT_POS (w->start, startp);
 18545 
 18546   eassert (current_buffer == XBUFFER (w->contents));
 18547 
 18548   if (!NILP (Vwindow_scroll_functions))
 18549     {
 18550       specpdl_ref count = SPECPDL_INDEX ();
 18551       specbind (Qinhibit_quit, Qt);
 18552       safe_run_hooks_2
 18553         (Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
 18554       unbind_to (count, Qnil);
 18555       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18556       /* In case the hook functions switch buffers.  */
 18557       set_buffer_internal (XBUFFER (w->contents));
 18558     }
 18559 
 18560   return startp;
 18561 }
 18562 
 18563 
 18564 /* Make sure the line containing the cursor is fully visible.
 18565    A value of true means there is nothing to be done.
 18566    (Either the line is fully visible, or it cannot be made so,
 18567    or we cannot tell.)
 18568 
 18569    If FORCE_P, return false even if partial visible cursor row
 18570    is higher than window.
 18571 
 18572    If CURRENT_MATRIX_P, use the information from the
 18573    window's current glyph matrix; otherwise use the desired glyph
 18574    matrix.
 18575 
 18576    If JUST_TEST_USER_PREFERENCE_P, just test what the value of
 18577    make-cursor-row-fully-visible requires, don't test the actual
 18578    cursor position.  The assumption is that in that case the caller
 18579    performs the necessary testing of the cursor position.
 18580 
 18581    A value of false means the caller should do scrolling
 18582    as if point had gone off the screen.  */
 18583 
 18584 static bool
 18585 cursor_row_fully_visible_p (struct window *w, bool force_p,
 18586                             bool current_matrix_p,
 18587                             bool just_test_user_preference_p)
 18588 {
 18589   struct glyph_matrix *matrix;
 18590   struct glyph_row *row;
 18591   int window_height;
 18592   Lisp_Object mclfv_p =
 18593     buffer_local_value (Qmake_cursor_line_fully_visible, w->contents);
 18594 
 18595   /* If no local binding, use the global value.  */
 18596   if (BASE_EQ (mclfv_p, Qunbound))
 18597     mclfv_p = Vmake_cursor_line_fully_visible;
 18598   /* Follow mode sets the variable to a Lisp function in buffers that
 18599      are under Follow mode.  */
 18600   if (FUNCTIONP (mclfv_p))
 18601     {
 18602       Lisp_Object window;
 18603       XSETWINDOW (window, w);
 18604       /* Implementation note: if the function we call here signals an
 18605          error, we will NOT scroll when the cursor is partially-visible.  */
 18606       Lisp_Object val = safe_call1 (mclfv_p, window);
 18607       if (NILP (val))
 18608         return true;
 18609       else if (just_test_user_preference_p)
 18610         return false;
 18611     }
 18612   else if (NILP (mclfv_p))
 18613     return true;
 18614   else if (just_test_user_preference_p)
 18615     return false;
 18616 
 18617   /* It's not always possible to find the cursor, e.g, when a window
 18618      is full of overlay strings.  Don't do anything in that case.  */
 18619   if (w->cursor.vpos < 0)
 18620     return true;
 18621 
 18622   matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
 18623   row = MATRIX_ROW (matrix, w->cursor.vpos);
 18624 
 18625   /* If the cursor row is not partially visible, there's nothing to do.  */
 18626   if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
 18627     return true;
 18628 
 18629   /* If the row the cursor is in is taller than the window's height,
 18630      it's not clear what to do, so do nothing.  */
 18631   window_height = window_box_height (w);
 18632   if (row->height >= window_height)
 18633     {
 18634       if (!force_p || MINI_WINDOW_P (w)
 18635           || w->vscroll || w->cursor.vpos == 0)
 18636         return true;
 18637     }
 18638   return false;
 18639 }
 18640 
 18641 
 18642 /* Try scrolling PT into view in window WINDOW.  JUST_THIS_ONE_P
 18643    means only WINDOW is redisplayed in redisplay_internal.
 18644    TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
 18645    in redisplay_window to bring a partially visible line into view in
 18646    the case that only the cursor has moved.
 18647 
 18648    LAST_LINE_MISFIT should be true if we're scrolling because the
 18649    last screen line's vertical height extends past the end of the screen.
 18650 
 18651    Value is
 18652 
 18653    1    if scrolling succeeded
 18654 
 18655    0    if scrolling didn't find point.
 18656 
 18657    -1   if new fonts have been loaded so that we must interrupt
 18658    redisplay, adjust glyph matrices, and try again.  */
 18659 
 18660 enum
 18661 {
 18662   SCROLLING_SUCCESS = 1,
 18663   SCROLLING_FAILED = 0,
 18664   SCROLLING_NEED_LARGER_MATRICES = -1
 18665 };
 18666 
 18667 /* If scroll-conservatively is more than this, never recenter.
 18668 
 18669    If you change this, don't forget to update the doc string of
 18670    `scroll-conservatively' and the Emacs manual.  */
 18671 #define SCROLL_LIMIT 100
 18672 
 18673 static int
 18674 try_scrolling (Lisp_Object window, bool just_this_one_p,
 18675                intmax_t arg_scroll_conservatively, intmax_t scroll_step,
 18676                bool temp_scroll_step, bool last_line_misfit)
 18677 {
 18678   struct window *w = XWINDOW (window);
 18679   struct text_pos pos, startp;
 18680   struct it it;
 18681   int this_scroll_margin, scroll_max, rc, height;
 18682   int dy = 0, amount_to_scroll = 0;
 18683   bool scroll_down_p = false;
 18684   int extra_scroll_margin_lines = last_line_misfit;
 18685   Lisp_Object aggressive;
 18686   /* We will never try scrolling more than this number of lines.  */
 18687   int scroll_limit = SCROLL_LIMIT;
 18688   int frame_line_height = default_line_pixel_height (w);
 18689 
 18690 #ifdef GLYPH_DEBUG
 18691   debug_method_add (w, "try_scrolling");
 18692 #endif
 18693 
 18694   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18695 
 18696   this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18697 
 18698   /* Force arg_scroll_conservatively to have a reasonable value, to
 18699      avoid scrolling too far away with slow move_it_* functions.  Note
 18700      that the user can supply scroll-conservatively equal to
 18701      `most-positive-fixnum', which can be larger than INT_MAX.  */
 18702   if (arg_scroll_conservatively > scroll_limit)
 18703     {
 18704       arg_scroll_conservatively = scroll_limit + 1;
 18705       scroll_max = scroll_limit * frame_line_height;
 18706     }
 18707   else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
 18708     /* Compute how much we should try to scroll maximally to bring
 18709        point into view.  */
 18710     {
 18711       intmax_t scroll_lines_max
 18712         = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
 18713       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
 18714       scroll_max = scroll_lines * frame_line_height;
 18715     }
 18716   else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
 18717            || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
 18718     /* We're trying to scroll because of aggressive scrolling but no
 18719        scroll_step is set.  Choose an arbitrary one.  */
 18720     scroll_max = 10 * frame_line_height;
 18721   else
 18722     scroll_max = 0;
 18723 
 18724  too_near_end:
 18725 
 18726   /* Decide whether to scroll down.  */
 18727   if (PT > CHARPOS (startp))
 18728     {
 18729       int scroll_margin_y;
 18730 
 18731       /* Compute the pixel ypos of the scroll margin, then move IT to
 18732          either that ypos or PT, whichever comes first.  */
 18733       start_display (&it, w, startp);
 18734       scroll_margin_y = it.last_visible_y - partial_line_height (&it)
 18735         - this_scroll_margin
 18736         - frame_line_height * extra_scroll_margin_lines;
 18737       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 18738                   (MOVE_TO_POS | MOVE_TO_Y));
 18739 
 18740       if (PT > CHARPOS (it.current.pos))
 18741         {
 18742           int y0 = line_bottom_y (&it);
 18743           /* Compute how many pixels below window bottom to stop searching
 18744              for PT.  This avoids costly search for PT that is far away if
 18745              the user limited scrolling by a small number of lines, but
 18746              always finds PT if scroll_conservatively is set to a large
 18747              number, such as most-positive-fixnum.  */
 18748           int slack = max (scroll_max, 10 * frame_line_height);
 18749           int y_to_move = it.last_visible_y + slack;
 18750 
 18751           /* Compute the distance from the scroll margin to PT or to
 18752              the scroll limit, whichever comes first.  This should
 18753              include the height of the cursor line, to make that line
 18754              fully visible.  */
 18755           move_it_to (&it, PT, -1, y_to_move,
 18756                       -1, MOVE_TO_POS | MOVE_TO_Y);
 18757           dy = line_bottom_y (&it) - y0;
 18758 
 18759           if (dy > scroll_max)
 18760             return SCROLLING_FAILED;
 18761 
 18762           if (dy > 0)
 18763             scroll_down_p = true;
 18764         }
 18765       else if (PT == IT_CHARPOS (it)
 18766                && IT_CHARPOS (it) < ZV
 18767                && it.method == GET_FROM_STRING
 18768                && arg_scroll_conservatively > scroll_limit
 18769                && it.current_x == 0)
 18770         {
 18771           enum move_it_result skip;
 18772           int y1 = it.current_y;
 18773           int vpos;
 18774 
 18775           /* A before-string that includes newlines and is displayed
 18776              on the last visible screen line could fail us under
 18777              scroll-conservatively > 100, because we will be unable to
 18778              position the cursor on that last visible line.  Try to
 18779              recover by finding the first screen line that has some
 18780              glyphs coming from the buffer text.  */
 18781           do {
 18782             skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 18783             if (skip != MOVE_NEWLINE_OR_CR
 18784                 || IT_CHARPOS (it) != PT
 18785                 || it.method == GET_FROM_BUFFER)
 18786               break;
 18787             vpos = it.vpos;
 18788             move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
 18789           } while (it.vpos > vpos);
 18790 
 18791           dy = it.current_y - y1;
 18792 
 18793           if (dy > scroll_max)
 18794             return SCROLLING_FAILED;
 18795 
 18796           if (dy > 0)
 18797             scroll_down_p = true;
 18798         }
 18799     }
 18800 
 18801   if (scroll_down_p)
 18802     {
 18803       /* Point is in or below the bottom scroll margin, so move the
 18804          window start down.  If scrolling conservatively, move it just
 18805          enough down to make point visible.  If scroll_step is set,
 18806          move it down by scroll_step.  */
 18807       if (arg_scroll_conservatively)
 18808         amount_to_scroll
 18809           = min (max (dy, frame_line_height),
 18810                  frame_line_height * arg_scroll_conservatively);
 18811       else if (scroll_step || temp_scroll_step)
 18812         amount_to_scroll = scroll_max;
 18813       else
 18814         {
 18815           aggressive = BVAR (current_buffer, scroll_up_aggressively);
 18816           height = WINDOW_BOX_TEXT_HEIGHT (w);
 18817           if (NUMBERP (aggressive))
 18818             {
 18819               double float_amount = XFLOATINT (aggressive) * height;
 18820               int aggressive_scroll = float_amount;
 18821               if (aggressive_scroll == 0 && float_amount > 0)
 18822                 aggressive_scroll = 1;
 18823               /* Don't let point enter the scroll margin near top of
 18824                  the window.  This could happen if the value of
 18825                  scroll_up_aggressively is too large and there are
 18826                  non-zero margins, because scroll_up_aggressively
 18827                  means put point that fraction of window height
 18828                  _from_the_bottom_margin_.  */
 18829               if (aggressive_scroll + 2 * this_scroll_margin > height)
 18830                 aggressive_scroll = height - 2 * this_scroll_margin;
 18831               amount_to_scroll = dy + aggressive_scroll;
 18832             }
 18833         }
 18834 
 18835       if (amount_to_scroll <= 0)
 18836         return SCROLLING_FAILED;
 18837 
 18838       start_display (&it, w, startp);
 18839       if (arg_scroll_conservatively <= scroll_limit)
 18840         move_it_vertically (&it, amount_to_scroll);
 18841       else
 18842         {
 18843           /* Extra precision for users who set scroll-conservatively
 18844              to a large number: make sure the amount we scroll
 18845              the window start is never less than amount_to_scroll,
 18846              which was computed as distance from window bottom to
 18847              point.  This matters when lines at window top and lines
 18848              below window bottom have different height.  */
 18849           struct it it1;
 18850           void *it1data = NULL;
 18851           /* We use a temporary it1 because line_bottom_y can modify
 18852              its argument, if it moves one line down; see there.  */
 18853           int start_y;
 18854 
 18855           SAVE_IT (it1, it, it1data);
 18856           start_y = line_bottom_y (&it1);
 18857           do {
 18858             RESTORE_IT (&it, &it, it1data);
 18859             move_it_by_lines (&it, 1);
 18860             SAVE_IT (it1, it, it1data);
 18861           } while (IT_CHARPOS (it) < ZV
 18862                    && line_bottom_y (&it1) - start_y < amount_to_scroll);
 18863           bidi_unshelve_cache (it1data, true);
 18864         }
 18865 
 18866       /* If STARTP is unchanged, move it down another screen line.  */
 18867       if (IT_CHARPOS (it) == CHARPOS (startp))
 18868         move_it_by_lines (&it, 1);
 18869       startp = it.current.pos;
 18870     }
 18871   else
 18872     {
 18873       struct text_pos scroll_margin_pos = startp;
 18874       int y_offset = 0;
 18875 
 18876       /* See if point is inside the scroll margin at the top of the
 18877          window.  */
 18878       if (this_scroll_margin)
 18879         {
 18880           int y_start;
 18881 
 18882           start_display (&it, w, startp);
 18883           y_start = it.current_y;
 18884           move_it_vertically (&it, this_scroll_margin);
 18885           scroll_margin_pos = it.current.pos;
 18886           /* If we didn't move enough before hitting ZV, request
 18887              additional amount of scroll, to move point out of the
 18888              scroll margin.  */
 18889           if (IT_CHARPOS (it) == ZV
 18890               && it.current_y - y_start < this_scroll_margin)
 18891             y_offset = this_scroll_margin - (it.current_y - y_start);
 18892         }
 18893 
 18894       if (PT < CHARPOS (scroll_margin_pos))
 18895         {
 18896           /* Point is in the scroll margin at the top of the window or
 18897              above what is displayed in the window.  */
 18898           int y0, y_to_move;
 18899 
 18900           /* Compute the vertical distance from PT to the scroll
 18901              margin position.  Move as far as scroll_max allows, or
 18902              one screenful, or 10 screen lines, whichever is largest.
 18903              Give up if distance is greater than scroll_max or if we
 18904              didn't reach the scroll margin position.  */
 18905           SET_TEXT_POS (pos, PT, PT_BYTE);
 18906           start_display (&it, w, pos);
 18907           y0 = it.current_y;
 18908           y_to_move = max (it.last_visible_y,
 18909                            max (scroll_max, 10 * frame_line_height));
 18910           move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
 18911                       y_to_move, -1,
 18912                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18913           dy = it.current_y - y0;
 18914           if (dy > scroll_max
 18915               || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
 18916             return SCROLLING_FAILED;
 18917 
 18918           /* Additional scroll for when ZV was too close to point.  */
 18919           dy += y_offset;
 18920 
 18921           /* Compute new window start.  */
 18922           start_display (&it, w, startp);
 18923 
 18924           if (arg_scroll_conservatively)
 18925             amount_to_scroll
 18926               = min (max (dy, frame_line_height),
 18927                      frame_line_height * arg_scroll_conservatively);
 18928           else if (scroll_step || temp_scroll_step)
 18929             amount_to_scroll = scroll_max;
 18930           else
 18931             {
 18932               aggressive = BVAR (current_buffer, scroll_down_aggressively);
 18933               height = WINDOW_BOX_TEXT_HEIGHT (w);
 18934               if (NUMBERP (aggressive))
 18935                 {
 18936                   double float_amount = XFLOATINT (aggressive) * height;
 18937                   int aggressive_scroll = float_amount;
 18938                   if (aggressive_scroll == 0 && float_amount > 0)
 18939                     aggressive_scroll = 1;
 18940                   /* Don't let point enter the scroll margin near
 18941                      bottom of the window, if the value of
 18942                      scroll_down_aggressively happens to be too
 18943                      large.  */
 18944                   if (aggressive_scroll + 2 * this_scroll_margin > height)
 18945                     aggressive_scroll = height - 2 * this_scroll_margin;
 18946                   amount_to_scroll = dy + aggressive_scroll;
 18947                 }
 18948             }
 18949 
 18950           if (amount_to_scroll <= 0)
 18951             return SCROLLING_FAILED;
 18952 
 18953           move_it_vertically_backward (&it, amount_to_scroll);
 18954           startp = it.current.pos;
 18955         }
 18956     }
 18957 
 18958   /* Run window scroll functions.  */
 18959   startp = run_window_scroll_functions (window, startp);
 18960 
 18961   /* Display the window.  Give up if new fonts are loaded, or if point
 18962      doesn't appear.  */
 18963   if (!try_window (window, startp, 0))
 18964     rc = SCROLLING_NEED_LARGER_MATRICES;
 18965   else if (w->cursor.vpos < 0)
 18966     {
 18967       clear_glyph_matrix (w->desired_matrix);
 18968       rc = SCROLLING_FAILED;
 18969     }
 18970   else
 18971     {
 18972       /* Maybe forget recorded base line for line number display.  */
 18973       if (!just_this_one_p
 18974           || current_buffer->clip_changed
 18975           || BEG_UNCHANGED < CHARPOS (startp))
 18976         w->base_line_number = 0;
 18977 
 18978       /* If cursor ends up on a partially visible line,
 18979          treat that as being off the bottom of the screen.  */
 18980       if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
 18981                                         false, false)
 18982           /* It's possible that the cursor is on the first line of the
 18983              buffer, which is partially obscured due to a vscroll
 18984              (Bug#7537).  In that case, avoid looping forever. */
 18985           && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
 18986         {
 18987           clear_glyph_matrix (w->desired_matrix);
 18988           ++extra_scroll_margin_lines;
 18989           goto too_near_end;
 18990         }
 18991       rc = SCROLLING_SUCCESS;
 18992     }
 18993 
 18994   return rc;
 18995 }
 18996 
 18997 
 18998 /* Compute a suitable window start for window W if display of W starts
 18999    on a continuation line.  Value is true if a new window start
 19000    was computed.
 19001 
 19002    The new window start will be computed, based on W's width, starting
 19003    from the start of the continued line.  It is the start of the
 19004    screen line with the minimum distance from the old start W->start,
 19005    which is still before point (otherwise point will definitely not
 19006    be visible in the window).  */
 19007 
 19008 static bool
 19009 compute_window_start_on_continuation_line (struct window *w)
 19010 {
 19011   struct text_pos pos, start_pos, pos_before_pt;
 19012   bool window_start_changed_p = false;
 19013 
 19014   SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19015 
 19016   /* If window start is on a continuation line...  Window start may be
 19017      < BEGV in case there's invisible text at the start of the
 19018      buffer (M-x rmail, for example).  */
 19019   if (CHARPOS (start_pos) > BEGV
 19020       && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
 19021     {
 19022       struct it it;
 19023       struct glyph_row *row;
 19024 
 19025       /* Handle the case that the window start is out of range.  */
 19026       if (CHARPOS (start_pos) < BEGV)
 19027         SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
 19028       else if (CHARPOS (start_pos) > ZV)
 19029         SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
 19030 
 19031       /* Find the start of the continued line.  This should be fast
 19032          because find_newline is fast (newline cache).  */
 19033       row = w->desired_matrix->rows + window_wants_tab_line (w)
 19034                                     + window_wants_header_line (w);
 19035       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
 19036                      row, DEFAULT_FACE_ID);
 19037       reseat_at_previous_visible_line_start (&it);
 19038 
 19039       /* Give up (by not using the code in the block below) and say it
 19040          takes too much time to compute a new window start, if the
 19041          line start is "too far" away from the window start.  Also,
 19042          give up if the line start is after point, as in that case
 19043          point will not be visible with any window start we
 19044          compute.  */
 19045       if (IT_CHARPOS (it) <= PT
 19046           && (CHARPOS (start_pos) - IT_CHARPOS (it)
 19047               /* PXW: Do we need upper bounds here?  */
 19048               < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
 19049         {
 19050           int min_distance, distance;
 19051 
 19052           /* Move forward by display lines to find the new window
 19053              start.  If window width was enlarged, the new start can
 19054              be expected to be > the old start.  If window width was
 19055              decreased, the new window start will be < the old start.
 19056              So, we're looking for the display line start with the
 19057              minimum distance from the old window start.  */
 19058           pos_before_pt = pos = it.current.pos;
 19059           min_distance = DISP_INFINITY;
 19060           while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
 19061                  distance < min_distance)
 19062             {
 19063               min_distance = distance;
 19064               if (CHARPOS (pos) <= PT)
 19065                 pos_before_pt = pos;
 19066               pos = it.current.pos;
 19067               if (it.line_wrap == WORD_WRAP)
 19068                 {
 19069                   /* Under WORD_WRAP, move_it_by_lines is likely to
 19070                      overshoot and stop not at the first, but the
 19071                      second character from the left margin.  So in
 19072                      that case, we need a more tight control on the X
 19073                      coordinate of the iterator than move_it_by_lines
 19074                      promises in its contract.  The method is to first
 19075                      go to the last (rightmost) visible character of a
 19076                      line, then move to the leftmost character on the
 19077                      next line in a separate call.  */
 19078                   move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
 19079                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19080                   move_it_to (&it, ZV, 0,
 19081                               it.current_y + it.max_ascent + it.max_descent, -1,
 19082                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19083                 }
 19084               else
 19085                 move_it_by_lines (&it, 1);
 19086             }
 19087 
 19088           /* It makes very little sense to make the new window start
 19089              after point, as point won't be visible.  If that's what
 19090              the loop above finds, fall back on the candidate before
 19091              or at point that is closest to the old window start.  */
 19092           if (CHARPOS (pos) > PT)
 19093             pos = pos_before_pt;
 19094 
 19095           /* Set the window start there.  */
 19096           SET_MARKER_FROM_TEXT_POS (w->start, pos);
 19097           window_start_changed_p = true;
 19098         }
 19099     }
 19100 
 19101   return window_start_changed_p;
 19102 }
 19103 
 19104 
 19105 /* Try cursor movement in case text has not changed in window WINDOW,
 19106    with window start STARTP.  Value is
 19107 
 19108    CURSOR_MOVEMENT_SUCCESS if successful
 19109 
 19110    CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
 19111 
 19112    CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
 19113    display.  *SCROLL_STEP is set to true, under certain circumstances, if
 19114    we want to scroll as if scroll-step were set to 1.  See the code.
 19115 
 19116    CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
 19117    which case we have to abort this redisplay, and adjust matrices
 19118    first.  */
 19119 
 19120 enum
 19121 {
 19122   CURSOR_MOVEMENT_SUCCESS,
 19123   CURSOR_MOVEMENT_CANNOT_BE_USED,
 19124   CURSOR_MOVEMENT_MUST_SCROLL,
 19125   CURSOR_MOVEMENT_NEED_LARGER_MATRICES
 19126 };
 19127 
 19128 static int
 19129 try_cursor_movement (Lisp_Object window, struct text_pos startp,
 19130                      bool *scroll_step)
 19131 {
 19132   struct window *w = XWINDOW (window);
 19133   struct frame *f = XFRAME (w->frame);
 19134   int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
 19135 
 19136 #ifdef GLYPH_DEBUG
 19137   if (inhibit_try_cursor_movement)
 19138     return rc;
 19139 #endif
 19140 
 19141   /* Previously, there was a check for Lisp integer in the
 19142      if-statement below. Now, this field is converted to
 19143      ptrdiff_t, thus zero means invalid position in a buffer.  */
 19144   eassert (w->last_point > 0);
 19145   /* Likewise there was a check whether window_end_vpos is nil or larger
 19146      than the window.  Now window_end_vpos is int and so never nil, but
 19147      let's leave eassert to check whether it fits in the window.  */
 19148   eassert (!w->window_end_valid
 19149            || w->window_end_vpos < w->current_matrix->nrows);
 19150 
 19151   /* Handle case where text has not changed, only point, and it has
 19152      not moved off the frame.  */
 19153   if (/* Point may be in this window.  */
 19154       PT >= CHARPOS (startp)
 19155       /* Selective display hasn't changed.  */
 19156       && !current_buffer->clip_changed
 19157       /* Function force-mode-line-update is used to force a thorough
 19158          redisplay.  It sets either windows_or_buffers_changed or
 19159          update_mode_lines.  So don't take a shortcut here for these
 19160          cases.  */
 19161       && !update_mode_lines
 19162       && !windows_or_buffers_changed
 19163       && !f->cursor_type_changed
 19164       && NILP (Vshow_trailing_whitespace)
 19165       /* When display-line-numbers is in relative mode, moving point
 19166          requires to redraw the entire window.  */
 19167       && !EQ (Vdisplay_line_numbers, Qrelative)
 19168       && !EQ (Vdisplay_line_numbers, Qvisual)
 19169       /* When the current line number should be displayed in a
 19170          distinct face, moving point cannot be handled in optimized
 19171          way as below.  */
 19172       && !(!NILP (Vdisplay_line_numbers)
 19173            && NILP (Finternal_lisp_face_equal_p (Qline_number,
 19174                                                  Qline_number_current_line,
 19175                                                  w->frame)))
 19176       /* This code is not used for mini-buffer for the sake of the case
 19177          of redisplaying to replace an echo area message; since in
 19178          that case the mini-buffer contents per se are usually
 19179          unchanged.  This code is of no real use in the mini-buffer
 19180          since the handling of this_line_start_pos, etc., in redisplay
 19181          handles the same cases.  */
 19182       && !EQ (window, minibuf_window)
 19183       /* When overlay arrow is shown in current buffer, point movement
 19184          is no longer "simple", as it typically causes the overlay
 19185          arrow to move as well.  */
 19186       && !overlay_arrow_in_current_buffer_p ())
 19187     {
 19188       int this_scroll_margin, top_scroll_margin;
 19189       struct glyph_row *row = NULL;
 19190 
 19191 #ifdef GLYPH_DEBUG
 19192       debug_method_add (w, "cursor movement");
 19193 #endif
 19194 
 19195       this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 19196 
 19197       top_scroll_margin = this_scroll_margin;
 19198       if (window_wants_tab_line (w))
 19199         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 19200       if (window_wants_header_line (w))
 19201         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 19202 
 19203       /* Start with the row the cursor was displayed during the last
 19204          not paused redisplay.  Give up if that row is not valid.  */
 19205       if (w->last_cursor_vpos < 0
 19206           || w->last_cursor_vpos >= w->current_matrix->nrows)
 19207         rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19208       else
 19209         {
 19210           row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
 19211           /* Skip the tab-line and header-line rows, if any.  */
 19212           if (row->tab_line_p)
 19213             ++row;
 19214           if (row->mode_line_p)
 19215             ++row;
 19216           if (!row->enabled_p)
 19217             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19218         }
 19219 
 19220       if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
 19221         {
 19222           bool scroll_p = false, must_scroll = false;
 19223           int last_y = window_text_bottom_y (w) - this_scroll_margin;
 19224 
 19225           if (PT > w->last_point)
 19226             {
 19227               /* Point has moved forward.  */
 19228               while (MATRIX_ROW_END_CHARPOS (row) < PT
 19229                      && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19230                 {
 19231                   eassert (row->enabled_p);
 19232                   ++row;
 19233                 }
 19234 
 19235               /* If the end position of a row equals the start
 19236                  position of the next row, and PT is at that position,
 19237                  we would rather display cursor in the next line.  */
 19238               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19239                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19240                      && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
 19241                      && MATRIX_ROW_START_CHARPOS (row+1) >= PT
 19242                      && !cursor_row_p (row))
 19243                 ++row;
 19244 
 19245               /* If within the scroll margin, scroll.  Note that
 19246                  MATRIX_ROW_BOTTOM_Y gives the pixel position at which
 19247                  the next line would be drawn, and that
 19248                  this_scroll_margin can be zero.  */
 19249               if (MATRIX_ROW_BOTTOM_Y (row) > last_y
 19250                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19251                   /* Line is completely visible last line in window
 19252                      and PT is to be set in the next line.  */
 19253                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19254                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19255                       && !row->ends_at_zv_p
 19256                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19257                 scroll_p = true;
 19258             }
 19259           else if (PT < w->last_point)
 19260             {
 19261               /* Cursor has to be moved backward.  Note that PT >=
 19262                  CHARPOS (startp) because of the outer if-statement.  */
 19263               struct glyph_row *row0 = row;
 19264 
 19265               while (!row->mode_line_p
 19266                      && (MATRIX_ROW_START_CHARPOS (row) > PT
 19267                          || (MATRIX_ROW_START_CHARPOS (row) == PT
 19268                              && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
 19269                                  || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
 19270                                      row > w->current_matrix->rows
 19271                                      && (row-1)->ends_in_newline_from_string_p))))
 19272                      && (row->y > top_scroll_margin
 19273                          || CHARPOS (startp) == BEGV))
 19274                 {
 19275                   eassert (row->enabled_p);
 19276                   --row;
 19277                 }
 19278 
 19279               /* With bidi-reordered rows we can have buffer positions
 19280                  _decrease_ when going down by rows.  If we haven't
 19281                  found our row in the loop above, give it another try
 19282                  now going in the other direction from the original row.  */
 19283               if (!(MATRIX_ROW_START_CHARPOS (row) <= PT
 19284                     && PT <= MATRIX_ROW_END_CHARPOS (row))
 19285                   && row0->continued_p)
 19286                 {
 19287                   row = row0;
 19288                   while (MATRIX_ROW_START_CHARPOS (row) > PT
 19289                          && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19290                     {
 19291                       eassert (row->enabled_p);
 19292                       ++row;
 19293                     }
 19294                 }
 19295 
 19296               /* Consider the following case: Window starts at BEGV,
 19297                  there is invisible, intangible text at BEGV, so that
 19298                  display starts at some point START > BEGV.  It can
 19299                  happen that we are called with PT somewhere between
 19300                  BEGV and START.  Try to handle that case.  */
 19301               if (row < w->current_matrix->rows
 19302                   || row->mode_line_p)
 19303                 {
 19304                   row = w->current_matrix->rows;
 19305                   /* Skip the tab-line and header-line rows, if any.  */
 19306                   if (row->tab_line_p)
 19307                     ++row;
 19308                   if (row->mode_line_p)
 19309                     ++row;
 19310                 }
 19311 
 19312               /* Due to newlines in overlay strings, we may have to
 19313                  skip forward over overlay strings.  */
 19314               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19315                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19316                      && !cursor_row_p (row))
 19317                 ++row;
 19318 
 19319               /* If within the scroll margin, either the top one or
 19320                  the bottom one, scroll.  */
 19321               if ((row->y < top_scroll_margin
 19322                    && CHARPOS (startp) != BEGV)
 19323                   || MATRIX_ROW_BOTTOM_Y (row) > last_y
 19324                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19325                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19326                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19327                       && !row->ends_at_zv_p
 19328                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19329                 scroll_p = true;
 19330             }
 19331           else
 19332             {
 19333               /* Cursor did not move.  So don't scroll even if cursor line
 19334                  is partially visible, as it was so before.  */
 19335                  rc = CURSOR_MOVEMENT_SUCCESS;
 19336             }
 19337 
 19338           if ((PT < MATRIX_ROW_START_CHARPOS (row)
 19339                && (row == MATRIX_FIRST_TEXT_ROW (w->current_matrix)
 19340                    /* Don't give up if point is inside invisible text
 19341                       at the beginning of its glyph row.  */
 19342                    || (MATRIX_ROW_END_CHARPOS (row-1)
 19343                        == MATRIX_ROW_START_CHARPOS (row))))
 19344               || PT > MATRIX_ROW_END_CHARPOS (row))
 19345             {
 19346               /* if PT is not in the glyph row, give up.  */
 19347               rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19348               must_scroll = true;
 19349             }
 19350           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19351                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19352             {
 19353               struct glyph_row *row1;
 19354 
 19355               /* If rows are bidi-reordered and point moved, back up
 19356                  until we find a row that does not belong to a
 19357                  continuation line.  This is because we must consider
 19358                  all rows of a continued line as candidates for the
 19359                  new cursor positioning, since row start and end
 19360                  positions change non-linearly with vertical position
 19361                  in such rows.  */
 19362               /* FIXME: Revisit this when glyph ``spilling'' in
 19363                  continuation lines' rows is implemented for
 19364                  bidi-reordered rows.  */
 19365               for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 19366                    MATRIX_ROW_CONTINUATION_LINE_P (row);
 19367                    --row)
 19368                 {
 19369                   /* If we hit the beginning of the displayed portion
 19370                      without finding the first row of a continued
 19371                      line, give up.  */
 19372                   if (row <= row1)
 19373                     {
 19374                       rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19375                       break;
 19376                     }
 19377                   eassert (row->enabled_p);
 19378                 }
 19379             }
 19380           if (must_scroll)
 19381             ;
 19382           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19383               && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
 19384               /* Make sure this isn't a header line nor a tab-line by
 19385                  any chance, since then MATRIX_ROW_PARTIALLY_VISIBLE_P
 19386                  might yield true.  */
 19387               && !row->mode_line_p
 19388               && !cursor_row_fully_visible_p (w, true, true, true))
 19389             {
 19390               if (PT == MATRIX_ROW_END_CHARPOS (row)
 19391                   && !row->ends_at_zv_p
 19392                   && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 19393                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19394               else if (row->height > window_box_height (w))
 19395                 {
 19396                   /* If we end up in a partially visible line, let's
 19397                      make it fully visible, except when it's taller
 19398                      than the window, in which case we can't do much
 19399                      about it.  */
 19400                   *scroll_step = true;
 19401                   rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19402                 }
 19403               else
 19404                 {
 19405                   set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 19406                   if (!cursor_row_fully_visible_p (w, false, true, false))
 19407                     rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19408                   else
 19409                     rc = CURSOR_MOVEMENT_SUCCESS;
 19410                 }
 19411             }
 19412           else if (scroll_p)
 19413             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19414           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19415                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19416             {
 19417               /* With bidi-reordered rows, there could be more than
 19418                  one candidate row whose start and end positions
 19419                  occlude point.  We need to let set_cursor_from_row
 19420                  find the best candidate.  */
 19421               /* FIXME: Revisit this when glyph ``spilling'' in
 19422                  continuation lines' rows is implemented for
 19423                  bidi-reordered rows.  */
 19424               bool rv = false;
 19425               bool pt_invis = false;
 19426               Lisp_Object val = get_char_property_and_overlay (make_fixnum (PT),
 19427                                                                Qinvisible,
 19428                                                                Qnil, NULL);
 19429 
 19430               if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 19431                 pt_invis = true;
 19432 
 19433               do
 19434                 {
 19435                   bool at_zv_p = false, exact_match_p = false;
 19436 
 19437                   /* If point is in invisible text, we cannot assume
 19438                      it must be after row's start position, since the
 19439                      row could have invisible text at its beginning
 19440                      where point is located.  */
 19441                   if ((pt_invis || MATRIX_ROW_START_CHARPOS (row) <= PT)
 19442                       && PT <= MATRIX_ROW_END_CHARPOS (row)
 19443                       && cursor_row_p (row))
 19444                     rv |= set_cursor_from_row (w, row, w->current_matrix,
 19445                                                0, 0, 0, 0);
 19446                   /* As soon as we've found the exact match for point,
 19447                      or the first suitable row whose ends_at_zv_p flag
 19448                      is set, we are done.  */
 19449                   if (rv)
 19450                     {
 19451                       at_zv_p = MATRIX_ROW (w->current_matrix,
 19452                                             w->cursor.vpos)->ends_at_zv_p;
 19453                       if (!at_zv_p
 19454                           && w->cursor.hpos >= 0
 19455                           && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
 19456                                                                w->cursor.vpos))
 19457                         {
 19458                           struct glyph_row *candidate =
 19459                             MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 19460                           struct glyph *g =
 19461                             candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 19462                           ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
 19463 
 19464                           exact_match_p =
 19465                             (BUFFERP (g->object) && g->charpos == PT)
 19466                             || (NILP (g->object)
 19467                                 && (g->charpos == PT
 19468                                     || (g->charpos == 0 && endpos - 1 == PT)));
 19469                           /* Perhaps the point position is inside
 19470                              invisible text?  In that case, we trust
 19471                              'set_cursor_from_row' to do its job and
 19472                              find the best position for the cursor.  */
 19473                           if (!exact_match_p && pt_invis)
 19474                             exact_match_p = true;
 19475                         }
 19476                       if (at_zv_p || exact_match_p)
 19477                         {
 19478                           rc = CURSOR_MOVEMENT_SUCCESS;
 19479                           break;
 19480                         }
 19481                     }
 19482                   if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
 19483                     break;
 19484                   ++row;
 19485                 }
 19486               while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
 19487                        || row->continued_p)
 19488                       && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
 19489                      || (MATRIX_ROW_START_CHARPOS (row) == PT
 19490                          && MATRIX_ROW_BOTTOM_Y (row) < last_y));
 19491               /* If we didn't find any candidate rows, or exited the
 19492                  loop before all the candidates were examined, signal
 19493                  to the caller that this method failed.  */
 19494               if (rc != CURSOR_MOVEMENT_SUCCESS
 19495                   && !(rv
 19496                        && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 19497                        && !row->continued_p))
 19498                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19499               else if (rv)
 19500                 rc = CURSOR_MOVEMENT_SUCCESS;
 19501             }
 19502           else
 19503             {
 19504               do
 19505                 {
 19506                   if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
 19507                     {
 19508                       rc = CURSOR_MOVEMENT_SUCCESS;
 19509                       break;
 19510                     }
 19511                   ++row;
 19512                 }
 19513               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19514                      && MATRIX_ROW_START_CHARPOS (row) == PT
 19515                      && cursor_row_p (row));
 19516             }
 19517         }
 19518     }
 19519 
 19520   return rc;
 19521 }
 19522 
 19523 
 19524 void
 19525 set_vertical_scroll_bar (struct window *w)
 19526 {
 19527   ptrdiff_t start, end, whole;
 19528 
 19529   /* Calculate the start and end positions for the current window.
 19530      At some point, it would be nice to choose between scrollbars
 19531      which reflect the whole buffer size, with special markers
 19532      indicating narrowing, and scrollbars which reflect only the
 19533      visible region.
 19534 
 19535      Note that mini-buffers sometimes aren't displaying any text.  */
 19536   if (!MINI_WINDOW_P (w)
 19537       || (w == XWINDOW (minibuf_window)
 19538           && NILP (echo_area_buffer[0])))
 19539     {
 19540       struct buffer *buf = XBUFFER (w->contents);
 19541 
 19542       whole = BUF_ZV (buf) - BUF_BEGV (buf);
 19543       start = marker_position (w->start) - BUF_BEGV (buf);
 19544       end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
 19545 
 19546       /* If w->window_end_pos cannot be trusted, recompute it "the
 19547          hard way".  But don't bother to be too accurate when
 19548          long-line shortcuts are in effect.  */
 19549       if (!w->window_end_valid && !buf->long_line_optimizations_p)
 19550         {
 19551           struct it it;
 19552           struct text_pos start_pos;
 19553           struct buffer *obuf = current_buffer;
 19554           /* When we display the scroll bar of a mini-window,
 19555              current_buffer is not guaranteed to be the mini-window's
 19556              buffer, see the beginning of redisplay_window.  */
 19557           set_buffer_internal_1 (XBUFFER (w->contents));
 19558           SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19559           start_display (&it, w, start_pos);
 19560           move_it_to (&it, -1, it.last_visible_x, window_box_height (w), -1,
 19561                       MOVE_TO_X | MOVE_TO_Y);
 19562           end -= (BUF_Z (buf) - IT_CHARPOS (it)) - w->window_end_pos;
 19563           set_buffer_internal_1 (obuf);
 19564         }
 19565 
 19566       if (end < start)
 19567         end = start;
 19568       if (whole < (end - start))
 19569         whole = end - start;
 19570     }
 19571   else
 19572     start = end = whole = 0;
 19573 
 19574   /* Indicate what this scroll bar ought to be displaying now.  */
 19575   if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19576     (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19577       (w, end - start, whole, start);
 19578 }
 19579 
 19580 
 19581 void
 19582 set_horizontal_scroll_bar (struct window *w)
 19583 {
 19584   int start, end, whole, portion;
 19585 
 19586   if (!MINI_WINDOW_P (w) || EQ (w->horizontal_scroll_bar_type, Qbottom))
 19587     {
 19588       struct buffer *b = XBUFFER (w->contents);
 19589       struct buffer *old_buffer = NULL;
 19590       struct it it;
 19591       struct text_pos startp;
 19592 
 19593       if (b != current_buffer)
 19594         {
 19595           old_buffer = current_buffer;
 19596           set_buffer_internal (b);
 19597         }
 19598 
 19599       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19600       start_display (&it, w, startp);
 19601       it.last_visible_x = INT_MAX;
 19602       whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
 19603                           MOVE_TO_X | MOVE_TO_Y);
 19604       /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
 19605                           window_box_height (w), -1,
 19606                           MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 19607 
 19608       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
 19609       end = start + window_box_width (w, TEXT_AREA);
 19610       portion = end - start;
 19611       /* After enlarging a horizontally scrolled window such that it
 19612          gets at least as wide as the text it contains, make sure that
 19613          the thumb doesn't fill the entire scroll bar so we can still
 19614          drag it back to see the entire text.  */
 19615       whole = max (whole, end);
 19616 
 19617       if (it.bidi_p)
 19618         {
 19619           Lisp_Object pdir;
 19620 
 19621           pdir = Fcurrent_bidi_paragraph_direction (Qnil);
 19622           if (EQ (pdir, Qright_to_left))
 19623             {
 19624               start = whole - end;
 19625               end = start + portion;
 19626             }
 19627         }
 19628 
 19629       if (old_buffer)
 19630         set_buffer_internal (old_buffer);
 19631     }
 19632   else
 19633     start = end = whole = portion = 0;
 19634 
 19635   w->hscroll_whole = whole;
 19636 
 19637   /* Indicate what this scroll bar ought to be displaying now.  */
 19638   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19639     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19640       (w, portion, whole, start);
 19641 }
 19642 
 19643 /* Subroutine of redisplay_window, to determine whether a window-start
 19644    point STARTP of WINDOW should be rejected.  */
 19645 static bool
 19646 window_start_acceptable_p (Lisp_Object window, ptrdiff_t startp)
 19647 {
 19648   if (!make_window_start_visible)
 19649     return true;
 19650 
 19651   struct window *w = XWINDOW (window);
 19652   struct frame *f = XFRAME (w->frame);
 19653   Lisp_Object startpos = make_fixnum (startp);
 19654   Lisp_Object invprop, disp_spec;
 19655   struct text_pos ignored;
 19656 
 19657   /* Is STARTP in invisible text?  */
 19658   if ((invprop = Fget_char_property (startpos, Qinvisible, window)),
 19659       TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19660     return false;
 19661 
 19662   /* Is STARTP covered by a replacing 'display' property?  */
 19663   if (!NILP (disp_spec = Fget_char_property (startpos, Qdisplay, window))
 19664       && handle_display_spec (NULL, disp_spec, Qnil, Qnil, &ignored, startp,
 19665                               FRAME_WINDOW_P (f)) > 0)
 19666     return false;
 19667 
 19668   return true;
 19669 }
 19670 
 19671 DEFUN ("long-line-optimizations-p", Flong_line_optimizations_p, Slong_line_optimizations_p,
 19672        0, 0, 0,
 19673        doc: /* Return non-nil if long-line optimizations are in effect in current buffer.
 19674 See `long-line-threshold' and `large-hscroll-threshold' for what these
 19675 optimizations mean and when they are in effect.  */)
 19676   (void)
 19677 {
 19678   return current_buffer->long_line_optimizations_p ? Qt : Qnil;
 19679 }
 19680 
 19681 /* Redisplay leaf window WINDOW.  JUST_THIS_ONE_P means only
 19682    selected_window is redisplayed.
 19683 
 19684    We can return without actually redisplaying the window if fonts has been
 19685    changed on window's frame.  In that case, redisplay_internal will retry.
 19686 
 19687    As one of the important parts of redisplaying a window, we need to
 19688    decide whether the previous window-start position (stored in the
 19689    window's w->start marker position) is still valid, and if it isn't,
 19690    recompute it.  Some details about that:
 19691 
 19692     . The previous window-start could be in a continuation line, in
 19693       which case we need to recompute it when the window width
 19694       changes.  See compute_window_start_on_continuation_line and its
 19695       call below.
 19696 
 19697     . The text that changed since last redisplay could include the
 19698       previous window-start position.  In that case, we try to salvage
 19699       what we can from the current glyph matrix by calling
 19700       try_scrolling, which see.
 19701 
 19702     . Some Emacs command could force us to use a specific window-start
 19703       position by setting the window's force_start flag, or gently
 19704       propose doing that by setting the window's optional_new_start
 19705       flag.  In these cases, we try using the specified start point if
 19706       that succeeds (i.e. the window desired matrix is successfully
 19707       recomputed, and point location is within the window).  In case
 19708       of optional_new_start, we first check if the specified start
 19709       position is feasible, i.e. if it will allow point to be
 19710       displayed in the window.  If using the specified start point
 19711       fails, e.g., if new fonts are needed to be loaded, we abort the
 19712       redisplay cycle and leave it up to the next cycle to figure out
 19713       things.
 19714 
 19715     . Note that the window's force_start flag is sometimes set by
 19716       redisplay itself, when it decides that the previous window start
 19717       point is fine and should be kept.  Search for "goto force_start"
 19718       below to see the details.  Like the values of window-start
 19719       specified outside of redisplay, these internally-deduced values
 19720       are tested for feasibility, and ignored if found to be
 19721       unfeasible.
 19722 
 19723     . Note that the function try_window, used to completely redisplay
 19724       a window, accepts the window's start point as its argument.
 19725       This is used several times in the redisplay code to control
 19726       where the window start will be, according to user options such
 19727       as scroll-conservatively, and also to ensure the screen line
 19728       showing point will be fully (as opposed to partially) visible on
 19729       display.  */
 19730 
 19731 static void
 19732 redisplay_window (Lisp_Object window, bool just_this_one_p)
 19733 {
 19734   struct window *w = XWINDOW (window);
 19735   struct frame *f = XFRAME (w->frame);
 19736   struct buffer *buffer = XBUFFER (w->contents);
 19737   struct buffer *old = current_buffer;
 19738   struct text_pos lpoint, opoint, startp;
 19739   bool update_mode_line;
 19740   int tem;
 19741   struct it it;
 19742   /* Record it now because it's overwritten.  */
 19743   bool current_matrix_up_to_date_p = false;
 19744   bool used_current_matrix_p = false;
 19745   /* This is less strict than current_matrix_up_to_date_p.
 19746      It indicates that the buffer contents and narrowing are unchanged.  */
 19747   bool buffer_unchanged_p = false;
 19748   bool temp_scroll_step = false;
 19749   specpdl_ref count = SPECPDL_INDEX ();
 19750   int rc;
 19751   int centering_position = -1;
 19752   bool last_line_misfit = false;
 19753   ptrdiff_t beg_unchanged, end_unchanged;
 19754   int frame_line_height, margin;
 19755   bool use_desired_matrix;
 19756   void *itdata = NULL;
 19757 
 19758   SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19759   opoint = lpoint;
 19760 
 19761 #ifdef GLYPH_DEBUG
 19762   *w->desired_matrix->method = 0;
 19763 #endif
 19764 
 19765   if (!just_this_one_p && needs_no_redisplay (w))
 19766     return;
 19767 
 19768   /* Make sure that both W's markers are valid.  */
 19769   eassert (XMARKER (w->start)->buffer == buffer);
 19770   eassert (XMARKER (w->pointm)->buffer == buffer);
 19771 
 19772   reconsider_clip_changes (w);
 19773   frame_line_height = default_line_pixel_height (w);
 19774   margin = window_scroll_margin (w, MARGIN_IN_LINES);
 19775 
 19776 
 19777   /* Has the mode line to be updated?  */
 19778   update_mode_line = (w->update_mode_line
 19779                       || update_mode_lines
 19780                       || buffer->clip_changed
 19781                       || buffer->prevent_redisplay_optimizations_p);
 19782 
 19783   if (!just_this_one_p)
 19784     /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
 19785        cleverly elsewhere.  */
 19786     w->must_be_updated_p = true;
 19787 
 19788   if (MINI_WINDOW_P (w))
 19789     {
 19790       if (w == XWINDOW (echo_area_window)
 19791           && !NILP (echo_area_buffer[0]))
 19792         {
 19793           if (update_mode_line)
 19794             /* We may have to update a tty frame's menu bar or a
 19795                tool-bar.  Example `M-x C-h C-h C-g'.  */
 19796             goto finish_menu_bars;
 19797           else
 19798             /* We've already displayed the echo area glyphs in this window.  */
 19799             goto finish_scroll_bars;
 19800         }
 19801       else if ((w != XWINDOW (minibuf_window)
 19802                 || minibuf_level == 0)
 19803                /* When buffer is nonempty, redisplay window normally.  */
 19804                && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
 19805                /* Quail displays non-mini buffers in minibuffer window.
 19806                   In that case, redisplay the window normally.  */
 19807                && !NILP (Fmemq (w->contents, Vminibuffer_list)))
 19808         {
 19809           /* W is a mini-buffer window, but it's not active, so clear
 19810              it.  */
 19811           int yb = window_text_bottom_y (w);
 19812           struct glyph_row *row;
 19813           int y;
 19814 
 19815           for (y = 0, row = w->desired_matrix->rows;
 19816                y < yb;
 19817                y += row->height, ++row)
 19818             blank_row (w, row, y);
 19819           goto finish_scroll_bars;
 19820         }
 19821       else if (minibuf_level >= 1)
 19822         {
 19823           /* We could have a message produced by set-minibuffer-message
 19824              displayed in the mini-window as an overlay, so resize the
 19825              mini-window if needed.  */
 19826           resize_mini_window (w, false);
 19827         }
 19828 
 19829       clear_glyph_matrix (w->desired_matrix);
 19830     }
 19831 
 19832   /* Otherwise set up data on this window; select its buffer and point
 19833      value.  */
 19834   /* Really select the buffer, for the sake of buffer-local
 19835      variables.  */
 19836   set_buffer_internal_1 (XBUFFER (w->contents));
 19837 
 19838   current_matrix_up_to_date_p
 19839     = (w->window_end_valid
 19840        && !current_buffer->clip_changed
 19841        && !current_buffer->prevent_redisplay_optimizations_p
 19842        && !window_outdated (w)
 19843        && !composition_break_at_point
 19844        && !hscrolling_current_line_p (w));
 19845 
 19846   beg_unchanged = BEG_UNCHANGED;
 19847   end_unchanged = END_UNCHANGED;
 19848 
 19849   SET_TEXT_POS (opoint, PT, PT_BYTE);
 19850 
 19851   specbind (Qinhibit_point_motion_hooks, Qt);
 19852 
 19853   buffer_unchanged_p
 19854     = (w->window_end_valid
 19855        && !current_buffer->clip_changed
 19856        && !window_outdated (w));
 19857 
 19858   /* When windows_or_buffers_changed is non-zero, we can't rely
 19859      on the window end being valid, so set it to zero there.  */
 19860   if (windows_or_buffers_changed)
 19861     {
 19862       /* If window starts on a continuation line, maybe adjust the
 19863          window start in case the window's width changed.  */
 19864       if (XMARKER (w->start)->buffer == current_buffer)
 19865         compute_window_start_on_continuation_line (w);
 19866 
 19867       w->window_end_valid = false;
 19868       /* If so, we also can't rely on current matrix
 19869          and should not fool try_cursor_movement below.  */
 19870       current_matrix_up_to_date_p = false;
 19871     }
 19872 
 19873   /* Some sanity checks.  */
 19874   CHECK_WINDOW_END (w);
 19875   if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
 19876     emacs_abort ();
 19877   if (BYTEPOS (opoint) < CHARPOS (opoint))
 19878     emacs_abort ();
 19879 
 19880   if (mode_line_update_needed (w))
 19881     update_mode_line = true;
 19882 
 19883   /* Point refers normally to the selected window.  For any other
 19884      window, set up appropriate value.  */
 19885   if (!EQ (window, selected_window))
 19886     {
 19887       ptrdiff_t new_pt = marker_position (w->pointm);
 19888       ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
 19889 
 19890       if (new_pt < BEGV)
 19891         {
 19892           new_pt = BEGV;
 19893           new_pt_byte = BEGV_BYTE;
 19894           set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
 19895         }
 19896       else if (new_pt > (ZV - 1))
 19897         {
 19898           new_pt = ZV;
 19899           new_pt_byte = ZV_BYTE;
 19900           set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
 19901         }
 19902 
 19903       /* We don't use SET_PT so that the point-motion hooks don't run.  */
 19904       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
 19905     }
 19906 
 19907   /* If any of the character widths specified in the display table
 19908      have changed, invalidate the width run cache.  It's true that
 19909      this may be a bit late to catch such changes, but the rest of
 19910      redisplay goes (non-fatally) haywire when the display table is
 19911      changed, so why should we worry about doing any better?  */
 19912   if (current_buffer->width_run_cache
 19913       || (current_buffer->base_buffer
 19914           && current_buffer->base_buffer->width_run_cache))
 19915     {
 19916       struct Lisp_Char_Table *disptab = buffer_display_table ();
 19917 
 19918       if (! disptab_matches_widthtab
 19919           (disptab, XVECTOR (BVAR (current_buffer, width_table))))
 19920         {
 19921           struct buffer *buf = current_buffer;
 19922 
 19923           if (buf->base_buffer)
 19924             buf = buf->base_buffer;
 19925           invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
 19926           recompute_width_table (current_buffer, disptab);
 19927         }
 19928     }
 19929 
 19930   /* Check whether the buffer to be displayed contains long lines.  */
 19931   if (!NILP (Vlong_line_threshold)
 19932       && !current_buffer->long_line_optimizations_p
 19933       && (CHARS_MODIFF - UNCHANGED_MODIFIED > 8
 19934           || current_buffer->clip_changed))
 19935     {
 19936       ptrdiff_t cur, next, found, max = 0, threshold;
 19937       threshold = XFIXNUM (Vlong_line_threshold);
 19938       for (cur = BEGV; cur < ZV; cur = next)
 19939         {
 19940           next = find_newline1 (cur, CHAR_TO_BYTE (cur), 0, -1, 1,
 19941                                 &found, NULL, true);
 19942           if (next - cur > max) max = next - cur;
 19943           if (!found || max > threshold) break;
 19944         }
 19945       if (max > threshold)
 19946         current_buffer->long_line_optimizations_p = true;
 19947     }
 19948 
 19949   /* If window-start is screwed up, choose a new one.  */
 19950   if (XMARKER (w->start)->buffer != current_buffer)
 19951     goto recenter;
 19952 
 19953   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19954 
 19955   /* If someone specified a new starting point but did not insist,
 19956      check whether it can be used.  */
 19957   if ((w->optional_new_start || window_frozen_p (w))
 19958       && CHARPOS (startp) >= BEGV
 19959       && CHARPOS (startp) <= ZV)
 19960     {
 19961       ptrdiff_t it_charpos;
 19962 
 19963       w->optional_new_start = false;
 19964       if (!w->force_start)
 19965         {
 19966           start_display (&it, w, startp);
 19967           move_it_to (&it, PT, 0, it.last_visible_y, -1,
 19968                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19969           /* Record IT's position now, since line_bottom_y might
 19970              change that.  */
 19971           it_charpos = IT_CHARPOS (it);
 19972           /* Make sure we set the force_start flag only if the cursor
 19973              row will be fully visible.  Otherwise, the code under
 19974              force_start label below will try to move point back into
 19975              view, which is not what the code which sets
 19976              optional_new_start wants.  */
 19977           if (it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
 19978             {
 19979               if (it_charpos == PT)
 19980                 w->force_start = true;
 19981               /* IT may overshoot PT if text at PT is invisible.  */
 19982               else if (it_charpos > PT && CHARPOS (startp) <= PT)
 19983                 w->force_start = true;
 19984 #ifdef GLYPH_DEBUG
 19985               if (w->force_start)
 19986                 {
 19987                   if (window_frozen_p (w))
 19988                     debug_method_add (w, "set force_start from frozen window start");
 19989                   else
 19990                     debug_method_add (w, "set force_start from optional_new_start");
 19991                 }
 19992 #endif
 19993             }
 19994         }
 19995     }
 19996 
 19997  force_start:
 19998 
 19999   /* Handle case where place to start displaying has been specified,
 20000      unless the specified location is outside the accessible range.  */
 20001   if (w->force_start)
 20002     {
 20003       /* We set this later on if we have to adjust point.  */
 20004       int new_vpos = -1;
 20005 
 20006       w->force_start = false;
 20007 
 20008       /* The vscroll should be preserved in this case, since
 20009          `pixel-scroll-precision-mode' must continue working normally
 20010          when a mini-window is resized.  (bug#55312) */
 20011       if (!w->preserve_vscroll_p || !window_frozen_p (w))
 20012         w->vscroll = 0;
 20013 
 20014       w->preserve_vscroll_p = false;
 20015       w->window_end_valid = false;
 20016 
 20017       /* Forget any recorded base line for line number display.  */
 20018       if (!buffer_unchanged_p)
 20019         w->base_line_number = 0;
 20020 
 20021       /* Redisplay the mode line.  Select the buffer properly for that.
 20022          Also, run the hook window-scroll-functions
 20023          because we have scrolled.  */
 20024       /* Note, we do this after clearing force_start because
 20025          if there's an error, it is better to forget about force_start
 20026          than to get into an infinite loop calling the hook functions
 20027          and having them get more errors.  */
 20028       if (!update_mode_line
 20029           || ! NILP (Vwindow_scroll_functions))
 20030         {
 20031           update_mode_line = true;
 20032           w->update_mode_line = true;
 20033           startp = run_window_scroll_functions (window, startp);
 20034         }
 20035 
 20036       if (CHARPOS (startp) < BEGV)
 20037         SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
 20038       else if (CHARPOS (startp) > ZV)
 20039         SET_TEXT_POS (startp, ZV, ZV_BYTE);
 20040 
 20041       /* Reject the specified start location if it is invisible, and
 20042          the buffer wants it always visible.  */
 20043       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20044         goto ignore_start;
 20045 
 20046       /* Redisplay, then check if cursor has been set during the
 20047          redisplay.  Give up if new fonts were loaded.  */
 20048       /* We used to issue a CHECK_MARGINS argument to try_window here,
 20049          but this causes scrolling to fail when point begins inside
 20050          the scroll margin (bug#148) -- cyd  */
 20051       clear_glyph_matrix (w->desired_matrix);
 20052       if (!try_window (window, startp, 0))
 20053         {
 20054           w->force_start = true;
 20055           clear_glyph_matrix (w->desired_matrix);
 20056           goto need_larger_matrices;
 20057         }
 20058 
 20059       if (w->cursor.vpos < 0)
 20060         {
 20061           /* If point does not appear, try to move point so it does
 20062              appear.  The desired matrix has been built above, so we
 20063              can use it here.  First see if point is in invisible
 20064              text, and if so, move it to the first visible buffer
 20065              position past that.  */
 20066           struct glyph_row *r = NULL;
 20067           Lisp_Object invprop =
 20068             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20069                                            Qnil, NULL);
 20070 
 20071           if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 20072             {
 20073               ptrdiff_t alt_pt;
 20074               Lisp_Object invprop_end =
 20075                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20076                                                    Qnil, Qnil);
 20077 
 20078               if (FIXNATP (invprop_end))
 20079                 alt_pt = XFIXNAT (invprop_end);
 20080               else
 20081                 alt_pt = ZV;
 20082               r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
 20083                                       NULL, 0);
 20084             }
 20085           if (r)
 20086             new_vpos = MATRIX_ROW_BOTTOM_Y (r);
 20087           else  /* Give up and just move to the middle of the window.  */
 20088             new_vpos = window_box_height (w) / 2;
 20089         }
 20090 
 20091       if (!cursor_row_fully_visible_p (w, false, false, false))
 20092         {
 20093           /* Point does appear, but on a line partly visible at end of window.
 20094              Move it back to a fully-visible line.  */
 20095           new_vpos = window_box_height (w);
 20096           /* But if window_box_height suggests a Y coordinate that is
 20097              not less than we already have, that line will clearly not
 20098              be fully visible, so give up and scroll the display.
 20099              This can happen when the default face uses a font whose
 20100              dimensions are different from the frame's default
 20101              font.  */
 20102           if (new_vpos >= w->cursor.y)
 20103             {
 20104               w->cursor.vpos = -1;
 20105               clear_glyph_matrix (w->desired_matrix);
 20106               goto try_to_scroll;
 20107             }
 20108         }
 20109       else if (w->cursor.vpos >= 0)
 20110         {
 20111           /* Some people insist on not letting point enter the scroll
 20112              margin, even though this part handles windows that didn't
 20113              scroll at all.  */
 20114           int pixel_margin = margin * frame_line_height;
 20115           bool tab_line = window_wants_tab_line (w);
 20116           bool header_line = window_wants_header_line (w);
 20117 
 20118           /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
 20119              below, which finds the row to move point to, advances by
 20120              the Y coordinate of the _next_ row, see the definition of
 20121              MATRIX_ROW_BOTTOM_Y.  */
 20122           if (w->cursor.vpos < margin + tab_line + header_line)
 20123             {
 20124               w->cursor.vpos = -1;
 20125               clear_glyph_matrix (w->desired_matrix);
 20126               goto try_to_scroll;
 20127             }
 20128           else
 20129             {
 20130               int window_height = window_box_height (w);
 20131 
 20132               if (tab_line)
 20133                 window_height += CURRENT_TAB_LINE_HEIGHT (w);
 20134               if (header_line)
 20135                 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
 20136               if (w->cursor.y >= window_height - pixel_margin)
 20137                 {
 20138                   w->cursor.vpos = -1;
 20139                   clear_glyph_matrix (w->desired_matrix);
 20140                   goto try_to_scroll;
 20141                 }
 20142             }
 20143         }
 20144 
 20145       /* If we need to move point for either of the above reasons,
 20146          now actually do it.  */
 20147       if (new_vpos >= 0)
 20148         {
 20149           struct glyph_row *row;
 20150 
 20151           row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
 20152           while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos
 20153                  && !row->ends_at_zv_p)
 20154             ++row;
 20155 
 20156           TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
 20157                             MATRIX_ROW_START_BYTEPOS (row));
 20158 
 20159           if (w != XWINDOW (selected_window))
 20160             set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
 20161           else if (current_buffer == old)
 20162             SET_TEXT_POS (lpoint, PT, PT_BYTE);
 20163 
 20164           set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 20165 
 20166           /* Re-run pre-redisplay-function so it can update the region
 20167              according to the new position of point.  */
 20168           /* Other than the cursor, w's redisplay is done so we can set its
 20169              redisplay to false.  Also the buffer's redisplay can be set to
 20170              false, since propagate_buffer_redisplay should have already
 20171              propagated its info to `w' anyway.  */
 20172           w->redisplay = false;
 20173           XBUFFER (w->contents)->text->redisplay = false;
 20174           safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
 20175 
 20176           if (w->redisplay || XBUFFER (w->contents)->text->redisplay
 20177               || ((EQ (Vdisplay_line_numbers, Qrelative)
 20178                    || EQ (Vdisplay_line_numbers, Qvisual))
 20179                   && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
 20180             {
 20181               /* Either pre-redisplay-function made changes (e.g. move
 20182                  the region), or we moved point in a window that is
 20183                  under display-line-numbers = relative mode.  We need
 20184                  another round of redisplay.  */
 20185               clear_glyph_matrix (w->desired_matrix);
 20186               if (!try_window (window, startp, 0))
 20187                 goto need_larger_matrices;
 20188             }
 20189         }
 20190       if (w->cursor.vpos < 0
 20191           || !cursor_row_fully_visible_p (w, false, false, false))
 20192         {
 20193           clear_glyph_matrix (w->desired_matrix);
 20194           goto try_to_scroll;
 20195         }
 20196 
 20197 #ifdef GLYPH_DEBUG
 20198       debug_method_add (w, "forced window start");
 20199 #endif
 20200       goto done;
 20201     }
 20202 
 20203  ignore_start:
 20204 
 20205   /* Handle case where text has not changed, only point, and it has
 20206      not moved off the frame, and we are not retrying after hscroll.
 20207      (current_matrix_up_to_date_p is true when retrying.)  */
 20208   if (current_matrix_up_to_date_p
 20209       && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
 20210           rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
 20211     {
 20212       switch (rc)
 20213         {
 20214         case CURSOR_MOVEMENT_SUCCESS:
 20215           used_current_matrix_p = true;
 20216           goto done;
 20217 
 20218         case CURSOR_MOVEMENT_MUST_SCROLL:
 20219           goto try_to_scroll;
 20220 
 20221         default:
 20222           emacs_abort ();
 20223         }
 20224     }
 20225   /* If current starting point was originally the beginning of a line
 20226      but no longer is, or if the starting point is invisible but the
 20227      buffer wants it always visible, find a new starting point.  */
 20228   else if (w->start_at_line_beg
 20229            && ((CHARPOS (startp) > BEGV
 20230                 && FETCH_BYTE (BYTEPOS (startp) - 1) != '\n')
 20231                || (CHARPOS (startp) >= BEGV
 20232                    && CHARPOS (startp) <= ZV
 20233                    && !window_start_acceptable_p (window, CHARPOS (startp)))))
 20234     {
 20235 #ifdef GLYPH_DEBUG
 20236       debug_method_add (w, "recenter 1");
 20237 #endif
 20238       goto recenter;
 20239     }
 20240 
 20241   /* Try scrolling with try_window_id.  Value is > 0 if update has
 20242      been done, it is -1 if we know that the same window start will
 20243      not work.  It is 0 if unsuccessful for some other reason.  */
 20244   else if ((tem = try_window_id (w)) != 0)
 20245     {
 20246 #ifdef GLYPH_DEBUG
 20247       debug_method_add (w, "try_window_id %d", tem);
 20248 #endif
 20249 
 20250       if (f->fonts_changed)
 20251         goto need_larger_matrices;
 20252       if (tem > 0)
 20253         goto done;
 20254 
 20255       /* Otherwise try_window_id has returned -1 which means that we
 20256          don't want the alternative below this comment to execute.  */
 20257     }
 20258   else if (CHARPOS (startp) >= BEGV
 20259            && CHARPOS (startp) <= ZV
 20260            && PT >= CHARPOS (startp)
 20261            && (CHARPOS (startp) < ZV
 20262                /* Avoid starting at end of buffer.  */
 20263                || CHARPOS (startp) == BEGV
 20264                || !window_outdated (w)))
 20265     {
 20266       int d1, d2, d5, d6;
 20267       int rtop, rbot;
 20268 
 20269       /* If first window line is a continuation line, and window start
 20270          is inside the modified region, but the first change is before
 20271          current window start, we must select a new window start.
 20272 
 20273          However, if this is the result of a down-mouse event (e.g. by
 20274          extending the mouse-drag-overlay), we don't want to select a
 20275          new window start, since that would change the position under
 20276          the mouse, resulting in an unwanted mouse-movement rather
 20277          than a simple mouse-click.  */
 20278       if (!w->start_at_line_beg
 20279           && NILP (track_mouse)
 20280           && CHARPOS (startp) > BEGV
 20281           && CHARPOS (startp) > BEG + beg_unchanged
 20282           && CHARPOS (startp) <= Z - end_unchanged
 20283           /* Even if w->start_at_line_beg is nil, a new window may
 20284              start at a line_beg, since that's how set_buffer_window
 20285              sets it.  So, we need to check the return value of
 20286              compute_window_start_on_continuation_line.  (See also
 20287              bug#197).  */
 20288           && XMARKER (w->start)->buffer == current_buffer
 20289           && compute_window_start_on_continuation_line (w)
 20290           /* It doesn't make sense to force the window start like we
 20291              do at label force_start if it is already known that point
 20292              will not be fully visible in the resulting window, because
 20293              doing so will move point from its correct position
 20294              instead of scrolling the window to bring point into view.
 20295              See bug#9324.  */
 20296           && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
 20297           /* A very tall row could need more than the window height,
 20298              in which case we accept that it is partially visible.  */
 20299           && (rtop != 0) == (rbot != 0))
 20300         {
 20301           w->force_start = true;
 20302           SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20303 #ifdef GLYPH_DEBUG
 20304           debug_method_add (w, "recomputed window start in continuation line");
 20305 #endif
 20306           goto force_start;
 20307         }
 20308 
 20309       /* Don't use the same window-start if it is invisible or covered
 20310          by a replacing 'display' property and the buffer requested
 20311          the window-start to be always visible.  */
 20312       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20313         {
 20314 #ifdef GLYPH_DEBUG
 20315           debug_method_add (w, "recenter 2");
 20316 #endif
 20317           goto recenter;
 20318         }
 20319 
 20320 #ifdef GLYPH_DEBUG
 20321       debug_method_add (w, "same window start");
 20322 #endif
 20323 
 20324       /* Try to redisplay starting at same place as before.
 20325          If point has not moved off frame, accept the results.  */
 20326       if (!current_matrix_up_to_date_p
 20327           /* Don't use try_window_reusing_current_matrix in this case
 20328              because a window scroll function can have changed the
 20329              buffer.  */
 20330           || !NILP (Vwindow_scroll_functions)
 20331           || MINI_WINDOW_P (w)
 20332           || !(used_current_matrix_p
 20333                = try_window_reusing_current_matrix (w)))
 20334         {
 20335           IF_DEBUG (debug_method_add (w, "1"));
 20336           clear_glyph_matrix (w->desired_matrix);
 20337           if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
 20338             /* -1 means we need to scroll.
 20339                0 means we need new matrices, but fonts_changed
 20340                is set in that case, so we will detect it below.  */
 20341             goto try_to_scroll;
 20342         }
 20343 
 20344       if (f->fonts_changed)
 20345         goto need_larger_matrices;
 20346 
 20347       if (w->cursor.vpos >= 0)
 20348         {
 20349           if (!just_this_one_p
 20350               || current_buffer->clip_changed
 20351               || BEG_UNCHANGED < CHARPOS (startp))
 20352             /* Forget any recorded base line for line number display.  */
 20353             w->base_line_number = 0;
 20354 
 20355           if (!cursor_row_fully_visible_p (w, true, false, false))
 20356             {
 20357               clear_glyph_matrix (w->desired_matrix);
 20358               last_line_misfit = true;
 20359             }
 20360             /* Drop through and scroll.  */
 20361           else
 20362             goto done;
 20363         }
 20364       else
 20365         clear_glyph_matrix (w->desired_matrix);
 20366     }
 20367 
 20368  try_to_scroll:
 20369 
 20370   /* Redisplay the mode line.  Select the buffer properly for that.  */
 20371   if (!update_mode_line)
 20372     {
 20373       update_mode_line = true;
 20374       w->update_mode_line = true;
 20375     }
 20376 
 20377   /* Try to scroll by specified few lines.  */
 20378   if ((0 < scroll_conservatively
 20379        /* FIXME: the option is supposed to affect minibuffers, but we
 20380           test MINI_WINDOW_P, which can also catch uses of
 20381           mini-windows for displaying the echo area.  Do we need to
 20382           distinguish these two use cases?  */
 20383        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
 20384        || 0 < emacs_scroll_step
 20385        || temp_scroll_step
 20386        || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
 20387        || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
 20388       && CHARPOS (startp) >= BEGV
 20389       && CHARPOS (startp) <= ZV)
 20390     {
 20391       /* The function returns -1 if new fonts were loaded, 1 if
 20392          successful, 0 if not successful.  */
 20393       int ss = try_scrolling (window, just_this_one_p,
 20394                               ((scroll_minibuffer_conservatively
 20395                                 && MINI_WINDOW_P (w))
 20396                                ? SCROLL_LIMIT + 1
 20397                                : scroll_conservatively),
 20398                               emacs_scroll_step,
 20399                               temp_scroll_step, last_line_misfit);
 20400       switch (ss)
 20401         {
 20402         case SCROLLING_SUCCESS:
 20403           goto done;
 20404 
 20405         case SCROLLING_NEED_LARGER_MATRICES:
 20406           goto need_larger_matrices;
 20407 
 20408         case SCROLLING_FAILED:
 20409           break;
 20410 
 20411         default:
 20412           emacs_abort ();
 20413         }
 20414     }
 20415 
 20416   /* Finally, just choose a place to start which positions point
 20417      according to user preferences.  */
 20418 
 20419  recenter:
 20420 
 20421 #ifdef GLYPH_DEBUG
 20422   debug_method_add (w, "recenter");
 20423 #endif
 20424 
 20425   /* Forget any previously recorded base line for line number display.  */
 20426   if (!buffer_unchanged_p)
 20427     w->base_line_number = 0;
 20428 
 20429   /* Determine the window start relative to point.  */
 20430   init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20431   it.current_y = it.last_visible_y;
 20432   if (centering_position < 0)
 20433     {
 20434       ptrdiff_t margin_pos = CHARPOS (startp);
 20435       Lisp_Object aggressive;
 20436       bool scrolling_up;
 20437 
 20438       /* If there is a scroll margin at the top of the window, find
 20439          its character position.  */
 20440       if (margin
 20441           /* Cannot call start_display if startp is not in the
 20442              accessible region of the buffer.  This can happen when we
 20443              have just switched to a different buffer and/or changed
 20444              its restriction.  In that case, startp is initialized to
 20445              the character position 1 (BEGV) because we did not yet
 20446              have chance to display the buffer even once.  */
 20447           && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
 20448         {
 20449           struct it it1;
 20450           void *it1data = NULL;
 20451 
 20452           SAVE_IT (it1, it, it1data);
 20453           start_display (&it1, w, startp);
 20454           move_it_vertically (&it1, margin * frame_line_height);
 20455           margin_pos = IT_CHARPOS (it1);
 20456           RESTORE_IT (&it, &it, it1data);
 20457         }
 20458       scrolling_up = PT > margin_pos;
 20459       aggressive =
 20460         scrolling_up
 20461         ? BVAR (current_buffer, scroll_up_aggressively)
 20462         : BVAR (current_buffer, scroll_down_aggressively);
 20463 
 20464       if (!MINI_WINDOW_P (w)
 20465           && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
 20466         {
 20467           int pt_offset = 0;
 20468 
 20469           /* Setting scroll-conservatively overrides
 20470              scroll-*-aggressively.  */
 20471           if (!scroll_conservatively && NUMBERP (aggressive))
 20472             {
 20473               double float_amount = XFLOATINT (aggressive);
 20474 
 20475               pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
 20476               if (pt_offset == 0 && float_amount > 0)
 20477                 pt_offset = 1;
 20478               if (pt_offset && margin > 0)
 20479                 margin -= 1;
 20480             }
 20481           /* Compute how much to move the window start backward from
 20482              point so that point will be displayed where the user
 20483              wants it.  */
 20484           if (scrolling_up)
 20485             {
 20486               centering_position = it.last_visible_y;
 20487               if (pt_offset)
 20488                 centering_position -= pt_offset;
 20489               centering_position -=
 20490                 (frame_line_height * (1 + margin + last_line_misfit)
 20491                  + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 20492               /* Don't let point enter the scroll margin near top of
 20493                  the window.  */
 20494               if (centering_position < margin * frame_line_height)
 20495                 centering_position = margin * frame_line_height;
 20496             }
 20497           else
 20498             centering_position = margin * frame_line_height + pt_offset;
 20499         }
 20500       else
 20501         /* Set the window start half the height of the window backward
 20502            from point.  */
 20503         centering_position = window_box_height (w) / 2;
 20504     }
 20505   if (current_buffer->long_line_optimizations_p
 20506       && it.line_wrap == TRUNCATE)
 20507     {
 20508       /* For very long and truncated lines, go back using a simplified
 20509          method, which ignored any inaccuracies due to line-height
 20510          differences, display properties/overlays, etc.  */
 20511       int nlines = centering_position / frame_line_height;
 20512 
 20513       while (nlines-- && IT_CHARPOS (it) > BEGV)
 20514         back_to_previous_visible_line_start (&it);
 20515       reseat_1 (&it, it.current.pos, true);
 20516     }
 20517   else
 20518     move_it_vertically_backward (&it, centering_position);
 20519 
 20520   eassert (IT_CHARPOS (it) >= BEGV);
 20521 
 20522   /* The function move_it_vertically_backward may move over more
 20523      than the specified y-distance.  If it->w is small, e.g. a
 20524      mini-buffer window, we may end up in front of the window's
 20525      display area.  Start displaying at the start of the line
 20526      containing PT in this case.  */
 20527   if (it.current_y <= 0)
 20528     {
 20529       init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20530       move_it_vertically_backward (&it, 0);
 20531       it.current_y = 0;
 20532     }
 20533 
 20534   it.current_x = it.hpos = 0;
 20535 
 20536   /* Set the window start position here explicitly, to avoid an
 20537      infinite loop in case the functions in window-scroll-functions
 20538      get errors.  */
 20539   set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
 20540 
 20541   /* Run scroll hooks.  */
 20542   startp = run_window_scroll_functions (window, it.current.pos);
 20543 
 20544   /* We invoke try_window and try_window_reusing_current_matrix below,
 20545      and they manipulate the bidi cache.  Save and restore the cache
 20546      state of our iterator, so we could continue using it after that.  */
 20547   itdata = bidi_shelve_cache ();
 20548 
 20549   /* Redisplay the window.  */
 20550   use_desired_matrix = false;
 20551   if (!current_matrix_up_to_date_p
 20552       || windows_or_buffers_changed
 20553       || f->cursor_type_changed
 20554       /* Don't use try_window_reusing_current_matrix in this case
 20555          because it can have changed the buffer.  */
 20556       || !NILP (Vwindow_scroll_functions)
 20557       || !just_this_one_p
 20558       || MINI_WINDOW_P (w)
 20559       || !(used_current_matrix_p
 20560            = try_window_reusing_current_matrix (w)))
 20561     use_desired_matrix = (try_window (window, startp, 0) == 1);
 20562 
 20563   bidi_unshelve_cache (itdata, false);
 20564 
 20565   /* If new fonts have been loaded (due to fontsets), give up.  We
 20566      have to start a new redisplay since we need to re-adjust glyph
 20567      matrices.  */
 20568   if (f->fonts_changed)
 20569     goto need_larger_matrices;
 20570 
 20571   /* If cursor did not appear assume that the middle of the window is
 20572      in the first line of the window.  Do it again with the next line.
 20573      (Imagine a window of height 100, displaying two lines of height
 20574      60.  Moving back 50 from it->last_visible_y will end in the first
 20575      line.)  */
 20576   if (w->cursor.vpos < 0)
 20577     {
 20578       if (w->window_end_valid && PT >= Z - w->window_end_pos)
 20579         {
 20580           clear_glyph_matrix (w->desired_matrix);
 20581           move_it_by_lines (&it, 1);
 20582           try_window (window, it.current.pos, 0);
 20583         }
 20584       else if (PT < IT_CHARPOS (it))
 20585         {
 20586           clear_glyph_matrix (w->desired_matrix);
 20587           move_it_by_lines (&it, -1);
 20588           try_window (window, it.current.pos, 0);
 20589         }
 20590       else if (scroll_conservatively > SCROLL_LIMIT
 20591                && (it.method == GET_FROM_STRING
 20592                    || overlay_touches_p (IT_CHARPOS (it)))
 20593                && IT_CHARPOS (it) < ZV)
 20594         {
 20595           /* If the window starts with a before-string that spans more
 20596              than one screen line, using that position to display the
 20597              window might fail to bring point into the view, because
 20598              start_display will always start by displaying the string,
 20599              whereas the code above determines where to set w->start
 20600              by the buffer position of the place where it takes screen
 20601              coordinates.  Try to recover by finding the next screen
 20602              line that displays buffer text.  */
 20603           ptrdiff_t pos0 = IT_CHARPOS (it);
 20604 
 20605           clear_glyph_matrix (w->desired_matrix);
 20606           do {
 20607             move_it_by_lines (&it, 1);
 20608           } while (IT_CHARPOS (it) == pos0);
 20609           try_window (window, it.current.pos, 0);
 20610         }
 20611       else
 20612         {
 20613           /* Not much we can do about it.  */
 20614         }
 20615     }
 20616 
 20617   /* Consider the following case: Window starts at BEGV, there is
 20618      invisible, intangible text at BEGV, so that display starts at
 20619      some point START > BEGV.  It can happen that we are called with
 20620      PT somewhere between BEGV and START.  Try to handle that case,
 20621      and similar ones.  */
 20622   if (w->cursor.vpos < 0)
 20623     {
 20624       /* Prefer the desired matrix to the current matrix, if possible,
 20625          in the fallback calculations below.  This is because using
 20626          the current matrix might completely goof, e.g. if its first
 20627          row is after point.  */
 20628       struct glyph_matrix *matrix =
 20629         use_desired_matrix ? w->desired_matrix : w->current_matrix;
 20630       /* First, try locating the proper glyph row for PT.  */
 20631       struct glyph_row *row =
 20632         row_containing_pos (w, PT, matrix->rows, NULL, 0);
 20633 
 20634       /* Sometimes point is at the beginning of invisible text that is
 20635          before the 1st character displayed in the row.  In that case,
 20636          row_containing_pos fails to find the row, because no glyphs
 20637          with appropriate buffer positions are present in the row.
 20638          Therefore, we next try to find the row which shows the 1st
 20639          position after the invisible text.  */
 20640       if (!row)
 20641         {
 20642           Lisp_Object val =
 20643             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20644                                            Qnil, NULL);
 20645 
 20646           if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 20647             {
 20648               ptrdiff_t alt_pos;
 20649               Lisp_Object invis_end =
 20650                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20651                                                    Qnil, Qnil);
 20652 
 20653               if (FIXNATP (invis_end))
 20654                 alt_pos = XFIXNAT (invis_end);
 20655               else
 20656                 alt_pos = ZV;
 20657               row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
 20658             }
 20659         }
 20660       /* Finally, fall back on the first row of the window after the
 20661          tab-line and header line (if any).  This is slightly better
 20662          than not displaying the cursor at all.  */
 20663       if (!row)
 20664         {
 20665           row = matrix->rows;
 20666           /* Skip the tab-line and header-line rows, if any.  */
 20667           if (row->tab_line_p)
 20668             ++row;
 20669           if (row->mode_line_p)
 20670             ++row;
 20671         }
 20672       set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
 20673     }
 20674 
 20675   if (!cursor_row_fully_visible_p (w, false, false, false))
 20676     {
 20677       /* If vscroll is enabled, disable it and try again.  */
 20678       if (w->vscroll)
 20679         {
 20680           w->vscroll = 0;
 20681           clear_glyph_matrix (w->desired_matrix);
 20682           goto recenter;
 20683         }
 20684 
 20685       /* Users who set scroll-conservatively to a large number want
 20686          point just above/below the scroll margin.  If we ended up
 20687          with point's row partially visible, move the window start to
 20688          make that row fully visible and out of the margin.  */
 20689       if (scroll_conservatively > SCROLL_LIMIT)
 20690         {
 20691           int window_total_lines
 20692             = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
 20693           bool move_down = w->cursor.vpos >= window_total_lines / 2;
 20694 
 20695           move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
 20696           clear_glyph_matrix (w->desired_matrix);
 20697           if (1 == try_window (window, it.current.pos,
 20698                                TRY_WINDOW_CHECK_MARGINS))
 20699             goto done;
 20700         }
 20701 
 20702       /* If centering point failed to make the whole line visible,
 20703          put point at the top instead.  That has to make the whole line
 20704          visible, if it can be done.  */
 20705       if (centering_position == 0)
 20706         goto done;
 20707 
 20708       clear_glyph_matrix (w->desired_matrix);
 20709       centering_position = 0;
 20710       goto recenter;
 20711     }
 20712 
 20713  done:
 20714 
 20715   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20716   w->start_at_line_beg = (CHARPOS (startp) == BEGV
 20717                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
 20718 
 20719   /* Display the mode line, header line, and tab-line, if we must.  */
 20720   if ((update_mode_line
 20721        /* If window not full width, must redo its mode line
 20722           if (a) the window to its side is being redone and
 20723           (b) we do a frame-based redisplay.  This is a consequence
 20724           of how inverted lines are drawn in frame-based redisplay.  */
 20725        || (!just_this_one_p
 20726            && !FRAME_WINDOW_P (f)
 20727            && !WINDOW_FULL_WIDTH_P (w))
 20728        /* Line number to display.  */
 20729        || w->base_line_pos > 0
 20730        /* Column number is displayed and different from the one displayed.  */
 20731        || (w->column_number_displayed != -1
 20732            && (w->column_number_displayed != current_column ())))
 20733       /* This means that the window has a mode line.  */
 20734       && (window_wants_mode_line (w)
 20735           || window_wants_header_line (w)
 20736           || window_wants_tab_line (w)))
 20737     {
 20738       specpdl_ref count1 = SPECPDL_INDEX ();
 20739 
 20740       specbind (Qinhibit_quit, Qt);
 20741       display_mode_lines (w);
 20742       unbind_to (count1, Qnil);
 20743 
 20744       /* If mode line height has changed, arrange for a thorough
 20745          immediate redisplay using the correct mode line height.  */
 20746       if (window_wants_mode_line (w)
 20747           && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
 20748         {
 20749           f->fonts_changed = true;
 20750           w->mode_line_height = -1;
 20751           MATRIX_MODE_LINE_ROW (w->current_matrix)->height
 20752             = DESIRED_MODE_LINE_HEIGHT (w);
 20753         }
 20754 
 20755       /* If tab line height has changed, arrange for a thorough
 20756          immediate redisplay using the correct tab line height.  */
 20757       if (window_wants_tab_line (w)
 20758           && CURRENT_TAB_LINE_HEIGHT (w) != DESIRED_TAB_LINE_HEIGHT (w))
 20759         {
 20760           f->fonts_changed = true;
 20761           w->tab_line_height = -1;
 20762           MATRIX_TAB_LINE_ROW (w->current_matrix)->height
 20763             = DESIRED_TAB_LINE_HEIGHT (w);
 20764         }
 20765 
 20766       /* If header line height has changed, arrange for a thorough
 20767          immediate redisplay using the correct header line height.  */
 20768       if (window_wants_header_line (w)
 20769           && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
 20770         {
 20771           f->fonts_changed = true;
 20772           w->header_line_height = -1;
 20773           MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
 20774             = DESIRED_HEADER_LINE_HEIGHT (w);
 20775         }
 20776 
 20777       if (f->fonts_changed)
 20778         goto need_larger_matrices;
 20779     }
 20780 
 20781   if (!line_number_displayed && w->base_line_pos != -1)
 20782     {
 20783       w->base_line_pos = 0;
 20784       w->base_line_number = 0;
 20785     }
 20786 
 20787  finish_menu_bars:
 20788 
 20789   /* When we reach a frame's selected window, redo the frame's menu
 20790      bar, tool bar, tab-bar, and the frame's title.  */
 20791   if (update_mode_line
 20792       && EQ (FRAME_SELECTED_WINDOW (f), window))
 20793     {
 20794       bool redisplay_menu_p;
 20795 
 20796       if (FRAME_WINDOW_P (f))
 20797         {
 20798 #ifdef HAVE_EXT_MENU_BAR
 20799           redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
 20800 #else
 20801           redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20802 #endif
 20803         }
 20804       else
 20805         redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20806 
 20807       if (redisplay_menu_p)
 20808         display_menu_bar (w);
 20809 
 20810 #ifdef HAVE_WINDOW_SYSTEM
 20811       if (FRAME_WINDOW_P (f))
 20812         {
 20813           if (WINDOWP (f->tab_bar_window)
 20814               && (FRAME_TAB_BAR_LINES (f) > 0
 20815                   || !NILP (Vauto_resize_tab_bars))
 20816               && redisplay_tab_bar (f))
 20817             ignore_mouse_drag_p = true;
 20818 
 20819 #ifdef HAVE_EXT_TOOL_BAR
 20820           if (FRAME_EXTERNAL_TOOL_BAR (f))
 20821             update_frame_tool_bar (f);
 20822 #else
 20823           if (WINDOWP (f->tool_bar_window)
 20824               && (FRAME_TOOL_BAR_LINES (f) > 0
 20825                   || !NILP (Vauto_resize_tool_bars))
 20826               && redisplay_tool_bar (f))
 20827             ignore_mouse_drag_p = true;
 20828 #endif
 20829         }
 20830       else
 20831         {
 20832           if ((FRAME_TAB_BAR_LINES (f) > 0))
 20833             display_tab_bar (w);
 20834         }
 20835 
 20836       gui_consider_frame_title (w->frame);
 20837 #else
 20838       if ((FRAME_TAB_BAR_LINES (f) > 0))
 20839         display_tab_bar (w);
 20840 #endif
 20841     }
 20842 
 20843 #ifdef HAVE_WINDOW_SYSTEM
 20844   if (FRAME_WINDOW_P (f)
 20845       && update_window_fringes (w, (just_this_one_p
 20846                                     || (!used_current_matrix_p && !overlay_arrow_seen)
 20847                                     || w->pseudo_window_p)))
 20848     {
 20849       update_begin (f);
 20850       block_input ();
 20851       if (draw_window_fringes (w, true))
 20852         {
 20853           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 20854             gui_draw_right_divider (w);
 20855           else
 20856             gui_draw_vertical_border (w);
 20857         }
 20858       unblock_input ();
 20859       update_end (f);
 20860     }
 20861 
 20862   if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 20863     gui_draw_bottom_divider (w);
 20864 #endif /* HAVE_WINDOW_SYSTEM */
 20865 
 20866   /* We go to this label, with fonts_changed set, if it is
 20867      necessary to try again using larger glyph matrices.
 20868      We have to redeem the scroll bar even in this case,
 20869      because the loop in redisplay_internal expects that.  */
 20870  need_larger_matrices:
 20871   ;
 20872  finish_scroll_bars:
 20873 
 20874    if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20875     {
 20876       if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
 20877         /* Set the thumb's position and size.  */
 20878         set_vertical_scroll_bar (w);
 20879 
 20880       if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20881         /* Set the thumb's position and size.  */
 20882         set_horizontal_scroll_bar (w);
 20883 
 20884       /* Note that we actually used the scroll bar attached to this
 20885          window, so it shouldn't be deleted at the end of redisplay.  */
 20886       if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
 20887         (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
 20888     }
 20889 
 20890   /* Restore current_buffer and value of point in it.  The window
 20891      update may have changed the buffer, so first make sure `opoint'
 20892      is still valid (Bug#6177).  */
 20893   if (CHARPOS (opoint) < BEGV)
 20894     TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
 20895   else if (CHARPOS (opoint) > ZV)
 20896     TEMP_SET_PT_BOTH (Z, Z_BYTE);
 20897   else
 20898     TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
 20899 
 20900   set_buffer_internal_1 (old);
 20901   /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
 20902      shorter.  This can be caused by log truncation in *Messages*.  */
 20903   if (CHARPOS (lpoint) <= ZV)
 20904     TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 20905 
 20906   unbind_to (count, Qnil);
 20907 }
 20908 
 20909 
 20910 /* Build the complete desired matrix of WINDOW with a window start
 20911    buffer position POS.
 20912 
 20913    Value is 1 if successful.  It is zero if fonts were loaded during
 20914    redisplay or the dimensions of the desired matrix were found
 20915    insufficient, which makes re-adjusting glyph matrices necessary.
 20916    Value is -1 if point would appear in the scroll margins.  (We check
 20917    the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is unset in
 20918    FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is set in
 20919    FLAGS.)
 20920 
 20921    Note that 'x-show-tip' invokes this function in a special way, and
 20922    in that case the return value of zero doesn't necessarily mean the
 20923    glyph matrices need to be re-adjusted, if the entire text of the
 20924    tooltip was processed and has its glyphs in the matrix's glyph
 20925    rows, i.e. if the dimensions of the matrix were found insufficient
 20926    while producing empty glyph rows beyond ZV.  */
 20927 
 20928 int
 20929 try_window (Lisp_Object window, struct text_pos pos, int flags)
 20930 {
 20931   struct window *w = XWINDOW (window);
 20932   struct it it;
 20933   struct glyph_row *last_text_row = NULL;
 20934   struct frame *f = XFRAME (w->frame);
 20935   int cursor_vpos = w->cursor.vpos;
 20936 
 20937   /* Make POS the new window start.  */
 20938   set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
 20939 
 20940   /* Mark cursor position as unknown.  No overlay arrow seen.  */
 20941   w->cursor.vpos = -1;
 20942   overlay_arrow_seen = false;
 20943 
 20944   /* Initialize iterator and info to start at POS.  */
 20945   start_display (&it, w, pos);
 20946   it.glyph_row->reversed_p = false;
 20947 
 20948   /* Display all lines of W.  */
 20949   while (it.current_y < it.last_visible_y)
 20950     {
 20951       int last_row_scale = it.w->nrows_scale_factor;
 20952       int last_col_scale = it.w->ncols_scale_factor;
 20953       if (display_line (&it, cursor_vpos))
 20954         last_text_row = it.glyph_row - 1;
 20955       if (f->fonts_changed
 20956           && !((flags & TRY_WINDOW_IGNORE_FONTS_CHANGE)
 20957                /* If the matrix dimensions are insufficient, we _must_
 20958                   fail and let dispnew.c reallocate the matrix.  */
 20959                && last_row_scale == it.w->nrows_scale_factor
 20960                && last_col_scale == it.w->ncols_scale_factor))
 20961         return 0;
 20962     }
 20963 
 20964   /* Save the character position of 'it' before we call
 20965      'start_display' again.  */
 20966   ptrdiff_t it_charpos = IT_CHARPOS (it);
 20967 
 20968   /* Don't let the cursor end in the scroll margins.  However, when
 20969      the window is vscrolled, we leave it to vscroll to handle the
 20970      margins, see window_scroll_pixel_based.  */
 20971   if ((flags & TRY_WINDOW_CHECK_MARGINS)
 20972       && w->vscroll == 0
 20973       && !MINI_WINDOW_P (w))
 20974     {
 20975       int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 20976       int bot_scroll_margin = top_scroll_margin;
 20977       if (window_wants_header_line (w))
 20978         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 20979       if (window_wants_tab_line (w))
 20980         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 20981       start_display (&it, w, pos);
 20982 
 20983       if ((w->cursor.y >= 0
 20984            && w->cursor.y < top_scroll_margin
 20985            && CHARPOS (pos) > BEGV)
 20986           /* rms: considering make_cursor_line_fully_visible_p here
 20987              seems to give wrong results.  We don't want to recenter
 20988              when the last line is partly visible, we want to allow
 20989              that case to be handled in the usual way.  */
 20990           || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
 20991                             - bot_scroll_margin - 1))
 20992         {
 20993           w->cursor.vpos = -1;
 20994           clear_glyph_matrix (w->desired_matrix);
 20995           return -1;
 20996         }
 20997     }
 20998 
 20999   /* If bottom moved off end of frame, change mode line percentage.  */
 21000   if (w->window_end_pos <= 0 && Z != it_charpos)
 21001     w->update_mode_line = true;
 21002 
 21003   /* Set window_end_pos to the offset of the last character displayed
 21004      on the window from the end of current_buffer.  Set
 21005      window_end_vpos to its row number.  */
 21006   if (last_text_row)
 21007     {
 21008       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
 21009       adjust_window_ends (w, last_text_row, false);
 21010       eassert
 21011         (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
 21012                                                  w->window_end_vpos)));
 21013     }
 21014   else
 21015     {
 21016       w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21017       w->window_end_pos = Z - ZV;
 21018       w->window_end_vpos = 0;
 21019     }
 21020 
 21021   /* But that is not valid info until redisplay finishes.  */
 21022   w->window_end_valid = false;
 21023   return 1;
 21024 }
 21025 
 21026 
 21027 
 21028 /************************************************************************
 21029     Window redisplay reusing current matrix when buffer has not changed
 21030  ************************************************************************/
 21031 
 21032 /* Try redisplay of window W showing an unchanged buffer with a
 21033    different window start than the last time it was displayed by
 21034    reusing its current matrix.  Value is true if successful.
 21035    W->start is the new window start.  */
 21036 
 21037 static bool
 21038 try_window_reusing_current_matrix (struct window *w)
 21039 {
 21040   struct frame *f = XFRAME (w->frame);
 21041   struct glyph_row *bottom_row;
 21042   struct it it;
 21043   struct run run;
 21044   struct text_pos start, new_start;
 21045   int nrows_scrolled, i;
 21046   struct glyph_row *last_text_row;
 21047   struct glyph_row *last_reused_text_row;
 21048   struct glyph_row *start_row;
 21049   int start_vpos, min_y, max_y;
 21050 
 21051 #ifdef GLYPH_DEBUG
 21052   if (inhibit_try_window_reusing)
 21053     return false;
 21054 #endif
 21055 
 21056   if (/* This function doesn't handle terminal frames.  */
 21057       !FRAME_WINDOW_P (f)
 21058       /* Don't try to reuse the display if windows have been split
 21059          or such.  */
 21060       || windows_or_buffers_changed
 21061       || f->cursor_type_changed
 21062       /* This function cannot handle buffers where the overlay arrow
 21063          is shown on the fringes, because if the arrow position
 21064          changes, we cannot just reuse the current matrix.  */
 21065       || overlay_arrow_in_current_buffer_p ())
 21066     return false;
 21067 
 21068   /* Can't do this if showing trailing whitespace.  */
 21069   if (!NILP (Vshow_trailing_whitespace))
 21070     return false;
 21071 
 21072   /* If top-line visibility has changed, give up.  */
 21073   if (window_wants_tab_line (w)
 21074       != MATRIX_TAB_LINE_ROW (w->current_matrix)->mode_line_p)
 21075     return false;
 21076 
 21077   /* If top-line visibility has changed, give up.  */
 21078   if (window_wants_header_line (w)
 21079       != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
 21080     return false;
 21081 
 21082   /* Give up if old or new display is scrolled vertically.  We could
 21083      make this function handle this, but right now it doesn't.  */
 21084   start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21085   if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
 21086     return false;
 21087 
 21088   /* Clear the desired matrix for the display below.  */
 21089   clear_glyph_matrix (w->desired_matrix);
 21090 
 21091   /* Give up if line numbers are being displayed, because reusing the
 21092      current matrix might use the wrong width for line-number
 21093      display.  */
 21094   if (!NILP (Vdisplay_line_numbers))
 21095     return false;
 21096 
 21097   /* Can't scroll the display of w32 GUI frames when position of point
 21098      is indicated by the system caret, because scrolling the display
 21099      will then "copy" the pixels used by the caret.  */
 21100 #ifdef HAVE_NTGUI
 21101   if (w32_use_visible_system_caret)
 21102     return false;
 21103 #endif
 21104 
 21105   /* The variable new_start now holds the new window start.  The old
 21106      start `start' can be determined from the current matrix.  */
 21107   SET_TEXT_POS_FROM_MARKER (new_start, w->start);
 21108   start = start_row->minpos;
 21109   start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 21110 
 21111   if (CHARPOS (new_start) <= CHARPOS (start))
 21112     {
 21113       /* Don't use this method if the display starts with an ellipsis
 21114          displayed for invisible text.  It's not easy to handle that case
 21115          below, and it's certainly not worth the effort since this is
 21116          not a frequent case.  */
 21117       if (in_ellipses_for_invisible_text_p (&start_row->start, w))
 21118         return false;
 21119 
 21120       IF_DEBUG (debug_method_add (w, "twu1"));
 21121 
 21122       /* Display up to a row that can be reused.  The variable
 21123          last_text_row is set to the last row displayed that displays
 21124          text.  Note that it.vpos == 0 if or if not there is a
 21125          header-line; it's not the same as the MATRIX_ROW_VPOS!  */
 21126       start_display (&it, w, new_start);
 21127       w->cursor.vpos = -1;
 21128       last_text_row = last_reused_text_row = NULL;
 21129 
 21130       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21131         {
 21132           /* If we have reached into the characters in the START row,
 21133              that means the line boundaries have changed.  So we
 21134              can't start copying with the row START.  Maybe it will
 21135              work to start copying with the following row.  */
 21136           while (IT_CHARPOS (it) > CHARPOS (start))
 21137             {
 21138               /* Advance to the next row as the "start".  */
 21139               start_row++;
 21140               start = start_row->minpos;
 21141               /* If there are no more rows to try, or just one, give up.  */
 21142               if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
 21143                   || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
 21144                   || CHARPOS (start) == ZV)
 21145                 {
 21146                   clear_glyph_matrix (w->desired_matrix);
 21147                   return false;
 21148                 }
 21149 
 21150               start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 21151             }
 21152           /* If we have reached alignment, we can copy the rest of the
 21153              rows.  */
 21154           if (IT_CHARPOS (it) == CHARPOS (start)
 21155               /* Don't accept "alignment" inside a display vector,
 21156                  since start_row could have started in the middle of
 21157                  that same display vector (thus their character
 21158                  positions match), and we have no way of telling if
 21159                  that is the case.  */
 21160               && it.current.dpvec_index < 0)
 21161             break;
 21162 
 21163           it.glyph_row->reversed_p = false;
 21164           if (display_line (&it, -1))
 21165             last_text_row = it.glyph_row - 1;
 21166 
 21167         }
 21168 
 21169       /* A value of current_y < last_visible_y means that we stopped
 21170          at the previous window start, which in turn means that we
 21171          have at least one reusable row.  */
 21172       if (it.current_y < it.last_visible_y)
 21173         {
 21174           struct glyph_row *row;
 21175 
 21176           /* IT.vpos always starts from 0; it counts text lines.  */
 21177           nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
 21178 
 21179           /* Find PT if not already found in the lines displayed.  */
 21180           if (w->cursor.vpos < 0)
 21181             {
 21182               int dy = it.current_y - start_row->y;
 21183 
 21184               row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21185               row = row_containing_pos (w, PT, row, NULL, dy);
 21186               if (row)
 21187                 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
 21188                                      dy, nrows_scrolled);
 21189               else
 21190                 {
 21191                   clear_glyph_matrix (w->desired_matrix);
 21192                   return false;
 21193                 }
 21194             }
 21195 
 21196           /* Scroll the display.  Do it before the current matrix is
 21197              changed.  The problem here is that update has not yet
 21198              run, i.e. part of the current matrix is not up to date.
 21199              scroll_run_hook will clear the cursor, and use the
 21200              current matrix to get the height of the row the cursor is
 21201              in.  */
 21202           run.current_y = start_row->y;
 21203           run.desired_y = it.current_y;
 21204           run.height = it.last_visible_y - it.current_y;
 21205 
 21206           if (run.height > 0 && run.current_y != run.desired_y)
 21207             {
 21208 #ifdef HAVE_WINDOW_SYSTEM
 21209               update_begin (f);
 21210               gui_update_window_begin (w);
 21211               FRAME_RIF (f)->clear_window_mouse_face (w);
 21212               FRAME_RIF (f)->scroll_run_hook (w, &run);
 21213               gui_update_window_end (w, false, false);
 21214               update_end (f);
 21215 #endif
 21216             }
 21217 
 21218           /* Shift current matrix down by nrows_scrolled lines.  */
 21219           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21220           rotate_matrix (w->current_matrix,
 21221                          start_vpos,
 21222                          MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21223                          nrows_scrolled);
 21224 
 21225           /* Disable lines that must be updated.  */
 21226           for (i = 0; i < nrows_scrolled; ++i)
 21227             (start_row + i)->enabled_p = false;
 21228 
 21229           /* Re-compute Y positions.  */
 21230           min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21231           max_y = it.last_visible_y;
 21232           for (row = start_row + nrows_scrolled;
 21233                row < bottom_row;
 21234                ++row)
 21235             {
 21236               row->y = it.current_y;
 21237               row->visible_height = row->height;
 21238 
 21239               if (row->y < min_y)
 21240                 row->visible_height -= min_y - row->y;
 21241               if (row->y + row->height > max_y)
 21242                 row->visible_height -= row->y + row->height - max_y;
 21243               if (row->fringe_bitmap_periodic_p)
 21244                 row->redraw_fringe_bitmaps_p = true;
 21245 
 21246               it.current_y += row->height;
 21247 
 21248               if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21249                 last_reused_text_row = row;
 21250               if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
 21251                 break;
 21252             }
 21253 
 21254           /* Disable lines in the current matrix which are now
 21255              below the window.  */
 21256           for (++row; row < bottom_row; ++row)
 21257             row->enabled_p = row->mode_line_p = row->tab_line_p = false;
 21258         }
 21259 
 21260       /* Update window_end_pos etc.; last_reused_text_row is the last
 21261          reused row from the current matrix containing text, if any.
 21262          The value of last_text_row is the last displayed line
 21263          containing text.  */
 21264       if (last_reused_text_row)
 21265         adjust_window_ends (w, last_reused_text_row, true);
 21266       else if (last_text_row)
 21267         adjust_window_ends (w, last_text_row, false);
 21268       else
 21269         {
 21270           /* This window must be completely empty.  */
 21271           w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21272           w->window_end_pos = Z - ZV;
 21273           w->window_end_vpos = 0;
 21274         }
 21275       w->window_end_valid = false;
 21276 
 21277       /* Update hint: don't try scrolling again in update_window.  */
 21278       w->desired_matrix->no_scrolling_p = true;
 21279 
 21280 #ifdef GLYPH_DEBUG
 21281       debug_method_add (w, "try_window_reusing_current_matrix 1");
 21282 #endif
 21283       return true;
 21284     }
 21285   else if (CHARPOS (new_start) > CHARPOS (start))
 21286     {
 21287       struct glyph_row *pt_row, *row;
 21288       struct glyph_row *first_reusable_row;
 21289       struct glyph_row *first_row_to_display;
 21290       int dy;
 21291       int yb = window_text_bottom_y (w);
 21292 
 21293       /* Find the row starting at new_start, if there is one.  Don't
 21294          reuse a partially visible line at the end.  */
 21295       first_reusable_row = start_row;
 21296       while (first_reusable_row->enabled_p
 21297              && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
 21298              && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21299                  < CHARPOS (new_start)))
 21300         ++first_reusable_row;
 21301 
 21302       /* Give up if there is no row to reuse.  */
 21303       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
 21304           || !first_reusable_row->enabled_p
 21305           || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21306               != CHARPOS (new_start)))
 21307         return false;
 21308 
 21309       /* We can reuse fully visible rows beginning with
 21310          first_reusable_row to the end of the window.  Set
 21311          first_row_to_display to the first row that cannot be reused.
 21312          Set pt_row to the row containing point, if there is any.  */
 21313       pt_row = NULL;
 21314       for (first_row_to_display = first_reusable_row;
 21315            MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
 21316            ++first_row_to_display)
 21317         {
 21318           if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
 21319               && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21320                   || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21321                       && first_row_to_display->ends_at_zv_p
 21322                       && pt_row == NULL)))
 21323             pt_row = first_row_to_display;
 21324         }
 21325 
 21326       if (first_row_to_display->y >= yb)
 21327         return false;
 21328 
 21329       /* Start displaying at the start of first_row_to_display.  */
 21330       init_to_row_start (&it, w, first_row_to_display);
 21331 
 21332       nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
 21333                         - start_vpos);
 21334       it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
 21335                  - nrows_scrolled);
 21336       it.current_y = (first_row_to_display->y - first_reusable_row->y
 21337                       + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 21338 
 21339       /* Display lines beginning with first_row_to_display in the
 21340          desired matrix.  Set last_text_row to the last row displayed
 21341          that displays text.  */
 21342       it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
 21343       if (pt_row == NULL)
 21344         w->cursor.vpos = -1;
 21345       last_text_row = NULL;
 21346       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21347         if (display_line (&it, w->cursor.vpos))
 21348           last_text_row = it.glyph_row - 1;
 21349 
 21350       /* If point is in a reused row, adjust y and vpos of the cursor
 21351          position.  */
 21352       if (pt_row)
 21353         {
 21354           w->cursor.vpos -= nrows_scrolled;
 21355           w->cursor.y -= first_reusable_row->y - start_row->y;
 21356         }
 21357 
 21358       /* Give up if point isn't in a row displayed or reused.  (This
 21359          also handles the case where w->cursor.vpos < nrows_scrolled
 21360          after the calls to display_line, which can happen with scroll
 21361          margins.  See bug#1295.)  */
 21362       if (w->cursor.vpos < 0)
 21363         {
 21364           clear_glyph_matrix (w->desired_matrix);
 21365           return false;
 21366         }
 21367 
 21368       /* Scroll the display.  */
 21369       run.current_y = first_reusable_row->y;
 21370       run.desired_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21371       run.height = it.last_visible_y - run.current_y;
 21372       dy = run.current_y - run.desired_y;
 21373 
 21374       if (run.height)
 21375         {
 21376 #ifdef HAVE_WINDOW_SYSTEM
 21377           update_begin (f);
 21378           gui_update_window_begin (w);
 21379           FRAME_RIF (f)->clear_window_mouse_face (w);
 21380           FRAME_RIF (f)->scroll_run_hook (w, &run);
 21381           gui_update_window_end (w, false, false);
 21382           update_end (f);
 21383 #endif
 21384         }
 21385 
 21386       /* Adjust Y positions of reused rows.  */
 21387       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21388       min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21389       max_y = it.last_visible_y;
 21390       for (row = first_reusable_row; row < first_row_to_display; ++row)
 21391         {
 21392           row->y -= dy;
 21393           row->visible_height = row->height;
 21394           if (row->y < min_y)
 21395             row->visible_height -= min_y - row->y;
 21396           if (row->y + row->height > max_y)
 21397             row->visible_height -= row->y + row->height - max_y;
 21398           if (row->fringe_bitmap_periodic_p)
 21399             row->redraw_fringe_bitmaps_p = true;
 21400         }
 21401 
 21402       /* Scroll the current matrix.  */
 21403       eassert (nrows_scrolled > 0);
 21404       rotate_matrix (w->current_matrix,
 21405                      start_vpos,
 21406                      MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21407                      -nrows_scrolled);
 21408 
 21409       /* Disable rows not reused.  */
 21410       for (row -= nrows_scrolled; row < bottom_row; ++row)
 21411         row->enabled_p = false;
 21412 
 21413       /* Point may have moved to a different line, so we cannot assume that
 21414          the previous cursor position is valid; locate the correct row.  */
 21415       if (pt_row)
 21416         {
 21417           for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 21418                row < bottom_row
 21419                  && PT >= MATRIX_ROW_END_CHARPOS (row)
 21420                  && !row->ends_at_zv_p;
 21421                row++)
 21422             {
 21423               w->cursor.vpos++;
 21424               w->cursor.y = row->y;
 21425             }
 21426           if (row < bottom_row)
 21427             {
 21428               /* Can't simply scan the row for point with
 21429                  bidi-reordered glyph rows.  Let set_cursor_from_row
 21430                  figure out where to put the cursor, and if it fails,
 21431                  give up.  */
 21432               if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 21433                 {
 21434                   if (!set_cursor_from_row (w, row, w->current_matrix,
 21435                                             0, 0, 0, 0))
 21436                     {
 21437                       clear_glyph_matrix (w->desired_matrix);
 21438                       return false;
 21439                     }
 21440                 }
 21441               else
 21442                 {
 21443                   struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
 21444                   struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21445 
 21446                   for (; glyph < end
 21447                          && (!BUFFERP (glyph->object)
 21448                              || glyph->charpos < PT);
 21449                        glyph++)
 21450                     {
 21451                       w->cursor.hpos++;
 21452                       w->cursor.x += glyph->pixel_width;
 21453                     }
 21454                 }
 21455             }
 21456         }
 21457 
 21458       /* Adjust window end.  A null value of last_text_row means that
 21459          the window end is in reused rows which in turn means that
 21460          only its vpos can have changed.  */
 21461       if (last_text_row)
 21462         adjust_window_ends (w, last_text_row, false);
 21463       else
 21464         w->window_end_vpos -= nrows_scrolled;
 21465 
 21466       w->window_end_valid = false;
 21467       w->desired_matrix->no_scrolling_p = true;
 21468 
 21469 #ifdef GLYPH_DEBUG
 21470       debug_method_add (w, "try_window_reusing_current_matrix 2");
 21471 #endif
 21472       return true;
 21473     }
 21474 
 21475   return false;
 21476 }
 21477 
 21478 
 21479 
 21480 /************************************************************************
 21481    Window redisplay reusing current matrix when buffer has changed
 21482  ************************************************************************/
 21483 
 21484 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
 21485 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 21486                                                           ptrdiff_t *, ptrdiff_t *);
 21487 static struct glyph_row *
 21488 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
 21489                                struct glyph_row *);
 21490 
 21491 
 21492 /* Return the last row in MATRIX displaying text.  If row START is
 21493    non-null, start searching with that row.  IT gives the dimensions
 21494    of the display.  Value is null if matrix is empty; otherwise it is
 21495    a pointer to the row found.  */
 21496 
 21497 static struct glyph_row *
 21498 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
 21499                                struct glyph_row *start)
 21500 {
 21501   struct glyph_row *row, *row_found;
 21502 
 21503   /* Set row_found to the last row in IT->w's current matrix
 21504      displaying text.  The loop looks funny but think of partially
 21505      visible lines.  */
 21506   row_found = NULL;
 21507   row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
 21508   while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21509     {
 21510       eassert (row->enabled_p);
 21511       row_found = row;
 21512       if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
 21513         break;
 21514       ++row;
 21515     }
 21516 
 21517   return row_found;
 21518 }
 21519 
 21520 
 21521 /* Return the last row in the current matrix of W that is not affected
 21522    by changes at the start of current_buffer that occurred since W's
 21523    current matrix was built.  Value is null if no such row exists.
 21524 
 21525    BEG_UNCHANGED is the number of characters unchanged at the start of
 21526    current_buffer.  BEG + BEG_UNCHANGED is the buffer position of the
 21527    first changed character in current_buffer.  Characters at positions <
 21528    BEG + BEG_UNCHANGED are at the same buffer positions as they were
 21529    when the current matrix was built.  */
 21530 
 21531 static struct glyph_row *
 21532 find_last_unchanged_at_beg_row (struct window *w)
 21533 {
 21534   ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
 21535   struct glyph_row *row;
 21536   struct glyph_row *row_found = NULL;
 21537   int yb = window_text_bottom_y (w);
 21538 
 21539   /* Find the last row displaying unchanged text.  */
 21540   for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21541        MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21542          && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
 21543        ++row)
 21544     {
 21545       if (/* If row ends before first_changed_pos, it is unchanged,
 21546              except in some case.  */
 21547           MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
 21548           /* When row ends in ZV and we write at ZV it is not
 21549              unchanged.  */
 21550           && !row->ends_at_zv_p
 21551           /* When first_changed_pos is the end of a continued line,
 21552              row is not unchanged because it may be no longer
 21553              continued.  */
 21554           && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
 21555                && (row->continued_p
 21556                    || row->exact_window_width_line_p))
 21557           /* If ROW->end is beyond ZV, then ROW->end is outdated and
 21558              needs to be recomputed, so don't consider this row as
 21559              unchanged.  This happens when the last line was
 21560              bidi-reordered and was killed immediately before this
 21561              redisplay cycle.  In that case, ROW->end stores the
 21562              buffer position of the first visual-order character of
 21563              the killed text, which is now beyond ZV.  */
 21564           && CHARPOS (row->end.pos) <= ZV)
 21565         row_found = row;
 21566 
 21567       /* Stop if last visible row.  */
 21568       if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
 21569         break;
 21570     }
 21571 
 21572   return row_found;
 21573 }
 21574 
 21575 
 21576 /* Find the first glyph row in the current matrix of W that is not
 21577    affected by changes at the end of current_buffer since the
 21578    time W's current matrix was built.
 21579 
 21580    Return in *DELTA the number of chars by which buffer positions in
 21581    unchanged text at the end of current_buffer must be adjusted.
 21582 
 21583    Return in *DELTA_BYTES the corresponding number of bytes.
 21584 
 21585    Value is null if no such row exists, i.e. all rows are affected by
 21586    changes.  */
 21587 
 21588 static struct glyph_row *
 21589 find_first_unchanged_at_end_row (struct window *w,
 21590                                  ptrdiff_t *delta, ptrdiff_t *delta_bytes)
 21591 {
 21592   struct glyph_row *row;
 21593   struct glyph_row *row_found = NULL;
 21594 
 21595   *delta = *delta_bytes = 0;
 21596 
 21597   /* Display must not have been paused, otherwise the current matrix
 21598      is not up to date.  */
 21599   eassert (w->window_end_valid);
 21600 
 21601   /* A value of window_end_pos >= END_UNCHANGED means that the window
 21602      end is in the range of changed text.  If so, there is no
 21603      unchanged row at the end of W's current matrix.  */
 21604   if (w->window_end_pos >= END_UNCHANGED)
 21605     return NULL;
 21606 
 21607   /* Set row to the last row in W's current matrix displaying text.  */
 21608   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21609 
 21610   /* If matrix is entirely empty, no unchanged row exists.  */
 21611   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21612     {
 21613       /* The value of row is the last glyph row in the matrix having a
 21614          meaningful buffer position in it.  The end position of row
 21615          corresponds to window_end_pos.  This allows us to translate
 21616          buffer positions in the current matrix to current buffer
 21617          positions for characters not in changed text.  */
 21618       ptrdiff_t Z_old =
 21619         MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21620       ptrdiff_t Z_BYTE_old =
 21621         MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21622       ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
 21623       struct glyph_row *first_text_row
 21624         = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21625 
 21626       *delta = Z - Z_old;
 21627       *delta_bytes = Z_BYTE - Z_BYTE_old;
 21628 
 21629       /* Set last_unchanged_pos to the buffer position of the last
 21630          character in the buffer that has not been changed.  Z is the
 21631          index + 1 of the last character in current_buffer, i.e. by
 21632          subtracting END_UNCHANGED we get the index of the last
 21633          unchanged character, and we have to add BEG to get its buffer
 21634          position.  */
 21635       last_unchanged_pos = Z - END_UNCHANGED + BEG;
 21636       last_unchanged_pos_old = last_unchanged_pos - *delta;
 21637 
 21638       /* Search backward from ROW for a row displaying a line that
 21639          starts at a minimum position >= last_unchanged_pos_old.  */
 21640       for (; row > first_text_row; --row)
 21641         {
 21642           /* This used to abort, but it can happen.
 21643              It is ok to just stop the search instead here.  KFS.  */
 21644           if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21645             break;
 21646 
 21647           if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
 21648             row_found = row;
 21649         }
 21650     }
 21651 
 21652   eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
 21653 
 21654   return row_found;
 21655 }
 21656 
 21657 
 21658 /* Make sure that glyph rows in the current matrix of window W
 21659    reference the same glyph memory as corresponding rows in the
 21660    frame's frame matrix.  This function is called after scrolling W's
 21661    current matrix on a terminal frame in try_window_id and
 21662    try_window_reusing_current_matrix.  */
 21663 
 21664 static void
 21665 sync_frame_with_window_matrix_rows (struct window *w)
 21666 {
 21667   struct frame *f = XFRAME (w->frame);
 21668   struct glyph_row *window_row, *window_row_end, *frame_row;
 21669 
 21670   /* Preconditions: W must be a leaf window and full-width.  Its frame
 21671      must have a frame matrix.  */
 21672   eassert (BUFFERP (w->contents));
 21673   eassert (WINDOW_FULL_WIDTH_P (w));
 21674   eassert (!FRAME_WINDOW_P (f));
 21675 
 21676   /* If W is a full-width window, glyph pointers in W's current matrix
 21677      have, by definition, to be the same as glyph pointers in the
 21678      corresponding frame matrix.  Note that frame matrices have no
 21679      marginal areas (see build_frame_matrix).  */
 21680   window_row = w->current_matrix->rows;
 21681   window_row_end = window_row + w->current_matrix->nrows;
 21682   frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 21683   while (window_row < window_row_end)
 21684     {
 21685       struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
 21686       struct glyph *end = window_row->glyphs[LAST_AREA];
 21687 
 21688       frame_row->glyphs[LEFT_MARGIN_AREA] = start;
 21689       frame_row->glyphs[TEXT_AREA] = start;
 21690       frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
 21691       frame_row->glyphs[LAST_AREA] = end;
 21692 
 21693       /* Disable frame rows whose corresponding window rows have
 21694          been disabled in try_window_id.  */
 21695       if (!window_row->enabled_p)
 21696         frame_row->enabled_p = false;
 21697 
 21698       ++window_row, ++frame_row;
 21699     }
 21700 }
 21701 
 21702 
 21703 /* Find the glyph row in window W containing CHARPOS.  Consider all
 21704    rows between START and END (not inclusive).  END null means search
 21705    all rows to the end of the display area of W.  Value is the row
 21706    containing CHARPOS or null.  */
 21707 
 21708 struct glyph_row *
 21709 row_containing_pos (struct window *w, ptrdiff_t charpos,
 21710                     struct glyph_row *start, struct glyph_row *end, int dy)
 21711 {
 21712   struct glyph_row *row = start;
 21713   struct glyph_row *best_row = NULL;
 21714   ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
 21715   int last_y;
 21716 
 21717   /* If we happen to start on a header-line or a tab-line, skip that.  */
 21718   if (row->tab_line_p)
 21719     ++row;
 21720   if (row->mode_line_p)
 21721     ++row;
 21722 
 21723   if ((end && row >= end) || !row->enabled_p)
 21724     return NULL;
 21725 
 21726   last_y = window_text_bottom_y (w) - dy;
 21727 
 21728   while (true)
 21729     {
 21730       /* Give up if we have gone too far.  */
 21731       if ((end && row >= end) || !row->enabled_p)
 21732         return NULL;
 21733       /* This formerly returned if they were equal.
 21734          I think that both quantities are of a "last plus one" type;
 21735          if so, when they are equal, the row is within the screen. -- rms.  */
 21736       if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
 21737         return NULL;
 21738 
 21739       /* If it is in this row, return this row.  */
 21740       if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
 21741              || (MATRIX_ROW_END_CHARPOS (row) == charpos
 21742                  /* The end position of a row equals the start
 21743                     position of the next row.  If CHARPOS is there, we
 21744                     would rather consider it displayed in the next
 21745                     line, except when this line ends in ZV.  */
 21746                  && !row_for_charpos_p (row, charpos)))
 21747           && charpos >= MATRIX_ROW_START_CHARPOS (row))
 21748         {
 21749           struct glyph *g;
 21750 
 21751           if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21752               || (!best_row && !row->continued_p))
 21753             return row;
 21754           /* In bidi-reordered rows, there could be several rows whose
 21755              edges surround CHARPOS, all of these rows belonging to
 21756              the same continued line.  We need to find the row which
 21757              fits CHARPOS the best.  */
 21758           for (g = row->glyphs[TEXT_AREA];
 21759                g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21760                g++)
 21761             {
 21762               if (!STRINGP (g->object))
 21763                 {
 21764                   if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
 21765                     {
 21766                       mindif = eabs (g->charpos - charpos);
 21767                       best_row = row;
 21768                       /* Exact match always wins.  */
 21769                       if (mindif == 0)
 21770                         return best_row;
 21771                     }
 21772                 }
 21773             }
 21774         }
 21775       else if (best_row && !row->continued_p)
 21776         return best_row;
 21777       ++row;
 21778     }
 21779 }
 21780 
 21781 
 21782 /* Try to redisplay window W by reusing its existing display.  W's
 21783    current matrix must be up to date when this function is called,
 21784    i.e., window_end_valid must be true.
 21785 
 21786    Value is
 21787 
 21788    >= 1 if successful, i.e. display has been updated
 21789          specifically:
 21790          1 means the changes were in front of a newline that precedes
 21791            the window start, and the whole current matrix was reused
 21792          2 means the changes were after the last position displayed
 21793            in the window, and the whole current matrix was reused
 21794          3 means portions of the current matrix were reused, while
 21795            some of the screen lines were redrawn
 21796    -1   if redisplay with same window start is known not to succeed
 21797    0    if otherwise unsuccessful
 21798 
 21799    The following steps are performed:
 21800 
 21801    1. Find the last row in the current matrix of W that is not
 21802    affected by changes at the start of current_buffer.  If no such row
 21803    is found, give up.
 21804 
 21805    2. Find the first row in W's current matrix that is not affected by
 21806    changes at the end of current_buffer.  Maybe there is no such row.
 21807 
 21808    3. Display lines beginning with the row + 1 found in step 1 to the
 21809    row found in step 2 or, if step 2 didn't find a row, to the end of
 21810    the window.
 21811 
 21812    4. If cursor is not known to appear on the window, give up.
 21813 
 21814    5. If display stopped at the row found in step 2, scroll the
 21815    display and current matrix as needed.
 21816 
 21817    6. Maybe display some lines at the end of W, if we must.  This can
 21818    happen under various circumstances, like a partially visible line
 21819    becoming fully visible, or because newly displayed lines are displayed
 21820    in smaller font sizes.
 21821 
 21822    7. Update W's window end information.  */
 21823 
 21824 static int
 21825 try_window_id (struct window *w)
 21826 {
 21827   struct frame *f = XFRAME (w->frame);
 21828   struct glyph_matrix *current_matrix = w->current_matrix;
 21829   struct glyph_matrix *desired_matrix = w->desired_matrix;
 21830   struct glyph_row *last_unchanged_at_beg_row;
 21831   struct glyph_row *first_unchanged_at_end_row;
 21832   struct glyph_row *row;
 21833   struct glyph_row *bottom_row;
 21834   int bottom_vpos;
 21835   struct it it;
 21836   ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
 21837   int dvpos, dy;
 21838   struct text_pos start_pos;
 21839   struct run run;
 21840   int first_unchanged_at_end_vpos = 0;
 21841   struct glyph_row *last_text_row, *last_text_row_at_end;
 21842   struct text_pos start;
 21843   ptrdiff_t first_changed_charpos, last_changed_charpos;
 21844 
 21845 #ifdef GLYPH_DEBUG
 21846   if (inhibit_try_window_id)
 21847     return 0;
 21848 #endif
 21849 
 21850   /* This is handy for debugging.  */
 21851 #if false
 21852 #define GIVE_UP(X)                                              \
 21853   do {                                                          \
 21854     redisplay_trace ("try_window_id give up %d\n", X);          \
 21855     return 0;                                                   \
 21856   } while (false)
 21857 #else
 21858 #define GIVE_UP(X) return 0
 21859 #endif
 21860 
 21861   SET_TEXT_POS_FROM_MARKER (start, w->start);
 21862 
 21863   /* Don't use this for mini-windows because these can show
 21864      messages and mini-buffers, and we don't handle that here.  */
 21865   if (MINI_WINDOW_P (w))
 21866     GIVE_UP (1);
 21867 
 21868   /* This flag is used to prevent redisplay optimizations.  */
 21869   if (windows_or_buffers_changed || f->cursor_type_changed)
 21870     GIVE_UP (2);
 21871 
 21872   /* This function's optimizations cannot be used if overlays have
 21873      changed in the buffer displayed by the window, so give up if they
 21874      have.  */
 21875   if (w->last_overlay_modified != OVERLAY_MODIFF)
 21876     GIVE_UP (200);
 21877 
 21878   /* Verify that narrowing has not changed.
 21879      Also verify that we were not told to prevent redisplay optimizations.
 21880      It would be nice to further
 21881      reduce the number of cases where this prevents try_window_id.  */
 21882   if (current_buffer->clip_changed
 21883       || current_buffer->prevent_redisplay_optimizations_p)
 21884     GIVE_UP (3);
 21885 
 21886   /* Window must either use window-based redisplay or be full width.  */
 21887   if (!FRAME_WINDOW_P (f)
 21888       && (!FRAME_LINE_INS_DEL_OK (f)
 21889           || !WINDOW_FULL_WIDTH_P (w)))
 21890     GIVE_UP (4);
 21891 
 21892   /* Give up if point is known NOT to appear in W.  */
 21893   if (PT < CHARPOS (start))
 21894     GIVE_UP (5);
 21895 
 21896   /* Another way to prevent redisplay optimizations.  */
 21897   if (w->last_modified == 0)
 21898     GIVE_UP (6);
 21899 
 21900   /* Verify that window is not hscrolled.  */
 21901   if (w->hscroll != 0)
 21902     GIVE_UP (7);
 21903 
 21904   /* Verify that display wasn't paused.  */
 21905   if (!w->window_end_valid)
 21906     GIVE_UP (8);
 21907 
 21908   /* Likewise if highlighting trailing whitespace.  */
 21909   if (!NILP (Vshow_trailing_whitespace))
 21910     GIVE_UP (11);
 21911 
 21912   /* Can't use this if overlay arrow position and/or string have
 21913      changed.  */
 21914   if (overlay_arrows_changed_p (false))
 21915     GIVE_UP (12);
 21916 
 21917   /* When word-wrap is on, adding a space to the first word of a
 21918      wrapped line can change the wrap position, altering the line
 21919      above it.  It might be worthwhile to handle this more
 21920      intelligently, but for now just redisplay from scratch.  */
 21921   if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
 21922     GIVE_UP (21);
 21923 
 21924   /* Under bidi reordering, adding or deleting a character in the
 21925      beginning of a paragraph, before the first strong directional
 21926      character, can change the base direction of the paragraph (unless
 21927      the buffer specifies a fixed paragraph direction), which will
 21928      require redisplaying the whole paragraph.  It might be worthwhile
 21929      to find the paragraph limits and widen the range of redisplayed
 21930      lines to that, but for now just give up this optimization and
 21931      redisplay from scratch.  */
 21932   if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21933       && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 21934     GIVE_UP (22);
 21935 
 21936   /* Give up if the buffer has line-spacing set, as Lisp-level changes
 21937      to that variable require thorough redisplay.  */
 21938   if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
 21939     GIVE_UP (23);
 21940 
 21941   /* Give up if display-line-numbers is in relative mode, or when the
 21942      current line's number needs to be displayed in a distinct face.  */
 21943   if (EQ (Vdisplay_line_numbers, Qrelative)
 21944       || EQ (Vdisplay_line_numbers, Qvisual)
 21945       || (!NILP (Vdisplay_line_numbers)
 21946           && NILP (Finternal_lisp_face_equal_p (Qline_number,
 21947                                                 Qline_number_current_line,
 21948                                                 w->frame))))
 21949     GIVE_UP (24);
 21950 
 21951   /* composition-break-at-point is incompatible with the optimizations
 21952      in this function, because we need to recompose characters when
 21953      point moves off their positions.  */
 21954   if (composition_break_at_point)
 21955     GIVE_UP (27);
 21956 
 21957   /* Make sure beg_unchanged and end_unchanged are up to date.  Do it
 21958      only if buffer has really changed.  The reason is that the gap is
 21959      initially at Z for freshly visited files.  The code below would
 21960      set end_unchanged to 0 in that case.  */
 21961   if (MODIFF > SAVE_MODIFF
 21962       /* This seems to happen sometimes after saving a buffer.  */
 21963       || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
 21964     {
 21965       if (GPT - BEG < BEG_UNCHANGED)
 21966         BEG_UNCHANGED = GPT - BEG;
 21967       if (Z - GPT < END_UNCHANGED)
 21968         END_UNCHANGED = Z - GPT;
 21969     }
 21970 
 21971   /* The position of the first and last character that has been changed.  */
 21972   first_changed_charpos = BEG + BEG_UNCHANGED;
 21973   last_changed_charpos  = Z - END_UNCHANGED;
 21974 
 21975   /* If window starts after a line end, and the last change is in
 21976      front of that newline, then changes don't affect the display.
 21977      This case happens with stealth-fontification.  Note that although
 21978      the display is unchanged, glyph positions in the matrix have to
 21979      be adjusted, of course.  */
 21980   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21981   if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21982       && ((last_changed_charpos < CHARPOS (start)
 21983            && CHARPOS (start) == BEGV)
 21984           || (last_changed_charpos < CHARPOS (start) - 1
 21985               && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
 21986     {
 21987       ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 21988       struct glyph_row *r0;
 21989 
 21990       /* Compute how many chars/bytes have been added to or removed
 21991          from the buffer.  */
 21992       Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21993       Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21994       Z_delta = Z - Z_old;
 21995       Z_delta_bytes = Z_BYTE - Z_BYTE_old;
 21996 
 21997       /* Give up if PT is not in the window.  Note that it already has
 21998          been checked at the start of try_window_id that PT is not in
 21999          front of the window start.  */
 22000       if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
 22001         GIVE_UP (13);
 22002 
 22003       /* If window start is unchanged, we can reuse the whole matrix
 22004          as is, after adjusting glyph positions.  No need to compute
 22005          the window end again, since its offset from Z hasn't changed.  */
 22006       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22007       if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
 22008           && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
 22009           /* PT must not be in a partially visible line.  */
 22010           && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
 22011                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 22012         {
 22013           /* Adjust positions in the glyph matrix.  */
 22014           if (Z_delta || Z_delta_bytes)
 22015             {
 22016               struct glyph_row *r1
 22017                 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22018               increment_matrix_positions (w->current_matrix,
 22019                                           MATRIX_ROW_VPOS (r0, current_matrix),
 22020                                           MATRIX_ROW_VPOS (r1, current_matrix),
 22021                                           Z_delta, Z_delta_bytes);
 22022             }
 22023 
 22024           /* Set the cursor.  */
 22025           row = row_containing_pos (w, PT, r0, NULL, 0);
 22026           if (row)
 22027             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 22028           return 1;
 22029         }
 22030     }
 22031 
 22032   /* Handle the case that changes are all below what is displayed in
 22033      the window, and that PT is in the window.  This shortcut cannot
 22034      be taken if ZV is visible in the window, and text has been added
 22035      there that is visible in the window.  */
 22036   if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
 22037       /* ZV is not visible in the window, or there are no
 22038          changes at ZV, actually.  */
 22039       && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
 22040           || first_changed_charpos == last_changed_charpos))
 22041     {
 22042       struct glyph_row *r0;
 22043 
 22044       /* Give up if PT is not in the window.  Note that it already has
 22045          been checked at the start of try_window_id that PT is not in
 22046          front of the window start.  */
 22047       if (PT >= MATRIX_ROW_END_CHARPOS (row))
 22048         GIVE_UP (14);
 22049 
 22050       /* If window start is unchanged, we can reuse the whole matrix
 22051          as is, without changing glyph positions since no text has
 22052          been added/removed in front of the window end.  */
 22053       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22054       if (TEXT_POS_EQUAL_P (start, r0->minpos)
 22055           /* PT must not be in a partially visible line.  */
 22056           && !(PT >= MATRIX_ROW_START_CHARPOS (row)
 22057                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 22058         {
 22059           /* We have to compute the window end anew since text
 22060              could have been added/removed after it.  */
 22061           w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22062           w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22063 
 22064           /* Set the cursor.  */
 22065           row = row_containing_pos (w, PT, r0, NULL, 0);
 22066           if (row)
 22067             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 22068           return 2;
 22069         }
 22070     }
 22071 
 22072   /* Give up if window start is in the changed area.
 22073 
 22074      The condition used to read
 22075 
 22076      (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
 22077 
 22078      but why that was tested escapes me at the moment.  */
 22079   if (CHARPOS (start) >= first_changed_charpos
 22080       && CHARPOS (start) <= last_changed_charpos)
 22081     GIVE_UP (15);
 22082 
 22083   /* Check that window start agrees with the start of the first glyph
 22084      row in its current matrix.  Check this after we know the window
 22085      start is not in changed text, otherwise positions would not be
 22086      comparable.  */
 22087   row = MATRIX_FIRST_TEXT_ROW (current_matrix);
 22088   if (!TEXT_POS_EQUAL_P (start, row->minpos))
 22089     GIVE_UP (16);
 22090 
 22091   /* Give up if the window ends in strings.  Overlay strings
 22092      at the end are difficult to handle, so don't try.  */
 22093   row = MATRIX_ROW (current_matrix, w->window_end_vpos);
 22094   if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
 22095     GIVE_UP (20);
 22096 
 22097   /* Can't let scroll_run_hook below run on w32 GUI frames when
 22098      position of point is indicated by the system caret, because
 22099      scrolling the display will then "copy" the pixels used by the
 22100      caret.  */
 22101 #ifdef HAVE_NTGUI
 22102   if (FRAME_W32_P (f) && w32_use_visible_system_caret)
 22103     GIVE_UP (25);
 22104 #endif
 22105 
 22106   /* Compute the position at which we have to start displaying new
 22107      lines.  Some of the lines at the top of the window might be
 22108      reusable because they are not displaying changed text.  Find the
 22109      last row in W's current matrix not affected by changes at the
 22110      start of current_buffer.  Value is null if changes start in the
 22111      first line of window.  */
 22112   last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
 22113   if (last_unchanged_at_beg_row)
 22114     {
 22115       /* Avoid starting to display in the middle of a character, a TAB
 22116          for instance.  This is easier than to set up the iterator
 22117          exactly, and it's not a frequent case, so the additional
 22118          effort wouldn't really pay off.  */
 22119       while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
 22120               || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
 22121              && last_unchanged_at_beg_row > w->current_matrix->rows)
 22122         --last_unchanged_at_beg_row;
 22123 
 22124       if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
 22125         GIVE_UP (17);
 22126 
 22127       if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
 22128         GIVE_UP (18);
 22129       /* Give up if the row starts with a display property that draws
 22130          on the fringes, since that could prevent correct display of
 22131          line-prefix and wrap-prefix.  */
 22132       if (it.sp > 1
 22133           && it.method == GET_FROM_IMAGE && it.image_id == -1)
 22134         GIVE_UP (26);
 22135       start_pos = it.current.pos;
 22136 
 22137       /* Start displaying new lines in the desired matrix at the same
 22138          vpos we would use in the current matrix, i.e. below
 22139          last_unchanged_at_beg_row.  */
 22140       it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
 22141                                      current_matrix);
 22142       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22143       it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
 22144 
 22145       eassert (it.hpos == 0 && it.current_x == 0);
 22146     }
 22147   else
 22148     {
 22149       /* There are no reusable lines at the start of the window.
 22150          Start displaying in the first text line.  */
 22151       start_display (&it, w, start);
 22152       it.vpos = it.first_vpos;
 22153       start_pos = it.current.pos;
 22154     }
 22155 
 22156   /* Find the first row that is not affected by changes at the end of
 22157      the buffer.  Value will be null if there is no unchanged row, in
 22158      which case we must redisplay to the end of the window.  delta
 22159      will be set to the value by which buffer positions beginning with
 22160      first_unchanged_at_end_row have to be adjusted due to text
 22161      changes.  */
 22162   first_unchanged_at_end_row
 22163     = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
 22164   IF_DEBUG (debug_delta = delta);
 22165   IF_DEBUG (debug_delta_bytes = delta_bytes);
 22166 
 22167   /* Set stop_pos to the buffer position up to which we will have to
 22168      display new lines.  If first_unchanged_at_end_row != NULL, this
 22169      is the buffer position of the start of the line displayed in that
 22170      row.  For first_unchanged_at_end_row == NULL, use 0 to indicate
 22171      that we don't stop at a buffer position.  */
 22172   stop_pos = 0;
 22173   if (first_unchanged_at_end_row)
 22174     {
 22175       eassert (last_unchanged_at_beg_row == NULL
 22176                || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
 22177 
 22178       /* If this is a continuation line, move forward to the next one
 22179          that isn't.  Changes in lines above affect this line.
 22180          Caution: this may move first_unchanged_at_end_row to a row
 22181          not displaying text.  */
 22182       while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
 22183              && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22184              && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22185                  < it.last_visible_y))
 22186         ++first_unchanged_at_end_row;
 22187 
 22188       if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22189           || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22190               >= it.last_visible_y))
 22191         first_unchanged_at_end_row = NULL;
 22192       else
 22193         {
 22194           stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
 22195                       + delta);
 22196           first_unchanged_at_end_vpos
 22197             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
 22198           eassert (stop_pos >= Z - END_UNCHANGED);
 22199         }
 22200     }
 22201   else if (last_unchanged_at_beg_row == NULL)
 22202     GIVE_UP (19);
 22203 
 22204 
 22205 #ifdef GLYPH_DEBUG
 22206 
 22207   /* Either there is no unchanged row at the end, or the one we have
 22208      now displays text.  This is a necessary condition for the window
 22209      end pos calculation at the end of this function.  */
 22210   eassert (first_unchanged_at_end_row == NULL
 22211            || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22212 
 22213   debug_last_unchanged_at_beg_vpos
 22214     = (last_unchanged_at_beg_row
 22215        ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
 22216        : -1);
 22217   debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
 22218 
 22219 #endif /* GLYPH_DEBUG */
 22220 
 22221 
 22222   /* Display new lines.  Set last_text_row to the last new line
 22223      displayed which has text on it, i.e. might end up as being the
 22224      line where the window_end_vpos is.  */
 22225   w->cursor.vpos = -1;
 22226   last_text_row = NULL;
 22227   overlay_arrow_seen = false;
 22228   if (it.current_y < it.last_visible_y
 22229       && !f->fonts_changed
 22230       && (first_unchanged_at_end_row == NULL
 22231           || IT_CHARPOS (it) < stop_pos))
 22232     it.glyph_row->reversed_p = false;
 22233   while (it.current_y < it.last_visible_y
 22234          && !f->fonts_changed
 22235          && (first_unchanged_at_end_row == NULL
 22236              || IT_CHARPOS (it) < stop_pos))
 22237     {
 22238       if (display_line (&it, -1))
 22239         last_text_row = it.glyph_row - 1;
 22240     }
 22241 
 22242   if (f->fonts_changed)
 22243     return -1;
 22244 
 22245   /* The redisplay iterations in display_line above could have
 22246      triggered font-lock, which could have done something that
 22247      invalidates IT->w window's end-point information, on which we
 22248      rely below.  E.g., one package, which will remain unnamed, used
 22249      to install a font-lock-fontify-region-function that called
 22250      bury-buffer, whose side effect is to switch the buffer displayed
 22251      by IT->w, and that predictably resets IT->w's window_end_valid
 22252      flag, which we already tested at the entry to this function.
 22253      Amply punish such packages/modes by giving up on this
 22254      optimization in those cases.  */
 22255   if (!w->window_end_valid)
 22256     {
 22257       clear_glyph_matrix (w->desired_matrix);
 22258       return -1;
 22259     }
 22260 
 22261   /* Compute differences in buffer positions, y-positions etc.  for
 22262      lines reused at the bottom of the window.  Compute what we can
 22263      scroll.  */
 22264   if (first_unchanged_at_end_row
 22265       /* No lines reused because we displayed everything up to the
 22266          bottom of the window.  */
 22267       && it.current_y < it.last_visible_y)
 22268     {
 22269       dvpos = (it.vpos
 22270                - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
 22271                                   current_matrix));
 22272       dy = it.current_y - first_unchanged_at_end_row->y;
 22273       run.current_y = first_unchanged_at_end_row->y;
 22274       run.desired_y = run.current_y + dy;
 22275       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
 22276     }
 22277   else
 22278     {
 22279       delta = delta_bytes = dvpos = dy
 22280         = run.current_y = run.desired_y = run.height = 0;
 22281       first_unchanged_at_end_row = NULL;
 22282     }
 22283   IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
 22284 
 22285 
 22286   /* Find the cursor if not already found.  We have to decide whether
 22287      PT will appear on this window (it sometimes doesn't, but this is
 22288      not a very frequent case.)  This decision has to be made before
 22289      the current matrix is altered.  A value of cursor.vpos < 0 means
 22290      that PT is either in one of the lines beginning at
 22291      first_unchanged_at_end_row or below the window.  Don't care for
 22292      lines that might be displayed later at the window end; as
 22293      mentioned, this is not a frequent case.  */
 22294   if (w->cursor.vpos < 0)
 22295     {
 22296       /* Cursor in unchanged rows at the top?  */
 22297       if (PT < CHARPOS (start_pos)
 22298           && last_unchanged_at_beg_row)
 22299         {
 22300           row = row_containing_pos (w, PT,
 22301                                     MATRIX_FIRST_TEXT_ROW (w->current_matrix),
 22302                                     last_unchanged_at_beg_row + 1, 0);
 22303           if (row)
 22304             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 22305         }
 22306 
 22307       /* Start from first_unchanged_at_end_row looking for PT.  */
 22308       else if (first_unchanged_at_end_row)
 22309         {
 22310           row = row_containing_pos (w, PT - delta,
 22311                                     first_unchanged_at_end_row, NULL, 0);
 22312           if (row)
 22313             set_cursor_from_row (w, row, w->current_matrix, delta,
 22314                                  delta_bytes, dy, dvpos);
 22315         }
 22316 
 22317       /* Give up if cursor was not found.  */
 22318       if (w->cursor.vpos < 0)
 22319         {
 22320           clear_glyph_matrix (w->desired_matrix);
 22321           return -1;
 22322         }
 22323     }
 22324 
 22325   /* Don't let the cursor end in the scroll margins.  */
 22326   {
 22327     int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 22328     int bot_scroll_margin = top_scroll_margin;
 22329     int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
 22330 
 22331     if (window_wants_header_line (w))
 22332       top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 22333     if (window_wants_tab_line (w))
 22334       top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 22335 
 22336     if ((w->cursor.y < top_scroll_margin
 22337          && CHARPOS (start) > BEGV)
 22338         /* Old redisplay didn't take scroll margin into account at the bottom,
 22339            but then global-hl-line-mode doesn't scroll.  KFS 2004-06-14 */
 22340         || (w->cursor.y
 22341             + (cursor_row_fully_visible_p (w, false, true, true)
 22342                ? 1
 22343                : cursor_height + bot_scroll_margin)) > it.last_visible_y)
 22344       {
 22345         w->cursor.vpos = -1;
 22346         clear_glyph_matrix (w->desired_matrix);
 22347         return -1;
 22348       }
 22349   }
 22350 
 22351   /* Scroll the display.  Do it before changing the current matrix so
 22352      that xterm.c doesn't get confused about where the cursor glyph is
 22353      found.  */
 22354   if (dy && run.height)
 22355     {
 22356       update_begin (f);
 22357 
 22358       if (FRAME_WINDOW_P (f))
 22359         {
 22360 #ifdef HAVE_WINDOW_SYSTEM
 22361           gui_update_window_begin (w);
 22362           FRAME_RIF (f)->clear_window_mouse_face (w);
 22363           FRAME_RIF (f)->scroll_run_hook (w, &run);
 22364           gui_update_window_end (w, false, false);
 22365 #endif
 22366         }
 22367       else
 22368         {
 22369           /* Terminal frame.  In this case, dvpos gives the number of
 22370              lines to scroll by; dvpos < 0 means scroll up.  */
 22371           int from_vpos
 22372             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
 22373           int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
 22374           int end = (WINDOW_TOP_EDGE_LINE (w)
 22375                      + window_wants_tab_line (w)
 22376                      + window_wants_header_line (w)
 22377                      + window_internal_height (w));
 22378 
 22379           gui_clear_window_mouse_face (w);
 22380 
 22381           /* Perform the operation on the screen.  */
 22382           if (dvpos > 0)
 22383             {
 22384               /* Scroll last_unchanged_at_beg_row to the end of the
 22385                  window down dvpos lines.  */
 22386               set_terminal_window (f, end);
 22387 
 22388               /* On dumb terminals delete dvpos lines at the end
 22389                  before inserting dvpos empty lines.  */
 22390               if (!FRAME_SCROLL_REGION_OK (f))
 22391                 ins_del_lines (f, end - dvpos, -dvpos);
 22392 
 22393               /* Insert dvpos empty lines in front of
 22394                  last_unchanged_at_beg_row.  */
 22395               ins_del_lines (f, from, dvpos);
 22396             }
 22397           else if (dvpos < 0)
 22398             {
 22399               /* Scroll up last_unchanged_at_beg_vpos to the end of
 22400                  the window to last_unchanged_at_beg_vpos - |dvpos|.  */
 22401               set_terminal_window (f, end);
 22402 
 22403               /* Delete dvpos lines in front of
 22404                  last_unchanged_at_beg_vpos.  ins_del_lines will set
 22405                  the cursor to the given vpos and emit |dvpos| delete
 22406                  line sequences.  */
 22407               ins_del_lines (f, from + dvpos, dvpos);
 22408 
 22409               /* On a dumb terminal insert dvpos empty lines at the
 22410                  end.  */
 22411               if (!FRAME_SCROLL_REGION_OK (f))
 22412                 ins_del_lines (f, end + dvpos, -dvpos);
 22413             }
 22414 
 22415           set_terminal_window (f, 0);
 22416         }
 22417 
 22418       update_end (f);
 22419     }
 22420 
 22421   /* Shift reused rows of the current matrix to the right position.
 22422      BOTTOM_ROW is the last + 1 row in the current matrix reserved for
 22423      text.  */
 22424   bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22425   bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
 22426   if (dvpos < 0)
 22427     {
 22428       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
 22429                      bottom_vpos, dvpos);
 22430       clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
 22431                                bottom_vpos);
 22432     }
 22433   else if (dvpos > 0)
 22434     {
 22435       rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
 22436                      bottom_vpos, dvpos);
 22437       clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
 22438                                first_unchanged_at_end_vpos + dvpos);
 22439     }
 22440 
 22441   /* For frame-based redisplay, make sure that current frame and window
 22442      matrix are in sync with respect to glyph memory.  */
 22443   if (!FRAME_WINDOW_P (f))
 22444     sync_frame_with_window_matrix_rows (w);
 22445 
 22446   /* Adjust buffer positions in reused rows.  */
 22447   if (delta || delta_bytes)
 22448     increment_matrix_positions (current_matrix,
 22449                                 first_unchanged_at_end_vpos + dvpos,
 22450                                 bottom_vpos, delta, delta_bytes);
 22451 
 22452   /* Adjust Y positions.  */
 22453   if (dy)
 22454     shift_glyph_matrix (w, current_matrix,
 22455                         first_unchanged_at_end_vpos + dvpos,
 22456                         bottom_vpos, dy);
 22457 
 22458   if (first_unchanged_at_end_row)
 22459     {
 22460       first_unchanged_at_end_row += dvpos;
 22461       if (first_unchanged_at_end_row->y >= it.last_visible_y
 22462           || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
 22463         first_unchanged_at_end_row = NULL;
 22464     }
 22465 
 22466   /* If scrolling up, there may be some lines to display at the end of
 22467      the window.  */
 22468   last_text_row_at_end = NULL;
 22469   if (dy < 0)
 22470     {
 22471       /* Scrolling up can leave for example a partially visible line
 22472          at the end of the window to be redisplayed.  */
 22473       /* Set last_row to the glyph row in the current matrix where the
 22474          window end line is found.  It has been moved up or down in
 22475          the matrix by dvpos.  */
 22476       int last_vpos = w->window_end_vpos + dvpos;
 22477       struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
 22478 
 22479       /* If last_row is the window end line, it should display text.  */
 22480       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
 22481 
 22482       /* If window end line was partially visible before, begin
 22483          displaying at that line.  Otherwise begin displaying with the
 22484          line following it.  */
 22485       if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
 22486         {
 22487           init_to_row_start (&it, w, last_row);
 22488           it.vpos = last_vpos;
 22489           it.current_y = last_row->y;
 22490         }
 22491       else
 22492         {
 22493           init_to_row_end (&it, w, last_row);
 22494           it.vpos = 1 + last_vpos;
 22495           it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
 22496           ++last_row;
 22497         }
 22498 
 22499       /* We may start in a continuation line.  If so, we have to
 22500          get the right continuation_lines_width and current_x.  */
 22501       it.continuation_lines_width = last_row->continuation_lines_width;
 22502       it.hpos = it.current_x = 0;
 22503 
 22504       /* Display the rest of the lines at the window end.  */
 22505       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22506       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 22507         {
 22508           /* Is it always sure that the display agrees with lines in
 22509              the current matrix?  I don't think so, so we mark rows
 22510              displayed invalid in the current matrix by setting their
 22511              enabled_p flag to false.  */
 22512           SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
 22513           if (display_line (&it, w->cursor.vpos))
 22514             last_text_row_at_end = it.glyph_row - 1;
 22515         }
 22516     }
 22517 
 22518   /* Update window_end_pos and window_end_vpos.  */
 22519   if (first_unchanged_at_end_row && !last_text_row_at_end)
 22520     {
 22521       /* Window end line if one of the preserved rows from the current
 22522          matrix.  Set row to the last row displaying text in current
 22523          matrix starting at first_unchanged_at_end_row, after
 22524          scrolling.  */
 22525       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22526       row = find_last_row_displaying_text (w->current_matrix, &it,
 22527                                            first_unchanged_at_end_row);
 22528       eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
 22529       adjust_window_ends (w, row, true);
 22530       eassert (w->window_end_bytepos >= 0);
 22531       IF_DEBUG (debug_method_add (w, "A"));
 22532     }
 22533   else if (last_text_row_at_end)
 22534     {
 22535       adjust_window_ends (w, last_text_row_at_end, false);
 22536       eassert (w->window_end_bytepos >= 0);
 22537       IF_DEBUG (debug_method_add (w, "B"));
 22538     }
 22539   else if (last_text_row)
 22540     {
 22541       /* We have displayed either to the end of the window or at the
 22542          end of the window, i.e. the last row with text is to be found
 22543          in the desired matrix.  */
 22544       adjust_window_ends (w, last_text_row, false);
 22545       eassert (w->window_end_bytepos >= 0);
 22546     }
 22547   else if (first_unchanged_at_end_row == NULL
 22548            && last_text_row == NULL
 22549            && last_text_row_at_end == NULL)
 22550     {
 22551       /* Displayed to end of window, but no line containing text was
 22552          displayed.  Lines were deleted at the end of the window.  */
 22553       int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
 22554       int vpos = w->window_end_vpos;
 22555       struct glyph_row *current_row = current_matrix->rows + vpos;
 22556       struct glyph_row *desired_row = desired_matrix->rows + vpos;
 22557 
 22558       for (row = NULL; !row; --vpos, --current_row, --desired_row)
 22559         {
 22560           eassert (first_vpos <= vpos);
 22561           if (desired_row->enabled_p)
 22562             {
 22563               if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
 22564                 row = desired_row;
 22565             }
 22566           else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
 22567             row  = current_row;
 22568         }
 22569 
 22570       w->window_end_vpos = vpos + 1;
 22571       w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22572       w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22573       eassert (w->window_end_bytepos >= 0);
 22574       IF_DEBUG (debug_method_add (w, "C"));
 22575     }
 22576   else
 22577     emacs_abort ();
 22578 
 22579   IF_DEBUG ((debug_end_pos = w->window_end_pos,
 22580              debug_end_vpos = w->window_end_vpos));
 22581 
 22582   /* Record that display has not been completed.  */
 22583   w->window_end_valid = false;
 22584   w->desired_matrix->no_scrolling_p = true;
 22585   return 3;
 22586 
 22587 #undef GIVE_UP
 22588 }
 22589 
 22590 
 22591 
 22592 /***********************************************************************
 22593                         More debugging support
 22594  ***********************************************************************/
 22595 
 22596 #ifdef GLYPH_DEBUG
 22597 
 22598 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
 22599 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
 22600 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
 22601 
 22602 
 22603 /* Dump the contents of glyph matrix MATRIX on stderr.
 22604 
 22605    GLYPHS 0 means don't show glyph contents.
 22606    GLYPHS 1 means show glyphs in short form
 22607    GLYPHS > 1 means show glyphs in long form.  */
 22608 
 22609 void
 22610 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
 22611 {
 22612   int i;
 22613   for (i = 0; i < matrix->nrows; ++i)
 22614     dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
 22615 }
 22616 
 22617 
 22618 /* Dump contents of glyph GLYPH to stderr.  ROW and AREA are
 22619    the glyph row and area where the glyph comes from.  */
 22620 
 22621 void
 22622 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
 22623 {
 22624   if (glyph->type == CHAR_GLYPH
 22625       || glyph->type == GLYPHLESS_GLYPH)
 22626     {
 22627       fprintf (stderr,
 22628                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22629                glyph - row->glyphs[TEXT_AREA],
 22630                (glyph->type == CHAR_GLYPH
 22631                 ? 'C'
 22632                 : 'G'),
 22633                glyph->charpos,
 22634                (BUFFERP (glyph->object)
 22635                 ? 'B'
 22636                 : (STRINGP (glyph->object)
 22637                    ? 'S'
 22638                    : (NILP (glyph->object)
 22639                       ? '0'
 22640                       : '-'))),
 22641                glyph->pixel_width,
 22642                glyph->u.ch,
 22643                (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
 22644                 ? (int) glyph->u.ch
 22645                 : '.'),
 22646                glyph->face_id,
 22647                glyph->left_box_line_p,
 22648                glyph->right_box_line_p);
 22649     }
 22650   else if (glyph->type == STRETCH_GLYPH)
 22651     {
 22652       fprintf (stderr,
 22653                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22654                glyph - row->glyphs[TEXT_AREA],
 22655                'S',
 22656                glyph->charpos,
 22657                (BUFFERP (glyph->object)
 22658                 ? 'B'
 22659                 : (STRINGP (glyph->object)
 22660                    ? 'S'
 22661                    : (NILP (glyph->object)
 22662                       ? '0'
 22663                       : '-'))),
 22664                glyph->pixel_width,
 22665                0u,
 22666                ' ',
 22667                glyph->face_id,
 22668                glyph->left_box_line_p,
 22669                glyph->right_box_line_p);
 22670     }
 22671   else if (glyph->type == IMAGE_GLYPH)
 22672     {
 22673       fprintf (stderr,
 22674                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22675                glyph - row->glyphs[TEXT_AREA],
 22676                'I',
 22677                glyph->charpos,
 22678                (BUFFERP (glyph->object)
 22679                 ? 'B'
 22680                 : (STRINGP (glyph->object)
 22681                    ? 'S'
 22682                    : (NILP (glyph->object)
 22683                       ? '0'
 22684                       : '-'))),
 22685                glyph->pixel_width,
 22686                (unsigned int) glyph->u.img_id,
 22687                '.',
 22688                glyph->face_id,
 22689                glyph->left_box_line_p,
 22690                glyph->right_box_line_p);
 22691     }
 22692   else if (glyph->type == COMPOSITE_GLYPH)
 22693     {
 22694       fprintf (stderr,
 22695                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x",
 22696                glyph - row->glyphs[TEXT_AREA],
 22697                '+',
 22698                glyph->charpos,
 22699                (BUFFERP (glyph->object)
 22700                 ? 'B'
 22701                 : (STRINGP (glyph->object)
 22702                    ? 'S'
 22703                    : (NILP (glyph->object)
 22704                       ? '0'
 22705                       : '-'))),
 22706                glyph->pixel_width,
 22707                (unsigned int) glyph->u.cmp.id);
 22708       if (glyph->u.cmp.automatic)
 22709         fprintf (stderr,
 22710                  "[%d-%d]",
 22711                  glyph->slice.cmp.from, glyph->slice.cmp.to);
 22712       fprintf (stderr, " . %4d %1.1d%1.1d\n",
 22713                glyph->face_id,
 22714                glyph->left_box_line_p,
 22715                glyph->right_box_line_p);
 22716     }
 22717   else if (glyph->type == XWIDGET_GLYPH)
 22718     {
 22719 #ifndef HAVE_XWIDGETS
 22720       eassume (false);
 22721 #else
 22722       fprintf (stderr,
 22723                "  %5td %4c %6td %c %3d %7p %c %4d %1.1d%1.1d\n",
 22724                glyph - row->glyphs[TEXT_AREA],
 22725                'X',
 22726                glyph->charpos,
 22727                (BUFFERP (glyph->object)
 22728                 ? 'B'
 22729                 : (STRINGP (glyph->object)
 22730                    ? 'S'
 22731                    : '-')),
 22732                glyph->pixel_width,
 22733                glyph->u.xwidget,
 22734                '.',
 22735                glyph->face_id,
 22736                glyph->left_box_line_p,
 22737                glyph->right_box_line_p);
 22738 #endif
 22739     }
 22740 }
 22741 
 22742 
 22743 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
 22744    GLYPHS 0 means don't show glyph contents.
 22745    GLYPHS 1 means show glyphs in short form
 22746    GLYPHS > 1 means show glyphs in long form.  */
 22747 
 22748 void
 22749 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
 22750 {
 22751   if (glyphs != 1)
 22752     {
 22753       fputs (("Row     Start       End Used oE><\\CTZFesm     X    Y    W    H    V    A    P\n"
 22754               "==============================================================================\n"),
 22755              stderr);
 22756       fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
 22757 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d  %4d %4d %4d %4d %4d %4d %4d\n",
 22758                vpos,
 22759                MATRIX_ROW_START_CHARPOS (row),
 22760                MATRIX_ROW_END_CHARPOS (row),
 22761                row->used[TEXT_AREA],
 22762                row->contains_overlapping_glyphs_p,
 22763                row->enabled_p,
 22764                row->truncated_on_left_p,
 22765                row->truncated_on_right_p,
 22766                row->continued_p,
 22767                MATRIX_ROW_CONTINUATION_LINE_P (row),
 22768                MATRIX_ROW_DISPLAYS_TEXT_P (row),
 22769                row->ends_at_zv_p,
 22770                row->fill_line_p,
 22771                row->ends_in_middle_of_char_p,
 22772                row->starts_in_middle_of_char_p,
 22773                row->mouse_face_p,
 22774                row->x,
 22775                row->y,
 22776                row->pixel_width,
 22777                row->height,
 22778                row->visible_height,
 22779                row->ascent,
 22780                row->phys_ascent);
 22781       /* The next 3 lines should align to "Start" in the header.  */
 22782       fprintf (stderr, "    %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
 22783                row->end.overlay_string_index,
 22784                row->continuation_lines_width);
 22785       fprintf (stderr, "    %9"pD"d %9"pD"d\n",
 22786                CHARPOS (row->start.string_pos),
 22787                CHARPOS (row->end.string_pos));
 22788       fprintf (stderr, "    %9d %9d\n", row->start.dpvec_index,
 22789                row->end.dpvec_index);
 22790     }
 22791 
 22792   if (glyphs > 1)
 22793     {
 22794       int area;
 22795 
 22796       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22797         {
 22798           struct glyph *glyph = row->glyphs[area];
 22799           struct glyph *glyph_end = glyph + row->used[area];
 22800 
 22801           /* Glyph for a line end in text.  */
 22802           if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
 22803             ++glyph_end;
 22804 
 22805           if (glyph < glyph_end)
 22806             fputs (" Glyph#  Type       Pos   O   W     Code      C Face LR\n",
 22807                    stderr);
 22808 
 22809           for (; glyph < glyph_end; ++glyph)
 22810             dump_glyph (row, glyph, area);
 22811         }
 22812     }
 22813   else if (glyphs == 1)
 22814     {
 22815       int area;
 22816       char s[SHRT_MAX + 4];
 22817 
 22818       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22819         {
 22820           int i;
 22821 
 22822           for (i = 0; i < row->used[area]; ++i)
 22823             {
 22824               struct glyph *glyph = row->glyphs[area] + i;
 22825               if (i == row->used[area] - 1
 22826                   && area == TEXT_AREA
 22827                   && NILP (glyph->object)
 22828                   && glyph->type == CHAR_GLYPH
 22829                   && glyph->u.ch == ' ')
 22830                 {
 22831                   strcpy (&s[i], "[\\n]");
 22832                   i += 4;
 22833                 }
 22834               else if (glyph->type == CHAR_GLYPH
 22835                        && glyph->u.ch < 0x80
 22836                        && glyph->u.ch >= ' ')
 22837                 s[i] = glyph->u.ch;
 22838               else
 22839                 s[i] = '.';
 22840             }
 22841 
 22842           s[i] = '\0';
 22843           fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
 22844         }
 22845     }
 22846 }
 22847 
 22848 
 22849 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 22850        Sdump_glyph_matrix, 0, 1, "p",
 22851        doc: /* Dump the current matrix of the selected window to stderr.
 22852 Shows contents of glyph row structures.  With non-nil
 22853 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
 22854 glyphs in short form, otherwise show glyphs in long form.
 22855 
 22856 Interactively, no argument means show glyphs in short form;
 22857 with numeric argument, its value is passed as the GLYPHS flag.  */)
 22858   (Lisp_Object glyphs)
 22859 {
 22860   struct window *w = XWINDOW (selected_window);
 22861   struct buffer *buffer = XBUFFER (w->contents);
 22862 
 22863   fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
 22864            BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
 22865   fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
 22866            w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
 22867   fputs ("=============================================\n", stderr);
 22868   dump_glyph_matrix (w->current_matrix,
 22869                      TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 0);
 22870   return Qnil;
 22871 }
 22872 
 22873 
 22874 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
 22875        Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
 22876 Only text-mode frames have frame glyph matrices.  */)
 22877   (void)
 22878 {
 22879   struct frame *f = XFRAME (selected_frame);
 22880 
 22881   if (f->current_matrix)
 22882     dump_glyph_matrix (f->current_matrix, 1);
 22883   else
 22884     fputs ("*** This frame doesn't have a frame glyph matrix ***\n", stderr);
 22885   return Qnil;
 22886 }
 22887 
 22888 
 22889 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
 22890        doc: /* Dump glyph row ROW to stderr.
 22891 Interactively, ROW is the prefix numeric argument and defaults to
 22892 the row which displays point.
 22893 Optional argument GLYPHS 0 means don't dump glyphs.
 22894 GLYPHS 1 means dump glyphs in short form.
 22895 GLYPHS > 1 or omitted means dump glyphs in long form.  */)
 22896   (Lisp_Object row, Lisp_Object glyphs)
 22897 {
 22898   struct glyph_matrix *matrix;
 22899   EMACS_INT vpos;
 22900 
 22901   if (NILP (row))
 22902     {
 22903       int d1, d2, d3, d4, d5, ypos;
 22904       bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
 22905                                       &d1, &d2, &d3, &d4, &d5, &ypos);
 22906       if (visible_p)
 22907         vpos = ypos;
 22908       else
 22909         vpos = 0;
 22910     }
 22911   else
 22912     {
 22913       CHECK_FIXNUM (row);
 22914       vpos = XFIXNUM (row);
 22915     }
 22916   matrix = XWINDOW (selected_window)->current_matrix;
 22917   if (vpos >= 0 && vpos < matrix->nrows)
 22918     dump_glyph_row (MATRIX_ROW (matrix, vpos),
 22919                     vpos,
 22920                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22921   return Qnil;
 22922 }
 22923 
 22924 
 22925 DEFUN ("dump-tab-bar-row", Fdump_tab_bar_row, Sdump_tab_bar_row, 1, 2, "P",
 22926        doc: /* Dump glyph row ROW of the tab-bar of the current frame to stderr.
 22927 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22928 GLYPHS 0 means don't dump glyphs.
 22929 GLYPHS 1 means dump glyphs in short form.
 22930 GLYPHS > 1 or omitted means dump glyphs in long form.
 22931 
 22932 If there's no tab-bar, or if the tab-bar is not drawn by Emacs,
 22933 do nothing.  */)
 22934   (Lisp_Object row, Lisp_Object glyphs)
 22935 {
 22936 #if defined (HAVE_WINDOW_SYSTEM)
 22937   struct frame *sf = SELECTED_FRAME ();
 22938   struct glyph_matrix *m = WINDOWP (sf->tab_bar_window)
 22939     ? XWINDOW (sf->tab_bar_window)->current_matrix
 22940     : sf->current_matrix;
 22941   EMACS_INT vpos;
 22942 
 22943   if (NILP (row))
 22944     vpos = WINDOWP (sf->tab_bar_window) ? 0 :
 22945       FRAME_MENU_BAR_LINES (sf) > 0 ? 1 : 0;
 22946   else
 22947     {
 22948       CHECK_FIXNUM (row);
 22949       vpos = XFIXNUM (row);
 22950     }
 22951   if (vpos >= 0 && vpos < m->nrows)
 22952     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22953                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22954 #endif
 22955   return Qnil;
 22956 }
 22957 
 22958 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
 22959        doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
 22960 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22961 GLYPHS 0 means don't dump glyphs.
 22962 GLYPHS 1 means dump glyphs in short form.
 22963 GLYPHS > 1 or omitted means dump glyphs in long form.
 22964 
 22965 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
 22966 do nothing.  */)
 22967   (Lisp_Object row, Lisp_Object glyphs)
 22968 {
 22969 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 22970   struct frame *sf = SELECTED_FRAME ();
 22971   struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 22972   EMACS_INT vpos;
 22973 
 22974   if (NILP (row))
 22975     vpos = 0;
 22976   else
 22977     {
 22978       CHECK_FIXNUM (row);
 22979       vpos = XFIXNUM (row);
 22980     }
 22981   if (vpos >= 0 && vpos < m->nrows)
 22982     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22983                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22984 #endif
 22985   return Qnil;
 22986 }
 22987 
 22988 
 22989 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
 22990        doc: /* Toggle tracing of redisplay.
 22991 With ARG, turn tracing on if and only if ARG is positive.  */)
 22992   (Lisp_Object arg)
 22993 {
 22994   if (NILP (arg))
 22995     trace_redisplay_p = !trace_redisplay_p;
 22996   else
 22997     {
 22998       arg = Fprefix_numeric_value (arg);
 22999       trace_redisplay_p = XFIXNUM (arg) > 0;
 23000     }
 23001 
 23002   return Qnil;
 23003 }
 23004 
 23005 
 23006 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
 23007        doc: /* Like `format', but print result to stderr.
 23008 usage: (trace-to-stderr STRING &rest OBJECTS)  */)
 23009   (ptrdiff_t nargs, Lisp_Object *args)
 23010 {
 23011   Lisp_Object s = Fformat (nargs, args);
 23012   fwrite (SDATA (s), 1, SBYTES (s), stderr);
 23013   return Qnil;
 23014 }
 23015 
 23016 #endif /* GLYPH_DEBUG */
 23017 
 23018 
 23019 
 23020 /***********************************************************************
 23021                      Building Desired Matrix Rows
 23022  ***********************************************************************/
 23023 
 23024 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
 23025    Used for non-window-redisplay windows, and for windows without left
 23026    fringe.  */
 23027 
 23028 static struct glyph_row *
 23029 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 23030 {
 23031   struct frame *f = XFRAME (WINDOW_FRAME (w));
 23032   struct buffer *buffer = XBUFFER (w->contents);
 23033   struct buffer *old = current_buffer;
 23034   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
 23035   ptrdiff_t arrow_len = SBYTES (overlay_arrow_string), char_num = 0;
 23036   const unsigned char *arrow_end = arrow_string + arrow_len;
 23037   const unsigned char *p;
 23038   struct it it;
 23039   bool multibyte_p;
 23040   int n_glyphs_before;
 23041 
 23042   set_buffer_temp (buffer);
 23043   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
 23044   scratch_glyph_row.reversed_p = false;
 23045   it.glyph_row->used[TEXT_AREA] = 0;
 23046   SET_TEXT_POS (it.position, 0, 0);
 23047 
 23048   multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
 23049   p = arrow_string;
 23050   while (p < arrow_end)
 23051     {
 23052       Lisp_Object face, ilisp;
 23053 
 23054       /* Get the next character.  */
 23055       if (multibyte_p)
 23056         it.c = it.char_to_display = check_char_and_length (p, &it.len);
 23057       else
 23058         {
 23059           it.c = it.char_to_display = *p, it.len = 1;
 23060           if (! ASCII_CHAR_P (it.c))
 23061             it.char_to_display = BYTE8_TO_CHAR (it.c);
 23062         }
 23063       p += it.len;
 23064 
 23065       /* Get its face.  */
 23066       ilisp = make_fixnum (char_num++);
 23067       face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
 23068       it.face_id = compute_char_face (f, it.char_to_display, face);
 23069 
 23070       /* Compute its width, get its glyphs.  */
 23071       n_glyphs_before = it.glyph_row->used[TEXT_AREA];
 23072       SET_TEXT_POS (it.position, -1, -1);
 23073       PRODUCE_GLYPHS (&it);
 23074 
 23075       /* If this character doesn't fit any more in the line, we have
 23076          to remove some glyphs.  */
 23077       if (it.current_x > it.last_visible_x)
 23078         {
 23079           it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
 23080           break;
 23081         }
 23082     }
 23083 
 23084   set_buffer_temp (old);
 23085   return it.glyph_row;
 23086 }
 23087 
 23088 
 23089 /* Insert truncation glyphs at the start of IT->glyph_row.  Which
 23090    glyphs to insert is determined by produce_special_glyphs.  */
 23091 
 23092 static void
 23093 insert_left_trunc_glyphs (struct it *it)
 23094 {
 23095   struct it truncate_it;
 23096   struct glyph *from, *end, *to, *toend;
 23097 
 23098   eassert (!FRAME_WINDOW_P (it->f)
 23099            || (!it->glyph_row->reversed_p
 23100                && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23101            || (it->glyph_row->reversed_p
 23102                && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
 23103 
 23104   /* Get the truncation glyphs.  */
 23105   truncate_it = *it;
 23106   truncate_it.current_x = 0;
 23107   truncate_it.face_id = DEFAULT_FACE_ID;
 23108   truncate_it.glyph_row = &scratch_glyph_row;
 23109   truncate_it.area = TEXT_AREA;
 23110   truncate_it.glyph_row->used[TEXT_AREA] = 0;
 23111   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
 23112   truncate_it.object = Qnil;
 23113   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 23114 
 23115   /* Overwrite glyphs from IT with truncation glyphs.  */
 23116   if (!it->glyph_row->reversed_p)
 23117     {
 23118       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 23119 
 23120       from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23121       end = from + tused;
 23122       to = it->glyph_row->glyphs[TEXT_AREA];
 23123       toend = to + it->glyph_row->used[TEXT_AREA];
 23124       if (FRAME_WINDOW_P (it->f))
 23125         {
 23126           /* On GUI frames, when variable-size fonts are displayed,
 23127              the truncation glyphs may need more pixels than the row's
 23128              glyphs they overwrite.  We overwrite more glyphs to free
 23129              enough screen real estate, and enlarge the stretch glyph
 23130              on the right (see display_line), if there is one, to
 23131              preserve the screen position of the truncation glyphs on
 23132              the right.  */
 23133           int w = 0;
 23134           struct glyph *g = to;
 23135           short used;
 23136 
 23137           /* The first glyph could be partially visible, in which case
 23138              it->glyph_row->x will be negative.  But we want the left
 23139              truncation glyphs to be aligned at the left margin of the
 23140              window, so we override the x coordinate at which the row
 23141              will begin.  */
 23142           it->glyph_row->x = 0;
 23143           while (g < toend && w < it->truncation_pixel_width)
 23144             {
 23145               w += g->pixel_width;
 23146               ++g;
 23147             }
 23148           if (g - to - tused > 0)
 23149             {
 23150               memmove (to + tused, g, (toend - g) * sizeof(*g));
 23151               it->glyph_row->used[TEXT_AREA] -= g - to - tused;
 23152             }
 23153           used = it->glyph_row->used[TEXT_AREA];
 23154           if (it->glyph_row->truncated_on_right_p
 23155               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
 23156               && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
 23157               == STRETCH_GLYPH)
 23158             {
 23159               int extra = w - it->truncation_pixel_width;
 23160 
 23161               it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
 23162             }
 23163         }
 23164 
 23165       while (from < end)
 23166         *to++ = *from++;
 23167 
 23168       /* There may be padding glyphs left over.  Overwrite them too.  */
 23169       if (!FRAME_WINDOW_P (it->f))
 23170         {
 23171           while (to < toend && CHAR_GLYPH_PADDING_P (*to))
 23172             {
 23173               from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23174               while (from < end)
 23175                 *to++ = *from++;
 23176             }
 23177         }
 23178 
 23179       if (to > toend)
 23180         it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
 23181     }
 23182   else
 23183     {
 23184       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 23185 
 23186       /* In R2L rows, overwrite the last (rightmost) glyphs, and do
 23187          that back to front.  */
 23188       end = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23189       from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23190       toend = it->glyph_row->glyphs[TEXT_AREA];
 23191       to = toend + it->glyph_row->used[TEXT_AREA] - 1;
 23192       if (FRAME_WINDOW_P (it->f))
 23193         {
 23194           int w = 0;
 23195           struct glyph *g = to;
 23196 
 23197           while (g >= toend && w < it->truncation_pixel_width)
 23198             {
 23199               w += g->pixel_width;
 23200               --g;
 23201             }
 23202           if (to - g - tused > 0)
 23203             to = g + tused;
 23204           if (it->glyph_row->truncated_on_right_p
 23205               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
 23206               && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
 23207             {
 23208               int extra = w - it->truncation_pixel_width;
 23209 
 23210               it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
 23211             }
 23212         }
 23213 
 23214       while (from >= end && to >= toend)
 23215         *to-- = *from--;
 23216       if (!FRAME_WINDOW_P (it->f))
 23217         {
 23218           while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
 23219             {
 23220               from =
 23221                 truncate_it.glyph_row->glyphs[TEXT_AREA]
 23222                 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23223               while (from >= end && to >= toend)
 23224                 *to-- = *from--;
 23225             }
 23226         }
 23227       if (from >= end)
 23228         {
 23229           /* Need to free some room before prepending additional
 23230              glyphs.  */
 23231           int move_by = from - end + 1;
 23232           struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
 23233           struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
 23234 
 23235           for ( ; g >= g0; g--)
 23236             g[move_by] = *g;
 23237           while (from >= end)
 23238             *to-- = *from--;
 23239           it->glyph_row->used[TEXT_AREA] += move_by;
 23240         }
 23241     }
 23242 }
 23243 
 23244 /* Compute the hash code for ROW.  */
 23245 unsigned
 23246 row_hash (struct glyph_row *row)
 23247 {
 23248   int area, k;
 23249   unsigned hashval = 0;
 23250 
 23251   for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 23252     for (k = 0; k < row->used[area]; ++k)
 23253       hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
 23254                   + row->glyphs[area][k].u.val
 23255                   + row->glyphs[area][k].face_id
 23256                   + row->glyphs[area][k].padding_p
 23257                   + (row->glyphs[area][k].type << 2));
 23258 
 23259   return hashval;
 23260 }
 23261 
 23262 /* Compute the pixel height and width of IT->glyph_row.
 23263 
 23264    Most of the time, ascent and height of a display line will be equal
 23265    to the max_ascent and max_height values of the display iterator
 23266    structure.  This is not the case if
 23267 
 23268    1. We hit ZV without displaying anything.  In this case, max_ascent
 23269    and max_height will be zero.
 23270 
 23271    2. We have some glyphs that don't contribute to the line height.
 23272    (The glyph row flag contributes_to_line_height_p is for future
 23273    pixmap extensions).
 23274 
 23275    The first case is easily covered by using default values because in
 23276    these cases, the line height does not really matter, except that it
 23277    must not be zero.  */
 23278 
 23279 static void
 23280 compute_line_metrics (struct it *it)
 23281 {
 23282   struct glyph_row *row = it->glyph_row;
 23283 
 23284   if (FRAME_WINDOW_P (it->f))
 23285     {
 23286       int i, min_y, max_y;
 23287 
 23288       /* The line may consist of one space only, that was added to
 23289          place the cursor on it.  If so, the row's height hasn't been
 23290          computed yet.  */
 23291       if (row->height == 0)
 23292         {
 23293           if (it->max_ascent + it->max_descent == 0)
 23294             it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
 23295           row->ascent = it->max_ascent;
 23296           row->height = it->max_ascent + it->max_descent;
 23297           row->phys_ascent = it->max_phys_ascent;
 23298           row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 23299           row->extra_line_spacing = it->max_extra_line_spacing;
 23300         }
 23301 
 23302       /* Compute the width of this line.  */
 23303       row->pixel_width = row->x;
 23304       for (i = 0; i < row->used[TEXT_AREA]; ++i)
 23305         row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
 23306 
 23307       eassert (row->pixel_width >= 0);
 23308       eassert (row->ascent >= 0 && row->height > 0);
 23309 
 23310       row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
 23311                             || MATRIX_ROW_OVERLAPS_PRED_P (row));
 23312 
 23313       /* If first line's physical ascent is larger than its logical
 23314          ascent, use the physical ascent, and make the row taller.
 23315          This makes accented characters fully visible.  */
 23316       if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
 23317           && row->phys_ascent > row->ascent)
 23318         {
 23319           row->height += row->phys_ascent - row->ascent;
 23320           row->ascent = row->phys_ascent;
 23321         }
 23322 
 23323       /* Compute how much of the line is visible.  */
 23324       row->visible_height = row->height;
 23325 
 23326       min_y = WINDOW_TAB_LINE_HEIGHT (it->w) + WINDOW_HEADER_LINE_HEIGHT (it->w);
 23327       max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
 23328 
 23329       if (row->y < min_y)
 23330         row->visible_height -= min_y - row->y;
 23331       if (row->y + row->height > max_y)
 23332         row->visible_height -= row->y + row->height - max_y;
 23333     }
 23334   else
 23335     {
 23336       row->pixel_width = row->used[TEXT_AREA];
 23337       if (row->continued_p)
 23338         row->pixel_width -= it->continuation_pixel_width;
 23339       else if (row->truncated_on_right_p)
 23340         row->pixel_width -= it->truncation_pixel_width;
 23341       row->ascent = row->phys_ascent = 0;
 23342       row->height = row->phys_height = row->visible_height = 1;
 23343       row->extra_line_spacing = 0;
 23344     }
 23345 
 23346   /* Compute a hash code for this row.  */
 23347   row->hash = row_hash (row);
 23348 
 23349   it->max_ascent = it->max_descent = 0;
 23350   it->max_phys_ascent = it->max_phys_descent = 0;
 23351 }
 23352 
 23353 
 23354 static void
 23355 clear_position (struct it *it)
 23356 {
 23357   it->position.charpos = 0;
 23358   it->position.bytepos = 0;
 23359 }
 23360 
 23361 /* Append one space to the glyph row of iterator IT if doing a
 23362    window-based redisplay.  The space has the same face as
 23363    IT->face_id.  Value is true if a space was added.
 23364 
 23365    This function is called to make sure that there is always one glyph
 23366    at the end of a glyph row that the cursor can be set on under
 23367    window-systems.  (If there weren't such a glyph we would not know
 23368    how wide and tall a box cursor should be displayed).
 23369 
 23370    At the same time this space let's a nicely handle clearing to the
 23371    end of the line if the row ends in italic text.  */
 23372 
 23373 static bool
 23374 append_space_for_newline (struct it *it, bool default_face_p)
 23375 {
 23376   int n = it->glyph_row->used[TEXT_AREA];
 23377 
 23378   if (it->glyph_row->glyphs[TEXT_AREA] + n
 23379       < it->glyph_row->glyphs[1 + TEXT_AREA])
 23380     {
 23381       /* Save some values that must not be changed.
 23382          Must save IT->c and IT->len because otherwise
 23383          ITERATOR_AT_END_P wouldn't work anymore after
 23384          append_space_for_newline has been called.  */
 23385       enum display_element_type saved_what = it->what;
 23386       int saved_c = it->c, saved_len = it->len;
 23387       int saved_char_to_display = it->char_to_display;
 23388       int saved_x = it->current_x;
 23389       const int saved_face_id = it->face_id;
 23390       bool saved_box_end = it->end_of_box_run_p;
 23391       struct text_pos saved_pos = it->position;
 23392       Lisp_Object saved_object = it->object;
 23393       struct face *face;
 23394 
 23395       it->what = IT_CHARACTER;
 23396       clear_position (it);
 23397       it->object = Qnil;
 23398       it->len = 1;
 23399 
 23400       int char_width = 1;
 23401 
 23402       if (default_face_p
 23403 #ifdef HAVE_WINDOW_SYSTEM
 23404           || FRAME_WINDOW_P (it->f)
 23405 #endif
 23406           )
 23407         {
 23408           const int local_default_face_id =
 23409             lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
 23410 
 23411 #ifdef HAVE_WINDOW_SYSTEM
 23412           if (FRAME_WINDOW_P (it->f))
 23413             {
 23414               struct face *default_face
 23415                 = FACE_FROM_ID (it->f, local_default_face_id);
 23416               struct font *font = (default_face->font
 23417                                    ? default_face->font
 23418                                    : FRAME_FONT (it->f));
 23419               char_width = (font->average_width
 23420                             ? font->average_width
 23421                             : font->space_width);
 23422             }
 23423 #endif
 23424           if (default_face_p)
 23425             it->face_id = local_default_face_id;
 23426         }
 23427       /* Corner case for when display-fill-column-indicator-mode
 23428          is active and the extra character should be added in the
 23429          same place than the line.  */
 23430 
 23431       const int indicator_column =
 23432         fill_column_indicator_column (it, char_width);
 23433       int saved_end_of_box_run = it->end_of_box_run_p;
 23434       bool should_keep_end_of_box_run = false;
 23435 
 23436       if (it->current_x == indicator_column)
 23437         {
 23438           it->c = it->char_to_display
 23439             = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23440           it->face_id
 23441             = merge_faces (it->w, Qfill_column_indicator,
 23442                            0, saved_face_id);
 23443           face = FACE_FROM_ID (it->f, it->face_id);
 23444         }
 23445       else
 23446         {
 23447           it->c = it->char_to_display = ' ';
 23448           /* If the default face was remapped, be sure to use the
 23449              remapped face for the appended newline.  */
 23450 
 23451           face = FACE_FROM_ID (it->f, it->face_id);
 23452           it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
 23453           /* In R2L rows, we will prepend a stretch glyph that will
 23454              have the end_of_box_run_p flag set for it, so there's no
 23455              need for the appended newline glyph to have that flag
 23456              set.  */
 23457           if (!(it->glyph_row->reversed_p
 23458                 /* But if the appended newline glyph goes all the way to
 23459                    the end of the row, there will be no stretch glyph,
 23460                    so leave the box flag set.  */
 23461                 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x))
 23462             should_keep_end_of_box_run = true;
 23463         }
 23464       PRODUCE_GLYPHS (it);
 23465       /* Restore the end_of_box_run_p flag which was reset by
 23466          PRODUCE_GLYPHS.  */
 23467       if (should_keep_end_of_box_run)
 23468         it->end_of_box_run_p = saved_end_of_box_run;
 23469 #ifdef HAVE_WINDOW_SYSTEM
 23470       if (FRAME_WINDOW_P (it->f))
 23471         {
 23472           /* Make sure this space glyph has the right ascent and
 23473              descent values, or else cursor at end of line will look
 23474              funny, and height of empty lines will be incorrect.  */
 23475           struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
 23476           struct font *font = face->font ? face->font : FRAME_FONT (it->f);
 23477           if (n == 0)
 23478             {
 23479               Lisp_Object height, total_height;
 23480               int extra_line_spacing = it->extra_line_spacing;
 23481               int boff = font->baseline_offset;
 23482 
 23483               if (font->vertical_centering)
 23484                 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 23485 
 23486               it->object = saved_object; /* get_it_property needs this */
 23487               normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 23488               /* Must do a subset of line height processing from
 23489                  gui_produce_glyph for newline characters.  */
 23490               height = get_it_property (it, Qline_height);
 23491               if (CONSP (height)
 23492                   && CONSP (XCDR (height))
 23493                   && NILP (XCDR (XCDR (height))))
 23494                 {
 23495                   total_height = XCAR (XCDR (height));
 23496                   height = XCAR (height);
 23497                 }
 23498               else
 23499                 total_height = Qnil;
 23500               height = calc_line_height_property (it, height, font, boff, true);
 23501 
 23502               if (it->override_ascent >= 0)
 23503                 {
 23504                   it->ascent = it->override_ascent;
 23505                   it->descent = it->override_descent;
 23506                   boff = it->override_boff;
 23507                 }
 23508               if (EQ (height, Qt))
 23509                 extra_line_spacing = 0;
 23510               else
 23511                 {
 23512                   Lisp_Object spacing;
 23513 
 23514                   it->phys_ascent = it->ascent;
 23515                   it->phys_descent = it->descent;
 23516                   if (!NILP (height)
 23517                       && XFIXNUM (height) > it->ascent + it->descent)
 23518                     it->ascent = XFIXNUM (height) - it->descent;
 23519 
 23520                   if (!NILP (total_height))
 23521                     spacing = calc_line_height_property (it, total_height, font,
 23522                                                          boff, false);
 23523                   else
 23524                     {
 23525                       spacing = get_it_property (it, Qline_spacing);
 23526                       spacing = calc_line_height_property (it, spacing, font,
 23527                                                            boff, false);
 23528                     }
 23529                   if (FIXNUMP (spacing))
 23530                     {
 23531                       extra_line_spacing = XFIXNUM (spacing);
 23532                       if (!NILP (total_height))
 23533                         extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 23534                     }
 23535                 }
 23536               if (extra_line_spacing > 0)
 23537                 {
 23538                   it->descent += extra_line_spacing;
 23539                   if (extra_line_spacing > it->max_extra_line_spacing)
 23540                     it->max_extra_line_spacing = extra_line_spacing;
 23541                 }
 23542               it->max_ascent = it->ascent;
 23543               it->max_descent = it->descent;
 23544               /* Make sure compute_line_metrics recomputes the row height.  */
 23545               it->glyph_row->height = 0;
 23546             }
 23547 
 23548           g->ascent = it->max_ascent;
 23549           g->descent = it->max_descent;
 23550         }
 23551 #endif /* HAVE_WINDOW_SYSTEM  */
 23552       it->override_ascent = -1;
 23553       it->constrain_row_ascent_descent_p = false;
 23554       it->current_x = saved_x;
 23555       it->object = saved_object;
 23556       it->position = saved_pos;
 23557       it->what = saved_what;
 23558       it->face_id = saved_face_id;
 23559       it->len = saved_len;
 23560       it->c = saved_c;
 23561       it->char_to_display = saved_char_to_display;
 23562       it->end_of_box_run_p = saved_box_end;
 23563       return true;
 23564     }
 23565 
 23566   return false;
 23567 }
 23568 
 23569 
 23570 /* Extend the face of the last glyph in the text area of IT->glyph_row
 23571    to the end of the display line.  Called from display_line.  If the
 23572    glyph row is empty, add a space glyph to it so that we know the
 23573    face to draw.  Set the glyph row flag fill_line_p.  If the glyph
 23574    row is R2L, prepend a stretch glyph to cover the empty space to the
 23575    left of the leftmost glyph.  */
 23576 
 23577 static void
 23578 extend_face_to_end_of_line (struct it *it)
 23579 {
 23580   struct frame *f = it->f;
 23581 
 23582   /* If line is already filled, do nothing.  Non window-system frames
 23583      get a grace of one more ``pixel'' because their characters are
 23584      1-``pixel'' wide, so they hit the equality too early.  This grace
 23585      is needed only for R2L rows that are not continued, to produce
 23586      one extra blank where we could display the cursor.  */
 23587   if ((it->current_x >= it->last_visible_x
 23588        + (!FRAME_WINDOW_P (f)
 23589           && it->glyph_row->reversed_p
 23590           && !it->glyph_row->continued_p))
 23591       /* If the window has display margins, we will need to extend
 23592          their face even if the text area is filled.  */
 23593       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23594            || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
 23595     return;
 23596 
 23597   specpdl_ref count = SPECPDL_INDEX ();
 23598 
 23599   /* Don't allow the user to quit out of face-merging code, in case
 23600      this is called when redisplaying a non-selected window, with
 23601      point temporarily moved to window-point.  */
 23602   specbind (Qinhibit_quit, Qt);
 23603   /* The default face, possibly remapped. */
 23604   struct face *default_face =
 23605     FACE_FROM_ID_OR_NULL (f, lookup_basic_face (it->w, f, DEFAULT_FACE_ID));
 23606   if (!default_face)
 23607     return;
 23608 
 23609   const int extend_face_id =
 23610     (it->face_id == default_face->id || it->s != NULL)
 23611     ? it->face_id
 23612     : (it->glyph_row->ends_at_zv_p
 23613        ? default_face->id
 23614        : face_at_pos (it, LFACE_EXTEND_INDEX));
 23615   unbind_to (count, Qnil);
 23616 
 23617   /* Face extension extends the background and box of IT->extend_face_id
 23618      to the end of the line.  If the background equals the background
 23619      of the frame, we don't have to do anything.  */
 23620   struct face *face = FACE_FROM_ID (f, (it->face_before_selective_p
 23621                                         ? it->saved_face_id
 23622                                         : extend_face_id));
 23623 
 23624   if (FRAME_WINDOW_P (f)
 23625       && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
 23626       && face->box == FACE_NO_BOX
 23627       && face->underline == FACE_NO_UNDERLINE
 23628       && !face->overline_p
 23629       && !face->strike_through_p
 23630       && face->background == FRAME_BACKGROUND_PIXEL (f)
 23631 #ifdef HAVE_WINDOW_SYSTEM
 23632       && !face->stipple
 23633 #endif
 23634       && !it->glyph_row->reversed_p
 23635       && !display_fill_column_indicator)
 23636     return;
 23637 
 23638   /* Set the glyph row flag indicating that the face of the last glyph
 23639      in the text area has to be drawn to the end of the text area.  */
 23640   it->glyph_row->fill_line_p = true;
 23641 
 23642   const int orig_face_id = it->face_id;
 23643   /* If current character of IT is not ASCII, make sure we have the
 23644      ASCII face.  This will be automatically undone the next time
 23645      get_next_display_element returns a multibyte character.  Note
 23646      that the character will always be single byte in unibyte
 23647      text.  */
 23648   if (!ASCII_CHAR_P (it->c))
 23649     it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
 23650 
 23651 
 23652 #ifdef HAVE_WINDOW_SYSTEM
 23653   if (FRAME_WINDOW_P (f))
 23654     {
 23655       /* If the row is empty, add a space with the current face of IT,
 23656          so that we know which face to draw.  */
 23657       if (it->glyph_row->used[TEXT_AREA] == 0)
 23658         {
 23659           it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
 23660           it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
 23661           it->glyph_row->used[TEXT_AREA] = 1;
 23662         }
 23663       /* Mode line and the header line don't have margins, and
 23664          likewise the frame's tool-bar window, if there is any.  */
 23665       if (!(it->glyph_row->mode_line_p
 23666             || (WINDOWP (f->tab_bar_window)
 23667                 && it->w == XWINDOW (f->tab_bar_window))
 23668 #ifndef HAVE_EXT_TOOL_BAR
 23669             || (WINDOWP (f->tool_bar_window)
 23670                 && it->w == XWINDOW (f->tool_bar_window))
 23671 #endif
 23672             ))
 23673         {
 23674           if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23675               && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
 23676             {
 23677               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 23678               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
 23679                 default_face->id;
 23680               it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 23681             }
 23682           if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23683               && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
 23684             {
 23685               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 23686               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
 23687                 default_face->id;
 23688               it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 23689             }
 23690 
 23691           struct font *font = (default_face->font
 23692                                ? default_face->font
 23693                                : FRAME_FONT (f));
 23694 
 23695           const int char_width = (font->average_width
 23696                                   ? font->average_width
 23697                                   : font->space_width);
 23698 
 23699           const int indicator_column =
 23700             fill_column_indicator_column (it, char_width);
 23701 
 23702           const char saved_char = it->char_to_display;
 23703           const struct text_pos saved_pos = it->position;
 23704           const bool saved_avoid_cursor = it->avoid_cursor_p;
 23705           const bool saved_box_start = it->start_of_box_run_p;
 23706           Lisp_Object save_object = it->object;
 23707           const int saved_face_id = it->face_id;
 23708 
 23709           it->face_id = extend_face_id;
 23710           it->avoid_cursor_p = true;
 23711           it->object = Qnil;
 23712 
 23713           const int stretch_height = it->ascent + it->descent;
 23714           const int stretch_ascent =
 23715             (stretch_height * FONT_BASE (font)) / FONT_HEIGHT (font);
 23716 
 23717           if (indicator_column >= 0
 23718               && indicator_column > it->current_x
 23719               && indicator_column < it->last_visible_x)
 23720             {
 23721 
 23722               /* Here we subtract char_width because we want the
 23723                  column indicator in the column INDICATOR_COLUMN,
 23724                  not after it.  */
 23725               const int stretch_width =
 23726                 indicator_column - it->current_x - char_width;
 23727 
 23728               clear_position (it);
 23729 
 23730               /* Only generate a stretch glyph if there is distance
 23731                  between current_x and the indicator position.  */
 23732               if (stretch_width > 0)
 23733                 {
 23734                   append_stretch_glyph (it, Qnil, stretch_width,
 23735                                         stretch_height, stretch_ascent);
 23736                 }
 23737 
 23738               /* Generate the glyph indicator only if
 23739                  append_space_for_newline didn't already.  */
 23740               if (it->current_x < indicator_column)
 23741                 {
 23742                   const int save_face_id = it->face_id;
 23743                   const int save_ascent = it->ascent;
 23744                   const int save_descent = it->descent;
 23745                   it->char_to_display
 23746                     = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23747                   it->face_id
 23748                     = merge_faces (it->w, Qfill_column_indicator,
 23749                                    0, extend_face_id);
 23750                   PRODUCE_GLYPHS (it);
 23751                   it->face_id = save_face_id;
 23752                   it->ascent = save_ascent;
 23753                   it->descent = save_descent;
 23754                 }
 23755             }
 23756 
 23757           /* Fill space until window edge with the merged face.  Do that
 23758              only for L2R rows, as R2L rows are handled specially below.  */
 23759           if (!it->glyph_row->reversed_p)
 23760             {
 23761               const int stretch_width = it->last_visible_x - it->current_x;
 23762 
 23763               if (stretch_width > 0)
 23764                 {
 23765                   clear_position (it);
 23766                   append_stretch_glyph (it, Qnil, stretch_width,
 23767                                         stretch_height, stretch_ascent);
 23768                 }
 23769             }
 23770 
 23771           it->char_to_display = saved_char;
 23772           it->position = saved_pos;
 23773           it->avoid_cursor_p = saved_avoid_cursor;
 23774           it->start_of_box_run_p = saved_box_start;
 23775           it->object = save_object;
 23776           it->face_id = saved_face_id;
 23777         }
 23778       if (it->glyph_row->reversed_p)
 23779         {
 23780           /* Prepend a stretch glyph to the row, such that the
 23781              rightmost glyph will be drawn flushed all the way to the
 23782              right margin of the window.  The stretch glyph that will
 23783              occupy the empty space, if any, to the left of the
 23784              glyph.  */
 23785           struct font *font = face->font ? face->font : FRAME_FONT (f);
 23786           struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
 23787           struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
 23788           struct glyph *g;
 23789           int row_width, stretch_ascent, stretch_width;
 23790           struct text_pos saved_pos;
 23791           int saved_face_id;
 23792           bool saved_avoid_cursor, saved_box_start;
 23793 
 23794           for (row_width = 0, g = row_start; g < row_end; g++)
 23795             row_width += g->pixel_width;
 23796 
 23797           /* FIXME: There are various minor display glitches in R2L
 23798              rows when only one of the fringes is missing.  The
 23799              strange condition below produces the least bad effect.  */
 23800           if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23801               == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
 23802               || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
 23803             stretch_width = window_box_width (it->w, TEXT_AREA);
 23804           else
 23805             stretch_width = it->last_visible_x - it->first_visible_x;
 23806           stretch_width -= row_width;
 23807 
 23808           if (stretch_width > 0)
 23809             {
 23810               stretch_ascent =
 23811                 (((it->ascent + it->descent)
 23812                   * FONT_BASE (font)) / FONT_HEIGHT (font));
 23813               saved_pos = it->position;
 23814               clear_position (it);
 23815               saved_avoid_cursor = it->avoid_cursor_p;
 23816               it->avoid_cursor_p = true;
 23817               saved_face_id = it->face_id;
 23818               saved_box_start = it->start_of_box_run_p;
 23819               /* The last row's stretch glyph should get the default
 23820                  face, to avoid painting the rest of the window with
 23821                  the region face, if the region ends at ZV.  */
 23822               it->face_id = (it->glyph_row->ends_at_zv_p ?
 23823                              default_face->id : face->id);
 23824 
 23825               it->start_of_box_run_p = false;
 23826               append_stretch_glyph (it, Qnil, stretch_width,
 23827                                     it->ascent + it->descent, stretch_ascent);
 23828               it->position = saved_pos;
 23829               it->avoid_cursor_p = saved_avoid_cursor;
 23830               it->face_id = saved_face_id;
 23831               it->start_of_box_run_p = saved_box_start;
 23832             }
 23833           /* If stretch_width comes out negative, it means that the
 23834              last glyph is only partially visible.  In R2L rows, we
 23835              want the leftmost glyph to be partially visible, so we
 23836              need to give the row the corresponding left offset.  */
 23837           if (stretch_width < 0)
 23838             it->glyph_row->x = stretch_width;
 23839         }
 23840       it->face_id = orig_face_id;
 23841     }
 23842   else
 23843 #endif  /* HAVE_WINDOW_SYSTEM */
 23844     {
 23845       /* Save some values that must not be changed.  */
 23846       int saved_x = it->current_x;
 23847       struct text_pos saved_pos = it->position;
 23848       Lisp_Object saved_object = it->object;;
 23849       enum display_element_type saved_what = it->what;
 23850 
 23851       it->what = IT_CHARACTER;
 23852       clear_position (it);
 23853       it->object = Qnil;
 23854       it->c = it->char_to_display = ' ';
 23855       it->len = 1;
 23856 
 23857       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23858           && (it->glyph_row->used[LEFT_MARGIN_AREA]
 23859               < WINDOW_LEFT_MARGIN_WIDTH (it->w))
 23860           && !it->glyph_row->mode_line_p
 23861           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23862         {
 23863           struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
 23864           struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
 23865 
 23866           for (it->current_x = 0; g < e; g++)
 23867             it->current_x += g->pixel_width;
 23868 
 23869           it->area = LEFT_MARGIN_AREA;
 23870           it->face_id = default_face->id;
 23871           while (it->glyph_row->used[LEFT_MARGIN_AREA]
 23872                  < WINDOW_LEFT_MARGIN_WIDTH (it->w)
 23873                  && g < it->glyph_row->glyphs[TEXT_AREA])
 23874             {
 23875               PRODUCE_GLYPHS (it);
 23876               /* term.c:produce_glyphs advances it->current_x only for
 23877                  TEXT_AREA.  */
 23878               it->current_x += it->pixel_width;
 23879               g++;
 23880             }
 23881 
 23882           it->current_x = saved_x;
 23883           it->area = TEXT_AREA;
 23884         }
 23885 
 23886       /* The last row's blank glyphs should get the default face, to
 23887          avoid painting the rest of the window with the region face,
 23888          if the region ends at ZV.  */
 23889       it->face_id = (it->glyph_row->ends_at_zv_p ?
 23890                      default_face->id : face->id);
 23891 
 23892       /* Make sure our idea of current_x is in sync with the glyphs
 23893          actually in the glyph row.  They might differ because
 23894          append_space_for_newline can insert one glyph without
 23895          updating current_x.  */
 23896       it->current_x = it->glyph_row->used[TEXT_AREA];
 23897 
 23898       /* The above assignment causes the code below to use a
 23899          non-standard semantics of it->current_x: it is measured
 23900          relative to the beginning of the text-area, thus disregarding
 23901          the window's hscroll.  That is why we need to correct the
 23902          indicator column for the hscroll, otherwise the indicator
 23903          will not move together with the text as result of horizontal
 23904          scrolling.  */
 23905       const int indicator_column =
 23906         fill_column_indicator_column (it, 1) - it->first_visible_x;
 23907 
 23908       /* Display fill-column indicator if needed.  */
 23909       while (it->current_x <= it->last_visible_x)
 23910         {
 23911           if (it->current_x != indicator_column)
 23912             PRODUCE_GLYPHS (it);
 23913           else
 23914             {
 23915               int saved_face_id = it->face_id;
 23916               it->face_id
 23917                 = merge_faces (it->w, Qfill_column_indicator, 0, extend_face_id);
 23918               it->c = it->char_to_display
 23919                 = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23920 
 23921               PRODUCE_GLYPHS (it);
 23922 
 23923               it->face_id = saved_face_id;
 23924               it->c = it->char_to_display = ' ';
 23925             }
 23926         }
 23927 
 23928       if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23929           && (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23930               < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
 23931           && !it->glyph_row->mode_line_p
 23932           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23933         {
 23934           struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
 23935           struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
 23936 
 23937           for ( ; g < e; g++)
 23938             it->current_x += g->pixel_width;
 23939 
 23940           it->area = RIGHT_MARGIN_AREA;
 23941           it->face_id = default_face->id;
 23942           while (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23943                  < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
 23944                  && g < it->glyph_row->glyphs[LAST_AREA])
 23945             {
 23946               PRODUCE_GLYPHS (it);
 23947               it->current_x += it->pixel_width;
 23948               g++;
 23949             }
 23950 
 23951           it->area = TEXT_AREA;
 23952         }
 23953 
 23954       /* Don't count these blanks really.  It would let us insert a left
 23955          truncation glyph below and make us set the cursor on them, maybe.  */
 23956       it->current_x = saved_x;
 23957       it->object = saved_object;
 23958       it->position = saved_pos;
 23959       it->what = saved_what;
 23960       it->face_id = orig_face_id;
 23961     }
 23962 }
 23963 
 23964 
 23965 /* Value is true if text starting at CHARPOS in current_buffer is
 23966    trailing whitespace.  */
 23967 
 23968 static bool
 23969 trailing_whitespace_p (ptrdiff_t charpos)
 23970 {
 23971   ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
 23972   int c = 0;
 23973 
 23974   while (bytepos < ZV_BYTE
 23975          && (c = FETCH_BYTE (bytepos),
 23976              c == ' ' || c == '\t'))
 23977     ++bytepos;
 23978 
 23979   if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
 23980     {
 23981       if (bytepos != PT_BYTE)
 23982         return true;
 23983     }
 23984   return false;
 23985 }
 23986 
 23987 
 23988 /* Highlight trailing whitespace, if any, in row at IT.  */
 23989 
 23990 static void
 23991 highlight_trailing_whitespace (struct it *it)
 23992 {
 23993   struct glyph_row *row = it->glyph_row;
 23994   int used = row->used[TEXT_AREA];
 23995 
 23996   if (used)
 23997     {
 23998       struct glyph *start = row->glyphs[TEXT_AREA];
 23999       struct glyph *glyph = start + used - 1;
 24000 
 24001       if (row->reversed_p)
 24002         {
 24003           /* Right-to-left rows need to be processed in the opposite
 24004              direction, so swap the edge pointers. */
 24005           glyph = start;
 24006           start = row->glyphs[TEXT_AREA] + used - 1;
 24007         }
 24008 
 24009       /* Skip over glyphs inserted to display the cursor at the
 24010          end of a line, for extending the face of the last glyph
 24011          to the end of the line on terminals, and for truncation
 24012          and continuation glyphs.  */
 24013       if (!row->reversed_p)
 24014         {
 24015           while (glyph >= start
 24016                  && (glyph->type == CHAR_GLYPH
 24017                      || glyph->type == STRETCH_GLYPH)
 24018                  && NILP (glyph->object))
 24019             --glyph;
 24020         }
 24021       else
 24022         {
 24023           while (glyph <= start
 24024                  && (glyph->type == CHAR_GLYPH || glyph->type == STRETCH_GLYPH)
 24025                  && NILP (glyph->object))
 24026             ++glyph;
 24027         }
 24028 
 24029       /* If last glyph is a space or stretch, and it's trailing
 24030          whitespace, set the face of all trailing whitespace glyphs in
 24031          IT->glyph_row to `trailing-whitespace'.  */
 24032       if ((row->reversed_p ? glyph <= start : glyph >= start)
 24033           && BUFFERP (glyph->object)
 24034           && (glyph->type == STRETCH_GLYPH
 24035               || (glyph->type == CHAR_GLYPH
 24036                   && glyph->u.ch == ' '))
 24037           && trailing_whitespace_p (glyph->charpos))
 24038         {
 24039           int face_id = lookup_named_face (it->w, it->f, Qtrailing_whitespace, false);
 24040           if (face_id < 0)
 24041             return;
 24042 
 24043           if (!row->reversed_p)
 24044             {
 24045               while (glyph >= start
 24046                      && BUFFERP (glyph->object)
 24047                      && (glyph->type == STRETCH_GLYPH
 24048                          || (glyph->type == CHAR_GLYPH
 24049                              && glyph->u.ch == ' ')))
 24050                 (glyph--)->face_id = face_id;
 24051             }
 24052           else
 24053             {
 24054               while (glyph <= start
 24055                      && BUFFERP (glyph->object)
 24056                      && (glyph->type == STRETCH_GLYPH
 24057                          || (glyph->type == CHAR_GLYPH
 24058                              && glyph->u.ch == ' ')))
 24059                 (glyph++)->face_id = face_id;
 24060             }
 24061         }
 24062     }
 24063 }
 24064 
 24065 
 24066 /* Value is true if glyph row ROW should be
 24067    considered to hold the buffer position CHARPOS.  */
 24068 
 24069 static bool
 24070 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
 24071 {
 24072   bool result = true;
 24073 
 24074   if (charpos == CHARPOS (row->end.pos)
 24075       || charpos == MATRIX_ROW_END_CHARPOS (row))
 24076     {
 24077       /* Suppose the row ends on a string.
 24078          Unless the row is continued, that means it ends on a newline
 24079          in the string.  If it's anything other than a display string
 24080          (e.g., a before-string from an overlay), we don't want the
 24081          cursor there.  (This heuristic seems to give the optimal
 24082          behavior for the various types of multi-line strings.)
 24083          One exception: if the string has `cursor' property on one of
 24084          its characters, we _do_ want the cursor there.  */
 24085       if (CHARPOS (row->end.string_pos) >= 0)
 24086         {
 24087           if (row->continued_p)
 24088             result = true;
 24089           else
 24090             {
 24091               /* Check for `display' property.  */
 24092               struct glyph *beg = row->glyphs[TEXT_AREA];
 24093               struct glyph *end = beg + row->used[TEXT_AREA] - 1;
 24094               struct glyph *glyph;
 24095 
 24096               result = false;
 24097               for (glyph = end; glyph >= beg; --glyph)
 24098                 if (STRINGP (glyph->object))
 24099                   {
 24100                     Lisp_Object prop
 24101                       = Fget_char_property (make_fixnum (charpos),
 24102                                             Qdisplay, Qnil);
 24103                     result =
 24104                       (!NILP (prop)
 24105                        && display_prop_string_p (prop, glyph->object));
 24106                     /* If there's a `cursor' property on one of the
 24107                        string's characters, this row is a cursor row,
 24108                        even though this is not a display string.  */
 24109                     if (!result)
 24110                       {
 24111                         Lisp_Object s = glyph->object;
 24112 
 24113                         for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
 24114                           {
 24115                             ptrdiff_t gpos = glyph->charpos;
 24116 
 24117                             if (!NILP (Fget_char_property (make_fixnum (gpos),
 24118                                                            Qcursor, s)))
 24119                               {
 24120                                 result = true;
 24121                                 break;
 24122                               }
 24123                           }
 24124                       }
 24125                     break;
 24126                   }
 24127             }
 24128         }
 24129       else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 24130         {
 24131           /* If the row ends in middle of a real character,
 24132              and the line is continued, we want the cursor here.
 24133              That's because CHARPOS (ROW->end.pos) would equal
 24134              PT if PT is before the character.  */
 24135           if (!row->ends_in_ellipsis_p)
 24136             result = row->continued_p;
 24137           else
 24138           /* If the row ends in an ellipsis, then
 24139              CHARPOS (ROW->end.pos) will equal point after the
 24140              invisible text.  We want that position to be displayed
 24141              after the ellipsis.  */
 24142             result = false;
 24143         }
 24144       /* If the row ends at ZV, display the cursor at the end of that
 24145          row instead of at the start of the row below.  */
 24146       else
 24147         result = row->ends_at_zv_p;
 24148     }
 24149 
 24150   return result;
 24151 }
 24152 
 24153 /* Value is true if glyph row ROW should be
 24154    used to hold the cursor.  */
 24155 
 24156 static bool
 24157 cursor_row_p (struct glyph_row *row)
 24158 {
 24159   return row_for_charpos_p (row, PT);
 24160 }
 24161 
 24162 
 24163 
 24164 /* Push the property PROP so that it will be rendered at the current
 24165    position in IT.  Return true if PROP was successfully pushed, false
 24166    otherwise.  Called from handle_line_prefix to handle the
 24167    `line-prefix' and `wrap-prefix' properties.  */
 24168 
 24169 static bool
 24170 push_prefix_prop (struct it *it, Lisp_Object prop)
 24171 {
 24172   struct text_pos pos =
 24173     STRINGP (it->string) ? it->current.string_pos : it->current.pos;
 24174 
 24175   eassert (it->method == GET_FROM_BUFFER
 24176            || it->method == GET_FROM_DISPLAY_VECTOR
 24177            || it->method == GET_FROM_STRING
 24178            || it->method == GET_FROM_IMAGE);
 24179 
 24180   /* We need to save the current buffer/string position, so it will be
 24181      restored by pop_it, because iterate_out_of_display_property
 24182      depends on that being set correctly, but some situations leave
 24183      it->position not yet set when this function is called.  */
 24184   push_it (it, &pos);
 24185 
 24186   if (STRINGP (prop))
 24187     {
 24188       if (SCHARS (prop) == 0)
 24189         {
 24190           pop_it (it);
 24191           return false;
 24192         }
 24193 
 24194       it->string = prop;
 24195       it->string_from_prefix_prop_p = true;
 24196       it->multibyte_p = STRING_MULTIBYTE (it->string);
 24197       it->current.overlay_string_index = -1;
 24198       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
 24199       it->end_charpos = it->string_nchars = SCHARS (it->string);
 24200       it->method = GET_FROM_STRING;
 24201       it->stop_charpos = 0;
 24202       it->prev_stop = 0;
 24203       it->base_level_stop = 0;
 24204       it->cmp_it.id = -1;
 24205 
 24206       /* Force paragraph direction to be that of the parent
 24207          buffer/string.  */
 24208       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 24209         it->paragraph_embedding = it->bidi_it.paragraph_dir;
 24210       else
 24211         it->paragraph_embedding = L2R;
 24212 
 24213       /* Set up the bidi iterator for this display string.  */
 24214       if (it->bidi_p)
 24215         {
 24216           it->bidi_it.string.lstring = it->string;
 24217           it->bidi_it.string.s = NULL;
 24218           it->bidi_it.string.schars = it->end_charpos;
 24219           it->bidi_it.string.bufpos = IT_CHARPOS (*it);
 24220           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
 24221           it->bidi_it.string.unibyte = !it->multibyte_p;
 24222           it->bidi_it.w = it->w;
 24223           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
 24224         }
 24225     }
 24226   else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
 24227     {
 24228       it->method = GET_FROM_STRETCH;
 24229       it->object = prop;
 24230     }
 24231 #ifdef HAVE_WINDOW_SYSTEM
 24232   else if (IMAGEP (prop))
 24233     {
 24234       it->what = IT_IMAGE;
 24235       it->image_id = lookup_image (it->f, prop, it->face_id);
 24236       it->method = GET_FROM_IMAGE;
 24237     }
 24238 #endif /* HAVE_WINDOW_SYSTEM */
 24239   else
 24240     {
 24241       pop_it (it);              /* bogus display property, give up */
 24242       return false;
 24243     }
 24244 
 24245   return true;
 24246 }
 24247 
 24248 /* Return the character-property PROP at the current position in IT.  */
 24249 
 24250 static Lisp_Object
 24251 get_it_property (struct it *it, Lisp_Object prop)
 24252 {
 24253   Lisp_Object position, object = it->object;
 24254 
 24255   if (STRINGP (object))
 24256     position = make_fixnum (IT_STRING_CHARPOS (*it));
 24257   else if (BUFFERP (object))
 24258     {
 24259       position = make_fixnum (IT_CHARPOS (*it));
 24260       object = it->window;
 24261     }
 24262   else
 24263     return Qnil;
 24264 
 24265   return Fget_char_property (position, prop, object);
 24266 }
 24267 
 24268 /* Return the line-prefix/wrap-prefix property, checking both the
 24269    current IT->OBJECT and the underlying buffer text.  */
 24270 
 24271 static Lisp_Object
 24272 get_line_prefix_it_property (struct it *it, Lisp_Object prop)
 24273 {
 24274   Lisp_Object prefix = get_it_property (it, prop);
 24275 
 24276   /* If we are looking at a display or overlay string, check also the
 24277      underlying buffer text.  */
 24278   if (NILP (prefix) && it->sp > 0 && STRINGP (it->object))
 24279     return Fget_char_property (make_fixnum (IT_CHARPOS (*it)), prop,
 24280                                it->w->contents);
 24281   return prefix;
 24282 }
 24283 
 24284 /* See if there's a line- or wrap-prefix, and if so, push it on IT.  */
 24285 
 24286 static void
 24287 handle_line_prefix (struct it *it)
 24288 {
 24289   Lisp_Object prefix;
 24290 
 24291   if (it->continuation_lines_width > 0)
 24292     {
 24293       prefix = get_line_prefix_it_property (it, Qwrap_prefix);
 24294       if (NILP (prefix))
 24295         prefix = Vwrap_prefix;
 24296     }
 24297   else
 24298     {
 24299       prefix = get_line_prefix_it_property (it, Qline_prefix);
 24300       if (NILP (prefix))
 24301         prefix = Vline_prefix;
 24302     }
 24303   if (! NILP (prefix) && push_prefix_prop (it, prefix))
 24304     {
 24305       /* If the prefix is wider than the window, and we try to wrap
 24306          it, it would acquire its own wrap prefix, and so on till the
 24307          iterator stack overflows.  So, don't wrap the prefix.  */
 24308       it->line_wrap = TRUNCATE;
 24309       it->avoid_cursor_p = true;
 24310     }
 24311 }
 24312 
 24313 
 24314 
 24315 /* Remove N glyphs at the start of a reversed IT->glyph_row.  Called
 24316    only for R2L lines from display_line and display_string, when they
 24317    decide that too many glyphs were produced by PRODUCE_GLYPHS, and
 24318    the line/string needs to be continued on the next glyph row.  */
 24319 static void
 24320 unproduce_glyphs (struct it *it, int n)
 24321 {
 24322   struct glyph *glyph, *end;
 24323 
 24324   eassert (it->glyph_row);
 24325   eassert (it->glyph_row->reversed_p);
 24326   eassert (it->area == TEXT_AREA);
 24327   eassert (n <= it->glyph_row->used[TEXT_AREA]);
 24328 
 24329   if (n > it->glyph_row->used[TEXT_AREA])
 24330     n = it->glyph_row->used[TEXT_AREA];
 24331   glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
 24332   end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
 24333   for ( ; glyph < end; glyph++)
 24334     glyph[-n] = *glyph;
 24335 }
 24336 
 24337 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
 24338    and ROW->maxpos.  */
 24339 static void
 24340 find_row_edges (struct it *it, struct glyph_row *row,
 24341                 ptrdiff_t min_pos, ptrdiff_t min_bpos,
 24342                 ptrdiff_t max_pos, ptrdiff_t max_bpos)
 24343 {
 24344   /* FIXME: Revisit this when glyph ``spilling'' in continuation
 24345      lines' rows is implemented for bidi-reordered rows.  */
 24346 
 24347   /* ROW->minpos is the value of min_pos, the minimal buffer position
 24348      we have in ROW, or ROW->start.pos if that is smaller.  */
 24349   if (min_pos <= ZV && min_pos < row->start.pos.charpos)
 24350     SET_TEXT_POS (row->minpos, min_pos, min_bpos);
 24351   else
 24352     /* We didn't find buffer positions smaller than ROW->start, or
 24353        didn't find _any_ valid buffer positions in any of the glyphs,
 24354        so we must trust the iterator's computed positions.  */
 24355       row->minpos = row->start.pos;
 24356   if (max_pos <= 0)
 24357     {
 24358       max_pos = CHARPOS (it->current.pos);
 24359       max_bpos = BYTEPOS (it->current.pos);
 24360     }
 24361 
 24362   /* Here are the various use-cases for ending the row, and the
 24363      corresponding values for ROW->maxpos:
 24364 
 24365      Line ends in a newline from buffer       eol_pos + 1
 24366      Line is continued from buffer            max_pos + 1
 24367      Line is truncated on right               it->current.pos
 24368      Line ends in a newline from string       max_pos + 1(*)
 24369       (*) + 1 only when line ends in a forward scan
 24370      Line is continued from string            max_pos
 24371      Line is continued from display vector    max_pos
 24372      Line is entirely from a string           min_pos == max_pos
 24373      Line is entirely from a display vector   min_pos == max_pos
 24374      Line that ends at ZV                     ZV
 24375 
 24376      If you discover other use-cases, please add them here as
 24377      appropriate.  */
 24378   if (row->ends_at_zv_p)
 24379     row->maxpos = it->current.pos;
 24380   else if (row->used[TEXT_AREA])
 24381     {
 24382       bool seen_this_string = false;
 24383       struct glyph_row *r1 = row - 1;
 24384 
 24385       /* Did we see the same display string on the previous row?  */
 24386       if (STRINGP (it->object)
 24387           /* this is not the first row */
 24388           && row > it->w->desired_matrix->rows
 24389           /* previous row is not the header line or tab-line */
 24390           && !r1->mode_line_p
 24391           /* previous row also ends in a newline from a string */
 24392           && r1->ends_in_newline_from_string_p)
 24393         {
 24394           struct glyph *start, *end;
 24395 
 24396           /* Search for the last glyph of the previous row that came
 24397              from buffer or string.  Depending on whether the row is
 24398              L2R or R2L, we need to process it front to back or the
 24399              other way round.  */
 24400           if (!r1->reversed_p)
 24401             {
 24402               start = r1->glyphs[TEXT_AREA];
 24403               end = start + r1->used[TEXT_AREA];
 24404               /* Glyphs inserted by redisplay have nil as their object.  */
 24405               while (end > start
 24406                      && NILP ((end - 1)->object)
 24407                      && (end - 1)->charpos <= 0)
 24408                 --end;
 24409               if (end > start)
 24410                 {
 24411                   if (EQ ((end - 1)->object, it->object))
 24412                     seen_this_string = true;
 24413                 }
 24414               else
 24415                 /* If all the glyphs of the previous row were inserted
 24416                    by redisplay, it means the previous row was
 24417                    produced from a single newline, which is only
 24418                    possible if that newline came from the same string
 24419                    as the one which produced this ROW.  */
 24420                 seen_this_string = true;
 24421             }
 24422           else
 24423             {
 24424               end = r1->glyphs[TEXT_AREA] - 1;
 24425               start = end + r1->used[TEXT_AREA];
 24426               while (end < start
 24427                      && NILP ((end + 1)->object)
 24428                      && (end + 1)->charpos <= 0)
 24429                 ++end;
 24430               if (end < start)
 24431                 {
 24432                   if (EQ ((end + 1)->object, it->object))
 24433                     seen_this_string = true;
 24434                 }
 24435               else
 24436                 seen_this_string = true;
 24437             }
 24438         }
 24439       /* Take note of each display string that covers a newline only
 24440          once, the first time we see it.  This is for when a display
 24441          string includes more than one newline in it.  */
 24442       if (row->ends_in_newline_from_string_p && !seen_this_string)
 24443         {
 24444           /* If we were scanning the buffer forward when we displayed
 24445              the string, we want to account for at least one buffer
 24446              position that belongs to this row (position covered by
 24447              the display string), so that cursor positioning will
 24448              consider this row as a candidate when point is at the end
 24449              of the visual line represented by this row.  This is not
 24450              required when scanning back, because max_pos will already
 24451              have a much larger value.  */
 24452           if (CHARPOS (row->end.pos) > max_pos)
 24453             inc_both (&max_pos, &max_bpos);
 24454           SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24455         }
 24456       else if (CHARPOS (it->eol_pos) > 0)
 24457         SET_TEXT_POS (row->maxpos,
 24458                       CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
 24459       else if (row->continued_p)
 24460         {
 24461           /* If max_pos is different from IT's current position, it
 24462              means IT->method does not belong to the display element
 24463              at max_pos.  However, it also means that the display
 24464              element at max_pos was displayed in its entirety on this
 24465              line, which is equivalent to saying that the next line
 24466              starts at the next buffer position.  */
 24467           if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
 24468             SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24469           else
 24470             {
 24471               inc_both (&max_pos, &max_bpos);
 24472               SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24473             }
 24474         }
 24475       else if (row->truncated_on_right_p)
 24476         /* display_line already called reseat_at_next_visible_line_start,
 24477            which puts the iterator at the beginning of the next line, in
 24478            the logical order. */
 24479         row->maxpos = it->current.pos;
 24480       else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
 24481         /* A line that is entirely from a string/image/stretch...  */
 24482         row->maxpos = row->minpos;
 24483       else
 24484         emacs_abort ();
 24485     }
 24486   else
 24487     row->maxpos = it->current.pos;
 24488 }
 24489 
 24490 /* Like display_count_lines, but capable of counting outside of the
 24491    current narrowed region.  */
 24492 static ptrdiff_t
 24493 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
 24494                                ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
 24495 {
 24496   if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
 24497     return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24498 
 24499   ptrdiff_t val;
 24500   specpdl_ref pdl_count = SPECPDL_INDEX ();
 24501   record_unwind_protect (save_restriction_restore, save_restriction_save ());
 24502   labeled_restrictions_remove_in_current_buffer ();
 24503   Fwiden ();
 24504   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24505   unbind_to (pdl_count, Qnil);
 24506   return val;
 24507 }
 24508 
 24509 /* Count the number of screen lines in window IT->w between character
 24510    position IT_CHARPOS(*IT) and the line showing that window's point.  */
 24511 static ptrdiff_t
 24512 display_count_lines_visually (struct it *it)
 24513 {
 24514   struct it tem_it;
 24515   ptrdiff_t to;
 24516   struct text_pos from;
 24517 
 24518   /* If we already calculated a relative line number, use that.  This
 24519      trick relies on the fact that visual lines (a.k.a. "glyph rows")
 24520      are laid out sequentially, one by one, for each sequence of calls
 24521      to display_line or other similar function that follows a call to
 24522      init_iterator.  */
 24523   if (it->lnum_bytepos > 0)
 24524     return it->lnum + 1;
 24525   else
 24526     {
 24527       specpdl_ref count = SPECPDL_INDEX ();
 24528 
 24529       if (IT_CHARPOS (*it) <= PT)
 24530         {
 24531           from = it->current.pos;
 24532           to = PT;
 24533         }
 24534       else
 24535         {
 24536           SET_TEXT_POS (from, PT, PT_BYTE);
 24537           to = IT_CHARPOS (*it);
 24538         }
 24539       /* Need to disable visual mode temporarily, since otherwise the
 24540          call to move_it_to below and inside start_display will cause
 24541          infinite recursion.  */
 24542       specbind (Qdisplay_line_numbers, Qrelative);
 24543       start_display (&tem_it, it->w, from);
 24544       /* Some redisplay optimizations could invoke us very far from
 24545          PT, which will make the caller painfully slow.  There should
 24546          be no need to go too far beyond the window's bottom, as any
 24547          such optimization will fail to show point anyway.  */
 24548       move_it_to (&tem_it, to, -1,
 24549                   tem_it.last_visible_y
 24550                   + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
 24551                   -1, MOVE_TO_POS | MOVE_TO_Y);
 24552       unbind_to (count, Qnil);
 24553       return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
 24554     }
 24555 }
 24556 
 24557 /* Produce the line-number glyphs for the current glyph_row.  If
 24558    IT->glyph_row is non-NULL, populate the row with the produced
 24559    glyphs.  */
 24560 static void
 24561 maybe_produce_line_number (struct it *it)
 24562 {
 24563   ptrdiff_t last_line = it->lnum;
 24564   ptrdiff_t start_from, bytepos;
 24565   ptrdiff_t this_line;
 24566   bool first_time = false;
 24567   ptrdiff_t beg_byte;
 24568   ptrdiff_t z_byte;
 24569   bool line_numbers_wide;
 24570   void *itdata = bidi_shelve_cache ();
 24571 
 24572   if (display_line_numbers_offset
 24573       && !display_line_numbers_widen
 24574       && !EQ (Vdisplay_line_numbers, Qvisual)
 24575       && !EQ (Vdisplay_line_numbers, Qrelative))
 24576     line_numbers_wide = true;
 24577   else
 24578     line_numbers_wide = display_line_numbers_widen;
 24579 
 24580   beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
 24581   z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
 24582 
 24583   if (EQ (Vdisplay_line_numbers, Qvisual))
 24584     this_line = display_count_lines_visually (it);
 24585   else
 24586     {
 24587       if (!last_line)
 24588         {
 24589           /* If possible, reuse data cached by line-number-mode.  */
 24590           if (it->w->base_line_number > 0
 24591               && it->w->base_line_pos > 0
 24592               && it->w->base_line_pos <= IT_CHARPOS (*it)
 24593               /* line-number-mode always displays narrowed line
 24594                  numbers, so we cannot use its data if the user wants
 24595                  line numbers that disregard narrowing, or if the
 24596                  buffer's narrowing has just changed.  */
 24597               && !(line_numbers_wide
 24598                    && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 24599               && !current_buffer->clip_changed)
 24600             {
 24601               start_from = CHAR_TO_BYTE (it->w->base_line_pos);
 24602               last_line = it->w->base_line_number - 1;
 24603             }
 24604           else
 24605             start_from = beg_byte;
 24606           if (!it->lnum_bytepos)
 24607             first_time = true;
 24608         }
 24609       else
 24610         start_from = it->lnum_bytepos;
 24611 
 24612       /* Paranoia: what if someone changes the narrowing since the
 24613          last time display_line was called?  Shouldn't really happen,
 24614          but who knows what some crazy Lisp invoked by :eval could do?  */
 24615       if (!(beg_byte <= start_from && start_from <= z_byte))
 24616         {
 24617           last_line = 0;
 24618           start_from = beg_byte;
 24619         }
 24620 
 24621       this_line =
 24622         last_line + display_count_lines_logically (start_from,
 24623                                                    IT_BYTEPOS (*it),
 24624                                                    IT_CHARPOS (*it), &bytepos);
 24625       eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
 24626       eassert (bytepos == IT_BYTEPOS (*it));
 24627     }
 24628 
 24629   /* Record the line number information.  */
 24630   if (this_line != last_line || !it->lnum_bytepos)
 24631     {
 24632       it->lnum = this_line;
 24633       it->lnum_bytepos = IT_BYTEPOS (*it);
 24634     }
 24635 
 24636   /* Produce the glyphs for the line number.  */
 24637   struct it tem_it;
 24638   char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
 24639   bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
 24640   ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
 24641   int lnum_face_id = merge_faces (it->w, Qline_number, 0, DEFAULT_FACE_ID);
 24642   int current_lnum_face_id
 24643     = merge_faces (it->w, Qline_number_current_line, 0, DEFAULT_FACE_ID);
 24644   /* From here onwards, we must prevent freeing realized faces, because
 24645      we are using the above 2 face IDs for the glyphs we produce.  */
 24646   bool save_free_realized_faces = inhibit_free_realized_faces;
 24647   inhibit_free_realized_faces = true;
 24648   /* Compute point's line number if needed.  */
 24649   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24650        || EQ (Vdisplay_line_numbers, Qvisual)
 24651        || lnum_face_id != current_lnum_face_id)
 24652       && !it->pt_lnum)
 24653     {
 24654       ptrdiff_t ignored;
 24655       if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
 24656         it->pt_lnum =
 24657           this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
 24658                                                      PT, &ignored);
 24659       else
 24660         it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
 24661                                                      &ignored);
 24662     }
 24663   /* Compute the required width if needed.  */
 24664   if (!it->lnum_width)
 24665     {
 24666       if (FIXNATP (Vdisplay_line_numbers_width))
 24667         it->lnum_width = XFIXNAT (Vdisplay_line_numbers_width);
 24668 
 24669       /* Max line number to be displayed cannot be more than the one
 24670          corresponding to the last row of the desired matrix.  */
 24671       ptrdiff_t max_lnum;
 24672 
 24673       if (NILP (Vdisplay_line_numbers_current_absolute)
 24674           && (EQ (Vdisplay_line_numbers, Qrelative)
 24675               || EQ (Vdisplay_line_numbers, Qvisual)))
 24676         /* We subtract one more because the current line is always
 24677            zero in this mode.  */
 24678         max_lnum = it->w->desired_matrix->nrows - 2;
 24679       else if (EQ (Vdisplay_line_numbers, Qvisual))
 24680         max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
 24681       else
 24682         max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
 24683       max_lnum = max (1, max_lnum);
 24684       it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
 24685       eassert (it->lnum_width > 0);
 24686     }
 24687   if (EQ (Vdisplay_line_numbers, Qrelative))
 24688     lnum_offset = it->pt_lnum;
 24689   else if (EQ (Vdisplay_line_numbers, Qvisual))
 24690     lnum_offset = 0;
 24691   else if (display_line_numbers_offset)
 24692     lnum_offset -= display_line_numbers_offset;
 24693 
 24694   /* Under 'relative', display the absolute line number for the
 24695      current line, unless the user requests otherwise.  */
 24696   ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
 24697   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24698        || EQ (Vdisplay_line_numbers, Qvisual))
 24699       && lnum_to_display == 0
 24700       && !NILP (Vdisplay_line_numbers_current_absolute))
 24701     lnum_to_display = it->pt_lnum + 1;
 24702   /* In L2R rows we need to append the blank separator, in R2L
 24703      rows we need to prepend it.  But this function is usually
 24704      called when no display elements were produced from the
 24705      following line, so the paragraph direction might be unknown.
 24706      Therefore we cheat and add 2 blanks, one on either side.  */
 24707   pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
 24708   strcat (lnum_buf, " ");
 24709 
 24710   /* Setup for producing the glyphs.  */
 24711   init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
 24712                  /* FIXME: Use specialized face.  */
 24713                  DEFAULT_FACE_ID);
 24714   scratch_glyph_row.reversed_p = false;
 24715   scratch_glyph_row.used[TEXT_AREA] = 0;
 24716   SET_TEXT_POS (tem_it.position, 0, 0);
 24717   tem_it.avoid_cursor_p = true;
 24718   tem_it.bidi_p = true;
 24719   tem_it.bidi_it.type = WEAK_EN;
 24720   /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
 24721      1 level in R2L paragraphs.  Emulate that, assuming we are in
 24722      an L2R paragraph.  */
 24723   tem_it.bidi_it.resolved_level = 2;
 24724 
 24725   /* We must leave space for 2 glyphs for continuation and truncation,
 24726      and at least one glyph for buffer text.  */
 24727   int width_limit =
 24728     tem_it.last_visible_x - tem_it.first_visible_x
 24729     - 3 * FRAME_COLUMN_WIDTH (it->f);
 24730 
 24731   tem_it.face_id = lnum_face_id;
 24732   /* Avoid displaying any face other than line-number on
 24733      empty lines beyond EOB.  */
 24734   if (lnum_face_id != current_lnum_face_id
 24735       && (EQ (Vdisplay_line_numbers, Qvisual)
 24736           ? this_line == 0
 24737           : this_line == it->pt_lnum)
 24738       && it->what != IT_EOB)
 24739     tem_it.face_id = current_lnum_face_id;
 24740   else if (!beyond_zv)
 24741     {
 24742       if (display_line_numbers_major_tick > 0
 24743           && (lnum_to_display % display_line_numbers_major_tick == 0))
 24744         tem_it.face_id = merge_faces (it->w, Qline_number_major_tick,
 24745                                       0, DEFAULT_FACE_ID);
 24746       else if (display_line_numbers_minor_tick > 0
 24747                && (lnum_to_display % display_line_numbers_minor_tick == 0))
 24748         tem_it.face_id = merge_faces (it->w, Qline_number_minor_tick,
 24749                                       0, DEFAULT_FACE_ID);
 24750     }
 24751 
 24752   /* Produce glyphs for the line number in a scratch glyph_row.  */
 24753   for (const char *p = lnum_buf; *p; p++)
 24754     {
 24755       /* For continuation lines and lines after ZV, instead of a line
 24756          number, produce a blank prefix of the same width.  */
 24757       if (beyond_zv
 24758           /* Don't display the same line number more than once.  */
 24759           || (!EQ (Vdisplay_line_numbers, Qvisual)
 24760               && (it->continuation_lines_width > 0
 24761                   || (this_line == last_line && !first_time))))
 24762         tem_it.c = tem_it.char_to_display = ' ';
 24763       else
 24764         tem_it.c = tem_it.char_to_display = *p;
 24765       tem_it.len = 1;
 24766       /* Make sure these glyphs will have a "position" of -1.  */
 24767       SET_TEXT_POS (tem_it.position, -1, -1);
 24768       PRODUCE_GLYPHS (&tem_it);
 24769 
 24770       /* Stop producing glyphs, and refrain from producing the line
 24771          number, if we don't have enough space on this line.  */
 24772       if (tem_it.current_x >= width_limit)
 24773         {
 24774           it->lnum_width = 0;
 24775           it->lnum_pixel_width = 0;
 24776           bidi_unshelve_cache (itdata, false);
 24777           inhibit_free_realized_faces = save_free_realized_faces;
 24778           return;
 24779         }
 24780     }
 24781 
 24782   inhibit_free_realized_faces = save_free_realized_faces;
 24783 
 24784   /* Record the width in pixels we need for the line number display.  */
 24785   it->lnum_pixel_width = tem_it.current_x;
 24786   /* Copy the produced glyphs into IT's glyph_row.  */
 24787   struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
 24788   struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
 24789   struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
 24790   short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
 24791 
 24792   eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
 24793 
 24794   for ( ; g < e; g++)
 24795     {
 24796       it->current_x += g->pixel_width;
 24797       /* The following is important when this function is called
 24798          from move_it_in_display_line_to: HPOS is incremented only
 24799          when we are in the visible portion of the glyph row.  */
 24800       if (it->current_x > it->first_visible_x)
 24801         it->hpos++;
 24802       if (p)
 24803         {
 24804           *p++ = *g;
 24805           (*u)++;
 24806         }
 24807     }
 24808 
 24809   /* Update IT's metrics due to glyphs produced for line numbers.
 24810      Don't do that for rows beyond ZV, to avoid displaying a cursor of
 24811      different dimensions there.  */
 24812   if (!beyond_zv)
 24813     {
 24814       if (it->glyph_row)
 24815         {
 24816           struct glyph_row *row = it->glyph_row;
 24817 
 24818           it->max_ascent = max (row->ascent, tem_it.max_ascent);
 24819           it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
 24820           it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
 24821           it->max_phys_descent = max (row->phys_height - row->phys_ascent,
 24822                                       tem_it.max_phys_descent);
 24823         }
 24824       else
 24825         {
 24826           it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
 24827           it->max_descent = max (it->max_descent, tem_it.max_descent);
 24828           it->max_phys_ascent = max (it->max_phys_ascent,
 24829                                      tem_it.max_phys_ascent);
 24830           it->max_phys_descent = max (it->max_phys_descent,
 24831                                       tem_it.max_phys_descent);
 24832         }
 24833     }
 24834 
 24835   it->line_number_produced_p = true;
 24836 
 24837   bidi_unshelve_cache (itdata, false);
 24838 }
 24839 
 24840 /* Return true if this glyph row needs a line number to be produced
 24841    for it.  */
 24842 static bool
 24843 should_produce_line_number (struct it *it)
 24844 {
 24845   if (NILP (Vdisplay_line_numbers))
 24846     return false;
 24847 
 24848   /* Don't display line numbers in minibuffer windows.  */
 24849   if (MINI_WINDOW_P (it->w))
 24850     return false;
 24851 
 24852 #ifdef HAVE_WINDOW_SYSTEM
 24853   /* Don't display line number in tooltip frames.  */
 24854   if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
 24855     return false;
 24856 #endif
 24857 
 24858   /* If the character at current position has a non-nil special
 24859      property, disable line numbers for this row.  This is for
 24860      packages such as company-mode, which need this for their tricky
 24861      layout, where line numbers get in the way.  */
 24862   Lisp_Object val = Fget_char_property (make_fixnum (IT_CHARPOS (*it)),
 24863                                         Qdisplay_line_numbers_disable,
 24864                                         it->window);
 24865   /* For ZV, we need to also look in empty overlays at that point,
 24866      because get-char-property always returns nil for ZV, except if
 24867      the property is in 'default-text-properties'.  */
 24868   if (NILP (val) && IT_CHARPOS (*it) >= ZV)
 24869     val = disable_line_numbers_overlay_at_eob ();
 24870   return NILP (val) ? true : false;
 24871 }
 24872 
 24873 /* Return true if ROW has no glyphs except those inserted by the
 24874    display engine.  This is needed for indicate-empty-lines and
 24875    similar features when the glyph row starts with glyphs which didn't
 24876    come from buffer or string.  */
 24877 static bool
 24878 row_text_area_empty (struct glyph_row *row)
 24879 {
 24880   if (!row->reversed_p)
 24881     {
 24882       for (struct glyph *g = row->glyphs[TEXT_AREA];
 24883            g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 24884            g++)
 24885         if (!NILP (g->object) || g->charpos > 0)
 24886           return false;
 24887     }
 24888   else
 24889     {
 24890       for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 24891            g > row->glyphs[TEXT_AREA];
 24892            g--)
 24893         if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
 24894           return false;
 24895     }
 24896 
 24897   return true;
 24898 }
 24899 
 24900 /* Construct the glyph row IT->glyph_row in the desired matrix of
 24901    IT->w from text at the current position of IT.  See dispextern.h
 24902    for an overview of struct it.  Value is true if
 24903    IT->glyph_row displays text, as opposed to a line displaying ZV
 24904    only.  CURSOR_VPOS is the window-relative vertical position of
 24905    the glyph row displaying the cursor, or -1 if unknown.  */
 24906 
 24907 static bool
 24908 display_line (struct it *it, int cursor_vpos)
 24909 {
 24910   struct glyph_row *row = it->glyph_row;
 24911   Lisp_Object overlay_arrow_string;
 24912   struct it wrap_it;
 24913   void *wrap_data = NULL;
 24914   bool may_wrap = false;
 24915   int wrap_x UNINIT;
 24916   int wrap_row_used = -1;
 24917   int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
 24918   int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
 24919   int wrap_row_extra_line_spacing UNINIT;
 24920   ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
 24921   ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
 24922   int cvpos;
 24923   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 24924   ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
 24925   bool pending_handle_line_prefix = false;
 24926   int tab_line = window_wants_tab_line (it->w);
 24927   int header_line = window_wants_header_line (it->w);
 24928   bool hscroll_this_line = (cursor_vpos >= 0
 24929                             && it->vpos == cursor_vpos - tab_line - header_line
 24930                             && hscrolling_current_line_p (it->w));
 24931   int first_visible_x = it->first_visible_x;
 24932   int last_visible_x = it->last_visible_x;
 24933   int x_incr = 0;
 24934 
 24935   /* We always start displaying at hpos zero even if hscrolled.  */
 24936   eassert (it->hpos == 0 && it->current_x == 0);
 24937 
 24938   if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
 24939       >= it->w->desired_matrix->nrows)
 24940     {
 24941       it->w->nrows_scale_factor++;
 24942       it->f->fonts_changed = true;
 24943       return false;
 24944     }
 24945 
 24946   /* Clear the result glyph row and enable it.  */
 24947   prepare_desired_row (it->w, row, false);
 24948 
 24949   row->y = it->current_y;
 24950   row->start = it->start;
 24951   row->continuation_lines_width = it->continuation_lines_width;
 24952   row->displays_text_p = true;
 24953   row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
 24954   it->starts_in_middle_of_char_p = false;
 24955   it->stretch_adjust = 0;
 24956   it->line_number_produced_p = false;
 24957 
 24958   /* If we are going to display the cursor's line, account for the
 24959      hscroll of that line.  We subtract the window's min_hscroll,
 24960      because that was already accounted for in init_iterator.  */
 24961   if (hscroll_this_line)
 24962     x_incr =
 24963       (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
 24964       * FRAME_COLUMN_WIDTH (it->f);
 24965 
 24966   bool line_number_needed = should_produce_line_number (it);
 24967 
 24968   /* Move over display elements that are not visible because we are
 24969      hscrolled.  This may stop at an x-position < first_visible_x
 24970      if the first glyph is partially visible or if we hit a line end.  */
 24971   if (it->current_x < it->first_visible_x + x_incr)
 24972     {
 24973       enum move_it_result move_result;
 24974 
 24975       this_line_min_pos = row->start.pos;
 24976       if (hscroll_this_line)
 24977         {
 24978           it->first_visible_x += x_incr;
 24979           it->last_visible_x  += x_incr;
 24980         }
 24981       if (current_buffer->long_line_optimizations_p
 24982           && it->line_wrap == TRUNCATE
 24983           && window_hscroll_limited (it->w, it->f) > large_hscroll_threshold)
 24984         {
 24985           /* Special optimization for very long and truncated lines
 24986              which are hscrolled far to the left: jump directly to the
 24987              (approximate) position that is visible, instead of slowly
 24988              walking there.  */
 24989           ptrdiff_t chars_to_skip =
 24990             it->first_visible_x / FRAME_COLUMN_WIDTH (it->f);
 24991           move_result = fast_move_it_horizontally (it, chars_to_skip);
 24992 
 24993           if (move_result == MOVE_X_REACHED)
 24994             it->current_x = it->first_visible_x;
 24995           else  /* use arbitrary value < first_visible_x */
 24996             it->current_x = it->first_visible_x - FRAME_COLUMN_WIDTH (it->f);
 24997         }
 24998       else
 24999         move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
 25000                                                   MOVE_TO_POS | MOVE_TO_X);
 25001       /* If we are under a large hscroll, move_it_in_display_line_to
 25002          could hit the end of the line without reaching
 25003          first_visible_x.  Pretend that we did reach it.  This is
 25004          especially important on a TTY, where we will call
 25005          extend_face_to_end_of_line, which needs to know how many
 25006          blank glyphs to produce.  */
 25007       if (it->current_x < it->first_visible_x
 25008           && (move_result == MOVE_NEWLINE_OR_CR
 25009               || move_result == MOVE_POS_MATCH_OR_ZV))
 25010         it->current_x = it->first_visible_x;
 25011 
 25012       /* In case move_it_in_display_line_to above "produced" the line
 25013          number.  */
 25014       it->line_number_produced_p = false;
 25015 
 25016       /* Record the smallest positions seen while we moved over
 25017          display elements that are not visible.  This is needed by
 25018          redisplay_internal for optimizing the case where the cursor
 25019          stays inside the same line.  The rest of this function only
 25020          considers positions that are actually displayed, so
 25021          RECORD_MAX_MIN_POS will not otherwise record positions that
 25022          are hscrolled to the left of the left edge of the window.  */
 25023       min_pos = CHARPOS (this_line_min_pos);
 25024       min_bpos = BYTEPOS (this_line_min_pos);
 25025 
 25026       /* Produce line number, if needed.  */
 25027       if (line_number_needed)
 25028         maybe_produce_line_number (it);
 25029     }
 25030   else if (it->area == TEXT_AREA)
 25031     {
 25032       /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25033       if (line_number_needed)
 25034         maybe_produce_line_number (it);
 25035 
 25036       /* We only do this when not calling move_it_in_display_line_to
 25037          above, because that function calls itself handle_line_prefix.  */
 25038       handle_line_prefix (it);
 25039     }
 25040   else
 25041     {
 25042       /* Line-prefix and wrap-prefix are always displayed in the text
 25043          area.  But if this is the first call to display_line after
 25044          init_iterator, the iterator might have been set up to write
 25045          into a marginal area, e.g. if the line begins with some
 25046          display property that writes to the margins.  So we need to
 25047          wait with the call to handle_line_prefix until whatever
 25048          writes to the margin has done its job.  */
 25049       pending_handle_line_prefix = true;
 25050     }
 25051 
 25052   /* Get the initial row height.  This is either the height of the
 25053      text hscrolled, if there is any, or zero.  */
 25054   row->ascent = it->max_ascent;
 25055   row->height = it->max_ascent + it->max_descent;
 25056   row->phys_ascent = it->max_phys_ascent;
 25057   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 25058   row->extra_line_spacing = it->max_extra_line_spacing;
 25059 
 25060 /* Utility macro to record max and min buffer positions seen until now.  */
 25061 #define RECORD_MAX_MIN_POS(IT)                                  \
 25062   do                                                            \
 25063     {                                                           \
 25064       bool composition_p                                        \
 25065         = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
 25066       ptrdiff_t current_pos =                                   \
 25067         composition_p ? (IT)->cmp_it.charpos                    \
 25068                       : IT_CHARPOS (*(IT));                     \
 25069       ptrdiff_t current_bpos =                                  \
 25070         composition_p ? CHAR_TO_BYTE (current_pos)              \
 25071                       : IT_BYTEPOS (*(IT));                     \
 25072       if (current_pos < min_pos)                                \
 25073         {                                                       \
 25074           min_pos = current_pos;                                \
 25075           min_bpos = current_bpos;                              \
 25076         }                                                       \
 25077       if (IT_CHARPOS (*it) > max_pos)                           \
 25078         {                                                       \
 25079           max_pos = IT_CHARPOS (*it);                           \
 25080           max_bpos = IT_BYTEPOS (*it);                          \
 25081         }                                                       \
 25082     }                                                           \
 25083   while (false)
 25084 
 25085   /* Loop generating characters.  The loop is left with IT on the next
 25086      character to display.  */
 25087   while (true)
 25088     {
 25089       int n_glyphs_before, hpos_before, x_before;
 25090       int x, nglyphs;
 25091       int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
 25092 
 25093       /* Retrieve the next thing to display.  Value is false if end of
 25094          buffer reached.  */
 25095       if (!get_next_display_element (it))
 25096         {
 25097           bool row_has_glyphs = false;
 25098           /* Maybe add a space at the end of this line that is used to
 25099              display the cursor there under X.  Set the charpos of the
 25100              first glyph of blank lines not corresponding to any text
 25101              to -1.  */
 25102           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25103             row->exact_window_width_line_p = true;
 25104           else if ((append_space_for_newline (it, true)
 25105                     && row->used[TEXT_AREA] == 1)
 25106                    || row->used[TEXT_AREA] == 0
 25107                    || (row_has_glyphs = row_text_area_empty (row)))
 25108             {
 25109               row->glyphs[TEXT_AREA]->charpos = -1;
 25110               /* Don't reset the displays_text_p flag if we are
 25111                  displaying line numbers or line-prefix.  */
 25112               if (!row_has_glyphs)
 25113                 row->displays_text_p = false;
 25114 
 25115               if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
 25116                   && (!MINI_WINDOW_P (it->w)))
 25117                 row->indicate_empty_line_p = true;
 25118             }
 25119 
 25120           it->continuation_lines_width = 0;
 25121           /* Reset those iterator values set from display property
 25122              values.  This is for the case when the display property
 25123              ends at ZV, and is not a replacing property, so pop_it is
 25124              not called.  */
 25125           it->font_height = Qnil;
 25126           it->voffset = 0;
 25127           row->ends_at_zv_p = true;
 25128           /* A row that displays right-to-left text must always have
 25129              its last face extended all the way to the end of line,
 25130              even if this row ends in ZV, because we still write to
 25131              the screen left to right.  We also need to extend the
 25132              last face if the default face is remapped to some
 25133              different face, otherwise the functions that clear
 25134              portions of the screen will clear with the default face's
 25135              background color.  */
 25136           if (row->reversed_p
 25137               || lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
 25138               != DEFAULT_FACE_ID)
 25139             extend_face_to_end_of_line (it);
 25140           break;
 25141         }
 25142 
 25143       /* Now, get the metrics of what we want to display.  This also
 25144          generates glyphs in `row' (which is IT->glyph_row).  */
 25145       n_glyphs_before = row->used[TEXT_AREA];
 25146       x = it->current_x;
 25147 
 25148       /* Remember the line height so far in case the next element doesn't
 25149          fit on the line.  */
 25150       if (it->line_wrap != TRUNCATE)
 25151         {
 25152           ascent = it->max_ascent;
 25153           descent = it->max_descent;
 25154           phys_ascent = it->max_phys_ascent;
 25155           phys_descent = it->max_phys_descent;
 25156 
 25157           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
 25158             {
 25159               bool next_may_wrap = may_wrap;
 25160               /* Can we wrap after this character?  */
 25161               if (char_can_wrap_after (it))
 25162                 next_may_wrap = true;
 25163               else
 25164                 next_may_wrap = false;
 25165               /* Can we wrap here? */
 25166               if (may_wrap && char_can_wrap_before (it))
 25167                 {
 25168                   SAVE_IT (wrap_it, *it, wrap_data);
 25169                   wrap_x = x;
 25170                   wrap_row_used = row->used[TEXT_AREA];
 25171                   wrap_row_ascent = row->ascent;
 25172                   wrap_row_height = row->height;
 25173                   wrap_row_phys_ascent = row->phys_ascent;
 25174                   wrap_row_phys_height = row->phys_height;
 25175                   wrap_row_extra_line_spacing = row->extra_line_spacing;
 25176                   wrap_row_min_pos = min_pos;
 25177                   wrap_row_min_bpos = min_bpos;
 25178                   wrap_row_max_pos = max_pos;
 25179                   wrap_row_max_bpos = max_bpos;
 25180                 }
 25181               /* Update may_wrap for the next iteration.  */
 25182               may_wrap = next_may_wrap;
 25183             }
 25184         }
 25185 
 25186       PRODUCE_GLYPHS (it);
 25187 
 25188       /* If this display element was in marginal areas, continue with
 25189          the next one.  */
 25190       if (it->area != TEXT_AREA)
 25191         {
 25192           row->ascent = max (row->ascent, it->max_ascent);
 25193           row->height = max (row->height, it->max_ascent + it->max_descent);
 25194           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25195           row->phys_height = max (row->phys_height,
 25196                                   it->max_phys_ascent + it->max_phys_descent);
 25197           row->extra_line_spacing = max (row->extra_line_spacing,
 25198                                          it->max_extra_line_spacing);
 25199           set_iterator_to_next (it, true);
 25200           /* If we didn't handle the line/wrap prefix above, and the
 25201              call to set_iterator_to_next just switched to TEXT_AREA,
 25202              process the prefix now.  */
 25203           if (it->area == TEXT_AREA && pending_handle_line_prefix)
 25204             {
 25205               /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25206               if (line_number_needed)
 25207                 maybe_produce_line_number (it);
 25208 
 25209               pending_handle_line_prefix = false;
 25210               handle_line_prefix (it);
 25211             }
 25212           continue;
 25213         }
 25214 
 25215       /* Does the display element fit on the line?  If we truncate
 25216          lines, we should draw past the right edge of the window.  If
 25217          we don't truncate, we want to stop so that we can display the
 25218          continuation glyph before the right margin.  If lines are
 25219          continued, there are two possible strategies for characters
 25220          resulting in more than 1 glyph (e.g. tabs): Display as many
 25221          glyphs as possible in this line and leave the rest for the
 25222          continuation line, or display the whole element in the next
 25223          line.  Original redisplay did the former, so we do it also.  */
 25224       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 25225       hpos_before = it->hpos;
 25226       x_before = x;
 25227 
 25228       if (/* Not a newline.  */
 25229           nglyphs > 0
 25230           /* Glyphs produced fit entirely in the line.  */
 25231           && it->current_x < it->last_visible_x)
 25232         {
 25233           it->hpos += nglyphs;
 25234           row->ascent = max (row->ascent, it->max_ascent);
 25235           row->height = max (row->height, it->max_ascent + it->max_descent);
 25236           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25237           row->phys_height = max (row->phys_height,
 25238                                   it->max_phys_ascent + it->max_phys_descent);
 25239           row->extra_line_spacing = max (row->extra_line_spacing,
 25240                                          it->max_extra_line_spacing);
 25241           if (it->current_x - it->pixel_width < it->first_visible_x
 25242               /* When line numbers are displayed, row->x should not be
 25243                  offset, as the first glyph after the line number can
 25244                  never be partially visible.  */
 25245               && !line_number_needed
 25246               /* In R2L rows, we arrange in extend_face_to_end_of_line
 25247                  to add a right offset to the line, by a suitable
 25248                  change to the stretch glyph that is the leftmost
 25249                  glyph of the line.  */
 25250               && !row->reversed_p)
 25251             row->x = x - it->first_visible_x;
 25252           /* Record the maximum and minimum buffer positions seen so
 25253              far in glyphs that will be displayed by this row.  */
 25254           if (it->bidi_p)
 25255             RECORD_MAX_MIN_POS (it);
 25256         }
 25257       else
 25258         {
 25259           int i, new_x;
 25260           struct glyph *glyph;
 25261 
 25262           for (i = 0; i < nglyphs; ++i, x = new_x)
 25263             {
 25264               /* Identify the glyphs added by the last call to
 25265                  PRODUCE_GLYPHS.  In R2L rows, they are prepended to
 25266                  the previous glyphs.  */
 25267               if (!row->reversed_p)
 25268                 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 25269               else
 25270                 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
 25271               new_x = x + glyph->pixel_width;
 25272 
 25273               if (/* Lines are continued.  */
 25274                   it->line_wrap != TRUNCATE
 25275                   && (/* Glyph doesn't fit on the line.  */
 25276                       new_x > it->last_visible_x
 25277                       /* Or it fits exactly on a window system frame.  */
 25278                       || (new_x == it->last_visible_x
 25279                           && FRAME_WINDOW_P (it->f)
 25280                           && (row->reversed_p
 25281                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25282                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 25283                 {
 25284                   /* End of a continued line.  */
 25285 
 25286                   if (it->hpos == 0
 25287                       || (new_x == it->last_visible_x
 25288                           && FRAME_WINDOW_P (it->f)
 25289                           && (row->reversed_p
 25290                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25291                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
 25292                     {
 25293                       /* Current glyph is the only one on the line or
 25294                          fits exactly on the line.  We must continue
 25295                          the line because we can't draw the cursor
 25296                          after the glyph.  */
 25297                       row->continued_p = true;
 25298                       it->current_x = new_x;
 25299                       it->continuation_lines_width += new_x;
 25300                       ++it->hpos;
 25301                       if (i == nglyphs - 1)
 25302                         {
 25303                           /* If line-wrap is on, check if a previous
 25304                              wrap point was found.  */
 25305                           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 25306                               && wrap_row_used > 0 /* Found.  */
 25307                               /* Even if there is a previous wrap
 25308                                  point, continue the line here as
 25309                                  usual, if (i) the previous character
 25310                                  allows wrapping after it, AND (ii)
 25311                                  the current character allows wrapping
 25312                                  before it.  Because this is a valid
 25313                                  break point, we can just continue to
 25314                                  the next line at here, there is no
 25315                                  need to wrap early at the previous
 25316                                  wrap point.  */
 25317                               && (!may_wrap || !char_can_wrap_before (it)))
 25318                             goto back_to_wrap;
 25319 
 25320                           /* Record the maximum and minimum buffer
 25321                              positions seen so far in glyphs that will be
 25322                              displayed by this row.  */
 25323                           if (it->bidi_p)
 25324                             RECORD_MAX_MIN_POS (it);
 25325                           set_iterator_to_next (it, true);
 25326                           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25327                             {
 25328                               if (!get_next_display_element (it))
 25329                                 {
 25330                                   row->exact_window_width_line_p = true;
 25331                                   it->continuation_lines_width = 0;
 25332                                   it->font_height = Qnil;
 25333                                   it->voffset = 0;
 25334                                   row->continued_p = false;
 25335                                   row->ends_at_zv_p = true;
 25336                                 }
 25337                               else if (ITERATOR_AT_END_OF_LINE_P (it))
 25338                                 {
 25339                                   row->continued_p = false;
 25340                                   row->exact_window_width_line_p = true;
 25341                                 }
 25342                               /* If line-wrap is on, check if a
 25343                                  previous wrap point was found.  */
 25344                               else if (wrap_row_used > 0
 25345                                        /* Even if there is a previous
 25346                                           wrap point, continue the
 25347                                           line here as usual, if (i)
 25348                                           the previous character was a
 25349                                           space or tab AND (ii) the
 25350                                           current character is not,
 25351                                           AND (iii) the current
 25352                                           character allows wrapping
 25353                                           before it.  */
 25354                                        && (!may_wrap || !char_can_wrap_before (it)))
 25355                                 goto back_to_wrap;
 25356 
 25357                             }
 25358                         }
 25359                       else if (it->bidi_p)
 25360                         RECORD_MAX_MIN_POS (it);
 25361                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25362                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25363                         extend_face_to_end_of_line (it);
 25364                     }
 25365                   else if (CHAR_GLYPH_PADDING_P (*glyph)
 25366                            && !FRAME_WINDOW_P (it->f))
 25367                     {
 25368                       /* A padding glyph that doesn't fit on this line.
 25369                          This means the whole character doesn't fit
 25370                          on the line.  */
 25371                       if (row->reversed_p)
 25372                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25373                                                - n_glyphs_before);
 25374                       row->used[TEXT_AREA] = n_glyphs_before;
 25375 
 25376                       /* Fill the rest of the row with continuation
 25377                          glyphs like in 20.x.  */
 25378                       while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
 25379                              < row->glyphs[1 + TEXT_AREA])
 25380                         produce_special_glyphs (it, IT_CONTINUATION);
 25381 
 25382                       row->continued_p = true;
 25383                       it->current_x = x_before;
 25384                       it->continuation_lines_width += x_before;
 25385 
 25386                       /* Restore the height to what it was before the
 25387                          element not fitting on the line.  */
 25388                       it->max_ascent = ascent;
 25389                       it->max_descent = descent;
 25390                       it->max_phys_ascent = phys_ascent;
 25391                       it->max_phys_descent = phys_descent;
 25392                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25393                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25394                         extend_face_to_end_of_line (it);
 25395                     }
 25396                   else if (wrap_row_used > 0)
 25397                     {
 25398                     back_to_wrap:
 25399                       if (row->reversed_p)
 25400                         unproduce_glyphs (it,
 25401                                           row->used[TEXT_AREA] - wrap_row_used);
 25402                       RESTORE_IT (it, &wrap_it, wrap_data);
 25403                       it->continuation_lines_width += wrap_x;
 25404                       row->used[TEXT_AREA] = wrap_row_used;
 25405                       row->ascent = wrap_row_ascent;
 25406                       row->height = wrap_row_height;
 25407                       row->phys_ascent = wrap_row_phys_ascent;
 25408                       row->phys_height = wrap_row_phys_height;
 25409                       row->extra_line_spacing = wrap_row_extra_line_spacing;
 25410                       min_pos = wrap_row_min_pos;
 25411                       min_bpos = wrap_row_min_bpos;
 25412                       max_pos = wrap_row_max_pos;
 25413                       max_bpos = wrap_row_max_bpos;
 25414                       row->continued_p = true;
 25415                       row->ends_at_zv_p = false;
 25416                       row->exact_window_width_line_p = false;
 25417 
 25418                       /* Make sure that a non-default face is extended
 25419                          up to the right margin of the window.  */
 25420                       extend_face_to_end_of_line (it);
 25421                     }
 25422                   else if ((it->what == IT_CHARACTER
 25423                             || it->what == IT_STRETCH
 25424                             || it->what == IT_COMPOSITION)
 25425                            && it->c == '\t' && FRAME_WINDOW_P (it->f))
 25426                     {
 25427                       /* A TAB that extends past the right edge of the
 25428                          window.  This produces a single glyph on
 25429                          window system frames.  We leave the glyph in
 25430                          this row and let it fill the row, but don't
 25431                          consume the TAB.  */
 25432                       if ((row->reversed_p
 25433                            ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25434                            : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25435                         produce_special_glyphs (it, IT_CONTINUATION);
 25436                       it->continuation_lines_width += it->last_visible_x;
 25437                       row->ends_in_middle_of_char_p = true;
 25438                       row->continued_p = true;
 25439                       glyph->pixel_width = it->last_visible_x - x;
 25440                       it->starts_in_middle_of_char_p = true;
 25441                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25442                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25443                         extend_face_to_end_of_line (it);
 25444                     }
 25445                   else
 25446                     {
 25447                       /* Something other than a TAB that draws past
 25448                          the right edge of the window.  Restore
 25449                          positions to values before the element.  */
 25450                       if (row->reversed_p)
 25451                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25452                                                - (n_glyphs_before + i));
 25453                       row->used[TEXT_AREA] = n_glyphs_before + i;
 25454 
 25455                       /* Display continuation glyphs.  */
 25456                       it->current_x = x_before;
 25457                       it->continuation_lines_width += x;
 25458                       if (!FRAME_WINDOW_P (it->f)
 25459                           || (row->reversed_p
 25460                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25461                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25462                         produce_special_glyphs (it, IT_CONTINUATION);
 25463                       row->continued_p = true;
 25464 
 25465                       extend_face_to_end_of_line (it);
 25466 
 25467                       if (nglyphs > 1 && i > 0)
 25468                         {
 25469                           row->ends_in_middle_of_char_p = true;
 25470                           it->starts_in_middle_of_char_p = true;
 25471                         }
 25472 
 25473                       /* Restore the height to what it was before the
 25474                          element not fitting on the line.  */
 25475                       it->max_ascent = ascent;
 25476                       it->max_descent = descent;
 25477                       it->max_phys_ascent = phys_ascent;
 25478                       it->max_phys_descent = phys_descent;
 25479                     }
 25480 
 25481                   break;
 25482                 }
 25483               else if (new_x > it->first_visible_x)
 25484                 {
 25485                   /* Increment number of glyphs actually displayed.  */
 25486                   ++it->hpos;
 25487 
 25488                   /* Record the maximum and minimum buffer positions
 25489                      seen so far in glyphs that will be displayed by
 25490                      this row.  */
 25491                   if (it->bidi_p)
 25492                     RECORD_MAX_MIN_POS (it);
 25493 
 25494                   if (x < it->first_visible_x && !row->reversed_p
 25495                       && !line_number_needed)
 25496                     /* Glyph is partially visible, i.e. row starts at
 25497                        negative X position.  Don't do that in R2L
 25498                        rows, where we arrange to add a right offset to
 25499                        the line in extend_face_to_end_of_line, by a
 25500                        suitable change to the stretch glyph that is
 25501                        the leftmost glyph of the line.  */
 25502                     row->x = x - it->first_visible_x;
 25503                   /* When the last glyph of an R2L row only fits
 25504                      partially on the line, we need to set row->x to a
 25505                      negative offset, so that the leftmost glyph is
 25506                      the one that is partially visible.  But if we are
 25507                      going to produce the truncation glyph, this will
 25508                      be taken care of in produce_special_glyphs.  */
 25509                   if (row->reversed_p
 25510                       && new_x > it->last_visible_x
 25511                       && !line_number_needed
 25512                       && !(it->line_wrap == TRUNCATE
 25513                            && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
 25514                     {
 25515                       eassert (FRAME_WINDOW_P (it->f));
 25516                       row->x = it->last_visible_x - new_x;
 25517                     }
 25518                 }
 25519               else
 25520                 {
 25521                   /* Glyph is completely off the left margin of the
 25522                      window.  This should not happen because of the
 25523                      move_it_in_display_line at the start of this
 25524                      function, unless the text display area of the
 25525                      window is empty.  */
 25526                   eassert (it->first_visible_x <= it->last_visible_x);
 25527                 }
 25528             }
 25529           /* Even if this display element produced no glyphs at all,
 25530              we want to record its position.  */
 25531           if (it->bidi_p && nglyphs == 0)
 25532             RECORD_MAX_MIN_POS (it);
 25533 
 25534           row->ascent = max (row->ascent, it->max_ascent);
 25535           row->height = max (row->height, it->max_ascent + it->max_descent);
 25536           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25537           row->phys_height = max (row->phys_height,
 25538                                   it->max_phys_ascent + it->max_phys_descent);
 25539           row->extra_line_spacing = max (row->extra_line_spacing,
 25540                                          it->max_extra_line_spacing);
 25541 
 25542           /* End of this display line if row is continued.  */
 25543           if (row->continued_p || row->ends_at_zv_p)
 25544             break;
 25545         }
 25546 
 25547     at_end_of_line:
 25548       /* Is this a line end?  If yes, we're also done, after making
 25549          sure that a non-default face is extended up to the right
 25550          margin of the window.  */
 25551       if (ITERATOR_AT_END_OF_LINE_P (it))
 25552         {
 25553           int used_before = row->used[TEXT_AREA];
 25554 
 25555           row->ends_in_newline_from_string_p = STRINGP (it->object);
 25556 
 25557           /* Add a space at the end of the line that is used to
 25558              display the cursor there.  */
 25559           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25560             append_space_for_newline (it, false);
 25561 
 25562           /* Extend the face to the end of the line.  */
 25563           extend_face_to_end_of_line (it);
 25564 
 25565           /* Make sure we have the position.  */
 25566           if (used_before == 0)
 25567             row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
 25568 
 25569           /* Record the position of the newline, for use in
 25570              find_row_edges.  */
 25571           it->eol_pos = it->current.pos;
 25572 
 25573           /* Consume the line end.  This skips over invisible lines.  */
 25574           set_iterator_to_next (it, true);
 25575           it->continuation_lines_width = 0;
 25576           break;
 25577         }
 25578 
 25579       /* Detect overly-wide wrap-prefixes made of (space ...) display
 25580          properties.  When such a wrap prefix reaches past the right
 25581          margin of the window, we need to avoid the call to
 25582          set_iterator_to_next below, so that it->line_wrap is left at
 25583          its TRUNCATE value wisely set by handle_line_prefix.
 25584          Otherwise, set_iterator_to_next will pop the iterator stack,
 25585          restore it->line_wrap, and redisplay might infloop.  */
 25586       bool overwide_wrap_prefix =
 25587         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 25588         && it->sp > 0 && it->method == GET_FROM_STRETCH
 25589         && it->current_x >= it->last_visible_x
 25590         && it->continuation_lines_width > 0
 25591         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 25592 
 25593       /* Proceed with next display element.  Note that this skips
 25594          over lines invisible because of selective display.  */
 25595       if (!overwide_wrap_prefix)
 25596         set_iterator_to_next (it, true);
 25597 
 25598       /* If we truncate lines, we are done when the last displayed
 25599          glyphs reach past the right margin of the window.  */
 25600       if (it->line_wrap == TRUNCATE
 25601           && ((FRAME_WINDOW_P (it->f)
 25602                /* Images are preprocessed in produce_image_glyph such
 25603                   that they are cropped at the right edge of the
 25604                   window, so an image glyph will always end exactly at
 25605                   last_visible_x, even if there's no right fringe.  */
 25606                && ((row->reversed_p
 25607                     ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25608                     : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
 25609                    || it->what == IT_IMAGE))
 25610               ? (it->current_x >= it->last_visible_x)
 25611               : (it->current_x > it->last_visible_x)))
 25612         {
 25613           /* Maybe add truncation glyphs.  */
 25614           if (!FRAME_WINDOW_P (it->f)
 25615               || (row->reversed_p
 25616                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25617                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25618             {
 25619               int i, n;
 25620 
 25621               if (!row->reversed_p)
 25622                 {
 25623                   for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
 25624                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25625                       break;
 25626                 }
 25627               else
 25628                 {
 25629                   for (i = 0; i < row->used[TEXT_AREA]; i++)
 25630                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25631                       break;
 25632                   /* Remove any padding glyphs at the front of ROW, to
 25633                      make room for the truncation glyphs we will be
 25634                      adding below.  The loop below always inserts at
 25635                      least one truncation glyph, so also remove the
 25636                      last glyph added to ROW.  */
 25637                   unproduce_glyphs (it, i + 1);
 25638                   /* Adjust i for the loop below.  */
 25639                   i = row->used[TEXT_AREA] - (i + 1);
 25640                 }
 25641 
 25642               /* produce_special_glyphs overwrites the last glyph, so
 25643                  we don't want that if we want to keep that last
 25644                  glyph, which means it's an image.  */
 25645               if (it->current_x > it->last_visible_x)
 25646                 {
 25647                   it->current_x = x_before;
 25648                   if (!FRAME_WINDOW_P (it->f))
 25649                     {
 25650                       for (n = row->used[TEXT_AREA]; i < n; ++i)
 25651                         {
 25652                           row->used[TEXT_AREA] = i;
 25653                           produce_special_glyphs (it, IT_TRUNCATION);
 25654                         }
 25655                     }
 25656                   else
 25657                     {
 25658                       row->used[TEXT_AREA] = i;
 25659                       produce_special_glyphs (it, IT_TRUNCATION);
 25660                     }
 25661                   it->hpos = hpos_before;
 25662                 }
 25663             }
 25664           else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25665             {
 25666               /* Don't truncate if we can overflow newline into fringe.  */
 25667               if (!get_next_display_element (it))
 25668                 {
 25669                   it->continuation_lines_width = 0;
 25670                   it->font_height = Qnil;
 25671                   it->voffset = 0;
 25672                   row->ends_at_zv_p = true;
 25673                   row->exact_window_width_line_p = true;
 25674                   break;
 25675                 }
 25676               if (ITERATOR_AT_END_OF_LINE_P (it))
 25677                 {
 25678                   row->exact_window_width_line_p = true;
 25679                   goto at_end_of_line;
 25680                 }
 25681               it->current_x = x_before;
 25682               it->hpos = hpos_before;
 25683             }
 25684 
 25685           row->truncated_on_right_p = true;
 25686           it->continuation_lines_width = 0;
 25687           reseat_at_next_visible_line_start (it, false);
 25688           /* We insist below that IT's position be at ZV because in
 25689              bidi-reordered lines the character at visible line start
 25690              might not be the character that follows the newline in
 25691              the logical order.  */
 25692           if (IT_BYTEPOS (*it) > BEG_BYTE)
 25693             row->ends_at_zv_p =
 25694               IT_BYTEPOS (*it) >= ZV_BYTE
 25695               && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n');
 25696           else
 25697             row->ends_at_zv_p = false;
 25698           break;
 25699         }
 25700     }
 25701 
 25702   if (wrap_data)
 25703     bidi_unshelve_cache (wrap_data, true);
 25704 
 25705   /* If line is not empty and hscrolled, maybe insert truncation glyphs
 25706      at the left window margin.  */
 25707   if (it->first_visible_x
 25708       && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
 25709     {
 25710       if (!FRAME_WINDOW_P (it->f)
 25711           || (((row->reversed_p
 25712                 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 25713                 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 25714               /* Don't let insert_left_trunc_glyphs overwrite the
 25715                  first glyph of the row if it is an image.  */
 25716               && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
 25717         insert_left_trunc_glyphs (it);
 25718       row->truncated_on_left_p = true;
 25719     }
 25720 
 25721   /* Remember the position at which this line ends.
 25722 
 25723      BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
 25724      cannot be before the call to find_row_edges below, since that is
 25725      where these positions are determined. */
 25726   row->end = it->current;
 25727   if (!it->bidi_p)
 25728     {
 25729       row->minpos = row->start.pos;
 25730       row->maxpos = row->end.pos;
 25731     }
 25732   else
 25733     {
 25734       /* ROW->minpos and ROW->maxpos must be the smallest and
 25735          `1 + the largest' buffer positions in ROW.  But if ROW was
 25736          bidi-reordered, these two positions can be anywhere in the
 25737          row, so we must determine them now.  */
 25738       find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
 25739     }
 25740 
 25741   /* If the start of this line is the overlay arrow-position, then
 25742      mark this glyph row as the one containing the overlay arrow.
 25743      This is clearly a mess with variable size fonts.  It would be
 25744      better to let it be displayed like cursors under X.  */
 25745   if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
 25746       && (overlay_arrow_string = overlay_arrow_at_row (it, row),
 25747           !NILP (overlay_arrow_string)))
 25748     {
 25749       /* Overlay arrow in window redisplay is a fringe bitmap.  */
 25750       if (STRINGP (overlay_arrow_string))
 25751         {
 25752           struct glyph_row *arrow_row
 25753             = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
 25754           struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
 25755           struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
 25756           struct glyph *p = row->glyphs[TEXT_AREA];
 25757           struct glyph *p2, *end;
 25758 
 25759           /* Copy the arrow glyphs.  */
 25760           while (glyph < arrow_end)
 25761             *p++ = *glyph++;
 25762 
 25763           /* Throw away padding glyphs.  */
 25764           p2 = p;
 25765           end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 25766           while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
 25767             ++p2;
 25768           if (p2 > p)
 25769             {
 25770               while (p2 < end)
 25771                 *p++ = *p2++;
 25772               row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
 25773             }
 25774         }
 25775       else
 25776         {
 25777           eassert (FIXNUMP (overlay_arrow_string));
 25778           row->overlay_arrow_bitmap = XFIXNUM (overlay_arrow_string);
 25779         }
 25780       overlay_arrow_seen = true;
 25781     }
 25782 
 25783   /* Highlight trailing whitespace.  */
 25784   if (!NILP (Vshow_trailing_whitespace))
 25785     highlight_trailing_whitespace (it);
 25786 
 25787   /* Compute pixel dimensions of this line.  */
 25788   compute_line_metrics (it);
 25789 
 25790   /* Implementation note: No changes in the glyphs of ROW or in their
 25791      faces can be done past this point, because compute_line_metrics
 25792      computes ROW's hash value and stores it within the glyph_row
 25793      structure.  */
 25794 
 25795   /* Record whether this row ends inside an ellipsis.  */
 25796   row->ends_in_ellipsis_p
 25797     = (it->method == GET_FROM_DISPLAY_VECTOR
 25798        && it->ellipsis_p);
 25799 
 25800   /* Save fringe bitmaps in this row.  */
 25801   row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
 25802   row->left_user_fringe_face_id = it->left_user_fringe_face_id;
 25803   row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
 25804   row->right_user_fringe_face_id = it->right_user_fringe_face_id;
 25805 
 25806   it->left_user_fringe_bitmap = 0;
 25807   it->left_user_fringe_face_id = 0;
 25808   it->right_user_fringe_bitmap = 0;
 25809   it->right_user_fringe_face_id = 0;
 25810 
 25811   /* When they turn off tooltip-mode on a GUI frame, we call 'message'
 25812      with message-truncate-lines bound to non-nil, which produces
 25813      truncation bitmaps on the fringe.  Force redrawing of the fringes
 25814      in that case, to make sure the fringe bitmaps are removed when a
 25815      shorter message is displayed.  */
 25816   if (MINI_WINDOW_P (it->w) && it->line_wrap == TRUNCATE
 25817       && FRAME_WINDOW_P (it->f) && !cursor_in_echo_area)
 25818     row->redraw_fringe_bitmaps_p = true;
 25819 
 25820   /* Maybe set the cursor.  */
 25821   cvpos = it->w->cursor.vpos;
 25822   if ((cvpos < 0
 25823        /* In bidi-reordered rows, keep checking for proper cursor
 25824           position even if one has been found already, because buffer
 25825           positions in such rows change non-linearly with ROW->VPOS,
 25826           when a line is continued.  One exception: when we are at ZV,
 25827           display cursor on the first suitable glyph row, since all
 25828           the empty rows after that also have their position set to ZV.  */
 25829        /* FIXME: Revisit this when glyph ``spilling'' in continuation
 25830           lines' rows is implemented for bidi-reordered rows.  */
 25831        || (it->bidi_p
 25832            && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
 25833       && PT >= MATRIX_ROW_START_CHARPOS (row)
 25834       && PT <= MATRIX_ROW_END_CHARPOS (row)
 25835       && cursor_row_p (row))
 25836     set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
 25837 
 25838   /* Prepare for the next line.  This line starts horizontally at (X
 25839      HPOS) = (0 0).  Vertical positions are incremented.  As a
 25840      convenience for the caller, IT->glyph_row is set to the next
 25841      row to be used.  */
 25842   it->current_x = it->hpos = 0;
 25843   it->current_y += row->height;
 25844   /* Restore the first and last visible X if we adjusted them for
 25845      current-line hscrolling.  */
 25846   if (hscroll_this_line)
 25847     {
 25848       it->first_visible_x = first_visible_x;
 25849       it->last_visible_x  = last_visible_x;
 25850     }
 25851   SET_TEXT_POS (it->eol_pos, 0, 0);
 25852   ++it->vpos;
 25853   ++it->glyph_row;
 25854   /* The next row should by default use the same value of the
 25855      reversed_p flag as this one.  set_iterator_to_next decides when
 25856      it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
 25857      the flag accordingly.  */
 25858   if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
 25859     it->glyph_row->reversed_p = row->reversed_p;
 25860   it->start = row->end;
 25861   return MATRIX_ROW_DISPLAYS_TEXT_P (row);
 25862 
 25863 #undef RECORD_MAX_MIN_POS
 25864 }
 25865 
 25866 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
 25867        Scurrent_bidi_paragraph_direction, 0, 1, 0,
 25868        doc: /* Return paragraph direction at point in BUFFER.
 25869 Value is either `left-to-right' or `right-to-left'.
 25870 If BUFFER is omitted or nil, it defaults to the current buffer.
 25871 
 25872 Paragraph direction determines how the text in the paragraph is displayed.
 25873 In left-to-right paragraphs, text begins at the left margin of the window
 25874 and the reading direction is generally left to right.  In right-to-left
 25875 paragraphs, text begins at the right margin and is read from right to left.
 25876 
 25877 See also `bidi-paragraph-direction'.  */)
 25878   (Lisp_Object buffer)
 25879 {
 25880   struct buffer *buf = current_buffer;
 25881   struct buffer *old = buf;
 25882 
 25883   if (! NILP (buffer))
 25884     {
 25885       CHECK_BUFFER (buffer);
 25886       buf = XBUFFER (buffer);
 25887     }
 25888 
 25889   if (NILP (BVAR (buf, bidi_display_reordering))
 25890       || NILP (BVAR (buf, enable_multibyte_characters))
 25891       /* When we are loading loadup.el, the character property tables
 25892          needed for bidi iteration are not yet available.  */
 25893       || redisplay__inhibit_bidi)
 25894     return Qleft_to_right;
 25895   else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
 25896     return BVAR (buf, bidi_paragraph_direction);
 25897   else
 25898     {
 25899       /* Determine the direction from buffer text.  We could try to
 25900          use current_matrix if it is up to date, but this seems fast
 25901          enough as it is.  */
 25902       struct bidi_it itb;
 25903       ptrdiff_t pos = BUF_PT (buf);
 25904       ptrdiff_t bytepos = BUF_PT_BYTE (buf);
 25905       int c;
 25906       void *itb_data = bidi_shelve_cache ();
 25907 
 25908       set_buffer_temp (buf);
 25909       /* bidi_paragraph_init finds the base direction of the paragraph
 25910          by searching forward from paragraph start.  We need the base
 25911          direction of the current or _previous_ paragraph, so we need
 25912          to make sure we are within that paragraph.  To that end, find
 25913          the previous non-empty line.  */
 25914       if (pos >= ZV && pos > BEGV)
 25915         dec_both (&pos, &bytepos);
 25916       AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
 25917       if (fast_looking_at (trailing_white_space,
 25918                            pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
 25919         {
 25920           while ((c = FETCH_BYTE (bytepos)) == '\n'
 25921                  || c == ' ' || c == '\t' || c == '\f')
 25922             {
 25923               if (bytepos <= BEGV_BYTE)
 25924                 break;
 25925               bytepos--;
 25926               pos--;
 25927             }
 25928           while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
 25929             bytepos--;
 25930         }
 25931       bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
 25932       itb.paragraph_dir = NEUTRAL_DIR;
 25933       itb.string.s = NULL;
 25934       itb.string.lstring = Qnil;
 25935       itb.string.bufpos = 0;
 25936       itb.string.from_disp_str = false;
 25937       itb.string.unibyte = false;
 25938       /* We have no window to use here for ignoring window-specific
 25939          overlays.  Using NULL for window pointer will cause
 25940          compute_display_string_pos to use the current buffer.  */
 25941       itb.w = NULL;
 25942       bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
 25943       bidi_unshelve_cache (itb_data, false);
 25944       set_buffer_temp (old);
 25945       switch (itb.paragraph_dir)
 25946         {
 25947         case L2R:
 25948           return Qleft_to_right;
 25949           break;
 25950         case R2L:
 25951           return Qright_to_left;
 25952           break;
 25953         default:
 25954           emacs_abort ();
 25955         }
 25956     }
 25957 }
 25958 
 25959 DEFUN ("bidi-find-overridden-directionality",
 25960        Fbidi_find_overridden_directionality,
 25961        Sbidi_find_overridden_directionality, 3, 4, 0,
 25962        doc: /* Return position between FROM and TO where directionality was overridden.
 25963 
 25964 This function returns the first character position in the specified
 25965 region of OBJECT where characters have their bidirectional
 25966 properties affected in a way that might make its text look confusingly
 25967 on display.  For example, characters whose `bidi-class' property is `L',
 25968 could be forced to display as `R' by a directional override, and
 25969 likewise characters whose `bidi-class' is `R' or `AL' that are
 25970 forced to display as `L'.
 25971 
 25972 If no such character is found, the function returns nil.
 25973 
 25974 OBJECT is a Lisp string or buffer to search for overridden
 25975 directionality, and defaults to the current buffer if nil.
 25976 OBJECT can also be a window, in which case the function will search
 25977 the buffer displayed in that window.  Passing the window instead of
 25978 a buffer is preferable when the buffer is displayed in some window,
 25979 because this function will then be able to correctly account for
 25980 window-specific overlays, which can affect the results.
 25981 
 25982 Optional argument BASE-DIR specifies the base paragraph directory
 25983 of the text.  It should be a symbol, either `left-to-right'
 25984 or `right-to-left', and defaults to `left-to-right'.
 25985 
 25986 Strong directional characters `L', `R', and `AL' can have their
 25987 intrinsic directionality overridden by directional override control
 25988 characters RLO (u+202E) and LRO (u+202D).  They can also have their
 25989 directionality affected by other formatting control characters: LRE
 25990 (u+202A), RLE (u+202B), LRI (u+2066), and RLI (u+2067).  See the
 25991 function `get-char-code-property' for a way to inquire about the
 25992 `bidi-class' property of a character.  Characters whose intrinsic
 25993 directionality is weak or neutral, such as numbers or punctuation
 25994 characters, can be forced to display in a very different place with
 25995 respect of its surrounding characters, so as to make the surrounding
 25996 text confuse the user regarding what the text says.
 25997 
 25998 Also see the `highlight-confusing-reorderings' function, which can be
 25999 useful in similar circumstances as this function.  */)
 26000   (Lisp_Object from, Lisp_Object to, Lisp_Object object, Lisp_Object base_dir)
 26001 {
 26002   struct buffer *buf = current_buffer;
 26003   struct buffer *old = buf;
 26004   struct window *w = NULL;
 26005   bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
 26006   struct bidi_it itb;
 26007   ptrdiff_t from_pos, to_pos, from_bpos;
 26008   void *itb_data;
 26009 
 26010   if (!NILP (object))
 26011     {
 26012       if (BUFFERP (object))
 26013         buf = XBUFFER (object);
 26014       else if (WINDOWP (object))
 26015         {
 26016           w = decode_live_window (object);
 26017           buf = XBUFFER (w->contents);
 26018           frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
 26019         }
 26020       else
 26021         CHECK_STRING (object);
 26022     }
 26023 
 26024   if (STRINGP (object))
 26025     {
 26026       /* Characters in unibyte strings are always treated by bidi.c as
 26027          strong LTR.  */
 26028       if (!STRING_MULTIBYTE (object)
 26029           /* When we are loading loadup.el, the character property
 26030              tables needed for bidi iteration are not yet
 26031              available.  */
 26032           || redisplay__inhibit_bidi)
 26033         return Qnil;
 26034 
 26035       validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
 26036       if (from_pos >= SCHARS (object))
 26037         return Qnil;
 26038 
 26039       /* Set up the bidi iterator.  */
 26040       itb_data = bidi_shelve_cache ();
 26041       itb.paragraph_dir = NEUTRAL_DIR;
 26042       itb.string.lstring = object;
 26043       itb.string.s = NULL;
 26044       itb.string.schars = SCHARS (object);
 26045       itb.string.bufpos = 0;
 26046       itb.string.from_disp_str = false;
 26047       itb.string.unibyte = false;
 26048       itb.w = w;
 26049       bidi_init_it (0, 0, frame_window_p, &itb);
 26050     }
 26051   else
 26052     {
 26053       /* Nothing this fancy can happen in unibyte buffers, or in a
 26054          buffer that disabled reordering, or if FROM is at EOB.  */
 26055       if (NILP (BVAR (buf, bidi_display_reordering))
 26056           || NILP (BVAR (buf, enable_multibyte_characters))
 26057           /* When we are loading loadup.el, the character property
 26058              tables needed for bidi iteration are not yet
 26059              available.  */
 26060           || redisplay__inhibit_bidi)
 26061         return Qnil;
 26062 
 26063       set_buffer_temp (buf);
 26064       validate_region (&from, &to);
 26065       from_pos = XFIXNUM (from);
 26066       to_pos = XFIXNUM (to);
 26067       if (from_pos >= ZV)
 26068         return Qnil;
 26069 
 26070       /* Set up the bidi iterator.  */
 26071       itb_data = bidi_shelve_cache ();
 26072       from_bpos = CHAR_TO_BYTE (from_pos);
 26073       if (from_pos == BEGV)
 26074         {
 26075           itb.charpos = BEGV;
 26076           itb.bytepos = BEGV_BYTE;
 26077         }
 26078       else if (FETCH_BYTE (from_bpos - 1) == '\n')
 26079         {
 26080           itb.charpos = from_pos;
 26081           itb.bytepos = from_bpos;
 26082         }
 26083       else
 26084         itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
 26085                                             -1, &itb.bytepos);
 26086       itb.paragraph_dir = NEUTRAL_DIR;
 26087       itb.string.s = NULL;
 26088       itb.string.lstring = Qnil;
 26089       itb.string.bufpos = 0;
 26090       itb.string.from_disp_str = false;
 26091       itb.string.unibyte = false;
 26092       itb.w = w;
 26093       bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
 26094     }
 26095 
 26096   ptrdiff_t found;
 26097   bidi_dir_t bdir = EQ (base_dir, Qright_to_left) ? R2L : L2R;
 26098   do {
 26099     bidi_paragraph_init (bdir, &itb, false);
 26100     while ((found = bidi_find_first_overridden (&itb)) < from_pos)
 26101       ;
 26102   } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
 26103 
 26104   bidi_unshelve_cache (itb_data, false);
 26105   set_buffer_temp (old);
 26106 
 26107   return (from_pos <= found && found < to_pos) ? make_fixnum (found) : Qnil;
 26108 }
 26109 
 26110 DEFUN ("move-point-visually", Fmove_point_visually,
 26111        Smove_point_visually, 1, 1, 0,
 26112        doc: /* Move point in the visual order in the specified DIRECTION.
 26113 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
 26114 left.
 26115 
 26116 Value is the new character position of point.  */)
 26117   (Lisp_Object direction)
 26118 {
 26119   struct window *w = XWINDOW (selected_window);
 26120   struct buffer *b = XBUFFER (w->contents);
 26121   struct glyph_row *row;
 26122   int dir;
 26123   Lisp_Object paragraph_dir;
 26124 
 26125 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)          \
 26126   (!(ROW)->continued_p                          \
 26127    && NILP ((GLYPH)->object)                    \
 26128    && (GLYPH)->type == CHAR_GLYPH               \
 26129    && (GLYPH)->u.ch == ' '                      \
 26130    && (GLYPH)->charpos >= 0                     \
 26131    && !(GLYPH)->avoid_cursor_p)
 26132 
 26133   CHECK_FIXNUM (direction);
 26134   dir = XFIXNUM (direction);
 26135   if (dir > 0)
 26136     dir = 1;
 26137   else
 26138     dir = -1;
 26139 
 26140   /* If current matrix is up-to-date, we can use the information
 26141      recorded in the glyphs, at least as long as the goal is on the
 26142      screen.  */
 26143   if (w->window_end_valid
 26144       && !windows_or_buffers_changed
 26145       && b
 26146       && !b->clip_changed
 26147       && !b->prevent_redisplay_optimizations_p
 26148       && !window_outdated (w)
 26149       /* We rely below on the cursor coordinates to be up to date, but
 26150          we cannot trust them if some command moved point since the
 26151          last complete redisplay.  */
 26152       && w->last_point == BUF_PT (b)
 26153       && w->cursor.vpos >= 0
 26154       && w->cursor.vpos < w->current_matrix->nrows
 26155       && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
 26156     {
 26157       struct glyph *g = row->glyphs[TEXT_AREA];
 26158       struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
 26159       struct glyph *gpt = g + w->cursor.hpos;
 26160 
 26161       for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
 26162         {
 26163           if (BUFFERP (g->object) && g->charpos != PT)
 26164             {
 26165               SET_PT (g->charpos);
 26166               w->cursor.vpos = -1;
 26167               return make_fixnum (PT);
 26168             }
 26169           else if (!NILP (g->object) && !EQ (g->object, gpt->object))
 26170             {
 26171               ptrdiff_t new_pos;
 26172 
 26173               if (BUFFERP (gpt->object))
 26174                 {
 26175                   new_pos = PT;
 26176                   if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
 26177                     new_pos += (row->reversed_p ? -dir : dir);
 26178                   else
 26179                     new_pos -= (row->reversed_p ? -dir : dir);
 26180                   new_pos = clip_to_bounds (BEGV, new_pos, ZV);
 26181                   /* If we didn't move, we've hit BEGV or ZV, so we
 26182                      need to signal a suitable error.  */
 26183                   if (new_pos == PT)
 26184                     break;
 26185                 }
 26186               else if (BUFFERP (g->object))
 26187                 new_pos = g->charpos;
 26188               else
 26189                 break;
 26190               SET_PT (new_pos);
 26191               w->cursor.vpos = -1;
 26192               return make_fixnum (PT);
 26193             }
 26194           else if (ROW_GLYPH_NEWLINE_P (row, g))
 26195             {
 26196               /* Glyphs inserted at the end of a non-empty line for
 26197                  positioning the cursor have zero charpos, so we must
 26198                  deduce the value of point by other means.  */
 26199               if (g->charpos > 0)
 26200                 SET_PT (g->charpos);
 26201               else if (row->ends_at_zv_p && PT != ZV)
 26202                 SET_PT (ZV);
 26203               else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
 26204                 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26205               else
 26206                 break;
 26207               w->cursor.vpos = -1;
 26208               return make_fixnum (PT);
 26209             }
 26210         }
 26211       if (g == e || NILP (g->object))
 26212         {
 26213           if (row->truncated_on_left_p || row->truncated_on_right_p)
 26214             goto simulate_display;
 26215           if (!row->reversed_p)
 26216             row += dir;
 26217           else
 26218             row -= dir;
 26219           if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
 26220                 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
 26221             goto simulate_display;
 26222 
 26223           if (dir > 0)
 26224             {
 26225               if (row->reversed_p && !row->continued_p)
 26226                 {
 26227                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26228                   w->cursor.vpos = -1;
 26229                   return make_fixnum (PT);
 26230                 }
 26231               g = row->glyphs[TEXT_AREA];
 26232               e = g + row->used[TEXT_AREA];
 26233               for ( ; g < e; g++)
 26234                 {
 26235                   if (BUFFERP (g->object)
 26236                       /* Empty lines have only one glyph, which stands
 26237                          for the newline, and whose charpos is the
 26238                          buffer position of the newline.  */
 26239                       || ROW_GLYPH_NEWLINE_P (row, g)
 26240                       /* When the buffer ends in a newline, the line at
 26241                          EOB also has one glyph, but its charpos is -1.  */
 26242                       || (row->ends_at_zv_p
 26243                           && !row->reversed_p
 26244                           && NILP (g->object)
 26245                           && g->type == CHAR_GLYPH
 26246                           && g->u.ch == ' '))
 26247                     {
 26248                       if (g->charpos > 0)
 26249                         SET_PT (g->charpos);
 26250                       else if (!row->reversed_p
 26251                                && row->ends_at_zv_p
 26252                                && PT != ZV)
 26253                         SET_PT (ZV);
 26254                       else
 26255                         continue;
 26256                       w->cursor.vpos = -1;
 26257                       return make_fixnum (PT);
 26258                     }
 26259                 }
 26260             }
 26261           else
 26262             {
 26263               if (!row->reversed_p && !row->continued_p)
 26264                 {
 26265                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26266                   w->cursor.vpos = -1;
 26267                   return make_fixnum (PT);
 26268                 }
 26269               e = row->glyphs[TEXT_AREA];
 26270               g = e + row->used[TEXT_AREA] - 1;
 26271               for ( ; g >= e; g--)
 26272                 {
 26273                   if (BUFFERP (g->object)
 26274                       || (ROW_GLYPH_NEWLINE_P (row, g)
 26275                           && g->charpos > 0)
 26276                       /* Empty R2L lines on GUI frames have the buffer
 26277                          position of the newline stored in the stretch
 26278                          glyph.  */
 26279                       || g->type == STRETCH_GLYPH
 26280                       || (row->ends_at_zv_p
 26281                           && row->reversed_p
 26282                           && NILP (g->object)
 26283                           && g->type == CHAR_GLYPH
 26284                           && g->u.ch == ' '))
 26285                     {
 26286                       if (g->charpos > 0)
 26287                         SET_PT (g->charpos);
 26288                       else if (row->reversed_p
 26289                                && row->ends_at_zv_p
 26290                                && PT != ZV)
 26291                         SET_PT (ZV);
 26292                       else
 26293                         continue;
 26294                       w->cursor.vpos = -1;
 26295                       return make_fixnum (PT);
 26296                     }
 26297                 }
 26298             }
 26299         }
 26300     }
 26301 
 26302  simulate_display:
 26303 
 26304   /* If we wind up here, we failed to move by using the glyphs, so we
 26305      need to simulate display instead.  */
 26306 
 26307   if (b)
 26308     paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
 26309   else
 26310     paragraph_dir = Qleft_to_right;
 26311   if (EQ (paragraph_dir, Qright_to_left))
 26312     dir = -dir;
 26313   if (PT <= BEGV && dir < 0)
 26314     xsignal0 (Qbeginning_of_buffer);
 26315   else if (PT >= ZV && dir > 0)
 26316     xsignal0 (Qend_of_buffer);
 26317   else
 26318     {
 26319       struct text_pos pt;
 26320       struct it it;
 26321       int pt_x, target_x, pixel_width, pt_vpos;
 26322       bool at_eol_p;
 26323       bool overshoot_expected = false;
 26324       bool target_is_eol_p = false;
 26325       void *itdata = bidi_shelve_cache ();
 26326 
 26327       /* Setup the arena.  */
 26328       SET_TEXT_POS (pt, PT, PT_BYTE);
 26329       start_display (&it, w, pt);
 26330       /* When lines are truncated, we could be called with point
 26331          outside of the windows edges, in which case move_it_*
 26332          functions either prematurely stop at window's edge or jump to
 26333          the next screen line, whereas we rely below on our ability to
 26334          reach point, in order to start from its X coordinate.  So we
 26335          need to disregard the window's horizontal extent in that case.  */
 26336       if (it.line_wrap == TRUNCATE)
 26337         it.last_visible_x = DISP_INFINITY;
 26338 
 26339       if (it.cmp_it.id < 0
 26340           && it.method == GET_FROM_STRING
 26341           && it.area == TEXT_AREA
 26342           && it.string_from_display_prop_p
 26343           && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
 26344         overshoot_expected = true;
 26345 
 26346       /* Find the X coordinate of point.  We start from the beginning
 26347          of this or previous line to make sure we are before point in
 26348          the logical order (since the move_it_* functions can only
 26349          move forward).  */
 26350     reseat:
 26351       reseat_at_previous_visible_line_start (&it);
 26352       it.current_x = it.hpos = it.current_y = it.vpos = 0;
 26353       if (IT_CHARPOS (it) != PT)
 26354         {
 26355           move_it_to (&it, overshoot_expected ? PT - 1 : PT,
 26356                       -1, -1, -1, MOVE_TO_POS);
 26357           /* If we missed point because the character there is
 26358              displayed out of a display vector that has more than one
 26359              glyph, retry expecting overshoot.  */
 26360           if (it.method == GET_FROM_DISPLAY_VECTOR
 26361               && it.current.dpvec_index > 0
 26362               && !overshoot_expected)
 26363             {
 26364               overshoot_expected = true;
 26365               goto reseat;
 26366             }
 26367           else if (IT_CHARPOS (it) != PT && !overshoot_expected)
 26368             move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
 26369         }
 26370       pt_x = it.current_x;
 26371       pt_vpos = it.vpos;
 26372       if (dir > 0 || overshoot_expected)
 26373         {
 26374           struct glyph_row *row = it.glyph_row;
 26375 
 26376           /* When point is at beginning of line, we don't have
 26377              information about the glyph there loaded into struct
 26378              it.  Calling get_next_display_element fixes that.  */
 26379           if (pt_x == 0)
 26380             get_next_display_element (&it);
 26381           at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26382           it.glyph_row = NULL;
 26383           PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
 26384           it.glyph_row = row;
 26385           /* PRODUCE_GLYPHS advances it.current_x, so we must restore
 26386              it, lest it will become out of sync with it's buffer
 26387              position.  */
 26388           it.current_x = pt_x;
 26389         }
 26390       else
 26391         at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26392       pixel_width = it.pixel_width;
 26393       if (overshoot_expected && at_eol_p)
 26394         pixel_width = 0;
 26395       else if (pixel_width <= 0)
 26396         pixel_width = 1;
 26397 
 26398       /* If there's a display string (or something similar) at point,
 26399          we are actually at the glyph to the left of point, so we need
 26400          to correct the X coordinate.  */
 26401       if (overshoot_expected)
 26402         {
 26403           if (it.bidi_p)
 26404             pt_x += pixel_width * it.bidi_it.scan_dir;
 26405           else
 26406             pt_x += pixel_width;
 26407         }
 26408 
 26409       /* Compute target X coordinate, either to the left or to the
 26410          right of point.  On TTY frames, all characters have the same
 26411          pixel width of 1, so we can use that.  On GUI frames we don't
 26412          have an easy way of getting at the pixel width of the
 26413          character to the left of point, so we use a different method
 26414          of getting to that place.  */
 26415       if (dir > 0)
 26416         target_x = pt_x + pixel_width;
 26417       else
 26418         target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
 26419 
 26420       /* Target X coordinate could be one line above or below the line
 26421          of point, in which case we need to adjust the target X
 26422          coordinate.  Also, if moving to the left, we need to begin at
 26423          the left edge of the point's screen line.  */
 26424       if (dir < 0)
 26425         {
 26426           if (pt_x > 0)
 26427             {
 26428               start_display (&it, w, pt);
 26429               if (it.line_wrap == TRUNCATE)
 26430                 it.last_visible_x = DISP_INFINITY;
 26431               reseat_at_previous_visible_line_start (&it);
 26432               it.current_x = it.current_y = it.hpos = 0;
 26433               if (pt_vpos != 0)
 26434                 move_it_by_lines (&it, pt_vpos);
 26435             }
 26436           else
 26437             {
 26438               move_it_by_lines (&it, -1);
 26439               target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
 26440               target_is_eol_p = true;
 26441               /* Under word-wrap, we don't know the x coordinate of
 26442                  the last character displayed on the previous line,
 26443                  which immediately precedes the wrap point.  To find
 26444                  out its x coordinate, we try moving to the right
 26445                  margin of the window, which will stop at the wrap
 26446                  point, and then reset target_x to point at the
 26447                  character that precedes the wrap point.  This is not
 26448                  needed on GUI frames, because (see below) there we
 26449                  move from the left margin one grapheme cluster at a
 26450                  time, and stop when we hit the wrap point.  */
 26451               if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
 26452                 {
 26453                   void *it_data = NULL;
 26454                   struct it it2;
 26455 
 26456                   SAVE_IT (it2, it, it_data);
 26457                   move_it_in_display_line_to (&it, ZV, target_x,
 26458                                               MOVE_TO_POS | MOVE_TO_X);
 26459                   /* If we arrived at target_x, that _is_ the last
 26460                      character on the previous line.  */
 26461                   if (it.current_x != target_x)
 26462                     target_x = it.current_x - 1;
 26463                   RESTORE_IT (&it, &it2, it_data);
 26464                 }
 26465             }
 26466         }
 26467       else
 26468         {
 26469           if (at_eol_p
 26470               || (target_x >= it.last_visible_x
 26471                   && it.line_wrap != TRUNCATE))
 26472             {
 26473               if (pt_x > 0)
 26474                 move_it_by_lines (&it, 0);
 26475               move_it_by_lines (&it, 1);
 26476               target_x = 0;
 26477             }
 26478         }
 26479 
 26480       /* Move to the target X coordinate.  */
 26481       /* On GUI frames, as we don't know the X coordinate of the
 26482          character to the left of point, moving point to the left
 26483          requires walking, one grapheme cluster at a time, until we
 26484          find ourself at a place immediately to the left of the
 26485          character at point.  */
 26486       if (FRAME_WINDOW_P (it.f) && dir < 0)
 26487         {
 26488           struct text_pos new_pos;
 26489           enum move_it_result rc = MOVE_X_REACHED;
 26490 
 26491           if (it.current_x == 0)
 26492             get_next_display_element (&it);
 26493           if (it.what == IT_COMPOSITION)
 26494             {
 26495               new_pos.charpos = it.cmp_it.charpos;
 26496               new_pos.bytepos = -1;
 26497             }
 26498           else
 26499             new_pos = it.current.pos;
 26500 
 26501           while (it.current_x + it.pixel_width <= target_x
 26502                  && (rc == MOVE_X_REACHED
 26503                      /* Under word-wrap, move_it_in_display_line_to
 26504                         stops at correct coordinates, but sometimes
 26505                         returns MOVE_POS_MATCH_OR_ZV.  */
 26506                      || (it.line_wrap == WORD_WRAP
 26507                          && rc == MOVE_POS_MATCH_OR_ZV)))
 26508             {
 26509               int new_x = it.current_x + it.pixel_width;
 26510 
 26511               /* For composed characters, we want the position of the
 26512                  first character in the grapheme cluster (usually, the
 26513                  composition's base character), whereas it.current
 26514                  might give us the position of the _last_ one, e.g. if
 26515                  the composition is rendered in reverse due to bidi
 26516                  reordering.  */
 26517               if (it.what == IT_COMPOSITION)
 26518                 {
 26519                   new_pos.charpos = it.cmp_it.charpos;
 26520                   new_pos.bytepos = -1;
 26521                 }
 26522               else
 26523                 new_pos = it.current.pos;
 26524               if (new_x == it.current_x)
 26525                 new_x++;
 26526               rc = move_it_in_display_line_to (&it, ZV, new_x,
 26527                                                MOVE_TO_POS | MOVE_TO_X);
 26528               if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
 26529                 break;
 26530             }
 26531           /* The previous position we saw in the loop is the one we
 26532              want.  */
 26533           if (new_pos.bytepos == -1)
 26534             new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
 26535           it.current.pos = new_pos;
 26536         }
 26537       else if (it.current_x != target_x)
 26538         move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
 26539 
 26540       /* If we ended up in a display string that covers point, move to
 26541          buffer position to the right in the visual order.  */
 26542       if (dir > 0)
 26543         {
 26544           while (IT_CHARPOS (it) == PT)
 26545             {
 26546               set_iterator_to_next (&it, false);
 26547               if (!get_next_display_element (&it))
 26548                 break;
 26549             }
 26550         }
 26551 
 26552       /* Move point to that position.  */
 26553       SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
 26554       bidi_unshelve_cache (itdata, false);
 26555     }
 26556 
 26557   return make_fixnum (PT);
 26558 
 26559 #undef ROW_GLYPH_NEWLINE_P
 26560 }
 26561 
 26562 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
 26563        Sbidi_resolved_levels, 0, 1, 0,
 26564        doc: /* Return the resolved bidirectional levels of characters at VPOS.
 26565 
 26566 The resolved levels are produced by the Emacs bidi reordering engine
 26567 that implements the UBA, the Unicode Bidirectional Algorithm.  Please
 26568 read the Unicode Standard Annex 9 (UAX#9) for background information
 26569 about these levels.
 26570 
 26571 VPOS is the zero-based number of the current window's screen line
 26572 for which to produce the resolved levels.  If VPOS is nil or omitted,
 26573 it defaults to the screen line of point.  If the window displays a
 26574 header line, VPOS of zero will report on the header line, and first
 26575 line of text in the window will have VPOS of 1.
 26576 
 26577 Value is an array of resolved levels, indexed by glyph number.
 26578 Glyphs are numbered from zero starting from the beginning of the
 26579 screen line, i.e. the left edge of the window for left-to-right lines
 26580 and from the right edge for right-to-left lines.  The resolved levels
 26581 are produced only for the window's text area; text in display margins
 26582 is not included.
 26583 
 26584 If the selected window's display is not up-to-date, or if the specified
 26585 screen line does not display text, this function returns nil.  It is
 26586 highly recommended to bind this function to some simple key, like F8,
 26587 in order to avoid these problems.
 26588 
 26589 This function exists mainly for testing the correctness of the
 26590 Emacs UBA implementation, in particular with the test suite.  */)
 26591   (Lisp_Object vpos)
 26592 {
 26593   struct window *w = XWINDOW (selected_window);
 26594   struct buffer *b = XBUFFER (w->contents);
 26595   int nrow;
 26596   struct glyph_row *row;
 26597 
 26598   if (NILP (vpos))
 26599     {
 26600       int d1, d2, d3, d4, d5;
 26601 
 26602       pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
 26603     }
 26604   else
 26605     {
 26606       CHECK_FIXNUM (vpos);
 26607       nrow = XFIXNUM (vpos);
 26608     }
 26609 
 26610   /* We require up-to-date glyph matrix for this window.  */
 26611   if (w->window_end_valid
 26612       && !windows_or_buffers_changed
 26613       && b
 26614       && !b->clip_changed
 26615       && !b->prevent_redisplay_optimizations_p
 26616       && !window_outdated (w)
 26617       && nrow >= 0
 26618       && nrow < w->current_matrix->nrows
 26619       && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
 26620       && MATRIX_ROW_DISPLAYS_TEXT_P (row))
 26621     {
 26622       struct glyph *g, *e, *g1;
 26623       int nglyphs, i;
 26624       Lisp_Object levels;
 26625 
 26626       if (!row->reversed_p)     /* Left-to-right glyph row.  */
 26627         {
 26628           g = g1 = row->glyphs[TEXT_AREA];
 26629           e = g + row->used[TEXT_AREA];
 26630 
 26631           /* Skip over glyphs at the start of the row that was
 26632              generated by redisplay for its own needs.  */
 26633           while (g < e
 26634                  && NILP (g->object)
 26635                  && g->charpos < 0)
 26636             g++;
 26637           g1 = g;
 26638 
 26639           /* Count the "interesting" glyphs in this row.  */
 26640           for (nglyphs = 0; g < e && !NILP (g->object); g++)
 26641             nglyphs++;
 26642 
 26643           /* Create and fill the array.  */
 26644           levels = make_uninit_vector (nglyphs);
 26645           for (i = 0; g1 < g; i++, g1++)
 26646             ASET (levels, i, make_fixnum (g1->resolved_level));
 26647         }
 26648       else                      /* Right-to-left glyph row.  */
 26649         {
 26650           g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 26651           e = row->glyphs[TEXT_AREA] - 1;
 26652           while (g > e
 26653                  && NILP (g->object)
 26654                  && g->charpos < 0)
 26655             g--;
 26656           g1 = g;
 26657           for (nglyphs = 0; g > e && !NILP (g->object); g--)
 26658             nglyphs++;
 26659           levels = make_uninit_vector (nglyphs);
 26660           for (i = 0; g1 > g; i++, g1--)
 26661             ASET (levels, i, make_fixnum (g1->resolved_level));
 26662         }
 26663       return levels;
 26664     }
 26665   else
 26666     return Qnil;
 26667 }
 26668 
 26669 
 26670 
 26671 /***********************************************************************
 26672                                Menu Bar
 26673  ***********************************************************************/
 26674 
 26675 /* Redisplay the menu bar in the frame for window W.
 26676 
 26677    The menu bar of X frames that don't have X toolkit support is
 26678    displayed in a special window W->frame->menu_bar_window.
 26679 
 26680    The menu bar of terminal frames is treated specially as far as
 26681    glyph matrices are concerned.  Menu bar lines are not part of
 26682    windows, so the update is done directly on the frame matrix rows
 26683    for the menu bar.  */
 26684 
 26685 static void
 26686 display_menu_bar (struct window *w)
 26687 {
 26688   struct frame *f = XFRAME (WINDOW_FRAME (w));
 26689   struct it it;
 26690   Lisp_Object items;
 26691   int i;
 26692 
 26693   /* Don't do all this for graphical frames.  */
 26694 #ifdef HAVE_NTGUI
 26695   if (FRAME_W32_P (f))
 26696     return;
 26697 #endif
 26698 #if defined (HAVE_PGTK)
 26699   if (FRAME_PGTK_P (f))
 26700     return;
 26701 #endif
 26702 
 26703 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26704   if (FRAME_X_P (f))
 26705     return;
 26706 #endif
 26707 
 26708 #ifdef HAVE_NS
 26709   if (FRAME_NS_P (f))
 26710     return;
 26711 #endif /* HAVE_NS */
 26712 
 26713 #ifdef HAVE_HAIKU
 26714   if (FRAME_HAIKU_P (f))
 26715     return;
 26716 #endif /* HAVE_HAIKU */
 26717 
 26718 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26719   eassert (!FRAME_WINDOW_P (f));
 26720   init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
 26721   it.first_visible_x = 0;
 26722   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26723 #elif defined (HAVE_X_WINDOWS) || defined (HAVE_ANDROID)
 26724   struct window *menu_window = NULL;
 26725   struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
 26726 
 26727   if (FRAME_WINDOW_P (f))
 26728     {
 26729       /* Menu bar lines are displayed in the desired matrix of the
 26730          dummy window menu_bar_window.  */
 26731       menu_window = XWINDOW (f->menu_bar_window);
 26732       init_iterator (&it, menu_window, -1, -1,
 26733                      menu_window->desired_matrix->rows,
 26734                      MENU_FACE_ID);
 26735     }
 26736   else
 26737 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 26738     {
 26739       /* This is a TTY frame, i.e. character hpos/vpos are used as
 26740          pixel x/y.  */
 26741       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
 26742                      MENU_FACE_ID);
 26743       it.first_visible_x = 0;
 26744       it.last_visible_x = FRAME_COLS (f);
 26745     }
 26746 
 26747   /* FIXME: This should be controlled by a user option.  See the
 26748      comments in redisplay_tool_bar and display_mode_line about
 26749      this.  */
 26750   it.paragraph_embedding = L2R;
 26751 
 26752   /* Clear all rows of the menu bar.  */
 26753   for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
 26754     {
 26755       struct glyph_row *row = it.glyph_row + i;
 26756       clear_glyph_row (row);
 26757       row->enabled_p = true;
 26758       row->full_width_p = true;
 26759       row->reversed_p = false;
 26760     }
 26761 
 26762   /* Display all items of the menu bar.  */
 26763   items = FRAME_MENU_BAR_ITEMS (it.f);
 26764   for (i = 0; i < ASIZE (items); i += 4)
 26765     {
 26766       Lisp_Object string;
 26767 
 26768       /* Stop at nil string.  */
 26769       string = AREF (items, i + 1);
 26770       if (NILP (string))
 26771         break;
 26772 
 26773       /* Remember where item was displayed.  */
 26774       ASET (items, i + 3, make_fixnum (it.hpos));
 26775 
 26776       /* Display the item, pad with one space.  */
 26777       if (it.current_x < it.last_visible_x)
 26778         display_string (NULL, string, Qnil, 0, 0, &it,
 26779                         SCHARS (string) + 1, 0, 0, STRING_MULTIBYTE (string));
 26780     }
 26781 
 26782   /* Fill out the line with spaces.  */
 26783   if (it.current_x < it.last_visible_x)
 26784     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 26785 
 26786   /* Compute the total height of the lines.  */
 26787   compute_line_metrics (&it);
 26788   it.glyph_row->full_width_p = true;
 26789   it.glyph_row->continued_p = false;
 26790   it.glyph_row->truncated_on_left_p = false;
 26791   it.glyph_row->truncated_on_right_p = false;
 26792 
 26793   /* This will break the moment someone tries to add another window
 26794      system that uses the no toolkit menu bar.  Oh well.  At least
 26795      there will be an error, meaning he will correct the ifdef inside
 26796      which `face' is defined.  */
 26797 #if defined HAVE_WINDOW_SYSTEM && !defined HAVE_EXT_MENU_BAR
 26798   /* Make a 3D menu bar have a shadow at its right end.  */
 26799   extend_face_to_end_of_line (&it);
 26800   if (face->box != FACE_NO_BOX)
 26801     {
 26802       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 26803                             + it.glyph_row->used[TEXT_AREA] - 1);
 26804       int box_thickness = face->box_vertical_line_width;
 26805       last->right_box_line_p = true;
 26806       /* Add back the space for the right box line we subtracted in
 26807          init_iterator, since the right_box_line_p flag will make the
 26808          glyph wider.  We actually add only as much space as is
 26809          available for the last glyph of the menu bar and whatever
 26810          space is left beyond it, since that glyph could be only
 26811          partially visible.  */
 26812       if (box_thickness > 0)
 26813         last->pixel_width += max (0, (box_thickness
 26814                                       - (it.current_x - it.last_visible_x)));
 26815     }
 26816 
 26817   /* With the non-toolkit version, modify the menu bar window height
 26818      accordingly.  */
 26819   if (FRAME_WINDOW_P (it.f) && menu_window)
 26820     {
 26821       struct glyph_row *row;
 26822       int delta_height;
 26823 
 26824       row = it.glyph_row;
 26825       delta_height
 26826         = ((row->y + row->height)
 26827            - WINDOW_BOX_HEIGHT_NO_MODE_LINE (menu_window));
 26828 
 26829       if (delta_height != 0)
 26830         {
 26831           FRAME_MENU_BAR_HEIGHT (it.f) += delta_height;
 26832           adjust_frame_size (it.f, -1, -1, 3, false, Qmenu_bar_lines);
 26833         }
 26834     }
 26835 #endif
 26836 }
 26837 
 26838 /* This code is never used on Android where there are only GUI and
 26839    initial frames.  */
 26840 
 26841 #ifndef HAVE_ANDROID
 26842 
 26843 /* Deep copy of a glyph row, including the glyphs.  */
 26844 static void
 26845 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
 26846 {
 26847   struct glyph *pointers[1 + LAST_AREA];
 26848   int to_used = to->used[TEXT_AREA];
 26849 
 26850   /* Save glyph pointers of TO.  */
 26851   memcpy (pointers, to->glyphs, sizeof to->glyphs);
 26852 
 26853   /* Do a structure assignment.  */
 26854   *to = *from;
 26855 
 26856   /* Restore original glyph pointers of TO.  */
 26857   memcpy (to->glyphs, pointers, sizeof to->glyphs);
 26858 
 26859   /* Copy the glyphs.  */
 26860   memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
 26861           min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
 26862 
 26863   /* If we filled only part of the TO row, fill the rest with
 26864      space_glyph (which will display as empty space).  */
 26865   if (to_used > from->used[TEXT_AREA])
 26866     fill_up_frame_row_with_spaces (to, to_used);
 26867 }
 26868 
 26869 /* Display one menu item on a TTY, by overwriting the glyphs in the
 26870    frame F's desired glyph matrix with glyphs produced from the menu
 26871    item text.  Called from term.c to display TTY drop-down menus one
 26872    item at a time.
 26873 
 26874    ITEM_TEXT is the menu item text as a C string.
 26875 
 26876    FACE_ID is the face ID to be used for this menu item.  FACE_ID
 26877    could specify one of 3 faces: a face for an enabled item, a face
 26878    for a disabled item, or a face for a selected item.
 26879 
 26880    X and Y are coordinates of the first glyph in the frame's desired
 26881    matrix to be overwritten by the menu item.  Since this is a TTY, Y
 26882    is the zero-based number of the glyph row and X is the zero-based
 26883    glyph number in the row, starting from left, where to start
 26884    displaying the item.
 26885 
 26886    SUBMENU means this menu item drops down a submenu, which
 26887    should be indicated by displaying a proper visual cue after the
 26888    item text.  */
 26889 
 26890 void
 26891 display_tty_menu_item (const char *item_text, int width, int face_id,
 26892                        int x, int y, bool submenu)
 26893 {
 26894   struct it it;
 26895   struct frame *f = SELECTED_FRAME ();
 26896   struct window *w = XWINDOW (f->selected_window);
 26897   struct glyph_row *row;
 26898   size_t item_len = strlen (item_text);
 26899 
 26900   eassert (FRAME_TERMCAP_P (f));
 26901 
 26902   /* Don't write beyond the matrix's last row.  This can happen for
 26903      TTY screens that are not high enough to show the entire menu.
 26904      (This is actually a bit of defensive programming, as
 26905      tty_menu_display already limits the number of menu items to one
 26906      less than the number of screen lines.)  */
 26907   if (y >= f->desired_matrix->nrows)
 26908     return;
 26909 
 26910   init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
 26911   it.first_visible_x = 0;
 26912   it.last_visible_x = FRAME_COLS (f) - 1;
 26913   row = it.glyph_row;
 26914   /* Start with the row contents from the current matrix.  */
 26915   deep_copy_glyph_row (row, f->current_matrix->rows + y);
 26916   bool saved_width = row->full_width_p;
 26917   row->full_width_p = true;
 26918   bool saved_reversed = row->reversed_p;
 26919   row->reversed_p = false;
 26920   row->enabled_p = true;
 26921 
 26922   /* Arrange for the menu item glyphs to start at (X,Y) and have the
 26923      desired face.  */
 26924   eassert (x < f->desired_matrix->matrix_w);
 26925   it.current_x = it.hpos = x;
 26926   it.current_y = it.vpos = y;
 26927   int saved_used = row->used[TEXT_AREA];
 26928   bool saved_truncated = row->truncated_on_right_p;
 26929   row->used[TEXT_AREA] = x;
 26930   it.face_id = face_id;
 26931   it.line_wrap = TRUNCATE;
 26932 
 26933   /* FIXME: This should be controlled by a user option.  See the
 26934      comments in redisplay_tool_bar and display_mode_line about this.
 26935      Also, if paragraph_embedding could ever be R2L, changes will be
 26936      needed to avoid shifting to the right the row characters in
 26937      term.c:append_glyph.  */
 26938   it.paragraph_embedding = L2R;
 26939 
 26940   /* Pad with a space on the left.  */
 26941   display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
 26942   width--;
 26943   /* Display the menu item, pad with spaces to WIDTH.  */
 26944   if (submenu)
 26945     {
 26946       display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26947                       item_len, 0, FRAME_COLS (f) - 1, -1);
 26948       width -= item_len;
 26949       /* Indicate with " >" that there's a submenu.  */
 26950       display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
 26951                       FRAME_COLS (f) - 1, -1);
 26952     }
 26953   else
 26954     display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26955                     width, 0, FRAME_COLS (f) - 1, -1);
 26956 
 26957   row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
 26958   row->truncated_on_right_p = saved_truncated;
 26959   row->hash = row_hash (row);
 26960   row->full_width_p = saved_width;
 26961   row->reversed_p = saved_reversed;
 26962 }
 26963 
 26964 #endif
 26965 
 26966 
 26967 /***********************************************************************
 26968                               Mode Line
 26969  ***********************************************************************/
 26970 
 26971 /* Redisplay mode lines in the window tree whose root is WINDOW.
 26972    If FORCE, redisplay mode lines unconditionally.
 26973    Otherwise, redisplay only mode lines that are garbaged.  Value is
 26974    the number of windows whose mode lines were redisplayed.  */
 26975 
 26976 static int
 26977 redisplay_mode_lines (Lisp_Object window, bool force)
 26978 {
 26979   int nwindows = 0;
 26980 
 26981   while (!NILP (window))
 26982     {
 26983       struct window *w = XWINDOW (window);
 26984 
 26985       if (WINDOWP (w->contents))
 26986         nwindows += redisplay_mode_lines (w->contents, force);
 26987       else if (force
 26988                || FRAME_GARBAGED_P (XFRAME (w->frame))
 26989                || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
 26990         {
 26991           struct text_pos lpoint;
 26992           struct buffer *old = current_buffer;
 26993 
 26994           /* Set the window's buffer for the mode line display.  */
 26995           SET_TEXT_POS (lpoint, PT, PT_BYTE);
 26996           set_buffer_internal_1 (XBUFFER (w->contents));
 26997 
 26998           /* Point refers normally to the selected window.  For any
 26999              other window, set up appropriate value.  */
 27000           if (!EQ (window, selected_window))
 27001             {
 27002               struct text_pos pt;
 27003 
 27004               CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
 27005               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
 27006             }
 27007 
 27008           /* Display mode lines.  */
 27009           clear_glyph_matrix (w->desired_matrix);
 27010           if (display_mode_lines (w))
 27011             ++nwindows;
 27012 
 27013           /* Restore old settings.  */
 27014           set_buffer_internal_1 (old);
 27015           TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 27016         }
 27017 
 27018       window = w->next;
 27019     }
 27020 
 27021   return nwindows;
 27022 }
 27023 
 27024 
 27025 /* Display the mode line, the header line, and the tab-line of window
 27026    W.  Value is the sum number of mode lines, header lines, and tab
 27027    lines actually displayed.  */
 27028 
 27029 static int
 27030 display_mode_lines (struct window *w)
 27031 {
 27032   Lisp_Object old_selected_window = selected_window;
 27033   Lisp_Object new_frame = w->frame;
 27034   specpdl_ref count = SPECPDL_INDEX ();
 27035   int n = 0;
 27036 
 27037   record_unwind_protect (restore_selected_window, selected_window);
 27038   record_unwind_protect
 27039     (restore_frame_selected_window, XFRAME (new_frame)->selected_window);
 27040 
 27041   if (window_wants_mode_line (w))
 27042     {
 27043       Lisp_Object window;
 27044       Lisp_Object default_help
 27045         = buffer_local_value (Qmode_line_default_help_echo, w->contents);
 27046 
 27047       /* Set up mode line help echo.  Do this before selecting w so it
 27048          can reasonably tell whether a mouse click will select w.  */
 27049       XSETWINDOW (window, w);
 27050       if (FUNCTIONP (default_help))
 27051         wset_mode_line_help_echo (w, safe_call1 (default_help, window));
 27052       else if (STRINGP (default_help))
 27053         wset_mode_line_help_echo (w, default_help);
 27054       else
 27055         wset_mode_line_help_echo (w, Qnil);
 27056     }
 27057 
 27058   selected_frame = new_frame;
 27059   /* FIXME: If we were to allow the mode-line's computation changing the buffer
 27060      or window's point, then we'd need select_window_1 here as well.  */
 27061   XSETWINDOW (selected_window, w);
 27062   XFRAME (new_frame)->selected_window = selected_window;
 27063 
 27064   /* These will be set while the mode line specs are processed.  */
 27065   line_number_displayed = false;
 27066   w->column_number_displayed = -1;
 27067 
 27068   if (window_wants_mode_line (w))
 27069     {
 27070       Lisp_Object window_mode_line_format
 27071         = window_parameter (w, Qmode_line_format);
 27072       struct window *sel_w = XWINDOW (old_selected_window);
 27073 
 27074       /* Select mode line face based on the real selected window.  */
 27075       display_mode_line (w,
 27076                          CURRENT_MODE_LINE_ACTIVE_FACE_ID_3 (sel_w, sel_w, w),
 27077                          NILP (window_mode_line_format)
 27078                          ? BVAR (current_buffer, mode_line_format)
 27079                          : window_mode_line_format);
 27080       ++n;
 27081     }
 27082 
 27083   if (window_wants_tab_line (w))
 27084     {
 27085       Lisp_Object window_tab_line_format
 27086         = window_parameter (w, Qtab_line_format);
 27087 
 27088       display_mode_line (w, TAB_LINE_FACE_ID,
 27089                          NILP (window_tab_line_format)
 27090                          ? BVAR (current_buffer, tab_line_format)
 27091                          : window_tab_line_format);
 27092       ++n;
 27093     }
 27094 
 27095   if (window_wants_header_line (w))
 27096     {
 27097       Lisp_Object window_header_line_format
 27098         = window_parameter (w, Qheader_line_format);
 27099 
 27100       display_mode_line (w, HEADER_LINE_FACE_ID,
 27101                          NILP (window_header_line_format)
 27102                          ? BVAR (current_buffer, header_line_format)
 27103                          : window_header_line_format);
 27104       ++n;
 27105     }
 27106 
 27107   unbind_to (count, Qnil);
 27108 
 27109   if (n > 0)
 27110     w->must_be_updated_p = true;
 27111   return n;
 27112 }
 27113 
 27114 
 27115 /* Display mode or header/tab line of window W.  FACE_ID specifies
 27116    which line to display; it is either MODE_LINE_ACTIVE_FACE_ID,
 27117    HEADER_LINE_FACE_ID or TAB_LINE_FACE_ID.  FORMAT is the
 27118    mode/header/tab line format to display.  Value is the pixel height
 27119    of the mode/header/tab line displayed.  */
 27120 
 27121 static int
 27122 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 27123 {
 27124   struct it it;
 27125   struct face *face;
 27126   specpdl_ref count = SPECPDL_INDEX ();
 27127 
 27128   init_iterator (&it, w, -1, -1, NULL, face_id);
 27129   /* Don't extend on a previously drawn mode-line.
 27130      This may happen if called from pos_visible_p.  */
 27131   it.glyph_row->enabled_p = false;
 27132   prepare_desired_row (w, it.glyph_row, true);
 27133 
 27134   it.glyph_row->mode_line_p = true;
 27135   if (face_id == TAB_LINE_FACE_ID)
 27136     {
 27137       it.glyph_row->tab_line_p = true;
 27138       w->desired_matrix->tab_line_p = true;
 27139     }
 27140   else if (face_id == HEADER_LINE_FACE_ID)
 27141     w->desired_matrix->header_line_p = true;
 27142 
 27143   /* FIXME: This should be controlled by a user option.  But
 27144      supporting such an option is not trivial, since the mode line is
 27145      made up of many separate strings.  */
 27146   it.paragraph_embedding = L2R;
 27147 
 27148   record_unwind_protect (unwind_format_mode_line,
 27149                          format_mode_line_unwind_data (NULL, NULL,
 27150                                                        Qnil, false));
 27151 
 27152   /* Temporarily make frame's keyboard the current kboard so that
 27153      kboard-local variables in the mode_line_format will get the right
 27154      values.  */
 27155   push_kboard (FRAME_KBOARD (it.f));
 27156   record_unwind_save_match_data ();
 27157 
 27158   if (NILP (Vmode_line_compact)
 27159       || face_id == HEADER_LINE_FACE_ID || face_id == TAB_LINE_FACE_ID)
 27160     {
 27161       mode_line_target = MODE_LINE_DISPLAY;
 27162       display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27163     }
 27164   else
 27165     {
 27166       Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
 27167       if (EQ (Vmode_line_compact, Qlong)
 27168           && WINDOW_TOTAL_COLS (w) >= SCHARS (mode_string))
 27169         {
 27170           /* The window is wide enough; just display the mode line we
 27171              just computed. */
 27172           display_string (NULL, mode_string, Qnil,
 27173                           0, 0, &it, 0, 0, 0,
 27174                           STRING_MULTIBYTE (mode_string));
 27175         }
 27176       else
 27177         {
 27178           /* Compress the mode line. */
 27179           ptrdiff_t i = 0, i_byte = 0, start = 0;
 27180           int prev = 0;
 27181 
 27182           while (i < SCHARS (mode_string))
 27183             {
 27184               int c = fetch_string_char_advance (mode_string, &i, &i_byte);
 27185               if (c == ' ' && prev == ' ')
 27186                 {
 27187                   display_string (NULL,
 27188                                   Fsubstring (mode_string, make_fixnum (start),
 27189                                               make_fixnum (i - 1)),
 27190                                   Qnil, 0, 0, &it, 0, 0, 0,
 27191                                   STRING_MULTIBYTE (mode_string));
 27192                   /* Skip past the rest of the space characters. */
 27193                   while (c == ' ' && i < SCHARS (mode_string))
 27194                       c = fetch_string_char_advance (mode_string, &i, &i_byte);
 27195                   start = i - 1;
 27196                 }
 27197               prev = c;
 27198             }
 27199 
 27200           /* Display the final bit. */
 27201           if (start < i)
 27202             display_string (NULL,
 27203                             Fsubstring (mode_string, make_fixnum (start),
 27204                                         make_fixnum (i)),
 27205                             Qnil, 0, 0, &it, 0, 0, 0,
 27206                             STRING_MULTIBYTE (mode_string));
 27207         }
 27208     }
 27209   pop_kboard ();
 27210 
 27211   unbind_to (count, Qnil);
 27212 
 27213   /* Fill up with spaces.  */
 27214   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
 27215 
 27216   compute_line_metrics (&it);
 27217   it.glyph_row->full_width_p = true;
 27218   it.glyph_row->continued_p = false;
 27219   it.glyph_row->truncated_on_left_p = false;
 27220   it.glyph_row->truncated_on_right_p = false;
 27221 
 27222   /* Make a 3D mode-line have a shadow at its right end.  */
 27223   face = FACE_FROM_ID (it.f, face_id);
 27224   extend_face_to_end_of_line (&it);
 27225   if (face->box != FACE_NO_BOX)
 27226     {
 27227       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 27228                             + it.glyph_row->used[TEXT_AREA] - 1);
 27229       int box_thickness = face->box_vertical_line_width;
 27230       last->right_box_line_p = true;
 27231       /* Add back the space for the right box line we subtracted in
 27232          init_iterator, since the right_box_line_p flag will make the
 27233          glyph wider.  We actually add only as much space as is
 27234          available for the last glyph of the modeline and whatever
 27235          space is left beyond it, since that glyph could be only
 27236          partially visible */
 27237       if (box_thickness > 0)
 27238         last->pixel_width += max (0, (box_thickness
 27239                                       - (it.current_x - it.last_visible_x)));
 27240     }
 27241 
 27242   return it.glyph_row->height;
 27243 }
 27244 
 27245 /* Move element ELT in LIST to the front of LIST.
 27246    Return the updated list.  */
 27247 
 27248 static Lisp_Object
 27249 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
 27250 {
 27251   register Lisp_Object tail, prev;
 27252   register Lisp_Object tem;
 27253 
 27254   tail = list;
 27255   prev = Qnil;
 27256   while (CONSP (tail))
 27257     {
 27258       tem = XCAR (tail);
 27259 
 27260       if (EQ (elt, tem))
 27261         {
 27262           /* Splice out the link TAIL.  */
 27263           if (NILP (prev))
 27264             list = XCDR (tail);
 27265           else
 27266             Fsetcdr (prev, XCDR (tail));
 27267 
 27268           /* Now make it the first.  */
 27269           Fsetcdr (tail, list);
 27270           return tail;
 27271         }
 27272       else
 27273         prev = tail;
 27274       tail = XCDR (tail);
 27275       maybe_quit ();
 27276     }
 27277 
 27278   /* Not found--return unchanged LIST.  */
 27279   return list;
 27280 }
 27281 
 27282 /* Subroutine to call Fset_text_properties through
 27283    internal_condition_case_n.  ARGS are the arguments of
 27284    Fset_text_properties, in order.  */
 27285 
 27286 static Lisp_Object
 27287 safe_set_text_properties (ptrdiff_t nargs, Lisp_Object *args)
 27288 {
 27289   eassert (nargs == 4);
 27290   return Fset_text_properties (args[0], args[1], args[2], args[3]);
 27291 }
 27292 
 27293 /* Contribute ELT to the mode line for window IT->w.  How it
 27294    translates into text depends on its data type.
 27295 
 27296    IT describes the display environment in which we display, as usual.
 27297 
 27298    DEPTH is the depth in recursion.  It is used to prevent
 27299    infinite recursion here.
 27300 
 27301    FIELD_WIDTH is the number of characters the display of ELT should
 27302    occupy in the mode line, and PRECISION is the maximum number of
 27303    characters to display from ELT's representation.  See
 27304    display_string for details.
 27305 
 27306    Returns the hpos of the end of the text generated by ELT.
 27307 
 27308    PROPS is a property list to add to any string we encounter.
 27309 
 27310    If RISKY, remove (disregard) any properties in any string
 27311    we encounter, and ignore :eval and :propertize.
 27312 
 27313    The global variable `mode_line_target' determines whether the
 27314    output is passed to `store_mode_line_noprop',
 27315    `store_mode_line_string', or `display_string'.  */
 27316 
 27317 static int
 27318 display_mode_element (struct it *it, int depth, int field_width, int precision,
 27319                       Lisp_Object elt, Lisp_Object props, bool risky)
 27320 {
 27321   int n = 0, field, prec;
 27322   bool literal = false;
 27323 
 27324  tail_recurse:
 27325   if (depth > 100)
 27326     elt = build_string ("*too-deep*");
 27327 
 27328   depth++;
 27329 
 27330   switch (XTYPE (elt))
 27331     {
 27332     case Lisp_String:
 27333       {
 27334         /* A string: output it and check for %-constructs within it.  */
 27335         unsigned char c;
 27336         ptrdiff_t offset = 0;
 27337 
 27338         if (SCHARS (elt) > 0
 27339             && (!NILP (props) || risky))
 27340           {
 27341             Lisp_Object oprops, aelt;
 27342             oprops = Ftext_properties_at (make_fixnum (0), elt);
 27343 
 27344             /* If the starting string's properties are not what
 27345                we want, translate the string.  Also, if the string
 27346                is risky, do that anyway.  */
 27347 
 27348             if (NILP (Fequal (props, oprops)) || risky)
 27349               {
 27350                 /* If the starting string has properties,
 27351                    merge the specified ones onto the existing ones.  */
 27352                 if (! NILP (oprops) && !risky)
 27353                   {
 27354                     Lisp_Object tem;
 27355 
 27356                     oprops = Fcopy_sequence (oprops);
 27357                     tem = props;
 27358                     while (CONSP (tem) && CONSP (XCDR (tem)))
 27359                       {
 27360                         oprops = plist_put (oprops, XCAR (tem),
 27361                                             XCAR (XCDR (tem)));
 27362                         tem = XCDR (XCDR (tem));
 27363                       }
 27364                     props = oprops;
 27365                   }
 27366 
 27367                 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
 27368                 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
 27369                   {
 27370                     /* AELT is what we want.  Move it to the front
 27371                        without consing.  */
 27372                     elt = XCAR (aelt);
 27373                     mode_line_proptrans_alist
 27374                       = move_elt_to_front (aelt, mode_line_proptrans_alist);
 27375                   }
 27376                 else
 27377                   {
 27378                     Lisp_Object tem;
 27379 
 27380                     /* If AELT has the wrong props, it is useless.
 27381                        so get rid of it.  */
 27382                     if (! NILP (aelt))
 27383                       mode_line_proptrans_alist
 27384                         = Fdelq (aelt, mode_line_proptrans_alist);
 27385 
 27386                     elt = Fcopy_sequence (elt);
 27387                     /* PROPS might cause set-text-properties to signal
 27388                        an error, so we call it via internal_condition_case_n,
 27389                        to avoid an infloop in redisplay due to the error.  */
 27390                     internal_condition_case_n (safe_set_text_properties,
 27391                                                4,
 27392                                                ((Lisp_Object [])
 27393                                                {make_fixnum (0),
 27394                                                    Flength (elt),
 27395                                                    props,
 27396                                                    elt}),
 27397                                                Qt, safe_eval_handler);
 27398                     /* Add this item to mode_line_proptrans_alist.  */
 27399                     mode_line_proptrans_alist
 27400                       = Fcons (Fcons (elt, props),
 27401                                mode_line_proptrans_alist);
 27402                     /* Truncate mode_line_proptrans_alist
 27403                        to at most 50 elements.  */
 27404                     tem = Fnthcdr (make_fixnum (50),
 27405                                    mode_line_proptrans_alist);
 27406                     if (! NILP (tem))
 27407                       XSETCDR (tem, Qnil);
 27408                   }
 27409               }
 27410           }
 27411 
 27412         offset = 0;
 27413 
 27414         if (literal)
 27415           {
 27416             prec = precision - n;
 27417             switch (mode_line_target)
 27418               {
 27419               case MODE_LINE_NOPROP:
 27420               case MODE_LINE_TITLE:
 27421                 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
 27422                 break;
 27423               case MODE_LINE_STRING:
 27424                 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
 27425                 break;
 27426               case MODE_LINE_DISPLAY:
 27427                 n += display_string (NULL, elt, Qnil, 0, 0, it,
 27428                                      0, prec, 0, STRING_MULTIBYTE (elt));
 27429                 break;
 27430               }
 27431 
 27432             break;
 27433           }
 27434 
 27435         /* Handle the non-literal case.  */
 27436 
 27437         while ((precision <= 0 || n < precision)
 27438                && SREF (elt, offset) != 0
 27439                && (mode_line_target != MODE_LINE_DISPLAY
 27440                    || it->current_x < it->last_visible_x))
 27441           {
 27442             ptrdiff_t last_offset = offset;
 27443 
 27444             /* Advance to end of string or next format specifier.  */
 27445             while ((c = SREF (elt, offset++)) != '\0' && c != '%')
 27446               ;
 27447 
 27448             if (offset - 1 != last_offset)
 27449               {
 27450                 ptrdiff_t nchars, nbytes;
 27451 
 27452                 /* Output to end of string or up to '%'.  Field width
 27453                    is length of string.  Don't output more than
 27454                    PRECISION allows us.  */
 27455                 offset--;
 27456 
 27457                 prec = c_string_width (SDATA (elt) + last_offset,
 27458                                        offset - last_offset, precision - n,
 27459                                        &nchars, &nbytes);
 27460 
 27461                 switch (mode_line_target)
 27462                   {
 27463                   case MODE_LINE_NOPROP:
 27464                   case MODE_LINE_TITLE:
 27465                     n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
 27466                     break;
 27467                   case MODE_LINE_STRING:
 27468                     {
 27469                       ptrdiff_t bytepos = last_offset;
 27470                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27471                       ptrdiff_t endpos = (precision <= 0
 27472                                           ? string_byte_to_char (elt, offset)
 27473                                           : charpos + nchars);
 27474                       Lisp_Object mode_string
 27475                         = Fsubstring (elt, make_fixnum (charpos),
 27476                                       make_fixnum (endpos));
 27477                       n += store_mode_line_string (NULL, mode_string, false,
 27478                                                    0, 0, Qnil);
 27479                     }
 27480                     break;
 27481                   case MODE_LINE_DISPLAY:
 27482                     {
 27483                       ptrdiff_t bytepos = last_offset;
 27484                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27485 
 27486                       if (precision <= 0)
 27487                         nchars = string_byte_to_char (elt, offset) - charpos;
 27488                       n += display_string (NULL, elt, Qnil, 0, charpos,
 27489                                            it, 0, nchars, 0,
 27490                                            STRING_MULTIBYTE (elt));
 27491                     }
 27492                     break;
 27493                   }
 27494               }
 27495             else /* c == '%' */
 27496               {
 27497                 ptrdiff_t percent_position = offset;
 27498 
 27499                 /* Get the specified minimum width.  Zero means
 27500                    don't pad.  */
 27501                 field = 0;
 27502                 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
 27503                   field = field * 10 + c - '0';
 27504 
 27505                 /* Don't pad beyond the total padding allowed.  */
 27506                 if (field_width - n > 0 && field > field_width - n)
 27507                   field = field_width - n;
 27508 
 27509                 /* Note that either PRECISION <= 0 or N < PRECISION.  */
 27510                 prec = precision - n;
 27511 
 27512                 if (c == 'M')
 27513                   n += display_mode_element (it, depth, field, prec,
 27514                                              Vglobal_mode_string, props,
 27515                                              risky);
 27516                 else if (c != 0)
 27517                   {
 27518                     bool multibyte;
 27519                     ptrdiff_t bytepos, charpos;
 27520                     const char *spec;
 27521                     Lisp_Object string;
 27522 
 27523                     bytepos = percent_position;
 27524                     charpos = (STRING_MULTIBYTE (elt)
 27525                                ? string_byte_to_char (elt, bytepos)
 27526                                : bytepos);
 27527                     spec = decode_mode_spec (it->w, c, field, &string);
 27528                     eassert (NILP (string) || STRINGP (string));
 27529                     multibyte = !NILP (string) && STRING_MULTIBYTE (string);
 27530                     /* Non-ASCII characters in SPEC should cause mode-line
 27531                        element be displayed as a multibyte string.  */
 27532                     ptrdiff_t nbytes = strlen (spec);
 27533                     ptrdiff_t nchars, mb_nbytes;
 27534                     parse_str_as_multibyte ((const unsigned char *)spec, nbytes,
 27535                                             &nchars, &mb_nbytes);
 27536                     if (!(nbytes == nchars || nbytes != mb_nbytes))
 27537                       multibyte = true;
 27538 
 27539                     switch (mode_line_target)
 27540                       {
 27541                       case MODE_LINE_NOPROP:
 27542                       case MODE_LINE_TITLE:
 27543                         n += store_mode_line_noprop (spec, field, prec);
 27544                         break;
 27545                       case MODE_LINE_STRING:
 27546                         {
 27547                           Lisp_Object tem = build_string (spec);
 27548                           props = Ftext_properties_at (make_fixnum (charpos), elt);
 27549                           /* Should only keep face property in props */
 27550                           n += store_mode_line_string (NULL, tem, false,
 27551                                                        field, prec, props);
 27552                         }
 27553                         break;
 27554                       case MODE_LINE_DISPLAY:
 27555                         {
 27556                           int nglyphs_before, nwritten;
 27557 
 27558                           nglyphs_before = it->glyph_row->used[TEXT_AREA];
 27559                           nwritten = display_string (spec, string, elt,
 27560                                                      charpos, 0, it,
 27561                                                      field, prec, 0,
 27562                                                      multibyte);
 27563 
 27564                           /* Assign to the glyphs written above the
 27565                              string where the `%x' came from, position
 27566                              of the `%'.  */
 27567                           if (nwritten > 0)
 27568                             {
 27569                               struct glyph *glyph
 27570                                 = (it->glyph_row->glyphs[TEXT_AREA]
 27571                                    + nglyphs_before);
 27572                               int i;
 27573 
 27574                               for (i = 0; i < nwritten; ++i)
 27575                                 {
 27576                                   glyph[i].object = elt;
 27577                                   glyph[i].charpos = charpos;
 27578                                 }
 27579 
 27580                               n += nwritten;
 27581                             }
 27582                         }
 27583                         break;
 27584                       }
 27585                   }
 27586                 else /* c == 0 */
 27587                   break;
 27588               }
 27589           }
 27590       }
 27591       break;
 27592 
 27593     case Lisp_Symbol:
 27594       /* A symbol: process the value of the symbol recursively
 27595          as if it appeared here directly.  Avoid error if symbol void.
 27596          Special case: if value of symbol is a string, output the string
 27597          literally.  */
 27598       {
 27599         register Lisp_Object tem;
 27600 
 27601         /* If the variable is not marked as risky to set
 27602            then its contents are risky to use.  */
 27603         if (NILP (Fget (elt, Qrisky_local_variable)))
 27604           risky = true;
 27605 
 27606         tem = Fboundp (elt);
 27607         if (!NILP (tem))
 27608           {
 27609             tem = Fsymbol_value (elt);
 27610             /* If value is a string, output that string literally:
 27611                don't check for % within it.  */
 27612             if (STRINGP (tem))
 27613               literal = true;
 27614 
 27615             if (!EQ (tem, elt))
 27616               {
 27617                 /* Give up right away for nil or t.  */
 27618                 elt = tem;
 27619                 goto tail_recurse;
 27620               }
 27621           }
 27622       }
 27623       break;
 27624 
 27625     case Lisp_Cons:
 27626       {
 27627         register Lisp_Object car, tem;
 27628 
 27629         /* A cons cell: five distinct cases.
 27630            If first element is :eval or :propertize, do something special.
 27631            If first element is a string or a cons, process all the elements
 27632            and effectively concatenate them.
 27633            If first element is a negative number, truncate displaying cdr to
 27634            at most that many characters.  If positive, pad (with spaces)
 27635            to at least that many characters.
 27636            If first element is a symbol, process the cadr or caddr recursively
 27637            according to whether the symbol's value is non-nil or nil.  */
 27638         car = XCAR (elt);
 27639         if (EQ (car, QCeval))
 27640           {
 27641             /* An element of the form (:eval FORM) means evaluate FORM
 27642                and use the result as mode line elements.  */
 27643 
 27644             if (risky)
 27645               break;
 27646 
 27647             if (CONSP (XCDR (elt)))
 27648               {
 27649                 Lisp_Object spec;
 27650                 spec = safe__eval (true, XCAR (XCDR (elt)));
 27651                 /* The :eval form could delete the frame stored in the
 27652                    iterator, which will cause a crash if we try to
 27653                    access faces and other fields (e.g., FRAME_KBOARD)
 27654                    on that frame.  This is a nonsensical thing to do,
 27655                    and signaling an error from redisplay might be
 27656                    dangerous, but we cannot continue with an invalid frame.  */
 27657                 if (!FRAME_LIVE_P (it->f))
 27658                   signal_error (":eval deleted the frame being displayed", elt);
 27659                 n += display_mode_element (it, depth, field_width - n,
 27660                                            precision - n, spec, props,
 27661                                            risky);
 27662               }
 27663           }
 27664         else if (EQ (car, QCpropertize))
 27665           {
 27666             /* An element of the form (:propertize ELT PROPS...)
 27667                means display ELT but applying properties PROPS.  */
 27668 
 27669             if (risky)
 27670               break;
 27671 
 27672             if (CONSP (XCDR (elt)))
 27673               n += display_mode_element (it, depth, field_width - n,
 27674                                          precision - n, XCAR (XCDR (elt)),
 27675                                          XCDR (XCDR (elt)), risky);
 27676           }
 27677         else if (SYMBOLP (car))
 27678           {
 27679             tem = Fboundp (car);
 27680             elt = XCDR (elt);
 27681             if (!CONSP (elt))
 27682               goto invalid;
 27683             /* elt is now the cdr, and we know it is a cons cell.
 27684                Use its car if CAR has a non-nil value.  */
 27685             if (!NILP (tem))
 27686               {
 27687                 tem = Fsymbol_value (car);
 27688                 if (!NILP (tem))
 27689                   {
 27690                     elt = XCAR (elt);
 27691                     goto tail_recurse;
 27692                   }
 27693               }
 27694             /* Symbol's value is nil (or symbol is unbound)
 27695                Get the cddr of the original list
 27696                and if possible find the caddr and use that.  */
 27697             elt = XCDR (elt);
 27698             if (NILP (elt))
 27699               break;
 27700             else if (!CONSP (elt))
 27701               goto invalid;
 27702             elt = XCAR (elt);
 27703             goto tail_recurse;
 27704           }
 27705         else if (FIXNUMP (car))
 27706           {
 27707             register int lim = XFIXNUM (car);
 27708             elt = XCDR (elt);
 27709             if (lim < 0)
 27710               {
 27711                 /* Negative int means reduce maximum width.  */
 27712                 if (precision <= 0)
 27713                   precision = -lim;
 27714                 else
 27715                   precision = min (precision, -lim);
 27716               }
 27717             else if (lim > 0)
 27718               {
 27719                 /* Padding specified.  Don't let it be more than
 27720                    current maximum.  */
 27721                 if (precision > 0)
 27722                   lim = min (precision, lim);
 27723 
 27724                 /* If that's more padding than already wanted, queue it.
 27725                    But don't reduce padding already specified even if
 27726                    that is beyond the current truncation point.  */
 27727                 field_width = max (lim, field_width);
 27728               }
 27729             goto tail_recurse;
 27730           }
 27731         else if (STRINGP (car) || CONSP (car))
 27732           FOR_EACH_TAIL_SAFE (elt)
 27733             {
 27734               if (0 < precision && precision <= n)
 27735                 break;
 27736               n += display_mode_element (it, depth,
 27737                                          /* Pad after only the last
 27738                                             list element.  */
 27739                                          (! CONSP (XCDR (elt))
 27740                                           ? field_width - n
 27741                                           : 0),
 27742                                          precision - n, XCAR (elt),
 27743                                          props, risky);
 27744             }
 27745       }
 27746       break;
 27747 
 27748     default:
 27749     invalid:
 27750       elt = build_string ("*invalid*");
 27751       goto tail_recurse;
 27752     }
 27753 
 27754   /* Pad to FIELD_WIDTH.  */
 27755   if (field_width > 0 && n < field_width)
 27756     {
 27757       switch (mode_line_target)
 27758         {
 27759         case MODE_LINE_NOPROP:
 27760         case MODE_LINE_TITLE:
 27761           n += store_mode_line_noprop ("", field_width - n, 0);
 27762           break;
 27763         case MODE_LINE_STRING:
 27764           n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
 27765                                        Qnil);
 27766           break;
 27767         case MODE_LINE_DISPLAY:
 27768           n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
 27769                                0, 0, 0);
 27770           break;
 27771         }
 27772     }
 27773 
 27774   return n;
 27775 }
 27776 
 27777 /* Store a mode-line string element in mode_line_string_list.
 27778 
 27779    If STRING is non-null, display that C string.  Otherwise, the Lisp
 27780    string LISP_STRING is displayed.
 27781 
 27782    FIELD_WIDTH is the minimum number of output glyphs to produce.
 27783    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 27784    with spaces.  FIELD_WIDTH <= 0 means don't pad.
 27785 
 27786    PRECISION is the maximum number of characters to output from
 27787    STRING.  PRECISION <= 0  means don't truncate the string.
 27788 
 27789    If COPY_STRING, make a copy of LISP_STRING before adding
 27790    properties to the string.
 27791 
 27792    PROPS are the properties to add to the string.
 27793    The mode_line_string_face face property is always added to the string.
 27794  */
 27795 
 27796 static int
 27797 store_mode_line_string (const char *string, Lisp_Object lisp_string,
 27798                         bool copy_string,
 27799                         int field_width, int precision, Lisp_Object props)
 27800 {
 27801   ptrdiff_t len;
 27802   int n = 0;
 27803 
 27804   if (string != NULL)
 27805     {
 27806       len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
 27807       lisp_string = make_string (string, len);
 27808       if (NILP (props))
 27809         props = mode_line_string_face_prop;
 27810       else if (!NILP (mode_line_string_face))
 27811         {
 27812           Lisp_Object face = plist_get (props, Qface);
 27813           props = Fcopy_sequence (props);
 27814           if (NILP (face))
 27815             face = mode_line_string_face;
 27816           else
 27817             face = list2 (face, mode_line_string_face);
 27818           props = plist_put (props, Qface, face);
 27819         }
 27820       Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27821                             props, lisp_string);
 27822     }
 27823   else
 27824     {
 27825       len = SCHARS (lisp_string);
 27826       if (precision > 0 && len > precision)
 27827         {
 27828           len = precision;
 27829           lisp_string = Fsubstring (lisp_string, make_fixnum (0), make_fixnum (len));
 27830           precision = -1;
 27831         }
 27832       if (!NILP (mode_line_string_face))
 27833         {
 27834           Lisp_Object face;
 27835           if (NILP (props))
 27836             props = Ftext_properties_at (make_fixnum (0), lisp_string);
 27837           face = plist_get (props, Qface);
 27838           if (NILP (face))
 27839             face = mode_line_string_face;
 27840           else
 27841             face = list2 (face, mode_line_string_face);
 27842           props = list2 (Qface, face);
 27843           if (copy_string)
 27844             lisp_string = Fcopy_sequence (lisp_string);
 27845         }
 27846       if (!NILP (props))
 27847         Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27848                               props, lisp_string);
 27849     }
 27850 
 27851   if (len > 0)
 27852     {
 27853       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27854       n += len;
 27855     }
 27856 
 27857   if (field_width > len)
 27858     {
 27859       field_width -= len;
 27860       lisp_string = Fmake_string (make_fixnum (field_width), make_fixnum (' '),
 27861                                   Qnil);
 27862       if (!NILP (props))
 27863         Fadd_text_properties (make_fixnum (0), make_fixnum (field_width),
 27864                               props, lisp_string);
 27865       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27866       n += field_width;
 27867     }
 27868 
 27869   return n;
 27870 }
 27871 
 27872 
 27873 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
 27874        1, 4, 0,
 27875        doc: /* Format a string out of a mode line format specification.
 27876 First arg FORMAT specifies the mode line format (see `mode-line-format'
 27877 for details) to use.
 27878 
 27879 By default, the format is evaluated for the currently selected window.
 27880 
 27881 Optional second arg FACE specifies the face property to put on all
 27882 characters for which no face is specified.  The value nil means the
 27883 default face.  The value t means whatever face the window's mode line
 27884 currently uses (either `mode-line' or `mode-line-inactive',
 27885 depending on whether the window is the selected window or not).
 27886 An integer value means the value string has no text
 27887 properties.
 27888 
 27889 Optional third and fourth args WINDOW and BUFFER specify the window
 27890 and buffer to use as the context for the formatting (defaults
 27891 are the selected window and the WINDOW's buffer).  */)
 27892      (Lisp_Object format, Lisp_Object face,
 27893       Lisp_Object window, Lisp_Object buffer)
 27894 {
 27895   struct it it;
 27896   int len;
 27897   struct window *w;
 27898   struct buffer *old_buffer = NULL;
 27899   int face_id;
 27900   bool no_props = FIXNUMP (face);
 27901   specpdl_ref count = SPECPDL_INDEX ();
 27902   Lisp_Object str;
 27903   int string_start = 0;
 27904 
 27905   w = decode_any_window (window);
 27906   XSETWINDOW (window, w);
 27907 
 27908   if (NILP (buffer))
 27909     buffer = w->contents;
 27910   CHECK_BUFFER (buffer);
 27911 
 27912   /* Make formatting the modeline a non-op when noninteractive, otherwise
 27913      there will be problems later caused by a partially initialized frame.  */
 27914   if (NILP (format) || noninteractive)
 27915     return empty_unibyte_string;
 27916 
 27917   if (no_props)
 27918     face = Qnil;
 27919 
 27920   face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
 27921     : EQ (face, Qt) ? (EQ (window, selected_window)
 27922                        ? MODE_LINE_ACTIVE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
 27923     : EQ (face, Qmode_line_active) ? MODE_LINE_ACTIVE_FACE_ID
 27924     : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
 27925     : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
 27926     : EQ (face, Qtab_line) ? TAB_LINE_FACE_ID
 27927     : EQ (face, Qtab_bar) ? TAB_BAR_FACE_ID
 27928     : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
 27929     : DEFAULT_FACE_ID;
 27930 
 27931   old_buffer = current_buffer;
 27932 
 27933   /* Save things including mode_line_proptrans_alist,
 27934      and set that to nil so that we don't alter the outer value.  */
 27935   record_unwind_protect (unwind_format_mode_line,
 27936                          format_mode_line_unwind_data
 27937                            (XFRAME (WINDOW_FRAME (w)),
 27938                             old_buffer, selected_window, true));
 27939   mode_line_proptrans_alist = Qnil;
 27940 
 27941   Fselect_window (window, Qt);
 27942   set_buffer_internal_1 (XBUFFER (buffer));
 27943 
 27944   init_iterator (&it, w, -1, -1, NULL, face_id);
 27945 
 27946   if (no_props)
 27947     {
 27948       mode_line_target = MODE_LINE_NOPROP;
 27949       mode_line_string_face_prop = Qnil;
 27950       mode_line_string_list = Qnil;
 27951       string_start = MODE_LINE_NOPROP_LEN (0);
 27952     }
 27953   else
 27954     {
 27955       mode_line_target = MODE_LINE_STRING;
 27956       mode_line_string_list = Qnil;
 27957       mode_line_string_face = face;
 27958       mode_line_string_face_prop
 27959         = NILP (face) ? Qnil : list2 (Qface, face);
 27960     }
 27961 
 27962   push_kboard (FRAME_KBOARD (it.f));
 27963   display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27964   pop_kboard ();
 27965 
 27966   if (no_props)
 27967     {
 27968       len = MODE_LINE_NOPROP_LEN (string_start);
 27969       str = make_string (mode_line_noprop_buf + string_start, len);
 27970     }
 27971   else
 27972     {
 27973       mode_line_string_list = Fnreverse (mode_line_string_list);
 27974       str = Fmapconcat (Qidentity, mode_line_string_list,
 27975                         empty_unibyte_string);
 27976     }
 27977 
 27978   return unbind_to (count, str);
 27979 }
 27980 
 27981 /* Write a null-terminated, right justified decimal representation of
 27982    the positive integer D to BUF using a minimal field width WIDTH.  */
 27983 
 27984 static void
 27985 pint2str (register char *buf, register int width, register ptrdiff_t d)
 27986 {
 27987   register char *p = buf;
 27988 
 27989   if (d <= 0)
 27990     *p++ = '0';
 27991   else
 27992     {
 27993       while (d > 0)
 27994         {
 27995           *p++ = d % 10 + '0';
 27996           d /= 10;
 27997         }
 27998     }
 27999 
 28000   for (width -= (int) (p - buf); width > 0; --width)
 28001     *p++ = ' ';
 28002   *p-- = '\0';
 28003   while (p > buf)
 28004     {
 28005       d = *buf;
 28006       *buf++ = *p;
 28007       *p-- = d;
 28008     }
 28009 }
 28010 
 28011 /* Write a null-terminated, right justified decimal and "human
 28012    readable" representation of the nonnegative integer D to BUF using
 28013    a minimal field width WIDTH.  D should be smaller than 999.5e24. */
 28014 
 28015 static const char power_letter[] =
 28016   {
 28017     0,   /* no letter */
 28018     'k', /* kilo */
 28019     'M', /* mega */
 28020     'G', /* giga */
 28021     'T', /* tera */
 28022     'P', /* peta */
 28023     'E', /* exa */
 28024     'Z', /* zetta */
 28025     'Y', /* yotta */
 28026     'R', /* ronna */
 28027     'Q'  /* quetta */
 28028   };
 28029 
 28030 static void
 28031 pint2hrstr (char *buf, int width, ptrdiff_t d)
 28032 {
 28033   /* We aim to represent the nonnegative integer D as
 28034      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 28035   ptrdiff_t quotient = d;
 28036   int remainder = 0;
 28037   /* -1 means: do not use TENTHS. */
 28038   int tenths = -1;
 28039   int exponent = 0;
 28040 
 28041   /* Length of QUOTIENT.TENTHS as a string. */
 28042   int length;
 28043 
 28044   char * psuffix;
 28045   char * p;
 28046 
 28047   if (quotient >= 1000)
 28048     {
 28049       /* Scale to the appropriate EXPONENT. */
 28050       do
 28051         {
 28052           remainder = quotient % 1000;
 28053           quotient /= 1000;
 28054           exponent++;
 28055         }
 28056       while (quotient >= 1000);
 28057 
 28058       /* Round to nearest and decide whether to use TENTHS or not. */
 28059       if (quotient <= 9)
 28060         {
 28061           tenths = remainder / 100;
 28062           if (remainder % 100 >= 50)
 28063             {
 28064               if (tenths < 9)
 28065                 tenths++;
 28066               else
 28067                 {
 28068                   quotient++;
 28069                   if (quotient == 10)
 28070                     tenths = -1;
 28071                   else
 28072                     tenths = 0;
 28073                 }
 28074             }
 28075         }
 28076       else
 28077         if (remainder >= 500)
 28078           {
 28079             if (quotient < 999)
 28080               quotient++;
 28081             else
 28082               {
 28083                 quotient = 1;
 28084                 exponent++;
 28085                 tenths = 0;
 28086               }
 28087           }
 28088     }
 28089 
 28090   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
 28091   if (tenths == -1 && quotient <= 99)
 28092     if (quotient <= 9)
 28093       length = 1;
 28094     else
 28095       length = 2;
 28096   else
 28097     length = 3;
 28098   p = psuffix = buf + max (width, length);
 28099 
 28100   /* Print EXPONENT. */
 28101   *psuffix++ = power_letter[exponent];
 28102   *psuffix = '\0';
 28103 
 28104   /* Print TENTHS. */
 28105   if (tenths >= 0)
 28106     {
 28107       *--p = '0' + tenths;
 28108       *--p = '.';
 28109     }
 28110 
 28111   /* Print QUOTIENT. */
 28112   do
 28113     {
 28114       int digit = quotient % 10;
 28115       *--p =  '0' + digit;
 28116     }
 28117   while ((quotient /= 10) != 0);
 28118 
 28119   /* Print leading spaces. */
 28120   while (buf < p)
 28121     *--p = ' ';
 28122 }
 28123 
 28124 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
 28125    If EOL_FLAG, set also a mnemonic character for end-of-line
 28126    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
 28127 
 28128 static unsigned char invalid_eol_type[] = "(*invalid*)";
 28129 
 28130 static char *
 28131 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
 28132 {
 28133   Lisp_Object val;
 28134   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 28135   const unsigned char *eol_str;
 28136   int eol_str_len;
 28137   /* The EOL conversion we are using.  */
 28138   Lisp_Object eoltype;
 28139 
 28140   val = CODING_SYSTEM_SPEC (coding_system);
 28141   eoltype = Qnil;
 28142 
 28143   if (!VECTORP (val))           /* Not yet decided.  */
 28144     {
 28145       *buf++ = multibyte ? '-' : ' ';
 28146       if (eol_flag)
 28147         eoltype = eol_mnemonic_undecided;
 28148       /* Don't mention EOL conversion if it isn't decided.  */
 28149     }
 28150   else
 28151     {
 28152       Lisp_Object attrs;
 28153       Lisp_Object eolvalue;
 28154 
 28155       attrs = AREF (val, 0);
 28156       eolvalue = AREF (val, 2);
 28157 
 28158       if (multibyte)
 28159         buf += CHAR_STRING (XFIXNAT (CODING_ATTR_MNEMONIC (attrs)),
 28160                             (unsigned char *) buf);
 28161       else
 28162         *buf++ = ' ';
 28163 
 28164       if (eol_flag)
 28165         {
 28166           /* The EOL conversion that is normal on this system.  */
 28167 
 28168           if (NILP (eolvalue))  /* Not yet decided.  */
 28169             eoltype = eol_mnemonic_undecided;
 28170           else if (VECTORP (eolvalue)) /* Not yet decided.  */
 28171             eoltype = eol_mnemonic_undecided;
 28172           else                  /* eolvalue is Qunix, Qdos, or Qmac.  */
 28173             eoltype = (EQ (eolvalue, Qunix)
 28174                        ? eol_mnemonic_unix
 28175                        : EQ (eolvalue, Qdos)
 28176                        ? eol_mnemonic_dos : eol_mnemonic_mac);
 28177         }
 28178     }
 28179 
 28180   if (eol_flag)
 28181     {
 28182       /* Mention the EOL conversion if it is not the usual one.  */
 28183       if (STRINGP (eoltype))
 28184         {
 28185           eol_str = SDATA (eoltype);
 28186           eol_str_len = SBYTES (eoltype);
 28187         }
 28188       else if (CHARACTERP (eoltype))
 28189         {
 28190           int c = XFIXNAT (eoltype);
 28191           return buf + CHAR_STRING (c, (unsigned char *) buf);
 28192         }
 28193       else
 28194         {
 28195           eol_str = invalid_eol_type;
 28196           eol_str_len = sizeof (invalid_eol_type) - 1;
 28197         }
 28198       memcpy (buf, eol_str, eol_str_len);
 28199       buf += eol_str_len;
 28200     }
 28201 
 28202   return buf;
 28203 }
 28204 
 28205 /* Return the approximate percentage N is of D (rounding upward), or 99,
 28206    whichever is less.  Assume 0 < D and 0 <= N <= D * INT_MAX / 100.  */
 28207 
 28208 static int
 28209 percent99 (ptrdiff_t n, ptrdiff_t d)
 28210 {
 28211   int percent = (d - 1 + 100.0 * n) / d;
 28212   return min (percent, 99);
 28213 }
 28214 
 28215 /* Return a string for the output of a mode line %-spec for window W,
 28216    generated by character C.  FIELD_WIDTH > 0 means pad the string
 28217    returned with spaces to that value.  Set *STRING to be a Lisp
 28218    string if the resulting string is taken from that Lisp string;
 28219    otherwise, set *STRING to Qnil.
 28220 
 28221    Note we operate on the current buffer for most purposes.  */
 28222 
 28223 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 28224 
 28225 static const char *
 28226 decode_mode_spec (struct window *w, register int c, int field_width,
 28227                   Lisp_Object *string)
 28228 {
 28229   Lisp_Object obj;
 28230   struct frame *f = XFRAME (WINDOW_FRAME (w));
 28231   char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
 28232   /* We are going to use f->decode_mode_spec_buffer as the buffer to
 28233      produce strings from numerical values, so limit preposterously
 28234      large values of FIELD_WIDTH to avoid overrunning the buffer's
 28235      end.  The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
 28236      bytes plus the terminating null.  */
 28237   int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
 28238   struct buffer *b = current_buffer;
 28239 
 28240   obj = Qnil;
 28241   *string = Qnil;
 28242 
 28243   switch (c)
 28244     {
 28245     case '*':
 28246       if (!NILP (BVAR (b, read_only)))
 28247         return "%";
 28248       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28249         return "*";
 28250       return "-";
 28251 
 28252     case '+':
 28253       /* This differs from %* only for a modified read-only buffer.  */
 28254       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28255         return "*";
 28256       if (!NILP (BVAR (b, read_only)))
 28257         return "%";
 28258       return "-";
 28259 
 28260     case '&':
 28261       /* This differs from %* in ignoring read-only-ness.  */
 28262       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28263         return "*";
 28264       return "-";
 28265 
 28266     case '%':
 28267       return "%";
 28268 
 28269     case '[':
 28270       {
 28271         int i;
 28272         char *p;
 28273 
 28274         if (command_loop_level > 5)
 28275           return "[[[... ";
 28276         p = decode_mode_spec_buf;
 28277         for (i = 0; i < command_loop_level; i++)
 28278           *p++ = '[';
 28279         *p = 0;
 28280         return decode_mode_spec_buf;
 28281       }
 28282 
 28283     case ']':
 28284       {
 28285         int i;
 28286         char *p;
 28287 
 28288         if (command_loop_level > 5)
 28289           return " ...]]]";
 28290         p = decode_mode_spec_buf;
 28291         for (i = 0; i < command_loop_level; i++)
 28292           *p++ = ']';
 28293         *p = 0;
 28294         return decode_mode_spec_buf;
 28295       }
 28296 
 28297     case '-':
 28298       {
 28299         register int i;
 28300 
 28301         /* Let lots_of_dashes be a string of infinite length.  */
 28302         if (mode_line_target == MODE_LINE_NOPROP
 28303             || mode_line_target == MODE_LINE_STRING)
 28304           return "--";
 28305         if (field_width <= 0
 28306             || field_width > sizeof (lots_of_dashes))
 28307           {
 28308             for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
 28309               decode_mode_spec_buf[i] = '-';
 28310             decode_mode_spec_buf[i] = '\0';
 28311             return decode_mode_spec_buf;
 28312           }
 28313         else
 28314           return lots_of_dashes;
 28315       }
 28316 
 28317     case 'b':
 28318       obj = BVAR (b, name);
 28319       break;
 28320 
 28321     case 'c':
 28322     case 'C':
 28323       /* %c, %C, and %l are ignored in `frame-title-format'.
 28324          (In redisplay_internal, the frame title is drawn _before_ the
 28325          windows are updated, so the stuff which depends on actual
 28326          window contents (such as %l) may fail to render properly, or
 28327          even crash emacs.)  */
 28328       if (mode_line_target == MODE_LINE_TITLE)
 28329         return "";
 28330       else
 28331         {
 28332           ptrdiff_t col = current_column ();
 28333           int disp_col = (c == 'C') ? col + 1 : col;
 28334           w->column_number_displayed = col;
 28335           pint2str (decode_mode_spec_buf, width, disp_col);
 28336           return decode_mode_spec_buf;
 28337         }
 28338 
 28339     case 'e':
 28340 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
 28341       {
 28342         if (NILP (Vmemory_full))
 28343           return "";
 28344         else
 28345           return "!MEM FULL! ";
 28346       }
 28347 #else
 28348       return "";
 28349 #endif
 28350 
 28351     case 'F':
 28352       /* %F displays the frame name.  */
 28353       if (!NILP (f->title))
 28354         return SSDATA (f->title);
 28355       if (f->explicit_name || ! FRAME_WINDOW_P (f))
 28356         return SSDATA (f->name);
 28357       return "Emacs";
 28358 
 28359     case 'f':
 28360       obj = BVAR (b, filename);
 28361       break;
 28362 
 28363     case 'i':
 28364       {
 28365         ptrdiff_t size = ZV - BEGV;
 28366         pint2str (decode_mode_spec_buf, width, size);
 28367         return decode_mode_spec_buf;
 28368       }
 28369 
 28370     case 'I':
 28371       {
 28372         ptrdiff_t size = ZV - BEGV;
 28373         pint2hrstr (decode_mode_spec_buf, width, size);
 28374         return decode_mode_spec_buf;
 28375       }
 28376 
 28377     case 'l':
 28378       {
 28379         ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 28380         ptrdiff_t topline, nlines, height;
 28381         ptrdiff_t junk;
 28382 
 28383         /* %c, %C, and %l are ignored in `frame-title-format'.  */
 28384         if (mode_line_target == MODE_LINE_TITLE)
 28385           return "";
 28386 
 28387         startpos = marker_position (w->start);
 28388         startpos_byte = marker_byte_position (w->start);
 28389         height = WINDOW_TOTAL_LINES (w);
 28390         /* We cannot cope with w->start being outside of the
 28391            accessible portion of the buffer; in particular,
 28392            display_count_lines call below might infloop if called with
 28393            startpos_byte outside of the [BEGV_BYTE..ZV_BYTE] region.
 28394            Such w->start means we were called in some "creative" way
 28395            when the buffer's restriction was changed, but the window
 28396            wasn't yet redisplayed after that.  If that happens, we
 28397            need to determine a new base line.  */
 28398         if (!(BUF_BEGV_BYTE (b) <= startpos_byte
 28399               && startpos_byte <= BUF_ZV_BYTE (b)))
 28400           {
 28401             startpos = BUF_BEGV (b);
 28402             startpos_byte = BUF_BEGV_BYTE (b);
 28403             w->base_line_pos = 0;
 28404             w->base_line_number = 0;
 28405           }
 28406 
 28407         /* If we decided that this buffer isn't suitable for line numbers,
 28408            don't forget that too fast.  */
 28409         if (w->base_line_pos == -1)
 28410           goto no_value;
 28411 
 28412         /* If the buffer is very big, don't waste time.  */
 28413         if (FIXNUMP (Vline_number_display_limit)
 28414             && BUF_ZV (b) - BUF_BEGV (b) > XFIXNUM (Vline_number_display_limit))
 28415           {
 28416             w->base_line_pos = 0;
 28417             w->base_line_number = 0;
 28418             goto no_value;
 28419           }
 28420 
 28421         if (w->base_line_number > 0
 28422             && w->base_line_pos > 0
 28423             && w->base_line_pos <= startpos)
 28424           {
 28425             line = w->base_line_number;
 28426             linepos = w->base_line_pos;
 28427             linepos_byte = buf_charpos_to_bytepos (b, linepos);
 28428           }
 28429         else
 28430           {
 28431             line = 1;
 28432             linepos = BUF_BEGV (b);
 28433             linepos_byte = BUF_BEGV_BYTE (b);
 28434           }
 28435 
 28436         /* Count lines from base line to window start position.  */
 28437         nlines = display_count_lines (linepos_byte,
 28438                                       startpos_byte,
 28439                                       startpos, &junk);
 28440 
 28441         topline = nlines + line;
 28442 
 28443         /* Determine a new base line, if the old one is too close
 28444            or too far away, or if we did not have one.
 28445            "Too close" means it's plausible a scroll-down would
 28446            go back past it.  */
 28447         if (startpos == BUF_BEGV (b))
 28448           {
 28449             w->base_line_number = topline;
 28450             w->base_line_pos = BUF_BEGV (b);
 28451           }
 28452         else if (nlines < height + 25 || nlines > height * 3 + 50
 28453                  || linepos == BUF_BEGV (b))
 28454           {
 28455             ptrdiff_t limit = BUF_BEGV (b);
 28456             ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 28457             ptrdiff_t position;
 28458             ptrdiff_t distance
 28459               = (line_number_display_limit_width < 0 ? 0
 28460                  : ckd_mul (&distance, line_number_display_limit_width,
 28461                             height * 2 + 30)
 28462                  ? PTRDIFF_MAX : distance);
 28463 
 28464             if (startpos - distance > limit)
 28465               {
 28466                 limit = startpos - distance;
 28467                 limit_byte = CHAR_TO_BYTE (limit);
 28468               }
 28469 
 28470             nlines = display_count_lines (startpos_byte,
 28471                                           limit_byte,
 28472                                           - (height * 2 + 30),
 28473                                           &position);
 28474             /* If we couldn't find the lines we wanted within
 28475                line_number_display_limit_width chars per line,
 28476                give up on line numbers for this window.  */
 28477             if (position == limit_byte && limit == startpos - distance)
 28478               {
 28479                 w->base_line_pos = -1;
 28480                 w->base_line_number = 0;
 28481                 goto no_value;
 28482               }
 28483 
 28484             w->base_line_number = topline - nlines;
 28485             w->base_line_pos = BYTE_TO_CHAR (position);
 28486           }
 28487 
 28488         /* Now count lines from the start pos to point.  */
 28489         nlines = display_count_lines (startpos_byte,
 28490                                       PT_BYTE, PT, &junk);
 28491 
 28492         /* Record that we did display the line number.  */
 28493         line_number_displayed = true;
 28494 
 28495         /* Make the string to show.  */
 28496         pint2str (decode_mode_spec_buf, width, topline + nlines);
 28497         return decode_mode_spec_buf;
 28498     no_value:
 28499         {
 28500           char *p = decode_mode_spec_buf;
 28501           int pad = width - 2;
 28502           while (pad-- > 0)
 28503             *p++ = ' ';
 28504           *p++ = '?';
 28505           *p++ = '?';
 28506           *p = '\0';
 28507           return decode_mode_spec_buf;
 28508         }
 28509       }
 28510       break;
 28511 
 28512     case 'm':
 28513       obj = BVAR (b, mode_name);
 28514       break;
 28515 
 28516     case 'n':
 28517       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
 28518         return " Narrow";
 28519       break;
 28520 
 28521       /* Display the "degree of travel" of the window through the buffer.  */
 28522     case 'o':
 28523       {
 28524         ptrdiff_t toppos = marker_position (w->start);
 28525         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28526         ptrdiff_t begv = BUF_BEGV (b);
 28527         ptrdiff_t zv = BUF_ZV (b);
 28528 
 28529         if (zv <= botpos)
 28530           return toppos <= begv ? "All" : "Bottom";
 28531         else if (toppos <= begv)
 28532           return "Top";
 28533         else
 28534           {
 28535           sprintf (decode_mode_spec_buf, "%2d%%",
 28536                    percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
 28537           return decode_mode_spec_buf;
 28538           }
 28539       }
 28540 
 28541       /* Display percentage of buffer above the top of the screen.  */
 28542     case 'p':
 28543       {
 28544         ptrdiff_t pos = marker_position (w->start);
 28545         ptrdiff_t begv = BUF_BEGV (b);
 28546         ptrdiff_t zv = BUF_ZV (b);
 28547 
 28548         if (w->window_end_pos <= BUF_Z (b) - zv)
 28549           return pos <= begv ? "All" : "Bottom";
 28550         else if (pos <= begv)
 28551           return "Top";
 28552         else
 28553           {
 28554             sprintf (decode_mode_spec_buf, "%2d%%",
 28555                      percent99 (pos - begv, zv - begv));
 28556             return decode_mode_spec_buf;
 28557           }
 28558       }
 28559 
 28560       /* Display percentage of size above the bottom of the screen.  */
 28561     case 'P':
 28562       {
 28563         ptrdiff_t toppos = marker_position (w->start);
 28564         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28565         ptrdiff_t begv = BUF_BEGV (b);
 28566         ptrdiff_t zv = BUF_ZV (b);
 28567 
 28568         if (zv <= botpos)
 28569           return toppos <= begv ? "All" : "Bottom";
 28570         else
 28571           {
 28572             sprintf (decode_mode_spec_buf,
 28573                      &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
 28574                      percent99 (botpos - begv, zv - begv));
 28575             return decode_mode_spec_buf;
 28576           }
 28577       }
 28578 
 28579       /* Display percentage offsets of top and bottom of the window,
 28580          using "All" (but not "Top" or "Bottom") where appropriate.  */
 28581     case 'q':
 28582       {
 28583         ptrdiff_t toppos = marker_position (w->start);
 28584         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28585         ptrdiff_t begv = BUF_BEGV (b);
 28586         ptrdiff_t zv = BUF_ZV (b);
 28587         int top_perc, bot_perc;
 28588 
 28589         if ((toppos <= begv) && (zv <= botpos))
 28590           return "All   ";
 28591 
 28592         top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
 28593         bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
 28594 
 28595         if (top_perc == bot_perc)
 28596           sprintf (decode_mode_spec_buf, "%d%%", top_perc);
 28597         else
 28598           sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
 28599 
 28600         return decode_mode_spec_buf;
 28601       }
 28602 
 28603     case 's':
 28604       /* status of process */
 28605       obj = Fget_buffer_process (Fcurrent_buffer ());
 28606       if (NILP (obj))
 28607         return "no process";
 28608 #ifndef MSDOS
 28609       obj = Fsymbol_name (Fprocess_status (obj));
 28610 #endif
 28611       break;
 28612 
 28613     case '@':
 28614       {
 28615         specpdl_ref count = inhibit_garbage_collection ();
 28616         Lisp_Object curdir = BVAR (current_buffer, directory);
 28617         Lisp_Object val = Qnil;
 28618 
 28619         if (STRINGP (curdir))
 28620           val = safe_call1 (intern ("file-remote-p"), curdir);
 28621 
 28622         val = unbind_to (count, val);
 28623 
 28624         if (NILP (val))
 28625           return "-";
 28626         else
 28627           return "@";
 28628       }
 28629 
 28630     case 'z':
 28631       /* coding-system (not including end-of-line format) */
 28632     case 'Z':
 28633       /* coding-system (including end-of-line type) */
 28634       {
 28635         bool eol_flag = (c == 'Z');
 28636         char *p = decode_mode_spec_buf;
 28637 
 28638         if (! FRAME_WINDOW_P (f))
 28639           {
 28640             /* No need to mention EOL here--the terminal never needs
 28641                to do EOL conversion.  */
 28642             p = decode_mode_spec_coding (CODING_ID_NAME
 28643                                          (FRAME_KEYBOARD_CODING (f)->id),
 28644                                          p, false);
 28645             p = decode_mode_spec_coding (CODING_ID_NAME
 28646                                          (FRAME_TERMINAL_CODING (f)->id),
 28647                                          p, false);
 28648           }
 28649         p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
 28650                                      p, eol_flag);
 28651 
 28652 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
 28653 #ifdef subprocesses
 28654         obj = Fget_buffer_process (Fcurrent_buffer ());
 28655         if (PROCESSP (obj))
 28656           {
 28657             p = decode_mode_spec_coding
 28658               (XPROCESS (obj)->decode_coding_system, p, eol_flag);
 28659             p = decode_mode_spec_coding
 28660               (XPROCESS (obj)->encode_coding_system, p, eol_flag);
 28661           }
 28662 #endif /* subprocesses */
 28663 #endif /* false */
 28664         *p = 0;
 28665         return decode_mode_spec_buf;
 28666       }
 28667     }
 28668 
 28669   if (STRINGP (obj))
 28670     {
 28671       *string = obj;
 28672       return SSDATA (obj);
 28673     }
 28674   else
 28675     return "";
 28676 }
 28677 
 28678 /* Return the number of lines between start_byte and end_byte in the
 28679    current buffer. */
 28680 
 28681 ptrdiff_t
 28682 count_lines (ptrdiff_t start_byte, ptrdiff_t end_byte)
 28683 {
 28684   ptrdiff_t ignored;
 28685   return display_count_lines (start_byte, end_byte, ZV, &ignored);
 28686 }
 28687 
 28688 /* Count up to COUNT lines starting from START_BYTE.  COUNT negative
 28689    means count lines back from START_BYTE.  But don't go beyond
 28690    LIMIT_BYTE.  Return the number of lines thus found (always
 28691    nonnegative).
 28692 
 28693    Set *BYTE_POS_PTR to the byte position where we stopped.  This is
 28694    either the position COUNT lines after/before START_BYTE, if we
 28695    found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
 28696    COUNT lines.  */
 28697 
 28698 static ptrdiff_t
 28699 display_count_lines (ptrdiff_t start_byte,
 28700                      ptrdiff_t limit_byte, ptrdiff_t count,
 28701                      ptrdiff_t *byte_pos_ptr)
 28702 {
 28703   register unsigned char *cursor;
 28704   unsigned char *base;
 28705 
 28706   register ptrdiff_t ceiling;
 28707   register unsigned char *ceiling_addr;
 28708   ptrdiff_t orig_count = count;
 28709 
 28710   /* If we are not in selective display mode,
 28711      check only for newlines.  */
 28712   bool selective_display
 28713     = (!NILP (BVAR (current_buffer, selective_display))
 28714        && !FIXNUMP (BVAR (current_buffer, selective_display)));
 28715 
 28716   if (count > 0)
 28717     {
 28718       while (start_byte < limit_byte)
 28719         {
 28720           ceiling =  BUFFER_CEILING_OF (start_byte);
 28721           ceiling = min (limit_byte - 1, ceiling);
 28722           ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
 28723           base = (cursor = BYTE_POS_ADDR (start_byte));
 28724 
 28725           do
 28726             {
 28727               if (selective_display)
 28728                 {
 28729                   while (*cursor != '\n' && *cursor != 015
 28730                          && ++cursor != ceiling_addr)
 28731                     continue;
 28732                   if (cursor == ceiling_addr)
 28733                     break;
 28734                 }
 28735               else
 28736                 {
 28737                   cursor = memchr (cursor, '\n', ceiling_addr - cursor);
 28738                   if (! cursor)
 28739                     break;
 28740                 }
 28741 
 28742               cursor++;
 28743 
 28744               if (--count == 0)
 28745                 {
 28746                   start_byte += cursor - base;
 28747                   *byte_pos_ptr = start_byte;
 28748                   return orig_count;
 28749                 }
 28750             }
 28751           while (cursor < ceiling_addr);
 28752 
 28753           start_byte += ceiling_addr - base;
 28754         }
 28755     }
 28756   else
 28757     {
 28758       while (start_byte > limit_byte)
 28759         {
 28760           ceiling = BUFFER_FLOOR_OF (start_byte - 1);
 28761           ceiling = max (limit_byte, ceiling);
 28762           ceiling_addr = BYTE_POS_ADDR (ceiling);
 28763           base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
 28764           while (true)
 28765             {
 28766               if (selective_display)
 28767                 {
 28768                   while (--cursor >= ceiling_addr
 28769                          && *cursor != '\n' && *cursor != 015)
 28770                     continue;
 28771                   if (cursor < ceiling_addr)
 28772                     break;
 28773                 }
 28774               else
 28775                 {
 28776                   cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
 28777                   if (! cursor)
 28778                     break;
 28779                 }
 28780 
 28781               if (++count == 0)
 28782                 {
 28783                   start_byte += cursor - base + 1;
 28784                   *byte_pos_ptr = start_byte;
 28785                   /* When scanning backwards, we should
 28786                      not count the newline posterior to which we stop.  */
 28787                   return - orig_count - 1;
 28788                 }
 28789             }
 28790           start_byte += ceiling_addr - base;
 28791         }
 28792     }
 28793 
 28794   *byte_pos_ptr = limit_byte;
 28795 
 28796   if (count < 0)
 28797     return - orig_count + count;
 28798   return orig_count - count;
 28799 
 28800 }
 28801 
 28802 
 28803 
 28804 /***********************************************************************
 28805                          Displaying strings
 28806  ***********************************************************************/
 28807 
 28808 /* Display a NUL-terminated string, starting with index START.
 28809 
 28810    If STRING is non-null, display that C string.  Otherwise, the Lisp
 28811    string LISP_STRING is displayed.  There's a case that STRING is
 28812    non-null and LISP_STRING is not nil.  It means STRING is a string
 28813    data of LISP_STRING.  In that case, we display LISP_STRING while
 28814    ignoring its text properties.
 28815 
 28816    If FACE_STRING is not nil, FACE_STRING_POS is a position in
 28817    FACE_STRING.  Display STRING or LISP_STRING with the face at
 28818    FACE_STRING_POS in FACE_STRING:
 28819 
 28820    Display the string in the environment given by IT, but use the
 28821    standard display table, temporarily.
 28822 
 28823    FIELD_WIDTH is the minimum number of output glyphs to produce.
 28824    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 28825    with spaces.  If STRING has more characters, more than FIELD_WIDTH
 28826    glyphs will be produced.  FIELD_WIDTH <= 0 means don't pad.
 28827 
 28828    PRECISION is the maximum number of characters to output from
 28829    STRING.  PRECISION < 0  means don't truncate the string.
 28830 
 28831    This is roughly equivalent to printf format specifiers:
 28832 
 28833    FIELD_WIDTH  PRECISION       PRINTF
 28834    ----------------------------------------
 28835    -1           -1              %s
 28836    -1           10              %.10s
 28837    10           -1              %10s
 28838    20           10              %20.10s
 28839 
 28840    MULTIBYTE zero means do not display multibyte chars, > 0 means do
 28841    display them, and < 0 means obey the current buffer's value of
 28842    enable_multibyte_characters.
 28843 
 28844    Value is the number of columns displayed.  */
 28845 
 28846 static int
 28847 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 28848                 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
 28849                 int field_width, int precision, int max_x, int multibyte)
 28850 {
 28851   int hpos_at_start = it->hpos;
 28852   int saved_face_id = it->face_id;
 28853   struct glyph_row *row = it->glyph_row;
 28854   ptrdiff_t it_charpos;
 28855 
 28856   /* Initialize the iterator IT for iteration over STRING beginning
 28857      with index START.  */
 28858   reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string,
 28859                     start, precision, field_width, multibyte);
 28860 
 28861   if (string && STRINGP (lisp_string))
 28862     /* LISP_STRING is the one returned by decode_mode_spec.  We should
 28863        ignore its text properties.  */
 28864     it->stop_charpos = it->end_charpos;
 28865 
 28866   /* If displaying STRING, set up the face of the iterator from
 28867      FACE_STRING, if that's given.  */
 28868   if (STRINGP (face_string))
 28869     {
 28870       ptrdiff_t endptr;
 28871       struct face *face;
 28872 
 28873       it->face_id
 28874         = face_at_string_position (it->w, face_string, face_string_pos,
 28875                                    0, &endptr, it->base_face_id, false, 0);
 28876       face = FACE_FROM_ID (it->f, it->face_id);
 28877       it->face_box_p = face->box != FACE_NO_BOX;
 28878 
 28879       /* If we have a display spec, but there's no Lisp string being
 28880          displayed, then check whether we've got one from the
 28881          :propertize being passed in and use that.  */
 28882       if (NILP (lisp_string))
 28883         {
 28884           Lisp_Object display = Fget_text_property (make_fixnum (0), Qdisplay,
 28885                                                     face_string);
 28886           if (!NILP (display))
 28887             {
 28888               Lisp_Object min_width = plist_get (display, Qmin_width);
 28889               if (!NILP (min_width))
 28890                 display_min_width (it, 0, face_string, min_width);
 28891             }
 28892         }
 28893     }
 28894 
 28895   /* Set max_x to the maximum allowed X position.  Don't let it go
 28896      beyond the right edge of the window.  */
 28897   if (max_x <= 0)
 28898     max_x = it->last_visible_x;
 28899   else
 28900     max_x = min (max_x, it->last_visible_x);
 28901 
 28902   /* Skip over display elements that are not visible because IT->w is
 28903      hscrolled.  */
 28904   if (it->current_x < it->first_visible_x)
 28905     move_it_in_display_line_to (it, 100000, it->first_visible_x,
 28906                                 MOVE_TO_POS | MOVE_TO_X);
 28907 
 28908   row->ascent = it->max_ascent;
 28909   row->height = it->max_ascent + it->max_descent;
 28910   row->phys_ascent = it->max_phys_ascent;
 28911   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 28912   row->extra_line_spacing = it->max_extra_line_spacing;
 28913 
 28914   if (STRINGP (it->string))
 28915     it_charpos = IT_STRING_CHARPOS (*it);
 28916   else
 28917     it_charpos = IT_CHARPOS (*it);
 28918 
 28919   /* This condition is for the case that we are called with current_x
 28920      past last_visible_x.  */
 28921   while (it->current_x < max_x)
 28922     {
 28923       int x_before, x, n_glyphs_before, i, nglyphs;
 28924 
 28925       /* Get the next display element.  */
 28926       if (!get_next_display_element (it))
 28927         break;
 28928 
 28929       /* Produce glyphs.  */
 28930       x_before = it->current_x;
 28931       n_glyphs_before = row->used[TEXT_AREA];
 28932       PRODUCE_GLYPHS (it);
 28933 
 28934       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 28935       i = 0;
 28936       x = x_before;
 28937       while (i < nglyphs)
 28938         {
 28939           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 28940 
 28941           if (it->line_wrap != TRUNCATE
 28942               && x + glyph->pixel_width > max_x)
 28943             {
 28944               /* End of continued line or max_x reached.  */
 28945               if (CHAR_GLYPH_PADDING_P (*glyph))
 28946                 {
 28947                   /* A wide character is unbreakable.  */
 28948                   if (row->reversed_p)
 28949                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28950                                       - n_glyphs_before);
 28951                   row->used[TEXT_AREA] = n_glyphs_before;
 28952                   it->current_x = x_before;
 28953                 }
 28954               else
 28955                 {
 28956                   if (row->reversed_p)
 28957                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28958                                       - (n_glyphs_before + i));
 28959                   row->used[TEXT_AREA] = n_glyphs_before + i;
 28960                   it->current_x = x;
 28961                 }
 28962               break;
 28963             }
 28964           else if (x + glyph->pixel_width >= it->first_visible_x)
 28965             {
 28966               /* Glyph is at least partially visible.  */
 28967               ++it->hpos;
 28968               if (x < it->first_visible_x)
 28969                 row->x = x - it->first_visible_x;
 28970             }
 28971           else
 28972             {
 28973               /* Glyph is off the left margin of the display area.
 28974                  Should not happen.  */
 28975               emacs_abort ();
 28976             }
 28977 
 28978           row->ascent = max (row->ascent, it->max_ascent);
 28979           row->height = max (row->height, it->max_ascent + it->max_descent);
 28980           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 28981           row->phys_height = max (row->phys_height,
 28982                                   it->max_phys_ascent + it->max_phys_descent);
 28983           row->extra_line_spacing = max (row->extra_line_spacing,
 28984                                          it->max_extra_line_spacing);
 28985           x += glyph->pixel_width;
 28986           ++i;
 28987         }
 28988 
 28989       /* Stop if max_x reached.  */
 28990       if (i < nglyphs)
 28991         break;
 28992 
 28993       /* Stop at line ends.  */
 28994       if (ITERATOR_AT_END_OF_LINE_P (it))
 28995         {
 28996           it->continuation_lines_width = 0;
 28997           break;
 28998         }
 28999 
 29000       set_iterator_to_next (it, true);
 29001       if (STRINGP (it->string))
 29002         it_charpos = IT_STRING_CHARPOS (*it);
 29003       else
 29004         it_charpos = IT_CHARPOS (*it);
 29005 
 29006       /* Stop if truncating at the right edge.  */
 29007       if (it->line_wrap == TRUNCATE
 29008           && it->current_x >= it->last_visible_x)
 29009         {
 29010           /* Add truncation mark, but don't do it if the line is
 29011              truncated at a padding space.  */
 29012           /* Need to do the below for the last string character as
 29013              well, since it could be a double-width character, in
 29014              which case the previous character ends before
 29015              last_visible_x.  Thus, comparison with <=, not <.  */
 29016           if (it_charpos <= it->string_nchars)
 29017             {
 29018               if (!FRAME_WINDOW_P (it->f))
 29019                 {
 29020                   int ii, n;
 29021 
 29022                   if (it->current_x > it->last_visible_x)
 29023                     {
 29024                       /* This flag is true if we are displaying mode
 29025                          line, false for header-line or tab-line.  */
 29026                       bool mode_line_p = false;
 29027 
 29028                       /* ROW->mode_line_p is true if we display mode
 29029                          line or header-line or tab-line.  */
 29030                       if (row->mode_line_p)
 29031                         {
 29032                           struct window *w = it->w;
 29033                           if (row == MATRIX_MODE_LINE_ROW (w->desired_matrix))
 29034                             mode_line_p = true;
 29035                         }
 29036                       if (!row->reversed_p)
 29037                         {
 29038                           for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
 29039                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 29040                               break;
 29041                         }
 29042                       else
 29043                         {
 29044                           for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
 29045                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 29046                               break;
 29047                           unproduce_glyphs (it, ii + 1);
 29048                           ii = row->used[TEXT_AREA] - (ii + 1);
 29049                         }
 29050                       for (n = row->used[TEXT_AREA]; ii < n; ++ii)
 29051                         {
 29052                           row->used[TEXT_AREA] = ii;
 29053                           if (row->mode_line_p)
 29054                             pad_mode_line (it, mode_line_p);
 29055                           else
 29056                             produce_special_glyphs (it, IT_TRUNCATION);
 29057                         }
 29058                     }
 29059                   produce_special_glyphs (it, IT_TRUNCATION);
 29060                 }
 29061               row->truncated_on_right_p = true;
 29062             }
 29063           break;
 29064         }
 29065     }
 29066 
 29067   /* Maybe insert a truncation at the left.  */
 29068   if (it->first_visible_x
 29069       && it_charpos > 0)
 29070     {
 29071       if (!FRAME_WINDOW_P (it->f)
 29072           || (row->reversed_p
 29073               ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29074               : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 29075         insert_left_trunc_glyphs (it);
 29076       row->truncated_on_left_p = true;
 29077     }
 29078 
 29079   it->face_id = saved_face_id;
 29080 
 29081   /* Value is number of columns displayed.  */
 29082   return it->hpos - hpos_at_start;
 29083 }
 29084 
 29085 
 29086 
 29087 /* This is like a combination of memq and assq.  Return 1/2 if PROPVAL
 29088    appears as an element of LIST or as the car of an element of LIST.
 29089    If PROPVAL is a list, compare each element against LIST in that
 29090    way, and return 1/2 if any element of PROPVAL is found in LIST.
 29091    Otherwise return 0.  This function cannot quit.
 29092    The return value is 2 if the text is invisible but with an ellipsis
 29093    and 1 if it's invisible and without an ellipsis.  */
 29094 
 29095 int
 29096 invisible_prop (Lisp_Object propval, Lisp_Object list)
 29097 {
 29098   Lisp_Object tail, proptail;
 29099 
 29100   for (tail = list; CONSP (tail); tail = XCDR (tail))
 29101     {
 29102       register Lisp_Object tem;
 29103       tem = XCAR (tail);
 29104       if (EQ (propval, tem))
 29105         return 1;
 29106       if (CONSP (tem) && EQ (propval, XCAR (tem)))
 29107         return NILP (XCDR (tem)) ? 1 : 2;
 29108     }
 29109 
 29110   if (CONSP (propval))
 29111     {
 29112       for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
 29113         {
 29114           Lisp_Object propelt;
 29115           propelt = XCAR (proptail);
 29116           for (tail = list; CONSP (tail); tail = XCDR (tail))
 29117             {
 29118               register Lisp_Object tem;
 29119               tem = XCAR (tail);
 29120               if (EQ (propelt, tem))
 29121                 return 1;
 29122               if (CONSP (tem) && EQ (propelt, XCAR (tem)))
 29123                 return NILP (XCDR (tem)) ? 1 : 2;
 29124             }
 29125         }
 29126     }
 29127 
 29128   return 0;
 29129 }
 29130 
 29131 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
 29132        doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
 29133 POS should be a marker or a buffer position; the value of the `invisible'
 29134 property at that position in the current buffer is examined.
 29135 POS can also be the actual value of the `invisible' text or overlay
 29136 property of the text of interest, in which case the value itself is
 29137 examined.
 29138 
 29139 The non-nil value returned can be t for currently invisible text that is
 29140 entirely hidden on display, or some other non-nil, non-t value if the
 29141 text is replaced by an ellipsis.
 29142 
 29143 Note that whether text with `invisible' property is actually hidden on
 29144 display may depend on `buffer-invisibility-spec', which see.  */)
 29145   (Lisp_Object pos)
 29146 {
 29147   Lisp_Object prop
 29148     = (FIXNATP (pos) || MARKERP (pos)
 29149        ? Fget_char_property (pos, Qinvisible, Qnil)
 29150        : pos);
 29151   int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
 29152   return (invis == 0 ? Qnil
 29153           : invis == 1 ? Qt
 29154           : make_fixnum (invis));
 29155 }
 29156 
 29157 /* Calculate a width or height in pixels from a specification using
 29158    the following elements:
 29159 
 29160    SPEC ::=
 29161      NUM      - a (fractional) multiple of the default font width/height
 29162      (NUM)    - specifies exactly NUM pixels
 29163      UNIT     - a fixed number of pixels, see below.
 29164      ELEMENT  - size of a display element in pixels, see below.
 29165      (NUM . SPEC) - equals NUM * SPEC
 29166      (+ SPEC SPEC ...)  - add pixel values
 29167      (- SPEC SPEC ...)  - subtract pixel values
 29168      (- SPEC)           - negate pixel value
 29169 
 29170    NUM ::=
 29171      INT or FLOAT   - a number constant
 29172      SYMBOL         - use symbol's (buffer local) variable binding.
 29173 
 29174    UNIT ::=
 29175      in       - pixels per inch  *)
 29176      mm       - pixels per 1/1000 meter  *)
 29177      cm       - pixels per 1/100 meter   *)
 29178      width    - width of current font in pixels.
 29179      height   - height of current font in pixels.
 29180 
 29181      *) using the ratio(s) defined in display-pixels-per-inch.
 29182 
 29183    ELEMENT ::=
 29184 
 29185      left-fringe          - left fringe width in pixels
 29186      right-fringe         - right fringe width in pixels
 29187 
 29188      left-margin          - left margin width in pixels
 29189      right-margin         - right margin width in pixels
 29190 
 29191      scroll-bar           - scroll-bar area width in pixels
 29192 
 29193    Examples:
 29194 
 29195    Pixels corresponding to 5 inches:
 29196      (5 . in)
 29197 
 29198    Total width of non-text areas on left side of window (if scroll-bar is on left):
 29199      '(space :width (+ left-fringe left-margin scroll-bar))
 29200 
 29201    Align to first text column (in header line):
 29202      '(space :align-to 0)
 29203 
 29204    Align to middle of text area minus half the width of variable `my-image'
 29205    containing a loaded image:
 29206      '(space :align-to (0.5 . (- text my-image)))
 29207 
 29208    Width of left margin minus width of 1 character in the default font:
 29209      '(space :width (- left-margin 1))
 29210 
 29211    Width of left margin minus width of 2 characters in the current font:
 29212      '(space :width (- left-margin (2 . width)))
 29213 
 29214    Center 1 character over left-margin (in header line):
 29215      '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
 29216 
 29217    Different ways to express width of left fringe plus left margin minus one pixel:
 29218      '(space :width (- (+ left-fringe left-margin) (1)))
 29219      '(space :width (+ left-fringe left-margin (- (1))))
 29220      '(space :width (+ left-fringe left-margin (-1)))
 29221 
 29222    If ALIGN_TO is NULL, returns the result in *RES.  If ALIGN_TO is
 29223    non-NULL, the value of *ALIGN_TO is a window-relative pixel
 29224    coordinate, and *RES is the additional pixel width from that point
 29225    till the end of the stretch glyph.
 29226 
 29227    WIDTH_P non-zero means take the width dimension or X coordinate of
 29228    the object specified by PROP, WIDTH_P zero means take the height
 29229    dimension or the Y coordinate.  (Therefore, if ALIGN_TO is
 29230    non-NULL, WIDTH_P should be non-zero.)
 29231 
 29232    FONT is the font of the face of the surrounding text.
 29233 
 29234    The return value is non-zero if width or height were successfully
 29235    calculated, i.e. if PROP is a valid spec.  */
 29236 
 29237 static bool
 29238 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 29239                             struct font *font, bool width_p, int *align_to)
 29240 {
 29241   /* Don't adjust for line number if we didn't yet produce it for this
 29242      screen line.  This is for when this function is called from
 29243      move_it_in_display_line_to that was called by display_line to get
 29244      past the glyphs hscrolled off the left side of the window.  */
 29245   int lnum_pixel_width = it->line_number_produced_p ? it->lnum_pixel_width : 0;
 29246   double pixels;
 29247 
 29248 # define OK_PIXELS(val) (*res = (val), true)
 29249 # define OK_ALIGN_TO(val) (*align_to = (val), true)
 29250 
 29251   if (NILP (prop))
 29252     return OK_PIXELS (0);
 29253 
 29254   eassert (FRAME_LIVE_P (it->f));
 29255 
 29256   if (SYMBOLP (prop))
 29257     {
 29258       if (SCHARS (SYMBOL_NAME (prop)) == 2)
 29259         {
 29260           char *unit = SSDATA (SYMBOL_NAME (prop));
 29261 
 29262           /* The UNIT expression, e.g. as part of (NUM . UNIT).  */
 29263           if (unit[0] == 'i' && unit[1] == 'n')
 29264             pixels = 1.0;
 29265           else if (unit[0] == 'm' && unit[1] == 'm')
 29266             pixels = 25.4;
 29267           else if (unit[0] == 'c' && unit[1] == 'm')
 29268             pixels = 2.54;
 29269           else
 29270             pixels = 0;
 29271           if (pixels > 0)
 29272             {
 29273               double ppi = (width_p ? FRAME_RES_X (it->f)
 29274                             : FRAME_RES_Y (it->f));
 29275 
 29276               if (ppi > 0)
 29277                 return OK_PIXELS (ppi / pixels);
 29278               return false;
 29279             }
 29280         }
 29281 
 29282 #ifdef HAVE_WINDOW_SYSTEM
 29283       /* 'height': the height of FONT.  */
 29284       if (EQ (prop, Qheight))
 29285         return OK_PIXELS (font
 29286                           ? normal_char_height (font, -1)
 29287                           : FRAME_LINE_HEIGHT (it->f));
 29288       /* 'width': the width of FONT.  */
 29289       if (EQ (prop, Qwidth))
 29290         return OK_PIXELS (font
 29291                           ? (font->average_width
 29292                              ? font->average_width
 29293                              : font->space_width)
 29294                           : FRAME_COLUMN_WIDTH (it->f));
 29295 #else
 29296       if (EQ (prop, Qheight) || EQ (prop, Qwidth))
 29297         return OK_PIXELS (1);
 29298 #endif
 29299 
 29300       /* 'text': the width or height of the text area.  */
 29301       if (EQ (prop, Qtext))
 29302           return OK_PIXELS (width_p
 29303                             ? (window_box_width (it->w, TEXT_AREA)
 29304                                - lnum_pixel_width)
 29305                             : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
 29306 
 29307       /* ':align_to'.  First time we compute the value, window
 29308          elements are interpreted as the position of the element's
 29309          left edge.  */
 29310       if (align_to && *align_to < 0)
 29311         {
 29312           *res = 0;
 29313           /* 'left': left edge of the text area.  */
 29314           if (EQ (prop, Qleft))
 29315             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29316                                 + lnum_pixel_width);
 29317           /* 'right': right edge of the text area.  */
 29318           if (EQ (prop, Qright))
 29319             return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
 29320           /* 'center': the center of the text area.  */
 29321           if (EQ (prop, Qcenter))
 29322             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29323                                 + lnum_pixel_width
 29324                                 + window_box_width (it->w, TEXT_AREA) / 2);
 29325           /* 'left-fringe': left edge of the left fringe.  */
 29326           if (EQ (prop, Qleft_fringe))
 29327             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29328                                 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
 29329                                 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
 29330           /* 'right-fringe': left edge of the right fringe.  */
 29331           if (EQ (prop, Qright_fringe))
 29332             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29333                                 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29334                                 : window_box_right_offset (it->w, TEXT_AREA));
 29335           /* 'left-margin': left edge of the left display margin.  */
 29336           if (EQ (prop, Qleft_margin))
 29337             return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
 29338           /* 'right-margin': left edge of the right display margin.  */
 29339           if (EQ (prop, Qright_margin))
 29340             return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
 29341           /* 'scroll-bar': left edge of the vertical scroll bar.  */
 29342           if (EQ (prop, Qscroll_bar))
 29343             return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
 29344                                 ? 0
 29345                                 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29346                                    + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29347                                       ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29348                                       : 0)));
 29349         }
 29350       else
 29351         {
 29352           /* Otherwise, the elements stand for their width.  */
 29353           if (EQ (prop, Qleft_fringe))
 29354             return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
 29355           if (EQ (prop, Qright_fringe))
 29356             return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
 29357           if (EQ (prop, Qleft_margin))
 29358             return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
 29359           if (EQ (prop, Qright_margin))
 29360             return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
 29361           if (EQ (prop, Qscroll_bar))
 29362             return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
 29363         }
 29364 
 29365       prop = buffer_local_value (prop, it->w->contents);
 29366       if (BASE_EQ (prop, Qunbound))
 29367         prop = Qnil;
 29368     }
 29369 
 29370   if (NUMBERP (prop))
 29371     {
 29372       int base_unit = (width_p
 29373                        ? FRAME_COLUMN_WIDTH (it->f)
 29374                        : FRAME_LINE_HEIGHT (it->f));
 29375       if (width_p && align_to && *align_to < 0)
 29376         return OK_PIXELS (XFLOATINT (prop) * base_unit + lnum_pixel_width);
 29377       return OK_PIXELS (XFLOATINT (prop) * base_unit);
 29378     }
 29379 
 29380   if (CONSP (prop))
 29381     {
 29382       Lisp_Object car = XCAR (prop);
 29383       Lisp_Object cdr = XCDR (prop);
 29384 
 29385       if (SYMBOLP (car))
 29386         {
 29387 #ifdef HAVE_WINDOW_SYSTEM
 29388           /* '(image PROPS...)': width or height of the specified image.  */
 29389           if (FRAME_WINDOW_P (it->f)
 29390               && valid_image_p (prop))
 29391             {
 29392               ptrdiff_t id = lookup_image (it->f, prop, it->face_id);
 29393               struct image *img = IMAGE_FROM_ID (it->f, id);
 29394 
 29395               return OK_PIXELS (width_p ? img->width : img->height);
 29396             }
 29397           /* '(xwidget PROPS...)': dimensions of the specified xwidget.  */
 29398           if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
 29399             {
 29400               /* TODO: Don't return dummy size.  */
 29401               return OK_PIXELS (100);
 29402             }
 29403 #endif
 29404           /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
 29405              recursively calculated values.  */
 29406           if (EQ (car, Qplus) || EQ (car, Qminus))
 29407             {
 29408               bool first = true;
 29409               double px;
 29410 
 29411               pixels = 0;
 29412               while (CONSP (cdr))
 29413                 {
 29414                   if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
 29415                                                    font, width_p, align_to))
 29416                     return false;
 29417                   if (first)
 29418                     pixels = (EQ (car, Qplus) ? px : -px), first = false;
 29419                   else
 29420                     pixels += px;
 29421                   cdr = XCDR (cdr);
 29422                 }
 29423               if (EQ (car, Qminus))
 29424                 pixels = -pixels;
 29425               return OK_PIXELS (pixels);
 29426             }
 29427 
 29428           car = buffer_local_value (car, it->w->contents);
 29429           if (BASE_EQ (car, Qunbound))
 29430             car = Qnil;
 29431         }
 29432 
 29433       /* '(NUM)': absolute number of pixels.  */
 29434       if (NUMBERP (car))
 29435         {
 29436           double fact;
 29437           int offset =
 29438             width_p && align_to && *align_to < 0 ? lnum_pixel_width : 0;
 29439           pixels = XFLOATINT (car);
 29440           if (NILP (cdr))
 29441             return OK_PIXELS (pixels + offset);
 29442           if (calc_pixel_width_or_height (&fact, it, cdr,
 29443                                           font, width_p, align_to))
 29444             return OK_PIXELS (pixels * fact + offset);
 29445           return false;
 29446         }
 29447 
 29448       return false;
 29449     }
 29450 
 29451   return false;
 29452 }
 29453 
 29454 void
 29455 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
 29456 {
 29457 #ifdef HAVE_WINDOW_SYSTEM
 29458   normal_char_ascent_descent (font, -1, ascent, descent);
 29459 #else
 29460   *ascent = 1;
 29461   *descent = 0;
 29462 #endif
 29463 }
 29464 
 29465 
 29466 /***********************************************************************
 29467                              Glyph Display
 29468  ***********************************************************************/
 29469 
 29470 #ifdef HAVE_WINDOW_SYSTEM
 29471 
 29472 #ifdef GLYPH_DEBUG
 29473 
 29474 extern void dump_glyph_string (struct glyph_string *) EXTERNALLY_VISIBLE;
 29475 void
 29476 dump_glyph_string (struct glyph_string *s)
 29477 {
 29478   fputs ("glyph string\n", stderr);
 29479   fprintf (stderr, "  x, y, w, h = %d, %d, %d, %d\n",
 29480            s->x, s->y, s->width, s->height);
 29481   fprintf (stderr, "  ybase = %d\n", s->ybase);
 29482   fprintf (stderr, "  hl = %u\n", s->hl);
 29483   fprintf (stderr, "  left overhang = %d, right = %d\n",
 29484            s->left_overhang, s->right_overhang);
 29485   fprintf (stderr, "  nchars = %d\n", s->nchars);
 29486   fprintf (stderr, "  extends to end of line = %d\n",
 29487            s->extends_to_end_of_line_p);
 29488   fprintf (stderr, "  font height = %d\n", FONT_HEIGHT (s->font));
 29489   fprintf (stderr, "  bg width = %d\n", s->background_width);
 29490 }
 29491 
 29492 #endif /* GLYPH_DEBUG */
 29493 
 29494 /* Initialize glyph string S.  CHAR2B is a suitably allocated vector
 29495    of 2-byte unsigned integers for S; it can't be allocated in
 29496    init_glyph_string because it must be allocated via `alloca'.  W
 29497    is the window on which S is drawn.  ROW and AREA are the glyph row
 29498    and area within the row from which S is constructed.  START is the
 29499    index of the first glyph structure covered by S.  HL is a
 29500    face-override for drawing S.  */
 29501 
 29502 #ifdef HAVE_NTGUI
 29503 /* We set inhibit-quit here due to paranoia: get_frame_dc acquires the
 29504    critical section, and we cannot QUIT while we hold the critical
 29505    section.  If any of the code run by callers of ALLOCATE_HDC happens
 29506    to call Lisp (might be possible due to all the hooks lying around),
 29507    we must prevent it from quitting.  */
 29508 # define ALLOCATE_HDC(hdc, f)                   \
 29509   Lisp_Object prev_quit = Vinhibit_quit;        \
 29510   Vinhibit_quit = Qt;                           \
 29511   HDC hdc = get_frame_dc ((f))
 29512 # define RELEASE_HDC(hdc, f)                    \
 29513   release_frame_dc ((f), (hdc));                \
 29514   Vinhibit_quit = prev_quit
 29515 #else
 29516 # define ALLOCATE_HDC(hdc, f)
 29517 # define RELEASE_HDC(hdc, f)
 29518 #endif
 29519 
 29520 static void
 29521 init_glyph_string (struct glyph_string *s,
 29522 #ifdef HAVE_NTGUI
 29523                    HDC hdc,
 29524 #endif
 29525                    unsigned *char2b, struct window *w, struct glyph_row *row,
 29526                    enum glyph_row_area area, int start, enum draw_glyphs_face hl)
 29527 {
 29528   memset (s, 0, sizeof *s);
 29529   s->w = w;
 29530   s->f = XFRAME (w->frame);
 29531 #ifdef HAVE_NTGUI
 29532   s->hdc = hdc;
 29533 #endif
 29534   s->char2b = char2b;
 29535   s->hl = hl;
 29536   s->row = row;
 29537   s->area = area;
 29538   s->first_glyph = row->glyphs[area] + start;
 29539   s->height = row->height;
 29540   s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
 29541   s->ybase = s->y + row->ascent;
 29542 }
 29543 
 29544 
 29545 /* Append the list of glyph strings with head H and tail T to the list
 29546    with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the result.  */
 29547 
 29548 static void
 29549 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29550                            struct glyph_string *h, struct glyph_string *t)
 29551 {
 29552   if (h)
 29553     {
 29554       if (*head)
 29555         (*tail)->next = h;
 29556       else
 29557         *head = h;
 29558       h->prev = *tail;
 29559       *tail = t;
 29560     }
 29561 }
 29562 
 29563 
 29564 /* Prepend the list of glyph strings with head H and tail T to the
 29565    list with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the
 29566    result.  */
 29567 
 29568 static void
 29569 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29570                             struct glyph_string *h, struct glyph_string *t)
 29571 {
 29572   if (h)
 29573     {
 29574       if (*head)
 29575         (*head)->prev = t;
 29576       else
 29577         *tail = t;
 29578       t->next = *head;
 29579       *head = h;
 29580     }
 29581 }
 29582 
 29583 
 29584 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
 29585    Set *HEAD and *TAIL to the resulting list.  */
 29586 
 29587 static void
 29588 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
 29589                      struct glyph_string *s)
 29590 {
 29591   s->next = s->prev = NULL;
 29592   append_glyph_string_lists (head, tail, s, s);
 29593 }
 29594 
 29595 
 29596 /* Get face and two-byte form of character C in face FACE_ID on frame F.
 29597    The encoding of C is returned in *CHAR2B.  DISPLAY_P means
 29598    make sure that X resources for the face returned are allocated.
 29599    Value is a pointer to a realized face that is ready for display if
 29600    DISPLAY_P.  */
 29601 
 29602 static struct face *
 29603 get_char_face_and_encoding (struct frame *f, int c, int face_id,
 29604                             unsigned *char2b, bool display_p)
 29605 {
 29606   struct face *face = FACE_FROM_ID (f, face_id);
 29607   unsigned code = 0;
 29608 
 29609   if (face->font)
 29610     {
 29611       code = face->font->driver->encode_char (face->font, c);
 29612 
 29613       if (code == FONT_INVALID_CODE)
 29614         code = 0;
 29615     }
 29616   /* Ensure that the code is only 2 bytes wide.  */
 29617   *char2b = code & 0xFFFF;
 29618 
 29619   /* Make sure X resources of the face are allocated.  */
 29620 #ifdef HAVE_X_WINDOWS
 29621   if (display_p)
 29622 #endif
 29623     {
 29624       eassert (face != NULL);
 29625       prepare_face_for_display (f, face);
 29626     }
 29627 
 29628   return face;
 29629 }
 29630 
 29631 
 29632 /* Get face and two-byte form of character glyph GLYPH on frame F.
 29633    The encoding of GLYPH->u.ch is returned in *CHAR2B.  Value is
 29634    a pointer to a realized face that is ready for display.  */
 29635 
 29636 static struct face *
 29637 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
 29638                              unsigned *char2b)
 29639 {
 29640   struct face *face;
 29641   unsigned code = 0;
 29642 
 29643   eassert (glyph->type == CHAR_GLYPH);
 29644   face = FACE_FROM_ID (f, glyph->face_id);
 29645 
 29646   /* Make sure X resources of the face are allocated.  */
 29647   prepare_face_for_display (f, face);
 29648 
 29649   if (face->font)
 29650     {
 29651       if (CHAR_BYTE8_P (glyph->u.ch))
 29652         code = CHAR_TO_BYTE8 (glyph->u.ch);
 29653       else
 29654         code = face->font->driver->encode_char (face->font, glyph->u.ch);
 29655 
 29656       if (code == FONT_INVALID_CODE)
 29657         code = 0;
 29658     }
 29659 
 29660   /* Ensure that the code is only 2 bytes wide.  */
 29661   *char2b = code & 0xFFFF;
 29662   return face;
 29663 }
 29664 
 29665 
 29666 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
 29667    Return true iff FONT has a glyph for C.  */
 29668 
 29669 static bool
 29670 get_char_glyph_code (int c, struct font *font, unsigned *char2b)
 29671 {
 29672   unsigned code;
 29673 
 29674   if (CHAR_BYTE8_P (c))
 29675     code = CHAR_TO_BYTE8 (c);
 29676   else
 29677     code = font->driver->encode_char (font, c);
 29678 
 29679   if (code == FONT_INVALID_CODE)
 29680     return false;
 29681 
 29682   /* Ensure that the code is only 2 bytes wide.  */
 29683   *char2b = code & 0xFFFF;
 29684   return true;
 29685 }
 29686 
 29687 
 29688 /* Fill glyph string S with composition components specified by S->cmp.
 29689 
 29690    BASE_FACE is the base face of the composition.
 29691    S->cmp_from is the index of the first component for S.
 29692 
 29693    OVERLAPS non-zero means S should draw the foreground only, and use
 29694    its physical height for clipping.  See also draw_glyphs.
 29695 
 29696    Value is the index of a component not in S.  */
 29697 
 29698 static int
 29699 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
 29700                              int overlaps)
 29701 {
 29702   int i;
 29703   /* For all glyphs of this composition, starting at the offset
 29704      S->cmp_from, until we reach the end of the definition or encounter a
 29705      glyph that requires the different face, add it to S.  */
 29706   struct face *face;
 29707 
 29708   eassert (s);
 29709 
 29710   s->for_overlaps = overlaps;
 29711   s->face = NULL;
 29712   s->font = NULL;
 29713   for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
 29714     {
 29715       int c = COMPOSITION_GLYPH (s->cmp, i);
 29716 
 29717       /* TAB in a composition means display glyphs with padding space
 29718          on the left or right.  */
 29719       if (c != '\t')
 29720         {
 29721           int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
 29722                                        -1, Qnil);
 29723 
 29724           face = get_char_face_and_encoding (s->f, c, face_id,
 29725                                              s->char2b + i, true);
 29726           if (face)
 29727             {
 29728               if (! s->face)
 29729                 {
 29730                   s->face = face;
 29731                   s->font = s->face->font;
 29732                 }
 29733               else if (s->face != face)
 29734                 break;
 29735             }
 29736         }
 29737       ++s->nchars;
 29738     }
 29739   s->cmp_to = i;
 29740 
 29741   if (s->face == NULL)
 29742     {
 29743       s->face = base_face->ascii_face;
 29744       s->font = s->face->font;
 29745     }
 29746 
 29747   if (s->hl == DRAW_MOUSE_FACE
 29748       || (s->hl == DRAW_CURSOR
 29749           && MATRIX_ROW (s->w->current_matrix,
 29750                          s->w->phys_cursor.vpos)->mouse_face_p
 29751           && cursor_in_mouse_face_p (s->w)))
 29752     {
 29753       int c = COMPOSITION_GLYPH (s->cmp, 0);
 29754       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29755       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29756       if (!s->face)
 29757         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29758 
 29759       s->face = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face, c, -1, Qnil));
 29760       prepare_face_for_display (s->f, s->face);
 29761     }
 29762 
 29763   /* All glyph strings for the same composition has the same width,
 29764      i.e. the width set for the first component of the composition.  */
 29765   s->width = s->first_glyph->pixel_width;
 29766 
 29767   /* If the specified font could not be loaded, use the frame's
 29768      default font, but record the fact that we couldn't load it in
 29769      the glyph string so that we can draw rectangles for the
 29770      characters of the glyph string.  */
 29771   if (s->font == NULL)
 29772     {
 29773       s->font_not_found_p = true;
 29774       s->font = FRAME_FONT (s->f);
 29775     }
 29776 
 29777   /* Adjust base line for subscript/superscript text.  */
 29778   s->ybase += s->first_glyph->voffset;
 29779 
 29780   return s->cmp_to;
 29781 }
 29782 
 29783 static int
 29784 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
 29785                            int start, int end, int overlaps)
 29786 {
 29787   struct glyph *glyph, *last;
 29788   int voffset;
 29789   Lisp_Object lgstring;
 29790   int i;
 29791   bool glyph_not_available_p;
 29792 
 29793   s->for_overlaps = overlaps;
 29794   glyph = s->row->glyphs[s->area] + start;
 29795   last = s->row->glyphs[s->area] + end;
 29796   voffset = glyph->voffset;
 29797   glyph_not_available_p = glyph->glyph_not_available_p;
 29798   s->cmp_id = glyph->u.cmp.id;
 29799   s->cmp_from = glyph->slice.cmp.from;
 29800   s->cmp_to = glyph->slice.cmp.to + 1;
 29801   if (s->hl == DRAW_MOUSE_FACE
 29802       || (s->hl == DRAW_CURSOR
 29803           && MATRIX_ROW (s->w->current_matrix,
 29804                          s->w->phys_cursor.vpos)->mouse_face_p
 29805           && cursor_in_mouse_face_p (s->w)))
 29806     {
 29807       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29808       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29809       if (!s->face)
 29810         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29811       prepare_face_for_display (s->f, s->face);
 29812     }
 29813   else
 29814     s->face = FACE_FROM_ID (s->f, face_id);
 29815   lgstring = composition_gstring_from_id (s->cmp_id);
 29816   s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
 29817   /* The width of a composition glyph string is the sum of the
 29818      composition's glyph widths.  */
 29819   s->width = s->first_glyph->pixel_width;
 29820   glyph++;
 29821   while (glyph < last
 29822          && glyph->u.cmp.automatic
 29823          && glyph->u.cmp.id == s->cmp_id
 29824          && glyph->face_id == face_id
 29825          && s->cmp_to == glyph->slice.cmp.from
 29826          && glyph->glyph_not_available_p == glyph_not_available_p)
 29827     {
 29828       s->width += glyph->pixel_width;
 29829       s->cmp_to = (glyph++)->slice.cmp.to + 1;
 29830     }
 29831 
 29832   for (i = s->cmp_from; i < s->cmp_to; i++)
 29833     {
 29834       Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
 29835       unsigned code = LGLYPH_CODE (lglyph);
 29836 
 29837       /* Ensure that the code is only 2 bytes wide.  */
 29838       s->char2b[i] = code & 0xFFFF;
 29839     }
 29840 
 29841   /* If the specified font could not be loaded, record that fact in
 29842      S->font_not_found_p so that we can draw rectangles for the
 29843      characters of the glyph string.  */
 29844   if (glyph_not_available_p)
 29845     s->font_not_found_p = true;
 29846 
 29847   /* Adjust base line for subscript/superscript text.  */
 29848   s->ybase += voffset;
 29849 
 29850   return glyph - s->row->glyphs[s->area];
 29851 }
 29852 
 29853 
 29854 /* Fill glyph string S from a sequence glyphs for glyphless characters.
 29855    See the comment of fill_glyph_string for arguments.
 29856    Value is the index of the first glyph not in S.  */
 29857 
 29858 
 29859 static int
 29860 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
 29861                              int start, int end, int overlaps)
 29862 {
 29863   struct glyph *glyph, *last;
 29864   int voffset;
 29865 
 29866   eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
 29867   s->for_overlaps = overlaps;
 29868   glyph = s->row->glyphs[s->area] + start;
 29869   last = s->row->glyphs[s->area] + end;
 29870   voffset = glyph->voffset;
 29871   s->face = FACE_FROM_ID (s->f, face_id);
 29872   s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
 29873   if (s->hl == DRAW_MOUSE_FACE
 29874       || (s->hl == DRAW_CURSOR
 29875           && MATRIX_ROW (s->w->current_matrix,
 29876                          s->w->phys_cursor.vpos)->mouse_face_p
 29877           && cursor_in_mouse_face_p (s->w)))
 29878     {
 29879       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29880       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29881       if (!s->face)
 29882         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29883       prepare_face_for_display (s->f, s->face);
 29884     }
 29885   s->nchars = 1;
 29886   s->width = glyph->pixel_width;
 29887   glyph++;
 29888   while (glyph < last
 29889          && glyph->type == GLYPHLESS_GLYPH
 29890          && glyph->voffset == voffset
 29891          && glyph->face_id == face_id)
 29892     {
 29893       s->nchars++;
 29894       s->width += glyph->pixel_width;
 29895       glyph++;
 29896     }
 29897   s->ybase += voffset;
 29898   return glyph - s->row->glyphs[s->area];
 29899 }
 29900 
 29901 
 29902 /* Fill glyph string S from a sequence of character glyphs.
 29903 
 29904    FACE_ID is the face id of the string.  START is the index of the
 29905    first glyph to consider, END is the index of the last + 1.
 29906    OVERLAPS non-zero means S should draw the foreground only, and use
 29907    its physical height for clipping.  See also draw_glyphs.
 29908 
 29909    Value is the index of the first glyph not in S.  */
 29910 
 29911 static int
 29912 fill_glyph_string (struct glyph_string *s, int face_id,
 29913                    int start, int end, int overlaps)
 29914 {
 29915   struct glyph *glyph, *last;
 29916   int voffset;
 29917   bool glyph_not_available_p;
 29918 
 29919   eassert (s->f == XFRAME (s->w->frame));
 29920   eassert (s->nchars == 0);
 29921   eassert (start >= 0 && end > start);
 29922 
 29923   s->for_overlaps = overlaps;
 29924   glyph = s->row->glyphs[s->area] + start;
 29925   last = s->row->glyphs[s->area] + end;
 29926   voffset = glyph->voffset;
 29927   s->padding_p = glyph->padding_p;
 29928   glyph_not_available_p = glyph->glyph_not_available_p;
 29929 
 29930   while (glyph < last
 29931          && glyph->type == CHAR_GLYPH
 29932          && glyph->voffset == voffset
 29933          /* Same face id implies same font, nowadays.  */
 29934          && glyph->face_id == face_id
 29935          && glyph->glyph_not_available_p == glyph_not_available_p)
 29936     {
 29937       s->face = get_glyph_face_and_encoding (s->f, glyph,
 29938                                              s->char2b + s->nchars);
 29939       ++s->nchars;
 29940       eassert (s->nchars <= end - start);
 29941       s->width += glyph->pixel_width;
 29942       if (glyph++->padding_p != s->padding_p)
 29943         break;
 29944     }
 29945 
 29946   s->font = s->face->font;
 29947 
 29948   if (s->hl == DRAW_MOUSE_FACE
 29949       || (s->hl == DRAW_CURSOR
 29950           && MATRIX_ROW (s->w->current_matrix,
 29951                          s->w->phys_cursor.vpos)->mouse_face_p
 29952           && cursor_in_mouse_face_p (s->w)))
 29953     {
 29954       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29955       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29956       if (!s->face)
 29957         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29958       s->face
 29959         = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face,
 29960                                              s->first_glyph->u.ch, -1, Qnil));
 29961       prepare_face_for_display (s->f, s->face);
 29962     }
 29963 
 29964   /* If the specified font could not be loaded, use the frame's font,
 29965      but record the fact that we couldn't load it in
 29966      S->font_not_found_p so that we can draw rectangles for the
 29967      characters of the glyph string.  */
 29968   if (s->font == NULL || glyph_not_available_p)
 29969     {
 29970       s->font_not_found_p = true;
 29971       s->font = FRAME_FONT (s->f);
 29972     }
 29973 
 29974   /* Adjust base line for subscript/superscript text.  */
 29975   s->ybase += voffset;
 29976 
 29977   eassert (s->face && s->face->gc);
 29978   return glyph - s->row->glyphs[s->area];
 29979 }
 29980 
 29981 
 29982 /* Fill glyph string S from image glyph S->first_glyph.  */
 29983 
 29984 static void
 29985 fill_image_glyph_string (struct glyph_string *s)
 29986 {
 29987   eassert (s->first_glyph->type == IMAGE_GLYPH);
 29988   s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
 29989   eassert (s->img);
 29990   s->slice = s->first_glyph->slice.img;
 29991   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29992   s->font = s->face->font;
 29993   if (s->hl == DRAW_MOUSE_FACE
 29994       || (s->hl == DRAW_CURSOR
 29995           && MATRIX_ROW (s->w->current_matrix,
 29996                          s->w->phys_cursor.vpos)->mouse_face_p
 29997           && cursor_in_mouse_face_p (s->w)))
 29998     {
 29999       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30000       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30001       if (!s->face)
 30002         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30003       prepare_face_for_display (s->f, s->face);
 30004     }
 30005   s->width = s->first_glyph->pixel_width;
 30006 
 30007   /* Adjust base line for subscript/superscript text.  */
 30008   s->ybase += s->first_glyph->voffset;
 30009 }
 30010 
 30011 
 30012 #ifdef HAVE_XWIDGETS
 30013 static void
 30014 fill_xwidget_glyph_string (struct glyph_string *s)
 30015 {
 30016   eassert (s->first_glyph->type == XWIDGET_GLYPH);
 30017   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 30018   s->font = s->face->font;
 30019   if (s->hl == DRAW_MOUSE_FACE
 30020       || (s->hl == DRAW_CURSOR
 30021           && MATRIX_ROW (s->w->current_matrix,
 30022                          s->w->phys_cursor.vpos)->mouse_face_p
 30023           && cursor_in_mouse_face_p (s->w)))
 30024     {
 30025       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30026       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30027       if (!s->face)
 30028         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30029       prepare_face_for_display (s->f, s->face);
 30030     }
 30031   s->width = s->first_glyph->pixel_width;
 30032   s->ybase += s->first_glyph->voffset;
 30033   s->xwidget = xwidget_from_id (s->first_glyph->u.xwidget);
 30034 }
 30035 #endif
 30036 /* Fill glyph string S from a sequence of stretch glyphs.
 30037 
 30038    START is the index of the first glyph to consider,
 30039    END is the index of the last + 1.
 30040 
 30041    Value is the index of the first glyph not in S.  */
 30042 
 30043 static int
 30044 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
 30045 {
 30046   struct glyph *glyph, *last;
 30047   int voffset, face_id;
 30048 
 30049   eassert (s->first_glyph->type == STRETCH_GLYPH);
 30050 
 30051   glyph = s->row->glyphs[s->area] + start;
 30052   last = s->row->glyphs[s->area] + end;
 30053   face_id = glyph->face_id;
 30054   s->face = FACE_FROM_ID (s->f, face_id);
 30055   s->font = s->face->font;
 30056   if (s->hl == DRAW_MOUSE_FACE
 30057       || (s->hl == DRAW_CURSOR
 30058           && MATRIX_ROW (s->w->current_matrix,
 30059                          s->w->phys_cursor.vpos)->mouse_face_p
 30060           && cursor_in_mouse_face_p (s->w)))
 30061     {
 30062       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 30063       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 30064       if (!s->face)
 30065         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 30066       prepare_face_for_display (s->f, s->face);
 30067     }
 30068   s->width = glyph->pixel_width;
 30069   s->nchars = 1;
 30070   voffset = glyph->voffset;
 30071 
 30072   for (++glyph;
 30073        (glyph < last
 30074         && glyph->type == STRETCH_GLYPH
 30075         && glyph->voffset == voffset
 30076         && glyph->face_id == face_id);
 30077        ++glyph)
 30078     s->width += glyph->pixel_width;
 30079 
 30080   /* Adjust base line for subscript/superscript text.  */
 30081   s->ybase += voffset;
 30082 
 30083   /* The case that face->gc == 0 is handled when drawing the glyph
 30084      string by calling prepare_face_for_display.  */
 30085   eassert (s->face);
 30086   return glyph - s->row->glyphs[s->area];
 30087 }
 30088 
 30089 static struct font_metrics *
 30090 get_per_char_metric (struct font *font, const unsigned *char2b)
 30091 {
 30092   static struct font_metrics metrics;
 30093 
 30094   if (! font)
 30095     return NULL;
 30096   if (*char2b == FONT_INVALID_CODE)
 30097     return NULL;
 30098 
 30099   font->driver->text_extents (font, char2b, 1, &metrics);
 30100   return &metrics;
 30101 }
 30102 
 30103 /* A subroutine that computes "normal" values of ASCENT and DESCENT
 30104    for FONT.  Values are taken from font-global ones, except for fonts
 30105    that claim preposterously large values, but whose glyphs actually
 30106    have reasonable dimensions.  C is the character to use for metrics
 30107    if the font-global values are too large; if C is negative, the
 30108    function selects a default character.  */
 30109 static void
 30110 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
 30111 {
 30112   *ascent = FONT_BASE (font);
 30113   *descent = FONT_DESCENT (font);
 30114 
 30115   if (FONT_TOO_HIGH (font))
 30116     {
 30117       unsigned char2b;
 30118 
 30119       /* Get metrics of C, defaulting to a reasonably sized ASCII
 30120          character.  */
 30121       if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
 30122         {
 30123           struct font_metrics *pcm = get_per_char_metric (font, &char2b);
 30124           eassume (pcm);
 30125 
 30126           if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
 30127             {
 30128               /* We add 1 pixel to character dimensions as heuristics
 30129                  that produces nicer display, e.g. when the face has
 30130                  the box attribute.  */
 30131               *ascent = pcm->ascent + 1;
 30132               *descent = pcm->descent + 1;
 30133             }
 30134         }
 30135     }
 30136 }
 30137 
 30138 /* A subroutine that computes a reasonable "normal character height"
 30139    for fonts that claim preposterously large vertical dimensions, but
 30140    whose glyphs are actually reasonably sized.  C is the character
 30141    whose metrics to use for those fonts, or -1 for default
 30142    character.  */
 30143 static int
 30144 normal_char_height (struct font *font, int c)
 30145 {
 30146   int ascent, descent;
 30147 
 30148   normal_char_ascent_descent (font, c, &ascent, &descent);
 30149 
 30150   return ascent + descent;
 30151 }
 30152 
 30153 /* EXPORT for RIF:
 30154    Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
 30155    frame F.  Overhangs of glyphs other than type CHAR_GLYPH are
 30156    assumed to be zero.  */
 30157 
 30158 void
 30159 gui_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
 30160 {
 30161   *left = *right = 0;
 30162 
 30163   if (glyph->type == CHAR_GLYPH)
 30164     {
 30165       unsigned char2b;
 30166       struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
 30167       if (face->font)
 30168         {
 30169           struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
 30170           if (pcm)
 30171             {
 30172               if (pcm->rbearing > pcm->width)
 30173                 *right = pcm->rbearing - pcm->width;
 30174               if (pcm->lbearing < 0)
 30175                 *left = -pcm->lbearing;
 30176             }
 30177         }
 30178     }
 30179   else if (glyph->type == COMPOSITE_GLYPH)
 30180     {
 30181       if (! glyph->u.cmp.automatic)
 30182         {
 30183           struct composition *cmp = composition_table[glyph->u.cmp.id];
 30184 
 30185           if (cmp->rbearing > cmp->pixel_width)
 30186             *right = cmp->rbearing - cmp->pixel_width;
 30187           if (cmp->lbearing < 0)
 30188             *left = - cmp->lbearing;
 30189         }
 30190       else
 30191         {
 30192           Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
 30193           struct font_metrics metrics;
 30194 
 30195           composition_gstring_width (gstring, glyph->slice.cmp.from,
 30196                                      glyph->slice.cmp.to + 1, &metrics);
 30197           if (metrics.rbearing > metrics.width)
 30198             *right = metrics.rbearing - metrics.width;
 30199           if (metrics.lbearing < 0)
 30200             *left = - metrics.lbearing;
 30201         }
 30202     }
 30203 }
 30204 
 30205 
 30206 /* Return the index of the first glyph preceding glyph string S that
 30207    is overwritten by S because of S's left overhang.  Value is -1
 30208    if no glyphs are overwritten.  */
 30209 
 30210 static int
 30211 left_overwritten (struct glyph_string *s)
 30212 {
 30213   int k;
 30214 
 30215   if (s->left_overhang)
 30216     {
 30217       int x = 0, i;
 30218       struct glyph *glyphs = s->row->glyphs[s->area];
 30219       int first = s->first_glyph - glyphs;
 30220 
 30221       for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
 30222         x -= glyphs[i].pixel_width;
 30223 
 30224       k = i + 1;
 30225     }
 30226   else
 30227     k = -1;
 30228 
 30229   return k;
 30230 }
 30231 
 30232 
 30233 /* Return the index of the first glyph preceding glyph string S that
 30234    is overwriting S because of its right overhang.  Value is -1 if no
 30235    glyph in front of S overwrites S.  */
 30236 
 30237 static int
 30238 left_overwriting (struct glyph_string *s)
 30239 {
 30240   int i, k, x;
 30241   struct glyph *glyphs = s->row->glyphs[s->area];
 30242   int first = s->first_glyph - glyphs;
 30243 
 30244   k = -1;
 30245   x = 0;
 30246   for (i = first - 1; i >= 0; --i)
 30247     {
 30248       int left, right;
 30249       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30250       if (x + right > 0)
 30251         k = i;
 30252       x -= glyphs[i].pixel_width;
 30253     }
 30254 
 30255   return k;
 30256 }
 30257 
 30258 
 30259 /* Return the index of the last glyph following glyph string S that is
 30260    overwritten by S because of S's right overhang.  Value is -1 if
 30261    no such glyph is found.  */
 30262 
 30263 static int
 30264 right_overwritten (struct glyph_string *s)
 30265 {
 30266   int k = -1;
 30267 
 30268   if (s->right_overhang)
 30269     {
 30270       int x = 0, i;
 30271       struct glyph *glyphs = s->row->glyphs[s->area];
 30272       int first = (s->first_glyph - glyphs
 30273                    + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30274       int end = s->row->used[s->area];
 30275 
 30276       for (i = first; i < end && s->right_overhang > x; ++i)
 30277         x += glyphs[i].pixel_width;
 30278 
 30279       k = i;
 30280     }
 30281 
 30282   return k;
 30283 }
 30284 
 30285 
 30286 /* Return the index of the last glyph following glyph string S that
 30287    overwrites S because of its left overhang.  Value is negative
 30288    if no such glyph is found.  */
 30289 
 30290 static int
 30291 right_overwriting (struct glyph_string *s)
 30292 {
 30293   int i, k, x;
 30294   int end = s->row->used[s->area];
 30295   struct glyph *glyphs = s->row->glyphs[s->area];
 30296   int first = (s->first_glyph - glyphs
 30297                + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30298 
 30299   k = -1;
 30300   x = 0;
 30301   for (i = first; i < end; ++i)
 30302     {
 30303       int left, right;
 30304       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30305       if (x - left < 0)
 30306         k = i;
 30307       x += glyphs[i].pixel_width;
 30308     }
 30309 
 30310   return k;
 30311 }
 30312 
 30313 
 30314 /* Set background width of glyph string S.  START is the index of the
 30315    first glyph following S.  LAST_X is the right-most x-position + 1
 30316    in the drawing area.
 30317 
 30318    If S->hl is DRAW_CURSOR, S->f is a window system frame, and the
 30319    cursor in S's window is currently inside mouse face, also update
 30320    S->width to take into account potentially differing :box
 30321    properties between the original face and the mouse face.  */
 30322 
 30323 static void
 30324 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
 30325 {
 30326   /* If the face of this glyph string has to be drawn to the end of
 30327      the drawing area, set S->extends_to_end_of_line_p.  */
 30328 
 30329   if (start == s->row->used[s->area]
 30330       && ((s->row->fill_line_p
 30331            && (s->hl == DRAW_NORMAL_TEXT
 30332                || s->hl == DRAW_IMAGE_RAISED
 30333                || s->hl == DRAW_IMAGE_SUNKEN))
 30334           || s->hl == DRAW_MOUSE_FACE))
 30335     s->extends_to_end_of_line_p = true;
 30336 
 30337   /* If S extends its face to the end of the line, set its
 30338      background_width to the distance to the right edge of the drawing
 30339      area.  */
 30340   if (s->extends_to_end_of_line_p)
 30341     s->background_width = last_x - s->x + 1;
 30342   else
 30343     {
 30344       s->background_width = s->width;
 30345 #ifdef HAVE_WINDOW_SYSTEM
 30346       if (FRAME_WINDOW_P (s->f)
 30347           && s->hl == DRAW_CURSOR
 30348           && MATRIX_ROW (s->w->current_matrix,
 30349                          s->w->phys_cursor.vpos)->mouse_face_p
 30350           && cursor_in_mouse_face_p (s->w))
 30351         {
 30352           /* Adjust the background width of the glyph string, because
 30353              if the glyph's face has the :box attribute, its
 30354              pixel_width might be different when it's displayed in the
 30355              mouse-face, if that also has the :box attribute.  */
 30356           struct glyph *g = s->first_glyph;
 30357           struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
 30358           s->background_width +=
 30359             adjust_glyph_width_for_mouse_face (g, s->row, s->w,
 30360                                                regular_face, s->face);
 30361           /* S->width is probably worth adjusting here as well.  */
 30362           s->width = s->background_width;
 30363         }
 30364 #endif
 30365     }
 30366 }
 30367 
 30368 
 30369 /* Return glyph string that shares background with glyph string S and
 30370    whose `background_width' member has been set.  */
 30371 
 30372 static struct glyph_string *
 30373 glyph_string_containing_background_width (struct glyph_string *s)
 30374 {
 30375   if (s->cmp)
 30376     while (s->cmp_from)
 30377       s = s->prev;
 30378 
 30379   return s;
 30380 }
 30381 
 30382 
 30383 /* Compute overhangs and x-positions for glyph string S and its
 30384    predecessors, or successors.  X is the starting x-position for S.
 30385    BACKWARD_P means process predecessors.  */
 30386 
 30387 static void
 30388 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
 30389 {
 30390   if (backward_p)
 30391     {
 30392       while (s)
 30393         {
 30394           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30395             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30396           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30397             x -= s->width;
 30398           s->x = x;
 30399           s = s->prev;
 30400         }
 30401     }
 30402   else
 30403     {
 30404       while (s)
 30405         {
 30406           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30407             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30408           s->x = x;
 30409           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30410             x += s->width;
 30411           s = s->next;
 30412         }
 30413     }
 30414 }
 30415 
 30416 
 30417 
 30418 /* The following macros are only called from draw_glyphs below.
 30419    They reference the following parameters of that function directly:
 30420      `w', `row', `area', and `overlap_p'
 30421    as well as the following local variables:
 30422      `s', `f', and `hdc' (in W32)  */
 30423 
 30424 #ifdef HAVE_NTGUI
 30425 /* On W32, silently add local `hdc' variable to argument list of
 30426    init_glyph_string.  */
 30427 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30428   init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
 30429 #else
 30430 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30431   init_glyph_string (s, char2b, w, row, area, start, hl)
 30432 #endif
 30433 
 30434 /* Add a glyph string for a stretch glyph to the list of strings
 30435    between HEAD and TAIL.  START is the index of the stretch glyph in
 30436    row area AREA of glyph row ROW.  END is the index of the last glyph
 30437    in that glyph row area.  X is the current output position assigned
 30438    to the new glyph string constructed.  HL overrides that face of the
 30439    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30440    is the right-most x-position of the drawing area.  */
 30441 
 30442 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
 30443    and below -- keep them on one line.  */
 30444 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30445      do                                                                     \
 30446        {                                                                    \
 30447          s = alloca (sizeof *s);                                            \
 30448          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);              \
 30449          START = fill_stretch_glyph_string (s, START, END);                 \
 30450          append_glyph_string (&HEAD, &TAIL, s);                             \
 30451          s->x = (X);                                                        \
 30452        }                                                                    \
 30453      while (false)
 30454 
 30455 
 30456 /* Add a glyph string for an image glyph to the list of strings
 30457    between HEAD and TAIL.  START is the index of the image glyph in
 30458    row area AREA of glyph row ROW.  END is the index of the last glyph
 30459    in that glyph row area.  X is the current output position assigned
 30460    to the new glyph string constructed.  HL overrides that face of the
 30461    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30462    is the right-most x-position of the drawing area.  */
 30463 
 30464 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30465      do                                                                 \
 30466        {                                                                \
 30467          s = alloca (sizeof *s);                                        \
 30468          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30469          fill_image_glyph_string (s);                                   \
 30470          append_glyph_string (&HEAD, &TAIL, s);                         \
 30471          ++START;                                                       \
 30472          s->x = (X);                                                    \
 30473        }                                                                \
 30474      while (false)
 30475 
 30476 #ifndef HAVE_XWIDGETS
 30477 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30478      eassume (false)
 30479 #else
 30480 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30481      do                                                                 \
 30482        {                                                                \
 30483          s = alloca (sizeof *s);                                        \
 30484          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30485          fill_xwidget_glyph_string (s);                                 \
 30486          append_glyph_string (&(HEAD), &(TAIL), s);                     \
 30487          ++(START);                                                     \
 30488          s->x = (X);                                                    \
 30489        }                                                                \
 30490      while (false)
 30491 #endif
 30492 
 30493 /* Add a glyph string for a sequence of character glyphs to the list
 30494    of strings between HEAD and TAIL.  START is the index of the first
 30495    glyph in row area AREA of glyph row ROW that is part of the new
 30496    glyph string.  END is the index of the last glyph in that glyph row
 30497    area.  X is the current output position assigned to the new glyph
 30498    string constructed.  HL overrides that face of the glyph; e.g. it
 30499    is DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the
 30500    right-most x-position of the drawing area.  */
 30501 
 30502 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30503      do                                                                    \
 30504        {                                                                   \
 30505          int face_id;                                                      \
 30506          unsigned *char2b;                                         \
 30507                                                                            \
 30508          face_id = (row)->glyphs[area][START].face_id;                     \
 30509                                                                            \
 30510          s = alloca (sizeof *s);                                           \
 30511          SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
 30512          INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
 30513          append_glyph_string (&HEAD, &TAIL, s);                            \
 30514          s->x = (X);                                                       \
 30515          START = fill_glyph_string (s, face_id, START, END, overlaps);     \
 30516        }                                                                   \
 30517      while (false)
 30518 
 30519 
 30520 /* Add a glyph string for a composite sequence to the list of strings
 30521    between HEAD and TAIL.  START is the index of the first glyph in
 30522    row area AREA of glyph row ROW that is part of the new glyph
 30523    string.  END is the index of the last glyph in that glyph row area.
 30524    X is the current output position assigned to the new glyph string
 30525    constructed.  HL overrides that face of the glyph; e.g. it is
 30526    DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the right-most
 30527    x-position of the drawing area.  */
 30528 
 30529 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30530   do {                                                                      \
 30531     int face_id = (row)->glyphs[area][START].face_id;                       \
 30532     struct face *base_face = FACE_FROM_ID (f, face_id);             \
 30533     ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id;                 \
 30534     struct composition *cmp = composition_table[cmp_id];                    \
 30535     unsigned *char2b;                                                       \
 30536     struct glyph_string *first_s = NULL;                                    \
 30537     int n;                                                                  \
 30538                                                                             \
 30539     SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                               \
 30540                                                                             \
 30541     /* Make glyph_strings for each glyph sequence that is drawable by       \
 30542        the same face, and append them to HEAD/TAIL.  */                     \
 30543     for (n = 0; n < cmp->glyph_len;)                                        \
 30544       {                                                                     \
 30545         s = alloca (sizeof *s);                                             \
 30546         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);             \
 30547         append_glyph_string (&(HEAD), &(TAIL), s);                          \
 30548         s->cmp = cmp;                                                       \
 30549         s->cmp_from = n;                                                    \
 30550         s->x = (X);                                                         \
 30551         if (n == 0)                                                         \
 30552           first_s = s;                                                      \
 30553         n = fill_composite_glyph_string (s, base_face, overlaps);           \
 30554       }                                                                     \
 30555                                                                             \
 30556     ++START;                                                                \
 30557     s = first_s;                                                            \
 30558   } while (false)
 30559 
 30560 
 30561 /* Add a glyph string for a glyph-string sequence to the list of strings
 30562    between HEAD and TAIL.  */
 30563 
 30564 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30565   do {                                                                    \
 30566     int face_id;                                                          \
 30567     unsigned *char2b;                                                     \
 30568     Lisp_Object gstring;                                                  \
 30569                                                                           \
 30570     face_id = (row)->glyphs[area][START].face_id;                         \
 30571     gstring = (composition_gstring_from_id                                \
 30572                ((row)->glyphs[area][START].u.cmp.id));                    \
 30573     s = alloca (sizeof *s);                                               \
 30574     SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));               \
 30575     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);               \
 30576     append_glyph_string (&(HEAD), &(TAIL), s);                            \
 30577     s->x = (X);                                                           \
 30578     START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
 30579   } while (false)
 30580 
 30581 
 30582 /* Add a glyph string for a sequence of glyphless character's glyphs
 30583    to the list of strings between HEAD and TAIL.  The meanings of
 30584    arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS.  */
 30585 
 30586 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30587   do                                                                        \
 30588     {                                                                       \
 30589       int face_id;                                                          \
 30590                                                                             \
 30591       face_id = (row)->glyphs[area][START].face_id;                         \
 30592                                                                             \
 30593       s = alloca (sizeof *s);                                               \
 30594       INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);                 \
 30595       append_glyph_string (&HEAD, &TAIL, s);                                \
 30596       s->x = (X);                                                           \
 30597       START = fill_glyphless_glyph_string (s, face_id, START, END,          \
 30598                                            overlaps);                       \
 30599     }                                                                       \
 30600   while (false)
 30601 
 30602 
 30603 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
 30604    of AREA of glyph row ROW on window W between indices START and END.
 30605    HL overrides the face for drawing glyph strings, e.g. it is
 30606    DRAW_CURSOR to draw a cursor.  X and LAST_X are start and end
 30607    x-positions of the drawing area.
 30608 
 30609    This is an ugly monster macro construct because we must use alloca
 30610    to allocate glyph strings (because draw_glyphs can be called
 30611    asynchronously).  */
 30612 
 30613 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30614   do                                                                    \
 30615     {                                                                   \
 30616       HEAD = TAIL = NULL;                                               \
 30617       while (START < END)                                               \
 30618         {                                                               \
 30619           struct glyph *first_glyph = (row)->glyphs[area] + START;      \
 30620           switch (first_glyph->type)                                    \
 30621             {                                                           \
 30622             case CHAR_GLYPH:                                            \
 30623               BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL,         \
 30624                                         HL, X, LAST_X);                 \
 30625               break;                                                    \
 30626                                                                         \
 30627             case COMPOSITE_GLYPH:                                       \
 30628               if (first_glyph->u.cmp.automatic)                         \
 30629                 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30630                                             HL, X, LAST_X);             \
 30631               else                                                      \
 30632                 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL,   \
 30633                                               HL, X, LAST_X);           \
 30634               break;                                                    \
 30635                                                                         \
 30636             case STRETCH_GLYPH:                                         \
 30637               BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30638                                           HL, X, LAST_X);               \
 30639               break;                                                    \
 30640                                                                         \
 30641             case IMAGE_GLYPH:                                           \
 30642               BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
 30643                                         HL, X, LAST_X);                 \
 30644               break;
 30645 
 30646 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30647             case XWIDGET_GLYPH:                                         \
 30648               BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30649                                           HL, X, LAST_X);               \
 30650               break;
 30651 
 30652 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30653             case GLYPHLESS_GLYPH:                                       \
 30654               BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30655                                             HL, X, LAST_X);             \
 30656               break;                                                    \
 30657                                                                         \
 30658             default:                                                    \
 30659               emacs_abort ();                                           \
 30660             }                                                           \
 30661                                                                         \
 30662           if (s)                                                        \
 30663             {                                                           \
 30664               set_glyph_string_background_width (s, START, LAST_X);     \
 30665               (X) += s->width;                                          \
 30666             }                                                           \
 30667         }                                                               \
 30668     } while (false)
 30669 
 30670 
 30671 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)      \
 30672     BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)        \
 30673     BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)       \
 30674     BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
 30675 
 30676 
 30677 /* Draw glyphs between START and END in AREA of ROW on window W,
 30678    starting at x-position X.  X is relative to AREA in W.  HL is a
 30679    face-override with the following meaning:
 30680 
 30681    DRAW_NORMAL_TEXT     draw normally
 30682    DRAW_CURSOR          draw in cursor face
 30683    DRAW_MOUSE_FACE      draw in mouse face.
 30684    DRAW_INVERSE_VIDEO   draw in mode line face
 30685    DRAW_IMAGE_SUNKEN    draw an image with a sunken relief around it
 30686    DRAW_IMAGE_RAISED    draw an image with a raised relief around it
 30687 
 30688    If OVERLAPS is non-zero, draw only the foreground of characters and
 30689    clip to the physical height of ROW.  Non-zero value also defines
 30690    the overlapping part to be drawn:
 30691 
 30692    OVERLAPS_PRED                overlap with preceding rows
 30693    OVERLAPS_SUCC                overlap with succeeding rows
 30694    OVERLAPS_BOTH                overlap with both preceding/succeeding rows
 30695    OVERLAPS_ERASED_CURSOR       overlap with erased cursor area
 30696 
 30697    Value is the x-position reached, relative to AREA of W.  */
 30698 
 30699 static int
 30700 draw_glyphs (struct window *w, int x, struct glyph_row *row,
 30701              enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
 30702              enum draw_glyphs_face hl, int overlaps)
 30703 {
 30704   struct glyph_string *head, *tail;
 30705   struct glyph_string *s;
 30706   struct glyph_string *clip_head = NULL, *clip_tail = NULL;
 30707   int i, j, x_reached, last_x, area_left = 0;
 30708   struct frame *f = XFRAME (WINDOW_FRAME (w));
 30709 
 30710   ALLOCATE_HDC (hdc, f);
 30711 
 30712   /* Let's rather be paranoid than getting a SEGV.  */
 30713   end = min (end, row->used[area]);
 30714   start = clip_to_bounds (0, start, end);
 30715 
 30716   /* Translate X to frame coordinates.  Set last_x to the right
 30717      end of the drawing area.  */
 30718   if (row->full_width_p)
 30719     {
 30720       /* X is relative to the left edge of W, without scroll bars
 30721          or fringes.  */
 30722       area_left = WINDOW_LEFT_EDGE_X (w);
 30723       last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
 30724                 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 30725     }
 30726   else
 30727     {
 30728       area_left = window_box_left (w, area);
 30729       last_x = area_left + window_box_width (w, area);
 30730     }
 30731   x += area_left;
 30732 
 30733   /* Build a doubly-linked list of glyph_string structures between
 30734      head and tail from what we have to draw.  Note that the macro
 30735      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
 30736      the reason we use a separate variable `i'.  */
 30737   i = start;
 30738   USE_SAFE_ALLOCA;
 30739   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
 30740   if (tail)
 30741     {
 30742       s = glyph_string_containing_background_width (tail);
 30743       x_reached = s->x + s->background_width;
 30744     }
 30745   else
 30746     x_reached = x;
 30747 
 30748   /* If there are any glyphs with lbearing < 0 or rbearing > width in
 30749      the row, redraw some glyphs in front or following the glyph
 30750      strings built above.  */
 30751   if (head && !overlaps && row->contains_overlapping_glyphs_p)
 30752     {
 30753       struct glyph_string *h, *t;
 30754       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 30755       int mouse_beg_col UNINIT, mouse_end_col UNINIT;
 30756       bool check_mouse_face = false;
 30757       int dummy_x = 0;
 30758 
 30759       /* If mouse highlighting is on, we may need to draw adjacent
 30760          glyphs using mouse-face highlighting.  */
 30761       if (area == TEXT_AREA && row->mouse_face_p
 30762           && hlinfo->mouse_face_beg_row >= 0
 30763           && hlinfo->mouse_face_end_row >= 0)
 30764         {
 30765           ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
 30766 
 30767           if (row_vpos >= hlinfo->mouse_face_beg_row
 30768               && row_vpos <= hlinfo->mouse_face_end_row)
 30769             {
 30770               check_mouse_face = true;
 30771               mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
 30772                 ? hlinfo->mouse_face_beg_col : 0;
 30773               mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
 30774                 ? hlinfo->mouse_face_end_col
 30775                 : row->used[TEXT_AREA];
 30776             }
 30777         }
 30778 
 30779       /* Compute overhangs for all glyph strings.  */
 30780       if (FRAME_RIF (f)->compute_glyph_string_overhangs)
 30781         for (s = head; s; s = s->next)
 30782           FRAME_RIF (f)->compute_glyph_string_overhangs (s);
 30783 
 30784       /* Prepend glyph strings for glyphs in front of the first glyph
 30785          string that are overwritten because of the first glyph
 30786          string's left overhang.  The background of all strings
 30787          prepended must be drawn because the first glyph string
 30788          draws over it.  */
 30789       i = left_overwritten (head);
 30790       if (i >= 0)
 30791         {
 30792           enum draw_glyphs_face overlap_hl;
 30793 
 30794           /* If this row contains mouse highlighting, attempt to draw
 30795              the overlapped glyphs with the correct highlight.  This
 30796              code fails if the overlap encompasses more than one glyph
 30797              and mouse-highlight spans only some of these glyphs.
 30798              However, making it work perfectly involves a lot more
 30799              code, and I don't know if the pathological case occurs in
 30800              practice, so we'll stick to this for now.  --- cyd  */
 30801           if (check_mouse_face
 30802               && mouse_beg_col < start && mouse_end_col > i)
 30803             overlap_hl = DRAW_MOUSE_FACE;
 30804           else
 30805             overlap_hl = DRAW_NORMAL_TEXT;
 30806 
 30807           if (hl != overlap_hl)
 30808             clip_head = head;
 30809           j = i;
 30810           BUILD_GLYPH_STRINGS (j, start, h, t,
 30811                                overlap_hl, dummy_x, last_x);
 30812           start = i;
 30813           compute_overhangs_and_x (t, head->x, true);
 30814           prepend_glyph_string_lists (&head, &tail, h, t);
 30815           if (clip_head == NULL)
 30816             clip_head = head;
 30817         }
 30818 
 30819       /* Prepend glyph strings for glyphs in front of the first glyph
 30820          string that overwrite that glyph string because of their
 30821          right overhang.  For these strings, only the foreground must
 30822          be drawn, because it draws over the glyph string at `head'.
 30823          The background must not be drawn because this would overwrite
 30824          right overhangs of preceding glyphs for which no glyph
 30825          strings exist.  */
 30826       i = left_overwriting (head);
 30827       if (i >= 0)
 30828         {
 30829           enum draw_glyphs_face overlap_hl;
 30830 
 30831           if (check_mouse_face
 30832               && mouse_beg_col < start && mouse_end_col > i)
 30833             overlap_hl = DRAW_MOUSE_FACE;
 30834           else
 30835             overlap_hl = DRAW_NORMAL_TEXT;
 30836 
 30837           if (hl == overlap_hl || clip_head == NULL)
 30838             clip_head = head;
 30839           BUILD_GLYPH_STRINGS (i, start, h, t,
 30840                                overlap_hl, dummy_x, last_x);
 30841           for (s = h; s; s = s->next)
 30842             s->background_filled_p = true;
 30843           compute_overhangs_and_x (t, head->x, true);
 30844           prepend_glyph_string_lists (&head, &tail, h, t);
 30845         }
 30846 
 30847       /* Append glyphs strings for glyphs following the last glyph
 30848          string tail that are overwritten by tail.  The background of
 30849          these strings has to be drawn because tail's foreground draws
 30850          over it.  */
 30851       i = right_overwritten (tail);
 30852       if (i >= 0)
 30853         {
 30854           enum draw_glyphs_face overlap_hl;
 30855 
 30856           if (check_mouse_face
 30857               && mouse_beg_col < i && mouse_end_col > end)
 30858             overlap_hl = DRAW_MOUSE_FACE;
 30859           else
 30860             overlap_hl = DRAW_NORMAL_TEXT;
 30861 
 30862           if (hl != overlap_hl)
 30863             clip_tail = tail;
 30864           BUILD_GLYPH_STRINGS (end, i, h, t,
 30865                                overlap_hl, x, last_x);
 30866           /* Because BUILD_GLYPH_STRINGS updates the first argument,
 30867              we don't have `end = i;' here.  */
 30868           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30869           append_glyph_string_lists (&head, &tail, h, t);
 30870           if (clip_tail == NULL)
 30871             clip_tail = tail;
 30872         }
 30873 
 30874       /* Append glyph strings for glyphs following the last glyph
 30875          string tail that overwrite tail.  The foreground of such
 30876          glyphs has to be drawn because it writes into the background
 30877          of tail.  The background must not be drawn because it could
 30878          paint over the foreground of following glyphs.  */
 30879       i = right_overwriting (tail);
 30880       if (i >= 0)
 30881         {
 30882           enum draw_glyphs_face overlap_hl;
 30883           if (check_mouse_face
 30884               && mouse_beg_col < i && mouse_end_col > end)
 30885             overlap_hl = DRAW_MOUSE_FACE;
 30886           else
 30887             overlap_hl = DRAW_NORMAL_TEXT;
 30888 
 30889           if (hl == overlap_hl || clip_tail == NULL)
 30890             clip_tail = tail;
 30891           i++;                  /* We must include the Ith glyph.  */
 30892           BUILD_GLYPH_STRINGS (end, i, h, t,
 30893                                overlap_hl, x, last_x);
 30894           for (s = h; s; s = s->next)
 30895             s->background_filled_p = true;
 30896           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30897           append_glyph_string_lists (&head, &tail, h, t);
 30898         }
 30899       tail = glyph_string_containing_background_width (tail);
 30900       if (clip_tail)
 30901         clip_tail = glyph_string_containing_background_width (clip_tail);
 30902       if (clip_head || clip_tail)
 30903         for (s = head; s; s = s->next)
 30904           {
 30905             s->clip_head = clip_head;
 30906             s->clip_tail = clip_tail;
 30907           }
 30908     }
 30909 
 30910   /* Draw all strings.  */
 30911   for (s = head; s; s = s->next)
 30912     FRAME_RIF (f)->draw_glyph_string (s);
 30913 
 30914   /* When focus a sole frame and move horizontally, this clears on_p
 30915      causing a failure to erase prev cursor position. */
 30916   if (area == TEXT_AREA
 30917       && !row->full_width_p
 30918       /* When drawing overlapping rows, only the glyph strings'
 30919          foreground is drawn, which doesn't erase a cursor
 30920          completely. */
 30921       && !overlaps)
 30922     {
 30923       int x0 = clip_head ? clip_head->x : (head ? head->x : x);
 30924       int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
 30925                 : (tail ? tail->x + tail->background_width : x));
 30926       x0 -= area_left;
 30927       x1 -= area_left;
 30928 
 30929       notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
 30930                                  row->y, MATRIX_ROW_BOTTOM_Y (row));
 30931     }
 30932 
 30933   /* Value is the x-position up to which drawn, relative to AREA of W.
 30934      This doesn't include parts drawn because of overhangs.  */
 30935   if (row->full_width_p)
 30936     x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
 30937   else
 30938     x_reached -= area_left;
 30939 
 30940   RELEASE_HDC (hdc, f);
 30941 
 30942   SAFE_FREE ();
 30943   return x_reached;
 30944 }
 30945 
 30946 /* Find the first glyph in the run of underlined glyphs preceding the
 30947    beginning of glyph string S, and return its font (which could be
 30948    NULL).  This is needed because that font determines the underline
 30949    position and thickness for the entire run of the underlined glyphs.
 30950    This function is called from the draw_glyph_string method of GUI
 30951    frame's redisplay interface (RIF) when it needs to draw in an
 30952    underlined face.  */
 30953 struct font *
 30954 font_for_underline_metrics (struct glyph_string *s)
 30955 {
 30956   struct glyph *g0 = s->row->glyphs[s->area], *g;
 30957 
 30958   for (g = s->first_glyph - 1; g >= g0; g--)
 30959     {
 30960       struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
 30961       if (!(prev_face && prev_face->underline != FACE_NO_UNDERLINE))
 30962         break;
 30963     }
 30964 
 30965   /* If preceding glyphs are not underlined, use the font of S.  */
 30966   if (g == s->first_glyph - 1)
 30967     return s->font;
 30968   else
 30969     {
 30970       /* Otherwise use the font of the last glyph we saw in the above
 30971          loop whose face had the underline_p flag set.  */
 30972       return FACE_FROM_ID (s->f, g[1].face_id)->font;
 30973     }
 30974 }
 30975 
 30976 /* Expand row matrix if too narrow.  Don't expand if area
 30977    is not present.  */
 30978 
 30979 #define IT_EXPAND_MATRIX_WIDTH(it, area)                \
 30980   {                                                     \
 30981     if (!it->f->fonts_changed                           \
 30982         && (it->glyph_row->glyphs[area]                 \
 30983             < it->glyph_row->glyphs[area + 1]))         \
 30984       {                                                 \
 30985         it->w->ncols_scale_factor++;                    \
 30986         it->f->fonts_changed = true;                    \
 30987       }                                                 \
 30988   }
 30989 
 30990 /* Store one glyph for IT->char_to_display in IT->glyph_row.
 30991    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30992 
 30993 static void
 30994 append_glyph (struct it *it)
 30995 {
 30996   struct glyph *glyph;
 30997   enum glyph_row_area area = it->area;
 30998 
 30999   eassert (it->glyph_row);
 31000   eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
 31001 
 31002   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31003   if (glyph < it->glyph_row->glyphs[area + 1])
 31004     {
 31005       /* If the glyph row is reversed, we need to prepend the glyph
 31006          rather than append it.  */
 31007       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31008         {
 31009           struct glyph *g;
 31010 
 31011           /* Make room for the additional glyph.  */
 31012           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31013             g[1] = *g;
 31014           glyph = it->glyph_row->glyphs[area];
 31015         }
 31016       glyph->charpos = CHARPOS (it->position);
 31017       glyph->object = it->object;
 31018       if (it->pixel_width > 0)
 31019         {
 31020           eassert (it->pixel_width <= SHRT_MAX);
 31021           glyph->pixel_width = it->pixel_width;
 31022           glyph->padding_p = false;
 31023         }
 31024       else
 31025         {
 31026           /* Assure at least 1-pixel width.  Otherwise, cursor can't
 31027              be displayed correctly.  */
 31028           glyph->pixel_width = 1;
 31029           glyph->padding_p = true;
 31030         }
 31031       glyph->ascent = it->ascent;
 31032       glyph->descent = it->descent;
 31033       glyph->voffset = it->voffset;
 31034       glyph->type = CHAR_GLYPH;
 31035       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31036       glyph->multibyte_p = it->multibyte_p;
 31037       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31038         {
 31039           /* In R2L rows, the left and the right box edges need to be
 31040              drawn in reverse direction.  */
 31041           glyph->right_box_line_p = it->start_of_box_run_p;
 31042           glyph->left_box_line_p = it->end_of_box_run_p;
 31043         }
 31044       else
 31045         {
 31046           glyph->left_box_line_p = it->start_of_box_run_p;
 31047           glyph->right_box_line_p = it->end_of_box_run_p;
 31048         }
 31049       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31050                                       || it->phys_descent > it->descent);
 31051       glyph->glyph_not_available_p = it->glyph_not_available_p;
 31052       glyph->face_id = it->face_id;
 31053       glyph->u.ch = it->char_to_display;
 31054       glyph->slice.img = null_glyph_slice;
 31055       glyph->font_type = FONT_TYPE_UNKNOWN;
 31056       if (it->bidi_p)
 31057         {
 31058           glyph->resolved_level = it->bidi_it.resolved_level;
 31059           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31060           glyph->bidi_type = it->bidi_it.type;
 31061         }
 31062       else
 31063         {
 31064           glyph->resolved_level = 0;
 31065           glyph->bidi_type = UNKNOWN_BT;
 31066         }
 31067       ++it->glyph_row->used[area];
 31068     }
 31069   else
 31070     IT_EXPAND_MATRIX_WIDTH (it, area);
 31071 }
 31072 
 31073 /* Store one glyph for the composition IT->cmp_it.id in IT->glyph_row.
 31074    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 31075 
 31076 static void
 31077 append_composite_glyph (struct it *it)
 31078 {
 31079   struct glyph *glyph;
 31080   enum glyph_row_area area = it->area;
 31081 
 31082   eassert (it->glyph_row);
 31083 
 31084   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31085   if (glyph < it->glyph_row->glyphs[area + 1])
 31086     {
 31087       /* If the glyph row is reversed, we need to prepend the glyph
 31088          rather than append it.  */
 31089       if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
 31090         {
 31091           struct glyph *g;
 31092 
 31093           /* Make room for the new glyph.  */
 31094           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31095             g[1] = *g;
 31096           glyph = it->glyph_row->glyphs[it->area];
 31097         }
 31098       glyph->charpos = it->cmp_it.charpos;
 31099       glyph->object = it->object;
 31100       eassert (it->pixel_width <= SHRT_MAX);
 31101       glyph->pixel_width = it->pixel_width;
 31102       glyph->ascent = it->ascent;
 31103       glyph->descent = it->descent;
 31104       glyph->voffset = it->voffset;
 31105       glyph->type = COMPOSITE_GLYPH;
 31106       if (it->cmp_it.ch < 0)
 31107         {
 31108           glyph->u.cmp.automatic = false;
 31109           glyph->u.cmp.id = it->cmp_it.id;
 31110           glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
 31111         }
 31112       else
 31113         {
 31114           glyph->u.cmp.automatic = true;
 31115           glyph->u.cmp.id = it->cmp_it.id;
 31116           glyph->slice.cmp.from = it->cmp_it.from;
 31117           glyph->slice.cmp.to = it->cmp_it.to - 1;
 31118         }
 31119       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31120       glyph->multibyte_p = it->multibyte_p;
 31121       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31122         {
 31123           /* In R2L rows, the left and the right box edges need to be
 31124              drawn in reverse direction.  */
 31125           glyph->right_box_line_p = it->start_of_box_run_p;
 31126           glyph->left_box_line_p = it->end_of_box_run_p;
 31127         }
 31128       else
 31129         {
 31130           glyph->left_box_line_p = it->start_of_box_run_p;
 31131           glyph->right_box_line_p = it->end_of_box_run_p;
 31132         }
 31133       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31134                                       || it->phys_descent > it->descent);
 31135       glyph->padding_p = false;
 31136       glyph->glyph_not_available_p = it->glyph_not_available_p;
 31137       glyph->face_id = it->face_id;
 31138       glyph->font_type = FONT_TYPE_UNKNOWN;
 31139       if (it->bidi_p)
 31140         {
 31141           glyph->resolved_level = it->bidi_it.resolved_level;
 31142           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31143           glyph->bidi_type = it->bidi_it.type;
 31144         }
 31145       ++it->glyph_row->used[area];
 31146     }
 31147   else
 31148     IT_EXPAND_MATRIX_WIDTH (it, area);
 31149 }
 31150 
 31151 
 31152 /* Change IT->ascent and IT->height according to the setting of
 31153    IT->voffset.  */
 31154 
 31155 static void
 31156 take_vertical_position_into_account (struct it *it)
 31157 {
 31158   if (it->voffset)
 31159     {
 31160       if (it->voffset < 0)
 31161         /* Increase the ascent so that we can display the text higher
 31162            in the line.  */
 31163         it->ascent -= it->voffset;
 31164       else
 31165         /* Increase the descent so that we can display the text lower
 31166            in the line.  */
 31167         it->descent += it->voffset;
 31168     }
 31169 }
 31170 
 31171 
 31172 /* Produce glyphs/get display metrics for the image IT is loaded with.
 31173    See the description of struct display_iterator in dispextern.h for
 31174    an overview of struct display_iterator.  */
 31175 
 31176 static void
 31177 produce_image_glyph (struct it *it)
 31178 {
 31179   struct image *img;
 31180   struct face *face;
 31181   int glyph_ascent, crop;
 31182   struct glyph_slice slice;
 31183 
 31184   eassert (it->what == IT_IMAGE);
 31185 
 31186   face = FACE_FROM_ID (it->f, it->face_id);
 31187   /* Make sure X resources of the face is loaded.  */
 31188   prepare_face_for_display (it->f, face);
 31189 
 31190   if (it->image_id < 0)
 31191     {
 31192       /* Fringe bitmap.  */
 31193       it->ascent = it->phys_ascent = 0;
 31194       it->descent = it->phys_descent = 0;
 31195       it->pixel_width = 0;
 31196       it->nglyphs = 0;
 31197       return;
 31198     }
 31199 
 31200   img = IMAGE_FROM_ID (it->f, it->image_id);
 31201   /* Make sure X resources of the image is loaded.  */
 31202   prepare_image_for_display (it->f, img);
 31203 
 31204   slice.x = slice.y = 0;
 31205   slice.width = img->width;
 31206   slice.height = img->height;
 31207 
 31208   if (FIXNUMP (it->slice.x))
 31209     slice.x = XFIXNUM (it->slice.x);
 31210   else if (FLOATP (it->slice.x))
 31211     slice.x = XFLOAT_DATA (it->slice.x) * img->width;
 31212 
 31213   if (FIXNUMP (it->slice.y))
 31214     slice.y = XFIXNUM (it->slice.y);
 31215   else if (FLOATP (it->slice.y))
 31216     slice.y = XFLOAT_DATA (it->slice.y) * img->height;
 31217 
 31218   if (FIXNUMP (it->slice.width))
 31219     slice.width = XFIXNUM (it->slice.width);
 31220   else if (FLOATP (it->slice.width))
 31221     slice.width = XFLOAT_DATA (it->slice.width) * img->width;
 31222 
 31223   if (FIXNUMP (it->slice.height))
 31224     slice.height = XFIXNUM (it->slice.height);
 31225   else if (FLOATP (it->slice.height))
 31226     slice.height = XFLOAT_DATA (it->slice.height) * img->height;
 31227 
 31228   if (slice.x >= img->width)
 31229     slice.x = img->width;
 31230   if (slice.y >= img->height)
 31231     slice.y = img->height;
 31232   if (slice.x + slice.width >= img->width)
 31233     slice.width = img->width - slice.x;
 31234   if (slice.y + slice.height > img->height)
 31235     slice.height = img->height - slice.y;
 31236 
 31237   if (slice.width == 0 || slice.height == 0)
 31238     return;
 31239 
 31240   it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
 31241 
 31242   it->descent = slice.height - glyph_ascent;
 31243   if (slice.y == 0)
 31244     it->descent += img->vmargin;
 31245   if (slice.y + slice.height == img->height)
 31246     it->descent += img->vmargin;
 31247   it->phys_descent = it->descent;
 31248 
 31249   it->pixel_width = slice.width;
 31250   if (slice.x == 0)
 31251     it->pixel_width += img->hmargin;
 31252   if (slice.x + slice.width == img->width)
 31253     it->pixel_width += img->hmargin;
 31254 
 31255   /* It's quite possible for images to have an ascent greater than
 31256      their height, so don't get confused in that case.  */
 31257   if (it->descent < 0)
 31258     it->descent = 0;
 31259 
 31260   it->nglyphs = 1;
 31261 
 31262   if (face->box != FACE_NO_BOX)
 31263     {
 31264       /* If you change the logic here, please change it in
 31265          get_cursor_offset_for_mouse_face as well. */
 31266       if (face->box_horizontal_line_width > 0)
 31267         {
 31268           if (slice.y == 0)
 31269             it->ascent += face->box_horizontal_line_width;
 31270           if (slice.y + slice.height == img->height)
 31271             it->descent += face->box_horizontal_line_width;
 31272         }
 31273 
 31274       if (face->box_vertical_line_width > 0)
 31275         {
 31276           if (it->start_of_box_run_p && slice.x == 0)
 31277             it->pixel_width += face->box_vertical_line_width;
 31278           if (it->end_of_box_run_p && slice.x + slice.width == img->width)
 31279             it->pixel_width += face->box_vertical_line_width;
 31280         }
 31281     }
 31282 
 31283   take_vertical_position_into_account (it);
 31284 
 31285   /* Automatically crop wide image glyphs at right edge so we can
 31286      draw the cursor on same display row.  */
 31287   if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
 31288       && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31289     {
 31290       it->pixel_width -= crop;
 31291       slice.width -= crop;
 31292     }
 31293 
 31294   if (it->glyph_row)
 31295     {
 31296       struct glyph *glyph;
 31297       enum glyph_row_area area = it->area;
 31298 
 31299       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31300       if (it->glyph_row->reversed_p)
 31301         {
 31302           struct glyph *g;
 31303 
 31304           /* Make room for the new glyph.  */
 31305           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31306             g[1] = *g;
 31307           glyph = it->glyph_row->glyphs[it->area];
 31308         }
 31309       if (glyph < it->glyph_row->glyphs[area + 1])
 31310         {
 31311           glyph->charpos = CHARPOS (it->position);
 31312           glyph->object = it->object;
 31313           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31314           glyph->ascent = glyph_ascent;
 31315           glyph->descent = it->descent;
 31316           glyph->voffset = it->voffset;
 31317           glyph->type = IMAGE_GLYPH;
 31318           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31319           glyph->multibyte_p = it->multibyte_p;
 31320           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31321             {
 31322               /* In R2L rows, the left and the right box edges need to be
 31323                  drawn in reverse direction.  */
 31324               glyph->right_box_line_p = it->start_of_box_run_p;
 31325               glyph->left_box_line_p = it->end_of_box_run_p;
 31326             }
 31327           else
 31328             {
 31329               glyph->left_box_line_p = it->start_of_box_run_p;
 31330               glyph->right_box_line_p = it->end_of_box_run_p;
 31331             }
 31332           glyph->overlaps_vertically_p = false;
 31333           glyph->padding_p = false;
 31334           glyph->glyph_not_available_p = false;
 31335           glyph->face_id = it->face_id;
 31336           glyph->u.img_id = img->id;
 31337           glyph->slice.img = slice;
 31338           glyph->font_type = FONT_TYPE_UNKNOWN;
 31339           if (it->bidi_p)
 31340             {
 31341               glyph->resolved_level = it->bidi_it.resolved_level;
 31342               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31343               glyph->bidi_type = it->bidi_it.type;
 31344             }
 31345           ++it->glyph_row->used[area];
 31346         }
 31347       else
 31348         IT_EXPAND_MATRIX_WIDTH (it, area);
 31349     }
 31350 }
 31351 
 31352 static void
 31353 produce_xwidget_glyph (struct it *it)
 31354 {
 31355 #ifdef HAVE_XWIDGETS
 31356   struct xwidget *xw;
 31357   int glyph_ascent, crop;
 31358   eassert (it->what == IT_XWIDGET);
 31359 
 31360   struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31361   /* Make sure X resources of the face is loaded.  */
 31362   prepare_face_for_display (it->f, face);
 31363 
 31364   xw = it->xwidget;
 31365   it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
 31366   it->descent = xw->height/2;
 31367   it->phys_descent = it->descent;
 31368   it->pixel_width = xw->width;
 31369   /* It's quite possible for images to have an ascent greater than
 31370      their height, so don't get confused in that case.  */
 31371   if (it->descent < 0)
 31372     it->descent = 0;
 31373 
 31374   it->nglyphs = 1;
 31375 
 31376   if (face->box != FACE_NO_BOX)
 31377     {
 31378       if (face->box_horizontal_line_width > 0)
 31379         {
 31380           it->ascent += face->box_horizontal_line_width;
 31381           it->descent += face->box_horizontal_line_width;
 31382         }
 31383 
 31384       if (face->box_vertical_line_width > 0)
 31385         {
 31386           if (it->start_of_box_run_p)
 31387             it->pixel_width += face->box_vertical_line_width;
 31388           it->pixel_width += face->box_vertical_line_width;
 31389         }
 31390     }
 31391 
 31392   take_vertical_position_into_account (it);
 31393 
 31394   /* Automatically crop wide image glyphs at right edge so we can
 31395      draw the cursor on same display row.  */
 31396   crop = it->pixel_width - (it->last_visible_x - it->current_x);
 31397   if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31398     it->pixel_width -= crop;
 31399 
 31400   if (it->glyph_row)
 31401     {
 31402       enum glyph_row_area area = it->area;
 31403       struct glyph *glyph
 31404         = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31405 
 31406       if (it->glyph_row->reversed_p)
 31407         {
 31408           struct glyph *g;
 31409 
 31410           /* Make room for the new glyph.  */
 31411           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31412             g[1] = *g;
 31413           glyph = it->glyph_row->glyphs[it->area];
 31414         }
 31415       if (glyph < it->glyph_row->glyphs[area + 1])
 31416         {
 31417           glyph->charpos = CHARPOS (it->position);
 31418           glyph->object = it->object;
 31419           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31420           glyph->ascent = glyph_ascent;
 31421           glyph->descent = it->descent;
 31422           glyph->voffset = it->voffset;
 31423           glyph->type = XWIDGET_GLYPH;
 31424           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31425           glyph->multibyte_p = it->multibyte_p;
 31426           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31427             {
 31428               /* In R2L rows, the left and the right box edges need to be
 31429                  drawn in reverse direction.  */
 31430               glyph->right_box_line_p = it->start_of_box_run_p;
 31431               glyph->left_box_line_p = it->end_of_box_run_p;
 31432             }
 31433           else
 31434             {
 31435               glyph->left_box_line_p = it->start_of_box_run_p;
 31436               glyph->right_box_line_p = it->end_of_box_run_p;
 31437             }
 31438           glyph->overlaps_vertically_p = 0;
 31439           glyph->padding_p = 0;
 31440           glyph->glyph_not_available_p = 0;
 31441           glyph->face_id = it->face_id;
 31442           glyph->u.xwidget = it->xwidget->xwidget_id;
 31443           glyph->font_type = FONT_TYPE_UNKNOWN;
 31444           if (it->bidi_p)
 31445             {
 31446               glyph->resolved_level = it->bidi_it.resolved_level;
 31447               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31448               glyph->bidi_type = it->bidi_it.type;
 31449             }
 31450           ++it->glyph_row->used[area];
 31451         }
 31452       else
 31453         IT_EXPAND_MATRIX_WIDTH (it, area);
 31454     }
 31455 #endif
 31456 }
 31457 
 31458 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
 31459    of the glyph, WIDTH and HEIGHT are the width and height of the
 31460    stretch.  ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT).  */
 31461 
 31462 static void
 31463 append_stretch_glyph (struct it *it, Lisp_Object object,
 31464                       int width, int height, int ascent)
 31465 {
 31466   struct glyph *glyph;
 31467   enum glyph_row_area area = it->area;
 31468 
 31469   eassert (ascent >= 0 && ascent <= height);
 31470 
 31471   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31472   if (glyph < it->glyph_row->glyphs[area + 1])
 31473     {
 31474       /* If the glyph row is reversed, we need to prepend the glyph
 31475          rather than append it.  */
 31476       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31477         {
 31478           struct glyph *g;
 31479 
 31480           /* Make room for the additional glyph.  */
 31481           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31482             g[1] = *g;
 31483           glyph = it->glyph_row->glyphs[area];
 31484 
 31485           /* Decrease the width of the first glyph of the row that
 31486              begins before first_visible_x (e.g., due to hscroll).
 31487              This is so the overall width of the row becomes smaller
 31488              by the scroll amount, and the stretch glyph appended by
 31489              extend_face_to_end_of_line will be wider, to shift the
 31490              row glyphs to the right.  (In L2R rows, the corresponding
 31491              left-shift effect is accomplished by setting row->x to a
 31492              negative value, which won't work with R2L rows.)
 31493 
 31494              This must leave us with a positive value of WIDTH, since
 31495              otherwise the call to move_it_in_display_line_to at the
 31496              beginning of display_line would have got past the entire
 31497              first glyph, and then it->current_x would have been
 31498              greater or equal to it->first_visible_x.  */
 31499           if (it->current_x < it->first_visible_x)
 31500             width -= it->first_visible_x - it->current_x;
 31501           eassert (width > 0);
 31502         }
 31503       glyph->charpos = CHARPOS (it->position);
 31504       glyph->object = object;
 31505       /* FIXME: It would be better to use TYPE_MAX here, but
 31506          __typeof__ is not portable enough...  */
 31507       glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
 31508       glyph->ascent = ascent;
 31509       glyph->descent = height - ascent;
 31510       glyph->voffset = it->voffset;
 31511       glyph->type = STRETCH_GLYPH;
 31512       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31513       glyph->multibyte_p = it->multibyte_p;
 31514       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31515         {
 31516           /* In R2L rows, the left and the right box edges need to be
 31517              drawn in reverse direction.  */
 31518           glyph->right_box_line_p = it->start_of_box_run_p;
 31519           glyph->left_box_line_p = it->end_of_box_run_p;
 31520         }
 31521       else
 31522         {
 31523           glyph->left_box_line_p = it->start_of_box_run_p;
 31524           glyph->right_box_line_p = it->end_of_box_run_p;
 31525         }
 31526       glyph->overlaps_vertically_p = false;
 31527       glyph->padding_p = false;
 31528       glyph->glyph_not_available_p = false;
 31529       glyph->face_id = it->face_id;
 31530       glyph->u.stretch.ascent = ascent;
 31531       glyph->u.stretch.height = height;
 31532       glyph->slice.img = null_glyph_slice;
 31533       glyph->font_type = FONT_TYPE_UNKNOWN;
 31534       if (it->bidi_p)
 31535         {
 31536           glyph->resolved_level = it->bidi_it.resolved_level;
 31537           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31538           glyph->bidi_type = it->bidi_it.type;
 31539         }
 31540       else
 31541         {
 31542           glyph->resolved_level = 0;
 31543           glyph->bidi_type = UNKNOWN_BT;
 31544         }
 31545       ++it->glyph_row->used[area];
 31546     }
 31547   else
 31548     IT_EXPAND_MATRIX_WIDTH (it, area);
 31549 }
 31550 
 31551 #endif  /* HAVE_WINDOW_SYSTEM */
 31552 
 31553 /* Produce a stretch glyph for iterator IT.  IT->object is the value
 31554    of the display property.  The value must be a list of the form
 31555    `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
 31556    being recognized:
 31557 
 31558    1. `:width WIDTH' specifies that the space should be WIDTH *
 31559    canonical char width wide.  WIDTH may be an integer or floating
 31560    point number.
 31561 
 31562    2. `:relative-width FACTOR' specifies that the width of the stretch
 31563    should be computed from the width of the first character having the
 31564    `display' property, and should be FACTOR times that width.
 31565 
 31566    3. `:align-to HPOS' specifies that the space should be wide enough
 31567    to reach HPOS, a value in canonical character units.
 31568 
 31569    Exactly one of the above pairs must be present.
 31570 
 31571    4. `:height HEIGHT' specifies that the height of the stretch produced
 31572    should be HEIGHT, measured in canonical character units.
 31573 
 31574    5. `:relative-height FACTOR' specifies that the height of the
 31575    stretch should be FACTOR times the height of the characters having
 31576    the display property.
 31577 
 31578    Either none or exactly one of 4 or 5 must be present.
 31579 
 31580    6. `:ascent ASCENT'  specifies that ASCENT percent of the height
 31581    of the stretch should be used for the ascent of the stretch.
 31582    ASCENT must be in the range 0 <= ASCENT <= 100.  */
 31583 
 31584 void
 31585 produce_stretch_glyph (struct it *it)
 31586 {
 31587   /* (space :width WIDTH :height HEIGHT ...)  */
 31588   Lisp_Object prop, plist;
 31589   int width = 0, height = 0, align_to = -1;
 31590   bool zero_width_ok_p = false;
 31591   double tem;
 31592   struct font *font = NULL;
 31593 
 31594 #ifdef HAVE_WINDOW_SYSTEM
 31595   int ascent = 0;
 31596   bool zero_height_ok_p = false;
 31597   struct face *face = NULL;     /* shut up GCC's -Wmaybe-uninitialized */
 31598 
 31599   if (FRAME_WINDOW_P (it->f))
 31600     {
 31601       face = FACE_FROM_ID (it->f, it->face_id);
 31602       font = face->font ? face->font : FRAME_FONT (it->f);
 31603       prepare_face_for_display (it->f, face);
 31604     }
 31605 #endif
 31606 
 31607   /* List should start with `space'.  */
 31608   eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
 31609   plist = XCDR (it->object);
 31610 
 31611   /* Compute the width of the stretch.  */
 31612   if ((prop = plist_get (plist, QCwidth), !NILP (prop))
 31613       && calc_pixel_width_or_height (&tem, it, prop, font, true, NULL))
 31614     {
 31615       /* Absolute width `:width WIDTH' specified and valid.  */
 31616       zero_width_ok_p = true;
 31617       width = (int)tem;
 31618     }
 31619   else if (prop = plist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
 31620     {
 31621       /* Relative width `:relative-width FACTOR' specified and valid.
 31622          Compute the width of the characters having this `display'
 31623          property.  */
 31624       struct it it2;
 31625       Lisp_Object object =
 31626         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31627       unsigned char *p = (STRINGP (object)
 31628                           ? SDATA (object) + IT_STRING_BYTEPOS (*it)
 31629                           : BYTE_POS_ADDR (IT_BYTEPOS (*it)));
 31630       bool multibyte_p =
 31631         STRINGP (object) ? STRING_MULTIBYTE (object) : it->multibyte_p;
 31632 
 31633       it2 = *it;
 31634       if (multibyte_p)
 31635         {
 31636           it2.c = it2.char_to_display = string_char_and_length (p, &it2.len);
 31637 #ifdef HAVE_WINDOW_SYSTEM
 31638           if (FRAME_WINDOW_P (it->f) && ! ASCII_CHAR_P (it2.c))
 31639             it2.face_id = FACE_FOR_CHAR (it->f, face, it2.c,
 31640                                          IT_CHARPOS (*it),
 31641                                          STRINGP (object)? object : Qnil);
 31642 #endif
 31643         }
 31644       else
 31645         {
 31646           it2.c = it2.char_to_display = *p, it2.len = 1;
 31647           if (! ASCII_CHAR_P (it2.c))
 31648             it2.char_to_display = BYTE8_TO_CHAR (it2.c);
 31649         }
 31650 
 31651       it2.glyph_row = NULL;
 31652       it2.what = IT_CHARACTER;
 31653       PRODUCE_GLYPHS (&it2);
 31654       width = NUMVAL (prop) * it2.pixel_width;
 31655     }
 31656   else if ((prop = plist_get (plist, QCalign_to), !NILP (prop))
 31657            && calc_pixel_width_or_height (&tem, it, prop, font, true,
 31658                                           &align_to))
 31659     {
 31660       int x = it->current_x + it->continuation_lines_width;
 31661       int x0 = x;
 31662       /* Adjust for line numbers, if needed.   */
 31663       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31664         {
 31665           x -= it->lnum_pixel_width;
 31666           /* Restore the original width, if required.  */
 31667           if (x + it->stretch_adjust >= it->first_visible_x)
 31668             x += it->stretch_adjust;
 31669         }
 31670 
 31671       if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
 31672         align_to = (align_to < 0
 31673                     ? 0
 31674                     : align_to - window_box_left_offset (it->w, TEXT_AREA));
 31675       else if (align_to < 0)
 31676         align_to = window_box_left_offset (it->w, TEXT_AREA);
 31677       width = max (0, (int)tem + align_to - x);
 31678 
 31679       int next_x = x + width;
 31680       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31681         {
 31682           /* If the line is hscrolled, and the stretch starts before
 31683              the first visible pixel, simulate negative row->x.  */
 31684           if (x < it->first_visible_x)
 31685             {
 31686               next_x -= it->first_visible_x - x;
 31687               it->stretch_adjust = it->first_visible_x - x;
 31688             }
 31689           else
 31690             next_x -= it->stretch_adjust;
 31691         }
 31692       width = next_x - x0;
 31693       zero_width_ok_p = true;
 31694     }
 31695   else
 31696     /* Nothing specified -> width defaults to canonical char width.  */
 31697     width = FRAME_COLUMN_WIDTH (it->f);
 31698 
 31699   if (width <= 0 && (width < 0 || !zero_width_ok_p))
 31700     width = 1;
 31701 
 31702 #ifdef HAVE_WINDOW_SYSTEM
 31703   /* Compute height.  */
 31704   if (FRAME_WINDOW_P (it->f))
 31705     {
 31706       int default_height = normal_char_height (font, ' ');
 31707 
 31708       if ((prop = plist_get (plist, QCheight), !NILP (prop))
 31709           && calc_pixel_width_or_height (&tem, it, prop, font, false, NULL))
 31710         {
 31711           height = (int)tem;
 31712           zero_height_ok_p = true;
 31713         }
 31714       else if (prop = plist_get (plist, QCrelative_height),
 31715                NUMVAL (prop) > 0)
 31716         height = default_height * NUMVAL (prop);
 31717       else
 31718         height = default_height;
 31719 
 31720       if (height <= 0 && (height < 0 || !zero_height_ok_p))
 31721         height = 1;
 31722 
 31723       /* Compute percentage of height used for ascent.  If
 31724          `:ascent ASCENT' is present and valid, use that.  Otherwise,
 31725          derive the ascent from the font in use.  */
 31726       if (prop = plist_get (plist, QCascent),
 31727           NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
 31728         ascent = height * NUMVAL (prop) / 100.0;
 31729       else if (!NILP (prop)
 31730                && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
 31731         ascent = min (max (0, (int)tem), height);
 31732       else
 31733         ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
 31734     }
 31735   else
 31736 #endif  /* HAVE_WINDOW_SYSTEM */
 31737     height = 1;
 31738 
 31739   if (width > 0
 31740       && it->area == TEXT_AREA && it->line_wrap != TRUNCATE
 31741       && it->current_x + width > it->last_visible_x)
 31742     {
 31743       width = it->last_visible_x - it->current_x;
 31744 #ifdef HAVE_WINDOW_SYSTEM
 31745       /* Subtract one more pixel from the stretch width, but only on
 31746          GUI frames, since on a TTY each glyph is one "pixel" wide.  */
 31747       width -= FRAME_WINDOW_P (it->f);
 31748 #endif
 31749     }
 31750 
 31751   if (width > 0 && height > 0 && it->glyph_row)
 31752     {
 31753       Lisp_Object o_object = it->object;
 31754       Lisp_Object object =
 31755         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31756       int n = width;
 31757 
 31758       if (!STRINGP (object))
 31759         object = it->w->contents;
 31760 #ifdef HAVE_WINDOW_SYSTEM
 31761       if (FRAME_WINDOW_P (it->f))
 31762         append_stretch_glyph (it, object, width, height, ascent);
 31763       else
 31764 #endif
 31765         {
 31766           it->object = object;
 31767           it->char_to_display = ' ';
 31768           it->pixel_width = it->len = 1;
 31769           while (n--)
 31770             tty_append_glyph (it);
 31771           it->object = o_object;
 31772         }
 31773     }
 31774 
 31775   it->pixel_width = width;
 31776 #ifdef HAVE_WINDOW_SYSTEM
 31777   if (FRAME_WINDOW_P (it->f))
 31778     {
 31779       it->ascent = it->phys_ascent = ascent;
 31780       it->descent = it->phys_descent = height - it->ascent;
 31781       it->nglyphs = width > 0 && height > 0;
 31782       take_vertical_position_into_account (it);
 31783     }
 31784   else
 31785 #endif
 31786     it->nglyphs = width;
 31787 }
 31788 
 31789 /* Get information about special display element WHAT in an
 31790    environment described by IT.  WHAT is one of IT_TRUNCATION or
 31791    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
 31792    non-null glyph_row member.  This function ensures that fields like
 31793    face_id, c, len of IT are left untouched.  */
 31794 
 31795 static void
 31796 produce_special_glyphs (struct it *it, enum display_element_type what)
 31797 {
 31798   struct it temp_it;
 31799   Lisp_Object gc;
 31800   GLYPH glyph;
 31801 
 31802   temp_it = *it;
 31803   temp_it.object = Qnil;
 31804   memset (&temp_it.current, 0, sizeof temp_it.current);
 31805 
 31806   if (what == IT_CONTINUATION)
 31807     {
 31808       /* Continuation glyph.  For R2L lines, we mirror it by hand.  */
 31809       if (it->bidi_it.paragraph_dir == R2L)
 31810         SET_GLYPH_FROM_CHAR (glyph, '/');
 31811       else
 31812         SET_GLYPH_FROM_CHAR (glyph, '\\');
 31813       if (it->dp
 31814           && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31815         {
 31816           /* FIXME: Should we mirror GC for R2L lines?  */
 31817           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31818           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31819         }
 31820     }
 31821   else if (what == IT_TRUNCATION)
 31822     {
 31823       /* Truncation glyph.  */
 31824       SET_GLYPH_FROM_CHAR (glyph, '$');
 31825       if (it->dp
 31826           && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31827         {
 31828           /* FIXME: Should we mirror GC for R2L lines?  */
 31829           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31830           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31831         }
 31832     }
 31833   else
 31834     emacs_abort ();
 31835 
 31836 #ifdef HAVE_WINDOW_SYSTEM
 31837   /* On a GUI frame, when the right fringe (left fringe for R2L rows)
 31838      is turned off, we precede the truncation/continuation glyphs by a
 31839      stretch glyph whose width is computed such that these special
 31840      glyphs are aligned at the window margin, even when very different
 31841      fonts are used in different glyph rows.  */
 31842   if (FRAME_WINDOW_P (temp_it.f)
 31843       /* init_iterator calls this with it->glyph_row == NULL, and it
 31844          wants only the pixel width of the truncation/continuation
 31845          glyphs.  */
 31846       && temp_it.glyph_row
 31847       /* insert_left_trunc_glyphs calls us at the beginning of the
 31848          row, and it has its own calculation of the stretch glyph
 31849          width.  */
 31850       && temp_it.glyph_row->used[TEXT_AREA] > 0
 31851       && (temp_it.glyph_row->reversed_p
 31852           ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
 31853           : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
 31854     {
 31855       int stretch_width = temp_it.last_visible_x - temp_it.current_x;
 31856 
 31857       if (stretch_width > 0)
 31858         {
 31859           struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
 31860           struct font *font =
 31861             face->font ? face->font : FRAME_FONT (temp_it.f);
 31862           int stretch_ascent =
 31863             (((temp_it.ascent + temp_it.descent)
 31864               * FONT_BASE (font)) / FONT_HEIGHT (font));
 31865 
 31866           append_stretch_glyph (&temp_it, Qnil, stretch_width,
 31867                                 temp_it.ascent + temp_it.descent,
 31868                                 stretch_ascent);
 31869         }
 31870     }
 31871 #endif
 31872 
 31873   temp_it.dp = NULL;
 31874   temp_it.what = IT_CHARACTER;
 31875   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31876   temp_it.face_id = GLYPH_FACE (glyph);
 31877   temp_it.len = CHAR_BYTES (temp_it.c);
 31878 
 31879   PRODUCE_GLYPHS (&temp_it);
 31880   it->pixel_width = temp_it.pixel_width;
 31881   it->nglyphs = temp_it.nglyphs;
 31882 }
 31883 
 31884 /* Produce padding glyphs for mode/header/tab-line whose text needs to
 31885    be truncated.  This is used when the last visible character leaves
 31886    one or more columns till the window edge, but the next character is
 31887    wider than that number of columns, and therefore cannot fit on the
 31888    line.  We then replace these columns with the appropriate padding
 31889    character: '-' for the mode line and SPC for the other two.  That's
 31890    because these lines should not show the usual truncation glyphs
 31891    there.  This function is only used on TTY frames.  */
 31892 static void
 31893 pad_mode_line (struct it *it, bool mode_line_p)
 31894 {
 31895   struct it temp_it;
 31896   GLYPH glyph;
 31897 
 31898   eassert (!FRAME_WINDOW_P (it->f));
 31899   temp_it = *it;
 31900   temp_it.object = Qnil;
 31901   memset (&temp_it.current, 0, sizeof temp_it.current);
 31902 
 31903   SET_GLYPH (glyph, mode_line_p ? '-' : ' ', it->base_face_id);
 31904 
 31905   temp_it.dp = NULL;
 31906   temp_it.what = IT_CHARACTER;
 31907   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31908   temp_it.face_id = GLYPH_FACE (glyph);
 31909   temp_it.len = CHAR_BYTES (temp_it.c);
 31910 
 31911   PRODUCE_GLYPHS (&temp_it);
 31912   it->pixel_width = temp_it.pixel_width;
 31913   it->nglyphs = temp_it.nglyphs;
 31914 }
 31915 
 31916 #ifdef HAVE_WINDOW_SYSTEM
 31917 
 31918 /* Calculate line-height and line-spacing properties.
 31919    An integer value specifies explicit pixel value.
 31920    A float value specifies relative value to current face height.
 31921    A cons (float . face-name) specifies relative value to
 31922    height of specified face font.
 31923 
 31924    Returns height in pixels, or nil.  */
 31925 
 31926 static Lisp_Object
 31927 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
 31928                            int boff, bool override)
 31929 {
 31930   Lisp_Object face_name = Qnil;
 31931   int ascent, descent, height;
 31932 
 31933   if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
 31934     return val;
 31935 
 31936   if (CONSP (val))
 31937     {
 31938       face_name = XCAR (val);
 31939       val = XCDR (val);
 31940       if (!NUMBERP (val))
 31941         val = make_fixnum (1);
 31942       if (NILP (face_name))
 31943         {
 31944           height = it->ascent + it->descent;
 31945           goto scale;
 31946         }
 31947     }
 31948 
 31949   if (NILP (face_name))
 31950     {
 31951       font = FRAME_FONT (it->f);
 31952       boff = FRAME_BASELINE_OFFSET (it->f);
 31953     }
 31954   else if (EQ (face_name, Qt))
 31955     {
 31956       override = false;
 31957     }
 31958   else
 31959     {
 31960       int face_id;
 31961       struct face *face;
 31962 
 31963       face_id = lookup_named_face (it->w, it->f, face_name, false);
 31964       face = FACE_FROM_ID_OR_NULL (it->f, face_id);
 31965       if (face == NULL || ((font = face->font) == NULL))
 31966         return make_fixnum (-1);
 31967       boff = font->baseline_offset;
 31968       if (font->vertical_centering)
 31969         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31970     }
 31971 
 31972   normal_char_ascent_descent (font, -1, &ascent, &descent);
 31973 
 31974   if (override)
 31975     {
 31976       it->override_ascent = ascent;
 31977       it->override_descent = descent;
 31978       it->override_boff = boff;
 31979     }
 31980 
 31981   height = ascent + descent;
 31982 
 31983  scale:
 31984   /* FIXME: Check for overflow in multiplication or conversion.  */
 31985   if (FLOATP (val))
 31986     height = (int)(XFLOAT_DATA (val) * height);
 31987   else if (INTEGERP (val))
 31988     {
 31989       intmax_t v;
 31990       if (integer_to_intmax (val, &v))
 31991         height *= v;
 31992     }
 31993 
 31994   return make_fixnum (height);
 31995 }
 31996 
 31997 
 31998 /* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
 31999    is a face ID to be used for the glyph.  FOR_NO_FONT is true if
 32000    and only if this is for a character for which no font was found.
 32001 
 32002    If the display method (it->glyphless_method) is
 32003    GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
 32004    length of the acronym or the hexadecimal string, UPPER_XOFF and
 32005    UPPER_YOFF are pixel offsets for the upper part of the string,
 32006    LOWER_XOFF and LOWER_YOFF are for the lower part.
 32007 
 32008    For the other display methods, LEN through LOWER_YOFF are zero.  */
 32009 
 32010 static void
 32011 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
 32012                         short upper_xoff, short upper_yoff,
 32013                         short lower_xoff, short lower_yoff)
 32014 {
 32015   struct glyph *glyph;
 32016   enum glyph_row_area area = it->area;
 32017 
 32018   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 32019   if (glyph < it->glyph_row->glyphs[area + 1])
 32020     {
 32021       /* If the glyph row is reversed, we need to prepend the glyph
 32022          rather than append it.  */
 32023       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 32024         {
 32025           struct glyph *g;
 32026 
 32027           /* Make room for the additional glyph.  */
 32028           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 32029             g[1] = *g;
 32030           glyph = it->glyph_row->glyphs[area];
 32031         }
 32032       glyph->charpos = CHARPOS (it->position);
 32033       glyph->object = it->object;
 32034       eassert (it->pixel_width <= SHRT_MAX);
 32035       glyph->pixel_width = it->pixel_width;
 32036       glyph->ascent = it->ascent;
 32037       glyph->descent = it->descent;
 32038       glyph->voffset = it->voffset;
 32039       glyph->type = GLYPHLESS_GLYPH;
 32040       glyph->u.glyphless.method = it->glyphless_method;
 32041       glyph->u.glyphless.for_no_font = for_no_font;
 32042       glyph->u.glyphless.len = len;
 32043       glyph->u.glyphless.ch = it->c;
 32044       glyph->slice.glyphless.upper_xoff = upper_xoff;
 32045       glyph->slice.glyphless.upper_yoff = upper_yoff;
 32046       glyph->slice.glyphless.lower_xoff = lower_xoff;
 32047       glyph->slice.glyphless.lower_yoff = lower_yoff;
 32048       glyph->avoid_cursor_p = it->avoid_cursor_p;
 32049       glyph->multibyte_p = it->multibyte_p;
 32050       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 32051         {
 32052           /* In R2L rows, the left and the right box edges need to be
 32053              drawn in reverse direction.  */
 32054           glyph->right_box_line_p = it->start_of_box_run_p;
 32055           glyph->left_box_line_p = it->end_of_box_run_p;
 32056         }
 32057       else
 32058         {
 32059           glyph->left_box_line_p = it->start_of_box_run_p;
 32060           glyph->right_box_line_p = it->end_of_box_run_p;
 32061         }
 32062       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 32063                                       || it->phys_descent > it->descent);
 32064       glyph->padding_p = false;
 32065       glyph->glyph_not_available_p = false;
 32066       glyph->face_id = face_id;
 32067       glyph->font_type = FONT_TYPE_UNKNOWN;
 32068       if (it->bidi_p)
 32069         {
 32070           glyph->resolved_level = it->bidi_it.resolved_level;
 32071           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 32072           glyph->bidi_type = it->bidi_it.type;
 32073         }
 32074       ++it->glyph_row->used[area];
 32075     }
 32076   else
 32077     IT_EXPAND_MATRIX_WIDTH (it, area);
 32078 }
 32079 
 32080 
 32081 /* Produce a glyph for a glyphless character for iterator IT.
 32082    IT->glyphless_method specifies which method to use for displaying
 32083    the character.  See the description of enum
 32084    glyphless_display_method in dispextern.h for the detail.
 32085 
 32086    FOR_NO_FONT is true if and only if this is for a character for
 32087    which no font was found.  ACRONYM, if non-nil, is an acronym string
 32088    for the character.  */
 32089 
 32090 static void
 32091 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
 32092 {
 32093   int face_id;
 32094   struct face *face;
 32095   struct font *font;
 32096   int base_width, base_height, width, height;
 32097   short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
 32098   int len;
 32099 
 32100   /* Get the metrics of the base font.  We always refer to the current
 32101      ASCII face.  */
 32102   face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
 32103   font = face->font ? face->font : FRAME_FONT (it->f);
 32104   normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 32105   it->ascent += font->baseline_offset;
 32106   it->descent -= font->baseline_offset;
 32107   base_height = it->ascent + it->descent;
 32108   base_width = font->average_width;
 32109 
 32110   face_id = merge_glyphless_glyph_face (it);
 32111 
 32112   if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
 32113     {
 32114       it->pixel_width = THIN_SPACE_WIDTH;
 32115       len = 0;
 32116       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 32117     }
 32118   else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
 32119     {
 32120       width = CHARACTER_WIDTH (it->c);
 32121       if (width == 0)
 32122         width = 1;
 32123       else if (width > 4)
 32124         width = 4;
 32125       it->pixel_width = base_width * width;
 32126       len = 0;
 32127       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 32128     }
 32129   else
 32130     {
 32131       char buf[7];
 32132       const char *str;
 32133       unsigned int code[6];
 32134       int upper_len;
 32135       int ascent, descent;
 32136       struct font_metrics metrics_upper, metrics_lower;
 32137 
 32138       face = FACE_FROM_ID (it->f, face_id);
 32139       font = face->font ? face->font : FRAME_FONT (it->f);
 32140       prepare_face_for_display (it->f, face);
 32141 
 32142       if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
 32143         {
 32144           if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
 32145             acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
 32146           if (CONSP (acronym))
 32147             acronym = XCAR (acronym);
 32148           str = STRINGP (acronym) ? SSDATA (acronym) : "";
 32149         }
 32150       else
 32151         {
 32152           eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
 32153           sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
 32154           str = buf;
 32155         }
 32156       for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
 32157         code[len] = font->driver->encode_char (font, str[len]);
 32158       upper_len = (len + 1) / 2;
 32159       font->driver->text_extents (font, code, upper_len,
 32160                                   &metrics_upper);
 32161       font->driver->text_extents (font, code + upper_len, len - upper_len,
 32162                                   &metrics_lower);
 32163 
 32164 
 32165 
 32166       /* +4 is for vertical bars of a box plus 1-pixel spaces at both side.  */
 32167       width = max (metrics_upper.width, metrics_lower.width) + 4;
 32168       upper_xoff = lower_xoff = 2; /* the typical case */
 32169       if (base_width >= width)
 32170         {
 32171           /* Align the upper to the left, the lower to the right.  */
 32172           it->pixel_width = base_width;
 32173           lower_xoff = base_width - 2 - metrics_lower.width;
 32174         }
 32175       else
 32176         {
 32177           /* Center the shorter one.  */
 32178           it->pixel_width = width;
 32179           if (metrics_upper.width >= metrics_lower.width)
 32180             lower_xoff = (width - metrics_lower.width) / 2;
 32181           else
 32182             upper_xoff = (width - metrics_upper.width) / 2;
 32183         }
 32184 
 32185       /* +5 is for horizontal bars of a box plus 1-pixel spaces at
 32186          top, bottom, and between upper and lower strings.  */
 32187       height = (metrics_upper.ascent + metrics_upper.descent
 32188                 + metrics_lower.ascent + metrics_lower.descent) + 5;
 32189       /* Center vertically.
 32190          H:base_height, D:base_descent
 32191          h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
 32192 
 32193          ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
 32194          descent = D - H/2 + h/2;
 32195          lower_yoff = descent - 2 - ld;
 32196          upper_yoff = lower_yoff - la - 1 - ud;  */
 32197       ascent = - (it->descent - (base_height + height + 1) / 2);
 32198       descent = it->descent - (base_height - height) / 2;
 32199       lower_yoff = descent - 2 - metrics_lower.descent;
 32200       upper_yoff = (lower_yoff - metrics_lower.ascent - 1
 32201                     - metrics_upper.descent);
 32202       /* Don't make the height shorter than the base height.  */
 32203       if (height > base_height)
 32204         {
 32205           it->ascent = ascent;
 32206           it->descent = descent;
 32207         }
 32208     }
 32209 
 32210   it->phys_ascent = it->ascent;
 32211   it->phys_descent = it->descent;
 32212   if (it->glyph_row)
 32213     append_glyphless_glyph (it, face_id, for_no_font, len,
 32214                             upper_xoff, upper_yoff,
 32215                             lower_xoff, lower_yoff);
 32216   it->nglyphs = 1;
 32217   take_vertical_position_into_account (it);
 32218 }
 32219 
 32220 
 32221 /* If face has a box, add the box thickness to the character
 32222    height.  If character has a box line to the left and/or
 32223    right, add the box line width to the character's width.  */
 32224 #define IT_APPLY_FACE_BOX(it, face)                             \
 32225   do {                                                          \
 32226     if (face->box != FACE_NO_BOX)                               \
 32227       {                                                         \
 32228         int thick = face->box_horizontal_line_width;            \
 32229         if (thick > 0)                                          \
 32230           {                                                     \
 32231             it->ascent += thick;                                \
 32232             it->descent += thick;                               \
 32233           }                                                     \
 32234                                                                 \
 32235         thick = face->box_vertical_line_width;                  \
 32236         if (thick > 0)                                          \
 32237           {                                                     \
 32238             if (it->start_of_box_run_p)                         \
 32239               it->pixel_width += thick;                         \
 32240             if (it->end_of_box_run_p)                           \
 32241               it->pixel_width += thick;                         \
 32242           }                                                     \
 32243       }                                                         \
 32244     } while (false)
 32245 
 32246 /* RIF:
 32247    Produce glyphs/get display metrics for the display element IT is
 32248    loaded with.  See the description of struct it in dispextern.h
 32249    for an overview of struct it.  */
 32250 
 32251 void
 32252 gui_produce_glyphs (struct it *it)
 32253 {
 32254   int extra_line_spacing = it->extra_line_spacing;
 32255 
 32256   it->glyph_not_available_p = false;
 32257 
 32258   if (it->what == IT_CHARACTER)
 32259     {
 32260       unsigned char2b;
 32261       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32262       struct font *font = face->font;
 32263       struct font_metrics *pcm = NULL;
 32264       int boff;                 /* Baseline offset.  */
 32265 
 32266       if (font == NULL)
 32267         {
 32268           /* When no suitable font is found, display this character by
 32269              the method specified in the first extra slot of
 32270              Vglyphless_char_display.  */
 32271           Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 32272 
 32273           eassert (it->what == IT_GLYPHLESS);
 32274           produce_glyphless_glyph (it, true,
 32275                                    STRINGP (acronym) ? acronym : Qnil);
 32276           goto done;
 32277         }
 32278 
 32279       boff = font->baseline_offset;
 32280       if (font->vertical_centering)
 32281         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32282 
 32283       if (it->char_to_display != '\n' && it->char_to_display != '\t')
 32284         {
 32285           it->nglyphs = 1;
 32286 
 32287           if (it->override_ascent >= 0)
 32288             {
 32289               it->ascent = it->override_ascent;
 32290               it->descent = it->override_descent;
 32291               boff = it->override_boff;
 32292             }
 32293           else
 32294             {
 32295               it->ascent = FONT_BASE (font) + boff;
 32296               it->descent = FONT_DESCENT (font) - boff;
 32297             }
 32298 
 32299           if (get_char_glyph_code (it->char_to_display, font, &char2b))
 32300             {
 32301               pcm = get_per_char_metric (font, &char2b);
 32302               if (pcm->width == 0
 32303                   && pcm->rbearing == 0 && pcm->lbearing == 0)
 32304                 pcm = NULL;
 32305             }
 32306 
 32307           if (pcm)
 32308             {
 32309               it->phys_ascent = pcm->ascent + boff;
 32310               it->phys_descent = pcm->descent - boff;
 32311               it->pixel_width = pcm->width;
 32312               /* Don't use font-global values for ascent and descent
 32313                  if they result in an exceedingly large line height.  */
 32314               if (it->override_ascent < 0)
 32315                 {
 32316                   if (FONT_TOO_HIGH (font))
 32317                     {
 32318                       it->ascent = it->phys_ascent;
 32319                       it->descent = it->phys_descent;
 32320                       /* These limitations are enforced by an
 32321                          assertion near the end of this function.  */
 32322                       if (it->ascent < 0)
 32323                         it->ascent = 0;
 32324                       if (it->descent < 0)
 32325                         it->descent = 0;
 32326                     }
 32327                 }
 32328             }
 32329           else
 32330             {
 32331               it->glyph_not_available_p = true;
 32332               it->phys_ascent = it->ascent;
 32333               it->phys_descent = it->descent;
 32334               it->pixel_width = font->space_width;
 32335             }
 32336 
 32337           if (it->constrain_row_ascent_descent_p)
 32338             {
 32339               if (it->descent > it->max_descent)
 32340                 {
 32341                   it->ascent += it->descent - it->max_descent;
 32342                   it->descent = it->max_descent;
 32343                 }
 32344               if (it->ascent > it->max_ascent)
 32345                 {
 32346                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32347                   it->ascent = it->max_ascent;
 32348                 }
 32349               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32350               it->phys_descent = min (it->phys_descent, it->descent);
 32351               extra_line_spacing = 0;
 32352             }
 32353 
 32354           /* If this is a space inside a region of text with
 32355              `space-width' property, change its width.  */
 32356           bool stretched_p
 32357             = it->char_to_display == ' ' && !NILP (it->space_width);
 32358           if (stretched_p)
 32359             it->pixel_width *= XFLOATINT (it->space_width);
 32360 
 32361           IT_APPLY_FACE_BOX(it, face);
 32362 
 32363           /* If face has an overline, add the height of the overline
 32364              (1 pixel) and a 1 pixel margin to the character height.  */
 32365           if (face->overline_p)
 32366             it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32367 
 32368           if (it->constrain_row_ascent_descent_p)
 32369             {
 32370               if (it->ascent > it->max_ascent)
 32371                 it->ascent = it->max_ascent;
 32372               if (it->descent > it->max_descent)
 32373                 it->descent = it->max_descent;
 32374             }
 32375 
 32376           take_vertical_position_into_account (it);
 32377 
 32378           /* If we have to actually produce glyphs, do it.  */
 32379           if (it->glyph_row)
 32380             {
 32381               if (stretched_p)
 32382                 {
 32383                   /* Translate a space with a `space-width' property
 32384                      into a stretch glyph.  */
 32385                   int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
 32386                                 / FONT_HEIGHT (font));
 32387                   append_stretch_glyph (it, it->object, it->pixel_width,
 32388                                         it->ascent + it->descent, ascent);
 32389                 }
 32390               else
 32391                 append_glyph (it);
 32392 
 32393               /* If characters with lbearing or rbearing are displayed
 32394                  in this line, record that fact in a flag of the
 32395                  glyph row.  This is used to optimize X output code.  */
 32396               if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
 32397                 it->glyph_row->contains_overlapping_glyphs_p = true;
 32398             }
 32399           if (! stretched_p && it->pixel_width == 0)
 32400             /* We assure that all visible glyphs have at least 1-pixel
 32401                width.  */
 32402             it->pixel_width = 1;
 32403         }
 32404       else if (it->char_to_display == '\n')
 32405         {
 32406           /* A newline has no width, but we need the height of the
 32407              line.  But if previous part of the line sets a height,
 32408              don't increase that height.  */
 32409 
 32410           Lisp_Object height;
 32411           Lisp_Object total_height = Qnil;
 32412 
 32413           it->override_ascent = -1;
 32414           it->pixel_width = 0;
 32415           it->nglyphs = 0;
 32416 
 32417           height = get_it_property (it, Qline_height);
 32418           /* Split (line-height total-height) list.  */
 32419           if (CONSP (height)
 32420               && CONSP (XCDR (height))
 32421               && NILP (XCDR (XCDR (height))))
 32422             {
 32423               total_height = XCAR (XCDR (height));
 32424               height = XCAR (height);
 32425             }
 32426           height = calc_line_height_property (it, height, font, boff, true);
 32427 
 32428           if (it->override_ascent >= 0)
 32429             {
 32430               it->ascent = it->override_ascent;
 32431               it->descent = it->override_descent;
 32432               boff = it->override_boff;
 32433             }
 32434           else
 32435             {
 32436               if (FONT_TOO_HIGH (font))
 32437                 {
 32438                   it->ascent = font->pixel_size + boff - 1;
 32439                   it->descent = -boff + 1;
 32440                   if (it->descent < 0)
 32441                     it->descent = 0;
 32442                 }
 32443               else
 32444                 {
 32445                   it->ascent = FONT_BASE (font) + boff;
 32446                   it->descent = FONT_DESCENT (font) - boff;
 32447                 }
 32448             }
 32449 
 32450           if (EQ (height, Qt))
 32451             {
 32452               if (it->descent > it->max_descent)
 32453                 {
 32454                   it->ascent += it->descent - it->max_descent;
 32455                   it->descent = it->max_descent;
 32456                 }
 32457               if (it->ascent > it->max_ascent)
 32458                 {
 32459                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32460                   it->ascent = it->max_ascent;
 32461                 }
 32462               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32463               it->phys_descent = min (it->phys_descent, it->descent);
 32464               it->constrain_row_ascent_descent_p = true;
 32465               extra_line_spacing = 0;
 32466             }
 32467           else
 32468             {
 32469               Lisp_Object spacing;
 32470 
 32471               it->phys_ascent = it->ascent;
 32472               it->phys_descent = it->descent;
 32473 
 32474               if ((it->max_ascent > 0 || it->max_descent > 0)
 32475                   && face->box != FACE_NO_BOX
 32476                   && face->box_horizontal_line_width > 0)
 32477                 {
 32478                   it->ascent += face->box_horizontal_line_width;
 32479                   it->descent += face->box_horizontal_line_width;
 32480                 }
 32481               if (!NILP (height)
 32482                   && XFIXNUM (height) > it->ascent + it->descent)
 32483                 it->ascent = XFIXNUM (height) - it->descent;
 32484 
 32485               if (!NILP (total_height))
 32486                 spacing = calc_line_height_property (it, total_height, font,
 32487                                                      boff, false);
 32488               else
 32489                 {
 32490                   spacing = get_it_property (it, Qline_spacing);
 32491                   spacing = calc_line_height_property (it, spacing, font,
 32492                                                        boff, false);
 32493                 }
 32494               if (FIXNUMP (spacing))
 32495                 {
 32496                   extra_line_spacing = XFIXNUM (spacing);
 32497                   if (!NILP (total_height))
 32498                     extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 32499                 }
 32500             }
 32501         }
 32502       else                    /* i.e. (it->char_to_display == '\t') */
 32503         {
 32504           if (font->space_width > 0)
 32505             {
 32506               int tab_width = it->tab_width * font->space_width;
 32507               int x = it->current_x + it->continuation_lines_width;
 32508               int x0 = x;
 32509               /* Adjust for line numbers, if needed.   */
 32510               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32511                 {
 32512                   x -= it->lnum_pixel_width;
 32513                   /* Restore the original TAB width, if required.  */
 32514                   if (x + it->stretch_adjust >= it->first_visible_x)
 32515                     x += it->stretch_adjust;
 32516                 }
 32517 
 32518               int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
 32519 
 32520               /* If the distance from the current position to the next tab
 32521                  stop is less than a space character width, use the
 32522                  tab stop after that.  */
 32523               if (next_tab_x - x < font->space_width)
 32524                 next_tab_x += tab_width;
 32525               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32526                 {
 32527                   next_tab_x += it->lnum_pixel_width;
 32528                   /* If the line is hscrolled, and the TAB starts before
 32529                      the first visible pixel, simulate negative row->x.  */
 32530                   if (x < it->first_visible_x)
 32531                     {
 32532                       next_tab_x -= it->first_visible_x - x;
 32533                       it->stretch_adjust = it->first_visible_x - x;
 32534                     }
 32535                   else
 32536                     next_tab_x -= it->stretch_adjust;
 32537                 }
 32538 
 32539               it->pixel_width = next_tab_x - x0;
 32540               it->nglyphs = 1;
 32541               if (FONT_TOO_HIGH (font))
 32542                 {
 32543                   if (get_char_glyph_code (' ', font, &char2b))
 32544                     {
 32545                       pcm = get_per_char_metric (font, &char2b);
 32546                       if (pcm->width == 0
 32547                           && pcm->rbearing == 0 && pcm->lbearing == 0)
 32548                         pcm = NULL;
 32549                     }
 32550 
 32551                   if (pcm)
 32552                     {
 32553                       it->ascent = pcm->ascent + boff;
 32554                       it->descent = pcm->descent - boff;
 32555                     }
 32556                   else
 32557                     {
 32558                       it->ascent = font->pixel_size + boff - 1;
 32559                       it->descent = -boff + 1;
 32560                     }
 32561                   if (it->ascent < 0)
 32562                     it->ascent = 0;
 32563                   if (it->descent < 0)
 32564                     it->descent = 0;
 32565                 }
 32566               else
 32567                 {
 32568                   it->ascent = FONT_BASE (font) + boff;
 32569                   it->descent = FONT_DESCENT (font) - boff;
 32570                 }
 32571               it->phys_ascent = it->ascent;
 32572               it->phys_descent = it->descent;
 32573 
 32574               if (it->glyph_row)
 32575                 {
 32576                   append_stretch_glyph (it, it->object, it->pixel_width,
 32577                                         it->ascent + it->descent, it->ascent);
 32578                 }
 32579             }
 32580           else
 32581             {
 32582               it->pixel_width = 0;
 32583               it->nglyphs = 1;
 32584             }
 32585         }
 32586 
 32587       if (FONT_TOO_HIGH (font))
 32588         {
 32589           int font_ascent, font_descent;
 32590 
 32591           /* For very large fonts, where we ignore the declared font
 32592              dimensions, and go by per-character metrics instead,
 32593              don't let the row ascent and descent values (and the row
 32594              height computed from them) be smaller than the "normal"
 32595              character metrics.  This avoids unpleasant effects
 32596              whereby lines on display would change their height
 32597              depending on which characters are shown.  */
 32598           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32599           it->max_ascent = max (it->max_ascent, font_ascent);
 32600           it->max_descent = max (it->max_descent, font_descent);
 32601         }
 32602 
 32603       if (it->ascent < 0)
 32604         it->ascent = 0;
 32605       if (it->descent < 0)
 32606         it->descent = 0;
 32607     }
 32608   else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
 32609     {
 32610       /* A static composition.
 32611 
 32612          Note: A composition is represented as one glyph in the
 32613          glyph matrix.  There are no padding glyphs.
 32614 
 32615          Important note: pixel_width, ascent, and descent are the
 32616          values of what is drawn by draw_glyphs (i.e. the values of
 32617          the overall glyphs composed).  */
 32618       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32619       int boff;                 /* baseline offset */
 32620       struct composition *cmp = composition_table[it->cmp_it.id];
 32621       int glyph_len = cmp->glyph_len;
 32622       struct font *font = face->font;
 32623 
 32624       it->nglyphs = 1;
 32625 
 32626       /* If we have not yet calculated pixel size data of glyphs of
 32627          the composition for the current face font, calculate them
 32628          now.  Theoretically, we have to check all fonts for the
 32629          glyphs, but that requires much time and memory space.  So,
 32630          here we check only the font of the first glyph.  This may
 32631          lead to incorrect display, but it's very rare, and C-l
 32632          (recenter-top-bottom) can correct the display anyway.  */
 32633       if (! cmp->font || cmp->font != font)
 32634         {
 32635           /* Ascent and descent of the font of the first character
 32636              of this composition (adjusted by baseline offset).
 32637              Ascent and descent of overall glyphs should not be less
 32638              than these, respectively.  */
 32639           int font_ascent, font_descent, font_height;
 32640           /* Bounding box of the overall glyphs.  */
 32641           int leftmost, rightmost, lowest, highest;
 32642           int lbearing, rbearing;
 32643           int i, width, ascent, descent;
 32644           int c;
 32645           unsigned char2b;
 32646           struct font_metrics *pcm;
 32647           ptrdiff_t pos;
 32648 
 32649           eassume (0 < glyph_len); /* See Bug#8512.  */
 32650           do
 32651             c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
 32652           while (c == '\t' && 0 < --glyph_len);
 32653 
 32654           bool right_padded = glyph_len < cmp->glyph_len;
 32655           for (i = 0; i < glyph_len; i++)
 32656             {
 32657               c = COMPOSITION_GLYPH (cmp, i);
 32658               if (c != '\t')
 32659                 break;
 32660               cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32661             }
 32662           bool left_padded = i > 0;
 32663 
 32664           pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
 32665                  : IT_CHARPOS (*it));
 32666           /* If no suitable font is found, use the default font.  */
 32667           bool font_not_found_p = font == NULL;
 32668           if (font_not_found_p)
 32669             {
 32670               face = face->ascii_face;
 32671               font = face->font;
 32672             }
 32673           boff = font->baseline_offset;
 32674           if (font->vertical_centering)
 32675             boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32676           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32677           font_ascent +=  boff;
 32678           font_descent -= boff;
 32679           font_height = font_ascent + font_descent;
 32680 
 32681           cmp->font = font;
 32682 
 32683           pcm = NULL;
 32684           if (! font_not_found_p)
 32685             {
 32686               get_char_face_and_encoding (it->f, c, it->face_id,
 32687                                           &char2b, false);
 32688               pcm = get_per_char_metric (font, &char2b);
 32689             }
 32690 
 32691           /* Initialize the bounding box.  */
 32692           if (pcm)
 32693             {
 32694               width = cmp->glyph_len > 0 ? pcm->width : 0;
 32695               ascent = pcm->ascent;
 32696               descent = pcm->descent;
 32697               lbearing = pcm->lbearing;
 32698               rbearing = pcm->rbearing;
 32699             }
 32700           else
 32701             {
 32702               width = cmp->glyph_len > 0 ? font->space_width : 0;
 32703               ascent = FONT_BASE (font);
 32704               descent = FONT_DESCENT (font);
 32705               lbearing = 0;
 32706               rbearing = width;
 32707             }
 32708 
 32709           rightmost = width;
 32710           leftmost = 0;
 32711           lowest = - descent + boff;
 32712           highest = ascent + boff;
 32713 
 32714           if (! font_not_found_p
 32715               && font->default_ascent
 32716               && CHAR_TABLE_P (Vuse_default_ascent)
 32717               && !NILP (Faref (Vuse_default_ascent,
 32718                                make_fixnum (it->char_to_display))))
 32719             highest = font->default_ascent + boff;
 32720 
 32721           /* Draw the first glyph at the normal position.  It may be
 32722              shifted to right later if some other glyphs are drawn
 32723              at the left.  */
 32724           cmp->offsets[i * 2] = 0;
 32725           cmp->offsets[i * 2 + 1] = boff;
 32726           cmp->lbearing = lbearing;
 32727           cmp->rbearing = rbearing;
 32728 
 32729           /* Set cmp->offsets for the remaining glyphs.  */
 32730           for (i++; i < glyph_len; i++)
 32731             {
 32732               int left, right, btm, top;
 32733               int ch = COMPOSITION_GLYPH (cmp, i);
 32734               int face_id;
 32735               struct face *this_face;
 32736 
 32737               if (ch == '\t')
 32738                 ch = ' ';
 32739               face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
 32740               this_face = FACE_FROM_ID (it->f, face_id);
 32741               font = this_face->font;
 32742 
 32743               if (font == NULL)
 32744                 pcm = NULL;
 32745               else
 32746                 {
 32747                   get_char_face_and_encoding (it->f, ch, face_id,
 32748                                               &char2b, false);
 32749                   pcm = get_per_char_metric (font, &char2b);
 32750                 }
 32751               if (! pcm)
 32752                 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32753               else
 32754                 {
 32755                   width = pcm->width;
 32756                   ascent = pcm->ascent;
 32757                   descent = pcm->descent;
 32758                   lbearing = pcm->lbearing;
 32759                   rbearing = pcm->rbearing;
 32760                   if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
 32761                     {
 32762                       /* Relative composition with or without
 32763                          alternate chars.  */
 32764                       left = (leftmost + rightmost - width) / 2;
 32765                       btm = - descent + boff;
 32766                       if (font->relative_compose
 32767                           && (! CHAR_TABLE_P (Vignore_relative_composition)
 32768                               || NILP (Faref (Vignore_relative_composition,
 32769                                               make_fixnum (ch)))))
 32770                         {
 32771 
 32772                           if (- descent >= font->relative_compose)
 32773                             /* One extra pixel between two glyphs.  */
 32774                             btm = highest + 1;
 32775                           else if (ascent <= 0)
 32776                             /* One extra pixel between two glyphs.  */
 32777                             btm = lowest - 1 - ascent - descent;
 32778                         }
 32779                     }
 32780                   else
 32781                     {
 32782                       /* A composition rule is specified by an integer
 32783                          value that encodes global and new reference
 32784                          points (GREF and NREF).  GREF and NREF are
 32785                          specified by numbers as below:
 32786 
 32787                          0---1---2 -- ascent
 32788                          |       |
 32789                          |       |
 32790                          |       |
 32791                          9--10--11 -- center
 32792                          |       |
 32793                          ---3---4---5--- baseline
 32794                          |       |
 32795                          6---7---8 -- descent
 32796                       */
 32797                       int rule = COMPOSITION_RULE (cmp, i);
 32798                       int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
 32799 
 32800                       COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
 32801                       grefx = gref % 3, nrefx = nref % 3;
 32802                       grefy = gref / 3, nrefy = nref / 3;
 32803                       if (xoff)
 32804                         xoff = font_height * (xoff - 128) / 256;
 32805                       if (yoff)
 32806                         yoff = font_height * (yoff - 128) / 256;
 32807 
 32808                       left = (leftmost
 32809                               + grefx * (rightmost - leftmost) / 2
 32810                               - nrefx * width / 2
 32811                               + xoff);
 32812 
 32813                       btm = ((grefy == 0 ? highest
 32814                               : grefy == 1 ? 0
 32815                               : grefy == 2 ? lowest
 32816                               : (highest + lowest) / 2)
 32817                              - (nrefy == 0 ? ascent + descent
 32818                                 : nrefy == 1 ? descent - boff
 32819                                 : nrefy == 2 ? 0
 32820                                 : (ascent + descent) / 2)
 32821                              + yoff);
 32822                     }
 32823 
 32824                   cmp->offsets[i * 2] = left;
 32825                   cmp->offsets[i * 2 + 1] = btm + descent;
 32826 
 32827                   /* Update the bounding box of the overall glyphs. */
 32828                   if (width > 0)
 32829                     {
 32830                       right = left + width;
 32831                       if (left < leftmost)
 32832                         leftmost = left;
 32833                       if (right > rightmost)
 32834                         rightmost = right;
 32835                     }
 32836                   top = btm + descent + ascent;
 32837                   if (top > highest)
 32838                     highest = top;
 32839                   if (btm < lowest)
 32840                     lowest = btm;
 32841 
 32842                   if (cmp->lbearing > left + lbearing)
 32843                     cmp->lbearing = left + lbearing;
 32844                   if (cmp->rbearing < left + rbearing)
 32845                     cmp->rbearing = left + rbearing;
 32846                 }
 32847             }
 32848 
 32849           /* If there are glyphs whose x-offsets are negative,
 32850              shift all glyphs to the right and make all x-offsets
 32851              non-negative.  */
 32852           if (leftmost < 0)
 32853             {
 32854               for (i = 0; i < cmp->glyph_len; i++)
 32855                 cmp->offsets[i * 2] -= leftmost;
 32856               rightmost -= leftmost;
 32857               cmp->lbearing -= leftmost;
 32858               cmp->rbearing -= leftmost;
 32859             }
 32860 
 32861           if (left_padded && cmp->lbearing < 0)
 32862             {
 32863               for (i = 0; i < cmp->glyph_len; i++)
 32864                 cmp->offsets[i * 2] -= cmp->lbearing;
 32865               rightmost -= cmp->lbearing;
 32866               cmp->rbearing -= cmp->lbearing;
 32867               cmp->lbearing = 0;
 32868             }
 32869           if (right_padded && rightmost < cmp->rbearing)
 32870             {
 32871               rightmost = cmp->rbearing;
 32872             }
 32873 
 32874           cmp->pixel_width = rightmost;
 32875           cmp->ascent = highest;
 32876           cmp->descent = - lowest;
 32877           if (cmp->ascent < font_ascent)
 32878             cmp->ascent = font_ascent;
 32879           if (cmp->descent < font_descent)
 32880             cmp->descent = font_descent;
 32881         }
 32882 
 32883       if (it->glyph_row
 32884           && (cmp->lbearing < 0
 32885               || cmp->rbearing > cmp->pixel_width))
 32886         it->glyph_row->contains_overlapping_glyphs_p = true;
 32887 
 32888       it->pixel_width = cmp->pixel_width;
 32889       it->ascent = it->phys_ascent = cmp->ascent;
 32890       it->descent = it->phys_descent = cmp->descent;
 32891       IT_APPLY_FACE_BOX(it, face);
 32892 
 32893       /* If face has an overline, add the height of the overline
 32894          (1 pixel) and a 1 pixel margin to the character height.  */
 32895       if (face->overline_p)
 32896         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32897 
 32898       take_vertical_position_into_account (it);
 32899       if (it->ascent < 0)
 32900         it->ascent = 0;
 32901       if (it->descent < 0)
 32902         it->descent = 0;
 32903 
 32904       if (it->glyph_row && cmp->glyph_len > 0)
 32905         append_composite_glyph (it);
 32906     }
 32907   else if (it->what == IT_COMPOSITION)
 32908     {
 32909       /* A dynamic (automatic) composition.  */
 32910       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32911       Lisp_Object gstring;
 32912       struct font_metrics metrics;
 32913 
 32914       it->nglyphs = 1;
 32915 
 32916       gstring = composition_gstring_from_id (it->cmp_it.id);
 32917       it->pixel_width
 32918         = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
 32919                                      &metrics);
 32920       if (it->pixel_width == 0)
 32921         {
 32922           it->glyph_not_available_p = true;
 32923           it->phys_ascent = it->ascent;
 32924           it->phys_descent = it->descent;
 32925           it->pixel_width = face->font->space_width;
 32926         }
 32927       else
 32928         {
 32929           if (it->glyph_row
 32930               && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
 32931             it->glyph_row->contains_overlapping_glyphs_p = true;
 32932           it->ascent = it->phys_ascent = metrics.ascent;
 32933           it->descent = it->phys_descent = metrics.descent;
 32934         }
 32935       IT_APPLY_FACE_BOX(it, face);
 32936 
 32937       /* If face has an overline, add the height of the overline
 32938          (1 pixel) and a 1 pixel margin to the character height.  */
 32939       if (face->overline_p)
 32940         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32941       take_vertical_position_into_account (it);
 32942       if (it->ascent < 0)
 32943         it->ascent = 0;
 32944       if (it->descent < 0)
 32945         it->descent = 0;
 32946 
 32947       if (it->glyph_row)
 32948         append_composite_glyph (it);
 32949     }
 32950   else if (it->what == IT_GLYPHLESS)
 32951     produce_glyphless_glyph (it, false, Qnil);
 32952   else if (it->what == IT_IMAGE)
 32953     produce_image_glyph (it);
 32954   else if (it->what == IT_STRETCH)
 32955     produce_stretch_glyph (it);
 32956   else if (it->what == IT_XWIDGET)
 32957     produce_xwidget_glyph (it);
 32958 
 32959  done:
 32960   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
 32961      because this isn't true for images with `:ascent 100'.  */
 32962   eassert (it->ascent >= 0 && it->descent >= 0);
 32963   if (it->area == TEXT_AREA)
 32964     it->current_x += it->pixel_width;
 32965 
 32966   if (extra_line_spacing > 0)
 32967     {
 32968       it->descent += extra_line_spacing;
 32969       if (extra_line_spacing > it->max_extra_line_spacing)
 32970         it->max_extra_line_spacing = extra_line_spacing;
 32971     }
 32972 
 32973   it->max_ascent = max (it->max_ascent, it->ascent);
 32974   it->max_descent = max (it->max_descent, it->descent);
 32975   it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
 32976   it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
 32977 }
 32978 
 32979 /* EXPORT for RIF:
 32980    Output LEN glyphs starting at START at the nominal cursor position.
 32981    Advance the nominal cursor over the text.  UPDATED_ROW is the glyph row
 32982    being updated, and UPDATED_AREA is the area of that row being updated.  */
 32983 
 32984 void
 32985 gui_write_glyphs (struct window *w, struct glyph_row *updated_row,
 32986                 struct glyph *start, enum glyph_row_area updated_area, int len)
 32987 {
 32988   int x, hpos, chpos = w->phys_cursor.hpos;
 32989 
 32990   eassert (updated_row);
 32991   /* When the window is hscrolled, cursor hpos can legitimately be out
 32992      of bounds, but we draw the cursor at the corresponding window
 32993      margin in that case.  */
 32994   if (!updated_row->reversed_p && chpos < 0)
 32995     chpos = 0;
 32996   if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
 32997     chpos = updated_row->used[TEXT_AREA] - 1;
 32998 
 32999   block_input ();
 33000 
 33001   /* Write glyphs.  */
 33002 
 33003   hpos = start - updated_row->glyphs[updated_area];
 33004   x = draw_glyphs (w, w->output_cursor.x,
 33005                    updated_row, updated_area,
 33006                    hpos, hpos + len,
 33007                    DRAW_NORMAL_TEXT, 0);
 33008 
 33009   /* Invalidate old phys cursor if the glyph at its hpos is redrawn.  */
 33010   if (updated_area == TEXT_AREA
 33011       && w->phys_cursor_on_p
 33012       && w->phys_cursor.vpos == w->output_cursor.vpos
 33013       && chpos >= hpos
 33014       && chpos < hpos + len)
 33015     w->phys_cursor_on_p = false;
 33016 
 33017   unblock_input ();
 33018 
 33019   /* Advance the output cursor.  */
 33020   w->output_cursor.hpos += len;
 33021   w->output_cursor.x = x;
 33022 }
 33023 
 33024 
 33025 /* EXPORT for RIF:
 33026    Insert LEN glyphs from START at the nominal cursor position.  */
 33027 
 33028 void
 33029 gui_insert_glyphs (struct window *w, struct glyph_row *updated_row,
 33030                  struct glyph *start, enum glyph_row_area updated_area, int len)
 33031 {
 33032   struct frame *f;
 33033   int line_height, shift_by_width, shifted_region_width;
 33034   struct glyph_row *row;
 33035   struct glyph *glyph;
 33036   int frame_x, frame_y;
 33037   ptrdiff_t hpos;
 33038 
 33039   eassert (updated_row);
 33040   block_input ();
 33041   f = XFRAME (WINDOW_FRAME (w));
 33042 
 33043   /* Get the height of the line we are in.  */
 33044   row = updated_row;
 33045   line_height = row->height;
 33046 
 33047   /* Get the width of the glyphs to insert.  */
 33048   shift_by_width = 0;
 33049   for (glyph = start; glyph < start + len; ++glyph)
 33050     shift_by_width += glyph->pixel_width;
 33051 
 33052   /* Get the width of the region to shift right.  */
 33053   shifted_region_width = (window_box_width (w, updated_area)
 33054                           - w->output_cursor.x
 33055                           - shift_by_width);
 33056 
 33057   /* Shift right.  */
 33058   frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
 33059   frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
 33060 
 33061   FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
 33062                                           line_height, shift_by_width);
 33063 
 33064   /* Write the glyphs.  */
 33065   hpos = start - row->glyphs[updated_area];
 33066   draw_glyphs (w, w->output_cursor.x, row, updated_area,
 33067                hpos, hpos + len,
 33068                DRAW_NORMAL_TEXT, 0);
 33069 
 33070   /* Advance the output cursor.  */
 33071   w->output_cursor.hpos += len;
 33072   w->output_cursor.x += shift_by_width;
 33073   unblock_input ();
 33074 }
 33075 
 33076 
 33077 /* EXPORT for RIF:
 33078    Erase the current text line from the nominal cursor position
 33079    (inclusive) to pixel column TO_X (exclusive).  The idea is that
 33080    everything from TO_X onward is already erased.
 33081 
 33082    TO_X is a pixel position relative to UPDATED_AREA of currently
 33083    updated window W.  TO_X == -1 means clear to the end of this area.  */
 33084 
 33085 void
 33086 gui_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
 33087                        enum glyph_row_area updated_area, int to_x)
 33088 {
 33089   struct frame *f;
 33090   int max_x, min_y, max_y;
 33091   int from_x, from_y, to_y;
 33092   struct face *face;
 33093 
 33094   eassert (updated_row);
 33095   f = XFRAME (w->frame);
 33096   face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
 33097 
 33098   if (updated_row->full_width_p)
 33099     max_x = (WINDOW_PIXEL_WIDTH (w)
 33100              - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 33101   else
 33102     max_x = window_box_width (w, updated_area);
 33103   max_y = window_text_bottom_y (w);
 33104 
 33105   /* TO_X == 0 means don't do anything.  TO_X < 0 means clear to end
 33106      of window.  For TO_X > 0, truncate to end of drawing area.  */
 33107   if (to_x == 0)
 33108     return;
 33109   else if (to_x < 0)
 33110     to_x = max_x;
 33111   else
 33112     to_x = min (to_x, max_x);
 33113 
 33114   to_y = min (max_y, w->output_cursor.y + updated_row->height);
 33115 
 33116   /* Notice if the cursor will be cleared by this operation.  */
 33117   if (!updated_row->full_width_p)
 33118     notice_overwritten_cursor (w, updated_area,
 33119                                w->output_cursor.x, -1,
 33120                                updated_row->y,
 33121                                MATRIX_ROW_BOTTOM_Y (updated_row));
 33122 
 33123   from_x = w->output_cursor.x;
 33124 
 33125   /* Translate to frame coordinates.  */
 33126   if (updated_row->full_width_p)
 33127     {
 33128       from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
 33129       to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
 33130     }
 33131   else
 33132     {
 33133       int area_left = window_box_left (w, updated_area);
 33134       from_x += area_left;
 33135       to_x += area_left;
 33136     }
 33137 
 33138   min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 33139   from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
 33140   to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
 33141 
 33142   /* Prevent inadvertently clearing to end of the X window.  */
 33143   if (to_x > from_x && to_y > from_y)
 33144     {
 33145       block_input ();
 33146       FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
 33147                                        to_x - from_x, to_y - from_y);
 33148 
 33149       if (face && !updated_row->stipple_p)
 33150         updated_row->stipple_p = face->stipple;
 33151       unblock_input ();
 33152     }
 33153 }
 33154 
 33155 #endif /* HAVE_WINDOW_SYSTEM */
 33156 
 33157 
 33158 
 33159 /***********************************************************************
 33160                              Cursor types
 33161  ***********************************************************************/
 33162 
 33163 /* Value is the internal representation of the specified cursor type
 33164    ARG.  If type is BAR_CURSOR, return in *WIDTH the specified width
 33165    of the bar cursor.  */
 33166 
 33167 static enum text_cursor_kinds
 33168 get_specified_cursor_type (Lisp_Object arg, int *width)
 33169 {
 33170   enum text_cursor_kinds type;
 33171 
 33172   if (NILP (arg))
 33173     return NO_CURSOR;
 33174 
 33175   if (EQ (arg, Qbox))
 33176     return FILLED_BOX_CURSOR;
 33177 
 33178   if (EQ (arg, Qhollow))
 33179     return HOLLOW_BOX_CURSOR;
 33180 
 33181   if (EQ (arg, Qbar))
 33182     {
 33183       *width = 2;
 33184       return BAR_CURSOR;
 33185     }
 33186 
 33187   if (EQ (arg, Qhbar))
 33188     {
 33189       *width = 2;
 33190       return HBAR_CURSOR;
 33191     }
 33192 
 33193   if (CONSP (arg)
 33194       && RANGED_FIXNUMP (0, XCDR (arg), INT_MAX))
 33195     {
 33196       *width = XFIXNUM (XCDR (arg));
 33197 
 33198       if (EQ (XCAR (arg), Qbox))
 33199           return FILLED_BOX_CURSOR;
 33200       else if (EQ (XCAR (arg), Qbar))
 33201           return BAR_CURSOR;
 33202       else if (EQ (XCAR (arg), Qhbar))
 33203           return HBAR_CURSOR;
 33204     }
 33205 
 33206   /* Treat anything unknown as "hollow box cursor".
 33207      It was bad to signal an error; people have trouble fixing
 33208      .Xdefaults with Emacs, when it has something bad in it.  */
 33209   type = HOLLOW_BOX_CURSOR;
 33210 
 33211   return type;
 33212 }
 33213 
 33214 /* Set the default cursor types for specified frame.  */
 33215 void
 33216 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
 33217 {
 33218   int width = 1;
 33219   Lisp_Object tem;
 33220 
 33221   FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
 33222   FRAME_CURSOR_WIDTH (f) = width;
 33223 
 33224   /* By default, set up the blink-off state depending on the on-state.  */
 33225 
 33226   tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
 33227   if (!NILP (tem))
 33228     {
 33229       FRAME_BLINK_OFF_CURSOR (f)
 33230         = get_specified_cursor_type (XCDR (tem), &width);
 33231       FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
 33232     }
 33233   else
 33234     FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
 33235 
 33236   /* Make sure the cursor gets redrawn.  */
 33237   f->cursor_type_changed = true;
 33238 }
 33239 
 33240 
 33241 #ifdef HAVE_WINDOW_SYSTEM
 33242 
 33243 /* Return the cursor we want to be displayed in window W.  Return
 33244    width of bar/hbar cursor through WIDTH arg.  Return with
 33245    ACTIVE_CURSOR arg set to true if cursor in window W is `active'
 33246    (i.e. if the `system caret' should track this cursor).
 33247 
 33248    In a mini-buffer window, we want the cursor only to appear if we
 33249    are reading input from this window.  For the selected window, we
 33250    want the cursor type given by the frame parameter or buffer local
 33251    setting of cursor-type.  If explicitly marked off, draw no cursor.
 33252    In all other cases, we want a hollow box cursor.  */
 33253 
 33254 static enum text_cursor_kinds
 33255 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 33256                         bool *active_cursor)
 33257 {
 33258   struct frame *f = XFRAME (w->frame);
 33259   struct buffer *b = XBUFFER (w->contents);
 33260   int cursor_type = DEFAULT_CURSOR;
 33261   Lisp_Object alt_cursor;
 33262   bool non_selected = false;
 33263 
 33264   *active_cursor = true;
 33265 
 33266   /* Echo area */
 33267   if (cursor_in_echo_area
 33268       && FRAME_HAS_MINIBUF_P (f)
 33269       && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
 33270     {
 33271       if (w == XWINDOW (echo_area_window))
 33272         {
 33273           if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
 33274             {
 33275               *width = FRAME_CURSOR_WIDTH (f);
 33276               return FRAME_DESIRED_CURSOR (f);
 33277             }
 33278           else
 33279             return get_specified_cursor_type (BVAR (b, cursor_type), width);
 33280         }
 33281 
 33282       *active_cursor = false;
 33283       non_selected = true;
 33284     }
 33285 
 33286   /* Detect a nonselected window or nonselected frame.  */
 33287   else if (w != XWINDOW (f->selected_window)
 33288            || f != FRAME_DISPLAY_INFO (f)->highlight_frame)
 33289     {
 33290       *active_cursor = false;
 33291 
 33292       if (MINI_WINDOW_P (w) &&
 33293           (minibuf_level == 0
 33294            || is_minibuffer (0, w->contents)))
 33295         return NO_CURSOR;
 33296 
 33297       non_selected = true;
 33298     }
 33299 
 33300   /* Never display a cursor in a window in which cursor-type is nil.  */
 33301   if (NILP (BVAR (b, cursor_type)))
 33302     return NO_CURSOR;
 33303 
 33304   /* Get the normal cursor type for this window.  */
 33305   if (EQ (BVAR (b, cursor_type), Qt))
 33306     {
 33307       cursor_type = FRAME_DESIRED_CURSOR (f);
 33308       *width = FRAME_CURSOR_WIDTH (f);
 33309     }
 33310   else
 33311     cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
 33312 
 33313   /* Use cursor-in-non-selected-windows instead
 33314      for non-selected window or frame.  */
 33315   if (non_selected)
 33316     {
 33317       alt_cursor = BVAR (b, cursor_in_non_selected_windows);
 33318       if (!EQ (Qt, alt_cursor))
 33319         return get_specified_cursor_type (alt_cursor, width);
 33320       /* t means modify the normal cursor type.  */
 33321       if (cursor_type == FILLED_BOX_CURSOR)
 33322         cursor_type = HOLLOW_BOX_CURSOR;
 33323       else if (cursor_type == BAR_CURSOR && *width > 1)
 33324         --*width;
 33325       return cursor_type;
 33326     }
 33327 
 33328   /* Use normal cursor if not blinked off.  */
 33329   if (!w->cursor_off_p)
 33330     {
 33331       if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
 33332         return NO_CURSOR;
 33333       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 33334         {
 33335           if (cursor_type == FILLED_BOX_CURSOR)
 33336             {
 33337               /* Using a block cursor on large images can be very
 33338                  annoying.  So use a hollow cursor for "large" images.
 33339                  If image is not transparent (no mask), also use
 33340                  hollow cursor.  */
 33341               struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 33342               if (img != NULL && IMAGEP (img->spec))
 33343                 {
 33344                   /* Interpret "large" as >SIZExSIZE and >NxN where
 33345                      SIZE is the value from cursor-type of the form
 33346                      (box . SIZE), where N = size of default frame
 33347                      font size.  So, setting cursor-type to (box . 32)
 33348                      should cover most of the "tiny" icons people may
 33349                      use.  */
 33350                   if (!img->mask
 33351                       || (CONSP (BVAR (b, cursor_type))
 33352                           && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 33353                           && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 33354                     cursor_type = HOLLOW_BOX_CURSOR;
 33355                 }
 33356             }
 33357           else if (cursor_type != NO_CURSOR)
 33358             {
 33359               /* Display current only supports BOX and HOLLOW cursors for images.
 33360                  So for now, unconditionally use a HOLLOW cursor when cursor is
 33361                  not a solid box cursor.  */
 33362               cursor_type = HOLLOW_BOX_CURSOR;
 33363             }
 33364       }
 33365       return cursor_type;
 33366     }
 33367 
 33368   /* Cursor is blinked off, so determine how to "toggle" it.  */
 33369 
 33370   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
 33371   if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
 33372     return get_specified_cursor_type (XCDR (alt_cursor), width);
 33373 
 33374   /* Then see if frame has specified a specific blink off cursor type.  */
 33375   if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
 33376     {
 33377       *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
 33378       return FRAME_BLINK_OFF_CURSOR (f);
 33379     }
 33380 
 33381 #if false
 33382   /* Some people liked having a permanently visible blinking cursor,
 33383      while others had very strong opinions against it.  So it was
 33384      decided to remove it.  KFS 2003-09-03 */
 33385 
 33386   /* Finally perform built-in cursor blinking:
 33387        filled box      <->   hollow box
 33388        wide [h]bar     <->   narrow [h]bar
 33389        narrow [h]bar   <->   no cursor
 33390        other type      <->   no cursor  */
 33391 
 33392   if (cursor_type == FILLED_BOX_CURSOR)
 33393     return HOLLOW_BOX_CURSOR;
 33394 
 33395   if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
 33396     {
 33397       *width = 1;
 33398       return cursor_type;
 33399     }
 33400 #endif
 33401 
 33402   return NO_CURSOR;
 33403 }
 33404 
 33405 
 33406 /* Notice when the text cursor of window W has been completely
 33407    overwritten by a drawing operation that outputs glyphs in AREA
 33408    starting at X0 and ending at X1 in the line starting at Y0 and
 33409    ending at Y1.  X coordinates are area-relative.  X1 < 0 means all
 33410    the rest of the line after X0 has been written.  Y coordinates
 33411    are window-relative.  */
 33412 
 33413 static void
 33414 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
 33415                            int x0, int x1, int y0, int y1)
 33416 {
 33417   int cx0, cx1, cy0, cy1;
 33418   struct glyph_row *row;
 33419 
 33420   if (!w->phys_cursor_on_p)
 33421     return;
 33422   if (area != TEXT_AREA)
 33423     return;
 33424 
 33425   if (w->phys_cursor.vpos < 0
 33426       || w->phys_cursor.vpos >= w->current_matrix->nrows
 33427       || (row = w->current_matrix->rows + w->phys_cursor.vpos,
 33428           !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
 33429     return;
 33430 
 33431   if (row->cursor_in_fringe_p)
 33432     {
 33433       row->cursor_in_fringe_p = false;
 33434       draw_fringe_bitmap (w, row, row->reversed_p);
 33435       w->phys_cursor_on_p = false;
 33436       return;
 33437     }
 33438 
 33439   cx0 = w->phys_cursor.x;
 33440   cx1 = cx0 + w->phys_cursor_width;
 33441   if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
 33442     return;
 33443 
 33444   /* The cursor image will be completely removed from the
 33445      screen if the output area intersects the cursor area in
 33446      y-direction.  When we draw in [y0 y1[, and some part of
 33447      the cursor is at y < y0, that part must have been drawn
 33448      before.  When scrolling, the cursor is erased before
 33449      actually scrolling, so we don't come here.  When not
 33450      scrolling, the rows above the old cursor row must have
 33451      changed, and in this case these rows must have written
 33452      over the cursor image.
 33453 
 33454      Likewise if part of the cursor is below y1, with the
 33455      exception of the cursor being in the first blank row at
 33456      the buffer and window end because update_text_area
 33457      doesn't draw that row.  (Except when it does, but
 33458      that's handled in update_text_area.)  */
 33459 
 33460   cy0 = w->phys_cursor.y;
 33461   cy1 = cy0 + w->phys_cursor_height;
 33462   if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
 33463     return;
 33464 
 33465   w->phys_cursor_on_p = false;
 33466 }
 33467 
 33468 #endif /* HAVE_WINDOW_SYSTEM */
 33469 
 33470 
 33471 /************************************************************************
 33472                               Mouse Face
 33473  ************************************************************************/
 33474 
 33475 #ifdef HAVE_WINDOW_SYSTEM
 33476 
 33477 /* EXPORT for RIF:
 33478    Fix the display of area AREA of overlapping row ROW in window W
 33479    with respect to the overlapping part OVERLAPS.  */
 33480 
 33481 void
 33482 gui_fix_overlapping_area (struct window *w, struct glyph_row *row,
 33483                         enum glyph_row_area area, int overlaps)
 33484 {
 33485   int i, x;
 33486 
 33487   block_input ();
 33488 
 33489   x = 0;
 33490   for (i = 0; i < row->used[area];)
 33491     {
 33492       if (row->glyphs[area][i].overlaps_vertically_p)
 33493         {
 33494           int start = i, start_x = x;
 33495 
 33496           do
 33497             {
 33498               x += row->glyphs[area][i].pixel_width;
 33499               ++i;
 33500             }
 33501           while (i < row->used[area]
 33502                  && row->glyphs[area][i].overlaps_vertically_p);
 33503 
 33504           draw_glyphs (w, start_x, row, area,
 33505                        start, i,
 33506                        DRAW_NORMAL_TEXT, overlaps);
 33507         }
 33508       else
 33509         {
 33510           x += row->glyphs[area][i].pixel_width;
 33511           ++i;
 33512         }
 33513     }
 33514 
 33515   unblock_input ();
 33516 }
 33517 
 33518 
 33519 /* EXPORT:
 33520    Draw the cursor glyph of window W in glyph row ROW.  See the
 33521    comment of draw_glyphs for the meaning of HL.  */
 33522 
 33523 void
 33524 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
 33525                         enum draw_glyphs_face hl)
 33526 {
 33527   /* If cursor hpos is out of bounds, don't draw garbage.  This can
 33528      happen in mini-buffer windows when switching between echo area
 33529      glyphs and mini-buffer.  */
 33530   if ((row->reversed_p
 33531        ? (w->phys_cursor.hpos >= 0)
 33532        : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
 33533     {
 33534       bool on_p = w->phys_cursor_on_p;
 33535       int x1;
 33536       int hpos = w->phys_cursor.hpos;
 33537 
 33538       /* When the window is hscrolled, cursor hpos can legitimately be
 33539          out of bounds, but we draw the cursor at the corresponding
 33540          window margin in that case.  */
 33541       if (!row->reversed_p && hpos < 0)
 33542         hpos = 0;
 33543       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33544         hpos = row->used[TEXT_AREA] - 1;
 33545 
 33546       x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
 33547                         hl, 0);
 33548       w->phys_cursor_on_p = on_p;
 33549 
 33550       if (hl == DRAW_CURSOR)
 33551         w->phys_cursor_width = x1 - w->phys_cursor.x;
 33552       /* When we erase the cursor, and ROW is overlapped by other
 33553          rows, make sure that these overlapping parts of other rows
 33554          are redrawn.  */
 33555       else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
 33556         {
 33557           w->phys_cursor_width = x1 - w->phys_cursor.x;
 33558 
 33559           if (row > w->current_matrix->rows
 33560               && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
 33561             gui_fix_overlapping_area (w, row - 1, TEXT_AREA,
 33562                                     OVERLAPS_ERASED_CURSOR);
 33563 
 33564           if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
 33565               && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
 33566             gui_fix_overlapping_area (w, row + 1, TEXT_AREA,
 33567                                     OVERLAPS_ERASED_CURSOR);
 33568         }
 33569     }
 33570 }
 33571 
 33572 
 33573 /* Erase the image of a cursor of window W from the screen.  */
 33574 
 33575 void
 33576 erase_phys_cursor (struct window *w)
 33577 {
 33578   struct frame *f = XFRAME (w->frame);
 33579   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 33580   int hpos = w->phys_cursor.hpos;
 33581   int vpos = w->phys_cursor.vpos;
 33582   bool mouse_face_here_p = false;
 33583   struct glyph_matrix *active_glyphs = w->current_matrix;
 33584   struct glyph_row *cursor_row;
 33585   struct glyph *cursor_glyph;
 33586   enum draw_glyphs_face hl;
 33587 
 33588   /* No cursor displayed or row invalidated => nothing to do on the
 33589      screen.  */
 33590   if (w->phys_cursor_type == NO_CURSOR)
 33591     goto mark_cursor_off;
 33592 
 33593   /* VPOS >= active_glyphs->nrows means that window has been resized.
 33594      Don't bother to erase the cursor.  */
 33595   if (vpos >= active_glyphs->nrows)
 33596     goto mark_cursor_off;
 33597 
 33598   /* If row containing cursor is marked invalid, there is nothing we
 33599      can do.  */
 33600   cursor_row = MATRIX_ROW (active_glyphs, vpos);
 33601   if (!cursor_row->enabled_p)
 33602     goto mark_cursor_off;
 33603 
 33604   /* If line spacing is > 0, old cursor may only be partially visible in
 33605      window after split-window.  So adjust visible height.  */
 33606   cursor_row->visible_height = min (cursor_row->visible_height,
 33607                                     window_text_bottom_y (w) - cursor_row->y);
 33608 
 33609   /* If row is completely invisible, don't attempt to delete a cursor which
 33610      isn't there.  This can happen if cursor is at top of a window, and
 33611      we switch to a buffer with a header line in that window.  */
 33612   if (cursor_row->visible_height <= 0)
 33613     goto mark_cursor_off;
 33614 
 33615   /* If cursor is in the fringe, erase by drawing actual bitmap there.  */
 33616   if (cursor_row->cursor_in_fringe_p)
 33617     {
 33618       cursor_row->cursor_in_fringe_p = false;
 33619       draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
 33620       goto mark_cursor_off;
 33621     }
 33622 
 33623   /* This can happen when the new row is shorter than the old one.
 33624      In this case, either draw_glyphs or clear_end_of_line
 33625      should have cleared the cursor.  Note that we wouldn't be
 33626      able to erase the cursor in this case because we don't have a
 33627      cursor glyph at hand.  */
 33628   if ((cursor_row->reversed_p
 33629        ? (w->phys_cursor.hpos < 0)
 33630        : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
 33631     goto mark_cursor_off;
 33632 
 33633   /* When the window is hscrolled, cursor hpos can legitimately be out
 33634      of bounds, but we draw the cursor at the corresponding window
 33635      margin in that case.  */
 33636   if (!cursor_row->reversed_p && hpos < 0)
 33637     hpos = 0;
 33638   if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
 33639     hpos = cursor_row->used[TEXT_AREA] - 1;
 33640 
 33641   /* If the cursor is in the mouse face area, redisplay that when
 33642      we clear the cursor.  */
 33643   if (! NILP (hlinfo->mouse_face_window)
 33644       && coords_in_mouse_face_p (w, hpos, vpos)
 33645       /* Don't redraw the cursor's spot in mouse face if it is at the
 33646          end of a line (on a newline).  The cursor appears there, but
 33647          mouse highlighting does not.  */
 33648       && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
 33649     mouse_face_here_p = true;
 33650 
 33651 #ifdef HAVE_WINDOW_SYSTEM
 33652   /* Since erasing the phys cursor will probably lead to corruption of
 33653      the mouse face display if the glyph's pixel_width is not kept up
 33654      to date with the :box property of the mouse face, just redraw the
 33655      mouse face.  */
 33656   if (FRAME_WINDOW_P (WINDOW_XFRAME (w)) && mouse_face_here_p)
 33657     {
 33658       w->phys_cursor_on_p = false;
 33659       w->phys_cursor_type = NO_CURSOR;
 33660       show_mouse_face (MOUSE_HL_INFO (WINDOW_XFRAME (w)), DRAW_MOUSE_FACE);
 33661       return;
 33662     }
 33663 #endif
 33664 
 33665   /* Maybe clear the display under the cursor.  */
 33666   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
 33667     {
 33668       int x, y;
 33669       int tab_line_height = WINDOW_TAB_LINE_HEIGHT (w);
 33670       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
 33671       int width;
 33672 
 33673       cursor_glyph = get_phys_cursor_glyph (w);
 33674       if (cursor_glyph == NULL)
 33675         goto mark_cursor_off;
 33676 
 33677       width = cursor_glyph->pixel_width;
 33678       x = w->phys_cursor.x;
 33679       if (x < 0)
 33680         {
 33681           width += x;
 33682           x = 0;
 33683         }
 33684       width = min (width, window_box_width (w, TEXT_AREA) - x);
 33685       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (tab_line_height, max (header_line_height, cursor_row->y)));
 33686       x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 33687 
 33688       if (width > 0)
 33689         FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
 33690     }
 33691 
 33692   /* Erase the cursor by redrawing the character underneath it.  */
 33693   if (mouse_face_here_p)
 33694     hl = DRAW_MOUSE_FACE;
 33695   else
 33696     hl = DRAW_NORMAL_TEXT;
 33697   draw_phys_cursor_glyph (w, cursor_row, hl);
 33698 
 33699  mark_cursor_off:
 33700   w->phys_cursor_on_p = false;
 33701   w->phys_cursor_type = NO_CURSOR;
 33702 }
 33703 
 33704 
 33705 /* Display or clear cursor of window W.  If !ON, clear the cursor.
 33706    If ON, display the cursor; where to put the cursor is specified by
 33707    HPOS, VPOS, X and Y.  */
 33708 
 33709 void
 33710 display_and_set_cursor (struct window *w, bool on,
 33711                         int hpos, int vpos, int x, int y)
 33712 {
 33713   struct frame *f = XFRAME (w->frame);
 33714   int new_cursor_type;
 33715   int new_cursor_width UNINIT;
 33716   bool active_cursor;
 33717   struct glyph_row *glyph_row;
 33718   struct glyph *glyph;
 33719 
 33720   /* This is pointless on invisible frames, and dangerous on garbaged
 33721      windows and frames; in the latter case, the frame or window may
 33722      be in the midst of changing its size, and x and y may be off the
 33723      window.  */
 33724   if (! FRAME_REDISPLAY_P (f)
 33725       || vpos >= w->current_matrix->nrows
 33726       || hpos >= w->current_matrix->matrix_w)
 33727     return;
 33728 
 33729   /* If cursor is off and we want it off, return quickly.  */
 33730   if (!on && !w->phys_cursor_on_p)
 33731     return;
 33732 
 33733   glyph_row = MATRIX_ROW (w->current_matrix, vpos);
 33734   /* If cursor row is not enabled, we don't really know where to
 33735      display the cursor.  */
 33736   if (!glyph_row->enabled_p)
 33737     {
 33738       w->phys_cursor_on_p = false;
 33739       return;
 33740     }
 33741 
 33742   /* A frame might be marked garbaged even though its cursor position
 33743      is correct, and will not change upon subsequent redisplay.  This
 33744      happens in some rare situations, like toggling the sort order in
 33745      Dired windows.  We've already established that VPOS is valid, so
 33746      it shouldn't do any harm to record the cursor position, as we are
 33747      going to return without acting on it anyway.  Otherwise, expose
 33748      events might come in and call update_window_cursor, which will
 33749      blindly use outdated values in w->phys_cursor.  */
 33750   if (FRAME_GARBAGED_P (f))
 33751     {
 33752       if (on)
 33753         {
 33754           w->phys_cursor.x = x;
 33755           w->phys_cursor.y = glyph_row->y;
 33756           w->phys_cursor.hpos = hpos;
 33757           w->phys_cursor.vpos = vpos;
 33758         }
 33759       return;
 33760     }
 33761 
 33762   glyph = NULL;
 33763   if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
 33764     glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
 33765 
 33766   eassert (input_blocked_p ());
 33767 
 33768   /* Set new_cursor_type to the cursor we want to be displayed.  */
 33769   new_cursor_type = get_window_cursor_type (w, glyph,
 33770                                             &new_cursor_width, &active_cursor);
 33771 
 33772   /* If cursor is currently being shown and we don't want it to be or
 33773      it is in the wrong place, or the cursor type is not what we want,
 33774      erase it.  */
 33775   if (w->phys_cursor_on_p
 33776       && (!on
 33777           || w->phys_cursor.x != x
 33778           || w->phys_cursor.y != y
 33779           /* HPOS can be negative in R2L rows whose
 33780              exact_window_width_line_p flag is set (i.e. their newline
 33781              would "overflow into the fringe").  */
 33782           || hpos < 0
 33783           || new_cursor_type != w->phys_cursor_type
 33784           || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
 33785               && new_cursor_width != w->phys_cursor_width)))
 33786     erase_phys_cursor (w);
 33787 
 33788   /* Don't check phys_cursor_on_p here because that flag is only set
 33789      to false in some cases where we know that the cursor has been
 33790      completely erased, to avoid the extra work of erasing the cursor
 33791      twice.  In other words, phys_cursor_on_p can be true and the cursor
 33792      still not be visible, or it has only been partly erased.  */
 33793   if (on)
 33794     {
 33795       w->phys_cursor_ascent = glyph_row->ascent;
 33796       w->phys_cursor_height = glyph_row->height;
 33797 
 33798       /* Set phys_cursor_.* before x_draw_.* is called because some
 33799          of them may need the information.  */
 33800       w->phys_cursor.x = x;
 33801       w->phys_cursor.y = glyph_row->y;
 33802       w->phys_cursor.hpos = hpos;
 33803       w->phys_cursor.vpos = vpos;
 33804     }
 33805 
 33806   FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
 33807                                      new_cursor_type, new_cursor_width,
 33808                                      on, active_cursor);
 33809 }
 33810 
 33811 
 33812 /* Switch the display of W's cursor on or off, according to the value
 33813    of ON.  */
 33814 
 33815 static void
 33816 update_window_cursor (struct window *w, bool on)
 33817 {
 33818   /* Don't update cursor in windows whose frame is in the process
 33819      of being deleted.  */
 33820   if (w->current_matrix)
 33821     {
 33822       int hpos = w->phys_cursor.hpos;
 33823       int vpos = w->phys_cursor.vpos;
 33824       struct glyph_row *row;
 33825 
 33826       if (vpos >= w->current_matrix->nrows
 33827           || hpos >= w->current_matrix->matrix_w)
 33828         return;
 33829 
 33830       row = MATRIX_ROW (w->current_matrix, vpos);
 33831 
 33832       /* When the window is hscrolled, cursor hpos can legitimately be
 33833          out of bounds, but we draw the cursor at the corresponding
 33834          window margin in that case.  */
 33835       if (!row->reversed_p && hpos < 0)
 33836         hpos = 0;
 33837       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33838         hpos = row->used[TEXT_AREA] - 1;
 33839 
 33840       block_input ();
 33841       display_and_set_cursor (w, on, hpos, vpos,
 33842                               w->phys_cursor.x, w->phys_cursor.y);
 33843       unblock_input ();
 33844     }
 33845 }
 33846 
 33847 
 33848 /* Call update_window_cursor with parameter ON_P on all leaf windows
 33849    in the window tree rooted at W.  */
 33850 
 33851 static void
 33852 update_cursor_in_window_tree (struct window *w, bool on_p)
 33853 {
 33854   while (w)
 33855     {
 33856       if (WINDOWP (w->contents))
 33857         update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
 33858       else
 33859         update_window_cursor (w, on_p);
 33860 
 33861       w = NILP (w->next) ? 0 : XWINDOW (w->next);
 33862     }
 33863 }
 33864 
 33865 
 33866 /* EXPORT:
 33867    Display the cursor on window W, or clear it, according to ON_P.
 33868    Don't change the cursor's position.  */
 33869 
 33870 void
 33871 gui_update_cursor (struct frame *f, bool on_p)
 33872 {
 33873   update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
 33874 }
 33875 
 33876 
 33877 /* EXPORT:
 33878    Clear the cursor of window W to background color, and mark the
 33879    cursor as not shown.  This is used when the text where the cursor
 33880    is about to be rewritten.  */
 33881 
 33882 void
 33883 gui_clear_cursor (struct window *w)
 33884 {
 33885   if (FRAME_REDISPLAY_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
 33886     update_window_cursor (w, false);
 33887 }
 33888 
 33889 #endif /* HAVE_WINDOW_SYSTEM */
 33890 
 33891 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
 33892    and MSDOS.  */
 33893 static void
 33894 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
 33895                           int start_hpos, int end_hpos,
 33896                           enum draw_glyphs_face draw)
 33897 {
 33898 #ifdef HAVE_WINDOW_SYSTEM
 33899   if (FRAME_WINDOW_P (XFRAME (w->frame)))
 33900     {
 33901       draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
 33902       return;
 33903     }
 33904 #endif
 33905 
 33906 #ifndef HAVE_ANDROID
 33907   tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
 33908 #endif
 33909 }
 33910 
 33911 /* Display the active region described by mouse_face_* according to DRAW.  */
 33912 
 33913 static void
 33914 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 33915 {
 33916   /* Don't bother doing anything if the mouse-face window is not set
 33917      up.  */
 33918   if (!WINDOWP (hlinfo->mouse_face_window))
 33919     return;
 33920 
 33921   struct window *w = XWINDOW (hlinfo->mouse_face_window);
 33922   struct frame *f = XFRAME (WINDOW_FRAME (w));
 33923 
 33924   /* Don't bother doing anything if we are on a wrong frame.  */
 33925   if (f != hlinfo->mouse_face_mouse_frame)
 33926     return;
 33927 
 33928   if (/* If window is in the process of being destroyed, don't bother
 33929          to do anything.  */
 33930       w->current_matrix != NULL
 33931       /* Don't update mouse highlight if hidden.  */
 33932       && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
 33933       /* Recognize when we are called to operate on rows that don't exist
 33934          anymore.  This can happen when a window is split.  */
 33935       && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
 33936     {
 33937       bool phys_cursor_on_p = w->phys_cursor_on_p;
 33938 #ifdef HAVE_WINDOW_SYSTEM
 33939       int mouse_off = 0;
 33940 #endif
 33941       struct glyph_row *row, *first, *last;
 33942 
 33943       first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
 33944       last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
 33945 
 33946       for (row = first; row <= last && row->enabled_p; ++row)
 33947         {
 33948           int start_hpos, end_hpos, start_x;
 33949 
 33950           /* For all but the first row, the highlight starts at column 0.  */
 33951           if (row == first)
 33952             {
 33953               /* R2L rows have BEG and END in reversed order, but the
 33954                  screen drawing geometry is always left to right.  So
 33955                  we need to mirror the beginning and end of the
 33956                  highlighted area in R2L rows.  */
 33957               if (!row->reversed_p)
 33958                 {
 33959                   start_hpos = hlinfo->mouse_face_beg_col;
 33960                   start_x = hlinfo->mouse_face_beg_x;
 33961                 }
 33962               else if (row == last)
 33963                 {
 33964                   start_hpos = hlinfo->mouse_face_end_col;
 33965                   start_x = hlinfo->mouse_face_end_x;
 33966                 }
 33967               else
 33968                 {
 33969                   start_hpos = 0;
 33970                   start_x = 0;
 33971                 }
 33972             }
 33973           else if (row->reversed_p && row == last)
 33974             {
 33975               start_hpos = hlinfo->mouse_face_end_col;
 33976               start_x = hlinfo->mouse_face_end_x;
 33977             }
 33978           else
 33979             {
 33980               start_hpos = 0;
 33981               start_x = 0;
 33982             }
 33983 
 33984           if (row == last)
 33985             {
 33986               if (!row->reversed_p)
 33987                 end_hpos = hlinfo->mouse_face_end_col;
 33988               else if (row == first)
 33989                 end_hpos = hlinfo->mouse_face_beg_col;
 33990               else
 33991                 {
 33992                   end_hpos = row->used[TEXT_AREA];
 33993                   if (draw == DRAW_NORMAL_TEXT)
 33994                     row->fill_line_p = true; /* Clear to end of line.  */
 33995                 }
 33996             }
 33997           else if (row->reversed_p && row == first)
 33998             end_hpos = hlinfo->mouse_face_beg_col;
 33999           else
 34000             {
 34001               end_hpos = row->used[TEXT_AREA];
 34002               if (draw == DRAW_NORMAL_TEXT)
 34003                 row->fill_line_p = true; /* Clear to end of line.  */
 34004             }
 34005 
 34006           if (end_hpos > start_hpos)
 34007             {
 34008               draw_row_with_mouse_face (w, start_x, row,
 34009                                         start_hpos, end_hpos, draw);
 34010 
 34011               row->mouse_face_p
 34012                 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
 34013             }
 34014 #ifdef HAVE_WINDOW_SYSTEM
 34015           /* Compute the cursor offset due to mouse-highlight.  */
 34016           if ((MATRIX_ROW_VPOS (row, w->current_matrix) == w->phys_cursor.vpos)
 34017               /* But not when highlighting a pseudo window, such as
 34018                  the toolbar, which can't have a cursor anyway.  */
 34019               && !w->pseudo_window_p
 34020               && draw == DRAW_MOUSE_FACE)
 34021             get_cursor_offset_for_mouse_face (w, row, &mouse_off);
 34022 #endif
 34023         }
 34024 
 34025       /* When we've written over the cursor, arrange for it to
 34026          be displayed again.  */
 34027       if (FRAME_WINDOW_P (f)
 34028           && phys_cursor_on_p && !w->phys_cursor_on_p)
 34029         {
 34030 #ifdef HAVE_WINDOW_SYSTEM
 34031           int hpos = w->phys_cursor.hpos;
 34032           int old_phys_cursor_x = w->phys_cursor.x;
 34033 
 34034           /* When the window is hscrolled, cursor hpos can legitimately be
 34035              out of bounds, but we draw the cursor at the corresponding
 34036              window margin in that case.  */
 34037           if (!row->reversed_p && hpos < 0)
 34038             hpos = 0;
 34039           if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 34040             hpos = row->used[TEXT_AREA] - 1;
 34041 
 34042           block_input ();
 34043           display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
 34044                                   w->phys_cursor.x + mouse_off,
 34045                                   w->phys_cursor.y);
 34046           /* Restore the original cursor coordinates, perhaps modified
 34047              to account for mouse-highlight.  */
 34048           w->phys_cursor.x = old_phys_cursor_x;
 34049           unblock_input ();
 34050 #endif  /* HAVE_WINDOW_SYSTEM */
 34051         }
 34052     }
 34053 
 34054 #ifdef HAVE_WINDOW_SYSTEM
 34055   /* Change the mouse cursor.  */
 34056   if (FRAME_WINDOW_P (f) && NILP (track_mouse))
 34057     {
 34058       if (draw == DRAW_NORMAL_TEXT
 34059 #ifndef HAVE_EXT_TOOL_BAR
 34060           && !EQ (hlinfo->mouse_face_window, f->tool_bar_window)
 34061 #endif
 34062           && !EQ (hlinfo->mouse_face_window, f->tab_bar_window))
 34063         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->text_cursor);
 34064       else
 34065       if (draw == DRAW_MOUSE_FACE)
 34066         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->hand_cursor);
 34067       else
 34068         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->nontext_cursor);
 34069     }
 34070 #endif  /* HAVE_WINDOW_SYSTEM */
 34071 }
 34072 
 34073 /* EXPORT:
 34074    Clear out the mouse-highlighted active region.
 34075    Redraw it un-highlighted first.  Value is true if mouse
 34076    face was actually drawn unhighlighted.  */
 34077 
 34078 bool
 34079 clear_mouse_face (Mouse_HLInfo *hlinfo)
 34080 {
 34081   bool cleared
 34082     = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
 34083   if (cleared)
 34084     show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
 34085   hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
 34086   hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
 34087   hlinfo->mouse_face_window = Qnil;
 34088   hlinfo->mouse_face_overlay = Qnil;
 34089   return cleared;
 34090 }
 34091 
 34092 /* Return true if the coordinates HPOS and VPOS on windows W are
 34093    within the mouse face on that window.  */
 34094 static bool
 34095 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
 34096 {
 34097   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 34098 
 34099   /* Quickly resolve the easy cases.  */
 34100   if (!(WINDOWP (hlinfo->mouse_face_window)
 34101         && XWINDOW (hlinfo->mouse_face_window) == w))
 34102     return false;
 34103   if (vpos < hlinfo->mouse_face_beg_row
 34104       || vpos > hlinfo->mouse_face_end_row)
 34105     return false;
 34106   if (vpos > hlinfo->mouse_face_beg_row
 34107       && vpos < hlinfo->mouse_face_end_row)
 34108     return true;
 34109 
 34110   if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
 34111     {
 34112       if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 34113         {
 34114           if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
 34115             return true;
 34116         }
 34117       else if ((vpos == hlinfo->mouse_face_beg_row
 34118                 && hpos >= hlinfo->mouse_face_beg_col)
 34119                || (vpos == hlinfo->mouse_face_end_row
 34120                    && hpos < hlinfo->mouse_face_end_col))
 34121         return true;
 34122     }
 34123   else
 34124     {
 34125        if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 34126         {
 34127           if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
 34128             return true;
 34129         }
 34130       else if ((vpos == hlinfo->mouse_face_beg_row
 34131                 && hpos <= hlinfo->mouse_face_beg_col)
 34132                || (vpos == hlinfo->mouse_face_end_row
 34133                    && hpos > hlinfo->mouse_face_end_col))
 34134         return true;
 34135     }
 34136   return false;
 34137 }
 34138 
 34139 
 34140 /* EXPORT:
 34141    True if physical cursor of window W is within mouse face.  */
 34142 
 34143 bool
 34144 cursor_in_mouse_face_p (struct window *w)
 34145 {
 34146   int vpos = w->phys_cursor.vpos;
 34147 
 34148   /* If the cursor is outside the matrix glyph rows, it cannot be
 34149      within the mouse face.  */
 34150   if (!(0 <= vpos && vpos < w->current_matrix->nrows))
 34151     return false;
 34152 
 34153   int hpos = w->phys_cursor.hpos;
 34154   struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
 34155 
 34156   /* When the window is hscrolled, cursor hpos can legitimately be out
 34157      of bounds, but we draw the cursor at the corresponding window
 34158      margin in that case.  */
 34159   if (!row->reversed_p && hpos < 0)
 34160     hpos = 0;
 34161   if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 34162     hpos = row->used[TEXT_AREA] - 1;
 34163 
 34164   return coords_in_mouse_face_p (w, hpos, vpos);
 34165 }
 34166 
 34167 
 34168 
 34169 /* Find the glyph rows START_ROW and END_ROW of window W that display
 34170    characters between buffer positions START_CHARPOS and END_CHARPOS
 34171    (excluding END_CHARPOS).  DISP_STRING is a display string that
 34172    covers these buffer positions.  This is similar to
 34173    row_containing_pos, but is more accurate when bidi reordering makes
 34174    buffer positions change non-linearly with glyph rows.  */
 34175 static void
 34176 rows_from_pos_range (struct window *w,
 34177                      ptrdiff_t start_charpos, ptrdiff_t end_charpos,
 34178                      Lisp_Object disp_string,
 34179                      struct glyph_row **start, struct glyph_row **end)
 34180 {
 34181   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34182   int last_y = window_text_bottom_y (w);
 34183   struct glyph_row *row;
 34184 
 34185   *start = NULL;
 34186   *end = NULL;
 34187 
 34188   while (!first->enabled_p
 34189          && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
 34190     first++;
 34191 
 34192   /* Find the START row.  */
 34193   for (row = first;
 34194        row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
 34195        row++)
 34196     {
 34197       /* A row can potentially be the START row if the range of the
 34198          characters it displays intersects the range
 34199          [START_CHARPOS..END_CHARPOS).  */
 34200       if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
 34201               && end_charpos < MATRIX_ROW_START_CHARPOS (row))
 34202              /* See the commentary in row_containing_pos, for the
 34203                 explanation of the complicated way to check whether
 34204                 some position is beyond the end of the characters
 34205                 displayed by a row.  */
 34206              || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
 34207                   || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
 34208                       && !row->ends_at_zv_p
 34209                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 34210                  && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
 34211                      || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
 34212                          && !row->ends_at_zv_p
 34213                          && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
 34214         {
 34215           /* Found a candidate row.  Now make sure at least one of the
 34216              glyphs it displays has a charpos from the range
 34217              [START_CHARPOS..END_CHARPOS).
 34218 
 34219              This is not obvious because bidi reordering could make
 34220              buffer positions of a row be 1,2,3,102,101,100, and if we
 34221              want to highlight characters in [50..60), we don't want
 34222              this row, even though [50..60) does intersect [1..103),
 34223              the range of character positions given by the row's start
 34224              and end positions.  */
 34225           struct glyph *g = row->glyphs[TEXT_AREA];
 34226           struct glyph *e = g + row->used[TEXT_AREA];
 34227 
 34228           while (g < e)
 34229             {
 34230               if (((BUFFERP (g->object) || NILP (g->object))
 34231                    && start_charpos <= g->charpos && g->charpos < end_charpos)
 34232                   /* A glyph that comes from DISP_STRING is by
 34233                      definition to be highlighted.  */
 34234                   || EQ (g->object, disp_string))
 34235                 *start = row;
 34236               g++;
 34237             }
 34238           if (*start)
 34239             break;
 34240         }
 34241     }
 34242 
 34243   /* Find the END row.  */
 34244   if (!*start
 34245       /* If the last row is partially visible, start looking for END
 34246          from that row, instead of starting from FIRST.  */
 34247       && !(row->enabled_p
 34248            && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
 34249     row = first;
 34250   for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
 34251     {
 34252       struct glyph_row *next = row + 1;
 34253       ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
 34254 
 34255       if (!next->enabled_p
 34256           || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
 34257           /* The first row >= START whose range of displayed characters
 34258              does NOT intersect the range [START_CHARPOS..END_CHARPOS]
 34259              is the row END + 1.  */
 34260           || (start_charpos < next_start
 34261               && end_charpos < next_start)
 34262           || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
 34263                || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
 34264                    && !next->ends_at_zv_p
 34265                    && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
 34266               && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
 34267                   || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
 34268                       && !next->ends_at_zv_p
 34269                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
 34270         {
 34271           *end = row;
 34272           break;
 34273         }
 34274       else
 34275         {
 34276           /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
 34277              but none of the characters it displays are in the range, it is
 34278              also END + 1. */
 34279           struct glyph *g = next->glyphs[TEXT_AREA];
 34280           struct glyph *s = g;
 34281           struct glyph *e = g + next->used[TEXT_AREA];
 34282 
 34283           while (g < e)
 34284             {
 34285               if (((BUFFERP (g->object) || NILP (g->object))
 34286                    && ((start_charpos <= g->charpos && g->charpos < end_charpos)
 34287                        /* If the buffer position of the first glyph in
 34288                           the row is equal to END_CHARPOS, it means
 34289                           the last character to be highlighted is the
 34290                           newline of ROW, and we must consider NEXT as
 34291                           END, not END+1.  */
 34292                        || (((!next->reversed_p && g == s)
 34293                             || (next->reversed_p && g == e - 1))
 34294                            && (g->charpos == end_charpos
 34295                                /* Special case for when NEXT is an
 34296                                   empty line at ZV.  */
 34297                                || (g->charpos == -1
 34298                                    && !row->ends_at_zv_p
 34299                                    && next_start == end_charpos)))))
 34300                   /* A glyph that comes from DISP_STRING is by
 34301                      definition to be highlighted.  */
 34302                   || EQ (g->object, disp_string))
 34303                 break;
 34304               g++;
 34305             }
 34306           if (g == e)
 34307             {
 34308               *end = row;
 34309               break;
 34310             }
 34311           /* The first row that ends at ZV must be the last to be
 34312              highlighted.  */
 34313           else if (next->ends_at_zv_p)
 34314             {
 34315               *end = next;
 34316               break;
 34317             }
 34318         }
 34319     }
 34320 }
 34321 
 34322 /* This function sets the mouse_face_* elements of HLINFO, assuming
 34323    the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
 34324    window WINDOW.  START_CHARPOS and END_CHARPOS are buffer positions
 34325    for the overlay or run of text properties specifying the mouse
 34326    face.  BEFORE_STRING and AFTER_STRING, if non-nil, are a
 34327    before-string and after-string that must also be highlighted.
 34328    DISP_STRING, if non-nil, is a display string that may cover some
 34329    or all of the highlighted text.  */
 34330 
 34331 static void
 34332 mouse_face_from_buffer_pos (Lisp_Object window,
 34333                             Mouse_HLInfo *hlinfo,
 34334                             ptrdiff_t mouse_charpos,
 34335                             ptrdiff_t start_charpos,
 34336                             ptrdiff_t end_charpos,
 34337                             Lisp_Object before_string,
 34338                             Lisp_Object after_string,
 34339                             Lisp_Object disp_string)
 34340 {
 34341   struct window *w = XWINDOW (window);
 34342   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34343   struct glyph_row *r1, *r2;
 34344   struct glyph *glyph, *end;
 34345   ptrdiff_t ignore, pos;
 34346   int x;
 34347 
 34348   eassert (NILP (disp_string) || STRINGP (disp_string));
 34349   eassert (NILP (before_string) || STRINGP (before_string));
 34350   eassert (NILP (after_string) || STRINGP (after_string));
 34351 
 34352   /* Find the rows corresponding to START_CHARPOS and END_CHARPOS.  */
 34353   rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
 34354   if (r1 == NULL)
 34355     r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34356   /* If the before-string or display-string contains newlines,
 34357      rows_from_pos_range skips to its last row.  Move back.  */
 34358   if (!NILP (before_string) || !NILP (disp_string))
 34359     {
 34360       struct glyph_row *prev;
 34361       while ((prev = r1 - 1, prev >= first)
 34362              && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
 34363              && prev->used[TEXT_AREA] > 0)
 34364         {
 34365           struct glyph *beg = prev->glyphs[TEXT_AREA];
 34366           glyph = beg + prev->used[TEXT_AREA];
 34367           while (--glyph >= beg && NILP (glyph->object));
 34368           if (glyph < beg
 34369               || !(EQ (glyph->object, before_string)
 34370                    || EQ (glyph->object, disp_string)))
 34371             break;
 34372           r1 = prev;
 34373         }
 34374     }
 34375   if (r2 == NULL)
 34376     {
 34377       r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34378       hlinfo->mouse_face_past_end = true;
 34379     }
 34380   else if (!NILP (after_string))
 34381     {
 34382       /* If the after-string has newlines, advance to its last row.  */
 34383       struct glyph_row *next;
 34384       struct glyph_row *last
 34385         = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34386 
 34387       for (next = r2 + 1;
 34388            next <= last
 34389              && next->used[TEXT_AREA] > 0
 34390              && EQ (next->glyphs[TEXT_AREA]->object, after_string);
 34391            ++next)
 34392         r2 = next;
 34393     }
 34394   /* The rest of the display engine assumes that mouse_face_beg_row is
 34395      either above mouse_face_end_row or identical to it.  But with
 34396      bidi-reordered continued lines, the row for START_CHARPOS could
 34397      be below the row for END_CHARPOS.  If so, swap the rows and store
 34398      them in correct order.  */
 34399   if (r1->y > r2->y)
 34400     {
 34401       struct glyph_row *tem = r2;
 34402 
 34403       r2 = r1;
 34404       r1 = tem;
 34405     }
 34406 
 34407   hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
 34408   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
 34409 
 34410   /* For a bidi-reordered row, the positions of BEFORE_STRING,
 34411      AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
 34412      could be anywhere in the row and in any order.  The strategy
 34413      below is to find the leftmost and the rightmost glyph that
 34414      belongs to either of these 3 strings, or whose position is
 34415      between START_CHARPOS and END_CHARPOS, and highlight all the
 34416      glyphs between those two.  This may cover more than just the text
 34417      between START_CHARPOS and END_CHARPOS if the range of characters
 34418      strides the bidi level boundary, e.g. if the beginning is in R2L
 34419      text while the end is in L2R text or vice versa.  */
 34420   if (!r1->reversed_p)
 34421     {
 34422       /* This row is in a left to right paragraph.  Scan it left to
 34423          right.  */
 34424       glyph = r1->glyphs[TEXT_AREA];
 34425       end = glyph + r1->used[TEXT_AREA];
 34426       x = r1->x;
 34427 
 34428       /* Skip truncation glyphs at the start of the glyph row.  */
 34429       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34430         for (; glyph < end
 34431                && NILP (glyph->object)
 34432                && glyph->charpos < 0;
 34433              ++glyph)
 34434           x += glyph->pixel_width;
 34435 
 34436       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34437          or DISP_STRING, and the first glyph from buffer whose
 34438          position is between START_CHARPOS and END_CHARPOS.  */
 34439       for (; glyph < end
 34440              && !NILP (glyph->object)
 34441              && !EQ (glyph->object, disp_string)
 34442              && !(BUFFERP (glyph->object)
 34443                   && (glyph->charpos >= start_charpos
 34444                       && glyph->charpos < end_charpos));
 34445            ++glyph)
 34446         {
 34447           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34448              are present at buffer positions between START_CHARPOS and
 34449              END_CHARPOS, or if they come from an overlay.  */
 34450           if (EQ (glyph->object, before_string))
 34451             {
 34452               pos = string_buffer_position (before_string,
 34453                                             start_charpos);
 34454               /* If pos == 0, it means before_string came from an
 34455                  overlay, not from a buffer position.  */
 34456               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34457                 break;
 34458             }
 34459           else if (EQ (glyph->object, after_string))
 34460             {
 34461               pos = string_buffer_position (after_string, end_charpos);
 34462               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34463                 break;
 34464             }
 34465           x += glyph->pixel_width;
 34466         }
 34467       hlinfo->mouse_face_beg_x = x;
 34468       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34469     }
 34470   else
 34471     {
 34472       /* This row is in a right to left paragraph.  Scan it right to
 34473          left.  */
 34474       struct glyph *g;
 34475 
 34476       end = r1->glyphs[TEXT_AREA] - 1;
 34477       glyph = end + r1->used[TEXT_AREA];
 34478 
 34479       /* Skip truncation glyphs at the start of the glyph row.  */
 34480       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34481         for (; glyph > end
 34482                && NILP (glyph->object)
 34483                && glyph->charpos < 0;
 34484              --glyph)
 34485           ;
 34486 
 34487       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34488          or DISP_STRING, and the first glyph from buffer whose
 34489          position is between START_CHARPOS and END_CHARPOS.  */
 34490       for (; glyph > end
 34491              && !NILP (glyph->object)
 34492              && !EQ (glyph->object, disp_string)
 34493              && !(BUFFERP (glyph->object)
 34494                   && (glyph->charpos >= start_charpos
 34495                       && glyph->charpos < end_charpos));
 34496            --glyph)
 34497         {
 34498           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34499              are present at buffer positions between START_CHARPOS and
 34500              END_CHARPOS, or if they come from an overlay.  */
 34501           if (EQ (glyph->object, before_string))
 34502             {
 34503               pos = string_buffer_position (before_string, start_charpos);
 34504               /* If pos == 0, it means before_string came from an
 34505                  overlay, not from a buffer position.  */
 34506               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34507                 break;
 34508             }
 34509           else if (EQ (glyph->object, after_string))
 34510             {
 34511               pos = string_buffer_position (after_string, end_charpos);
 34512               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34513                 break;
 34514             }
 34515         }
 34516 
 34517       glyph++; /* first glyph to the right of the highlighted area */
 34518       for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
 34519         x += g->pixel_width;
 34520       hlinfo->mouse_face_beg_x = x;
 34521       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34522     }
 34523 
 34524   /* If the highlight ends in a different row, compute GLYPH and END
 34525      for the end row.  Otherwise, reuse the values computed above for
 34526      the row where the highlight begins.  */
 34527   if (r2 != r1)
 34528     {
 34529       if (!r2->reversed_p)
 34530         {
 34531           glyph = r2->glyphs[TEXT_AREA];
 34532           end = glyph + r2->used[TEXT_AREA];
 34533           x = r2->x;
 34534         }
 34535       else
 34536         {
 34537           end = r2->glyphs[TEXT_AREA] - 1;
 34538           glyph = end + r2->used[TEXT_AREA];
 34539         }
 34540     }
 34541 
 34542   if (!r2->reversed_p)
 34543     {
 34544       /* Skip truncation and continuation glyphs near the end of the
 34545          row, and also blanks and stretch glyphs inserted by
 34546          extend_face_to_end_of_line.  */
 34547       while (end > glyph
 34548              && NILP ((end - 1)->object))
 34549         --end;
 34550       /* Scan the rest of the glyph row from the end, looking for the
 34551          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34552          DISP_STRING, or whose position is between START_CHARPOS
 34553          and END_CHARPOS */
 34554       for (--end;
 34555              end > glyph
 34556              && !NILP (end->object)
 34557              && !EQ (end->object, disp_string)
 34558              && !(BUFFERP (end->object)
 34559                   && (end->charpos >= start_charpos
 34560                       && end->charpos < end_charpos));
 34561            --end)
 34562         {
 34563           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34564              are present at buffer positions between START_CHARPOS and
 34565              END_CHARPOS, or if they come from an overlay.  */
 34566           if (EQ (end->object, before_string))
 34567             {
 34568               pos = string_buffer_position (before_string, start_charpos);
 34569               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34570                 break;
 34571             }
 34572           else if (EQ (end->object, after_string))
 34573             {
 34574               pos = string_buffer_position (after_string, end_charpos);
 34575               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34576                 break;
 34577             }
 34578         }
 34579       /* Find the X coordinate of the last glyph to be highlighted.  */
 34580       for (; glyph <= end; ++glyph)
 34581         x += glyph->pixel_width;
 34582 
 34583       hlinfo->mouse_face_end_x = x;
 34584       hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
 34585     }
 34586   else
 34587     {
 34588       /* Skip truncation and continuation glyphs near the end of the
 34589          row, and also blanks and stretch glyphs inserted by
 34590          extend_face_to_end_of_line.  */
 34591       x = r2->x;
 34592       end++;
 34593       while (end < glyph
 34594              && NILP (end->object))
 34595         {
 34596           x += end->pixel_width;
 34597           ++end;
 34598         }
 34599       /* Scan the rest of the glyph row from the end, looking for the
 34600          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34601          DISP_STRING, or whose position is between START_CHARPOS
 34602          and END_CHARPOS */
 34603       for ( ;
 34604              end < glyph
 34605              && !NILP (end->object)
 34606              && !EQ (end->object, disp_string)
 34607              && !(BUFFERP (end->object)
 34608                   && (end->charpos >= start_charpos
 34609                       && end->charpos < end_charpos));
 34610            ++end)
 34611         {
 34612           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34613              are present at buffer positions between START_CHARPOS and
 34614              END_CHARPOS, or if they come from an overlay.  */
 34615           if (EQ (end->object, before_string))
 34616             {
 34617               pos = string_buffer_position (before_string, start_charpos);
 34618               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34619                 break;
 34620             }
 34621           else if (EQ (end->object, after_string))
 34622             {
 34623               pos = string_buffer_position (after_string, end_charpos);
 34624               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34625                 break;
 34626             }
 34627           x += end->pixel_width;
 34628         }
 34629       /* If we exited the above loop because we arrived at the last
 34630          glyph of the row, and its buffer position is still not in
 34631          range, it means the last character in range is the preceding
 34632          newline.  Bump the end column and x values to get past the
 34633          last glyph.  */
 34634       if (end == glyph
 34635           && BUFFERP (end->object)
 34636           && (end->charpos < start_charpos
 34637               || end->charpos >= end_charpos))
 34638         {
 34639           x += end->pixel_width;
 34640           ++end;
 34641         }
 34642       hlinfo->mouse_face_end_x = x;
 34643       hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
 34644     }
 34645 
 34646   hlinfo->mouse_face_window = window;
 34647   hlinfo->mouse_face_face_id
 34648     = face_at_buffer_position (w, mouse_charpos, &ignore,
 34649                                mouse_charpos + 1,
 34650                                !hlinfo->mouse_face_hidden, -1, 0);
 34651   show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 34652 }
 34653 
 34654 /* The following function is not used anymore (replaced with
 34655    mouse_face_from_string_pos), but I leave it here for the time
 34656    being, in case someone would.  */
 34657 
 34658 #if false       /* not used */
 34659 
 34660 /* Find the position of the glyph for position POS in OBJECT in
 34661    window W's current matrix, and return in *X, *Y the pixel
 34662    coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
 34663 
 34664    RIGHT_P means return the position of the right edge of the glyph.
 34665    !RIGHT_P means return the left edge position.
 34666 
 34667    If no glyph for POS exists in the matrix, return the position of
 34668    the glyph with the next smaller position that is in the matrix, if
 34669    RIGHT_P is false.  If RIGHT_P, and no glyph for POS
 34670    exists in the matrix, return the position of the glyph with the
 34671    next larger position in OBJECT.
 34672 
 34673    Value is true if a glyph was found.  */
 34674 
 34675 static bool
 34676 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
 34677                       int *hpos, int *vpos, int *x, int *y, bool right_p)
 34678 {
 34679   int yb = window_text_bottom_y (w);
 34680   struct glyph_row *r;
 34681   struct glyph *best_glyph = NULL;
 34682   struct glyph_row *best_row = NULL;
 34683   int best_x = 0;
 34684 
 34685   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34686        r->enabled_p && r->y < yb;
 34687        ++r)
 34688     {
 34689       struct glyph *g = r->glyphs[TEXT_AREA];
 34690       struct glyph *e = g + r->used[TEXT_AREA];
 34691       int gx;
 34692 
 34693       for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34694         if (EQ (g->object, object))
 34695           {
 34696             if (g->charpos == pos)
 34697               {
 34698                 best_glyph = g;
 34699                 best_x = gx;
 34700                 best_row = r;
 34701                 goto found;
 34702               }
 34703             else if (best_glyph == NULL
 34704                      || ((eabs (g->charpos - pos)
 34705                          < eabs (best_glyph->charpos - pos))
 34706                          && (right_p
 34707                              ? g->charpos < pos
 34708                              : g->charpos > pos)))
 34709               {
 34710                 best_glyph = g;
 34711                 best_x = gx;
 34712                 best_row = r;
 34713               }
 34714           }
 34715     }
 34716 
 34717  found:
 34718 
 34719   if (best_glyph)
 34720     {
 34721       *x = best_x;
 34722       *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
 34723 
 34724       if (right_p)
 34725         {
 34726           *x += best_glyph->pixel_width;
 34727           ++*hpos;
 34728         }
 34729 
 34730       *y = best_row->y;
 34731       *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
 34732     }
 34733 
 34734   return best_glyph != NULL;
 34735 }
 34736 #endif  /* not used */
 34737 
 34738 /* Find the positions of the first and the last glyphs in window W's
 34739    current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
 34740    (assumed to be a string), and return in HLINFO's mouse_face_*
 34741    members the pixel and column/row coordinates of those glyphs.  */
 34742 
 34743 static void
 34744 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
 34745                             Lisp_Object object,
 34746                             ptrdiff_t startpos, ptrdiff_t endpos)
 34747 {
 34748   int yb = window_text_bottom_y (w);
 34749   struct glyph_row *r;
 34750   struct glyph *g, *e;
 34751   int gx;
 34752   bool found = false;
 34753 
 34754   /* Find the glyph row with at least one position in the range
 34755      [STARTPOS..ENDPOS), and the first glyph in that row whose
 34756      position belongs to that range.  */
 34757   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34758        r->enabled_p && r->y < yb;
 34759        ++r)
 34760     {
 34761       if (!r->reversed_p)
 34762         {
 34763           g = r->glyphs[TEXT_AREA];
 34764           e = g + r->used[TEXT_AREA];
 34765           for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34766             if (EQ (g->object, object)
 34767                 && startpos <= g->charpos && g->charpos < endpos)
 34768               {
 34769                 hlinfo->mouse_face_beg_row
 34770                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34771                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34772                 hlinfo->mouse_face_beg_x = gx;
 34773                 found = true;
 34774                 break;
 34775               }
 34776         }
 34777       else
 34778         {
 34779           struct glyph *g1;
 34780 
 34781           e = r->glyphs[TEXT_AREA];
 34782           g = e + r->used[TEXT_AREA];
 34783           for ( ; g > e; --g)
 34784             if (EQ ((g-1)->object, object)
 34785                 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
 34786               {
 34787                 hlinfo->mouse_face_beg_row
 34788                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34789                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34790                 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
 34791                   gx += g1->pixel_width;
 34792                 hlinfo->mouse_face_beg_x = gx;
 34793                 found = true;
 34794                 break;
 34795               }
 34796         }
 34797       if (found)
 34798         break;
 34799     }
 34800 
 34801   if (!found)
 34802     return;
 34803 
 34804   /* Starting with the next row, look for the first row which does NOT
 34805      include any glyphs whose positions are in the range.  */
 34806   for (++r; r->enabled_p && r->y < yb; ++r)
 34807     {
 34808       g = r->glyphs[TEXT_AREA];
 34809       e = g + r->used[TEXT_AREA];
 34810       found = false;
 34811       for ( ; g < e; ++g)
 34812         if (EQ (g->object, object)
 34813             && startpos <= g->charpos && g->charpos < endpos)
 34814           {
 34815             found = true;
 34816             break;
 34817           }
 34818       if (!found)
 34819         break;
 34820     }
 34821 
 34822   /* The highlighted region ends on the previous row.  */
 34823   r--;
 34824 
 34825   /* Set the end row.  */
 34826   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
 34827 
 34828   /* Compute and set the end column and the end column's horizontal
 34829      pixel coordinate.  */
 34830   if (!r->reversed_p)
 34831     {
 34832       g = r->glyphs[TEXT_AREA];
 34833       e = g + r->used[TEXT_AREA];
 34834       for ( ; e > g; --e)
 34835         if (EQ ((e-1)->object, object)
 34836             && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
 34837           break;
 34838       hlinfo->mouse_face_end_col = e - g;
 34839 
 34840       for (gx = r->x; g < e; ++g)
 34841         gx += g->pixel_width;
 34842       hlinfo->mouse_face_end_x = gx;
 34843     }
 34844   else
 34845     {
 34846       e = r->glyphs[TEXT_AREA];
 34847       g = e + r->used[TEXT_AREA];
 34848       for (gx = r->x ; e < g; ++e)
 34849         {
 34850           if (EQ (e->object, object)
 34851               && startpos <= e->charpos && e->charpos < endpos)
 34852             break;
 34853           gx += e->pixel_width;
 34854         }
 34855       hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
 34856       hlinfo->mouse_face_end_x = gx;
 34857     }
 34858 }
 34859 
 34860 #ifdef HAVE_WINDOW_SYSTEM
 34861 
 34862 /* See if position X, Y is within a hot-spot of an image.  */
 34863 
 34864 static bool
 34865 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
 34866 {
 34867   if (!CONSP (hot_spot))
 34868     return false;
 34869 
 34870   if (EQ (XCAR (hot_spot), Qrect))
 34871     {
 34872       /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1))  */
 34873       Lisp_Object rect = XCDR (hot_spot);
 34874       Lisp_Object tem;
 34875       if (!CONSP (rect))
 34876         return false;
 34877       if (!CONSP (XCAR (rect)))
 34878         return false;
 34879       if (!CONSP (XCDR (rect)))
 34880         return false;
 34881       if (!(tem = XCAR (XCAR (rect)), FIXNUMP (tem) && x >= XFIXNUM (tem)))
 34882         return false;
 34883       if (!(tem = XCDR (XCAR (rect)), FIXNUMP (tem) && y >= XFIXNUM (tem)))
 34884         return false;
 34885       if (!(tem = XCAR (XCDR (rect)), FIXNUMP (tem) && x <= XFIXNUM (tem)))
 34886         return false;
 34887       if (!(tem = XCDR (XCDR (rect)), FIXNUMP (tem) && y <= XFIXNUM (tem)))
 34888         return false;
 34889       return true;
 34890     }
 34891   else if (EQ (XCAR (hot_spot), Qcircle))
 34892     {
 34893       /* CDR is (Center . Radius) = ((x0 . y0) . r) */
 34894       Lisp_Object circ = XCDR (hot_spot);
 34895       Lisp_Object lr, lx0, ly0;
 34896       if (CONSP (circ)
 34897           && CONSP (XCAR (circ))
 34898           && (lr = XCDR (circ), NUMBERP (lr))
 34899           && (lx0 = XCAR (XCAR (circ)), FIXNUMP (lx0))
 34900           && (ly0 = XCDR (XCAR (circ)), FIXNUMP (ly0)))
 34901         {
 34902           double r = XFLOATINT (lr);
 34903           double dx = XFIXNUM (lx0) - x;
 34904           double dy = XFIXNUM (ly0) - y;
 34905           return (dx * dx + dy * dy <= r * r);
 34906         }
 34907     }
 34908   else if (EQ (XCAR (hot_spot), Qpoly))
 34909     {
 34910       /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
 34911       if (VECTORP (XCDR (hot_spot)))
 34912         {
 34913           struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
 34914           Lisp_Object *poly = v->contents;
 34915           ptrdiff_t n = v->header.size;
 34916           ptrdiff_t i;
 34917           bool inside = false;
 34918           Lisp_Object lx, ly;
 34919           int x0, y0;
 34920 
 34921           /* Need an even number of coordinates, and at least 3 edges.  */
 34922           if (n < 6 || n & 1)
 34923             return false;
 34924 
 34925           /* Count edge segments intersecting line from (X,Y) to (X,infinity).
 34926              If count is odd, we are inside polygon.  Pixels on edges
 34927              may or may not be included depending on actual geometry of the
 34928              polygon.  */
 34929           if ((lx = poly[n-2], !FIXNUMP (lx))
 34930               || (ly = poly[n-1], !FIXNUMP (lx)))
 34931             return false;
 34932           x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34933           for (i = 0; i < n; i += 2)
 34934             {
 34935               int x1 = x0, y1 = y0;
 34936               if ((lx = poly[i], !FIXNUMP (lx))
 34937                   || (ly = poly[i+1], !FIXNUMP (ly)))
 34938                 return false;
 34939               x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34940 
 34941               /* Does this segment cross the X line?  */
 34942               if (x0 >= x)
 34943                 {
 34944                   if (x1 >= x)
 34945                     continue;
 34946                 }
 34947               else if (x1 < x)
 34948                 continue;
 34949               if (y > y0 && y > y1)
 34950                 continue;
 34951               if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
 34952                 inside = !inside;
 34953             }
 34954           return inside;
 34955         }
 34956     }
 34957   return false;
 34958 }
 34959 
 34960 Lisp_Object
 34961 find_hot_spot (Lisp_Object map, int x, int y)
 34962 {
 34963   while (CONSP (map))
 34964     {
 34965       if (CONSP (XCAR (map))
 34966           && on_hot_spot_p (XCAR (XCAR (map)), x, y))
 34967         return XCAR (map);
 34968       map = XCDR (map);
 34969     }
 34970 
 34971   return Qnil;
 34972 }
 34973 
 34974 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
 34975        3, 3, 0,
 34976        doc: /* Lookup in image map MAP coordinates X and Y.
 34977 An image map is an alist where each element has the format (AREA ID PLIST).
 34978 An AREA is specified as either a rectangle, a circle, or a polygon:
 34979 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
 34980 pixel coordinates of the upper left and bottom right corners.
 34981 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
 34982 and the radius of the circle; r may be a float or integer.
 34983 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
 34984 vector describes one corner in the polygon.
 34985 Returns the alist element for the first matching AREA in MAP.  */)
 34986   (Lisp_Object map, Lisp_Object x, Lisp_Object y)
 34987 {
 34988   if (NILP (map))
 34989     return Qnil;
 34990 
 34991   CHECK_FIXNUM (x);
 34992   CHECK_FIXNUM (y);
 34993 
 34994   return find_hot_spot (map,
 34995                         clip_to_bounds (INT_MIN, XFIXNUM (x), INT_MAX),
 34996                         clip_to_bounds (INT_MIN, XFIXNUM (y), INT_MAX));
 34997 }
 34998 #endif  /* HAVE_WINDOW_SYSTEM */
 34999 
 35000 
 35001 /* Display frame CURSOR, optionally using shape defined by POINTER.  */
 35002 static void
 35003 define_frame_cursor1 (struct frame *f, Emacs_Cursor cursor, Lisp_Object pointer)
 35004 {
 35005 #ifdef HAVE_WINDOW_SYSTEM
 35006   if (!FRAME_WINDOW_P (f))
 35007     return;
 35008 
 35009   /* Do not change cursor shape while dragging mouse.  */
 35010   if (EQ (track_mouse, Qdragging) || EQ (track_mouse, Qdropping)
 35011       || EQ (track_mouse, Qdrag_source))
 35012     return;
 35013 
 35014   if (!NILP (pointer))
 35015     {
 35016       if (EQ (pointer, Qarrow))
 35017         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35018       else if (EQ (pointer, Qhand))
 35019         cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35020       else if (EQ (pointer, Qtext))
 35021         cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35022       else if (EQ (pointer, intern ("hdrag")))
 35023         cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35024       else if (EQ (pointer, intern ("nhdrag")))
 35025         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35026 # ifdef HAVE_X_WINDOWS
 35027       else if (EQ (pointer, intern ("vdrag")))
 35028         cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
 35029 # endif
 35030       else if (EQ (pointer, intern ("hourglass")))
 35031         cursor = FRAME_OUTPUT_DATA (f)->hourglass_cursor;
 35032       else if (EQ (pointer, Qmodeline))
 35033         cursor = FRAME_OUTPUT_DATA (f)->modeline_cursor;
 35034       else
 35035         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35036     }
 35037 
 35038   if (cursor != No_Cursor)
 35039     FRAME_RIF (f)->define_frame_cursor (f, cursor);
 35040 #endif
 35041 }
 35042 
 35043 /* Take proper action when mouse has moved to the mode or header line
 35044    or marginal area AREA of window W, x-position X and y-position Y.
 35045    X is relative to the start of the text display area of W, so the
 35046    width of bitmap areas and scroll bars must be subtracted to get a
 35047    position relative to the start of the mode line.  */
 35048 
 35049 static void
 35050 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
 35051                                     enum window_part area)
 35052 {
 35053   struct window *w = XWINDOW (window);
 35054   struct frame *f = XFRAME (w->frame);
 35055   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35056   Emacs_Cursor cursor = No_Cursor;
 35057   Lisp_Object pointer = Qnil;
 35058   int dx, dy, width, height;
 35059   ptrdiff_t charpos;
 35060   Lisp_Object string, object = Qnil;
 35061   Lisp_Object pos UNINIT;
 35062   Lisp_Object mouse_face;
 35063   int original_x_pixel = x;
 35064   struct glyph * glyph = NULL, * row_start_glyph = NULL;
 35065   struct glyph_row *row UNINIT;
 35066 
 35067   if (area == ON_MODE_LINE || area == ON_HEADER_LINE || area == ON_TAB_LINE)
 35068     {
 35069       int x0;
 35070       struct glyph *end;
 35071 
 35072       /* Kludge alert: mode_line_string takes X/Y in pixels, but
 35073          returns them in row/column units!  */
 35074       string = mode_line_string (w, area, &x, &y, &charpos,
 35075                                  &object, &dx, &dy, &width, &height);
 35076 
 35077       row = (area == ON_MODE_LINE
 35078              ? MATRIX_MODE_LINE_ROW (w->current_matrix)
 35079              : (area == ON_TAB_LINE
 35080                 ? MATRIX_TAB_LINE_ROW (w->current_matrix)
 35081                 : MATRIX_HEADER_LINE_ROW (w->current_matrix)));
 35082 
 35083       /* Find the glyph under the mouse pointer.  */
 35084       if (row->mode_line_p && row->enabled_p)
 35085         {
 35086           glyph = row_start_glyph = row->glyphs[TEXT_AREA];
 35087           end = glyph + row->used[TEXT_AREA];
 35088 
 35089           for (x0 = original_x_pixel;
 35090                glyph < end && x0 >= glyph->pixel_width;
 35091                ++glyph)
 35092             x0 -= glyph->pixel_width;
 35093 
 35094           if (glyph >= end)
 35095             glyph = NULL;
 35096         }
 35097     }
 35098   else
 35099     {
 35100       x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
 35101       /* Kludge alert: marginal_area_string takes X/Y in pixels, but
 35102          returns them in row/column units!  */
 35103       string = marginal_area_string (w, area, &x, &y, &charpos,
 35104                                      &object, &dx, &dy, &width, &height);
 35105     }
 35106 
 35107   Lisp_Object help = Qnil;
 35108 
 35109 #ifdef HAVE_WINDOW_SYSTEM
 35110   if (IMAGEP (object))
 35111     {
 35112       Lisp_Object image_map, hotspot;
 35113       if ((image_map = plist_get (XCDR (object), QCmap),
 35114            !NILP (image_map))
 35115           && (hotspot = find_hot_spot (image_map, dx, dy),
 35116               CONSP (hotspot))
 35117           && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35118         {
 35119           Lisp_Object plist;
 35120 
 35121           /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
 35122              If so, we could look for mouse-enter, mouse-leave
 35123              properties in PLIST (and do something...).  */
 35124           hotspot = XCDR (hotspot);
 35125           if (CONSP (hotspot)
 35126               && (plist = XCAR (hotspot), CONSP (plist)))
 35127             {
 35128               pointer = plist_get (plist, Qpointer);
 35129               if (NILP (pointer))
 35130                 pointer = Qhand;
 35131               help = plist_get (plist, Qhelp_echo);
 35132               if (!NILP (help))
 35133                 {
 35134                   help_echo_string = help;
 35135                   XSETWINDOW (help_echo_window, w);
 35136                   help_echo_object = w->contents;
 35137                   help_echo_pos = charpos;
 35138                 }
 35139             }
 35140         }
 35141       if (NILP (pointer))
 35142         pointer = plist_get (XCDR (object), QCpointer);
 35143     }
 35144 #endif  /* HAVE_WINDOW_SYSTEM */
 35145 
 35146   /* CHARPOS can be beyond the last position of STRING due, e.g., to
 35147      min-width 'display' property.  Fix that, to let all the calls to
 35148      get-text-property below do their thing.  */
 35149   if (STRINGP (string))
 35150     pos = make_fixnum (min (charpos, SCHARS (string) - 1));
 35151 
 35152   /* Set the help text and mouse pointer.  If the mouse is on a part
 35153      of the mode line without any text (e.g. past the right edge of
 35154      the mode line text), use that windows's mode line help echo if it
 35155      has been set.  */
 35156   if (STRINGP (string) || area == ON_MODE_LINE || area == ON_HEADER_LINE
 35157       || area == ON_TAB_LINE)
 35158     {
 35159       /* Arrange to display the help by setting the global variables
 35160          help_echo_string, help_echo_object, and help_echo_pos.  */
 35161       if (NILP (help))
 35162         {
 35163           if (STRINGP (string))
 35164             help = Fget_text_property (pos, Qhelp_echo, string);
 35165 
 35166           if (!NILP (help))
 35167             {
 35168               help_echo_string = help;
 35169               XSETWINDOW (help_echo_window, w);
 35170               help_echo_object = string;
 35171               help_echo_pos = charpos;
 35172             }
 35173           else if (area == ON_MODE_LINE
 35174                    && !NILP (w->mode_line_help_echo))
 35175             {
 35176               help_echo_string =  w->mode_line_help_echo;
 35177               XSETWINDOW (help_echo_window, w);
 35178               help_echo_object = Qnil;
 35179               help_echo_pos = -1;
 35180             }
 35181         }
 35182 
 35183 #ifdef HAVE_WINDOW_SYSTEM
 35184       /* Change the mouse pointer according to what is under it.  */
 35185       if (FRAME_WINDOW_P (f))
 35186         {
 35187           bool draggable = (! WINDOW_BOTTOMMOST_P (w)
 35188                             || minibuf_level
 35189                             || NILP (Vresize_mini_windows));
 35190 
 35191           if (STRINGP (string))
 35192             {
 35193               cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35194 
 35195               if (NILP (pointer))
 35196                 pointer = Fget_text_property (pos, Qpointer, string);
 35197 
 35198               /* Change the mouse pointer according to what is under X/Y.  */
 35199               if (NILP (pointer)
 35200                   && (area == ON_MODE_LINE || area == ON_HEADER_LINE
 35201                       || area == ON_TAB_LINE))
 35202                 {
 35203                   Lisp_Object map;
 35204 
 35205                   map = Fget_text_property (pos, Qlocal_map, string);
 35206                   if (!KEYMAPP (map))
 35207                     map = Fget_text_property (pos, Qkeymap, string);
 35208                   if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
 35209                     cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35210                 }
 35211             }
 35212           else if (draggable && area == ON_MODE_LINE)
 35213             cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35214           else if ((area == ON_MODE_LINE
 35215                     && WINDOW_BOTTOMMOST_P (w)
 35216                     && !FRAME_HAS_MINIBUF_P (f)
 35217                     && !NILP (Fframe_parameter
 35218                               (w->frame, Qdrag_with_mode_line)))
 35219                    || (((area == ON_HEADER_LINE
 35220                          && !NILP (Fframe_parameter
 35221                                    (w->frame, Qdrag_with_header_line)))
 35222                         || (area == ON_TAB_LINE
 35223                             && !NILP (Fframe_parameter
 35224                                       (w->frame, Qdrag_with_tab_line))))
 35225                        && WINDOW_TOPMOST_P (w)))
 35226             cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35227           else
 35228             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35229         }
 35230 #endif
 35231     }
 35232 
 35233   /* Change the mouse face according to what is under X/Y.  */
 35234   bool mouse_face_shown = false;
 35235 
 35236   if (STRINGP (string))
 35237     {
 35238       mouse_face = Fget_text_property (pos, Qmouse_face, string);
 35239       if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
 35240           && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)
 35241               || (area == ON_TAB_LINE))
 35242           && glyph)
 35243         {
 35244           Lisp_Object b, e;
 35245 
 35246           struct glyph * tmp_glyph;
 35247 
 35248           int gpos;
 35249           int gseq_length;
 35250           int total_pixel_width;
 35251           ptrdiff_t begpos, endpos, ignore;
 35252 
 35253           int vpos, hpos;
 35254 
 35255           b = Fprevious_single_property_change (make_fixnum (charpos + 1),
 35256                                                 Qmouse_face, string, Qnil);
 35257           if (NILP (b))
 35258             begpos = 0;
 35259           else
 35260             begpos = XFIXNUM (b);
 35261 
 35262           e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
 35263           if (NILP (e))
 35264             endpos = SCHARS (string);
 35265           else
 35266             endpos = XFIXNUM (e);
 35267 
 35268           /* Calculate the glyph position GPOS of GLYPH in the
 35269              displayed string, relative to the beginning of the
 35270              highlighted part of the string.
 35271 
 35272              Note: GPOS is different from CHARPOS.  CHARPOS is the
 35273              position of GLYPH in the internal string object.  A mode
 35274              line string format has structures which are converted to
 35275              a flattened string by the Emacs Lisp interpreter.  The
 35276              internal string is an element of those structures.  The
 35277              displayed string is the flattened string.  */
 35278           tmp_glyph = row_start_glyph;
 35279           while (tmp_glyph < glyph
 35280                  && (!(EQ (tmp_glyph->object, glyph->object)
 35281                        && begpos <= tmp_glyph->charpos
 35282                        && tmp_glyph->charpos < endpos)))
 35283             tmp_glyph++;
 35284           gpos = glyph - tmp_glyph;
 35285 
 35286           /* Calculate the length GSEQ_LENGTH of the glyph sequence of
 35287              the highlighted part of the displayed string to which
 35288              GLYPH belongs.  Note: GSEQ_LENGTH is different from
 35289              SCHARS (STRING), because the latter returns the length of
 35290              the internal string.  */
 35291           for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 35292                tmp_glyph > glyph
 35293                  && (!(EQ (tmp_glyph->object, glyph->object)
 35294                        && begpos <= tmp_glyph->charpos
 35295                        && tmp_glyph->charpos < endpos));
 35296                tmp_glyph--)
 35297             ;
 35298           gseq_length = gpos + (tmp_glyph - glyph) + 1;
 35299 
 35300           /* Calculate the total pixel width of all the glyphs between
 35301              the beginning of the highlighted area and GLYPH.  */
 35302           total_pixel_width = 0;
 35303           for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
 35304             total_pixel_width += tmp_glyph->pixel_width;
 35305 
 35306           /* Pre calculation of re-rendering position.  Note: X is in
 35307              column units here, after the call to mode_line_string or
 35308              marginal_area_string.  */
 35309           hpos = x - gpos;
 35310           vpos = (area == ON_MODE_LINE
 35311                   ? (w->current_matrix)->nrows - 1
 35312                   : (area == ON_TAB_LINE
 35313                      ? 0
 35314                      : (w->current_matrix->tab_line_p
 35315                         ? 1
 35316                         : 0)));
 35317 
 35318           /* If GLYPH's position is included in the region that is
 35319              already drawn in mouse face, we have nothing to do.  */
 35320           if ( EQ (window, hlinfo->mouse_face_window)
 35321                && (!row->reversed_p
 35322                    ? (hlinfo->mouse_face_beg_col <= hpos
 35323                       && hpos < hlinfo->mouse_face_end_col)
 35324                    /* In R2L rows we swap BEG and END, see below.  */
 35325                    : (hlinfo->mouse_face_end_col <= hpos
 35326                       && hpos < hlinfo->mouse_face_beg_col))
 35327                && hlinfo->mouse_face_beg_row == vpos )
 35328             return;
 35329 
 35330           if (clear_mouse_face (hlinfo))
 35331             cursor = No_Cursor;
 35332 
 35333           if (!row->reversed_p)
 35334             {
 35335               hlinfo->mouse_face_beg_col = hpos;
 35336               hlinfo->mouse_face_beg_x   = original_x_pixel
 35337                                             - (total_pixel_width + dx);
 35338               hlinfo->mouse_face_end_col = hpos + gseq_length;
 35339               hlinfo->mouse_face_end_x   = 0;
 35340             }
 35341           else
 35342             {
 35343               /* In R2L rows, show_mouse_face expects BEG and END
 35344                  coordinates to be swapped.  */
 35345               hlinfo->mouse_face_end_col = hpos;
 35346               hlinfo->mouse_face_end_x   = original_x_pixel
 35347                                             - (total_pixel_width + dx);
 35348               hlinfo->mouse_face_beg_col = hpos + gseq_length;
 35349               hlinfo->mouse_face_beg_x   = 0;
 35350             }
 35351 
 35352           hlinfo->mouse_face_beg_row  = vpos;
 35353           hlinfo->mouse_face_end_row  = hlinfo->mouse_face_beg_row;
 35354           hlinfo->mouse_face_past_end = false;
 35355           hlinfo->mouse_face_window   = window;
 35356 
 35357           hlinfo->mouse_face_face_id =
 35358             face_at_string_position (w, string, charpos, 0, &ignore,
 35359                                      glyph->face_id, true, 0);
 35360 
 35361           show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35362           mouse_face_shown = true;
 35363 
 35364           if (NILP (pointer))
 35365             pointer = Qhand;
 35366         }
 35367     }
 35368 
 35369   /* If mouse-face doesn't need to be shown, clear any existing
 35370      mouse-face.  */
 35371   if ((area == ON_MODE_LINE || area == ON_HEADER_LINE
 35372        || area == ON_TAB_LINE) && !mouse_face_shown)
 35373     clear_mouse_face (hlinfo);
 35374 
 35375   define_frame_cursor1 (f, cursor, pointer);
 35376 }
 35377 
 35378 
 35379 /* EXPORT:
 35380    Take proper action when the mouse has moved to position X, Y on
 35381    frame F with regards to highlighting portions of display that have
 35382    mouse-face properties.  Also de-highlight portions of display where
 35383    the mouse was before, set the mouse pointer shape as appropriate
 35384    for the mouse coordinates, and activate help echo (tooltips).
 35385    X and Y can be negative or out of range.  */
 35386 
 35387 void
 35388 note_mouse_highlight (struct frame *f, int x, int y)
 35389 {
 35390   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35391   enum window_part part = ON_NOTHING;
 35392   Lisp_Object window;
 35393   struct window *w;
 35394   Emacs_Cursor cursor = No_Cursor;
 35395   Lisp_Object pointer = Qnil;  /* Takes precedence over cursor!  */
 35396   struct buffer *b;
 35397 
 35398   /* When a menu is active, don't highlight because this looks odd.  */
 35399 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS) || defined (MSDOS) \
 35400   || defined (HAVE_ANDROID)
 35401   if (popup_activated ())
 35402     return;
 35403 #endif
 35404 
 35405 #if defined (HAVE_HAIKU)
 35406   if (popup_activated_p)
 35407     return;
 35408 #endif
 35409 
 35410   if (!f->glyphs_initialized_p
 35411       || f->pointer_invisible)
 35412     return;
 35413 
 35414   hlinfo->mouse_face_mouse_x = x;
 35415   hlinfo->mouse_face_mouse_y = y;
 35416   hlinfo->mouse_face_mouse_frame = f;
 35417 
 35418   if (hlinfo->mouse_face_defer)
 35419     return;
 35420 
 35421   /* Which window is that in?  */
 35422   window = window_from_coordinates (f, x, y, &part, true, true);
 35423 
 35424   /* If displaying active text in another window, clear that.  */
 35425   if (! EQ (window, hlinfo->mouse_face_window)
 35426       /* Also clear if we move out of text area in same window.  */
 35427       || (!NILP (hlinfo->mouse_face_window)
 35428           && !NILP (window)
 35429           && part != ON_TEXT
 35430           && part != ON_MODE_LINE
 35431           && part != ON_HEADER_LINE
 35432           && part != ON_TAB_LINE))
 35433     clear_mouse_face (hlinfo);
 35434 
 35435   /* Reset help_echo_string.  It will get recomputed below.  */
 35436   help_echo_string = Qnil;
 35437 
 35438   /* Handle tab-bar highlight on mouse-capable TTY frames.  */
 35439   if (!FRAME_WINDOW_P (f)
 35440       && (y >= FRAME_MENU_BAR_LINES (f)
 35441           && y < FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f)))
 35442     {
 35443       int prop_idx;
 35444       bool ignore;
 35445       Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &ignore);
 35446 
 35447       if (!NILP (caption))
 35448         {
 35449           help_echo_object = help_echo_window = Qnil;
 35450           help_echo_pos = -1;
 35451           help_echo_string = AREF (f->tab_bar_items,
 35452                                    prop_idx * TAB_BAR_ITEM_NSLOTS
 35453                                    + TAB_BAR_ITEM_HELP);
 35454           if (NILP (help_echo_string))
 35455             help_echo_string = caption;
 35456         }
 35457     }
 35458 
 35459 #ifdef HAVE_WINDOW_SYSTEM
 35460   /* If the cursor is on the internal border of FRAME and FRAME's
 35461      internal border is draggable, provide some visual feedback.  */
 35462   if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
 35463       && !NILP (get_frame_param (f, Qdrag_internal_border)))
 35464     {
 35465       enum internal_border_part part = frame_internal_border_part (f, x, y);
 35466 
 35467       switch (part)
 35468         {
 35469         case INTERNAL_BORDER_NONE:
 35470           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35471             /* Reset cursor.  */
 35472             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35473           break;
 35474         case INTERNAL_BORDER_LEFT_EDGE:
 35475           cursor = FRAME_OUTPUT_DATA (f)->left_edge_cursor;
 35476           break;
 35477         case INTERNAL_BORDER_TOP_LEFT_CORNER:
 35478           cursor = FRAME_OUTPUT_DATA (f)->top_left_corner_cursor;
 35479           break;
 35480         case INTERNAL_BORDER_TOP_EDGE:
 35481           cursor = FRAME_OUTPUT_DATA (f)->top_edge_cursor;
 35482           break;
 35483         case INTERNAL_BORDER_TOP_RIGHT_CORNER:
 35484           cursor = FRAME_OUTPUT_DATA (f)->top_right_corner_cursor;
 35485           break;
 35486         case INTERNAL_BORDER_RIGHT_EDGE:
 35487           cursor = FRAME_OUTPUT_DATA (f)->right_edge_cursor;
 35488           break;
 35489         case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
 35490           cursor = FRAME_OUTPUT_DATA (f)->bottom_right_corner_cursor;
 35491           break;
 35492         case INTERNAL_BORDER_BOTTOM_EDGE:
 35493           cursor = FRAME_OUTPUT_DATA (f)->bottom_edge_cursor;
 35494           break;
 35495         case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
 35496           cursor = FRAME_OUTPUT_DATA (f)->bottom_left_corner_cursor;
 35497           break;
 35498         default:
 35499           /* This should not happen.  */
 35500           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35501             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35502         }
 35503 
 35504       if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35505         {
 35506           /* Do we really want a help echo here?  */
 35507           help_echo_string = build_string ("drag-mouse-1: resize frame");
 35508           goto set_cursor;
 35509         }
 35510     }
 35511 #endif /* HAVE_WINDOW_SYSTEM */
 35512 
 35513   /* Not on a window -> return.  */
 35514   if (!WINDOWP (window))
 35515     return;
 35516 
 35517   /* Convert to window-relative pixel coordinates.  */
 35518   w = XWINDOW (window);
 35519   frame_to_window_pixel_xy (w, &x, &y);
 35520 
 35521 #if defined (HAVE_WINDOW_SYSTEM)
 35522   /* Handle tab-bar window differently since it doesn't display a
 35523      buffer.  */
 35524   if (EQ (window, f->tab_bar_window))
 35525     {
 35526       note_tab_bar_highlight (f, x, y);
 35527       if (tab_bar__dragging_in_progress)
 35528         {
 35529           cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35530           goto set_cursor;
 35531         }
 35532       else
 35533         return;
 35534     }
 35535   else
 35536     {
 35537       /* The mouse might have pressed into the tab bar, but might
 35538          also have been released outside the tab bar, so
 35539          f->last_tab_bar_item must be reset, in order to make sure the
 35540          item can be still highlighted again in the future.  */
 35541       f->last_tab_bar_item = -1;
 35542     }
 35543 #endif
 35544 
 35545 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 35546   /* Handle tool-bar window differently since it doesn't display a
 35547      buffer.  */
 35548   if (EQ (window, f->tool_bar_window))
 35549     {
 35550       note_tool_bar_highlight (f, x, y);
 35551       return;
 35552     }
 35553 #endif
 35554 
 35555   /* Mouse is on the mode, header line or margin?  */
 35556   if (part == ON_MODE_LINE || part == ON_HEADER_LINE || part == ON_TAB_LINE
 35557       || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35558     {
 35559       note_mode_line_or_margin_highlight (window, x, y, part);
 35560 
 35561 #ifdef HAVE_WINDOW_SYSTEM
 35562       if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35563         {
 35564           cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35565           /* Show non-text cursor (Bug#16647).  */
 35566           goto set_cursor;
 35567         }
 35568       else
 35569 #endif
 35570         return;
 35571     }
 35572 
 35573 #ifdef HAVE_WINDOW_SYSTEM
 35574   if (part == ON_VERTICAL_BORDER)
 35575     {
 35576       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35577       help_echo_string = build_string ("drag-mouse-1: resize");
 35578       goto set_cursor;
 35579     }
 35580   else if (part == ON_RIGHT_DIVIDER)
 35581     {
 35582       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35583       help_echo_string = build_string ("drag-mouse-1: resize");
 35584       goto set_cursor;
 35585     }
 35586   else if (part == ON_BOTTOM_DIVIDER)
 35587     if (! WINDOW_BOTTOMMOST_P (w)
 35588         || minibuf_level
 35589         || NILP (Vresize_mini_windows))
 35590       {
 35591         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35592         help_echo_string = build_string ("drag-mouse-1: resize");
 35593         goto set_cursor;
 35594       }
 35595     else
 35596       cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35597   else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
 35598            || part == ON_VERTICAL_SCROLL_BAR
 35599            || part == ON_HORIZONTAL_SCROLL_BAR)
 35600     cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35601   else
 35602     cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35603 #endif
 35604 
 35605   /* Are we in a window whose display is up to date?
 35606      And verify the buffer's text has not changed.  */
 35607   b = XBUFFER (w->contents);
 35608   if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
 35609     {
 35610       int hpos, vpos, dx, dy, area = LAST_AREA;
 35611       ptrdiff_t pos;
 35612       struct glyph *glyph;
 35613       Lisp_Object object;
 35614       Lisp_Object mouse_face = Qnil, position;
 35615       Lisp_Object *overlay_vec = NULL;
 35616       ptrdiff_t i, noverlays;
 35617       struct buffer *obuf;
 35618       ptrdiff_t obegv, ozv;
 35619       bool same_region;
 35620 
 35621       /* Find the glyph under X/Y.  */
 35622       glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
 35623 
 35624 #ifdef HAVE_WINDOW_SYSTEM
 35625       /* Look for :pointer property on image.  */
 35626       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 35627         {
 35628           struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 35629           if (img != NULL && IMAGEP (img->spec))
 35630             {
 35631               Lisp_Object image_map, hotspot;
 35632               if ((image_map = plist_get (XCDR (img->spec), QCmap),
 35633                    !NILP (image_map))
 35634                   && (hotspot = find_hot_spot (image_map,
 35635                                                glyph->slice.img.x + dx,
 35636                                                glyph->slice.img.y + dy),
 35637                       CONSP (hotspot))
 35638                   && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35639                 {
 35640                   Lisp_Object plist;
 35641 
 35642                   /* Could check XCAR (hotspot) to see if we enter/leave
 35643                      this hot-spot.
 35644                      If so, we could look for mouse-enter, mouse-leave
 35645                      properties in PLIST (and do something...).  */
 35646                   hotspot = XCDR (hotspot);
 35647                   if (CONSP (hotspot)
 35648                       && (plist = XCAR (hotspot), CONSP (plist)))
 35649                     {
 35650                       pointer = plist_get (plist, Qpointer);
 35651                       if (NILP (pointer))
 35652                         pointer = Qhand;
 35653                       help_echo_string = plist_get (plist, Qhelp_echo);
 35654                       if (!NILP (help_echo_string))
 35655                         {
 35656                           help_echo_window = window;
 35657                           help_echo_object = glyph->object;
 35658                           help_echo_pos = glyph->charpos;
 35659                         }
 35660                     }
 35661                 }
 35662               if (NILP (pointer))
 35663                 pointer = plist_get (XCDR (img->spec), QCpointer);
 35664             }
 35665         }
 35666 #endif  /* HAVE_WINDOW_SYSTEM */
 35667 
 35668       /* Clear mouse face if X/Y not over text.  */
 35669       if (glyph == NULL
 35670           || area != TEXT_AREA
 35671           || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
 35672           /* Glyph's OBJECT is nil for glyphs inserted by the
 35673              display engine for its internal purposes, like truncation
 35674              and continuation glyphs and blanks beyond the end of
 35675              line's text on text terminals.  If we are over such a
 35676              glyph, we are not over any text.  */
 35677           || NILP (glyph->object)
 35678           /* R2L rows have a stretch glyph at their front, which
 35679              stands for no text, whereas L2R rows have no glyphs at
 35680              all beyond the end of text.  Treat such stretch glyphs
 35681              like we do with NULL glyphs in L2R rows.  */
 35682           || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
 35683               && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
 35684               && glyph->type == STRETCH_GLYPH
 35685               && glyph->avoid_cursor_p))
 35686         {
 35687           if (clear_mouse_face (hlinfo))
 35688             cursor = No_Cursor;
 35689           if (FRAME_WINDOW_P (f) && NILP (pointer))
 35690             {
 35691 #ifdef HAVE_WINDOW_SYSTEM
 35692               if (area != TEXT_AREA)
 35693                 cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35694               else
 35695                 pointer = Vvoid_text_area_pointer;
 35696 #endif
 35697             }
 35698           goto set_cursor;
 35699         }
 35700 
 35701       pos = glyph->charpos;
 35702       object = glyph->object;
 35703       if (!STRINGP (object) && !BUFFERP (object))
 35704         goto set_cursor;
 35705 
 35706       /* If we get an out-of-range value, return now; avoid an error.  */
 35707       if (BUFFERP (object) && pos > BUF_Z (b))
 35708         goto set_cursor;
 35709 
 35710       /* Make the window's buffer temporarily current for
 35711          overlays_at and compute_char_face.  */
 35712       obuf = current_buffer;
 35713       current_buffer = b;
 35714       obegv = BEGV;
 35715       ozv = ZV;
 35716       BEGV = BEG;
 35717       ZV = Z;
 35718 
 35719       /* Is this char mouse-active or does it have help-echo?  */
 35720       position = make_fixnum (pos);
 35721 
 35722       USE_SAFE_ALLOCA;
 35723 
 35724       if (BUFFERP (object))
 35725         {
 35726           /* Put all the overlays we want in a vector in overlay_vec.  */
 35727           GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL);
 35728           /* Sort overlays into increasing priority order.  */
 35729           noverlays = sort_overlays (overlay_vec, noverlays, w);
 35730         }
 35731       else
 35732         noverlays = 0;
 35733 
 35734       if (NILP (Vmouse_highlight))
 35735         {
 35736           clear_mouse_face (hlinfo);
 35737           goto check_help_echo;
 35738         }
 35739 
 35740       same_region = coords_in_mouse_face_p (w, hpos, vpos);
 35741 
 35742       if (same_region)
 35743         cursor = No_Cursor;
 35744 
 35745       /* Check mouse-face highlighting.  */
 35746       if (! same_region
 35747           /* If there exists an overlay with mouse-face overlapping
 35748              the one we are currently highlighting, we have to check
 35749              if we enter the overlapping overlay, and then highlight
 35750              only that.  Skip the check when mouse-face highlighting
 35751              is currently hidden to avoid Bug#30519.  */
 35752           || (!hlinfo->mouse_face_hidden
 35753               && OVERLAYP (hlinfo->mouse_face_overlay)
 35754               /* It's possible the overlay was deleted (Bug#35273).  */
 35755               && OVERLAY_BUFFER (hlinfo->mouse_face_overlay)
 35756               && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
 35757         {
 35758           /* Find the highest priority overlay with a mouse-face.  */
 35759           Lisp_Object overlay = Qnil;
 35760           for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
 35761             {
 35762               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
 35763               if (!NILP (mouse_face))
 35764                 overlay = overlay_vec[i];
 35765             }
 35766 
 35767           /* If we're highlighting the same overlay as before, there's
 35768              no need to do that again.  */
 35769           if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
 35770             goto check_help_echo;
 35771 
 35772           /* Clear the display of the old active region, if any.  */
 35773           if (clear_mouse_face (hlinfo))
 35774             cursor = No_Cursor;
 35775 
 35776           /* Record the overlay, if any, to be highlighted.  */
 35777           hlinfo->mouse_face_overlay = overlay;
 35778 
 35779           /* If no overlay applies, get a text property.  */
 35780           if (NILP (overlay))
 35781             mouse_face = Fget_text_property (position, Qmouse_face, object);
 35782 
 35783           /* Next, compute the bounds of the mouse highlighting and
 35784              display it.  */
 35785           if (!NILP (mouse_face) && STRINGP (object))
 35786             {
 35787               /* The mouse-highlighting comes from a display string
 35788                  with a mouse-face.  */
 35789               Lisp_Object s, e;
 35790               ptrdiff_t ignore;
 35791 
 35792               s = Fprevious_single_property_change
 35793                 (make_fixnum (pos + 1), Qmouse_face, object, Qnil);
 35794               e = Fnext_single_property_change
 35795                 (position, Qmouse_face, object, Qnil);
 35796               if (NILP (s))
 35797                 s = make_fixnum (0);
 35798               if (NILP (e))
 35799                 e = make_fixnum (SCHARS (object));
 35800               mouse_face_from_string_pos (w, hlinfo, object,
 35801                                           XFIXNUM (s), XFIXNUM (e));
 35802               hlinfo->mouse_face_past_end = false;
 35803               hlinfo->mouse_face_window = window;
 35804               hlinfo->mouse_face_face_id
 35805                 = face_at_string_position (w, object, pos, 0, &ignore,
 35806                                            glyph->face_id, true, 0);
 35807               show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35808               cursor = No_Cursor;
 35809             }
 35810           else
 35811             {
 35812               /* The mouse-highlighting, if any, comes from an overlay
 35813                  or text property in the buffer.  */
 35814               Lisp_Object buffer UNINIT;
 35815               Lisp_Object disp_string UNINIT;
 35816 
 35817               if (STRINGP (object))
 35818                 {
 35819                   /* If we are on a display string with no mouse-face,
 35820                      check if the text under it has one.  */
 35821                   struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 35822                   ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35823                   pos = string_buffer_position (object, start);
 35824                   if (pos > 0)
 35825                     {
 35826                       mouse_face = get_char_property_and_overlay
 35827                         (make_fixnum (pos), Qmouse_face, w->contents, &overlay);
 35828                       buffer = w->contents;
 35829                       disp_string = object;
 35830                     }
 35831                 }
 35832               else
 35833                 {
 35834                   buffer = object;
 35835                   disp_string = Qnil;
 35836                 }
 35837 
 35838               if (!NILP (mouse_face))
 35839                 {
 35840                   Lisp_Object before, after;
 35841                   Lisp_Object before_string, after_string;
 35842                   /* To correctly find the limits of mouse highlight
 35843                      in a bidi-reordered buffer, we must not use the
 35844                      optimization of limiting the search in
 35845                      previous-single-property-change and
 35846                      next-single-property-change, because
 35847                      rows_from_pos_range needs the real start and end
 35848                      positions to DTRT in this case.  That's because
 35849                      the first row visible in a window does not
 35850                      necessarily display the character whose position
 35851                      is the smallest.  */
 35852                   Lisp_Object lim1
 35853                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35854                     ? Fmarker_position (w->start)
 35855                     : Qnil;
 35856                   Lisp_Object lim2
 35857                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35858                     ? make_fixnum (BUF_Z (XBUFFER (buffer))
 35859                                    - w->window_end_pos)
 35860                     : Qnil;
 35861 
 35862                   if (NILP (overlay))
 35863                     {
 35864                       /* Handle the text property case.  */
 35865                       before = Fprevious_single_property_change
 35866                         (make_fixnum (pos + 1), Qmouse_face, buffer, lim1);
 35867                       after = Fnext_single_property_change
 35868                         (make_fixnum (pos), Qmouse_face, buffer, lim2);
 35869                       before_string = after_string = Qnil;
 35870                     }
 35871                   else
 35872                     {
 35873                       /* Handle the overlay case.  */
 35874                       before = Foverlay_start (overlay);
 35875                       after = Foverlay_end (overlay);
 35876                       before_string = Foverlay_get (overlay, Qbefore_string);
 35877                       after_string = Foverlay_get (overlay, Qafter_string);
 35878 
 35879                       if (!STRINGP (before_string)) before_string = Qnil;
 35880                       if (!STRINGP (after_string))  after_string = Qnil;
 35881                     }
 35882 
 35883                   mouse_face_from_buffer_pos (window, hlinfo, pos,
 35884                                               NILP (before)
 35885                                               ? 1
 35886                                               : XFIXNAT (before),
 35887                                               NILP (after)
 35888                                               ? BUF_Z (XBUFFER (buffer))
 35889                                               : XFIXNAT (after),
 35890                                               before_string, after_string,
 35891                                               disp_string);
 35892                   cursor = No_Cursor;
 35893                 }
 35894             }
 35895         }
 35896 
 35897     check_help_echo:
 35898 
 35899       /* Look for a `help-echo' property.  */
 35900       if (NILP (help_echo_string)) {
 35901         Lisp_Object help, overlay;
 35902 
 35903         /* Check overlays first.  */
 35904         help = overlay = Qnil;
 35905         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
 35906           {
 35907             overlay = overlay_vec[i];
 35908             help = Foverlay_get (overlay, Qhelp_echo);
 35909           }
 35910 
 35911         if (!NILP (help))
 35912           {
 35913             help_echo_string = help;
 35914             help_echo_window = window;
 35915             help_echo_object = overlay;
 35916             help_echo_pos = pos;
 35917           }
 35918         else
 35919           {
 35920             Lisp_Object obj = glyph->object;
 35921             ptrdiff_t charpos = glyph->charpos;
 35922 
 35923             /* Try text properties.  */
 35924             if (STRINGP (obj)
 35925                 && charpos >= 0
 35926                 && charpos < SCHARS (obj))
 35927               {
 35928                 help = Fget_text_property (make_fixnum (charpos),
 35929                                            Qhelp_echo, obj);
 35930                 if (NILP (help))
 35931                   {
 35932                     /* If the string itself doesn't specify a help-echo,
 35933                        see if the buffer text ``under'' it does.  */
 35934                     struct glyph_row *r
 35935                       = MATRIX_ROW (w->current_matrix, vpos);
 35936                     ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35937                     ptrdiff_t p = string_buffer_position (obj, start);
 35938                     if (p > 0)
 35939                       {
 35940                         help = Fget_char_property (make_fixnum (p),
 35941                                                    Qhelp_echo, w->contents);
 35942                         if (!NILP (help))
 35943                           {
 35944                             charpos = p;
 35945                             obj = w->contents;
 35946                           }
 35947                       }
 35948                   }
 35949               }
 35950             else if (BUFFERP (obj)
 35951                      && charpos >= BEGV
 35952                      && charpos < ZV)
 35953               help = Fget_text_property (make_fixnum (charpos), Qhelp_echo,
 35954                                          obj);
 35955 
 35956             if (!NILP (help))
 35957               {
 35958                 help_echo_string = help;
 35959                 help_echo_window = window;
 35960                 help_echo_object = obj;
 35961                 help_echo_pos = charpos;
 35962               }
 35963           }
 35964       }
 35965 
 35966 #ifdef HAVE_WINDOW_SYSTEM
 35967       /* Look for a `pointer' property.  */
 35968       if (FRAME_WINDOW_P (f) && NILP (pointer))
 35969         {
 35970           /* Check overlays first.  */
 35971           for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
 35972             pointer = Foverlay_get (overlay_vec[i], Qpointer);
 35973 
 35974           if (NILP (pointer))
 35975             {
 35976               Lisp_Object obj = glyph->object;
 35977               ptrdiff_t charpos = glyph->charpos;
 35978 
 35979               /* Try text properties.  */
 35980               if (STRINGP (obj)
 35981                   && charpos >= 0
 35982                   && charpos < SCHARS (obj))
 35983                 {
 35984                   pointer = Fget_text_property (make_fixnum (charpos),
 35985                                                 Qpointer, obj);
 35986                   if (NILP (pointer))
 35987                     {
 35988                       /* If the string itself doesn't specify a pointer,
 35989                          see if the buffer text ``under'' it does.  */
 35990                       struct glyph_row *r
 35991                         = MATRIX_ROW (w->current_matrix, vpos);
 35992                       ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35993                       ptrdiff_t p = string_buffer_position (obj, start);
 35994                       if (p > 0)
 35995                         pointer = Fget_char_property (make_fixnum (p),
 35996                                                       Qpointer, w->contents);
 35997                     }
 35998                 }
 35999               else if (BUFFERP (obj)
 36000                        && charpos >= BEGV
 36001                        && charpos < ZV)
 36002                 pointer = Fget_text_property (make_fixnum (charpos),
 36003                                               Qpointer, obj);
 36004             }
 36005         }
 36006 #endif  /* HAVE_WINDOW_SYSTEM */
 36007 
 36008       BEGV = obegv;
 36009       ZV = ozv;
 36010       current_buffer = obuf;
 36011       SAFE_FREE ();
 36012     }
 36013 
 36014  set_cursor:
 36015   define_frame_cursor1 (f, cursor, pointer);
 36016 }
 36017 
 36018 
 36019 /* EXPORT for RIF:
 36020    Clear any mouse-face on window W.  This function is part of the
 36021    redisplay interface, and is called from try_window_id and similar
 36022    functions to ensure the mouse-highlight is off.  */
 36023 
 36024 void
 36025 gui_clear_window_mouse_face (struct window *w)
 36026 {
 36027   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 36028   Lisp_Object window;
 36029 
 36030   block_input ();
 36031   XSETWINDOW (window, w);
 36032   if (EQ (window, hlinfo->mouse_face_window))
 36033     clear_mouse_face (hlinfo);
 36034   unblock_input ();
 36035 }
 36036 
 36037 
 36038 /* EXPORT:
 36039    Just discard the mouse face information for frame F, if any.
 36040    This is used when the size of F is changed.  */
 36041 
 36042 void
 36043 cancel_mouse_face (struct frame *f)
 36044 {
 36045   Lisp_Object window;
 36046   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36047 
 36048   window = hlinfo->mouse_face_window;
 36049   if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
 36050     reset_mouse_highlight (hlinfo);
 36051 }
 36052 
 36053 
 36054 
 36055 /***********************************************************************
 36056                            Exposure Events
 36057  ***********************************************************************/
 36058 
 36059 #ifdef HAVE_WINDOW_SYSTEM
 36060 
 36061 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
 36062    which intersects rectangle R.  R is in window-relative coordinates.  */
 36063 
 36064 static void
 36065 expose_area (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r,
 36066              enum glyph_row_area area)
 36067 {
 36068   struct glyph *first = row->glyphs[area];
 36069   struct glyph *end = row->glyphs[area] + row->used[area];
 36070   struct glyph *last;
 36071   int first_x, start_x, x;
 36072 
 36073   if (area == TEXT_AREA && row->fill_line_p)
 36074     /* If row extends face to end of line write the whole line.  */
 36075     draw_glyphs (w, row->x, row, area,
 36076                  0, row->used[area],
 36077                  DRAW_NORMAL_TEXT, 0);
 36078   else
 36079     {
 36080       /* Set START_X to the window-relative start position for drawing glyphs of
 36081          AREA.  The first glyph of the text area can be partially visible.
 36082          The first glyphs of other areas cannot.  */
 36083       start_x = window_box_left_offset (w, area);
 36084       x = start_x;
 36085       if (area == TEXT_AREA)
 36086         x += row->x;
 36087 
 36088       /* Find the first glyph that must be redrawn.  */
 36089       while (first < end
 36090              && x + first->pixel_width < r->x)
 36091         {
 36092           x += first->pixel_width;
 36093           ++first;
 36094         }
 36095 
 36096       /* Find the last one.  */
 36097       last = first;
 36098       first_x = x;
 36099       /* Use a signed int intermediate value to avoid catastrophic
 36100          failures due to comparison between signed and unsigned, when
 36101          x is negative (can happen for wide images that are hscrolled).  */
 36102       int r_end = r->x + r->width;
 36103       while (last < end && x < r_end)
 36104         {
 36105           x += last->pixel_width;
 36106           ++last;
 36107         }
 36108 
 36109       /* Repaint.  */
 36110       if (last > first)
 36111         draw_glyphs (w, first_x - start_x, row, area,
 36112                      first - row->glyphs[area], last - row->glyphs[area],
 36113                      DRAW_NORMAL_TEXT, 0);
 36114     }
 36115 }
 36116 
 36117 
 36118 /* Redraw the parts of the glyph row ROW on window W intersecting
 36119    rectangle R.  R is in window-relative coordinates.  Value is
 36120    true if mouse-face was overwritten.  */
 36121 
 36122 static bool
 36123 expose_line (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r)
 36124 {
 36125   eassert (row->enabled_p);
 36126 
 36127   if (row->mode_line_p || w->pseudo_window_p)
 36128     draw_glyphs (w, 0, row, TEXT_AREA,
 36129                  0, row->used[TEXT_AREA],
 36130                  DRAW_NORMAL_TEXT, 0);
 36131   else
 36132     {
 36133       if (row->used[LEFT_MARGIN_AREA])
 36134         expose_area (w, row, r, LEFT_MARGIN_AREA);
 36135       if (row->used[TEXT_AREA])
 36136         expose_area (w, row, r, TEXT_AREA);
 36137       if (row->used[RIGHT_MARGIN_AREA])
 36138         expose_area (w, row, r, RIGHT_MARGIN_AREA);
 36139       draw_row_fringe_bitmaps (w, row);
 36140     }
 36141 
 36142   return row->mouse_face_p;
 36143 }
 36144 
 36145 
 36146 /* Redraw those parts of glyphs rows during expose event handling that
 36147    overlap other rows.  Redrawing of an exposed line writes over parts
 36148    of lines overlapping that exposed line; this function fixes that.
 36149 
 36150    W is the window being exposed.  FIRST_OVERLAPPING_ROW is the first
 36151    row in W's current matrix that is exposed and overlaps other rows.
 36152    LAST_OVERLAPPING_ROW is the last such row.  */
 36153 
 36154 static void
 36155 expose_overlaps (struct window *w,
 36156                  struct glyph_row *first_overlapping_row,
 36157                  struct glyph_row *last_overlapping_row,
 36158                  const Emacs_Rectangle *r)
 36159 {
 36160   struct glyph_row *row;
 36161 
 36162   for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
 36163     if (row->overlapping_p)
 36164       {
 36165         eassert (row->enabled_p && !row->mode_line_p);
 36166 
 36167         row->clip = r;
 36168         if (row->used[LEFT_MARGIN_AREA])
 36169           gui_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
 36170 
 36171         if (row->used[TEXT_AREA])
 36172           gui_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
 36173 
 36174         if (row->used[RIGHT_MARGIN_AREA])
 36175           gui_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
 36176         row->clip = NULL;
 36177       }
 36178 }
 36179 
 36180 
 36181 /* Return true if W's cursor intersects rectangle R.  */
 36182 
 36183 static bool
 36184 phys_cursor_in_rect_p (struct window *w, const Emacs_Rectangle *r)
 36185 {
 36186   Emacs_Rectangle cr, result;
 36187   struct glyph *cursor_glyph;
 36188   struct glyph_row *row;
 36189 
 36190   if (w->phys_cursor.vpos >= 0
 36191       && w->phys_cursor.vpos < w->current_matrix->nrows
 36192       && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
 36193           row->enabled_p)
 36194       && row->cursor_in_fringe_p)
 36195     {
 36196       /* Cursor is in the fringe.  */
 36197       cr.x = window_box_right_offset (w,
 36198                                       (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
 36199                                        ? RIGHT_MARGIN_AREA
 36200                                        : TEXT_AREA));
 36201       cr.y = row->y;
 36202       cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
 36203       cr.height = row->height;
 36204       return gui_intersect_rectangles (&cr, r, &result);
 36205     }
 36206 
 36207   cursor_glyph = get_phys_cursor_glyph (w);
 36208   if (cursor_glyph)
 36209     {
 36210       /* r is relative to W's box, but w->phys_cursor.x is relative
 36211          to left edge of W's TEXT area.  Adjust it.  */
 36212       cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
 36213       cr.y = w->phys_cursor.y;
 36214       cr.width = cursor_glyph->pixel_width;
 36215       cr.height = w->phys_cursor_height;
 36216       /* ++KFS: W32 version used W32-specific IntersectRect here, but
 36217          I assume the effect is the same -- and this is portable.  */
 36218       return gui_intersect_rectangles (&cr, r, &result);
 36219     }
 36220   /* If we don't understand the format, pretend we're not in the hot-spot.  */
 36221   return false;
 36222 }
 36223 
 36224 
 36225 /* EXPORT:
 36226    Draw a vertical window border to the right of window W if W doesn't
 36227    have vertical scroll bars.  */
 36228 
 36229 void
 36230 gui_draw_vertical_border (struct window *w)
 36231 {
 36232   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36233 
 36234   /* We could do better, if we knew what type of scroll-bar the adjacent
 36235      windows (on either side) have...  But we don't :-(
 36236      However, I think this works ok.  ++KFS 2003-04-25 */
 36237 
 36238   /* Redraw borders between horizontally adjacent windows.  Don't
 36239      do it for frames with vertical scroll bars because either the
 36240      right scroll bar of a window, or the left scroll bar of its
 36241      neighbor will suffice as a border.  */
 36242   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
 36243     return;
 36244 
 36245   /* Note: It is necessary to redraw both the left and the right
 36246      borders, for when only this single window W is being
 36247      redisplayed.  */
 36248   if (!WINDOW_RIGHTMOST_P (w)
 36249       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
 36250     {
 36251       int x0, x1, y0, y1;
 36252 
 36253       window_box_edges (w, &x0, &y0, &x1, &y1);
 36254       y1 -= 1;
 36255 
 36256       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36257         x1 -= 1;
 36258 
 36259       FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
 36260     }
 36261 
 36262   if (!WINDOW_LEFTMOST_P (w)
 36263       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
 36264     {
 36265       int x0, x1, y0, y1;
 36266 
 36267       window_box_edges (w, &x0, &y0, &x1, &y1);
 36268       y1 -= 1;
 36269 
 36270       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36271         x0 -= 1;
 36272 
 36273       FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
 36274     }
 36275 }
 36276 
 36277 
 36278 /* Draw window dividers for window W.  */
 36279 
 36280 void
 36281 gui_draw_right_divider (struct window *w)
 36282 {
 36283   struct frame *f = WINDOW_XFRAME (w);
 36284 
 36285   if (w->mini || w->pseudo_window_p)
 36286     return;
 36287   else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36288     {
 36289       int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36290       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36291       int y0 = WINDOW_TOP_EDGE_Y (w);
 36292       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36293 
 36294       /* If W is horizontally combined and has a right sibling, don't
 36295          draw over any bottom divider.  */
 36296       if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
 36297           && !NILP (w->parent)
 36298           && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
 36299           && !NILP (w->next))
 36300         y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36301 
 36302       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36303     }
 36304 }
 36305 
 36306 static void
 36307 gui_draw_bottom_divider (struct window *w)
 36308 {
 36309   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36310 
 36311   if (w->mini || w->pseudo_window_p)
 36312     return;
 36313   else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36314     {
 36315       int x0 = WINDOW_LEFT_EDGE_X (w);
 36316       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36317       int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36318       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36319       struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
 36320 
 36321       /* If W is vertically combined and has a sibling below, don't draw
 36322          over any right divider.  */
 36323       if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
 36324           && p
 36325           && ((WINDOW_VERTICAL_COMBINATION_P (p)
 36326                && !NILP (w->next))
 36327               || (WINDOW_HORIZONTAL_COMBINATION_P (p)
 36328                   && NILP (w->next)
 36329                   && !NILP (p->parent)
 36330                   && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
 36331                   && !NILP (XWINDOW (p->parent)->next))))
 36332         x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36333 
 36334       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36335     }
 36336 }
 36337 
 36338 /* Redraw the part of window W intersection rectangle FR.  Pixel
 36339    coordinates in FR are frame-relative.  Call this function with
 36340    input blocked.  Value is true if the exposure overwrites
 36341    mouse-face.  */
 36342 
 36343 static bool
 36344 expose_window (struct window *w, const Emacs_Rectangle *fr)
 36345 {
 36346   struct frame *f = XFRAME (w->frame);
 36347   Emacs_Rectangle wr, r;
 36348   bool mouse_face_overwritten_p = false;
 36349 
 36350   /* If window is not yet fully initialized, do nothing.  This can
 36351      happen when toolkit scroll bars are used and a window is split.
 36352      Reconfiguring the scroll bar will generate an expose for a newly
 36353      created window.  */
 36354   if (w->current_matrix == NULL)
 36355     return false;
 36356 
 36357   /* When we're currently updating the window, display and current
 36358      matrix usually don't agree.  Arrange for a thorough display
 36359      later.  */
 36360   if (w->must_be_updated_p)
 36361     {
 36362       SET_FRAME_GARBAGED (f);
 36363       return false;
 36364     }
 36365 
 36366   /* Frame-relative pixel rectangle of W.  */
 36367   wr.x = WINDOW_LEFT_EDGE_X (w);
 36368   wr.y = WINDOW_TOP_EDGE_Y (w);
 36369   wr.width = WINDOW_PIXEL_WIDTH (w);
 36370   wr.height = WINDOW_PIXEL_HEIGHT (w);
 36371 
 36372   if (gui_intersect_rectangles (fr, &wr, &r))
 36373     {
 36374       int yb = window_text_bottom_y (w);
 36375       struct glyph_row *row;
 36376       struct glyph_row *first_overlapping_row, *last_overlapping_row;
 36377 
 36378       redisplay_trace ("expose_window (%d, %d, %u, %u)\n",
 36379                        r.x, r.y, r.width, r.height);
 36380 
 36381       /* Convert to window coordinates.  */
 36382       r.x -= WINDOW_LEFT_EDGE_X (w);
 36383       r.y -= WINDOW_TOP_EDGE_Y (w);
 36384 
 36385       /* Turn off the cursor.  */
 36386       bool cursor_cleared_p = (!w->pseudo_window_p
 36387                                && phys_cursor_in_rect_p (w, &r));
 36388       if (cursor_cleared_p)
 36389         gui_clear_cursor (w);
 36390 
 36391       /* If the row containing the cursor extends face to end of line,
 36392          then expose_area might overwrite the cursor outside the
 36393          rectangle and thus notice_overwritten_cursor might clear
 36394          w->phys_cursor_on_p.  We remember the original value and
 36395          check later if it is changed.  */
 36396       bool phys_cursor_on_p = w->phys_cursor_on_p;
 36397 
 36398       /* Use a signed int intermediate value to avoid catastrophic
 36399          failures due to comparison between signed and unsigned, when
 36400          y0 or y1 is negative (can happen for tall images).  */
 36401       int r_bottom = r.y + r.height;
 36402 
 36403       /* We must temporarily switch to the window's buffer, in case
 36404          the fringe face has been remapped in that buffer's
 36405          face-remapping-alist, so that draw_row_fringe_bitmaps,
 36406          called from expose_line, will use the right face.  */
 36407       bool buffer_changed = false;
 36408       struct buffer *oldbuf = current_buffer;
 36409       if (!w->pseudo_window_p)
 36410         {
 36411           set_buffer_internal_1 (XBUFFER (w->contents));
 36412           buffer_changed = true;
 36413         }
 36414 
 36415       /* Update lines intersecting rectangle R.  */
 36416       first_overlapping_row = last_overlapping_row = NULL;
 36417       for (row = w->current_matrix->rows;
 36418            row->enabled_p;
 36419            ++row)
 36420         {
 36421           int y0 = row->y;
 36422           int y1 = MATRIX_ROW_BOTTOM_Y (row);
 36423 
 36424           if ((y0 >= r.y && y0 < r_bottom)
 36425               || (y1 > r.y && y1 < r_bottom)
 36426               || (r.y >= y0 && r.y < y1)
 36427               || (r_bottom > y0 && r_bottom < y1))
 36428             {
 36429               /* A header line may be overlapping, but there is no need
 36430                  to fix overlapping areas for them.  KFS 2005-02-12 */
 36431               if (row->overlapping_p && !row->mode_line_p)
 36432                 {
 36433                   if (first_overlapping_row == NULL)
 36434                     first_overlapping_row = row;
 36435                   last_overlapping_row = row;
 36436                 }
 36437 
 36438               row->clip = fr;
 36439               if (expose_line (w, row, &r))
 36440                 mouse_face_overwritten_p = true;
 36441               row->clip = NULL;
 36442             }
 36443           else if (row->overlapping_p)
 36444             {
 36445               /* We must redraw a row overlapping the exposed area.  */
 36446               if (y0 < r.y
 36447                   ? y0 + row->phys_height > r.y
 36448                   : y0 + row->ascent - row->phys_ascent < r.y +r.height)
 36449                 {
 36450                   if (first_overlapping_row == NULL)
 36451                     first_overlapping_row = row;
 36452                   last_overlapping_row = row;
 36453                 }
 36454             }
 36455 
 36456           if (y1 >= yb)
 36457             break;
 36458         }
 36459 
 36460       if (buffer_changed)
 36461         set_buffer_internal_1 (oldbuf);
 36462 
 36463       /* Display the mode line if there is one.  */
 36464       if (window_wants_mode_line (w)
 36465           && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
 36466               row->enabled_p)
 36467           && row->y < r_bottom)
 36468         {
 36469           if (expose_line (w, row, &r))
 36470             mouse_face_overwritten_p = true;
 36471         }
 36472 
 36473       if (!w->pseudo_window_p)
 36474         {
 36475           /* Fix the display of overlapping rows.  */
 36476           if (first_overlapping_row)
 36477             expose_overlaps (w, first_overlapping_row, last_overlapping_row,
 36478                              fr);
 36479 
 36480           /* Draw border between windows.  */
 36481           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36482             gui_draw_right_divider (w);
 36483           else
 36484             gui_draw_vertical_border (w);
 36485 
 36486           if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36487             gui_draw_bottom_divider (w);
 36488 
 36489           /* Turn the cursor on again.  */
 36490           if (cursor_cleared_p
 36491               || (phys_cursor_on_p && !w->phys_cursor_on_p))
 36492             update_window_cursor (w, true);
 36493         }
 36494     }
 36495 
 36496   return mouse_face_overwritten_p;
 36497 }
 36498 
 36499 
 36500 
 36501 /* Redraw (parts) of all windows in the window tree rooted at W that
 36502    intersect R.  R contains frame pixel coordinates.  Value is
 36503    true if the exposure overwrites mouse-face.  */
 36504 
 36505 static bool
 36506 expose_window_tree (struct window *w, const Emacs_Rectangle *r)
 36507 {
 36508   struct frame *f = XFRAME (w->frame);
 36509   bool mouse_face_overwritten_p = false;
 36510 
 36511   while (w && !FRAME_GARBAGED_P (f))
 36512     {
 36513       mouse_face_overwritten_p
 36514         |= (WINDOWP (w->contents)
 36515             ? expose_window_tree (XWINDOW (w->contents), r)
 36516             : expose_window (w, r));
 36517 
 36518       w = NILP (w->next) ? NULL : XWINDOW (w->next);
 36519     }
 36520 
 36521   return mouse_face_overwritten_p;
 36522 }
 36523 
 36524 
 36525 /* EXPORT:
 36526    Redisplay an exposed area of frame F.  X and Y are the upper-left
 36527    corner of the exposed rectangle.  W and H are width and height of
 36528    the exposed area.  All are pixel values.  W or H zero means redraw
 36529    the entire frame.  */
 36530 
 36531 void
 36532 expose_frame (struct frame *f, int x, int y, int w, int h)
 36533 {
 36534   Emacs_Rectangle r;
 36535   bool mouse_face_overwritten_p = false;
 36536 
 36537   if (FRAME_GARBAGED_P (f))
 36538     {
 36539       redisplay_trace ("expose_frame garbaged\n");
 36540       return;
 36541     }
 36542 
 36543   /* If basic faces haven't been realized yet, there is no point in
 36544      trying to redraw anything.  This can happen when we get an expose
 36545      event while Emacs is starting, e.g. by moving another window.  */
 36546   if (FRAME_FACE_CACHE (f) == NULL
 36547       || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
 36548     {
 36549       redisplay_trace ("expose_frame no faces\n");
 36550       return;
 36551     }
 36552 
 36553   if (w == 0 || h == 0)
 36554     {
 36555       r.x = r.y = 0;
 36556       r.width = FRAME_TEXT_WIDTH (f);
 36557       r.height = FRAME_TEXT_HEIGHT (f);
 36558     }
 36559   else
 36560     {
 36561       r.x = x;
 36562       r.y = y;
 36563       r.width = w;
 36564       r.height = h;
 36565     }
 36566 
 36567   redisplay_trace ("expose_frame (%d, %d, %u, %u)\n",
 36568                    r.x, r.y, r.width, r.height);
 36569   mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
 36570 
 36571   if (WINDOWP (f->tab_bar_window))
 36572     mouse_face_overwritten_p
 36573       |= expose_window (XWINDOW (f->tab_bar_window), &r);
 36574 
 36575 #ifndef HAVE_EXT_TOOL_BAR
 36576   if (WINDOWP (f->tool_bar_window))
 36577     mouse_face_overwritten_p
 36578       |= expose_window (XWINDOW (f->tool_bar_window), &r);
 36579 #endif
 36580 
 36581 #if defined HAVE_WINDOW_SYSTEM && !defined HAVE_EXT_MENU_BAR
 36582   if (WINDOWP (f->menu_bar_window))
 36583     mouse_face_overwritten_p
 36584       |= expose_window (XWINDOW (f->menu_bar_window), &r);
 36585 #endif
 36586 
 36587   /* Some window managers support a focus-follows-mouse style with
 36588      delayed raising of frames.  Imagine a partially obscured frame,
 36589      and moving the mouse into partially obscured mouse-face on that
 36590      frame.  The visible part of the mouse-face will be highlighted,
 36591      then the WM raises the obscured frame.  With at least one WM, KDE
 36592      2.1, Emacs is not getting any event for the raising of the frame
 36593      (even tried with SubstructureRedirectMask), only Expose events.
 36594      These expose events will draw text normally, i.e. not
 36595      highlighted.  Which means we must redo the highlight here.
 36596      Subsume it under ``we love X''.  --gerd 2001-08-15  */
 36597   /* Included in Windows version because Windows most likely does not
 36598      do the right thing if any third party tool offers
 36599      focus-follows-mouse with delayed raise.  --jason 2001-10-12  */
 36600   if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
 36601     {
 36602       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36603       if (f == hlinfo->mouse_face_mouse_frame)
 36604         {
 36605           int mouse_x = hlinfo->mouse_face_mouse_x;
 36606           int mouse_y = hlinfo->mouse_face_mouse_y;
 36607           clear_mouse_face (hlinfo);
 36608           note_mouse_highlight (f, mouse_x, mouse_y);
 36609         }
 36610     }
 36611 }
 36612 
 36613 
 36614 /* EXPORT:
 36615    Determine the intersection of two rectangles R1 and R2.  Return
 36616    the intersection in *RESULT.  Value is true if RESULT is not
 36617    empty.  */
 36618 
 36619 bool
 36620 gui_intersect_rectangles (const Emacs_Rectangle *r1, const Emacs_Rectangle *r2,
 36621                           Emacs_Rectangle *result)
 36622 {
 36623   const Emacs_Rectangle *left, *right;
 36624   const Emacs_Rectangle *upper, *lower;
 36625   bool intersection_p = false;
 36626 
 36627   /* Rearrange so that R1 is the left-most rectangle.  */
 36628   if (r1->x < r2->x)
 36629     left = r1, right = r2;
 36630   else
 36631     left = r2, right = r1;
 36632 
 36633   /* X0 of the intersection is right.x0, if this is inside R1,
 36634      otherwise there is no intersection.  */
 36635   if (right->x <= left->x + left->width)
 36636     {
 36637       result->x = right->x;
 36638 
 36639       /* The right end of the intersection is the minimum of
 36640          the right ends of left and right.  */
 36641       result->width = (min (left->x + left->width, right->x + right->width)
 36642                        - result->x);
 36643 
 36644       /* Same game for Y.  */
 36645       if (r1->y < r2->y)
 36646         upper = r1, lower = r2;
 36647       else
 36648         upper = r2, lower = r1;
 36649 
 36650       /* The upper end of the intersection is lower.y0, if this is inside
 36651          of upper.  Otherwise, there is no intersection.  */
 36652       if (lower->y <= upper->y + upper->height)
 36653         {
 36654           result->y = lower->y;
 36655 
 36656           /* The lower end of the intersection is the minimum of the lower
 36657              ends of upper and lower.  */
 36658           result->height = (min (lower->y + lower->height,
 36659                                  upper->y + upper->height)
 36660                             - result->y);
 36661           intersection_p = true;
 36662         }
 36663     }
 36664 
 36665   return intersection_p;
 36666 }
 36667 
 36668 /* EXPORT:
 36669    Determine the union of the rectangles A and B.  Return the smallest
 36670    rectangle encompassing both the bounds of A and B in *RESULT.  It
 36671    is safe for all three arguments to point to each other.  */
 36672 
 36673 void
 36674 gui_union_rectangles (const Emacs_Rectangle *a, const Emacs_Rectangle *b,
 36675                       Emacs_Rectangle *result)
 36676 {
 36677   struct gui_box a_box, b_box, result_box;
 36678 
 36679   /* Handle special cases where one of the rectangles is empty.  */
 36680 
 36681   if (!a->width || !a->height)
 36682     {
 36683       *result = *b;
 36684       return;
 36685     }
 36686   else if (!b->width || !b->height)
 36687     {
 36688       *result = *a;
 36689       return;
 36690     }
 36691 
 36692   /* Convert A and B to boxes.  */
 36693   a_box.x1 = a->x;
 36694   a_box.y1 = a->y;
 36695   a_box.x2 = a->x + a->width;
 36696   a_box.y2 = a->y + a->height;
 36697 
 36698   b_box.x1 = b->x;
 36699   b_box.y1 = b->y;
 36700   b_box.x2 = b->x + b->width;
 36701   b_box.y2 = b->y + b->height;
 36702 
 36703   /* Compute the union of the boxes.  */
 36704   result_box.x1 = min (a_box.x1, b_box.x1);
 36705   result_box.y1 = min (a_box.y1, b_box.y1);
 36706   result_box.x2 = max (a_box.x2, b_box.x2);
 36707   result_box.y2 = max (a_box.y2, b_box.y2);
 36708 
 36709   /* Convert result_box to an XRectangle and put the result in
 36710      RESULT.  */
 36711   result->x = result_box.x1;
 36712   result->y = result_box.y1;
 36713   result->width = result_box.x2 - result_box.x1;
 36714   result->height = result_box.y2 - result_box.y1;
 36715 }
 36716 
 36717 #endif /* HAVE_WINDOW_SYSTEM */
 36718 
 36719 
 36720 /***********************************************************************
 36721                             Initialization
 36722  ***********************************************************************/
 36723 
 36724 void
 36725 syms_of_xdisp (void)
 36726 {
 36727   Vwith_echo_area_save_vector = Qnil;
 36728   staticpro (&Vwith_echo_area_save_vector);
 36729 
 36730   Vmessage_stack = Qnil;
 36731   staticpro (&Vmessage_stack);
 36732 
 36733   /* Non-nil means don't actually do any redisplay.  */
 36734   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
 36735 
 36736   DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
 36737 
 36738   DEFVAR_BOOL ("scroll-minibuffer-conservatively",
 36739                scroll_minibuffer_conservatively,
 36740                doc: /* Non-nil means scroll conservatively in minibuffer windows.
 36741 When the value is nil, scrolling in minibuffer windows obeys the
 36742 settings of `scroll-conservatively'.  */);
 36743   scroll_minibuffer_conservatively = true; /* bug#44070 */
 36744 
 36745   DEFVAR_BOOL ("inhibit-message", inhibit_message,
 36746               doc:  /* Non-nil means calls to `message' are not displayed.
 36747 They are still logged to the *Messages* buffer.
 36748 
 36749 Do NOT set this globally to a non-nil value, as doing that will
 36750 disable messages everywhere, including in I-search and other
 36751 places where they are necessary.  This variable is intended to
 36752 be let-bound around code that needs to disable messages temporarily. */);
 36753   inhibit_message = false;
 36754 
 36755   message_dolog_marker1 = Fmake_marker ();
 36756   staticpro (&message_dolog_marker1);
 36757   message_dolog_marker2 = Fmake_marker ();
 36758   staticpro (&message_dolog_marker2);
 36759   message_dolog_marker3 = Fmake_marker ();
 36760   staticpro (&message_dolog_marker3);
 36761 
 36762   defsubr (&Sset_buffer_redisplay);
 36763 #ifdef GLYPH_DEBUG
 36764   defsubr (&Sdump_frame_glyph_matrix);
 36765   defsubr (&Sdump_glyph_matrix);
 36766   defsubr (&Sdump_glyph_row);
 36767   defsubr (&Sdump_tab_bar_row);
 36768   defsubr (&Sdump_tool_bar_row);
 36769   defsubr (&Strace_redisplay);
 36770   defsubr (&Strace_to_stderr);
 36771 #endif
 36772 #ifdef HAVE_WINDOW_SYSTEM
 36773   defsubr (&Stab_bar_height);
 36774   defsubr (&Stool_bar_height);
 36775   defsubr (&Slookup_image_map);
 36776 #endif
 36777   defsubr (&Sline_pixel_height);
 36778   defsubr (&Sformat_mode_line);
 36779   defsubr (&Sinvisible_p);
 36780   defsubr (&Scurrent_bidi_paragraph_direction);
 36781   defsubr (&Swindow_text_pixel_size);
 36782   defsubr (&Sbuffer_text_pixel_size);
 36783   defsubr (&Smove_point_visually);
 36784   defsubr (&Sbidi_find_overridden_directionality);
 36785   defsubr (&Sdisplay__line_is_continued_p);
 36786   defsubr (&Sget_display_property);
 36787   defsubr (&Slong_line_optimizations_p);
 36788 
 36789   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
 36790   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
 36791   DEFSYM (Qoverriding_local_map, "overriding-local-map");
 36792   DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
 36793   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
 36794   DEFSYM (Qeval, "eval");
 36795   DEFSYM (QCdata, ":data");
 36796 
 36797   /* Names of text properties relevant for redisplay.  */
 36798   DEFSYM (Qdisplay, "display");
 36799   DEFSYM (Qspace_width, "space-width");
 36800   DEFSYM (Qraise, "raise");
 36801   DEFSYM (Qslice, "slice");
 36802   DEFSYM (Qspace, "space");
 36803   DEFSYM (Qmargin, "margin");
 36804   DEFSYM (Qpointer, "pointer");
 36805   DEFSYM (Qleft_margin, "left-margin");
 36806   DEFSYM (Qright_margin, "right-margin");
 36807   DEFSYM (Qcenter, "center");
 36808   DEFSYM (Qline_height, "line-height");
 36809   DEFSYM (QCalign_to, ":align-to");
 36810   DEFSYM (QCrelative_width, ":relative-width");
 36811   DEFSYM (QCrelative_height, ":relative-height");
 36812   DEFSYM (QCeval, ":eval");
 36813   DEFSYM (QCpropertize, ":propertize");
 36814   DEFSYM (QCfile, ":file");
 36815   DEFSYM (Qfontified, "fontified");
 36816   DEFSYM (Qfontification_functions, "fontification-functions");
 36817   DEFSYM (Qlong_line_optimizations_in_fontification_functions,
 36818           "long-line-optimizations-in-fontification-functions");
 36819 
 36820   /* Name of the symbol which disables Lisp evaluation in 'display'
 36821      properties.  This is used by enriched.el.  */
 36822   DEFSYM (Qdisable_eval, "disable-eval");
 36823 
 36824   /* Name of the face used to highlight trailing whitespace.  */
 36825   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
 36826 
 36827   /* Names of the faces used to display line numbers.  */
 36828   DEFSYM (Qline_number, "line-number");
 36829   DEFSYM (Qline_number_current_line, "line-number-current-line");
 36830   DEFSYM (Qline_number_major_tick, "line-number-major-tick");
 36831   DEFSYM (Qline_number_minor_tick, "line-number-minor-tick");
 36832   /* Name of a text property which disables line-number display.  */
 36833   DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
 36834 
 36835   /* Name of the face used to display fill column indicator character.  */
 36836   DEFSYM (Qfill_column_indicator, "fill-column-indicator");
 36837 
 36838   /* Name and number of the face used to highlight escape glyphs.  */
 36839   DEFSYM (Qescape_glyph, "escape-glyph");
 36840 
 36841   /* Name and number of the face used to highlight non-breaking
 36842      spaces/hyphens.  */
 36843   DEFSYM (Qnobreak_space, "nobreak-space");
 36844   DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
 36845 
 36846   /* The symbol 'image' which is the car of the lists used to represent
 36847      images in Lisp.  Also a tool bar style.  */
 36848   DEFSYM (Qimage, "image");
 36849 
 36850   /* Tool bar styles.  */
 36851   DEFSYM (Qtext, "text");
 36852   DEFSYM (Qboth, "both");
 36853   DEFSYM (Qboth_horiz, "both-horiz");
 36854   DEFSYM (Qtext_image_horiz, "text-image-horiz");
 36855 
 36856   /* The image map types.  */
 36857   DEFSYM (QCmap, ":map");
 36858   DEFSYM (QCpointer, ":pointer");
 36859   DEFSYM (Qrect, "rect");
 36860   DEFSYM (Qcircle, "circle");
 36861   DEFSYM (Qpoly, "poly");
 36862 
 36863   DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
 36864 
 36865   DEFSYM (Qgrow_only, "grow-only");
 36866   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
 36867   DEFSYM (Qposition, "position");
 36868   DEFSYM (Qbuffer_position, "buffer-position");
 36869   DEFSYM (Qobject, "object");
 36870 
 36871   /* Cursor shapes.  */
 36872   DEFSYM (Qbar, "bar");
 36873   DEFSYM (Qhbar, "hbar");
 36874   DEFSYM (Qbox, "box");
 36875   DEFSYM (Qhollow, "hollow");
 36876 
 36877   /* Pointer shapes.  */
 36878   DEFSYM (Qhand, "hand");
 36879   DEFSYM (Qarrow, "arrow");
 36880   /* also Qtext */
 36881 
 36882   DEFSYM (Qdragging, "dragging");
 36883   DEFSYM (Qdropping, "dropping");
 36884   DEFSYM (Qdrag_source, "drag-source");
 36885 
 36886   DEFSYM (Qdrag_with_mode_line, "drag-with-mode-line");
 36887   DEFSYM (Qdrag_with_header_line, "drag-with-header-line");
 36888   DEFSYM (Qdrag_with_tab_line, "drag-with-tab-line");
 36889 
 36890   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 36891 
 36892   list_of_error = list1 (Qerror);
 36893   staticpro (&list_of_error);
 36894 
 36895   /* Values of those variables at last redisplay are stored as
 36896      properties on 'overlay-arrow-position' symbol.  However, if
 36897      Voverlay_arrow_position is a marker, last-arrow-position is its
 36898      numerical position.  */
 36899   DEFSYM (Qlast_arrow_position, "last-arrow-position");
 36900   DEFSYM (Qlast_arrow_string, "last-arrow-string");
 36901 
 36902   /* Alternative overlay-arrow-string and overlay-arrow-bitmap
 36903      properties on a symbol in overlay-arrow-variable-list.  */
 36904   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
 36905   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 36906 
 36907   echo_buffer[0] = echo_buffer[1] = Qnil;
 36908   staticpro (&echo_buffer[0]);
 36909   staticpro (&echo_buffer[1]);
 36910 
 36911   echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
 36912   staticpro (&echo_area_buffer[0]);
 36913   staticpro (&echo_area_buffer[1]);
 36914 
 36915   DEFVAR_LISP ("messages-buffer-name", Vmessages_buffer_name,
 36916     doc: /* The name of the buffer where messages are logged.
 36917 This is normally \"\*Messages*\", but can be rebound by packages that
 36918 wish to redirect messages to a different buffer.  (If the buffer
 36919 doesn't exist, it will be created and put into
 36920 `messages-buffer-mode'.)  */);
 36921   Vmessages_buffer_name = build_string ("*Messages*");
 36922 
 36923   mode_line_proptrans_alist = Qnil;
 36924   staticpro (&mode_line_proptrans_alist);
 36925   mode_line_string_list = Qnil;
 36926   staticpro (&mode_line_string_list);
 36927   mode_line_string_face = Qnil;
 36928   staticpro (&mode_line_string_face);
 36929   mode_line_string_face_prop = Qnil;
 36930   staticpro (&mode_line_string_face_prop);
 36931   Vmode_line_unwind_vector = Qnil;
 36932   staticpro (&Vmode_line_unwind_vector);
 36933 
 36934   DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
 36935 
 36936   help_echo_string = Qnil;
 36937   staticpro (&help_echo_string);
 36938   help_echo_object = Qnil;
 36939   staticpro (&help_echo_object);
 36940   help_echo_window = Qnil;
 36941   staticpro (&help_echo_window);
 36942   previous_help_echo_string = Qnil;
 36943   staticpro (&previous_help_echo_string);
 36944   help_echo_pos = -1;
 36945 
 36946   DEFSYM (Qright_to_left, "right-to-left");
 36947   DEFSYM (Qleft_to_right, "left-to-right");
 36948   defsubr (&Sbidi_resolved_levels);
 36949 
 36950 #ifdef HAVE_WINDOW_SYSTEM
 36951   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
 36952     doc: /* Non-nil means draw block cursor as wide as the glyph under it.
 36953 For example, if a block cursor is over a tab, it will be drawn as
 36954 wide as that tab on the display.  */);
 36955   x_stretch_cursor_p = 0;
 36956 #endif
 36957 
 36958   DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
 36959     doc: /* Non-nil means highlight trailing whitespace.
 36960 The face used for trailing whitespace is `trailing-whitespace'.  */);
 36961   Vshow_trailing_whitespace = Qnil;
 36962 
 36963   DEFVAR_LISP ("mode-line-compact", Vmode_line_compact,
 36964     doc: /* Non-nil means that mode lines should be compact.
 36965 This means that repeating spaces will be replaced with a single space.
 36966 If this variable is `long', only mode lines that are wider than the
 36967 currently selected window are compressed. */);
 36968   Vmode_line_compact = Qnil;
 36969   DEFSYM (Qlong, "long");
 36970 
 36971   DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
 36972     doc: /* Control highlighting of non-ASCII space and hyphen chars.
 36973 If the value is t, Emacs highlights non-ASCII chars which have the
 36974 same appearance as an ASCII space or hyphen, using the `nobreak-space'
 36975 or `nobreak-hyphen' face respectively.
 36976 
 36977 All of the non-ASCII characters in the Unicode horizontal whitespace
 36978 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36979 U+2011 (non-breaking hyphen) are affected.
 36980 
 36981 Any other non-nil value means to display these characters as an escape
 36982 glyph followed by an ordinary space or hyphen.
 36983 
 36984 A value of nil means no special handling of these characters.  */);
 36985   Vnobreak_char_display = Qt;
 36986 
 36987   DEFVAR_BOOL ("nobreak-char-ascii-display", nobreak_char_ascii_display,
 36988     doc: /* Control display of non-ASCII space and hyphen chars.
 36989 If the value of this variable is nil, the default, Emacs displays
 36990 non-ASCII chars which have the same appearance as an ASCII space
 36991 or hyphen as themselves, with the `nobreak-space' or `nobreak-hyphen'
 36992 face, respectively.
 36993 
 36994 If the value is t, these characters are displayed as their ASCII
 36995 counterparts: whitespace characters as ASCII space, hyphen characters
 36996 as ASCII hyphen (a.k.a. \"dash\"), using the `nobreak-space' or
 36997 the `nobreak-hyphen' face.
 36998 
 36999 This variable has effect only if `nobreak-char-display' is t;
 37000 otherwise it is ignored.
 37001 
 37002 All of the non-ASCII characters in the Unicode horizontal whitespace
 37003 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 37004 U+2011 (non-breaking hyphen) are affected.  */);
 37005   nobreak_char_ascii_display = false;
 37006 
 37007   DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
 37008     doc: /* The pointer shape to show in void text areas.
 37009 A value of nil means to show the text pointer.  Other options are
 37010 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
 37011 `hourglass'.  */);
 37012   Vvoid_text_area_pointer = Qarrow;
 37013 
 37014   DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
 37015     doc: /* Non-nil means don't actually do any redisplay.
 37016 This is used for internal purposes.  */);
 37017   Vinhibit_redisplay = Qnil;
 37018 
 37019   DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
 37020     doc: /* String (or mode line construct) included (normally) in `mode-line-misc-info'.  */);
 37021   Vglobal_mode_string = Qnil;
 37022 
 37023   DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
 37024     doc: /* Marker for where to display an arrow on top of the buffer text.
 37025 This must be the beginning of a line in order to work.
 37026 See also `overlay-arrow-string'.  */);
 37027   Voverlay_arrow_position = Qnil;
 37028 
 37029   DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
 37030     doc: /* String to display as an arrow in non-window frames.
 37031 See also `overlay-arrow-position'.  */);
 37032   Voverlay_arrow_string = build_pure_c_string ("=>");
 37033 
 37034   DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
 37035     doc: /* List of variables (symbols) which hold markers for overlay arrows.
 37036 The symbols on this list are examined during redisplay to determine
 37037 where to display overlay arrows.  */);
 37038   Voverlay_arrow_variable_list
 37039     = list1 (intern_c_string ("overlay-arrow-position"));
 37040 
 37041   DEFVAR_INT ("scroll-step", emacs_scroll_step,
 37042     doc: /* The number of lines to try scrolling a window by when point moves out.
 37043 If that fails to bring point back on frame, point is centered instead.
 37044 If this is zero, point is always centered after it moves off frame.
 37045 If you want scrolling to always be a line at a time, you should set
 37046 `scroll-conservatively' to a large value rather than set this to 1.  */);
 37047 
 37048   DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
 37049     doc: /* Scroll up to this many lines, to bring point back on screen.
 37050 If point moves off-screen, redisplay will scroll by up to
 37051 `scroll-conservatively' lines in order to bring point just barely
 37052 onto the screen again.  If that cannot be done, then redisplay
 37053 recenters point as usual.
 37054 
 37055 If the value is greater than 100, redisplay will never recenter point,
 37056 but will always scroll just enough text to bring point into view, even
 37057 if you move far away.
 37058 
 37059 A value of zero means always recenter point if it moves off screen.  */);
 37060   scroll_conservatively = 0;
 37061 
 37062   DEFVAR_INT ("scroll-margin", scroll_margin,
 37063     doc: /* Number of lines of margin at the top and bottom of a window.
 37064 Trigger automatic scrolling whenever point gets within this many lines
 37065 of the top or bottom of the window (see info node `Auto Scrolling').  */);
 37066   scroll_margin = 0;
 37067 
 37068   DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
 37069     doc: /* Maximum effective value of `scroll-margin'.
 37070 Given as a fraction of the current window's lines.  The value should
 37071 be a floating point number between 0.0 and 0.5.  The effective maximum
 37072 is limited to (/ (1- window-lines) 2).  Non-float values for this
 37073 variable are ignored and the default 0.25 is used instead.  */);
 37074   Vmaximum_scroll_margin = make_float (0.25);
 37075 
 37076   DEFVAR_LISP ("display-pixels-per-inch",  Vdisplay_pixels_per_inch,
 37077     doc: /* Pixels per inch value for non-window system displays.
 37078 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI).  */);
 37079   Vdisplay_pixels_per_inch = make_float (72.0);
 37080 
 37081 #ifdef GLYPH_DEBUG
 37082   DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask.  */);
 37083 #endif
 37084 
 37085   DEFVAR_LISP ("truncate-partial-width-windows",
 37086                Vtruncate_partial_width_windows,
 37087     doc: /* Non-nil means truncate lines in windows narrower than the frame.
 37088 For an integer value, truncate lines in each window narrower than the
 37089 full frame width, provided the total window width in column units is less
 37090 than that integer; otherwise, respect the value of `truncate-lines'.
 37091 The total width of the window is as returned by `window-total-width', it
 37092 includes the fringes, the continuation and truncation glyphs, the
 37093 display margins (if any), and the scroll bar
 37094 
 37095 For any other non-nil value, truncate lines in all windows that do
 37096 not span the full frame width.
 37097 
 37098 A value of nil means to respect the value of `truncate-lines'.
 37099 
 37100 If `word-wrap' is enabled, you might want to reduce the value of this.
 37101 
 37102 Don't set this to a non-nil value when `visual-line-mode' is
 37103 turned on, as it could produce confusing results.  */);
 37104   Vtruncate_partial_width_windows = make_fixnum (50);
 37105 
 37106   DEFVAR_BOOL("word-wrap-by-category", word_wrap_by_category, doc: /*
 37107     Non-nil means also wrap after characters of a certain category.
 37108 Normally when `word-wrap' is on, Emacs only breaks lines after
 37109 whitespace characters.  When this option is turned on, Emacs also
 37110 breaks lines after characters that have the "|" category (defined in
 37111 characters.el).  This is useful for allowing breaking after CJK
 37112 characters and improves the word-wrapping for CJK text mixed with
 37113 Latin text.
 37114 
 37115 If this variable is set using Customize, Emacs automatically loads
 37116 kinsoku.el.  When kinsoku.el is loaded, Emacs respects kinsoku rules
 37117 when breaking lines.  That means characters with the ">" category
 37118 don't appear at the beginning of a line (e.g., FULLWIDTH COMMA), and
 37119 characters with the "<" category don't appear at the end of a line
 37120 (e.g., LEFT DOUBLE ANGLE BRACKET).  */);
 37121   word_wrap_by_category = false;
 37122 
 37123   DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
 37124     doc: /* Maximum buffer size for which line number should be displayed.
 37125 If the buffer is bigger than this, the line number does not appear
 37126 in the mode line.  A value of nil means no limit.  */);
 37127   Vline_number_display_limit = Qnil;
 37128 
 37129   DEFVAR_INT ("line-number-display-limit-width",
 37130               line_number_display_limit_width,
 37131     doc: /* Maximum line width (in characters) for line number display.
 37132 If the average length of the lines near point is bigger than this, then the
 37133 line number may be omitted from the mode line.  */);
 37134   line_number_display_limit_width = 200;
 37135 
 37136   DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
 37137     doc: /* Non-nil means highlight active region even in nonselected windows.
 37138 When nil (the default), the active region is only highlighted when
 37139 the window is selected.  */);
 37140   highlight_nonselected_windows = false;
 37141 
 37142   DEFVAR_BOOL ("multiple-frames", multiple_frames,
 37143     doc: /* Non-nil if more than one frame is visible on this display.
 37144 Minibuffer-only frames don't count, but iconified frames do.
 37145 This variable is not guaranteed to be accurate except while processing
 37146 `frame-title-format' and `icon-title-format'.  */);
 37147 
 37148   DEFVAR_LISP ("frame-title-format", Vframe_title_format,
 37149     doc: /* Template for displaying the title bar of visible frames.
 37150 \(Assuming the window manager supports this feature.)
 37151 
 37152 This variable has the same structure as `mode-line-format', except that
 37153 the %c, %C, and %l constructs are ignored.  It is used only on frames for
 37154 which no explicit name has been set (see `modify-frame-parameters').  */);
 37155 
 37156   DEFVAR_LISP ("icon-title-format", Vicon_title_format,
 37157     doc: /* Template for displaying the title bar of an iconified frame.
 37158 \(Assuming the window manager supports this feature.)
 37159 If the value is a string, it should have the same structure
 37160 as `mode-line-format' (which see), and is used only on frames
 37161 for which no explicit name has been set \(see `modify-frame-parameters').
 37162 If the value is t, that means use `frame-title-format' for
 37163 iconified frames.  */);
 37164   /* Do not nest calls to pure_list.  This works around a bug in
 37165      Oracle Developer Studio 12.6.  */
 37166   Lisp_Object icon_title_name_format
 37167     = pure_list (empty_unibyte_string,
 37168                  build_pure_c_string ("%b - GNU Emacs at "),
 37169                  intern_c_string ("system-name"));
 37170   Vicon_title_format
 37171     = Vframe_title_format
 37172     = pure_list (intern_c_string ("multiple-frames"),
 37173                  build_pure_c_string ("%b"),
 37174                  icon_title_name_format);
 37175 
 37176   DEFVAR_LISP ("message-log-max", Vmessage_log_max,
 37177     doc: /* Maximum number of lines to keep in the message log buffer.
 37178 If nil, disable message logging.  If t, log messages but don't truncate
 37179 the buffer when it becomes large.  */);
 37180   Vmessage_log_max = make_fixnum (1000);
 37181 
 37182   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
 37183     doc: /* List of functions to call before redisplaying a window with scrolling.
 37184 Each function is called with two arguments, the window and its new
 37185 display-start position.
 37186 These functions are called whenever the `window-start' marker is modified,
 37187 either to point into another buffer (e.g. via `set-window-buffer') or another
 37188 place in the same buffer.
 37189 When each function is called, the `window-start' marker of its window
 37190 argument has been already set to the new value, and the buffer which that
 37191 window will display is set to be the current buffer.
 37192 Note that the value of `window-end' is not valid when these functions are
 37193 called.
 37194 
 37195 Warning: Do not use this feature to alter the way the window
 37196 is scrolled.  It is not designed for that, and such use probably won't
 37197 work.  */);
 37198   Vwindow_scroll_functions = Qnil;
 37199 
 37200   DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
 37201      doc: /* Non-nil means autoselect window with mouse pointer.
 37202 If nil, do not autoselect windows.
 37203 A positive number means delay autoselection by that many seconds: a
 37204 window is autoselected only after the mouse has remained in that
 37205 window for the duration of the delay.
 37206 A negative number has a similar effect, but causes windows to be
 37207 autoselected only after the mouse has stopped moving.  (Because of
 37208 the way Emacs compares mouse events, you will occasionally wait twice
 37209 that time before the window gets selected.)
 37210 Any other value means to autoselect window instantaneously when the
 37211 mouse pointer enters it.
 37212 
 37213 Autoselection selects the minibuffer only if it is active, and never
 37214 unselects the minibuffer if it is active.
 37215 
 37216 If you want to use the mouse to autoselect a window on another frame,
 37217 make sure that (1) your window manager has focus follow the mouse and
 37218 (2) the value of the option `focus-follows-mouse' matches the policy
 37219 of your window manager.  */);
 37220   Vmouse_autoselect_window = Qnil;
 37221 
 37222   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
 37223     doc: /* Non-nil means automatically resize tab-bars.
 37224 This dynamically changes the tab-bar's height to the minimum height
 37225 that is needed to make all tab-bar items visible.
 37226 If value is `grow-only', the tab-bar's height is only increased
 37227 automatically; to decrease the tab-bar height, use \\[recenter],
 37228 after setting `recenter-redisplay' to the value of t.  */);
 37229   Vauto_resize_tab_bars = Qt;
 37230 
 37231   DEFVAR_BOOL ("auto-raise-tab-bar-buttons", auto_raise_tab_bar_buttons_p,
 37232     doc: /* Non-nil means raise tab-bar buttons when the mouse moves over them.  */);
 37233   auto_raise_tab_bar_buttons_p = true;
 37234 
 37235   DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
 37236     doc: /* Non-nil means automatically resize tool-bars.
 37237 This dynamically changes the tool-bar's height to the minimum height
 37238 that is needed to make all tool-bar items visible.
 37239 If value is `grow-only', the tool-bar's height is only increased
 37240 automatically; to decrease the tool-bar height, use \\[recenter],
 37241 after setting `recenter-redisplay' to the value of t.  */);
 37242   Vauto_resize_tool_bars = Qt;
 37243 
 37244   DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
 37245     doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them.  */);
 37246   auto_raise_tool_bar_buttons_p = true;
 37247 
 37248   DEFVAR_LISP ("make-cursor-line-fully-visible", Vmake_cursor_line_fully_visible,
 37249     doc: /* Whether to scroll the window if the cursor line is not fully visible.
 37250 If the value is non-nil, Emacs scrolls or recenters the window to make
 37251 the cursor line fully visible.  The value could also be a function, which
 37252 is called with a single argument, the window to be scrolled, and should
 37253 return non-nil if the partially-visible cursor requires scrolling the
 37254 window, nil if it's okay to leave the cursor partially-visible.  */);
 37255   Vmake_cursor_line_fully_visible = Qt;
 37256   DEFSYM (Qmake_cursor_line_fully_visible, "make-cursor-line-fully-visible");
 37257 
 37258   DEFVAR_BOOL ("make-window-start-visible", make_window_start_visible,
 37259     doc: /* Whether to ensure `window-start' position is never invisible.  */);
 37260   make_window_start_visible = false;
 37261   DEFSYM (Qmake_window_start_visible, "make-window-start-visible");
 37262   Fmake_variable_buffer_local (Qmake_window_start_visible);
 37263 
 37264   DEFSYM (Qclose_tab, "close-tab");
 37265   DEFVAR_LISP ("tab-bar-border", Vtab_bar_border,
 37266     doc: /* Border below tab-bar in pixels.
 37267 If an integer, use it as the height of the border.
 37268 If it is one of `internal-border-width' or `border-width', use the
 37269 value of the corresponding frame parameter.
 37270 Otherwise, no border is added below the tab-bar.  */);
 37271   Vtab_bar_border = Qinternal_border_width;
 37272 
 37273   DEFVAR_LISP ("tab-bar-button-margin", Vtab_bar_button_margin,
 37274     doc: /* Margin around tab-bar buttons in pixels.
 37275 If an integer, use that for both horizontal and vertical margins.
 37276 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 37277 HORZ specifying the horizontal margin, and VERT specifying the
 37278 vertical margin.  */);
 37279   Vtab_bar_button_margin = make_fixnum (DEFAULT_TAB_BAR_BUTTON_MARGIN);
 37280 
 37281   DEFVAR_INT ("tab-bar-button-relief", tab_bar_button_relief,
 37282     doc: /* Relief thickness of tab-bar buttons.  */);
 37283   tab_bar_button_relief = DEFAULT_TAB_BAR_BUTTON_RELIEF;
 37284 
 37285   DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
 37286     doc: /* Border below tool-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 tool-bar.  */);
 37291   Vtool_bar_border = Qinternal_border_width;
 37292 
 37293   DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
 37294     doc: /* Margin around tool-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   Vtool_bar_button_margin = make_fixnum (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
 37300 
 37301   DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
 37302     doc: /* Relief thickness of tool-bar buttons.  */);
 37303   tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
 37304 
 37305   DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
 37306     doc: /* Tool bar style to use.
 37307 It can be one of
 37308  image            - show images only
 37309  text             - show text only
 37310  both             - show both, text below image
 37311  both-horiz       - show text to the right of the image
 37312  text-image-horiz - show text to the left of the image
 37313  any other        - use system default or image if no system default.
 37314 
 37315 This variable only affects the GTK+ toolkit version of Emacs.  */);
 37316   Vtool_bar_style = Qnil;
 37317 
 37318   DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
 37319     doc: /* Maximum number of characters a label can have to be shown.
 37320 The tool bar style must also show labels for this to have any effect, see
 37321 `tool-bar-style'.  */);
 37322   tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
 37323 
 37324   DEFVAR_LISP ("fontification-functions", Vfontification_functions,
 37325     doc: /* List of functions to call to fontify regions of text.
 37326 Each function is called with one argument POS.  Functions must
 37327 fontify a region starting at POS in the current buffer, and give
 37328 fontified regions the property `fontified' with a non-nil value.
 37329 
 37330 Note that, when `long-line-optimizations-p' is non-nil in the buffer,
 37331 these functions are called as if they were in a `with-restriction' form,
 37332 with a `long-line-optimizations-in-fontification-functions' label and
 37333 with the buffer narrowed to a portion around POS whose size is
 37334 specified by `long-line-optimizations-region-size'.  */);
 37335   Vfontification_functions = Qnil;
 37336   Fmake_variable_buffer_local (Qfontification_functions);
 37337 
 37338   DEFVAR_BOOL ("unibyte-display-via-language-environment",
 37339                unibyte_display_via_language_environment,
 37340     doc: /* Non-nil means display unibyte text according to language environment.
 37341 Specifically, this means that raw bytes in the range 160-255 decimal
 37342 are displayed by converting them to the equivalent multibyte characters
 37343 according to the current language environment.  As a result, they are
 37344 displayed according to the current fontset.
 37345 
 37346 Note that this variable affects only how these bytes are displayed,
 37347 but does not change the fact they are interpreted as raw bytes.  */);
 37348   unibyte_display_via_language_environment = false;
 37349 
 37350   DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
 37351     doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
 37352 If a float, it specifies the maximum height in units of the
 37353 mini-window frame's height.
 37354 If an integer, it specifies the maximum height in units of the
 37355 mini-window frame's default font's height.  */);
 37356   Vmax_mini_window_height = make_float (0.25);
 37357 
 37358   DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
 37359     doc: /* How to resize mini-windows (the minibuffer and the echo area).
 37360 A value of nil means don't automatically resize mini-windows.
 37361 A value of t means resize them to fit the text displayed in them.
 37362 A value of `grow-only', the default, means let mini-windows grow only;
 37363 they return to their normal size when the minibuffer is closed, or the
 37364 echo area becomes empty.
 37365 
 37366 This variable does not affect resizing of the minibuffer window of
 37367 minibuffer-only frames.  These are handled by `resize-mini-frames'
 37368 only.  */);
 37369   /* Contrary to the doc string, we initialize this to nil, so that
 37370      loading loadup.el won't try to resize windows before loading
 37371      window.el, where some functions we need to call for this live.
 37372      We assign the 'grow-only' value right after loading window.el
 37373      during loadup.  */
 37374   Vresize_mini_windows = Qnil;
 37375 
 37376   DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
 37377     doc: /* Alist specifying how to blink the cursor off.
 37378 Each element has the form (ON-STATE . OFF-STATE).  Whenever the
 37379 `cursor-type' frame-parameter or variable equals ON-STATE,
 37380 comparing using `equal', Emacs uses OFF-STATE to specify
 37381 how to blink it off.  ON-STATE and OFF-STATE are values for
 37382 the `cursor-type' frame parameter.
 37383 
 37384 If a frame's ON-STATE has no entry in this list,
 37385 the frame's other specifications determine how to blink the cursor off.  */);
 37386   Vblink_cursor_alist = Qnil;
 37387 
 37388   DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
 37389     doc: /* Allow or disallow automatic horizontal scrolling of windows.
 37390 The value `current-line' means the line displaying point in each window
 37391 is automatically scrolled horizontally to make point visible.
 37392 Any other non-nil value means all the lines in a window are automatically
 37393 scrolled horizontally to make point visible.  */);
 37394   automatic_hscrolling = Qt;
 37395   DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
 37396   DEFSYM (Qcurrent_line, "current-line");
 37397 
 37398   DEFVAR_INT ("hscroll-margin", hscroll_margin,
 37399     doc: /* How many columns away from the window edge point is allowed to get
 37400 before automatic hscrolling will horizontally scroll the window.  */);
 37401   hscroll_margin = 5;
 37402 
 37403   DEFVAR_LISP ("hscroll-step", Vhscroll_step,
 37404     doc: /* How many columns to scroll the window when point gets too close to the edge.
 37405 When point is less than `hscroll-margin' columns from the window
 37406 edge, automatic hscrolling will scroll the window by the amount of columns
 37407 determined by this variable.  If its value is a positive integer, scroll that
 37408 many columns.  If it's a positive floating-point number, it specifies the
 37409 fraction of the window's width to scroll.  If it's nil or zero, point will be
 37410 centered horizontally after the scroll.  Any other value, including negative
 37411 numbers, are treated as if the value were zero.
 37412 
 37413 Automatic hscrolling always moves point outside the scroll margin, so if
 37414 point was more than scroll step columns inside the margin, the window will
 37415 scroll more than the value given by the scroll step.
 37416 
 37417 Note that the lower bound for automatic hscrolling specified by `scroll-left'
 37418 and `scroll-right' overrides this variable's effect.  */);
 37419   Vhscroll_step = make_fixnum (0);
 37420 
 37421   DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
 37422     doc: /* If non-nil, messages are truncated when displaying the echo area.
 37423 Bind this around calls to `message' to let it take effect.  */);
 37424   message_truncate_lines = false;
 37425 
 37426   DEFVAR_LISP ("menu-bar-update-hook",  Vmenu_bar_update_hook,
 37427     doc: /* Normal hook run to update the menu bar definitions.
 37428 Redisplay runs this hook before it redisplays the menu bar.
 37429 This is used to update menus such as Buffers, whose contents depend on
 37430 various data.  */);
 37431   Vmenu_bar_update_hook = Qnil;
 37432 
 37433   DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
 37434                doc: /* Frame for which we are updating a menu.
 37435 The enable predicate for a menu binding should check this variable.  */);
 37436   Vmenu_updating_frame = Qnil;
 37437 
 37438   DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
 37439     doc: /* Non-nil means don't update menu bars.  Internal use only.  */);
 37440   inhibit_menubar_update = false;
 37441 
 37442   DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
 37443     doc: /* Prefix prepended to all continuation lines at display time.
 37444 The value may be a string, an image, or a stretch-glyph; it is
 37445 interpreted in the same way as the value of a `display' text property.
 37446 
 37447 This variable is overridden by any `wrap-prefix' text or overlay
 37448 property.
 37449 
 37450 To add a prefix to non-continuation lines, use `line-prefix'.  */);
 37451   Vwrap_prefix = Qnil;
 37452   DEFSYM (Qwrap_prefix, "wrap-prefix");
 37453   Fmake_variable_buffer_local (Qwrap_prefix);
 37454 
 37455   DEFVAR_LISP ("line-prefix", Vline_prefix,
 37456     doc: /* Prefix prepended to all non-continuation lines at display time.
 37457 The value may be a string, an image, or a stretch-glyph; it is
 37458 interpreted in the same way as the value of a `display' text property.
 37459 
 37460 This variable is overridden by any `line-prefix' text or overlay
 37461 property.
 37462 
 37463 To add a prefix to continuation lines, use `wrap-prefix'.  */);
 37464   Vline_prefix = Qnil;
 37465   DEFSYM (Qline_prefix, "line-prefix");
 37466   Fmake_variable_buffer_local (Qline_prefix);
 37467 
 37468   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
 37469     doc: /* Non-nil means display line numbers.
 37470 
 37471 If the value is t, display the absolute number of each line of a buffer
 37472 shown in a window.  Absolute line numbers count from the beginning of
 37473 the current narrowing, or from buffer beginning.  The variable
 37474 `display-line-numbers-offset', if non-zero, is a signed offset added
 37475 to each absolute line number; it also forces line numbers to be counted
 37476 from the beginning of the buffer, as if `display-line-numbers-widen'
 37477 were non-nil.  It has no effect when line numbers are not absolute.
 37478 
 37479 If the value is `relative', display for each line not containing the
 37480 window's point its relative number instead, i.e. the number of the line
 37481 relative to the line showing the window's point.
 37482 
 37483 In either case, line numbers are displayed at the beginning of each
 37484 non-continuation line that displays buffer text, i.e. after each newline
 37485 character that comes from the buffer.  The value `visual' is like
 37486 `relative' but counts screen lines instead of buffer lines.  In practice
 37487 this means that continuation lines count as well when calculating the
 37488 relative number of a line.
 37489 
 37490 Lisp programs can disable display of a line number of a particular
 37491 buffer line by putting the `display-line-numbers-disable' text property
 37492 or overlay property on the first visible character of that line.  */);
 37493   Vdisplay_line_numbers = Qnil;
 37494   DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
 37495   Fmake_variable_buffer_local (Qdisplay_line_numbers);
 37496   DEFSYM (Qrelative, "relative");
 37497   DEFSYM (Qvisual, "visual");
 37498 
 37499   DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
 37500     doc: /* Minimum width of space reserved for line number display.
 37501 A positive number means reserve that many columns for line numbers,
 37502 even if the actual number needs less space.
 37503 The default value of nil means compute the space dynamically.
 37504 Any other value is treated as nil.  */);
 37505   Vdisplay_line_numbers_width = Qnil;
 37506   DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
 37507   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 37508 
 37509   DEFVAR_LISP ("display-line-numbers-current-absolute",
 37510                Vdisplay_line_numbers_current_absolute,
 37511     doc: /* Non-nil means display absolute number of current line.
 37512 This variable has effect only when `display-line-numbers' is
 37513 either `relative' or `visual'.  */);
 37514   Vdisplay_line_numbers_current_absolute = Qt;
 37515 
 37516   DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
 37517     doc: /* Non-nil means display line numbers disregarding any narrowing.  */);
 37518   display_line_numbers_widen = false;
 37519   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
 37520   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 37521 
 37522   DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
 37523     doc: /* A signed integer added to each absolute line number.
 37524 When this variable is non-zero, line numbers are always counted from
 37525 the beginning of the buffer even if `display-line-numbers-widen' is nil.
 37526 It has no effect when set to 0, or when line numbers are not absolute.  */);
 37527   display_line_numbers_offset = 0;
 37528   DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
 37529   Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
 37530 
 37531   DEFVAR_BOOL ("display-fill-column-indicator", display_fill_column_indicator,
 37532     doc: /* Non-nil means display the fill column indicator.
 37533 If you set this non-nil, make sure `display-fill-column-indicator-character'
 37534 is also non-nil.
 37535 See Info node `Displaying Boundaries' for details.  */);
 37536   display_fill_column_indicator = false;
 37537   DEFSYM (Qdisplay_fill_column_indicator, "display-fill-column-indicator");
 37538   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator);
 37539 
 37540   DEFVAR_LISP ("display-fill-column-indicator-column", Vdisplay_fill_column_indicator_column,
 37541     doc: /* Column for indicator when `display-fill-column-indicator' is non-nil.
 37542 The default value is t which means that the indicator
 37543 will use the `fill-column' variable.  If it is set to an integer the
 37544 indicator will be drawn in that column.
 37545 See Info node `Displaying Boundaries' for details.  */);
 37546   Vdisplay_fill_column_indicator_column = Qt;
 37547   DEFSYM (Qdisplay_fill_column_indicator_column, "display-fill-column-indicator-column");
 37548   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_column);
 37549 
 37550   DEFVAR_LISP ("display-fill-column-indicator-character", Vdisplay_fill_column_indicator_character,
 37551     doc: /* Character to draw the indicator when `display-fill-column-indicator' is non-nil.
 37552 A good candidate is U+2502, and an alternative is (ascii 124) if the
 37553 font of `fill-column-indicator' face does not support Unicode characters.
 37554 See Info node `Displaying Boundaries' for details.  */);
 37555   Vdisplay_fill_column_indicator_character = Qnil;
 37556   DEFSYM (Qdisplay_fill_column_indicator_character, "display-fill-column-indicator-character");
 37557   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_character);
 37558 
 37559   DEFVAR_INT ("display-line-numbers-major-tick", display_line_numbers_major_tick,
 37560     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37561 The line number is shown with the `line-number-major-tick' face.
 37562 Otherwise, no special highlighting is done every Nth line.
 37563 Note that major ticks take precedence over minor ticks.  */);
 37564   display_line_numbers_major_tick = 0;
 37565 
 37566   DEFVAR_INT ("display-line-numbers-minor-tick", display_line_numbers_minor_tick,
 37567     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37568 The line number is shown with the `line-number-minor-tick' face.
 37569 Otherwise, no special highlighting is done every Nth line.
 37570 Note that major ticks take precedence over minor ticks.  */);
 37571   display_line_numbers_minor_tick = 0;
 37572 
 37573   DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
 37574     doc: /* Non-nil means don't eval Lisp during redisplay.  */);
 37575   inhibit_eval_during_redisplay = false;
 37576 
 37577   DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
 37578     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
 37579   inhibit_free_realized_faces = false;
 37580 
 37581   DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
 37582     doc: /* Non-nil means don't mirror characters even when bidi context requires that.
 37583 Intended for use during debugging and for testing bidi display;
 37584 see biditest.el in the test suite.  */);
 37585   inhibit_bidi_mirroring = false;
 37586 
 37587   DEFVAR_BOOL ("bidi-inhibit-bpa", bidi_inhibit_bpa,
 37588     doc: /* Non-nil means inhibit the Bidirectional Parentheses Algorithm.
 37589 Disabling the BPA makes redisplay faster, but might produce incorrect
 37590 display reordering of bidirectional text with embedded parentheses and
 37591 other bracket characters whose `paired-bracket' Unicode property is
 37592 non-nil, see `get-char-code-property'.  */);
 37593   bidi_inhibit_bpa = false;
 37594 
 37595 #ifdef GLYPH_DEBUG
 37596   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
 37597                doc: /* Inhibit try_window_id display optimization.  */);
 37598   inhibit_try_window_id = false;
 37599 
 37600   DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
 37601                doc: /* Inhibit try_window_reusing display optimization.  */);
 37602   inhibit_try_window_reusing = false;
 37603 
 37604   DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
 37605                doc: /* Inhibit try_cursor_movement display optimization.  */);
 37606   inhibit_try_cursor_movement = false;
 37607 #endif /* GLYPH_DEBUG */
 37608 
 37609   DEFVAR_INT ("overline-margin", overline_margin,
 37610                doc: /* Space between overline and text, in pixels.
 37611 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
 37612 margin to the character height.  */);
 37613   overline_margin = 2;
 37614 
 37615   DEFVAR_INT ("underline-minimum-offset",
 37616                underline_minimum_offset,
 37617      doc: /* Minimum distance between baseline and underline.
 37618 This can improve legibility of underlined text at small font sizes,
 37619 particularly when using variable `x-use-underline-position-properties'
 37620 with fonts that specify an UNDERLINE_POSITION relatively close to the
 37621 baseline.  The default value is 1.  */);
 37622   underline_minimum_offset = 1;
 37623   DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 37624 
 37625   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 37626                doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
 37627 This feature only works when on a window system that can change
 37628 cursor shapes.  */);
 37629   display_hourglass_p = true;
 37630 
 37631   DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
 37632                doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy.  */);
 37633   Vhourglass_delay = make_fixnum (DEFAULT_HOURGLASS_DELAY);
 37634 
 37635 #ifdef HAVE_WINDOW_SYSTEM
 37636   hourglass_atimer = NULL;
 37637   hourglass_shown_p = false;
 37638 #endif /* HAVE_WINDOW_SYSTEM */
 37639 
 37640   /* Name of the face used to display glyphless characters.  */
 37641   DEFSYM (Qglyphless_char, "glyphless-char");
 37642 
 37643   /* Method symbols for Vglyphless_char_display.  */
 37644   DEFSYM (Qhex_code, "hex-code");
 37645   DEFSYM (Qempty_box, "empty-box");
 37646   DEFSYM (Qthin_space, "thin-space");
 37647   DEFSYM (Qzero_width, "zero-width");
 37648 
 37649   DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
 37650                doc: /* Function run just before redisplay.
 37651 It is called with one argument, which is the set of windows that are to
 37652 be redisplayed.  This set can be nil (meaning, only the selected window),
 37653 or t (meaning all windows).  */);
 37654   Vpre_redisplay_function = intern ("ignore");
 37655 
 37656   /* Symbol for the purpose of Vglyphless_char_display.  */
 37657   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
 37658   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_fixnum (1));
 37659 
 37660   DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
 37661                doc: /* Char-table defining glyphless characters.
 37662 Each element, if non-nil, should be one of the following:
 37663   an ASCII acronym string: display this string in a box
 37664   `hex-code':   display the hexadecimal code of a character in a box
 37665   `empty-box':  display as an empty box
 37666   `thin-space': display as 1-pixel width space
 37667   `zero-width': don't display
 37668 Any other value is interpreted as `empty-box'.
 37669 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
 37670 display method for graphical terminals and text terminals respectively.
 37671 GRAPHICAL and TEXT should each have one of the values listed above.
 37672 
 37673 The char-table has one extra slot to control the display of characters
 37674 for which no font is found on graphical terminals, and characters that
 37675 cannot be displayed by text-mode terminals.  Its value should be an
 37676 ASCII acronym string, `hex-code', `empty-box', or `thin-space'.  It
 37677 could also be a cons cell of any two of these, to specify separate
 37678 values for graphical and text terminals.  The default is `empty-box'.
 37679 
 37680 With the obvious exception of `zero-width', all the other representations
 37681 are displayed using the face `glyphless-char'.
 37682 
 37683 If a character has a non-nil entry in an active display table, the
 37684 display table takes effect; in this case, Emacs does not consult
 37685 `glyphless-char-display' at all.  */);
 37686   Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
 37687   Fset_char_table_extra_slot (Vglyphless_char_display, make_fixnum (0),
 37688                               Qempty_box);
 37689 
 37690   DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
 37691                doc: /* If non-nil, debug if a message matching this regexp is displayed.  */);
 37692   Vdebug_on_message = Qnil;
 37693 
 37694   DEFVAR_LISP ("set-message-function", Vset_message_function,
 37695                doc: /* If non-nil, function to handle display of echo-area messages.
 37696 The function is called with one argument that is the text of a message.
 37697 If this function returns nil, the message is displayed in the echo area
 37698 as usual.  If the function returns a string, the returned string is
 37699 displayed in the echo area.  If this function returns any other non-nil
 37700 value, this means that the message was already handled, and the original
 37701 message text will not be displayed in the echo area.
 37702 
 37703 Also see `clear-message-function' (which can be used to clear the
 37704 message displayed by this function), and `command-error-function'
 37705 (which controls how error messages are displayed).  */);
 37706   Vset_message_function = Qnil;
 37707 
 37708   DEFSYM (Qdont_clear_message, "dont-clear-message");
 37709   DEFVAR_LISP ("clear-message-function", Vclear_message_function,
 37710                doc: /* If non-nil, function to clear echo-area messages.
 37711 Usually this function is called when the next input event arrives.
 37712 It is expected to clear the message displayed by its counterpart
 37713 function specified by `set-message-function'.
 37714 
 37715 The function is called without arguments.
 37716 
 37717 If this function returns a value that isn't `dont-clear-message', the
 37718 message is cleared from the echo area as usual.  If this function
 37719 returns `dont-clear-message', this means that the message was already
 37720 handled, and the original message text will not be cleared from the
 37721 echo area.  */);
 37722   Vclear_message_function = Qnil;
 37723 
 37724   DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
 37725                doc: /* Code of the cause for redisplaying all windows.
 37726 Internal use only.  */);
 37727   Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
 37728 
 37729   DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
 37730                doc: /* Code of the cause for redisplaying mode lines.
 37731 Internal use only.  */);
 37732   Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
 37733 
 37734   DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
 37735      doc: /* Non-nil means it is not safe to attempt bidi reordering for display.  */);
 37736   /* Initialize to t, since we need to disable reordering until
 37737      loadup.el successfully loads charprop.el.  */
 37738   redisplay__inhibit_bidi = true;
 37739 
 37740   DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
 37741     doc: /* Non-nil means display raw bytes in hexadecimal format.
 37742 The default is to use octal format (\\200) whereas hexadecimal (\\x80)
 37743 may be more familiar to users.  */);
 37744   display_raw_bytes_as_hex = false;
 37745 
 37746   DEFVAR_BOOL ("mouse-fine-grained-tracking", mouse_fine_grained_tracking,
 37747     doc: /* Non-nil for pixelwise mouse-movement.
 37748 When nil, mouse-movement events will not be generated as long as the
 37749 mouse stays within the extent of a single glyph (except for images).
 37750 When nil and `mouse-prefer-closest-glyph' is non-nil, mouse-movement
 37751 events will instead not be generated as long as the mouse stays within
 37752 the extent of a single left/right half glyph (except for images).  */);
 37753   mouse_fine_grained_tracking = false;
 37754 
 37755   DEFVAR_BOOL ("tab-bar--dragging-in-progress", tab_bar__dragging_in_progress,
 37756     doc: /* Non-nil when maybe dragging tab bar item.  */);
 37757   tab_bar__dragging_in_progress = false;
 37758 
 37759   DEFVAR_BOOL ("redisplay-skip-initial-frame", redisplay_skip_initial_frame,
 37760     doc: /* Non-nil means skip redisplay of the initial frame.
 37761 The initial frame is the text-mode frame used by Emacs internally during
 37762 the early stages of startup.  That frame is not displayed anywhere, so
 37763 skipping it is best except in special circumstances such as running
 37764 redisplay tests in batch mode.   */);
 37765   redisplay_skip_initial_frame = true;
 37766 
 37767   DEFVAR_BOOL ("redisplay-skip-fontification-on-input",
 37768                redisplay_skip_fontification_on_input,
 37769     doc: /* Skip `fontification_functions` when there is input pending.
 37770 If non-nil and there was input pending at the beginning of the command,
 37771 the `fontification_functions` hook is not run.  This usually does not
 37772 affect the display because redisplay is completely skipped anyway if input
 37773 was pending, but it can make scrolling smoother by avoiding
 37774 unnecessary fontification.
 37775 It is similar to `fast-but-imprecise-scrolling' with similar tradeoffs,
 37776 but with the advantage that it should only affect the behavior when Emacs
 37777 has trouble keeping up with the incoming input rate.  */);
 37778   redisplay_skip_fontification_on_input = false;
 37779 
 37780   DEFVAR_BOOL ("redisplay-adhoc-scroll-in-resize-mini-windows",
 37781                redisplay_adhoc_scroll_in_resize_mini_windows,
 37782     doc: /* If nil always use normal scrolling in minibuffer windows.
 37783 Otherwise, use custom-tailored code after resizing minibuffer windows to try
 37784 and display the most important part of the minibuffer.   */);
 37785   /* See bug#43519 for some discussion around this.  */
 37786   redisplay_adhoc_scroll_in_resize_mini_windows = true;
 37787 
 37788   DEFVAR_BOOL ("composition-break-at-point", composition_break_at_point,
 37789     doc: /* If non-nil, prevent auto-composition of characters around point.
 37790 This makes it easier to edit character sequences that are
 37791 composed on display.  */);
 37792   composition_break_at_point = false;
 37793 
 37794   DEFVAR_INT ("max-redisplay-ticks", max_redisplay_ticks,
 37795     doc: /* Maximum number of redisplay ticks before aborting redisplay of a window.
 37796 
 37797 This allows to abort the display of a window if the amount of low-level
 37798 redisplay operations exceeds the value of this variable.  When display of
 37799 a window is aborted due to this reason, the buffer shown in that window
 37800 will not have its windows redisplayed until the buffer is modified or until
 37801 you type \\[recenter-top-bottom] with one of its windows selected.
 37802 You can also decide to kill the buffer and visit it in some
 37803 other way, like under `so-long-mode' or literally.
 37804 
 37805 The default value is zero, which disables this feature.
 37806 The recommended non-zero value is between 100000 and 1000000,
 37807 depending on your patience and the speed of your system.  */);
 37808   max_redisplay_ticks = 0;
 37809 }
 37810 
 37811 
 37812 /* Initialize this module when Emacs starts.  */
 37813 
 37814 void
 37815 init_xdisp (void)
 37816 {
 37817   CHARPOS (this_line_start_pos) = 0;
 37818 
 37819   echo_area_window = minibuf_window;
 37820 
 37821   if (!noninteractive)
 37822     {
 37823       struct window *m = XWINDOW (minibuf_window);
 37824       Lisp_Object frame = m->frame;
 37825       struct frame *f = XFRAME (frame);
 37826       Lisp_Object root = FRAME_ROOT_WINDOW (f);
 37827       struct window *r = XWINDOW (root);
 37828       int i;
 37829 
 37830       r->top_line = FRAME_TOP_MARGIN (f);
 37831       r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
 37832       r->total_cols = FRAME_COLS (f);
 37833       r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
 37834       r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_MARGINS (f);
 37835       r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
 37836 
 37837       m->top_line = FRAME_TOTAL_LINES (f) - 1;
 37838       m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
 37839       m->total_cols = FRAME_COLS (f);
 37840       m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
 37841       m->total_lines = 1;
 37842       m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
 37843 
 37844       scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
 37845       scratch_glyph_row.glyphs[TEXT_AREA + 1]
 37846         = scratch_glyphs + MAX_SCRATCH_GLYPHS;
 37847 
 37848       /* The default ellipsis glyphs `...'.  */
 37849       for (i = 0; i < 3; ++i)
 37850         default_invis_vector[i] = make_fixnum ('.');
 37851     }
 37852 
 37853   {
 37854     /* Allocate the buffer for frame titles.
 37855        Also used for `format-mode-line'.  */
 37856     int size = 100;
 37857     mode_line_noprop_buf = xmalloc (size);
 37858     mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 37859     mode_line_noprop_ptr = mode_line_noprop_buf;
 37860     mode_line_target = MODE_LINE_DISPLAY;
 37861   }
 37862 
 37863   help_echo_showing_p = false;
 37864 }
 37865 
 37866 #ifdef HAVE_WINDOW_SYSTEM
 37867 
 37868 /* Platform-independent portion of hourglass implementation.  */
 37869 
 37870 /* Timer function of hourglass_atimer.  */
 37871 
 37872 static void
 37873 show_hourglass (struct atimer *timer)
 37874 {
 37875   /* The timer implementation will cancel this timer automatically
 37876      after this function has run.  Set hourglass_atimer to null
 37877      so that we know the timer doesn't have to be canceled.  */
 37878   hourglass_atimer = NULL;
 37879 
 37880   if (!hourglass_shown_p)
 37881     {
 37882       Lisp_Object tail, frame;
 37883 
 37884       block_input ();
 37885 
 37886       FOR_EACH_FRAME (tail, frame)
 37887         {
 37888           struct frame *f = XFRAME (frame);
 37889 
 37890           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37891               && FRAME_RIF (f)->show_hourglass)
 37892             FRAME_RIF (f)->show_hourglass (f);
 37893         }
 37894 
 37895       hourglass_shown_p = true;
 37896       unblock_input ();
 37897     }
 37898 }
 37899 
 37900 /* Cancel a currently active hourglass timer, and start a new one.  */
 37901 
 37902 void
 37903 start_hourglass (void)
 37904 {
 37905   struct timespec delay;
 37906 
 37907   cancel_hourglass ();
 37908 
 37909   if (FIXNUMP (Vhourglass_delay)
 37910       && XFIXNUM (Vhourglass_delay) > 0)
 37911     delay = make_timespec (min (XFIXNUM (Vhourglass_delay),
 37912                                   TYPE_MAXIMUM (time_t)),
 37913                            0);
 37914   else if (FLOATP (Vhourglass_delay)
 37915            && XFLOAT_DATA (Vhourglass_delay) > 0)
 37916     delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
 37917   else
 37918     delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
 37919 
 37920   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
 37921                                    show_hourglass, NULL);
 37922 }
 37923 
 37924 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
 37925    shown.  */
 37926 
 37927 void
 37928 cancel_hourglass (void)
 37929 {
 37930   if (hourglass_atimer)
 37931     {
 37932       cancel_atimer (hourglass_atimer);
 37933       hourglass_atimer = NULL;
 37934     }
 37935 
 37936   if (hourglass_shown_p)
 37937     {
 37938       Lisp_Object tail, frame;
 37939 
 37940       block_input ();
 37941 
 37942       FOR_EACH_FRAME (tail, frame)
 37943         {
 37944           struct frame *f = XFRAME (frame);
 37945 
 37946           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37947               && FRAME_RIF (f)->hide_hourglass)
 37948             FRAME_RIF (f)->hide_hourglass (f);
 37949 #ifdef HAVE_NTGUI
 37950           /* No cursors on non GUI frames - restore to stock arrow cursor.  */
 37951           else if (!FRAME_W32_P (f))
 37952             w32_arrow_cursor ();
 37953 #endif
 37954         }
 37955 
 37956       hourglass_shown_p = false;
 37957       unblock_input ();
 37958     }
 37959 }
 37960 
 37961 /* Return a correction to be applied to G->pixel_width when it is
 37962    displayed in MOUSE_FACE.  This is needed for the first and the last
 37963    glyphs of text inside a face with :box when it is displayed with
 37964    MOUSE_FACE that has a different or no :box attribute.
 37965    ORIGINAL_FACE is the face G was originally drawn in, and MOUSE_FACE
 37966    is the face it will be drawn in now.  ROW is the G's glyph row and
 37967    W is its window.  */
 37968 static int
 37969 adjust_glyph_width_for_mouse_face (struct glyph *g, struct glyph_row *row,
 37970                                    struct window *w,
 37971                                    struct face *original_face,
 37972                                    struct face *mouse_face)
 37973 {
 37974   int sum = 0;
 37975 
 37976   bool do_left_box_p = g->left_box_line_p;
 37977   bool do_right_box_p = g->right_box_line_p;
 37978 
 37979   /* This is required because we test some parameters of the image
 37980      slice before applying the box in produce_image_glyph.  */
 37981   if (g->type == IMAGE_GLYPH)
 37982     {
 37983       if (!row->reversed_p)
 37984         {
 37985           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37986                                              g->u.img_id);
 37987           do_left_box_p = g->left_box_line_p &&
 37988             g->slice.img.x == 0;
 37989           do_right_box_p = g->right_box_line_p &&
 37990             g->slice.img.x + g->slice.img.width == img->width;
 37991         }
 37992       else
 37993         {
 37994           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37995                                              g->u.img_id);
 37996           do_left_box_p = g->left_box_line_p &&
 37997             g->slice.img.x + g->slice.img.width == img->width;
 37998           do_right_box_p = g->right_box_line_p &&
 37999             g->slice.img.x == 0;
 38000         }
 38001     }
 38002 
 38003   /* If the glyph has a left box line, subtract it from the offset.  */
 38004   if (do_left_box_p)
 38005     sum -= max (0, original_face->box_vertical_line_width);
 38006   /* Likewise with the right box line, as there may be a
 38007      box there as well.  */
 38008   if (do_right_box_p)
 38009     sum -= max (0, original_face->box_vertical_line_width);
 38010   /* Now add the line widths from the new face.  */
 38011   if (g->left_box_line_p)
 38012     sum += max (0, mouse_face->box_vertical_line_width);
 38013   if (g->right_box_line_p)
 38014     sum += max (0, mouse_face->box_vertical_line_width);
 38015 
 38016   return sum;
 38017 }
 38018 
 38019 /* Get the offset due to mouse-highlight to apply before drawing
 38020    phys_cursor, and return it in OFFSET.  ROW should be the row that
 38021    is under mouse face and contains the phys cursor.
 38022 
 38023    This is required because the produce_XXX_glyph series of functions
 38024    add the width of the various vertical box lines to the total width
 38025    of the glyphs, but that must be updated when the row is put under
 38026    mouse face, which can have different box dimensions.  */
 38027 static void
 38028 get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
 38029                                   int *offset)
 38030 {
 38031   int sum = 0;
 38032   /* Return because the mode line can't possibly have a cursor. */
 38033   if (row->mode_line_p)
 38034     return;
 38035 
 38036   block_input ();
 38037 
 38038   struct frame *f = WINDOW_XFRAME (w);
 38039   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 38040   struct glyph *start, *end;
 38041   struct face *mouse_face = FACE_FROM_ID (f, hlinfo->mouse_face_face_id);
 38042   int hpos = w->phys_cursor.hpos;
 38043   end = &row->glyphs[TEXT_AREA][hpos];
 38044 
 38045   if (!row->reversed_p)
 38046     {
 38047       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 38048           hlinfo->mouse_face_beg_row)
 38049         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_beg_col];
 38050       else
 38051         start = row->glyphs[TEXT_AREA];
 38052     }
 38053   else
 38054     {
 38055       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 38056           hlinfo->mouse_face_end_row)
 38057         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_end_col];
 38058       else
 38059         start = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
 38060     }
 38061 
 38062   /* Calculate the offset by which to correct phys_cursor x if we are
 38063      drawing the cursor inside mouse-face highlighted text.  */
 38064 
 38065   for ( ; row->reversed_p ? start > end : start < end;
 38066           row->reversed_p ? --start : ++start)
 38067     sum += adjust_glyph_width_for_mouse_face (start, row, w,
 38068                                               FACE_FROM_ID (f, start->face_id),
 38069                                               mouse_face);
 38070 
 38071   if (row->reversed_p)
 38072     sum = -sum;
 38073 
 38074   *offset = sum;
 38075 
 38076   unblock_input ();
 38077 }
 38078 #endif /* HAVE_WINDOW_SYSTEM */

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