]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix display of glyphless characters with problematic fonts
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in 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. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
324
325 #define INFINITY 10000000
326
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
329
330 #ifdef HAVE_WINDOW_SYSTEM
331
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
334
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
342
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
345 #endif /* HAVE_WINDOW_SYSTEM */
346
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
350
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
362
363 /* True means print newline to stdout before next mini-buffer message. */
364
365 bool noninteractive_need_newline;
366
367 /* True means print newline to message log before next message. */
368
369 static bool message_log_need_newline;
370
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
377 \f
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
382
383 static struct text_pos this_line_start_pos;
384
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
387
388 static struct text_pos this_line_end_pos;
389
390 /* The vertical positions and the height of this line. */
391
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
395
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
398
399 static int this_line_start_x;
400
401 /* The smallest character position seen by move_it_* functions as they
402 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
403 hscrolled lines, see display_line. */
404
405 static struct text_pos this_line_min_pos;
406
407 /* Buffer that this_line_.* variables are referring to. */
408
409 static struct buffer *this_line_buffer;
410
411 /* True if an overlay arrow has been displayed in this window. */
412
413 static bool overlay_arrow_seen;
414
415 /* Vector containing glyphs for an ellipsis `...'. */
416
417 static Lisp_Object default_invis_vector[3];
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 static Lisp_Object Vmessage_stack;
431
432 /* True means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 static bool message_enable_multibyte;
436
437 /* Nonzero if we should redraw the mode lines on the next redisplay.
438 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
439 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
440 (the number used is then only used to track down the cause for this
441 full-redisplay). */
442
443 int update_mode_lines;
444
445 /* Nonzero if window sizes or contents other than selected-window have changed
446 since last redisplay that finished.
447 If it has value REDISPLAY_SOME, then only redisplay the windows where
448 the `redisplay' bit has been set. Otherwise, redisplay all windows
449 (the number used is then only used to track down the cause for this
450 full-redisplay). */
451
452 int windows_or_buffers_changed;
453
454 /* True after display_mode_line if %l was used and it displayed a
455 line number. */
456
457 static bool line_number_displayed;
458
459 /* The name of the *Messages* buffer, a string. */
460
461 static Lisp_Object Vmessages_buffer_name;
462
463 /* Current, index 0, and last displayed echo area message. Either
464 buffers from echo_buffers, or nil to indicate no message. */
465
466 Lisp_Object echo_area_buffer[2];
467
468 /* The buffers referenced from echo_area_buffer. */
469
470 static Lisp_Object echo_buffer[2];
471
472 /* A vector saved used in with_area_buffer to reduce consing. */
473
474 static Lisp_Object Vwith_echo_area_save_vector;
475
476 /* True means display_echo_area should display the last echo area
477 message again. Set by redisplay_preserve_echo_area. */
478
479 static bool display_last_displayed_message_p;
480
481 /* True if echo area is being used by print; false if being used by
482 message. */
483
484 static bool message_buf_print;
485
486 /* Set to true in clear_message to make redisplay_internal aware
487 of an emptied echo area. */
488
489 static bool message_cleared_p;
490
491 /* A scratch glyph row with contents used for generating truncation
492 glyphs. Also used in direct_output_for_insert. */
493
494 #define MAX_SCRATCH_GLYPHS 100
495 static struct glyph_row scratch_glyph_row;
496 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
497
498 /* Ascent and height of the last line processed by move_it_to. */
499
500 static int last_height;
501
502 /* True if there's a help-echo in the echo area. */
503
504 bool help_echo_showing_p;
505
506 /* The maximum distance to look ahead for text properties. Values
507 that are too small let us call compute_char_face and similar
508 functions too often which is expensive. Values that are too large
509 let us call compute_char_face and alike too often because we
510 might not be interested in text properties that far away. */
511
512 #define TEXT_PROP_DISTANCE_LIMIT 100
513
514 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
515 iterator state and later restore it. This is needed because the
516 bidi iterator on bidi.c keeps a stacked cache of its states, which
517 is really a singleton. When we use scratch iterator objects to
518 move around the buffer, we can cause the bidi cache to be pushed or
519 popped, and therefore we need to restore the cache state when we
520 return to the original iterator. */
521 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
522 do { \
523 if (CACHE) \
524 bidi_unshelve_cache (CACHE, true); \
525 ITCOPY = ITORIG; \
526 CACHE = bidi_shelve_cache (); \
527 } while (false)
528
529 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
530 do { \
531 if (pITORIG != pITCOPY) \
532 *(pITORIG) = *(pITCOPY); \
533 bidi_unshelve_cache (CACHE, false); \
534 CACHE = NULL; \
535 } while (false)
536
537 /* Functions to mark elements as needing redisplay. */
538 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
539
540 void
541 redisplay_other_windows (void)
542 {
543 if (!windows_or_buffers_changed)
544 windows_or_buffers_changed = REDISPLAY_SOME;
545 }
546
547 void
548 wset_redisplay (struct window *w)
549 {
550 /* Beware: selected_window can be nil during early stages. */
551 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
552 redisplay_other_windows ();
553 w->redisplay = true;
554 }
555
556 void
557 fset_redisplay (struct frame *f)
558 {
559 redisplay_other_windows ();
560 f->redisplay = true;
561 }
562
563 void
564 bset_redisplay (struct buffer *b)
565 {
566 int count = buffer_window_count (b);
567 if (count > 0)
568 {
569 /* ... it's visible in other window than selected, */
570 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
571 redisplay_other_windows ();
572 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
573 so that if we later set windows_or_buffers_changed, this buffer will
574 not be omitted. */
575 b->text->redisplay = true;
576 }
577 }
578
579 void
580 bset_update_mode_line (struct buffer *b)
581 {
582 if (!update_mode_lines)
583 update_mode_lines = REDISPLAY_SOME;
584 b->text->redisplay = true;
585 }
586
587 #ifdef GLYPH_DEBUG
588
589 /* True means print traces of redisplay if compiled with
590 GLYPH_DEBUG defined. */
591
592 bool trace_redisplay_p;
593
594 #endif /* GLYPH_DEBUG */
595
596 #ifdef DEBUG_TRACE_MOVE
597 /* True means trace with TRACE_MOVE to stderr. */
598 static bool trace_move;
599
600 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
601 #else
602 #define TRACE_MOVE(x) (void) 0
603 #endif
604
605 /* Buffer being redisplayed -- for redisplay_window_error. */
606
607 static struct buffer *displayed_buffer;
608
609 /* Value returned from text property handlers (see below). */
610
611 enum prop_handled
612 {
613 HANDLED_NORMALLY,
614 HANDLED_RECOMPUTE_PROPS,
615 HANDLED_OVERLAY_STRING_CONSUMED,
616 HANDLED_RETURN
617 };
618
619 /* A description of text properties that redisplay is interested
620 in. */
621
622 struct props
623 {
624 /* The symbol index of the name of the property. */
625 short name;
626
627 /* A unique index for the property. */
628 enum prop_idx idx;
629
630 /* A handler function called to set up iterator IT from the property
631 at IT's current position. Value is used to steer handle_stop. */
632 enum prop_handled (*handler) (struct it *it);
633 };
634
635 static enum prop_handled handle_face_prop (struct it *);
636 static enum prop_handled handle_invisible_prop (struct it *);
637 static enum prop_handled handle_display_prop (struct it *);
638 static enum prop_handled handle_composition_prop (struct it *);
639 static enum prop_handled handle_overlay_change (struct it *);
640 static enum prop_handled handle_fontified_prop (struct it *);
641
642 /* Properties handled by iterators. */
643
644 static struct props it_props[] =
645 {
646 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
647 /* Handle `face' before `display' because some sub-properties of
648 `display' need to know the face. */
649 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
650 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
651 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
652 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
653 {0, 0, NULL}
654 };
655
656 /* Value is the position described by X. If X is a marker, value is
657 the marker_position of X. Otherwise, value is X. */
658
659 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
660
661 /* Enumeration returned by some move_it_.* functions internally. */
662
663 enum move_it_result
664 {
665 /* Not used. Undefined value. */
666 MOVE_UNDEFINED,
667
668 /* Move ended at the requested buffer position or ZV. */
669 MOVE_POS_MATCH_OR_ZV,
670
671 /* Move ended at the requested X pixel position. */
672 MOVE_X_REACHED,
673
674 /* Move within a line ended at the end of a line that must be
675 continued. */
676 MOVE_LINE_CONTINUED,
677
678 /* Move within a line ended at the end of a line that would
679 be displayed truncated. */
680 MOVE_LINE_TRUNCATED,
681
682 /* Move within a line ended at a line end. */
683 MOVE_NEWLINE_OR_CR
684 };
685
686 /* This counter is used to clear the face cache every once in a while
687 in redisplay_internal. It is incremented for each redisplay.
688 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
689 cleared. */
690
691 #define CLEAR_FACE_CACHE_COUNT 500
692 static int clear_face_cache_count;
693
694 /* Similarly for the image cache. */
695
696 #ifdef HAVE_WINDOW_SYSTEM
697 #define CLEAR_IMAGE_CACHE_COUNT 101
698 static int clear_image_cache_count;
699
700 /* Null glyph slice */
701 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
702 #endif
703
704 /* True while redisplay_internal is in progress. */
705
706 bool redisplaying_p;
707
708 /* If a string, XTread_socket generates an event to display that string.
709 (The display is done in read_char.) */
710
711 Lisp_Object help_echo_string;
712 Lisp_Object help_echo_window;
713 Lisp_Object help_echo_object;
714 ptrdiff_t help_echo_pos;
715
716 /* Temporary variable for XTread_socket. */
717
718 Lisp_Object previous_help_echo_string;
719
720 /* Platform-independent portion of hourglass implementation. */
721
722 #ifdef HAVE_WINDOW_SYSTEM
723
724 /* True means an hourglass cursor is currently shown. */
725 static bool hourglass_shown_p;
726
727 /* If non-null, an asynchronous timer that, when it expires, displays
728 an hourglass cursor on all frames. */
729 static struct atimer *hourglass_atimer;
730
731 #endif /* HAVE_WINDOW_SYSTEM */
732
733 /* Default number of seconds to wait before displaying an hourglass
734 cursor. */
735 #define DEFAULT_HOURGLASS_DELAY 1
736
737 #ifdef HAVE_WINDOW_SYSTEM
738
739 /* Default pixel width of `thin-space' display method. */
740 #define THIN_SPACE_WIDTH 1
741
742 #endif /* HAVE_WINDOW_SYSTEM */
743
744 /* Function prototypes. */
745
746 static void setup_for_ellipsis (struct it *, int);
747 static void set_iterator_to_next (struct it *, bool);
748 static void mark_window_display_accurate_1 (struct window *, bool);
749 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
750 static bool cursor_row_p (struct glyph_row *);
751 static int redisplay_mode_lines (Lisp_Object, bool);
752
753 static void handle_line_prefix (struct it *);
754
755 static void handle_stop_backwards (struct it *, ptrdiff_t);
756 static void unwind_with_echo_area_buffer (Lisp_Object);
757 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
758 static bool current_message_1 (ptrdiff_t, Lisp_Object);
759 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
760 static void set_message (Lisp_Object);
761 static bool set_message_1 (ptrdiff_t, Lisp_Object);
762 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
763 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
764 static void unwind_redisplay (void);
765 static void extend_face_to_end_of_line (struct it *);
766 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
767 static void push_it (struct it *, struct text_pos *);
768 static void iterate_out_of_display_property (struct it *);
769 static void pop_it (struct it *);
770 static void redisplay_internal (void);
771 static bool echo_area_display (bool);
772 static void redisplay_windows (Lisp_Object);
773 static void redisplay_window (Lisp_Object, bool);
774 static Lisp_Object redisplay_window_error (Lisp_Object);
775 static Lisp_Object redisplay_window_0 (Lisp_Object);
776 static Lisp_Object redisplay_window_1 (Lisp_Object);
777 static bool set_cursor_from_row (struct window *, struct glyph_row *,
778 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
779 int, int);
780 static bool update_menu_bar (struct frame *, bool, bool);
781 static bool try_window_reusing_current_matrix (struct window *);
782 static int try_window_id (struct window *);
783 static bool display_line (struct it *);
784 static int display_mode_lines (struct window *);
785 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
786 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
787 Lisp_Object, bool);
788 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
789 Lisp_Object);
790 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
791 static void display_menu_bar (struct window *);
792 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
793 ptrdiff_t *);
794 static int display_string (const char *, Lisp_Object, Lisp_Object,
795 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
796 static void compute_line_metrics (struct it *);
797 static void run_redisplay_end_trigger_hook (struct it *);
798 static bool get_overlay_strings (struct it *, ptrdiff_t);
799 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
800 static void next_overlay_string (struct it *);
801 static void reseat (struct it *, struct text_pos, bool);
802 static void reseat_1 (struct it *, struct text_pos, bool);
803 static bool next_element_from_display_vector (struct it *);
804 static bool next_element_from_string (struct it *);
805 static bool next_element_from_c_string (struct it *);
806 static bool next_element_from_buffer (struct it *);
807 static bool next_element_from_composition (struct it *);
808 static bool next_element_from_image (struct it *);
809 static bool next_element_from_stretch (struct it *);
810 static void load_overlay_strings (struct it *, ptrdiff_t);
811 static bool get_next_display_element (struct it *);
812 static enum move_it_result
813 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
814 enum move_operation_enum);
815 static void get_visually_first_element (struct it *);
816 static void compute_stop_pos (struct it *);
817 static int face_before_or_after_it_pos (struct it *, bool);
818 static ptrdiff_t next_overlay_change (ptrdiff_t);
819 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
820 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
821 static int handle_single_display_spec (struct it *, Lisp_Object,
822 Lisp_Object, Lisp_Object,
823 struct text_pos *, ptrdiff_t, int, bool);
824 static int underlying_face_id (struct it *);
825
826 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
827 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
828
829 #ifdef HAVE_WINDOW_SYSTEM
830
831 static void update_tool_bar (struct frame *, bool);
832 static void x_draw_bottom_divider (struct window *w);
833 static void notice_overwritten_cursor (struct window *,
834 enum glyph_row_area,
835 int, int, int, int);
836 static void append_stretch_glyph (struct it *, Lisp_Object,
837 int, int, int);
838
839
840 #endif /* HAVE_WINDOW_SYSTEM */
841
842 static void produce_special_glyphs (struct it *, enum display_element_type);
843 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
844 static bool coords_in_mouse_face_p (struct window *, int, int);
845
846
847 \f
848 /***********************************************************************
849 Window display dimensions
850 ***********************************************************************/
851
852 /* Return the bottom boundary y-position for text lines in window W.
853 This is the first y position at which a line cannot start.
854 It is relative to the top of the window.
855
856 This is the height of W minus the height of a mode line, if any. */
857
858 int
859 window_text_bottom_y (struct window *w)
860 {
861 int height = WINDOW_PIXEL_HEIGHT (w);
862
863 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
864
865 if (WINDOW_WANTS_MODELINE_P (w))
866 height -= CURRENT_MODE_LINE_HEIGHT (w);
867
868 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
869
870 return height;
871 }
872
873 /* Return the pixel width of display area AREA of window W.
874 ANY_AREA means return the total width of W, not including
875 fringes to the left and right of the window. */
876
877 int
878 window_box_width (struct window *w, enum glyph_row_area area)
879 {
880 int width = w->pixel_width;
881
882 if (!w->pseudo_window_p)
883 {
884 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
885 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
886
887 if (area == TEXT_AREA)
888 width -= (WINDOW_MARGINS_WIDTH (w)
889 + WINDOW_FRINGES_WIDTH (w));
890 else if (area == LEFT_MARGIN_AREA)
891 width = WINDOW_LEFT_MARGIN_WIDTH (w);
892 else if (area == RIGHT_MARGIN_AREA)
893 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
894 }
895
896 /* With wide margins, fringes, etc. we might end up with a negative
897 width, correct that here. */
898 return max (0, width);
899 }
900
901
902 /* Return the pixel height of the display area of window W, not
903 including mode lines of W, if any. */
904
905 int
906 window_box_height (struct window *w)
907 {
908 struct frame *f = XFRAME (w->frame);
909 int height = WINDOW_PIXEL_HEIGHT (w);
910
911 eassert (height >= 0);
912
913 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
914 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
915
916 /* Note: the code below that determines the mode-line/header-line
917 height is essentially the same as that contained in the macro
918 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
919 the appropriate glyph row has its `mode_line_p' flag set,
920 and if it doesn't, uses estimate_mode_line_height instead. */
921
922 if (WINDOW_WANTS_MODELINE_P (w))
923 {
924 struct glyph_row *ml_row
925 = (w->current_matrix && w->current_matrix->rows
926 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
927 : 0);
928 if (ml_row && ml_row->mode_line_p)
929 height -= ml_row->height;
930 else
931 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
932 }
933
934 if (WINDOW_WANTS_HEADER_LINE_P (w))
935 {
936 struct glyph_row *hl_row
937 = (w->current_matrix && w->current_matrix->rows
938 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
939 : 0);
940 if (hl_row && hl_row->mode_line_p)
941 height -= hl_row->height;
942 else
943 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
944 }
945
946 /* With a very small font and a mode-line that's taller than
947 default, we might end up with a negative height. */
948 return max (0, height);
949 }
950
951 /* Return the window-relative coordinate of the left edge of display
952 area AREA of window W. ANY_AREA means return the left edge of the
953 whole window, to the right of the left fringe of W. */
954
955 int
956 window_box_left_offset (struct window *w, enum glyph_row_area area)
957 {
958 int x;
959
960 if (w->pseudo_window_p)
961 return 0;
962
963 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
964
965 if (area == TEXT_AREA)
966 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
967 + window_box_width (w, LEFT_MARGIN_AREA));
968 else if (area == RIGHT_MARGIN_AREA)
969 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
970 + window_box_width (w, LEFT_MARGIN_AREA)
971 + window_box_width (w, TEXT_AREA)
972 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
973 ? 0
974 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
975 else if (area == LEFT_MARGIN_AREA
976 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
977 x += WINDOW_LEFT_FRINGE_WIDTH (w);
978
979 /* Don't return more than the window's pixel width. */
980 return min (x, w->pixel_width);
981 }
982
983
984 /* Return the window-relative coordinate of the right edge of display
985 area AREA of window W. ANY_AREA means return the right edge of the
986 whole window, to the left of the right fringe of W. */
987
988 static int
989 window_box_right_offset (struct window *w, enum glyph_row_area area)
990 {
991 /* Don't return more than the window's pixel width. */
992 return min (window_box_left_offset (w, area) + window_box_width (w, area),
993 w->pixel_width);
994 }
995
996 /* Return the frame-relative coordinate of the left edge of display
997 area AREA of window W. ANY_AREA means return the left edge of the
998 whole window, to the right of the left fringe of W. */
999
1000 int
1001 window_box_left (struct window *w, enum glyph_row_area area)
1002 {
1003 struct frame *f = XFRAME (w->frame);
1004 int x;
1005
1006 if (w->pseudo_window_p)
1007 return FRAME_INTERNAL_BORDER_WIDTH (f);
1008
1009 x = (WINDOW_LEFT_EDGE_X (w)
1010 + window_box_left_offset (w, area));
1011
1012 return x;
1013 }
1014
1015
1016 /* Return the frame-relative coordinate of the right edge of display
1017 area AREA of window W. ANY_AREA means return the right edge of the
1018 whole window, to the left of the right fringe of W. */
1019
1020 int
1021 window_box_right (struct window *w, enum glyph_row_area area)
1022 {
1023 return window_box_left (w, area) + window_box_width (w, area);
1024 }
1025
1026 /* Get the bounding box of the display area AREA of window W, without
1027 mode lines, in frame-relative coordinates. ANY_AREA means the
1028 whole window, not including the left and right fringes of
1029 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1030 coordinates of the upper-left corner of the box. Return in
1031 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1032
1033 void
1034 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1035 int *box_y, int *box_width, int *box_height)
1036 {
1037 if (box_width)
1038 *box_width = window_box_width (w, area);
1039 if (box_height)
1040 *box_height = window_box_height (w);
1041 if (box_x)
1042 *box_x = window_box_left (w, area);
1043 if (box_y)
1044 {
1045 *box_y = WINDOW_TOP_EDGE_Y (w);
1046 if (WINDOW_WANTS_HEADER_LINE_P (w))
1047 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1048 }
1049 }
1050
1051 #ifdef HAVE_WINDOW_SYSTEM
1052
1053 /* Get the bounding box of the display area AREA of window W, without
1054 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1055 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1056 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1057 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1058 box. */
1059
1060 static void
1061 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1062 int *bottom_right_x, int *bottom_right_y)
1063 {
1064 window_box (w, ANY_AREA, top_left_x, top_left_y,
1065 bottom_right_x, bottom_right_y);
1066 *bottom_right_x += *top_left_x;
1067 *bottom_right_y += *top_left_y;
1068 }
1069
1070 #endif /* HAVE_WINDOW_SYSTEM */
1071
1072 /***********************************************************************
1073 Utilities
1074 ***********************************************************************/
1075
1076 /* Return the bottom y-position of the line the iterator IT is in.
1077 This can modify IT's settings. */
1078
1079 int
1080 line_bottom_y (struct it *it)
1081 {
1082 int line_height = it->max_ascent + it->max_descent;
1083 int line_top_y = it->current_y;
1084
1085 if (line_height == 0)
1086 {
1087 if (last_height)
1088 line_height = last_height;
1089 else if (IT_CHARPOS (*it) < ZV)
1090 {
1091 move_it_by_lines (it, 1);
1092 line_height = (it->max_ascent || it->max_descent
1093 ? it->max_ascent + it->max_descent
1094 : last_height);
1095 }
1096 else
1097 {
1098 struct glyph_row *row = it->glyph_row;
1099
1100 /* Use the default character height. */
1101 it->glyph_row = NULL;
1102 it->what = IT_CHARACTER;
1103 it->c = ' ';
1104 it->len = 1;
1105 PRODUCE_GLYPHS (it);
1106 line_height = it->ascent + it->descent;
1107 it->glyph_row = row;
1108 }
1109 }
1110
1111 return line_top_y + line_height;
1112 }
1113
1114 DEFUN ("line-pixel-height", Fline_pixel_height,
1115 Sline_pixel_height, 0, 0, 0,
1116 doc: /* Return height in pixels of text line in the selected window.
1117
1118 Value is the height in pixels of the line at point. */)
1119 (void)
1120 {
1121 struct it it;
1122 struct text_pos pt;
1123 struct window *w = XWINDOW (selected_window);
1124 struct buffer *old_buffer = NULL;
1125 Lisp_Object result;
1126
1127 if (XBUFFER (w->contents) != current_buffer)
1128 {
1129 old_buffer = current_buffer;
1130 set_buffer_internal_1 (XBUFFER (w->contents));
1131 }
1132 SET_TEXT_POS (pt, PT, PT_BYTE);
1133 start_display (&it, w, pt);
1134 it.vpos = it.current_y = 0;
1135 last_height = 0;
1136 result = make_number (line_bottom_y (&it));
1137 if (old_buffer)
1138 set_buffer_internal_1 (old_buffer);
1139
1140 return result;
1141 }
1142
1143 /* Return the default pixel height of text lines in window W. The
1144 value is the canonical height of the W frame's default font, plus
1145 any extra space required by the line-spacing variable or frame
1146 parameter.
1147
1148 Implementation note: this ignores any line-spacing text properties
1149 put on the newline characters. This is because those properties
1150 only affect the _screen_ line ending in the newline (i.e., in a
1151 continued line, only the last screen line will be affected), which
1152 means only a small number of lines in a buffer can ever use this
1153 feature. Since this function is used to compute the default pixel
1154 equivalent of text lines in a window, we can safely ignore those
1155 few lines. For the same reasons, we ignore the line-height
1156 properties. */
1157 int
1158 default_line_pixel_height (struct window *w)
1159 {
1160 struct frame *f = WINDOW_XFRAME (w);
1161 int height = FRAME_LINE_HEIGHT (f);
1162
1163 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1164 {
1165 struct buffer *b = XBUFFER (w->contents);
1166 Lisp_Object val = BVAR (b, extra_line_spacing);
1167
1168 if (NILP (val))
1169 val = BVAR (&buffer_defaults, extra_line_spacing);
1170 if (!NILP (val))
1171 {
1172 if (RANGED_INTEGERP (0, val, INT_MAX))
1173 height += XFASTINT (val);
1174 else if (FLOATP (val))
1175 {
1176 int addon = XFLOAT_DATA (val) * height + 0.5;
1177
1178 if (addon >= 0)
1179 height += addon;
1180 }
1181 }
1182 else
1183 height += f->extra_line_spacing;
1184 }
1185
1186 return height;
1187 }
1188
1189 /* Subroutine of pos_visible_p below. Extracts a display string, if
1190 any, from the display spec given as its argument. */
1191 static Lisp_Object
1192 string_from_display_spec (Lisp_Object spec)
1193 {
1194 if (CONSP (spec))
1195 {
1196 while (CONSP (spec))
1197 {
1198 if (STRINGP (XCAR (spec)))
1199 return XCAR (spec);
1200 spec = XCDR (spec);
1201 }
1202 }
1203 else if (VECTORP (spec))
1204 {
1205 ptrdiff_t i;
1206
1207 for (i = 0; i < ASIZE (spec); i++)
1208 {
1209 if (STRINGP (AREF (spec, i)))
1210 return AREF (spec, i);
1211 }
1212 return Qnil;
1213 }
1214
1215 return spec;
1216 }
1217
1218
1219 /* Limit insanely large values of W->hscroll on frame F to the largest
1220 value that will still prevent first_visible_x and last_visible_x of
1221 'struct it' from overflowing an int. */
1222 static int
1223 window_hscroll_limited (struct window *w, struct frame *f)
1224 {
1225 ptrdiff_t window_hscroll = w->hscroll;
1226 int window_text_width = window_box_width (w, TEXT_AREA);
1227 int colwidth = FRAME_COLUMN_WIDTH (f);
1228
1229 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1230 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1231
1232 return window_hscroll;
1233 }
1234
1235 /* Return true if position CHARPOS is visible in window W.
1236 CHARPOS < 0 means return info about WINDOW_END position.
1237 If visible, set *X and *Y to pixel coordinates of top left corner.
1238 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1239 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1240
1241 bool
1242 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1243 int *rtop, int *rbot, int *rowh, int *vpos)
1244 {
1245 struct it it;
1246 void *itdata = bidi_shelve_cache ();
1247 struct text_pos top;
1248 bool visible_p = false;
1249 struct buffer *old_buffer = NULL;
1250 bool r2l = false;
1251
1252 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1253 return visible_p;
1254
1255 if (XBUFFER (w->contents) != current_buffer)
1256 {
1257 old_buffer = current_buffer;
1258 set_buffer_internal_1 (XBUFFER (w->contents));
1259 }
1260
1261 SET_TEXT_POS_FROM_MARKER (top, w->start);
1262 /* Scrolling a minibuffer window via scroll bar when the echo area
1263 shows long text sometimes resets the minibuffer contents behind
1264 our backs. */
1265 if (CHARPOS (top) > ZV)
1266 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1267
1268 /* Compute exact mode line heights. */
1269 if (WINDOW_WANTS_MODELINE_P (w))
1270 w->mode_line_height
1271 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1272 BVAR (current_buffer, mode_line_format));
1273
1274 if (WINDOW_WANTS_HEADER_LINE_P (w))
1275 w->header_line_height
1276 = display_mode_line (w, HEADER_LINE_FACE_ID,
1277 BVAR (current_buffer, header_line_format));
1278
1279 start_display (&it, w, top);
1280 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1281 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1282
1283 if (charpos >= 0
1284 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1285 && IT_CHARPOS (it) >= charpos)
1286 /* When scanning backwards under bidi iteration, move_it_to
1287 stops at or _before_ CHARPOS, because it stops at or to
1288 the _right_ of the character at CHARPOS. */
1289 || (it.bidi_p && it.bidi_it.scan_dir == -1
1290 && IT_CHARPOS (it) <= charpos)))
1291 {
1292 /* We have reached CHARPOS, or passed it. How the call to
1293 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1294 or covered by a display property, move_it_to stops at the end
1295 of the invisible text, to the right of CHARPOS. (ii) If
1296 CHARPOS is in a display vector, move_it_to stops on its last
1297 glyph. */
1298 int top_x = it.current_x;
1299 int top_y = it.current_y;
1300 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1301 int bottom_y;
1302 struct it save_it;
1303 void *save_it_data = NULL;
1304
1305 /* Calling line_bottom_y may change it.method, it.position, etc. */
1306 SAVE_IT (save_it, it, save_it_data);
1307 last_height = 0;
1308 bottom_y = line_bottom_y (&it);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = true;
1313 if (bottom_y >= it.last_visible_y
1314 && it.bidi_p && it.bidi_it.scan_dir == -1
1315 && IT_CHARPOS (it) < charpos)
1316 {
1317 /* When the last line of the window is scanned backwards
1318 under bidi iteration, we could be duped into thinking
1319 that we have passed CHARPOS, when in fact move_it_to
1320 simply stopped short of CHARPOS because it reached
1321 last_visible_y. To see if that's what happened, we call
1322 move_it_to again with a slightly larger vertical limit,
1323 and see if it actually moved vertically; if it did, we
1324 didn't really reach CHARPOS, which is beyond window end. */
1325 /* Why 10? because we don't know how many canonical lines
1326 will the height of the next line(s) be. So we guess. */
1327 int ten_more_lines = 10 * default_line_pixel_height (w);
1328
1329 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1330 MOVE_TO_POS | MOVE_TO_Y);
1331 if (it.current_y > top_y)
1332 visible_p = false;
1333
1334 }
1335 RESTORE_IT (&it, &save_it, save_it_data);
1336 if (visible_p)
1337 {
1338 if (it.method == GET_FROM_DISPLAY_VECTOR)
1339 {
1340 /* We stopped on the last glyph of a display vector.
1341 Try and recompute. Hack alert! */
1342 if (charpos < 2 || top.charpos >= charpos)
1343 top_x = it.glyph_row->x;
1344 else
1345 {
1346 struct it it2, it2_prev;
1347 /* The idea is to get to the previous buffer
1348 position, consume the character there, and use
1349 the pixel coordinates we get after that. But if
1350 the previous buffer position is also displayed
1351 from a display vector, we need to consume all of
1352 the glyphs from that display vector. */
1353 start_display (&it2, w, top);
1354 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1355 /* If we didn't get to CHARPOS - 1, there's some
1356 replacing display property at that position, and
1357 we stopped after it. That is exactly the place
1358 whose coordinates we want. */
1359 if (IT_CHARPOS (it2) != charpos - 1)
1360 it2_prev = it2;
1361 else
1362 {
1363 /* Iterate until we get out of the display
1364 vector that displays the character at
1365 CHARPOS - 1. */
1366 do {
1367 get_next_display_element (&it2);
1368 PRODUCE_GLYPHS (&it2);
1369 it2_prev = it2;
1370 set_iterator_to_next (&it2, true);
1371 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1372 && IT_CHARPOS (it2) < charpos);
1373 }
1374 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1375 || it2_prev.current_x > it2_prev.last_visible_x)
1376 top_x = it.glyph_row->x;
1377 else
1378 {
1379 top_x = it2_prev.current_x;
1380 top_y = it2_prev.current_y;
1381 }
1382 }
1383 }
1384 else if (IT_CHARPOS (it) != charpos)
1385 {
1386 Lisp_Object cpos = make_number (charpos);
1387 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1388 Lisp_Object string = string_from_display_spec (spec);
1389 struct text_pos tpos;
1390 bool newline_in_string
1391 = (STRINGP (string)
1392 && memchr (SDATA (string), '\n', SBYTES (string)));
1393
1394 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1395 bool replacing_spec_p
1396 = (!NILP (spec)
1397 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1398 charpos, FRAME_WINDOW_P (it.f)));
1399 /* The tricky code below is needed because there's a
1400 discrepancy between move_it_to and how we set cursor
1401 when PT is at the beginning of a portion of text
1402 covered by a display property or an overlay with a
1403 display property, or the display line ends in a
1404 newline from a display string. move_it_to will stop
1405 _after_ such display strings, whereas
1406 set_cursor_from_row conspires with cursor_row_p to
1407 place the cursor on the first glyph produced from the
1408 display string. */
1409
1410 /* We have overshoot PT because it is covered by a
1411 display property that replaces the text it covers.
1412 If the string includes embedded newlines, we are also
1413 in the wrong display line. Backtrack to the correct
1414 line, where the display property begins. */
1415 if (replacing_spec_p)
1416 {
1417 Lisp_Object startpos, endpos;
1418 EMACS_INT start, end;
1419 struct it it3;
1420
1421 /* Find the first and the last buffer positions
1422 covered by the display string. */
1423 endpos =
1424 Fnext_single_char_property_change (cpos, Qdisplay,
1425 Qnil, Qnil);
1426 startpos =
1427 Fprevious_single_char_property_change (endpos, Qdisplay,
1428 Qnil, Qnil);
1429 start = XFASTINT (startpos);
1430 end = XFASTINT (endpos);
1431 /* Move to the last buffer position before the
1432 display property. */
1433 start_display (&it3, w, top);
1434 if (start > CHARPOS (top))
1435 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1436 /* Move forward one more line if the position before
1437 the display string is a newline or if it is the
1438 rightmost character on a line that is
1439 continued or word-wrapped. */
1440 if (it3.method == GET_FROM_BUFFER
1441 && (it3.c == '\n'
1442 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1443 move_it_by_lines (&it3, 1);
1444 else if (move_it_in_display_line_to (&it3, -1,
1445 it3.current_x
1446 + it3.pixel_width,
1447 MOVE_TO_X)
1448 == MOVE_LINE_CONTINUED)
1449 {
1450 move_it_by_lines (&it3, 1);
1451 /* When we are under word-wrap, the #$@%!
1452 move_it_by_lines moves 2 lines, so we need to
1453 fix that up. */
1454 if (it3.line_wrap == WORD_WRAP)
1455 move_it_by_lines (&it3, -1);
1456 }
1457
1458 /* Record the vertical coordinate of the display
1459 line where we wound up. */
1460 top_y = it3.current_y;
1461 if (it3.bidi_p)
1462 {
1463 /* When characters are reordered for display,
1464 the character displayed to the left of the
1465 display string could be _after_ the display
1466 property in the logical order. Use the
1467 smallest vertical position of these two. */
1468 start_display (&it3, w, top);
1469 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1470 if (it3.current_y < top_y)
1471 top_y = it3.current_y;
1472 }
1473 /* Move from the top of the window to the beginning
1474 of the display line where the display string
1475 begins. */
1476 start_display (&it3, w, top);
1477 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1478 /* If it3_moved stays false after the 'while' loop
1479 below, that means we already were at a newline
1480 before the loop (e.g., the display string begins
1481 with a newline), so we don't need to (and cannot)
1482 inspect the glyphs of it3.glyph_row, because
1483 PRODUCE_GLYPHS will not produce anything for a
1484 newline, and thus it3.glyph_row stays at its
1485 stale content it got at top of the window. */
1486 bool it3_moved = false;
1487 /* Finally, advance the iterator until we hit the
1488 first display element whose character position is
1489 CHARPOS, or until the first newline from the
1490 display string, which signals the end of the
1491 display line. */
1492 while (get_next_display_element (&it3))
1493 {
1494 PRODUCE_GLYPHS (&it3);
1495 if (IT_CHARPOS (it3) == charpos
1496 || ITERATOR_AT_END_OF_LINE_P (&it3))
1497 break;
1498 it3_moved = true;
1499 set_iterator_to_next (&it3, false);
1500 }
1501 top_x = it3.current_x - it3.pixel_width;
1502 /* Normally, we would exit the above loop because we
1503 found the display element whose character
1504 position is CHARPOS. For the contingency that we
1505 didn't, and stopped at the first newline from the
1506 display string, move back over the glyphs
1507 produced from the string, until we find the
1508 rightmost glyph not from the string. */
1509 if (it3_moved
1510 && newline_in_string
1511 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1512 {
1513 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1514 + it3.glyph_row->used[TEXT_AREA];
1515
1516 while (EQ ((g - 1)->object, string))
1517 {
1518 --g;
1519 top_x -= g->pixel_width;
1520 }
1521 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1522 + it3.glyph_row->used[TEXT_AREA]);
1523 }
1524 }
1525 }
1526
1527 *x = top_x;
1528 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1529 *rtop = max (0, window_top_y - top_y);
1530 *rbot = max (0, bottom_y - it.last_visible_y);
1531 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1532 - max (top_y, window_top_y)));
1533 *vpos = it.vpos;
1534 if (it.bidi_it.paragraph_dir == R2L)
1535 r2l = true;
1536 }
1537 }
1538 else
1539 {
1540 /* Either we were asked to provide info about WINDOW_END, or
1541 CHARPOS is in the partially visible glyph row at end of
1542 window. */
1543 struct it it2;
1544 void *it2data = NULL;
1545
1546 SAVE_IT (it2, it, it2data);
1547 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1548 move_it_by_lines (&it, 1);
1549 if (charpos < IT_CHARPOS (it)
1550 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1551 {
1552 visible_p = true;
1553 RESTORE_IT (&it2, &it2, it2data);
1554 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1555 *x = it2.current_x;
1556 *y = it2.current_y + it2.max_ascent - it2.ascent;
1557 *rtop = max (0, -it2.current_y);
1558 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1559 - it.last_visible_y));
1560 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1561 it.last_visible_y)
1562 - max (it2.current_y,
1563 WINDOW_HEADER_LINE_HEIGHT (w))));
1564 *vpos = it2.vpos;
1565 if (it2.bidi_it.paragraph_dir == R2L)
1566 r2l = true;
1567 }
1568 else
1569 bidi_unshelve_cache (it2data, true);
1570 }
1571 bidi_unshelve_cache (itdata, false);
1572
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1575
1576 if (visible_p)
1577 {
1578 if (w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582 /* For lines in an R2L paragraph, we need to mirror the X pixel
1583 coordinate wrt the text area. For the reasons, see the
1584 commentary in buffer_posn_from_coords and the explanation of
1585 the geometry used by the move_it_* functions at the end of
1586 the large commentary near the beginning of this file. */
1587 if (r2l)
1588 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1589 }
1590
1591 #if false
1592 /* Debugging code. */
1593 if (visible_p)
1594 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1595 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1596 else
1597 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1598 #endif
1599
1600 return visible_p;
1601 }
1602
1603
1604 /* Return the next character from STR. Return in *LEN the length of
1605 the character. This is like STRING_CHAR_AND_LENGTH but never
1606 returns an invalid character. If we find one, we return a `?', but
1607 with the length of the invalid character. */
1608
1609 static int
1610 string_char_and_length (const unsigned char *str, int *len)
1611 {
1612 int c;
1613
1614 c = STRING_CHAR_AND_LENGTH (str, *len);
1615 if (!CHAR_VALID_P (c))
1616 /* We may not change the length here because other places in Emacs
1617 don't use this function, i.e. they silently accept invalid
1618 characters. */
1619 c = '?';
1620
1621 return c;
1622 }
1623
1624
1625
1626 /* Given a position POS containing a valid character and byte position
1627 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1628
1629 static struct text_pos
1630 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1631 {
1632 eassert (STRINGP (string) && nchars >= 0);
1633
1634 if (STRING_MULTIBYTE (string))
1635 {
1636 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1637 int len;
1638
1639 while (nchars--)
1640 {
1641 string_char_and_length (p, &len);
1642 p += len;
1643 CHARPOS (pos) += 1;
1644 BYTEPOS (pos) += len;
1645 }
1646 }
1647 else
1648 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1649
1650 return pos;
1651 }
1652
1653
1654 /* Value is the text position, i.e. character and byte position,
1655 for character position CHARPOS in STRING. */
1656
1657 static struct text_pos
1658 string_pos (ptrdiff_t charpos, Lisp_Object string)
1659 {
1660 struct text_pos pos;
1661 eassert (STRINGP (string));
1662 eassert (charpos >= 0);
1663 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1664 return pos;
1665 }
1666
1667
1668 /* Value is a text position, i.e. character and byte position, for
1669 character position CHARPOS in C string S. MULTIBYTE_P
1670 means recognize multibyte characters. */
1671
1672 static struct text_pos
1673 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1674 {
1675 struct text_pos pos;
1676
1677 eassert (s != NULL);
1678 eassert (charpos >= 0);
1679
1680 if (multibyte_p)
1681 {
1682 int len;
1683
1684 SET_TEXT_POS (pos, 0, 0);
1685 while (charpos--)
1686 {
1687 string_char_and_length ((const unsigned char *) s, &len);
1688 s += len;
1689 CHARPOS (pos) += 1;
1690 BYTEPOS (pos) += len;
1691 }
1692 }
1693 else
1694 SET_TEXT_POS (pos, charpos, charpos);
1695
1696 return pos;
1697 }
1698
1699
1700 /* Value is the number of characters in C string S. MULTIBYTE_P
1701 means recognize multibyte characters. */
1702
1703 static ptrdiff_t
1704 number_of_chars (const char *s, bool multibyte_p)
1705 {
1706 ptrdiff_t nchars;
1707
1708 if (multibyte_p)
1709 {
1710 ptrdiff_t rest = strlen (s);
1711 int len;
1712 const unsigned char *p = (const unsigned char *) s;
1713
1714 for (nchars = 0; rest > 0; ++nchars)
1715 {
1716 string_char_and_length (p, &len);
1717 rest -= len, p += len;
1718 }
1719 }
1720 else
1721 nchars = strlen (s);
1722
1723 return nchars;
1724 }
1725
1726
1727 /* Compute byte position NEWPOS->bytepos corresponding to
1728 NEWPOS->charpos. POS is a known position in string STRING.
1729 NEWPOS->charpos must be >= POS.charpos. */
1730
1731 static void
1732 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1733 {
1734 eassert (STRINGP (string));
1735 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1736
1737 if (STRING_MULTIBYTE (string))
1738 *newpos = string_pos_nchars_ahead (pos, string,
1739 CHARPOS (*newpos) - CHARPOS (pos));
1740 else
1741 BYTEPOS (*newpos) = CHARPOS (*newpos);
1742 }
1743
1744 /* EXPORT:
1745 Return an estimation of the pixel height of mode or header lines on
1746 frame F. FACE_ID specifies what line's height to estimate. */
1747
1748 int
1749 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1750 {
1751 #ifdef HAVE_WINDOW_SYSTEM
1752 if (FRAME_WINDOW_P (f))
1753 {
1754 int height = FONT_HEIGHT (FRAME_FONT (f));
1755
1756 /* This function is called so early when Emacs starts that the face
1757 cache and mode line face are not yet initialized. */
1758 if (FRAME_FACE_CACHE (f))
1759 {
1760 struct face *face = FACE_FROM_ID (f, face_id);
1761 if (face)
1762 {
1763 if (face->font)
1764 height = FONT_HEIGHT (face->font);
1765 if (face->box_line_width > 0)
1766 height += 2 * face->box_line_width;
1767 }
1768 }
1769
1770 return height;
1771 }
1772 #endif
1773
1774 return 1;
1775 }
1776
1777 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1778 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1779 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1780 not force the value into range. */
1781
1782 void
1783 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1784 NativeRectangle *bounds, bool noclip)
1785 {
1786
1787 #ifdef HAVE_WINDOW_SYSTEM
1788 if (FRAME_WINDOW_P (f))
1789 {
1790 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1791 even for negative values. */
1792 if (pix_x < 0)
1793 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1794 if (pix_y < 0)
1795 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1796
1797 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1798 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1799
1800 if (bounds)
1801 STORE_NATIVE_RECT (*bounds,
1802 FRAME_COL_TO_PIXEL_X (f, pix_x),
1803 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1804 FRAME_COLUMN_WIDTH (f) - 1,
1805 FRAME_LINE_HEIGHT (f) - 1);
1806
1807 /* PXW: Should we clip pixels before converting to columns/lines? */
1808 if (!noclip)
1809 {
1810 if (pix_x < 0)
1811 pix_x = 0;
1812 else if (pix_x > FRAME_TOTAL_COLS (f))
1813 pix_x = FRAME_TOTAL_COLS (f);
1814
1815 if (pix_y < 0)
1816 pix_y = 0;
1817 else if (pix_y > FRAME_TOTAL_LINES (f))
1818 pix_y = FRAME_TOTAL_LINES (f);
1819 }
1820 }
1821 #endif
1822
1823 *x = pix_x;
1824 *y = pix_y;
1825 }
1826
1827
1828 /* Find the glyph under window-relative coordinates X/Y in window W.
1829 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1830 strings. Return in *HPOS and *VPOS the row and column number of
1831 the glyph found. Return in *AREA the glyph area containing X.
1832 Value is a pointer to the glyph found or null if X/Y is not on
1833 text, or we can't tell because W's current matrix is not up to
1834 date. */
1835
1836 static struct glyph *
1837 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1838 int *dx, int *dy, int *area)
1839 {
1840 struct glyph *glyph, *end;
1841 struct glyph_row *row = NULL;
1842 int x0, i;
1843
1844 /* Find row containing Y. Give up if some row is not enabled. */
1845 for (i = 0; i < w->current_matrix->nrows; ++i)
1846 {
1847 row = MATRIX_ROW (w->current_matrix, i);
1848 if (!row->enabled_p)
1849 return NULL;
1850 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1851 break;
1852 }
1853
1854 *vpos = i;
1855 *hpos = 0;
1856
1857 /* Give up if Y is not in the window. */
1858 if (i == w->current_matrix->nrows)
1859 return NULL;
1860
1861 /* Get the glyph area containing X. */
1862 if (w->pseudo_window_p)
1863 {
1864 *area = TEXT_AREA;
1865 x0 = 0;
1866 }
1867 else
1868 {
1869 if (x < window_box_left_offset (w, TEXT_AREA))
1870 {
1871 *area = LEFT_MARGIN_AREA;
1872 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1873 }
1874 else if (x < window_box_right_offset (w, TEXT_AREA))
1875 {
1876 *area = TEXT_AREA;
1877 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1878 }
1879 else
1880 {
1881 *area = RIGHT_MARGIN_AREA;
1882 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1883 }
1884 }
1885
1886 /* Find glyph containing X. */
1887 glyph = row->glyphs[*area];
1888 end = glyph + row->used[*area];
1889 x -= x0;
1890 while (glyph < end && x >= glyph->pixel_width)
1891 {
1892 x -= glyph->pixel_width;
1893 ++glyph;
1894 }
1895
1896 if (glyph == end)
1897 return NULL;
1898
1899 if (dx)
1900 {
1901 *dx = x;
1902 *dy = y - (row->y + row->ascent - glyph->ascent);
1903 }
1904
1905 *hpos = glyph - row->glyphs[*area];
1906 return glyph;
1907 }
1908
1909 /* Convert frame-relative x/y to coordinates relative to window W.
1910 Takes pseudo-windows into account. */
1911
1912 static void
1913 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1914 {
1915 if (w->pseudo_window_p)
1916 {
1917 /* A pseudo-window is always full-width, and starts at the
1918 left edge of the frame, plus a frame border. */
1919 struct frame *f = XFRAME (w->frame);
1920 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1921 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1922 }
1923 else
1924 {
1925 *x -= WINDOW_LEFT_EDGE_X (w);
1926 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1927 }
1928 }
1929
1930 #ifdef HAVE_WINDOW_SYSTEM
1931
1932 /* EXPORT:
1933 Return in RECTS[] at most N clipping rectangles for glyph string S.
1934 Return the number of stored rectangles. */
1935
1936 int
1937 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1938 {
1939 XRectangle r;
1940
1941 if (n <= 0)
1942 return 0;
1943
1944 if (s->row->full_width_p)
1945 {
1946 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1947 r.x = WINDOW_LEFT_EDGE_X (s->w);
1948 if (s->row->mode_line_p)
1949 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1950 else
1951 r.width = WINDOW_PIXEL_WIDTH (s->w);
1952
1953 /* Unless displaying a mode or menu bar line, which are always
1954 fully visible, clip to the visible part of the row. */
1955 if (s->w->pseudo_window_p)
1956 r.height = s->row->visible_height;
1957 else
1958 r.height = s->height;
1959 }
1960 else
1961 {
1962 /* This is a text line that may be partially visible. */
1963 r.x = window_box_left (s->w, s->area);
1964 r.width = window_box_width (s->w, s->area);
1965 r.height = s->row->visible_height;
1966 }
1967
1968 if (s->clip_head)
1969 if (r.x < s->clip_head->x)
1970 {
1971 if (r.width >= s->clip_head->x - r.x)
1972 r.width -= s->clip_head->x - r.x;
1973 else
1974 r.width = 0;
1975 r.x = s->clip_head->x;
1976 }
1977 if (s->clip_tail)
1978 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1979 {
1980 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1981 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1982 else
1983 r.width = 0;
1984 }
1985
1986 /* If S draws overlapping rows, it's sufficient to use the top and
1987 bottom of the window for clipping because this glyph string
1988 intentionally draws over other lines. */
1989 if (s->for_overlaps)
1990 {
1991 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1992 r.height = window_text_bottom_y (s->w) - r.y;
1993
1994 /* Alas, the above simple strategy does not work for the
1995 environments with anti-aliased text: if the same text is
1996 drawn onto the same place multiple times, it gets thicker.
1997 If the overlap we are processing is for the erased cursor, we
1998 take the intersection with the rectangle of the cursor. */
1999 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2000 {
2001 XRectangle rc, r_save = r;
2002
2003 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2004 rc.y = s->w->phys_cursor.y;
2005 rc.width = s->w->phys_cursor_width;
2006 rc.height = s->w->phys_cursor_height;
2007
2008 x_intersect_rectangles (&r_save, &rc, &r);
2009 }
2010 }
2011 else
2012 {
2013 /* Don't use S->y for clipping because it doesn't take partially
2014 visible lines into account. For example, it can be negative for
2015 partially visible lines at the top of a window. */
2016 if (!s->row->full_width_p
2017 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2018 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2019 else
2020 r.y = max (0, s->row->y);
2021 }
2022
2023 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2024
2025 /* If drawing the cursor, don't let glyph draw outside its
2026 advertised boundaries. Cleartype does this under some circumstances. */
2027 if (s->hl == DRAW_CURSOR)
2028 {
2029 struct glyph *glyph = s->first_glyph;
2030 int height, max_y;
2031
2032 if (s->x > r.x)
2033 {
2034 if (r.width >= s->x - r.x)
2035 r.width -= s->x - r.x;
2036 else /* R2L hscrolled row with cursor outside text area */
2037 r.width = 0;
2038 r.x = s->x;
2039 }
2040 r.width = min (r.width, glyph->pixel_width);
2041
2042 /* If r.y is below window bottom, ensure that we still see a cursor. */
2043 height = min (glyph->ascent + glyph->descent,
2044 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2045 max_y = window_text_bottom_y (s->w) - height;
2046 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2047 if (s->ybase - glyph->ascent > max_y)
2048 {
2049 r.y = max_y;
2050 r.height = height;
2051 }
2052 else
2053 {
2054 /* Don't draw cursor glyph taller than our actual glyph. */
2055 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2056 if (height < r.height)
2057 {
2058 max_y = r.y + r.height;
2059 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2060 r.height = min (max_y - r.y, height);
2061 }
2062 }
2063 }
2064
2065 if (s->row->clip)
2066 {
2067 XRectangle r_save = r;
2068
2069 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2070 r.width = 0;
2071 }
2072
2073 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2074 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2075 {
2076 #ifdef CONVERT_FROM_XRECT
2077 CONVERT_FROM_XRECT (r, *rects);
2078 #else
2079 *rects = r;
2080 #endif
2081 return 1;
2082 }
2083 else
2084 {
2085 /* If we are processing overlapping and allowed to return
2086 multiple clipping rectangles, we exclude the row of the glyph
2087 string from the clipping rectangle. This is to avoid drawing
2088 the same text on the environment with anti-aliasing. */
2089 #ifdef CONVERT_FROM_XRECT
2090 XRectangle rs[2];
2091 #else
2092 XRectangle *rs = rects;
2093 #endif
2094 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2095
2096 if (s->for_overlaps & OVERLAPS_PRED)
2097 {
2098 rs[i] = r;
2099 if (r.y + r.height > row_y)
2100 {
2101 if (r.y < row_y)
2102 rs[i].height = row_y - r.y;
2103 else
2104 rs[i].height = 0;
2105 }
2106 i++;
2107 }
2108 if (s->for_overlaps & OVERLAPS_SUCC)
2109 {
2110 rs[i] = r;
2111 if (r.y < row_y + s->row->visible_height)
2112 {
2113 if (r.y + r.height > row_y + s->row->visible_height)
2114 {
2115 rs[i].y = row_y + s->row->visible_height;
2116 rs[i].height = r.y + r.height - rs[i].y;
2117 }
2118 else
2119 rs[i].height = 0;
2120 }
2121 i++;
2122 }
2123
2124 n = i;
2125 #ifdef CONVERT_FROM_XRECT
2126 for (i = 0; i < n; i++)
2127 CONVERT_FROM_XRECT (rs[i], rects[i]);
2128 #endif
2129 return n;
2130 }
2131 }
2132
2133 /* EXPORT:
2134 Return in *NR the clipping rectangle for glyph string S. */
2135
2136 void
2137 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2138 {
2139 get_glyph_string_clip_rects (s, nr, 1);
2140 }
2141
2142
2143 /* EXPORT:
2144 Return the position and height of the phys cursor in window W.
2145 Set w->phys_cursor_width to width of phys cursor.
2146 */
2147
2148 void
2149 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2150 struct glyph *glyph, int *xp, int *yp, int *heightp)
2151 {
2152 struct frame *f = XFRAME (WINDOW_FRAME (w));
2153 int x, y, wd, h, h0, y0, ascent;
2154
2155 /* Compute the width of the rectangle to draw. If on a stretch
2156 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2157 rectangle as wide as the glyph, but use a canonical character
2158 width instead. */
2159 wd = glyph->pixel_width;
2160
2161 x = w->phys_cursor.x;
2162 if (x < 0)
2163 {
2164 wd += x;
2165 x = 0;
2166 }
2167
2168 if (glyph->type == STRETCH_GLYPH
2169 && !x_stretch_cursor_p)
2170 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2171 w->phys_cursor_width = wd;
2172
2173 /* Don't let the hollow cursor glyph descend below the glyph row's
2174 ascent value, lest the hollow cursor looks funny. */
2175 y = w->phys_cursor.y;
2176 ascent = row->ascent;
2177 if (row->ascent < glyph->ascent)
2178 {
2179 y =- glyph->ascent - row->ascent;
2180 ascent = glyph->ascent;
2181 }
2182
2183 /* If y is below window bottom, ensure that we still see a cursor. */
2184 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2185
2186 h = max (h0, ascent + glyph->descent);
2187 h0 = min (h0, ascent + glyph->descent);
2188
2189 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2190 if (y < y0)
2191 {
2192 h = max (h - (y0 - y) + 1, h0);
2193 y = y0 - 1;
2194 }
2195 else
2196 {
2197 y0 = window_text_bottom_y (w) - h0;
2198 if (y > y0)
2199 {
2200 h += y - y0;
2201 y = y0;
2202 }
2203 }
2204
2205 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2206 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2207 *heightp = h;
2208 }
2209
2210 /*
2211 * Remember which glyph the mouse is over.
2212 */
2213
2214 void
2215 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2216 {
2217 Lisp_Object window;
2218 struct window *w;
2219 struct glyph_row *r, *gr, *end_row;
2220 enum window_part part;
2221 enum glyph_row_area area;
2222 int x, y, width, height;
2223
2224 /* Try to determine frame pixel position and size of the glyph under
2225 frame pixel coordinates X/Y on frame F. */
2226
2227 if (window_resize_pixelwise)
2228 {
2229 width = height = 1;
2230 goto virtual_glyph;
2231 }
2232 else if (!f->glyphs_initialized_p
2233 || (window = window_from_coordinates (f, gx, gy, &part, false),
2234 NILP (window)))
2235 {
2236 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2237 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2238 goto virtual_glyph;
2239 }
2240
2241 w = XWINDOW (window);
2242 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2243 height = WINDOW_FRAME_LINE_HEIGHT (w);
2244
2245 x = window_relative_x_coord (w, part, gx);
2246 y = gy - WINDOW_TOP_EDGE_Y (w);
2247
2248 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2249 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2250
2251 if (w->pseudo_window_p)
2252 {
2253 area = TEXT_AREA;
2254 part = ON_MODE_LINE; /* Don't adjust margin. */
2255 goto text_glyph;
2256 }
2257
2258 switch (part)
2259 {
2260 case ON_LEFT_MARGIN:
2261 area = LEFT_MARGIN_AREA;
2262 goto text_glyph;
2263
2264 case ON_RIGHT_MARGIN:
2265 area = RIGHT_MARGIN_AREA;
2266 goto text_glyph;
2267
2268 case ON_HEADER_LINE:
2269 case ON_MODE_LINE:
2270 gr = (part == ON_HEADER_LINE
2271 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2272 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2273 gy = gr->y;
2274 area = TEXT_AREA;
2275 goto text_glyph_row_found;
2276
2277 case ON_TEXT:
2278 area = TEXT_AREA;
2279
2280 text_glyph:
2281 gr = 0; gy = 0;
2282 for (; r <= end_row && r->enabled_p; ++r)
2283 if (r->y + r->height > y)
2284 {
2285 gr = r; gy = r->y;
2286 break;
2287 }
2288
2289 text_glyph_row_found:
2290 if (gr && gy <= y)
2291 {
2292 struct glyph *g = gr->glyphs[area];
2293 struct glyph *end = g + gr->used[area];
2294
2295 height = gr->height;
2296 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2297 if (gx + g->pixel_width > x)
2298 break;
2299
2300 if (g < end)
2301 {
2302 if (g->type == IMAGE_GLYPH)
2303 {
2304 /* Don't remember when mouse is over image, as
2305 image may have hot-spots. */
2306 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2307 return;
2308 }
2309 width = g->pixel_width;
2310 }
2311 else
2312 {
2313 /* Use nominal char spacing at end of line. */
2314 x -= gx;
2315 gx += (x / width) * width;
2316 }
2317
2318 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2319 {
2320 gx += window_box_left_offset (w, area);
2321 /* Don't expand over the modeline to make sure the vertical
2322 drag cursor is shown early enough. */
2323 height = min (height,
2324 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2325 }
2326 }
2327 else
2328 {
2329 /* Use nominal line height at end of window. */
2330 gx = (x / width) * width;
2331 y -= gy;
2332 gy += (y / height) * height;
2333 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2334 /* See comment above. */
2335 height = min (height,
2336 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2337 }
2338 break;
2339
2340 case ON_LEFT_FRINGE:
2341 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2342 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2343 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2344 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2345 goto row_glyph;
2346
2347 case ON_RIGHT_FRINGE:
2348 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2349 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2350 : window_box_right_offset (w, TEXT_AREA));
2351 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2352 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2353 && !WINDOW_RIGHTMOST_P (w))
2354 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2355 /* Make sure the vertical border can get her own glyph to the
2356 right of the one we build here. */
2357 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2358 else
2359 width = WINDOW_PIXEL_WIDTH (w) - gx;
2360 else
2361 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2362
2363 goto row_glyph;
2364
2365 case ON_VERTICAL_BORDER:
2366 gx = WINDOW_PIXEL_WIDTH (w) - width;
2367 goto row_glyph;
2368
2369 case ON_VERTICAL_SCROLL_BAR:
2370 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2371 ? 0
2372 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2373 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2374 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2375 : 0)));
2376 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2377
2378 row_glyph:
2379 gr = 0, gy = 0;
2380 for (; r <= end_row && r->enabled_p; ++r)
2381 if (r->y + r->height > y)
2382 {
2383 gr = r; gy = r->y;
2384 break;
2385 }
2386
2387 if (gr && gy <= y)
2388 height = gr->height;
2389 else
2390 {
2391 /* Use nominal line height at end of window. */
2392 y -= gy;
2393 gy += (y / height) * height;
2394 }
2395 break;
2396
2397 case ON_RIGHT_DIVIDER:
2398 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2399 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2400 gy = 0;
2401 /* The bottom divider prevails. */
2402 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2403 goto add_edge;
2404
2405 case ON_BOTTOM_DIVIDER:
2406 gx = 0;
2407 width = WINDOW_PIXEL_WIDTH (w);
2408 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2409 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2410 goto add_edge;
2411
2412 default:
2413 ;
2414 virtual_glyph:
2415 /* If there is no glyph under the mouse, then we divide the screen
2416 into a grid of the smallest glyph in the frame, and use that
2417 as our "glyph". */
2418
2419 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2420 round down even for negative values. */
2421 if (gx < 0)
2422 gx -= width - 1;
2423 if (gy < 0)
2424 gy -= height - 1;
2425
2426 gx = (gx / width) * width;
2427 gy = (gy / height) * height;
2428
2429 goto store_rect;
2430 }
2431
2432 add_edge:
2433 gx += WINDOW_LEFT_EDGE_X (w);
2434 gy += WINDOW_TOP_EDGE_Y (w);
2435
2436 store_rect:
2437 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2438
2439 /* Visible feedback for debugging. */
2440 #if false && defined HAVE_X_WINDOWS
2441 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2442 f->output_data.x->normal_gc,
2443 gx, gy, width, height);
2444 #endif
2445 }
2446
2447
2448 #endif /* HAVE_WINDOW_SYSTEM */
2449
2450 static void
2451 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2452 {
2453 eassert (w);
2454 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2455 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2456 w->window_end_vpos
2457 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2458 }
2459
2460 /***********************************************************************
2461 Lisp form evaluation
2462 ***********************************************************************/
2463
2464 /* Error handler for safe_eval and safe_call. */
2465
2466 static Lisp_Object
2467 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2468 {
2469 add_to_log ("Error during redisplay: %S signaled %S",
2470 Flist (nargs, args), arg);
2471 return Qnil;
2472 }
2473
2474 /* Call function FUNC with the rest of NARGS - 1 arguments
2475 following. Return the result, or nil if something went
2476 wrong. Prevent redisplay during the evaluation. */
2477
2478 static Lisp_Object
2479 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2480 {
2481 Lisp_Object val;
2482
2483 if (inhibit_eval_during_redisplay)
2484 val = Qnil;
2485 else
2486 {
2487 ptrdiff_t i;
2488 ptrdiff_t count = SPECPDL_INDEX ();
2489 Lisp_Object *args;
2490 USE_SAFE_ALLOCA;
2491 SAFE_ALLOCA_LISP (args, nargs);
2492
2493 args[0] = func;
2494 for (i = 1; i < nargs; i++)
2495 args[i] = va_arg (ap, Lisp_Object);
2496
2497 specbind (Qinhibit_redisplay, Qt);
2498 if (inhibit_quit)
2499 specbind (Qinhibit_quit, Qt);
2500 /* Use Qt to ensure debugger does not run,
2501 so there is no possibility of wanting to redisplay. */
2502 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2503 safe_eval_handler);
2504 SAFE_FREE ();
2505 val = unbind_to (count, val);
2506 }
2507
2508 return val;
2509 }
2510
2511 Lisp_Object
2512 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2513 {
2514 Lisp_Object retval;
2515 va_list ap;
2516
2517 va_start (ap, func);
2518 retval = safe__call (false, nargs, func, ap);
2519 va_end (ap);
2520 return retval;
2521 }
2522
2523 /* Call function FN with one argument ARG.
2524 Return the result, or nil if something went wrong. */
2525
2526 Lisp_Object
2527 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2528 {
2529 return safe_call (2, fn, arg);
2530 }
2531
2532 static Lisp_Object
2533 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2534 {
2535 Lisp_Object retval;
2536 va_list ap;
2537
2538 va_start (ap, fn);
2539 retval = safe__call (inhibit_quit, 2, fn, ap);
2540 va_end (ap);
2541 return retval;
2542 }
2543
2544 Lisp_Object
2545 safe_eval (Lisp_Object sexpr)
2546 {
2547 return safe__call1 (false, Qeval, sexpr);
2548 }
2549
2550 static Lisp_Object
2551 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2552 {
2553 return safe__call1 (inhibit_quit, Qeval, sexpr);
2554 }
2555
2556 /* Call function FN with two arguments ARG1 and ARG2.
2557 Return the result, or nil if something went wrong. */
2558
2559 Lisp_Object
2560 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2561 {
2562 return safe_call (3, fn, arg1, arg2);
2563 }
2564
2565
2566 \f
2567 /***********************************************************************
2568 Debugging
2569 ***********************************************************************/
2570
2571 /* Define CHECK_IT to perform sanity checks on iterators.
2572 This is for debugging. It is too slow to do unconditionally. */
2573
2574 static void
2575 CHECK_IT (struct it *it)
2576 {
2577 #if false
2578 if (it->method == GET_FROM_STRING)
2579 {
2580 eassert (STRINGP (it->string));
2581 eassert (IT_STRING_CHARPOS (*it) >= 0);
2582 }
2583 else
2584 {
2585 eassert (IT_STRING_CHARPOS (*it) < 0);
2586 if (it->method == GET_FROM_BUFFER)
2587 {
2588 /* Check that character and byte positions agree. */
2589 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2590 }
2591 }
2592
2593 if (it->dpvec)
2594 eassert (it->current.dpvec_index >= 0);
2595 else
2596 eassert (it->current.dpvec_index < 0);
2597 #endif
2598 }
2599
2600
2601 /* Check that the window end of window W is what we expect it
2602 to be---the last row in the current matrix displaying text. */
2603
2604 static void
2605 CHECK_WINDOW_END (struct window *w)
2606 {
2607 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2608 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2609 {
2610 struct glyph_row *row;
2611 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2612 !row->enabled_p
2613 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2614 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2615 }
2616 #endif
2617 }
2618
2619 /***********************************************************************
2620 Iterator initialization
2621 ***********************************************************************/
2622
2623 /* Initialize IT for displaying current_buffer in window W, starting
2624 at character position CHARPOS. CHARPOS < 0 means that no buffer
2625 position is specified which is useful when the iterator is assigned
2626 a position later. BYTEPOS is the byte position corresponding to
2627 CHARPOS.
2628
2629 If ROW is not null, calls to produce_glyphs with IT as parameter
2630 will produce glyphs in that row.
2631
2632 BASE_FACE_ID is the id of a base face to use. It must be one of
2633 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2634 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2635 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2636
2637 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2638 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2639 will be initialized to use the corresponding mode line glyph row of
2640 the desired matrix of W. */
2641
2642 void
2643 init_iterator (struct it *it, struct window *w,
2644 ptrdiff_t charpos, ptrdiff_t bytepos,
2645 struct glyph_row *row, enum face_id base_face_id)
2646 {
2647 enum face_id remapped_base_face_id = base_face_id;
2648
2649 /* Some precondition checks. */
2650 eassert (w != NULL && it != NULL);
2651 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2652 && charpos <= ZV));
2653
2654 /* If face attributes have been changed since the last redisplay,
2655 free realized faces now because they depend on face definitions
2656 that might have changed. Don't free faces while there might be
2657 desired matrices pending which reference these faces. */
2658 if (face_change && !inhibit_free_realized_faces)
2659 {
2660 face_change = false;
2661 free_all_realized_faces (Qnil);
2662 }
2663
2664 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2665 if (! NILP (Vface_remapping_alist))
2666 remapped_base_face_id
2667 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2668
2669 /* Use one of the mode line rows of W's desired matrix if
2670 appropriate. */
2671 if (row == NULL)
2672 {
2673 if (base_face_id == MODE_LINE_FACE_ID
2674 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2675 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2676 else if (base_face_id == HEADER_LINE_FACE_ID)
2677 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2678 }
2679
2680 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2681 Other parts of redisplay rely on that. */
2682 memclear (it, sizeof *it);
2683 it->current.overlay_string_index = -1;
2684 it->current.dpvec_index = -1;
2685 it->base_face_id = remapped_base_face_id;
2686 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2687 it->paragraph_embedding = L2R;
2688 it->bidi_it.w = w;
2689
2690 /* The window in which we iterate over current_buffer: */
2691 XSETWINDOW (it->window, w);
2692 it->w = w;
2693 it->f = XFRAME (w->frame);
2694
2695 it->cmp_it.id = -1;
2696
2697 /* Extra space between lines (on window systems only). */
2698 if (base_face_id == DEFAULT_FACE_ID
2699 && FRAME_WINDOW_P (it->f))
2700 {
2701 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2702 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2703 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2704 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2705 * FRAME_LINE_HEIGHT (it->f));
2706 else if (it->f->extra_line_spacing > 0)
2707 it->extra_line_spacing = it->f->extra_line_spacing;
2708 }
2709
2710 /* If realized faces have been removed, e.g. because of face
2711 attribute changes of named faces, recompute them. When running
2712 in batch mode, the face cache of the initial frame is null. If
2713 we happen to get called, make a dummy face cache. */
2714 if (FRAME_FACE_CACHE (it->f) == NULL)
2715 init_frame_faces (it->f);
2716 if (FRAME_FACE_CACHE (it->f)->used == 0)
2717 recompute_basic_faces (it->f);
2718
2719 it->override_ascent = -1;
2720
2721 /* Are control characters displayed as `^C'? */
2722 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2723
2724 /* -1 means everything between a CR and the following line end
2725 is invisible. >0 means lines indented more than this value are
2726 invisible. */
2727 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2728 ? (clip_to_bounds
2729 (-1, XINT (BVAR (current_buffer, selective_display)),
2730 PTRDIFF_MAX))
2731 : (!NILP (BVAR (current_buffer, selective_display))
2732 ? -1 : 0));
2733 it->selective_display_ellipsis_p
2734 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2735
2736 /* Display table to use. */
2737 it->dp = window_display_table (w);
2738
2739 /* Are multibyte characters enabled in current_buffer? */
2740 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2741
2742 /* Get the position at which the redisplay_end_trigger hook should
2743 be run, if it is to be run at all. */
2744 if (MARKERP (w->redisplay_end_trigger)
2745 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2746 it->redisplay_end_trigger_charpos
2747 = marker_position (w->redisplay_end_trigger);
2748 else if (INTEGERP (w->redisplay_end_trigger))
2749 it->redisplay_end_trigger_charpos
2750 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2751 PTRDIFF_MAX);
2752
2753 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2754
2755 /* Are lines in the display truncated? */
2756 if (TRUNCATE != 0)
2757 it->line_wrap = TRUNCATE;
2758 if (base_face_id == DEFAULT_FACE_ID
2759 && !it->w->hscroll
2760 && (WINDOW_FULL_WIDTH_P (it->w)
2761 || NILP (Vtruncate_partial_width_windows)
2762 || (INTEGERP (Vtruncate_partial_width_windows)
2763 /* PXW: Shall we do something about this? */
2764 && (XINT (Vtruncate_partial_width_windows)
2765 <= WINDOW_TOTAL_COLS (it->w))))
2766 && NILP (BVAR (current_buffer, truncate_lines)))
2767 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2768 ? WINDOW_WRAP : WORD_WRAP;
2769
2770 /* Get dimensions of truncation and continuation glyphs. These are
2771 displayed as fringe bitmaps under X, but we need them for such
2772 frames when the fringes are turned off. But leave the dimensions
2773 zero for tooltip frames, as these glyphs look ugly there and also
2774 sabotage calculations of tooltip dimensions in x-show-tip. */
2775 #ifdef HAVE_WINDOW_SYSTEM
2776 if (!(FRAME_WINDOW_P (it->f)
2777 && FRAMEP (tip_frame)
2778 && it->f == XFRAME (tip_frame)))
2779 #endif
2780 {
2781 if (it->line_wrap == TRUNCATE)
2782 {
2783 /* We will need the truncation glyph. */
2784 eassert (it->glyph_row == NULL);
2785 produce_special_glyphs (it, IT_TRUNCATION);
2786 it->truncation_pixel_width = it->pixel_width;
2787 }
2788 else
2789 {
2790 /* We will need the continuation glyph. */
2791 eassert (it->glyph_row == NULL);
2792 produce_special_glyphs (it, IT_CONTINUATION);
2793 it->continuation_pixel_width = it->pixel_width;
2794 }
2795 }
2796
2797 /* Reset these values to zero because the produce_special_glyphs
2798 above has changed them. */
2799 it->pixel_width = it->ascent = it->descent = 0;
2800 it->phys_ascent = it->phys_descent = 0;
2801
2802 /* Set this after getting the dimensions of truncation and
2803 continuation glyphs, so that we don't produce glyphs when calling
2804 produce_special_glyphs, above. */
2805 it->glyph_row = row;
2806 it->area = TEXT_AREA;
2807
2808 /* Get the dimensions of the display area. The display area
2809 consists of the visible window area plus a horizontally scrolled
2810 part to the left of the window. All x-values are relative to the
2811 start of this total display area. */
2812 if (base_face_id != DEFAULT_FACE_ID)
2813 {
2814 /* Mode lines, menu bar in terminal frames. */
2815 it->first_visible_x = 0;
2816 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2817 }
2818 else
2819 {
2820 it->first_visible_x
2821 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2822 it->last_visible_x = (it->first_visible_x
2823 + window_box_width (w, TEXT_AREA));
2824
2825 /* If we truncate lines, leave room for the truncation glyph(s) at
2826 the right margin. Otherwise, leave room for the continuation
2827 glyph(s). Done only if the window has no right fringe. */
2828 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2829 {
2830 if (it->line_wrap == TRUNCATE)
2831 it->last_visible_x -= it->truncation_pixel_width;
2832 else
2833 it->last_visible_x -= it->continuation_pixel_width;
2834 }
2835
2836 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2837 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2838 }
2839
2840 /* Leave room for a border glyph. */
2841 if (!FRAME_WINDOW_P (it->f)
2842 && !WINDOW_RIGHTMOST_P (it->w))
2843 it->last_visible_x -= 1;
2844
2845 it->last_visible_y = window_text_bottom_y (w);
2846
2847 /* For mode lines and alike, arrange for the first glyph having a
2848 left box line if the face specifies a box. */
2849 if (base_face_id != DEFAULT_FACE_ID)
2850 {
2851 struct face *face;
2852
2853 it->face_id = remapped_base_face_id;
2854
2855 /* If we have a boxed mode line, make the first character appear
2856 with a left box line. */
2857 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2858 if (face && face->box != FACE_NO_BOX)
2859 it->start_of_box_run_p = true;
2860 }
2861
2862 /* If a buffer position was specified, set the iterator there,
2863 getting overlays and face properties from that position. */
2864 if (charpos >= BUF_BEG (current_buffer))
2865 {
2866 it->stop_charpos = charpos;
2867 it->end_charpos = ZV;
2868 eassert (charpos == BYTE_TO_CHAR (bytepos));
2869 IT_CHARPOS (*it) = charpos;
2870 IT_BYTEPOS (*it) = bytepos;
2871
2872 /* We will rely on `reseat' to set this up properly, via
2873 handle_face_prop. */
2874 it->face_id = it->base_face_id;
2875
2876 it->start = it->current;
2877 /* Do we need to reorder bidirectional text? Not if this is a
2878 unibyte buffer: by definition, none of the single-byte
2879 characters are strong R2L, so no reordering is needed. And
2880 bidi.c doesn't support unibyte buffers anyway. Also, don't
2881 reorder while we are loading loadup.el, since the tables of
2882 character properties needed for reordering are not yet
2883 available. */
2884 it->bidi_p =
2885 NILP (Vpurify_flag)
2886 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2887 && it->multibyte_p;
2888
2889 /* If we are to reorder bidirectional text, init the bidi
2890 iterator. */
2891 if (it->bidi_p)
2892 {
2893 /* Since we don't know at this point whether there will be
2894 any R2L lines in the window, we reserve space for
2895 truncation/continuation glyphs even if only the left
2896 fringe is absent. */
2897 if (base_face_id == DEFAULT_FACE_ID
2898 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2899 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2900 {
2901 if (it->line_wrap == TRUNCATE)
2902 it->last_visible_x -= it->truncation_pixel_width;
2903 else
2904 it->last_visible_x -= it->continuation_pixel_width;
2905 }
2906 /* Note the paragraph direction that this buffer wants to
2907 use. */
2908 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2909 Qleft_to_right))
2910 it->paragraph_embedding = L2R;
2911 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2912 Qright_to_left))
2913 it->paragraph_embedding = R2L;
2914 else
2915 it->paragraph_embedding = NEUTRAL_DIR;
2916 bidi_unshelve_cache (NULL, false);
2917 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2918 &it->bidi_it);
2919 }
2920
2921 /* Compute faces etc. */
2922 reseat (it, it->current.pos, true);
2923 }
2924
2925 CHECK_IT (it);
2926 }
2927
2928
2929 /* Initialize IT for the display of window W with window start POS. */
2930
2931 void
2932 start_display (struct it *it, struct window *w, struct text_pos pos)
2933 {
2934 struct glyph_row *row;
2935 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2936
2937 row = w->desired_matrix->rows + first_vpos;
2938 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2939 it->first_vpos = first_vpos;
2940
2941 /* Don't reseat to previous visible line start if current start
2942 position is in a string or image. */
2943 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2944 {
2945 int first_y = it->current_y;
2946
2947 /* If window start is not at a line start, skip forward to POS to
2948 get the correct continuation lines width. */
2949 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
2950 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2951 if (!start_at_line_beg_p)
2952 {
2953 int new_x;
2954
2955 reseat_at_previous_visible_line_start (it);
2956 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2957
2958 new_x = it->current_x + it->pixel_width;
2959
2960 /* If lines are continued, this line may end in the middle
2961 of a multi-glyph character (e.g. a control character
2962 displayed as \003, or in the middle of an overlay
2963 string). In this case move_it_to above will not have
2964 taken us to the start of the continuation line but to the
2965 end of the continued line. */
2966 if (it->current_x > 0
2967 && it->line_wrap != TRUNCATE /* Lines are continued. */
2968 && (/* And glyph doesn't fit on the line. */
2969 new_x > it->last_visible_x
2970 /* Or it fits exactly and we're on a window
2971 system frame. */
2972 || (new_x == it->last_visible_x
2973 && FRAME_WINDOW_P (it->f)
2974 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2975 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2976 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2977 {
2978 if ((it->current.dpvec_index >= 0
2979 || it->current.overlay_string_index >= 0)
2980 /* If we are on a newline from a display vector or
2981 overlay string, then we are already at the end of
2982 a screen line; no need to go to the next line in
2983 that case, as this line is not really continued.
2984 (If we do go to the next line, C-e will not DTRT.) */
2985 && it->c != '\n')
2986 {
2987 set_iterator_to_next (it, true);
2988 move_it_in_display_line_to (it, -1, -1, 0);
2989 }
2990
2991 it->continuation_lines_width += it->current_x;
2992 }
2993 /* If the character at POS is displayed via a display
2994 vector, move_it_to above stops at the final glyph of
2995 IT->dpvec. To make the caller redisplay that character
2996 again (a.k.a. start at POS), we need to reset the
2997 dpvec_index to the beginning of IT->dpvec. */
2998 else if (it->current.dpvec_index >= 0)
2999 it->current.dpvec_index = 0;
3000
3001 /* We're starting a new display line, not affected by the
3002 height of the continued line, so clear the appropriate
3003 fields in the iterator structure. */
3004 it->max_ascent = it->max_descent = 0;
3005 it->max_phys_ascent = it->max_phys_descent = 0;
3006
3007 it->current_y = first_y;
3008 it->vpos = 0;
3009 it->current_x = it->hpos = 0;
3010 }
3011 }
3012 }
3013
3014
3015 /* Return true if POS is a position in ellipses displayed for invisible
3016 text. W is the window we display, for text property lookup. */
3017
3018 static bool
3019 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3020 {
3021 Lisp_Object prop, window;
3022 bool ellipses_p = false;
3023 ptrdiff_t charpos = CHARPOS (pos->pos);
3024
3025 /* If POS specifies a position in a display vector, this might
3026 be for an ellipsis displayed for invisible text. We won't
3027 get the iterator set up for delivering that ellipsis unless
3028 we make sure that it gets aware of the invisible text. */
3029 if (pos->dpvec_index >= 0
3030 && pos->overlay_string_index < 0
3031 && CHARPOS (pos->string_pos) < 0
3032 && charpos > BEGV
3033 && (XSETWINDOW (window, w),
3034 prop = Fget_char_property (make_number (charpos),
3035 Qinvisible, window),
3036 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3037 {
3038 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3039 window);
3040 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3041 }
3042
3043 return ellipses_p;
3044 }
3045
3046
3047 /* Initialize IT for stepping through current_buffer in window W,
3048 starting at position POS that includes overlay string and display
3049 vector/ control character translation position information. Value
3050 is false if there are overlay strings with newlines at POS. */
3051
3052 static bool
3053 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3054 {
3055 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3056 int i;
3057 bool overlay_strings_with_newlines = false;
3058
3059 /* If POS specifies a position in a display vector, this might
3060 be for an ellipsis displayed for invisible text. We won't
3061 get the iterator set up for delivering that ellipsis unless
3062 we make sure that it gets aware of the invisible text. */
3063 if (in_ellipses_for_invisible_text_p (pos, w))
3064 {
3065 --charpos;
3066 bytepos = 0;
3067 }
3068
3069 /* Keep in mind: the call to reseat in init_iterator skips invisible
3070 text, so we might end up at a position different from POS. This
3071 is only a problem when POS is a row start after a newline and an
3072 overlay starts there with an after-string, and the overlay has an
3073 invisible property. Since we don't skip invisible text in
3074 display_line and elsewhere immediately after consuming the
3075 newline before the row start, such a POS will not be in a string,
3076 but the call to init_iterator below will move us to the
3077 after-string. */
3078 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3079
3080 /* This only scans the current chunk -- it should scan all chunks.
3081 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3082 to 16 in 22.1 to make this a lesser problem. */
3083 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3084 {
3085 const char *s = SSDATA (it->overlay_strings[i]);
3086 const char *e = s + SBYTES (it->overlay_strings[i]);
3087
3088 while (s < e && *s != '\n')
3089 ++s;
3090
3091 if (s < e)
3092 {
3093 overlay_strings_with_newlines = true;
3094 break;
3095 }
3096 }
3097
3098 /* If position is within an overlay string, set up IT to the right
3099 overlay string. */
3100 if (pos->overlay_string_index >= 0)
3101 {
3102 int relative_index;
3103
3104 /* If the first overlay string happens to have a `display'
3105 property for an image, the iterator will be set up for that
3106 image, and we have to undo that setup first before we can
3107 correct the overlay string index. */
3108 if (it->method == GET_FROM_IMAGE)
3109 pop_it (it);
3110
3111 /* We already have the first chunk of overlay strings in
3112 IT->overlay_strings. Load more until the one for
3113 pos->overlay_string_index is in IT->overlay_strings. */
3114 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3115 {
3116 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3117 it->current.overlay_string_index = 0;
3118 while (n--)
3119 {
3120 load_overlay_strings (it, 0);
3121 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3122 }
3123 }
3124
3125 it->current.overlay_string_index = pos->overlay_string_index;
3126 relative_index = (it->current.overlay_string_index
3127 % OVERLAY_STRING_CHUNK_SIZE);
3128 it->string = it->overlay_strings[relative_index];
3129 eassert (STRINGP (it->string));
3130 it->current.string_pos = pos->string_pos;
3131 it->method = GET_FROM_STRING;
3132 it->end_charpos = SCHARS (it->string);
3133 /* Set up the bidi iterator for this overlay string. */
3134 if (it->bidi_p)
3135 {
3136 it->bidi_it.string.lstring = it->string;
3137 it->bidi_it.string.s = NULL;
3138 it->bidi_it.string.schars = SCHARS (it->string);
3139 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3140 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3141 it->bidi_it.string.unibyte = !it->multibyte_p;
3142 it->bidi_it.w = it->w;
3143 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3144 FRAME_WINDOW_P (it->f), &it->bidi_it);
3145
3146 /* Synchronize the state of the bidi iterator with
3147 pos->string_pos. For any string position other than
3148 zero, this will be done automagically when we resume
3149 iteration over the string and get_visually_first_element
3150 is called. But if string_pos is zero, and the string is
3151 to be reordered for display, we need to resync manually,
3152 since it could be that the iteration state recorded in
3153 pos ended at string_pos of 0 moving backwards in string. */
3154 if (CHARPOS (pos->string_pos) == 0)
3155 {
3156 get_visually_first_element (it);
3157 if (IT_STRING_CHARPOS (*it) != 0)
3158 do {
3159 /* Paranoia. */
3160 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3161 bidi_move_to_visually_next (&it->bidi_it);
3162 } while (it->bidi_it.charpos != 0);
3163 }
3164 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3165 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3166 }
3167 }
3168
3169 if (CHARPOS (pos->string_pos) >= 0)
3170 {
3171 /* Recorded position is not in an overlay string, but in another
3172 string. This can only be a string from a `display' property.
3173 IT should already be filled with that string. */
3174 it->current.string_pos = pos->string_pos;
3175 eassert (STRINGP (it->string));
3176 if (it->bidi_p)
3177 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3178 FRAME_WINDOW_P (it->f), &it->bidi_it);
3179 }
3180
3181 /* Restore position in display vector translations, control
3182 character translations or ellipses. */
3183 if (pos->dpvec_index >= 0)
3184 {
3185 if (it->dpvec == NULL)
3186 get_next_display_element (it);
3187 eassert (it->dpvec && it->current.dpvec_index == 0);
3188 it->current.dpvec_index = pos->dpvec_index;
3189 }
3190
3191 CHECK_IT (it);
3192 return !overlay_strings_with_newlines;
3193 }
3194
3195
3196 /* Initialize IT for stepping through current_buffer in window W
3197 starting at ROW->start. */
3198
3199 static void
3200 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3201 {
3202 init_from_display_pos (it, w, &row->start);
3203 it->start = row->start;
3204 it->continuation_lines_width = row->continuation_lines_width;
3205 CHECK_IT (it);
3206 }
3207
3208
3209 /* Initialize IT for stepping through current_buffer in window W
3210 starting in the line following ROW, i.e. starting at ROW->end.
3211 Value is false if there are overlay strings with newlines at ROW's
3212 end position. */
3213
3214 static bool
3215 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3216 {
3217 bool success = false;
3218
3219 if (init_from_display_pos (it, w, &row->end))
3220 {
3221 if (row->continued_p)
3222 it->continuation_lines_width
3223 = row->continuation_lines_width + row->pixel_width;
3224 CHECK_IT (it);
3225 success = true;
3226 }
3227
3228 return success;
3229 }
3230
3231
3232
3233 \f
3234 /***********************************************************************
3235 Text properties
3236 ***********************************************************************/
3237
3238 /* Called when IT reaches IT->stop_charpos. Handle text property and
3239 overlay changes. Set IT->stop_charpos to the next position where
3240 to stop. */
3241
3242 static void
3243 handle_stop (struct it *it)
3244 {
3245 enum prop_handled handled;
3246 bool handle_overlay_change_p;
3247 struct props *p;
3248
3249 it->dpvec = NULL;
3250 it->current.dpvec_index = -1;
3251 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3252 it->ellipsis_p = false;
3253
3254 /* Use face of preceding text for ellipsis (if invisible) */
3255 if (it->selective_display_ellipsis_p)
3256 it->saved_face_id = it->face_id;
3257
3258 /* Here's the description of the semantics of, and the logic behind,
3259 the various HANDLED_* statuses:
3260
3261 HANDLED_NORMALLY means the handler did its job, and the loop
3262 should proceed to calling the next handler in order.
3263
3264 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3265 change in the properties and overlays at current position, so the
3266 loop should be restarted, to re-invoke the handlers that were
3267 already called. This happens when fontification-functions were
3268 called by handle_fontified_prop, and actually fontified
3269 something. Another case where HANDLED_RECOMPUTE_PROPS is
3270 returned is when we discover overlay strings that need to be
3271 displayed right away. The loop below will continue for as long
3272 as the status is HANDLED_RECOMPUTE_PROPS.
3273
3274 HANDLED_RETURN means return immediately to the caller, to
3275 continue iteration without calling any further handlers. This is
3276 used when we need to act on some property right away, for example
3277 when we need to display the ellipsis or a replacing display
3278 property, such as display string or image.
3279
3280 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3281 consumed, and the handler switched to the next overlay string.
3282 This signals the loop below to refrain from looking for more
3283 overlays before all the overlay strings of the current overlay
3284 are processed.
3285
3286 Some of the handlers called by the loop push the iterator state
3287 onto the stack (see 'push_it'), and arrange for the iteration to
3288 continue with another object, such as an image, a display string,
3289 or an overlay string. In most such cases, it->stop_charpos is
3290 set to the first character of the string, so that when the
3291 iteration resumes, this function will immediately be called
3292 again, to examine the properties at the beginning of the string.
3293
3294 When a display or overlay string is exhausted, the iterator state
3295 is popped (see 'pop_it'), and iteration continues with the
3296 previous object. Again, in many such cases this function is
3297 called again to find the next position where properties might
3298 change. */
3299
3300 do
3301 {
3302 handled = HANDLED_NORMALLY;
3303
3304 /* Call text property handlers. */
3305 for (p = it_props; p->handler; ++p)
3306 {
3307 handled = p->handler (it);
3308
3309 if (handled == HANDLED_RECOMPUTE_PROPS)
3310 break;
3311 else if (handled == HANDLED_RETURN)
3312 {
3313 /* We still want to show before and after strings from
3314 overlays even if the actual buffer text is replaced. */
3315 if (!handle_overlay_change_p
3316 || it->sp > 1
3317 /* Don't call get_overlay_strings_1 if we already
3318 have overlay strings loaded, because doing so
3319 will load them again and push the iterator state
3320 onto the stack one more time, which is not
3321 expected by the rest of the code that processes
3322 overlay strings. */
3323 || (it->current.overlay_string_index < 0
3324 && !get_overlay_strings_1 (it, 0, false)))
3325 {
3326 if (it->ellipsis_p)
3327 setup_for_ellipsis (it, 0);
3328 /* When handling a display spec, we might load an
3329 empty string. In that case, discard it here. We
3330 used to discard it in handle_single_display_spec,
3331 but that causes get_overlay_strings_1, above, to
3332 ignore overlay strings that we must check. */
3333 if (STRINGP (it->string) && !SCHARS (it->string))
3334 pop_it (it);
3335 return;
3336 }
3337 else if (STRINGP (it->string) && !SCHARS (it->string))
3338 pop_it (it);
3339 else
3340 {
3341 it->string_from_display_prop_p = false;
3342 it->from_disp_prop_p = false;
3343 handle_overlay_change_p = false;
3344 }
3345 handled = HANDLED_RECOMPUTE_PROPS;
3346 break;
3347 }
3348 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3349 handle_overlay_change_p = false;
3350 }
3351
3352 if (handled != HANDLED_RECOMPUTE_PROPS)
3353 {
3354 /* Don't check for overlay strings below when set to deliver
3355 characters from a display vector. */
3356 if (it->method == GET_FROM_DISPLAY_VECTOR)
3357 handle_overlay_change_p = false;
3358
3359 /* Handle overlay changes.
3360 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3361 if it finds overlays. */
3362 if (handle_overlay_change_p)
3363 handled = handle_overlay_change (it);
3364 }
3365
3366 if (it->ellipsis_p)
3367 {
3368 setup_for_ellipsis (it, 0);
3369 break;
3370 }
3371 }
3372 while (handled == HANDLED_RECOMPUTE_PROPS);
3373
3374 /* Determine where to stop next. */
3375 if (handled == HANDLED_NORMALLY)
3376 compute_stop_pos (it);
3377 }
3378
3379
3380 /* Compute IT->stop_charpos from text property and overlay change
3381 information for IT's current position. */
3382
3383 static void
3384 compute_stop_pos (struct it *it)
3385 {
3386 register INTERVAL iv, next_iv;
3387 Lisp_Object object, limit, position;
3388 ptrdiff_t charpos, bytepos;
3389
3390 if (STRINGP (it->string))
3391 {
3392 /* Strings are usually short, so don't limit the search for
3393 properties. */
3394 it->stop_charpos = it->end_charpos;
3395 object = it->string;
3396 limit = Qnil;
3397 charpos = IT_STRING_CHARPOS (*it);
3398 bytepos = IT_STRING_BYTEPOS (*it);
3399 }
3400 else
3401 {
3402 ptrdiff_t pos;
3403
3404 /* If end_charpos is out of range for some reason, such as a
3405 misbehaving display function, rationalize it (Bug#5984). */
3406 if (it->end_charpos > ZV)
3407 it->end_charpos = ZV;
3408 it->stop_charpos = it->end_charpos;
3409
3410 /* If next overlay change is in front of the current stop pos
3411 (which is IT->end_charpos), stop there. Note: value of
3412 next_overlay_change is point-max if no overlay change
3413 follows. */
3414 charpos = IT_CHARPOS (*it);
3415 bytepos = IT_BYTEPOS (*it);
3416 pos = next_overlay_change (charpos);
3417 if (pos < it->stop_charpos)
3418 it->stop_charpos = pos;
3419
3420 /* Set up variables for computing the stop position from text
3421 property changes. */
3422 XSETBUFFER (object, current_buffer);
3423 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3424 }
3425
3426 /* Get the interval containing IT's position. Value is a null
3427 interval if there isn't such an interval. */
3428 position = make_number (charpos);
3429 iv = validate_interval_range (object, &position, &position, false);
3430 if (iv)
3431 {
3432 Lisp_Object values_here[LAST_PROP_IDX];
3433 struct props *p;
3434
3435 /* Get properties here. */
3436 for (p = it_props; p->handler; ++p)
3437 values_here[p->idx] = textget (iv->plist,
3438 builtin_lisp_symbol (p->name));
3439
3440 /* Look for an interval following iv that has different
3441 properties. */
3442 for (next_iv = next_interval (iv);
3443 (next_iv
3444 && (NILP (limit)
3445 || XFASTINT (limit) > next_iv->position));
3446 next_iv = next_interval (next_iv))
3447 {
3448 for (p = it_props; p->handler; ++p)
3449 {
3450 Lisp_Object new_value = textget (next_iv->plist,
3451 builtin_lisp_symbol (p->name));
3452 if (!EQ (values_here[p->idx], new_value))
3453 break;
3454 }
3455
3456 if (p->handler)
3457 break;
3458 }
3459
3460 if (next_iv)
3461 {
3462 if (INTEGERP (limit)
3463 && next_iv->position >= XFASTINT (limit))
3464 /* No text property change up to limit. */
3465 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3466 else
3467 /* Text properties change in next_iv. */
3468 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3469 }
3470 }
3471
3472 if (it->cmp_it.id < 0)
3473 {
3474 ptrdiff_t stoppos = it->end_charpos;
3475
3476 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3477 stoppos = -1;
3478 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3479 stoppos, it->string);
3480 }
3481
3482 eassert (STRINGP (it->string)
3483 || (it->stop_charpos >= BEGV
3484 && it->stop_charpos >= IT_CHARPOS (*it)));
3485 }
3486
3487
3488 /* Return the position of the next overlay change after POS in
3489 current_buffer. Value is point-max if no overlay change
3490 follows. This is like `next-overlay-change' but doesn't use
3491 xmalloc. */
3492
3493 static ptrdiff_t
3494 next_overlay_change (ptrdiff_t pos)
3495 {
3496 ptrdiff_t i, noverlays;
3497 ptrdiff_t endpos;
3498 Lisp_Object *overlays;
3499 USE_SAFE_ALLOCA;
3500
3501 /* Get all overlays at the given position. */
3502 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3503
3504 /* If any of these overlays ends before endpos,
3505 use its ending point instead. */
3506 for (i = 0; i < noverlays; ++i)
3507 {
3508 Lisp_Object oend;
3509 ptrdiff_t oendpos;
3510
3511 oend = OVERLAY_END (overlays[i]);
3512 oendpos = OVERLAY_POSITION (oend);
3513 endpos = min (endpos, oendpos);
3514 }
3515
3516 SAFE_FREE ();
3517 return endpos;
3518 }
3519
3520 /* How many characters forward to search for a display property or
3521 display string. Searching too far forward makes the bidi display
3522 sluggish, especially in small windows. */
3523 #define MAX_DISP_SCAN 250
3524
3525 /* Return the character position of a display string at or after
3526 position specified by POSITION. If no display string exists at or
3527 after POSITION, return ZV. A display string is either an overlay
3528 with `display' property whose value is a string, or a `display'
3529 text property whose value is a string. STRING is data about the
3530 string to iterate; if STRING->lstring is nil, we are iterating a
3531 buffer. FRAME_WINDOW_P is true when we are displaying a window
3532 on a GUI frame. DISP_PROP is set to zero if we searched
3533 MAX_DISP_SCAN characters forward without finding any display
3534 strings, non-zero otherwise. It is set to 2 if the display string
3535 uses any kind of `(space ...)' spec that will produce a stretch of
3536 white space in the text area. */
3537 ptrdiff_t
3538 compute_display_string_pos (struct text_pos *position,
3539 struct bidi_string_data *string,
3540 struct window *w,
3541 bool frame_window_p, int *disp_prop)
3542 {
3543 /* OBJECT = nil means current buffer. */
3544 Lisp_Object object, object1;
3545 Lisp_Object pos, spec, limpos;
3546 bool string_p = string && (STRINGP (string->lstring) || string->s);
3547 ptrdiff_t eob = string_p ? string->schars : ZV;
3548 ptrdiff_t begb = string_p ? 0 : BEGV;
3549 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3550 ptrdiff_t lim =
3551 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3552 struct text_pos tpos;
3553 int rv = 0;
3554
3555 if (string && STRINGP (string->lstring))
3556 object1 = object = string->lstring;
3557 else if (w && !string_p)
3558 {
3559 XSETWINDOW (object, w);
3560 object1 = Qnil;
3561 }
3562 else
3563 object1 = object = Qnil;
3564
3565 *disp_prop = 1;
3566
3567 if (charpos >= eob
3568 /* We don't support display properties whose values are strings
3569 that have display string properties. */
3570 || string->from_disp_str
3571 /* C strings cannot have display properties. */
3572 || (string->s && !STRINGP (object)))
3573 {
3574 *disp_prop = 0;
3575 return eob;
3576 }
3577
3578 /* If the character at CHARPOS is where the display string begins,
3579 return CHARPOS. */
3580 pos = make_number (charpos);
3581 if (STRINGP (object))
3582 bufpos = string->bufpos;
3583 else
3584 bufpos = charpos;
3585 tpos = *position;
3586 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3587 && (charpos <= begb
3588 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3589 object),
3590 spec))
3591 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3592 frame_window_p)))
3593 {
3594 if (rv == 2)
3595 *disp_prop = 2;
3596 return charpos;
3597 }
3598
3599 /* Look forward for the first character with a `display' property
3600 that will replace the underlying text when displayed. */
3601 limpos = make_number (lim);
3602 do {
3603 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3604 CHARPOS (tpos) = XFASTINT (pos);
3605 if (CHARPOS (tpos) >= lim)
3606 {
3607 *disp_prop = 0;
3608 break;
3609 }
3610 if (STRINGP (object))
3611 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3612 else
3613 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3614 spec = Fget_char_property (pos, Qdisplay, object);
3615 if (!STRINGP (object))
3616 bufpos = CHARPOS (tpos);
3617 } while (NILP (spec)
3618 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3619 bufpos, frame_window_p)));
3620 if (rv == 2)
3621 *disp_prop = 2;
3622
3623 return CHARPOS (tpos);
3624 }
3625
3626 /* Return the character position of the end of the display string that
3627 started at CHARPOS. If there's no display string at CHARPOS,
3628 return -1. A display string is either an overlay with `display'
3629 property whose value is a string or a `display' text property whose
3630 value is a string. */
3631 ptrdiff_t
3632 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3633 {
3634 /* OBJECT = nil means current buffer. */
3635 Lisp_Object object =
3636 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3637 Lisp_Object pos = make_number (charpos);
3638 ptrdiff_t eob =
3639 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3640
3641 if (charpos >= eob || (string->s && !STRINGP (object)))
3642 return eob;
3643
3644 /* It could happen that the display property or overlay was removed
3645 since we found it in compute_display_string_pos above. One way
3646 this can happen is if JIT font-lock was called (through
3647 handle_fontified_prop), and jit-lock-functions remove text
3648 properties or overlays from the portion of buffer that includes
3649 CHARPOS. Muse mode is known to do that, for example. In this
3650 case, we return -1 to the caller, to signal that no display
3651 string is actually present at CHARPOS. See bidi_fetch_char for
3652 how this is handled.
3653
3654 An alternative would be to never look for display properties past
3655 it->stop_charpos. But neither compute_display_string_pos nor
3656 bidi_fetch_char that calls it know or care where the next
3657 stop_charpos is. */
3658 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3659 return -1;
3660
3661 /* Look forward for the first character where the `display' property
3662 changes. */
3663 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3664
3665 return XFASTINT (pos);
3666 }
3667
3668
3669 \f
3670 /***********************************************************************
3671 Fontification
3672 ***********************************************************************/
3673
3674 /* Handle changes in the `fontified' property of the current buffer by
3675 calling hook functions from Qfontification_functions to fontify
3676 regions of text. */
3677
3678 static enum prop_handled
3679 handle_fontified_prop (struct it *it)
3680 {
3681 Lisp_Object prop, pos;
3682 enum prop_handled handled = HANDLED_NORMALLY;
3683
3684 if (!NILP (Vmemory_full))
3685 return handled;
3686
3687 /* Get the value of the `fontified' property at IT's current buffer
3688 position. (The `fontified' property doesn't have a special
3689 meaning in strings.) If the value is nil, call functions from
3690 Qfontification_functions. */
3691 if (!STRINGP (it->string)
3692 && it->s == NULL
3693 && !NILP (Vfontification_functions)
3694 && !NILP (Vrun_hooks)
3695 && (pos = make_number (IT_CHARPOS (*it)),
3696 prop = Fget_char_property (pos, Qfontified, Qnil),
3697 /* Ignore the special cased nil value always present at EOB since
3698 no amount of fontifying will be able to change it. */
3699 NILP (prop) && IT_CHARPOS (*it) < Z))
3700 {
3701 ptrdiff_t count = SPECPDL_INDEX ();
3702 Lisp_Object val;
3703 struct buffer *obuf = current_buffer;
3704 ptrdiff_t begv = BEGV, zv = ZV;
3705 bool old_clip_changed = current_buffer->clip_changed;
3706
3707 val = Vfontification_functions;
3708 specbind (Qfontification_functions, Qnil);
3709
3710 eassert (it->end_charpos == ZV);
3711
3712 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3713 safe_call1 (val, pos);
3714 else
3715 {
3716 Lisp_Object fns, fn;
3717 struct gcpro gcpro1, gcpro2;
3718
3719 fns = Qnil;
3720 GCPRO2 (val, fns);
3721
3722 for (; CONSP (val); val = XCDR (val))
3723 {
3724 fn = XCAR (val);
3725
3726 if (EQ (fn, Qt))
3727 {
3728 /* A value of t indicates this hook has a local
3729 binding; it means to run the global binding too.
3730 In a global value, t should not occur. If it
3731 does, we must ignore it to avoid an endless
3732 loop. */
3733 for (fns = Fdefault_value (Qfontification_functions);
3734 CONSP (fns);
3735 fns = XCDR (fns))
3736 {
3737 fn = XCAR (fns);
3738 if (!EQ (fn, Qt))
3739 safe_call1 (fn, pos);
3740 }
3741 }
3742 else
3743 safe_call1 (fn, pos);
3744 }
3745
3746 UNGCPRO;
3747 }
3748
3749 unbind_to (count, Qnil);
3750
3751 /* Fontification functions routinely call `save-restriction'.
3752 Normally, this tags clip_changed, which can confuse redisplay
3753 (see discussion in Bug#6671). Since we don't perform any
3754 special handling of fontification changes in the case where
3755 `save-restriction' isn't called, there's no point doing so in
3756 this case either. So, if the buffer's restrictions are
3757 actually left unchanged, reset clip_changed. */
3758 if (obuf == current_buffer)
3759 {
3760 if (begv == BEGV && zv == ZV)
3761 current_buffer->clip_changed = old_clip_changed;
3762 }
3763 /* There isn't much we can reasonably do to protect against
3764 misbehaving fontification, but here's a fig leaf. */
3765 else if (BUFFER_LIVE_P (obuf))
3766 set_buffer_internal_1 (obuf);
3767
3768 /* The fontification code may have added/removed text.
3769 It could do even a lot worse, but let's at least protect against
3770 the most obvious case where only the text past `pos' gets changed',
3771 as is/was done in grep.el where some escapes sequences are turned
3772 into face properties (bug#7876). */
3773 it->end_charpos = ZV;
3774
3775 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3776 something. This avoids an endless loop if they failed to
3777 fontify the text for which reason ever. */
3778 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3779 handled = HANDLED_RECOMPUTE_PROPS;
3780 }
3781
3782 return handled;
3783 }
3784
3785
3786 \f
3787 /***********************************************************************
3788 Faces
3789 ***********************************************************************/
3790
3791 /* Set up iterator IT from face properties at its current position.
3792 Called from handle_stop. */
3793
3794 static enum prop_handled
3795 handle_face_prop (struct it *it)
3796 {
3797 int new_face_id;
3798 ptrdiff_t next_stop;
3799
3800 if (!STRINGP (it->string))
3801 {
3802 new_face_id
3803 = face_at_buffer_position (it->w,
3804 IT_CHARPOS (*it),
3805 &next_stop,
3806 (IT_CHARPOS (*it)
3807 + TEXT_PROP_DISTANCE_LIMIT),
3808 false, it->base_face_id);
3809
3810 /* Is this a start of a run of characters with box face?
3811 Caveat: this can be called for a freshly initialized
3812 iterator; face_id is -1 in this case. We know that the new
3813 face will not change until limit, i.e. if the new face has a
3814 box, all characters up to limit will have one. But, as
3815 usual, we don't know whether limit is really the end. */
3816 if (new_face_id != it->face_id)
3817 {
3818 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3819 /* If it->face_id is -1, old_face below will be NULL, see
3820 the definition of FACE_FROM_ID. This will happen if this
3821 is the initial call that gets the face. */
3822 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3823
3824 /* If the value of face_id of the iterator is -1, we have to
3825 look in front of IT's position and see whether there is a
3826 face there that's different from new_face_id. */
3827 if (!old_face && IT_CHARPOS (*it) > BEG)
3828 {
3829 int prev_face_id = face_before_it_pos (it);
3830
3831 old_face = FACE_FROM_ID (it->f, prev_face_id);
3832 }
3833
3834 /* If the new face has a box, but the old face does not,
3835 this is the start of a run of characters with box face,
3836 i.e. this character has a shadow on the left side. */
3837 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3838 && (old_face == NULL || !old_face->box));
3839 it->face_box_p = new_face->box != FACE_NO_BOX;
3840 }
3841 }
3842 else
3843 {
3844 int base_face_id;
3845 ptrdiff_t bufpos;
3846 int i;
3847 Lisp_Object from_overlay
3848 = (it->current.overlay_string_index >= 0
3849 ? it->string_overlays[it->current.overlay_string_index
3850 % OVERLAY_STRING_CHUNK_SIZE]
3851 : Qnil);
3852
3853 /* See if we got to this string directly or indirectly from
3854 an overlay property. That includes the before-string or
3855 after-string of an overlay, strings in display properties
3856 provided by an overlay, their text properties, etc.
3857
3858 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3859 if (! NILP (from_overlay))
3860 for (i = it->sp - 1; i >= 0; i--)
3861 {
3862 if (it->stack[i].current.overlay_string_index >= 0)
3863 from_overlay
3864 = it->string_overlays[it->stack[i].current.overlay_string_index
3865 % OVERLAY_STRING_CHUNK_SIZE];
3866 else if (! NILP (it->stack[i].from_overlay))
3867 from_overlay = it->stack[i].from_overlay;
3868
3869 if (!NILP (from_overlay))
3870 break;
3871 }
3872
3873 if (! NILP (from_overlay))
3874 {
3875 bufpos = IT_CHARPOS (*it);
3876 /* For a string from an overlay, the base face depends
3877 only on text properties and ignores overlays. */
3878 base_face_id
3879 = face_for_overlay_string (it->w,
3880 IT_CHARPOS (*it),
3881 &next_stop,
3882 (IT_CHARPOS (*it)
3883 + TEXT_PROP_DISTANCE_LIMIT),
3884 false,
3885 from_overlay);
3886 }
3887 else
3888 {
3889 bufpos = 0;
3890
3891 /* For strings from a `display' property, use the face at
3892 IT's current buffer position as the base face to merge
3893 with, so that overlay strings appear in the same face as
3894 surrounding text, unless they specify their own faces.
3895 For strings from wrap-prefix and line-prefix properties,
3896 use the default face, possibly remapped via
3897 Vface_remapping_alist. */
3898 /* Note that the fact that we use the face at _buffer_
3899 position means that a 'display' property on an overlay
3900 string will not inherit the face of that overlay string,
3901 but will instead revert to the face of buffer text
3902 covered by the overlay. This is visible, e.g., when the
3903 overlay specifies a box face, but neither the buffer nor
3904 the display string do. This sounds like a design bug,
3905 but Emacs always did that since v21.1, so changing that
3906 might be a big deal. */
3907 base_face_id = it->string_from_prefix_prop_p
3908 ? (!NILP (Vface_remapping_alist)
3909 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3910 : DEFAULT_FACE_ID)
3911 : underlying_face_id (it);
3912 }
3913
3914 new_face_id = face_at_string_position (it->w,
3915 it->string,
3916 IT_STRING_CHARPOS (*it),
3917 bufpos,
3918 &next_stop,
3919 base_face_id, false);
3920
3921 /* Is this a start of a run of characters with box? Caveat:
3922 this can be called for a freshly allocated iterator; face_id
3923 is -1 is this case. We know that the new face will not
3924 change until the next check pos, i.e. if the new face has a
3925 box, all characters up to that position will have a
3926 box. But, as usual, we don't know whether that position
3927 is really the end. */
3928 if (new_face_id != it->face_id)
3929 {
3930 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3931 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3932
3933 /* If new face has a box but old face hasn't, this is the
3934 start of a run of characters with box, i.e. it has a
3935 shadow on the left side. */
3936 it->start_of_box_run_p
3937 = new_face->box && (old_face == NULL || !old_face->box);
3938 it->face_box_p = new_face->box != FACE_NO_BOX;
3939 }
3940 }
3941
3942 it->face_id = new_face_id;
3943 return HANDLED_NORMALLY;
3944 }
3945
3946
3947 /* Return the ID of the face ``underlying'' IT's current position,
3948 which is in a string. If the iterator is associated with a
3949 buffer, return the face at IT's current buffer position.
3950 Otherwise, use the iterator's base_face_id. */
3951
3952 static int
3953 underlying_face_id (struct it *it)
3954 {
3955 int face_id = it->base_face_id, i;
3956
3957 eassert (STRINGP (it->string));
3958
3959 for (i = it->sp - 1; i >= 0; --i)
3960 if (NILP (it->stack[i].string))
3961 face_id = it->stack[i].face_id;
3962
3963 return face_id;
3964 }
3965
3966
3967 /* Compute the face one character before or after the current position
3968 of IT, in the visual order. BEFORE_P means get the face
3969 in front (to the left in L2R paragraphs, to the right in R2L
3970 paragraphs) of IT's screen position. Value is the ID of the face. */
3971
3972 static int
3973 face_before_or_after_it_pos (struct it *it, bool before_p)
3974 {
3975 int face_id, limit;
3976 ptrdiff_t next_check_charpos;
3977 struct it it_copy;
3978 void *it_copy_data = NULL;
3979
3980 eassert (it->s == NULL);
3981
3982 if (STRINGP (it->string))
3983 {
3984 ptrdiff_t bufpos, charpos;
3985 int base_face_id;
3986
3987 /* No face change past the end of the string (for the case
3988 we are padding with spaces). No face change before the
3989 string start. */
3990 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3991 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3992 return it->face_id;
3993
3994 if (!it->bidi_p)
3995 {
3996 /* Set charpos to the position before or after IT's current
3997 position, in the logical order, which in the non-bidi
3998 case is the same as the visual order. */
3999 if (before_p)
4000 charpos = IT_STRING_CHARPOS (*it) - 1;
4001 else if (it->what == IT_COMPOSITION)
4002 /* For composition, we must check the character after the
4003 composition. */
4004 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4005 else
4006 charpos = IT_STRING_CHARPOS (*it) + 1;
4007 }
4008 else
4009 {
4010 if (before_p)
4011 {
4012 /* With bidi iteration, the character before the current
4013 in the visual order cannot be found by simple
4014 iteration, because "reverse" reordering is not
4015 supported. Instead, we need to use the move_it_*
4016 family of functions. */
4017 /* Ignore face changes before the first visible
4018 character on this display line. */
4019 if (it->current_x <= it->first_visible_x)
4020 return it->face_id;
4021 SAVE_IT (it_copy, *it, it_copy_data);
4022 /* Implementation note: Since move_it_in_display_line
4023 works in the iterator geometry, and thinks the first
4024 character is always the leftmost, even in R2L lines,
4025 we don't need to distinguish between the R2L and L2R
4026 cases here. */
4027 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4028 it_copy.current_x - 1, MOVE_TO_X);
4029 charpos = IT_STRING_CHARPOS (it_copy);
4030 RESTORE_IT (it, it, it_copy_data);
4031 }
4032 else
4033 {
4034 /* Set charpos to the string position of the character
4035 that comes after IT's current position in the visual
4036 order. */
4037 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4038
4039 it_copy = *it;
4040 while (n--)
4041 bidi_move_to_visually_next (&it_copy.bidi_it);
4042
4043 charpos = it_copy.bidi_it.charpos;
4044 }
4045 }
4046 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4047
4048 if (it->current.overlay_string_index >= 0)
4049 bufpos = IT_CHARPOS (*it);
4050 else
4051 bufpos = 0;
4052
4053 base_face_id = underlying_face_id (it);
4054
4055 /* Get the face for ASCII, or unibyte. */
4056 face_id = face_at_string_position (it->w,
4057 it->string,
4058 charpos,
4059 bufpos,
4060 &next_check_charpos,
4061 base_face_id, false);
4062
4063 /* Correct the face for charsets different from ASCII. Do it
4064 for the multibyte case only. The face returned above is
4065 suitable for unibyte text if IT->string is unibyte. */
4066 if (STRING_MULTIBYTE (it->string))
4067 {
4068 struct text_pos pos1 = string_pos (charpos, it->string);
4069 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4070 int c, len;
4071 struct face *face = FACE_FROM_ID (it->f, face_id);
4072
4073 c = string_char_and_length (p, &len);
4074 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4075 }
4076 }
4077 else
4078 {
4079 struct text_pos pos;
4080
4081 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4082 || (IT_CHARPOS (*it) <= BEGV && before_p))
4083 return it->face_id;
4084
4085 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4086 pos = it->current.pos;
4087
4088 if (!it->bidi_p)
4089 {
4090 if (before_p)
4091 DEC_TEXT_POS (pos, it->multibyte_p);
4092 else
4093 {
4094 if (it->what == IT_COMPOSITION)
4095 {
4096 /* For composition, we must check the position after
4097 the composition. */
4098 pos.charpos += it->cmp_it.nchars;
4099 pos.bytepos += it->len;
4100 }
4101 else
4102 INC_TEXT_POS (pos, it->multibyte_p);
4103 }
4104 }
4105 else
4106 {
4107 if (before_p)
4108 {
4109 /* With bidi iteration, the character before the current
4110 in the visual order cannot be found by simple
4111 iteration, because "reverse" reordering is not
4112 supported. Instead, we need to use the move_it_*
4113 family of functions. */
4114 /* Ignore face changes before the first visible
4115 character on this display line. */
4116 if (it->current_x <= it->first_visible_x)
4117 return it->face_id;
4118 SAVE_IT (it_copy, *it, it_copy_data);
4119 /* Implementation note: Since move_it_in_display_line
4120 works in the iterator geometry, and thinks the first
4121 character is always the leftmost, even in R2L lines,
4122 we don't need to distinguish between the R2L and L2R
4123 cases here. */
4124 move_it_in_display_line (&it_copy, ZV,
4125 it_copy.current_x - 1, MOVE_TO_X);
4126 pos = it_copy.current.pos;
4127 RESTORE_IT (it, it, it_copy_data);
4128 }
4129 else
4130 {
4131 /* Set charpos to the buffer position of the character
4132 that comes after IT's current position in the visual
4133 order. */
4134 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4135
4136 it_copy = *it;
4137 while (n--)
4138 bidi_move_to_visually_next (&it_copy.bidi_it);
4139
4140 SET_TEXT_POS (pos,
4141 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4142 }
4143 }
4144 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4145
4146 /* Determine face for CHARSET_ASCII, or unibyte. */
4147 face_id = face_at_buffer_position (it->w,
4148 CHARPOS (pos),
4149 &next_check_charpos,
4150 limit, false, -1);
4151
4152 /* Correct the face for charsets different from ASCII. Do it
4153 for the multibyte case only. The face returned above is
4154 suitable for unibyte text if current_buffer is unibyte. */
4155 if (it->multibyte_p)
4156 {
4157 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4158 struct face *face = FACE_FROM_ID (it->f, face_id);
4159 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4160 }
4161 }
4162
4163 return face_id;
4164 }
4165
4166
4167 \f
4168 /***********************************************************************
4169 Invisible text
4170 ***********************************************************************/
4171
4172 /* Set up iterator IT from invisible properties at its current
4173 position. Called from handle_stop. */
4174
4175 static enum prop_handled
4176 handle_invisible_prop (struct it *it)
4177 {
4178 enum prop_handled handled = HANDLED_NORMALLY;
4179 int invis;
4180 Lisp_Object prop;
4181
4182 if (STRINGP (it->string))
4183 {
4184 Lisp_Object end_charpos, limit, charpos;
4185
4186 /* Get the value of the invisible text property at the
4187 current position. Value will be nil if there is no such
4188 property. */
4189 charpos = make_number (IT_STRING_CHARPOS (*it));
4190 prop = Fget_text_property (charpos, Qinvisible, it->string);
4191 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4192
4193 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4194 {
4195 /* Record whether we have to display an ellipsis for the
4196 invisible text. */
4197 bool display_ellipsis_p = (invis == 2);
4198 ptrdiff_t len, endpos;
4199
4200 handled = HANDLED_RECOMPUTE_PROPS;
4201
4202 /* Get the position at which the next visible text can be
4203 found in IT->string, if any. */
4204 endpos = len = SCHARS (it->string);
4205 XSETINT (limit, len);
4206 do
4207 {
4208 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4209 it->string, limit);
4210 if (INTEGERP (end_charpos))
4211 {
4212 endpos = XFASTINT (end_charpos);
4213 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4214 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4215 if (invis == 2)
4216 display_ellipsis_p = true;
4217 }
4218 }
4219 while (invis != 0 && endpos < len);
4220
4221 if (display_ellipsis_p)
4222 it->ellipsis_p = true;
4223
4224 if (endpos < len)
4225 {
4226 /* Text at END_CHARPOS is visible. Move IT there. */
4227 struct text_pos old;
4228 ptrdiff_t oldpos;
4229
4230 old = it->current.string_pos;
4231 oldpos = CHARPOS (old);
4232 if (it->bidi_p)
4233 {
4234 if (it->bidi_it.first_elt
4235 && it->bidi_it.charpos < SCHARS (it->string))
4236 bidi_paragraph_init (it->paragraph_embedding,
4237 &it->bidi_it, true);
4238 /* Bidi-iterate out of the invisible text. */
4239 do
4240 {
4241 bidi_move_to_visually_next (&it->bidi_it);
4242 }
4243 while (oldpos <= it->bidi_it.charpos
4244 && it->bidi_it.charpos < endpos);
4245
4246 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4248 if (IT_CHARPOS (*it) >= endpos)
4249 it->prev_stop = endpos;
4250 }
4251 else
4252 {
4253 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4254 compute_string_pos (&it->current.string_pos, old, it->string);
4255 }
4256 }
4257 else
4258 {
4259 /* The rest of the string is invisible. If this is an
4260 overlay string, proceed with the next overlay string
4261 or whatever comes and return a character from there. */
4262 if (it->current.overlay_string_index >= 0
4263 && !display_ellipsis_p)
4264 {
4265 next_overlay_string (it);
4266 /* Don't check for overlay strings when we just
4267 finished processing them. */
4268 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4269 }
4270 else
4271 {
4272 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4273 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4274 }
4275 }
4276 }
4277 }
4278 else
4279 {
4280 ptrdiff_t newpos, next_stop, start_charpos, tem;
4281 Lisp_Object pos, overlay;
4282
4283 /* First of all, is there invisible text at this position? */
4284 tem = start_charpos = IT_CHARPOS (*it);
4285 pos = make_number (tem);
4286 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4287 &overlay);
4288 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4289
4290 /* If we are on invisible text, skip over it. */
4291 if (invis != 0 && start_charpos < it->end_charpos)
4292 {
4293 /* Record whether we have to display an ellipsis for the
4294 invisible text. */
4295 bool display_ellipsis_p = invis == 2;
4296
4297 handled = HANDLED_RECOMPUTE_PROPS;
4298
4299 /* Loop skipping over invisible text. The loop is left at
4300 ZV or with IT on the first char being visible again. */
4301 do
4302 {
4303 /* Try to skip some invisible text. Return value is the
4304 position reached which can be equal to where we start
4305 if there is nothing invisible there. This skips both
4306 over invisible text properties and overlays with
4307 invisible property. */
4308 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4309
4310 /* If we skipped nothing at all we weren't at invisible
4311 text in the first place. If everything to the end of
4312 the buffer was skipped, end the loop. */
4313 if (newpos == tem || newpos >= ZV)
4314 invis = 0;
4315 else
4316 {
4317 /* We skipped some characters but not necessarily
4318 all there are. Check if we ended up on visible
4319 text. Fget_char_property returns the property of
4320 the char before the given position, i.e. if we
4321 get invis = 0, this means that the char at
4322 newpos is visible. */
4323 pos = make_number (newpos);
4324 prop = Fget_char_property (pos, Qinvisible, it->window);
4325 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4326 }
4327
4328 /* If we ended up on invisible text, proceed to
4329 skip starting with next_stop. */
4330 if (invis != 0)
4331 tem = next_stop;
4332
4333 /* If there are adjacent invisible texts, don't lose the
4334 second one's ellipsis. */
4335 if (invis == 2)
4336 display_ellipsis_p = true;
4337 }
4338 while (invis != 0);
4339
4340 /* The position newpos is now either ZV or on visible text. */
4341 if (it->bidi_p)
4342 {
4343 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4344 bool on_newline
4345 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4346 bool after_newline
4347 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4348
4349 /* If the invisible text ends on a newline or on a
4350 character after a newline, we can avoid the costly,
4351 character by character, bidi iteration to NEWPOS, and
4352 instead simply reseat the iterator there. That's
4353 because all bidi reordering information is tossed at
4354 the newline. This is a big win for modes that hide
4355 complete lines, like Outline, Org, etc. */
4356 if (on_newline || after_newline)
4357 {
4358 struct text_pos tpos;
4359 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4360
4361 SET_TEXT_POS (tpos, newpos, bpos);
4362 reseat_1 (it, tpos, false);
4363 /* If we reseat on a newline/ZV, we need to prep the
4364 bidi iterator for advancing to the next character
4365 after the newline/EOB, keeping the current paragraph
4366 direction (so that PRODUCE_GLYPHS does TRT wrt
4367 prepending/appending glyphs to a glyph row). */
4368 if (on_newline)
4369 {
4370 it->bidi_it.first_elt = false;
4371 it->bidi_it.paragraph_dir = pdir;
4372 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4373 it->bidi_it.nchars = 1;
4374 it->bidi_it.ch_len = 1;
4375 }
4376 }
4377 else /* Must use the slow method. */
4378 {
4379 /* With bidi iteration, the region of invisible text
4380 could start and/or end in the middle of a
4381 non-base embedding level. Therefore, we need to
4382 skip invisible text using the bidi iterator,
4383 starting at IT's current position, until we find
4384 ourselves outside of the invisible text.
4385 Skipping invisible text _after_ bidi iteration
4386 avoids affecting the visual order of the
4387 displayed text when invisible properties are
4388 added or removed. */
4389 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4390 {
4391 /* If we were `reseat'ed to a new paragraph,
4392 determine the paragraph base direction. We
4393 need to do it now because
4394 next_element_from_buffer may not have a
4395 chance to do it, if we are going to skip any
4396 text at the beginning, which resets the
4397 FIRST_ELT flag. */
4398 bidi_paragraph_init (it->paragraph_embedding,
4399 &it->bidi_it, true);
4400 }
4401 do
4402 {
4403 bidi_move_to_visually_next (&it->bidi_it);
4404 }
4405 while (it->stop_charpos <= it->bidi_it.charpos
4406 && it->bidi_it.charpos < newpos);
4407 IT_CHARPOS (*it) = it->bidi_it.charpos;
4408 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4409 /* If we overstepped NEWPOS, record its position in
4410 the iterator, so that we skip invisible text if
4411 later the bidi iteration lands us in the
4412 invisible region again. */
4413 if (IT_CHARPOS (*it) >= newpos)
4414 it->prev_stop = newpos;
4415 }
4416 }
4417 else
4418 {
4419 IT_CHARPOS (*it) = newpos;
4420 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4421 }
4422
4423 if (display_ellipsis_p)
4424 {
4425 /* Make sure that the glyphs of the ellipsis will get
4426 correct `charpos' values. If we would not update
4427 it->position here, the glyphs would belong to the
4428 last visible character _before_ the invisible
4429 text, which confuses `set_cursor_from_row'.
4430
4431 We use the last invisible position instead of the
4432 first because this way the cursor is always drawn on
4433 the first "." of the ellipsis, whenever PT is inside
4434 the invisible text. Otherwise the cursor would be
4435 placed _after_ the ellipsis when the point is after the
4436 first invisible character. */
4437 if (!STRINGP (it->object))
4438 {
4439 it->position.charpos = newpos - 1;
4440 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4441 }
4442 }
4443
4444 /* If there are before-strings at the start of invisible
4445 text, and the text is invisible because of a text
4446 property, arrange to show before-strings because 20.x did
4447 it that way. (If the text is invisible because of an
4448 overlay property instead of a text property, this is
4449 already handled in the overlay code.) */
4450 if (NILP (overlay)
4451 && get_overlay_strings (it, it->stop_charpos))
4452 {
4453 handled = HANDLED_RECOMPUTE_PROPS;
4454 if (it->sp > 0)
4455 {
4456 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4457 /* The call to get_overlay_strings above recomputes
4458 it->stop_charpos, but it only considers changes
4459 in properties and overlays beyond iterator's
4460 current position. This causes us to miss changes
4461 that happen exactly where the invisible property
4462 ended. So we play it safe here and force the
4463 iterator to check for potential stop positions
4464 immediately after the invisible text. Note that
4465 if get_overlay_strings returns true, it
4466 normally also pushed the iterator stack, so we
4467 need to update the stop position in the slot
4468 below the current one. */
4469 it->stack[it->sp - 1].stop_charpos
4470 = CHARPOS (it->stack[it->sp - 1].current.pos);
4471 }
4472 }
4473 else if (display_ellipsis_p)
4474 {
4475 it->ellipsis_p = true;
4476 /* Let the ellipsis display before
4477 considering any properties of the following char.
4478 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4479 handled = HANDLED_RETURN;
4480 }
4481 }
4482 }
4483
4484 return handled;
4485 }
4486
4487
4488 /* Make iterator IT return `...' next.
4489 Replaces LEN characters from buffer. */
4490
4491 static void
4492 setup_for_ellipsis (struct it *it, int len)
4493 {
4494 /* Use the display table definition for `...'. Invalid glyphs
4495 will be handled by the method returning elements from dpvec. */
4496 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4497 {
4498 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4499 it->dpvec = v->contents;
4500 it->dpend = v->contents + v->header.size;
4501 }
4502 else
4503 {
4504 /* Default `...'. */
4505 it->dpvec = default_invis_vector;
4506 it->dpend = default_invis_vector + 3;
4507 }
4508
4509 it->dpvec_char_len = len;
4510 it->current.dpvec_index = 0;
4511 it->dpvec_face_id = -1;
4512
4513 /* Remember the current face id in case glyphs specify faces.
4514 IT's face is restored in set_iterator_to_next.
4515 saved_face_id was set to preceding char's face in handle_stop. */
4516 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4517 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4518
4519 /* If the ellipsis represents buffer text, it means we advanced in
4520 the buffer, so we should no longer ignore overlay strings. */
4521 if (it->method == GET_FROM_BUFFER)
4522 it->ignore_overlay_strings_at_pos_p = false;
4523
4524 it->method = GET_FROM_DISPLAY_VECTOR;
4525 it->ellipsis_p = true;
4526 }
4527
4528
4529 \f
4530 /***********************************************************************
4531 'display' property
4532 ***********************************************************************/
4533
4534 /* Set up iterator IT from `display' property at its current position.
4535 Called from handle_stop.
4536 We return HANDLED_RETURN if some part of the display property
4537 overrides the display of the buffer text itself.
4538 Otherwise we return HANDLED_NORMALLY. */
4539
4540 static enum prop_handled
4541 handle_display_prop (struct it *it)
4542 {
4543 Lisp_Object propval, object, overlay;
4544 struct text_pos *position;
4545 ptrdiff_t bufpos;
4546 /* Nonzero if some property replaces the display of the text itself. */
4547 int display_replaced = 0;
4548
4549 if (STRINGP (it->string))
4550 {
4551 object = it->string;
4552 position = &it->current.string_pos;
4553 bufpos = CHARPOS (it->current.pos);
4554 }
4555 else
4556 {
4557 XSETWINDOW (object, it->w);
4558 position = &it->current.pos;
4559 bufpos = CHARPOS (*position);
4560 }
4561
4562 /* Reset those iterator values set from display property values. */
4563 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4564 it->space_width = Qnil;
4565 it->font_height = Qnil;
4566 it->voffset = 0;
4567
4568 /* We don't support recursive `display' properties, i.e. string
4569 values that have a string `display' property, that have a string
4570 `display' property etc. */
4571 if (!it->string_from_display_prop_p)
4572 it->area = TEXT_AREA;
4573
4574 propval = get_char_property_and_overlay (make_number (position->charpos),
4575 Qdisplay, object, &overlay);
4576 if (NILP (propval))
4577 return HANDLED_NORMALLY;
4578 /* Now OVERLAY is the overlay that gave us this property, or nil
4579 if it was a text property. */
4580
4581 if (!STRINGP (it->string))
4582 object = it->w->contents;
4583
4584 display_replaced = handle_display_spec (it, propval, object, overlay,
4585 position, bufpos,
4586 FRAME_WINDOW_P (it->f));
4587 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4588 }
4589
4590 /* Subroutine of handle_display_prop. Returns non-zero if the display
4591 specification in SPEC is a replacing specification, i.e. it would
4592 replace the text covered by `display' property with something else,
4593 such as an image or a display string. If SPEC includes any kind or
4594 `(space ...) specification, the value is 2; this is used by
4595 compute_display_string_pos, which see.
4596
4597 See handle_single_display_spec for documentation of arguments.
4598 FRAME_WINDOW_P is true if the window being redisplayed is on a
4599 GUI frame; this argument is used only if IT is NULL, see below.
4600
4601 IT can be NULL, if this is called by the bidi reordering code
4602 through compute_display_string_pos, which see. In that case, this
4603 function only examines SPEC, but does not otherwise "handle" it, in
4604 the sense that it doesn't set up members of IT from the display
4605 spec. */
4606 static int
4607 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4608 Lisp_Object overlay, struct text_pos *position,
4609 ptrdiff_t bufpos, bool frame_window_p)
4610 {
4611 int replacing = 0;
4612
4613 if (CONSP (spec)
4614 /* Simple specifications. */
4615 && !EQ (XCAR (spec), Qimage)
4616 && !EQ (XCAR (spec), Qspace)
4617 && !EQ (XCAR (spec), Qwhen)
4618 && !EQ (XCAR (spec), Qslice)
4619 && !EQ (XCAR (spec), Qspace_width)
4620 && !EQ (XCAR (spec), Qheight)
4621 && !EQ (XCAR (spec), Qraise)
4622 /* Marginal area specifications. */
4623 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4624 && !EQ (XCAR (spec), Qleft_fringe)
4625 && !EQ (XCAR (spec), Qright_fringe)
4626 && !NILP (XCAR (spec)))
4627 {
4628 for (; CONSP (spec); spec = XCDR (spec))
4629 {
4630 int rv = handle_single_display_spec (it, XCAR (spec), object,
4631 overlay, position, bufpos,
4632 replacing, frame_window_p);
4633 if (rv != 0)
4634 {
4635 replacing = rv;
4636 /* If some text in a string is replaced, `position' no
4637 longer points to the position of `object'. */
4638 if (!it || STRINGP (object))
4639 break;
4640 }
4641 }
4642 }
4643 else if (VECTORP (spec))
4644 {
4645 ptrdiff_t i;
4646 for (i = 0; i < ASIZE (spec); ++i)
4647 {
4648 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4649 overlay, position, bufpos,
4650 replacing, frame_window_p);
4651 if (rv != 0)
4652 {
4653 replacing = rv;
4654 /* If some text in a string is replaced, `position' no
4655 longer points to the position of `object'. */
4656 if (!it || STRINGP (object))
4657 break;
4658 }
4659 }
4660 }
4661 else
4662 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4663 bufpos, 0, frame_window_p);
4664 return replacing;
4665 }
4666
4667 /* Value is the position of the end of the `display' property starting
4668 at START_POS in OBJECT. */
4669
4670 static struct text_pos
4671 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4672 {
4673 Lisp_Object end;
4674 struct text_pos end_pos;
4675
4676 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4677 Qdisplay, object, Qnil);
4678 CHARPOS (end_pos) = XFASTINT (end);
4679 if (STRINGP (object))
4680 compute_string_pos (&end_pos, start_pos, it->string);
4681 else
4682 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4683
4684 return end_pos;
4685 }
4686
4687
4688 /* Set up IT from a single `display' property specification SPEC. OBJECT
4689 is the object in which the `display' property was found. *POSITION
4690 is the position in OBJECT at which the `display' property was found.
4691 BUFPOS is the buffer position of OBJECT (different from POSITION if
4692 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4693 previously saw a display specification which already replaced text
4694 display with something else, for example an image; we ignore such
4695 properties after the first one has been processed.
4696
4697 OVERLAY is the overlay this `display' property came from,
4698 or nil if it was a text property.
4699
4700 If SPEC is a `space' or `image' specification, and in some other
4701 cases too, set *POSITION to the position where the `display'
4702 property ends.
4703
4704 If IT is NULL, only examine the property specification in SPEC, but
4705 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4706 is intended to be displayed in a window on a GUI frame.
4707
4708 Value is non-zero if something was found which replaces the display
4709 of buffer or string text. */
4710
4711 static int
4712 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4713 Lisp_Object overlay, struct text_pos *position,
4714 ptrdiff_t bufpos, int display_replaced,
4715 bool frame_window_p)
4716 {
4717 Lisp_Object form;
4718 Lisp_Object location, value;
4719 struct text_pos start_pos = *position;
4720
4721 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4722 If the result is non-nil, use VALUE instead of SPEC. */
4723 form = Qt;
4724 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4725 {
4726 spec = XCDR (spec);
4727 if (!CONSP (spec))
4728 return 0;
4729 form = XCAR (spec);
4730 spec = XCDR (spec);
4731 }
4732
4733 if (!NILP (form) && !EQ (form, Qt))
4734 {
4735 ptrdiff_t count = SPECPDL_INDEX ();
4736 struct gcpro gcpro1;
4737
4738 /* Bind `object' to the object having the `display' property, a
4739 buffer or string. Bind `position' to the position in the
4740 object where the property was found, and `buffer-position'
4741 to the current position in the buffer. */
4742
4743 if (NILP (object))
4744 XSETBUFFER (object, current_buffer);
4745 specbind (Qobject, object);
4746 specbind (Qposition, make_number (CHARPOS (*position)));
4747 specbind (Qbuffer_position, make_number (bufpos));
4748 GCPRO1 (form);
4749 form = safe_eval (form);
4750 UNGCPRO;
4751 unbind_to (count, Qnil);
4752 }
4753
4754 if (NILP (form))
4755 return 0;
4756
4757 /* Handle `(height HEIGHT)' specifications. */
4758 if (CONSP (spec)
4759 && EQ (XCAR (spec), Qheight)
4760 && CONSP (XCDR (spec)))
4761 {
4762 if (it)
4763 {
4764 if (!FRAME_WINDOW_P (it->f))
4765 return 0;
4766
4767 it->font_height = XCAR (XCDR (spec));
4768 if (!NILP (it->font_height))
4769 {
4770 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4771 int new_height = -1;
4772
4773 if (CONSP (it->font_height)
4774 && (EQ (XCAR (it->font_height), Qplus)
4775 || EQ (XCAR (it->font_height), Qminus))
4776 && CONSP (XCDR (it->font_height))
4777 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4778 {
4779 /* `(+ N)' or `(- N)' where N is an integer. */
4780 int steps = XINT (XCAR (XCDR (it->font_height)));
4781 if (EQ (XCAR (it->font_height), Qplus))
4782 steps = - steps;
4783 it->face_id = smaller_face (it->f, it->face_id, steps);
4784 }
4785 else if (FUNCTIONP (it->font_height))
4786 {
4787 /* Call function with current height as argument.
4788 Value is the new height. */
4789 Lisp_Object height;
4790 height = safe_call1 (it->font_height,
4791 face->lface[LFACE_HEIGHT_INDEX]);
4792 if (NUMBERP (height))
4793 new_height = XFLOATINT (height);
4794 }
4795 else if (NUMBERP (it->font_height))
4796 {
4797 /* Value is a multiple of the canonical char height. */
4798 struct face *f;
4799
4800 f = FACE_FROM_ID (it->f,
4801 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4802 new_height = (XFLOATINT (it->font_height)
4803 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4804 }
4805 else
4806 {
4807 /* Evaluate IT->font_height with `height' bound to the
4808 current specified height to get the new height. */
4809 ptrdiff_t count = SPECPDL_INDEX ();
4810
4811 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4812 value = safe_eval (it->font_height);
4813 unbind_to (count, Qnil);
4814
4815 if (NUMBERP (value))
4816 new_height = XFLOATINT (value);
4817 }
4818
4819 if (new_height > 0)
4820 it->face_id = face_with_height (it->f, it->face_id, new_height);
4821 }
4822 }
4823
4824 return 0;
4825 }
4826
4827 /* Handle `(space-width WIDTH)'. */
4828 if (CONSP (spec)
4829 && EQ (XCAR (spec), Qspace_width)
4830 && CONSP (XCDR (spec)))
4831 {
4832 if (it)
4833 {
4834 if (!FRAME_WINDOW_P (it->f))
4835 return 0;
4836
4837 value = XCAR (XCDR (spec));
4838 if (NUMBERP (value) && XFLOATINT (value) > 0)
4839 it->space_width = value;
4840 }
4841
4842 return 0;
4843 }
4844
4845 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4846 if (CONSP (spec)
4847 && EQ (XCAR (spec), Qslice))
4848 {
4849 Lisp_Object tem;
4850
4851 if (it)
4852 {
4853 if (!FRAME_WINDOW_P (it->f))
4854 return 0;
4855
4856 if (tem = XCDR (spec), CONSP (tem))
4857 {
4858 it->slice.x = XCAR (tem);
4859 if (tem = XCDR (tem), CONSP (tem))
4860 {
4861 it->slice.y = XCAR (tem);
4862 if (tem = XCDR (tem), CONSP (tem))
4863 {
4864 it->slice.width = XCAR (tem);
4865 if (tem = XCDR (tem), CONSP (tem))
4866 it->slice.height = XCAR (tem);
4867 }
4868 }
4869 }
4870 }
4871
4872 return 0;
4873 }
4874
4875 /* Handle `(raise FACTOR)'. */
4876 if (CONSP (spec)
4877 && EQ (XCAR (spec), Qraise)
4878 && CONSP (XCDR (spec)))
4879 {
4880 if (it)
4881 {
4882 if (!FRAME_WINDOW_P (it->f))
4883 return 0;
4884
4885 #ifdef HAVE_WINDOW_SYSTEM
4886 value = XCAR (XCDR (spec));
4887 if (NUMBERP (value))
4888 {
4889 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4890 it->voffset = - (XFLOATINT (value)
4891 * (FONT_HEIGHT (face->font)));
4892 }
4893 #endif /* HAVE_WINDOW_SYSTEM */
4894 }
4895
4896 return 0;
4897 }
4898
4899 /* Don't handle the other kinds of display specifications
4900 inside a string that we got from a `display' property. */
4901 if (it && it->string_from_display_prop_p)
4902 return 0;
4903
4904 /* Characters having this form of property are not displayed, so
4905 we have to find the end of the property. */
4906 if (it)
4907 {
4908 start_pos = *position;
4909 *position = display_prop_end (it, object, start_pos);
4910 /* If the display property comes from an overlay, don't consider
4911 any potential stop_charpos values before the end of that
4912 overlay. Since display_prop_end will happily find another
4913 'display' property coming from some other overlay or text
4914 property on buffer positions before this overlay's end, we
4915 need to ignore them, or else we risk displaying this
4916 overlay's display string/image twice. */
4917 if (!NILP (overlay))
4918 {
4919 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4920
4921 if (ovendpos > CHARPOS (*position))
4922 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4923 }
4924 }
4925 value = Qnil;
4926
4927 /* Stop the scan at that end position--we assume that all
4928 text properties change there. */
4929 if (it)
4930 it->stop_charpos = position->charpos;
4931
4932 /* Handle `(left-fringe BITMAP [FACE])'
4933 and `(right-fringe BITMAP [FACE])'. */
4934 if (CONSP (spec)
4935 && (EQ (XCAR (spec), Qleft_fringe)
4936 || EQ (XCAR (spec), Qright_fringe))
4937 && CONSP (XCDR (spec)))
4938 {
4939 int fringe_bitmap;
4940
4941 if (it)
4942 {
4943 if (!FRAME_WINDOW_P (it->f))
4944 /* If we return here, POSITION has been advanced
4945 across the text with this property. */
4946 {
4947 /* Synchronize the bidi iterator with POSITION. This is
4948 needed because we are not going to push the iterator
4949 on behalf of this display property, so there will be
4950 no pop_it call to do this synchronization for us. */
4951 if (it->bidi_p)
4952 {
4953 it->position = *position;
4954 iterate_out_of_display_property (it);
4955 *position = it->position;
4956 }
4957 return 1;
4958 }
4959 }
4960 else if (!frame_window_p)
4961 return 1;
4962
4963 #ifdef HAVE_WINDOW_SYSTEM
4964 value = XCAR (XCDR (spec));
4965 if (!SYMBOLP (value)
4966 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4967 /* If we return here, POSITION has been advanced
4968 across the text with this property. */
4969 {
4970 if (it && it->bidi_p)
4971 {
4972 it->position = *position;
4973 iterate_out_of_display_property (it);
4974 *position = it->position;
4975 }
4976 return 1;
4977 }
4978
4979 if (it)
4980 {
4981 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4982
4983 if (CONSP (XCDR (XCDR (spec))))
4984 {
4985 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4986 int face_id2 = lookup_derived_face (it->f, face_name,
4987 FRINGE_FACE_ID, false);
4988 if (face_id2 >= 0)
4989 face_id = face_id2;
4990 }
4991
4992 /* Save current settings of IT so that we can restore them
4993 when we are finished with the glyph property value. */
4994 push_it (it, position);
4995
4996 it->area = TEXT_AREA;
4997 it->what = IT_IMAGE;
4998 it->image_id = -1; /* no image */
4999 it->position = start_pos;
5000 it->object = NILP (object) ? it->w->contents : object;
5001 it->method = GET_FROM_IMAGE;
5002 it->from_overlay = Qnil;
5003 it->face_id = face_id;
5004 it->from_disp_prop_p = true;
5005
5006 /* Say that we haven't consumed the characters with
5007 `display' property yet. The call to pop_it in
5008 set_iterator_to_next will clean this up. */
5009 *position = start_pos;
5010
5011 if (EQ (XCAR (spec), Qleft_fringe))
5012 {
5013 it->left_user_fringe_bitmap = fringe_bitmap;
5014 it->left_user_fringe_face_id = face_id;
5015 }
5016 else
5017 {
5018 it->right_user_fringe_bitmap = fringe_bitmap;
5019 it->right_user_fringe_face_id = face_id;
5020 }
5021 }
5022 #endif /* HAVE_WINDOW_SYSTEM */
5023 return 1;
5024 }
5025
5026 /* Prepare to handle `((margin left-margin) ...)',
5027 `((margin right-margin) ...)' and `((margin nil) ...)'
5028 prefixes for display specifications. */
5029 location = Qunbound;
5030 if (CONSP (spec) && CONSP (XCAR (spec)))
5031 {
5032 Lisp_Object tem;
5033
5034 value = XCDR (spec);
5035 if (CONSP (value))
5036 value = XCAR (value);
5037
5038 tem = XCAR (spec);
5039 if (EQ (XCAR (tem), Qmargin)
5040 && (tem = XCDR (tem),
5041 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5042 (NILP (tem)
5043 || EQ (tem, Qleft_margin)
5044 || EQ (tem, Qright_margin))))
5045 location = tem;
5046 }
5047
5048 if (EQ (location, Qunbound))
5049 {
5050 location = Qnil;
5051 value = spec;
5052 }
5053
5054 /* After this point, VALUE is the property after any
5055 margin prefix has been stripped. It must be a string,
5056 an image specification, or `(space ...)'.
5057
5058 LOCATION specifies where to display: `left-margin',
5059 `right-margin' or nil. */
5060
5061 bool valid_p = (STRINGP (value)
5062 #ifdef HAVE_WINDOW_SYSTEM
5063 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5064 && valid_image_p (value))
5065 #endif /* not HAVE_WINDOW_SYSTEM */
5066 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5067
5068 if (valid_p && display_replaced == 0)
5069 {
5070 int retval = 1;
5071
5072 if (!it)
5073 {
5074 /* Callers need to know whether the display spec is any kind
5075 of `(space ...)' spec that is about to affect text-area
5076 display. */
5077 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5078 retval = 2;
5079 return retval;
5080 }
5081
5082 /* Save current settings of IT so that we can restore them
5083 when we are finished with the glyph property value. */
5084 push_it (it, position);
5085 it->from_overlay = overlay;
5086 it->from_disp_prop_p = true;
5087
5088 if (NILP (location))
5089 it->area = TEXT_AREA;
5090 else if (EQ (location, Qleft_margin))
5091 it->area = LEFT_MARGIN_AREA;
5092 else
5093 it->area = RIGHT_MARGIN_AREA;
5094
5095 if (STRINGP (value))
5096 {
5097 it->string = value;
5098 it->multibyte_p = STRING_MULTIBYTE (it->string);
5099 it->current.overlay_string_index = -1;
5100 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5101 it->end_charpos = it->string_nchars = SCHARS (it->string);
5102 it->method = GET_FROM_STRING;
5103 it->stop_charpos = 0;
5104 it->prev_stop = 0;
5105 it->base_level_stop = 0;
5106 it->string_from_display_prop_p = true;
5107 /* Say that we haven't consumed the characters with
5108 `display' property yet. The call to pop_it in
5109 set_iterator_to_next will clean this up. */
5110 if (BUFFERP (object))
5111 *position = start_pos;
5112
5113 /* Force paragraph direction to be that of the parent
5114 object. If the parent object's paragraph direction is
5115 not yet determined, default to L2R. */
5116 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5117 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5118 else
5119 it->paragraph_embedding = L2R;
5120
5121 /* Set up the bidi iterator for this display string. */
5122 if (it->bidi_p)
5123 {
5124 it->bidi_it.string.lstring = it->string;
5125 it->bidi_it.string.s = NULL;
5126 it->bidi_it.string.schars = it->end_charpos;
5127 it->bidi_it.string.bufpos = bufpos;
5128 it->bidi_it.string.from_disp_str = true;
5129 it->bidi_it.string.unibyte = !it->multibyte_p;
5130 it->bidi_it.w = it->w;
5131 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5132 }
5133 }
5134 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5135 {
5136 it->method = GET_FROM_STRETCH;
5137 it->object = value;
5138 *position = it->position = start_pos;
5139 retval = 1 + (it->area == TEXT_AREA);
5140 }
5141 #ifdef HAVE_WINDOW_SYSTEM
5142 else
5143 {
5144 it->what = IT_IMAGE;
5145 it->image_id = lookup_image (it->f, value);
5146 it->position = start_pos;
5147 it->object = NILP (object) ? it->w->contents : object;
5148 it->method = GET_FROM_IMAGE;
5149
5150 /* Say that we haven't consumed the characters with
5151 `display' property yet. The call to pop_it in
5152 set_iterator_to_next will clean this up. */
5153 *position = start_pos;
5154 }
5155 #endif /* HAVE_WINDOW_SYSTEM */
5156
5157 return retval;
5158 }
5159
5160 /* Invalid property or property not supported. Restore
5161 POSITION to what it was before. */
5162 *position = start_pos;
5163 return 0;
5164 }
5165
5166 /* Check if PROP is a display property value whose text should be
5167 treated as intangible. OVERLAY is the overlay from which PROP
5168 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5169 specify the buffer position covered by PROP. */
5170
5171 bool
5172 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5173 ptrdiff_t charpos, ptrdiff_t bytepos)
5174 {
5175 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5176 struct text_pos position;
5177
5178 SET_TEXT_POS (position, charpos, bytepos);
5179 return (handle_display_spec (NULL, prop, Qnil, overlay,
5180 &position, charpos, frame_window_p)
5181 != 0);
5182 }
5183
5184
5185 /* Return true if PROP is a display sub-property value containing STRING.
5186
5187 Implementation note: this and the following function are really
5188 special cases of handle_display_spec and
5189 handle_single_display_spec, and should ideally use the same code.
5190 Until they do, these two pairs must be consistent and must be
5191 modified in sync. */
5192
5193 static bool
5194 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5195 {
5196 if (EQ (string, prop))
5197 return true;
5198
5199 /* Skip over `when FORM'. */
5200 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5201 {
5202 prop = XCDR (prop);
5203 if (!CONSP (prop))
5204 return false;
5205 /* Actually, the condition following `when' should be eval'ed,
5206 like handle_single_display_spec does, and we should return
5207 false if it evaluates to nil. However, this function is
5208 called only when the buffer was already displayed and some
5209 glyph in the glyph matrix was found to come from a display
5210 string. Therefore, the condition was already evaluated, and
5211 the result was non-nil, otherwise the display string wouldn't
5212 have been displayed and we would have never been called for
5213 this property. Thus, we can skip the evaluation and assume
5214 its result is non-nil. */
5215 prop = XCDR (prop);
5216 }
5217
5218 if (CONSP (prop))
5219 /* Skip over `margin LOCATION'. */
5220 if (EQ (XCAR (prop), Qmargin))
5221 {
5222 prop = XCDR (prop);
5223 if (!CONSP (prop))
5224 return false;
5225
5226 prop = XCDR (prop);
5227 if (!CONSP (prop))
5228 return false;
5229 }
5230
5231 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5232 }
5233
5234
5235 /* Return true if STRING appears in the `display' property PROP. */
5236
5237 static bool
5238 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5239 {
5240 if (CONSP (prop)
5241 && !EQ (XCAR (prop), Qwhen)
5242 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5243 {
5244 /* A list of sub-properties. */
5245 while (CONSP (prop))
5246 {
5247 if (single_display_spec_string_p (XCAR (prop), string))
5248 return true;
5249 prop = XCDR (prop);
5250 }
5251 }
5252 else if (VECTORP (prop))
5253 {
5254 /* A vector of sub-properties. */
5255 ptrdiff_t i;
5256 for (i = 0; i < ASIZE (prop); ++i)
5257 if (single_display_spec_string_p (AREF (prop, i), string))
5258 return true;
5259 }
5260 else
5261 return single_display_spec_string_p (prop, string);
5262
5263 return false;
5264 }
5265
5266 /* Look for STRING in overlays and text properties in the current
5267 buffer, between character positions FROM and TO (excluding TO).
5268 BACK_P means look back (in this case, TO is supposed to be
5269 less than FROM).
5270 Value is the first character position where STRING was found, or
5271 zero if it wasn't found before hitting TO.
5272
5273 This function may only use code that doesn't eval because it is
5274 called asynchronously from note_mouse_highlight. */
5275
5276 static ptrdiff_t
5277 string_buffer_position_lim (Lisp_Object string,
5278 ptrdiff_t from, ptrdiff_t to, bool back_p)
5279 {
5280 Lisp_Object limit, prop, pos;
5281 bool found = false;
5282
5283 pos = make_number (max (from, BEGV));
5284
5285 if (!back_p) /* looking forward */
5286 {
5287 limit = make_number (min (to, ZV));
5288 while (!found && !EQ (pos, limit))
5289 {
5290 prop = Fget_char_property (pos, Qdisplay, Qnil);
5291 if (!NILP (prop) && display_prop_string_p (prop, string))
5292 found = true;
5293 else
5294 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5295 limit);
5296 }
5297 }
5298 else /* looking back */
5299 {
5300 limit = make_number (max (to, BEGV));
5301 while (!found && !EQ (pos, limit))
5302 {
5303 prop = Fget_char_property (pos, Qdisplay, Qnil);
5304 if (!NILP (prop) && display_prop_string_p (prop, string))
5305 found = true;
5306 else
5307 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5308 limit);
5309 }
5310 }
5311
5312 return found ? XINT (pos) : 0;
5313 }
5314
5315 /* Determine which buffer position in current buffer STRING comes from.
5316 AROUND_CHARPOS is an approximate position where it could come from.
5317 Value is the buffer position or 0 if it couldn't be determined.
5318
5319 This function is necessary because we don't record buffer positions
5320 in glyphs generated from strings (to keep struct glyph small).
5321 This function may only use code that doesn't eval because it is
5322 called asynchronously from note_mouse_highlight. */
5323
5324 static ptrdiff_t
5325 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5326 {
5327 const int MAX_DISTANCE = 1000;
5328 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5329 around_charpos + MAX_DISTANCE,
5330 false);
5331
5332 if (!found)
5333 found = string_buffer_position_lim (string, around_charpos,
5334 around_charpos - MAX_DISTANCE, true);
5335 return found;
5336 }
5337
5338
5339 \f
5340 /***********************************************************************
5341 `composition' property
5342 ***********************************************************************/
5343
5344 /* Set up iterator IT from `composition' property at its current
5345 position. Called from handle_stop. */
5346
5347 static enum prop_handled
5348 handle_composition_prop (struct it *it)
5349 {
5350 Lisp_Object prop, string;
5351 ptrdiff_t pos, pos_byte, start, end;
5352
5353 if (STRINGP (it->string))
5354 {
5355 unsigned char *s;
5356
5357 pos = IT_STRING_CHARPOS (*it);
5358 pos_byte = IT_STRING_BYTEPOS (*it);
5359 string = it->string;
5360 s = SDATA (string) + pos_byte;
5361 it->c = STRING_CHAR (s);
5362 }
5363 else
5364 {
5365 pos = IT_CHARPOS (*it);
5366 pos_byte = IT_BYTEPOS (*it);
5367 string = Qnil;
5368 it->c = FETCH_CHAR (pos_byte);
5369 }
5370
5371 /* If there's a valid composition and point is not inside of the
5372 composition (in the case that the composition is from the current
5373 buffer), draw a glyph composed from the composition components. */
5374 if (find_composition (pos, -1, &start, &end, &prop, string)
5375 && composition_valid_p (start, end, prop)
5376 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5377 {
5378 if (start < pos)
5379 /* As we can't handle this situation (perhaps font-lock added
5380 a new composition), we just return here hoping that next
5381 redisplay will detect this composition much earlier. */
5382 return HANDLED_NORMALLY;
5383 if (start != pos)
5384 {
5385 if (STRINGP (it->string))
5386 pos_byte = string_char_to_byte (it->string, start);
5387 else
5388 pos_byte = CHAR_TO_BYTE (start);
5389 }
5390 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5391 prop, string);
5392
5393 if (it->cmp_it.id >= 0)
5394 {
5395 it->cmp_it.ch = -1;
5396 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5397 it->cmp_it.nglyphs = -1;
5398 }
5399 }
5400
5401 return HANDLED_NORMALLY;
5402 }
5403
5404
5405 \f
5406 /***********************************************************************
5407 Overlay strings
5408 ***********************************************************************/
5409
5410 /* The following structure is used to record overlay strings for
5411 later sorting in load_overlay_strings. */
5412
5413 struct overlay_entry
5414 {
5415 Lisp_Object overlay;
5416 Lisp_Object string;
5417 EMACS_INT priority;
5418 bool after_string_p;
5419 };
5420
5421
5422 /* Set up iterator IT from overlay strings at its current position.
5423 Called from handle_stop. */
5424
5425 static enum prop_handled
5426 handle_overlay_change (struct it *it)
5427 {
5428 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5429 return HANDLED_RECOMPUTE_PROPS;
5430 else
5431 return HANDLED_NORMALLY;
5432 }
5433
5434
5435 /* Set up the next overlay string for delivery by IT, if there is an
5436 overlay string to deliver. Called by set_iterator_to_next when the
5437 end of the current overlay string is reached. If there are more
5438 overlay strings to display, IT->string and
5439 IT->current.overlay_string_index are set appropriately here.
5440 Otherwise IT->string is set to nil. */
5441
5442 static void
5443 next_overlay_string (struct it *it)
5444 {
5445 ++it->current.overlay_string_index;
5446 if (it->current.overlay_string_index == it->n_overlay_strings)
5447 {
5448 /* No more overlay strings. Restore IT's settings to what
5449 they were before overlay strings were processed, and
5450 continue to deliver from current_buffer. */
5451
5452 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5453 pop_it (it);
5454 eassert (it->sp > 0
5455 || (NILP (it->string)
5456 && it->method == GET_FROM_BUFFER
5457 && it->stop_charpos >= BEGV
5458 && it->stop_charpos <= it->end_charpos));
5459 it->current.overlay_string_index = -1;
5460 it->n_overlay_strings = 0;
5461 /* If there's an empty display string on the stack, pop the
5462 stack, to resync the bidi iterator with IT's position. Such
5463 empty strings are pushed onto the stack in
5464 get_overlay_strings_1. */
5465 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5466 pop_it (it);
5467
5468 /* Since we've exhausted overlay strings at this buffer
5469 position, set the flag to ignore overlays until we move to
5470 another position. The flag is reset in
5471 next_element_from_buffer. */
5472 it->ignore_overlay_strings_at_pos_p = true;
5473
5474 /* If we're at the end of the buffer, record that we have
5475 processed the overlay strings there already, so that
5476 next_element_from_buffer doesn't try it again. */
5477 if (NILP (it->string)
5478 && IT_CHARPOS (*it) >= it->end_charpos
5479 && it->overlay_strings_charpos >= it->end_charpos)
5480 it->overlay_strings_at_end_processed_p = true;
5481 /* Note: we reset overlay_strings_charpos only here, to make
5482 sure the just-processed overlays were indeed at EOB.
5483 Otherwise, overlays on text with invisible text property,
5484 which are processed with IT's position past the invisible
5485 text, might fool us into thinking the overlays at EOB were
5486 already processed (linum-mode can cause this, for
5487 example). */
5488 it->overlay_strings_charpos = -1;
5489 }
5490 else
5491 {
5492 /* There are more overlay strings to process. If
5493 IT->current.overlay_string_index has advanced to a position
5494 where we must load IT->overlay_strings with more strings, do
5495 it. We must load at the IT->overlay_strings_charpos where
5496 IT->n_overlay_strings was originally computed; when invisible
5497 text is present, this might not be IT_CHARPOS (Bug#7016). */
5498 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5499
5500 if (it->current.overlay_string_index && i == 0)
5501 load_overlay_strings (it, it->overlay_strings_charpos);
5502
5503 /* Initialize IT to deliver display elements from the overlay
5504 string. */
5505 it->string = it->overlay_strings[i];
5506 it->multibyte_p = STRING_MULTIBYTE (it->string);
5507 SET_TEXT_POS (it->current.string_pos, 0, 0);
5508 it->method = GET_FROM_STRING;
5509 it->stop_charpos = 0;
5510 it->end_charpos = SCHARS (it->string);
5511 if (it->cmp_it.stop_pos >= 0)
5512 it->cmp_it.stop_pos = 0;
5513 it->prev_stop = 0;
5514 it->base_level_stop = 0;
5515
5516 /* Set up the bidi iterator for this overlay string. */
5517 if (it->bidi_p)
5518 {
5519 it->bidi_it.string.lstring = it->string;
5520 it->bidi_it.string.s = NULL;
5521 it->bidi_it.string.schars = SCHARS (it->string);
5522 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5523 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5524 it->bidi_it.string.unibyte = !it->multibyte_p;
5525 it->bidi_it.w = it->w;
5526 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5527 }
5528 }
5529
5530 CHECK_IT (it);
5531 }
5532
5533
5534 /* Compare two overlay_entry structures E1 and E2. Used as a
5535 comparison function for qsort in load_overlay_strings. Overlay
5536 strings for the same position are sorted so that
5537
5538 1. All after-strings come in front of before-strings, except
5539 when they come from the same overlay.
5540
5541 2. Within after-strings, strings are sorted so that overlay strings
5542 from overlays with higher priorities come first.
5543
5544 2. Within before-strings, strings are sorted so that overlay
5545 strings from overlays with higher priorities come last.
5546
5547 Value is analogous to strcmp. */
5548
5549
5550 static int
5551 compare_overlay_entries (const void *e1, const void *e2)
5552 {
5553 struct overlay_entry const *entry1 = e1;
5554 struct overlay_entry const *entry2 = e2;
5555 int result;
5556
5557 if (entry1->after_string_p != entry2->after_string_p)
5558 {
5559 /* Let after-strings appear in front of before-strings if
5560 they come from different overlays. */
5561 if (EQ (entry1->overlay, entry2->overlay))
5562 result = entry1->after_string_p ? 1 : -1;
5563 else
5564 result = entry1->after_string_p ? -1 : 1;
5565 }
5566 else if (entry1->priority != entry2->priority)
5567 {
5568 if (entry1->after_string_p)
5569 /* After-strings sorted in order of decreasing priority. */
5570 result = entry2->priority < entry1->priority ? -1 : 1;
5571 else
5572 /* Before-strings sorted in order of increasing priority. */
5573 result = entry1->priority < entry2->priority ? -1 : 1;
5574 }
5575 else
5576 result = 0;
5577
5578 return result;
5579 }
5580
5581
5582 /* Load the vector IT->overlay_strings with overlay strings from IT's
5583 current buffer position, or from CHARPOS if that is > 0. Set
5584 IT->n_overlays to the total number of overlay strings found.
5585
5586 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5587 a time. On entry into load_overlay_strings,
5588 IT->current.overlay_string_index gives the number of overlay
5589 strings that have already been loaded by previous calls to this
5590 function.
5591
5592 IT->add_overlay_start contains an additional overlay start
5593 position to consider for taking overlay strings from, if non-zero.
5594 This position comes into play when the overlay has an `invisible'
5595 property, and both before and after-strings. When we've skipped to
5596 the end of the overlay, because of its `invisible' property, we
5597 nevertheless want its before-string to appear.
5598 IT->add_overlay_start will contain the overlay start position
5599 in this case.
5600
5601 Overlay strings are sorted so that after-string strings come in
5602 front of before-string strings. Within before and after-strings,
5603 strings are sorted by overlay priority. See also function
5604 compare_overlay_entries. */
5605
5606 static void
5607 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5608 {
5609 Lisp_Object overlay, window, str, invisible;
5610 struct Lisp_Overlay *ov;
5611 ptrdiff_t start, end;
5612 ptrdiff_t n = 0, i, j;
5613 int invis;
5614 struct overlay_entry entriesbuf[20];
5615 ptrdiff_t size = ARRAYELTS (entriesbuf);
5616 struct overlay_entry *entries = entriesbuf;
5617 USE_SAFE_ALLOCA;
5618
5619 if (charpos <= 0)
5620 charpos = IT_CHARPOS (*it);
5621
5622 /* Append the overlay string STRING of overlay OVERLAY to vector
5623 `entries' which has size `size' and currently contains `n'
5624 elements. AFTER_P means STRING is an after-string of
5625 OVERLAY. */
5626 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5627 do \
5628 { \
5629 Lisp_Object priority; \
5630 \
5631 if (n == size) \
5632 { \
5633 struct overlay_entry *old = entries; \
5634 SAFE_NALLOCA (entries, 2, size); \
5635 memcpy (entries, old, size * sizeof *entries); \
5636 size *= 2; \
5637 } \
5638 \
5639 entries[n].string = (STRING); \
5640 entries[n].overlay = (OVERLAY); \
5641 priority = Foverlay_get ((OVERLAY), Qpriority); \
5642 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5643 entries[n].after_string_p = (AFTER_P); \
5644 ++n; \
5645 } \
5646 while (false)
5647
5648 /* Process overlay before the overlay center. */
5649 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5650 {
5651 XSETMISC (overlay, ov);
5652 eassert (OVERLAYP (overlay));
5653 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5654 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5655
5656 if (end < charpos)
5657 break;
5658
5659 /* Skip this overlay if it doesn't start or end at IT's current
5660 position. */
5661 if (end != charpos && start != charpos)
5662 continue;
5663
5664 /* Skip this overlay if it doesn't apply to IT->w. */
5665 window = Foverlay_get (overlay, Qwindow);
5666 if (WINDOWP (window) && XWINDOW (window) != it->w)
5667 continue;
5668
5669 /* If the text ``under'' the overlay is invisible, both before-
5670 and after-strings from this overlay are visible; start and
5671 end position are indistinguishable. */
5672 invisible = Foverlay_get (overlay, Qinvisible);
5673 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5674
5675 /* If overlay has a non-empty before-string, record it. */
5676 if ((start == charpos || (end == charpos && invis != 0))
5677 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5678 && SCHARS (str))
5679 RECORD_OVERLAY_STRING (overlay, str, false);
5680
5681 /* If overlay has a non-empty after-string, record it. */
5682 if ((end == charpos || (start == charpos && invis != 0))
5683 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, true);
5686 }
5687
5688 /* Process overlays after the overlay center. */
5689 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5690 {
5691 XSETMISC (overlay, ov);
5692 eassert (OVERLAYP (overlay));
5693 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5694 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5695
5696 if (start > charpos)
5697 break;
5698
5699 /* Skip this overlay if it doesn't start or end at IT's current
5700 position. */
5701 if (end != charpos && start != charpos)
5702 continue;
5703
5704 /* Skip this overlay if it doesn't apply to IT->w. */
5705 window = Foverlay_get (overlay, Qwindow);
5706 if (WINDOWP (window) && XWINDOW (window) != it->w)
5707 continue;
5708
5709 /* If the text ``under'' the overlay is invisible, it has a zero
5710 dimension, and both before- and after-strings apply. */
5711 invisible = Foverlay_get (overlay, Qinvisible);
5712 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5713
5714 /* If overlay has a non-empty before-string, record it. */
5715 if ((start == charpos || (end == charpos && invis != 0))
5716 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5717 && SCHARS (str))
5718 RECORD_OVERLAY_STRING (overlay, str, false);
5719
5720 /* If overlay has a non-empty after-string, record it. */
5721 if ((end == charpos || (start == charpos && invis != 0))
5722 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5723 && SCHARS (str))
5724 RECORD_OVERLAY_STRING (overlay, str, true);
5725 }
5726
5727 #undef RECORD_OVERLAY_STRING
5728
5729 /* Sort entries. */
5730 if (n > 1)
5731 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5732
5733 /* Record number of overlay strings, and where we computed it. */
5734 it->n_overlay_strings = n;
5735 it->overlay_strings_charpos = charpos;
5736
5737 /* IT->current.overlay_string_index is the number of overlay strings
5738 that have already been consumed by IT. Copy some of the
5739 remaining overlay strings to IT->overlay_strings. */
5740 i = 0;
5741 j = it->current.overlay_string_index;
5742 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5743 {
5744 it->overlay_strings[i] = entries[j].string;
5745 it->string_overlays[i++] = entries[j++].overlay;
5746 }
5747
5748 CHECK_IT (it);
5749 SAFE_FREE ();
5750 }
5751
5752
5753 /* Get the first chunk of overlay strings at IT's current buffer
5754 position, or at CHARPOS if that is > 0. Value is true if at
5755 least one overlay string was found. */
5756
5757 static bool
5758 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5759 {
5760 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5761 process. This fills IT->overlay_strings with strings, and sets
5762 IT->n_overlay_strings to the total number of strings to process.
5763 IT->pos.overlay_string_index has to be set temporarily to zero
5764 because load_overlay_strings needs this; it must be set to -1
5765 when no overlay strings are found because a zero value would
5766 indicate a position in the first overlay string. */
5767 it->current.overlay_string_index = 0;
5768 load_overlay_strings (it, charpos);
5769
5770 /* If we found overlay strings, set up IT to deliver display
5771 elements from the first one. Otherwise set up IT to deliver
5772 from current_buffer. */
5773 if (it->n_overlay_strings)
5774 {
5775 /* Make sure we know settings in current_buffer, so that we can
5776 restore meaningful values when we're done with the overlay
5777 strings. */
5778 if (compute_stop_p)
5779 compute_stop_pos (it);
5780 eassert (it->face_id >= 0);
5781
5782 /* Save IT's settings. They are restored after all overlay
5783 strings have been processed. */
5784 eassert (!compute_stop_p || it->sp == 0);
5785
5786 /* When called from handle_stop, there might be an empty display
5787 string loaded. In that case, don't bother saving it. But
5788 don't use this optimization with the bidi iterator, since we
5789 need the corresponding pop_it call to resync the bidi
5790 iterator's position with IT's position, after we are done
5791 with the overlay strings. (The corresponding call to pop_it
5792 in case of an empty display string is in
5793 next_overlay_string.) */
5794 if (!(!it->bidi_p
5795 && STRINGP (it->string) && !SCHARS (it->string)))
5796 push_it (it, NULL);
5797
5798 /* Set up IT to deliver display elements from the first overlay
5799 string. */
5800 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5801 it->string = it->overlay_strings[0];
5802 it->from_overlay = Qnil;
5803 it->stop_charpos = 0;
5804 eassert (STRINGP (it->string));
5805 it->end_charpos = SCHARS (it->string);
5806 it->prev_stop = 0;
5807 it->base_level_stop = 0;
5808 it->multibyte_p = STRING_MULTIBYTE (it->string);
5809 it->method = GET_FROM_STRING;
5810 it->from_disp_prop_p = 0;
5811
5812 /* Force paragraph direction to be that of the parent
5813 buffer. */
5814 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5815 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5816 else
5817 it->paragraph_embedding = L2R;
5818
5819 /* Set up the bidi iterator for this overlay string. */
5820 if (it->bidi_p)
5821 {
5822 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5823
5824 it->bidi_it.string.lstring = it->string;
5825 it->bidi_it.string.s = NULL;
5826 it->bidi_it.string.schars = SCHARS (it->string);
5827 it->bidi_it.string.bufpos = pos;
5828 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5829 it->bidi_it.string.unibyte = !it->multibyte_p;
5830 it->bidi_it.w = it->w;
5831 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5832 }
5833 return true;
5834 }
5835
5836 it->current.overlay_string_index = -1;
5837 return false;
5838 }
5839
5840 static bool
5841 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5842 {
5843 it->string = Qnil;
5844 it->method = GET_FROM_BUFFER;
5845
5846 get_overlay_strings_1 (it, charpos, true);
5847
5848 CHECK_IT (it);
5849
5850 /* Value is true if we found at least one overlay string. */
5851 return STRINGP (it->string);
5852 }
5853
5854
5855 \f
5856 /***********************************************************************
5857 Saving and restoring state
5858 ***********************************************************************/
5859
5860 /* Save current settings of IT on IT->stack. Called, for example,
5861 before setting up IT for an overlay string, to be able to restore
5862 IT's settings to what they were after the overlay string has been
5863 processed. If POSITION is non-NULL, it is the position to save on
5864 the stack instead of IT->position. */
5865
5866 static void
5867 push_it (struct it *it, struct text_pos *position)
5868 {
5869 struct iterator_stack_entry *p;
5870
5871 eassert (it->sp < IT_STACK_SIZE);
5872 p = it->stack + it->sp;
5873
5874 p->stop_charpos = it->stop_charpos;
5875 p->prev_stop = it->prev_stop;
5876 p->base_level_stop = it->base_level_stop;
5877 p->cmp_it = it->cmp_it;
5878 eassert (it->face_id >= 0);
5879 p->face_id = it->face_id;
5880 p->string = it->string;
5881 p->method = it->method;
5882 p->from_overlay = it->from_overlay;
5883 switch (p->method)
5884 {
5885 case GET_FROM_IMAGE:
5886 p->u.image.object = it->object;
5887 p->u.image.image_id = it->image_id;
5888 p->u.image.slice = it->slice;
5889 break;
5890 case GET_FROM_STRETCH:
5891 p->u.stretch.object = it->object;
5892 break;
5893 }
5894 p->position = position ? *position : it->position;
5895 p->current = it->current;
5896 p->end_charpos = it->end_charpos;
5897 p->string_nchars = it->string_nchars;
5898 p->area = it->area;
5899 p->multibyte_p = it->multibyte_p;
5900 p->avoid_cursor_p = it->avoid_cursor_p;
5901 p->space_width = it->space_width;
5902 p->font_height = it->font_height;
5903 p->voffset = it->voffset;
5904 p->string_from_display_prop_p = it->string_from_display_prop_p;
5905 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5906 p->display_ellipsis_p = false;
5907 p->line_wrap = it->line_wrap;
5908 p->bidi_p = it->bidi_p;
5909 p->paragraph_embedding = it->paragraph_embedding;
5910 p->from_disp_prop_p = it->from_disp_prop_p;
5911 ++it->sp;
5912
5913 /* Save the state of the bidi iterator as well. */
5914 if (it->bidi_p)
5915 bidi_push_it (&it->bidi_it);
5916 }
5917
5918 static void
5919 iterate_out_of_display_property (struct it *it)
5920 {
5921 bool buffer_p = !STRINGP (it->string);
5922 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5923 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5924
5925 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5926
5927 /* Maybe initialize paragraph direction. If we are at the beginning
5928 of a new paragraph, next_element_from_buffer may not have a
5929 chance to do that. */
5930 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5931 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5932 /* prev_stop can be zero, so check against BEGV as well. */
5933 while (it->bidi_it.charpos >= bob
5934 && it->prev_stop <= it->bidi_it.charpos
5935 && it->bidi_it.charpos < CHARPOS (it->position)
5936 && it->bidi_it.charpos < eob)
5937 bidi_move_to_visually_next (&it->bidi_it);
5938 /* Record the stop_pos we just crossed, for when we cross it
5939 back, maybe. */
5940 if (it->bidi_it.charpos > CHARPOS (it->position))
5941 it->prev_stop = CHARPOS (it->position);
5942 /* If we ended up not where pop_it put us, resync IT's
5943 positional members with the bidi iterator. */
5944 if (it->bidi_it.charpos != CHARPOS (it->position))
5945 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5946 if (buffer_p)
5947 it->current.pos = it->position;
5948 else
5949 it->current.string_pos = it->position;
5950 }
5951
5952 /* Restore IT's settings from IT->stack. Called, for example, when no
5953 more overlay strings must be processed, and we return to delivering
5954 display elements from a buffer, or when the end of a string from a
5955 `display' property is reached and we return to delivering display
5956 elements from an overlay string, or from a buffer. */
5957
5958 static void
5959 pop_it (struct it *it)
5960 {
5961 struct iterator_stack_entry *p;
5962 bool from_display_prop = it->from_disp_prop_p;
5963
5964 eassert (it->sp > 0);
5965 --it->sp;
5966 p = it->stack + it->sp;
5967 it->stop_charpos = p->stop_charpos;
5968 it->prev_stop = p->prev_stop;
5969 it->base_level_stop = p->base_level_stop;
5970 it->cmp_it = p->cmp_it;
5971 it->face_id = p->face_id;
5972 it->current = p->current;
5973 it->position = p->position;
5974 it->string = p->string;
5975 it->from_overlay = p->from_overlay;
5976 if (NILP (it->string))
5977 SET_TEXT_POS (it->current.string_pos, -1, -1);
5978 it->method = p->method;
5979 switch (it->method)
5980 {
5981 case GET_FROM_IMAGE:
5982 it->image_id = p->u.image.image_id;
5983 it->object = p->u.image.object;
5984 it->slice = p->u.image.slice;
5985 break;
5986 case GET_FROM_STRETCH:
5987 it->object = p->u.stretch.object;
5988 break;
5989 case GET_FROM_BUFFER:
5990 it->object = it->w->contents;
5991 break;
5992 case GET_FROM_STRING:
5993 {
5994 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5995
5996 /* Restore the face_box_p flag, since it could have been
5997 overwritten by the face of the object that we just finished
5998 displaying. */
5999 if (face)
6000 it->face_box_p = face->box != FACE_NO_BOX;
6001 it->object = it->string;
6002 }
6003 break;
6004 case GET_FROM_DISPLAY_VECTOR:
6005 if (it->s)
6006 it->method = GET_FROM_C_STRING;
6007 else if (STRINGP (it->string))
6008 it->method = GET_FROM_STRING;
6009 else
6010 {
6011 it->method = GET_FROM_BUFFER;
6012 it->object = it->w->contents;
6013 }
6014 }
6015 it->end_charpos = p->end_charpos;
6016 it->string_nchars = p->string_nchars;
6017 it->area = p->area;
6018 it->multibyte_p = p->multibyte_p;
6019 it->avoid_cursor_p = p->avoid_cursor_p;
6020 it->space_width = p->space_width;
6021 it->font_height = p->font_height;
6022 it->voffset = p->voffset;
6023 it->string_from_display_prop_p = p->string_from_display_prop_p;
6024 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6025 it->line_wrap = p->line_wrap;
6026 it->bidi_p = p->bidi_p;
6027 it->paragraph_embedding = p->paragraph_embedding;
6028 it->from_disp_prop_p = p->from_disp_prop_p;
6029 if (it->bidi_p)
6030 {
6031 bidi_pop_it (&it->bidi_it);
6032 /* Bidi-iterate until we get out of the portion of text, if any,
6033 covered by a `display' text property or by an overlay with
6034 `display' property. (We cannot just jump there, because the
6035 internal coherency of the bidi iterator state can not be
6036 preserved across such jumps.) We also must determine the
6037 paragraph base direction if the overlay we just processed is
6038 at the beginning of a new paragraph. */
6039 if (from_display_prop
6040 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6041 iterate_out_of_display_property (it);
6042
6043 eassert ((BUFFERP (it->object)
6044 && IT_CHARPOS (*it) == it->bidi_it.charpos
6045 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6046 || (STRINGP (it->object)
6047 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6048 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6049 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6050 }
6051 }
6052
6053
6054 \f
6055 /***********************************************************************
6056 Moving over lines
6057 ***********************************************************************/
6058
6059 /* Set IT's current position to the previous line start. */
6060
6061 static void
6062 back_to_previous_line_start (struct it *it)
6063 {
6064 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6065
6066 DEC_BOTH (cp, bp);
6067 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6068 }
6069
6070
6071 /* Move IT to the next line start.
6072
6073 Value is true if a newline was found. Set *SKIPPED_P to true if
6074 we skipped over part of the text (as opposed to moving the iterator
6075 continuously over the text). Otherwise, don't change the value
6076 of *SKIPPED_P.
6077
6078 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6079 iterator on the newline, if it was found.
6080
6081 Newlines may come from buffer text, overlay strings, or strings
6082 displayed via the `display' property. That's the reason we can't
6083 simply use find_newline_no_quit.
6084
6085 Note that this function may not skip over invisible text that is so
6086 because of text properties and immediately follows a newline. If
6087 it would, function reseat_at_next_visible_line_start, when called
6088 from set_iterator_to_next, would effectively make invisible
6089 characters following a newline part of the wrong glyph row, which
6090 leads to wrong cursor motion. */
6091
6092 static bool
6093 forward_to_next_line_start (struct it *it, bool *skipped_p,
6094 struct bidi_it *bidi_it_prev)
6095 {
6096 ptrdiff_t old_selective;
6097 bool newline_found_p = false;
6098 int n;
6099 const int MAX_NEWLINE_DISTANCE = 500;
6100
6101 /* If already on a newline, just consume it to avoid unintended
6102 skipping over invisible text below. */
6103 if (it->what == IT_CHARACTER
6104 && it->c == '\n'
6105 && CHARPOS (it->position) == IT_CHARPOS (*it))
6106 {
6107 if (it->bidi_p && bidi_it_prev)
6108 *bidi_it_prev = it->bidi_it;
6109 set_iterator_to_next (it, false);
6110 it->c = 0;
6111 return true;
6112 }
6113
6114 /* Don't handle selective display in the following. It's (a)
6115 unnecessary because it's done by the caller, and (b) leads to an
6116 infinite recursion because next_element_from_ellipsis indirectly
6117 calls this function. */
6118 old_selective = it->selective;
6119 it->selective = 0;
6120
6121 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6122 from buffer text. */
6123 for (n = 0;
6124 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6125 n += !STRINGP (it->string))
6126 {
6127 if (!get_next_display_element (it))
6128 return false;
6129 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6130 if (newline_found_p && it->bidi_p && bidi_it_prev)
6131 *bidi_it_prev = it->bidi_it;
6132 set_iterator_to_next (it, false);
6133 }
6134
6135 /* If we didn't find a newline near enough, see if we can use a
6136 short-cut. */
6137 if (!newline_found_p)
6138 {
6139 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6140 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6141 1, &bytepos);
6142 Lisp_Object pos;
6143
6144 eassert (!STRINGP (it->string));
6145
6146 /* If there isn't any `display' property in sight, and no
6147 overlays, we can just use the position of the newline in
6148 buffer text. */
6149 if (it->stop_charpos >= limit
6150 || ((pos = Fnext_single_property_change (make_number (start),
6151 Qdisplay, Qnil,
6152 make_number (limit)),
6153 NILP (pos))
6154 && next_overlay_change (start) == ZV))
6155 {
6156 if (!it->bidi_p)
6157 {
6158 IT_CHARPOS (*it) = limit;
6159 IT_BYTEPOS (*it) = bytepos;
6160 }
6161 else
6162 {
6163 struct bidi_it bprev;
6164
6165 /* Help bidi.c avoid expensive searches for display
6166 properties and overlays, by telling it that there are
6167 none up to `limit'. */
6168 if (it->bidi_it.disp_pos < limit)
6169 {
6170 it->bidi_it.disp_pos = limit;
6171 it->bidi_it.disp_prop = 0;
6172 }
6173 do {
6174 bprev = it->bidi_it;
6175 bidi_move_to_visually_next (&it->bidi_it);
6176 } while (it->bidi_it.charpos != limit);
6177 IT_CHARPOS (*it) = limit;
6178 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6179 if (bidi_it_prev)
6180 *bidi_it_prev = bprev;
6181 }
6182 *skipped_p = newline_found_p = true;
6183 }
6184 else
6185 {
6186 while (get_next_display_element (it)
6187 && !newline_found_p)
6188 {
6189 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6190 if (newline_found_p && it->bidi_p && bidi_it_prev)
6191 *bidi_it_prev = it->bidi_it;
6192 set_iterator_to_next (it, false);
6193 }
6194 }
6195 }
6196
6197 it->selective = old_selective;
6198 return newline_found_p;
6199 }
6200
6201
6202 /* Set IT's current position to the previous visible line start. Skip
6203 invisible text that is so either due to text properties or due to
6204 selective display. Caution: this does not change IT->current_x and
6205 IT->hpos. */
6206
6207 static void
6208 back_to_previous_visible_line_start (struct it *it)
6209 {
6210 while (IT_CHARPOS (*it) > BEGV)
6211 {
6212 back_to_previous_line_start (it);
6213
6214 if (IT_CHARPOS (*it) <= BEGV)
6215 break;
6216
6217 /* If selective > 0, then lines indented more than its value are
6218 invisible. */
6219 if (it->selective > 0
6220 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6221 it->selective))
6222 continue;
6223
6224 /* Check the newline before point for invisibility. */
6225 {
6226 Lisp_Object prop;
6227 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6228 Qinvisible, it->window);
6229 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6230 continue;
6231 }
6232
6233 if (IT_CHARPOS (*it) <= BEGV)
6234 break;
6235
6236 {
6237 struct it it2;
6238 void *it2data = NULL;
6239 ptrdiff_t pos;
6240 ptrdiff_t beg, end;
6241 Lisp_Object val, overlay;
6242
6243 SAVE_IT (it2, *it, it2data);
6244
6245 /* If newline is part of a composition, continue from start of composition */
6246 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6247 && beg < IT_CHARPOS (*it))
6248 goto replaced;
6249
6250 /* If newline is replaced by a display property, find start of overlay
6251 or interval and continue search from that point. */
6252 pos = --IT_CHARPOS (it2);
6253 --IT_BYTEPOS (it2);
6254 it2.sp = 0;
6255 bidi_unshelve_cache (NULL, false);
6256 it2.string_from_display_prop_p = false;
6257 it2.from_disp_prop_p = false;
6258 if (handle_display_prop (&it2) == HANDLED_RETURN
6259 && !NILP (val = get_char_property_and_overlay
6260 (make_number (pos), Qdisplay, Qnil, &overlay))
6261 && (OVERLAYP (overlay)
6262 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6263 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6264 {
6265 RESTORE_IT (it, it, it2data);
6266 goto replaced;
6267 }
6268
6269 /* Newline is not replaced by anything -- so we are done. */
6270 RESTORE_IT (it, it, it2data);
6271 break;
6272
6273 replaced:
6274 if (beg < BEGV)
6275 beg = BEGV;
6276 IT_CHARPOS (*it) = beg;
6277 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6278 }
6279 }
6280
6281 it->continuation_lines_width = 0;
6282
6283 eassert (IT_CHARPOS (*it) >= BEGV);
6284 eassert (IT_CHARPOS (*it) == BEGV
6285 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6286 CHECK_IT (it);
6287 }
6288
6289
6290 /* Reseat iterator IT at the previous visible line start. Skip
6291 invisible text that is so either due to text properties or due to
6292 selective display. At the end, update IT's overlay information,
6293 face information etc. */
6294
6295 void
6296 reseat_at_previous_visible_line_start (struct it *it)
6297 {
6298 back_to_previous_visible_line_start (it);
6299 reseat (it, it->current.pos, true);
6300 CHECK_IT (it);
6301 }
6302
6303
6304 /* Reseat iterator IT on the next visible line start in the current
6305 buffer. ON_NEWLINE_P means position IT on the newline
6306 preceding the line start. Skip over invisible text that is so
6307 because of selective display. Compute faces, overlays etc at the
6308 new position. Note that this function does not skip over text that
6309 is invisible because of text properties. */
6310
6311 static void
6312 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6313 {
6314 bool skipped_p = false;
6315 struct bidi_it bidi_it_prev;
6316 bool newline_found_p
6317 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6318
6319 /* Skip over lines that are invisible because they are indented
6320 more than the value of IT->selective. */
6321 if (it->selective > 0)
6322 while (IT_CHARPOS (*it) < ZV
6323 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6324 it->selective))
6325 {
6326 eassert (IT_BYTEPOS (*it) == BEGV
6327 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6328 newline_found_p =
6329 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6330 }
6331
6332 /* Position on the newline if that's what's requested. */
6333 if (on_newline_p && newline_found_p)
6334 {
6335 if (STRINGP (it->string))
6336 {
6337 if (IT_STRING_CHARPOS (*it) > 0)
6338 {
6339 if (!it->bidi_p)
6340 {
6341 --IT_STRING_CHARPOS (*it);
6342 --IT_STRING_BYTEPOS (*it);
6343 }
6344 else
6345 {
6346 /* We need to restore the bidi iterator to the state
6347 it had on the newline, and resync the IT's
6348 position with that. */
6349 it->bidi_it = bidi_it_prev;
6350 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6351 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6352 }
6353 }
6354 }
6355 else if (IT_CHARPOS (*it) > BEGV)
6356 {
6357 if (!it->bidi_p)
6358 {
6359 --IT_CHARPOS (*it);
6360 --IT_BYTEPOS (*it);
6361 }
6362 else
6363 {
6364 /* We need to restore the bidi iterator to the state it
6365 had on the newline and resync IT with that. */
6366 it->bidi_it = bidi_it_prev;
6367 IT_CHARPOS (*it) = it->bidi_it.charpos;
6368 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6369 }
6370 reseat (it, it->current.pos, false);
6371 }
6372 }
6373 else if (skipped_p)
6374 reseat (it, it->current.pos, false);
6375
6376 CHECK_IT (it);
6377 }
6378
6379
6380 \f
6381 /***********************************************************************
6382 Changing an iterator's position
6383 ***********************************************************************/
6384
6385 /* Change IT's current position to POS in current_buffer.
6386 If FORCE_P, always check for text properties at the new position.
6387 Otherwise, text properties are only looked up if POS >=
6388 IT->check_charpos of a property. */
6389
6390 static void
6391 reseat (struct it *it, struct text_pos pos, bool force_p)
6392 {
6393 ptrdiff_t original_pos = IT_CHARPOS (*it);
6394
6395 reseat_1 (it, pos, false);
6396
6397 /* Determine where to check text properties. Avoid doing it
6398 where possible because text property lookup is very expensive. */
6399 if (force_p
6400 || CHARPOS (pos) > it->stop_charpos
6401 || CHARPOS (pos) < original_pos)
6402 {
6403 if (it->bidi_p)
6404 {
6405 /* For bidi iteration, we need to prime prev_stop and
6406 base_level_stop with our best estimations. */
6407 /* Implementation note: Of course, POS is not necessarily a
6408 stop position, so assigning prev_pos to it is a lie; we
6409 should have called compute_stop_backwards. However, if
6410 the current buffer does not include any R2L characters,
6411 that call would be a waste of cycles, because the
6412 iterator will never move back, and thus never cross this
6413 "fake" stop position. So we delay that backward search
6414 until the time we really need it, in next_element_from_buffer. */
6415 if (CHARPOS (pos) != it->prev_stop)
6416 it->prev_stop = CHARPOS (pos);
6417 if (CHARPOS (pos) < it->base_level_stop)
6418 it->base_level_stop = 0; /* meaning it's unknown */
6419 handle_stop (it);
6420 }
6421 else
6422 {
6423 handle_stop (it);
6424 it->prev_stop = it->base_level_stop = 0;
6425 }
6426
6427 }
6428
6429 CHECK_IT (it);
6430 }
6431
6432
6433 /* Change IT's buffer position to POS. SET_STOP_P means set
6434 IT->stop_pos to POS, also. */
6435
6436 static void
6437 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6438 {
6439 /* Don't call this function when scanning a C string. */
6440 eassert (it->s == NULL);
6441
6442 /* POS must be a reasonable value. */
6443 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6444
6445 it->current.pos = it->position = pos;
6446 it->end_charpos = ZV;
6447 it->dpvec = NULL;
6448 it->current.dpvec_index = -1;
6449 it->current.overlay_string_index = -1;
6450 IT_STRING_CHARPOS (*it) = -1;
6451 IT_STRING_BYTEPOS (*it) = -1;
6452 it->string = Qnil;
6453 it->method = GET_FROM_BUFFER;
6454 it->object = it->w->contents;
6455 it->area = TEXT_AREA;
6456 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6457 it->sp = 0;
6458 it->string_from_display_prop_p = false;
6459 it->string_from_prefix_prop_p = false;
6460
6461 it->from_disp_prop_p = false;
6462 it->face_before_selective_p = false;
6463 if (it->bidi_p)
6464 {
6465 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6466 &it->bidi_it);
6467 bidi_unshelve_cache (NULL, false);
6468 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6469 it->bidi_it.string.s = NULL;
6470 it->bidi_it.string.lstring = Qnil;
6471 it->bidi_it.string.bufpos = 0;
6472 it->bidi_it.string.from_disp_str = false;
6473 it->bidi_it.string.unibyte = false;
6474 it->bidi_it.w = it->w;
6475 }
6476
6477 if (set_stop_p)
6478 {
6479 it->stop_charpos = CHARPOS (pos);
6480 it->base_level_stop = CHARPOS (pos);
6481 }
6482 /* This make the information stored in it->cmp_it invalidate. */
6483 it->cmp_it.id = -1;
6484 }
6485
6486
6487 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6488 If S is non-null, it is a C string to iterate over. Otherwise,
6489 STRING gives a Lisp string to iterate over.
6490
6491 If PRECISION > 0, don't return more then PRECISION number of
6492 characters from the string.
6493
6494 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6495 characters have been returned. FIELD_WIDTH < 0 means an infinite
6496 field width.
6497
6498 MULTIBYTE = 0 means disable processing of multibyte characters,
6499 MULTIBYTE > 0 means enable it,
6500 MULTIBYTE < 0 means use IT->multibyte_p.
6501
6502 IT must be initialized via a prior call to init_iterator before
6503 calling this function. */
6504
6505 static void
6506 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6507 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6508 int multibyte)
6509 {
6510 /* No text property checks performed by default, but see below. */
6511 it->stop_charpos = -1;
6512
6513 /* Set iterator position and end position. */
6514 memset (&it->current, 0, sizeof it->current);
6515 it->current.overlay_string_index = -1;
6516 it->current.dpvec_index = -1;
6517 eassert (charpos >= 0);
6518
6519 /* If STRING is specified, use its multibyteness, otherwise use the
6520 setting of MULTIBYTE, if specified. */
6521 if (multibyte >= 0)
6522 it->multibyte_p = multibyte > 0;
6523
6524 /* Bidirectional reordering of strings is controlled by the default
6525 value of bidi-display-reordering. Don't try to reorder while
6526 loading loadup.el, as the necessary character property tables are
6527 not yet available. */
6528 it->bidi_p =
6529 NILP (Vpurify_flag)
6530 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6531
6532 if (s == NULL)
6533 {
6534 eassert (STRINGP (string));
6535 it->string = string;
6536 it->s = NULL;
6537 it->end_charpos = it->string_nchars = SCHARS (string);
6538 it->method = GET_FROM_STRING;
6539 it->current.string_pos = string_pos (charpos, string);
6540
6541 if (it->bidi_p)
6542 {
6543 it->bidi_it.string.lstring = string;
6544 it->bidi_it.string.s = NULL;
6545 it->bidi_it.string.schars = it->end_charpos;
6546 it->bidi_it.string.bufpos = 0;
6547 it->bidi_it.string.from_disp_str = false;
6548 it->bidi_it.string.unibyte = !it->multibyte_p;
6549 it->bidi_it.w = it->w;
6550 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6551 FRAME_WINDOW_P (it->f), &it->bidi_it);
6552 }
6553 }
6554 else
6555 {
6556 it->s = (const unsigned char *) s;
6557 it->string = Qnil;
6558
6559 /* Note that we use IT->current.pos, not it->current.string_pos,
6560 for displaying C strings. */
6561 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6562 if (it->multibyte_p)
6563 {
6564 it->current.pos = c_string_pos (charpos, s, true);
6565 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6566 }
6567 else
6568 {
6569 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6570 it->end_charpos = it->string_nchars = strlen (s);
6571 }
6572
6573 if (it->bidi_p)
6574 {
6575 it->bidi_it.string.lstring = Qnil;
6576 it->bidi_it.string.s = (const unsigned char *) s;
6577 it->bidi_it.string.schars = it->end_charpos;
6578 it->bidi_it.string.bufpos = 0;
6579 it->bidi_it.string.from_disp_str = false;
6580 it->bidi_it.string.unibyte = !it->multibyte_p;
6581 it->bidi_it.w = it->w;
6582 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6583 &it->bidi_it);
6584 }
6585 it->method = GET_FROM_C_STRING;
6586 }
6587
6588 /* PRECISION > 0 means don't return more than PRECISION characters
6589 from the string. */
6590 if (precision > 0 && it->end_charpos - charpos > precision)
6591 {
6592 it->end_charpos = it->string_nchars = charpos + precision;
6593 if (it->bidi_p)
6594 it->bidi_it.string.schars = it->end_charpos;
6595 }
6596
6597 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6598 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6599 FIELD_WIDTH < 0 means infinite field width. This is useful for
6600 padding with `-' at the end of a mode line. */
6601 if (field_width < 0)
6602 field_width = INFINITY;
6603 /* Implementation note: We deliberately don't enlarge
6604 it->bidi_it.string.schars here to fit it->end_charpos, because
6605 the bidi iterator cannot produce characters out of thin air. */
6606 if (field_width > it->end_charpos - charpos)
6607 it->end_charpos = charpos + field_width;
6608
6609 /* Use the standard display table for displaying strings. */
6610 if (DISP_TABLE_P (Vstandard_display_table))
6611 it->dp = XCHAR_TABLE (Vstandard_display_table);
6612
6613 it->stop_charpos = charpos;
6614 it->prev_stop = charpos;
6615 it->base_level_stop = 0;
6616 if (it->bidi_p)
6617 {
6618 it->bidi_it.first_elt = true;
6619 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6620 it->bidi_it.disp_pos = -1;
6621 }
6622 if (s == NULL && it->multibyte_p)
6623 {
6624 ptrdiff_t endpos = SCHARS (it->string);
6625 if (endpos > it->end_charpos)
6626 endpos = it->end_charpos;
6627 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6628 it->string);
6629 }
6630 CHECK_IT (it);
6631 }
6632
6633
6634 \f
6635 /***********************************************************************
6636 Iteration
6637 ***********************************************************************/
6638
6639 /* Map enum it_method value to corresponding next_element_from_* function. */
6640
6641 typedef bool (*next_element_function) (struct it *);
6642
6643 static next_element_function const get_next_element[NUM_IT_METHODS] =
6644 {
6645 next_element_from_buffer,
6646 next_element_from_display_vector,
6647 next_element_from_string,
6648 next_element_from_c_string,
6649 next_element_from_image,
6650 next_element_from_stretch
6651 };
6652
6653 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6654
6655
6656 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6657 (possibly with the following characters). */
6658
6659 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6660 ((IT)->cmp_it.id >= 0 \
6661 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6662 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6663 END_CHARPOS, (IT)->w, \
6664 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6665 (IT)->string)))
6666
6667
6668 /* Lookup the char-table Vglyphless_char_display for character C (-1
6669 if we want information for no-font case), and return the display
6670 method symbol. By side-effect, update it->what and
6671 it->glyphless_method. This function is called from
6672 get_next_display_element for each character element, and from
6673 x_produce_glyphs when no suitable font was found. */
6674
6675 Lisp_Object
6676 lookup_glyphless_char_display (int c, struct it *it)
6677 {
6678 Lisp_Object glyphless_method = Qnil;
6679
6680 if (CHAR_TABLE_P (Vglyphless_char_display)
6681 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6682 {
6683 if (c >= 0)
6684 {
6685 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6686 if (CONSP (glyphless_method))
6687 glyphless_method = FRAME_WINDOW_P (it->f)
6688 ? XCAR (glyphless_method)
6689 : XCDR (glyphless_method);
6690 }
6691 else
6692 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6693 }
6694
6695 retry:
6696 if (NILP (glyphless_method))
6697 {
6698 if (c >= 0)
6699 /* The default is to display the character by a proper font. */
6700 return Qnil;
6701 /* The default for the no-font case is to display an empty box. */
6702 glyphless_method = Qempty_box;
6703 }
6704 if (EQ (glyphless_method, Qzero_width))
6705 {
6706 if (c >= 0)
6707 return glyphless_method;
6708 /* This method can't be used for the no-font case. */
6709 glyphless_method = Qempty_box;
6710 }
6711 if (EQ (glyphless_method, Qthin_space))
6712 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6713 else if (EQ (glyphless_method, Qempty_box))
6714 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6715 else if (EQ (glyphless_method, Qhex_code))
6716 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6717 else if (STRINGP (glyphless_method))
6718 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6719 else
6720 {
6721 /* Invalid value. We use the default method. */
6722 glyphless_method = Qnil;
6723 goto retry;
6724 }
6725 it->what = IT_GLYPHLESS;
6726 return glyphless_method;
6727 }
6728
6729 /* Merge escape glyph face and cache the result. */
6730
6731 static struct frame *last_escape_glyph_frame = NULL;
6732 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6733 static int last_escape_glyph_merged_face_id = 0;
6734
6735 static int
6736 merge_escape_glyph_face (struct it *it)
6737 {
6738 int face_id;
6739
6740 if (it->f == last_escape_glyph_frame
6741 && it->face_id == last_escape_glyph_face_id)
6742 face_id = last_escape_glyph_merged_face_id;
6743 else
6744 {
6745 /* Merge the `escape-glyph' face into the current face. */
6746 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6747 last_escape_glyph_frame = it->f;
6748 last_escape_glyph_face_id = it->face_id;
6749 last_escape_glyph_merged_face_id = face_id;
6750 }
6751 return face_id;
6752 }
6753
6754 /* Likewise for glyphless glyph face. */
6755
6756 static struct frame *last_glyphless_glyph_frame = NULL;
6757 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6758 static int last_glyphless_glyph_merged_face_id = 0;
6759
6760 int
6761 merge_glyphless_glyph_face (struct it *it)
6762 {
6763 int face_id;
6764
6765 if (it->f == last_glyphless_glyph_frame
6766 && it->face_id == last_glyphless_glyph_face_id)
6767 face_id = last_glyphless_glyph_merged_face_id;
6768 else
6769 {
6770 /* Merge the `glyphless-char' face into the current face. */
6771 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6772 last_glyphless_glyph_frame = it->f;
6773 last_glyphless_glyph_face_id = it->face_id;
6774 last_glyphless_glyph_merged_face_id = face_id;
6775 }
6776 return face_id;
6777 }
6778
6779 /* Load IT's display element fields with information about the next
6780 display element from the current position of IT. Value is false if
6781 end of buffer (or C string) is reached. */
6782
6783 static bool
6784 get_next_display_element (struct it *it)
6785 {
6786 /* True means that we found a display element. False means that
6787 we hit the end of what we iterate over. Performance note: the
6788 function pointer `method' used here turns out to be faster than
6789 using a sequence of if-statements. */
6790 bool success_p;
6791
6792 get_next:
6793 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6794
6795 if (it->what == IT_CHARACTER)
6796 {
6797 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6798 and only if (a) the resolved directionality of that character
6799 is R..." */
6800 /* FIXME: Do we need an exception for characters from display
6801 tables? */
6802 if (it->bidi_p && it->bidi_it.type == STRONG_R
6803 && !inhibit_bidi_mirroring)
6804 it->c = bidi_mirror_char (it->c);
6805 /* Map via display table or translate control characters.
6806 IT->c, IT->len etc. have been set to the next character by
6807 the function call above. If we have a display table, and it
6808 contains an entry for IT->c, translate it. Don't do this if
6809 IT->c itself comes from a display table, otherwise we could
6810 end up in an infinite recursion. (An alternative could be to
6811 count the recursion depth of this function and signal an
6812 error when a certain maximum depth is reached.) Is it worth
6813 it? */
6814 if (success_p && it->dpvec == NULL)
6815 {
6816 Lisp_Object dv;
6817 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6818 bool nonascii_space_p = false;
6819 bool nonascii_hyphen_p = false;
6820 int c = it->c; /* This is the character to display. */
6821
6822 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6823 {
6824 eassert (SINGLE_BYTE_CHAR_P (c));
6825 if (unibyte_display_via_language_environment)
6826 {
6827 c = DECODE_CHAR (unibyte, c);
6828 if (c < 0)
6829 c = BYTE8_TO_CHAR (it->c);
6830 }
6831 else
6832 c = BYTE8_TO_CHAR (it->c);
6833 }
6834
6835 if (it->dp
6836 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6837 VECTORP (dv)))
6838 {
6839 struct Lisp_Vector *v = XVECTOR (dv);
6840
6841 /* Return the first character from the display table
6842 entry, if not empty. If empty, don't display the
6843 current character. */
6844 if (v->header.size)
6845 {
6846 it->dpvec_char_len = it->len;
6847 it->dpvec = v->contents;
6848 it->dpend = v->contents + v->header.size;
6849 it->current.dpvec_index = 0;
6850 it->dpvec_face_id = -1;
6851 it->saved_face_id = it->face_id;
6852 it->method = GET_FROM_DISPLAY_VECTOR;
6853 it->ellipsis_p = false;
6854 }
6855 else
6856 {
6857 set_iterator_to_next (it, false);
6858 }
6859 goto get_next;
6860 }
6861
6862 if (! NILP (lookup_glyphless_char_display (c, it)))
6863 {
6864 if (it->what == IT_GLYPHLESS)
6865 goto done;
6866 /* Don't display this character. */
6867 set_iterator_to_next (it, false);
6868 goto get_next;
6869 }
6870
6871 /* If `nobreak-char-display' is non-nil, we display
6872 non-ASCII spaces and hyphens specially. */
6873 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6874 {
6875 if (c == 0xA0)
6876 nonascii_space_p = true;
6877 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6878 nonascii_hyphen_p = true;
6879 }
6880
6881 /* Translate control characters into `\003' or `^C' form.
6882 Control characters coming from a display table entry are
6883 currently not translated because we use IT->dpvec to hold
6884 the translation. This could easily be changed but I
6885 don't believe that it is worth doing.
6886
6887 The characters handled by `nobreak-char-display' must be
6888 translated too.
6889
6890 Non-printable characters and raw-byte characters are also
6891 translated to octal form. */
6892 if (((c < ' ' || c == 127) /* ASCII control chars. */
6893 ? (it->area != TEXT_AREA
6894 /* In mode line, treat \n, \t like other crl chars. */
6895 || (c != '\t'
6896 && it->glyph_row
6897 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6898 || (c != '\n' && c != '\t'))
6899 : (nonascii_space_p
6900 || nonascii_hyphen_p
6901 || CHAR_BYTE8_P (c)
6902 || ! CHAR_PRINTABLE_P (c))))
6903 {
6904 /* C is a control character, non-ASCII space/hyphen,
6905 raw-byte, or a non-printable character which must be
6906 displayed either as '\003' or as `^C' where the '\\'
6907 and '^' can be defined in the display table. Fill
6908 IT->ctl_chars with glyphs for what we have to
6909 display. Then, set IT->dpvec to these glyphs. */
6910 Lisp_Object gc;
6911 int ctl_len;
6912 int face_id;
6913 int lface_id = 0;
6914 int escape_glyph;
6915
6916 /* Handle control characters with ^. */
6917
6918 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6919 {
6920 int g;
6921
6922 g = '^'; /* default glyph for Control */
6923 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6924 if (it->dp
6925 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6926 {
6927 g = GLYPH_CODE_CHAR (gc);
6928 lface_id = GLYPH_CODE_FACE (gc);
6929 }
6930
6931 face_id = (lface_id
6932 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6933 : merge_escape_glyph_face (it));
6934
6935 XSETINT (it->ctl_chars[0], g);
6936 XSETINT (it->ctl_chars[1], c ^ 0100);
6937 ctl_len = 2;
6938 goto display_control;
6939 }
6940
6941 /* Handle non-ascii space in the mode where it only gets
6942 highlighting. */
6943
6944 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6945 {
6946 /* Merge `nobreak-space' into the current face. */
6947 face_id = merge_faces (it->f, Qnobreak_space, 0,
6948 it->face_id);
6949 XSETINT (it->ctl_chars[0], ' ');
6950 ctl_len = 1;
6951 goto display_control;
6952 }
6953
6954 /* Handle sequences that start with the "escape glyph". */
6955
6956 /* the default escape glyph is \. */
6957 escape_glyph = '\\';
6958
6959 if (it->dp
6960 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6961 {
6962 escape_glyph = GLYPH_CODE_CHAR (gc);
6963 lface_id = GLYPH_CODE_FACE (gc);
6964 }
6965
6966 face_id = (lface_id
6967 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6968 : merge_escape_glyph_face (it));
6969
6970 /* Draw non-ASCII hyphen with just highlighting: */
6971
6972 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6973 {
6974 XSETINT (it->ctl_chars[0], '-');
6975 ctl_len = 1;
6976 goto display_control;
6977 }
6978
6979 /* Draw non-ASCII space/hyphen with escape glyph: */
6980
6981 if (nonascii_space_p || nonascii_hyphen_p)
6982 {
6983 XSETINT (it->ctl_chars[0], escape_glyph);
6984 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6985 ctl_len = 2;
6986 goto display_control;
6987 }
6988
6989 {
6990 char str[10];
6991 int len, i;
6992
6993 if (CHAR_BYTE8_P (c))
6994 /* Display \200 instead of \17777600. */
6995 c = CHAR_TO_BYTE8 (c);
6996 len = sprintf (str, "%03o", c + 0u);
6997
6998 XSETINT (it->ctl_chars[0], escape_glyph);
6999 for (i = 0; i < len; i++)
7000 XSETINT (it->ctl_chars[i + 1], str[i]);
7001 ctl_len = len + 1;
7002 }
7003
7004 display_control:
7005 /* Set up IT->dpvec and return first character from it. */
7006 it->dpvec_char_len = it->len;
7007 it->dpvec = it->ctl_chars;
7008 it->dpend = it->dpvec + ctl_len;
7009 it->current.dpvec_index = 0;
7010 it->dpvec_face_id = face_id;
7011 it->saved_face_id = it->face_id;
7012 it->method = GET_FROM_DISPLAY_VECTOR;
7013 it->ellipsis_p = false;
7014 goto get_next;
7015 }
7016 it->char_to_display = c;
7017 }
7018 else if (success_p)
7019 {
7020 it->char_to_display = it->c;
7021 }
7022 }
7023
7024 #ifdef HAVE_WINDOW_SYSTEM
7025 /* Adjust face id for a multibyte character. There are no multibyte
7026 character in unibyte text. */
7027 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7028 && it->multibyte_p
7029 && success_p
7030 && FRAME_WINDOW_P (it->f))
7031 {
7032 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7033
7034 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7035 {
7036 /* Automatic composition with glyph-string. */
7037 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7038
7039 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7040 }
7041 else
7042 {
7043 ptrdiff_t pos = (it->s ? -1
7044 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7045 : IT_CHARPOS (*it));
7046 int c;
7047
7048 if (it->what == IT_CHARACTER)
7049 c = it->char_to_display;
7050 else
7051 {
7052 struct composition *cmp = composition_table[it->cmp_it.id];
7053 int i;
7054
7055 c = ' ';
7056 for (i = 0; i < cmp->glyph_len; i++)
7057 /* TAB in a composition means display glyphs with
7058 padding space on the left or right. */
7059 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7060 break;
7061 }
7062 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7063 }
7064 }
7065 #endif /* HAVE_WINDOW_SYSTEM */
7066
7067 done:
7068 /* Is this character the last one of a run of characters with
7069 box? If yes, set IT->end_of_box_run_p to true. */
7070 if (it->face_box_p
7071 && it->s == NULL)
7072 {
7073 if (it->method == GET_FROM_STRING && it->sp)
7074 {
7075 int face_id = underlying_face_id (it);
7076 struct face *face = FACE_FROM_ID (it->f, face_id);
7077
7078 if (face)
7079 {
7080 if (face->box == FACE_NO_BOX)
7081 {
7082 /* If the box comes from face properties in a
7083 display string, check faces in that string. */
7084 int string_face_id = face_after_it_pos (it);
7085 it->end_of_box_run_p
7086 = (FACE_FROM_ID (it->f, string_face_id)->box
7087 == FACE_NO_BOX);
7088 }
7089 /* Otherwise, the box comes from the underlying face.
7090 If this is the last string character displayed, check
7091 the next buffer location. */
7092 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7093 /* n_overlay_strings is unreliable unless
7094 overlay_string_index is non-negative. */
7095 && ((it->current.overlay_string_index >= 0
7096 && (it->current.overlay_string_index
7097 == it->n_overlay_strings - 1))
7098 /* A string from display property. */
7099 || it->from_disp_prop_p))
7100 {
7101 ptrdiff_t ignore;
7102 int next_face_id;
7103 struct text_pos pos = it->current.pos;
7104
7105 /* For a string from a display property, the next
7106 buffer position is stored in the 'position'
7107 member of the iteration stack slot below the
7108 current one, see handle_single_display_spec. By
7109 contrast, it->current.pos was is not yet updated
7110 to point to that buffer position; that will
7111 happen in pop_it, after we finish displaying the
7112 current string. Note that we already checked
7113 above that it->sp is positive, so subtracting one
7114 from it is safe. */
7115 if (it->from_disp_prop_p)
7116 pos = (it->stack + it->sp - 1)->position;
7117 else
7118 INC_TEXT_POS (pos, it->multibyte_p);
7119
7120 if (CHARPOS (pos) >= ZV)
7121 it->end_of_box_run_p = true;
7122 else
7123 {
7124 next_face_id = face_at_buffer_position
7125 (it->w, CHARPOS (pos), &ignore,
7126 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7127 it->end_of_box_run_p
7128 = (FACE_FROM_ID (it->f, next_face_id)->box
7129 == FACE_NO_BOX);
7130 }
7131 }
7132 }
7133 }
7134 /* next_element_from_display_vector sets this flag according to
7135 faces of the display vector glyphs, see there. */
7136 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7137 {
7138 int face_id = face_after_it_pos (it);
7139 it->end_of_box_run_p
7140 = (face_id != it->face_id
7141 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7142 }
7143 }
7144 /* If we reached the end of the object we've been iterating (e.g., a
7145 display string or an overlay string), and there's something on
7146 IT->stack, proceed with what's on the stack. It doesn't make
7147 sense to return false if there's unprocessed stuff on the stack,
7148 because otherwise that stuff will never be displayed. */
7149 if (!success_p && it->sp > 0)
7150 {
7151 set_iterator_to_next (it, false);
7152 success_p = get_next_display_element (it);
7153 }
7154
7155 /* Value is false if end of buffer or string reached. */
7156 return success_p;
7157 }
7158
7159
7160 /* Move IT to the next display element.
7161
7162 RESEAT_P means if called on a newline in buffer text,
7163 skip to the next visible line start.
7164
7165 Functions get_next_display_element and set_iterator_to_next are
7166 separate because I find this arrangement easier to handle than a
7167 get_next_display_element function that also increments IT's
7168 position. The way it is we can first look at an iterator's current
7169 display element, decide whether it fits on a line, and if it does,
7170 increment the iterator position. The other way around we probably
7171 would either need a flag indicating whether the iterator has to be
7172 incremented the next time, or we would have to implement a
7173 decrement position function which would not be easy to write. */
7174
7175 void
7176 set_iterator_to_next (struct it *it, bool reseat_p)
7177 {
7178 /* Reset flags indicating start and end of a sequence of characters
7179 with box. Reset them at the start of this function because
7180 moving the iterator to a new position might set them. */
7181 it->start_of_box_run_p = it->end_of_box_run_p = false;
7182
7183 switch (it->method)
7184 {
7185 case GET_FROM_BUFFER:
7186 /* The current display element of IT is a character from
7187 current_buffer. Advance in the buffer, and maybe skip over
7188 invisible lines that are so because of selective display. */
7189 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7190 reseat_at_next_visible_line_start (it, false);
7191 else if (it->cmp_it.id >= 0)
7192 {
7193 /* We are currently getting glyphs from a composition. */
7194 if (! it->bidi_p)
7195 {
7196 IT_CHARPOS (*it) += it->cmp_it.nchars;
7197 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7198 }
7199 else
7200 {
7201 int i;
7202
7203 /* Update IT's char/byte positions to point to the first
7204 character of the next grapheme cluster, or to the
7205 character visually after the current composition. */
7206 for (i = 0; i < it->cmp_it.nchars; i++)
7207 bidi_move_to_visually_next (&it->bidi_it);
7208 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7209 IT_CHARPOS (*it) = it->bidi_it.charpos;
7210 }
7211
7212 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7213 && it->cmp_it.to < it->cmp_it.nglyphs)
7214 {
7215 /* Composition created while scanning forward. Proceed
7216 to the next grapheme cluster. */
7217 it->cmp_it.from = it->cmp_it.to;
7218 }
7219 else if ((it->bidi_p && it->cmp_it.reversed_p)
7220 && it->cmp_it.from > 0)
7221 {
7222 /* Composition created while scanning backward. Proceed
7223 to the previous grapheme cluster. */
7224 it->cmp_it.to = it->cmp_it.from;
7225 }
7226 else
7227 {
7228 /* No more grapheme clusters in this composition.
7229 Find the next stop position. */
7230 ptrdiff_t stop = it->end_charpos;
7231
7232 if (it->bidi_it.scan_dir < 0)
7233 /* Now we are scanning backward and don't know
7234 where to stop. */
7235 stop = -1;
7236 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7237 IT_BYTEPOS (*it), stop, Qnil);
7238 }
7239 }
7240 else
7241 {
7242 eassert (it->len != 0);
7243
7244 if (!it->bidi_p)
7245 {
7246 IT_BYTEPOS (*it) += it->len;
7247 IT_CHARPOS (*it) += 1;
7248 }
7249 else
7250 {
7251 int prev_scan_dir = it->bidi_it.scan_dir;
7252 /* If this is a new paragraph, determine its base
7253 direction (a.k.a. its base embedding level). */
7254 if (it->bidi_it.new_paragraph)
7255 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7256 false);
7257 bidi_move_to_visually_next (&it->bidi_it);
7258 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7259 IT_CHARPOS (*it) = it->bidi_it.charpos;
7260 if (prev_scan_dir != it->bidi_it.scan_dir)
7261 {
7262 /* As the scan direction was changed, we must
7263 re-compute the stop position for composition. */
7264 ptrdiff_t stop = it->end_charpos;
7265 if (it->bidi_it.scan_dir < 0)
7266 stop = -1;
7267 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7268 IT_BYTEPOS (*it), stop, Qnil);
7269 }
7270 }
7271 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7272 }
7273 break;
7274
7275 case GET_FROM_C_STRING:
7276 /* Current display element of IT is from a C string. */
7277 if (!it->bidi_p
7278 /* If the string position is beyond string's end, it means
7279 next_element_from_c_string is padding the string with
7280 blanks, in which case we bypass the bidi iterator,
7281 because it cannot deal with such virtual characters. */
7282 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7283 {
7284 IT_BYTEPOS (*it) += it->len;
7285 IT_CHARPOS (*it) += 1;
7286 }
7287 else
7288 {
7289 bidi_move_to_visually_next (&it->bidi_it);
7290 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7291 IT_CHARPOS (*it) = it->bidi_it.charpos;
7292 }
7293 break;
7294
7295 case GET_FROM_DISPLAY_VECTOR:
7296 /* Current display element of IT is from a display table entry.
7297 Advance in the display table definition. Reset it to null if
7298 end reached, and continue with characters from buffers/
7299 strings. */
7300 ++it->current.dpvec_index;
7301
7302 /* Restore face of the iterator to what they were before the
7303 display vector entry (these entries may contain faces). */
7304 it->face_id = it->saved_face_id;
7305
7306 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7307 {
7308 bool recheck_faces = it->ellipsis_p;
7309
7310 if (it->s)
7311 it->method = GET_FROM_C_STRING;
7312 else if (STRINGP (it->string))
7313 it->method = GET_FROM_STRING;
7314 else
7315 {
7316 it->method = GET_FROM_BUFFER;
7317 it->object = it->w->contents;
7318 }
7319
7320 it->dpvec = NULL;
7321 it->current.dpvec_index = -1;
7322
7323 /* Skip over characters which were displayed via IT->dpvec. */
7324 if (it->dpvec_char_len < 0)
7325 reseat_at_next_visible_line_start (it, true);
7326 else if (it->dpvec_char_len > 0)
7327 {
7328 it->len = it->dpvec_char_len;
7329 set_iterator_to_next (it, reseat_p);
7330 }
7331
7332 /* Maybe recheck faces after display vector. */
7333 if (recheck_faces)
7334 {
7335 if (it->method == GET_FROM_STRING)
7336 it->stop_charpos = IT_STRING_CHARPOS (*it);
7337 else
7338 it->stop_charpos = IT_CHARPOS (*it);
7339 }
7340 }
7341 break;
7342
7343 case GET_FROM_STRING:
7344 /* Current display element is a character from a Lisp string. */
7345 eassert (it->s == NULL && STRINGP (it->string));
7346 /* Don't advance past string end. These conditions are true
7347 when set_iterator_to_next is called at the end of
7348 get_next_display_element, in which case the Lisp string is
7349 already exhausted, and all we want is pop the iterator
7350 stack. */
7351 if (it->current.overlay_string_index >= 0)
7352 {
7353 /* This is an overlay string, so there's no padding with
7354 spaces, and the number of characters in the string is
7355 where the string ends. */
7356 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7357 goto consider_string_end;
7358 }
7359 else
7360 {
7361 /* Not an overlay string. There could be padding, so test
7362 against it->end_charpos. */
7363 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7364 goto consider_string_end;
7365 }
7366 if (it->cmp_it.id >= 0)
7367 {
7368 /* We are delivering display elements from a composition.
7369 Update the string position past the grapheme cluster
7370 we've just processed. */
7371 if (! it->bidi_p)
7372 {
7373 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7374 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7375 }
7376 else
7377 {
7378 int i;
7379
7380 for (i = 0; i < it->cmp_it.nchars; i++)
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 }
7385
7386 /* Did we exhaust all the grapheme clusters of this
7387 composition? */
7388 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7389 && (it->cmp_it.to < it->cmp_it.nglyphs))
7390 {
7391 /* Not all the grapheme clusters were processed yet;
7392 advance to the next cluster. */
7393 it->cmp_it.from = it->cmp_it.to;
7394 }
7395 else if ((it->bidi_p && it->cmp_it.reversed_p)
7396 && it->cmp_it.from > 0)
7397 {
7398 /* Likewise: advance to the next cluster, but going in
7399 the reverse direction. */
7400 it->cmp_it.to = it->cmp_it.from;
7401 }
7402 else
7403 {
7404 /* This composition was fully processed; find the next
7405 candidate place for checking for composed
7406 characters. */
7407 /* Always limit string searches to the string length;
7408 any padding spaces are not part of the string, and
7409 there cannot be any compositions in that padding. */
7410 ptrdiff_t stop = SCHARS (it->string);
7411
7412 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7413 stop = -1;
7414 else if (it->end_charpos < stop)
7415 {
7416 /* Cf. PRECISION in reseat_to_string: we might be
7417 limited in how many of the string characters we
7418 need to deliver. */
7419 stop = it->end_charpos;
7420 }
7421 composition_compute_stop_pos (&it->cmp_it,
7422 IT_STRING_CHARPOS (*it),
7423 IT_STRING_BYTEPOS (*it), stop,
7424 it->string);
7425 }
7426 }
7427 else
7428 {
7429 if (!it->bidi_p
7430 /* If the string position is beyond string's end, it
7431 means next_element_from_string is padding the string
7432 with blanks, in which case we bypass the bidi
7433 iterator, because it cannot deal with such virtual
7434 characters. */
7435 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7436 {
7437 IT_STRING_BYTEPOS (*it) += it->len;
7438 IT_STRING_CHARPOS (*it) += 1;
7439 }
7440 else
7441 {
7442 int prev_scan_dir = it->bidi_it.scan_dir;
7443
7444 bidi_move_to_visually_next (&it->bidi_it);
7445 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7446 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7447 /* If the scan direction changes, we may need to update
7448 the place where to check for composed characters. */
7449 if (prev_scan_dir != it->bidi_it.scan_dir)
7450 {
7451 ptrdiff_t stop = SCHARS (it->string);
7452
7453 if (it->bidi_it.scan_dir < 0)
7454 stop = -1;
7455 else if (it->end_charpos < stop)
7456 stop = it->end_charpos;
7457
7458 composition_compute_stop_pos (&it->cmp_it,
7459 IT_STRING_CHARPOS (*it),
7460 IT_STRING_BYTEPOS (*it), stop,
7461 it->string);
7462 }
7463 }
7464 }
7465
7466 consider_string_end:
7467
7468 if (it->current.overlay_string_index >= 0)
7469 {
7470 /* IT->string is an overlay string. Advance to the
7471 next, if there is one. */
7472 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7473 {
7474 it->ellipsis_p = false;
7475 next_overlay_string (it);
7476 if (it->ellipsis_p)
7477 setup_for_ellipsis (it, 0);
7478 }
7479 }
7480 else
7481 {
7482 /* IT->string is not an overlay string. If we reached
7483 its end, and there is something on IT->stack, proceed
7484 with what is on the stack. This can be either another
7485 string, this time an overlay string, or a buffer. */
7486 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7487 && it->sp > 0)
7488 {
7489 pop_it (it);
7490 if (it->method == GET_FROM_STRING)
7491 goto consider_string_end;
7492 }
7493 }
7494 break;
7495
7496 case GET_FROM_IMAGE:
7497 case GET_FROM_STRETCH:
7498 /* The position etc with which we have to proceed are on
7499 the stack. The position may be at the end of a string,
7500 if the `display' property takes up the whole string. */
7501 eassert (it->sp > 0);
7502 pop_it (it);
7503 if (it->method == GET_FROM_STRING)
7504 goto consider_string_end;
7505 break;
7506
7507 default:
7508 /* There are no other methods defined, so this should be a bug. */
7509 emacs_abort ();
7510 }
7511
7512 eassert (it->method != GET_FROM_STRING
7513 || (STRINGP (it->string)
7514 && IT_STRING_CHARPOS (*it) >= 0));
7515 }
7516
7517 /* Load IT's display element fields with information about the next
7518 display element which comes from a display table entry or from the
7519 result of translating a control character to one of the forms `^C'
7520 or `\003'.
7521
7522 IT->dpvec holds the glyphs to return as characters.
7523 IT->saved_face_id holds the face id before the display vector--it
7524 is restored into IT->face_id in set_iterator_to_next. */
7525
7526 static bool
7527 next_element_from_display_vector (struct it *it)
7528 {
7529 Lisp_Object gc;
7530 int prev_face_id = it->face_id;
7531 int next_face_id;
7532
7533 /* Precondition. */
7534 eassert (it->dpvec && it->current.dpvec_index >= 0);
7535
7536 it->face_id = it->saved_face_id;
7537
7538 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7539 That seemed totally bogus - so I changed it... */
7540 gc = it->dpvec[it->current.dpvec_index];
7541
7542 if (GLYPH_CODE_P (gc))
7543 {
7544 struct face *this_face, *prev_face, *next_face;
7545
7546 it->c = GLYPH_CODE_CHAR (gc);
7547 it->len = CHAR_BYTES (it->c);
7548
7549 /* The entry may contain a face id to use. Such a face id is
7550 the id of a Lisp face, not a realized face. A face id of
7551 zero means no face is specified. */
7552 if (it->dpvec_face_id >= 0)
7553 it->face_id = it->dpvec_face_id;
7554 else
7555 {
7556 int lface_id = GLYPH_CODE_FACE (gc);
7557 if (lface_id > 0)
7558 it->face_id = merge_faces (it->f, Qt, lface_id,
7559 it->saved_face_id);
7560 }
7561
7562 /* Glyphs in the display vector could have the box face, so we
7563 need to set the related flags in the iterator, as
7564 appropriate. */
7565 this_face = FACE_FROM_ID (it->f, it->face_id);
7566 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7567
7568 /* Is this character the first character of a box-face run? */
7569 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7570 && (!prev_face
7571 || prev_face->box == FACE_NO_BOX));
7572
7573 /* For the last character of the box-face run, we need to look
7574 either at the next glyph from the display vector, or at the
7575 face we saw before the display vector. */
7576 next_face_id = it->saved_face_id;
7577 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7578 {
7579 if (it->dpvec_face_id >= 0)
7580 next_face_id = it->dpvec_face_id;
7581 else
7582 {
7583 int lface_id =
7584 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7585
7586 if (lface_id > 0)
7587 next_face_id = merge_faces (it->f, Qt, lface_id,
7588 it->saved_face_id);
7589 }
7590 }
7591 next_face = FACE_FROM_ID (it->f, next_face_id);
7592 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7593 && (!next_face
7594 || next_face->box == FACE_NO_BOX));
7595 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7596 }
7597 else
7598 /* Display table entry is invalid. Return a space. */
7599 it->c = ' ', it->len = 1;
7600
7601 /* Don't change position and object of the iterator here. They are
7602 still the values of the character that had this display table
7603 entry or was translated, and that's what we want. */
7604 it->what = IT_CHARACTER;
7605 return true;
7606 }
7607
7608 /* Get the first element of string/buffer in the visual order, after
7609 being reseated to a new position in a string or a buffer. */
7610 static void
7611 get_visually_first_element (struct it *it)
7612 {
7613 bool string_p = STRINGP (it->string) || it->s;
7614 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7615 ptrdiff_t bob = (string_p ? 0 : BEGV);
7616
7617 if (STRINGP (it->string))
7618 {
7619 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7620 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7621 }
7622 else
7623 {
7624 it->bidi_it.charpos = IT_CHARPOS (*it);
7625 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7626 }
7627
7628 if (it->bidi_it.charpos == eob)
7629 {
7630 /* Nothing to do, but reset the FIRST_ELT flag, like
7631 bidi_paragraph_init does, because we are not going to
7632 call it. */
7633 it->bidi_it.first_elt = false;
7634 }
7635 else if (it->bidi_it.charpos == bob
7636 || (!string_p
7637 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7638 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7639 {
7640 /* If we are at the beginning of a line/string, we can produce
7641 the next element right away. */
7642 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7643 bidi_move_to_visually_next (&it->bidi_it);
7644 }
7645 else
7646 {
7647 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7648
7649 /* We need to prime the bidi iterator starting at the line's or
7650 string's beginning, before we will be able to produce the
7651 next element. */
7652 if (string_p)
7653 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7654 else
7655 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7656 IT_BYTEPOS (*it), -1,
7657 &it->bidi_it.bytepos);
7658 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7659 do
7660 {
7661 /* Now return to buffer/string position where we were asked
7662 to get the next display element, and produce that. */
7663 bidi_move_to_visually_next (&it->bidi_it);
7664 }
7665 while (it->bidi_it.bytepos != orig_bytepos
7666 && it->bidi_it.charpos < eob);
7667 }
7668
7669 /* Adjust IT's position information to where we ended up. */
7670 if (STRINGP (it->string))
7671 {
7672 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7673 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7674 }
7675 else
7676 {
7677 IT_CHARPOS (*it) = it->bidi_it.charpos;
7678 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7679 }
7680
7681 if (STRINGP (it->string) || !it->s)
7682 {
7683 ptrdiff_t stop, charpos, bytepos;
7684
7685 if (STRINGP (it->string))
7686 {
7687 eassert (!it->s);
7688 stop = SCHARS (it->string);
7689 if (stop > it->end_charpos)
7690 stop = it->end_charpos;
7691 charpos = IT_STRING_CHARPOS (*it);
7692 bytepos = IT_STRING_BYTEPOS (*it);
7693 }
7694 else
7695 {
7696 stop = it->end_charpos;
7697 charpos = IT_CHARPOS (*it);
7698 bytepos = IT_BYTEPOS (*it);
7699 }
7700 if (it->bidi_it.scan_dir < 0)
7701 stop = -1;
7702 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7703 it->string);
7704 }
7705 }
7706
7707 /* Load IT with the next display element from Lisp string IT->string.
7708 IT->current.string_pos is the current position within the string.
7709 If IT->current.overlay_string_index >= 0, the Lisp string is an
7710 overlay string. */
7711
7712 static bool
7713 next_element_from_string (struct it *it)
7714 {
7715 struct text_pos position;
7716
7717 eassert (STRINGP (it->string));
7718 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7719 eassert (IT_STRING_CHARPOS (*it) >= 0);
7720 position = it->current.string_pos;
7721
7722 /* With bidi reordering, the character to display might not be the
7723 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7724 that we were reseat()ed to a new string, whose paragraph
7725 direction is not known. */
7726 if (it->bidi_p && it->bidi_it.first_elt)
7727 {
7728 get_visually_first_element (it);
7729 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7730 }
7731
7732 /* Time to check for invisible text? */
7733 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7734 {
7735 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7736 {
7737 if (!(!it->bidi_p
7738 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7739 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7740 {
7741 /* With bidi non-linear iteration, we could find
7742 ourselves far beyond the last computed stop_charpos,
7743 with several other stop positions in between that we
7744 missed. Scan them all now, in buffer's logical
7745 order, until we find and handle the last stop_charpos
7746 that precedes our current position. */
7747 handle_stop_backwards (it, it->stop_charpos);
7748 return GET_NEXT_DISPLAY_ELEMENT (it);
7749 }
7750 else
7751 {
7752 if (it->bidi_p)
7753 {
7754 /* Take note of the stop position we just moved
7755 across, for when we will move back across it. */
7756 it->prev_stop = it->stop_charpos;
7757 /* If we are at base paragraph embedding level, take
7758 note of the last stop position seen at this
7759 level. */
7760 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7761 it->base_level_stop = it->stop_charpos;
7762 }
7763 handle_stop (it);
7764
7765 /* Since a handler may have changed IT->method, we must
7766 recurse here. */
7767 return GET_NEXT_DISPLAY_ELEMENT (it);
7768 }
7769 }
7770 else if (it->bidi_p
7771 /* If we are before prev_stop, we may have overstepped
7772 on our way backwards a stop_pos, and if so, we need
7773 to handle that stop_pos. */
7774 && IT_STRING_CHARPOS (*it) < it->prev_stop
7775 /* We can sometimes back up for reasons that have nothing
7776 to do with bidi reordering. E.g., compositions. The
7777 code below is only needed when we are above the base
7778 embedding level, so test for that explicitly. */
7779 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7780 {
7781 /* If we lost track of base_level_stop, we have no better
7782 place for handle_stop_backwards to start from than string
7783 beginning. This happens, e.g., when we were reseated to
7784 the previous screenful of text by vertical-motion. */
7785 if (it->base_level_stop <= 0
7786 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7787 it->base_level_stop = 0;
7788 handle_stop_backwards (it, it->base_level_stop);
7789 return GET_NEXT_DISPLAY_ELEMENT (it);
7790 }
7791 }
7792
7793 if (it->current.overlay_string_index >= 0)
7794 {
7795 /* Get the next character from an overlay string. In overlay
7796 strings, there is no field width or padding with spaces to
7797 do. */
7798 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7799 {
7800 it->what = IT_EOB;
7801 return false;
7802 }
7803 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7804 IT_STRING_BYTEPOS (*it),
7805 it->bidi_it.scan_dir < 0
7806 ? -1
7807 : SCHARS (it->string))
7808 && next_element_from_composition (it))
7809 {
7810 return true;
7811 }
7812 else if (STRING_MULTIBYTE (it->string))
7813 {
7814 const unsigned char *s = (SDATA (it->string)
7815 + IT_STRING_BYTEPOS (*it));
7816 it->c = string_char_and_length (s, &it->len);
7817 }
7818 else
7819 {
7820 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7821 it->len = 1;
7822 }
7823 }
7824 else
7825 {
7826 /* Get the next character from a Lisp string that is not an
7827 overlay string. Such strings come from the mode line, for
7828 example. We may have to pad with spaces, or truncate the
7829 string. See also next_element_from_c_string. */
7830 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7831 {
7832 it->what = IT_EOB;
7833 return false;
7834 }
7835 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7836 {
7837 /* Pad with spaces. */
7838 it->c = ' ', it->len = 1;
7839 CHARPOS (position) = BYTEPOS (position) = -1;
7840 }
7841 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7842 IT_STRING_BYTEPOS (*it),
7843 it->bidi_it.scan_dir < 0
7844 ? -1
7845 : it->string_nchars)
7846 && next_element_from_composition (it))
7847 {
7848 return true;
7849 }
7850 else if (STRING_MULTIBYTE (it->string))
7851 {
7852 const unsigned char *s = (SDATA (it->string)
7853 + IT_STRING_BYTEPOS (*it));
7854 it->c = string_char_and_length (s, &it->len);
7855 }
7856 else
7857 {
7858 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7859 it->len = 1;
7860 }
7861 }
7862
7863 /* Record what we have and where it came from. */
7864 it->what = IT_CHARACTER;
7865 it->object = it->string;
7866 it->position = position;
7867 return true;
7868 }
7869
7870
7871 /* Load IT with next display element from C string IT->s.
7872 IT->string_nchars is the maximum number of characters to return
7873 from the string. IT->end_charpos may be greater than
7874 IT->string_nchars when this function is called, in which case we
7875 may have to return padding spaces. Value is false if end of string
7876 reached, including padding spaces. */
7877
7878 static bool
7879 next_element_from_c_string (struct it *it)
7880 {
7881 bool success_p = true;
7882
7883 eassert (it->s);
7884 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7885 it->what = IT_CHARACTER;
7886 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7887 it->object = make_number (0);
7888
7889 /* With bidi reordering, the character to display might not be the
7890 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7891 we were reseated to a new string, whose paragraph direction is
7892 not known. */
7893 if (it->bidi_p && it->bidi_it.first_elt)
7894 get_visually_first_element (it);
7895
7896 /* IT's position can be greater than IT->string_nchars in case a
7897 field width or precision has been specified when the iterator was
7898 initialized. */
7899 if (IT_CHARPOS (*it) >= it->end_charpos)
7900 {
7901 /* End of the game. */
7902 it->what = IT_EOB;
7903 success_p = false;
7904 }
7905 else if (IT_CHARPOS (*it) >= it->string_nchars)
7906 {
7907 /* Pad with spaces. */
7908 it->c = ' ', it->len = 1;
7909 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7910 }
7911 else if (it->multibyte_p)
7912 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7913 else
7914 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7915
7916 return success_p;
7917 }
7918
7919
7920 /* Set up IT to return characters from an ellipsis, if appropriate.
7921 The definition of the ellipsis glyphs may come from a display table
7922 entry. This function fills IT with the first glyph from the
7923 ellipsis if an ellipsis is to be displayed. */
7924
7925 static bool
7926 next_element_from_ellipsis (struct it *it)
7927 {
7928 if (it->selective_display_ellipsis_p)
7929 setup_for_ellipsis (it, it->len);
7930 else
7931 {
7932 /* The face at the current position may be different from the
7933 face we find after the invisible text. Remember what it
7934 was in IT->saved_face_id, and signal that it's there by
7935 setting face_before_selective_p. */
7936 it->saved_face_id = it->face_id;
7937 it->method = GET_FROM_BUFFER;
7938 it->object = it->w->contents;
7939 reseat_at_next_visible_line_start (it, true);
7940 it->face_before_selective_p = true;
7941 }
7942
7943 return GET_NEXT_DISPLAY_ELEMENT (it);
7944 }
7945
7946
7947 /* Deliver an image display element. The iterator IT is already
7948 filled with image information (done in handle_display_prop). Value
7949 is always true. */
7950
7951
7952 static bool
7953 next_element_from_image (struct it *it)
7954 {
7955 it->what = IT_IMAGE;
7956 return true;
7957 }
7958
7959
7960 /* Fill iterator IT with next display element from a stretch glyph
7961 property. IT->object is the value of the text property. Value is
7962 always true. */
7963
7964 static bool
7965 next_element_from_stretch (struct it *it)
7966 {
7967 it->what = IT_STRETCH;
7968 return true;
7969 }
7970
7971 /* Scan backwards from IT's current position until we find a stop
7972 position, or until BEGV. This is called when we find ourself
7973 before both the last known prev_stop and base_level_stop while
7974 reordering bidirectional text. */
7975
7976 static void
7977 compute_stop_pos_backwards (struct it *it)
7978 {
7979 const int SCAN_BACK_LIMIT = 1000;
7980 struct text_pos pos;
7981 struct display_pos save_current = it->current;
7982 struct text_pos save_position = it->position;
7983 ptrdiff_t charpos = IT_CHARPOS (*it);
7984 ptrdiff_t where_we_are = charpos;
7985 ptrdiff_t save_stop_pos = it->stop_charpos;
7986 ptrdiff_t save_end_pos = it->end_charpos;
7987
7988 eassert (NILP (it->string) && !it->s);
7989 eassert (it->bidi_p);
7990 it->bidi_p = false;
7991 do
7992 {
7993 it->end_charpos = min (charpos + 1, ZV);
7994 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7995 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7996 reseat_1 (it, pos, false);
7997 compute_stop_pos (it);
7998 /* We must advance forward, right? */
7999 if (it->stop_charpos <= charpos)
8000 emacs_abort ();
8001 }
8002 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8003
8004 if (it->stop_charpos <= where_we_are)
8005 it->prev_stop = it->stop_charpos;
8006 else
8007 it->prev_stop = BEGV;
8008 it->bidi_p = true;
8009 it->current = save_current;
8010 it->position = save_position;
8011 it->stop_charpos = save_stop_pos;
8012 it->end_charpos = save_end_pos;
8013 }
8014
8015 /* Scan forward from CHARPOS in the current buffer/string, until we
8016 find a stop position > current IT's position. Then handle the stop
8017 position before that. This is called when we bump into a stop
8018 position while reordering bidirectional text. CHARPOS should be
8019 the last previously processed stop_pos (or BEGV/0, if none were
8020 processed yet) whose position is less that IT's current
8021 position. */
8022
8023 static void
8024 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8025 {
8026 bool bufp = !STRINGP (it->string);
8027 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8028 struct display_pos save_current = it->current;
8029 struct text_pos save_position = it->position;
8030 struct text_pos pos1;
8031 ptrdiff_t next_stop;
8032
8033 /* Scan in strict logical order. */
8034 eassert (it->bidi_p);
8035 it->bidi_p = false;
8036 do
8037 {
8038 it->prev_stop = charpos;
8039 if (bufp)
8040 {
8041 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8042 reseat_1 (it, pos1, false);
8043 }
8044 else
8045 it->current.string_pos = string_pos (charpos, it->string);
8046 compute_stop_pos (it);
8047 /* We must advance forward, right? */
8048 if (it->stop_charpos <= it->prev_stop)
8049 emacs_abort ();
8050 charpos = it->stop_charpos;
8051 }
8052 while (charpos <= where_we_are);
8053
8054 it->bidi_p = true;
8055 it->current = save_current;
8056 it->position = save_position;
8057 next_stop = it->stop_charpos;
8058 it->stop_charpos = it->prev_stop;
8059 handle_stop (it);
8060 it->stop_charpos = next_stop;
8061 }
8062
8063 /* Load IT with the next display element from current_buffer. Value
8064 is false if end of buffer reached. IT->stop_charpos is the next
8065 position at which to stop and check for text properties or buffer
8066 end. */
8067
8068 static bool
8069 next_element_from_buffer (struct it *it)
8070 {
8071 bool success_p = true;
8072
8073 eassert (IT_CHARPOS (*it) >= BEGV);
8074 eassert (NILP (it->string) && !it->s);
8075 eassert (!it->bidi_p
8076 || (EQ (it->bidi_it.string.lstring, Qnil)
8077 && it->bidi_it.string.s == NULL));
8078
8079 /* With bidi reordering, the character to display might not be the
8080 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8081 we were reseat()ed to a new buffer position, which is potentially
8082 a different paragraph. */
8083 if (it->bidi_p && it->bidi_it.first_elt)
8084 {
8085 get_visually_first_element (it);
8086 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8087 }
8088
8089 if (IT_CHARPOS (*it) >= it->stop_charpos)
8090 {
8091 if (IT_CHARPOS (*it) >= it->end_charpos)
8092 {
8093 bool overlay_strings_follow_p;
8094
8095 /* End of the game, except when overlay strings follow that
8096 haven't been returned yet. */
8097 if (it->overlay_strings_at_end_processed_p)
8098 overlay_strings_follow_p = false;
8099 else
8100 {
8101 it->overlay_strings_at_end_processed_p = true;
8102 overlay_strings_follow_p = get_overlay_strings (it, 0);
8103 }
8104
8105 if (overlay_strings_follow_p)
8106 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8107 else
8108 {
8109 it->what = IT_EOB;
8110 it->position = it->current.pos;
8111 success_p = false;
8112 }
8113 }
8114 else if (!(!it->bidi_p
8115 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8116 || IT_CHARPOS (*it) == it->stop_charpos))
8117 {
8118 /* With bidi non-linear iteration, we could find ourselves
8119 far beyond the last computed stop_charpos, with several
8120 other stop positions in between that we missed. Scan
8121 them all now, in buffer's logical order, until we find
8122 and handle the last stop_charpos that precedes our
8123 current position. */
8124 handle_stop_backwards (it, it->stop_charpos);
8125 it->ignore_overlay_strings_at_pos_p = false;
8126 return GET_NEXT_DISPLAY_ELEMENT (it);
8127 }
8128 else
8129 {
8130 if (it->bidi_p)
8131 {
8132 /* Take note of the stop position we just moved across,
8133 for when we will move back across it. */
8134 it->prev_stop = it->stop_charpos;
8135 /* If we are at base paragraph embedding level, take
8136 note of the last stop position seen at this
8137 level. */
8138 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8139 it->base_level_stop = it->stop_charpos;
8140 }
8141 handle_stop (it);
8142 it->ignore_overlay_strings_at_pos_p = false;
8143 return GET_NEXT_DISPLAY_ELEMENT (it);
8144 }
8145 }
8146 else if (it->bidi_p
8147 /* If we are before prev_stop, we may have overstepped on
8148 our way backwards a stop_pos, and if so, we need to
8149 handle that stop_pos. */
8150 && IT_CHARPOS (*it) < it->prev_stop
8151 /* We can sometimes back up for reasons that have nothing
8152 to do with bidi reordering. E.g., compositions. The
8153 code below is only needed when we are above the base
8154 embedding level, so test for that explicitly. */
8155 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8156 {
8157 if (it->base_level_stop <= 0
8158 || IT_CHARPOS (*it) < it->base_level_stop)
8159 {
8160 /* If we lost track of base_level_stop, we need to find
8161 prev_stop by looking backwards. This happens, e.g., when
8162 we were reseated to the previous screenful of text by
8163 vertical-motion. */
8164 it->base_level_stop = BEGV;
8165 compute_stop_pos_backwards (it);
8166 handle_stop_backwards (it, it->prev_stop);
8167 }
8168 else
8169 handle_stop_backwards (it, it->base_level_stop);
8170 it->ignore_overlay_strings_at_pos_p = false;
8171 return GET_NEXT_DISPLAY_ELEMENT (it);
8172 }
8173 else
8174 {
8175 /* No face changes, overlays etc. in sight, so just return a
8176 character from current_buffer. */
8177 unsigned char *p;
8178 ptrdiff_t stop;
8179
8180 /* We moved to the next buffer position, so any info about
8181 previously seen overlays is no longer valid. */
8182 it->ignore_overlay_strings_at_pos_p = false;
8183
8184 /* Maybe run the redisplay end trigger hook. Performance note:
8185 This doesn't seem to cost measurable time. */
8186 if (it->redisplay_end_trigger_charpos
8187 && it->glyph_row
8188 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8189 run_redisplay_end_trigger_hook (it);
8190
8191 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8192 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8193 stop)
8194 && next_element_from_composition (it))
8195 {
8196 return true;
8197 }
8198
8199 /* Get the next character, maybe multibyte. */
8200 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8201 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8202 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8203 else
8204 it->c = *p, it->len = 1;
8205
8206 /* Record what we have and where it came from. */
8207 it->what = IT_CHARACTER;
8208 it->object = it->w->contents;
8209 it->position = it->current.pos;
8210
8211 /* Normally we return the character found above, except when we
8212 really want to return an ellipsis for selective display. */
8213 if (it->selective)
8214 {
8215 if (it->c == '\n')
8216 {
8217 /* A value of selective > 0 means hide lines indented more
8218 than that number of columns. */
8219 if (it->selective > 0
8220 && IT_CHARPOS (*it) + 1 < ZV
8221 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8222 IT_BYTEPOS (*it) + 1,
8223 it->selective))
8224 {
8225 success_p = next_element_from_ellipsis (it);
8226 it->dpvec_char_len = -1;
8227 }
8228 }
8229 else if (it->c == '\r' && it->selective == -1)
8230 {
8231 /* A value of selective == -1 means that everything from the
8232 CR to the end of the line is invisible, with maybe an
8233 ellipsis displayed for it. */
8234 success_p = next_element_from_ellipsis (it);
8235 it->dpvec_char_len = -1;
8236 }
8237 }
8238 }
8239
8240 /* Value is false if end of buffer reached. */
8241 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8242 return success_p;
8243 }
8244
8245
8246 /* Run the redisplay end trigger hook for IT. */
8247
8248 static void
8249 run_redisplay_end_trigger_hook (struct it *it)
8250 {
8251 /* IT->glyph_row should be non-null, i.e. we should be actually
8252 displaying something, or otherwise we should not run the hook. */
8253 eassert (it->glyph_row);
8254
8255 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8256 it->redisplay_end_trigger_charpos = 0;
8257
8258 /* Since we are *trying* to run these functions, don't try to run
8259 them again, even if they get an error. */
8260 wset_redisplay_end_trigger (it->w, Qnil);
8261 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8262 make_number (charpos));
8263
8264 /* Notice if it changed the face of the character we are on. */
8265 handle_face_prop (it);
8266 }
8267
8268
8269 /* Deliver a composition display element. Unlike the other
8270 next_element_from_XXX, this function is not registered in the array
8271 get_next_element[]. It is called from next_element_from_buffer and
8272 next_element_from_string when necessary. */
8273
8274 static bool
8275 next_element_from_composition (struct it *it)
8276 {
8277 it->what = IT_COMPOSITION;
8278 it->len = it->cmp_it.nbytes;
8279 if (STRINGP (it->string))
8280 {
8281 if (it->c < 0)
8282 {
8283 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8284 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8285 return false;
8286 }
8287 it->position = it->current.string_pos;
8288 it->object = it->string;
8289 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8290 IT_STRING_BYTEPOS (*it), it->string);
8291 }
8292 else
8293 {
8294 if (it->c < 0)
8295 {
8296 IT_CHARPOS (*it) += it->cmp_it.nchars;
8297 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8298 if (it->bidi_p)
8299 {
8300 if (it->bidi_it.new_paragraph)
8301 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8302 false);
8303 /* Resync the bidi iterator with IT's new position.
8304 FIXME: this doesn't support bidirectional text. */
8305 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8306 bidi_move_to_visually_next (&it->bidi_it);
8307 }
8308 return false;
8309 }
8310 it->position = it->current.pos;
8311 it->object = it->w->contents;
8312 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8313 IT_BYTEPOS (*it), Qnil);
8314 }
8315 return true;
8316 }
8317
8318
8319 \f
8320 /***********************************************************************
8321 Moving an iterator without producing glyphs
8322 ***********************************************************************/
8323
8324 /* Check if iterator is at a position corresponding to a valid buffer
8325 position after some move_it_ call. */
8326
8327 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8328 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8329
8330
8331 /* Move iterator IT to a specified buffer or X position within one
8332 line on the display without producing glyphs.
8333
8334 OP should be a bit mask including some or all of these bits:
8335 MOVE_TO_X: Stop upon reaching x-position TO_X.
8336 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8337 Regardless of OP's value, stop upon reaching the end of the display line.
8338
8339 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8340 This means, in particular, that TO_X includes window's horizontal
8341 scroll amount.
8342
8343 The return value has several possible values that
8344 say what condition caused the scan to stop:
8345
8346 MOVE_POS_MATCH_OR_ZV
8347 - when TO_POS or ZV was reached.
8348
8349 MOVE_X_REACHED
8350 -when TO_X was reached before TO_POS or ZV were reached.
8351
8352 MOVE_LINE_CONTINUED
8353 - when we reached the end of the display area and the line must
8354 be continued.
8355
8356 MOVE_LINE_TRUNCATED
8357 - when we reached the end of the display area and the line is
8358 truncated.
8359
8360 MOVE_NEWLINE_OR_CR
8361 - when we stopped at a line end, i.e. a newline or a CR and selective
8362 display is on. */
8363
8364 static enum move_it_result
8365 move_it_in_display_line_to (struct it *it,
8366 ptrdiff_t to_charpos, int to_x,
8367 enum move_operation_enum op)
8368 {
8369 enum move_it_result result = MOVE_UNDEFINED;
8370 struct glyph_row *saved_glyph_row;
8371 struct it wrap_it, atpos_it, atx_it, ppos_it;
8372 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8373 void *ppos_data = NULL;
8374 bool may_wrap = false;
8375 enum it_method prev_method = it->method;
8376 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8377 bool saw_smaller_pos = prev_pos < to_charpos;
8378
8379 /* Don't produce glyphs in produce_glyphs. */
8380 saved_glyph_row = it->glyph_row;
8381 it->glyph_row = NULL;
8382
8383 /* Use wrap_it to save a copy of IT wherever a word wrap could
8384 occur. Use atpos_it to save a copy of IT at the desired buffer
8385 position, if found, so that we can scan ahead and check if the
8386 word later overshoots the window edge. Use atx_it similarly, for
8387 pixel positions. */
8388 wrap_it.sp = -1;
8389 atpos_it.sp = -1;
8390 atx_it.sp = -1;
8391
8392 /* Use ppos_it under bidi reordering to save a copy of IT for the
8393 initial position. We restore that position in IT when we have
8394 scanned the entire display line without finding a match for
8395 TO_CHARPOS and all the character positions are greater than
8396 TO_CHARPOS. We then restart the scan from the initial position,
8397 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8398 the closest to TO_CHARPOS. */
8399 if (it->bidi_p)
8400 {
8401 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8402 {
8403 SAVE_IT (ppos_it, *it, ppos_data);
8404 closest_pos = IT_CHARPOS (*it);
8405 }
8406 else
8407 closest_pos = ZV;
8408 }
8409
8410 #define BUFFER_POS_REACHED_P() \
8411 ((op & MOVE_TO_POS) != 0 \
8412 && BUFFERP (it->object) \
8413 && (IT_CHARPOS (*it) == to_charpos \
8414 || ((!it->bidi_p \
8415 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8416 && IT_CHARPOS (*it) > to_charpos) \
8417 || (it->what == IT_COMPOSITION \
8418 && ((IT_CHARPOS (*it) > to_charpos \
8419 && to_charpos >= it->cmp_it.charpos) \
8420 || (IT_CHARPOS (*it) < to_charpos \
8421 && to_charpos <= it->cmp_it.charpos)))) \
8422 && (it->method == GET_FROM_BUFFER \
8423 || (it->method == GET_FROM_DISPLAY_VECTOR \
8424 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8425
8426 /* If there's a line-/wrap-prefix, handle it. */
8427 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8428 && it->current_y < it->last_visible_y)
8429 handle_line_prefix (it);
8430
8431 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8432 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8433
8434 while (true)
8435 {
8436 int x, i, ascent = 0, descent = 0;
8437
8438 /* Utility macro to reset an iterator with x, ascent, and descent. */
8439 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8440 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8441 (IT)->max_descent = descent)
8442
8443 /* Stop if we move beyond TO_CHARPOS (after an image or a
8444 display string or stretch glyph). */
8445 if ((op & MOVE_TO_POS) != 0
8446 && BUFFERP (it->object)
8447 && it->method == GET_FROM_BUFFER
8448 && (((!it->bidi_p
8449 /* When the iterator is at base embedding level, we
8450 are guaranteed that characters are delivered for
8451 display in strictly increasing order of their
8452 buffer positions. */
8453 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8454 && IT_CHARPOS (*it) > to_charpos)
8455 || (it->bidi_p
8456 && (prev_method == GET_FROM_IMAGE
8457 || prev_method == GET_FROM_STRETCH
8458 || prev_method == GET_FROM_STRING)
8459 /* Passed TO_CHARPOS from left to right. */
8460 && ((prev_pos < to_charpos
8461 && IT_CHARPOS (*it) > to_charpos)
8462 /* Passed TO_CHARPOS from right to left. */
8463 || (prev_pos > to_charpos
8464 && IT_CHARPOS (*it) < to_charpos)))))
8465 {
8466 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8467 {
8468 result = MOVE_POS_MATCH_OR_ZV;
8469 break;
8470 }
8471 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8472 /* If wrap_it is valid, the current position might be in a
8473 word that is wrapped. So, save the iterator in
8474 atpos_it and continue to see if wrapping happens. */
8475 SAVE_IT (atpos_it, *it, atpos_data);
8476 }
8477
8478 /* Stop when ZV reached.
8479 We used to stop here when TO_CHARPOS reached as well, but that is
8480 too soon if this glyph does not fit on this line. So we handle it
8481 explicitly below. */
8482 if (!get_next_display_element (it))
8483 {
8484 result = MOVE_POS_MATCH_OR_ZV;
8485 break;
8486 }
8487
8488 if (it->line_wrap == TRUNCATE)
8489 {
8490 if (BUFFER_POS_REACHED_P ())
8491 {
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 break;
8494 }
8495 }
8496 else
8497 {
8498 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8499 {
8500 if (IT_DISPLAYING_WHITESPACE (it))
8501 may_wrap = true;
8502 else if (may_wrap)
8503 {
8504 /* We have reached a glyph that follows one or more
8505 whitespace characters. If the position is
8506 already found, we are done. */
8507 if (atpos_it.sp >= 0)
8508 {
8509 RESTORE_IT (it, &atpos_it, atpos_data);
8510 result = MOVE_POS_MATCH_OR_ZV;
8511 goto done;
8512 }
8513 if (atx_it.sp >= 0)
8514 {
8515 RESTORE_IT (it, &atx_it, atx_data);
8516 result = MOVE_X_REACHED;
8517 goto done;
8518 }
8519 /* Otherwise, we can wrap here. */
8520 SAVE_IT (wrap_it, *it, wrap_data);
8521 may_wrap = false;
8522 }
8523 }
8524 }
8525
8526 /* Remember the line height for the current line, in case
8527 the next element doesn't fit on the line. */
8528 ascent = it->max_ascent;
8529 descent = it->max_descent;
8530
8531 /* The call to produce_glyphs will get the metrics of the
8532 display element IT is loaded with. Record the x-position
8533 before this display element, in case it doesn't fit on the
8534 line. */
8535 x = it->current_x;
8536
8537 PRODUCE_GLYPHS (it);
8538
8539 if (it->area != TEXT_AREA)
8540 {
8541 prev_method = it->method;
8542 if (it->method == GET_FROM_BUFFER)
8543 prev_pos = IT_CHARPOS (*it);
8544 set_iterator_to_next (it, true);
8545 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8546 SET_TEXT_POS (this_line_min_pos,
8547 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8548 if (it->bidi_p
8549 && (op & MOVE_TO_POS)
8550 && IT_CHARPOS (*it) > to_charpos
8551 && IT_CHARPOS (*it) < closest_pos)
8552 closest_pos = IT_CHARPOS (*it);
8553 continue;
8554 }
8555
8556 /* The number of glyphs we get back in IT->nglyphs will normally
8557 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8558 character on a terminal frame, or (iii) a line end. For the
8559 second case, IT->nglyphs - 1 padding glyphs will be present.
8560 (On X frames, there is only one glyph produced for a
8561 composite character.)
8562
8563 The behavior implemented below means, for continuation lines,
8564 that as many spaces of a TAB as fit on the current line are
8565 displayed there. For terminal frames, as many glyphs of a
8566 multi-glyph character are displayed in the current line, too.
8567 This is what the old redisplay code did, and we keep it that
8568 way. Under X, the whole shape of a complex character must
8569 fit on the line or it will be completely displayed in the
8570 next line.
8571
8572 Note that both for tabs and padding glyphs, all glyphs have
8573 the same width. */
8574 if (it->nglyphs)
8575 {
8576 /* More than one glyph or glyph doesn't fit on line. All
8577 glyphs have the same width. */
8578 int single_glyph_width = it->pixel_width / it->nglyphs;
8579 int new_x;
8580 int x_before_this_char = x;
8581 int hpos_before_this_char = it->hpos;
8582
8583 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8584 {
8585 new_x = x + single_glyph_width;
8586
8587 /* We want to leave anything reaching TO_X to the caller. */
8588 if ((op & MOVE_TO_X) && new_x > to_x)
8589 {
8590 if (BUFFER_POS_REACHED_P ())
8591 {
8592 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8593 goto buffer_pos_reached;
8594 if (atpos_it.sp < 0)
8595 {
8596 SAVE_IT (atpos_it, *it, atpos_data);
8597 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8598 }
8599 }
8600 else
8601 {
8602 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8603 {
8604 it->current_x = x;
8605 result = MOVE_X_REACHED;
8606 break;
8607 }
8608 if (atx_it.sp < 0)
8609 {
8610 SAVE_IT (atx_it, *it, atx_data);
8611 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8612 }
8613 }
8614 }
8615
8616 if (/* Lines are continued. */
8617 it->line_wrap != TRUNCATE
8618 && (/* And glyph doesn't fit on the line. */
8619 new_x > it->last_visible_x
8620 /* Or it fits exactly and we're on a window
8621 system frame. */
8622 || (new_x == it->last_visible_x
8623 && FRAME_WINDOW_P (it->f)
8624 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8627 {
8628 if (/* IT->hpos == 0 means the very first glyph
8629 doesn't fit on the line, e.g. a wide image. */
8630 it->hpos == 0
8631 || (new_x == it->last_visible_x
8632 && FRAME_WINDOW_P (it->f)))
8633 {
8634 ++it->hpos;
8635 it->current_x = new_x;
8636
8637 /* The character's last glyph just barely fits
8638 in this row. */
8639 if (i == it->nglyphs - 1)
8640 {
8641 /* If this is the destination position,
8642 return a position *before* it in this row,
8643 now that we know it fits in this row. */
8644 if (BUFFER_POS_REACHED_P ())
8645 {
8646 if (it->line_wrap != WORD_WRAP
8647 || wrap_it.sp < 0
8648 /* If we've just found whitespace to
8649 wrap, effectively ignore the
8650 previous wrap point -- it is no
8651 longer relevant, but we won't
8652 have an opportunity to update it,
8653 since we've reached the edge of
8654 this screen line. */
8655 || (may_wrap
8656 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8657 {
8658 it->hpos = hpos_before_this_char;
8659 it->current_x = x_before_this_char;
8660 result = MOVE_POS_MATCH_OR_ZV;
8661 break;
8662 }
8663 if (it->line_wrap == WORD_WRAP
8664 && atpos_it.sp < 0)
8665 {
8666 SAVE_IT (atpos_it, *it, atpos_data);
8667 atpos_it.current_x = x_before_this_char;
8668 atpos_it.hpos = hpos_before_this_char;
8669 }
8670 }
8671
8672 prev_method = it->method;
8673 if (it->method == GET_FROM_BUFFER)
8674 prev_pos = IT_CHARPOS (*it);
8675 set_iterator_to_next (it, true);
8676 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8677 SET_TEXT_POS (this_line_min_pos,
8678 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8679 /* On graphical terminals, newlines may
8680 "overflow" into the fringe if
8681 overflow-newline-into-fringe is non-nil.
8682 On text terminals, and on graphical
8683 terminals with no right margin, newlines
8684 may overflow into the last glyph on the
8685 display line.*/
8686 if (!FRAME_WINDOW_P (it->f)
8687 || ((it->bidi_p
8688 && it->bidi_it.paragraph_dir == R2L)
8689 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8690 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8691 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8692 {
8693 if (!get_next_display_element (it))
8694 {
8695 result = MOVE_POS_MATCH_OR_ZV;
8696 break;
8697 }
8698 if (BUFFER_POS_REACHED_P ())
8699 {
8700 if (ITERATOR_AT_END_OF_LINE_P (it))
8701 result = MOVE_POS_MATCH_OR_ZV;
8702 else
8703 result = MOVE_LINE_CONTINUED;
8704 break;
8705 }
8706 if (ITERATOR_AT_END_OF_LINE_P (it)
8707 && (it->line_wrap != WORD_WRAP
8708 || wrap_it.sp < 0
8709 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8710 {
8711 result = MOVE_NEWLINE_OR_CR;
8712 break;
8713 }
8714 }
8715 }
8716 }
8717 else
8718 IT_RESET_X_ASCENT_DESCENT (it);
8719
8720 /* If the screen line ends with whitespace, and we
8721 are under word-wrap, don't use wrap_it: it is no
8722 longer relevant, but we won't have an opportunity
8723 to update it, since we are done with this screen
8724 line. */
8725 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8726 {
8727 /* If we've found TO_X, go back there, as we now
8728 know the last word fits on this screen line. */
8729 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8730 && atx_it.sp >= 0)
8731 {
8732 RESTORE_IT (it, &atx_it, atx_data);
8733 atpos_it.sp = -1;
8734 atx_it.sp = -1;
8735 result = MOVE_X_REACHED;
8736 break;
8737 }
8738 }
8739 else if (wrap_it.sp >= 0)
8740 {
8741 RESTORE_IT (it, &wrap_it, wrap_data);
8742 atpos_it.sp = -1;
8743 atx_it.sp = -1;
8744 }
8745
8746 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8747 IT_CHARPOS (*it)));
8748 result = MOVE_LINE_CONTINUED;
8749 break;
8750 }
8751
8752 if (BUFFER_POS_REACHED_P ())
8753 {
8754 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8755 goto buffer_pos_reached;
8756 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8757 {
8758 SAVE_IT (atpos_it, *it, atpos_data);
8759 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8760 }
8761 }
8762
8763 if (new_x > it->first_visible_x)
8764 {
8765 /* Glyph is visible. Increment number of glyphs that
8766 would be displayed. */
8767 ++it->hpos;
8768 }
8769 }
8770
8771 if (result != MOVE_UNDEFINED)
8772 break;
8773 }
8774 else if (BUFFER_POS_REACHED_P ())
8775 {
8776 buffer_pos_reached:
8777 IT_RESET_X_ASCENT_DESCENT (it);
8778 result = MOVE_POS_MATCH_OR_ZV;
8779 break;
8780 }
8781 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8782 {
8783 /* Stop when TO_X specified and reached. This check is
8784 necessary here because of lines consisting of a line end,
8785 only. The line end will not produce any glyphs and we
8786 would never get MOVE_X_REACHED. */
8787 eassert (it->nglyphs == 0);
8788 result = MOVE_X_REACHED;
8789 break;
8790 }
8791
8792 /* Is this a line end? If yes, we're done. */
8793 if (ITERATOR_AT_END_OF_LINE_P (it))
8794 {
8795 /* If we are past TO_CHARPOS, but never saw any character
8796 positions smaller than TO_CHARPOS, return
8797 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8798 did. */
8799 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8800 {
8801 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8802 {
8803 if (closest_pos < ZV)
8804 {
8805 RESTORE_IT (it, &ppos_it, ppos_data);
8806 /* Don't recurse if closest_pos is equal to
8807 to_charpos, since we have just tried that. */
8808 if (closest_pos != to_charpos)
8809 move_it_in_display_line_to (it, closest_pos, -1,
8810 MOVE_TO_POS);
8811 result = MOVE_POS_MATCH_OR_ZV;
8812 }
8813 else
8814 goto buffer_pos_reached;
8815 }
8816 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8817 && IT_CHARPOS (*it) > to_charpos)
8818 goto buffer_pos_reached;
8819 else
8820 result = MOVE_NEWLINE_OR_CR;
8821 }
8822 else
8823 result = MOVE_NEWLINE_OR_CR;
8824 break;
8825 }
8826
8827 prev_method = it->method;
8828 if (it->method == GET_FROM_BUFFER)
8829 prev_pos = IT_CHARPOS (*it);
8830 /* The current display element has been consumed. Advance
8831 to the next. */
8832 set_iterator_to_next (it, true);
8833 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8834 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8835 if (IT_CHARPOS (*it) < to_charpos)
8836 saw_smaller_pos = true;
8837 if (it->bidi_p
8838 && (op & MOVE_TO_POS)
8839 && IT_CHARPOS (*it) >= to_charpos
8840 && IT_CHARPOS (*it) < closest_pos)
8841 closest_pos = IT_CHARPOS (*it);
8842
8843 /* Stop if lines are truncated and IT's current x-position is
8844 past the right edge of the window now. */
8845 if (it->line_wrap == TRUNCATE
8846 && it->current_x >= it->last_visible_x)
8847 {
8848 if (!FRAME_WINDOW_P (it->f)
8849 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8850 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8851 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8852 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8853 {
8854 bool at_eob_p = false;
8855
8856 if ((at_eob_p = !get_next_display_element (it))
8857 || BUFFER_POS_REACHED_P ()
8858 /* If we are past TO_CHARPOS, but never saw any
8859 character positions smaller than TO_CHARPOS,
8860 return MOVE_POS_MATCH_OR_ZV, like the
8861 unidirectional display did. */
8862 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8863 && !saw_smaller_pos
8864 && IT_CHARPOS (*it) > to_charpos))
8865 {
8866 if (it->bidi_p
8867 && !BUFFER_POS_REACHED_P ()
8868 && !at_eob_p && closest_pos < ZV)
8869 {
8870 RESTORE_IT (it, &ppos_it, ppos_data);
8871 if (closest_pos != to_charpos)
8872 move_it_in_display_line_to (it, closest_pos, -1,
8873 MOVE_TO_POS);
8874 }
8875 result = MOVE_POS_MATCH_OR_ZV;
8876 break;
8877 }
8878 if (ITERATOR_AT_END_OF_LINE_P (it))
8879 {
8880 result = MOVE_NEWLINE_OR_CR;
8881 break;
8882 }
8883 }
8884 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8885 && !saw_smaller_pos
8886 && IT_CHARPOS (*it) > to_charpos)
8887 {
8888 if (closest_pos < ZV)
8889 {
8890 RESTORE_IT (it, &ppos_it, ppos_data);
8891 if (closest_pos != to_charpos)
8892 move_it_in_display_line_to (it, closest_pos, -1,
8893 MOVE_TO_POS);
8894 }
8895 result = MOVE_POS_MATCH_OR_ZV;
8896 break;
8897 }
8898 result = MOVE_LINE_TRUNCATED;
8899 break;
8900 }
8901 #undef IT_RESET_X_ASCENT_DESCENT
8902 }
8903
8904 #undef BUFFER_POS_REACHED_P
8905
8906 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8907 restore the saved iterator. */
8908 if (atpos_it.sp >= 0)
8909 RESTORE_IT (it, &atpos_it, atpos_data);
8910 else if (atx_it.sp >= 0)
8911 RESTORE_IT (it, &atx_it, atx_data);
8912
8913 done:
8914
8915 if (atpos_data)
8916 bidi_unshelve_cache (atpos_data, true);
8917 if (atx_data)
8918 bidi_unshelve_cache (atx_data, true);
8919 if (wrap_data)
8920 bidi_unshelve_cache (wrap_data, true);
8921 if (ppos_data)
8922 bidi_unshelve_cache (ppos_data, true);
8923
8924 /* Restore the iterator settings altered at the beginning of this
8925 function. */
8926 it->glyph_row = saved_glyph_row;
8927 return result;
8928 }
8929
8930 /* For external use. */
8931 void
8932 move_it_in_display_line (struct it *it,
8933 ptrdiff_t to_charpos, int to_x,
8934 enum move_operation_enum op)
8935 {
8936 if (it->line_wrap == WORD_WRAP
8937 && (op & MOVE_TO_X))
8938 {
8939 struct it save_it;
8940 void *save_data = NULL;
8941 int skip;
8942
8943 SAVE_IT (save_it, *it, save_data);
8944 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8945 /* When word-wrap is on, TO_X may lie past the end
8946 of a wrapped line. Then it->current is the
8947 character on the next line, so backtrack to the
8948 space before the wrap point. */
8949 if (skip == MOVE_LINE_CONTINUED)
8950 {
8951 int prev_x = max (it->current_x - 1, 0);
8952 RESTORE_IT (it, &save_it, save_data);
8953 move_it_in_display_line_to
8954 (it, -1, prev_x, MOVE_TO_X);
8955 }
8956 else
8957 bidi_unshelve_cache (save_data, true);
8958 }
8959 else
8960 move_it_in_display_line_to (it, to_charpos, to_x, op);
8961 }
8962
8963
8964 /* Move IT forward until it satisfies one or more of the criteria in
8965 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8966
8967 OP is a bit-mask that specifies where to stop, and in particular,
8968 which of those four position arguments makes a difference. See the
8969 description of enum move_operation_enum.
8970
8971 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8972 screen line, this function will set IT to the next position that is
8973 displayed to the right of TO_CHARPOS on the screen.
8974
8975 Return the maximum pixel length of any line scanned but never more
8976 than it.last_visible_x. */
8977
8978 int
8979 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8980 {
8981 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8982 int line_height, line_start_x = 0, reached = 0;
8983 int max_current_x = 0;
8984 void *backup_data = NULL;
8985
8986 for (;;)
8987 {
8988 if (op & MOVE_TO_VPOS)
8989 {
8990 /* If no TO_CHARPOS and no TO_X specified, stop at the
8991 start of the line TO_VPOS. */
8992 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8993 {
8994 if (it->vpos == to_vpos)
8995 {
8996 reached = 1;
8997 break;
8998 }
8999 else
9000 skip = move_it_in_display_line_to (it, -1, -1, 0);
9001 }
9002 else
9003 {
9004 /* TO_VPOS >= 0 means stop at TO_X in the line at
9005 TO_VPOS, or at TO_POS, whichever comes first. */
9006 if (it->vpos == to_vpos)
9007 {
9008 reached = 2;
9009 break;
9010 }
9011
9012 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9013
9014 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9015 {
9016 reached = 3;
9017 break;
9018 }
9019 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9020 {
9021 /* We have reached TO_X but not in the line we want. */
9022 skip = move_it_in_display_line_to (it, to_charpos,
9023 -1, MOVE_TO_POS);
9024 if (skip == MOVE_POS_MATCH_OR_ZV)
9025 {
9026 reached = 4;
9027 break;
9028 }
9029 }
9030 }
9031 }
9032 else if (op & MOVE_TO_Y)
9033 {
9034 struct it it_backup;
9035
9036 if (it->line_wrap == WORD_WRAP)
9037 SAVE_IT (it_backup, *it, backup_data);
9038
9039 /* TO_Y specified means stop at TO_X in the line containing
9040 TO_Y---or at TO_CHARPOS if this is reached first. The
9041 problem is that we can't really tell whether the line
9042 contains TO_Y before we have completely scanned it, and
9043 this may skip past TO_X. What we do is to first scan to
9044 TO_X.
9045
9046 If TO_X is not specified, use a TO_X of zero. The reason
9047 is to make the outcome of this function more predictable.
9048 If we didn't use TO_X == 0, we would stop at the end of
9049 the line which is probably not what a caller would expect
9050 to happen. */
9051 skip = move_it_in_display_line_to
9052 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9053 (MOVE_TO_X | (op & MOVE_TO_POS)));
9054
9055 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9056 if (skip == MOVE_POS_MATCH_OR_ZV)
9057 reached = 5;
9058 else if (skip == MOVE_X_REACHED)
9059 {
9060 /* If TO_X was reached, we want to know whether TO_Y is
9061 in the line. We know this is the case if the already
9062 scanned glyphs make the line tall enough. Otherwise,
9063 we must check by scanning the rest of the line. */
9064 line_height = it->max_ascent + it->max_descent;
9065 if (to_y >= it->current_y
9066 && to_y < it->current_y + line_height)
9067 {
9068 reached = 6;
9069 break;
9070 }
9071 SAVE_IT (it_backup, *it, backup_data);
9072 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9073 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9074 op & MOVE_TO_POS);
9075 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9076 line_height = it->max_ascent + it->max_descent;
9077 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9078
9079 if (to_y >= it->current_y
9080 && to_y < it->current_y + line_height)
9081 {
9082 /* If TO_Y is in this line and TO_X was reached
9083 above, we scanned too far. We have to restore
9084 IT's settings to the ones before skipping. But
9085 keep the more accurate values of max_ascent and
9086 max_descent we've found while skipping the rest
9087 of the line, for the sake of callers, such as
9088 pos_visible_p, that need to know the line
9089 height. */
9090 int max_ascent = it->max_ascent;
9091 int max_descent = it->max_descent;
9092
9093 RESTORE_IT (it, &it_backup, backup_data);
9094 it->max_ascent = max_ascent;
9095 it->max_descent = max_descent;
9096 reached = 6;
9097 }
9098 else
9099 {
9100 skip = skip2;
9101 if (skip == MOVE_POS_MATCH_OR_ZV)
9102 reached = 7;
9103 }
9104 }
9105 else
9106 {
9107 /* Check whether TO_Y is in this line. */
9108 line_height = it->max_ascent + it->max_descent;
9109 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9110
9111 if (to_y >= it->current_y
9112 && to_y < it->current_y + line_height)
9113 {
9114 if (to_y > it->current_y)
9115 max_current_x = max (it->current_x, max_current_x);
9116
9117 /* When word-wrap is on, TO_X may lie past the end
9118 of a wrapped line. Then it->current is the
9119 character on the next line, so backtrack to the
9120 space before the wrap point. */
9121 if (skip == MOVE_LINE_CONTINUED
9122 && it->line_wrap == WORD_WRAP)
9123 {
9124 int prev_x = max (it->current_x - 1, 0);
9125 RESTORE_IT (it, &it_backup, backup_data);
9126 skip = move_it_in_display_line_to
9127 (it, -1, prev_x, MOVE_TO_X);
9128 }
9129
9130 reached = 6;
9131 }
9132 }
9133
9134 if (reached)
9135 {
9136 max_current_x = max (it->current_x, max_current_x);
9137 break;
9138 }
9139 }
9140 else if (BUFFERP (it->object)
9141 && (it->method == GET_FROM_BUFFER
9142 || it->method == GET_FROM_STRETCH)
9143 && IT_CHARPOS (*it) >= to_charpos
9144 /* Under bidi iteration, a call to set_iterator_to_next
9145 can scan far beyond to_charpos if the initial
9146 portion of the next line needs to be reordered. In
9147 that case, give move_it_in_display_line_to another
9148 chance below. */
9149 && !(it->bidi_p
9150 && it->bidi_it.scan_dir == -1))
9151 skip = MOVE_POS_MATCH_OR_ZV;
9152 else
9153 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9154
9155 switch (skip)
9156 {
9157 case MOVE_POS_MATCH_OR_ZV:
9158 max_current_x = max (it->current_x, max_current_x);
9159 reached = 8;
9160 goto out;
9161
9162 case MOVE_NEWLINE_OR_CR:
9163 max_current_x = max (it->current_x, max_current_x);
9164 set_iterator_to_next (it, true);
9165 it->continuation_lines_width = 0;
9166 break;
9167
9168 case MOVE_LINE_TRUNCATED:
9169 max_current_x = it->last_visible_x;
9170 it->continuation_lines_width = 0;
9171 reseat_at_next_visible_line_start (it, false);
9172 if ((op & MOVE_TO_POS) != 0
9173 && IT_CHARPOS (*it) > to_charpos)
9174 {
9175 reached = 9;
9176 goto out;
9177 }
9178 break;
9179
9180 case MOVE_LINE_CONTINUED:
9181 max_current_x = it->last_visible_x;
9182 /* For continued lines ending in a tab, some of the glyphs
9183 associated with the tab are displayed on the current
9184 line. Since it->current_x does not include these glyphs,
9185 we use it->last_visible_x instead. */
9186 if (it->c == '\t')
9187 {
9188 it->continuation_lines_width += it->last_visible_x;
9189 /* When moving by vpos, ensure that the iterator really
9190 advances to the next line (bug#847, bug#969). Fixme:
9191 do we need to do this in other circumstances? */
9192 if (it->current_x != it->last_visible_x
9193 && (op & MOVE_TO_VPOS)
9194 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9195 {
9196 line_start_x = it->current_x + it->pixel_width
9197 - it->last_visible_x;
9198 if (FRAME_WINDOW_P (it->f))
9199 {
9200 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9201 struct font *face_font = face->font;
9202
9203 /* When display_line produces a continued line
9204 that ends in a TAB, it skips a tab stop that
9205 is closer than the font's space character
9206 width (see x_produce_glyphs where it produces
9207 the stretch glyph which represents a TAB).
9208 We need to reproduce the same logic here. */
9209 eassert (face_font);
9210 if (face_font)
9211 {
9212 if (line_start_x < face_font->space_width)
9213 line_start_x
9214 += it->tab_width * face_font->space_width;
9215 }
9216 }
9217 set_iterator_to_next (it, false);
9218 }
9219 }
9220 else
9221 it->continuation_lines_width += it->current_x;
9222 break;
9223
9224 default:
9225 emacs_abort ();
9226 }
9227
9228 /* Reset/increment for the next run. */
9229 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9230 it->current_x = line_start_x;
9231 line_start_x = 0;
9232 it->hpos = 0;
9233 it->current_y += it->max_ascent + it->max_descent;
9234 ++it->vpos;
9235 last_height = it->max_ascent + it->max_descent;
9236 it->max_ascent = it->max_descent = 0;
9237 }
9238
9239 out:
9240
9241 /* On text terminals, we may stop at the end of a line in the middle
9242 of a multi-character glyph. If the glyph itself is continued,
9243 i.e. it is actually displayed on the next line, don't treat this
9244 stopping point as valid; move to the next line instead (unless
9245 that brings us offscreen). */
9246 if (!FRAME_WINDOW_P (it->f)
9247 && op & MOVE_TO_POS
9248 && IT_CHARPOS (*it) == to_charpos
9249 && it->what == IT_CHARACTER
9250 && it->nglyphs > 1
9251 && it->line_wrap == WINDOW_WRAP
9252 && it->current_x == it->last_visible_x - 1
9253 && it->c != '\n'
9254 && it->c != '\t'
9255 && it->w->window_end_valid
9256 && it->vpos < it->w->window_end_vpos)
9257 {
9258 it->continuation_lines_width += it->current_x;
9259 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9260 it->current_y += it->max_ascent + it->max_descent;
9261 ++it->vpos;
9262 last_height = it->max_ascent + it->max_descent;
9263 }
9264
9265 if (backup_data)
9266 bidi_unshelve_cache (backup_data, true);
9267
9268 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9269
9270 return max_current_x;
9271 }
9272
9273
9274 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9275
9276 If DY > 0, move IT backward at least that many pixels. DY = 0
9277 means move IT backward to the preceding line start or BEGV. This
9278 function may move over more than DY pixels if IT->current_y - DY
9279 ends up in the middle of a line; in this case IT->current_y will be
9280 set to the top of the line moved to. */
9281
9282 void
9283 move_it_vertically_backward (struct it *it, int dy)
9284 {
9285 int nlines, h;
9286 struct it it2, it3;
9287 void *it2data = NULL, *it3data = NULL;
9288 ptrdiff_t start_pos;
9289 int nchars_per_row
9290 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9291 ptrdiff_t pos_limit;
9292
9293 move_further_back:
9294 eassert (dy >= 0);
9295
9296 start_pos = IT_CHARPOS (*it);
9297
9298 /* Estimate how many newlines we must move back. */
9299 nlines = max (1, dy / default_line_pixel_height (it->w));
9300 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9301 pos_limit = BEGV;
9302 else
9303 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9304
9305 /* Set the iterator's position that many lines back. But don't go
9306 back more than NLINES full screen lines -- this wins a day with
9307 buffers which have very long lines. */
9308 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9309 back_to_previous_visible_line_start (it);
9310
9311 /* Reseat the iterator here. When moving backward, we don't want
9312 reseat to skip forward over invisible text, set up the iterator
9313 to deliver from overlay strings at the new position etc. So,
9314 use reseat_1 here. */
9315 reseat_1 (it, it->current.pos, true);
9316
9317 /* We are now surely at a line start. */
9318 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9319 reordering is in effect. */
9320 it->continuation_lines_width = 0;
9321
9322 /* Move forward and see what y-distance we moved. First move to the
9323 start of the next line so that we get its height. We need this
9324 height to be able to tell whether we reached the specified
9325 y-distance. */
9326 SAVE_IT (it2, *it, it2data);
9327 it2.max_ascent = it2.max_descent = 0;
9328 do
9329 {
9330 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9331 MOVE_TO_POS | MOVE_TO_VPOS);
9332 }
9333 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9334 /* If we are in a display string which starts at START_POS,
9335 and that display string includes a newline, and we are
9336 right after that newline (i.e. at the beginning of a
9337 display line), exit the loop, because otherwise we will
9338 infloop, since move_it_to will see that it is already at
9339 START_POS and will not move. */
9340 || (it2.method == GET_FROM_STRING
9341 && IT_CHARPOS (it2) == start_pos
9342 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9343 eassert (IT_CHARPOS (*it) >= BEGV);
9344 SAVE_IT (it3, it2, it3data);
9345
9346 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9347 eassert (IT_CHARPOS (*it) >= BEGV);
9348 /* H is the actual vertical distance from the position in *IT
9349 and the starting position. */
9350 h = it2.current_y - it->current_y;
9351 /* NLINES is the distance in number of lines. */
9352 nlines = it2.vpos - it->vpos;
9353
9354 /* Correct IT's y and vpos position
9355 so that they are relative to the starting point. */
9356 it->vpos -= nlines;
9357 it->current_y -= h;
9358
9359 if (dy == 0)
9360 {
9361 /* DY == 0 means move to the start of the screen line. The
9362 value of nlines is > 0 if continuation lines were involved,
9363 or if the original IT position was at start of a line. */
9364 RESTORE_IT (it, it, it2data);
9365 if (nlines > 0)
9366 move_it_by_lines (it, nlines);
9367 /* The above code moves us to some position NLINES down,
9368 usually to its first glyph (leftmost in an L2R line), but
9369 that's not necessarily the start of the line, under bidi
9370 reordering. We want to get to the character position
9371 that is immediately after the newline of the previous
9372 line. */
9373 if (it->bidi_p
9374 && !it->continuation_lines_width
9375 && !STRINGP (it->string)
9376 && IT_CHARPOS (*it) > BEGV
9377 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9378 {
9379 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9380
9381 DEC_BOTH (cp, bp);
9382 cp = find_newline_no_quit (cp, bp, -1, NULL);
9383 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9384 }
9385 bidi_unshelve_cache (it3data, true);
9386 }
9387 else
9388 {
9389 /* The y-position we try to reach, relative to *IT.
9390 Note that H has been subtracted in front of the if-statement. */
9391 int target_y = it->current_y + h - dy;
9392 int y0 = it3.current_y;
9393 int y1;
9394 int line_height;
9395
9396 RESTORE_IT (&it3, &it3, it3data);
9397 y1 = line_bottom_y (&it3);
9398 line_height = y1 - y0;
9399 RESTORE_IT (it, it, it2data);
9400 /* If we did not reach target_y, try to move further backward if
9401 we can. If we moved too far backward, try to move forward. */
9402 if (target_y < it->current_y
9403 /* This is heuristic. In a window that's 3 lines high, with
9404 a line height of 13 pixels each, recentering with point
9405 on the bottom line will try to move -39/2 = 19 pixels
9406 backward. Try to avoid moving into the first line. */
9407 && (it->current_y - target_y
9408 > min (window_box_height (it->w), line_height * 2 / 3))
9409 && IT_CHARPOS (*it) > BEGV)
9410 {
9411 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9412 target_y - it->current_y));
9413 dy = it->current_y - target_y;
9414 goto move_further_back;
9415 }
9416 else if (target_y >= it->current_y + line_height
9417 && IT_CHARPOS (*it) < ZV)
9418 {
9419 /* Should move forward by at least one line, maybe more.
9420
9421 Note: Calling move_it_by_lines can be expensive on
9422 terminal frames, where compute_motion is used (via
9423 vmotion) to do the job, when there are very long lines
9424 and truncate-lines is nil. That's the reason for
9425 treating terminal frames specially here. */
9426
9427 if (!FRAME_WINDOW_P (it->f))
9428 move_it_vertically (it, target_y - (it->current_y + line_height));
9429 else
9430 {
9431 do
9432 {
9433 move_it_by_lines (it, 1);
9434 }
9435 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9436 }
9437 }
9438 }
9439 }
9440
9441
9442 /* Move IT by a specified amount of pixel lines DY. DY negative means
9443 move backwards. DY = 0 means move to start of screen line. At the
9444 end, IT will be on the start of a screen line. */
9445
9446 void
9447 move_it_vertically (struct it *it, int dy)
9448 {
9449 if (dy <= 0)
9450 move_it_vertically_backward (it, -dy);
9451 else
9452 {
9453 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9454 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9455 MOVE_TO_POS | MOVE_TO_Y);
9456 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9457
9458 /* If buffer ends in ZV without a newline, move to the start of
9459 the line to satisfy the post-condition. */
9460 if (IT_CHARPOS (*it) == ZV
9461 && ZV > BEGV
9462 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9463 move_it_by_lines (it, 0);
9464 }
9465 }
9466
9467
9468 /* Move iterator IT past the end of the text line it is in. */
9469
9470 void
9471 move_it_past_eol (struct it *it)
9472 {
9473 enum move_it_result rc;
9474
9475 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9476 if (rc == MOVE_NEWLINE_OR_CR)
9477 set_iterator_to_next (it, false);
9478 }
9479
9480
9481 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9482 negative means move up. DVPOS == 0 means move to the start of the
9483 screen line.
9484
9485 Optimization idea: If we would know that IT->f doesn't use
9486 a face with proportional font, we could be faster for
9487 truncate-lines nil. */
9488
9489 void
9490 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9491 {
9492
9493 /* The commented-out optimization uses vmotion on terminals. This
9494 gives bad results, because elements like it->what, on which
9495 callers such as pos_visible_p rely, aren't updated. */
9496 /* struct position pos;
9497 if (!FRAME_WINDOW_P (it->f))
9498 {
9499 struct text_pos textpos;
9500
9501 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9502 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9503 reseat (it, textpos, true);
9504 it->vpos += pos.vpos;
9505 it->current_y += pos.vpos;
9506 }
9507 else */
9508
9509 if (dvpos == 0)
9510 {
9511 /* DVPOS == 0 means move to the start of the screen line. */
9512 move_it_vertically_backward (it, 0);
9513 /* Let next call to line_bottom_y calculate real line height. */
9514 last_height = 0;
9515 }
9516 else if (dvpos > 0)
9517 {
9518 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9519 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9520 {
9521 /* Only move to the next buffer position if we ended up in a
9522 string from display property, not in an overlay string
9523 (before-string or after-string). That is because the
9524 latter don't conceal the underlying buffer position, so
9525 we can ask to move the iterator to the exact position we
9526 are interested in. Note that, even if we are already at
9527 IT_CHARPOS (*it), the call below is not a no-op, as it
9528 will detect that we are at the end of the string, pop the
9529 iterator, and compute it->current_x and it->hpos
9530 correctly. */
9531 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9532 -1, -1, -1, MOVE_TO_POS);
9533 }
9534 }
9535 else
9536 {
9537 struct it it2;
9538 void *it2data = NULL;
9539 ptrdiff_t start_charpos, i;
9540 int nchars_per_row
9541 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9542 bool hit_pos_limit = false;
9543 ptrdiff_t pos_limit;
9544
9545 /* Start at the beginning of the screen line containing IT's
9546 position. This may actually move vertically backwards,
9547 in case of overlays, so adjust dvpos accordingly. */
9548 dvpos += it->vpos;
9549 move_it_vertically_backward (it, 0);
9550 dvpos -= it->vpos;
9551
9552 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9553 screen lines, and reseat the iterator there. */
9554 start_charpos = IT_CHARPOS (*it);
9555 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9556 pos_limit = BEGV;
9557 else
9558 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9559
9560 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9561 back_to_previous_visible_line_start (it);
9562 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9563 hit_pos_limit = true;
9564 reseat (it, it->current.pos, true);
9565
9566 /* Move further back if we end up in a string or an image. */
9567 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9568 {
9569 /* First try to move to start of display line. */
9570 dvpos += it->vpos;
9571 move_it_vertically_backward (it, 0);
9572 dvpos -= it->vpos;
9573 if (IT_POS_VALID_AFTER_MOVE_P (it))
9574 break;
9575 /* If start of line is still in string or image,
9576 move further back. */
9577 back_to_previous_visible_line_start (it);
9578 reseat (it, it->current.pos, true);
9579 dvpos--;
9580 }
9581
9582 it->current_x = it->hpos = 0;
9583
9584 /* Above call may have moved too far if continuation lines
9585 are involved. Scan forward and see if it did. */
9586 SAVE_IT (it2, *it, it2data);
9587 it2.vpos = it2.current_y = 0;
9588 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9589 it->vpos -= it2.vpos;
9590 it->current_y -= it2.current_y;
9591 it->current_x = it->hpos = 0;
9592
9593 /* If we moved too far back, move IT some lines forward. */
9594 if (it2.vpos > -dvpos)
9595 {
9596 int delta = it2.vpos + dvpos;
9597
9598 RESTORE_IT (&it2, &it2, it2data);
9599 SAVE_IT (it2, *it, it2data);
9600 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9601 /* Move back again if we got too far ahead. */
9602 if (IT_CHARPOS (*it) >= start_charpos)
9603 RESTORE_IT (it, &it2, it2data);
9604 else
9605 bidi_unshelve_cache (it2data, true);
9606 }
9607 else if (hit_pos_limit && pos_limit > BEGV
9608 && dvpos < 0 && it2.vpos < -dvpos)
9609 {
9610 /* If we hit the limit, but still didn't make it far enough
9611 back, that means there's a display string with a newline
9612 covering a large chunk of text, and that caused
9613 back_to_previous_visible_line_start try to go too far.
9614 Punish those who commit such atrocities by going back
9615 until we've reached DVPOS, after lifting the limit, which
9616 could make it slow for very long lines. "If it hurts,
9617 don't do that!" */
9618 dvpos += it2.vpos;
9619 RESTORE_IT (it, it, it2data);
9620 for (i = -dvpos; i > 0; --i)
9621 {
9622 back_to_previous_visible_line_start (it);
9623 it->vpos--;
9624 }
9625 reseat_1 (it, it->current.pos, true);
9626 }
9627 else
9628 RESTORE_IT (it, it, it2data);
9629 }
9630 }
9631
9632 /* Return true if IT points into the middle of a display vector. */
9633
9634 bool
9635 in_display_vector_p (struct it *it)
9636 {
9637 return (it->method == GET_FROM_DISPLAY_VECTOR
9638 && it->current.dpvec_index > 0
9639 && it->dpvec + it->current.dpvec_index != it->dpend);
9640 }
9641
9642 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9643 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9644 WINDOW must be a live window and defaults to the selected one. The
9645 return value is a cons of the maximum pixel-width of any text line and
9646 the maximum pixel-height of all text lines.
9647
9648 The optional argument FROM, if non-nil, specifies the first text
9649 position and defaults to the minimum accessible position of the buffer.
9650 If FROM is t, use the minimum accessible position that is not a newline
9651 character. TO, if non-nil, specifies the last text position and
9652 defaults to the maximum accessible position of the buffer. If TO is t,
9653 use the maximum accessible position that is not a newline character.
9654
9655 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9656 width that can be returned. X-LIMIT nil or omitted, means to use the
9657 pixel-width of WINDOW's body; use this if you do not intend to change
9658 the width of WINDOW. Use the maximum width WINDOW may assume if you
9659 intend to change WINDOW's width. In any case, text whose x-coordinate
9660 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9661 can take some time, it's always a good idea to make this argument as
9662 small as possible; in particular, if the buffer contains long lines that
9663 shall be truncated anyway.
9664
9665 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9666 height that can be returned. Text lines whose y-coordinate is beyond
9667 Y-LIMIT are ignored. Since calculating the text height of a large
9668 buffer can take some time, it makes sense to specify this argument if
9669 the size of the buffer is unknown.
9670
9671 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9672 include the height of the mode- or header-line of WINDOW in the return
9673 value. If it is either the symbol `mode-line' or `header-line', include
9674 only the height of that line, if present, in the return value. If t,
9675 include the height of both, if present, in the return value. */)
9676 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9677 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9678 {
9679 struct window *w = decode_live_window (window);
9680 Lisp_Object buffer = w->contents;
9681 struct buffer *b;
9682 struct it it;
9683 struct buffer *old_b = NULL;
9684 ptrdiff_t start, end, pos;
9685 struct text_pos startp;
9686 void *itdata = NULL;
9687 int c, max_y = -1, x = 0, y = 0;
9688
9689 CHECK_BUFFER (buffer);
9690 b = XBUFFER (buffer);
9691
9692 if (b != current_buffer)
9693 {
9694 old_b = current_buffer;
9695 set_buffer_internal (b);
9696 }
9697
9698 if (NILP (from))
9699 start = BEGV;
9700 else if (EQ (from, Qt))
9701 {
9702 start = pos = BEGV;
9703 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9704 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9705 start = pos;
9706 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9707 start = pos;
9708 }
9709 else
9710 {
9711 CHECK_NUMBER_COERCE_MARKER (from);
9712 start = min (max (XINT (from), BEGV), ZV);
9713 }
9714
9715 if (NILP (to))
9716 end = ZV;
9717 else if (EQ (to, Qt))
9718 {
9719 end = pos = ZV;
9720 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9721 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9722 end = pos;
9723 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9724 end = pos;
9725 }
9726 else
9727 {
9728 CHECK_NUMBER_COERCE_MARKER (to);
9729 end = max (start, min (XINT (to), ZV));
9730 }
9731
9732 if (!NILP (y_limit))
9733 {
9734 CHECK_NUMBER (y_limit);
9735 max_y = min (XINT (y_limit), INT_MAX);
9736 }
9737
9738 itdata = bidi_shelve_cache ();
9739 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9740 start_display (&it, w, startp);
9741
9742 if (NILP (x_limit))
9743 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9744 else
9745 {
9746 CHECK_NUMBER (x_limit);
9747 it.last_visible_x = min (XINT (x_limit), INFINITY);
9748 /* Actually, we never want move_it_to stop at to_x. But to make
9749 sure that move_it_in_display_line_to always moves far enough,
9750 we set it to INT_MAX and specify MOVE_TO_X. */
9751 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9752 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9753 }
9754
9755 y = it.current_y + it.max_ascent + it.max_descent;
9756
9757 if (!EQ (mode_and_header_line, Qheader_line)
9758 && !EQ (mode_and_header_line, Qt))
9759 /* Do not count the header-line which was counted automatically by
9760 start_display. */
9761 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9762
9763 if (EQ (mode_and_header_line, Qmode_line)
9764 || EQ (mode_and_header_line, Qt))
9765 /* Do count the mode-line which is not included automatically by
9766 start_display. */
9767 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9768
9769 bidi_unshelve_cache (itdata, false);
9770
9771 if (old_b)
9772 set_buffer_internal (old_b);
9773
9774 return Fcons (make_number (x), make_number (y));
9775 }
9776 \f
9777 /***********************************************************************
9778 Messages
9779 ***********************************************************************/
9780
9781
9782 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9783 to *Messages*. */
9784
9785 void
9786 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9787 {
9788 Lisp_Object msg, fmt;
9789 char *buffer;
9790 ptrdiff_t len;
9791 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9792 USE_SAFE_ALLOCA;
9793
9794 fmt = msg = Qnil;
9795 GCPRO4 (fmt, msg, arg1, arg2);
9796
9797 fmt = build_string (format);
9798 msg = CALLN (Fformat, fmt, arg1, arg2);
9799
9800 len = SBYTES (msg) + 1;
9801 buffer = SAFE_ALLOCA (len);
9802 memcpy (buffer, SDATA (msg), len);
9803
9804 message_dolog (buffer, len - 1, true, false);
9805 SAFE_FREE ();
9806
9807 UNGCPRO;
9808 }
9809
9810
9811 /* Output a newline in the *Messages* buffer if "needs" one. */
9812
9813 void
9814 message_log_maybe_newline (void)
9815 {
9816 if (message_log_need_newline)
9817 message_dolog ("", 0, true, false);
9818 }
9819
9820
9821 /* Add a string M of length NBYTES to the message log, optionally
9822 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9823 true, means interpret the contents of M as multibyte. This
9824 function calls low-level routines in order to bypass text property
9825 hooks, etc. which might not be safe to run.
9826
9827 This may GC (insert may run before/after change hooks),
9828 so the buffer M must NOT point to a Lisp string. */
9829
9830 void
9831 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9832 {
9833 const unsigned char *msg = (const unsigned char *) m;
9834
9835 if (!NILP (Vmemory_full))
9836 return;
9837
9838 if (!NILP (Vmessage_log_max))
9839 {
9840 struct buffer *oldbuf;
9841 Lisp_Object oldpoint, oldbegv, oldzv;
9842 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9843 ptrdiff_t point_at_end = 0;
9844 ptrdiff_t zv_at_end = 0;
9845 Lisp_Object old_deactivate_mark;
9846 struct gcpro gcpro1;
9847
9848 old_deactivate_mark = Vdeactivate_mark;
9849 oldbuf = current_buffer;
9850
9851 /* Ensure the Messages buffer exists, and switch to it.
9852 If we created it, set the major-mode. */
9853 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9854 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9855 if (newbuffer
9856 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9857 call0 (intern ("messages-buffer-mode"));
9858
9859 bset_undo_list (current_buffer, Qt);
9860 bset_cache_long_scans (current_buffer, Qnil);
9861
9862 oldpoint = message_dolog_marker1;
9863 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9864 oldbegv = message_dolog_marker2;
9865 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9866 oldzv = message_dolog_marker3;
9867 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9868 GCPRO1 (old_deactivate_mark);
9869
9870 if (PT == Z)
9871 point_at_end = 1;
9872 if (ZV == Z)
9873 zv_at_end = 1;
9874
9875 BEGV = BEG;
9876 BEGV_BYTE = BEG_BYTE;
9877 ZV = Z;
9878 ZV_BYTE = Z_BYTE;
9879 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9880
9881 /* Insert the string--maybe converting multibyte to single byte
9882 or vice versa, so that all the text fits the buffer. */
9883 if (multibyte
9884 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9885 {
9886 ptrdiff_t i;
9887 int c, char_bytes;
9888 char work[1];
9889
9890 /* Convert a multibyte string to single-byte
9891 for the *Message* buffer. */
9892 for (i = 0; i < nbytes; i += char_bytes)
9893 {
9894 c = string_char_and_length (msg + i, &char_bytes);
9895 work[0] = CHAR_TO_BYTE8 (c);
9896 insert_1_both (work, 1, 1, true, false, false);
9897 }
9898 }
9899 else if (! multibyte
9900 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9901 {
9902 ptrdiff_t i;
9903 int c, char_bytes;
9904 unsigned char str[MAX_MULTIBYTE_LENGTH];
9905 /* Convert a single-byte string to multibyte
9906 for the *Message* buffer. */
9907 for (i = 0; i < nbytes; i++)
9908 {
9909 c = msg[i];
9910 MAKE_CHAR_MULTIBYTE (c);
9911 char_bytes = CHAR_STRING (c, str);
9912 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
9913 }
9914 }
9915 else if (nbytes)
9916 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
9917 true, false, false);
9918
9919 if (nlflag)
9920 {
9921 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9922 printmax_t dups;
9923
9924 insert_1_both ("\n", 1, 1, true, false, false);
9925
9926 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
9927 this_bol = PT;
9928 this_bol_byte = PT_BYTE;
9929
9930 /* See if this line duplicates the previous one.
9931 If so, combine duplicates. */
9932 if (this_bol > BEG)
9933 {
9934 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
9935 prev_bol = PT;
9936 prev_bol_byte = PT_BYTE;
9937
9938 dups = message_log_check_duplicate (prev_bol_byte,
9939 this_bol_byte);
9940 if (dups)
9941 {
9942 del_range_both (prev_bol, prev_bol_byte,
9943 this_bol, this_bol_byte, false);
9944 if (dups > 1)
9945 {
9946 char dupstr[sizeof " [ times]"
9947 + INT_STRLEN_BOUND (printmax_t)];
9948
9949 /* If you change this format, don't forget to also
9950 change message_log_check_duplicate. */
9951 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9952 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9953 insert_1_both (dupstr, duplen, duplen,
9954 true, false, true);
9955 }
9956 }
9957 }
9958
9959 /* If we have more than the desired maximum number of lines
9960 in the *Messages* buffer now, delete the oldest ones.
9961 This is safe because we don't have undo in this buffer. */
9962
9963 if (NATNUMP (Vmessage_log_max))
9964 {
9965 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9966 -XFASTINT (Vmessage_log_max) - 1, false);
9967 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
9968 }
9969 }
9970 BEGV = marker_position (oldbegv);
9971 BEGV_BYTE = marker_byte_position (oldbegv);
9972
9973 if (zv_at_end)
9974 {
9975 ZV = Z;
9976 ZV_BYTE = Z_BYTE;
9977 }
9978 else
9979 {
9980 ZV = marker_position (oldzv);
9981 ZV_BYTE = marker_byte_position (oldzv);
9982 }
9983
9984 if (point_at_end)
9985 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9986 else
9987 /* We can't do Fgoto_char (oldpoint) because it will run some
9988 Lisp code. */
9989 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9990 marker_byte_position (oldpoint));
9991
9992 UNGCPRO;
9993 unchain_marker (XMARKER (oldpoint));
9994 unchain_marker (XMARKER (oldbegv));
9995 unchain_marker (XMARKER (oldzv));
9996
9997 /* We called insert_1_both above with its 5th argument (PREPARE)
9998 false, which prevents insert_1_both from calling
9999 prepare_to_modify_buffer, which in turns prevents us from
10000 incrementing windows_or_buffers_changed even if *Messages* is
10001 shown in some window. So we must manually set
10002 windows_or_buffers_changed here to make up for that. */
10003 windows_or_buffers_changed = old_windows_or_buffers_changed;
10004 bset_redisplay (current_buffer);
10005
10006 set_buffer_internal (oldbuf);
10007
10008 message_log_need_newline = !nlflag;
10009 Vdeactivate_mark = old_deactivate_mark;
10010 }
10011 }
10012
10013
10014 /* We are at the end of the buffer after just having inserted a newline.
10015 (Note: We depend on the fact we won't be crossing the gap.)
10016 Check to see if the most recent message looks a lot like the previous one.
10017 Return 0 if different, 1 if the new one should just replace it, or a
10018 value N > 1 if we should also append " [N times]". */
10019
10020 static intmax_t
10021 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10022 {
10023 ptrdiff_t i;
10024 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10025 bool seen_dots = false;
10026 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10027 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10028
10029 for (i = 0; i < len; i++)
10030 {
10031 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10032 seen_dots = true;
10033 if (p1[i] != p2[i])
10034 return seen_dots;
10035 }
10036 p1 += len;
10037 if (*p1 == '\n')
10038 return 2;
10039 if (*p1++ == ' ' && *p1++ == '[')
10040 {
10041 char *pend;
10042 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10043 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10044 return n + 1;
10045 }
10046 return 0;
10047 }
10048 \f
10049
10050 /* Display an echo area message M with a specified length of NBYTES
10051 bytes. The string may include null characters. If M is not a
10052 string, clear out any existing message, and let the mini-buffer
10053 text show through.
10054
10055 This function cancels echoing. */
10056
10057 void
10058 message3 (Lisp_Object m)
10059 {
10060 struct gcpro gcpro1;
10061
10062 GCPRO1 (m);
10063 clear_message (true, true);
10064 cancel_echoing ();
10065
10066 /* First flush out any partial line written with print. */
10067 message_log_maybe_newline ();
10068 if (STRINGP (m))
10069 {
10070 ptrdiff_t nbytes = SBYTES (m);
10071 bool multibyte = STRING_MULTIBYTE (m);
10072 char *buffer;
10073 USE_SAFE_ALLOCA;
10074 SAFE_ALLOCA_STRING (buffer, m);
10075 message_dolog (buffer, nbytes, true, multibyte);
10076 SAFE_FREE ();
10077 }
10078 if (! inhibit_message)
10079 message3_nolog (m);
10080 UNGCPRO;
10081 }
10082
10083
10084 /* The non-logging version of message3.
10085 This does not cancel echoing, because it is used for echoing.
10086 Perhaps we need to make a separate function for echoing
10087 and make this cancel echoing. */
10088
10089 void
10090 message3_nolog (Lisp_Object m)
10091 {
10092 struct frame *sf = SELECTED_FRAME ();
10093
10094 if (FRAME_INITIAL_P (sf))
10095 {
10096 if (noninteractive_need_newline)
10097 putc ('\n', stderr);
10098 noninteractive_need_newline = false;
10099 if (STRINGP (m))
10100 {
10101 Lisp_Object s = ENCODE_SYSTEM (m);
10102
10103 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10104 }
10105 if (!cursor_in_echo_area)
10106 fprintf (stderr, "\n");
10107 fflush (stderr);
10108 }
10109 /* Error messages get reported properly by cmd_error, so this must be just an
10110 informative message; if the frame hasn't really been initialized yet, just
10111 toss it. */
10112 else if (INTERACTIVE && sf->glyphs_initialized_p)
10113 {
10114 /* Get the frame containing the mini-buffer
10115 that the selected frame is using. */
10116 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10117 Lisp_Object frame = XWINDOW (mini_window)->frame;
10118 struct frame *f = XFRAME (frame);
10119
10120 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10121 Fmake_frame_visible (frame);
10122
10123 if (STRINGP (m) && SCHARS (m) > 0)
10124 {
10125 set_message (m);
10126 if (minibuffer_auto_raise)
10127 Fraise_frame (frame);
10128 /* Assume we are not echoing.
10129 (If we are, echo_now will override this.) */
10130 echo_message_buffer = Qnil;
10131 }
10132 else
10133 clear_message (true, true);
10134
10135 do_pending_window_change (false);
10136 echo_area_display (true);
10137 do_pending_window_change (false);
10138 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10139 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10140 }
10141 }
10142
10143
10144 /* Display a null-terminated echo area message M. If M is 0, clear
10145 out any existing message, and let the mini-buffer text show through.
10146
10147 The buffer M must continue to exist until after the echo area gets
10148 cleared or some other message gets displayed there. Do not pass
10149 text that is stored in a Lisp string. Do not pass text in a buffer
10150 that was alloca'd. */
10151
10152 void
10153 message1 (const char *m)
10154 {
10155 message3 (m ? build_unibyte_string (m) : Qnil);
10156 }
10157
10158
10159 /* The non-logging counterpart of message1. */
10160
10161 void
10162 message1_nolog (const char *m)
10163 {
10164 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10165 }
10166
10167 /* Display a message M which contains a single %s
10168 which gets replaced with STRING. */
10169
10170 void
10171 message_with_string (const char *m, Lisp_Object string, bool log)
10172 {
10173 CHECK_STRING (string);
10174
10175 if (noninteractive)
10176 {
10177 if (m)
10178 {
10179 /* ENCODE_SYSTEM below can GC and/or relocate the
10180 Lisp data, so make sure we don't use it here. */
10181 eassert (relocatable_string_data_p (m) != 1);
10182
10183 if (noninteractive_need_newline)
10184 putc ('\n', stderr);
10185 noninteractive_need_newline = false;
10186 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10187 if (!cursor_in_echo_area)
10188 fprintf (stderr, "\n");
10189 fflush (stderr);
10190 }
10191 }
10192 else if (INTERACTIVE)
10193 {
10194 /* The frame whose minibuffer we're going to display the message on.
10195 It may be larger than the selected frame, so we need
10196 to use its buffer, not the selected frame's buffer. */
10197 Lisp_Object mini_window;
10198 struct frame *f, *sf = SELECTED_FRAME ();
10199
10200 /* Get the frame containing the minibuffer
10201 that the selected frame is using. */
10202 mini_window = FRAME_MINIBUF_WINDOW (sf);
10203 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10204
10205 /* Error messages get reported properly by cmd_error, so this must be
10206 just an informative message; if the frame hasn't really been
10207 initialized yet, just toss it. */
10208 if (f->glyphs_initialized_p)
10209 {
10210 struct gcpro gcpro1, gcpro2;
10211
10212 Lisp_Object fmt = build_string (m);
10213 Lisp_Object msg = string;
10214 GCPRO2 (fmt, msg);
10215
10216 msg = CALLN (Fformat, fmt, msg);
10217
10218 if (log)
10219 message3 (msg);
10220 else
10221 message3_nolog (msg);
10222
10223 UNGCPRO;
10224
10225 /* Print should start at the beginning of the message
10226 buffer next time. */
10227 message_buf_print = false;
10228 }
10229 }
10230 }
10231
10232
10233 /* Dump an informative message to the minibuf. If M is 0, clear out
10234 any existing message, and let the mini-buffer text show through. */
10235
10236 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10237 vmessage (const char *m, va_list ap)
10238 {
10239 if (noninteractive)
10240 {
10241 if (m)
10242 {
10243 if (noninteractive_need_newline)
10244 putc ('\n', stderr);
10245 noninteractive_need_newline = false;
10246 vfprintf (stderr, m, ap);
10247 if (!cursor_in_echo_area)
10248 fprintf (stderr, "\n");
10249 fflush (stderr);
10250 }
10251 }
10252 else if (INTERACTIVE)
10253 {
10254 /* The frame whose mini-buffer we're going to display the message
10255 on. It may be larger than the selected frame, so we need to
10256 use its buffer, not the selected frame's buffer. */
10257 Lisp_Object mini_window;
10258 struct frame *f, *sf = SELECTED_FRAME ();
10259
10260 /* Get the frame containing the mini-buffer
10261 that the selected frame is using. */
10262 mini_window = FRAME_MINIBUF_WINDOW (sf);
10263 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10264
10265 /* Error messages get reported properly by cmd_error, so this must be
10266 just an informative message; if the frame hasn't really been
10267 initialized yet, just toss it. */
10268 if (f->glyphs_initialized_p)
10269 {
10270 if (m)
10271 {
10272 ptrdiff_t len;
10273 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10274 USE_SAFE_ALLOCA;
10275 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10276
10277 len = doprnt (message_buf, maxsize, m, 0, ap);
10278
10279 message3 (make_string (message_buf, len));
10280 SAFE_FREE ();
10281 }
10282 else
10283 message1 (0);
10284
10285 /* Print should start at the beginning of the message
10286 buffer next time. */
10287 message_buf_print = false;
10288 }
10289 }
10290 }
10291
10292 void
10293 message (const char *m, ...)
10294 {
10295 va_list ap;
10296 va_start (ap, m);
10297 vmessage (m, ap);
10298 va_end (ap);
10299 }
10300
10301
10302 #if false
10303 /* The non-logging version of message. */
10304
10305 void
10306 message_nolog (const char *m, ...)
10307 {
10308 Lisp_Object old_log_max;
10309 va_list ap;
10310 va_start (ap, m);
10311 old_log_max = Vmessage_log_max;
10312 Vmessage_log_max = Qnil;
10313 vmessage (m, ap);
10314 Vmessage_log_max = old_log_max;
10315 va_end (ap);
10316 }
10317 #endif
10318
10319
10320 /* Display the current message in the current mini-buffer. This is
10321 only called from error handlers in process.c, and is not time
10322 critical. */
10323
10324 void
10325 update_echo_area (void)
10326 {
10327 if (!NILP (echo_area_buffer[0]))
10328 {
10329 Lisp_Object string;
10330 string = Fcurrent_message ();
10331 message3 (string);
10332 }
10333 }
10334
10335
10336 /* Make sure echo area buffers in `echo_buffers' are live.
10337 If they aren't, make new ones. */
10338
10339 static void
10340 ensure_echo_area_buffers (void)
10341 {
10342 int i;
10343
10344 for (i = 0; i < 2; ++i)
10345 if (!BUFFERP (echo_buffer[i])
10346 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10347 {
10348 char name[30];
10349 Lisp_Object old_buffer;
10350 int j;
10351
10352 old_buffer = echo_buffer[i];
10353 echo_buffer[i] = Fget_buffer_create
10354 (make_formatted_string (name, " *Echo Area %d*", i));
10355 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10356 /* to force word wrap in echo area -
10357 it was decided to postpone this*/
10358 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10359
10360 for (j = 0; j < 2; ++j)
10361 if (EQ (old_buffer, echo_area_buffer[j]))
10362 echo_area_buffer[j] = echo_buffer[i];
10363 }
10364 }
10365
10366
10367 /* Call FN with args A1..A2 with either the current or last displayed
10368 echo_area_buffer as current buffer.
10369
10370 WHICH zero means use the current message buffer
10371 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10372 from echo_buffer[] and clear it.
10373
10374 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10375 suitable buffer from echo_buffer[] and clear it.
10376
10377 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10378 that the current message becomes the last displayed one, make
10379 choose a suitable buffer for echo_area_buffer[0], and clear it.
10380
10381 Value is what FN returns. */
10382
10383 static bool
10384 with_echo_area_buffer (struct window *w, int which,
10385 bool (*fn) (ptrdiff_t, Lisp_Object),
10386 ptrdiff_t a1, Lisp_Object a2)
10387 {
10388 Lisp_Object buffer;
10389 bool this_one, the_other, clear_buffer_p, rc;
10390 ptrdiff_t count = SPECPDL_INDEX ();
10391
10392 /* If buffers aren't live, make new ones. */
10393 ensure_echo_area_buffers ();
10394
10395 clear_buffer_p = false;
10396
10397 if (which == 0)
10398 this_one = false, the_other = true;
10399 else if (which > 0)
10400 this_one = true, the_other = false;
10401 else
10402 {
10403 this_one = false, the_other = true;
10404 clear_buffer_p = true;
10405
10406 /* We need a fresh one in case the current echo buffer equals
10407 the one containing the last displayed echo area message. */
10408 if (!NILP (echo_area_buffer[this_one])
10409 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10410 echo_area_buffer[this_one] = Qnil;
10411 }
10412
10413 /* Choose a suitable buffer from echo_buffer[] is we don't
10414 have one. */
10415 if (NILP (echo_area_buffer[this_one]))
10416 {
10417 echo_area_buffer[this_one]
10418 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10419 ? echo_buffer[the_other]
10420 : echo_buffer[this_one]);
10421 clear_buffer_p = true;
10422 }
10423
10424 buffer = echo_area_buffer[this_one];
10425
10426 /* Don't get confused by reusing the buffer used for echoing
10427 for a different purpose. */
10428 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10429 cancel_echoing ();
10430
10431 record_unwind_protect (unwind_with_echo_area_buffer,
10432 with_echo_area_buffer_unwind_data (w));
10433
10434 /* Make the echo area buffer current. Note that for display
10435 purposes, it is not necessary that the displayed window's buffer
10436 == current_buffer, except for text property lookup. So, let's
10437 only set that buffer temporarily here without doing a full
10438 Fset_window_buffer. We must also change w->pointm, though,
10439 because otherwise an assertions in unshow_buffer fails, and Emacs
10440 aborts. */
10441 set_buffer_internal_1 (XBUFFER (buffer));
10442 if (w)
10443 {
10444 wset_buffer (w, buffer);
10445 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10446 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10447 }
10448
10449 bset_undo_list (current_buffer, Qt);
10450 bset_read_only (current_buffer, Qnil);
10451 specbind (Qinhibit_read_only, Qt);
10452 specbind (Qinhibit_modification_hooks, Qt);
10453
10454 if (clear_buffer_p && Z > BEG)
10455 del_range (BEG, Z);
10456
10457 eassert (BEGV >= BEG);
10458 eassert (ZV <= Z && ZV >= BEGV);
10459
10460 rc = fn (a1, a2);
10461
10462 eassert (BEGV >= BEG);
10463 eassert (ZV <= Z && ZV >= BEGV);
10464
10465 unbind_to (count, Qnil);
10466 return rc;
10467 }
10468
10469
10470 /* Save state that should be preserved around the call to the function
10471 FN called in with_echo_area_buffer. */
10472
10473 static Lisp_Object
10474 with_echo_area_buffer_unwind_data (struct window *w)
10475 {
10476 int i = 0;
10477 Lisp_Object vector, tmp;
10478
10479 /* Reduce consing by keeping one vector in
10480 Vwith_echo_area_save_vector. */
10481 vector = Vwith_echo_area_save_vector;
10482 Vwith_echo_area_save_vector = Qnil;
10483
10484 if (NILP (vector))
10485 vector = Fmake_vector (make_number (11), Qnil);
10486
10487 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10488 ASET (vector, i, Vdeactivate_mark); ++i;
10489 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10490
10491 if (w)
10492 {
10493 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10494 ASET (vector, i, w->contents); ++i;
10495 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10496 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10497 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10498 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10499 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10500 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10501 }
10502 else
10503 {
10504 int end = i + 8;
10505 for (; i < end; ++i)
10506 ASET (vector, i, Qnil);
10507 }
10508
10509 eassert (i == ASIZE (vector));
10510 return vector;
10511 }
10512
10513
10514 /* Restore global state from VECTOR which was created by
10515 with_echo_area_buffer_unwind_data. */
10516
10517 static void
10518 unwind_with_echo_area_buffer (Lisp_Object vector)
10519 {
10520 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10521 Vdeactivate_mark = AREF (vector, 1);
10522 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10523
10524 if (WINDOWP (AREF (vector, 3)))
10525 {
10526 struct window *w;
10527 Lisp_Object buffer;
10528
10529 w = XWINDOW (AREF (vector, 3));
10530 buffer = AREF (vector, 4);
10531
10532 wset_buffer (w, buffer);
10533 set_marker_both (w->pointm, buffer,
10534 XFASTINT (AREF (vector, 5)),
10535 XFASTINT (AREF (vector, 6)));
10536 set_marker_both (w->old_pointm, buffer,
10537 XFASTINT (AREF (vector, 7)),
10538 XFASTINT (AREF (vector, 8)));
10539 set_marker_both (w->start, buffer,
10540 XFASTINT (AREF (vector, 9)),
10541 XFASTINT (AREF (vector, 10)));
10542 }
10543
10544 Vwith_echo_area_save_vector = vector;
10545 }
10546
10547
10548 /* Set up the echo area for use by print functions. MULTIBYTE_P
10549 means we will print multibyte. */
10550
10551 void
10552 setup_echo_area_for_printing (bool multibyte_p)
10553 {
10554 /* If we can't find an echo area any more, exit. */
10555 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10556 Fkill_emacs (Qnil);
10557
10558 ensure_echo_area_buffers ();
10559
10560 if (!message_buf_print)
10561 {
10562 /* A message has been output since the last time we printed.
10563 Choose a fresh echo area buffer. */
10564 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10565 echo_area_buffer[0] = echo_buffer[1];
10566 else
10567 echo_area_buffer[0] = echo_buffer[0];
10568
10569 /* Switch to that buffer and clear it. */
10570 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10571 bset_truncate_lines (current_buffer, Qnil);
10572
10573 if (Z > BEG)
10574 {
10575 ptrdiff_t count = SPECPDL_INDEX ();
10576 specbind (Qinhibit_read_only, Qt);
10577 /* Note that undo recording is always disabled. */
10578 del_range (BEG, Z);
10579 unbind_to (count, Qnil);
10580 }
10581 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10582
10583 /* Set up the buffer for the multibyteness we need. */
10584 if (multibyte_p
10585 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10586 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10587
10588 /* Raise the frame containing the echo area. */
10589 if (minibuffer_auto_raise)
10590 {
10591 struct frame *sf = SELECTED_FRAME ();
10592 Lisp_Object mini_window;
10593 mini_window = FRAME_MINIBUF_WINDOW (sf);
10594 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10595 }
10596
10597 message_log_maybe_newline ();
10598 message_buf_print = true;
10599 }
10600 else
10601 {
10602 if (NILP (echo_area_buffer[0]))
10603 {
10604 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10605 echo_area_buffer[0] = echo_buffer[1];
10606 else
10607 echo_area_buffer[0] = echo_buffer[0];
10608 }
10609
10610 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10611 {
10612 /* Someone switched buffers between print requests. */
10613 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10614 bset_truncate_lines (current_buffer, Qnil);
10615 }
10616 }
10617 }
10618
10619
10620 /* Display an echo area message in window W. Value is true if W's
10621 height is changed. If display_last_displayed_message_p,
10622 display the message that was last displayed, otherwise
10623 display the current message. */
10624
10625 static bool
10626 display_echo_area (struct window *w)
10627 {
10628 bool no_message_p, window_height_changed_p;
10629
10630 /* Temporarily disable garbage collections while displaying the echo
10631 area. This is done because a GC can print a message itself.
10632 That message would modify the echo area buffer's contents while a
10633 redisplay of the buffer is going on, and seriously confuse
10634 redisplay. */
10635 ptrdiff_t count = inhibit_garbage_collection ();
10636
10637 /* If there is no message, we must call display_echo_area_1
10638 nevertheless because it resizes the window. But we will have to
10639 reset the echo_area_buffer in question to nil at the end because
10640 with_echo_area_buffer will sets it to an empty buffer. */
10641 bool i = display_last_displayed_message_p;
10642 no_message_p = NILP (echo_area_buffer[i]);
10643
10644 window_height_changed_p
10645 = with_echo_area_buffer (w, display_last_displayed_message_p,
10646 display_echo_area_1,
10647 (intptr_t) w, Qnil);
10648
10649 if (no_message_p)
10650 echo_area_buffer[i] = Qnil;
10651
10652 unbind_to (count, Qnil);
10653 return window_height_changed_p;
10654 }
10655
10656
10657 /* Helper for display_echo_area. Display the current buffer which
10658 contains the current echo area message in window W, a mini-window,
10659 a pointer to which is passed in A1. A2..A4 are currently not used.
10660 Change the height of W so that all of the message is displayed.
10661 Value is true if height of W was changed. */
10662
10663 static bool
10664 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10665 {
10666 intptr_t i1 = a1;
10667 struct window *w = (struct window *) i1;
10668 Lisp_Object window;
10669 struct text_pos start;
10670
10671 /* Do this before displaying, so that we have a large enough glyph
10672 matrix for the display. If we can't get enough space for the
10673 whole text, display the last N lines. That works by setting w->start. */
10674 bool window_height_changed_p = resize_mini_window (w, false);
10675
10676 /* Use the starting position chosen by resize_mini_window. */
10677 SET_TEXT_POS_FROM_MARKER (start, w->start);
10678
10679 /* Display. */
10680 clear_glyph_matrix (w->desired_matrix);
10681 XSETWINDOW (window, w);
10682 try_window (window, start, 0);
10683
10684 return window_height_changed_p;
10685 }
10686
10687
10688 /* Resize the echo area window to exactly the size needed for the
10689 currently displayed message, if there is one. If a mini-buffer
10690 is active, don't shrink it. */
10691
10692 void
10693 resize_echo_area_exactly (void)
10694 {
10695 if (BUFFERP (echo_area_buffer[0])
10696 && WINDOWP (echo_area_window))
10697 {
10698 struct window *w = XWINDOW (echo_area_window);
10699 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10700 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10701 (intptr_t) w, resize_exactly);
10702 if (resized_p)
10703 {
10704 windows_or_buffers_changed = 42;
10705 update_mode_lines = 30;
10706 redisplay_internal ();
10707 }
10708 }
10709 }
10710
10711
10712 /* Callback function for with_echo_area_buffer, when used from
10713 resize_echo_area_exactly. A1 contains a pointer to the window to
10714 resize, EXACTLY non-nil means resize the mini-window exactly to the
10715 size of the text displayed. A3 and A4 are not used. Value is what
10716 resize_mini_window returns. */
10717
10718 static bool
10719 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10720 {
10721 intptr_t i1 = a1;
10722 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10723 }
10724
10725
10726 /* Resize mini-window W to fit the size of its contents. EXACT_P
10727 means size the window exactly to the size needed. Otherwise, it's
10728 only enlarged until W's buffer is empty.
10729
10730 Set W->start to the right place to begin display. If the whole
10731 contents fit, start at the beginning. Otherwise, start so as
10732 to make the end of the contents appear. This is particularly
10733 important for y-or-n-p, but seems desirable generally.
10734
10735 Value is true if the window height has been changed. */
10736
10737 bool
10738 resize_mini_window (struct window *w, bool exact_p)
10739 {
10740 struct frame *f = XFRAME (w->frame);
10741 bool window_height_changed_p = false;
10742
10743 eassert (MINI_WINDOW_P (w));
10744
10745 /* By default, start display at the beginning. */
10746 set_marker_both (w->start, w->contents,
10747 BUF_BEGV (XBUFFER (w->contents)),
10748 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10749
10750 /* Don't resize windows while redisplaying a window; it would
10751 confuse redisplay functions when the size of the window they are
10752 displaying changes from under them. Such a resizing can happen,
10753 for instance, when which-func prints a long message while
10754 we are running fontification-functions. We're running these
10755 functions with safe_call which binds inhibit-redisplay to t. */
10756 if (!NILP (Vinhibit_redisplay))
10757 return false;
10758
10759 /* Nil means don't try to resize. */
10760 if (NILP (Vresize_mini_windows)
10761 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10762 return false;
10763
10764 if (!FRAME_MINIBUF_ONLY_P (f))
10765 {
10766 struct it it;
10767 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10768 + WINDOW_PIXEL_HEIGHT (w));
10769 int unit = FRAME_LINE_HEIGHT (f);
10770 int height, max_height;
10771 struct text_pos start;
10772 struct buffer *old_current_buffer = NULL;
10773
10774 if (current_buffer != XBUFFER (w->contents))
10775 {
10776 old_current_buffer = current_buffer;
10777 set_buffer_internal (XBUFFER (w->contents));
10778 }
10779
10780 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10781
10782 /* Compute the max. number of lines specified by the user. */
10783 if (FLOATP (Vmax_mini_window_height))
10784 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10785 else if (INTEGERP (Vmax_mini_window_height))
10786 max_height = XINT (Vmax_mini_window_height) * unit;
10787 else
10788 max_height = total_height / 4;
10789
10790 /* Correct that max. height if it's bogus. */
10791 max_height = clip_to_bounds (unit, max_height, total_height);
10792
10793 /* Find out the height of the text in the window. */
10794 if (it.line_wrap == TRUNCATE)
10795 height = unit;
10796 else
10797 {
10798 last_height = 0;
10799 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10800 if (it.max_ascent == 0 && it.max_descent == 0)
10801 height = it.current_y + last_height;
10802 else
10803 height = it.current_y + it.max_ascent + it.max_descent;
10804 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10805 }
10806
10807 /* Compute a suitable window start. */
10808 if (height > max_height)
10809 {
10810 height = (max_height / unit) * unit;
10811 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10812 move_it_vertically_backward (&it, height - unit);
10813 start = it.current.pos;
10814 }
10815 else
10816 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10817 SET_MARKER_FROM_TEXT_POS (w->start, start);
10818
10819 if (EQ (Vresize_mini_windows, Qgrow_only))
10820 {
10821 /* Let it grow only, until we display an empty message, in which
10822 case the window shrinks again. */
10823 if (height > WINDOW_PIXEL_HEIGHT (w))
10824 {
10825 int old_height = WINDOW_PIXEL_HEIGHT (w);
10826
10827 FRAME_WINDOWS_FROZEN (f) = true;
10828 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10829 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10830 }
10831 else if (height < WINDOW_PIXEL_HEIGHT (w)
10832 && (exact_p || BEGV == ZV))
10833 {
10834 int old_height = WINDOW_PIXEL_HEIGHT (w);
10835
10836 FRAME_WINDOWS_FROZEN (f) = false;
10837 shrink_mini_window (w, true);
10838 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10839 }
10840 }
10841 else
10842 {
10843 /* Always resize to exact size needed. */
10844 if (height > WINDOW_PIXEL_HEIGHT (w))
10845 {
10846 int old_height = WINDOW_PIXEL_HEIGHT (w);
10847
10848 FRAME_WINDOWS_FROZEN (f) = true;
10849 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10850 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10851 }
10852 else if (height < WINDOW_PIXEL_HEIGHT (w))
10853 {
10854 int old_height = WINDOW_PIXEL_HEIGHT (w);
10855
10856 FRAME_WINDOWS_FROZEN (f) = false;
10857 shrink_mini_window (w, true);
10858
10859 if (height)
10860 {
10861 FRAME_WINDOWS_FROZEN (f) = true;
10862 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10863 }
10864
10865 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10866 }
10867 }
10868
10869 if (old_current_buffer)
10870 set_buffer_internal (old_current_buffer);
10871 }
10872
10873 return window_height_changed_p;
10874 }
10875
10876
10877 /* Value is the current message, a string, or nil if there is no
10878 current message. */
10879
10880 Lisp_Object
10881 current_message (void)
10882 {
10883 Lisp_Object msg;
10884
10885 if (!BUFFERP (echo_area_buffer[0]))
10886 msg = Qnil;
10887 else
10888 {
10889 with_echo_area_buffer (0, 0, current_message_1,
10890 (intptr_t) &msg, Qnil);
10891 if (NILP (msg))
10892 echo_area_buffer[0] = Qnil;
10893 }
10894
10895 return msg;
10896 }
10897
10898
10899 static bool
10900 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10901 {
10902 intptr_t i1 = a1;
10903 Lisp_Object *msg = (Lisp_Object *) i1;
10904
10905 if (Z > BEG)
10906 *msg = make_buffer_string (BEG, Z, true);
10907 else
10908 *msg = Qnil;
10909 return false;
10910 }
10911
10912
10913 /* Push the current message on Vmessage_stack for later restoration
10914 by restore_message. Value is true if the current message isn't
10915 empty. This is a relatively infrequent operation, so it's not
10916 worth optimizing. */
10917
10918 bool
10919 push_message (void)
10920 {
10921 Lisp_Object msg = current_message ();
10922 Vmessage_stack = Fcons (msg, Vmessage_stack);
10923 return STRINGP (msg);
10924 }
10925
10926
10927 /* Restore message display from the top of Vmessage_stack. */
10928
10929 void
10930 restore_message (void)
10931 {
10932 eassert (CONSP (Vmessage_stack));
10933 message3_nolog (XCAR (Vmessage_stack));
10934 }
10935
10936
10937 /* Handler for unwind-protect calling pop_message. */
10938
10939 void
10940 pop_message_unwind (void)
10941 {
10942 /* Pop the top-most entry off Vmessage_stack. */
10943 eassert (CONSP (Vmessage_stack));
10944 Vmessage_stack = XCDR (Vmessage_stack);
10945 }
10946
10947
10948 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10949 exits. If the stack is not empty, we have a missing pop_message
10950 somewhere. */
10951
10952 void
10953 check_message_stack (void)
10954 {
10955 if (!NILP (Vmessage_stack))
10956 emacs_abort ();
10957 }
10958
10959
10960 /* Truncate to NCHARS what will be displayed in the echo area the next
10961 time we display it---but don't redisplay it now. */
10962
10963 void
10964 truncate_echo_area (ptrdiff_t nchars)
10965 {
10966 if (nchars == 0)
10967 echo_area_buffer[0] = Qnil;
10968 else if (!noninteractive
10969 && INTERACTIVE
10970 && !NILP (echo_area_buffer[0]))
10971 {
10972 struct frame *sf = SELECTED_FRAME ();
10973 /* Error messages get reported properly by cmd_error, so this must be
10974 just an informative message; if the frame hasn't really been
10975 initialized yet, just toss it. */
10976 if (sf->glyphs_initialized_p)
10977 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10978 }
10979 }
10980
10981
10982 /* Helper function for truncate_echo_area. Truncate the current
10983 message to at most NCHARS characters. */
10984
10985 static bool
10986 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10987 {
10988 if (BEG + nchars < Z)
10989 del_range (BEG + nchars, Z);
10990 if (Z == BEG)
10991 echo_area_buffer[0] = Qnil;
10992 return false;
10993 }
10994
10995 /* Set the current message to STRING. */
10996
10997 static void
10998 set_message (Lisp_Object string)
10999 {
11000 eassert (STRINGP (string));
11001
11002 message_enable_multibyte = STRING_MULTIBYTE (string);
11003
11004 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11005 message_buf_print = false;
11006 help_echo_showing_p = false;
11007
11008 if (STRINGP (Vdebug_on_message)
11009 && STRINGP (string)
11010 && fast_string_match (Vdebug_on_message, string) >= 0)
11011 call_debugger (list2 (Qerror, string));
11012 }
11013
11014
11015 /* Helper function for set_message. First argument is ignored and second
11016 argument has the same meaning as for set_message.
11017 This function is called with the echo area buffer being current. */
11018
11019 static bool
11020 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11021 {
11022 eassert (STRINGP (string));
11023
11024 /* Change multibyteness of the echo buffer appropriately. */
11025 if (message_enable_multibyte
11026 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11027 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11028
11029 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11030 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11031 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11032
11033 /* Insert new message at BEG. */
11034 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11035
11036 /* This function takes care of single/multibyte conversion.
11037 We just have to ensure that the echo area buffer has the right
11038 setting of enable_multibyte_characters. */
11039 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11040
11041 return false;
11042 }
11043
11044
11045 /* Clear messages. CURRENT_P means clear the current message.
11046 LAST_DISPLAYED_P means clear the message last displayed. */
11047
11048 void
11049 clear_message (bool current_p, bool last_displayed_p)
11050 {
11051 if (current_p)
11052 {
11053 echo_area_buffer[0] = Qnil;
11054 message_cleared_p = true;
11055 }
11056
11057 if (last_displayed_p)
11058 echo_area_buffer[1] = Qnil;
11059
11060 message_buf_print = false;
11061 }
11062
11063 /* Clear garbaged frames.
11064
11065 This function is used where the old redisplay called
11066 redraw_garbaged_frames which in turn called redraw_frame which in
11067 turn called clear_frame. The call to clear_frame was a source of
11068 flickering. I believe a clear_frame is not necessary. It should
11069 suffice in the new redisplay to invalidate all current matrices,
11070 and ensure a complete redisplay of all windows. */
11071
11072 static void
11073 clear_garbaged_frames (void)
11074 {
11075 if (frame_garbaged)
11076 {
11077 Lisp_Object tail, frame;
11078
11079 FOR_EACH_FRAME (tail, frame)
11080 {
11081 struct frame *f = XFRAME (frame);
11082
11083 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11084 {
11085 if (f->resized_p)
11086 redraw_frame (f);
11087 else
11088 clear_current_matrices (f);
11089 fset_redisplay (f);
11090 f->garbaged = false;
11091 f->resized_p = false;
11092 }
11093 }
11094
11095 frame_garbaged = false;
11096 }
11097 }
11098
11099
11100 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P,
11101 update selected_frame. Value is true if the mini-windows height
11102 has been changed. */
11103
11104 static bool
11105 echo_area_display (bool update_frame_p)
11106 {
11107 Lisp_Object mini_window;
11108 struct window *w;
11109 struct frame *f;
11110 bool window_height_changed_p = false;
11111 struct frame *sf = SELECTED_FRAME ();
11112
11113 mini_window = FRAME_MINIBUF_WINDOW (sf);
11114 w = XWINDOW (mini_window);
11115 f = XFRAME (WINDOW_FRAME (w));
11116
11117 /* Don't display if frame is invisible or not yet initialized. */
11118 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11119 return false;
11120
11121 #ifdef HAVE_WINDOW_SYSTEM
11122 /* When Emacs starts, selected_frame may be the initial terminal
11123 frame. If we let this through, a message would be displayed on
11124 the terminal. */
11125 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11126 return false;
11127 #endif /* HAVE_WINDOW_SYSTEM */
11128
11129 /* Redraw garbaged frames. */
11130 clear_garbaged_frames ();
11131
11132 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11133 {
11134 echo_area_window = mini_window;
11135 window_height_changed_p = display_echo_area (w);
11136 w->must_be_updated_p = true;
11137
11138 /* Update the display, unless called from redisplay_internal.
11139 Also don't update the screen during redisplay itself. The
11140 update will happen at the end of redisplay, and an update
11141 here could cause confusion. */
11142 if (update_frame_p && !redisplaying_p)
11143 {
11144 int n = 0;
11145
11146 /* If the display update has been interrupted by pending
11147 input, update mode lines in the frame. Due to the
11148 pending input, it might have been that redisplay hasn't
11149 been called, so that mode lines above the echo area are
11150 garbaged. This looks odd, so we prevent it here. */
11151 if (!display_completed)
11152 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11153
11154 if (window_height_changed_p
11155 /* Don't do this if Emacs is shutting down. Redisplay
11156 needs to run hooks. */
11157 && !NILP (Vrun_hooks))
11158 {
11159 /* Must update other windows. Likewise as in other
11160 cases, don't let this update be interrupted by
11161 pending input. */
11162 ptrdiff_t count = SPECPDL_INDEX ();
11163 specbind (Qredisplay_dont_pause, Qt);
11164 windows_or_buffers_changed = 44;
11165 redisplay_internal ();
11166 unbind_to (count, Qnil);
11167 }
11168 else if (FRAME_WINDOW_P (f) && n == 0)
11169 {
11170 /* Window configuration is the same as before.
11171 Can do with a display update of the echo area,
11172 unless we displayed some mode lines. */
11173 update_single_window (w);
11174 flush_frame (f);
11175 }
11176 else
11177 update_frame (f, true, true);
11178
11179 /* If cursor is in the echo area, make sure that the next
11180 redisplay displays the minibuffer, so that the cursor will
11181 be replaced with what the minibuffer wants. */
11182 if (cursor_in_echo_area)
11183 wset_redisplay (XWINDOW (mini_window));
11184 }
11185 }
11186 else if (!EQ (mini_window, selected_window))
11187 wset_redisplay (XWINDOW (mini_window));
11188
11189 /* Last displayed message is now the current message. */
11190 echo_area_buffer[1] = echo_area_buffer[0];
11191 /* Inform read_char that we're not echoing. */
11192 echo_message_buffer = Qnil;
11193
11194 /* Prevent redisplay optimization in redisplay_internal by resetting
11195 this_line_start_pos. This is done because the mini-buffer now
11196 displays the message instead of its buffer text. */
11197 if (EQ (mini_window, selected_window))
11198 CHARPOS (this_line_start_pos) = 0;
11199
11200 return window_height_changed_p;
11201 }
11202
11203 /* True if W's buffer was changed but not saved. */
11204
11205 static bool
11206 window_buffer_changed (struct window *w)
11207 {
11208 struct buffer *b = XBUFFER (w->contents);
11209
11210 eassert (BUFFER_LIVE_P (b));
11211
11212 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11213 }
11214
11215 /* True if W has %c in its mode line and mode line should be updated. */
11216
11217 static bool
11218 mode_line_update_needed (struct window *w)
11219 {
11220 return (w->column_number_displayed != -1
11221 && !(PT == w->last_point && !window_outdated (w))
11222 && (w->column_number_displayed != current_column ()));
11223 }
11224
11225 /* True if window start of W is frozen and may not be changed during
11226 redisplay. */
11227
11228 static bool
11229 window_frozen_p (struct window *w)
11230 {
11231 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11232 {
11233 Lisp_Object window;
11234
11235 XSETWINDOW (window, w);
11236 if (MINI_WINDOW_P (w))
11237 return false;
11238 else if (EQ (window, selected_window))
11239 return false;
11240 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11241 && EQ (window, Vminibuf_scroll_window))
11242 /* This special window can't be frozen too. */
11243 return false;
11244 else
11245 return true;
11246 }
11247 return false;
11248 }
11249
11250 /***********************************************************************
11251 Mode Lines and Frame Titles
11252 ***********************************************************************/
11253
11254 /* A buffer for constructing non-propertized mode-line strings and
11255 frame titles in it; allocated from the heap in init_xdisp and
11256 resized as needed in store_mode_line_noprop_char. */
11257
11258 static char *mode_line_noprop_buf;
11259
11260 /* The buffer's end, and a current output position in it. */
11261
11262 static char *mode_line_noprop_buf_end;
11263 static char *mode_line_noprop_ptr;
11264
11265 #define MODE_LINE_NOPROP_LEN(start) \
11266 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11267
11268 static enum {
11269 MODE_LINE_DISPLAY = 0,
11270 MODE_LINE_TITLE,
11271 MODE_LINE_NOPROP,
11272 MODE_LINE_STRING
11273 } mode_line_target;
11274
11275 /* Alist that caches the results of :propertize.
11276 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11277 static Lisp_Object mode_line_proptrans_alist;
11278
11279 /* List of strings making up the mode-line. */
11280 static Lisp_Object mode_line_string_list;
11281
11282 /* Base face property when building propertized mode line string. */
11283 static Lisp_Object mode_line_string_face;
11284 static Lisp_Object mode_line_string_face_prop;
11285
11286
11287 /* Unwind data for mode line strings */
11288
11289 static Lisp_Object Vmode_line_unwind_vector;
11290
11291 static Lisp_Object
11292 format_mode_line_unwind_data (struct frame *target_frame,
11293 struct buffer *obuf,
11294 Lisp_Object owin,
11295 bool save_proptrans)
11296 {
11297 Lisp_Object vector, tmp;
11298
11299 /* Reduce consing by keeping one vector in
11300 Vwith_echo_area_save_vector. */
11301 vector = Vmode_line_unwind_vector;
11302 Vmode_line_unwind_vector = Qnil;
11303
11304 if (NILP (vector))
11305 vector = Fmake_vector (make_number (10), Qnil);
11306
11307 ASET (vector, 0, make_number (mode_line_target));
11308 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11309 ASET (vector, 2, mode_line_string_list);
11310 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11311 ASET (vector, 4, mode_line_string_face);
11312 ASET (vector, 5, mode_line_string_face_prop);
11313
11314 if (obuf)
11315 XSETBUFFER (tmp, obuf);
11316 else
11317 tmp = Qnil;
11318 ASET (vector, 6, tmp);
11319 ASET (vector, 7, owin);
11320 if (target_frame)
11321 {
11322 /* Similarly to `with-selected-window', if the operation selects
11323 a window on another frame, we must restore that frame's
11324 selected window, and (for a tty) the top-frame. */
11325 ASET (vector, 8, target_frame->selected_window);
11326 if (FRAME_TERMCAP_P (target_frame))
11327 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11328 }
11329
11330 return vector;
11331 }
11332
11333 static void
11334 unwind_format_mode_line (Lisp_Object vector)
11335 {
11336 Lisp_Object old_window = AREF (vector, 7);
11337 Lisp_Object target_frame_window = AREF (vector, 8);
11338 Lisp_Object old_top_frame = AREF (vector, 9);
11339
11340 mode_line_target = XINT (AREF (vector, 0));
11341 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11342 mode_line_string_list = AREF (vector, 2);
11343 if (! EQ (AREF (vector, 3), Qt))
11344 mode_line_proptrans_alist = AREF (vector, 3);
11345 mode_line_string_face = AREF (vector, 4);
11346 mode_line_string_face_prop = AREF (vector, 5);
11347
11348 /* Select window before buffer, since it may change the buffer. */
11349 if (!NILP (old_window))
11350 {
11351 /* If the operation that we are unwinding had selected a window
11352 on a different frame, reset its frame-selected-window. For a
11353 text terminal, reset its top-frame if necessary. */
11354 if (!NILP (target_frame_window))
11355 {
11356 Lisp_Object frame
11357 = WINDOW_FRAME (XWINDOW (target_frame_window));
11358
11359 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11360 Fselect_window (target_frame_window, Qt);
11361
11362 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11363 Fselect_frame (old_top_frame, Qt);
11364 }
11365
11366 Fselect_window (old_window, Qt);
11367 }
11368
11369 if (!NILP (AREF (vector, 6)))
11370 {
11371 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11372 ASET (vector, 6, Qnil);
11373 }
11374
11375 Vmode_line_unwind_vector = vector;
11376 }
11377
11378
11379 /* Store a single character C for the frame title in mode_line_noprop_buf.
11380 Re-allocate mode_line_noprop_buf if necessary. */
11381
11382 static void
11383 store_mode_line_noprop_char (char c)
11384 {
11385 /* If output position has reached the end of the allocated buffer,
11386 increase the buffer's size. */
11387 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11388 {
11389 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11390 ptrdiff_t size = len;
11391 mode_line_noprop_buf =
11392 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11393 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11394 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11395 }
11396
11397 *mode_line_noprop_ptr++ = c;
11398 }
11399
11400
11401 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11402 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11403 characters that yield more columns than PRECISION; PRECISION <= 0
11404 means copy the whole string. Pad with spaces until FIELD_WIDTH
11405 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11406 pad. Called from display_mode_element when it is used to build a
11407 frame title. */
11408
11409 static int
11410 store_mode_line_noprop (const char *string, int field_width, int precision)
11411 {
11412 const unsigned char *str = (const unsigned char *) string;
11413 int n = 0;
11414 ptrdiff_t dummy, nbytes;
11415
11416 /* Copy at most PRECISION chars from STR. */
11417 nbytes = strlen (string);
11418 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11419 while (nbytes--)
11420 store_mode_line_noprop_char (*str++);
11421
11422 /* Fill up with spaces until FIELD_WIDTH reached. */
11423 while (field_width > 0
11424 && n < field_width)
11425 {
11426 store_mode_line_noprop_char (' ');
11427 ++n;
11428 }
11429
11430 return n;
11431 }
11432
11433 /***********************************************************************
11434 Frame Titles
11435 ***********************************************************************/
11436
11437 #ifdef HAVE_WINDOW_SYSTEM
11438
11439 /* Set the title of FRAME, if it has changed. The title format is
11440 Vicon_title_format if FRAME is iconified, otherwise it is
11441 frame_title_format. */
11442
11443 static void
11444 x_consider_frame_title (Lisp_Object frame)
11445 {
11446 struct frame *f = XFRAME (frame);
11447
11448 if (FRAME_WINDOW_P (f)
11449 || FRAME_MINIBUF_ONLY_P (f)
11450 || f->explicit_name)
11451 {
11452 /* Do we have more than one visible frame on this X display? */
11453 Lisp_Object tail, other_frame, fmt;
11454 ptrdiff_t title_start;
11455 char *title;
11456 ptrdiff_t len;
11457 struct it it;
11458 ptrdiff_t count = SPECPDL_INDEX ();
11459
11460 FOR_EACH_FRAME (tail, other_frame)
11461 {
11462 struct frame *tf = XFRAME (other_frame);
11463
11464 if (tf != f
11465 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11466 && !FRAME_MINIBUF_ONLY_P (tf)
11467 && !EQ (other_frame, tip_frame)
11468 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11469 break;
11470 }
11471
11472 /* Set global variable indicating that multiple frames exist. */
11473 multiple_frames = CONSP (tail);
11474
11475 /* Switch to the buffer of selected window of the frame. Set up
11476 mode_line_target so that display_mode_element will output into
11477 mode_line_noprop_buf; then display the title. */
11478 record_unwind_protect (unwind_format_mode_line,
11479 format_mode_line_unwind_data
11480 (f, current_buffer, selected_window, false));
11481
11482 Fselect_window (f->selected_window, Qt);
11483 set_buffer_internal_1
11484 (XBUFFER (XWINDOW (f->selected_window)->contents));
11485 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11486
11487 mode_line_target = MODE_LINE_TITLE;
11488 title_start = MODE_LINE_NOPROP_LEN (0);
11489 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11490 NULL, DEFAULT_FACE_ID);
11491 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11492 len = MODE_LINE_NOPROP_LEN (title_start);
11493 title = mode_line_noprop_buf + title_start;
11494 unbind_to (count, Qnil);
11495
11496 /* Set the title only if it's changed. This avoids consing in
11497 the common case where it hasn't. (If it turns out that we've
11498 already wasted too much time by walking through the list with
11499 display_mode_element, then we might need to optimize at a
11500 higher level than this.) */
11501 if (! STRINGP (f->name)
11502 || SBYTES (f->name) != len
11503 || memcmp (title, SDATA (f->name), len) != 0)
11504 x_implicitly_set_name (f, make_string (title, len), Qnil);
11505 }
11506 }
11507
11508 #endif /* not HAVE_WINDOW_SYSTEM */
11509
11510 \f
11511 /***********************************************************************
11512 Menu Bars
11513 ***********************************************************************/
11514
11515 /* True if we will not redisplay all visible windows. */
11516 #define REDISPLAY_SOME_P() \
11517 ((windows_or_buffers_changed == 0 \
11518 || windows_or_buffers_changed == REDISPLAY_SOME) \
11519 && (update_mode_lines == 0 \
11520 || update_mode_lines == REDISPLAY_SOME))
11521
11522 /* Prepare for redisplay by updating menu-bar item lists when
11523 appropriate. This can call eval. */
11524
11525 static void
11526 prepare_menu_bars (void)
11527 {
11528 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11529 bool some_windows = REDISPLAY_SOME_P ();
11530 struct gcpro gcpro1, gcpro2;
11531 Lisp_Object tooltip_frame;
11532
11533 #ifdef HAVE_WINDOW_SYSTEM
11534 tooltip_frame = tip_frame;
11535 #else
11536 tooltip_frame = Qnil;
11537 #endif
11538
11539 if (FUNCTIONP (Vpre_redisplay_function))
11540 {
11541 Lisp_Object windows = all_windows ? Qt : Qnil;
11542 if (all_windows && some_windows)
11543 {
11544 Lisp_Object ws = window_list ();
11545 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11546 {
11547 Lisp_Object this = XCAR (ws);
11548 struct window *w = XWINDOW (this);
11549 if (w->redisplay
11550 || XFRAME (w->frame)->redisplay
11551 || XBUFFER (w->contents)->text->redisplay)
11552 {
11553 windows = Fcons (this, windows);
11554 }
11555 }
11556 }
11557 safe__call1 (true, Vpre_redisplay_function, windows);
11558 }
11559
11560 /* Update all frame titles based on their buffer names, etc. We do
11561 this before the menu bars so that the buffer-menu will show the
11562 up-to-date frame titles. */
11563 #ifdef HAVE_WINDOW_SYSTEM
11564 if (all_windows)
11565 {
11566 Lisp_Object tail, frame;
11567
11568 FOR_EACH_FRAME (tail, frame)
11569 {
11570 struct frame *f = XFRAME (frame);
11571 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11572 if (some_windows
11573 && !f->redisplay
11574 && !w->redisplay
11575 && !XBUFFER (w->contents)->text->redisplay)
11576 continue;
11577
11578 if (!EQ (frame, tooltip_frame)
11579 && (FRAME_ICONIFIED_P (f)
11580 || FRAME_VISIBLE_P (f) == 1
11581 /* Exclude TTY frames that are obscured because they
11582 are not the top frame on their console. This is
11583 because x_consider_frame_title actually switches
11584 to the frame, which for TTY frames means it is
11585 marked as garbaged, and will be completely
11586 redrawn on the next redisplay cycle. This causes
11587 TTY frames to be completely redrawn, when there
11588 are more than one of them, even though nothing
11589 should be changed on display. */
11590 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11591 x_consider_frame_title (frame);
11592 }
11593 }
11594 #endif /* HAVE_WINDOW_SYSTEM */
11595
11596 /* Update the menu bar item lists, if appropriate. This has to be
11597 done before any actual redisplay or generation of display lines. */
11598
11599 if (all_windows)
11600 {
11601 Lisp_Object tail, frame;
11602 ptrdiff_t count = SPECPDL_INDEX ();
11603 /* True means that update_menu_bar has run its hooks
11604 so any further calls to update_menu_bar shouldn't do so again. */
11605 bool menu_bar_hooks_run = false;
11606
11607 record_unwind_save_match_data ();
11608
11609 FOR_EACH_FRAME (tail, frame)
11610 {
11611 struct frame *f = XFRAME (frame);
11612 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11613
11614 /* Ignore tooltip frame. */
11615 if (EQ (frame, tooltip_frame))
11616 continue;
11617
11618 if (some_windows
11619 && !f->redisplay
11620 && !w->redisplay
11621 && !XBUFFER (w->contents)->text->redisplay)
11622 continue;
11623
11624 /* If a window on this frame changed size, report that to
11625 the user and clear the size-change flag. */
11626 if (FRAME_WINDOW_SIZES_CHANGED (f))
11627 {
11628 Lisp_Object functions;
11629
11630 /* Clear flag first in case we get an error below. */
11631 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11632 functions = Vwindow_size_change_functions;
11633 GCPRO2 (tail, functions);
11634
11635 while (CONSP (functions))
11636 {
11637 if (!EQ (XCAR (functions), Qt))
11638 call1 (XCAR (functions), frame);
11639 functions = XCDR (functions);
11640 }
11641 UNGCPRO;
11642 }
11643
11644 GCPRO1 (tail);
11645 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11646 #ifdef HAVE_WINDOW_SYSTEM
11647 update_tool_bar (f, false);
11648 #endif
11649 UNGCPRO;
11650 }
11651
11652 unbind_to (count, Qnil);
11653 }
11654 else
11655 {
11656 struct frame *sf = SELECTED_FRAME ();
11657 update_menu_bar (sf, true, false);
11658 #ifdef HAVE_WINDOW_SYSTEM
11659 update_tool_bar (sf, true);
11660 #endif
11661 }
11662 }
11663
11664
11665 /* Update the menu bar item list for frame F. This has to be done
11666 before we start to fill in any display lines, because it can call
11667 eval.
11668
11669 If SAVE_MATCH_DATA, we must save and restore it here.
11670
11671 If HOOKS_RUN, a previous call to update_menu_bar
11672 already ran the menu bar hooks for this redisplay, so there
11673 is no need to run them again. The return value is the
11674 updated value of this flag, to pass to the next call. */
11675
11676 static bool
11677 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11678 {
11679 Lisp_Object window;
11680 struct window *w;
11681
11682 /* If called recursively during a menu update, do nothing. This can
11683 happen when, for instance, an activate-menubar-hook causes a
11684 redisplay. */
11685 if (inhibit_menubar_update)
11686 return hooks_run;
11687
11688 window = FRAME_SELECTED_WINDOW (f);
11689 w = XWINDOW (window);
11690
11691 if (FRAME_WINDOW_P (f)
11692 ?
11693 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11694 || defined (HAVE_NS) || defined (USE_GTK)
11695 FRAME_EXTERNAL_MENU_BAR (f)
11696 #else
11697 FRAME_MENU_BAR_LINES (f) > 0
11698 #endif
11699 : FRAME_MENU_BAR_LINES (f) > 0)
11700 {
11701 /* If the user has switched buffers or windows, we need to
11702 recompute to reflect the new bindings. But we'll
11703 recompute when update_mode_lines is set too; that means
11704 that people can use force-mode-line-update to request
11705 that the menu bar be recomputed. The adverse effect on
11706 the rest of the redisplay algorithm is about the same as
11707 windows_or_buffers_changed anyway. */
11708 if (windows_or_buffers_changed
11709 /* This used to test w->update_mode_line, but we believe
11710 there is no need to recompute the menu in that case. */
11711 || update_mode_lines
11712 || window_buffer_changed (w))
11713 {
11714 struct buffer *prev = current_buffer;
11715 ptrdiff_t count = SPECPDL_INDEX ();
11716
11717 specbind (Qinhibit_menubar_update, Qt);
11718
11719 set_buffer_internal_1 (XBUFFER (w->contents));
11720 if (save_match_data)
11721 record_unwind_save_match_data ();
11722 if (NILP (Voverriding_local_map_menu_flag))
11723 {
11724 specbind (Qoverriding_terminal_local_map, Qnil);
11725 specbind (Qoverriding_local_map, Qnil);
11726 }
11727
11728 if (!hooks_run)
11729 {
11730 /* Run the Lucid hook. */
11731 safe_run_hooks (Qactivate_menubar_hook);
11732
11733 /* If it has changed current-menubar from previous value,
11734 really recompute the menu-bar from the value. */
11735 if (! NILP (Vlucid_menu_bar_dirty_flag))
11736 call0 (Qrecompute_lucid_menubar);
11737
11738 safe_run_hooks (Qmenu_bar_update_hook);
11739
11740 hooks_run = true;
11741 }
11742
11743 XSETFRAME (Vmenu_updating_frame, f);
11744 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11745
11746 /* Redisplay the menu bar in case we changed it. */
11747 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11748 || defined (HAVE_NS) || defined (USE_GTK)
11749 if (FRAME_WINDOW_P (f))
11750 {
11751 #if defined (HAVE_NS)
11752 /* All frames on Mac OS share the same menubar. So only
11753 the selected frame should be allowed to set it. */
11754 if (f == SELECTED_FRAME ())
11755 #endif
11756 set_frame_menubar (f, false, false);
11757 }
11758 else
11759 /* On a terminal screen, the menu bar is an ordinary screen
11760 line, and this makes it get updated. */
11761 w->update_mode_line = true;
11762 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11763 /* In the non-toolkit version, the menu bar is an ordinary screen
11764 line, and this makes it get updated. */
11765 w->update_mode_line = true;
11766 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11767
11768 unbind_to (count, Qnil);
11769 set_buffer_internal_1 (prev);
11770 }
11771 }
11772
11773 return hooks_run;
11774 }
11775
11776 /***********************************************************************
11777 Tool-bars
11778 ***********************************************************************/
11779
11780 #ifdef HAVE_WINDOW_SYSTEM
11781
11782 /* Select `frame' temporarily without running all the code in
11783 do_switch_frame.
11784 FIXME: Maybe do_switch_frame should be trimmed down similarly
11785 when `norecord' is set. */
11786 static void
11787 fast_set_selected_frame (Lisp_Object frame)
11788 {
11789 if (!EQ (selected_frame, frame))
11790 {
11791 selected_frame = frame;
11792 selected_window = XFRAME (frame)->selected_window;
11793 }
11794 }
11795
11796 /* Update the tool-bar item list for frame F. This has to be done
11797 before we start to fill in any display lines. Called from
11798 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11799 and restore it here. */
11800
11801 static void
11802 update_tool_bar (struct frame *f, bool save_match_data)
11803 {
11804 #if defined (USE_GTK) || defined (HAVE_NS)
11805 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11806 #else
11807 bool do_update = (WINDOWP (f->tool_bar_window)
11808 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11809 #endif
11810
11811 if (do_update)
11812 {
11813 Lisp_Object window;
11814 struct window *w;
11815
11816 window = FRAME_SELECTED_WINDOW (f);
11817 w = XWINDOW (window);
11818
11819 /* If the user has switched buffers or windows, we need to
11820 recompute to reflect the new bindings. But we'll
11821 recompute when update_mode_lines is set too; that means
11822 that people can use force-mode-line-update to request
11823 that the menu bar be recomputed. The adverse effect on
11824 the rest of the redisplay algorithm is about the same as
11825 windows_or_buffers_changed anyway. */
11826 if (windows_or_buffers_changed
11827 || w->update_mode_line
11828 || update_mode_lines
11829 || window_buffer_changed (w))
11830 {
11831 struct buffer *prev = current_buffer;
11832 ptrdiff_t count = SPECPDL_INDEX ();
11833 Lisp_Object frame, new_tool_bar;
11834 int new_n_tool_bar;
11835 struct gcpro gcpro1;
11836
11837 /* Set current_buffer to the buffer of the selected
11838 window of the frame, so that we get the right local
11839 keymaps. */
11840 set_buffer_internal_1 (XBUFFER (w->contents));
11841
11842 /* Save match data, if we must. */
11843 if (save_match_data)
11844 record_unwind_save_match_data ();
11845
11846 /* Make sure that we don't accidentally use bogus keymaps. */
11847 if (NILP (Voverriding_local_map_menu_flag))
11848 {
11849 specbind (Qoverriding_terminal_local_map, Qnil);
11850 specbind (Qoverriding_local_map, Qnil);
11851 }
11852
11853 GCPRO1 (new_tool_bar);
11854
11855 /* We must temporarily set the selected frame to this frame
11856 before calling tool_bar_items, because the calculation of
11857 the tool-bar keymap uses the selected frame (see
11858 `tool-bar-make-keymap' in tool-bar.el). */
11859 eassert (EQ (selected_window,
11860 /* Since we only explicitly preserve selected_frame,
11861 check that selected_window would be redundant. */
11862 XFRAME (selected_frame)->selected_window));
11863 record_unwind_protect (fast_set_selected_frame, selected_frame);
11864 XSETFRAME (frame, f);
11865 fast_set_selected_frame (frame);
11866
11867 /* Build desired tool-bar items from keymaps. */
11868 new_tool_bar
11869 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11870 &new_n_tool_bar);
11871
11872 /* Redisplay the tool-bar if we changed it. */
11873 if (new_n_tool_bar != f->n_tool_bar_items
11874 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11875 {
11876 /* Redisplay that happens asynchronously due to an expose event
11877 may access f->tool_bar_items. Make sure we update both
11878 variables within BLOCK_INPUT so no such event interrupts. */
11879 block_input ();
11880 fset_tool_bar_items (f, new_tool_bar);
11881 f->n_tool_bar_items = new_n_tool_bar;
11882 w->update_mode_line = true;
11883 unblock_input ();
11884 }
11885
11886 UNGCPRO;
11887
11888 unbind_to (count, Qnil);
11889 set_buffer_internal_1 (prev);
11890 }
11891 }
11892 }
11893
11894 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11895
11896 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11897 F's desired tool-bar contents. F->tool_bar_items must have
11898 been set up previously by calling prepare_menu_bars. */
11899
11900 static void
11901 build_desired_tool_bar_string (struct frame *f)
11902 {
11903 int i, size, size_needed;
11904 struct gcpro gcpro1, gcpro2;
11905 Lisp_Object image, plist;
11906
11907 image = plist = Qnil;
11908 GCPRO2 (image, plist);
11909
11910 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11911 Otherwise, make a new string. */
11912
11913 /* The size of the string we might be able to reuse. */
11914 size = (STRINGP (f->desired_tool_bar_string)
11915 ? SCHARS (f->desired_tool_bar_string)
11916 : 0);
11917
11918 /* We need one space in the string for each image. */
11919 size_needed = f->n_tool_bar_items;
11920
11921 /* Reuse f->desired_tool_bar_string, if possible. */
11922 if (size < size_needed || NILP (f->desired_tool_bar_string))
11923 fset_desired_tool_bar_string
11924 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11925 else
11926 {
11927 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11928 struct gcpro gcpro1;
11929 GCPRO1 (props);
11930 Fremove_text_properties (make_number (0), make_number (size),
11931 props, f->desired_tool_bar_string);
11932 UNGCPRO;
11933 }
11934
11935 /* Put a `display' property on the string for the images to display,
11936 put a `menu_item' property on tool-bar items with a value that
11937 is the index of the item in F's tool-bar item vector. */
11938 for (i = 0; i < f->n_tool_bar_items; ++i)
11939 {
11940 #define PROP(IDX) \
11941 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11942
11943 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11944 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11945 int hmargin, vmargin, relief, idx, end;
11946
11947 /* If image is a vector, choose the image according to the
11948 button state. */
11949 image = PROP (TOOL_BAR_ITEM_IMAGES);
11950 if (VECTORP (image))
11951 {
11952 if (enabled_p)
11953 idx = (selected_p
11954 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11955 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11956 else
11957 idx = (selected_p
11958 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11959 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11960
11961 eassert (ASIZE (image) >= idx);
11962 image = AREF (image, idx);
11963 }
11964 else
11965 idx = -1;
11966
11967 /* Ignore invalid image specifications. */
11968 if (!valid_image_p (image))
11969 continue;
11970
11971 /* Display the tool-bar button pressed, or depressed. */
11972 plist = Fcopy_sequence (XCDR (image));
11973
11974 /* Compute margin and relief to draw. */
11975 relief = (tool_bar_button_relief >= 0
11976 ? tool_bar_button_relief
11977 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11978 hmargin = vmargin = relief;
11979
11980 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11981 INT_MAX - max (hmargin, vmargin)))
11982 {
11983 hmargin += XFASTINT (Vtool_bar_button_margin);
11984 vmargin += XFASTINT (Vtool_bar_button_margin);
11985 }
11986 else if (CONSP (Vtool_bar_button_margin))
11987 {
11988 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11989 INT_MAX - hmargin))
11990 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11991
11992 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11993 INT_MAX - vmargin))
11994 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11995 }
11996
11997 if (auto_raise_tool_bar_buttons_p)
11998 {
11999 /* Add a `:relief' property to the image spec if the item is
12000 selected. */
12001 if (selected_p)
12002 {
12003 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12004 hmargin -= relief;
12005 vmargin -= relief;
12006 }
12007 }
12008 else
12009 {
12010 /* If image is selected, display it pressed, i.e. with a
12011 negative relief. If it's not selected, display it with a
12012 raised relief. */
12013 plist = Fplist_put (plist, QCrelief,
12014 (selected_p
12015 ? make_number (-relief)
12016 : make_number (relief)));
12017 hmargin -= relief;
12018 vmargin -= relief;
12019 }
12020
12021 /* Put a margin around the image. */
12022 if (hmargin || vmargin)
12023 {
12024 if (hmargin == vmargin)
12025 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12026 else
12027 plist = Fplist_put (plist, QCmargin,
12028 Fcons (make_number (hmargin),
12029 make_number (vmargin)));
12030 }
12031
12032 /* If button is not enabled, and we don't have special images
12033 for the disabled state, make the image appear disabled by
12034 applying an appropriate algorithm to it. */
12035 if (!enabled_p && idx < 0)
12036 plist = Fplist_put (plist, QCconversion, Qdisabled);
12037
12038 /* Put a `display' text property on the string for the image to
12039 display. Put a `menu-item' property on the string that gives
12040 the start of this item's properties in the tool-bar items
12041 vector. */
12042 image = Fcons (Qimage, plist);
12043 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12044 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12045 struct gcpro gcpro1;
12046 GCPRO1 (props);
12047
12048 /* Let the last image hide all remaining spaces in the tool bar
12049 string. The string can be longer than needed when we reuse a
12050 previous string. */
12051 if (i + 1 == f->n_tool_bar_items)
12052 end = SCHARS (f->desired_tool_bar_string);
12053 else
12054 end = i + 1;
12055 Fadd_text_properties (make_number (i), make_number (end),
12056 props, f->desired_tool_bar_string);
12057 UNGCPRO;
12058 #undef PROP
12059 }
12060
12061 UNGCPRO;
12062 }
12063
12064
12065 /* Display one line of the tool-bar of frame IT->f.
12066
12067 HEIGHT specifies the desired height of the tool-bar line.
12068 If the actual height of the glyph row is less than HEIGHT, the
12069 row's height is increased to HEIGHT, and the icons are centered
12070 vertically in the new height.
12071
12072 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12073 count a final empty row in case the tool-bar width exactly matches
12074 the window width.
12075 */
12076
12077 static void
12078 display_tool_bar_line (struct it *it, int height)
12079 {
12080 struct glyph_row *row = it->glyph_row;
12081 int max_x = it->last_visible_x;
12082 struct glyph *last;
12083
12084 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12085 clear_glyph_row (row);
12086 row->enabled_p = true;
12087 row->y = it->current_y;
12088
12089 /* Note that this isn't made use of if the face hasn't a box,
12090 so there's no need to check the face here. */
12091 it->start_of_box_run_p = true;
12092
12093 while (it->current_x < max_x)
12094 {
12095 int x, n_glyphs_before, i, nglyphs;
12096 struct it it_before;
12097
12098 /* Get the next display element. */
12099 if (!get_next_display_element (it))
12100 {
12101 /* Don't count empty row if we are counting needed tool-bar lines. */
12102 if (height < 0 && !it->hpos)
12103 return;
12104 break;
12105 }
12106
12107 /* Produce glyphs. */
12108 n_glyphs_before = row->used[TEXT_AREA];
12109 it_before = *it;
12110
12111 PRODUCE_GLYPHS (it);
12112
12113 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12114 i = 0;
12115 x = it_before.current_x;
12116 while (i < nglyphs)
12117 {
12118 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12119
12120 if (x + glyph->pixel_width > max_x)
12121 {
12122 /* Glyph doesn't fit on line. Backtrack. */
12123 row->used[TEXT_AREA] = n_glyphs_before;
12124 *it = it_before;
12125 /* If this is the only glyph on this line, it will never fit on the
12126 tool-bar, so skip it. But ensure there is at least one glyph,
12127 so we don't accidentally disable the tool-bar. */
12128 if (n_glyphs_before == 0
12129 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12130 break;
12131 goto out;
12132 }
12133
12134 ++it->hpos;
12135 x += glyph->pixel_width;
12136 ++i;
12137 }
12138
12139 /* Stop at line end. */
12140 if (ITERATOR_AT_END_OF_LINE_P (it))
12141 break;
12142
12143 set_iterator_to_next (it, true);
12144 }
12145
12146 out:;
12147
12148 row->displays_text_p = row->used[TEXT_AREA] != 0;
12149
12150 /* Use default face for the border below the tool bar.
12151
12152 FIXME: When auto-resize-tool-bars is grow-only, there is
12153 no additional border below the possibly empty tool-bar lines.
12154 So to make the extra empty lines look "normal", we have to
12155 use the tool-bar face for the border too. */
12156 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12157 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12158 it->face_id = DEFAULT_FACE_ID;
12159
12160 extend_face_to_end_of_line (it);
12161 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12162 last->right_box_line_p = true;
12163 if (last == row->glyphs[TEXT_AREA])
12164 last->left_box_line_p = true;
12165
12166 /* Make line the desired height and center it vertically. */
12167 if ((height -= it->max_ascent + it->max_descent) > 0)
12168 {
12169 /* Don't add more than one line height. */
12170 height %= FRAME_LINE_HEIGHT (it->f);
12171 it->max_ascent += height / 2;
12172 it->max_descent += (height + 1) / 2;
12173 }
12174
12175 compute_line_metrics (it);
12176
12177 /* If line is empty, make it occupy the rest of the tool-bar. */
12178 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12179 {
12180 row->height = row->phys_height = it->last_visible_y - row->y;
12181 row->visible_height = row->height;
12182 row->ascent = row->phys_ascent = 0;
12183 row->extra_line_spacing = 0;
12184 }
12185
12186 row->full_width_p = true;
12187 row->continued_p = false;
12188 row->truncated_on_left_p = false;
12189 row->truncated_on_right_p = false;
12190
12191 it->current_x = it->hpos = 0;
12192 it->current_y += row->height;
12193 ++it->vpos;
12194 ++it->glyph_row;
12195 }
12196
12197
12198 /* Value is the number of pixels needed to make all tool-bar items of
12199 frame F visible. The actual number of glyph rows needed is
12200 returned in *N_ROWS if non-NULL. */
12201 static int
12202 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12203 {
12204 struct window *w = XWINDOW (f->tool_bar_window);
12205 struct it it;
12206 /* tool_bar_height is called from redisplay_tool_bar after building
12207 the desired matrix, so use (unused) mode-line row as temporary row to
12208 avoid destroying the first tool-bar row. */
12209 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12210
12211 /* Initialize an iterator for iteration over
12212 F->desired_tool_bar_string in the tool-bar window of frame F. */
12213 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12214 temp_row->reversed_p = false;
12215 it.first_visible_x = 0;
12216 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12217 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12218 it.paragraph_embedding = L2R;
12219
12220 while (!ITERATOR_AT_END_P (&it))
12221 {
12222 clear_glyph_row (temp_row);
12223 it.glyph_row = temp_row;
12224 display_tool_bar_line (&it, -1);
12225 }
12226 clear_glyph_row (temp_row);
12227
12228 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12229 if (n_rows)
12230 *n_rows = it.vpos > 0 ? it.vpos : -1;
12231
12232 if (pixelwise)
12233 return it.current_y;
12234 else
12235 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12236 }
12237
12238 #endif /* !USE_GTK && !HAVE_NS */
12239
12240 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12241 0, 2, 0,
12242 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12243 If FRAME is nil or omitted, use the selected frame. Optional argument
12244 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12245 (Lisp_Object frame, Lisp_Object pixelwise)
12246 {
12247 int height = 0;
12248
12249 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12250 struct frame *f = decode_any_frame (frame);
12251
12252 if (WINDOWP (f->tool_bar_window)
12253 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12254 {
12255 update_tool_bar (f, true);
12256 if (f->n_tool_bar_items)
12257 {
12258 build_desired_tool_bar_string (f);
12259 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12260 }
12261 }
12262 #endif
12263
12264 return make_number (height);
12265 }
12266
12267
12268 /* Display the tool-bar of frame F. Value is true if tool-bar's
12269 height should be changed. */
12270 static bool
12271 redisplay_tool_bar (struct frame *f)
12272 {
12273 #if defined (USE_GTK) || defined (HAVE_NS)
12274
12275 if (FRAME_EXTERNAL_TOOL_BAR (f))
12276 update_frame_tool_bar (f);
12277 return false;
12278
12279 #else /* !USE_GTK && !HAVE_NS */
12280
12281 struct window *w;
12282 struct it it;
12283 struct glyph_row *row;
12284
12285 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12286 do anything. This means you must start with tool-bar-lines
12287 non-zero to get the auto-sizing effect. Or in other words, you
12288 can turn off tool-bars by specifying tool-bar-lines zero. */
12289 if (!WINDOWP (f->tool_bar_window)
12290 || (w = XWINDOW (f->tool_bar_window),
12291 WINDOW_TOTAL_LINES (w) == 0))
12292 return false;
12293
12294 /* Set up an iterator for the tool-bar window. */
12295 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12296 it.first_visible_x = 0;
12297 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12298 row = it.glyph_row;
12299 row->reversed_p = false;
12300
12301 /* Build a string that represents the contents of the tool-bar. */
12302 build_desired_tool_bar_string (f);
12303 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12304 /* FIXME: This should be controlled by a user option. But it
12305 doesn't make sense to have an R2L tool bar if the menu bar cannot
12306 be drawn also R2L, and making the menu bar R2L is tricky due
12307 toolkit-specific code that implements it. If an R2L tool bar is
12308 ever supported, display_tool_bar_line should also be augmented to
12309 call unproduce_glyphs like display_line and display_string
12310 do. */
12311 it.paragraph_embedding = L2R;
12312
12313 if (f->n_tool_bar_rows == 0)
12314 {
12315 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12316
12317 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12318 {
12319 x_change_tool_bar_height (f, new_height);
12320 frame_default_tool_bar_height = new_height;
12321 /* Always do that now. */
12322 clear_glyph_matrix (w->desired_matrix);
12323 f->fonts_changed = true;
12324 return true;
12325 }
12326 }
12327
12328 /* Display as many lines as needed to display all tool-bar items. */
12329
12330 if (f->n_tool_bar_rows > 0)
12331 {
12332 int border, rows, height, extra;
12333
12334 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12335 border = XINT (Vtool_bar_border);
12336 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12337 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12338 else if (EQ (Vtool_bar_border, Qborder_width))
12339 border = f->border_width;
12340 else
12341 border = 0;
12342 if (border < 0)
12343 border = 0;
12344
12345 rows = f->n_tool_bar_rows;
12346 height = max (1, (it.last_visible_y - border) / rows);
12347 extra = it.last_visible_y - border - height * rows;
12348
12349 while (it.current_y < it.last_visible_y)
12350 {
12351 int h = 0;
12352 if (extra > 0 && rows-- > 0)
12353 {
12354 h = (extra + rows - 1) / rows;
12355 extra -= h;
12356 }
12357 display_tool_bar_line (&it, height + h);
12358 }
12359 }
12360 else
12361 {
12362 while (it.current_y < it.last_visible_y)
12363 display_tool_bar_line (&it, 0);
12364 }
12365
12366 /* It doesn't make much sense to try scrolling in the tool-bar
12367 window, so don't do it. */
12368 w->desired_matrix->no_scrolling_p = true;
12369 w->must_be_updated_p = true;
12370
12371 if (!NILP (Vauto_resize_tool_bars))
12372 {
12373 bool change_height_p = true;
12374
12375 /* If we couldn't display everything, change the tool-bar's
12376 height if there is room for more. */
12377 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12378 change_height_p = true;
12379
12380 /* We subtract 1 because display_tool_bar_line advances the
12381 glyph_row pointer before returning to its caller. We want to
12382 examine the last glyph row produced by
12383 display_tool_bar_line. */
12384 row = it.glyph_row - 1;
12385
12386 /* If there are blank lines at the end, except for a partially
12387 visible blank line at the end that is smaller than
12388 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12389 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12390 && row->height >= FRAME_LINE_HEIGHT (f))
12391 change_height_p = true;
12392
12393 /* If row displays tool-bar items, but is partially visible,
12394 change the tool-bar's height. */
12395 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12396 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12397 change_height_p = true;
12398
12399 /* Resize windows as needed by changing the `tool-bar-lines'
12400 frame parameter. */
12401 if (change_height_p)
12402 {
12403 int nrows;
12404 int new_height = tool_bar_height (f, &nrows, true);
12405
12406 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12407 && !f->minimize_tool_bar_window_p)
12408 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12409 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12410 f->minimize_tool_bar_window_p = false;
12411
12412 if (change_height_p)
12413 {
12414 x_change_tool_bar_height (f, new_height);
12415 frame_default_tool_bar_height = new_height;
12416 clear_glyph_matrix (w->desired_matrix);
12417 f->n_tool_bar_rows = nrows;
12418 f->fonts_changed = true;
12419
12420 return true;
12421 }
12422 }
12423 }
12424
12425 f->minimize_tool_bar_window_p = false;
12426 return false;
12427
12428 #endif /* USE_GTK || HAVE_NS */
12429 }
12430
12431 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12432
12433 /* Get information about the tool-bar item which is displayed in GLYPH
12434 on frame F. Return in *PROP_IDX the index where tool-bar item
12435 properties start in F->tool_bar_items. Value is false if
12436 GLYPH doesn't display a tool-bar item. */
12437
12438 static bool
12439 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12440 {
12441 Lisp_Object prop;
12442 int charpos;
12443
12444 /* This function can be called asynchronously, which means we must
12445 exclude any possibility that Fget_text_property signals an
12446 error. */
12447 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12448 charpos = max (0, charpos);
12449
12450 /* Get the text property `menu-item' at pos. The value of that
12451 property is the start index of this item's properties in
12452 F->tool_bar_items. */
12453 prop = Fget_text_property (make_number (charpos),
12454 Qmenu_item, f->current_tool_bar_string);
12455 if (! INTEGERP (prop))
12456 return false;
12457 *prop_idx = XINT (prop);
12458 return true;
12459 }
12460
12461 \f
12462 /* Get information about the tool-bar item at position X/Y on frame F.
12463 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12464 the current matrix of the tool-bar window of F, or NULL if not
12465 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12466 item in F->tool_bar_items. Value is
12467
12468 -1 if X/Y is not on a tool-bar item
12469 0 if X/Y is on the same item that was highlighted before.
12470 1 otherwise. */
12471
12472 static int
12473 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12474 int *hpos, int *vpos, int *prop_idx)
12475 {
12476 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12477 struct window *w = XWINDOW (f->tool_bar_window);
12478 int area;
12479
12480 /* Find the glyph under X/Y. */
12481 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12482 if (*glyph == NULL)
12483 return -1;
12484
12485 /* Get the start of this tool-bar item's properties in
12486 f->tool_bar_items. */
12487 if (!tool_bar_item_info (f, *glyph, prop_idx))
12488 return -1;
12489
12490 /* Is mouse on the highlighted item? */
12491 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12492 && *vpos >= hlinfo->mouse_face_beg_row
12493 && *vpos <= hlinfo->mouse_face_end_row
12494 && (*vpos > hlinfo->mouse_face_beg_row
12495 || *hpos >= hlinfo->mouse_face_beg_col)
12496 && (*vpos < hlinfo->mouse_face_end_row
12497 || *hpos < hlinfo->mouse_face_end_col
12498 || hlinfo->mouse_face_past_end))
12499 return 0;
12500
12501 return 1;
12502 }
12503
12504
12505 /* EXPORT:
12506 Handle mouse button event on the tool-bar of frame F, at
12507 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12508 false for button release. MODIFIERS is event modifiers for button
12509 release. */
12510
12511 void
12512 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12513 int modifiers)
12514 {
12515 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12516 struct window *w = XWINDOW (f->tool_bar_window);
12517 int hpos, vpos, prop_idx;
12518 struct glyph *glyph;
12519 Lisp_Object enabled_p;
12520 int ts;
12521
12522 /* If not on the highlighted tool-bar item, and mouse-highlight is
12523 non-nil, return. This is so we generate the tool-bar button
12524 click only when the mouse button is released on the same item as
12525 where it was pressed. However, when mouse-highlight is disabled,
12526 generate the click when the button is released regardless of the
12527 highlight, since tool-bar items are not highlighted in that
12528 case. */
12529 frame_to_window_pixel_xy (w, &x, &y);
12530 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12531 if (ts == -1
12532 || (ts != 0 && !NILP (Vmouse_highlight)))
12533 return;
12534
12535 /* When mouse-highlight is off, generate the click for the item
12536 where the button was pressed, disregarding where it was
12537 released. */
12538 if (NILP (Vmouse_highlight) && !down_p)
12539 prop_idx = f->last_tool_bar_item;
12540
12541 /* If item is disabled, do nothing. */
12542 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12543 if (NILP (enabled_p))
12544 return;
12545
12546 if (down_p)
12547 {
12548 /* Show item in pressed state. */
12549 if (!NILP (Vmouse_highlight))
12550 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12551 f->last_tool_bar_item = prop_idx;
12552 }
12553 else
12554 {
12555 Lisp_Object key, frame;
12556 struct input_event event;
12557 EVENT_INIT (event);
12558
12559 /* Show item in released state. */
12560 if (!NILP (Vmouse_highlight))
12561 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12562
12563 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12564
12565 XSETFRAME (frame, f);
12566 event.kind = TOOL_BAR_EVENT;
12567 event.frame_or_window = frame;
12568 event.arg = frame;
12569 kbd_buffer_store_event (&event);
12570
12571 event.kind = TOOL_BAR_EVENT;
12572 event.frame_or_window = frame;
12573 event.arg = key;
12574 event.modifiers = modifiers;
12575 kbd_buffer_store_event (&event);
12576 f->last_tool_bar_item = -1;
12577 }
12578 }
12579
12580
12581 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12582 tool-bar window-relative coordinates X/Y. Called from
12583 note_mouse_highlight. */
12584
12585 static void
12586 note_tool_bar_highlight (struct frame *f, int x, int y)
12587 {
12588 Lisp_Object window = f->tool_bar_window;
12589 struct window *w = XWINDOW (window);
12590 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12591 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12592 int hpos, vpos;
12593 struct glyph *glyph;
12594 struct glyph_row *row;
12595 int i;
12596 Lisp_Object enabled_p;
12597 int prop_idx;
12598 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12599 bool mouse_down_p;
12600 int rc;
12601
12602 /* Function note_mouse_highlight is called with negative X/Y
12603 values when mouse moves outside of the frame. */
12604 if (x <= 0 || y <= 0)
12605 {
12606 clear_mouse_face (hlinfo);
12607 return;
12608 }
12609
12610 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12611 if (rc < 0)
12612 {
12613 /* Not on tool-bar item. */
12614 clear_mouse_face (hlinfo);
12615 return;
12616 }
12617 else if (rc == 0)
12618 /* On same tool-bar item as before. */
12619 goto set_help_echo;
12620
12621 clear_mouse_face (hlinfo);
12622
12623 /* Mouse is down, but on different tool-bar item? */
12624 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12625 && f == dpyinfo->last_mouse_frame);
12626
12627 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12628 return;
12629
12630 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12631
12632 /* If tool-bar item is not enabled, don't highlight it. */
12633 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12634 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12635 {
12636 /* Compute the x-position of the glyph. In front and past the
12637 image is a space. We include this in the highlighted area. */
12638 row = MATRIX_ROW (w->current_matrix, vpos);
12639 for (i = x = 0; i < hpos; ++i)
12640 x += row->glyphs[TEXT_AREA][i].pixel_width;
12641
12642 /* Record this as the current active region. */
12643 hlinfo->mouse_face_beg_col = hpos;
12644 hlinfo->mouse_face_beg_row = vpos;
12645 hlinfo->mouse_face_beg_x = x;
12646 hlinfo->mouse_face_past_end = false;
12647
12648 hlinfo->mouse_face_end_col = hpos + 1;
12649 hlinfo->mouse_face_end_row = vpos;
12650 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12651 hlinfo->mouse_face_window = window;
12652 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12653
12654 /* Display it as active. */
12655 show_mouse_face (hlinfo, draw);
12656 }
12657
12658 set_help_echo:
12659
12660 /* Set help_echo_string to a help string to display for this tool-bar item.
12661 XTread_socket does the rest. */
12662 help_echo_object = help_echo_window = Qnil;
12663 help_echo_pos = -1;
12664 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12665 if (NILP (help_echo_string))
12666 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12667 }
12668
12669 #endif /* !USE_GTK && !HAVE_NS */
12670
12671 #endif /* HAVE_WINDOW_SYSTEM */
12672
12673
12674 \f
12675 /************************************************************************
12676 Horizontal scrolling
12677 ************************************************************************/
12678
12679 /* For all leaf windows in the window tree rooted at WINDOW, set their
12680 hscroll value so that PT is (i) visible in the window, and (ii) so
12681 that it is not within a certain margin at the window's left and
12682 right border. Value is true if any window's hscroll has been
12683 changed. */
12684
12685 static bool
12686 hscroll_window_tree (Lisp_Object window)
12687 {
12688 bool hscrolled_p = false;
12689 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12690 int hscroll_step_abs = 0;
12691 double hscroll_step_rel = 0;
12692
12693 if (hscroll_relative_p)
12694 {
12695 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12696 if (hscroll_step_rel < 0)
12697 {
12698 hscroll_relative_p = false;
12699 hscroll_step_abs = 0;
12700 }
12701 }
12702 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12703 {
12704 hscroll_step_abs = XINT (Vhscroll_step);
12705 if (hscroll_step_abs < 0)
12706 hscroll_step_abs = 0;
12707 }
12708 else
12709 hscroll_step_abs = 0;
12710
12711 while (WINDOWP (window))
12712 {
12713 struct window *w = XWINDOW (window);
12714
12715 if (WINDOWP (w->contents))
12716 hscrolled_p |= hscroll_window_tree (w->contents);
12717 else if (w->cursor.vpos >= 0)
12718 {
12719 int h_margin;
12720 int text_area_width;
12721 struct glyph_row *cursor_row;
12722 struct glyph_row *bottom_row;
12723
12724 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12725 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12726 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12727 else
12728 cursor_row = bottom_row - 1;
12729
12730 if (!cursor_row->enabled_p)
12731 {
12732 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12733 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12734 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12735 else
12736 cursor_row = bottom_row - 1;
12737 }
12738 bool row_r2l_p = cursor_row->reversed_p;
12739
12740 text_area_width = window_box_width (w, TEXT_AREA);
12741
12742 /* Scroll when cursor is inside this scroll margin. */
12743 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12744
12745 /* If the position of this window's point has explicitly
12746 changed, no more suspend auto hscrolling. */
12747 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12748 w->suspend_auto_hscroll = false;
12749
12750 /* Remember window point. */
12751 Fset_marker (w->old_pointm,
12752 ((w == XWINDOW (selected_window))
12753 ? make_number (BUF_PT (XBUFFER (w->contents)))
12754 : Fmarker_position (w->pointm)),
12755 w->contents);
12756
12757 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12758 && !w->suspend_auto_hscroll
12759 /* In some pathological cases, like restoring a window
12760 configuration into a frame that is much smaller than
12761 the one from which the configuration was saved, we
12762 get glyph rows whose start and end have zero buffer
12763 positions, which we cannot handle below. Just skip
12764 such windows. */
12765 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12766 /* For left-to-right rows, hscroll when cursor is either
12767 (i) inside the right hscroll margin, or (ii) if it is
12768 inside the left margin and the window is already
12769 hscrolled. */
12770 && ((!row_r2l_p
12771 && ((w->hscroll && w->cursor.x <= h_margin)
12772 || (cursor_row->enabled_p
12773 && cursor_row->truncated_on_right_p
12774 && (w->cursor.x >= text_area_width - h_margin))))
12775 /* For right-to-left rows, the logic is similar,
12776 except that rules for scrolling to left and right
12777 are reversed. E.g., if cursor.x <= h_margin, we
12778 need to hscroll "to the right" unconditionally,
12779 and that will scroll the screen to the left so as
12780 to reveal the next portion of the row. */
12781 || (row_r2l_p
12782 && ((cursor_row->enabled_p
12783 /* FIXME: It is confusing to set the
12784 truncated_on_right_p flag when R2L rows
12785 are actually truncated on the left. */
12786 && cursor_row->truncated_on_right_p
12787 && w->cursor.x <= h_margin)
12788 || (w->hscroll
12789 && (w->cursor.x >= text_area_width - h_margin))))))
12790 {
12791 struct it it;
12792 ptrdiff_t hscroll;
12793 struct buffer *saved_current_buffer;
12794 ptrdiff_t pt;
12795 int wanted_x;
12796
12797 /* Find point in a display of infinite width. */
12798 saved_current_buffer = current_buffer;
12799 current_buffer = XBUFFER (w->contents);
12800
12801 if (w == XWINDOW (selected_window))
12802 pt = PT;
12803 else
12804 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12805
12806 /* Move iterator to pt starting at cursor_row->start in
12807 a line with infinite width. */
12808 init_to_row_start (&it, w, cursor_row);
12809 it.last_visible_x = INFINITY;
12810 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12811 current_buffer = saved_current_buffer;
12812
12813 /* Position cursor in window. */
12814 if (!hscroll_relative_p && hscroll_step_abs == 0)
12815 hscroll = max (0, (it.current_x
12816 - (ITERATOR_AT_END_OF_LINE_P (&it)
12817 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12818 : (text_area_width / 2))))
12819 / FRAME_COLUMN_WIDTH (it.f);
12820 else if ((!row_r2l_p
12821 && w->cursor.x >= text_area_width - h_margin)
12822 || (row_r2l_p && w->cursor.x <= h_margin))
12823 {
12824 if (hscroll_relative_p)
12825 wanted_x = text_area_width * (1 - hscroll_step_rel)
12826 - h_margin;
12827 else
12828 wanted_x = text_area_width
12829 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12830 - h_margin;
12831 hscroll
12832 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12833 }
12834 else
12835 {
12836 if (hscroll_relative_p)
12837 wanted_x = text_area_width * hscroll_step_rel
12838 + h_margin;
12839 else
12840 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12841 + h_margin;
12842 hscroll
12843 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12844 }
12845 hscroll = max (hscroll, w->min_hscroll);
12846
12847 /* Don't prevent redisplay optimizations if hscroll
12848 hasn't changed, as it will unnecessarily slow down
12849 redisplay. */
12850 if (w->hscroll != hscroll)
12851 {
12852 struct buffer *b = XBUFFER (w->contents);
12853 b->prevent_redisplay_optimizations_p = true;
12854 w->hscroll = hscroll;
12855 hscrolled_p = true;
12856 }
12857 }
12858 }
12859
12860 window = w->next;
12861 }
12862
12863 /* Value is true if hscroll of any leaf window has been changed. */
12864 return hscrolled_p;
12865 }
12866
12867
12868 /* Set hscroll so that cursor is visible and not inside horizontal
12869 scroll margins for all windows in the tree rooted at WINDOW. See
12870 also hscroll_window_tree above. Value is true if any window's
12871 hscroll has been changed. If it has, desired matrices on the frame
12872 of WINDOW are cleared. */
12873
12874 static bool
12875 hscroll_windows (Lisp_Object window)
12876 {
12877 bool hscrolled_p = hscroll_window_tree (window);
12878 if (hscrolled_p)
12879 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12880 return hscrolled_p;
12881 }
12882
12883
12884 \f
12885 /************************************************************************
12886 Redisplay
12887 ************************************************************************/
12888
12889 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12890 This is sometimes handy to have in a debugger session. */
12891
12892 #ifdef GLYPH_DEBUG
12893
12894 /* First and last unchanged row for try_window_id. */
12895
12896 static int debug_first_unchanged_at_end_vpos;
12897 static int debug_last_unchanged_at_beg_vpos;
12898
12899 /* Delta vpos and y. */
12900
12901 static int debug_dvpos, debug_dy;
12902
12903 /* Delta in characters and bytes for try_window_id. */
12904
12905 static ptrdiff_t debug_delta, debug_delta_bytes;
12906
12907 /* Values of window_end_pos and window_end_vpos at the end of
12908 try_window_id. */
12909
12910 static ptrdiff_t debug_end_vpos;
12911
12912 /* Append a string to W->desired_matrix->method. FMT is a printf
12913 format string. If trace_redisplay_p is true also printf the
12914 resulting string to stderr. */
12915
12916 static void debug_method_add (struct window *, char const *, ...)
12917 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12918
12919 static void
12920 debug_method_add (struct window *w, char const *fmt, ...)
12921 {
12922 void *ptr = w;
12923 char *method = w->desired_matrix->method;
12924 int len = strlen (method);
12925 int size = sizeof w->desired_matrix->method;
12926 int remaining = size - len - 1;
12927 va_list ap;
12928
12929 if (len && remaining)
12930 {
12931 method[len] = '|';
12932 --remaining, ++len;
12933 }
12934
12935 va_start (ap, fmt);
12936 vsnprintf (method + len, remaining + 1, fmt, ap);
12937 va_end (ap);
12938
12939 if (trace_redisplay_p)
12940 fprintf (stderr, "%p (%s): %s\n",
12941 ptr,
12942 ((BUFFERP (w->contents)
12943 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12944 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12945 : "no buffer"),
12946 method + len);
12947 }
12948
12949 #endif /* GLYPH_DEBUG */
12950
12951
12952 /* Value is true if all changes in window W, which displays
12953 current_buffer, are in the text between START and END. START is a
12954 buffer position, END is given as a distance from Z. Used in
12955 redisplay_internal for display optimization. */
12956
12957 static bool
12958 text_outside_line_unchanged_p (struct window *w,
12959 ptrdiff_t start, ptrdiff_t end)
12960 {
12961 bool unchanged_p = true;
12962
12963 /* If text or overlays have changed, see where. */
12964 if (window_outdated (w))
12965 {
12966 /* Gap in the line? */
12967 if (GPT < start || Z - GPT < end)
12968 unchanged_p = false;
12969
12970 /* Changes start in front of the line, or end after it? */
12971 if (unchanged_p
12972 && (BEG_UNCHANGED < start - 1
12973 || END_UNCHANGED < end))
12974 unchanged_p = false;
12975
12976 /* If selective display, can't optimize if changes start at the
12977 beginning of the line. */
12978 if (unchanged_p
12979 && INTEGERP (BVAR (current_buffer, selective_display))
12980 && XINT (BVAR (current_buffer, selective_display)) > 0
12981 && (BEG_UNCHANGED < start || GPT <= start))
12982 unchanged_p = false;
12983
12984 /* If there are overlays at the start or end of the line, these
12985 may have overlay strings with newlines in them. A change at
12986 START, for instance, may actually concern the display of such
12987 overlay strings as well, and they are displayed on different
12988 lines. So, quickly rule out this case. (For the future, it
12989 might be desirable to implement something more telling than
12990 just BEG/END_UNCHANGED.) */
12991 if (unchanged_p)
12992 {
12993 if (BEG + BEG_UNCHANGED == start
12994 && overlay_touches_p (start))
12995 unchanged_p = false;
12996 if (END_UNCHANGED == end
12997 && overlay_touches_p (Z - end))
12998 unchanged_p = false;
12999 }
13000
13001 /* Under bidi reordering, adding or deleting a character in the
13002 beginning of a paragraph, before the first strong directional
13003 character, can change the base direction of the paragraph (unless
13004 the buffer specifies a fixed paragraph direction), which will
13005 require to redisplay the whole paragraph. It might be worthwhile
13006 to find the paragraph limits and widen the range of redisplayed
13007 lines to that, but for now just give up this optimization. */
13008 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13009 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13010 unchanged_p = false;
13011 }
13012
13013 return unchanged_p;
13014 }
13015
13016
13017 /* Do a frame update, taking possible shortcuts into account. This is
13018 the main external entry point for redisplay.
13019
13020 If the last redisplay displayed an echo area message and that message
13021 is no longer requested, we clear the echo area or bring back the
13022 mini-buffer if that is in use. */
13023
13024 void
13025 redisplay (void)
13026 {
13027 redisplay_internal ();
13028 }
13029
13030
13031 static Lisp_Object
13032 overlay_arrow_string_or_property (Lisp_Object var)
13033 {
13034 Lisp_Object val;
13035
13036 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13037 return val;
13038
13039 return Voverlay_arrow_string;
13040 }
13041
13042 /* Return true if there are any overlay-arrows in current_buffer. */
13043 static bool
13044 overlay_arrow_in_current_buffer_p (void)
13045 {
13046 Lisp_Object vlist;
13047
13048 for (vlist = Voverlay_arrow_variable_list;
13049 CONSP (vlist);
13050 vlist = XCDR (vlist))
13051 {
13052 Lisp_Object var = XCAR (vlist);
13053 Lisp_Object val;
13054
13055 if (!SYMBOLP (var))
13056 continue;
13057 val = find_symbol_value (var);
13058 if (MARKERP (val)
13059 && current_buffer == XMARKER (val)->buffer)
13060 return true;
13061 }
13062 return false;
13063 }
13064
13065
13066 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13067 has changed. */
13068
13069 static bool
13070 overlay_arrows_changed_p (void)
13071 {
13072 Lisp_Object vlist;
13073
13074 for (vlist = Voverlay_arrow_variable_list;
13075 CONSP (vlist);
13076 vlist = XCDR (vlist))
13077 {
13078 Lisp_Object var = XCAR (vlist);
13079 Lisp_Object val, pstr;
13080
13081 if (!SYMBOLP (var))
13082 continue;
13083 val = find_symbol_value (var);
13084 if (!MARKERP (val))
13085 continue;
13086 if (! EQ (COERCE_MARKER (val),
13087 Fget (var, Qlast_arrow_position))
13088 || ! (pstr = overlay_arrow_string_or_property (var),
13089 EQ (pstr, Fget (var, Qlast_arrow_string))))
13090 return true;
13091 }
13092 return false;
13093 }
13094
13095 /* Mark overlay arrows to be updated on next redisplay. */
13096
13097 static void
13098 update_overlay_arrows (int up_to_date)
13099 {
13100 Lisp_Object vlist;
13101
13102 for (vlist = Voverlay_arrow_variable_list;
13103 CONSP (vlist);
13104 vlist = XCDR (vlist))
13105 {
13106 Lisp_Object var = XCAR (vlist);
13107
13108 if (!SYMBOLP (var))
13109 continue;
13110
13111 if (up_to_date > 0)
13112 {
13113 Lisp_Object val = find_symbol_value (var);
13114 Fput (var, Qlast_arrow_position,
13115 COERCE_MARKER (val));
13116 Fput (var, Qlast_arrow_string,
13117 overlay_arrow_string_or_property (var));
13118 }
13119 else if (up_to_date < 0
13120 || !NILP (Fget (var, Qlast_arrow_position)))
13121 {
13122 Fput (var, Qlast_arrow_position, Qt);
13123 Fput (var, Qlast_arrow_string, Qt);
13124 }
13125 }
13126 }
13127
13128
13129 /* Return overlay arrow string to display at row.
13130 Return integer (bitmap number) for arrow bitmap in left fringe.
13131 Return nil if no overlay arrow. */
13132
13133 static Lisp_Object
13134 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13135 {
13136 Lisp_Object vlist;
13137
13138 for (vlist = Voverlay_arrow_variable_list;
13139 CONSP (vlist);
13140 vlist = XCDR (vlist))
13141 {
13142 Lisp_Object var = XCAR (vlist);
13143 Lisp_Object val;
13144
13145 if (!SYMBOLP (var))
13146 continue;
13147
13148 val = find_symbol_value (var);
13149
13150 if (MARKERP (val)
13151 && current_buffer == XMARKER (val)->buffer
13152 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13153 {
13154 if (FRAME_WINDOW_P (it->f)
13155 /* FIXME: if ROW->reversed_p is set, this should test
13156 the right fringe, not the left one. */
13157 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13158 {
13159 #ifdef HAVE_WINDOW_SYSTEM
13160 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13161 {
13162 int fringe_bitmap = lookup_fringe_bitmap (val);
13163 if (fringe_bitmap != 0)
13164 return make_number (fringe_bitmap);
13165 }
13166 #endif
13167 return make_number (-1); /* Use default arrow bitmap. */
13168 }
13169 return overlay_arrow_string_or_property (var);
13170 }
13171 }
13172
13173 return Qnil;
13174 }
13175
13176 /* Return true if point moved out of or into a composition. Otherwise
13177 return false. PREV_BUF and PREV_PT are the last point buffer and
13178 position. BUF and PT are the current point buffer and position. */
13179
13180 static bool
13181 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13182 struct buffer *buf, ptrdiff_t pt)
13183 {
13184 ptrdiff_t start, end;
13185 Lisp_Object prop;
13186 Lisp_Object buffer;
13187
13188 XSETBUFFER (buffer, buf);
13189 /* Check a composition at the last point if point moved within the
13190 same buffer. */
13191 if (prev_buf == buf)
13192 {
13193 if (prev_pt == pt)
13194 /* Point didn't move. */
13195 return false;
13196
13197 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13198 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13199 && composition_valid_p (start, end, prop)
13200 && start < prev_pt && end > prev_pt)
13201 /* The last point was within the composition. Return true iff
13202 point moved out of the composition. */
13203 return (pt <= start || pt >= end);
13204 }
13205
13206 /* Check a composition at the current point. */
13207 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13208 && find_composition (pt, -1, &start, &end, &prop, buffer)
13209 && composition_valid_p (start, end, prop)
13210 && start < pt && end > pt);
13211 }
13212
13213 /* Reconsider the clip changes of buffer which is displayed in W. */
13214
13215 static void
13216 reconsider_clip_changes (struct window *w)
13217 {
13218 struct buffer *b = XBUFFER (w->contents);
13219
13220 if (b->clip_changed
13221 && w->window_end_valid
13222 && w->current_matrix->buffer == b
13223 && w->current_matrix->zv == BUF_ZV (b)
13224 && w->current_matrix->begv == BUF_BEGV (b))
13225 b->clip_changed = false;
13226
13227 /* If display wasn't paused, and W is not a tool bar window, see if
13228 point has been moved into or out of a composition. In that case,
13229 set b->clip_changed to force updating the screen. If
13230 b->clip_changed has already been set, skip this check. */
13231 if (!b->clip_changed && w->window_end_valid)
13232 {
13233 ptrdiff_t pt = (w == XWINDOW (selected_window)
13234 ? PT : marker_position (w->pointm));
13235
13236 if ((w->current_matrix->buffer != b || pt != w->last_point)
13237 && check_point_in_composition (w->current_matrix->buffer,
13238 w->last_point, b, pt))
13239 b->clip_changed = true;
13240 }
13241 }
13242
13243 static void
13244 propagate_buffer_redisplay (void)
13245 { /* Resetting b->text->redisplay is problematic!
13246 We can't just reset it in the case that some window that displays
13247 it has not been redisplayed; and such a window can stay
13248 unredisplayed for a long time if it's currently invisible.
13249 But we do want to reset it at the end of redisplay otherwise
13250 its displayed windows will keep being redisplayed over and over
13251 again.
13252 So we copy all b->text->redisplay flags up to their windows here,
13253 such that mark_window_display_accurate can safely reset
13254 b->text->redisplay. */
13255 Lisp_Object ws = window_list ();
13256 for (; CONSP (ws); ws = XCDR (ws))
13257 {
13258 struct window *thisw = XWINDOW (XCAR (ws));
13259 struct buffer *thisb = XBUFFER (thisw->contents);
13260 if (thisb->text->redisplay)
13261 thisw->redisplay = true;
13262 }
13263 }
13264
13265 #define STOP_POLLING \
13266 do { if (! polling_stopped_here) stop_polling (); \
13267 polling_stopped_here = true; } while (false)
13268
13269 #define RESUME_POLLING \
13270 do { if (polling_stopped_here) start_polling (); \
13271 polling_stopped_here = false; } while (false)
13272
13273
13274 /* Perhaps in the future avoid recentering windows if it
13275 is not necessary; currently that causes some problems. */
13276
13277 static void
13278 redisplay_internal (void)
13279 {
13280 struct window *w = XWINDOW (selected_window);
13281 struct window *sw;
13282 struct frame *fr;
13283 bool pending;
13284 bool must_finish = false, match_p;
13285 struct text_pos tlbufpos, tlendpos;
13286 int number_of_visible_frames;
13287 ptrdiff_t count;
13288 struct frame *sf;
13289 bool polling_stopped_here = false;
13290 Lisp_Object tail, frame;
13291
13292 /* True means redisplay has to consider all windows on all
13293 frames. False, only selected_window is considered. */
13294 bool consider_all_windows_p;
13295
13296 /* True means redisplay has to redisplay the miniwindow. */
13297 bool update_miniwindow_p = false;
13298
13299 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13300
13301 /* No redisplay if running in batch mode or frame is not yet fully
13302 initialized, or redisplay is explicitly turned off by setting
13303 Vinhibit_redisplay. */
13304 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13305 || !NILP (Vinhibit_redisplay))
13306 return;
13307
13308 /* Don't examine these until after testing Vinhibit_redisplay.
13309 When Emacs is shutting down, perhaps because its connection to
13310 X has dropped, we should not look at them at all. */
13311 fr = XFRAME (w->frame);
13312 sf = SELECTED_FRAME ();
13313
13314 if (!fr->glyphs_initialized_p)
13315 return;
13316
13317 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13318 if (popup_activated ())
13319 return;
13320 #endif
13321
13322 /* I don't think this happens but let's be paranoid. */
13323 if (redisplaying_p)
13324 return;
13325
13326 /* Record a function that clears redisplaying_p
13327 when we leave this function. */
13328 count = SPECPDL_INDEX ();
13329 record_unwind_protect_void (unwind_redisplay);
13330 redisplaying_p = true;
13331 specbind (Qinhibit_free_realized_faces, Qnil);
13332
13333 /* Record this function, so it appears on the profiler's backtraces. */
13334 record_in_backtrace (Qredisplay_internal, 0, 0);
13335
13336 FOR_EACH_FRAME (tail, frame)
13337 XFRAME (frame)->already_hscrolled_p = false;
13338
13339 retry:
13340 /* Remember the currently selected window. */
13341 sw = w;
13342
13343 pending = false;
13344 last_escape_glyph_frame = NULL;
13345 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13346 last_glyphless_glyph_frame = NULL;
13347 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13348
13349 /* If face_change, init_iterator will free all realized faces, which
13350 includes the faces referenced from current matrices. So, we
13351 can't reuse current matrices in this case. */
13352 if (face_change)
13353 windows_or_buffers_changed = 47;
13354
13355 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13356 && FRAME_TTY (sf)->previous_frame != sf)
13357 {
13358 /* Since frames on a single ASCII terminal share the same
13359 display area, displaying a different frame means redisplay
13360 the whole thing. */
13361 SET_FRAME_GARBAGED (sf);
13362 #ifndef DOS_NT
13363 set_tty_color_mode (FRAME_TTY (sf), sf);
13364 #endif
13365 FRAME_TTY (sf)->previous_frame = sf;
13366 }
13367
13368 /* Set the visible flags for all frames. Do this before checking for
13369 resized or garbaged frames; they want to know if their frames are
13370 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13371 number_of_visible_frames = 0;
13372
13373 FOR_EACH_FRAME (tail, frame)
13374 {
13375 struct frame *f = XFRAME (frame);
13376
13377 if (FRAME_VISIBLE_P (f))
13378 {
13379 ++number_of_visible_frames;
13380 /* Adjust matrices for visible frames only. */
13381 if (f->fonts_changed)
13382 {
13383 adjust_frame_glyphs (f);
13384 /* Disable all redisplay optimizations for this frame.
13385 This is because adjust_frame_glyphs resets the
13386 enabled_p flag for all glyph rows of all windows, so
13387 many optimizations will fail anyway, and some might
13388 fail to test that flag and do bogus things as
13389 result. */
13390 SET_FRAME_GARBAGED (f);
13391 f->fonts_changed = false;
13392 }
13393 /* If cursor type has been changed on the frame
13394 other than selected, consider all frames. */
13395 if (f != sf && f->cursor_type_changed)
13396 update_mode_lines = 31;
13397 }
13398 clear_desired_matrices (f);
13399 }
13400
13401 /* Notice any pending interrupt request to change frame size. */
13402 do_pending_window_change (true);
13403
13404 /* do_pending_window_change could change the selected_window due to
13405 frame resizing which makes the selected window too small. */
13406 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13407 sw = w;
13408
13409 /* Clear frames marked as garbaged. */
13410 clear_garbaged_frames ();
13411
13412 /* Build menubar and tool-bar items. */
13413 if (NILP (Vmemory_full))
13414 prepare_menu_bars ();
13415
13416 reconsider_clip_changes (w);
13417
13418 /* In most cases selected window displays current buffer. */
13419 match_p = XBUFFER (w->contents) == current_buffer;
13420 if (match_p)
13421 {
13422 /* Detect case that we need to write or remove a star in the mode line. */
13423 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13424 w->update_mode_line = true;
13425
13426 if (mode_line_update_needed (w))
13427 w->update_mode_line = true;
13428
13429 /* If reconsider_clip_changes above decided that the narrowing
13430 in the current buffer changed, make sure all other windows
13431 showing that buffer will be redisplayed. */
13432 if (current_buffer->clip_changed)
13433 bset_update_mode_line (current_buffer);
13434 }
13435
13436 /* Normally the message* functions will have already displayed and
13437 updated the echo area, but the frame may have been trashed, or
13438 the update may have been preempted, so display the echo area
13439 again here. Checking message_cleared_p captures the case that
13440 the echo area should be cleared. */
13441 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13442 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13443 || (message_cleared_p
13444 && minibuf_level == 0
13445 /* If the mini-window is currently selected, this means the
13446 echo-area doesn't show through. */
13447 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13448 {
13449 bool window_height_changed_p = echo_area_display (false);
13450
13451 if (message_cleared_p)
13452 update_miniwindow_p = true;
13453
13454 must_finish = true;
13455
13456 /* If we don't display the current message, don't clear the
13457 message_cleared_p flag, because, if we did, we wouldn't clear
13458 the echo area in the next redisplay which doesn't preserve
13459 the echo area. */
13460 if (!display_last_displayed_message_p)
13461 message_cleared_p = false;
13462
13463 if (window_height_changed_p)
13464 {
13465 windows_or_buffers_changed = 50;
13466
13467 /* If window configuration was changed, frames may have been
13468 marked garbaged. Clear them or we will experience
13469 surprises wrt scrolling. */
13470 clear_garbaged_frames ();
13471 }
13472 }
13473 else if (EQ (selected_window, minibuf_window)
13474 && (current_buffer->clip_changed || window_outdated (w))
13475 && resize_mini_window (w, false))
13476 {
13477 /* Resized active mini-window to fit the size of what it is
13478 showing if its contents might have changed. */
13479 must_finish = true;
13480
13481 /* If window configuration was changed, frames may have been
13482 marked garbaged. Clear them or we will experience
13483 surprises wrt scrolling. */
13484 clear_garbaged_frames ();
13485 }
13486
13487 if (windows_or_buffers_changed && !update_mode_lines)
13488 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13489 only the windows's contents needs to be refreshed, or whether the
13490 mode-lines also need a refresh. */
13491 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13492 ? REDISPLAY_SOME : 32);
13493
13494 /* If specs for an arrow have changed, do thorough redisplay
13495 to ensure we remove any arrow that should no longer exist. */
13496 if (overlay_arrows_changed_p ())
13497 /* Apparently, this is the only case where we update other windows,
13498 without updating other mode-lines. */
13499 windows_or_buffers_changed = 49;
13500
13501 consider_all_windows_p = (update_mode_lines
13502 || windows_or_buffers_changed);
13503
13504 #define AINC(a,i) \
13505 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13506 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13507
13508 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13509 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13510
13511 /* Optimize the case that only the line containing the cursor in the
13512 selected window has changed. Variables starting with this_ are
13513 set in display_line and record information about the line
13514 containing the cursor. */
13515 tlbufpos = this_line_start_pos;
13516 tlendpos = this_line_end_pos;
13517 if (!consider_all_windows_p
13518 && CHARPOS (tlbufpos) > 0
13519 && !w->update_mode_line
13520 && !current_buffer->clip_changed
13521 && !current_buffer->prevent_redisplay_optimizations_p
13522 && FRAME_VISIBLE_P (XFRAME (w->frame))
13523 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13524 && !XFRAME (w->frame)->cursor_type_changed
13525 /* Make sure recorded data applies to current buffer, etc. */
13526 && this_line_buffer == current_buffer
13527 && match_p
13528 && !w->force_start
13529 && !w->optional_new_start
13530 /* Point must be on the line that we have info recorded about. */
13531 && PT >= CHARPOS (tlbufpos)
13532 && PT <= Z - CHARPOS (tlendpos)
13533 /* All text outside that line, including its final newline,
13534 must be unchanged. */
13535 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13536 CHARPOS (tlendpos)))
13537 {
13538 if (CHARPOS (tlbufpos) > BEGV
13539 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13540 && (CHARPOS (tlbufpos) == ZV
13541 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13542 /* Former continuation line has disappeared by becoming empty. */
13543 goto cancel;
13544 else if (window_outdated (w) || MINI_WINDOW_P (w))
13545 {
13546 /* We have to handle the case of continuation around a
13547 wide-column character (see the comment in indent.c around
13548 line 1340).
13549
13550 For instance, in the following case:
13551
13552 -------- Insert --------
13553 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13554 J_I_ ==> J_I_ `^^' are cursors.
13555 ^^ ^^
13556 -------- --------
13557
13558 As we have to redraw the line above, we cannot use this
13559 optimization. */
13560
13561 struct it it;
13562 int line_height_before = this_line_pixel_height;
13563
13564 /* Note that start_display will handle the case that the
13565 line starting at tlbufpos is a continuation line. */
13566 start_display (&it, w, tlbufpos);
13567
13568 /* Implementation note: It this still necessary? */
13569 if (it.current_x != this_line_start_x)
13570 goto cancel;
13571
13572 TRACE ((stderr, "trying display optimization 1\n"));
13573 w->cursor.vpos = -1;
13574 overlay_arrow_seen = false;
13575 it.vpos = this_line_vpos;
13576 it.current_y = this_line_y;
13577 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13578 display_line (&it);
13579
13580 /* If line contains point, is not continued,
13581 and ends at same distance from eob as before, we win. */
13582 if (w->cursor.vpos >= 0
13583 /* Line is not continued, otherwise this_line_start_pos
13584 would have been set to 0 in display_line. */
13585 && CHARPOS (this_line_start_pos)
13586 /* Line ends as before. */
13587 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13588 /* Line has same height as before. Otherwise other lines
13589 would have to be shifted up or down. */
13590 && this_line_pixel_height == line_height_before)
13591 {
13592 /* If this is not the window's last line, we must adjust
13593 the charstarts of the lines below. */
13594 if (it.current_y < it.last_visible_y)
13595 {
13596 struct glyph_row *row
13597 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13598 ptrdiff_t delta, delta_bytes;
13599
13600 /* We used to distinguish between two cases here,
13601 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13602 when the line ends in a newline or the end of the
13603 buffer's accessible portion. But both cases did
13604 the same, so they were collapsed. */
13605 delta = (Z
13606 - CHARPOS (tlendpos)
13607 - MATRIX_ROW_START_CHARPOS (row));
13608 delta_bytes = (Z_BYTE
13609 - BYTEPOS (tlendpos)
13610 - MATRIX_ROW_START_BYTEPOS (row));
13611
13612 increment_matrix_positions (w->current_matrix,
13613 this_line_vpos + 1,
13614 w->current_matrix->nrows,
13615 delta, delta_bytes);
13616 }
13617
13618 /* If this row displays text now but previously didn't,
13619 or vice versa, w->window_end_vpos may have to be
13620 adjusted. */
13621 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13622 {
13623 if (w->window_end_vpos < this_line_vpos)
13624 w->window_end_vpos = this_line_vpos;
13625 }
13626 else if (w->window_end_vpos == this_line_vpos
13627 && this_line_vpos > 0)
13628 w->window_end_vpos = this_line_vpos - 1;
13629 w->window_end_valid = false;
13630
13631 /* Update hint: No need to try to scroll in update_window. */
13632 w->desired_matrix->no_scrolling_p = true;
13633
13634 #ifdef GLYPH_DEBUG
13635 *w->desired_matrix->method = 0;
13636 debug_method_add (w, "optimization 1");
13637 #endif
13638 #ifdef HAVE_WINDOW_SYSTEM
13639 update_window_fringes (w, false);
13640 #endif
13641 goto update;
13642 }
13643 else
13644 goto cancel;
13645 }
13646 else if (/* Cursor position hasn't changed. */
13647 PT == w->last_point
13648 /* Make sure the cursor was last displayed
13649 in this window. Otherwise we have to reposition it. */
13650
13651 /* PXW: Must be converted to pixels, probably. */
13652 && 0 <= w->cursor.vpos
13653 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13654 {
13655 if (!must_finish)
13656 {
13657 do_pending_window_change (true);
13658 /* If selected_window changed, redisplay again. */
13659 if (WINDOWP (selected_window)
13660 && (w = XWINDOW (selected_window)) != sw)
13661 goto retry;
13662
13663 /* We used to always goto end_of_redisplay here, but this
13664 isn't enough if we have a blinking cursor. */
13665 if (w->cursor_off_p == w->last_cursor_off_p)
13666 goto end_of_redisplay;
13667 }
13668 goto update;
13669 }
13670 /* If highlighting the region, or if the cursor is in the echo area,
13671 then we can't just move the cursor. */
13672 else if (NILP (Vshow_trailing_whitespace)
13673 && !cursor_in_echo_area)
13674 {
13675 struct it it;
13676 struct glyph_row *row;
13677
13678 /* Skip from tlbufpos to PT and see where it is. Note that
13679 PT may be in invisible text. If so, we will end at the
13680 next visible position. */
13681 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13682 NULL, DEFAULT_FACE_ID);
13683 it.current_x = this_line_start_x;
13684 it.current_y = this_line_y;
13685 it.vpos = this_line_vpos;
13686
13687 /* The call to move_it_to stops in front of PT, but
13688 moves over before-strings. */
13689 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13690
13691 if (it.vpos == this_line_vpos
13692 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13693 row->enabled_p))
13694 {
13695 eassert (this_line_vpos == it.vpos);
13696 eassert (this_line_y == it.current_y);
13697 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13698 #ifdef GLYPH_DEBUG
13699 *w->desired_matrix->method = 0;
13700 debug_method_add (w, "optimization 3");
13701 #endif
13702 goto update;
13703 }
13704 else
13705 goto cancel;
13706 }
13707
13708 cancel:
13709 /* Text changed drastically or point moved off of line. */
13710 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13711 }
13712
13713 CHARPOS (this_line_start_pos) = 0;
13714 ++clear_face_cache_count;
13715 #ifdef HAVE_WINDOW_SYSTEM
13716 ++clear_image_cache_count;
13717 #endif
13718
13719 /* Build desired matrices, and update the display. If
13720 consider_all_windows_p, do it for all windows on all frames.
13721 Otherwise do it for selected_window, only. */
13722
13723 if (consider_all_windows_p)
13724 {
13725 FOR_EACH_FRAME (tail, frame)
13726 XFRAME (frame)->updated_p = false;
13727
13728 propagate_buffer_redisplay ();
13729
13730 FOR_EACH_FRAME (tail, frame)
13731 {
13732 struct frame *f = XFRAME (frame);
13733
13734 /* We don't have to do anything for unselected terminal
13735 frames. */
13736 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13737 && !EQ (FRAME_TTY (f)->top_frame, frame))
13738 continue;
13739
13740 retry_frame:
13741
13742 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13743 /* Redisplay internal tool bar if this is the first time so we
13744 can adjust the frame height right now, if necessary. */
13745 if (!f->tool_bar_redisplayed_once)
13746 {
13747 if (redisplay_tool_bar (f))
13748 adjust_frame_glyphs (f);
13749 f->tool_bar_redisplayed_once = true;
13750 }
13751 #endif
13752
13753 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13754 {
13755 bool gcscrollbars
13756 /* Only GC scrollbars when we redisplay the whole frame. */
13757 = f->redisplay || !REDISPLAY_SOME_P ();
13758 /* Mark all the scroll bars to be removed; we'll redeem
13759 the ones we want when we redisplay their windows. */
13760 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13761 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13762
13763 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13764 redisplay_windows (FRAME_ROOT_WINDOW (f));
13765 /* Remember that the invisible frames need to be redisplayed next
13766 time they're visible. */
13767 else if (!REDISPLAY_SOME_P ())
13768 f->redisplay = true;
13769
13770 /* The X error handler may have deleted that frame. */
13771 if (!FRAME_LIVE_P (f))
13772 continue;
13773
13774 /* Any scroll bars which redisplay_windows should have
13775 nuked should now go away. */
13776 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13777 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13778
13779 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13780 {
13781 /* If fonts changed on visible frame, display again. */
13782 if (f->fonts_changed)
13783 {
13784 adjust_frame_glyphs (f);
13785 /* Disable all redisplay optimizations for this
13786 frame. For the reasons, see the comment near
13787 the previous call to adjust_frame_glyphs above. */
13788 SET_FRAME_GARBAGED (f);
13789 f->fonts_changed = false;
13790 goto retry_frame;
13791 }
13792
13793 /* See if we have to hscroll. */
13794 if (!f->already_hscrolled_p)
13795 {
13796 f->already_hscrolled_p = true;
13797 if (hscroll_windows (f->root_window))
13798 goto retry_frame;
13799 }
13800
13801 /* Prevent various kinds of signals during display
13802 update. stdio is not robust about handling
13803 signals, which can cause an apparent I/O error. */
13804 if (interrupt_input)
13805 unrequest_sigio ();
13806 STOP_POLLING;
13807
13808 pending |= update_frame (f, false, false);
13809 f->cursor_type_changed = false;
13810 f->updated_p = true;
13811 }
13812 }
13813 }
13814
13815 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13816
13817 if (!pending)
13818 {
13819 /* Do the mark_window_display_accurate after all windows have
13820 been redisplayed because this call resets flags in buffers
13821 which are needed for proper redisplay. */
13822 FOR_EACH_FRAME (tail, frame)
13823 {
13824 struct frame *f = XFRAME (frame);
13825 if (f->updated_p)
13826 {
13827 f->redisplay = false;
13828 mark_window_display_accurate (f->root_window, true);
13829 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13830 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13831 }
13832 }
13833 }
13834 }
13835 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13836 {
13837 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13838 struct frame *mini_frame;
13839
13840 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13841 /* Use list_of_error, not Qerror, so that
13842 we catch only errors and don't run the debugger. */
13843 internal_condition_case_1 (redisplay_window_1, selected_window,
13844 list_of_error,
13845 redisplay_window_error);
13846 if (update_miniwindow_p)
13847 internal_condition_case_1 (redisplay_window_1, mini_window,
13848 list_of_error,
13849 redisplay_window_error);
13850
13851 /* Compare desired and current matrices, perform output. */
13852
13853 update:
13854 /* If fonts changed, display again. */
13855 if (sf->fonts_changed)
13856 goto retry;
13857
13858 /* Prevent various kinds of signals during display update.
13859 stdio is not robust about handling signals,
13860 which can cause an apparent I/O error. */
13861 if (interrupt_input)
13862 unrequest_sigio ();
13863 STOP_POLLING;
13864
13865 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13866 {
13867 if (hscroll_windows (selected_window))
13868 goto retry;
13869
13870 XWINDOW (selected_window)->must_be_updated_p = true;
13871 pending = update_frame (sf, false, false);
13872 sf->cursor_type_changed = false;
13873 }
13874
13875 /* We may have called echo_area_display at the top of this
13876 function. If the echo area is on another frame, that may
13877 have put text on a frame other than the selected one, so the
13878 above call to update_frame would not have caught it. Catch
13879 it here. */
13880 mini_window = FRAME_MINIBUF_WINDOW (sf);
13881 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13882
13883 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13884 {
13885 XWINDOW (mini_window)->must_be_updated_p = true;
13886 pending |= update_frame (mini_frame, false, false);
13887 mini_frame->cursor_type_changed = false;
13888 if (!pending && hscroll_windows (mini_window))
13889 goto retry;
13890 }
13891 }
13892
13893 /* If display was paused because of pending input, make sure we do a
13894 thorough update the next time. */
13895 if (pending)
13896 {
13897 /* Prevent the optimization at the beginning of
13898 redisplay_internal that tries a single-line update of the
13899 line containing the cursor in the selected window. */
13900 CHARPOS (this_line_start_pos) = 0;
13901
13902 /* Let the overlay arrow be updated the next time. */
13903 update_overlay_arrows (0);
13904
13905 /* If we pause after scrolling, some rows in the current
13906 matrices of some windows are not valid. */
13907 if (!WINDOW_FULL_WIDTH_P (w)
13908 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13909 update_mode_lines = 36;
13910 }
13911 else
13912 {
13913 if (!consider_all_windows_p)
13914 {
13915 /* This has already been done above if
13916 consider_all_windows_p is set. */
13917 if (XBUFFER (w->contents)->text->redisplay
13918 && buffer_window_count (XBUFFER (w->contents)) > 1)
13919 /* This can happen if b->text->redisplay was set during
13920 jit-lock. */
13921 propagate_buffer_redisplay ();
13922 mark_window_display_accurate_1 (w, true);
13923
13924 /* Say overlay arrows are up to date. */
13925 update_overlay_arrows (1);
13926
13927 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13928 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13929 }
13930
13931 update_mode_lines = 0;
13932 windows_or_buffers_changed = 0;
13933 }
13934
13935 /* Start SIGIO interrupts coming again. Having them off during the
13936 code above makes it less likely one will discard output, but not
13937 impossible, since there might be stuff in the system buffer here.
13938 But it is much hairier to try to do anything about that. */
13939 if (interrupt_input)
13940 request_sigio ();
13941 RESUME_POLLING;
13942
13943 /* If a frame has become visible which was not before, redisplay
13944 again, so that we display it. Expose events for such a frame
13945 (which it gets when becoming visible) don't call the parts of
13946 redisplay constructing glyphs, so simply exposing a frame won't
13947 display anything in this case. So, we have to display these
13948 frames here explicitly. */
13949 if (!pending)
13950 {
13951 int new_count = 0;
13952
13953 FOR_EACH_FRAME (tail, frame)
13954 {
13955 if (XFRAME (frame)->visible)
13956 new_count++;
13957 }
13958
13959 if (new_count != number_of_visible_frames)
13960 windows_or_buffers_changed = 52;
13961 }
13962
13963 /* Change frame size now if a change is pending. */
13964 do_pending_window_change (true);
13965
13966 /* If we just did a pending size change, or have additional
13967 visible frames, or selected_window changed, redisplay again. */
13968 if ((windows_or_buffers_changed && !pending)
13969 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13970 goto retry;
13971
13972 /* Clear the face and image caches.
13973
13974 We used to do this only if consider_all_windows_p. But the cache
13975 needs to be cleared if a timer creates images in the current
13976 buffer (e.g. the test case in Bug#6230). */
13977
13978 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13979 {
13980 clear_face_cache (false);
13981 clear_face_cache_count = 0;
13982 }
13983
13984 #ifdef HAVE_WINDOW_SYSTEM
13985 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13986 {
13987 clear_image_caches (Qnil);
13988 clear_image_cache_count = 0;
13989 }
13990 #endif /* HAVE_WINDOW_SYSTEM */
13991
13992 end_of_redisplay:
13993 #ifdef HAVE_NS
13994 ns_set_doc_edited ();
13995 #endif
13996 if (interrupt_input && interrupts_deferred)
13997 request_sigio ();
13998
13999 unbind_to (count, Qnil);
14000 RESUME_POLLING;
14001 }
14002
14003
14004 /* Redisplay, but leave alone any recent echo area message unless
14005 another message has been requested in its place.
14006
14007 This is useful in situations where you need to redisplay but no
14008 user action has occurred, making it inappropriate for the message
14009 area to be cleared. See tracking_off and
14010 wait_reading_process_output for examples of these situations.
14011
14012 FROM_WHERE is an integer saying from where this function was
14013 called. This is useful for debugging. */
14014
14015 void
14016 redisplay_preserve_echo_area (int from_where)
14017 {
14018 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14019
14020 if (!NILP (echo_area_buffer[1]))
14021 {
14022 /* We have a previously displayed message, but no current
14023 message. Redisplay the previous message. */
14024 display_last_displayed_message_p = true;
14025 redisplay_internal ();
14026 display_last_displayed_message_p = false;
14027 }
14028 else
14029 redisplay_internal ();
14030
14031 flush_frame (SELECTED_FRAME ());
14032 }
14033
14034
14035 /* Function registered with record_unwind_protect in redisplay_internal. */
14036
14037 static void
14038 unwind_redisplay (void)
14039 {
14040 redisplaying_p = false;
14041 }
14042
14043
14044 /* Mark the display of leaf window W as accurate or inaccurate.
14045 If ACCURATE_P, mark display of W as accurate.
14046 If !ACCURATE_P, arrange for W to be redisplayed the next
14047 time redisplay_internal is called. */
14048
14049 static void
14050 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14051 {
14052 struct buffer *b = XBUFFER (w->contents);
14053
14054 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14055 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14056 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14057
14058 if (accurate_p)
14059 {
14060 b->clip_changed = false;
14061 b->prevent_redisplay_optimizations_p = false;
14062 eassert (buffer_window_count (b) > 0);
14063 /* Resetting b->text->redisplay is problematic!
14064 In order to make it safer to do it here, redisplay_internal must
14065 have copied all b->text->redisplay to their respective windows. */
14066 b->text->redisplay = false;
14067
14068 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14069 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14070 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14071 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14072
14073 w->current_matrix->buffer = b;
14074 w->current_matrix->begv = BUF_BEGV (b);
14075 w->current_matrix->zv = BUF_ZV (b);
14076
14077 w->last_cursor_vpos = w->cursor.vpos;
14078 w->last_cursor_off_p = w->cursor_off_p;
14079
14080 if (w == XWINDOW (selected_window))
14081 w->last_point = BUF_PT (b);
14082 else
14083 w->last_point = marker_position (w->pointm);
14084
14085 w->window_end_valid = true;
14086 w->update_mode_line = false;
14087 }
14088
14089 w->redisplay = !accurate_p;
14090 }
14091
14092
14093 /* Mark the display of windows in the window tree rooted at WINDOW as
14094 accurate or inaccurate. If ACCURATE_P, mark display of
14095 windows as accurate. If !ACCURATE_P, arrange for windows to
14096 be redisplayed the next time redisplay_internal is called. */
14097
14098 void
14099 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14100 {
14101 struct window *w;
14102
14103 for (; !NILP (window); window = w->next)
14104 {
14105 w = XWINDOW (window);
14106 if (WINDOWP (w->contents))
14107 mark_window_display_accurate (w->contents, accurate_p);
14108 else
14109 mark_window_display_accurate_1 (w, accurate_p);
14110 }
14111
14112 if (accurate_p)
14113 update_overlay_arrows (1);
14114 else
14115 /* Force a thorough redisplay the next time by setting
14116 last_arrow_position and last_arrow_string to t, which is
14117 unequal to any useful value of Voverlay_arrow_... */
14118 update_overlay_arrows (-1);
14119 }
14120
14121
14122 /* Return value in display table DP (Lisp_Char_Table *) for character
14123 C. Since a display table doesn't have any parent, we don't have to
14124 follow parent. Do not call this function directly but use the
14125 macro DISP_CHAR_VECTOR. */
14126
14127 Lisp_Object
14128 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14129 {
14130 Lisp_Object val;
14131
14132 if (ASCII_CHAR_P (c))
14133 {
14134 val = dp->ascii;
14135 if (SUB_CHAR_TABLE_P (val))
14136 val = XSUB_CHAR_TABLE (val)->contents[c];
14137 }
14138 else
14139 {
14140 Lisp_Object table;
14141
14142 XSETCHAR_TABLE (table, dp);
14143 val = char_table_ref (table, c);
14144 }
14145 if (NILP (val))
14146 val = dp->defalt;
14147 return val;
14148 }
14149
14150
14151 \f
14152 /***********************************************************************
14153 Window Redisplay
14154 ***********************************************************************/
14155
14156 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14157
14158 static void
14159 redisplay_windows (Lisp_Object window)
14160 {
14161 while (!NILP (window))
14162 {
14163 struct window *w = XWINDOW (window);
14164
14165 if (WINDOWP (w->contents))
14166 redisplay_windows (w->contents);
14167 else if (BUFFERP (w->contents))
14168 {
14169 displayed_buffer = XBUFFER (w->contents);
14170 /* Use list_of_error, not Qerror, so that
14171 we catch only errors and don't run the debugger. */
14172 internal_condition_case_1 (redisplay_window_0, window,
14173 list_of_error,
14174 redisplay_window_error);
14175 }
14176
14177 window = w->next;
14178 }
14179 }
14180
14181 static Lisp_Object
14182 redisplay_window_error (Lisp_Object ignore)
14183 {
14184 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14185 return Qnil;
14186 }
14187
14188 static Lisp_Object
14189 redisplay_window_0 (Lisp_Object window)
14190 {
14191 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14192 redisplay_window (window, false);
14193 return Qnil;
14194 }
14195
14196 static Lisp_Object
14197 redisplay_window_1 (Lisp_Object window)
14198 {
14199 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14200 redisplay_window (window, true);
14201 return Qnil;
14202 }
14203 \f
14204
14205 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14206 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14207 which positions recorded in ROW differ from current buffer
14208 positions.
14209
14210 Return true iff cursor is on this row. */
14211
14212 static bool
14213 set_cursor_from_row (struct window *w, struct glyph_row *row,
14214 struct glyph_matrix *matrix,
14215 ptrdiff_t delta, ptrdiff_t delta_bytes,
14216 int dy, int dvpos)
14217 {
14218 struct glyph *glyph = row->glyphs[TEXT_AREA];
14219 struct glyph *end = glyph + row->used[TEXT_AREA];
14220 struct glyph *cursor = NULL;
14221 /* The last known character position in row. */
14222 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14223 int x = row->x;
14224 ptrdiff_t pt_old = PT - delta;
14225 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14226 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14227 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14228 /* A glyph beyond the edge of TEXT_AREA which we should never
14229 touch. */
14230 struct glyph *glyphs_end = end;
14231 /* True means we've found a match for cursor position, but that
14232 glyph has the avoid_cursor_p flag set. */
14233 bool match_with_avoid_cursor = false;
14234 /* True means we've seen at least one glyph that came from a
14235 display string. */
14236 bool string_seen = false;
14237 /* Largest and smallest buffer positions seen so far during scan of
14238 glyph row. */
14239 ptrdiff_t bpos_max = pos_before;
14240 ptrdiff_t bpos_min = pos_after;
14241 /* Last buffer position covered by an overlay string with an integer
14242 `cursor' property. */
14243 ptrdiff_t bpos_covered = 0;
14244 /* True means the display string on which to display the cursor
14245 comes from a text property, not from an overlay. */
14246 bool string_from_text_prop = false;
14247
14248 /* Don't even try doing anything if called for a mode-line or
14249 header-line row, since the rest of the code isn't prepared to
14250 deal with such calamities. */
14251 eassert (!row->mode_line_p);
14252 if (row->mode_line_p)
14253 return false;
14254
14255 /* Skip over glyphs not having an object at the start and the end of
14256 the row. These are special glyphs like truncation marks on
14257 terminal frames. */
14258 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14259 {
14260 if (!row->reversed_p)
14261 {
14262 while (glyph < end
14263 && NILP (glyph->object)
14264 && glyph->charpos < 0)
14265 {
14266 x += glyph->pixel_width;
14267 ++glyph;
14268 }
14269 while (end > glyph
14270 && NILP ((end - 1)->object)
14271 /* CHARPOS is zero for blanks and stretch glyphs
14272 inserted by extend_face_to_end_of_line. */
14273 && (end - 1)->charpos <= 0)
14274 --end;
14275 glyph_before = glyph - 1;
14276 glyph_after = end;
14277 }
14278 else
14279 {
14280 struct glyph *g;
14281
14282 /* If the glyph row is reversed, we need to process it from back
14283 to front, so swap the edge pointers. */
14284 glyphs_end = end = glyph - 1;
14285 glyph += row->used[TEXT_AREA] - 1;
14286
14287 while (glyph > end + 1
14288 && NILP (glyph->object)
14289 && glyph->charpos < 0)
14290 {
14291 --glyph;
14292 x -= glyph->pixel_width;
14293 }
14294 if (NILP (glyph->object) && glyph->charpos < 0)
14295 --glyph;
14296 /* By default, in reversed rows we put the cursor on the
14297 rightmost (first in the reading order) glyph. */
14298 for (g = end + 1; g < glyph; g++)
14299 x += g->pixel_width;
14300 while (end < glyph
14301 && NILP ((end + 1)->object)
14302 && (end + 1)->charpos <= 0)
14303 ++end;
14304 glyph_before = glyph + 1;
14305 glyph_after = end;
14306 }
14307 }
14308 else if (row->reversed_p)
14309 {
14310 /* In R2L rows that don't display text, put the cursor on the
14311 rightmost glyph. Case in point: an empty last line that is
14312 part of an R2L paragraph. */
14313 cursor = end - 1;
14314 /* Avoid placing the cursor on the last glyph of the row, where
14315 on terminal frames we hold the vertical border between
14316 adjacent windows. */
14317 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14318 && !WINDOW_RIGHTMOST_P (w)
14319 && cursor == row->glyphs[LAST_AREA] - 1)
14320 cursor--;
14321 x = -1; /* will be computed below, at label compute_x */
14322 }
14323
14324 /* Step 1: Try to find the glyph whose character position
14325 corresponds to point. If that's not possible, find 2 glyphs
14326 whose character positions are the closest to point, one before
14327 point, the other after it. */
14328 if (!row->reversed_p)
14329 while (/* not marched to end of glyph row */
14330 glyph < end
14331 /* glyph was not inserted by redisplay for internal purposes */
14332 && !NILP (glyph->object))
14333 {
14334 if (BUFFERP (glyph->object))
14335 {
14336 ptrdiff_t dpos = glyph->charpos - pt_old;
14337
14338 if (glyph->charpos > bpos_max)
14339 bpos_max = glyph->charpos;
14340 if (glyph->charpos < bpos_min)
14341 bpos_min = glyph->charpos;
14342 if (!glyph->avoid_cursor_p)
14343 {
14344 /* If we hit point, we've found the glyph on which to
14345 display the cursor. */
14346 if (dpos == 0)
14347 {
14348 match_with_avoid_cursor = false;
14349 break;
14350 }
14351 /* See if we've found a better approximation to
14352 POS_BEFORE or to POS_AFTER. */
14353 if (0 > dpos && dpos > pos_before - pt_old)
14354 {
14355 pos_before = glyph->charpos;
14356 glyph_before = glyph;
14357 }
14358 else if (0 < dpos && dpos < pos_after - pt_old)
14359 {
14360 pos_after = glyph->charpos;
14361 glyph_after = glyph;
14362 }
14363 }
14364 else if (dpos == 0)
14365 match_with_avoid_cursor = true;
14366 }
14367 else if (STRINGP (glyph->object))
14368 {
14369 Lisp_Object chprop;
14370 ptrdiff_t glyph_pos = glyph->charpos;
14371
14372 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14373 glyph->object);
14374 if (!NILP (chprop))
14375 {
14376 /* If the string came from a `display' text property,
14377 look up the buffer position of that property and
14378 use that position to update bpos_max, as if we
14379 actually saw such a position in one of the row's
14380 glyphs. This helps with supporting integer values
14381 of `cursor' property on the display string in
14382 situations where most or all of the row's buffer
14383 text is completely covered by display properties,
14384 so that no glyph with valid buffer positions is
14385 ever seen in the row. */
14386 ptrdiff_t prop_pos =
14387 string_buffer_position_lim (glyph->object, pos_before,
14388 pos_after, false);
14389
14390 if (prop_pos >= pos_before)
14391 bpos_max = prop_pos;
14392 }
14393 if (INTEGERP (chprop))
14394 {
14395 bpos_covered = bpos_max + XINT (chprop);
14396 /* If the `cursor' property covers buffer positions up
14397 to and including point, we should display cursor on
14398 this glyph. Note that, if a `cursor' property on one
14399 of the string's characters has an integer value, we
14400 will break out of the loop below _before_ we get to
14401 the position match above. IOW, integer values of
14402 the `cursor' property override the "exact match for
14403 point" strategy of positioning the cursor. */
14404 /* Implementation note: bpos_max == pt_old when, e.g.,
14405 we are in an empty line, where bpos_max is set to
14406 MATRIX_ROW_START_CHARPOS, see above. */
14407 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14408 {
14409 cursor = glyph;
14410 break;
14411 }
14412 }
14413
14414 string_seen = true;
14415 }
14416 x += glyph->pixel_width;
14417 ++glyph;
14418 }
14419 else if (glyph > end) /* row is reversed */
14420 while (!NILP (glyph->object))
14421 {
14422 if (BUFFERP (glyph->object))
14423 {
14424 ptrdiff_t dpos = glyph->charpos - pt_old;
14425
14426 if (glyph->charpos > bpos_max)
14427 bpos_max = glyph->charpos;
14428 if (glyph->charpos < bpos_min)
14429 bpos_min = glyph->charpos;
14430 if (!glyph->avoid_cursor_p)
14431 {
14432 if (dpos == 0)
14433 {
14434 match_with_avoid_cursor = false;
14435 break;
14436 }
14437 if (0 > dpos && dpos > pos_before - pt_old)
14438 {
14439 pos_before = glyph->charpos;
14440 glyph_before = glyph;
14441 }
14442 else if (0 < dpos && dpos < pos_after - pt_old)
14443 {
14444 pos_after = glyph->charpos;
14445 glyph_after = glyph;
14446 }
14447 }
14448 else if (dpos == 0)
14449 match_with_avoid_cursor = true;
14450 }
14451 else if (STRINGP (glyph->object))
14452 {
14453 Lisp_Object chprop;
14454 ptrdiff_t glyph_pos = glyph->charpos;
14455
14456 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14457 glyph->object);
14458 if (!NILP (chprop))
14459 {
14460 ptrdiff_t prop_pos =
14461 string_buffer_position_lim (glyph->object, pos_before,
14462 pos_after, false);
14463
14464 if (prop_pos >= pos_before)
14465 bpos_max = prop_pos;
14466 }
14467 if (INTEGERP (chprop))
14468 {
14469 bpos_covered = bpos_max + XINT (chprop);
14470 /* If the `cursor' property covers buffer positions up
14471 to and including point, we should display cursor on
14472 this glyph. */
14473 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14474 {
14475 cursor = glyph;
14476 break;
14477 }
14478 }
14479 string_seen = true;
14480 }
14481 --glyph;
14482 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14483 {
14484 x--; /* can't use any pixel_width */
14485 break;
14486 }
14487 x -= glyph->pixel_width;
14488 }
14489
14490 /* Step 2: If we didn't find an exact match for point, we need to
14491 look for a proper place to put the cursor among glyphs between
14492 GLYPH_BEFORE and GLYPH_AFTER. */
14493 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14494 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14495 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14496 {
14497 /* An empty line has a single glyph whose OBJECT is nil and
14498 whose CHARPOS is the position of a newline on that line.
14499 Note that on a TTY, there are more glyphs after that, which
14500 were produced by extend_face_to_end_of_line, but their
14501 CHARPOS is zero or negative. */
14502 bool empty_line_p =
14503 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14504 && NILP (glyph->object) && glyph->charpos > 0
14505 /* On a TTY, continued and truncated rows also have a glyph at
14506 their end whose OBJECT is nil and whose CHARPOS is
14507 positive (the continuation and truncation glyphs), but such
14508 rows are obviously not "empty". */
14509 && !(row->continued_p || row->truncated_on_right_p));
14510
14511 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14512 {
14513 ptrdiff_t ellipsis_pos;
14514
14515 /* Scan back over the ellipsis glyphs. */
14516 if (!row->reversed_p)
14517 {
14518 ellipsis_pos = (glyph - 1)->charpos;
14519 while (glyph > row->glyphs[TEXT_AREA]
14520 && (glyph - 1)->charpos == ellipsis_pos)
14521 glyph--, x -= glyph->pixel_width;
14522 /* That loop always goes one position too far, including
14523 the glyph before the ellipsis. So scan forward over
14524 that one. */
14525 x += glyph->pixel_width;
14526 glyph++;
14527 }
14528 else /* row is reversed */
14529 {
14530 ellipsis_pos = (glyph + 1)->charpos;
14531 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14532 && (glyph + 1)->charpos == ellipsis_pos)
14533 glyph++, x += glyph->pixel_width;
14534 x -= glyph->pixel_width;
14535 glyph--;
14536 }
14537 }
14538 else if (match_with_avoid_cursor)
14539 {
14540 cursor = glyph_after;
14541 x = -1;
14542 }
14543 else if (string_seen)
14544 {
14545 int incr = row->reversed_p ? -1 : +1;
14546
14547 /* Need to find the glyph that came out of a string which is
14548 present at point. That glyph is somewhere between
14549 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14550 positioned between POS_BEFORE and POS_AFTER in the
14551 buffer. */
14552 struct glyph *start, *stop;
14553 ptrdiff_t pos = pos_before;
14554
14555 x = -1;
14556
14557 /* If the row ends in a newline from a display string,
14558 reordering could have moved the glyphs belonging to the
14559 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14560 in this case we extend the search to the last glyph in
14561 the row that was not inserted by redisplay. */
14562 if (row->ends_in_newline_from_string_p)
14563 {
14564 glyph_after = end;
14565 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14566 }
14567
14568 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14569 correspond to POS_BEFORE and POS_AFTER, respectively. We
14570 need START and STOP in the order that corresponds to the
14571 row's direction as given by its reversed_p flag. If the
14572 directionality of characters between POS_BEFORE and
14573 POS_AFTER is the opposite of the row's base direction,
14574 these characters will have been reordered for display,
14575 and we need to reverse START and STOP. */
14576 if (!row->reversed_p)
14577 {
14578 start = min (glyph_before, glyph_after);
14579 stop = max (glyph_before, glyph_after);
14580 }
14581 else
14582 {
14583 start = max (glyph_before, glyph_after);
14584 stop = min (glyph_before, glyph_after);
14585 }
14586 for (glyph = start + incr;
14587 row->reversed_p ? glyph > stop : glyph < stop; )
14588 {
14589
14590 /* Any glyphs that come from the buffer are here because
14591 of bidi reordering. Skip them, and only pay
14592 attention to glyphs that came from some string. */
14593 if (STRINGP (glyph->object))
14594 {
14595 Lisp_Object str;
14596 ptrdiff_t tem;
14597 /* If the display property covers the newline, we
14598 need to search for it one position farther. */
14599 ptrdiff_t lim = pos_after
14600 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14601
14602 string_from_text_prop = false;
14603 str = glyph->object;
14604 tem = string_buffer_position_lim (str, pos, lim, false);
14605 if (tem == 0 /* from overlay */
14606 || pos <= tem)
14607 {
14608 /* If the string from which this glyph came is
14609 found in the buffer at point, or at position
14610 that is closer to point than pos_after, then
14611 we've found the glyph we've been looking for.
14612 If it comes from an overlay (tem == 0), and
14613 it has the `cursor' property on one of its
14614 glyphs, record that glyph as a candidate for
14615 displaying the cursor. (As in the
14616 unidirectional version, we will display the
14617 cursor on the last candidate we find.) */
14618 if (tem == 0
14619 || tem == pt_old
14620 || (tem - pt_old > 0 && tem < pos_after))
14621 {
14622 /* The glyphs from this string could have
14623 been reordered. Find the one with the
14624 smallest string position. Or there could
14625 be a character in the string with the
14626 `cursor' property, which means display
14627 cursor on that character's glyph. */
14628 ptrdiff_t strpos = glyph->charpos;
14629
14630 if (tem)
14631 {
14632 cursor = glyph;
14633 string_from_text_prop = true;
14634 }
14635 for ( ;
14636 (row->reversed_p ? glyph > stop : glyph < stop)
14637 && EQ (glyph->object, str);
14638 glyph += incr)
14639 {
14640 Lisp_Object cprop;
14641 ptrdiff_t gpos = glyph->charpos;
14642
14643 cprop = Fget_char_property (make_number (gpos),
14644 Qcursor,
14645 glyph->object);
14646 if (!NILP (cprop))
14647 {
14648 cursor = glyph;
14649 break;
14650 }
14651 if (tem && glyph->charpos < strpos)
14652 {
14653 strpos = glyph->charpos;
14654 cursor = glyph;
14655 }
14656 }
14657
14658 if (tem == pt_old
14659 || (tem - pt_old > 0 && tem < pos_after))
14660 goto compute_x;
14661 }
14662 if (tem)
14663 pos = tem + 1; /* don't find previous instances */
14664 }
14665 /* This string is not what we want; skip all of the
14666 glyphs that came from it. */
14667 while ((row->reversed_p ? glyph > stop : glyph < stop)
14668 && EQ (glyph->object, str))
14669 glyph += incr;
14670 }
14671 else
14672 glyph += incr;
14673 }
14674
14675 /* If we reached the end of the line, and END was from a string,
14676 the cursor is not on this line. */
14677 if (cursor == NULL
14678 && (row->reversed_p ? glyph <= end : glyph >= end)
14679 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14680 && STRINGP (end->object)
14681 && row->continued_p)
14682 return false;
14683 }
14684 /* A truncated row may not include PT among its character positions.
14685 Setting the cursor inside the scroll margin will trigger
14686 recalculation of hscroll in hscroll_window_tree. But if a
14687 display string covers point, defer to the string-handling
14688 code below to figure this out. */
14689 else if (row->truncated_on_left_p && pt_old < bpos_min)
14690 {
14691 cursor = glyph_before;
14692 x = -1;
14693 }
14694 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14695 /* Zero-width characters produce no glyphs. */
14696 || (!empty_line_p
14697 && (row->reversed_p
14698 ? glyph_after > glyphs_end
14699 : glyph_after < glyphs_end)))
14700 {
14701 cursor = glyph_after;
14702 x = -1;
14703 }
14704 }
14705
14706 compute_x:
14707 if (cursor != NULL)
14708 glyph = cursor;
14709 else if (glyph == glyphs_end
14710 && pos_before == pos_after
14711 && STRINGP ((row->reversed_p
14712 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14713 : row->glyphs[TEXT_AREA])->object))
14714 {
14715 /* If all the glyphs of this row came from strings, put the
14716 cursor on the first glyph of the row. This avoids having the
14717 cursor outside of the text area in this very rare and hard
14718 use case. */
14719 glyph =
14720 row->reversed_p
14721 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14722 : row->glyphs[TEXT_AREA];
14723 }
14724 if (x < 0)
14725 {
14726 struct glyph *g;
14727
14728 /* Need to compute x that corresponds to GLYPH. */
14729 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14730 {
14731 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14732 emacs_abort ();
14733 x += g->pixel_width;
14734 }
14735 }
14736
14737 /* ROW could be part of a continued line, which, under bidi
14738 reordering, might have other rows whose start and end charpos
14739 occlude point. Only set w->cursor if we found a better
14740 approximation to the cursor position than we have from previously
14741 examined candidate rows belonging to the same continued line. */
14742 if (/* We already have a candidate row. */
14743 w->cursor.vpos >= 0
14744 /* That candidate is not the row we are processing. */
14745 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14746 /* Make sure cursor.vpos specifies a row whose start and end
14747 charpos occlude point, and it is valid candidate for being a
14748 cursor-row. This is because some callers of this function
14749 leave cursor.vpos at the row where the cursor was displayed
14750 during the last redisplay cycle. */
14751 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14752 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14753 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14754 {
14755 struct glyph *g1
14756 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14757
14758 /* Don't consider glyphs that are outside TEXT_AREA. */
14759 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14760 return false;
14761 /* Keep the candidate whose buffer position is the closest to
14762 point or has the `cursor' property. */
14763 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14764 w->cursor.hpos >= 0
14765 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14766 && ((BUFFERP (g1->object)
14767 && (g1->charpos == pt_old /* An exact match always wins. */
14768 || (BUFFERP (glyph->object)
14769 && eabs (g1->charpos - pt_old)
14770 < eabs (glyph->charpos - pt_old))))
14771 /* Previous candidate is a glyph from a string that has
14772 a non-nil `cursor' property. */
14773 || (STRINGP (g1->object)
14774 && (!NILP (Fget_char_property (make_number (g1->charpos),
14775 Qcursor, g1->object))
14776 /* Previous candidate is from the same display
14777 string as this one, and the display string
14778 came from a text property. */
14779 || (EQ (g1->object, glyph->object)
14780 && string_from_text_prop)
14781 /* this candidate is from newline and its
14782 position is not an exact match */
14783 || (NILP (glyph->object)
14784 && glyph->charpos != pt_old)))))
14785 return false;
14786 /* If this candidate gives an exact match, use that. */
14787 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14788 /* If this candidate is a glyph created for the
14789 terminating newline of a line, and point is on that
14790 newline, it wins because it's an exact match. */
14791 || (!row->continued_p
14792 && NILP (glyph->object)
14793 && glyph->charpos == 0
14794 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14795 /* Otherwise, keep the candidate that comes from a row
14796 spanning less buffer positions. This may win when one or
14797 both candidate positions are on glyphs that came from
14798 display strings, for which we cannot compare buffer
14799 positions. */
14800 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14801 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14802 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14803 return false;
14804 }
14805 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14806 w->cursor.x = x;
14807 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14808 w->cursor.y = row->y + dy;
14809
14810 if (w == XWINDOW (selected_window))
14811 {
14812 if (!row->continued_p
14813 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14814 && row->x == 0)
14815 {
14816 this_line_buffer = XBUFFER (w->contents);
14817
14818 CHARPOS (this_line_start_pos)
14819 = MATRIX_ROW_START_CHARPOS (row) + delta;
14820 BYTEPOS (this_line_start_pos)
14821 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14822
14823 CHARPOS (this_line_end_pos)
14824 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14825 BYTEPOS (this_line_end_pos)
14826 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14827
14828 this_line_y = w->cursor.y;
14829 this_line_pixel_height = row->height;
14830 this_line_vpos = w->cursor.vpos;
14831 this_line_start_x = row->x;
14832 }
14833 else
14834 CHARPOS (this_line_start_pos) = 0;
14835 }
14836
14837 return true;
14838 }
14839
14840
14841 /* Run window scroll functions, if any, for WINDOW with new window
14842 start STARTP. Sets the window start of WINDOW to that position.
14843
14844 We assume that the window's buffer is really current. */
14845
14846 static struct text_pos
14847 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14848 {
14849 struct window *w = XWINDOW (window);
14850 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14851
14852 eassert (current_buffer == XBUFFER (w->contents));
14853
14854 if (!NILP (Vwindow_scroll_functions))
14855 {
14856 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14857 make_number (CHARPOS (startp)));
14858 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14859 /* In case the hook functions switch buffers. */
14860 set_buffer_internal (XBUFFER (w->contents));
14861 }
14862
14863 return startp;
14864 }
14865
14866
14867 /* Make sure the line containing the cursor is fully visible.
14868 A value of true means there is nothing to be done.
14869 (Either the line is fully visible, or it cannot be made so,
14870 or we cannot tell.)
14871
14872 If FORCE_P, return false even if partial visible cursor row
14873 is higher than window.
14874
14875 If CURRENT_MATRIX_P, use the information from the
14876 window's current glyph matrix; otherwise use the desired glyph
14877 matrix.
14878
14879 A value of false means the caller should do scrolling
14880 as if point had gone off the screen. */
14881
14882 static bool
14883 cursor_row_fully_visible_p (struct window *w, bool force_p,
14884 bool current_matrix_p)
14885 {
14886 struct glyph_matrix *matrix;
14887 struct glyph_row *row;
14888 int window_height;
14889
14890 if (!make_cursor_line_fully_visible_p)
14891 return true;
14892
14893 /* It's not always possible to find the cursor, e.g, when a window
14894 is full of overlay strings. Don't do anything in that case. */
14895 if (w->cursor.vpos < 0)
14896 return true;
14897
14898 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14899 row = MATRIX_ROW (matrix, w->cursor.vpos);
14900
14901 /* If the cursor row is not partially visible, there's nothing to do. */
14902 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14903 return true;
14904
14905 /* If the row the cursor is in is taller than the window's height,
14906 it's not clear what to do, so do nothing. */
14907 window_height = window_box_height (w);
14908 if (row->height >= window_height)
14909 {
14910 if (!force_p || MINI_WINDOW_P (w)
14911 || w->vscroll || w->cursor.vpos == 0)
14912 return true;
14913 }
14914 return false;
14915 }
14916
14917
14918 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14919 means only WINDOW is redisplayed in redisplay_internal.
14920 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14921 in redisplay_window to bring a partially visible line into view in
14922 the case that only the cursor has moved.
14923
14924 LAST_LINE_MISFIT should be true if we're scrolling because the
14925 last screen line's vertical height extends past the end of the screen.
14926
14927 Value is
14928
14929 1 if scrolling succeeded
14930
14931 0 if scrolling didn't find point.
14932
14933 -1 if new fonts have been loaded so that we must interrupt
14934 redisplay, adjust glyph matrices, and try again. */
14935
14936 enum
14937 {
14938 SCROLLING_SUCCESS,
14939 SCROLLING_FAILED,
14940 SCROLLING_NEED_LARGER_MATRICES
14941 };
14942
14943 /* If scroll-conservatively is more than this, never recenter.
14944
14945 If you change this, don't forget to update the doc string of
14946 `scroll-conservatively' and the Emacs manual. */
14947 #define SCROLL_LIMIT 100
14948
14949 static int
14950 try_scrolling (Lisp_Object window, bool just_this_one_p,
14951 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14952 bool temp_scroll_step, bool last_line_misfit)
14953 {
14954 struct window *w = XWINDOW (window);
14955 struct frame *f = XFRAME (w->frame);
14956 struct text_pos pos, startp;
14957 struct it it;
14958 int this_scroll_margin, scroll_max, rc, height;
14959 int dy = 0, amount_to_scroll = 0;
14960 bool scroll_down_p = false;
14961 int extra_scroll_margin_lines = last_line_misfit;
14962 Lisp_Object aggressive;
14963 /* We will never try scrolling more than this number of lines. */
14964 int scroll_limit = SCROLL_LIMIT;
14965 int frame_line_height = default_line_pixel_height (w);
14966 int window_total_lines
14967 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14968
14969 #ifdef GLYPH_DEBUG
14970 debug_method_add (w, "try_scrolling");
14971 #endif
14972
14973 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14974
14975 /* Compute scroll margin height in pixels. We scroll when point is
14976 within this distance from the top or bottom of the window. */
14977 if (scroll_margin > 0)
14978 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14979 * frame_line_height;
14980 else
14981 this_scroll_margin = 0;
14982
14983 /* Force arg_scroll_conservatively to have a reasonable value, to
14984 avoid scrolling too far away with slow move_it_* functions. Note
14985 that the user can supply scroll-conservatively equal to
14986 `most-positive-fixnum', which can be larger than INT_MAX. */
14987 if (arg_scroll_conservatively > scroll_limit)
14988 {
14989 arg_scroll_conservatively = scroll_limit + 1;
14990 scroll_max = scroll_limit * frame_line_height;
14991 }
14992 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14993 /* Compute how much we should try to scroll maximally to bring
14994 point into view. */
14995 scroll_max = (max (scroll_step,
14996 max (arg_scroll_conservatively, temp_scroll_step))
14997 * frame_line_height);
14998 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14999 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15000 /* We're trying to scroll because of aggressive scrolling but no
15001 scroll_step is set. Choose an arbitrary one. */
15002 scroll_max = 10 * frame_line_height;
15003 else
15004 scroll_max = 0;
15005
15006 too_near_end:
15007
15008 /* Decide whether to scroll down. */
15009 if (PT > CHARPOS (startp))
15010 {
15011 int scroll_margin_y;
15012
15013 /* Compute the pixel ypos of the scroll margin, then move IT to
15014 either that ypos or PT, whichever comes first. */
15015 start_display (&it, w, startp);
15016 scroll_margin_y = it.last_visible_y - this_scroll_margin
15017 - frame_line_height * extra_scroll_margin_lines;
15018 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15019 (MOVE_TO_POS | MOVE_TO_Y));
15020
15021 if (PT > CHARPOS (it.current.pos))
15022 {
15023 int y0 = line_bottom_y (&it);
15024 /* Compute how many pixels below window bottom to stop searching
15025 for PT. This avoids costly search for PT that is far away if
15026 the user limited scrolling by a small number of lines, but
15027 always finds PT if scroll_conservatively is set to a large
15028 number, such as most-positive-fixnum. */
15029 int slack = max (scroll_max, 10 * frame_line_height);
15030 int y_to_move = it.last_visible_y + slack;
15031
15032 /* Compute the distance from the scroll margin to PT or to
15033 the scroll limit, whichever comes first. This should
15034 include the height of the cursor line, to make that line
15035 fully visible. */
15036 move_it_to (&it, PT, -1, y_to_move,
15037 -1, MOVE_TO_POS | MOVE_TO_Y);
15038 dy = line_bottom_y (&it) - y0;
15039
15040 if (dy > scroll_max)
15041 return SCROLLING_FAILED;
15042
15043 if (dy > 0)
15044 scroll_down_p = true;
15045 }
15046 }
15047
15048 if (scroll_down_p)
15049 {
15050 /* Point is in or below the bottom scroll margin, so move the
15051 window start down. If scrolling conservatively, move it just
15052 enough down to make point visible. If scroll_step is set,
15053 move it down by scroll_step. */
15054 if (arg_scroll_conservatively)
15055 amount_to_scroll
15056 = min (max (dy, frame_line_height),
15057 frame_line_height * arg_scroll_conservatively);
15058 else if (scroll_step || temp_scroll_step)
15059 amount_to_scroll = scroll_max;
15060 else
15061 {
15062 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15063 height = WINDOW_BOX_TEXT_HEIGHT (w);
15064 if (NUMBERP (aggressive))
15065 {
15066 double float_amount = XFLOATINT (aggressive) * height;
15067 int aggressive_scroll = float_amount;
15068 if (aggressive_scroll == 0 && float_amount > 0)
15069 aggressive_scroll = 1;
15070 /* Don't let point enter the scroll margin near top of
15071 the window. This could happen if the value of
15072 scroll_up_aggressively is too large and there are
15073 non-zero margins, because scroll_up_aggressively
15074 means put point that fraction of window height
15075 _from_the_bottom_margin_. */
15076 if (aggressive_scroll + 2 * this_scroll_margin > height)
15077 aggressive_scroll = height - 2 * this_scroll_margin;
15078 amount_to_scroll = dy + aggressive_scroll;
15079 }
15080 }
15081
15082 if (amount_to_scroll <= 0)
15083 return SCROLLING_FAILED;
15084
15085 start_display (&it, w, startp);
15086 if (arg_scroll_conservatively <= scroll_limit)
15087 move_it_vertically (&it, amount_to_scroll);
15088 else
15089 {
15090 /* Extra precision for users who set scroll-conservatively
15091 to a large number: make sure the amount we scroll
15092 the window start is never less than amount_to_scroll,
15093 which was computed as distance from window bottom to
15094 point. This matters when lines at window top and lines
15095 below window bottom have different height. */
15096 struct it it1;
15097 void *it1data = NULL;
15098 /* We use a temporary it1 because line_bottom_y can modify
15099 its argument, if it moves one line down; see there. */
15100 int start_y;
15101
15102 SAVE_IT (it1, it, it1data);
15103 start_y = line_bottom_y (&it1);
15104 do {
15105 RESTORE_IT (&it, &it, it1data);
15106 move_it_by_lines (&it, 1);
15107 SAVE_IT (it1, it, it1data);
15108 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15109 }
15110
15111 /* If STARTP is unchanged, move it down another screen line. */
15112 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15113 move_it_by_lines (&it, 1);
15114 startp = it.current.pos;
15115 }
15116 else
15117 {
15118 struct text_pos scroll_margin_pos = startp;
15119 int y_offset = 0;
15120
15121 /* See if point is inside the scroll margin at the top of the
15122 window. */
15123 if (this_scroll_margin)
15124 {
15125 int y_start;
15126
15127 start_display (&it, w, startp);
15128 y_start = it.current_y;
15129 move_it_vertically (&it, this_scroll_margin);
15130 scroll_margin_pos = it.current.pos;
15131 /* If we didn't move enough before hitting ZV, request
15132 additional amount of scroll, to move point out of the
15133 scroll margin. */
15134 if (IT_CHARPOS (it) == ZV
15135 && it.current_y - y_start < this_scroll_margin)
15136 y_offset = this_scroll_margin - (it.current_y - y_start);
15137 }
15138
15139 if (PT < CHARPOS (scroll_margin_pos))
15140 {
15141 /* Point is in the scroll margin at the top of the window or
15142 above what is displayed in the window. */
15143 int y0, y_to_move;
15144
15145 /* Compute the vertical distance from PT to the scroll
15146 margin position. Move as far as scroll_max allows, or
15147 one screenful, or 10 screen lines, whichever is largest.
15148 Give up if distance is greater than scroll_max or if we
15149 didn't reach the scroll margin position. */
15150 SET_TEXT_POS (pos, PT, PT_BYTE);
15151 start_display (&it, w, pos);
15152 y0 = it.current_y;
15153 y_to_move = max (it.last_visible_y,
15154 max (scroll_max, 10 * frame_line_height));
15155 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15156 y_to_move, -1,
15157 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15158 dy = it.current_y - y0;
15159 if (dy > scroll_max
15160 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15161 return SCROLLING_FAILED;
15162
15163 /* Additional scroll for when ZV was too close to point. */
15164 dy += y_offset;
15165
15166 /* Compute new window start. */
15167 start_display (&it, w, startp);
15168
15169 if (arg_scroll_conservatively)
15170 amount_to_scroll = max (dy, frame_line_height
15171 * max (scroll_step, temp_scroll_step));
15172 else if (scroll_step || temp_scroll_step)
15173 amount_to_scroll = scroll_max;
15174 else
15175 {
15176 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15177 height = WINDOW_BOX_TEXT_HEIGHT (w);
15178 if (NUMBERP (aggressive))
15179 {
15180 double float_amount = XFLOATINT (aggressive) * height;
15181 int aggressive_scroll = float_amount;
15182 if (aggressive_scroll == 0 && float_amount > 0)
15183 aggressive_scroll = 1;
15184 /* Don't let point enter the scroll margin near
15185 bottom of the window, if the value of
15186 scroll_down_aggressively happens to be too
15187 large. */
15188 if (aggressive_scroll + 2 * this_scroll_margin > height)
15189 aggressive_scroll = height - 2 * this_scroll_margin;
15190 amount_to_scroll = dy + aggressive_scroll;
15191 }
15192 }
15193
15194 if (amount_to_scroll <= 0)
15195 return SCROLLING_FAILED;
15196
15197 move_it_vertically_backward (&it, amount_to_scroll);
15198 startp = it.current.pos;
15199 }
15200 }
15201
15202 /* Run window scroll functions. */
15203 startp = run_window_scroll_functions (window, startp);
15204
15205 /* Display the window. Give up if new fonts are loaded, or if point
15206 doesn't appear. */
15207 if (!try_window (window, startp, 0))
15208 rc = SCROLLING_NEED_LARGER_MATRICES;
15209 else if (w->cursor.vpos < 0)
15210 {
15211 clear_glyph_matrix (w->desired_matrix);
15212 rc = SCROLLING_FAILED;
15213 }
15214 else
15215 {
15216 /* Maybe forget recorded base line for line number display. */
15217 if (!just_this_one_p
15218 || current_buffer->clip_changed
15219 || BEG_UNCHANGED < CHARPOS (startp))
15220 w->base_line_number = 0;
15221
15222 /* If cursor ends up on a partially visible line,
15223 treat that as being off the bottom of the screen. */
15224 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15225 false)
15226 /* It's possible that the cursor is on the first line of the
15227 buffer, which is partially obscured due to a vscroll
15228 (Bug#7537). In that case, avoid looping forever. */
15229 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15230 {
15231 clear_glyph_matrix (w->desired_matrix);
15232 ++extra_scroll_margin_lines;
15233 goto too_near_end;
15234 }
15235 rc = SCROLLING_SUCCESS;
15236 }
15237
15238 return rc;
15239 }
15240
15241
15242 /* Compute a suitable window start for window W if display of W starts
15243 on a continuation line. Value is true if a new window start
15244 was computed.
15245
15246 The new window start will be computed, based on W's width, starting
15247 from the start of the continued line. It is the start of the
15248 screen line with the minimum distance from the old start W->start. */
15249
15250 static bool
15251 compute_window_start_on_continuation_line (struct window *w)
15252 {
15253 struct text_pos pos, start_pos;
15254 bool window_start_changed_p = false;
15255
15256 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15257
15258 /* If window start is on a continuation line... Window start may be
15259 < BEGV in case there's invisible text at the start of the
15260 buffer (M-x rmail, for example). */
15261 if (CHARPOS (start_pos) > BEGV
15262 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15263 {
15264 struct it it;
15265 struct glyph_row *row;
15266
15267 /* Handle the case that the window start is out of range. */
15268 if (CHARPOS (start_pos) < BEGV)
15269 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15270 else if (CHARPOS (start_pos) > ZV)
15271 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15272
15273 /* Find the start of the continued line. This should be fast
15274 because find_newline is fast (newline cache). */
15275 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15276 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15277 row, DEFAULT_FACE_ID);
15278 reseat_at_previous_visible_line_start (&it);
15279
15280 /* If the line start is "too far" away from the window start,
15281 say it takes too much time to compute a new window start. */
15282 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15283 /* PXW: Do we need upper bounds here? */
15284 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15285 {
15286 int min_distance, distance;
15287
15288 /* Move forward by display lines to find the new window
15289 start. If window width was enlarged, the new start can
15290 be expected to be > the old start. If window width was
15291 decreased, the new window start will be < the old start.
15292 So, we're looking for the display line start with the
15293 minimum distance from the old window start. */
15294 pos = it.current.pos;
15295 min_distance = INFINITY;
15296 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15297 distance < min_distance)
15298 {
15299 min_distance = distance;
15300 pos = it.current.pos;
15301 if (it.line_wrap == WORD_WRAP)
15302 {
15303 /* Under WORD_WRAP, move_it_by_lines is likely to
15304 overshoot and stop not at the first, but the
15305 second character from the left margin. So in
15306 that case, we need a more tight control on the X
15307 coordinate of the iterator than move_it_by_lines
15308 promises in its contract. The method is to first
15309 go to the last (rightmost) visible character of a
15310 line, then move to the leftmost character on the
15311 next line in a separate call. */
15312 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15313 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15314 move_it_to (&it, ZV, 0,
15315 it.current_y + it.max_ascent + it.max_descent, -1,
15316 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15317 }
15318 else
15319 move_it_by_lines (&it, 1);
15320 }
15321
15322 /* Set the window start there. */
15323 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15324 window_start_changed_p = true;
15325 }
15326 }
15327
15328 return window_start_changed_p;
15329 }
15330
15331
15332 /* Try cursor movement in case text has not changed in window WINDOW,
15333 with window start STARTP. Value is
15334
15335 CURSOR_MOVEMENT_SUCCESS if successful
15336
15337 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15338
15339 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15340 display. *SCROLL_STEP is set to true, under certain circumstances, if
15341 we want to scroll as if scroll-step were set to 1. See the code.
15342
15343 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15344 which case we have to abort this redisplay, and adjust matrices
15345 first. */
15346
15347 enum
15348 {
15349 CURSOR_MOVEMENT_SUCCESS,
15350 CURSOR_MOVEMENT_CANNOT_BE_USED,
15351 CURSOR_MOVEMENT_MUST_SCROLL,
15352 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15353 };
15354
15355 static int
15356 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15357 bool *scroll_step)
15358 {
15359 struct window *w = XWINDOW (window);
15360 struct frame *f = XFRAME (w->frame);
15361 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15362
15363 #ifdef GLYPH_DEBUG
15364 if (inhibit_try_cursor_movement)
15365 return rc;
15366 #endif
15367
15368 /* Previously, there was a check for Lisp integer in the
15369 if-statement below. Now, this field is converted to
15370 ptrdiff_t, thus zero means invalid position in a buffer. */
15371 eassert (w->last_point > 0);
15372 /* Likewise there was a check whether window_end_vpos is nil or larger
15373 than the window. Now window_end_vpos is int and so never nil, but
15374 let's leave eassert to check whether it fits in the window. */
15375 eassert (!w->window_end_valid
15376 || w->window_end_vpos < w->current_matrix->nrows);
15377
15378 /* Handle case where text has not changed, only point, and it has
15379 not moved off the frame. */
15380 if (/* Point may be in this window. */
15381 PT >= CHARPOS (startp)
15382 /* Selective display hasn't changed. */
15383 && !current_buffer->clip_changed
15384 /* Function force-mode-line-update is used to force a thorough
15385 redisplay. It sets either windows_or_buffers_changed or
15386 update_mode_lines. So don't take a shortcut here for these
15387 cases. */
15388 && !update_mode_lines
15389 && !windows_or_buffers_changed
15390 && !f->cursor_type_changed
15391 && NILP (Vshow_trailing_whitespace)
15392 /* This code is not used for mini-buffer for the sake of the case
15393 of redisplaying to replace an echo area message; since in
15394 that case the mini-buffer contents per se are usually
15395 unchanged. This code is of no real use in the mini-buffer
15396 since the handling of this_line_start_pos, etc., in redisplay
15397 handles the same cases. */
15398 && !EQ (window, minibuf_window)
15399 && (FRAME_WINDOW_P (f)
15400 || !overlay_arrow_in_current_buffer_p ()))
15401 {
15402 int this_scroll_margin, top_scroll_margin;
15403 struct glyph_row *row = NULL;
15404 int frame_line_height = default_line_pixel_height (w);
15405 int window_total_lines
15406 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15407
15408 #ifdef GLYPH_DEBUG
15409 debug_method_add (w, "cursor movement");
15410 #endif
15411
15412 /* Scroll if point within this distance from the top or bottom
15413 of the window. This is a pixel value. */
15414 if (scroll_margin > 0)
15415 {
15416 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15417 this_scroll_margin *= frame_line_height;
15418 }
15419 else
15420 this_scroll_margin = 0;
15421
15422 top_scroll_margin = this_scroll_margin;
15423 if (WINDOW_WANTS_HEADER_LINE_P (w))
15424 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15425
15426 /* Start with the row the cursor was displayed during the last
15427 not paused redisplay. Give up if that row is not valid. */
15428 if (w->last_cursor_vpos < 0
15429 || w->last_cursor_vpos >= w->current_matrix->nrows)
15430 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15431 else
15432 {
15433 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15434 if (row->mode_line_p)
15435 ++row;
15436 if (!row->enabled_p)
15437 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15438 }
15439
15440 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15441 {
15442 bool scroll_p = false, must_scroll = false;
15443 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15444
15445 if (PT > w->last_point)
15446 {
15447 /* Point has moved forward. */
15448 while (MATRIX_ROW_END_CHARPOS (row) < PT
15449 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15450 {
15451 eassert (row->enabled_p);
15452 ++row;
15453 }
15454
15455 /* If the end position of a row equals the start
15456 position of the next row, and PT is at that position,
15457 we would rather display cursor in the next line. */
15458 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15459 && MATRIX_ROW_END_CHARPOS (row) == PT
15460 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15461 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15462 && !cursor_row_p (row))
15463 ++row;
15464
15465 /* If within the scroll margin, scroll. Note that
15466 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15467 the next line would be drawn, and that
15468 this_scroll_margin can be zero. */
15469 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15470 || PT > MATRIX_ROW_END_CHARPOS (row)
15471 /* Line is completely visible last line in window
15472 and PT is to be set in the next line. */
15473 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15474 && PT == MATRIX_ROW_END_CHARPOS (row)
15475 && !row->ends_at_zv_p
15476 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15477 scroll_p = true;
15478 }
15479 else if (PT < w->last_point)
15480 {
15481 /* Cursor has to be moved backward. Note that PT >=
15482 CHARPOS (startp) because of the outer if-statement. */
15483 while (!row->mode_line_p
15484 && (MATRIX_ROW_START_CHARPOS (row) > PT
15485 || (MATRIX_ROW_START_CHARPOS (row) == PT
15486 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15487 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15488 row > w->current_matrix->rows
15489 && (row-1)->ends_in_newline_from_string_p))))
15490 && (row->y > top_scroll_margin
15491 || CHARPOS (startp) == BEGV))
15492 {
15493 eassert (row->enabled_p);
15494 --row;
15495 }
15496
15497 /* Consider the following case: Window starts at BEGV,
15498 there is invisible, intangible text at BEGV, so that
15499 display starts at some point START > BEGV. It can
15500 happen that we are called with PT somewhere between
15501 BEGV and START. Try to handle that case. */
15502 if (row < w->current_matrix->rows
15503 || row->mode_line_p)
15504 {
15505 row = w->current_matrix->rows;
15506 if (row->mode_line_p)
15507 ++row;
15508 }
15509
15510 /* Due to newlines in overlay strings, we may have to
15511 skip forward over overlay strings. */
15512 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15513 && MATRIX_ROW_END_CHARPOS (row) == PT
15514 && !cursor_row_p (row))
15515 ++row;
15516
15517 /* If within the scroll margin, scroll. */
15518 if (row->y < top_scroll_margin
15519 && CHARPOS (startp) != BEGV)
15520 scroll_p = true;
15521 }
15522 else
15523 {
15524 /* Cursor did not move. So don't scroll even if cursor line
15525 is partially visible, as it was so before. */
15526 rc = CURSOR_MOVEMENT_SUCCESS;
15527 }
15528
15529 if (PT < MATRIX_ROW_START_CHARPOS (row)
15530 || PT > MATRIX_ROW_END_CHARPOS (row))
15531 {
15532 /* if PT is not in the glyph row, give up. */
15533 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15534 must_scroll = true;
15535 }
15536 else if (rc != CURSOR_MOVEMENT_SUCCESS
15537 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15538 {
15539 struct glyph_row *row1;
15540
15541 /* If rows are bidi-reordered and point moved, back up
15542 until we find a row that does not belong to a
15543 continuation line. This is because we must consider
15544 all rows of a continued line as candidates for the
15545 new cursor positioning, since row start and end
15546 positions change non-linearly with vertical position
15547 in such rows. */
15548 /* FIXME: Revisit this when glyph ``spilling'' in
15549 continuation lines' rows is implemented for
15550 bidi-reordered rows. */
15551 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15552 MATRIX_ROW_CONTINUATION_LINE_P (row);
15553 --row)
15554 {
15555 /* If we hit the beginning of the displayed portion
15556 without finding the first row of a continued
15557 line, give up. */
15558 if (row <= row1)
15559 {
15560 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15561 break;
15562 }
15563 eassert (row->enabled_p);
15564 }
15565 }
15566 if (must_scroll)
15567 ;
15568 else if (rc != CURSOR_MOVEMENT_SUCCESS
15569 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15570 /* Make sure this isn't a header line by any chance, since
15571 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15572 && !row->mode_line_p
15573 && make_cursor_line_fully_visible_p)
15574 {
15575 if (PT == MATRIX_ROW_END_CHARPOS (row)
15576 && !row->ends_at_zv_p
15577 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15578 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15579 else if (row->height > window_box_height (w))
15580 {
15581 /* If we end up in a partially visible line, let's
15582 make it fully visible, except when it's taller
15583 than the window, in which case we can't do much
15584 about it. */
15585 *scroll_step = true;
15586 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15587 }
15588 else
15589 {
15590 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15591 if (!cursor_row_fully_visible_p (w, false, true))
15592 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15593 else
15594 rc = CURSOR_MOVEMENT_SUCCESS;
15595 }
15596 }
15597 else if (scroll_p)
15598 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15599 else if (rc != CURSOR_MOVEMENT_SUCCESS
15600 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15601 {
15602 /* With bidi-reordered rows, there could be more than
15603 one candidate row whose start and end positions
15604 occlude point. We need to let set_cursor_from_row
15605 find the best candidate. */
15606 /* FIXME: Revisit this when glyph ``spilling'' in
15607 continuation lines' rows is implemented for
15608 bidi-reordered rows. */
15609 bool rv = false;
15610
15611 do
15612 {
15613 bool at_zv_p = false, exact_match_p = false;
15614
15615 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15616 && PT <= MATRIX_ROW_END_CHARPOS (row)
15617 && cursor_row_p (row))
15618 rv |= set_cursor_from_row (w, row, w->current_matrix,
15619 0, 0, 0, 0);
15620 /* As soon as we've found the exact match for point,
15621 or the first suitable row whose ends_at_zv_p flag
15622 is set, we are done. */
15623 if (rv)
15624 {
15625 at_zv_p = MATRIX_ROW (w->current_matrix,
15626 w->cursor.vpos)->ends_at_zv_p;
15627 if (!at_zv_p
15628 && w->cursor.hpos >= 0
15629 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15630 w->cursor.vpos))
15631 {
15632 struct glyph_row *candidate =
15633 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15634 struct glyph *g =
15635 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15636 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15637
15638 exact_match_p =
15639 (BUFFERP (g->object) && g->charpos == PT)
15640 || (NILP (g->object)
15641 && (g->charpos == PT
15642 || (g->charpos == 0 && endpos - 1 == PT)));
15643 }
15644 if (at_zv_p || exact_match_p)
15645 {
15646 rc = CURSOR_MOVEMENT_SUCCESS;
15647 break;
15648 }
15649 }
15650 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15651 break;
15652 ++row;
15653 }
15654 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15655 || row->continued_p)
15656 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15657 || (MATRIX_ROW_START_CHARPOS (row) == PT
15658 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15659 /* If we didn't find any candidate rows, or exited the
15660 loop before all the candidates were examined, signal
15661 to the caller that this method failed. */
15662 if (rc != CURSOR_MOVEMENT_SUCCESS
15663 && !(rv
15664 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15665 && !row->continued_p))
15666 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15667 else if (rv)
15668 rc = CURSOR_MOVEMENT_SUCCESS;
15669 }
15670 else
15671 {
15672 do
15673 {
15674 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15675 {
15676 rc = CURSOR_MOVEMENT_SUCCESS;
15677 break;
15678 }
15679 ++row;
15680 }
15681 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15682 && MATRIX_ROW_START_CHARPOS (row) == PT
15683 && cursor_row_p (row));
15684 }
15685 }
15686 }
15687
15688 return rc;
15689 }
15690
15691
15692 void
15693 set_vertical_scroll_bar (struct window *w)
15694 {
15695 ptrdiff_t start, end, whole;
15696
15697 /* Calculate the start and end positions for the current window.
15698 At some point, it would be nice to choose between scrollbars
15699 which reflect the whole buffer size, with special markers
15700 indicating narrowing, and scrollbars which reflect only the
15701 visible region.
15702
15703 Note that mini-buffers sometimes aren't displaying any text. */
15704 if (!MINI_WINDOW_P (w)
15705 || (w == XWINDOW (minibuf_window)
15706 && NILP (echo_area_buffer[0])))
15707 {
15708 struct buffer *buf = XBUFFER (w->contents);
15709 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15710 start = marker_position (w->start) - BUF_BEGV (buf);
15711 /* I don't think this is guaranteed to be right. For the
15712 moment, we'll pretend it is. */
15713 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15714
15715 if (end < start)
15716 end = start;
15717 if (whole < (end - start))
15718 whole = end - start;
15719 }
15720 else
15721 start = end = whole = 0;
15722
15723 /* Indicate what this scroll bar ought to be displaying now. */
15724 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15725 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15726 (w, end - start, whole, start);
15727 }
15728
15729
15730 void
15731 set_horizontal_scroll_bar (struct window *w)
15732 {
15733 int start, end, whole, portion;
15734
15735 if (!MINI_WINDOW_P (w)
15736 || (w == XWINDOW (minibuf_window)
15737 && NILP (echo_area_buffer[0])))
15738 {
15739 struct buffer *b = XBUFFER (w->contents);
15740 struct buffer *old_buffer = NULL;
15741 struct it it;
15742 struct text_pos startp;
15743
15744 if (b != current_buffer)
15745 {
15746 old_buffer = current_buffer;
15747 set_buffer_internal (b);
15748 }
15749
15750 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15751 start_display (&it, w, startp);
15752 it.last_visible_x = INT_MAX;
15753 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15754 MOVE_TO_X | MOVE_TO_Y);
15755 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15756 window_box_height (w), -1,
15757 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15758
15759 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15760 end = start + window_box_width (w, TEXT_AREA);
15761 portion = end - start;
15762 /* After enlarging a horizontally scrolled window such that it
15763 gets at least as wide as the text it contains, make sure that
15764 the thumb doesn't fill the entire scroll bar so we can still
15765 drag it back to see the entire text. */
15766 whole = max (whole, end);
15767
15768 if (it.bidi_p)
15769 {
15770 Lisp_Object pdir;
15771
15772 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15773 if (EQ (pdir, Qright_to_left))
15774 {
15775 start = whole - end;
15776 end = start + portion;
15777 }
15778 }
15779
15780 if (old_buffer)
15781 set_buffer_internal (old_buffer);
15782 }
15783 else
15784 start = end = whole = portion = 0;
15785
15786 w->hscroll_whole = whole;
15787
15788 /* Indicate what this scroll bar ought to be displaying now. */
15789 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15790 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15791 (w, portion, whole, start);
15792 }
15793
15794
15795 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15796 selected_window is redisplayed.
15797
15798 We can return without actually redisplaying the window if fonts has been
15799 changed on window's frame. In that case, redisplay_internal will retry.
15800
15801 As one of the important parts of redisplaying a window, we need to
15802 decide whether the previous window-start position (stored in the
15803 window's w->start marker position) is still valid, and if it isn't,
15804 recompute it. Some details about that:
15805
15806 . The previous window-start could be in a continuation line, in
15807 which case we need to recompute it when the window width
15808 changes. See compute_window_start_on_continuation_line and its
15809 call below.
15810
15811 . The text that changed since last redisplay could include the
15812 previous window-start position. In that case, we try to salvage
15813 what we can from the current glyph matrix by calling
15814 try_scrolling, which see.
15815
15816 . Some Emacs command could force us to use a specific window-start
15817 position by setting the window's force_start flag, or gently
15818 propose doing that by setting the window's optional_new_start
15819 flag. In these cases, we try using the specified start point if
15820 that succeeds (i.e. the window desired matrix is successfully
15821 recomputed, and point location is within the window). In case
15822 of optional_new_start, we first check if the specified start
15823 position is feasible, i.e. if it will allow point to be
15824 displayed in the window. If using the specified start point
15825 fails, e.g., if new fonts are needed to be loaded, we abort the
15826 redisplay cycle and leave it up to the next cycle to figure out
15827 things.
15828
15829 . Note that the window's force_start flag is sometimes set by
15830 redisplay itself, when it decides that the previous window start
15831 point is fine and should be kept. Search for "goto force_start"
15832 below to see the details. Like the values of window-start
15833 specified outside of redisplay, these internally-deduced values
15834 are tested for feasibility, and ignored if found to be
15835 unfeasible.
15836
15837 . Note that the function try_window, used to completely redisplay
15838 a window, accepts the window's start point as its argument.
15839 This is used several times in the redisplay code to control
15840 where the window start will be, according to user options such
15841 as scroll-conservatively, and also to ensure the screen line
15842 showing point will be fully (as opposed to partially) visible on
15843 display. */
15844
15845 static void
15846 redisplay_window (Lisp_Object window, bool just_this_one_p)
15847 {
15848 struct window *w = XWINDOW (window);
15849 struct frame *f = XFRAME (w->frame);
15850 struct buffer *buffer = XBUFFER (w->contents);
15851 struct buffer *old = current_buffer;
15852 struct text_pos lpoint, opoint, startp;
15853 bool update_mode_line;
15854 int tem;
15855 struct it it;
15856 /* Record it now because it's overwritten. */
15857 bool current_matrix_up_to_date_p = false;
15858 bool used_current_matrix_p = false;
15859 /* This is less strict than current_matrix_up_to_date_p.
15860 It indicates that the buffer contents and narrowing are unchanged. */
15861 bool buffer_unchanged_p = false;
15862 bool temp_scroll_step = false;
15863 ptrdiff_t count = SPECPDL_INDEX ();
15864 int rc;
15865 int centering_position = -1;
15866 bool last_line_misfit = false;
15867 ptrdiff_t beg_unchanged, end_unchanged;
15868 int frame_line_height;
15869
15870 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15871 opoint = lpoint;
15872
15873 #ifdef GLYPH_DEBUG
15874 *w->desired_matrix->method = 0;
15875 #endif
15876
15877 if (!just_this_one_p
15878 && REDISPLAY_SOME_P ()
15879 && !w->redisplay
15880 && !w->update_mode_line
15881 && !f->redisplay
15882 && !buffer->text->redisplay
15883 && BUF_PT (buffer) == w->last_point)
15884 return;
15885
15886 /* Make sure that both W's markers are valid. */
15887 eassert (XMARKER (w->start)->buffer == buffer);
15888 eassert (XMARKER (w->pointm)->buffer == buffer);
15889
15890 /* We come here again if we need to run window-text-change-functions
15891 below. */
15892 restart:
15893 reconsider_clip_changes (w);
15894 frame_line_height = default_line_pixel_height (w);
15895
15896 /* Has the mode line to be updated? */
15897 update_mode_line = (w->update_mode_line
15898 || update_mode_lines
15899 || buffer->clip_changed
15900 || buffer->prevent_redisplay_optimizations_p);
15901
15902 if (!just_this_one_p)
15903 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15904 cleverly elsewhere. */
15905 w->must_be_updated_p = true;
15906
15907 if (MINI_WINDOW_P (w))
15908 {
15909 if (w == XWINDOW (echo_area_window)
15910 && !NILP (echo_area_buffer[0]))
15911 {
15912 if (update_mode_line)
15913 /* We may have to update a tty frame's menu bar or a
15914 tool-bar. Example `M-x C-h C-h C-g'. */
15915 goto finish_menu_bars;
15916 else
15917 /* We've already displayed the echo area glyphs in this window. */
15918 goto finish_scroll_bars;
15919 }
15920 else if ((w != XWINDOW (minibuf_window)
15921 || minibuf_level == 0)
15922 /* When buffer is nonempty, redisplay window normally. */
15923 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15924 /* Quail displays non-mini buffers in minibuffer window.
15925 In that case, redisplay the window normally. */
15926 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15927 {
15928 /* W is a mini-buffer window, but it's not active, so clear
15929 it. */
15930 int yb = window_text_bottom_y (w);
15931 struct glyph_row *row;
15932 int y;
15933
15934 for (y = 0, row = w->desired_matrix->rows;
15935 y < yb;
15936 y += row->height, ++row)
15937 blank_row (w, row, y);
15938 goto finish_scroll_bars;
15939 }
15940
15941 clear_glyph_matrix (w->desired_matrix);
15942 }
15943
15944 /* Otherwise set up data on this window; select its buffer and point
15945 value. */
15946 /* Really select the buffer, for the sake of buffer-local
15947 variables. */
15948 set_buffer_internal_1 (XBUFFER (w->contents));
15949
15950 current_matrix_up_to_date_p
15951 = (w->window_end_valid
15952 && !current_buffer->clip_changed
15953 && !current_buffer->prevent_redisplay_optimizations_p
15954 && !window_outdated (w));
15955
15956 /* Run the window-text-change-functions
15957 if it is possible that the text on the screen has changed
15958 (either due to modification of the text, or any other reason). */
15959 if (!current_matrix_up_to_date_p
15960 && !NILP (Vwindow_text_change_functions))
15961 {
15962 safe_run_hooks (Qwindow_text_change_functions);
15963 goto restart;
15964 }
15965
15966 beg_unchanged = BEG_UNCHANGED;
15967 end_unchanged = END_UNCHANGED;
15968
15969 SET_TEXT_POS (opoint, PT, PT_BYTE);
15970
15971 specbind (Qinhibit_point_motion_hooks, Qt);
15972
15973 buffer_unchanged_p
15974 = (w->window_end_valid
15975 && !current_buffer->clip_changed
15976 && !window_outdated (w));
15977
15978 /* When windows_or_buffers_changed is non-zero, we can't rely
15979 on the window end being valid, so set it to zero there. */
15980 if (windows_or_buffers_changed)
15981 {
15982 /* If window starts on a continuation line, maybe adjust the
15983 window start in case the window's width changed. */
15984 if (XMARKER (w->start)->buffer == current_buffer)
15985 compute_window_start_on_continuation_line (w);
15986
15987 w->window_end_valid = false;
15988 /* If so, we also can't rely on current matrix
15989 and should not fool try_cursor_movement below. */
15990 current_matrix_up_to_date_p = false;
15991 }
15992
15993 /* Some sanity checks. */
15994 CHECK_WINDOW_END (w);
15995 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15996 emacs_abort ();
15997 if (BYTEPOS (opoint) < CHARPOS (opoint))
15998 emacs_abort ();
15999
16000 if (mode_line_update_needed (w))
16001 update_mode_line = true;
16002
16003 /* Point refers normally to the selected window. For any other
16004 window, set up appropriate value. */
16005 if (!EQ (window, selected_window))
16006 {
16007 ptrdiff_t new_pt = marker_position (w->pointm);
16008 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16009
16010 if (new_pt < BEGV)
16011 {
16012 new_pt = BEGV;
16013 new_pt_byte = BEGV_BYTE;
16014 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16015 }
16016 else if (new_pt > (ZV - 1))
16017 {
16018 new_pt = ZV;
16019 new_pt_byte = ZV_BYTE;
16020 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16021 }
16022
16023 /* We don't use SET_PT so that the point-motion hooks don't run. */
16024 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16025 }
16026
16027 /* If any of the character widths specified in the display table
16028 have changed, invalidate the width run cache. It's true that
16029 this may be a bit late to catch such changes, but the rest of
16030 redisplay goes (non-fatally) haywire when the display table is
16031 changed, so why should we worry about doing any better? */
16032 if (current_buffer->width_run_cache
16033 || (current_buffer->base_buffer
16034 && current_buffer->base_buffer->width_run_cache))
16035 {
16036 struct Lisp_Char_Table *disptab = buffer_display_table ();
16037
16038 if (! disptab_matches_widthtab
16039 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16040 {
16041 struct buffer *buf = current_buffer;
16042
16043 if (buf->base_buffer)
16044 buf = buf->base_buffer;
16045 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16046 recompute_width_table (current_buffer, disptab);
16047 }
16048 }
16049
16050 /* If window-start is screwed up, choose a new one. */
16051 if (XMARKER (w->start)->buffer != current_buffer)
16052 goto recenter;
16053
16054 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16055
16056 /* If someone specified a new starting point but did not insist,
16057 check whether it can be used. */
16058 if ((w->optional_new_start || window_frozen_p (w))
16059 && CHARPOS (startp) >= BEGV
16060 && CHARPOS (startp) <= ZV)
16061 {
16062 ptrdiff_t it_charpos;
16063
16064 w->optional_new_start = false;
16065 start_display (&it, w, startp);
16066 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16067 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16068 /* Record IT's position now, since line_bottom_y might change
16069 that. */
16070 it_charpos = IT_CHARPOS (it);
16071 /* Make sure we set the force_start flag only if the cursor row
16072 will be fully visible. Otherwise, the code under force_start
16073 label below will try to move point back into view, which is
16074 not what the code which sets optional_new_start wants. */
16075 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16076 && !w->force_start)
16077 {
16078 if (it_charpos == PT)
16079 w->force_start = true;
16080 /* IT may overshoot PT if text at PT is invisible. */
16081 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16082 w->force_start = true;
16083 #ifdef GLYPH_DEBUG
16084 if (w->force_start)
16085 {
16086 if (window_frozen_p (w))
16087 debug_method_add (w, "set force_start from frozen window start");
16088 else
16089 debug_method_add (w, "set force_start from optional_new_start");
16090 }
16091 #endif
16092 }
16093 }
16094
16095 force_start:
16096
16097 /* Handle case where place to start displaying has been specified,
16098 unless the specified location is outside the accessible range. */
16099 if (w->force_start)
16100 {
16101 /* We set this later on if we have to adjust point. */
16102 int new_vpos = -1;
16103
16104 w->force_start = false;
16105 w->vscroll = 0;
16106 w->window_end_valid = false;
16107
16108 /* Forget any recorded base line for line number display. */
16109 if (!buffer_unchanged_p)
16110 w->base_line_number = 0;
16111
16112 /* Redisplay the mode line. Select the buffer properly for that.
16113 Also, run the hook window-scroll-functions
16114 because we have scrolled. */
16115 /* Note, we do this after clearing force_start because
16116 if there's an error, it is better to forget about force_start
16117 than to get into an infinite loop calling the hook functions
16118 and having them get more errors. */
16119 if (!update_mode_line
16120 || ! NILP (Vwindow_scroll_functions))
16121 {
16122 update_mode_line = true;
16123 w->update_mode_line = true;
16124 startp = run_window_scroll_functions (window, startp);
16125 }
16126
16127 if (CHARPOS (startp) < BEGV)
16128 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16129 else if (CHARPOS (startp) > ZV)
16130 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16131
16132 /* Redisplay, then check if cursor has been set during the
16133 redisplay. Give up if new fonts were loaded. */
16134 /* We used to issue a CHECK_MARGINS argument to try_window here,
16135 but this causes scrolling to fail when point begins inside
16136 the scroll margin (bug#148) -- cyd */
16137 if (!try_window (window, startp, 0))
16138 {
16139 w->force_start = true;
16140 clear_glyph_matrix (w->desired_matrix);
16141 goto need_larger_matrices;
16142 }
16143
16144 if (w->cursor.vpos < 0)
16145 {
16146 /* If point does not appear, try to move point so it does
16147 appear. The desired matrix has been built above, so we
16148 can use it here. */
16149 new_vpos = window_box_height (w) / 2;
16150 }
16151
16152 if (!cursor_row_fully_visible_p (w, false, false))
16153 {
16154 /* Point does appear, but on a line partly visible at end of window.
16155 Move it back to a fully-visible line. */
16156 new_vpos = window_box_height (w);
16157 /* But if window_box_height suggests a Y coordinate that is
16158 not less than we already have, that line will clearly not
16159 be fully visible, so give up and scroll the display.
16160 This can happen when the default face uses a font whose
16161 dimensions are different from the frame's default
16162 font. */
16163 if (new_vpos >= w->cursor.y)
16164 {
16165 w->cursor.vpos = -1;
16166 clear_glyph_matrix (w->desired_matrix);
16167 goto try_to_scroll;
16168 }
16169 }
16170 else if (w->cursor.vpos >= 0)
16171 {
16172 /* Some people insist on not letting point enter the scroll
16173 margin, even though this part handles windows that didn't
16174 scroll at all. */
16175 int window_total_lines
16176 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16177 int margin = min (scroll_margin, window_total_lines / 4);
16178 int pixel_margin = margin * frame_line_height;
16179 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16180
16181 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16182 below, which finds the row to move point to, advances by
16183 the Y coordinate of the _next_ row, see the definition of
16184 MATRIX_ROW_BOTTOM_Y. */
16185 if (w->cursor.vpos < margin + header_line)
16186 {
16187 w->cursor.vpos = -1;
16188 clear_glyph_matrix (w->desired_matrix);
16189 goto try_to_scroll;
16190 }
16191 else
16192 {
16193 int window_height = window_box_height (w);
16194
16195 if (header_line)
16196 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16197 if (w->cursor.y >= window_height - pixel_margin)
16198 {
16199 w->cursor.vpos = -1;
16200 clear_glyph_matrix (w->desired_matrix);
16201 goto try_to_scroll;
16202 }
16203 }
16204 }
16205
16206 /* If we need to move point for either of the above reasons,
16207 now actually do it. */
16208 if (new_vpos >= 0)
16209 {
16210 struct glyph_row *row;
16211
16212 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16213 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16214 ++row;
16215
16216 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16217 MATRIX_ROW_START_BYTEPOS (row));
16218
16219 if (w != XWINDOW (selected_window))
16220 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16221 else if (current_buffer == old)
16222 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16223
16224 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16225
16226 /* Re-run pre-redisplay-function so it can update the region
16227 according to the new position of point. */
16228 /* Other than the cursor, w's redisplay is done so we can set its
16229 redisplay to false. Also the buffer's redisplay can be set to
16230 false, since propagate_buffer_redisplay should have already
16231 propagated its info to `w' anyway. */
16232 w->redisplay = false;
16233 XBUFFER (w->contents)->text->redisplay = false;
16234 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16235
16236 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16237 {
16238 /* pre-redisplay-function made changes (e.g. move the region)
16239 that require another round of redisplay. */
16240 clear_glyph_matrix (w->desired_matrix);
16241 if (!try_window (window, startp, 0))
16242 goto need_larger_matrices;
16243 }
16244 }
16245 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16246 {
16247 clear_glyph_matrix (w->desired_matrix);
16248 goto try_to_scroll;
16249 }
16250
16251 #ifdef GLYPH_DEBUG
16252 debug_method_add (w, "forced window start");
16253 #endif
16254 goto done;
16255 }
16256
16257 /* Handle case where text has not changed, only point, and it has
16258 not moved off the frame, and we are not retrying after hscroll.
16259 (current_matrix_up_to_date_p is true when retrying.) */
16260 if (current_matrix_up_to_date_p
16261 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16262 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16263 {
16264 switch (rc)
16265 {
16266 case CURSOR_MOVEMENT_SUCCESS:
16267 used_current_matrix_p = true;
16268 goto done;
16269
16270 case CURSOR_MOVEMENT_MUST_SCROLL:
16271 goto try_to_scroll;
16272
16273 default:
16274 emacs_abort ();
16275 }
16276 }
16277 /* If current starting point was originally the beginning of a line
16278 but no longer is, find a new starting point. */
16279 else if (w->start_at_line_beg
16280 && !(CHARPOS (startp) <= BEGV
16281 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16282 {
16283 #ifdef GLYPH_DEBUG
16284 debug_method_add (w, "recenter 1");
16285 #endif
16286 goto recenter;
16287 }
16288
16289 /* Try scrolling with try_window_id. Value is > 0 if update has
16290 been done, it is -1 if we know that the same window start will
16291 not work. It is 0 if unsuccessful for some other reason. */
16292 else if ((tem = try_window_id (w)) != 0)
16293 {
16294 #ifdef GLYPH_DEBUG
16295 debug_method_add (w, "try_window_id %d", tem);
16296 #endif
16297
16298 if (f->fonts_changed)
16299 goto need_larger_matrices;
16300 if (tem > 0)
16301 goto done;
16302
16303 /* Otherwise try_window_id has returned -1 which means that we
16304 don't want the alternative below this comment to execute. */
16305 }
16306 else if (CHARPOS (startp) >= BEGV
16307 && CHARPOS (startp) <= ZV
16308 && PT >= CHARPOS (startp)
16309 && (CHARPOS (startp) < ZV
16310 /* Avoid starting at end of buffer. */
16311 || CHARPOS (startp) == BEGV
16312 || !window_outdated (w)))
16313 {
16314 int d1, d2, d5, d6;
16315 int rtop, rbot;
16316
16317 /* If first window line is a continuation line, and window start
16318 is inside the modified region, but the first change is before
16319 current window start, we must select a new window start.
16320
16321 However, if this is the result of a down-mouse event (e.g. by
16322 extending the mouse-drag-overlay), we don't want to select a
16323 new window start, since that would change the position under
16324 the mouse, resulting in an unwanted mouse-movement rather
16325 than a simple mouse-click. */
16326 if (!w->start_at_line_beg
16327 && NILP (do_mouse_tracking)
16328 && CHARPOS (startp) > BEGV
16329 && CHARPOS (startp) > BEG + beg_unchanged
16330 && CHARPOS (startp) <= Z - end_unchanged
16331 /* Even if w->start_at_line_beg is nil, a new window may
16332 start at a line_beg, since that's how set_buffer_window
16333 sets it. So, we need to check the return value of
16334 compute_window_start_on_continuation_line. (See also
16335 bug#197). */
16336 && XMARKER (w->start)->buffer == current_buffer
16337 && compute_window_start_on_continuation_line (w)
16338 /* It doesn't make sense to force the window start like we
16339 do at label force_start if it is already known that point
16340 will not be fully visible in the resulting window, because
16341 doing so will move point from its correct position
16342 instead of scrolling the window to bring point into view.
16343 See bug#9324. */
16344 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16345 /* A very tall row could need more than the window height,
16346 in which case we accept that it is partially visible. */
16347 && (rtop != 0) == (rbot != 0))
16348 {
16349 w->force_start = true;
16350 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16351 #ifdef GLYPH_DEBUG
16352 debug_method_add (w, "recomputed window start in continuation line");
16353 #endif
16354 goto force_start;
16355 }
16356
16357 #ifdef GLYPH_DEBUG
16358 debug_method_add (w, "same window start");
16359 #endif
16360
16361 /* Try to redisplay starting at same place as before.
16362 If point has not moved off frame, accept the results. */
16363 if (!current_matrix_up_to_date_p
16364 /* Don't use try_window_reusing_current_matrix in this case
16365 because a window scroll function can have changed the
16366 buffer. */
16367 || !NILP (Vwindow_scroll_functions)
16368 || MINI_WINDOW_P (w)
16369 || !(used_current_matrix_p
16370 = try_window_reusing_current_matrix (w)))
16371 {
16372 IF_DEBUG (debug_method_add (w, "1"));
16373 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16374 /* -1 means we need to scroll.
16375 0 means we need new matrices, but fonts_changed
16376 is set in that case, so we will detect it below. */
16377 goto try_to_scroll;
16378 }
16379
16380 if (f->fonts_changed)
16381 goto need_larger_matrices;
16382
16383 if (w->cursor.vpos >= 0)
16384 {
16385 if (!just_this_one_p
16386 || current_buffer->clip_changed
16387 || BEG_UNCHANGED < CHARPOS (startp))
16388 /* Forget any recorded base line for line number display. */
16389 w->base_line_number = 0;
16390
16391 if (!cursor_row_fully_visible_p (w, true, false))
16392 {
16393 clear_glyph_matrix (w->desired_matrix);
16394 last_line_misfit = true;
16395 }
16396 /* Drop through and scroll. */
16397 else
16398 goto done;
16399 }
16400 else
16401 clear_glyph_matrix (w->desired_matrix);
16402 }
16403
16404 try_to_scroll:
16405
16406 /* Redisplay the mode line. Select the buffer properly for that. */
16407 if (!update_mode_line)
16408 {
16409 update_mode_line = true;
16410 w->update_mode_line = true;
16411 }
16412
16413 /* Try to scroll by specified few lines. */
16414 if ((scroll_conservatively
16415 || emacs_scroll_step
16416 || temp_scroll_step
16417 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16418 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16419 && CHARPOS (startp) >= BEGV
16420 && CHARPOS (startp) <= ZV)
16421 {
16422 /* The function returns -1 if new fonts were loaded, 1 if
16423 successful, 0 if not successful. */
16424 int ss = try_scrolling (window, just_this_one_p,
16425 scroll_conservatively,
16426 emacs_scroll_step,
16427 temp_scroll_step, last_line_misfit);
16428 switch (ss)
16429 {
16430 case SCROLLING_SUCCESS:
16431 goto done;
16432
16433 case SCROLLING_NEED_LARGER_MATRICES:
16434 goto need_larger_matrices;
16435
16436 case SCROLLING_FAILED:
16437 break;
16438
16439 default:
16440 emacs_abort ();
16441 }
16442 }
16443
16444 /* Finally, just choose a place to start which positions point
16445 according to user preferences. */
16446
16447 recenter:
16448
16449 #ifdef GLYPH_DEBUG
16450 debug_method_add (w, "recenter");
16451 #endif
16452
16453 /* Forget any previously recorded base line for line number display. */
16454 if (!buffer_unchanged_p)
16455 w->base_line_number = 0;
16456
16457 /* Determine the window start relative to point. */
16458 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16459 it.current_y = it.last_visible_y;
16460 if (centering_position < 0)
16461 {
16462 int window_total_lines
16463 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16464 int margin
16465 = scroll_margin > 0
16466 ? min (scroll_margin, window_total_lines / 4)
16467 : 0;
16468 ptrdiff_t margin_pos = CHARPOS (startp);
16469 Lisp_Object aggressive;
16470 bool scrolling_up;
16471
16472 /* If there is a scroll margin at the top of the window, find
16473 its character position. */
16474 if (margin
16475 /* Cannot call start_display if startp is not in the
16476 accessible region of the buffer. This can happen when we
16477 have just switched to a different buffer and/or changed
16478 its restriction. In that case, startp is initialized to
16479 the character position 1 (BEGV) because we did not yet
16480 have chance to display the buffer even once. */
16481 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16482 {
16483 struct it it1;
16484 void *it1data = NULL;
16485
16486 SAVE_IT (it1, it, it1data);
16487 start_display (&it1, w, startp);
16488 move_it_vertically (&it1, margin * frame_line_height);
16489 margin_pos = IT_CHARPOS (it1);
16490 RESTORE_IT (&it, &it, it1data);
16491 }
16492 scrolling_up = PT > margin_pos;
16493 aggressive =
16494 scrolling_up
16495 ? BVAR (current_buffer, scroll_up_aggressively)
16496 : BVAR (current_buffer, scroll_down_aggressively);
16497
16498 if (!MINI_WINDOW_P (w)
16499 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16500 {
16501 int pt_offset = 0;
16502
16503 /* Setting scroll-conservatively overrides
16504 scroll-*-aggressively. */
16505 if (!scroll_conservatively && NUMBERP (aggressive))
16506 {
16507 double float_amount = XFLOATINT (aggressive);
16508
16509 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16510 if (pt_offset == 0 && float_amount > 0)
16511 pt_offset = 1;
16512 if (pt_offset && margin > 0)
16513 margin -= 1;
16514 }
16515 /* Compute how much to move the window start backward from
16516 point so that point will be displayed where the user
16517 wants it. */
16518 if (scrolling_up)
16519 {
16520 centering_position = it.last_visible_y;
16521 if (pt_offset)
16522 centering_position -= pt_offset;
16523 centering_position -=
16524 (frame_line_height * (1 + margin + last_line_misfit)
16525 + WINDOW_HEADER_LINE_HEIGHT (w));
16526 /* Don't let point enter the scroll margin near top of
16527 the window. */
16528 if (centering_position < margin * frame_line_height)
16529 centering_position = margin * frame_line_height;
16530 }
16531 else
16532 centering_position = margin * frame_line_height + pt_offset;
16533 }
16534 else
16535 /* Set the window start half the height of the window backward
16536 from point. */
16537 centering_position = window_box_height (w) / 2;
16538 }
16539 move_it_vertically_backward (&it, centering_position);
16540
16541 eassert (IT_CHARPOS (it) >= BEGV);
16542
16543 /* The function move_it_vertically_backward may move over more
16544 than the specified y-distance. If it->w is small, e.g. a
16545 mini-buffer window, we may end up in front of the window's
16546 display area. Start displaying at the start of the line
16547 containing PT in this case. */
16548 if (it.current_y <= 0)
16549 {
16550 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16551 move_it_vertically_backward (&it, 0);
16552 it.current_y = 0;
16553 }
16554
16555 it.current_x = it.hpos = 0;
16556
16557 /* Set the window start position here explicitly, to avoid an
16558 infinite loop in case the functions in window-scroll-functions
16559 get errors. */
16560 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16561
16562 /* Run scroll hooks. */
16563 startp = run_window_scroll_functions (window, it.current.pos);
16564
16565 /* Redisplay the window. */
16566 if (!current_matrix_up_to_date_p
16567 || windows_or_buffers_changed
16568 || f->cursor_type_changed
16569 /* Don't use try_window_reusing_current_matrix in this case
16570 because it can have changed the buffer. */
16571 || !NILP (Vwindow_scroll_functions)
16572 || !just_this_one_p
16573 || MINI_WINDOW_P (w)
16574 || !(used_current_matrix_p
16575 = try_window_reusing_current_matrix (w)))
16576 try_window (window, startp, 0);
16577
16578 /* If new fonts have been loaded (due to fontsets), give up. We
16579 have to start a new redisplay since we need to re-adjust glyph
16580 matrices. */
16581 if (f->fonts_changed)
16582 goto need_larger_matrices;
16583
16584 /* If cursor did not appear assume that the middle of the window is
16585 in the first line of the window. Do it again with the next line.
16586 (Imagine a window of height 100, displaying two lines of height
16587 60. Moving back 50 from it->last_visible_y will end in the first
16588 line.) */
16589 if (w->cursor.vpos < 0)
16590 {
16591 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16592 {
16593 clear_glyph_matrix (w->desired_matrix);
16594 move_it_by_lines (&it, 1);
16595 try_window (window, it.current.pos, 0);
16596 }
16597 else if (PT < IT_CHARPOS (it))
16598 {
16599 clear_glyph_matrix (w->desired_matrix);
16600 move_it_by_lines (&it, -1);
16601 try_window (window, it.current.pos, 0);
16602 }
16603 else
16604 {
16605 /* Not much we can do about it. */
16606 }
16607 }
16608
16609 /* Consider the following case: Window starts at BEGV, there is
16610 invisible, intangible text at BEGV, so that display starts at
16611 some point START > BEGV. It can happen that we are called with
16612 PT somewhere between BEGV and START. Try to handle that case,
16613 and similar ones. */
16614 if (w->cursor.vpos < 0)
16615 {
16616 /* First, try locating the proper glyph row for PT. */
16617 struct glyph_row *row =
16618 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16619
16620 /* Sometimes point is at the beginning of invisible text that is
16621 before the 1st character displayed in the row. In that case,
16622 row_containing_pos fails to find the row, because no glyphs
16623 with appropriate buffer positions are present in the row.
16624 Therefore, we next try to find the row which shows the 1st
16625 position after the invisible text. */
16626 if (!row)
16627 {
16628 Lisp_Object val =
16629 get_char_property_and_overlay (make_number (PT), Qinvisible,
16630 Qnil, NULL);
16631
16632 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16633 {
16634 ptrdiff_t alt_pos;
16635 Lisp_Object invis_end =
16636 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16637 Qnil, Qnil);
16638
16639 if (NATNUMP (invis_end))
16640 alt_pos = XFASTINT (invis_end);
16641 else
16642 alt_pos = ZV;
16643 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16644 NULL, 0);
16645 }
16646 }
16647 /* Finally, fall back on the first row of the window after the
16648 header line (if any). This is slightly better than not
16649 displaying the cursor at all. */
16650 if (!row)
16651 {
16652 row = w->current_matrix->rows;
16653 if (row->mode_line_p)
16654 ++row;
16655 }
16656 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16657 }
16658
16659 if (!cursor_row_fully_visible_p (w, false, false))
16660 {
16661 /* If vscroll is enabled, disable it and try again. */
16662 if (w->vscroll)
16663 {
16664 w->vscroll = 0;
16665 clear_glyph_matrix (w->desired_matrix);
16666 goto recenter;
16667 }
16668
16669 /* Users who set scroll-conservatively to a large number want
16670 point just above/below the scroll margin. If we ended up
16671 with point's row partially visible, move the window start to
16672 make that row fully visible and out of the margin. */
16673 if (scroll_conservatively > SCROLL_LIMIT)
16674 {
16675 int window_total_lines
16676 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16677 int margin =
16678 scroll_margin > 0
16679 ? min (scroll_margin, window_total_lines / 4)
16680 : 0;
16681 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16682
16683 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16684 clear_glyph_matrix (w->desired_matrix);
16685 if (1 == try_window (window, it.current.pos,
16686 TRY_WINDOW_CHECK_MARGINS))
16687 goto done;
16688 }
16689
16690 /* If centering point failed to make the whole line visible,
16691 put point at the top instead. That has to make the whole line
16692 visible, if it can be done. */
16693 if (centering_position == 0)
16694 goto done;
16695
16696 clear_glyph_matrix (w->desired_matrix);
16697 centering_position = 0;
16698 goto recenter;
16699 }
16700
16701 done:
16702
16703 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16704 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16705 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16706
16707 /* Display the mode line, if we must. */
16708 if ((update_mode_line
16709 /* If window not full width, must redo its mode line
16710 if (a) the window to its side is being redone and
16711 (b) we do a frame-based redisplay. This is a consequence
16712 of how inverted lines are drawn in frame-based redisplay. */
16713 || (!just_this_one_p
16714 && !FRAME_WINDOW_P (f)
16715 && !WINDOW_FULL_WIDTH_P (w))
16716 /* Line number to display. */
16717 || w->base_line_pos > 0
16718 /* Column number is displayed and different from the one displayed. */
16719 || (w->column_number_displayed != -1
16720 && (w->column_number_displayed != current_column ())))
16721 /* This means that the window has a mode line. */
16722 && (WINDOW_WANTS_MODELINE_P (w)
16723 || WINDOW_WANTS_HEADER_LINE_P (w)))
16724 {
16725
16726 display_mode_lines (w);
16727
16728 /* If mode line height has changed, arrange for a thorough
16729 immediate redisplay using the correct mode line height. */
16730 if (WINDOW_WANTS_MODELINE_P (w)
16731 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16732 {
16733 f->fonts_changed = true;
16734 w->mode_line_height = -1;
16735 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16736 = DESIRED_MODE_LINE_HEIGHT (w);
16737 }
16738
16739 /* If header line height has changed, arrange for a thorough
16740 immediate redisplay using the correct header line height. */
16741 if (WINDOW_WANTS_HEADER_LINE_P (w)
16742 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16743 {
16744 f->fonts_changed = true;
16745 w->header_line_height = -1;
16746 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16747 = DESIRED_HEADER_LINE_HEIGHT (w);
16748 }
16749
16750 if (f->fonts_changed)
16751 goto need_larger_matrices;
16752 }
16753
16754 if (!line_number_displayed && w->base_line_pos != -1)
16755 {
16756 w->base_line_pos = 0;
16757 w->base_line_number = 0;
16758 }
16759
16760 finish_menu_bars:
16761
16762 /* When we reach a frame's selected window, redo the frame's menu bar. */
16763 if (update_mode_line
16764 && EQ (FRAME_SELECTED_WINDOW (f), window))
16765 {
16766 bool redisplay_menu_p;
16767
16768 if (FRAME_WINDOW_P (f))
16769 {
16770 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16771 || defined (HAVE_NS) || defined (USE_GTK)
16772 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16773 #else
16774 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16775 #endif
16776 }
16777 else
16778 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16779
16780 if (redisplay_menu_p)
16781 display_menu_bar (w);
16782
16783 #ifdef HAVE_WINDOW_SYSTEM
16784 if (FRAME_WINDOW_P (f))
16785 {
16786 #if defined (USE_GTK) || defined (HAVE_NS)
16787 if (FRAME_EXTERNAL_TOOL_BAR (f))
16788 redisplay_tool_bar (f);
16789 #else
16790 if (WINDOWP (f->tool_bar_window)
16791 && (FRAME_TOOL_BAR_LINES (f) > 0
16792 || !NILP (Vauto_resize_tool_bars))
16793 && redisplay_tool_bar (f))
16794 ignore_mouse_drag_p = true;
16795 #endif
16796 }
16797 #endif
16798 }
16799
16800 #ifdef HAVE_WINDOW_SYSTEM
16801 if (FRAME_WINDOW_P (f)
16802 && update_window_fringes (w, (just_this_one_p
16803 || (!used_current_matrix_p && !overlay_arrow_seen)
16804 || w->pseudo_window_p)))
16805 {
16806 update_begin (f);
16807 block_input ();
16808 if (draw_window_fringes (w, true))
16809 {
16810 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16811 x_draw_right_divider (w);
16812 else
16813 x_draw_vertical_border (w);
16814 }
16815 unblock_input ();
16816 update_end (f);
16817 }
16818
16819 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16820 x_draw_bottom_divider (w);
16821 #endif /* HAVE_WINDOW_SYSTEM */
16822
16823 /* We go to this label, with fonts_changed set, if it is
16824 necessary to try again using larger glyph matrices.
16825 We have to redeem the scroll bar even in this case,
16826 because the loop in redisplay_internal expects that. */
16827 need_larger_matrices:
16828 ;
16829 finish_scroll_bars:
16830
16831 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16832 {
16833 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16834 /* Set the thumb's position and size. */
16835 set_vertical_scroll_bar (w);
16836
16837 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16838 /* Set the thumb's position and size. */
16839 set_horizontal_scroll_bar (w);
16840
16841 /* Note that we actually used the scroll bar attached to this
16842 window, so it shouldn't be deleted at the end of redisplay. */
16843 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16844 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16845 }
16846
16847 /* Restore current_buffer and value of point in it. The window
16848 update may have changed the buffer, so first make sure `opoint'
16849 is still valid (Bug#6177). */
16850 if (CHARPOS (opoint) < BEGV)
16851 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16852 else if (CHARPOS (opoint) > ZV)
16853 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16854 else
16855 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16856
16857 set_buffer_internal_1 (old);
16858 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16859 shorter. This can be caused by log truncation in *Messages*. */
16860 if (CHARPOS (lpoint) <= ZV)
16861 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16862
16863 unbind_to (count, Qnil);
16864 }
16865
16866
16867 /* Build the complete desired matrix of WINDOW with a window start
16868 buffer position POS.
16869
16870 Value is 1 if successful. It is zero if fonts were loaded during
16871 redisplay which makes re-adjusting glyph matrices necessary, and -1
16872 if point would appear in the scroll margins.
16873 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16874 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16875 set in FLAGS.) */
16876
16877 int
16878 try_window (Lisp_Object window, struct text_pos pos, int flags)
16879 {
16880 struct window *w = XWINDOW (window);
16881 struct it it;
16882 struct glyph_row *last_text_row = NULL;
16883 struct frame *f = XFRAME (w->frame);
16884 int frame_line_height = default_line_pixel_height (w);
16885
16886 /* Make POS the new window start. */
16887 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16888
16889 /* Mark cursor position as unknown. No overlay arrow seen. */
16890 w->cursor.vpos = -1;
16891 overlay_arrow_seen = false;
16892
16893 /* Initialize iterator and info to start at POS. */
16894 start_display (&it, w, pos);
16895 it.glyph_row->reversed_p = false;
16896
16897 /* Display all lines of W. */
16898 while (it.current_y < it.last_visible_y)
16899 {
16900 if (display_line (&it))
16901 last_text_row = it.glyph_row - 1;
16902 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16903 return 0;
16904 }
16905
16906 /* Don't let the cursor end in the scroll margins. */
16907 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16908 && !MINI_WINDOW_P (w))
16909 {
16910 int this_scroll_margin;
16911 int window_total_lines
16912 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16913
16914 if (scroll_margin > 0)
16915 {
16916 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16917 this_scroll_margin *= frame_line_height;
16918 }
16919 else
16920 this_scroll_margin = 0;
16921
16922 if ((w->cursor.y >= 0 /* not vscrolled */
16923 && w->cursor.y < this_scroll_margin
16924 && CHARPOS (pos) > BEGV
16925 && IT_CHARPOS (it) < ZV)
16926 /* rms: considering make_cursor_line_fully_visible_p here
16927 seems to give wrong results. We don't want to recenter
16928 when the last line is partly visible, we want to allow
16929 that case to be handled in the usual way. */
16930 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16931 {
16932 w->cursor.vpos = -1;
16933 clear_glyph_matrix (w->desired_matrix);
16934 return -1;
16935 }
16936 }
16937
16938 /* If bottom moved off end of frame, change mode line percentage. */
16939 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16940 w->update_mode_line = true;
16941
16942 /* Set window_end_pos to the offset of the last character displayed
16943 on the window from the end of current_buffer. Set
16944 window_end_vpos to its row number. */
16945 if (last_text_row)
16946 {
16947 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16948 adjust_window_ends (w, last_text_row, false);
16949 eassert
16950 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16951 w->window_end_vpos)));
16952 }
16953 else
16954 {
16955 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16956 w->window_end_pos = Z - ZV;
16957 w->window_end_vpos = 0;
16958 }
16959
16960 /* But that is not valid info until redisplay finishes. */
16961 w->window_end_valid = false;
16962 return 1;
16963 }
16964
16965
16966 \f
16967 /************************************************************************
16968 Window redisplay reusing current matrix when buffer has not changed
16969 ************************************************************************/
16970
16971 /* Try redisplay of window W showing an unchanged buffer with a
16972 different window start than the last time it was displayed by
16973 reusing its current matrix. Value is true if successful.
16974 W->start is the new window start. */
16975
16976 static bool
16977 try_window_reusing_current_matrix (struct window *w)
16978 {
16979 struct frame *f = XFRAME (w->frame);
16980 struct glyph_row *bottom_row;
16981 struct it it;
16982 struct run run;
16983 struct text_pos start, new_start;
16984 int nrows_scrolled, i;
16985 struct glyph_row *last_text_row;
16986 struct glyph_row *last_reused_text_row;
16987 struct glyph_row *start_row;
16988 int start_vpos, min_y, max_y;
16989
16990 #ifdef GLYPH_DEBUG
16991 if (inhibit_try_window_reusing)
16992 return false;
16993 #endif
16994
16995 if (/* This function doesn't handle terminal frames. */
16996 !FRAME_WINDOW_P (f)
16997 /* Don't try to reuse the display if windows have been split
16998 or such. */
16999 || windows_or_buffers_changed
17000 || f->cursor_type_changed)
17001 return false;
17002
17003 /* Can't do this if showing trailing whitespace. */
17004 if (!NILP (Vshow_trailing_whitespace))
17005 return false;
17006
17007 /* If top-line visibility has changed, give up. */
17008 if (WINDOW_WANTS_HEADER_LINE_P (w)
17009 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17010 return false;
17011
17012 /* Give up if old or new display is scrolled vertically. We could
17013 make this function handle this, but right now it doesn't. */
17014 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17015 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17016 return false;
17017
17018 /* The variable new_start now holds the new window start. The old
17019 start `start' can be determined from the current matrix. */
17020 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17021 start = start_row->minpos;
17022 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17023
17024 /* Clear the desired matrix for the display below. */
17025 clear_glyph_matrix (w->desired_matrix);
17026
17027 if (CHARPOS (new_start) <= CHARPOS (start))
17028 {
17029 /* Don't use this method if the display starts with an ellipsis
17030 displayed for invisible text. It's not easy to handle that case
17031 below, and it's certainly not worth the effort since this is
17032 not a frequent case. */
17033 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17034 return false;
17035
17036 IF_DEBUG (debug_method_add (w, "twu1"));
17037
17038 /* Display up to a row that can be reused. The variable
17039 last_text_row is set to the last row displayed that displays
17040 text. Note that it.vpos == 0 if or if not there is a
17041 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17042 start_display (&it, w, new_start);
17043 w->cursor.vpos = -1;
17044 last_text_row = last_reused_text_row = NULL;
17045
17046 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17047 {
17048 /* If we have reached into the characters in the START row,
17049 that means the line boundaries have changed. So we
17050 can't start copying with the row START. Maybe it will
17051 work to start copying with the following row. */
17052 while (IT_CHARPOS (it) > CHARPOS (start))
17053 {
17054 /* Advance to the next row as the "start". */
17055 start_row++;
17056 start = start_row->minpos;
17057 /* If there are no more rows to try, or just one, give up. */
17058 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17059 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17060 || CHARPOS (start) == ZV)
17061 {
17062 clear_glyph_matrix (w->desired_matrix);
17063 return false;
17064 }
17065
17066 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17067 }
17068 /* If we have reached alignment, we can copy the rest of the
17069 rows. */
17070 if (IT_CHARPOS (it) == CHARPOS (start)
17071 /* Don't accept "alignment" inside a display vector,
17072 since start_row could have started in the middle of
17073 that same display vector (thus their character
17074 positions match), and we have no way of telling if
17075 that is the case. */
17076 && it.current.dpvec_index < 0)
17077 break;
17078
17079 it.glyph_row->reversed_p = false;
17080 if (display_line (&it))
17081 last_text_row = it.glyph_row - 1;
17082
17083 }
17084
17085 /* A value of current_y < last_visible_y means that we stopped
17086 at the previous window start, which in turn means that we
17087 have at least one reusable row. */
17088 if (it.current_y < it.last_visible_y)
17089 {
17090 struct glyph_row *row;
17091
17092 /* IT.vpos always starts from 0; it counts text lines. */
17093 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17094
17095 /* Find PT if not already found in the lines displayed. */
17096 if (w->cursor.vpos < 0)
17097 {
17098 int dy = it.current_y - start_row->y;
17099
17100 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17101 row = row_containing_pos (w, PT, row, NULL, dy);
17102 if (row)
17103 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17104 dy, nrows_scrolled);
17105 else
17106 {
17107 clear_glyph_matrix (w->desired_matrix);
17108 return false;
17109 }
17110 }
17111
17112 /* Scroll the display. Do it before the current matrix is
17113 changed. The problem here is that update has not yet
17114 run, i.e. part of the current matrix is not up to date.
17115 scroll_run_hook will clear the cursor, and use the
17116 current matrix to get the height of the row the cursor is
17117 in. */
17118 run.current_y = start_row->y;
17119 run.desired_y = it.current_y;
17120 run.height = it.last_visible_y - it.current_y;
17121
17122 if (run.height > 0 && run.current_y != run.desired_y)
17123 {
17124 update_begin (f);
17125 FRAME_RIF (f)->update_window_begin_hook (w);
17126 FRAME_RIF (f)->clear_window_mouse_face (w);
17127 FRAME_RIF (f)->scroll_run_hook (w, &run);
17128 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17129 update_end (f);
17130 }
17131
17132 /* Shift current matrix down by nrows_scrolled lines. */
17133 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17134 rotate_matrix (w->current_matrix,
17135 start_vpos,
17136 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17137 nrows_scrolled);
17138
17139 /* Disable lines that must be updated. */
17140 for (i = 0; i < nrows_scrolled; ++i)
17141 (start_row + i)->enabled_p = false;
17142
17143 /* Re-compute Y positions. */
17144 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17145 max_y = it.last_visible_y;
17146 for (row = start_row + nrows_scrolled;
17147 row < bottom_row;
17148 ++row)
17149 {
17150 row->y = it.current_y;
17151 row->visible_height = row->height;
17152
17153 if (row->y < min_y)
17154 row->visible_height -= min_y - row->y;
17155 if (row->y + row->height > max_y)
17156 row->visible_height -= row->y + row->height - max_y;
17157 if (row->fringe_bitmap_periodic_p)
17158 row->redraw_fringe_bitmaps_p = true;
17159
17160 it.current_y += row->height;
17161
17162 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17163 last_reused_text_row = row;
17164 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17165 break;
17166 }
17167
17168 /* Disable lines in the current matrix which are now
17169 below the window. */
17170 for (++row; row < bottom_row; ++row)
17171 row->enabled_p = row->mode_line_p = false;
17172 }
17173
17174 /* Update window_end_pos etc.; last_reused_text_row is the last
17175 reused row from the current matrix containing text, if any.
17176 The value of last_text_row is the last displayed line
17177 containing text. */
17178 if (last_reused_text_row)
17179 adjust_window_ends (w, last_reused_text_row, true);
17180 else if (last_text_row)
17181 adjust_window_ends (w, last_text_row, false);
17182 else
17183 {
17184 /* This window must be completely empty. */
17185 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17186 w->window_end_pos = Z - ZV;
17187 w->window_end_vpos = 0;
17188 }
17189 w->window_end_valid = false;
17190
17191 /* Update hint: don't try scrolling again in update_window. */
17192 w->desired_matrix->no_scrolling_p = true;
17193
17194 #ifdef GLYPH_DEBUG
17195 debug_method_add (w, "try_window_reusing_current_matrix 1");
17196 #endif
17197 return true;
17198 }
17199 else if (CHARPOS (new_start) > CHARPOS (start))
17200 {
17201 struct glyph_row *pt_row, *row;
17202 struct glyph_row *first_reusable_row;
17203 struct glyph_row *first_row_to_display;
17204 int dy;
17205 int yb = window_text_bottom_y (w);
17206
17207 /* Find the row starting at new_start, if there is one. Don't
17208 reuse a partially visible line at the end. */
17209 first_reusable_row = start_row;
17210 while (first_reusable_row->enabled_p
17211 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17212 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17213 < CHARPOS (new_start)))
17214 ++first_reusable_row;
17215
17216 /* Give up if there is no row to reuse. */
17217 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17218 || !first_reusable_row->enabled_p
17219 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17220 != CHARPOS (new_start)))
17221 return false;
17222
17223 /* We can reuse fully visible rows beginning with
17224 first_reusable_row to the end of the window. Set
17225 first_row_to_display to the first row that cannot be reused.
17226 Set pt_row to the row containing point, if there is any. */
17227 pt_row = NULL;
17228 for (first_row_to_display = first_reusable_row;
17229 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17230 ++first_row_to_display)
17231 {
17232 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17233 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17234 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17235 && first_row_to_display->ends_at_zv_p
17236 && pt_row == NULL)))
17237 pt_row = first_row_to_display;
17238 }
17239
17240 /* Start displaying at the start of first_row_to_display. */
17241 eassert (first_row_to_display->y < yb);
17242 init_to_row_start (&it, w, first_row_to_display);
17243
17244 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17245 - start_vpos);
17246 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17247 - nrows_scrolled);
17248 it.current_y = (first_row_to_display->y - first_reusable_row->y
17249 + WINDOW_HEADER_LINE_HEIGHT (w));
17250
17251 /* Display lines beginning with first_row_to_display in the
17252 desired matrix. Set last_text_row to the last row displayed
17253 that displays text. */
17254 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17255 if (pt_row == NULL)
17256 w->cursor.vpos = -1;
17257 last_text_row = NULL;
17258 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17259 if (display_line (&it))
17260 last_text_row = it.glyph_row - 1;
17261
17262 /* If point is in a reused row, adjust y and vpos of the cursor
17263 position. */
17264 if (pt_row)
17265 {
17266 w->cursor.vpos -= nrows_scrolled;
17267 w->cursor.y -= first_reusable_row->y - start_row->y;
17268 }
17269
17270 /* Give up if point isn't in a row displayed or reused. (This
17271 also handles the case where w->cursor.vpos < nrows_scrolled
17272 after the calls to display_line, which can happen with scroll
17273 margins. See bug#1295.) */
17274 if (w->cursor.vpos < 0)
17275 {
17276 clear_glyph_matrix (w->desired_matrix);
17277 return false;
17278 }
17279
17280 /* Scroll the display. */
17281 run.current_y = first_reusable_row->y;
17282 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17283 run.height = it.last_visible_y - run.current_y;
17284 dy = run.current_y - run.desired_y;
17285
17286 if (run.height)
17287 {
17288 update_begin (f);
17289 FRAME_RIF (f)->update_window_begin_hook (w);
17290 FRAME_RIF (f)->clear_window_mouse_face (w);
17291 FRAME_RIF (f)->scroll_run_hook (w, &run);
17292 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17293 update_end (f);
17294 }
17295
17296 /* Adjust Y positions of reused rows. */
17297 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17298 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17299 max_y = it.last_visible_y;
17300 for (row = first_reusable_row; row < first_row_to_display; ++row)
17301 {
17302 row->y -= dy;
17303 row->visible_height = row->height;
17304 if (row->y < min_y)
17305 row->visible_height -= min_y - row->y;
17306 if (row->y + row->height > max_y)
17307 row->visible_height -= row->y + row->height - max_y;
17308 if (row->fringe_bitmap_periodic_p)
17309 row->redraw_fringe_bitmaps_p = true;
17310 }
17311
17312 /* Scroll the current matrix. */
17313 eassert (nrows_scrolled > 0);
17314 rotate_matrix (w->current_matrix,
17315 start_vpos,
17316 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17317 -nrows_scrolled);
17318
17319 /* Disable rows not reused. */
17320 for (row -= nrows_scrolled; row < bottom_row; ++row)
17321 row->enabled_p = false;
17322
17323 /* Point may have moved to a different line, so we cannot assume that
17324 the previous cursor position is valid; locate the correct row. */
17325 if (pt_row)
17326 {
17327 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17328 row < bottom_row
17329 && PT >= MATRIX_ROW_END_CHARPOS (row)
17330 && !row->ends_at_zv_p;
17331 row++)
17332 {
17333 w->cursor.vpos++;
17334 w->cursor.y = row->y;
17335 }
17336 if (row < bottom_row)
17337 {
17338 /* Can't simply scan the row for point with
17339 bidi-reordered glyph rows. Let set_cursor_from_row
17340 figure out where to put the cursor, and if it fails,
17341 give up. */
17342 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17343 {
17344 if (!set_cursor_from_row (w, row, w->current_matrix,
17345 0, 0, 0, 0))
17346 {
17347 clear_glyph_matrix (w->desired_matrix);
17348 return false;
17349 }
17350 }
17351 else
17352 {
17353 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17354 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17355
17356 for (; glyph < end
17357 && (!BUFFERP (glyph->object)
17358 || glyph->charpos < PT);
17359 glyph++)
17360 {
17361 w->cursor.hpos++;
17362 w->cursor.x += glyph->pixel_width;
17363 }
17364 }
17365 }
17366 }
17367
17368 /* Adjust window end. A null value of last_text_row means that
17369 the window end is in reused rows which in turn means that
17370 only its vpos can have changed. */
17371 if (last_text_row)
17372 adjust_window_ends (w, last_text_row, false);
17373 else
17374 w->window_end_vpos -= nrows_scrolled;
17375
17376 w->window_end_valid = false;
17377 w->desired_matrix->no_scrolling_p = true;
17378
17379 #ifdef GLYPH_DEBUG
17380 debug_method_add (w, "try_window_reusing_current_matrix 2");
17381 #endif
17382 return true;
17383 }
17384
17385 return false;
17386 }
17387
17388
17389 \f
17390 /************************************************************************
17391 Window redisplay reusing current matrix when buffer has changed
17392 ************************************************************************/
17393
17394 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17395 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17396 ptrdiff_t *, ptrdiff_t *);
17397 static struct glyph_row *
17398 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17399 struct glyph_row *);
17400
17401
17402 /* Return the last row in MATRIX displaying text. If row START is
17403 non-null, start searching with that row. IT gives the dimensions
17404 of the display. Value is null if matrix is empty; otherwise it is
17405 a pointer to the row found. */
17406
17407 static struct glyph_row *
17408 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17409 struct glyph_row *start)
17410 {
17411 struct glyph_row *row, *row_found;
17412
17413 /* Set row_found to the last row in IT->w's current matrix
17414 displaying text. The loop looks funny but think of partially
17415 visible lines. */
17416 row_found = NULL;
17417 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17418 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17419 {
17420 eassert (row->enabled_p);
17421 row_found = row;
17422 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17423 break;
17424 ++row;
17425 }
17426
17427 return row_found;
17428 }
17429
17430
17431 /* Return the last row in the current matrix of W that is not affected
17432 by changes at the start of current_buffer that occurred since W's
17433 current matrix was built. Value is null if no such row exists.
17434
17435 BEG_UNCHANGED us the number of characters unchanged at the start of
17436 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17437 first changed character in current_buffer. Characters at positions <
17438 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17439 when the current matrix was built. */
17440
17441 static struct glyph_row *
17442 find_last_unchanged_at_beg_row (struct window *w)
17443 {
17444 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17445 struct glyph_row *row;
17446 struct glyph_row *row_found = NULL;
17447 int yb = window_text_bottom_y (w);
17448
17449 /* Find the last row displaying unchanged text. */
17450 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17451 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17452 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17453 ++row)
17454 {
17455 if (/* If row ends before first_changed_pos, it is unchanged,
17456 except in some case. */
17457 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17458 /* When row ends in ZV and we write at ZV it is not
17459 unchanged. */
17460 && !row->ends_at_zv_p
17461 /* When first_changed_pos is the end of a continued line,
17462 row is not unchanged because it may be no longer
17463 continued. */
17464 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17465 && (row->continued_p
17466 || row->exact_window_width_line_p))
17467 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17468 needs to be recomputed, so don't consider this row as
17469 unchanged. This happens when the last line was
17470 bidi-reordered and was killed immediately before this
17471 redisplay cycle. In that case, ROW->end stores the
17472 buffer position of the first visual-order character of
17473 the killed text, which is now beyond ZV. */
17474 && CHARPOS (row->end.pos) <= ZV)
17475 row_found = row;
17476
17477 /* Stop if last visible row. */
17478 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17479 break;
17480 }
17481
17482 return row_found;
17483 }
17484
17485
17486 /* Find the first glyph row in the current matrix of W that is not
17487 affected by changes at the end of current_buffer since the
17488 time W's current matrix was built.
17489
17490 Return in *DELTA the number of chars by which buffer positions in
17491 unchanged text at the end of current_buffer must be adjusted.
17492
17493 Return in *DELTA_BYTES the corresponding number of bytes.
17494
17495 Value is null if no such row exists, i.e. all rows are affected by
17496 changes. */
17497
17498 static struct glyph_row *
17499 find_first_unchanged_at_end_row (struct window *w,
17500 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17501 {
17502 struct glyph_row *row;
17503 struct glyph_row *row_found = NULL;
17504
17505 *delta = *delta_bytes = 0;
17506
17507 /* Display must not have been paused, otherwise the current matrix
17508 is not up to date. */
17509 eassert (w->window_end_valid);
17510
17511 /* A value of window_end_pos >= END_UNCHANGED means that the window
17512 end is in the range of changed text. If so, there is no
17513 unchanged row at the end of W's current matrix. */
17514 if (w->window_end_pos >= END_UNCHANGED)
17515 return NULL;
17516
17517 /* Set row to the last row in W's current matrix displaying text. */
17518 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17519
17520 /* If matrix is entirely empty, no unchanged row exists. */
17521 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17522 {
17523 /* The value of row is the last glyph row in the matrix having a
17524 meaningful buffer position in it. The end position of row
17525 corresponds to window_end_pos. This allows us to translate
17526 buffer positions in the current matrix to current buffer
17527 positions for characters not in changed text. */
17528 ptrdiff_t Z_old =
17529 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17530 ptrdiff_t Z_BYTE_old =
17531 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17532 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17533 struct glyph_row *first_text_row
17534 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17535
17536 *delta = Z - Z_old;
17537 *delta_bytes = Z_BYTE - Z_BYTE_old;
17538
17539 /* Set last_unchanged_pos to the buffer position of the last
17540 character in the buffer that has not been changed. Z is the
17541 index + 1 of the last character in current_buffer, i.e. by
17542 subtracting END_UNCHANGED we get the index of the last
17543 unchanged character, and we have to add BEG to get its buffer
17544 position. */
17545 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17546 last_unchanged_pos_old = last_unchanged_pos - *delta;
17547
17548 /* Search backward from ROW for a row displaying a line that
17549 starts at a minimum position >= last_unchanged_pos_old. */
17550 for (; row > first_text_row; --row)
17551 {
17552 /* This used to abort, but it can happen.
17553 It is ok to just stop the search instead here. KFS. */
17554 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17555 break;
17556
17557 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17558 row_found = row;
17559 }
17560 }
17561
17562 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17563
17564 return row_found;
17565 }
17566
17567
17568 /* Make sure that glyph rows in the current matrix of window W
17569 reference the same glyph memory as corresponding rows in the
17570 frame's frame matrix. This function is called after scrolling W's
17571 current matrix on a terminal frame in try_window_id and
17572 try_window_reusing_current_matrix. */
17573
17574 static void
17575 sync_frame_with_window_matrix_rows (struct window *w)
17576 {
17577 struct frame *f = XFRAME (w->frame);
17578 struct glyph_row *window_row, *window_row_end, *frame_row;
17579
17580 /* Preconditions: W must be a leaf window and full-width. Its frame
17581 must have a frame matrix. */
17582 eassert (BUFFERP (w->contents));
17583 eassert (WINDOW_FULL_WIDTH_P (w));
17584 eassert (!FRAME_WINDOW_P (f));
17585
17586 /* If W is a full-width window, glyph pointers in W's current matrix
17587 have, by definition, to be the same as glyph pointers in the
17588 corresponding frame matrix. Note that frame matrices have no
17589 marginal areas (see build_frame_matrix). */
17590 window_row = w->current_matrix->rows;
17591 window_row_end = window_row + w->current_matrix->nrows;
17592 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17593 while (window_row < window_row_end)
17594 {
17595 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17596 struct glyph *end = window_row->glyphs[LAST_AREA];
17597
17598 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17599 frame_row->glyphs[TEXT_AREA] = start;
17600 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17601 frame_row->glyphs[LAST_AREA] = end;
17602
17603 /* Disable frame rows whose corresponding window rows have
17604 been disabled in try_window_id. */
17605 if (!window_row->enabled_p)
17606 frame_row->enabled_p = false;
17607
17608 ++window_row, ++frame_row;
17609 }
17610 }
17611
17612
17613 /* Find the glyph row in window W containing CHARPOS. Consider all
17614 rows between START and END (not inclusive). END null means search
17615 all rows to the end of the display area of W. Value is the row
17616 containing CHARPOS or null. */
17617
17618 struct glyph_row *
17619 row_containing_pos (struct window *w, ptrdiff_t charpos,
17620 struct glyph_row *start, struct glyph_row *end, int dy)
17621 {
17622 struct glyph_row *row = start;
17623 struct glyph_row *best_row = NULL;
17624 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17625 int last_y;
17626
17627 /* If we happen to start on a header-line, skip that. */
17628 if (row->mode_line_p)
17629 ++row;
17630
17631 if ((end && row >= end) || !row->enabled_p)
17632 return NULL;
17633
17634 last_y = window_text_bottom_y (w) - dy;
17635
17636 while (true)
17637 {
17638 /* Give up if we have gone too far. */
17639 if (end && row >= end)
17640 return NULL;
17641 /* This formerly returned if they were equal.
17642 I think that both quantities are of a "last plus one" type;
17643 if so, when they are equal, the row is within the screen. -- rms. */
17644 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17645 return NULL;
17646
17647 /* If it is in this row, return this row. */
17648 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17649 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17650 /* The end position of a row equals the start
17651 position of the next row. If CHARPOS is there, we
17652 would rather consider it displayed in the next
17653 line, except when this line ends in ZV. */
17654 && !row_for_charpos_p (row, charpos)))
17655 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17656 {
17657 struct glyph *g;
17658
17659 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17660 || (!best_row && !row->continued_p))
17661 return row;
17662 /* In bidi-reordered rows, there could be several rows whose
17663 edges surround CHARPOS, all of these rows belonging to
17664 the same continued line. We need to find the row which
17665 fits CHARPOS the best. */
17666 for (g = row->glyphs[TEXT_AREA];
17667 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17668 g++)
17669 {
17670 if (!STRINGP (g->object))
17671 {
17672 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17673 {
17674 mindif = eabs (g->charpos - charpos);
17675 best_row = row;
17676 /* Exact match always wins. */
17677 if (mindif == 0)
17678 return best_row;
17679 }
17680 }
17681 }
17682 }
17683 else if (best_row && !row->continued_p)
17684 return best_row;
17685 ++row;
17686 }
17687 }
17688
17689
17690 /* Try to redisplay window W by reusing its existing display. W's
17691 current matrix must be up to date when this function is called,
17692 i.e., window_end_valid must be true.
17693
17694 Value is
17695
17696 >= 1 if successful, i.e. display has been updated
17697 specifically:
17698 1 means the changes were in front of a newline that precedes
17699 the window start, and the whole current matrix was reused
17700 2 means the changes were after the last position displayed
17701 in the window, and the whole current matrix was reused
17702 3 means portions of the current matrix were reused, while
17703 some of the screen lines were redrawn
17704 -1 if redisplay with same window start is known not to succeed
17705 0 if otherwise unsuccessful
17706
17707 The following steps are performed:
17708
17709 1. Find the last row in the current matrix of W that is not
17710 affected by changes at the start of current_buffer. If no such row
17711 is found, give up.
17712
17713 2. Find the first row in W's current matrix that is not affected by
17714 changes at the end of current_buffer. Maybe there is no such row.
17715
17716 3. Display lines beginning with the row + 1 found in step 1 to the
17717 row found in step 2 or, if step 2 didn't find a row, to the end of
17718 the window.
17719
17720 4. If cursor is not known to appear on the window, give up.
17721
17722 5. If display stopped at the row found in step 2, scroll the
17723 display and current matrix as needed.
17724
17725 6. Maybe display some lines at the end of W, if we must. This can
17726 happen under various circumstances, like a partially visible line
17727 becoming fully visible, or because newly displayed lines are displayed
17728 in smaller font sizes.
17729
17730 7. Update W's window end information. */
17731
17732 static int
17733 try_window_id (struct window *w)
17734 {
17735 struct frame *f = XFRAME (w->frame);
17736 struct glyph_matrix *current_matrix = w->current_matrix;
17737 struct glyph_matrix *desired_matrix = w->desired_matrix;
17738 struct glyph_row *last_unchanged_at_beg_row;
17739 struct glyph_row *first_unchanged_at_end_row;
17740 struct glyph_row *row;
17741 struct glyph_row *bottom_row;
17742 int bottom_vpos;
17743 struct it it;
17744 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17745 int dvpos, dy;
17746 struct text_pos start_pos;
17747 struct run run;
17748 int first_unchanged_at_end_vpos = 0;
17749 struct glyph_row *last_text_row, *last_text_row_at_end;
17750 struct text_pos start;
17751 ptrdiff_t first_changed_charpos, last_changed_charpos;
17752
17753 #ifdef GLYPH_DEBUG
17754 if (inhibit_try_window_id)
17755 return 0;
17756 #endif
17757
17758 /* This is handy for debugging. */
17759 #if false
17760 #define GIVE_UP(X) \
17761 do { \
17762 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17763 return 0; \
17764 } while (false)
17765 #else
17766 #define GIVE_UP(X) return 0
17767 #endif
17768
17769 SET_TEXT_POS_FROM_MARKER (start, w->start);
17770
17771 /* Don't use this for mini-windows because these can show
17772 messages and mini-buffers, and we don't handle that here. */
17773 if (MINI_WINDOW_P (w))
17774 GIVE_UP (1);
17775
17776 /* This flag is used to prevent redisplay optimizations. */
17777 if (windows_or_buffers_changed || f->cursor_type_changed)
17778 GIVE_UP (2);
17779
17780 /* This function's optimizations cannot be used if overlays have
17781 changed in the buffer displayed by the window, so give up if they
17782 have. */
17783 if (w->last_overlay_modified != OVERLAY_MODIFF)
17784 GIVE_UP (21);
17785
17786 /* Verify that narrowing has not changed.
17787 Also verify that we were not told to prevent redisplay optimizations.
17788 It would be nice to further
17789 reduce the number of cases where this prevents try_window_id. */
17790 if (current_buffer->clip_changed
17791 || current_buffer->prevent_redisplay_optimizations_p)
17792 GIVE_UP (3);
17793
17794 /* Window must either use window-based redisplay or be full width. */
17795 if (!FRAME_WINDOW_P (f)
17796 && (!FRAME_LINE_INS_DEL_OK (f)
17797 || !WINDOW_FULL_WIDTH_P (w)))
17798 GIVE_UP (4);
17799
17800 /* Give up if point is known NOT to appear in W. */
17801 if (PT < CHARPOS (start))
17802 GIVE_UP (5);
17803
17804 /* Another way to prevent redisplay optimizations. */
17805 if (w->last_modified == 0)
17806 GIVE_UP (6);
17807
17808 /* Verify that window is not hscrolled. */
17809 if (w->hscroll != 0)
17810 GIVE_UP (7);
17811
17812 /* Verify that display wasn't paused. */
17813 if (!w->window_end_valid)
17814 GIVE_UP (8);
17815
17816 /* Likewise if highlighting trailing whitespace. */
17817 if (!NILP (Vshow_trailing_whitespace))
17818 GIVE_UP (11);
17819
17820 /* Can't use this if overlay arrow position and/or string have
17821 changed. */
17822 if (overlay_arrows_changed_p ())
17823 GIVE_UP (12);
17824
17825 /* When word-wrap is on, adding a space to the first word of a
17826 wrapped line can change the wrap position, altering the line
17827 above it. It might be worthwhile to handle this more
17828 intelligently, but for now just redisplay from scratch. */
17829 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17830 GIVE_UP (21);
17831
17832 /* Under bidi reordering, adding or deleting a character in the
17833 beginning of a paragraph, before the first strong directional
17834 character, can change the base direction of the paragraph (unless
17835 the buffer specifies a fixed paragraph direction), which will
17836 require to redisplay the whole paragraph. It might be worthwhile
17837 to find the paragraph limits and widen the range of redisplayed
17838 lines to that, but for now just give up this optimization and
17839 redisplay from scratch. */
17840 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17841 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17842 GIVE_UP (22);
17843
17844 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17845 only if buffer has really changed. The reason is that the gap is
17846 initially at Z for freshly visited files. The code below would
17847 set end_unchanged to 0 in that case. */
17848 if (MODIFF > SAVE_MODIFF
17849 /* This seems to happen sometimes after saving a buffer. */
17850 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17851 {
17852 if (GPT - BEG < BEG_UNCHANGED)
17853 BEG_UNCHANGED = GPT - BEG;
17854 if (Z - GPT < END_UNCHANGED)
17855 END_UNCHANGED = Z - GPT;
17856 }
17857
17858 /* The position of the first and last character that has been changed. */
17859 first_changed_charpos = BEG + BEG_UNCHANGED;
17860 last_changed_charpos = Z - END_UNCHANGED;
17861
17862 /* If window starts after a line end, and the last change is in
17863 front of that newline, then changes don't affect the display.
17864 This case happens with stealth-fontification. Note that although
17865 the display is unchanged, glyph positions in the matrix have to
17866 be adjusted, of course. */
17867 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17868 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17869 && ((last_changed_charpos < CHARPOS (start)
17870 && CHARPOS (start) == BEGV)
17871 || (last_changed_charpos < CHARPOS (start) - 1
17872 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17873 {
17874 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17875 struct glyph_row *r0;
17876
17877 /* Compute how many chars/bytes have been added to or removed
17878 from the buffer. */
17879 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17880 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17881 Z_delta = Z - Z_old;
17882 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17883
17884 /* Give up if PT is not in the window. Note that it already has
17885 been checked at the start of try_window_id that PT is not in
17886 front of the window start. */
17887 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17888 GIVE_UP (13);
17889
17890 /* If window start is unchanged, we can reuse the whole matrix
17891 as is, after adjusting glyph positions. No need to compute
17892 the window end again, since its offset from Z hasn't changed. */
17893 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17894 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17895 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17896 /* PT must not be in a partially visible line. */
17897 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17898 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17899 {
17900 /* Adjust positions in the glyph matrix. */
17901 if (Z_delta || Z_delta_bytes)
17902 {
17903 struct glyph_row *r1
17904 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17905 increment_matrix_positions (w->current_matrix,
17906 MATRIX_ROW_VPOS (r0, current_matrix),
17907 MATRIX_ROW_VPOS (r1, current_matrix),
17908 Z_delta, Z_delta_bytes);
17909 }
17910
17911 /* Set the cursor. */
17912 row = row_containing_pos (w, PT, r0, NULL, 0);
17913 if (row)
17914 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17915 return 1;
17916 }
17917 }
17918
17919 /* Handle the case that changes are all below what is displayed in
17920 the window, and that PT is in the window. This shortcut cannot
17921 be taken if ZV is visible in the window, and text has been added
17922 there that is visible in the window. */
17923 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17924 /* ZV is not visible in the window, or there are no
17925 changes at ZV, actually. */
17926 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17927 || first_changed_charpos == last_changed_charpos))
17928 {
17929 struct glyph_row *r0;
17930
17931 /* Give up if PT is not in the window. Note that it already has
17932 been checked at the start of try_window_id that PT is not in
17933 front of the window start. */
17934 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17935 GIVE_UP (14);
17936
17937 /* If window start is unchanged, we can reuse the whole matrix
17938 as is, without changing glyph positions since no text has
17939 been added/removed in front of the window end. */
17940 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17941 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17942 /* PT must not be in a partially visible line. */
17943 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17944 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17945 {
17946 /* We have to compute the window end anew since text
17947 could have been added/removed after it. */
17948 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17949 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17950
17951 /* Set the cursor. */
17952 row = row_containing_pos (w, PT, r0, NULL, 0);
17953 if (row)
17954 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17955 return 2;
17956 }
17957 }
17958
17959 /* Give up if window start is in the changed area.
17960
17961 The condition used to read
17962
17963 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17964
17965 but why that was tested escapes me at the moment. */
17966 if (CHARPOS (start) >= first_changed_charpos
17967 && CHARPOS (start) <= last_changed_charpos)
17968 GIVE_UP (15);
17969
17970 /* Check that window start agrees with the start of the first glyph
17971 row in its current matrix. Check this after we know the window
17972 start is not in changed text, otherwise positions would not be
17973 comparable. */
17974 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17975 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17976 GIVE_UP (16);
17977
17978 /* Give up if the window ends in strings. Overlay strings
17979 at the end are difficult to handle, so don't try. */
17980 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17981 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17982 GIVE_UP (20);
17983
17984 /* Compute the position at which we have to start displaying new
17985 lines. Some of the lines at the top of the window might be
17986 reusable because they are not displaying changed text. Find the
17987 last row in W's current matrix not affected by changes at the
17988 start of current_buffer. Value is null if changes start in the
17989 first line of window. */
17990 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17991 if (last_unchanged_at_beg_row)
17992 {
17993 /* Avoid starting to display in the middle of a character, a TAB
17994 for instance. This is easier than to set up the iterator
17995 exactly, and it's not a frequent case, so the additional
17996 effort wouldn't really pay off. */
17997 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17998 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17999 && last_unchanged_at_beg_row > w->current_matrix->rows)
18000 --last_unchanged_at_beg_row;
18001
18002 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18003 GIVE_UP (17);
18004
18005 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18006 GIVE_UP (18);
18007 start_pos = it.current.pos;
18008
18009 /* Start displaying new lines in the desired matrix at the same
18010 vpos we would use in the current matrix, i.e. below
18011 last_unchanged_at_beg_row. */
18012 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18013 current_matrix);
18014 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18015 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18016
18017 eassert (it.hpos == 0 && it.current_x == 0);
18018 }
18019 else
18020 {
18021 /* There are no reusable lines at the start of the window.
18022 Start displaying in the first text line. */
18023 start_display (&it, w, start);
18024 it.vpos = it.first_vpos;
18025 start_pos = it.current.pos;
18026 }
18027
18028 /* Find the first row that is not affected by changes at the end of
18029 the buffer. Value will be null if there is no unchanged row, in
18030 which case we must redisplay to the end of the window. delta
18031 will be set to the value by which buffer positions beginning with
18032 first_unchanged_at_end_row have to be adjusted due to text
18033 changes. */
18034 first_unchanged_at_end_row
18035 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18036 IF_DEBUG (debug_delta = delta);
18037 IF_DEBUG (debug_delta_bytes = delta_bytes);
18038
18039 /* Set stop_pos to the buffer position up to which we will have to
18040 display new lines. If first_unchanged_at_end_row != NULL, this
18041 is the buffer position of the start of the line displayed in that
18042 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18043 that we don't stop at a buffer position. */
18044 stop_pos = 0;
18045 if (first_unchanged_at_end_row)
18046 {
18047 eassert (last_unchanged_at_beg_row == NULL
18048 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18049
18050 /* If this is a continuation line, move forward to the next one
18051 that isn't. Changes in lines above affect this line.
18052 Caution: this may move first_unchanged_at_end_row to a row
18053 not displaying text. */
18054 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18055 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18056 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18057 < it.last_visible_y))
18058 ++first_unchanged_at_end_row;
18059
18060 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18061 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18062 >= it.last_visible_y))
18063 first_unchanged_at_end_row = NULL;
18064 else
18065 {
18066 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18067 + delta);
18068 first_unchanged_at_end_vpos
18069 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18070 eassert (stop_pos >= Z - END_UNCHANGED);
18071 }
18072 }
18073 else if (last_unchanged_at_beg_row == NULL)
18074 GIVE_UP (19);
18075
18076
18077 #ifdef GLYPH_DEBUG
18078
18079 /* Either there is no unchanged row at the end, or the one we have
18080 now displays text. This is a necessary condition for the window
18081 end pos calculation at the end of this function. */
18082 eassert (first_unchanged_at_end_row == NULL
18083 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18084
18085 debug_last_unchanged_at_beg_vpos
18086 = (last_unchanged_at_beg_row
18087 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18088 : -1);
18089 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18090
18091 #endif /* GLYPH_DEBUG */
18092
18093
18094 /* Display new lines. Set last_text_row to the last new line
18095 displayed which has text on it, i.e. might end up as being the
18096 line where the window_end_vpos is. */
18097 w->cursor.vpos = -1;
18098 last_text_row = NULL;
18099 overlay_arrow_seen = false;
18100 if (it.current_y < it.last_visible_y
18101 && !f->fonts_changed
18102 && (first_unchanged_at_end_row == NULL
18103 || IT_CHARPOS (it) < stop_pos))
18104 it.glyph_row->reversed_p = false;
18105 while (it.current_y < it.last_visible_y
18106 && !f->fonts_changed
18107 && (first_unchanged_at_end_row == NULL
18108 || IT_CHARPOS (it) < stop_pos))
18109 {
18110 if (display_line (&it))
18111 last_text_row = it.glyph_row - 1;
18112 }
18113
18114 if (f->fonts_changed)
18115 return -1;
18116
18117 /* The redisplay iterations in display_line above could have
18118 triggered font-lock, which could have done something that
18119 invalidates IT->w window's end-point information, on which we
18120 rely below. E.g., one package, which will remain unnamed, used
18121 to install a font-lock-fontify-region-function that called
18122 bury-buffer, whose side effect is to switch the buffer displayed
18123 by IT->w, and that predictably resets IT->w's window_end_valid
18124 flag, which we already tested at the entry to this function.
18125 Amply punish such packages/modes by giving up on this
18126 optimization in those cases. */
18127 if (!w->window_end_valid)
18128 {
18129 clear_glyph_matrix (w->desired_matrix);
18130 return -1;
18131 }
18132
18133 /* Compute differences in buffer positions, y-positions etc. for
18134 lines reused at the bottom of the window. Compute what we can
18135 scroll. */
18136 if (first_unchanged_at_end_row
18137 /* No lines reused because we displayed everything up to the
18138 bottom of the window. */
18139 && it.current_y < it.last_visible_y)
18140 {
18141 dvpos = (it.vpos
18142 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18143 current_matrix));
18144 dy = it.current_y - first_unchanged_at_end_row->y;
18145 run.current_y = first_unchanged_at_end_row->y;
18146 run.desired_y = run.current_y + dy;
18147 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18148 }
18149 else
18150 {
18151 delta = delta_bytes = dvpos = dy
18152 = run.current_y = run.desired_y = run.height = 0;
18153 first_unchanged_at_end_row = NULL;
18154 }
18155 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18156
18157
18158 /* Find the cursor if not already found. We have to decide whether
18159 PT will appear on this window (it sometimes doesn't, but this is
18160 not a very frequent case.) This decision has to be made before
18161 the current matrix is altered. A value of cursor.vpos < 0 means
18162 that PT is either in one of the lines beginning at
18163 first_unchanged_at_end_row or below the window. Don't care for
18164 lines that might be displayed later at the window end; as
18165 mentioned, this is not a frequent case. */
18166 if (w->cursor.vpos < 0)
18167 {
18168 /* Cursor in unchanged rows at the top? */
18169 if (PT < CHARPOS (start_pos)
18170 && last_unchanged_at_beg_row)
18171 {
18172 row = row_containing_pos (w, PT,
18173 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18174 last_unchanged_at_beg_row + 1, 0);
18175 if (row)
18176 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18177 }
18178
18179 /* Start from first_unchanged_at_end_row looking for PT. */
18180 else if (first_unchanged_at_end_row)
18181 {
18182 row = row_containing_pos (w, PT - delta,
18183 first_unchanged_at_end_row, NULL, 0);
18184 if (row)
18185 set_cursor_from_row (w, row, w->current_matrix, delta,
18186 delta_bytes, dy, dvpos);
18187 }
18188
18189 /* Give up if cursor was not found. */
18190 if (w->cursor.vpos < 0)
18191 {
18192 clear_glyph_matrix (w->desired_matrix);
18193 return -1;
18194 }
18195 }
18196
18197 /* Don't let the cursor end in the scroll margins. */
18198 {
18199 int this_scroll_margin, cursor_height;
18200 int frame_line_height = default_line_pixel_height (w);
18201 int window_total_lines
18202 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18203
18204 this_scroll_margin =
18205 max (0, min (scroll_margin, window_total_lines / 4));
18206 this_scroll_margin *= frame_line_height;
18207 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18208
18209 if ((w->cursor.y < this_scroll_margin
18210 && CHARPOS (start) > BEGV)
18211 /* Old redisplay didn't take scroll margin into account at the bottom,
18212 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18213 || (w->cursor.y + (make_cursor_line_fully_visible_p
18214 ? cursor_height + this_scroll_margin
18215 : 1)) > it.last_visible_y)
18216 {
18217 w->cursor.vpos = -1;
18218 clear_glyph_matrix (w->desired_matrix);
18219 return -1;
18220 }
18221 }
18222
18223 /* Scroll the display. Do it before changing the current matrix so
18224 that xterm.c doesn't get confused about where the cursor glyph is
18225 found. */
18226 if (dy && run.height)
18227 {
18228 update_begin (f);
18229
18230 if (FRAME_WINDOW_P (f))
18231 {
18232 FRAME_RIF (f)->update_window_begin_hook (w);
18233 FRAME_RIF (f)->clear_window_mouse_face (w);
18234 FRAME_RIF (f)->scroll_run_hook (w, &run);
18235 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18236 }
18237 else
18238 {
18239 /* Terminal frame. In this case, dvpos gives the number of
18240 lines to scroll by; dvpos < 0 means scroll up. */
18241 int from_vpos
18242 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18243 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18244 int end = (WINDOW_TOP_EDGE_LINE (w)
18245 + WINDOW_WANTS_HEADER_LINE_P (w)
18246 + window_internal_height (w));
18247
18248 #if defined (HAVE_GPM) || defined (MSDOS)
18249 x_clear_window_mouse_face (w);
18250 #endif
18251 /* Perform the operation on the screen. */
18252 if (dvpos > 0)
18253 {
18254 /* Scroll last_unchanged_at_beg_row to the end of the
18255 window down dvpos lines. */
18256 set_terminal_window (f, end);
18257
18258 /* On dumb terminals delete dvpos lines at the end
18259 before inserting dvpos empty lines. */
18260 if (!FRAME_SCROLL_REGION_OK (f))
18261 ins_del_lines (f, end - dvpos, -dvpos);
18262
18263 /* Insert dvpos empty lines in front of
18264 last_unchanged_at_beg_row. */
18265 ins_del_lines (f, from, dvpos);
18266 }
18267 else if (dvpos < 0)
18268 {
18269 /* Scroll up last_unchanged_at_beg_vpos to the end of
18270 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18271 set_terminal_window (f, end);
18272
18273 /* Delete dvpos lines in front of
18274 last_unchanged_at_beg_vpos. ins_del_lines will set
18275 the cursor to the given vpos and emit |dvpos| delete
18276 line sequences. */
18277 ins_del_lines (f, from + dvpos, dvpos);
18278
18279 /* On a dumb terminal insert dvpos empty lines at the
18280 end. */
18281 if (!FRAME_SCROLL_REGION_OK (f))
18282 ins_del_lines (f, end + dvpos, -dvpos);
18283 }
18284
18285 set_terminal_window (f, 0);
18286 }
18287
18288 update_end (f);
18289 }
18290
18291 /* Shift reused rows of the current matrix to the right position.
18292 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18293 text. */
18294 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18295 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18296 if (dvpos < 0)
18297 {
18298 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18299 bottom_vpos, dvpos);
18300 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18301 bottom_vpos);
18302 }
18303 else if (dvpos > 0)
18304 {
18305 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18306 bottom_vpos, dvpos);
18307 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18308 first_unchanged_at_end_vpos + dvpos);
18309 }
18310
18311 /* For frame-based redisplay, make sure that current frame and window
18312 matrix are in sync with respect to glyph memory. */
18313 if (!FRAME_WINDOW_P (f))
18314 sync_frame_with_window_matrix_rows (w);
18315
18316 /* Adjust buffer positions in reused rows. */
18317 if (delta || delta_bytes)
18318 increment_matrix_positions (current_matrix,
18319 first_unchanged_at_end_vpos + dvpos,
18320 bottom_vpos, delta, delta_bytes);
18321
18322 /* Adjust Y positions. */
18323 if (dy)
18324 shift_glyph_matrix (w, current_matrix,
18325 first_unchanged_at_end_vpos + dvpos,
18326 bottom_vpos, dy);
18327
18328 if (first_unchanged_at_end_row)
18329 {
18330 first_unchanged_at_end_row += dvpos;
18331 if (first_unchanged_at_end_row->y >= it.last_visible_y
18332 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18333 first_unchanged_at_end_row = NULL;
18334 }
18335
18336 /* If scrolling up, there may be some lines to display at the end of
18337 the window. */
18338 last_text_row_at_end = NULL;
18339 if (dy < 0)
18340 {
18341 /* Scrolling up can leave for example a partially visible line
18342 at the end of the window to be redisplayed. */
18343 /* Set last_row to the glyph row in the current matrix where the
18344 window end line is found. It has been moved up or down in
18345 the matrix by dvpos. */
18346 int last_vpos = w->window_end_vpos + dvpos;
18347 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18348
18349 /* If last_row is the window end line, it should display text. */
18350 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18351
18352 /* If window end line was partially visible before, begin
18353 displaying at that line. Otherwise begin displaying with the
18354 line following it. */
18355 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18356 {
18357 init_to_row_start (&it, w, last_row);
18358 it.vpos = last_vpos;
18359 it.current_y = last_row->y;
18360 }
18361 else
18362 {
18363 init_to_row_end (&it, w, last_row);
18364 it.vpos = 1 + last_vpos;
18365 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18366 ++last_row;
18367 }
18368
18369 /* We may start in a continuation line. If so, we have to
18370 get the right continuation_lines_width and current_x. */
18371 it.continuation_lines_width = last_row->continuation_lines_width;
18372 it.hpos = it.current_x = 0;
18373
18374 /* Display the rest of the lines at the window end. */
18375 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18376 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18377 {
18378 /* Is it always sure that the display agrees with lines in
18379 the current matrix? I don't think so, so we mark rows
18380 displayed invalid in the current matrix by setting their
18381 enabled_p flag to false. */
18382 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18383 if (display_line (&it))
18384 last_text_row_at_end = it.glyph_row - 1;
18385 }
18386 }
18387
18388 /* Update window_end_pos and window_end_vpos. */
18389 if (first_unchanged_at_end_row && !last_text_row_at_end)
18390 {
18391 /* Window end line if one of the preserved rows from the current
18392 matrix. Set row to the last row displaying text in current
18393 matrix starting at first_unchanged_at_end_row, after
18394 scrolling. */
18395 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18396 row = find_last_row_displaying_text (w->current_matrix, &it,
18397 first_unchanged_at_end_row);
18398 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18399 adjust_window_ends (w, row, true);
18400 eassert (w->window_end_bytepos >= 0);
18401 IF_DEBUG (debug_method_add (w, "A"));
18402 }
18403 else if (last_text_row_at_end)
18404 {
18405 adjust_window_ends (w, last_text_row_at_end, false);
18406 eassert (w->window_end_bytepos >= 0);
18407 IF_DEBUG (debug_method_add (w, "B"));
18408 }
18409 else if (last_text_row)
18410 {
18411 /* We have displayed either to the end of the window or at the
18412 end of the window, i.e. the last row with text is to be found
18413 in the desired matrix. */
18414 adjust_window_ends (w, last_text_row, false);
18415 eassert (w->window_end_bytepos >= 0);
18416 }
18417 else if (first_unchanged_at_end_row == NULL
18418 && last_text_row == NULL
18419 && last_text_row_at_end == NULL)
18420 {
18421 /* Displayed to end of window, but no line containing text was
18422 displayed. Lines were deleted at the end of the window. */
18423 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18424 int vpos = w->window_end_vpos;
18425 struct glyph_row *current_row = current_matrix->rows + vpos;
18426 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18427
18428 for (row = NULL;
18429 row == NULL && vpos >= first_vpos;
18430 --vpos, --current_row, --desired_row)
18431 {
18432 if (desired_row->enabled_p)
18433 {
18434 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18435 row = desired_row;
18436 }
18437 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18438 row = current_row;
18439 }
18440
18441 eassert (row != NULL);
18442 w->window_end_vpos = vpos + 1;
18443 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18444 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18445 eassert (w->window_end_bytepos >= 0);
18446 IF_DEBUG (debug_method_add (w, "C"));
18447 }
18448 else
18449 emacs_abort ();
18450
18451 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18452 debug_end_vpos = w->window_end_vpos));
18453
18454 /* Record that display has not been completed. */
18455 w->window_end_valid = false;
18456 w->desired_matrix->no_scrolling_p = true;
18457 return 3;
18458
18459 #undef GIVE_UP
18460 }
18461
18462
18463 \f
18464 /***********************************************************************
18465 More debugging support
18466 ***********************************************************************/
18467
18468 #ifdef GLYPH_DEBUG
18469
18470 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18471 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18472 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18473
18474
18475 /* Dump the contents of glyph matrix MATRIX on stderr.
18476
18477 GLYPHS 0 means don't show glyph contents.
18478 GLYPHS 1 means show glyphs in short form
18479 GLYPHS > 1 means show glyphs in long form. */
18480
18481 void
18482 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18483 {
18484 int i;
18485 for (i = 0; i < matrix->nrows; ++i)
18486 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18487 }
18488
18489
18490 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18491 the glyph row and area where the glyph comes from. */
18492
18493 void
18494 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18495 {
18496 if (glyph->type == CHAR_GLYPH
18497 || glyph->type == GLYPHLESS_GLYPH)
18498 {
18499 fprintf (stderr,
18500 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18501 glyph - row->glyphs[TEXT_AREA],
18502 (glyph->type == CHAR_GLYPH
18503 ? 'C'
18504 : 'G'),
18505 glyph->charpos,
18506 (BUFFERP (glyph->object)
18507 ? 'B'
18508 : (STRINGP (glyph->object)
18509 ? 'S'
18510 : (NILP (glyph->object)
18511 ? '0'
18512 : '-'))),
18513 glyph->pixel_width,
18514 glyph->u.ch,
18515 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18516 ? glyph->u.ch
18517 : '.'),
18518 glyph->face_id,
18519 glyph->left_box_line_p,
18520 glyph->right_box_line_p);
18521 }
18522 else if (glyph->type == STRETCH_GLYPH)
18523 {
18524 fprintf (stderr,
18525 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18526 glyph - row->glyphs[TEXT_AREA],
18527 'S',
18528 glyph->charpos,
18529 (BUFFERP (glyph->object)
18530 ? 'B'
18531 : (STRINGP (glyph->object)
18532 ? 'S'
18533 : (NILP (glyph->object)
18534 ? '0'
18535 : '-'))),
18536 glyph->pixel_width,
18537 0,
18538 ' ',
18539 glyph->face_id,
18540 glyph->left_box_line_p,
18541 glyph->right_box_line_p);
18542 }
18543 else if (glyph->type == IMAGE_GLYPH)
18544 {
18545 fprintf (stderr,
18546 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18547 glyph - row->glyphs[TEXT_AREA],
18548 'I',
18549 glyph->charpos,
18550 (BUFFERP (glyph->object)
18551 ? 'B'
18552 : (STRINGP (glyph->object)
18553 ? 'S'
18554 : (NILP (glyph->object)
18555 ? '0'
18556 : '-'))),
18557 glyph->pixel_width,
18558 glyph->u.img_id,
18559 '.',
18560 glyph->face_id,
18561 glyph->left_box_line_p,
18562 glyph->right_box_line_p);
18563 }
18564 else if (glyph->type == COMPOSITE_GLYPH)
18565 {
18566 fprintf (stderr,
18567 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18568 glyph - row->glyphs[TEXT_AREA],
18569 '+',
18570 glyph->charpos,
18571 (BUFFERP (glyph->object)
18572 ? 'B'
18573 : (STRINGP (glyph->object)
18574 ? 'S'
18575 : (NILP (glyph->object)
18576 ? '0'
18577 : '-'))),
18578 glyph->pixel_width,
18579 glyph->u.cmp.id);
18580 if (glyph->u.cmp.automatic)
18581 fprintf (stderr,
18582 "[%d-%d]",
18583 glyph->slice.cmp.from, glyph->slice.cmp.to);
18584 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18585 glyph->face_id,
18586 glyph->left_box_line_p,
18587 glyph->right_box_line_p);
18588 }
18589 }
18590
18591
18592 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18593 GLYPHS 0 means don't show glyph contents.
18594 GLYPHS 1 means show glyphs in short form
18595 GLYPHS > 1 means show glyphs in long form. */
18596
18597 void
18598 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18599 {
18600 if (glyphs != 1)
18601 {
18602 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18603 fprintf (stderr, "==============================================================================\n");
18604
18605 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18606 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18607 vpos,
18608 MATRIX_ROW_START_CHARPOS (row),
18609 MATRIX_ROW_END_CHARPOS (row),
18610 row->used[TEXT_AREA],
18611 row->contains_overlapping_glyphs_p,
18612 row->enabled_p,
18613 row->truncated_on_left_p,
18614 row->truncated_on_right_p,
18615 row->continued_p,
18616 MATRIX_ROW_CONTINUATION_LINE_P (row),
18617 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18618 row->ends_at_zv_p,
18619 row->fill_line_p,
18620 row->ends_in_middle_of_char_p,
18621 row->starts_in_middle_of_char_p,
18622 row->mouse_face_p,
18623 row->x,
18624 row->y,
18625 row->pixel_width,
18626 row->height,
18627 row->visible_height,
18628 row->ascent,
18629 row->phys_ascent);
18630 /* The next 3 lines should align to "Start" in the header. */
18631 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18632 row->end.overlay_string_index,
18633 row->continuation_lines_width);
18634 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18635 CHARPOS (row->start.string_pos),
18636 CHARPOS (row->end.string_pos));
18637 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18638 row->end.dpvec_index);
18639 }
18640
18641 if (glyphs > 1)
18642 {
18643 int area;
18644
18645 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18646 {
18647 struct glyph *glyph = row->glyphs[area];
18648 struct glyph *glyph_end = glyph + row->used[area];
18649
18650 /* Glyph for a line end in text. */
18651 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18652 ++glyph_end;
18653
18654 if (glyph < glyph_end)
18655 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18656
18657 for (; glyph < glyph_end; ++glyph)
18658 dump_glyph (row, glyph, area);
18659 }
18660 }
18661 else if (glyphs == 1)
18662 {
18663 int area;
18664 char s[SHRT_MAX + 4];
18665
18666 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18667 {
18668 int i;
18669
18670 for (i = 0; i < row->used[area]; ++i)
18671 {
18672 struct glyph *glyph = row->glyphs[area] + i;
18673 if (i == row->used[area] - 1
18674 && area == TEXT_AREA
18675 && NILP (glyph->object)
18676 && glyph->type == CHAR_GLYPH
18677 && glyph->u.ch == ' ')
18678 {
18679 strcpy (&s[i], "[\\n]");
18680 i += 4;
18681 }
18682 else if (glyph->type == CHAR_GLYPH
18683 && glyph->u.ch < 0x80
18684 && glyph->u.ch >= ' ')
18685 s[i] = glyph->u.ch;
18686 else
18687 s[i] = '.';
18688 }
18689
18690 s[i] = '\0';
18691 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18692 }
18693 }
18694 }
18695
18696
18697 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18698 Sdump_glyph_matrix, 0, 1, "p",
18699 doc: /* Dump the current matrix of the selected window to stderr.
18700 Shows contents of glyph row structures. With non-nil
18701 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18702 glyphs in short form, otherwise show glyphs in long form.
18703
18704 Interactively, no argument means show glyphs in short form;
18705 with numeric argument, its value is passed as the GLYPHS flag. */)
18706 (Lisp_Object glyphs)
18707 {
18708 struct window *w = XWINDOW (selected_window);
18709 struct buffer *buffer = XBUFFER (w->contents);
18710
18711 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18712 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18713 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18714 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18715 fprintf (stderr, "=============================================\n");
18716 dump_glyph_matrix (w->current_matrix,
18717 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18718 return Qnil;
18719 }
18720
18721
18722 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18723 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18724 Only text-mode frames have frame glyph matrices. */)
18725 (void)
18726 {
18727 struct frame *f = XFRAME (selected_frame);
18728
18729 if (f->current_matrix)
18730 dump_glyph_matrix (f->current_matrix, 1);
18731 else
18732 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18733 return Qnil;
18734 }
18735
18736
18737 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18738 doc: /* Dump glyph row ROW to stderr.
18739 GLYPH 0 means don't dump glyphs.
18740 GLYPH 1 means dump glyphs in short form.
18741 GLYPH > 1 or omitted means dump glyphs in long form. */)
18742 (Lisp_Object row, Lisp_Object glyphs)
18743 {
18744 struct glyph_matrix *matrix;
18745 EMACS_INT vpos;
18746
18747 CHECK_NUMBER (row);
18748 matrix = XWINDOW (selected_window)->current_matrix;
18749 vpos = XINT (row);
18750 if (vpos >= 0 && vpos < matrix->nrows)
18751 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18752 vpos,
18753 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18754 return Qnil;
18755 }
18756
18757
18758 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18759 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18760 GLYPH 0 means don't dump glyphs.
18761 GLYPH 1 means dump glyphs in short form.
18762 GLYPH > 1 or omitted means dump glyphs in long form.
18763
18764 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18765 do nothing. */)
18766 (Lisp_Object row, Lisp_Object glyphs)
18767 {
18768 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18769 struct frame *sf = SELECTED_FRAME ();
18770 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18771 EMACS_INT vpos;
18772
18773 CHECK_NUMBER (row);
18774 vpos = XINT (row);
18775 if (vpos >= 0 && vpos < m->nrows)
18776 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18777 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18778 #endif
18779 return Qnil;
18780 }
18781
18782
18783 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18784 doc: /* Toggle tracing of redisplay.
18785 With ARG, turn tracing on if and only if ARG is positive. */)
18786 (Lisp_Object arg)
18787 {
18788 if (NILP (arg))
18789 trace_redisplay_p = !trace_redisplay_p;
18790 else
18791 {
18792 arg = Fprefix_numeric_value (arg);
18793 trace_redisplay_p = XINT (arg) > 0;
18794 }
18795
18796 return Qnil;
18797 }
18798
18799
18800 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18801 doc: /* Like `format', but print result to stderr.
18802 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18803 (ptrdiff_t nargs, Lisp_Object *args)
18804 {
18805 Lisp_Object s = Fformat (nargs, args);
18806 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18807 return Qnil;
18808 }
18809
18810 #endif /* GLYPH_DEBUG */
18811
18812
18813 \f
18814 /***********************************************************************
18815 Building Desired Matrix Rows
18816 ***********************************************************************/
18817
18818 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18819 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18820
18821 static struct glyph_row *
18822 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18823 {
18824 struct frame *f = XFRAME (WINDOW_FRAME (w));
18825 struct buffer *buffer = XBUFFER (w->contents);
18826 struct buffer *old = current_buffer;
18827 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18828 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18829 const unsigned char *arrow_end = arrow_string + arrow_len;
18830 const unsigned char *p;
18831 struct it it;
18832 bool multibyte_p;
18833 int n_glyphs_before;
18834
18835 set_buffer_temp (buffer);
18836 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18837 scratch_glyph_row.reversed_p = false;
18838 it.glyph_row->used[TEXT_AREA] = 0;
18839 SET_TEXT_POS (it.position, 0, 0);
18840
18841 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18842 p = arrow_string;
18843 while (p < arrow_end)
18844 {
18845 Lisp_Object face, ilisp;
18846
18847 /* Get the next character. */
18848 if (multibyte_p)
18849 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18850 else
18851 {
18852 it.c = it.char_to_display = *p, it.len = 1;
18853 if (! ASCII_CHAR_P (it.c))
18854 it.char_to_display = BYTE8_TO_CHAR (it.c);
18855 }
18856 p += it.len;
18857
18858 /* Get its face. */
18859 ilisp = make_number (p - arrow_string);
18860 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18861 it.face_id = compute_char_face (f, it.char_to_display, face);
18862
18863 /* Compute its width, get its glyphs. */
18864 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18865 SET_TEXT_POS (it.position, -1, -1);
18866 PRODUCE_GLYPHS (&it);
18867
18868 /* If this character doesn't fit any more in the line, we have
18869 to remove some glyphs. */
18870 if (it.current_x > it.last_visible_x)
18871 {
18872 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18873 break;
18874 }
18875 }
18876
18877 set_buffer_temp (old);
18878 return it.glyph_row;
18879 }
18880
18881
18882 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18883 glyphs to insert is determined by produce_special_glyphs. */
18884
18885 static void
18886 insert_left_trunc_glyphs (struct it *it)
18887 {
18888 struct it truncate_it;
18889 struct glyph *from, *end, *to, *toend;
18890
18891 eassert (!FRAME_WINDOW_P (it->f)
18892 || (!it->glyph_row->reversed_p
18893 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18894 || (it->glyph_row->reversed_p
18895 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18896
18897 /* Get the truncation glyphs. */
18898 truncate_it = *it;
18899 truncate_it.current_x = 0;
18900 truncate_it.face_id = DEFAULT_FACE_ID;
18901 truncate_it.glyph_row = &scratch_glyph_row;
18902 truncate_it.area = TEXT_AREA;
18903 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18904 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18905 truncate_it.object = Qnil;
18906 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18907
18908 /* Overwrite glyphs from IT with truncation glyphs. */
18909 if (!it->glyph_row->reversed_p)
18910 {
18911 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18912
18913 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18914 end = from + tused;
18915 to = it->glyph_row->glyphs[TEXT_AREA];
18916 toend = to + it->glyph_row->used[TEXT_AREA];
18917 if (FRAME_WINDOW_P (it->f))
18918 {
18919 /* On GUI frames, when variable-size fonts are displayed,
18920 the truncation glyphs may need more pixels than the row's
18921 glyphs they overwrite. We overwrite more glyphs to free
18922 enough screen real estate, and enlarge the stretch glyph
18923 on the right (see display_line), if there is one, to
18924 preserve the screen position of the truncation glyphs on
18925 the right. */
18926 int w = 0;
18927 struct glyph *g = to;
18928 short used;
18929
18930 /* The first glyph could be partially visible, in which case
18931 it->glyph_row->x will be negative. But we want the left
18932 truncation glyphs to be aligned at the left margin of the
18933 window, so we override the x coordinate at which the row
18934 will begin. */
18935 it->glyph_row->x = 0;
18936 while (g < toend && w < it->truncation_pixel_width)
18937 {
18938 w += g->pixel_width;
18939 ++g;
18940 }
18941 if (g - to - tused > 0)
18942 {
18943 memmove (to + tused, g, (toend - g) * sizeof(*g));
18944 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18945 }
18946 used = it->glyph_row->used[TEXT_AREA];
18947 if (it->glyph_row->truncated_on_right_p
18948 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18949 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18950 == STRETCH_GLYPH)
18951 {
18952 int extra = w - it->truncation_pixel_width;
18953
18954 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18955 }
18956 }
18957
18958 while (from < end)
18959 *to++ = *from++;
18960
18961 /* There may be padding glyphs left over. Overwrite them too. */
18962 if (!FRAME_WINDOW_P (it->f))
18963 {
18964 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18965 {
18966 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18967 while (from < end)
18968 *to++ = *from++;
18969 }
18970 }
18971
18972 if (to > toend)
18973 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18974 }
18975 else
18976 {
18977 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18978
18979 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18980 that back to front. */
18981 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18982 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18983 toend = it->glyph_row->glyphs[TEXT_AREA];
18984 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18985 if (FRAME_WINDOW_P (it->f))
18986 {
18987 int w = 0;
18988 struct glyph *g = to;
18989
18990 while (g >= toend && w < it->truncation_pixel_width)
18991 {
18992 w += g->pixel_width;
18993 --g;
18994 }
18995 if (to - g - tused > 0)
18996 to = g + tused;
18997 if (it->glyph_row->truncated_on_right_p
18998 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18999 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19000 {
19001 int extra = w - it->truncation_pixel_width;
19002
19003 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19004 }
19005 }
19006
19007 while (from >= end && to >= toend)
19008 *to-- = *from--;
19009 if (!FRAME_WINDOW_P (it->f))
19010 {
19011 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19012 {
19013 from =
19014 truncate_it.glyph_row->glyphs[TEXT_AREA]
19015 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19016 while (from >= end && to >= toend)
19017 *to-- = *from--;
19018 }
19019 }
19020 if (from >= end)
19021 {
19022 /* Need to free some room before prepending additional
19023 glyphs. */
19024 int move_by = from - end + 1;
19025 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19026 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19027
19028 for ( ; g >= g0; g--)
19029 g[move_by] = *g;
19030 while (from >= end)
19031 *to-- = *from--;
19032 it->glyph_row->used[TEXT_AREA] += move_by;
19033 }
19034 }
19035 }
19036
19037 /* Compute the hash code for ROW. */
19038 unsigned
19039 row_hash (struct glyph_row *row)
19040 {
19041 int area, k;
19042 unsigned hashval = 0;
19043
19044 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19045 for (k = 0; k < row->used[area]; ++k)
19046 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19047 + row->glyphs[area][k].u.val
19048 + row->glyphs[area][k].face_id
19049 + row->glyphs[area][k].padding_p
19050 + (row->glyphs[area][k].type << 2));
19051
19052 return hashval;
19053 }
19054
19055 /* Compute the pixel height and width of IT->glyph_row.
19056
19057 Most of the time, ascent and height of a display line will be equal
19058 to the max_ascent and max_height values of the display iterator
19059 structure. This is not the case if
19060
19061 1. We hit ZV without displaying anything. In this case, max_ascent
19062 and max_height will be zero.
19063
19064 2. We have some glyphs that don't contribute to the line height.
19065 (The glyph row flag contributes_to_line_height_p is for future
19066 pixmap extensions).
19067
19068 The first case is easily covered by using default values because in
19069 these cases, the line height does not really matter, except that it
19070 must not be zero. */
19071
19072 static void
19073 compute_line_metrics (struct it *it)
19074 {
19075 struct glyph_row *row = it->glyph_row;
19076
19077 if (FRAME_WINDOW_P (it->f))
19078 {
19079 int i, min_y, max_y;
19080
19081 /* The line may consist of one space only, that was added to
19082 place the cursor on it. If so, the row's height hasn't been
19083 computed yet. */
19084 if (row->height == 0)
19085 {
19086 if (it->max_ascent + it->max_descent == 0)
19087 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19088 row->ascent = it->max_ascent;
19089 row->height = it->max_ascent + it->max_descent;
19090 row->phys_ascent = it->max_phys_ascent;
19091 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19092 row->extra_line_spacing = it->max_extra_line_spacing;
19093 }
19094
19095 /* Compute the width of this line. */
19096 row->pixel_width = row->x;
19097 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19098 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19099
19100 eassert (row->pixel_width >= 0);
19101 eassert (row->ascent >= 0 && row->height > 0);
19102
19103 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19104 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19105
19106 /* If first line's physical ascent is larger than its logical
19107 ascent, use the physical ascent, and make the row taller.
19108 This makes accented characters fully visible. */
19109 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19110 && row->phys_ascent > row->ascent)
19111 {
19112 row->height += row->phys_ascent - row->ascent;
19113 row->ascent = row->phys_ascent;
19114 }
19115
19116 /* Compute how much of the line is visible. */
19117 row->visible_height = row->height;
19118
19119 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19120 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19121
19122 if (row->y < min_y)
19123 row->visible_height -= min_y - row->y;
19124 if (row->y + row->height > max_y)
19125 row->visible_height -= row->y + row->height - max_y;
19126 }
19127 else
19128 {
19129 row->pixel_width = row->used[TEXT_AREA];
19130 if (row->continued_p)
19131 row->pixel_width -= it->continuation_pixel_width;
19132 else if (row->truncated_on_right_p)
19133 row->pixel_width -= it->truncation_pixel_width;
19134 row->ascent = row->phys_ascent = 0;
19135 row->height = row->phys_height = row->visible_height = 1;
19136 row->extra_line_spacing = 0;
19137 }
19138
19139 /* Compute a hash code for this row. */
19140 row->hash = row_hash (row);
19141
19142 it->max_ascent = it->max_descent = 0;
19143 it->max_phys_ascent = it->max_phys_descent = 0;
19144 }
19145
19146
19147 /* Append one space to the glyph row of iterator IT if doing a
19148 window-based redisplay. The space has the same face as
19149 IT->face_id. Value is true if a space was added.
19150
19151 This function is called to make sure that there is always one glyph
19152 at the end of a glyph row that the cursor can be set on under
19153 window-systems. (If there weren't such a glyph we would not know
19154 how wide and tall a box cursor should be displayed).
19155
19156 At the same time this space let's a nicely handle clearing to the
19157 end of the line if the row ends in italic text. */
19158
19159 static bool
19160 append_space_for_newline (struct it *it, bool default_face_p)
19161 {
19162 if (FRAME_WINDOW_P (it->f))
19163 {
19164 int n = it->glyph_row->used[TEXT_AREA];
19165
19166 if (it->glyph_row->glyphs[TEXT_AREA] + n
19167 < it->glyph_row->glyphs[1 + TEXT_AREA])
19168 {
19169 /* Save some values that must not be changed.
19170 Must save IT->c and IT->len because otherwise
19171 ITERATOR_AT_END_P wouldn't work anymore after
19172 append_space_for_newline has been called. */
19173 enum display_element_type saved_what = it->what;
19174 int saved_c = it->c, saved_len = it->len;
19175 int saved_char_to_display = it->char_to_display;
19176 int saved_x = it->current_x;
19177 int saved_face_id = it->face_id;
19178 bool saved_box_end = it->end_of_box_run_p;
19179 struct text_pos saved_pos;
19180 Lisp_Object saved_object;
19181 struct face *face;
19182 struct glyph *g;
19183
19184 saved_object = it->object;
19185 saved_pos = it->position;
19186
19187 it->what = IT_CHARACTER;
19188 memset (&it->position, 0, sizeof it->position);
19189 it->object = Qnil;
19190 it->c = it->char_to_display = ' ';
19191 it->len = 1;
19192
19193 /* If the default face was remapped, be sure to use the
19194 remapped face for the appended newline. */
19195 if (default_face_p)
19196 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19197 else if (it->face_before_selective_p)
19198 it->face_id = it->saved_face_id;
19199 face = FACE_FROM_ID (it->f, it->face_id);
19200 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19201 /* In R2L rows, we will prepend a stretch glyph that will
19202 have the end_of_box_run_p flag set for it, so there's no
19203 need for the appended newline glyph to have that flag
19204 set. */
19205 if (it->glyph_row->reversed_p
19206 /* But if the appended newline glyph goes all the way to
19207 the end of the row, there will be no stretch glyph,
19208 so leave the box flag set. */
19209 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19210 it->end_of_box_run_p = false;
19211
19212 PRODUCE_GLYPHS (it);
19213
19214 /* Make sure this space glyph has the right ascent value, or
19215 else hollow cursor at end of line will look funny. */
19216 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19217 g->ascent = it->glyph_row->ascent;
19218
19219 it->override_ascent = -1;
19220 it->constrain_row_ascent_descent_p = false;
19221 it->current_x = saved_x;
19222 it->object = saved_object;
19223 it->position = saved_pos;
19224 it->what = saved_what;
19225 it->face_id = saved_face_id;
19226 it->len = saved_len;
19227 it->c = saved_c;
19228 it->char_to_display = saved_char_to_display;
19229 it->end_of_box_run_p = saved_box_end;
19230 return true;
19231 }
19232 }
19233
19234 return false;
19235 }
19236
19237
19238 /* Extend the face of the last glyph in the text area of IT->glyph_row
19239 to the end of the display line. Called from display_line. If the
19240 glyph row is empty, add a space glyph to it so that we know the
19241 face to draw. Set the glyph row flag fill_line_p. If the glyph
19242 row is R2L, prepend a stretch glyph to cover the empty space to the
19243 left of the leftmost glyph. */
19244
19245 static void
19246 extend_face_to_end_of_line (struct it *it)
19247 {
19248 struct face *face, *default_face;
19249 struct frame *f = it->f;
19250
19251 /* If line is already filled, do nothing. Non window-system frames
19252 get a grace of one more ``pixel'' because their characters are
19253 1-``pixel'' wide, so they hit the equality too early. This grace
19254 is needed only for R2L rows that are not continued, to produce
19255 one extra blank where we could display the cursor. */
19256 if ((it->current_x >= it->last_visible_x
19257 + (!FRAME_WINDOW_P (f)
19258 && it->glyph_row->reversed_p
19259 && !it->glyph_row->continued_p))
19260 /* If the window has display margins, we will need to extend
19261 their face even if the text area is filled. */
19262 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19263 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19264 return;
19265
19266 /* The default face, possibly remapped. */
19267 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19268
19269 /* Face extension extends the background and box of IT->face_id
19270 to the end of the line. If the background equals the background
19271 of the frame, we don't have to do anything. */
19272 if (it->face_before_selective_p)
19273 face = FACE_FROM_ID (f, it->saved_face_id);
19274 else
19275 face = FACE_FROM_ID (f, it->face_id);
19276
19277 if (FRAME_WINDOW_P (f)
19278 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19279 && face->box == FACE_NO_BOX
19280 && face->background == FRAME_BACKGROUND_PIXEL (f)
19281 #ifdef HAVE_WINDOW_SYSTEM
19282 && !face->stipple
19283 #endif
19284 && !it->glyph_row->reversed_p)
19285 return;
19286
19287 /* Set the glyph row flag indicating that the face of the last glyph
19288 in the text area has to be drawn to the end of the text area. */
19289 it->glyph_row->fill_line_p = true;
19290
19291 /* If current character of IT is not ASCII, make sure we have the
19292 ASCII face. This will be automatically undone the next time
19293 get_next_display_element returns a multibyte character. Note
19294 that the character will always be single byte in unibyte
19295 text. */
19296 if (!ASCII_CHAR_P (it->c))
19297 {
19298 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19299 }
19300
19301 if (FRAME_WINDOW_P (f))
19302 {
19303 /* If the row is empty, add a space with the current face of IT,
19304 so that we know which face to draw. */
19305 if (it->glyph_row->used[TEXT_AREA] == 0)
19306 {
19307 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19308 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19309 it->glyph_row->used[TEXT_AREA] = 1;
19310 }
19311 /* Mode line and the header line don't have margins, and
19312 likewise the frame's tool-bar window, if there is any. */
19313 if (!(it->glyph_row->mode_line_p
19314 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19315 || (WINDOWP (f->tool_bar_window)
19316 && it->w == XWINDOW (f->tool_bar_window))
19317 #endif
19318 ))
19319 {
19320 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19321 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19322 {
19323 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19324 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19325 default_face->id;
19326 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19327 }
19328 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19329 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19330 {
19331 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19332 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19333 default_face->id;
19334 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19335 }
19336 }
19337 #ifdef HAVE_WINDOW_SYSTEM
19338 if (it->glyph_row->reversed_p)
19339 {
19340 /* Prepend a stretch glyph to the row, such that the
19341 rightmost glyph will be drawn flushed all the way to the
19342 right margin of the window. The stretch glyph that will
19343 occupy the empty space, if any, to the left of the
19344 glyphs. */
19345 struct font *font = face->font ? face->font : FRAME_FONT (f);
19346 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19347 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19348 struct glyph *g;
19349 int row_width, stretch_ascent, stretch_width;
19350 struct text_pos saved_pos;
19351 int saved_face_id;
19352 bool saved_avoid_cursor, saved_box_start;
19353
19354 for (row_width = 0, g = row_start; g < row_end; g++)
19355 row_width += g->pixel_width;
19356
19357 /* FIXME: There are various minor display glitches in R2L
19358 rows when only one of the fringes is missing. The
19359 strange condition below produces the least bad effect. */
19360 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19361 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19362 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19363 stretch_width = window_box_width (it->w, TEXT_AREA);
19364 else
19365 stretch_width = it->last_visible_x - it->first_visible_x;
19366 stretch_width -= row_width;
19367
19368 if (stretch_width > 0)
19369 {
19370 stretch_ascent =
19371 (((it->ascent + it->descent)
19372 * FONT_BASE (font)) / FONT_HEIGHT (font));
19373 saved_pos = it->position;
19374 memset (&it->position, 0, sizeof it->position);
19375 saved_avoid_cursor = it->avoid_cursor_p;
19376 it->avoid_cursor_p = true;
19377 saved_face_id = it->face_id;
19378 saved_box_start = it->start_of_box_run_p;
19379 /* The last row's stretch glyph should get the default
19380 face, to avoid painting the rest of the window with
19381 the region face, if the region ends at ZV. */
19382 if (it->glyph_row->ends_at_zv_p)
19383 it->face_id = default_face->id;
19384 else
19385 it->face_id = face->id;
19386 it->start_of_box_run_p = false;
19387 append_stretch_glyph (it, Qnil, stretch_width,
19388 it->ascent + it->descent, stretch_ascent);
19389 it->position = saved_pos;
19390 it->avoid_cursor_p = saved_avoid_cursor;
19391 it->face_id = saved_face_id;
19392 it->start_of_box_run_p = saved_box_start;
19393 }
19394 /* If stretch_width comes out negative, it means that the
19395 last glyph is only partially visible. In R2L rows, we
19396 want the leftmost glyph to be partially visible, so we
19397 need to give the row the corresponding left offset. */
19398 if (stretch_width < 0)
19399 it->glyph_row->x = stretch_width;
19400 }
19401 #endif /* HAVE_WINDOW_SYSTEM */
19402 }
19403 else
19404 {
19405 /* Save some values that must not be changed. */
19406 int saved_x = it->current_x;
19407 struct text_pos saved_pos;
19408 Lisp_Object saved_object;
19409 enum display_element_type saved_what = it->what;
19410 int saved_face_id = it->face_id;
19411
19412 saved_object = it->object;
19413 saved_pos = it->position;
19414
19415 it->what = IT_CHARACTER;
19416 memset (&it->position, 0, sizeof it->position);
19417 it->object = Qnil;
19418 it->c = it->char_to_display = ' ';
19419 it->len = 1;
19420
19421 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19422 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19423 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19424 && !it->glyph_row->mode_line_p
19425 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19426 {
19427 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19428 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19429
19430 for (it->current_x = 0; g < e; g++)
19431 it->current_x += g->pixel_width;
19432
19433 it->area = LEFT_MARGIN_AREA;
19434 it->face_id = default_face->id;
19435 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19436 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19437 {
19438 PRODUCE_GLYPHS (it);
19439 /* term.c:produce_glyphs advances it->current_x only for
19440 TEXT_AREA. */
19441 it->current_x += it->pixel_width;
19442 }
19443
19444 it->current_x = saved_x;
19445 it->area = TEXT_AREA;
19446 }
19447
19448 /* The last row's blank glyphs should get the default face, to
19449 avoid painting the rest of the window with the region face,
19450 if the region ends at ZV. */
19451 if (it->glyph_row->ends_at_zv_p)
19452 it->face_id = default_face->id;
19453 else
19454 it->face_id = face->id;
19455 PRODUCE_GLYPHS (it);
19456
19457 while (it->current_x <= it->last_visible_x)
19458 PRODUCE_GLYPHS (it);
19459
19460 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19461 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19462 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19463 && !it->glyph_row->mode_line_p
19464 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19465 {
19466 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19467 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19468
19469 for ( ; g < e; g++)
19470 it->current_x += g->pixel_width;
19471
19472 it->area = RIGHT_MARGIN_AREA;
19473 it->face_id = default_face->id;
19474 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19475 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19476 {
19477 PRODUCE_GLYPHS (it);
19478 it->current_x += it->pixel_width;
19479 }
19480
19481 it->area = TEXT_AREA;
19482 }
19483
19484 /* Don't count these blanks really. It would let us insert a left
19485 truncation glyph below and make us set the cursor on them, maybe. */
19486 it->current_x = saved_x;
19487 it->object = saved_object;
19488 it->position = saved_pos;
19489 it->what = saved_what;
19490 it->face_id = saved_face_id;
19491 }
19492 }
19493
19494
19495 /* Value is true if text starting at CHARPOS in current_buffer is
19496 trailing whitespace. */
19497
19498 static bool
19499 trailing_whitespace_p (ptrdiff_t charpos)
19500 {
19501 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19502 int c = 0;
19503
19504 while (bytepos < ZV_BYTE
19505 && (c = FETCH_CHAR (bytepos),
19506 c == ' ' || c == '\t'))
19507 ++bytepos;
19508
19509 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19510 {
19511 if (bytepos != PT_BYTE)
19512 return true;
19513 }
19514 return false;
19515 }
19516
19517
19518 /* Highlight trailing whitespace, if any, in ROW. */
19519
19520 static void
19521 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19522 {
19523 int used = row->used[TEXT_AREA];
19524
19525 if (used)
19526 {
19527 struct glyph *start = row->glyphs[TEXT_AREA];
19528 struct glyph *glyph = start + used - 1;
19529
19530 if (row->reversed_p)
19531 {
19532 /* Right-to-left rows need to be processed in the opposite
19533 direction, so swap the edge pointers. */
19534 glyph = start;
19535 start = row->glyphs[TEXT_AREA] + used - 1;
19536 }
19537
19538 /* Skip over glyphs inserted to display the cursor at the
19539 end of a line, for extending the face of the last glyph
19540 to the end of the line on terminals, and for truncation
19541 and continuation glyphs. */
19542 if (!row->reversed_p)
19543 {
19544 while (glyph >= start
19545 && glyph->type == CHAR_GLYPH
19546 && NILP (glyph->object))
19547 --glyph;
19548 }
19549 else
19550 {
19551 while (glyph <= start
19552 && glyph->type == CHAR_GLYPH
19553 && NILP (glyph->object))
19554 ++glyph;
19555 }
19556
19557 /* If last glyph is a space or stretch, and it's trailing
19558 whitespace, set the face of all trailing whitespace glyphs in
19559 IT->glyph_row to `trailing-whitespace'. */
19560 if ((row->reversed_p ? glyph <= start : glyph >= start)
19561 && BUFFERP (glyph->object)
19562 && (glyph->type == STRETCH_GLYPH
19563 || (glyph->type == CHAR_GLYPH
19564 && glyph->u.ch == ' '))
19565 && trailing_whitespace_p (glyph->charpos))
19566 {
19567 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19568 if (face_id < 0)
19569 return;
19570
19571 if (!row->reversed_p)
19572 {
19573 while (glyph >= start
19574 && BUFFERP (glyph->object)
19575 && (glyph->type == STRETCH_GLYPH
19576 || (glyph->type == CHAR_GLYPH
19577 && glyph->u.ch == ' ')))
19578 (glyph--)->face_id = face_id;
19579 }
19580 else
19581 {
19582 while (glyph <= start
19583 && BUFFERP (glyph->object)
19584 && (glyph->type == STRETCH_GLYPH
19585 || (glyph->type == CHAR_GLYPH
19586 && glyph->u.ch == ' ')))
19587 (glyph++)->face_id = face_id;
19588 }
19589 }
19590 }
19591 }
19592
19593
19594 /* Value is true if glyph row ROW should be
19595 considered to hold the buffer position CHARPOS. */
19596
19597 static bool
19598 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19599 {
19600 bool result = true;
19601
19602 if (charpos == CHARPOS (row->end.pos)
19603 || charpos == MATRIX_ROW_END_CHARPOS (row))
19604 {
19605 /* Suppose the row ends on a string.
19606 Unless the row is continued, that means it ends on a newline
19607 in the string. If it's anything other than a display string
19608 (e.g., a before-string from an overlay), we don't want the
19609 cursor there. (This heuristic seems to give the optimal
19610 behavior for the various types of multi-line strings.)
19611 One exception: if the string has `cursor' property on one of
19612 its characters, we _do_ want the cursor there. */
19613 if (CHARPOS (row->end.string_pos) >= 0)
19614 {
19615 if (row->continued_p)
19616 result = true;
19617 else
19618 {
19619 /* Check for `display' property. */
19620 struct glyph *beg = row->glyphs[TEXT_AREA];
19621 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19622 struct glyph *glyph;
19623
19624 result = false;
19625 for (glyph = end; glyph >= beg; --glyph)
19626 if (STRINGP (glyph->object))
19627 {
19628 Lisp_Object prop
19629 = Fget_char_property (make_number (charpos),
19630 Qdisplay, Qnil);
19631 result =
19632 (!NILP (prop)
19633 && display_prop_string_p (prop, glyph->object));
19634 /* If there's a `cursor' property on one of the
19635 string's characters, this row is a cursor row,
19636 even though this is not a display string. */
19637 if (!result)
19638 {
19639 Lisp_Object s = glyph->object;
19640
19641 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19642 {
19643 ptrdiff_t gpos = glyph->charpos;
19644
19645 if (!NILP (Fget_char_property (make_number (gpos),
19646 Qcursor, s)))
19647 {
19648 result = true;
19649 break;
19650 }
19651 }
19652 }
19653 break;
19654 }
19655 }
19656 }
19657 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19658 {
19659 /* If the row ends in middle of a real character,
19660 and the line is continued, we want the cursor here.
19661 That's because CHARPOS (ROW->end.pos) would equal
19662 PT if PT is before the character. */
19663 if (!row->ends_in_ellipsis_p)
19664 result = row->continued_p;
19665 else
19666 /* If the row ends in an ellipsis, then
19667 CHARPOS (ROW->end.pos) will equal point after the
19668 invisible text. We want that position to be displayed
19669 after the ellipsis. */
19670 result = false;
19671 }
19672 /* If the row ends at ZV, display the cursor at the end of that
19673 row instead of at the start of the row below. */
19674 else
19675 result = row->ends_at_zv_p;
19676 }
19677
19678 return result;
19679 }
19680
19681 /* Value is true if glyph row ROW should be
19682 used to hold the cursor. */
19683
19684 static bool
19685 cursor_row_p (struct glyph_row *row)
19686 {
19687 return row_for_charpos_p (row, PT);
19688 }
19689
19690 \f
19691
19692 /* Push the property PROP so that it will be rendered at the current
19693 position in IT. Return true if PROP was successfully pushed, false
19694 otherwise. Called from handle_line_prefix to handle the
19695 `line-prefix' and `wrap-prefix' properties. */
19696
19697 static bool
19698 push_prefix_prop (struct it *it, Lisp_Object prop)
19699 {
19700 struct text_pos pos =
19701 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19702
19703 eassert (it->method == GET_FROM_BUFFER
19704 || it->method == GET_FROM_DISPLAY_VECTOR
19705 || it->method == GET_FROM_STRING);
19706
19707 /* We need to save the current buffer/string position, so it will be
19708 restored by pop_it, because iterate_out_of_display_property
19709 depends on that being set correctly, but some situations leave
19710 it->position not yet set when this function is called. */
19711 push_it (it, &pos);
19712
19713 if (STRINGP (prop))
19714 {
19715 if (SCHARS (prop) == 0)
19716 {
19717 pop_it (it);
19718 return false;
19719 }
19720
19721 it->string = prop;
19722 it->string_from_prefix_prop_p = true;
19723 it->multibyte_p = STRING_MULTIBYTE (it->string);
19724 it->current.overlay_string_index = -1;
19725 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19726 it->end_charpos = it->string_nchars = SCHARS (it->string);
19727 it->method = GET_FROM_STRING;
19728 it->stop_charpos = 0;
19729 it->prev_stop = 0;
19730 it->base_level_stop = 0;
19731
19732 /* Force paragraph direction to be that of the parent
19733 buffer/string. */
19734 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19735 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19736 else
19737 it->paragraph_embedding = L2R;
19738
19739 /* Set up the bidi iterator for this display string. */
19740 if (it->bidi_p)
19741 {
19742 it->bidi_it.string.lstring = it->string;
19743 it->bidi_it.string.s = NULL;
19744 it->bidi_it.string.schars = it->end_charpos;
19745 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19746 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19747 it->bidi_it.string.unibyte = !it->multibyte_p;
19748 it->bidi_it.w = it->w;
19749 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19750 }
19751 }
19752 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19753 {
19754 it->method = GET_FROM_STRETCH;
19755 it->object = prop;
19756 }
19757 #ifdef HAVE_WINDOW_SYSTEM
19758 else if (IMAGEP (prop))
19759 {
19760 it->what = IT_IMAGE;
19761 it->image_id = lookup_image (it->f, prop);
19762 it->method = GET_FROM_IMAGE;
19763 }
19764 #endif /* HAVE_WINDOW_SYSTEM */
19765 else
19766 {
19767 pop_it (it); /* bogus display property, give up */
19768 return false;
19769 }
19770
19771 return true;
19772 }
19773
19774 /* Return the character-property PROP at the current position in IT. */
19775
19776 static Lisp_Object
19777 get_it_property (struct it *it, Lisp_Object prop)
19778 {
19779 Lisp_Object position, object = it->object;
19780
19781 if (STRINGP (object))
19782 position = make_number (IT_STRING_CHARPOS (*it));
19783 else if (BUFFERP (object))
19784 {
19785 position = make_number (IT_CHARPOS (*it));
19786 object = it->window;
19787 }
19788 else
19789 return Qnil;
19790
19791 return Fget_char_property (position, prop, object);
19792 }
19793
19794 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19795
19796 static void
19797 handle_line_prefix (struct it *it)
19798 {
19799 Lisp_Object prefix;
19800
19801 if (it->continuation_lines_width > 0)
19802 {
19803 prefix = get_it_property (it, Qwrap_prefix);
19804 if (NILP (prefix))
19805 prefix = Vwrap_prefix;
19806 }
19807 else
19808 {
19809 prefix = get_it_property (it, Qline_prefix);
19810 if (NILP (prefix))
19811 prefix = Vline_prefix;
19812 }
19813 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19814 {
19815 /* If the prefix is wider than the window, and we try to wrap
19816 it, it would acquire its own wrap prefix, and so on till the
19817 iterator stack overflows. So, don't wrap the prefix. */
19818 it->line_wrap = TRUNCATE;
19819 it->avoid_cursor_p = true;
19820 }
19821 }
19822
19823 \f
19824
19825 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19826 only for R2L lines from display_line and display_string, when they
19827 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19828 the line/string needs to be continued on the next glyph row. */
19829 static void
19830 unproduce_glyphs (struct it *it, int n)
19831 {
19832 struct glyph *glyph, *end;
19833
19834 eassert (it->glyph_row);
19835 eassert (it->glyph_row->reversed_p);
19836 eassert (it->area == TEXT_AREA);
19837 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19838
19839 if (n > it->glyph_row->used[TEXT_AREA])
19840 n = it->glyph_row->used[TEXT_AREA];
19841 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19842 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19843 for ( ; glyph < end; glyph++)
19844 glyph[-n] = *glyph;
19845 }
19846
19847 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19848 and ROW->maxpos. */
19849 static void
19850 find_row_edges (struct it *it, struct glyph_row *row,
19851 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19852 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19853 {
19854 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19855 lines' rows is implemented for bidi-reordered rows. */
19856
19857 /* ROW->minpos is the value of min_pos, the minimal buffer position
19858 we have in ROW, or ROW->start.pos if that is smaller. */
19859 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19860 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19861 else
19862 /* We didn't find buffer positions smaller than ROW->start, or
19863 didn't find _any_ valid buffer positions in any of the glyphs,
19864 so we must trust the iterator's computed positions. */
19865 row->minpos = row->start.pos;
19866 if (max_pos <= 0)
19867 {
19868 max_pos = CHARPOS (it->current.pos);
19869 max_bpos = BYTEPOS (it->current.pos);
19870 }
19871
19872 /* Here are the various use-cases for ending the row, and the
19873 corresponding values for ROW->maxpos:
19874
19875 Line ends in a newline from buffer eol_pos + 1
19876 Line is continued from buffer max_pos + 1
19877 Line is truncated on right it->current.pos
19878 Line ends in a newline from string max_pos + 1(*)
19879 (*) + 1 only when line ends in a forward scan
19880 Line is continued from string max_pos
19881 Line is continued from display vector max_pos
19882 Line is entirely from a string min_pos == max_pos
19883 Line is entirely from a display vector min_pos == max_pos
19884 Line that ends at ZV ZV
19885
19886 If you discover other use-cases, please add them here as
19887 appropriate. */
19888 if (row->ends_at_zv_p)
19889 row->maxpos = it->current.pos;
19890 else if (row->used[TEXT_AREA])
19891 {
19892 bool seen_this_string = false;
19893 struct glyph_row *r1 = row - 1;
19894
19895 /* Did we see the same display string on the previous row? */
19896 if (STRINGP (it->object)
19897 /* this is not the first row */
19898 && row > it->w->desired_matrix->rows
19899 /* previous row is not the header line */
19900 && !r1->mode_line_p
19901 /* previous row also ends in a newline from a string */
19902 && r1->ends_in_newline_from_string_p)
19903 {
19904 struct glyph *start, *end;
19905
19906 /* Search for the last glyph of the previous row that came
19907 from buffer or string. Depending on whether the row is
19908 L2R or R2L, we need to process it front to back or the
19909 other way round. */
19910 if (!r1->reversed_p)
19911 {
19912 start = r1->glyphs[TEXT_AREA];
19913 end = start + r1->used[TEXT_AREA];
19914 /* Glyphs inserted by redisplay have nil as their object. */
19915 while (end > start
19916 && NILP ((end - 1)->object)
19917 && (end - 1)->charpos <= 0)
19918 --end;
19919 if (end > start)
19920 {
19921 if (EQ ((end - 1)->object, it->object))
19922 seen_this_string = true;
19923 }
19924 else
19925 /* If all the glyphs of the previous row were inserted
19926 by redisplay, it means the previous row was
19927 produced from a single newline, which is only
19928 possible if that newline came from the same string
19929 as the one which produced this ROW. */
19930 seen_this_string = true;
19931 }
19932 else
19933 {
19934 end = r1->glyphs[TEXT_AREA] - 1;
19935 start = end + r1->used[TEXT_AREA];
19936 while (end < start
19937 && NILP ((end + 1)->object)
19938 && (end + 1)->charpos <= 0)
19939 ++end;
19940 if (end < start)
19941 {
19942 if (EQ ((end + 1)->object, it->object))
19943 seen_this_string = true;
19944 }
19945 else
19946 seen_this_string = true;
19947 }
19948 }
19949 /* Take note of each display string that covers a newline only
19950 once, the first time we see it. This is for when a display
19951 string includes more than one newline in it. */
19952 if (row->ends_in_newline_from_string_p && !seen_this_string)
19953 {
19954 /* If we were scanning the buffer forward when we displayed
19955 the string, we want to account for at least one buffer
19956 position that belongs to this row (position covered by
19957 the display string), so that cursor positioning will
19958 consider this row as a candidate when point is at the end
19959 of the visual line represented by this row. This is not
19960 required when scanning back, because max_pos will already
19961 have a much larger value. */
19962 if (CHARPOS (row->end.pos) > max_pos)
19963 INC_BOTH (max_pos, max_bpos);
19964 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19965 }
19966 else if (CHARPOS (it->eol_pos) > 0)
19967 SET_TEXT_POS (row->maxpos,
19968 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19969 else if (row->continued_p)
19970 {
19971 /* If max_pos is different from IT's current position, it
19972 means IT->method does not belong to the display element
19973 at max_pos. However, it also means that the display
19974 element at max_pos was displayed in its entirety on this
19975 line, which is equivalent to saying that the next line
19976 starts at the next buffer position. */
19977 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19978 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19979 else
19980 {
19981 INC_BOTH (max_pos, max_bpos);
19982 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19983 }
19984 }
19985 else if (row->truncated_on_right_p)
19986 /* display_line already called reseat_at_next_visible_line_start,
19987 which puts the iterator at the beginning of the next line, in
19988 the logical order. */
19989 row->maxpos = it->current.pos;
19990 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19991 /* A line that is entirely from a string/image/stretch... */
19992 row->maxpos = row->minpos;
19993 else
19994 emacs_abort ();
19995 }
19996 else
19997 row->maxpos = it->current.pos;
19998 }
19999
20000 /* Construct the glyph row IT->glyph_row in the desired matrix of
20001 IT->w from text at the current position of IT. See dispextern.h
20002 for an overview of struct it. Value is true if
20003 IT->glyph_row displays text, as opposed to a line displaying ZV
20004 only. */
20005
20006 static bool
20007 display_line (struct it *it)
20008 {
20009 struct glyph_row *row = it->glyph_row;
20010 Lisp_Object overlay_arrow_string;
20011 struct it wrap_it;
20012 void *wrap_data = NULL;
20013 bool may_wrap = false;
20014 int wrap_x IF_LINT (= 0);
20015 int wrap_row_used = -1;
20016 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20017 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20018 int wrap_row_extra_line_spacing IF_LINT (= 0);
20019 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20020 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20021 int cvpos;
20022 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20023 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20024 bool pending_handle_line_prefix = false;
20025
20026 /* We always start displaying at hpos zero even if hscrolled. */
20027 eassert (it->hpos == 0 && it->current_x == 0);
20028
20029 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20030 >= it->w->desired_matrix->nrows)
20031 {
20032 it->w->nrows_scale_factor++;
20033 it->f->fonts_changed = true;
20034 return false;
20035 }
20036
20037 /* Clear the result glyph row and enable it. */
20038 prepare_desired_row (it->w, row, false);
20039
20040 row->y = it->current_y;
20041 row->start = it->start;
20042 row->continuation_lines_width = it->continuation_lines_width;
20043 row->displays_text_p = true;
20044 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20045 it->starts_in_middle_of_char_p = false;
20046
20047 /* Arrange the overlays nicely for our purposes. Usually, we call
20048 display_line on only one line at a time, in which case this
20049 can't really hurt too much, or we call it on lines which appear
20050 one after another in the buffer, in which case all calls to
20051 recenter_overlay_lists but the first will be pretty cheap. */
20052 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20053
20054 /* Move over display elements that are not visible because we are
20055 hscrolled. This may stop at an x-position < IT->first_visible_x
20056 if the first glyph is partially visible or if we hit a line end. */
20057 if (it->current_x < it->first_visible_x)
20058 {
20059 enum move_it_result move_result;
20060
20061 this_line_min_pos = row->start.pos;
20062 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20063 MOVE_TO_POS | MOVE_TO_X);
20064 /* If we are under a large hscroll, move_it_in_display_line_to
20065 could hit the end of the line without reaching
20066 it->first_visible_x. Pretend that we did reach it. This is
20067 especially important on a TTY, where we will call
20068 extend_face_to_end_of_line, which needs to know how many
20069 blank glyphs to produce. */
20070 if (it->current_x < it->first_visible_x
20071 && (move_result == MOVE_NEWLINE_OR_CR
20072 || move_result == MOVE_POS_MATCH_OR_ZV))
20073 it->current_x = it->first_visible_x;
20074
20075 /* Record the smallest positions seen while we moved over
20076 display elements that are not visible. This is needed by
20077 redisplay_internal for optimizing the case where the cursor
20078 stays inside the same line. The rest of this function only
20079 considers positions that are actually displayed, so
20080 RECORD_MAX_MIN_POS will not otherwise record positions that
20081 are hscrolled to the left of the left edge of the window. */
20082 min_pos = CHARPOS (this_line_min_pos);
20083 min_bpos = BYTEPOS (this_line_min_pos);
20084 }
20085 else if (it->area == TEXT_AREA)
20086 {
20087 /* We only do this when not calling move_it_in_display_line_to
20088 above, because that function calls itself handle_line_prefix. */
20089 handle_line_prefix (it);
20090 }
20091 else
20092 {
20093 /* Line-prefix and wrap-prefix are always displayed in the text
20094 area. But if this is the first call to display_line after
20095 init_iterator, the iterator might have been set up to write
20096 into a marginal area, e.g. if the line begins with some
20097 display property that writes to the margins. So we need to
20098 wait with the call to handle_line_prefix until whatever
20099 writes to the margin has done its job. */
20100 pending_handle_line_prefix = true;
20101 }
20102
20103 /* Get the initial row height. This is either the height of the
20104 text hscrolled, if there is any, or zero. */
20105 row->ascent = it->max_ascent;
20106 row->height = it->max_ascent + it->max_descent;
20107 row->phys_ascent = it->max_phys_ascent;
20108 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20109 row->extra_line_spacing = it->max_extra_line_spacing;
20110
20111 /* Utility macro to record max and min buffer positions seen until now. */
20112 #define RECORD_MAX_MIN_POS(IT) \
20113 do \
20114 { \
20115 bool composition_p \
20116 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20117 ptrdiff_t current_pos = \
20118 composition_p ? (IT)->cmp_it.charpos \
20119 : IT_CHARPOS (*(IT)); \
20120 ptrdiff_t current_bpos = \
20121 composition_p ? CHAR_TO_BYTE (current_pos) \
20122 : IT_BYTEPOS (*(IT)); \
20123 if (current_pos < min_pos) \
20124 { \
20125 min_pos = current_pos; \
20126 min_bpos = current_bpos; \
20127 } \
20128 if (IT_CHARPOS (*it) > max_pos) \
20129 { \
20130 max_pos = IT_CHARPOS (*it); \
20131 max_bpos = IT_BYTEPOS (*it); \
20132 } \
20133 } \
20134 while (false)
20135
20136 /* Loop generating characters. The loop is left with IT on the next
20137 character to display. */
20138 while (true)
20139 {
20140 int n_glyphs_before, hpos_before, x_before;
20141 int x, nglyphs;
20142 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20143
20144 /* Retrieve the next thing to display. Value is false if end of
20145 buffer reached. */
20146 if (!get_next_display_element (it))
20147 {
20148 /* Maybe add a space at the end of this line that is used to
20149 display the cursor there under X. Set the charpos of the
20150 first glyph of blank lines not corresponding to any text
20151 to -1. */
20152 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20153 row->exact_window_width_line_p = true;
20154 else if ((append_space_for_newline (it, true)
20155 && row->used[TEXT_AREA] == 1)
20156 || row->used[TEXT_AREA] == 0)
20157 {
20158 row->glyphs[TEXT_AREA]->charpos = -1;
20159 row->displays_text_p = false;
20160
20161 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20162 && (!MINI_WINDOW_P (it->w)
20163 || (minibuf_level && EQ (it->window, minibuf_window))))
20164 row->indicate_empty_line_p = true;
20165 }
20166
20167 it->continuation_lines_width = 0;
20168 row->ends_at_zv_p = true;
20169 /* A row that displays right-to-left text must always have
20170 its last face extended all the way to the end of line,
20171 even if this row ends in ZV, because we still write to
20172 the screen left to right. We also need to extend the
20173 last face if the default face is remapped to some
20174 different face, otherwise the functions that clear
20175 portions of the screen will clear with the default face's
20176 background color. */
20177 if (row->reversed_p
20178 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20179 extend_face_to_end_of_line (it);
20180 break;
20181 }
20182
20183 /* Now, get the metrics of what we want to display. This also
20184 generates glyphs in `row' (which is IT->glyph_row). */
20185 n_glyphs_before = row->used[TEXT_AREA];
20186 x = it->current_x;
20187
20188 /* Remember the line height so far in case the next element doesn't
20189 fit on the line. */
20190 if (it->line_wrap != TRUNCATE)
20191 {
20192 ascent = it->max_ascent;
20193 descent = it->max_descent;
20194 phys_ascent = it->max_phys_ascent;
20195 phys_descent = it->max_phys_descent;
20196
20197 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20198 {
20199 if (IT_DISPLAYING_WHITESPACE (it))
20200 may_wrap = true;
20201 else if (may_wrap)
20202 {
20203 SAVE_IT (wrap_it, *it, wrap_data);
20204 wrap_x = x;
20205 wrap_row_used = row->used[TEXT_AREA];
20206 wrap_row_ascent = row->ascent;
20207 wrap_row_height = row->height;
20208 wrap_row_phys_ascent = row->phys_ascent;
20209 wrap_row_phys_height = row->phys_height;
20210 wrap_row_extra_line_spacing = row->extra_line_spacing;
20211 wrap_row_min_pos = min_pos;
20212 wrap_row_min_bpos = min_bpos;
20213 wrap_row_max_pos = max_pos;
20214 wrap_row_max_bpos = max_bpos;
20215 may_wrap = false;
20216 }
20217 }
20218 }
20219
20220 PRODUCE_GLYPHS (it);
20221
20222 /* If this display element was in marginal areas, continue with
20223 the next one. */
20224 if (it->area != TEXT_AREA)
20225 {
20226 row->ascent = max (row->ascent, it->max_ascent);
20227 row->height = max (row->height, it->max_ascent + it->max_descent);
20228 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20229 row->phys_height = max (row->phys_height,
20230 it->max_phys_ascent + it->max_phys_descent);
20231 row->extra_line_spacing = max (row->extra_line_spacing,
20232 it->max_extra_line_spacing);
20233 set_iterator_to_next (it, true);
20234 /* If we didn't handle the line/wrap prefix above, and the
20235 call to set_iterator_to_next just switched to TEXT_AREA,
20236 process the prefix now. */
20237 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20238 {
20239 pending_handle_line_prefix = false;
20240 handle_line_prefix (it);
20241 }
20242 continue;
20243 }
20244
20245 /* Does the display element fit on the line? If we truncate
20246 lines, we should draw past the right edge of the window. If
20247 we don't truncate, we want to stop so that we can display the
20248 continuation glyph before the right margin. If lines are
20249 continued, there are two possible strategies for characters
20250 resulting in more than 1 glyph (e.g. tabs): Display as many
20251 glyphs as possible in this line and leave the rest for the
20252 continuation line, or display the whole element in the next
20253 line. Original redisplay did the former, so we do it also. */
20254 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20255 hpos_before = it->hpos;
20256 x_before = x;
20257
20258 if (/* Not a newline. */
20259 nglyphs > 0
20260 /* Glyphs produced fit entirely in the line. */
20261 && it->current_x < it->last_visible_x)
20262 {
20263 it->hpos += nglyphs;
20264 row->ascent = max (row->ascent, it->max_ascent);
20265 row->height = max (row->height, it->max_ascent + it->max_descent);
20266 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20267 row->phys_height = max (row->phys_height,
20268 it->max_phys_ascent + it->max_phys_descent);
20269 row->extra_line_spacing = max (row->extra_line_spacing,
20270 it->max_extra_line_spacing);
20271 if (it->current_x - it->pixel_width < it->first_visible_x
20272 /* In R2L rows, we arrange in extend_face_to_end_of_line
20273 to add a right offset to the line, by a suitable
20274 change to the stretch glyph that is the leftmost
20275 glyph of the line. */
20276 && !row->reversed_p)
20277 row->x = x - it->first_visible_x;
20278 /* Record the maximum and minimum buffer positions seen so
20279 far in glyphs that will be displayed by this row. */
20280 if (it->bidi_p)
20281 RECORD_MAX_MIN_POS (it);
20282 }
20283 else
20284 {
20285 int i, new_x;
20286 struct glyph *glyph;
20287
20288 for (i = 0; i < nglyphs; ++i, x = new_x)
20289 {
20290 /* Identify the glyphs added by the last call to
20291 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20292 the previous glyphs. */
20293 if (!row->reversed_p)
20294 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20295 else
20296 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20297 new_x = x + glyph->pixel_width;
20298
20299 if (/* Lines are continued. */
20300 it->line_wrap != TRUNCATE
20301 && (/* Glyph doesn't fit on the line. */
20302 new_x > it->last_visible_x
20303 /* Or it fits exactly on a window system frame. */
20304 || (new_x == it->last_visible_x
20305 && FRAME_WINDOW_P (it->f)
20306 && (row->reversed_p
20307 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20308 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20309 {
20310 /* End of a continued line. */
20311
20312 if (it->hpos == 0
20313 || (new_x == it->last_visible_x
20314 && FRAME_WINDOW_P (it->f)
20315 && (row->reversed_p
20316 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20317 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20318 {
20319 /* Current glyph is the only one on the line or
20320 fits exactly on the line. We must continue
20321 the line because we can't draw the cursor
20322 after the glyph. */
20323 row->continued_p = true;
20324 it->current_x = new_x;
20325 it->continuation_lines_width += new_x;
20326 ++it->hpos;
20327 if (i == nglyphs - 1)
20328 {
20329 /* If line-wrap is on, check if a previous
20330 wrap point was found. */
20331 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20332 && wrap_row_used > 0
20333 /* Even if there is a previous wrap
20334 point, continue the line here as
20335 usual, if (i) the previous character
20336 was a space or tab AND (ii) the
20337 current character is not. */
20338 && (!may_wrap
20339 || IT_DISPLAYING_WHITESPACE (it)))
20340 goto back_to_wrap;
20341
20342 /* Record the maximum and minimum buffer
20343 positions seen so far in glyphs that will be
20344 displayed by this row. */
20345 if (it->bidi_p)
20346 RECORD_MAX_MIN_POS (it);
20347 set_iterator_to_next (it, true);
20348 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20349 {
20350 if (!get_next_display_element (it))
20351 {
20352 row->exact_window_width_line_p = true;
20353 it->continuation_lines_width = 0;
20354 row->continued_p = false;
20355 row->ends_at_zv_p = true;
20356 }
20357 else if (ITERATOR_AT_END_OF_LINE_P (it))
20358 {
20359 row->continued_p = false;
20360 row->exact_window_width_line_p = true;
20361 }
20362 /* If line-wrap is on, check if a
20363 previous wrap point was found. */
20364 else if (wrap_row_used > 0
20365 /* Even if there is a previous wrap
20366 point, continue the line here as
20367 usual, if (i) the previous character
20368 was a space or tab AND (ii) the
20369 current character is not. */
20370 && (!may_wrap
20371 || IT_DISPLAYING_WHITESPACE (it)))
20372 goto back_to_wrap;
20373
20374 }
20375 }
20376 else if (it->bidi_p)
20377 RECORD_MAX_MIN_POS (it);
20378 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20379 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20380 extend_face_to_end_of_line (it);
20381 }
20382 else if (CHAR_GLYPH_PADDING_P (*glyph)
20383 && !FRAME_WINDOW_P (it->f))
20384 {
20385 /* A padding glyph that doesn't fit on this line.
20386 This means the whole character doesn't fit
20387 on the line. */
20388 if (row->reversed_p)
20389 unproduce_glyphs (it, row->used[TEXT_AREA]
20390 - n_glyphs_before);
20391 row->used[TEXT_AREA] = n_glyphs_before;
20392
20393 /* Fill the rest of the row with continuation
20394 glyphs like in 20.x. */
20395 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20396 < row->glyphs[1 + TEXT_AREA])
20397 produce_special_glyphs (it, IT_CONTINUATION);
20398
20399 row->continued_p = true;
20400 it->current_x = x_before;
20401 it->continuation_lines_width += x_before;
20402
20403 /* Restore the height to what it was before the
20404 element not fitting on the line. */
20405 it->max_ascent = ascent;
20406 it->max_descent = descent;
20407 it->max_phys_ascent = phys_ascent;
20408 it->max_phys_descent = phys_descent;
20409 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20410 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20411 extend_face_to_end_of_line (it);
20412 }
20413 else if (wrap_row_used > 0)
20414 {
20415 back_to_wrap:
20416 if (row->reversed_p)
20417 unproduce_glyphs (it,
20418 row->used[TEXT_AREA] - wrap_row_used);
20419 RESTORE_IT (it, &wrap_it, wrap_data);
20420 it->continuation_lines_width += wrap_x;
20421 row->used[TEXT_AREA] = wrap_row_used;
20422 row->ascent = wrap_row_ascent;
20423 row->height = wrap_row_height;
20424 row->phys_ascent = wrap_row_phys_ascent;
20425 row->phys_height = wrap_row_phys_height;
20426 row->extra_line_spacing = wrap_row_extra_line_spacing;
20427 min_pos = wrap_row_min_pos;
20428 min_bpos = wrap_row_min_bpos;
20429 max_pos = wrap_row_max_pos;
20430 max_bpos = wrap_row_max_bpos;
20431 row->continued_p = true;
20432 row->ends_at_zv_p = false;
20433 row->exact_window_width_line_p = false;
20434 it->continuation_lines_width += x;
20435
20436 /* Make sure that a non-default face is extended
20437 up to the right margin of the window. */
20438 extend_face_to_end_of_line (it);
20439 }
20440 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20441 {
20442 /* A TAB that extends past the right edge of the
20443 window. This produces a single glyph on
20444 window system frames. We leave the glyph in
20445 this row and let it fill the row, but don't
20446 consume the TAB. */
20447 if ((row->reversed_p
20448 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20449 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20450 produce_special_glyphs (it, IT_CONTINUATION);
20451 it->continuation_lines_width += it->last_visible_x;
20452 row->ends_in_middle_of_char_p = true;
20453 row->continued_p = true;
20454 glyph->pixel_width = it->last_visible_x - x;
20455 it->starts_in_middle_of_char_p = true;
20456 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20457 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20458 extend_face_to_end_of_line (it);
20459 }
20460 else
20461 {
20462 /* Something other than a TAB that draws past
20463 the right edge of the window. Restore
20464 positions to values before the element. */
20465 if (row->reversed_p)
20466 unproduce_glyphs (it, row->used[TEXT_AREA]
20467 - (n_glyphs_before + i));
20468 row->used[TEXT_AREA] = n_glyphs_before + i;
20469
20470 /* Display continuation glyphs. */
20471 it->current_x = x_before;
20472 it->continuation_lines_width += x;
20473 if (!FRAME_WINDOW_P (it->f)
20474 || (row->reversed_p
20475 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20476 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20477 produce_special_glyphs (it, IT_CONTINUATION);
20478 row->continued_p = true;
20479
20480 extend_face_to_end_of_line (it);
20481
20482 if (nglyphs > 1 && i > 0)
20483 {
20484 row->ends_in_middle_of_char_p = true;
20485 it->starts_in_middle_of_char_p = true;
20486 }
20487
20488 /* Restore the height to what it was before the
20489 element not fitting on the line. */
20490 it->max_ascent = ascent;
20491 it->max_descent = descent;
20492 it->max_phys_ascent = phys_ascent;
20493 it->max_phys_descent = phys_descent;
20494 }
20495
20496 break;
20497 }
20498 else if (new_x > it->first_visible_x)
20499 {
20500 /* Increment number of glyphs actually displayed. */
20501 ++it->hpos;
20502
20503 /* Record the maximum and minimum buffer positions
20504 seen so far in glyphs that will be displayed by
20505 this row. */
20506 if (it->bidi_p)
20507 RECORD_MAX_MIN_POS (it);
20508
20509 if (x < it->first_visible_x && !row->reversed_p)
20510 /* Glyph is partially visible, i.e. row starts at
20511 negative X position. Don't do that in R2L
20512 rows, where we arrange to add a right offset to
20513 the line in extend_face_to_end_of_line, by a
20514 suitable change to the stretch glyph that is
20515 the leftmost glyph of the line. */
20516 row->x = x - it->first_visible_x;
20517 /* When the last glyph of an R2L row only fits
20518 partially on the line, we need to set row->x to a
20519 negative offset, so that the leftmost glyph is
20520 the one that is partially visible. But if we are
20521 going to produce the truncation glyph, this will
20522 be taken care of in produce_special_glyphs. */
20523 if (row->reversed_p
20524 && new_x > it->last_visible_x
20525 && !(it->line_wrap == TRUNCATE
20526 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20527 {
20528 eassert (FRAME_WINDOW_P (it->f));
20529 row->x = it->last_visible_x - new_x;
20530 }
20531 }
20532 else
20533 {
20534 /* Glyph is completely off the left margin of the
20535 window. This should not happen because of the
20536 move_it_in_display_line at the start of this
20537 function, unless the text display area of the
20538 window is empty. */
20539 eassert (it->first_visible_x <= it->last_visible_x);
20540 }
20541 }
20542 /* Even if this display element produced no glyphs at all,
20543 we want to record its position. */
20544 if (it->bidi_p && nglyphs == 0)
20545 RECORD_MAX_MIN_POS (it);
20546
20547 row->ascent = max (row->ascent, it->max_ascent);
20548 row->height = max (row->height, it->max_ascent + it->max_descent);
20549 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20550 row->phys_height = max (row->phys_height,
20551 it->max_phys_ascent + it->max_phys_descent);
20552 row->extra_line_spacing = max (row->extra_line_spacing,
20553 it->max_extra_line_spacing);
20554
20555 /* End of this display line if row is continued. */
20556 if (row->continued_p || row->ends_at_zv_p)
20557 break;
20558 }
20559
20560 at_end_of_line:
20561 /* Is this a line end? If yes, we're also done, after making
20562 sure that a non-default face is extended up to the right
20563 margin of the window. */
20564 if (ITERATOR_AT_END_OF_LINE_P (it))
20565 {
20566 int used_before = row->used[TEXT_AREA];
20567
20568 row->ends_in_newline_from_string_p = STRINGP (it->object);
20569
20570 /* Add a space at the end of the line that is used to
20571 display the cursor there. */
20572 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20573 append_space_for_newline (it, false);
20574
20575 /* Extend the face to the end of the line. */
20576 extend_face_to_end_of_line (it);
20577
20578 /* Make sure we have the position. */
20579 if (used_before == 0)
20580 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20581
20582 /* Record the position of the newline, for use in
20583 find_row_edges. */
20584 it->eol_pos = it->current.pos;
20585
20586 /* Consume the line end. This skips over invisible lines. */
20587 set_iterator_to_next (it, true);
20588 it->continuation_lines_width = 0;
20589 break;
20590 }
20591
20592 /* Proceed with next display element. Note that this skips
20593 over lines invisible because of selective display. */
20594 set_iterator_to_next (it, true);
20595
20596 /* If we truncate lines, we are done when the last displayed
20597 glyphs reach past the right margin of the window. */
20598 if (it->line_wrap == TRUNCATE
20599 && ((FRAME_WINDOW_P (it->f)
20600 /* Images are preprocessed in produce_image_glyph such
20601 that they are cropped at the right edge of the
20602 window, so an image glyph will always end exactly at
20603 last_visible_x, even if there's no right fringe. */
20604 && ((row->reversed_p
20605 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20606 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20607 || it->what == IT_IMAGE))
20608 ? (it->current_x >= it->last_visible_x)
20609 : (it->current_x > it->last_visible_x)))
20610 {
20611 /* Maybe add truncation glyphs. */
20612 if (!FRAME_WINDOW_P (it->f)
20613 || (row->reversed_p
20614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20616 {
20617 int i, n;
20618
20619 if (!row->reversed_p)
20620 {
20621 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20622 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20623 break;
20624 }
20625 else
20626 {
20627 for (i = 0; i < row->used[TEXT_AREA]; i++)
20628 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20629 break;
20630 /* Remove any padding glyphs at the front of ROW, to
20631 make room for the truncation glyphs we will be
20632 adding below. The loop below always inserts at
20633 least one truncation glyph, so also remove the
20634 last glyph added to ROW. */
20635 unproduce_glyphs (it, i + 1);
20636 /* Adjust i for the loop below. */
20637 i = row->used[TEXT_AREA] - (i + 1);
20638 }
20639
20640 /* produce_special_glyphs overwrites the last glyph, so
20641 we don't want that if we want to keep that last
20642 glyph, which means it's an image. */
20643 if (it->current_x > it->last_visible_x)
20644 {
20645 it->current_x = x_before;
20646 if (!FRAME_WINDOW_P (it->f))
20647 {
20648 for (n = row->used[TEXT_AREA]; i < n; ++i)
20649 {
20650 row->used[TEXT_AREA] = i;
20651 produce_special_glyphs (it, IT_TRUNCATION);
20652 }
20653 }
20654 else
20655 {
20656 row->used[TEXT_AREA] = i;
20657 produce_special_glyphs (it, IT_TRUNCATION);
20658 }
20659 it->hpos = hpos_before;
20660 }
20661 }
20662 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20663 {
20664 /* Don't truncate if we can overflow newline into fringe. */
20665 if (!get_next_display_element (it))
20666 {
20667 it->continuation_lines_width = 0;
20668 row->ends_at_zv_p = true;
20669 row->exact_window_width_line_p = true;
20670 break;
20671 }
20672 if (ITERATOR_AT_END_OF_LINE_P (it))
20673 {
20674 row->exact_window_width_line_p = true;
20675 goto at_end_of_line;
20676 }
20677 it->current_x = x_before;
20678 it->hpos = hpos_before;
20679 }
20680
20681 row->truncated_on_right_p = true;
20682 it->continuation_lines_width = 0;
20683 reseat_at_next_visible_line_start (it, false);
20684 /* We insist below that IT's position be at ZV because in
20685 bidi-reordered lines the character at visible line start
20686 might not be the character that follows the newline in
20687 the logical order. */
20688 if (IT_BYTEPOS (*it) > BEG_BYTE)
20689 row->ends_at_zv_p =
20690 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20691 else
20692 row->ends_at_zv_p = false;
20693 break;
20694 }
20695 }
20696
20697 if (wrap_data)
20698 bidi_unshelve_cache (wrap_data, true);
20699
20700 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20701 at the left window margin. */
20702 if (it->first_visible_x
20703 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20704 {
20705 if (!FRAME_WINDOW_P (it->f)
20706 || (((row->reversed_p
20707 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20708 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20709 /* Don't let insert_left_trunc_glyphs overwrite the
20710 first glyph of the row if it is an image. */
20711 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20712 insert_left_trunc_glyphs (it);
20713 row->truncated_on_left_p = true;
20714 }
20715
20716 /* Remember the position at which this line ends.
20717
20718 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20719 cannot be before the call to find_row_edges below, since that is
20720 where these positions are determined. */
20721 row->end = it->current;
20722 if (!it->bidi_p)
20723 {
20724 row->minpos = row->start.pos;
20725 row->maxpos = row->end.pos;
20726 }
20727 else
20728 {
20729 /* ROW->minpos and ROW->maxpos must be the smallest and
20730 `1 + the largest' buffer positions in ROW. But if ROW was
20731 bidi-reordered, these two positions can be anywhere in the
20732 row, so we must determine them now. */
20733 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20734 }
20735
20736 /* If the start of this line is the overlay arrow-position, then
20737 mark this glyph row as the one containing the overlay arrow.
20738 This is clearly a mess with variable size fonts. It would be
20739 better to let it be displayed like cursors under X. */
20740 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20741 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20742 !NILP (overlay_arrow_string)))
20743 {
20744 /* Overlay arrow in window redisplay is a fringe bitmap. */
20745 if (STRINGP (overlay_arrow_string))
20746 {
20747 struct glyph_row *arrow_row
20748 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20749 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20750 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20751 struct glyph *p = row->glyphs[TEXT_AREA];
20752 struct glyph *p2, *end;
20753
20754 /* Copy the arrow glyphs. */
20755 while (glyph < arrow_end)
20756 *p++ = *glyph++;
20757
20758 /* Throw away padding glyphs. */
20759 p2 = p;
20760 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20761 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20762 ++p2;
20763 if (p2 > p)
20764 {
20765 while (p2 < end)
20766 *p++ = *p2++;
20767 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20768 }
20769 }
20770 else
20771 {
20772 eassert (INTEGERP (overlay_arrow_string));
20773 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20774 }
20775 overlay_arrow_seen = true;
20776 }
20777
20778 /* Highlight trailing whitespace. */
20779 if (!NILP (Vshow_trailing_whitespace))
20780 highlight_trailing_whitespace (it->f, it->glyph_row);
20781
20782 /* Compute pixel dimensions of this line. */
20783 compute_line_metrics (it);
20784
20785 /* Implementation note: No changes in the glyphs of ROW or in their
20786 faces can be done past this point, because compute_line_metrics
20787 computes ROW's hash value and stores it within the glyph_row
20788 structure. */
20789
20790 /* Record whether this row ends inside an ellipsis. */
20791 row->ends_in_ellipsis_p
20792 = (it->method == GET_FROM_DISPLAY_VECTOR
20793 && it->ellipsis_p);
20794
20795 /* Save fringe bitmaps in this row. */
20796 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20797 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20798 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20799 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20800
20801 it->left_user_fringe_bitmap = 0;
20802 it->left_user_fringe_face_id = 0;
20803 it->right_user_fringe_bitmap = 0;
20804 it->right_user_fringe_face_id = 0;
20805
20806 /* Maybe set the cursor. */
20807 cvpos = it->w->cursor.vpos;
20808 if ((cvpos < 0
20809 /* In bidi-reordered rows, keep checking for proper cursor
20810 position even if one has been found already, because buffer
20811 positions in such rows change non-linearly with ROW->VPOS,
20812 when a line is continued. One exception: when we are at ZV,
20813 display cursor on the first suitable glyph row, since all
20814 the empty rows after that also have their position set to ZV. */
20815 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20816 lines' rows is implemented for bidi-reordered rows. */
20817 || (it->bidi_p
20818 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20819 && PT >= MATRIX_ROW_START_CHARPOS (row)
20820 && PT <= MATRIX_ROW_END_CHARPOS (row)
20821 && cursor_row_p (row))
20822 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20823
20824 /* Prepare for the next line. This line starts horizontally at (X
20825 HPOS) = (0 0). Vertical positions are incremented. As a
20826 convenience for the caller, IT->glyph_row is set to the next
20827 row to be used. */
20828 it->current_x = it->hpos = 0;
20829 it->current_y += row->height;
20830 SET_TEXT_POS (it->eol_pos, 0, 0);
20831 ++it->vpos;
20832 ++it->glyph_row;
20833 /* The next row should by default use the same value of the
20834 reversed_p flag as this one. set_iterator_to_next decides when
20835 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20836 the flag accordingly. */
20837 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20838 it->glyph_row->reversed_p = row->reversed_p;
20839 it->start = row->end;
20840 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20841
20842 #undef RECORD_MAX_MIN_POS
20843 }
20844
20845 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20846 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20847 doc: /* Return paragraph direction at point in BUFFER.
20848 Value is either `left-to-right' or `right-to-left'.
20849 If BUFFER is omitted or nil, it defaults to the current buffer.
20850
20851 Paragraph direction determines how the text in the paragraph is displayed.
20852 In left-to-right paragraphs, text begins at the left margin of the window
20853 and the reading direction is generally left to right. In right-to-left
20854 paragraphs, text begins at the right margin and is read from right to left.
20855
20856 See also `bidi-paragraph-direction'. */)
20857 (Lisp_Object buffer)
20858 {
20859 struct buffer *buf = current_buffer;
20860 struct buffer *old = buf;
20861
20862 if (! NILP (buffer))
20863 {
20864 CHECK_BUFFER (buffer);
20865 buf = XBUFFER (buffer);
20866 }
20867
20868 if (NILP (BVAR (buf, bidi_display_reordering))
20869 || NILP (BVAR (buf, enable_multibyte_characters))
20870 /* When we are loading loadup.el, the character property tables
20871 needed for bidi iteration are not yet available. */
20872 || !NILP (Vpurify_flag))
20873 return Qleft_to_right;
20874 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20875 return BVAR (buf, bidi_paragraph_direction);
20876 else
20877 {
20878 /* Determine the direction from buffer text. We could try to
20879 use current_matrix if it is up to date, but this seems fast
20880 enough as it is. */
20881 struct bidi_it itb;
20882 ptrdiff_t pos = BUF_PT (buf);
20883 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20884 int c;
20885 void *itb_data = bidi_shelve_cache ();
20886
20887 set_buffer_temp (buf);
20888 /* bidi_paragraph_init finds the base direction of the paragraph
20889 by searching forward from paragraph start. We need the base
20890 direction of the current or _previous_ paragraph, so we need
20891 to make sure we are within that paragraph. To that end, find
20892 the previous non-empty line. */
20893 if (pos >= ZV && pos > BEGV)
20894 DEC_BOTH (pos, bytepos);
20895 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20896 if (fast_looking_at (trailing_white_space,
20897 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20898 {
20899 while ((c = FETCH_BYTE (bytepos)) == '\n'
20900 || c == ' ' || c == '\t' || c == '\f')
20901 {
20902 if (bytepos <= BEGV_BYTE)
20903 break;
20904 bytepos--;
20905 pos--;
20906 }
20907 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20908 bytepos--;
20909 }
20910 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20911 itb.paragraph_dir = NEUTRAL_DIR;
20912 itb.string.s = NULL;
20913 itb.string.lstring = Qnil;
20914 itb.string.bufpos = 0;
20915 itb.string.from_disp_str = false;
20916 itb.string.unibyte = false;
20917 /* We have no window to use here for ignoring window-specific
20918 overlays. Using NULL for window pointer will cause
20919 compute_display_string_pos to use the current buffer. */
20920 itb.w = NULL;
20921 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
20922 bidi_unshelve_cache (itb_data, false);
20923 set_buffer_temp (old);
20924 switch (itb.paragraph_dir)
20925 {
20926 case L2R:
20927 return Qleft_to_right;
20928 break;
20929 case R2L:
20930 return Qright_to_left;
20931 break;
20932 default:
20933 emacs_abort ();
20934 }
20935 }
20936 }
20937
20938 DEFUN ("bidi-find-overridden-directionality",
20939 Fbidi_find_overridden_directionality,
20940 Sbidi_find_overridden_directionality, 2, 3, 0,
20941 doc: /* Return position between FROM and TO where directionality was overridden.
20942
20943 This function returns the first character position in the specified
20944 region of OBJECT where there is a character whose `bidi-class' property
20945 is `L', but which was forced to display as `R' by a directional
20946 override, and likewise with characters whose `bidi-class' is `R'
20947 or `AL' that were forced to display as `L'.
20948
20949 If no such character is found, the function returns nil.
20950
20951 OBJECT is a Lisp string or buffer to search for overridden
20952 directionality, and defaults to the current buffer if nil or omitted.
20953 OBJECT can also be a window, in which case the function will search
20954 the buffer displayed in that window. Passing the window instead of
20955 a buffer is preferable when the buffer is displayed in some window,
20956 because this function will then be able to correctly account for
20957 window-specific overlays, which can affect the results.
20958
20959 Strong directional characters `L', `R', and `AL' can have their
20960 intrinsic directionality overridden by directional override
20961 control characters RLO \(u+202e) and LRO \(u+202d). See the
20962 function `get-char-code-property' for a way to inquire about
20963 the `bidi-class' property of a character. */)
20964 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20965 {
20966 struct buffer *buf = current_buffer;
20967 struct buffer *old = buf;
20968 struct window *w = NULL;
20969 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20970 struct bidi_it itb;
20971 ptrdiff_t from_pos, to_pos, from_bpos;
20972 void *itb_data;
20973
20974 if (!NILP (object))
20975 {
20976 if (BUFFERP (object))
20977 buf = XBUFFER (object);
20978 else if (WINDOWP (object))
20979 {
20980 w = decode_live_window (object);
20981 buf = XBUFFER (w->contents);
20982 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20983 }
20984 else
20985 CHECK_STRING (object);
20986 }
20987
20988 if (STRINGP (object))
20989 {
20990 /* Characters in unibyte strings are always treated by bidi.c as
20991 strong LTR. */
20992 if (!STRING_MULTIBYTE (object)
20993 /* When we are loading loadup.el, the character property
20994 tables needed for bidi iteration are not yet
20995 available. */
20996 || !NILP (Vpurify_flag))
20997 return Qnil;
20998
20999 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21000 if (from_pos >= SCHARS (object))
21001 return Qnil;
21002
21003 /* Set up the bidi iterator. */
21004 itb_data = bidi_shelve_cache ();
21005 itb.paragraph_dir = NEUTRAL_DIR;
21006 itb.string.lstring = object;
21007 itb.string.s = NULL;
21008 itb.string.schars = SCHARS (object);
21009 itb.string.bufpos = 0;
21010 itb.string.from_disp_str = false;
21011 itb.string.unibyte = false;
21012 itb.w = w;
21013 bidi_init_it (0, 0, frame_window_p, &itb);
21014 }
21015 else
21016 {
21017 /* Nothing this fancy can happen in unibyte buffers, or in a
21018 buffer that disabled reordering, or if FROM is at EOB. */
21019 if (NILP (BVAR (buf, bidi_display_reordering))
21020 || NILP (BVAR (buf, enable_multibyte_characters))
21021 /* When we are loading loadup.el, the character property
21022 tables needed for bidi iteration are not yet
21023 available. */
21024 || !NILP (Vpurify_flag))
21025 return Qnil;
21026
21027 set_buffer_temp (buf);
21028 validate_region (&from, &to);
21029 from_pos = XINT (from);
21030 to_pos = XINT (to);
21031 if (from_pos >= ZV)
21032 return Qnil;
21033
21034 /* Set up the bidi iterator. */
21035 itb_data = bidi_shelve_cache ();
21036 from_bpos = CHAR_TO_BYTE (from_pos);
21037 if (from_pos == BEGV)
21038 {
21039 itb.charpos = BEGV;
21040 itb.bytepos = BEGV_BYTE;
21041 }
21042 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21043 {
21044 itb.charpos = from_pos;
21045 itb.bytepos = from_bpos;
21046 }
21047 else
21048 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21049 -1, &itb.bytepos);
21050 itb.paragraph_dir = NEUTRAL_DIR;
21051 itb.string.s = NULL;
21052 itb.string.lstring = Qnil;
21053 itb.string.bufpos = 0;
21054 itb.string.from_disp_str = false;
21055 itb.string.unibyte = false;
21056 itb.w = w;
21057 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21058 }
21059
21060 ptrdiff_t found;
21061 do {
21062 /* For the purposes of this function, the actual base direction of
21063 the paragraph doesn't matter, so just set it to L2R. */
21064 bidi_paragraph_init (L2R, &itb, false);
21065 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21066 ;
21067 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21068
21069 bidi_unshelve_cache (itb_data, false);
21070 set_buffer_temp (old);
21071
21072 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21073 }
21074
21075 DEFUN ("move-point-visually", Fmove_point_visually,
21076 Smove_point_visually, 1, 1, 0,
21077 doc: /* Move point in the visual order in the specified DIRECTION.
21078 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21079 left.
21080
21081 Value is the new character position of point. */)
21082 (Lisp_Object direction)
21083 {
21084 struct window *w = XWINDOW (selected_window);
21085 struct buffer *b = XBUFFER (w->contents);
21086 struct glyph_row *row;
21087 int dir;
21088 Lisp_Object paragraph_dir;
21089
21090 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21091 (!(ROW)->continued_p \
21092 && NILP ((GLYPH)->object) \
21093 && (GLYPH)->type == CHAR_GLYPH \
21094 && (GLYPH)->u.ch == ' ' \
21095 && (GLYPH)->charpos >= 0 \
21096 && !(GLYPH)->avoid_cursor_p)
21097
21098 CHECK_NUMBER (direction);
21099 dir = XINT (direction);
21100 if (dir > 0)
21101 dir = 1;
21102 else
21103 dir = -1;
21104
21105 /* If current matrix is up-to-date, we can use the information
21106 recorded in the glyphs, at least as long as the goal is on the
21107 screen. */
21108 if (w->window_end_valid
21109 && !windows_or_buffers_changed
21110 && b
21111 && !b->clip_changed
21112 && !b->prevent_redisplay_optimizations_p
21113 && !window_outdated (w)
21114 /* We rely below on the cursor coordinates to be up to date, but
21115 we cannot trust them if some command moved point since the
21116 last complete redisplay. */
21117 && w->last_point == BUF_PT (b)
21118 && w->cursor.vpos >= 0
21119 && w->cursor.vpos < w->current_matrix->nrows
21120 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21121 {
21122 struct glyph *g = row->glyphs[TEXT_AREA];
21123 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21124 struct glyph *gpt = g + w->cursor.hpos;
21125
21126 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21127 {
21128 if (BUFFERP (g->object) && g->charpos != PT)
21129 {
21130 SET_PT (g->charpos);
21131 w->cursor.vpos = -1;
21132 return make_number (PT);
21133 }
21134 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21135 {
21136 ptrdiff_t new_pos;
21137
21138 if (BUFFERP (gpt->object))
21139 {
21140 new_pos = PT;
21141 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21142 new_pos += (row->reversed_p ? -dir : dir);
21143 else
21144 new_pos -= (row->reversed_p ? -dir : dir);
21145 }
21146 else if (BUFFERP (g->object))
21147 new_pos = g->charpos;
21148 else
21149 break;
21150 SET_PT (new_pos);
21151 w->cursor.vpos = -1;
21152 return make_number (PT);
21153 }
21154 else if (ROW_GLYPH_NEWLINE_P (row, g))
21155 {
21156 /* Glyphs inserted at the end of a non-empty line for
21157 positioning the cursor have zero charpos, so we must
21158 deduce the value of point by other means. */
21159 if (g->charpos > 0)
21160 SET_PT (g->charpos);
21161 else if (row->ends_at_zv_p && PT != ZV)
21162 SET_PT (ZV);
21163 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21164 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21165 else
21166 break;
21167 w->cursor.vpos = -1;
21168 return make_number (PT);
21169 }
21170 }
21171 if (g == e || NILP (g->object))
21172 {
21173 if (row->truncated_on_left_p || row->truncated_on_right_p)
21174 goto simulate_display;
21175 if (!row->reversed_p)
21176 row += dir;
21177 else
21178 row -= dir;
21179 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21180 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21181 goto simulate_display;
21182
21183 if (dir > 0)
21184 {
21185 if (row->reversed_p && !row->continued_p)
21186 {
21187 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21188 w->cursor.vpos = -1;
21189 return make_number (PT);
21190 }
21191 g = row->glyphs[TEXT_AREA];
21192 e = g + row->used[TEXT_AREA];
21193 for ( ; g < e; g++)
21194 {
21195 if (BUFFERP (g->object)
21196 /* Empty lines have only one glyph, which stands
21197 for the newline, and whose charpos is the
21198 buffer position of the newline. */
21199 || ROW_GLYPH_NEWLINE_P (row, g)
21200 /* When the buffer ends in a newline, the line at
21201 EOB also has one glyph, but its charpos is -1. */
21202 || (row->ends_at_zv_p
21203 && !row->reversed_p
21204 && NILP (g->object)
21205 && g->type == CHAR_GLYPH
21206 && g->u.ch == ' '))
21207 {
21208 if (g->charpos > 0)
21209 SET_PT (g->charpos);
21210 else if (!row->reversed_p
21211 && row->ends_at_zv_p
21212 && PT != ZV)
21213 SET_PT (ZV);
21214 else
21215 continue;
21216 w->cursor.vpos = -1;
21217 return make_number (PT);
21218 }
21219 }
21220 }
21221 else
21222 {
21223 if (!row->reversed_p && !row->continued_p)
21224 {
21225 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21226 w->cursor.vpos = -1;
21227 return make_number (PT);
21228 }
21229 e = row->glyphs[TEXT_AREA];
21230 g = e + row->used[TEXT_AREA] - 1;
21231 for ( ; g >= e; g--)
21232 {
21233 if (BUFFERP (g->object)
21234 || (ROW_GLYPH_NEWLINE_P (row, g)
21235 && g->charpos > 0)
21236 /* Empty R2L lines on GUI frames have the buffer
21237 position of the newline stored in the stretch
21238 glyph. */
21239 || g->type == STRETCH_GLYPH
21240 || (row->ends_at_zv_p
21241 && row->reversed_p
21242 && NILP (g->object)
21243 && g->type == CHAR_GLYPH
21244 && g->u.ch == ' '))
21245 {
21246 if (g->charpos > 0)
21247 SET_PT (g->charpos);
21248 else if (row->reversed_p
21249 && row->ends_at_zv_p
21250 && PT != ZV)
21251 SET_PT (ZV);
21252 else
21253 continue;
21254 w->cursor.vpos = -1;
21255 return make_number (PT);
21256 }
21257 }
21258 }
21259 }
21260 }
21261
21262 simulate_display:
21263
21264 /* If we wind up here, we failed to move by using the glyphs, so we
21265 need to simulate display instead. */
21266
21267 if (b)
21268 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21269 else
21270 paragraph_dir = Qleft_to_right;
21271 if (EQ (paragraph_dir, Qright_to_left))
21272 dir = -dir;
21273 if (PT <= BEGV && dir < 0)
21274 xsignal0 (Qbeginning_of_buffer);
21275 else if (PT >= ZV && dir > 0)
21276 xsignal0 (Qend_of_buffer);
21277 else
21278 {
21279 struct text_pos pt;
21280 struct it it;
21281 int pt_x, target_x, pixel_width, pt_vpos;
21282 bool at_eol_p;
21283 bool overshoot_expected = false;
21284 bool target_is_eol_p = false;
21285
21286 /* Setup the arena. */
21287 SET_TEXT_POS (pt, PT, PT_BYTE);
21288 start_display (&it, w, pt);
21289
21290 if (it.cmp_it.id < 0
21291 && it.method == GET_FROM_STRING
21292 && it.area == TEXT_AREA
21293 && it.string_from_display_prop_p
21294 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21295 overshoot_expected = true;
21296
21297 /* Find the X coordinate of point. We start from the beginning
21298 of this or previous line to make sure we are before point in
21299 the logical order (since the move_it_* functions can only
21300 move forward). */
21301 reseat:
21302 reseat_at_previous_visible_line_start (&it);
21303 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21304 if (IT_CHARPOS (it) != PT)
21305 {
21306 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21307 -1, -1, -1, MOVE_TO_POS);
21308 /* If we missed point because the character there is
21309 displayed out of a display vector that has more than one
21310 glyph, retry expecting overshoot. */
21311 if (it.method == GET_FROM_DISPLAY_VECTOR
21312 && it.current.dpvec_index > 0
21313 && !overshoot_expected)
21314 {
21315 overshoot_expected = true;
21316 goto reseat;
21317 }
21318 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21319 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21320 }
21321 pt_x = it.current_x;
21322 pt_vpos = it.vpos;
21323 if (dir > 0 || overshoot_expected)
21324 {
21325 struct glyph_row *row = it.glyph_row;
21326
21327 /* When point is at beginning of line, we don't have
21328 information about the glyph there loaded into struct
21329 it. Calling get_next_display_element fixes that. */
21330 if (pt_x == 0)
21331 get_next_display_element (&it);
21332 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21333 it.glyph_row = NULL;
21334 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21335 it.glyph_row = row;
21336 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21337 it, lest it will become out of sync with it's buffer
21338 position. */
21339 it.current_x = pt_x;
21340 }
21341 else
21342 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21343 pixel_width = it.pixel_width;
21344 if (overshoot_expected && at_eol_p)
21345 pixel_width = 0;
21346 else if (pixel_width <= 0)
21347 pixel_width = 1;
21348
21349 /* If there's a display string (or something similar) at point,
21350 we are actually at the glyph to the left of point, so we need
21351 to correct the X coordinate. */
21352 if (overshoot_expected)
21353 {
21354 if (it.bidi_p)
21355 pt_x += pixel_width * it.bidi_it.scan_dir;
21356 else
21357 pt_x += pixel_width;
21358 }
21359
21360 /* Compute target X coordinate, either to the left or to the
21361 right of point. On TTY frames, all characters have the same
21362 pixel width of 1, so we can use that. On GUI frames we don't
21363 have an easy way of getting at the pixel width of the
21364 character to the left of point, so we use a different method
21365 of getting to that place. */
21366 if (dir > 0)
21367 target_x = pt_x + pixel_width;
21368 else
21369 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21370
21371 /* Target X coordinate could be one line above or below the line
21372 of point, in which case we need to adjust the target X
21373 coordinate. Also, if moving to the left, we need to begin at
21374 the left edge of the point's screen line. */
21375 if (dir < 0)
21376 {
21377 if (pt_x > 0)
21378 {
21379 start_display (&it, w, pt);
21380 reseat_at_previous_visible_line_start (&it);
21381 it.current_x = it.current_y = it.hpos = 0;
21382 if (pt_vpos != 0)
21383 move_it_by_lines (&it, pt_vpos);
21384 }
21385 else
21386 {
21387 move_it_by_lines (&it, -1);
21388 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21389 target_is_eol_p = true;
21390 /* Under word-wrap, we don't know the x coordinate of
21391 the last character displayed on the previous line,
21392 which immediately precedes the wrap point. To find
21393 out its x coordinate, we try moving to the right
21394 margin of the window, which will stop at the wrap
21395 point, and then reset target_x to point at the
21396 character that precedes the wrap point. This is not
21397 needed on GUI frames, because (see below) there we
21398 move from the left margin one grapheme cluster at a
21399 time, and stop when we hit the wrap point. */
21400 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21401 {
21402 void *it_data = NULL;
21403 struct it it2;
21404
21405 SAVE_IT (it2, it, it_data);
21406 move_it_in_display_line_to (&it, ZV, target_x,
21407 MOVE_TO_POS | MOVE_TO_X);
21408 /* If we arrived at target_x, that _is_ the last
21409 character on the previous line. */
21410 if (it.current_x != target_x)
21411 target_x = it.current_x - 1;
21412 RESTORE_IT (&it, &it2, it_data);
21413 }
21414 }
21415 }
21416 else
21417 {
21418 if (at_eol_p
21419 || (target_x >= it.last_visible_x
21420 && it.line_wrap != TRUNCATE))
21421 {
21422 if (pt_x > 0)
21423 move_it_by_lines (&it, 0);
21424 move_it_by_lines (&it, 1);
21425 target_x = 0;
21426 }
21427 }
21428
21429 /* Move to the target X coordinate. */
21430 #ifdef HAVE_WINDOW_SYSTEM
21431 /* On GUI frames, as we don't know the X coordinate of the
21432 character to the left of point, moving point to the left
21433 requires walking, one grapheme cluster at a time, until we
21434 find ourself at a place immediately to the left of the
21435 character at point. */
21436 if (FRAME_WINDOW_P (it.f) && dir < 0)
21437 {
21438 struct text_pos new_pos;
21439 enum move_it_result rc = MOVE_X_REACHED;
21440
21441 if (it.current_x == 0)
21442 get_next_display_element (&it);
21443 if (it.what == IT_COMPOSITION)
21444 {
21445 new_pos.charpos = it.cmp_it.charpos;
21446 new_pos.bytepos = -1;
21447 }
21448 else
21449 new_pos = it.current.pos;
21450
21451 while (it.current_x + it.pixel_width <= target_x
21452 && (rc == MOVE_X_REACHED
21453 /* Under word-wrap, move_it_in_display_line_to
21454 stops at correct coordinates, but sometimes
21455 returns MOVE_POS_MATCH_OR_ZV. */
21456 || (it.line_wrap == WORD_WRAP
21457 && rc == MOVE_POS_MATCH_OR_ZV)))
21458 {
21459 int new_x = it.current_x + it.pixel_width;
21460
21461 /* For composed characters, we want the position of the
21462 first character in the grapheme cluster (usually, the
21463 composition's base character), whereas it.current
21464 might give us the position of the _last_ one, e.g. if
21465 the composition is rendered in reverse due to bidi
21466 reordering. */
21467 if (it.what == IT_COMPOSITION)
21468 {
21469 new_pos.charpos = it.cmp_it.charpos;
21470 new_pos.bytepos = -1;
21471 }
21472 else
21473 new_pos = it.current.pos;
21474 if (new_x == it.current_x)
21475 new_x++;
21476 rc = move_it_in_display_line_to (&it, ZV, new_x,
21477 MOVE_TO_POS | MOVE_TO_X);
21478 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21479 break;
21480 }
21481 /* The previous position we saw in the loop is the one we
21482 want. */
21483 if (new_pos.bytepos == -1)
21484 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21485 it.current.pos = new_pos;
21486 }
21487 else
21488 #endif
21489 if (it.current_x != target_x)
21490 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21491
21492 /* When lines are truncated, the above loop will stop at the
21493 window edge. But we want to get to the end of line, even if
21494 it is beyond the window edge; automatic hscroll will then
21495 scroll the window to show point as appropriate. */
21496 if (target_is_eol_p && it.line_wrap == TRUNCATE
21497 && get_next_display_element (&it))
21498 {
21499 struct text_pos new_pos = it.current.pos;
21500
21501 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21502 {
21503 set_iterator_to_next (&it, false);
21504 if (it.method == GET_FROM_BUFFER)
21505 new_pos = it.current.pos;
21506 if (!get_next_display_element (&it))
21507 break;
21508 }
21509
21510 it.current.pos = new_pos;
21511 }
21512
21513 /* If we ended up in a display string that covers point, move to
21514 buffer position to the right in the visual order. */
21515 if (dir > 0)
21516 {
21517 while (IT_CHARPOS (it) == PT)
21518 {
21519 set_iterator_to_next (&it, false);
21520 if (!get_next_display_element (&it))
21521 break;
21522 }
21523 }
21524
21525 /* Move point to that position. */
21526 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21527 }
21528
21529 return make_number (PT);
21530
21531 #undef ROW_GLYPH_NEWLINE_P
21532 }
21533
21534 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21535 Sbidi_resolved_levels, 0, 1, 0,
21536 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21537
21538 The resolved levels are produced by the Emacs bidi reordering engine
21539 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21540 read the Unicode Standard Annex 9 (UAX#9) for background information
21541 about these levels.
21542
21543 VPOS is the zero-based number of the current window's screen line
21544 for which to produce the resolved levels. If VPOS is nil or omitted,
21545 it defaults to the screen line of point. If the window displays a
21546 header line, VPOS of zero will report on the header line, and first
21547 line of text in the window will have VPOS of 1.
21548
21549 Value is an array of resolved levels, indexed by glyph number.
21550 Glyphs are numbered from zero starting from the beginning of the
21551 screen line, i.e. the left edge of the window for left-to-right lines
21552 and from the right edge for right-to-left lines. The resolved levels
21553 are produced only for the window's text area; text in display margins
21554 is not included.
21555
21556 If the selected window's display is not up-to-date, or if the specified
21557 screen line does not display text, this function returns nil. It is
21558 highly recommended to bind this function to some simple key, like F8,
21559 in order to avoid these problems.
21560
21561 This function exists mainly for testing the correctness of the
21562 Emacs UBA implementation, in particular with the test suite. */)
21563 (Lisp_Object vpos)
21564 {
21565 struct window *w = XWINDOW (selected_window);
21566 struct buffer *b = XBUFFER (w->contents);
21567 int nrow;
21568 struct glyph_row *row;
21569
21570 if (NILP (vpos))
21571 {
21572 int d1, d2, d3, d4, d5;
21573
21574 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21575 }
21576 else
21577 {
21578 CHECK_NUMBER_COERCE_MARKER (vpos);
21579 nrow = XINT (vpos);
21580 }
21581
21582 /* We require up-to-date glyph matrix for this window. */
21583 if (w->window_end_valid
21584 && !windows_or_buffers_changed
21585 && b
21586 && !b->clip_changed
21587 && !b->prevent_redisplay_optimizations_p
21588 && !window_outdated (w)
21589 && nrow >= 0
21590 && nrow < w->current_matrix->nrows
21591 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21592 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21593 {
21594 struct glyph *g, *e, *g1;
21595 int nglyphs, i;
21596 Lisp_Object levels;
21597
21598 if (!row->reversed_p) /* Left-to-right glyph row. */
21599 {
21600 g = g1 = row->glyphs[TEXT_AREA];
21601 e = g + row->used[TEXT_AREA];
21602
21603 /* Skip over glyphs at the start of the row that was
21604 generated by redisplay for its own needs. */
21605 while (g < e
21606 && NILP (g->object)
21607 && g->charpos < 0)
21608 g++;
21609 g1 = g;
21610
21611 /* Count the "interesting" glyphs in this row. */
21612 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21613 nglyphs++;
21614
21615 /* Create and fill the array. */
21616 levels = make_uninit_vector (nglyphs);
21617 for (i = 0; g1 < g; i++, g1++)
21618 ASET (levels, i, make_number (g1->resolved_level));
21619 }
21620 else /* Right-to-left glyph row. */
21621 {
21622 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21623 e = row->glyphs[TEXT_AREA] - 1;
21624 while (g > e
21625 && NILP (g->object)
21626 && g->charpos < 0)
21627 g--;
21628 g1 = g;
21629 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21630 nglyphs++;
21631 levels = make_uninit_vector (nglyphs);
21632 for (i = 0; g1 > g; i++, g1--)
21633 ASET (levels, i, make_number (g1->resolved_level));
21634 }
21635 return levels;
21636 }
21637 else
21638 return Qnil;
21639 }
21640
21641
21642 \f
21643 /***********************************************************************
21644 Menu Bar
21645 ***********************************************************************/
21646
21647 /* Redisplay the menu bar in the frame for window W.
21648
21649 The menu bar of X frames that don't have X toolkit support is
21650 displayed in a special window W->frame->menu_bar_window.
21651
21652 The menu bar of terminal frames is treated specially as far as
21653 glyph matrices are concerned. Menu bar lines are not part of
21654 windows, so the update is done directly on the frame matrix rows
21655 for the menu bar. */
21656
21657 static void
21658 display_menu_bar (struct window *w)
21659 {
21660 struct frame *f = XFRAME (WINDOW_FRAME (w));
21661 struct it it;
21662 Lisp_Object items;
21663 int i;
21664
21665 /* Don't do all this for graphical frames. */
21666 #ifdef HAVE_NTGUI
21667 if (FRAME_W32_P (f))
21668 return;
21669 #endif
21670 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21671 if (FRAME_X_P (f))
21672 return;
21673 #endif
21674
21675 #ifdef HAVE_NS
21676 if (FRAME_NS_P (f))
21677 return;
21678 #endif /* HAVE_NS */
21679
21680 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21681 eassert (!FRAME_WINDOW_P (f));
21682 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21683 it.first_visible_x = 0;
21684 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21685 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21686 if (FRAME_WINDOW_P (f))
21687 {
21688 /* Menu bar lines are displayed in the desired matrix of the
21689 dummy window menu_bar_window. */
21690 struct window *menu_w;
21691 menu_w = XWINDOW (f->menu_bar_window);
21692 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21693 MENU_FACE_ID);
21694 it.first_visible_x = 0;
21695 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21696 }
21697 else
21698 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21699 {
21700 /* This is a TTY frame, i.e. character hpos/vpos are used as
21701 pixel x/y. */
21702 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21703 MENU_FACE_ID);
21704 it.first_visible_x = 0;
21705 it.last_visible_x = FRAME_COLS (f);
21706 }
21707
21708 /* FIXME: This should be controlled by a user option. See the
21709 comments in redisplay_tool_bar and display_mode_line about
21710 this. */
21711 it.paragraph_embedding = L2R;
21712
21713 /* Clear all rows of the menu bar. */
21714 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21715 {
21716 struct glyph_row *row = it.glyph_row + i;
21717 clear_glyph_row (row);
21718 row->enabled_p = true;
21719 row->full_width_p = true;
21720 row->reversed_p = false;
21721 }
21722
21723 /* Display all items of the menu bar. */
21724 items = FRAME_MENU_BAR_ITEMS (it.f);
21725 for (i = 0; i < ASIZE (items); i += 4)
21726 {
21727 Lisp_Object string;
21728
21729 /* Stop at nil string. */
21730 string = AREF (items, i + 1);
21731 if (NILP (string))
21732 break;
21733
21734 /* Remember where item was displayed. */
21735 ASET (items, i + 3, make_number (it.hpos));
21736
21737 /* Display the item, pad with one space. */
21738 if (it.current_x < it.last_visible_x)
21739 display_string (NULL, string, Qnil, 0, 0, &it,
21740 SCHARS (string) + 1, 0, 0, -1);
21741 }
21742
21743 /* Fill out the line with spaces. */
21744 if (it.current_x < it.last_visible_x)
21745 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21746
21747 /* Compute the total height of the lines. */
21748 compute_line_metrics (&it);
21749 }
21750
21751 /* Deep copy of a glyph row, including the glyphs. */
21752 static void
21753 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21754 {
21755 struct glyph *pointers[1 + LAST_AREA];
21756 int to_used = to->used[TEXT_AREA];
21757
21758 /* Save glyph pointers of TO. */
21759 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21760
21761 /* Do a structure assignment. */
21762 *to = *from;
21763
21764 /* Restore original glyph pointers of TO. */
21765 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21766
21767 /* Copy the glyphs. */
21768 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21769 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21770
21771 /* If we filled only part of the TO row, fill the rest with
21772 space_glyph (which will display as empty space). */
21773 if (to_used > from->used[TEXT_AREA])
21774 fill_up_frame_row_with_spaces (to, to_used);
21775 }
21776
21777 /* Display one menu item on a TTY, by overwriting the glyphs in the
21778 frame F's desired glyph matrix with glyphs produced from the menu
21779 item text. Called from term.c to display TTY drop-down menus one
21780 item at a time.
21781
21782 ITEM_TEXT is the menu item text as a C string.
21783
21784 FACE_ID is the face ID to be used for this menu item. FACE_ID
21785 could specify one of 3 faces: a face for an enabled item, a face
21786 for a disabled item, or a face for a selected item.
21787
21788 X and Y are coordinates of the first glyph in the frame's desired
21789 matrix to be overwritten by the menu item. Since this is a TTY, Y
21790 is the zero-based number of the glyph row and X is the zero-based
21791 glyph number in the row, starting from left, where to start
21792 displaying the item.
21793
21794 SUBMENU means this menu item drops down a submenu, which
21795 should be indicated by displaying a proper visual cue after the
21796 item text. */
21797
21798 void
21799 display_tty_menu_item (const char *item_text, int width, int face_id,
21800 int x, int y, bool submenu)
21801 {
21802 struct it it;
21803 struct frame *f = SELECTED_FRAME ();
21804 struct window *w = XWINDOW (f->selected_window);
21805 struct glyph_row *row;
21806 size_t item_len = strlen (item_text);
21807
21808 eassert (FRAME_TERMCAP_P (f));
21809
21810 /* Don't write beyond the matrix's last row. This can happen for
21811 TTY screens that are not high enough to show the entire menu.
21812 (This is actually a bit of defensive programming, as
21813 tty_menu_display already limits the number of menu items to one
21814 less than the number of screen lines.) */
21815 if (y >= f->desired_matrix->nrows)
21816 return;
21817
21818 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21819 it.first_visible_x = 0;
21820 it.last_visible_x = FRAME_COLS (f) - 1;
21821 row = it.glyph_row;
21822 /* Start with the row contents from the current matrix. */
21823 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21824 bool saved_width = row->full_width_p;
21825 row->full_width_p = true;
21826 bool saved_reversed = row->reversed_p;
21827 row->reversed_p = false;
21828 row->enabled_p = true;
21829
21830 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21831 desired face. */
21832 eassert (x < f->desired_matrix->matrix_w);
21833 it.current_x = it.hpos = x;
21834 it.current_y = it.vpos = y;
21835 int saved_used = row->used[TEXT_AREA];
21836 bool saved_truncated = row->truncated_on_right_p;
21837 row->used[TEXT_AREA] = x;
21838 it.face_id = face_id;
21839 it.line_wrap = TRUNCATE;
21840
21841 /* FIXME: This should be controlled by a user option. See the
21842 comments in redisplay_tool_bar and display_mode_line about this.
21843 Also, if paragraph_embedding could ever be R2L, changes will be
21844 needed to avoid shifting to the right the row characters in
21845 term.c:append_glyph. */
21846 it.paragraph_embedding = L2R;
21847
21848 /* Pad with a space on the left. */
21849 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21850 width--;
21851 /* Display the menu item, pad with spaces to WIDTH. */
21852 if (submenu)
21853 {
21854 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21855 item_len, 0, FRAME_COLS (f) - 1, -1);
21856 width -= item_len;
21857 /* Indicate with " >" that there's a submenu. */
21858 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21859 FRAME_COLS (f) - 1, -1);
21860 }
21861 else
21862 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21863 width, 0, FRAME_COLS (f) - 1, -1);
21864
21865 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21866 row->truncated_on_right_p = saved_truncated;
21867 row->hash = row_hash (row);
21868 row->full_width_p = saved_width;
21869 row->reversed_p = saved_reversed;
21870 }
21871 \f
21872 /***********************************************************************
21873 Mode Line
21874 ***********************************************************************/
21875
21876 /* Redisplay mode lines in the window tree whose root is WINDOW.
21877 If FORCE, redisplay mode lines unconditionally.
21878 Otherwise, redisplay only mode lines that are garbaged. Value is
21879 the number of windows whose mode lines were redisplayed. */
21880
21881 static int
21882 redisplay_mode_lines (Lisp_Object window, bool force)
21883 {
21884 int nwindows = 0;
21885
21886 while (!NILP (window))
21887 {
21888 struct window *w = XWINDOW (window);
21889
21890 if (WINDOWP (w->contents))
21891 nwindows += redisplay_mode_lines (w->contents, force);
21892 else if (force
21893 || FRAME_GARBAGED_P (XFRAME (w->frame))
21894 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21895 {
21896 struct text_pos lpoint;
21897 struct buffer *old = current_buffer;
21898
21899 /* Set the window's buffer for the mode line display. */
21900 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21901 set_buffer_internal_1 (XBUFFER (w->contents));
21902
21903 /* Point refers normally to the selected window. For any
21904 other window, set up appropriate value. */
21905 if (!EQ (window, selected_window))
21906 {
21907 struct text_pos pt;
21908
21909 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21910 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21911 }
21912
21913 /* Display mode lines. */
21914 clear_glyph_matrix (w->desired_matrix);
21915 if (display_mode_lines (w))
21916 ++nwindows;
21917
21918 /* Restore old settings. */
21919 set_buffer_internal_1 (old);
21920 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21921 }
21922
21923 window = w->next;
21924 }
21925
21926 return nwindows;
21927 }
21928
21929
21930 /* Display the mode and/or header line of window W. Value is the
21931 sum number of mode lines and header lines displayed. */
21932
21933 static int
21934 display_mode_lines (struct window *w)
21935 {
21936 Lisp_Object old_selected_window = selected_window;
21937 Lisp_Object old_selected_frame = selected_frame;
21938 Lisp_Object new_frame = w->frame;
21939 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21940 int n = 0;
21941
21942 selected_frame = new_frame;
21943 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21944 or window's point, then we'd need select_window_1 here as well. */
21945 XSETWINDOW (selected_window, w);
21946 XFRAME (new_frame)->selected_window = selected_window;
21947
21948 /* These will be set while the mode line specs are processed. */
21949 line_number_displayed = false;
21950 w->column_number_displayed = -1;
21951
21952 if (WINDOW_WANTS_MODELINE_P (w))
21953 {
21954 struct window *sel_w = XWINDOW (old_selected_window);
21955
21956 /* Select mode line face based on the real selected window. */
21957 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21958 BVAR (current_buffer, mode_line_format));
21959 ++n;
21960 }
21961
21962 if (WINDOW_WANTS_HEADER_LINE_P (w))
21963 {
21964 display_mode_line (w, HEADER_LINE_FACE_ID,
21965 BVAR (current_buffer, header_line_format));
21966 ++n;
21967 }
21968
21969 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21970 selected_frame = old_selected_frame;
21971 selected_window = old_selected_window;
21972 if (n > 0)
21973 w->must_be_updated_p = true;
21974 return n;
21975 }
21976
21977
21978 /* Display mode or header line of window W. FACE_ID specifies which
21979 line to display; it is either MODE_LINE_FACE_ID or
21980 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21981 display. Value is the pixel height of the mode/header line
21982 displayed. */
21983
21984 static int
21985 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21986 {
21987 struct it it;
21988 struct face *face;
21989 ptrdiff_t count = SPECPDL_INDEX ();
21990
21991 init_iterator (&it, w, -1, -1, NULL, face_id);
21992 /* Don't extend on a previously drawn mode-line.
21993 This may happen if called from pos_visible_p. */
21994 it.glyph_row->enabled_p = false;
21995 prepare_desired_row (w, it.glyph_row, true);
21996
21997 it.glyph_row->mode_line_p = true;
21998
21999 /* FIXME: This should be controlled by a user option. But
22000 supporting such an option is not trivial, since the mode line is
22001 made up of many separate strings. */
22002 it.paragraph_embedding = L2R;
22003
22004 record_unwind_protect (unwind_format_mode_line,
22005 format_mode_line_unwind_data (NULL, NULL,
22006 Qnil, false));
22007
22008 mode_line_target = MODE_LINE_DISPLAY;
22009
22010 /* Temporarily make frame's keyboard the current kboard so that
22011 kboard-local variables in the mode_line_format will get the right
22012 values. */
22013 push_kboard (FRAME_KBOARD (it.f));
22014 record_unwind_save_match_data ();
22015 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22016 pop_kboard ();
22017
22018 unbind_to (count, Qnil);
22019
22020 /* Fill up with spaces. */
22021 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22022
22023 compute_line_metrics (&it);
22024 it.glyph_row->full_width_p = true;
22025 it.glyph_row->continued_p = false;
22026 it.glyph_row->truncated_on_left_p = false;
22027 it.glyph_row->truncated_on_right_p = false;
22028
22029 /* Make a 3D mode-line have a shadow at its right end. */
22030 face = FACE_FROM_ID (it.f, face_id);
22031 extend_face_to_end_of_line (&it);
22032 if (face->box != FACE_NO_BOX)
22033 {
22034 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22035 + it.glyph_row->used[TEXT_AREA] - 1);
22036 last->right_box_line_p = true;
22037 }
22038
22039 return it.glyph_row->height;
22040 }
22041
22042 /* Move element ELT in LIST to the front of LIST.
22043 Return the updated list. */
22044
22045 static Lisp_Object
22046 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22047 {
22048 register Lisp_Object tail, prev;
22049 register Lisp_Object tem;
22050
22051 tail = list;
22052 prev = Qnil;
22053 while (CONSP (tail))
22054 {
22055 tem = XCAR (tail);
22056
22057 if (EQ (elt, tem))
22058 {
22059 /* Splice out the link TAIL. */
22060 if (NILP (prev))
22061 list = XCDR (tail);
22062 else
22063 Fsetcdr (prev, XCDR (tail));
22064
22065 /* Now make it the first. */
22066 Fsetcdr (tail, list);
22067 return tail;
22068 }
22069 else
22070 prev = tail;
22071 tail = XCDR (tail);
22072 QUIT;
22073 }
22074
22075 /* Not found--return unchanged LIST. */
22076 return list;
22077 }
22078
22079 /* Contribute ELT to the mode line for window IT->w. How it
22080 translates into text depends on its data type.
22081
22082 IT describes the display environment in which we display, as usual.
22083
22084 DEPTH is the depth in recursion. It is used to prevent
22085 infinite recursion here.
22086
22087 FIELD_WIDTH is the number of characters the display of ELT should
22088 occupy in the mode line, and PRECISION is the maximum number of
22089 characters to display from ELT's representation. See
22090 display_string for details.
22091
22092 Returns the hpos of the end of the text generated by ELT.
22093
22094 PROPS is a property list to add to any string we encounter.
22095
22096 If RISKY, remove (disregard) any properties in any string
22097 we encounter, and ignore :eval and :propertize.
22098
22099 The global variable `mode_line_target' determines whether the
22100 output is passed to `store_mode_line_noprop',
22101 `store_mode_line_string', or `display_string'. */
22102
22103 static int
22104 display_mode_element (struct it *it, int depth, int field_width, int precision,
22105 Lisp_Object elt, Lisp_Object props, bool risky)
22106 {
22107 int n = 0, field, prec;
22108 bool literal = false;
22109
22110 tail_recurse:
22111 if (depth > 100)
22112 elt = build_string ("*too-deep*");
22113
22114 depth++;
22115
22116 switch (XTYPE (elt))
22117 {
22118 case Lisp_String:
22119 {
22120 /* A string: output it and check for %-constructs within it. */
22121 unsigned char c;
22122 ptrdiff_t offset = 0;
22123
22124 if (SCHARS (elt) > 0
22125 && (!NILP (props) || risky))
22126 {
22127 Lisp_Object oprops, aelt;
22128 oprops = Ftext_properties_at (make_number (0), elt);
22129
22130 /* If the starting string's properties are not what
22131 we want, translate the string. Also, if the string
22132 is risky, do that anyway. */
22133
22134 if (NILP (Fequal (props, oprops)) || risky)
22135 {
22136 /* If the starting string has properties,
22137 merge the specified ones onto the existing ones. */
22138 if (! NILP (oprops) && !risky)
22139 {
22140 Lisp_Object tem;
22141
22142 oprops = Fcopy_sequence (oprops);
22143 tem = props;
22144 while (CONSP (tem))
22145 {
22146 oprops = Fplist_put (oprops, XCAR (tem),
22147 XCAR (XCDR (tem)));
22148 tem = XCDR (XCDR (tem));
22149 }
22150 props = oprops;
22151 }
22152
22153 aelt = Fassoc (elt, mode_line_proptrans_alist);
22154 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22155 {
22156 /* AELT is what we want. Move it to the front
22157 without consing. */
22158 elt = XCAR (aelt);
22159 mode_line_proptrans_alist
22160 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22161 }
22162 else
22163 {
22164 Lisp_Object tem;
22165
22166 /* If AELT has the wrong props, it is useless.
22167 so get rid of it. */
22168 if (! NILP (aelt))
22169 mode_line_proptrans_alist
22170 = Fdelq (aelt, mode_line_proptrans_alist);
22171
22172 elt = Fcopy_sequence (elt);
22173 Fset_text_properties (make_number (0), Flength (elt),
22174 props, elt);
22175 /* Add this item to mode_line_proptrans_alist. */
22176 mode_line_proptrans_alist
22177 = Fcons (Fcons (elt, props),
22178 mode_line_proptrans_alist);
22179 /* Truncate mode_line_proptrans_alist
22180 to at most 50 elements. */
22181 tem = Fnthcdr (make_number (50),
22182 mode_line_proptrans_alist);
22183 if (! NILP (tem))
22184 XSETCDR (tem, Qnil);
22185 }
22186 }
22187 }
22188
22189 offset = 0;
22190
22191 if (literal)
22192 {
22193 prec = precision - n;
22194 switch (mode_line_target)
22195 {
22196 case MODE_LINE_NOPROP:
22197 case MODE_LINE_TITLE:
22198 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22199 break;
22200 case MODE_LINE_STRING:
22201 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22202 break;
22203 case MODE_LINE_DISPLAY:
22204 n += display_string (NULL, elt, Qnil, 0, 0, it,
22205 0, prec, 0, STRING_MULTIBYTE (elt));
22206 break;
22207 }
22208
22209 break;
22210 }
22211
22212 /* Handle the non-literal case. */
22213
22214 while ((precision <= 0 || n < precision)
22215 && SREF (elt, offset) != 0
22216 && (mode_line_target != MODE_LINE_DISPLAY
22217 || it->current_x < it->last_visible_x))
22218 {
22219 ptrdiff_t last_offset = offset;
22220
22221 /* Advance to end of string or next format specifier. */
22222 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22223 ;
22224
22225 if (offset - 1 != last_offset)
22226 {
22227 ptrdiff_t nchars, nbytes;
22228
22229 /* Output to end of string or up to '%'. Field width
22230 is length of string. Don't output more than
22231 PRECISION allows us. */
22232 offset--;
22233
22234 prec = c_string_width (SDATA (elt) + last_offset,
22235 offset - last_offset, precision - n,
22236 &nchars, &nbytes);
22237
22238 switch (mode_line_target)
22239 {
22240 case MODE_LINE_NOPROP:
22241 case MODE_LINE_TITLE:
22242 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22243 break;
22244 case MODE_LINE_STRING:
22245 {
22246 ptrdiff_t bytepos = last_offset;
22247 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22248 ptrdiff_t endpos = (precision <= 0
22249 ? string_byte_to_char (elt, offset)
22250 : charpos + nchars);
22251 Lisp_Object mode_string
22252 = Fsubstring (elt, make_number (charpos),
22253 make_number (endpos));
22254 n += store_mode_line_string (NULL, mode_string, false,
22255 0, 0, Qnil);
22256 }
22257 break;
22258 case MODE_LINE_DISPLAY:
22259 {
22260 ptrdiff_t bytepos = last_offset;
22261 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22262
22263 if (precision <= 0)
22264 nchars = string_byte_to_char (elt, offset) - charpos;
22265 n += display_string (NULL, elt, Qnil, 0, charpos,
22266 it, 0, nchars, 0,
22267 STRING_MULTIBYTE (elt));
22268 }
22269 break;
22270 }
22271 }
22272 else /* c == '%' */
22273 {
22274 ptrdiff_t percent_position = offset;
22275
22276 /* Get the specified minimum width. Zero means
22277 don't pad. */
22278 field = 0;
22279 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22280 field = field * 10 + c - '0';
22281
22282 /* Don't pad beyond the total padding allowed. */
22283 if (field_width - n > 0 && field > field_width - n)
22284 field = field_width - n;
22285
22286 /* Note that either PRECISION <= 0 or N < PRECISION. */
22287 prec = precision - n;
22288
22289 if (c == 'M')
22290 n += display_mode_element (it, depth, field, prec,
22291 Vglobal_mode_string, props,
22292 risky);
22293 else if (c != 0)
22294 {
22295 bool multibyte;
22296 ptrdiff_t bytepos, charpos;
22297 const char *spec;
22298 Lisp_Object string;
22299
22300 bytepos = percent_position;
22301 charpos = (STRING_MULTIBYTE (elt)
22302 ? string_byte_to_char (elt, bytepos)
22303 : bytepos);
22304 spec = decode_mode_spec (it->w, c, field, &string);
22305 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22306
22307 switch (mode_line_target)
22308 {
22309 case MODE_LINE_NOPROP:
22310 case MODE_LINE_TITLE:
22311 n += store_mode_line_noprop (spec, field, prec);
22312 break;
22313 case MODE_LINE_STRING:
22314 {
22315 Lisp_Object tem = build_string (spec);
22316 props = Ftext_properties_at (make_number (charpos), elt);
22317 /* Should only keep face property in props */
22318 n += store_mode_line_string (NULL, tem, false,
22319 field, prec, props);
22320 }
22321 break;
22322 case MODE_LINE_DISPLAY:
22323 {
22324 int nglyphs_before, nwritten;
22325
22326 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22327 nwritten = display_string (spec, string, elt,
22328 charpos, 0, it,
22329 field, prec, 0,
22330 multibyte);
22331
22332 /* Assign to the glyphs written above the
22333 string where the `%x' came from, position
22334 of the `%'. */
22335 if (nwritten > 0)
22336 {
22337 struct glyph *glyph
22338 = (it->glyph_row->glyphs[TEXT_AREA]
22339 + nglyphs_before);
22340 int i;
22341
22342 for (i = 0; i < nwritten; ++i)
22343 {
22344 glyph[i].object = elt;
22345 glyph[i].charpos = charpos;
22346 }
22347
22348 n += nwritten;
22349 }
22350 }
22351 break;
22352 }
22353 }
22354 else /* c == 0 */
22355 break;
22356 }
22357 }
22358 }
22359 break;
22360
22361 case Lisp_Symbol:
22362 /* A symbol: process the value of the symbol recursively
22363 as if it appeared here directly. Avoid error if symbol void.
22364 Special case: if value of symbol is a string, output the string
22365 literally. */
22366 {
22367 register Lisp_Object tem;
22368
22369 /* If the variable is not marked as risky to set
22370 then its contents are risky to use. */
22371 if (NILP (Fget (elt, Qrisky_local_variable)))
22372 risky = true;
22373
22374 tem = Fboundp (elt);
22375 if (!NILP (tem))
22376 {
22377 tem = Fsymbol_value (elt);
22378 /* If value is a string, output that string literally:
22379 don't check for % within it. */
22380 if (STRINGP (tem))
22381 literal = true;
22382
22383 if (!EQ (tem, elt))
22384 {
22385 /* Give up right away for nil or t. */
22386 elt = tem;
22387 goto tail_recurse;
22388 }
22389 }
22390 }
22391 break;
22392
22393 case Lisp_Cons:
22394 {
22395 register Lisp_Object car, tem;
22396
22397 /* A cons cell: five distinct cases.
22398 If first element is :eval or :propertize, do something special.
22399 If first element is a string or a cons, process all the elements
22400 and effectively concatenate them.
22401 If first element is a negative number, truncate displaying cdr to
22402 at most that many characters. If positive, pad (with spaces)
22403 to at least that many characters.
22404 If first element is a symbol, process the cadr or caddr recursively
22405 according to whether the symbol's value is non-nil or nil. */
22406 car = XCAR (elt);
22407 if (EQ (car, QCeval))
22408 {
22409 /* An element of the form (:eval FORM) means evaluate FORM
22410 and use the result as mode line elements. */
22411
22412 if (risky)
22413 break;
22414
22415 if (CONSP (XCDR (elt)))
22416 {
22417 Lisp_Object spec;
22418 spec = safe__eval (true, XCAR (XCDR (elt)));
22419 n += display_mode_element (it, depth, field_width - n,
22420 precision - n, spec, props,
22421 risky);
22422 }
22423 }
22424 else if (EQ (car, QCpropertize))
22425 {
22426 /* An element of the form (:propertize ELT PROPS...)
22427 means display ELT but applying properties PROPS. */
22428
22429 if (risky)
22430 break;
22431
22432 if (CONSP (XCDR (elt)))
22433 n += display_mode_element (it, depth, field_width - n,
22434 precision - n, XCAR (XCDR (elt)),
22435 XCDR (XCDR (elt)), risky);
22436 }
22437 else if (SYMBOLP (car))
22438 {
22439 tem = Fboundp (car);
22440 elt = XCDR (elt);
22441 if (!CONSP (elt))
22442 goto invalid;
22443 /* elt is now the cdr, and we know it is a cons cell.
22444 Use its car if CAR has a non-nil value. */
22445 if (!NILP (tem))
22446 {
22447 tem = Fsymbol_value (car);
22448 if (!NILP (tem))
22449 {
22450 elt = XCAR (elt);
22451 goto tail_recurse;
22452 }
22453 }
22454 /* Symbol's value is nil (or symbol is unbound)
22455 Get the cddr of the original list
22456 and if possible find the caddr and use that. */
22457 elt = XCDR (elt);
22458 if (NILP (elt))
22459 break;
22460 else if (!CONSP (elt))
22461 goto invalid;
22462 elt = XCAR (elt);
22463 goto tail_recurse;
22464 }
22465 else if (INTEGERP (car))
22466 {
22467 register int lim = XINT (car);
22468 elt = XCDR (elt);
22469 if (lim < 0)
22470 {
22471 /* Negative int means reduce maximum width. */
22472 if (precision <= 0)
22473 precision = -lim;
22474 else
22475 precision = min (precision, -lim);
22476 }
22477 else if (lim > 0)
22478 {
22479 /* Padding specified. Don't let it be more than
22480 current maximum. */
22481 if (precision > 0)
22482 lim = min (precision, lim);
22483
22484 /* If that's more padding than already wanted, queue it.
22485 But don't reduce padding already specified even if
22486 that is beyond the current truncation point. */
22487 field_width = max (lim, field_width);
22488 }
22489 goto tail_recurse;
22490 }
22491 else if (STRINGP (car) || CONSP (car))
22492 {
22493 Lisp_Object halftail = elt;
22494 int len = 0;
22495
22496 while (CONSP (elt)
22497 && (precision <= 0 || n < precision))
22498 {
22499 n += display_mode_element (it, depth,
22500 /* Do padding only after the last
22501 element in the list. */
22502 (! CONSP (XCDR (elt))
22503 ? field_width - n
22504 : 0),
22505 precision - n, XCAR (elt),
22506 props, risky);
22507 elt = XCDR (elt);
22508 len++;
22509 if ((len & 1) == 0)
22510 halftail = XCDR (halftail);
22511 /* Check for cycle. */
22512 if (EQ (halftail, elt))
22513 break;
22514 }
22515 }
22516 }
22517 break;
22518
22519 default:
22520 invalid:
22521 elt = build_string ("*invalid*");
22522 goto tail_recurse;
22523 }
22524
22525 /* Pad to FIELD_WIDTH. */
22526 if (field_width > 0 && n < field_width)
22527 {
22528 switch (mode_line_target)
22529 {
22530 case MODE_LINE_NOPROP:
22531 case MODE_LINE_TITLE:
22532 n += store_mode_line_noprop ("", field_width - n, 0);
22533 break;
22534 case MODE_LINE_STRING:
22535 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22536 Qnil);
22537 break;
22538 case MODE_LINE_DISPLAY:
22539 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22540 0, 0, 0);
22541 break;
22542 }
22543 }
22544
22545 return n;
22546 }
22547
22548 /* Store a mode-line string element in mode_line_string_list.
22549
22550 If STRING is non-null, display that C string. Otherwise, the Lisp
22551 string LISP_STRING is displayed.
22552
22553 FIELD_WIDTH is the minimum number of output glyphs to produce.
22554 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22555 with spaces. FIELD_WIDTH <= 0 means don't pad.
22556
22557 PRECISION is the maximum number of characters to output from
22558 STRING. PRECISION <= 0 means don't truncate the string.
22559
22560 If COPY_STRING, make a copy of LISP_STRING before adding
22561 properties to the string.
22562
22563 PROPS are the properties to add to the string.
22564 The mode_line_string_face face property is always added to the string.
22565 */
22566
22567 static int
22568 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22569 bool copy_string,
22570 int field_width, int precision, Lisp_Object props)
22571 {
22572 ptrdiff_t len;
22573 int n = 0;
22574
22575 if (string != NULL)
22576 {
22577 len = strlen (string);
22578 if (precision > 0 && len > precision)
22579 len = precision;
22580 lisp_string = make_string (string, len);
22581 if (NILP (props))
22582 props = mode_line_string_face_prop;
22583 else if (!NILP (mode_line_string_face))
22584 {
22585 Lisp_Object face = Fplist_get (props, Qface);
22586 props = Fcopy_sequence (props);
22587 if (NILP (face))
22588 face = mode_line_string_face;
22589 else
22590 face = list2 (face, mode_line_string_face);
22591 props = Fplist_put (props, Qface, face);
22592 }
22593 Fadd_text_properties (make_number (0), make_number (len),
22594 props, lisp_string);
22595 }
22596 else
22597 {
22598 len = XFASTINT (Flength (lisp_string));
22599 if (precision > 0 && len > precision)
22600 {
22601 len = precision;
22602 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22603 precision = -1;
22604 }
22605 if (!NILP (mode_line_string_face))
22606 {
22607 Lisp_Object face;
22608 if (NILP (props))
22609 props = Ftext_properties_at (make_number (0), lisp_string);
22610 face = Fplist_get (props, Qface);
22611 if (NILP (face))
22612 face = mode_line_string_face;
22613 else
22614 face = list2 (face, mode_line_string_face);
22615 props = list2 (Qface, face);
22616 if (copy_string)
22617 lisp_string = Fcopy_sequence (lisp_string);
22618 }
22619 if (!NILP (props))
22620 Fadd_text_properties (make_number (0), make_number (len),
22621 props, lisp_string);
22622 }
22623
22624 if (len > 0)
22625 {
22626 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22627 n += len;
22628 }
22629
22630 if (field_width > len)
22631 {
22632 field_width -= len;
22633 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22634 if (!NILP (props))
22635 Fadd_text_properties (make_number (0), make_number (field_width),
22636 props, lisp_string);
22637 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22638 n += field_width;
22639 }
22640
22641 return n;
22642 }
22643
22644
22645 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22646 1, 4, 0,
22647 doc: /* Format a string out of a mode line format specification.
22648 First arg FORMAT specifies the mode line format (see `mode-line-format'
22649 for details) to use.
22650
22651 By default, the format is evaluated for the currently selected window.
22652
22653 Optional second arg FACE specifies the face property to put on all
22654 characters for which no face is specified. The value nil means the
22655 default face. The value t means whatever face the window's mode line
22656 currently uses (either `mode-line' or `mode-line-inactive',
22657 depending on whether the window is the selected window or not).
22658 An integer value means the value string has no text
22659 properties.
22660
22661 Optional third and fourth args WINDOW and BUFFER specify the window
22662 and buffer to use as the context for the formatting (defaults
22663 are the selected window and the WINDOW's buffer). */)
22664 (Lisp_Object format, Lisp_Object face,
22665 Lisp_Object window, Lisp_Object buffer)
22666 {
22667 struct it it;
22668 int len;
22669 struct window *w;
22670 struct buffer *old_buffer = NULL;
22671 int face_id;
22672 bool no_props = INTEGERP (face);
22673 ptrdiff_t count = SPECPDL_INDEX ();
22674 Lisp_Object str;
22675 int string_start = 0;
22676
22677 w = decode_any_window (window);
22678 XSETWINDOW (window, w);
22679
22680 if (NILP (buffer))
22681 buffer = w->contents;
22682 CHECK_BUFFER (buffer);
22683
22684 /* Make formatting the modeline a non-op when noninteractive, otherwise
22685 there will be problems later caused by a partially initialized frame. */
22686 if (NILP (format) || noninteractive)
22687 return empty_unibyte_string;
22688
22689 if (no_props)
22690 face = Qnil;
22691
22692 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22693 : EQ (face, Qt) ? (EQ (window, selected_window)
22694 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22695 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22696 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22697 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22698 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22699 : DEFAULT_FACE_ID;
22700
22701 old_buffer = current_buffer;
22702
22703 /* Save things including mode_line_proptrans_alist,
22704 and set that to nil so that we don't alter the outer value. */
22705 record_unwind_protect (unwind_format_mode_line,
22706 format_mode_line_unwind_data
22707 (XFRAME (WINDOW_FRAME (w)),
22708 old_buffer, selected_window, true));
22709 mode_line_proptrans_alist = Qnil;
22710
22711 Fselect_window (window, Qt);
22712 set_buffer_internal_1 (XBUFFER (buffer));
22713
22714 init_iterator (&it, w, -1, -1, NULL, face_id);
22715
22716 if (no_props)
22717 {
22718 mode_line_target = MODE_LINE_NOPROP;
22719 mode_line_string_face_prop = Qnil;
22720 mode_line_string_list = Qnil;
22721 string_start = MODE_LINE_NOPROP_LEN (0);
22722 }
22723 else
22724 {
22725 mode_line_target = MODE_LINE_STRING;
22726 mode_line_string_list = Qnil;
22727 mode_line_string_face = face;
22728 mode_line_string_face_prop
22729 = NILP (face) ? Qnil : list2 (Qface, face);
22730 }
22731
22732 push_kboard (FRAME_KBOARD (it.f));
22733 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22734 pop_kboard ();
22735
22736 if (no_props)
22737 {
22738 len = MODE_LINE_NOPROP_LEN (string_start);
22739 str = make_string (mode_line_noprop_buf + string_start, len);
22740 }
22741 else
22742 {
22743 mode_line_string_list = Fnreverse (mode_line_string_list);
22744 str = Fmapconcat (Qidentity, mode_line_string_list,
22745 empty_unibyte_string);
22746 }
22747
22748 unbind_to (count, Qnil);
22749 return str;
22750 }
22751
22752 /* Write a null-terminated, right justified decimal representation of
22753 the positive integer D to BUF using a minimal field width WIDTH. */
22754
22755 static void
22756 pint2str (register char *buf, register int width, register ptrdiff_t d)
22757 {
22758 register char *p = buf;
22759
22760 if (d <= 0)
22761 *p++ = '0';
22762 else
22763 {
22764 while (d > 0)
22765 {
22766 *p++ = d % 10 + '0';
22767 d /= 10;
22768 }
22769 }
22770
22771 for (width -= (int) (p - buf); width > 0; --width)
22772 *p++ = ' ';
22773 *p-- = '\0';
22774 while (p > buf)
22775 {
22776 d = *buf;
22777 *buf++ = *p;
22778 *p-- = d;
22779 }
22780 }
22781
22782 /* Write a null-terminated, right justified decimal and "human
22783 readable" representation of the nonnegative integer D to BUF using
22784 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22785
22786 static const char power_letter[] =
22787 {
22788 0, /* no letter */
22789 'k', /* kilo */
22790 'M', /* mega */
22791 'G', /* giga */
22792 'T', /* tera */
22793 'P', /* peta */
22794 'E', /* exa */
22795 'Z', /* zetta */
22796 'Y' /* yotta */
22797 };
22798
22799 static void
22800 pint2hrstr (char *buf, int width, ptrdiff_t d)
22801 {
22802 /* We aim to represent the nonnegative integer D as
22803 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22804 ptrdiff_t quotient = d;
22805 int remainder = 0;
22806 /* -1 means: do not use TENTHS. */
22807 int tenths = -1;
22808 int exponent = 0;
22809
22810 /* Length of QUOTIENT.TENTHS as a string. */
22811 int length;
22812
22813 char * psuffix;
22814 char * p;
22815
22816 if (quotient >= 1000)
22817 {
22818 /* Scale to the appropriate EXPONENT. */
22819 do
22820 {
22821 remainder = quotient % 1000;
22822 quotient /= 1000;
22823 exponent++;
22824 }
22825 while (quotient >= 1000);
22826
22827 /* Round to nearest and decide whether to use TENTHS or not. */
22828 if (quotient <= 9)
22829 {
22830 tenths = remainder / 100;
22831 if (remainder % 100 >= 50)
22832 {
22833 if (tenths < 9)
22834 tenths++;
22835 else
22836 {
22837 quotient++;
22838 if (quotient == 10)
22839 tenths = -1;
22840 else
22841 tenths = 0;
22842 }
22843 }
22844 }
22845 else
22846 if (remainder >= 500)
22847 {
22848 if (quotient < 999)
22849 quotient++;
22850 else
22851 {
22852 quotient = 1;
22853 exponent++;
22854 tenths = 0;
22855 }
22856 }
22857 }
22858
22859 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22860 if (tenths == -1 && quotient <= 99)
22861 if (quotient <= 9)
22862 length = 1;
22863 else
22864 length = 2;
22865 else
22866 length = 3;
22867 p = psuffix = buf + max (width, length);
22868
22869 /* Print EXPONENT. */
22870 *psuffix++ = power_letter[exponent];
22871 *psuffix = '\0';
22872
22873 /* Print TENTHS. */
22874 if (tenths >= 0)
22875 {
22876 *--p = '0' + tenths;
22877 *--p = '.';
22878 }
22879
22880 /* Print QUOTIENT. */
22881 do
22882 {
22883 int digit = quotient % 10;
22884 *--p = '0' + digit;
22885 }
22886 while ((quotient /= 10) != 0);
22887
22888 /* Print leading spaces. */
22889 while (buf < p)
22890 *--p = ' ';
22891 }
22892
22893 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22894 If EOL_FLAG, set also a mnemonic character for end-of-line
22895 type of CODING_SYSTEM. Return updated pointer into BUF. */
22896
22897 static unsigned char invalid_eol_type[] = "(*invalid*)";
22898
22899 static char *
22900 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
22901 {
22902 Lisp_Object val;
22903 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22904 const unsigned char *eol_str;
22905 int eol_str_len;
22906 /* The EOL conversion we are using. */
22907 Lisp_Object eoltype;
22908
22909 val = CODING_SYSTEM_SPEC (coding_system);
22910 eoltype = Qnil;
22911
22912 if (!VECTORP (val)) /* Not yet decided. */
22913 {
22914 *buf++ = multibyte ? '-' : ' ';
22915 if (eol_flag)
22916 eoltype = eol_mnemonic_undecided;
22917 /* Don't mention EOL conversion if it isn't decided. */
22918 }
22919 else
22920 {
22921 Lisp_Object attrs;
22922 Lisp_Object eolvalue;
22923
22924 attrs = AREF (val, 0);
22925 eolvalue = AREF (val, 2);
22926
22927 *buf++ = multibyte
22928 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22929 : ' ';
22930
22931 if (eol_flag)
22932 {
22933 /* The EOL conversion that is normal on this system. */
22934
22935 if (NILP (eolvalue)) /* Not yet decided. */
22936 eoltype = eol_mnemonic_undecided;
22937 else if (VECTORP (eolvalue)) /* Not yet decided. */
22938 eoltype = eol_mnemonic_undecided;
22939 else /* eolvalue is Qunix, Qdos, or Qmac. */
22940 eoltype = (EQ (eolvalue, Qunix)
22941 ? eol_mnemonic_unix
22942 : EQ (eolvalue, Qdos)
22943 ? eol_mnemonic_dos : eol_mnemonic_mac);
22944 }
22945 }
22946
22947 if (eol_flag)
22948 {
22949 /* Mention the EOL conversion if it is not the usual one. */
22950 if (STRINGP (eoltype))
22951 {
22952 eol_str = SDATA (eoltype);
22953 eol_str_len = SBYTES (eoltype);
22954 }
22955 else if (CHARACTERP (eoltype))
22956 {
22957 int c = XFASTINT (eoltype);
22958 return buf + CHAR_STRING (c, (unsigned char *) buf);
22959 }
22960 else
22961 {
22962 eol_str = invalid_eol_type;
22963 eol_str_len = sizeof (invalid_eol_type) - 1;
22964 }
22965 memcpy (buf, eol_str, eol_str_len);
22966 buf += eol_str_len;
22967 }
22968
22969 return buf;
22970 }
22971
22972 /* Return a string for the output of a mode line %-spec for window W,
22973 generated by character C. FIELD_WIDTH > 0 means pad the string
22974 returned with spaces to that value. Return a Lisp string in
22975 *STRING if the resulting string is taken from that Lisp string.
22976
22977 Note we operate on the current buffer for most purposes. */
22978
22979 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22980
22981 static const char *
22982 decode_mode_spec (struct window *w, register int c, int field_width,
22983 Lisp_Object *string)
22984 {
22985 Lisp_Object obj;
22986 struct frame *f = XFRAME (WINDOW_FRAME (w));
22987 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22988 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22989 produce strings from numerical values, so limit preposterously
22990 large values of FIELD_WIDTH to avoid overrunning the buffer's
22991 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22992 bytes plus the terminating null. */
22993 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22994 struct buffer *b = current_buffer;
22995
22996 obj = Qnil;
22997 *string = Qnil;
22998
22999 switch (c)
23000 {
23001 case '*':
23002 if (!NILP (BVAR (b, read_only)))
23003 return "%";
23004 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23005 return "*";
23006 return "-";
23007
23008 case '+':
23009 /* This differs from %* only for a modified read-only buffer. */
23010 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23011 return "*";
23012 if (!NILP (BVAR (b, read_only)))
23013 return "%";
23014 return "-";
23015
23016 case '&':
23017 /* This differs from %* in ignoring read-only-ness. */
23018 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23019 return "*";
23020 return "-";
23021
23022 case '%':
23023 return "%";
23024
23025 case '[':
23026 {
23027 int i;
23028 char *p;
23029
23030 if (command_loop_level > 5)
23031 return "[[[... ";
23032 p = decode_mode_spec_buf;
23033 for (i = 0; i < command_loop_level; i++)
23034 *p++ = '[';
23035 *p = 0;
23036 return decode_mode_spec_buf;
23037 }
23038
23039 case ']':
23040 {
23041 int i;
23042 char *p;
23043
23044 if (command_loop_level > 5)
23045 return " ...]]]";
23046 p = decode_mode_spec_buf;
23047 for (i = 0; i < command_loop_level; i++)
23048 *p++ = ']';
23049 *p = 0;
23050 return decode_mode_spec_buf;
23051 }
23052
23053 case '-':
23054 {
23055 register int i;
23056
23057 /* Let lots_of_dashes be a string of infinite length. */
23058 if (mode_line_target == MODE_LINE_NOPROP
23059 || mode_line_target == MODE_LINE_STRING)
23060 return "--";
23061 if (field_width <= 0
23062 || field_width > sizeof (lots_of_dashes))
23063 {
23064 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23065 decode_mode_spec_buf[i] = '-';
23066 decode_mode_spec_buf[i] = '\0';
23067 return decode_mode_spec_buf;
23068 }
23069 else
23070 return lots_of_dashes;
23071 }
23072
23073 case 'b':
23074 obj = BVAR (b, name);
23075 break;
23076
23077 case 'c':
23078 /* %c and %l are ignored in `frame-title-format'.
23079 (In redisplay_internal, the frame title is drawn _before_ the
23080 windows are updated, so the stuff which depends on actual
23081 window contents (such as %l) may fail to render properly, or
23082 even crash emacs.) */
23083 if (mode_line_target == MODE_LINE_TITLE)
23084 return "";
23085 else
23086 {
23087 ptrdiff_t col = current_column ();
23088 w->column_number_displayed = col;
23089 pint2str (decode_mode_spec_buf, width, col);
23090 return decode_mode_spec_buf;
23091 }
23092
23093 case 'e':
23094 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23095 {
23096 if (NILP (Vmemory_full))
23097 return "";
23098 else
23099 return "!MEM FULL! ";
23100 }
23101 #else
23102 return "";
23103 #endif
23104
23105 case 'F':
23106 /* %F displays the frame name. */
23107 if (!NILP (f->title))
23108 return SSDATA (f->title);
23109 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23110 return SSDATA (f->name);
23111 return "Emacs";
23112
23113 case 'f':
23114 obj = BVAR (b, filename);
23115 break;
23116
23117 case 'i':
23118 {
23119 ptrdiff_t size = ZV - BEGV;
23120 pint2str (decode_mode_spec_buf, width, size);
23121 return decode_mode_spec_buf;
23122 }
23123
23124 case 'I':
23125 {
23126 ptrdiff_t size = ZV - BEGV;
23127 pint2hrstr (decode_mode_spec_buf, width, size);
23128 return decode_mode_spec_buf;
23129 }
23130
23131 case 'l':
23132 {
23133 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23134 ptrdiff_t topline, nlines, height;
23135 ptrdiff_t junk;
23136
23137 /* %c and %l are ignored in `frame-title-format'. */
23138 if (mode_line_target == MODE_LINE_TITLE)
23139 return "";
23140
23141 startpos = marker_position (w->start);
23142 startpos_byte = marker_byte_position (w->start);
23143 height = WINDOW_TOTAL_LINES (w);
23144
23145 /* If we decided that this buffer isn't suitable for line numbers,
23146 don't forget that too fast. */
23147 if (w->base_line_pos == -1)
23148 goto no_value;
23149
23150 /* If the buffer is very big, don't waste time. */
23151 if (INTEGERP (Vline_number_display_limit)
23152 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23153 {
23154 w->base_line_pos = 0;
23155 w->base_line_number = 0;
23156 goto no_value;
23157 }
23158
23159 if (w->base_line_number > 0
23160 && w->base_line_pos > 0
23161 && w->base_line_pos <= startpos)
23162 {
23163 line = w->base_line_number;
23164 linepos = w->base_line_pos;
23165 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23166 }
23167 else
23168 {
23169 line = 1;
23170 linepos = BUF_BEGV (b);
23171 linepos_byte = BUF_BEGV_BYTE (b);
23172 }
23173
23174 /* Count lines from base line to window start position. */
23175 nlines = display_count_lines (linepos_byte,
23176 startpos_byte,
23177 startpos, &junk);
23178
23179 topline = nlines + line;
23180
23181 /* Determine a new base line, if the old one is too close
23182 or too far away, or if we did not have one.
23183 "Too close" means it's plausible a scroll-down would
23184 go back past it. */
23185 if (startpos == BUF_BEGV (b))
23186 {
23187 w->base_line_number = topline;
23188 w->base_line_pos = BUF_BEGV (b);
23189 }
23190 else if (nlines < height + 25 || nlines > height * 3 + 50
23191 || linepos == BUF_BEGV (b))
23192 {
23193 ptrdiff_t limit = BUF_BEGV (b);
23194 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23195 ptrdiff_t position;
23196 ptrdiff_t distance =
23197 (height * 2 + 30) * line_number_display_limit_width;
23198
23199 if (startpos - distance > limit)
23200 {
23201 limit = startpos - distance;
23202 limit_byte = CHAR_TO_BYTE (limit);
23203 }
23204
23205 nlines = display_count_lines (startpos_byte,
23206 limit_byte,
23207 - (height * 2 + 30),
23208 &position);
23209 /* If we couldn't find the lines we wanted within
23210 line_number_display_limit_width chars per line,
23211 give up on line numbers for this window. */
23212 if (position == limit_byte && limit == startpos - distance)
23213 {
23214 w->base_line_pos = -1;
23215 w->base_line_number = 0;
23216 goto no_value;
23217 }
23218
23219 w->base_line_number = topline - nlines;
23220 w->base_line_pos = BYTE_TO_CHAR (position);
23221 }
23222
23223 /* Now count lines from the start pos to point. */
23224 nlines = display_count_lines (startpos_byte,
23225 PT_BYTE, PT, &junk);
23226
23227 /* Record that we did display the line number. */
23228 line_number_displayed = true;
23229
23230 /* Make the string to show. */
23231 pint2str (decode_mode_spec_buf, width, topline + nlines);
23232 return decode_mode_spec_buf;
23233 no_value:
23234 {
23235 char *p = decode_mode_spec_buf;
23236 int pad = width - 2;
23237 while (pad-- > 0)
23238 *p++ = ' ';
23239 *p++ = '?';
23240 *p++ = '?';
23241 *p = '\0';
23242 return decode_mode_spec_buf;
23243 }
23244 }
23245 break;
23246
23247 case 'm':
23248 obj = BVAR (b, mode_name);
23249 break;
23250
23251 case 'n':
23252 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23253 return " Narrow";
23254 break;
23255
23256 case 'p':
23257 {
23258 ptrdiff_t pos = marker_position (w->start);
23259 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23260
23261 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23262 {
23263 if (pos <= BUF_BEGV (b))
23264 return "All";
23265 else
23266 return "Bottom";
23267 }
23268 else if (pos <= BUF_BEGV (b))
23269 return "Top";
23270 else
23271 {
23272 if (total > 1000000)
23273 /* Do it differently for a large value, to avoid overflow. */
23274 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23275 else
23276 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23277 /* We can't normally display a 3-digit number,
23278 so get us a 2-digit number that is close. */
23279 if (total == 100)
23280 total = 99;
23281 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23282 return decode_mode_spec_buf;
23283 }
23284 }
23285
23286 /* Display percentage of size above the bottom of the screen. */
23287 case 'P':
23288 {
23289 ptrdiff_t toppos = marker_position (w->start);
23290 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23291 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23292
23293 if (botpos >= BUF_ZV (b))
23294 {
23295 if (toppos <= BUF_BEGV (b))
23296 return "All";
23297 else
23298 return "Bottom";
23299 }
23300 else
23301 {
23302 if (total > 1000000)
23303 /* Do it differently for a large value, to avoid overflow. */
23304 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23305 else
23306 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23307 /* We can't normally display a 3-digit number,
23308 so get us a 2-digit number that is close. */
23309 if (total == 100)
23310 total = 99;
23311 if (toppos <= BUF_BEGV (b))
23312 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23313 else
23314 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23315 return decode_mode_spec_buf;
23316 }
23317 }
23318
23319 case 's':
23320 /* status of process */
23321 obj = Fget_buffer_process (Fcurrent_buffer ());
23322 if (NILP (obj))
23323 return "no process";
23324 #ifndef MSDOS
23325 obj = Fsymbol_name (Fprocess_status (obj));
23326 #endif
23327 break;
23328
23329 case '@':
23330 {
23331 ptrdiff_t count = inhibit_garbage_collection ();
23332 Lisp_Object curdir = BVAR (current_buffer, directory);
23333 Lisp_Object val = Qnil;
23334
23335 if (STRINGP (curdir))
23336 val = call1 (intern ("file-remote-p"), curdir);
23337
23338 unbind_to (count, Qnil);
23339
23340 if (NILP (val))
23341 return "-";
23342 else
23343 return "@";
23344 }
23345
23346 case 'z':
23347 /* coding-system (not including end-of-line format) */
23348 case 'Z':
23349 /* coding-system (including end-of-line type) */
23350 {
23351 bool eol_flag = (c == 'Z');
23352 char *p = decode_mode_spec_buf;
23353
23354 if (! FRAME_WINDOW_P (f))
23355 {
23356 /* No need to mention EOL here--the terminal never needs
23357 to do EOL conversion. */
23358 p = decode_mode_spec_coding (CODING_ID_NAME
23359 (FRAME_KEYBOARD_CODING (f)->id),
23360 p, false);
23361 p = decode_mode_spec_coding (CODING_ID_NAME
23362 (FRAME_TERMINAL_CODING (f)->id),
23363 p, false);
23364 }
23365 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23366 p, eol_flag);
23367
23368 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23369 #ifdef subprocesses
23370 obj = Fget_buffer_process (Fcurrent_buffer ());
23371 if (PROCESSP (obj))
23372 {
23373 p = decode_mode_spec_coding
23374 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23375 p = decode_mode_spec_coding
23376 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23377 }
23378 #endif /* subprocesses */
23379 #endif /* false */
23380 *p = 0;
23381 return decode_mode_spec_buf;
23382 }
23383 }
23384
23385 if (STRINGP (obj))
23386 {
23387 *string = obj;
23388 return SSDATA (obj);
23389 }
23390 else
23391 return "";
23392 }
23393
23394
23395 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23396 means count lines back from START_BYTE. But don't go beyond
23397 LIMIT_BYTE. Return the number of lines thus found (always
23398 nonnegative).
23399
23400 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23401 either the position COUNT lines after/before START_BYTE, if we
23402 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23403 COUNT lines. */
23404
23405 static ptrdiff_t
23406 display_count_lines (ptrdiff_t start_byte,
23407 ptrdiff_t limit_byte, ptrdiff_t count,
23408 ptrdiff_t *byte_pos_ptr)
23409 {
23410 register unsigned char *cursor;
23411 unsigned char *base;
23412
23413 register ptrdiff_t ceiling;
23414 register unsigned char *ceiling_addr;
23415 ptrdiff_t orig_count = count;
23416
23417 /* If we are not in selective display mode,
23418 check only for newlines. */
23419 bool selective_display
23420 = (!NILP (BVAR (current_buffer, selective_display))
23421 && !INTEGERP (BVAR (current_buffer, selective_display)));
23422
23423 if (count > 0)
23424 {
23425 while (start_byte < limit_byte)
23426 {
23427 ceiling = BUFFER_CEILING_OF (start_byte);
23428 ceiling = min (limit_byte - 1, ceiling);
23429 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23430 base = (cursor = BYTE_POS_ADDR (start_byte));
23431
23432 do
23433 {
23434 if (selective_display)
23435 {
23436 while (*cursor != '\n' && *cursor != 015
23437 && ++cursor != ceiling_addr)
23438 continue;
23439 if (cursor == ceiling_addr)
23440 break;
23441 }
23442 else
23443 {
23444 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23445 if (! cursor)
23446 break;
23447 }
23448
23449 cursor++;
23450
23451 if (--count == 0)
23452 {
23453 start_byte += cursor - base;
23454 *byte_pos_ptr = start_byte;
23455 return orig_count;
23456 }
23457 }
23458 while (cursor < ceiling_addr);
23459
23460 start_byte += ceiling_addr - base;
23461 }
23462 }
23463 else
23464 {
23465 while (start_byte > limit_byte)
23466 {
23467 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23468 ceiling = max (limit_byte, ceiling);
23469 ceiling_addr = BYTE_POS_ADDR (ceiling);
23470 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23471 while (true)
23472 {
23473 if (selective_display)
23474 {
23475 while (--cursor >= ceiling_addr
23476 && *cursor != '\n' && *cursor != 015)
23477 continue;
23478 if (cursor < ceiling_addr)
23479 break;
23480 }
23481 else
23482 {
23483 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23484 if (! cursor)
23485 break;
23486 }
23487
23488 if (++count == 0)
23489 {
23490 start_byte += cursor - base + 1;
23491 *byte_pos_ptr = start_byte;
23492 /* When scanning backwards, we should
23493 not count the newline posterior to which we stop. */
23494 return - orig_count - 1;
23495 }
23496 }
23497 start_byte += ceiling_addr - base;
23498 }
23499 }
23500
23501 *byte_pos_ptr = limit_byte;
23502
23503 if (count < 0)
23504 return - orig_count + count;
23505 return orig_count - count;
23506
23507 }
23508
23509
23510 \f
23511 /***********************************************************************
23512 Displaying strings
23513 ***********************************************************************/
23514
23515 /* Display a NUL-terminated string, starting with index START.
23516
23517 If STRING is non-null, display that C string. Otherwise, the Lisp
23518 string LISP_STRING is displayed. There's a case that STRING is
23519 non-null and LISP_STRING is not nil. It means STRING is a string
23520 data of LISP_STRING. In that case, we display LISP_STRING while
23521 ignoring its text properties.
23522
23523 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23524 FACE_STRING. Display STRING or LISP_STRING with the face at
23525 FACE_STRING_POS in FACE_STRING:
23526
23527 Display the string in the environment given by IT, but use the
23528 standard display table, temporarily.
23529
23530 FIELD_WIDTH is the minimum number of output glyphs to produce.
23531 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23532 with spaces. If STRING has more characters, more than FIELD_WIDTH
23533 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23534
23535 PRECISION is the maximum number of characters to output from
23536 STRING. PRECISION < 0 means don't truncate the string.
23537
23538 This is roughly equivalent to printf format specifiers:
23539
23540 FIELD_WIDTH PRECISION PRINTF
23541 ----------------------------------------
23542 -1 -1 %s
23543 -1 10 %.10s
23544 10 -1 %10s
23545 20 10 %20.10s
23546
23547 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23548 display them, and < 0 means obey the current buffer's value of
23549 enable_multibyte_characters.
23550
23551 Value is the number of columns displayed. */
23552
23553 static int
23554 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23555 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23556 int field_width, int precision, int max_x, int multibyte)
23557 {
23558 int hpos_at_start = it->hpos;
23559 int saved_face_id = it->face_id;
23560 struct glyph_row *row = it->glyph_row;
23561 ptrdiff_t it_charpos;
23562
23563 /* Initialize the iterator IT for iteration over STRING beginning
23564 with index START. */
23565 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23566 precision, field_width, multibyte);
23567 if (string && STRINGP (lisp_string))
23568 /* LISP_STRING is the one returned by decode_mode_spec. We should
23569 ignore its text properties. */
23570 it->stop_charpos = it->end_charpos;
23571
23572 /* If displaying STRING, set up the face of the iterator from
23573 FACE_STRING, if that's given. */
23574 if (STRINGP (face_string))
23575 {
23576 ptrdiff_t endptr;
23577 struct face *face;
23578
23579 it->face_id
23580 = face_at_string_position (it->w, face_string, face_string_pos,
23581 0, &endptr, it->base_face_id, false);
23582 face = FACE_FROM_ID (it->f, it->face_id);
23583 it->face_box_p = face->box != FACE_NO_BOX;
23584 }
23585
23586 /* Set max_x to the maximum allowed X position. Don't let it go
23587 beyond the right edge of the window. */
23588 if (max_x <= 0)
23589 max_x = it->last_visible_x;
23590 else
23591 max_x = min (max_x, it->last_visible_x);
23592
23593 /* Skip over display elements that are not visible. because IT->w is
23594 hscrolled. */
23595 if (it->current_x < it->first_visible_x)
23596 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23597 MOVE_TO_POS | MOVE_TO_X);
23598
23599 row->ascent = it->max_ascent;
23600 row->height = it->max_ascent + it->max_descent;
23601 row->phys_ascent = it->max_phys_ascent;
23602 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23603 row->extra_line_spacing = it->max_extra_line_spacing;
23604
23605 if (STRINGP (it->string))
23606 it_charpos = IT_STRING_CHARPOS (*it);
23607 else
23608 it_charpos = IT_CHARPOS (*it);
23609
23610 /* This condition is for the case that we are called with current_x
23611 past last_visible_x. */
23612 while (it->current_x < max_x)
23613 {
23614 int x_before, x, n_glyphs_before, i, nglyphs;
23615
23616 /* Get the next display element. */
23617 if (!get_next_display_element (it))
23618 break;
23619
23620 /* Produce glyphs. */
23621 x_before = it->current_x;
23622 n_glyphs_before = row->used[TEXT_AREA];
23623 PRODUCE_GLYPHS (it);
23624
23625 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23626 i = 0;
23627 x = x_before;
23628 while (i < nglyphs)
23629 {
23630 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23631
23632 if (it->line_wrap != TRUNCATE
23633 && x + glyph->pixel_width > max_x)
23634 {
23635 /* End of continued line or max_x reached. */
23636 if (CHAR_GLYPH_PADDING_P (*glyph))
23637 {
23638 /* A wide character is unbreakable. */
23639 if (row->reversed_p)
23640 unproduce_glyphs (it, row->used[TEXT_AREA]
23641 - n_glyphs_before);
23642 row->used[TEXT_AREA] = n_glyphs_before;
23643 it->current_x = x_before;
23644 }
23645 else
23646 {
23647 if (row->reversed_p)
23648 unproduce_glyphs (it, row->used[TEXT_AREA]
23649 - (n_glyphs_before + i));
23650 row->used[TEXT_AREA] = n_glyphs_before + i;
23651 it->current_x = x;
23652 }
23653 break;
23654 }
23655 else if (x + glyph->pixel_width >= it->first_visible_x)
23656 {
23657 /* Glyph is at least partially visible. */
23658 ++it->hpos;
23659 if (x < it->first_visible_x)
23660 row->x = x - it->first_visible_x;
23661 }
23662 else
23663 {
23664 /* Glyph is off the left margin of the display area.
23665 Should not happen. */
23666 emacs_abort ();
23667 }
23668
23669 row->ascent = max (row->ascent, it->max_ascent);
23670 row->height = max (row->height, it->max_ascent + it->max_descent);
23671 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23672 row->phys_height = max (row->phys_height,
23673 it->max_phys_ascent + it->max_phys_descent);
23674 row->extra_line_spacing = max (row->extra_line_spacing,
23675 it->max_extra_line_spacing);
23676 x += glyph->pixel_width;
23677 ++i;
23678 }
23679
23680 /* Stop if max_x reached. */
23681 if (i < nglyphs)
23682 break;
23683
23684 /* Stop at line ends. */
23685 if (ITERATOR_AT_END_OF_LINE_P (it))
23686 {
23687 it->continuation_lines_width = 0;
23688 break;
23689 }
23690
23691 set_iterator_to_next (it, true);
23692 if (STRINGP (it->string))
23693 it_charpos = IT_STRING_CHARPOS (*it);
23694 else
23695 it_charpos = IT_CHARPOS (*it);
23696
23697 /* Stop if truncating at the right edge. */
23698 if (it->line_wrap == TRUNCATE
23699 && it->current_x >= it->last_visible_x)
23700 {
23701 /* Add truncation mark, but don't do it if the line is
23702 truncated at a padding space. */
23703 if (it_charpos < it->string_nchars)
23704 {
23705 if (!FRAME_WINDOW_P (it->f))
23706 {
23707 int ii, n;
23708
23709 if (it->current_x > it->last_visible_x)
23710 {
23711 if (!row->reversed_p)
23712 {
23713 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23714 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23715 break;
23716 }
23717 else
23718 {
23719 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23720 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23721 break;
23722 unproduce_glyphs (it, ii + 1);
23723 ii = row->used[TEXT_AREA] - (ii + 1);
23724 }
23725 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23726 {
23727 row->used[TEXT_AREA] = ii;
23728 produce_special_glyphs (it, IT_TRUNCATION);
23729 }
23730 }
23731 produce_special_glyphs (it, IT_TRUNCATION);
23732 }
23733 row->truncated_on_right_p = true;
23734 }
23735 break;
23736 }
23737 }
23738
23739 /* Maybe insert a truncation at the left. */
23740 if (it->first_visible_x
23741 && it_charpos > 0)
23742 {
23743 if (!FRAME_WINDOW_P (it->f)
23744 || (row->reversed_p
23745 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23746 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23747 insert_left_trunc_glyphs (it);
23748 row->truncated_on_left_p = true;
23749 }
23750
23751 it->face_id = saved_face_id;
23752
23753 /* Value is number of columns displayed. */
23754 return it->hpos - hpos_at_start;
23755 }
23756
23757
23758 \f
23759 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23760 appears as an element of LIST or as the car of an element of LIST.
23761 If PROPVAL is a list, compare each element against LIST in that
23762 way, and return 1/2 if any element of PROPVAL is found in LIST.
23763 Otherwise return 0. This function cannot quit.
23764 The return value is 2 if the text is invisible but with an ellipsis
23765 and 1 if it's invisible and without an ellipsis. */
23766
23767 int
23768 invisible_prop (Lisp_Object propval, Lisp_Object list)
23769 {
23770 Lisp_Object tail, proptail;
23771
23772 for (tail = list; CONSP (tail); tail = XCDR (tail))
23773 {
23774 register Lisp_Object tem;
23775 tem = XCAR (tail);
23776 if (EQ (propval, tem))
23777 return 1;
23778 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23779 return NILP (XCDR (tem)) ? 1 : 2;
23780 }
23781
23782 if (CONSP (propval))
23783 {
23784 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23785 {
23786 Lisp_Object propelt;
23787 propelt = XCAR (proptail);
23788 for (tail = list; CONSP (tail); tail = XCDR (tail))
23789 {
23790 register Lisp_Object tem;
23791 tem = XCAR (tail);
23792 if (EQ (propelt, tem))
23793 return 1;
23794 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23795 return NILP (XCDR (tem)) ? 1 : 2;
23796 }
23797 }
23798 }
23799
23800 return 0;
23801 }
23802
23803 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23804 doc: /* Non-nil if the property makes the text invisible.
23805 POS-OR-PROP can be a marker or number, in which case it is taken to be
23806 a position in the current buffer and the value of the `invisible' property
23807 is checked; or it can be some other value, which is then presumed to be the
23808 value of the `invisible' property of the text of interest.
23809 The non-nil value returned can be t for truly invisible text or something
23810 else if the text is replaced by an ellipsis. */)
23811 (Lisp_Object pos_or_prop)
23812 {
23813 Lisp_Object prop
23814 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23815 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23816 : pos_or_prop);
23817 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23818 return (invis == 0 ? Qnil
23819 : invis == 1 ? Qt
23820 : make_number (invis));
23821 }
23822
23823 /* Calculate a width or height in pixels from a specification using
23824 the following elements:
23825
23826 SPEC ::=
23827 NUM - a (fractional) multiple of the default font width/height
23828 (NUM) - specifies exactly NUM pixels
23829 UNIT - a fixed number of pixels, see below.
23830 ELEMENT - size of a display element in pixels, see below.
23831 (NUM . SPEC) - equals NUM * SPEC
23832 (+ SPEC SPEC ...) - add pixel values
23833 (- SPEC SPEC ...) - subtract pixel values
23834 (- SPEC) - negate pixel value
23835
23836 NUM ::=
23837 INT or FLOAT - a number constant
23838 SYMBOL - use symbol's (buffer local) variable binding.
23839
23840 UNIT ::=
23841 in - pixels per inch *)
23842 mm - pixels per 1/1000 meter *)
23843 cm - pixels per 1/100 meter *)
23844 width - width of current font in pixels.
23845 height - height of current font in pixels.
23846
23847 *) using the ratio(s) defined in display-pixels-per-inch.
23848
23849 ELEMENT ::=
23850
23851 left-fringe - left fringe width in pixels
23852 right-fringe - right fringe width in pixels
23853
23854 left-margin - left margin width in pixels
23855 right-margin - right margin width in pixels
23856
23857 scroll-bar - scroll-bar area width in pixels
23858
23859 Examples:
23860
23861 Pixels corresponding to 5 inches:
23862 (5 . in)
23863
23864 Total width of non-text areas on left side of window (if scroll-bar is on left):
23865 '(space :width (+ left-fringe left-margin scroll-bar))
23866
23867 Align to first text column (in header line):
23868 '(space :align-to 0)
23869
23870 Align to middle of text area minus half the width of variable `my-image'
23871 containing a loaded image:
23872 '(space :align-to (0.5 . (- text my-image)))
23873
23874 Width of left margin minus width of 1 character in the default font:
23875 '(space :width (- left-margin 1))
23876
23877 Width of left margin minus width of 2 characters in the current font:
23878 '(space :width (- left-margin (2 . width)))
23879
23880 Center 1 character over left-margin (in header line):
23881 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23882
23883 Different ways to express width of left fringe plus left margin minus one pixel:
23884 '(space :width (- (+ left-fringe left-margin) (1)))
23885 '(space :width (+ left-fringe left-margin (- (1))))
23886 '(space :width (+ left-fringe left-margin (-1)))
23887
23888 */
23889
23890 static bool
23891 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23892 struct font *font, bool width_p, int *align_to)
23893 {
23894 double pixels;
23895
23896 # define OK_PIXELS(val) (*res = (val), true)
23897 # define OK_ALIGN_TO(val) (*align_to = (val), true)
23898
23899 if (NILP (prop))
23900 return OK_PIXELS (0);
23901
23902 eassert (FRAME_LIVE_P (it->f));
23903
23904 if (SYMBOLP (prop))
23905 {
23906 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23907 {
23908 char *unit = SSDATA (SYMBOL_NAME (prop));
23909
23910 if (unit[0] == 'i' && unit[1] == 'n')
23911 pixels = 1.0;
23912 else if (unit[0] == 'm' && unit[1] == 'm')
23913 pixels = 25.4;
23914 else if (unit[0] == 'c' && unit[1] == 'm')
23915 pixels = 2.54;
23916 else
23917 pixels = 0;
23918 if (pixels > 0)
23919 {
23920 double ppi = (width_p ? FRAME_RES_X (it->f)
23921 : FRAME_RES_Y (it->f));
23922
23923 if (ppi > 0)
23924 return OK_PIXELS (ppi / pixels);
23925 return false;
23926 }
23927 }
23928
23929 #ifdef HAVE_WINDOW_SYSTEM
23930 if (EQ (prop, Qheight))
23931 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23932 if (EQ (prop, Qwidth))
23933 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23934 #else
23935 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23936 return OK_PIXELS (1);
23937 #endif
23938
23939 if (EQ (prop, Qtext))
23940 return OK_PIXELS (width_p
23941 ? window_box_width (it->w, TEXT_AREA)
23942 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23943
23944 if (align_to && *align_to < 0)
23945 {
23946 *res = 0;
23947 if (EQ (prop, Qleft))
23948 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23949 if (EQ (prop, Qright))
23950 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23951 if (EQ (prop, Qcenter))
23952 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23953 + window_box_width (it->w, TEXT_AREA) / 2);
23954 if (EQ (prop, Qleft_fringe))
23955 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23956 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23957 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23958 if (EQ (prop, Qright_fringe))
23959 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23960 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23961 : window_box_right_offset (it->w, TEXT_AREA));
23962 if (EQ (prop, Qleft_margin))
23963 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23964 if (EQ (prop, Qright_margin))
23965 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23966 if (EQ (prop, Qscroll_bar))
23967 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23968 ? 0
23969 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23970 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23971 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23972 : 0)));
23973 }
23974 else
23975 {
23976 if (EQ (prop, Qleft_fringe))
23977 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23978 if (EQ (prop, Qright_fringe))
23979 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23980 if (EQ (prop, Qleft_margin))
23981 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23982 if (EQ (prop, Qright_margin))
23983 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23984 if (EQ (prop, Qscroll_bar))
23985 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23986 }
23987
23988 prop = buffer_local_value (prop, it->w->contents);
23989 if (EQ (prop, Qunbound))
23990 prop = Qnil;
23991 }
23992
23993 if (INTEGERP (prop) || FLOATP (prop))
23994 {
23995 int base_unit = (width_p
23996 ? FRAME_COLUMN_WIDTH (it->f)
23997 : FRAME_LINE_HEIGHT (it->f));
23998 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23999 }
24000
24001 if (CONSP (prop))
24002 {
24003 Lisp_Object car = XCAR (prop);
24004 Lisp_Object cdr = XCDR (prop);
24005
24006 if (SYMBOLP (car))
24007 {
24008 #ifdef HAVE_WINDOW_SYSTEM
24009 if (FRAME_WINDOW_P (it->f)
24010 && valid_image_p (prop))
24011 {
24012 ptrdiff_t id = lookup_image (it->f, prop);
24013 struct image *img = IMAGE_FROM_ID (it->f, id);
24014
24015 return OK_PIXELS (width_p ? img->width : img->height);
24016 }
24017 #endif
24018 if (EQ (car, Qplus) || EQ (car, Qminus))
24019 {
24020 bool first = true;
24021 double px;
24022
24023 pixels = 0;
24024 while (CONSP (cdr))
24025 {
24026 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24027 font, width_p, align_to))
24028 return false;
24029 if (first)
24030 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24031 else
24032 pixels += px;
24033 cdr = XCDR (cdr);
24034 }
24035 if (EQ (car, Qminus))
24036 pixels = -pixels;
24037 return OK_PIXELS (pixels);
24038 }
24039
24040 car = buffer_local_value (car, it->w->contents);
24041 if (EQ (car, Qunbound))
24042 car = Qnil;
24043 }
24044
24045 if (INTEGERP (car) || FLOATP (car))
24046 {
24047 double fact;
24048 pixels = XFLOATINT (car);
24049 if (NILP (cdr))
24050 return OK_PIXELS (pixels);
24051 if (calc_pixel_width_or_height (&fact, it, cdr,
24052 font, width_p, align_to))
24053 return OK_PIXELS (pixels * fact);
24054 return false;
24055 }
24056
24057 return false;
24058 }
24059
24060 return false;
24061 }
24062
24063 \f
24064 /***********************************************************************
24065 Glyph Display
24066 ***********************************************************************/
24067
24068 #ifdef HAVE_WINDOW_SYSTEM
24069
24070 #ifdef GLYPH_DEBUG
24071
24072 void
24073 dump_glyph_string (struct glyph_string *s)
24074 {
24075 fprintf (stderr, "glyph string\n");
24076 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24077 s->x, s->y, s->width, s->height);
24078 fprintf (stderr, " ybase = %d\n", s->ybase);
24079 fprintf (stderr, " hl = %d\n", s->hl);
24080 fprintf (stderr, " left overhang = %d, right = %d\n",
24081 s->left_overhang, s->right_overhang);
24082 fprintf (stderr, " nchars = %d\n", s->nchars);
24083 fprintf (stderr, " extends to end of line = %d\n",
24084 s->extends_to_end_of_line_p);
24085 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24086 fprintf (stderr, " bg width = %d\n", s->background_width);
24087 }
24088
24089 #endif /* GLYPH_DEBUG */
24090
24091 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24092 of XChar2b structures for S; it can't be allocated in
24093 init_glyph_string because it must be allocated via `alloca'. W
24094 is the window on which S is drawn. ROW and AREA are the glyph row
24095 and area within the row from which S is constructed. START is the
24096 index of the first glyph structure covered by S. HL is a
24097 face-override for drawing S. */
24098
24099 #ifdef HAVE_NTGUI
24100 #define OPTIONAL_HDC(hdc) HDC hdc,
24101 #define DECLARE_HDC(hdc) HDC hdc;
24102 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24103 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24104 #endif
24105
24106 #ifndef OPTIONAL_HDC
24107 #define OPTIONAL_HDC(hdc)
24108 #define DECLARE_HDC(hdc)
24109 #define ALLOCATE_HDC(hdc, f)
24110 #define RELEASE_HDC(hdc, f)
24111 #endif
24112
24113 static void
24114 init_glyph_string (struct glyph_string *s,
24115 OPTIONAL_HDC (hdc)
24116 XChar2b *char2b, struct window *w, struct glyph_row *row,
24117 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24118 {
24119 memset (s, 0, sizeof *s);
24120 s->w = w;
24121 s->f = XFRAME (w->frame);
24122 #ifdef HAVE_NTGUI
24123 s->hdc = hdc;
24124 #endif
24125 s->display = FRAME_X_DISPLAY (s->f);
24126 s->window = FRAME_X_WINDOW (s->f);
24127 s->char2b = char2b;
24128 s->hl = hl;
24129 s->row = row;
24130 s->area = area;
24131 s->first_glyph = row->glyphs[area] + start;
24132 s->height = row->height;
24133 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24134 s->ybase = s->y + row->ascent;
24135 }
24136
24137
24138 /* Append the list of glyph strings with head H and tail T to the list
24139 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24140
24141 static void
24142 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24143 struct glyph_string *h, struct glyph_string *t)
24144 {
24145 if (h)
24146 {
24147 if (*head)
24148 (*tail)->next = h;
24149 else
24150 *head = h;
24151 h->prev = *tail;
24152 *tail = t;
24153 }
24154 }
24155
24156
24157 /* Prepend the list of glyph strings with head H and tail T to the
24158 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24159 result. */
24160
24161 static void
24162 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24163 struct glyph_string *h, struct glyph_string *t)
24164 {
24165 if (h)
24166 {
24167 if (*head)
24168 (*head)->prev = t;
24169 else
24170 *tail = t;
24171 t->next = *head;
24172 *head = h;
24173 }
24174 }
24175
24176
24177 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24178 Set *HEAD and *TAIL to the resulting list. */
24179
24180 static void
24181 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24182 struct glyph_string *s)
24183 {
24184 s->next = s->prev = NULL;
24185 append_glyph_string_lists (head, tail, s, s);
24186 }
24187
24188
24189 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24190 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24191 make sure that X resources for the face returned are allocated.
24192 Value is a pointer to a realized face that is ready for display if
24193 DISPLAY_P. */
24194
24195 static struct face *
24196 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24197 XChar2b *char2b, bool display_p)
24198 {
24199 struct face *face = FACE_FROM_ID (f, face_id);
24200 unsigned code = 0;
24201
24202 if (face->font)
24203 {
24204 code = face->font->driver->encode_char (face->font, c);
24205
24206 if (code == FONT_INVALID_CODE)
24207 code = 0;
24208 }
24209 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24210
24211 /* Make sure X resources of the face are allocated. */
24212 #ifdef HAVE_X_WINDOWS
24213 if (display_p)
24214 #endif
24215 {
24216 eassert (face != NULL);
24217 prepare_face_for_display (f, face);
24218 }
24219
24220 return face;
24221 }
24222
24223
24224 /* Get face and two-byte form of character glyph GLYPH on frame F.
24225 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24226 a pointer to a realized face that is ready for display. */
24227
24228 static struct face *
24229 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24230 XChar2b *char2b)
24231 {
24232 struct face *face;
24233 unsigned code = 0;
24234
24235 eassert (glyph->type == CHAR_GLYPH);
24236 face = FACE_FROM_ID (f, glyph->face_id);
24237
24238 /* Make sure X resources of the face are allocated. */
24239 eassert (face != NULL);
24240 prepare_face_for_display (f, face);
24241
24242 if (face->font)
24243 {
24244 if (CHAR_BYTE8_P (glyph->u.ch))
24245 code = CHAR_TO_BYTE8 (glyph->u.ch);
24246 else
24247 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24248
24249 if (code == FONT_INVALID_CODE)
24250 code = 0;
24251 }
24252
24253 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24254 return face;
24255 }
24256
24257
24258 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24259 Return true iff FONT has a glyph for C. */
24260
24261 static bool
24262 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24263 {
24264 unsigned code;
24265
24266 if (CHAR_BYTE8_P (c))
24267 code = CHAR_TO_BYTE8 (c);
24268 else
24269 code = font->driver->encode_char (font, c);
24270
24271 if (code == FONT_INVALID_CODE)
24272 return false;
24273 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24274 return true;
24275 }
24276
24277
24278 /* Fill glyph string S with composition components specified by S->cmp.
24279
24280 BASE_FACE is the base face of the composition.
24281 S->cmp_from is the index of the first component for S.
24282
24283 OVERLAPS non-zero means S should draw the foreground only, and use
24284 its physical height for clipping. See also draw_glyphs.
24285
24286 Value is the index of a component not in S. */
24287
24288 static int
24289 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24290 int overlaps)
24291 {
24292 int i;
24293 /* For all glyphs of this composition, starting at the offset
24294 S->cmp_from, until we reach the end of the definition or encounter a
24295 glyph that requires the different face, add it to S. */
24296 struct face *face;
24297
24298 eassert (s);
24299
24300 s->for_overlaps = overlaps;
24301 s->face = NULL;
24302 s->font = NULL;
24303 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24304 {
24305 int c = COMPOSITION_GLYPH (s->cmp, i);
24306
24307 /* TAB in a composition means display glyphs with padding space
24308 on the left or right. */
24309 if (c != '\t')
24310 {
24311 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24312 -1, Qnil);
24313
24314 face = get_char_face_and_encoding (s->f, c, face_id,
24315 s->char2b + i, true);
24316 if (face)
24317 {
24318 if (! s->face)
24319 {
24320 s->face = face;
24321 s->font = s->face->font;
24322 }
24323 else if (s->face != face)
24324 break;
24325 }
24326 }
24327 ++s->nchars;
24328 }
24329 s->cmp_to = i;
24330
24331 if (s->face == NULL)
24332 {
24333 s->face = base_face->ascii_face;
24334 s->font = s->face->font;
24335 }
24336
24337 /* All glyph strings for the same composition has the same width,
24338 i.e. the width set for the first component of the composition. */
24339 s->width = s->first_glyph->pixel_width;
24340
24341 /* If the specified font could not be loaded, use the frame's
24342 default font, but record the fact that we couldn't load it in
24343 the glyph string so that we can draw rectangles for the
24344 characters of the glyph string. */
24345 if (s->font == NULL)
24346 {
24347 s->font_not_found_p = true;
24348 s->font = FRAME_FONT (s->f);
24349 }
24350
24351 /* Adjust base line for subscript/superscript text. */
24352 s->ybase += s->first_glyph->voffset;
24353
24354 return s->cmp_to;
24355 }
24356
24357 static int
24358 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24359 int start, int end, int overlaps)
24360 {
24361 struct glyph *glyph, *last;
24362 Lisp_Object lgstring;
24363 int i;
24364
24365 s->for_overlaps = overlaps;
24366 glyph = s->row->glyphs[s->area] + start;
24367 last = s->row->glyphs[s->area] + end;
24368 s->cmp_id = glyph->u.cmp.id;
24369 s->cmp_from = glyph->slice.cmp.from;
24370 s->cmp_to = glyph->slice.cmp.to + 1;
24371 s->face = FACE_FROM_ID (s->f, face_id);
24372 lgstring = composition_gstring_from_id (s->cmp_id);
24373 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24374 glyph++;
24375 while (glyph < last
24376 && glyph->u.cmp.automatic
24377 && glyph->u.cmp.id == s->cmp_id
24378 && s->cmp_to == glyph->slice.cmp.from)
24379 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24380
24381 for (i = s->cmp_from; i < s->cmp_to; i++)
24382 {
24383 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24384 unsigned code = LGLYPH_CODE (lglyph);
24385
24386 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24387 }
24388 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24389 return glyph - s->row->glyphs[s->area];
24390 }
24391
24392
24393 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24394 See the comment of fill_glyph_string for arguments.
24395 Value is the index of the first glyph not in S. */
24396
24397
24398 static int
24399 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24400 int start, int end, int overlaps)
24401 {
24402 struct glyph *glyph, *last;
24403 int voffset;
24404
24405 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24406 s->for_overlaps = overlaps;
24407 glyph = s->row->glyphs[s->area] + start;
24408 last = s->row->glyphs[s->area] + end;
24409 voffset = glyph->voffset;
24410 s->face = FACE_FROM_ID (s->f, face_id);
24411 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24412 s->nchars = 1;
24413 s->width = glyph->pixel_width;
24414 glyph++;
24415 while (glyph < last
24416 && glyph->type == GLYPHLESS_GLYPH
24417 && glyph->voffset == voffset
24418 && glyph->face_id == face_id)
24419 {
24420 s->nchars++;
24421 s->width += glyph->pixel_width;
24422 glyph++;
24423 }
24424 s->ybase += voffset;
24425 return glyph - s->row->glyphs[s->area];
24426 }
24427
24428
24429 /* Fill glyph string S from a sequence of character glyphs.
24430
24431 FACE_ID is the face id of the string. START is the index of the
24432 first glyph to consider, END is the index of the last + 1.
24433 OVERLAPS non-zero means S should draw the foreground only, and use
24434 its physical height for clipping. See also draw_glyphs.
24435
24436 Value is the index of the first glyph not in S. */
24437
24438 static int
24439 fill_glyph_string (struct glyph_string *s, int face_id,
24440 int start, int end, int overlaps)
24441 {
24442 struct glyph *glyph, *last;
24443 int voffset;
24444 bool glyph_not_available_p;
24445
24446 eassert (s->f == XFRAME (s->w->frame));
24447 eassert (s->nchars == 0);
24448 eassert (start >= 0 && end > start);
24449
24450 s->for_overlaps = overlaps;
24451 glyph = s->row->glyphs[s->area] + start;
24452 last = s->row->glyphs[s->area] + end;
24453 voffset = glyph->voffset;
24454 s->padding_p = glyph->padding_p;
24455 glyph_not_available_p = glyph->glyph_not_available_p;
24456
24457 while (glyph < last
24458 && glyph->type == CHAR_GLYPH
24459 && glyph->voffset == voffset
24460 /* Same face id implies same font, nowadays. */
24461 && glyph->face_id == face_id
24462 && glyph->glyph_not_available_p == glyph_not_available_p)
24463 {
24464 s->face = get_glyph_face_and_encoding (s->f, glyph,
24465 s->char2b + s->nchars);
24466 ++s->nchars;
24467 eassert (s->nchars <= end - start);
24468 s->width += glyph->pixel_width;
24469 if (glyph++->padding_p != s->padding_p)
24470 break;
24471 }
24472
24473 s->font = s->face->font;
24474
24475 /* If the specified font could not be loaded, use the frame's font,
24476 but record the fact that we couldn't load it in
24477 S->font_not_found_p so that we can draw rectangles for the
24478 characters of the glyph string. */
24479 if (s->font == NULL || glyph_not_available_p)
24480 {
24481 s->font_not_found_p = true;
24482 s->font = FRAME_FONT (s->f);
24483 }
24484
24485 /* Adjust base line for subscript/superscript text. */
24486 s->ybase += voffset;
24487
24488 eassert (s->face && s->face->gc);
24489 return glyph - s->row->glyphs[s->area];
24490 }
24491
24492
24493 /* Fill glyph string S from image glyph S->first_glyph. */
24494
24495 static void
24496 fill_image_glyph_string (struct glyph_string *s)
24497 {
24498 eassert (s->first_glyph->type == IMAGE_GLYPH);
24499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24500 eassert (s->img);
24501 s->slice = s->first_glyph->slice.img;
24502 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24503 s->font = s->face->font;
24504 s->width = s->first_glyph->pixel_width;
24505
24506 /* Adjust base line for subscript/superscript text. */
24507 s->ybase += s->first_glyph->voffset;
24508 }
24509
24510
24511 /* Fill glyph string S from a sequence of stretch glyphs.
24512
24513 START is the index of the first glyph to consider,
24514 END is the index of the last + 1.
24515
24516 Value is the index of the first glyph not in S. */
24517
24518 static int
24519 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24520 {
24521 struct glyph *glyph, *last;
24522 int voffset, face_id;
24523
24524 eassert (s->first_glyph->type == STRETCH_GLYPH);
24525
24526 glyph = s->row->glyphs[s->area] + start;
24527 last = s->row->glyphs[s->area] + end;
24528 face_id = glyph->face_id;
24529 s->face = FACE_FROM_ID (s->f, face_id);
24530 s->font = s->face->font;
24531 s->width = glyph->pixel_width;
24532 s->nchars = 1;
24533 voffset = glyph->voffset;
24534
24535 for (++glyph;
24536 (glyph < last
24537 && glyph->type == STRETCH_GLYPH
24538 && glyph->voffset == voffset
24539 && glyph->face_id == face_id);
24540 ++glyph)
24541 s->width += glyph->pixel_width;
24542
24543 /* Adjust base line for subscript/superscript text. */
24544 s->ybase += voffset;
24545
24546 /* The case that face->gc == 0 is handled when drawing the glyph
24547 string by calling prepare_face_for_display. */
24548 eassert (s->face);
24549 return glyph - s->row->glyphs[s->area];
24550 }
24551
24552 static struct font_metrics *
24553 get_per_char_metric (struct font *font, XChar2b *char2b)
24554 {
24555 static struct font_metrics metrics;
24556 unsigned code;
24557
24558 if (! font)
24559 return NULL;
24560 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24561 if (code == FONT_INVALID_CODE)
24562 return NULL;
24563 font->driver->text_extents (font, &code, 1, &metrics);
24564 return &metrics;
24565 }
24566
24567 /* EXPORT for RIF:
24568 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24569 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24570 assumed to be zero. */
24571
24572 void
24573 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24574 {
24575 *left = *right = 0;
24576
24577 if (glyph->type == CHAR_GLYPH)
24578 {
24579 XChar2b char2b;
24580 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24581 if (face->font)
24582 {
24583 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24584 if (pcm)
24585 {
24586 if (pcm->rbearing > pcm->width)
24587 *right = pcm->rbearing - pcm->width;
24588 if (pcm->lbearing < 0)
24589 *left = -pcm->lbearing;
24590 }
24591 }
24592 }
24593 else if (glyph->type == COMPOSITE_GLYPH)
24594 {
24595 if (! glyph->u.cmp.automatic)
24596 {
24597 struct composition *cmp = composition_table[glyph->u.cmp.id];
24598
24599 if (cmp->rbearing > cmp->pixel_width)
24600 *right = cmp->rbearing - cmp->pixel_width;
24601 if (cmp->lbearing < 0)
24602 *left = - cmp->lbearing;
24603 }
24604 else
24605 {
24606 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24607 struct font_metrics metrics;
24608
24609 composition_gstring_width (gstring, glyph->slice.cmp.from,
24610 glyph->slice.cmp.to + 1, &metrics);
24611 if (metrics.rbearing > metrics.width)
24612 *right = metrics.rbearing - metrics.width;
24613 if (metrics.lbearing < 0)
24614 *left = - metrics.lbearing;
24615 }
24616 }
24617 }
24618
24619
24620 /* Return the index of the first glyph preceding glyph string S that
24621 is overwritten by S because of S's left overhang. Value is -1
24622 if no glyphs are overwritten. */
24623
24624 static int
24625 left_overwritten (struct glyph_string *s)
24626 {
24627 int k;
24628
24629 if (s->left_overhang)
24630 {
24631 int x = 0, i;
24632 struct glyph *glyphs = s->row->glyphs[s->area];
24633 int first = s->first_glyph - glyphs;
24634
24635 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24636 x -= glyphs[i].pixel_width;
24637
24638 k = i + 1;
24639 }
24640 else
24641 k = -1;
24642
24643 return k;
24644 }
24645
24646
24647 /* Return the index of the first glyph preceding glyph string S that
24648 is overwriting S because of its right overhang. Value is -1 if no
24649 glyph in front of S overwrites S. */
24650
24651 static int
24652 left_overwriting (struct glyph_string *s)
24653 {
24654 int i, k, x;
24655 struct glyph *glyphs = s->row->glyphs[s->area];
24656 int first = s->first_glyph - glyphs;
24657
24658 k = -1;
24659 x = 0;
24660 for (i = first - 1; i >= 0; --i)
24661 {
24662 int left, right;
24663 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24664 if (x + right > 0)
24665 k = i;
24666 x -= glyphs[i].pixel_width;
24667 }
24668
24669 return k;
24670 }
24671
24672
24673 /* Return the index of the last glyph following glyph string S that is
24674 overwritten by S because of S's right overhang. Value is -1 if
24675 no such glyph is found. */
24676
24677 static int
24678 right_overwritten (struct glyph_string *s)
24679 {
24680 int k = -1;
24681
24682 if (s->right_overhang)
24683 {
24684 int x = 0, i;
24685 struct glyph *glyphs = s->row->glyphs[s->area];
24686 int first = (s->first_glyph - glyphs
24687 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24688 int end = s->row->used[s->area];
24689
24690 for (i = first; i < end && s->right_overhang > x; ++i)
24691 x += glyphs[i].pixel_width;
24692
24693 k = i;
24694 }
24695
24696 return k;
24697 }
24698
24699
24700 /* Return the index of the last glyph following glyph string S that
24701 overwrites S because of its left overhang. Value is negative
24702 if no such glyph is found. */
24703
24704 static int
24705 right_overwriting (struct glyph_string *s)
24706 {
24707 int i, k, x;
24708 int end = s->row->used[s->area];
24709 struct glyph *glyphs = s->row->glyphs[s->area];
24710 int first = (s->first_glyph - glyphs
24711 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24712
24713 k = -1;
24714 x = 0;
24715 for (i = first; i < end; ++i)
24716 {
24717 int left, right;
24718 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24719 if (x - left < 0)
24720 k = i;
24721 x += glyphs[i].pixel_width;
24722 }
24723
24724 return k;
24725 }
24726
24727
24728 /* Set background width of glyph string S. START is the index of the
24729 first glyph following S. LAST_X is the right-most x-position + 1
24730 in the drawing area. */
24731
24732 static void
24733 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24734 {
24735 /* If the face of this glyph string has to be drawn to the end of
24736 the drawing area, set S->extends_to_end_of_line_p. */
24737
24738 if (start == s->row->used[s->area]
24739 && ((s->row->fill_line_p
24740 && (s->hl == DRAW_NORMAL_TEXT
24741 || s->hl == DRAW_IMAGE_RAISED
24742 || s->hl == DRAW_IMAGE_SUNKEN))
24743 || s->hl == DRAW_MOUSE_FACE))
24744 s->extends_to_end_of_line_p = true;
24745
24746 /* If S extends its face to the end of the line, set its
24747 background_width to the distance to the right edge of the drawing
24748 area. */
24749 if (s->extends_to_end_of_line_p)
24750 s->background_width = last_x - s->x + 1;
24751 else
24752 s->background_width = s->width;
24753 }
24754
24755
24756 /* Compute overhangs and x-positions for glyph string S and its
24757 predecessors, or successors. X is the starting x-position for S.
24758 BACKWARD_P means process predecessors. */
24759
24760 static void
24761 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24762 {
24763 if (backward_p)
24764 {
24765 while (s)
24766 {
24767 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24768 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24769 x -= s->width;
24770 s->x = x;
24771 s = s->prev;
24772 }
24773 }
24774 else
24775 {
24776 while (s)
24777 {
24778 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24779 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24780 s->x = x;
24781 x += s->width;
24782 s = s->next;
24783 }
24784 }
24785 }
24786
24787
24788
24789 /* The following macros are only called from draw_glyphs below.
24790 They reference the following parameters of that function directly:
24791 `w', `row', `area', and `overlap_p'
24792 as well as the following local variables:
24793 `s', `f', and `hdc' (in W32) */
24794
24795 #ifdef HAVE_NTGUI
24796 /* On W32, silently add local `hdc' variable to argument list of
24797 init_glyph_string. */
24798 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24799 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24800 #else
24801 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24802 init_glyph_string (s, char2b, w, row, area, start, hl)
24803 #endif
24804
24805 /* Add a glyph string for a stretch glyph to the list of strings
24806 between HEAD and TAIL. START is the index of the stretch glyph in
24807 row area AREA of glyph row ROW. END is the index of the last glyph
24808 in that glyph row area. X is the current output position assigned
24809 to the new glyph string constructed. HL overrides that face of the
24810 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24811 is the right-most x-position of the drawing area. */
24812
24813 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24814 and below -- keep them on one line. */
24815 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24816 do \
24817 { \
24818 s = alloca (sizeof *s); \
24819 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24820 START = fill_stretch_glyph_string (s, START, END); \
24821 append_glyph_string (&HEAD, &TAIL, s); \
24822 s->x = (X); \
24823 } \
24824 while (false)
24825
24826
24827 /* Add a glyph string for an image glyph to the list of strings
24828 between HEAD and TAIL. START is the index of the image glyph in
24829 row area AREA of glyph row ROW. END is the index of the last glyph
24830 in that glyph row area. X is the current output position assigned
24831 to the new glyph string constructed. HL overrides that face of the
24832 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24833 is the right-most x-position of the drawing area. */
24834
24835 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24836 do \
24837 { \
24838 s = alloca (sizeof *s); \
24839 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24840 fill_image_glyph_string (s); \
24841 append_glyph_string (&HEAD, &TAIL, s); \
24842 ++START; \
24843 s->x = (X); \
24844 } \
24845 while (false)
24846
24847
24848 /* Add a glyph string for a sequence of character glyphs to the list
24849 of strings between HEAD and TAIL. START is the index of the first
24850 glyph in row area AREA of glyph row ROW that is part of the new
24851 glyph string. END is the index of the last glyph in that glyph row
24852 area. X is the current output position assigned to the new glyph
24853 string constructed. HL overrides that face of the glyph; e.g. it
24854 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24855 right-most x-position of the drawing area. */
24856
24857 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24858 do \
24859 { \
24860 int face_id; \
24861 XChar2b *char2b; \
24862 \
24863 face_id = (row)->glyphs[area][START].face_id; \
24864 \
24865 s = alloca (sizeof *s); \
24866 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24867 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24868 append_glyph_string (&HEAD, &TAIL, s); \
24869 s->x = (X); \
24870 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24871 } \
24872 while (false)
24873
24874
24875 /* Add a glyph string for a composite sequence to the list of strings
24876 between HEAD and TAIL. START is the index of the first glyph in
24877 row area AREA of glyph row ROW that is part of the new glyph
24878 string. END is the index of the last glyph in that glyph row area.
24879 X is the current output position assigned to the new glyph string
24880 constructed. HL overrides that face of the glyph; e.g. it is
24881 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24882 x-position of the drawing area. */
24883
24884 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24885 do { \
24886 int face_id = (row)->glyphs[area][START].face_id; \
24887 struct face *base_face = FACE_FROM_ID (f, face_id); \
24888 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24889 struct composition *cmp = composition_table[cmp_id]; \
24890 XChar2b *char2b; \
24891 struct glyph_string *first_s = NULL; \
24892 int n; \
24893 \
24894 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24895 \
24896 /* Make glyph_strings for each glyph sequence that is drawable by \
24897 the same face, and append them to HEAD/TAIL. */ \
24898 for (n = 0; n < cmp->glyph_len;) \
24899 { \
24900 s = alloca (sizeof *s); \
24901 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24902 append_glyph_string (&(HEAD), &(TAIL), s); \
24903 s->cmp = cmp; \
24904 s->cmp_from = n; \
24905 s->x = (X); \
24906 if (n == 0) \
24907 first_s = s; \
24908 n = fill_composite_glyph_string (s, base_face, overlaps); \
24909 } \
24910 \
24911 ++START; \
24912 s = first_s; \
24913 } while (false)
24914
24915
24916 /* Add a glyph string for a glyph-string sequence to the list of strings
24917 between HEAD and TAIL. */
24918
24919 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24920 do { \
24921 int face_id; \
24922 XChar2b *char2b; \
24923 Lisp_Object gstring; \
24924 \
24925 face_id = (row)->glyphs[area][START].face_id; \
24926 gstring = (composition_gstring_from_id \
24927 ((row)->glyphs[area][START].u.cmp.id)); \
24928 s = alloca (sizeof *s); \
24929 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24930 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24931 append_glyph_string (&(HEAD), &(TAIL), s); \
24932 s->x = (X); \
24933 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24934 } while (false)
24935
24936
24937 /* Add a glyph string for a sequence of glyphless character's glyphs
24938 to the list of strings between HEAD and TAIL. The meanings of
24939 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24940
24941 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24942 do \
24943 { \
24944 int face_id; \
24945 \
24946 face_id = (row)->glyphs[area][START].face_id; \
24947 \
24948 s = alloca (sizeof *s); \
24949 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24950 append_glyph_string (&HEAD, &TAIL, s); \
24951 s->x = (X); \
24952 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24953 overlaps); \
24954 } \
24955 while (false)
24956
24957
24958 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24959 of AREA of glyph row ROW on window W between indices START and END.
24960 HL overrides the face for drawing glyph strings, e.g. it is
24961 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24962 x-positions of the drawing area.
24963
24964 This is an ugly monster macro construct because we must use alloca
24965 to allocate glyph strings (because draw_glyphs can be called
24966 asynchronously). */
24967
24968 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24969 do \
24970 { \
24971 HEAD = TAIL = NULL; \
24972 while (START < END) \
24973 { \
24974 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24975 switch (first_glyph->type) \
24976 { \
24977 case CHAR_GLYPH: \
24978 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24979 HL, X, LAST_X); \
24980 break; \
24981 \
24982 case COMPOSITE_GLYPH: \
24983 if (first_glyph->u.cmp.automatic) \
24984 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24985 HL, X, LAST_X); \
24986 else \
24987 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24988 HL, X, LAST_X); \
24989 break; \
24990 \
24991 case STRETCH_GLYPH: \
24992 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24993 HL, X, LAST_X); \
24994 break; \
24995 \
24996 case IMAGE_GLYPH: \
24997 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24998 HL, X, LAST_X); \
24999 break; \
25000 \
25001 case GLYPHLESS_GLYPH: \
25002 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25003 HL, X, LAST_X); \
25004 break; \
25005 \
25006 default: \
25007 emacs_abort (); \
25008 } \
25009 \
25010 if (s) \
25011 { \
25012 set_glyph_string_background_width (s, START, LAST_X); \
25013 (X) += s->width; \
25014 } \
25015 } \
25016 } while (false)
25017
25018
25019 /* Draw glyphs between START and END in AREA of ROW on window W,
25020 starting at x-position X. X is relative to AREA in W. HL is a
25021 face-override with the following meaning:
25022
25023 DRAW_NORMAL_TEXT draw normally
25024 DRAW_CURSOR draw in cursor face
25025 DRAW_MOUSE_FACE draw in mouse face.
25026 DRAW_INVERSE_VIDEO draw in mode line face
25027 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25028 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25029
25030 If OVERLAPS is non-zero, draw only the foreground of characters and
25031 clip to the physical height of ROW. Non-zero value also defines
25032 the overlapping part to be drawn:
25033
25034 OVERLAPS_PRED overlap with preceding rows
25035 OVERLAPS_SUCC overlap with succeeding rows
25036 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25037 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25038
25039 Value is the x-position reached, relative to AREA of W. */
25040
25041 static int
25042 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25043 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25044 enum draw_glyphs_face hl, int overlaps)
25045 {
25046 struct glyph_string *head, *tail;
25047 struct glyph_string *s;
25048 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25049 int i, j, x_reached, last_x, area_left = 0;
25050 struct frame *f = XFRAME (WINDOW_FRAME (w));
25051 DECLARE_HDC (hdc);
25052
25053 ALLOCATE_HDC (hdc, f);
25054
25055 /* Let's rather be paranoid than getting a SEGV. */
25056 end = min (end, row->used[area]);
25057 start = clip_to_bounds (0, start, end);
25058
25059 /* Translate X to frame coordinates. Set last_x to the right
25060 end of the drawing area. */
25061 if (row->full_width_p)
25062 {
25063 /* X is relative to the left edge of W, without scroll bars
25064 or fringes. */
25065 area_left = WINDOW_LEFT_EDGE_X (w);
25066 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25067 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25068 }
25069 else
25070 {
25071 area_left = window_box_left (w, area);
25072 last_x = area_left + window_box_width (w, area);
25073 }
25074 x += area_left;
25075
25076 /* Build a doubly-linked list of glyph_string structures between
25077 head and tail from what we have to draw. Note that the macro
25078 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25079 the reason we use a separate variable `i'. */
25080 i = start;
25081 USE_SAFE_ALLOCA;
25082 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25083 if (tail)
25084 x_reached = tail->x + tail->background_width;
25085 else
25086 x_reached = x;
25087
25088 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25089 the row, redraw some glyphs in front or following the glyph
25090 strings built above. */
25091 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25092 {
25093 struct glyph_string *h, *t;
25094 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25095 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25096 bool check_mouse_face = false;
25097 int dummy_x = 0;
25098
25099 /* If mouse highlighting is on, we may need to draw adjacent
25100 glyphs using mouse-face highlighting. */
25101 if (area == TEXT_AREA && row->mouse_face_p
25102 && hlinfo->mouse_face_beg_row >= 0
25103 && hlinfo->mouse_face_end_row >= 0)
25104 {
25105 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25106
25107 if (row_vpos >= hlinfo->mouse_face_beg_row
25108 && row_vpos <= hlinfo->mouse_face_end_row)
25109 {
25110 check_mouse_face = true;
25111 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25112 ? hlinfo->mouse_face_beg_col : 0;
25113 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25114 ? hlinfo->mouse_face_end_col
25115 : row->used[TEXT_AREA];
25116 }
25117 }
25118
25119 /* Compute overhangs for all glyph strings. */
25120 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25121 for (s = head; s; s = s->next)
25122 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25123
25124 /* Prepend glyph strings for glyphs in front of the first glyph
25125 string that are overwritten because of the first glyph
25126 string's left overhang. The background of all strings
25127 prepended must be drawn because the first glyph string
25128 draws over it. */
25129 i = left_overwritten (head);
25130 if (i >= 0)
25131 {
25132 enum draw_glyphs_face overlap_hl;
25133
25134 /* If this row contains mouse highlighting, attempt to draw
25135 the overlapped glyphs with the correct highlight. This
25136 code fails if the overlap encompasses more than one glyph
25137 and mouse-highlight spans only some of these glyphs.
25138 However, making it work perfectly involves a lot more
25139 code, and I don't know if the pathological case occurs in
25140 practice, so we'll stick to this for now. --- cyd */
25141 if (check_mouse_face
25142 && mouse_beg_col < start && mouse_end_col > i)
25143 overlap_hl = DRAW_MOUSE_FACE;
25144 else
25145 overlap_hl = DRAW_NORMAL_TEXT;
25146
25147 if (hl != overlap_hl)
25148 clip_head = head;
25149 j = i;
25150 BUILD_GLYPH_STRINGS (j, start, h, t,
25151 overlap_hl, dummy_x, last_x);
25152 start = i;
25153 compute_overhangs_and_x (t, head->x, true);
25154 prepend_glyph_string_lists (&head, &tail, h, t);
25155 if (clip_head == NULL)
25156 clip_head = head;
25157 }
25158
25159 /* Prepend glyph strings for glyphs in front of the first glyph
25160 string that overwrite that glyph string because of their
25161 right overhang. For these strings, only the foreground must
25162 be drawn, because it draws over the glyph string at `head'.
25163 The background must not be drawn because this would overwrite
25164 right overhangs of preceding glyphs for which no glyph
25165 strings exist. */
25166 i = left_overwriting (head);
25167 if (i >= 0)
25168 {
25169 enum draw_glyphs_face overlap_hl;
25170
25171 if (check_mouse_face
25172 && mouse_beg_col < start && mouse_end_col > i)
25173 overlap_hl = DRAW_MOUSE_FACE;
25174 else
25175 overlap_hl = DRAW_NORMAL_TEXT;
25176
25177 if (hl == overlap_hl || clip_head == NULL)
25178 clip_head = head;
25179 BUILD_GLYPH_STRINGS (i, start, h, t,
25180 overlap_hl, dummy_x, last_x);
25181 for (s = h; s; s = s->next)
25182 s->background_filled_p = true;
25183 compute_overhangs_and_x (t, head->x, true);
25184 prepend_glyph_string_lists (&head, &tail, h, t);
25185 }
25186
25187 /* Append glyphs strings for glyphs following the last glyph
25188 string tail that are overwritten by tail. The background of
25189 these strings has to be drawn because tail's foreground draws
25190 over it. */
25191 i = right_overwritten (tail);
25192 if (i >= 0)
25193 {
25194 enum draw_glyphs_face overlap_hl;
25195
25196 if (check_mouse_face
25197 && mouse_beg_col < i && mouse_end_col > end)
25198 overlap_hl = DRAW_MOUSE_FACE;
25199 else
25200 overlap_hl = DRAW_NORMAL_TEXT;
25201
25202 if (hl != overlap_hl)
25203 clip_tail = tail;
25204 BUILD_GLYPH_STRINGS (end, i, h, t,
25205 overlap_hl, x, last_x);
25206 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25207 we don't have `end = i;' here. */
25208 compute_overhangs_and_x (h, tail->x + tail->width, false);
25209 append_glyph_string_lists (&head, &tail, h, t);
25210 if (clip_tail == NULL)
25211 clip_tail = tail;
25212 }
25213
25214 /* Append glyph strings for glyphs following the last glyph
25215 string tail that overwrite tail. The foreground of such
25216 glyphs has to be drawn because it writes into the background
25217 of tail. The background must not be drawn because it could
25218 paint over the foreground of following glyphs. */
25219 i = right_overwriting (tail);
25220 if (i >= 0)
25221 {
25222 enum draw_glyphs_face overlap_hl;
25223 if (check_mouse_face
25224 && mouse_beg_col < i && mouse_end_col > end)
25225 overlap_hl = DRAW_MOUSE_FACE;
25226 else
25227 overlap_hl = DRAW_NORMAL_TEXT;
25228
25229 if (hl == overlap_hl || clip_tail == NULL)
25230 clip_tail = tail;
25231 i++; /* We must include the Ith glyph. */
25232 BUILD_GLYPH_STRINGS (end, i, h, t,
25233 overlap_hl, x, last_x);
25234 for (s = h; s; s = s->next)
25235 s->background_filled_p = true;
25236 compute_overhangs_and_x (h, tail->x + tail->width, false);
25237 append_glyph_string_lists (&head, &tail, h, t);
25238 }
25239 if (clip_head || clip_tail)
25240 for (s = head; s; s = s->next)
25241 {
25242 s->clip_head = clip_head;
25243 s->clip_tail = clip_tail;
25244 }
25245 }
25246
25247 /* Draw all strings. */
25248 for (s = head; s; s = s->next)
25249 FRAME_RIF (f)->draw_glyph_string (s);
25250
25251 #ifndef HAVE_NS
25252 /* When focus a sole frame and move horizontally, this clears on_p
25253 causing a failure to erase prev cursor position. */
25254 if (area == TEXT_AREA
25255 && !row->full_width_p
25256 /* When drawing overlapping rows, only the glyph strings'
25257 foreground is drawn, which doesn't erase a cursor
25258 completely. */
25259 && !overlaps)
25260 {
25261 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25262 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25263 : (tail ? tail->x + tail->background_width : x));
25264 x0 -= area_left;
25265 x1 -= area_left;
25266
25267 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25268 row->y, MATRIX_ROW_BOTTOM_Y (row));
25269 }
25270 #endif
25271
25272 /* Value is the x-position up to which drawn, relative to AREA of W.
25273 This doesn't include parts drawn because of overhangs. */
25274 if (row->full_width_p)
25275 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25276 else
25277 x_reached -= area_left;
25278
25279 RELEASE_HDC (hdc, f);
25280
25281 SAFE_FREE ();
25282 return x_reached;
25283 }
25284
25285 /* Expand row matrix if too narrow. Don't expand if area
25286 is not present. */
25287
25288 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25289 { \
25290 if (!it->f->fonts_changed \
25291 && (it->glyph_row->glyphs[area] \
25292 < it->glyph_row->glyphs[area + 1])) \
25293 { \
25294 it->w->ncols_scale_factor++; \
25295 it->f->fonts_changed = true; \
25296 } \
25297 }
25298
25299 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25300 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25301
25302 static void
25303 append_glyph (struct it *it)
25304 {
25305 struct glyph *glyph;
25306 enum glyph_row_area area = it->area;
25307
25308 eassert (it->glyph_row);
25309 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25310
25311 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25312 if (glyph < it->glyph_row->glyphs[area + 1])
25313 {
25314 /* If the glyph row is reversed, we need to prepend the glyph
25315 rather than append it. */
25316 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25317 {
25318 struct glyph *g;
25319
25320 /* Make room for the additional glyph. */
25321 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25322 g[1] = *g;
25323 glyph = it->glyph_row->glyphs[area];
25324 }
25325 glyph->charpos = CHARPOS (it->position);
25326 glyph->object = it->object;
25327 if (it->pixel_width > 0)
25328 {
25329 glyph->pixel_width = it->pixel_width;
25330 glyph->padding_p = false;
25331 }
25332 else
25333 {
25334 /* Assure at least 1-pixel width. Otherwise, cursor can't
25335 be displayed correctly. */
25336 glyph->pixel_width = 1;
25337 glyph->padding_p = true;
25338 }
25339 glyph->ascent = it->ascent;
25340 glyph->descent = it->descent;
25341 glyph->voffset = it->voffset;
25342 glyph->type = CHAR_GLYPH;
25343 glyph->avoid_cursor_p = it->avoid_cursor_p;
25344 glyph->multibyte_p = it->multibyte_p;
25345 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25346 {
25347 /* In R2L rows, the left and the right box edges need to be
25348 drawn in reverse direction. */
25349 glyph->right_box_line_p = it->start_of_box_run_p;
25350 glyph->left_box_line_p = it->end_of_box_run_p;
25351 }
25352 else
25353 {
25354 glyph->left_box_line_p = it->start_of_box_run_p;
25355 glyph->right_box_line_p = it->end_of_box_run_p;
25356 }
25357 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25358 || it->phys_descent > it->descent);
25359 glyph->glyph_not_available_p = it->glyph_not_available_p;
25360 glyph->face_id = it->face_id;
25361 glyph->u.ch = it->char_to_display;
25362 glyph->slice.img = null_glyph_slice;
25363 glyph->font_type = FONT_TYPE_UNKNOWN;
25364 if (it->bidi_p)
25365 {
25366 glyph->resolved_level = it->bidi_it.resolved_level;
25367 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25368 glyph->bidi_type = it->bidi_it.type;
25369 }
25370 else
25371 {
25372 glyph->resolved_level = 0;
25373 glyph->bidi_type = UNKNOWN_BT;
25374 }
25375 ++it->glyph_row->used[area];
25376 }
25377 else
25378 IT_EXPAND_MATRIX_WIDTH (it, area);
25379 }
25380
25381 /* Store one glyph for the composition IT->cmp_it.id in
25382 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25383 non-null. */
25384
25385 static void
25386 append_composite_glyph (struct it *it)
25387 {
25388 struct glyph *glyph;
25389 enum glyph_row_area area = it->area;
25390
25391 eassert (it->glyph_row);
25392
25393 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25394 if (glyph < it->glyph_row->glyphs[area + 1])
25395 {
25396 /* If the glyph row is reversed, we need to prepend the glyph
25397 rather than append it. */
25398 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25399 {
25400 struct glyph *g;
25401
25402 /* Make room for the new glyph. */
25403 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25404 g[1] = *g;
25405 glyph = it->glyph_row->glyphs[it->area];
25406 }
25407 glyph->charpos = it->cmp_it.charpos;
25408 glyph->object = it->object;
25409 glyph->pixel_width = it->pixel_width;
25410 glyph->ascent = it->ascent;
25411 glyph->descent = it->descent;
25412 glyph->voffset = it->voffset;
25413 glyph->type = COMPOSITE_GLYPH;
25414 if (it->cmp_it.ch < 0)
25415 {
25416 glyph->u.cmp.automatic = false;
25417 glyph->u.cmp.id = it->cmp_it.id;
25418 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25419 }
25420 else
25421 {
25422 glyph->u.cmp.automatic = true;
25423 glyph->u.cmp.id = it->cmp_it.id;
25424 glyph->slice.cmp.from = it->cmp_it.from;
25425 glyph->slice.cmp.to = it->cmp_it.to - 1;
25426 }
25427 glyph->avoid_cursor_p = it->avoid_cursor_p;
25428 glyph->multibyte_p = it->multibyte_p;
25429 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25430 {
25431 /* In R2L rows, the left and the right box edges need to be
25432 drawn in reverse direction. */
25433 glyph->right_box_line_p = it->start_of_box_run_p;
25434 glyph->left_box_line_p = it->end_of_box_run_p;
25435 }
25436 else
25437 {
25438 glyph->left_box_line_p = it->start_of_box_run_p;
25439 glyph->right_box_line_p = it->end_of_box_run_p;
25440 }
25441 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25442 || it->phys_descent > it->descent);
25443 glyph->padding_p = false;
25444 glyph->glyph_not_available_p = false;
25445 glyph->face_id = it->face_id;
25446 glyph->font_type = FONT_TYPE_UNKNOWN;
25447 if (it->bidi_p)
25448 {
25449 glyph->resolved_level = it->bidi_it.resolved_level;
25450 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25451 glyph->bidi_type = it->bidi_it.type;
25452 }
25453 ++it->glyph_row->used[area];
25454 }
25455 else
25456 IT_EXPAND_MATRIX_WIDTH (it, area);
25457 }
25458
25459
25460 /* Change IT->ascent and IT->height according to the setting of
25461 IT->voffset. */
25462
25463 static void
25464 take_vertical_position_into_account (struct it *it)
25465 {
25466 if (it->voffset)
25467 {
25468 if (it->voffset < 0)
25469 /* Increase the ascent so that we can display the text higher
25470 in the line. */
25471 it->ascent -= it->voffset;
25472 else
25473 /* Increase the descent so that we can display the text lower
25474 in the line. */
25475 it->descent += it->voffset;
25476 }
25477 }
25478
25479
25480 /* Produce glyphs/get display metrics for the image IT is loaded with.
25481 See the description of struct display_iterator in dispextern.h for
25482 an overview of struct display_iterator. */
25483
25484 static void
25485 produce_image_glyph (struct it *it)
25486 {
25487 struct image *img;
25488 struct face *face;
25489 int glyph_ascent, crop;
25490 struct glyph_slice slice;
25491
25492 eassert (it->what == IT_IMAGE);
25493
25494 face = FACE_FROM_ID (it->f, it->face_id);
25495 eassert (face);
25496 /* Make sure X resources of the face is loaded. */
25497 prepare_face_for_display (it->f, face);
25498
25499 if (it->image_id < 0)
25500 {
25501 /* Fringe bitmap. */
25502 it->ascent = it->phys_ascent = 0;
25503 it->descent = it->phys_descent = 0;
25504 it->pixel_width = 0;
25505 it->nglyphs = 0;
25506 return;
25507 }
25508
25509 img = IMAGE_FROM_ID (it->f, it->image_id);
25510 eassert (img);
25511 /* Make sure X resources of the image is loaded. */
25512 prepare_image_for_display (it->f, img);
25513
25514 slice.x = slice.y = 0;
25515 slice.width = img->width;
25516 slice.height = img->height;
25517
25518 if (INTEGERP (it->slice.x))
25519 slice.x = XINT (it->slice.x);
25520 else if (FLOATP (it->slice.x))
25521 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25522
25523 if (INTEGERP (it->slice.y))
25524 slice.y = XINT (it->slice.y);
25525 else if (FLOATP (it->slice.y))
25526 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25527
25528 if (INTEGERP (it->slice.width))
25529 slice.width = XINT (it->slice.width);
25530 else if (FLOATP (it->slice.width))
25531 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25532
25533 if (INTEGERP (it->slice.height))
25534 slice.height = XINT (it->slice.height);
25535 else if (FLOATP (it->slice.height))
25536 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25537
25538 if (slice.x >= img->width)
25539 slice.x = img->width;
25540 if (slice.y >= img->height)
25541 slice.y = img->height;
25542 if (slice.x + slice.width >= img->width)
25543 slice.width = img->width - slice.x;
25544 if (slice.y + slice.height > img->height)
25545 slice.height = img->height - slice.y;
25546
25547 if (slice.width == 0 || slice.height == 0)
25548 return;
25549
25550 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25551
25552 it->descent = slice.height - glyph_ascent;
25553 if (slice.y == 0)
25554 it->descent += img->vmargin;
25555 if (slice.y + slice.height == img->height)
25556 it->descent += img->vmargin;
25557 it->phys_descent = it->descent;
25558
25559 it->pixel_width = slice.width;
25560 if (slice.x == 0)
25561 it->pixel_width += img->hmargin;
25562 if (slice.x + slice.width == img->width)
25563 it->pixel_width += img->hmargin;
25564
25565 /* It's quite possible for images to have an ascent greater than
25566 their height, so don't get confused in that case. */
25567 if (it->descent < 0)
25568 it->descent = 0;
25569
25570 it->nglyphs = 1;
25571
25572 if (face->box != FACE_NO_BOX)
25573 {
25574 if (face->box_line_width > 0)
25575 {
25576 if (slice.y == 0)
25577 it->ascent += face->box_line_width;
25578 if (slice.y + slice.height == img->height)
25579 it->descent += face->box_line_width;
25580 }
25581
25582 if (it->start_of_box_run_p && slice.x == 0)
25583 it->pixel_width += eabs (face->box_line_width);
25584 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25585 it->pixel_width += eabs (face->box_line_width);
25586 }
25587
25588 take_vertical_position_into_account (it);
25589
25590 /* Automatically crop wide image glyphs at right edge so we can
25591 draw the cursor on same display row. */
25592 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25593 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25594 {
25595 it->pixel_width -= crop;
25596 slice.width -= crop;
25597 }
25598
25599 if (it->glyph_row)
25600 {
25601 struct glyph *glyph;
25602 enum glyph_row_area area = it->area;
25603
25604 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25605 if (it->glyph_row->reversed_p)
25606 {
25607 struct glyph *g;
25608
25609 /* Make room for the new glyph. */
25610 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25611 g[1] = *g;
25612 glyph = it->glyph_row->glyphs[it->area];
25613 }
25614 if (glyph < it->glyph_row->glyphs[area + 1])
25615 {
25616 glyph->charpos = CHARPOS (it->position);
25617 glyph->object = it->object;
25618 glyph->pixel_width = it->pixel_width;
25619 glyph->ascent = glyph_ascent;
25620 glyph->descent = it->descent;
25621 glyph->voffset = it->voffset;
25622 glyph->type = IMAGE_GLYPH;
25623 glyph->avoid_cursor_p = it->avoid_cursor_p;
25624 glyph->multibyte_p = it->multibyte_p;
25625 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25626 {
25627 /* In R2L rows, the left and the right box edges need to be
25628 drawn in reverse direction. */
25629 glyph->right_box_line_p = it->start_of_box_run_p;
25630 glyph->left_box_line_p = it->end_of_box_run_p;
25631 }
25632 else
25633 {
25634 glyph->left_box_line_p = it->start_of_box_run_p;
25635 glyph->right_box_line_p = it->end_of_box_run_p;
25636 }
25637 glyph->overlaps_vertically_p = false;
25638 glyph->padding_p = false;
25639 glyph->glyph_not_available_p = false;
25640 glyph->face_id = it->face_id;
25641 glyph->u.img_id = img->id;
25642 glyph->slice.img = slice;
25643 glyph->font_type = FONT_TYPE_UNKNOWN;
25644 if (it->bidi_p)
25645 {
25646 glyph->resolved_level = it->bidi_it.resolved_level;
25647 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25648 glyph->bidi_type = it->bidi_it.type;
25649 }
25650 ++it->glyph_row->used[area];
25651 }
25652 else
25653 IT_EXPAND_MATRIX_WIDTH (it, area);
25654 }
25655 }
25656
25657
25658 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25659 of the glyph, WIDTH and HEIGHT are the width and height of the
25660 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25661
25662 static void
25663 append_stretch_glyph (struct it *it, Lisp_Object object,
25664 int width, int height, int ascent)
25665 {
25666 struct glyph *glyph;
25667 enum glyph_row_area area = it->area;
25668
25669 eassert (ascent >= 0 && ascent <= height);
25670
25671 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25672 if (glyph < it->glyph_row->glyphs[area + 1])
25673 {
25674 /* If the glyph row is reversed, we need to prepend the glyph
25675 rather than append it. */
25676 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25677 {
25678 struct glyph *g;
25679
25680 /* Make room for the additional glyph. */
25681 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25682 g[1] = *g;
25683 glyph = it->glyph_row->glyphs[area];
25684
25685 /* Decrease the width of the first glyph of the row that
25686 begins before first_visible_x (e.g., due to hscroll).
25687 This is so the overall width of the row becomes smaller
25688 by the scroll amount, and the stretch glyph appended by
25689 extend_face_to_end_of_line will be wider, to shift the
25690 row glyphs to the right. (In L2R rows, the corresponding
25691 left-shift effect is accomplished by setting row->x to a
25692 negative value, which won't work with R2L rows.)
25693
25694 This must leave us with a positive value of WIDTH, since
25695 otherwise the call to move_it_in_display_line_to at the
25696 beginning of display_line would have got past the entire
25697 first glyph, and then it->current_x would have been
25698 greater or equal to it->first_visible_x. */
25699 if (it->current_x < it->first_visible_x)
25700 width -= it->first_visible_x - it->current_x;
25701 eassert (width > 0);
25702 }
25703 glyph->charpos = CHARPOS (it->position);
25704 glyph->object = object;
25705 glyph->pixel_width = width;
25706 glyph->ascent = ascent;
25707 glyph->descent = height - ascent;
25708 glyph->voffset = it->voffset;
25709 glyph->type = STRETCH_GLYPH;
25710 glyph->avoid_cursor_p = it->avoid_cursor_p;
25711 glyph->multibyte_p = it->multibyte_p;
25712 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25713 {
25714 /* In R2L rows, the left and the right box edges need to be
25715 drawn in reverse direction. */
25716 glyph->right_box_line_p = it->start_of_box_run_p;
25717 glyph->left_box_line_p = it->end_of_box_run_p;
25718 }
25719 else
25720 {
25721 glyph->left_box_line_p = it->start_of_box_run_p;
25722 glyph->right_box_line_p = it->end_of_box_run_p;
25723 }
25724 glyph->overlaps_vertically_p = false;
25725 glyph->padding_p = false;
25726 glyph->glyph_not_available_p = false;
25727 glyph->face_id = it->face_id;
25728 glyph->u.stretch.ascent = ascent;
25729 glyph->u.stretch.height = height;
25730 glyph->slice.img = null_glyph_slice;
25731 glyph->font_type = FONT_TYPE_UNKNOWN;
25732 if (it->bidi_p)
25733 {
25734 glyph->resolved_level = it->bidi_it.resolved_level;
25735 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25736 glyph->bidi_type = it->bidi_it.type;
25737 }
25738 else
25739 {
25740 glyph->resolved_level = 0;
25741 glyph->bidi_type = UNKNOWN_BT;
25742 }
25743 ++it->glyph_row->used[area];
25744 }
25745 else
25746 IT_EXPAND_MATRIX_WIDTH (it, area);
25747 }
25748
25749 #endif /* HAVE_WINDOW_SYSTEM */
25750
25751 /* Produce a stretch glyph for iterator IT. IT->object is the value
25752 of the glyph property displayed. The value must be a list
25753 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25754 being recognized:
25755
25756 1. `:width WIDTH' specifies that the space should be WIDTH *
25757 canonical char width wide. WIDTH may be an integer or floating
25758 point number.
25759
25760 2. `:relative-width FACTOR' specifies that the width of the stretch
25761 should be computed from the width of the first character having the
25762 `glyph' property, and should be FACTOR times that width.
25763
25764 3. `:align-to HPOS' specifies that the space should be wide enough
25765 to reach HPOS, a value in canonical character units.
25766
25767 Exactly one of the above pairs must be present.
25768
25769 4. `:height HEIGHT' specifies that the height of the stretch produced
25770 should be HEIGHT, measured in canonical character units.
25771
25772 5. `:relative-height FACTOR' specifies that the height of the
25773 stretch should be FACTOR times the height of the characters having
25774 the glyph property.
25775
25776 Either none or exactly one of 4 or 5 must be present.
25777
25778 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25779 of the stretch should be used for the ascent of the stretch.
25780 ASCENT must be in the range 0 <= ASCENT <= 100. */
25781
25782 void
25783 produce_stretch_glyph (struct it *it)
25784 {
25785 /* (space :width WIDTH :height HEIGHT ...) */
25786 Lisp_Object prop, plist;
25787 int width = 0, height = 0, align_to = -1;
25788 bool zero_width_ok_p = false;
25789 double tem;
25790 struct font *font = NULL;
25791
25792 #ifdef HAVE_WINDOW_SYSTEM
25793 int ascent = 0;
25794 bool zero_height_ok_p = false;
25795
25796 if (FRAME_WINDOW_P (it->f))
25797 {
25798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25799 font = face->font ? face->font : FRAME_FONT (it->f);
25800 prepare_face_for_display (it->f, face);
25801 }
25802 #endif
25803
25804 /* List should start with `space'. */
25805 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25806 plist = XCDR (it->object);
25807
25808 /* Compute the width of the stretch. */
25809 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25810 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
25811 {
25812 /* Absolute width `:width WIDTH' specified and valid. */
25813 zero_width_ok_p = true;
25814 width = (int)tem;
25815 }
25816 #ifdef HAVE_WINDOW_SYSTEM
25817 else if (FRAME_WINDOW_P (it->f)
25818 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25819 {
25820 /* Relative width `:relative-width FACTOR' specified and valid.
25821 Compute the width of the characters having the `glyph'
25822 property. */
25823 struct it it2;
25824 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25825
25826 it2 = *it;
25827 if (it->multibyte_p)
25828 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25829 else
25830 {
25831 it2.c = it2.char_to_display = *p, it2.len = 1;
25832 if (! ASCII_CHAR_P (it2.c))
25833 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25834 }
25835
25836 it2.glyph_row = NULL;
25837 it2.what = IT_CHARACTER;
25838 x_produce_glyphs (&it2);
25839 width = NUMVAL (prop) * it2.pixel_width;
25840 }
25841 #endif /* HAVE_WINDOW_SYSTEM */
25842 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25843 && calc_pixel_width_or_height (&tem, it, prop, font, true,
25844 &align_to))
25845 {
25846 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25847 align_to = (align_to < 0
25848 ? 0
25849 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25850 else if (align_to < 0)
25851 align_to = window_box_left_offset (it->w, TEXT_AREA);
25852 width = max (0, (int)tem + align_to - it->current_x);
25853 zero_width_ok_p = true;
25854 }
25855 else
25856 /* Nothing specified -> width defaults to canonical char width. */
25857 width = FRAME_COLUMN_WIDTH (it->f);
25858
25859 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25860 width = 1;
25861
25862 #ifdef HAVE_WINDOW_SYSTEM
25863 /* Compute height. */
25864 if (FRAME_WINDOW_P (it->f))
25865 {
25866 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25867 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25868 {
25869 height = (int)tem;
25870 zero_height_ok_p = true;
25871 }
25872 else if (prop = Fplist_get (plist, QCrelative_height),
25873 NUMVAL (prop) > 0)
25874 height = FONT_HEIGHT (font) * NUMVAL (prop);
25875 else
25876 height = FONT_HEIGHT (font);
25877
25878 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25879 height = 1;
25880
25881 /* Compute percentage of height used for ascent. If
25882 `:ascent ASCENT' is present and valid, use that. Otherwise,
25883 derive the ascent from the font in use. */
25884 if (prop = Fplist_get (plist, QCascent),
25885 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25886 ascent = height * NUMVAL (prop) / 100.0;
25887 else if (!NILP (prop)
25888 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25889 ascent = min (max (0, (int)tem), height);
25890 else
25891 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25892 }
25893 else
25894 #endif /* HAVE_WINDOW_SYSTEM */
25895 height = 1;
25896
25897 if (width > 0 && it->line_wrap != TRUNCATE
25898 && it->current_x + width > it->last_visible_x)
25899 {
25900 width = it->last_visible_x - it->current_x;
25901 #ifdef HAVE_WINDOW_SYSTEM
25902 /* Subtract one more pixel from the stretch width, but only on
25903 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25904 width -= FRAME_WINDOW_P (it->f);
25905 #endif
25906 }
25907
25908 if (width > 0 && height > 0 && it->glyph_row)
25909 {
25910 Lisp_Object o_object = it->object;
25911 Lisp_Object object = it->stack[it->sp - 1].string;
25912 int n = width;
25913
25914 if (!STRINGP (object))
25915 object = it->w->contents;
25916 #ifdef HAVE_WINDOW_SYSTEM
25917 if (FRAME_WINDOW_P (it->f))
25918 append_stretch_glyph (it, object, width, height, ascent);
25919 else
25920 #endif
25921 {
25922 it->object = object;
25923 it->char_to_display = ' ';
25924 it->pixel_width = it->len = 1;
25925 while (n--)
25926 tty_append_glyph (it);
25927 it->object = o_object;
25928 }
25929 }
25930
25931 it->pixel_width = width;
25932 #ifdef HAVE_WINDOW_SYSTEM
25933 if (FRAME_WINDOW_P (it->f))
25934 {
25935 it->ascent = it->phys_ascent = ascent;
25936 it->descent = it->phys_descent = height - it->ascent;
25937 it->nglyphs = width > 0 && height > 0;
25938 take_vertical_position_into_account (it);
25939 }
25940 else
25941 #endif
25942 it->nglyphs = width;
25943 }
25944
25945 /* Get information about special display element WHAT in an
25946 environment described by IT. WHAT is one of IT_TRUNCATION or
25947 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25948 non-null glyph_row member. This function ensures that fields like
25949 face_id, c, len of IT are left untouched. */
25950
25951 static void
25952 produce_special_glyphs (struct it *it, enum display_element_type what)
25953 {
25954 struct it temp_it;
25955 Lisp_Object gc;
25956 GLYPH glyph;
25957
25958 temp_it = *it;
25959 temp_it.object = Qnil;
25960 memset (&temp_it.current, 0, sizeof temp_it.current);
25961
25962 if (what == IT_CONTINUATION)
25963 {
25964 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25965 if (it->bidi_it.paragraph_dir == R2L)
25966 SET_GLYPH_FROM_CHAR (glyph, '/');
25967 else
25968 SET_GLYPH_FROM_CHAR (glyph, '\\');
25969 if (it->dp
25970 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25971 {
25972 /* FIXME: Should we mirror GC for R2L lines? */
25973 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25974 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25975 }
25976 }
25977 else if (what == IT_TRUNCATION)
25978 {
25979 /* Truncation glyph. */
25980 SET_GLYPH_FROM_CHAR (glyph, '$');
25981 if (it->dp
25982 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25983 {
25984 /* FIXME: Should we mirror GC for R2L lines? */
25985 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25986 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25987 }
25988 }
25989 else
25990 emacs_abort ();
25991
25992 #ifdef HAVE_WINDOW_SYSTEM
25993 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25994 is turned off, we precede the truncation/continuation glyphs by a
25995 stretch glyph whose width is computed such that these special
25996 glyphs are aligned at the window margin, even when very different
25997 fonts are used in different glyph rows. */
25998 if (FRAME_WINDOW_P (temp_it.f)
25999 /* init_iterator calls this with it->glyph_row == NULL, and it
26000 wants only the pixel width of the truncation/continuation
26001 glyphs. */
26002 && temp_it.glyph_row
26003 /* insert_left_trunc_glyphs calls us at the beginning of the
26004 row, and it has its own calculation of the stretch glyph
26005 width. */
26006 && temp_it.glyph_row->used[TEXT_AREA] > 0
26007 && (temp_it.glyph_row->reversed_p
26008 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26009 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26010 {
26011 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26012
26013 if (stretch_width > 0)
26014 {
26015 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26016 struct font *font =
26017 face->font ? face->font : FRAME_FONT (temp_it.f);
26018 int stretch_ascent =
26019 (((temp_it.ascent + temp_it.descent)
26020 * FONT_BASE (font)) / FONT_HEIGHT (font));
26021
26022 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26023 temp_it.ascent + temp_it.descent,
26024 stretch_ascent);
26025 }
26026 }
26027 #endif
26028
26029 temp_it.dp = NULL;
26030 temp_it.what = IT_CHARACTER;
26031 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26032 temp_it.face_id = GLYPH_FACE (glyph);
26033 temp_it.len = CHAR_BYTES (temp_it.c);
26034
26035 PRODUCE_GLYPHS (&temp_it);
26036 it->pixel_width = temp_it.pixel_width;
26037 it->nglyphs = temp_it.nglyphs;
26038 }
26039
26040 #ifdef HAVE_WINDOW_SYSTEM
26041
26042 /* Calculate line-height and line-spacing properties.
26043 An integer value specifies explicit pixel value.
26044 A float value specifies relative value to current face height.
26045 A cons (float . face-name) specifies relative value to
26046 height of specified face font.
26047
26048 Returns height in pixels, or nil. */
26049
26050 static Lisp_Object
26051 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26052 int boff, bool override)
26053 {
26054 Lisp_Object face_name = Qnil;
26055 int ascent, descent, height;
26056
26057 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26058 return val;
26059
26060 if (CONSP (val))
26061 {
26062 face_name = XCAR (val);
26063 val = XCDR (val);
26064 if (!NUMBERP (val))
26065 val = make_number (1);
26066 if (NILP (face_name))
26067 {
26068 height = it->ascent + it->descent;
26069 goto scale;
26070 }
26071 }
26072
26073 if (NILP (face_name))
26074 {
26075 font = FRAME_FONT (it->f);
26076 boff = FRAME_BASELINE_OFFSET (it->f);
26077 }
26078 else if (EQ (face_name, Qt))
26079 {
26080 override = false;
26081 }
26082 else
26083 {
26084 int face_id;
26085 struct face *face;
26086
26087 face_id = lookup_named_face (it->f, face_name, false);
26088 if (face_id < 0)
26089 return make_number (-1);
26090
26091 face = FACE_FROM_ID (it->f, face_id);
26092 font = face->font;
26093 if (font == NULL)
26094 return make_number (-1);
26095 boff = font->baseline_offset;
26096 if (font->vertical_centering)
26097 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26098 }
26099
26100 ascent = FONT_BASE (font) + boff;
26101 descent = FONT_DESCENT (font) - boff;
26102
26103 if (override)
26104 {
26105 it->override_ascent = ascent;
26106 it->override_descent = descent;
26107 it->override_boff = boff;
26108 }
26109
26110 height = ascent + descent;
26111
26112 scale:
26113 if (FLOATP (val))
26114 height = (int)(XFLOAT_DATA (val) * height);
26115 else if (INTEGERP (val))
26116 height *= XINT (val);
26117
26118 return make_number (height);
26119 }
26120
26121
26122 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26123 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26124 and only if this is for a character for which no font was found.
26125
26126 If the display method (it->glyphless_method) is
26127 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26128 length of the acronym or the hexadecimal string, UPPER_XOFF and
26129 UPPER_YOFF are pixel offsets for the upper part of the string,
26130 LOWER_XOFF and LOWER_YOFF are for the lower part.
26131
26132 For the other display methods, LEN through LOWER_YOFF are zero. */
26133
26134 static void
26135 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26136 short upper_xoff, short upper_yoff,
26137 short lower_xoff, short lower_yoff)
26138 {
26139 struct glyph *glyph;
26140 enum glyph_row_area area = it->area;
26141
26142 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26143 if (glyph < it->glyph_row->glyphs[area + 1])
26144 {
26145 /* If the glyph row is reversed, we need to prepend the glyph
26146 rather than append it. */
26147 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26148 {
26149 struct glyph *g;
26150
26151 /* Make room for the additional glyph. */
26152 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26153 g[1] = *g;
26154 glyph = it->glyph_row->glyphs[area];
26155 }
26156 glyph->charpos = CHARPOS (it->position);
26157 glyph->object = it->object;
26158 glyph->pixel_width = it->pixel_width;
26159 glyph->ascent = it->ascent;
26160 glyph->descent = it->descent;
26161 glyph->voffset = it->voffset;
26162 glyph->type = GLYPHLESS_GLYPH;
26163 glyph->u.glyphless.method = it->glyphless_method;
26164 glyph->u.glyphless.for_no_font = for_no_font;
26165 glyph->u.glyphless.len = len;
26166 glyph->u.glyphless.ch = it->c;
26167 glyph->slice.glyphless.upper_xoff = upper_xoff;
26168 glyph->slice.glyphless.upper_yoff = upper_yoff;
26169 glyph->slice.glyphless.lower_xoff = lower_xoff;
26170 glyph->slice.glyphless.lower_yoff = lower_yoff;
26171 glyph->avoid_cursor_p = it->avoid_cursor_p;
26172 glyph->multibyte_p = it->multibyte_p;
26173 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26174 {
26175 /* In R2L rows, the left and the right box edges need to be
26176 drawn in reverse direction. */
26177 glyph->right_box_line_p = it->start_of_box_run_p;
26178 glyph->left_box_line_p = it->end_of_box_run_p;
26179 }
26180 else
26181 {
26182 glyph->left_box_line_p = it->start_of_box_run_p;
26183 glyph->right_box_line_p = it->end_of_box_run_p;
26184 }
26185 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26186 || it->phys_descent > it->descent);
26187 glyph->padding_p = false;
26188 glyph->glyph_not_available_p = false;
26189 glyph->face_id = face_id;
26190 glyph->font_type = FONT_TYPE_UNKNOWN;
26191 if (it->bidi_p)
26192 {
26193 glyph->resolved_level = it->bidi_it.resolved_level;
26194 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26195 glyph->bidi_type = it->bidi_it.type;
26196 }
26197 ++it->glyph_row->used[area];
26198 }
26199 else
26200 IT_EXPAND_MATRIX_WIDTH (it, area);
26201 }
26202
26203
26204 /* Produce a glyph for a glyphless character for iterator IT.
26205 IT->glyphless_method specifies which method to use for displaying
26206 the character. See the description of enum
26207 glyphless_display_method in dispextern.h for the detail.
26208
26209 FOR_NO_FONT is true if and only if this is for a character for
26210 which no font was found. ACRONYM, if non-nil, is an acronym string
26211 for the character. */
26212
26213 static void
26214 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26215 {
26216 int face_id;
26217 struct face *face;
26218 struct font *font;
26219 int base_width, base_height, width, height;
26220 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26221 int len;
26222
26223 /* Get the metrics of the base font. We always refer to the current
26224 ASCII face. */
26225 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26226 font = face->font ? face->font : FRAME_FONT (it->f);
26227 it->ascent = FONT_BASE (font);
26228 it->descent = FONT_DESCENT (font);
26229 /* Attempt to fix box height for fonts that claim preposterously
26230 large height. */
26231 if (FONT_TOO_HIGH (font))
26232 {
26233 XChar2b char2b;
26234
26235 /* Get metrics of a reasonably sized ASCII character. */
26236 if (get_char_glyph_code ('{', font, &char2b))
26237 {
26238 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
26239
26240 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
26241 {
26242 it->ascent = pcm->ascent;
26243 it->descent = pcm->descent;
26244 }
26245 }
26246 }
26247 it->ascent += font->baseline_offset;
26248 it->descent -= font->baseline_offset;
26249 base_height = it->ascent + it->descent;
26250 base_width = font->average_width;
26251
26252 face_id = merge_glyphless_glyph_face (it);
26253
26254 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26255 {
26256 it->pixel_width = THIN_SPACE_WIDTH;
26257 len = 0;
26258 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26259 }
26260 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26261 {
26262 width = CHAR_WIDTH (it->c);
26263 if (width == 0)
26264 width = 1;
26265 else if (width > 4)
26266 width = 4;
26267 it->pixel_width = base_width * width;
26268 len = 0;
26269 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26270 }
26271 else
26272 {
26273 char buf[7];
26274 const char *str;
26275 unsigned int code[6];
26276 int upper_len;
26277 int ascent, descent;
26278 struct font_metrics metrics_upper, metrics_lower;
26279
26280 face = FACE_FROM_ID (it->f, face_id);
26281 font = face->font ? face->font : FRAME_FONT (it->f);
26282 prepare_face_for_display (it->f, face);
26283
26284 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26285 {
26286 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26287 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26288 if (CONSP (acronym))
26289 acronym = XCAR (acronym);
26290 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26291 }
26292 else
26293 {
26294 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26295 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26296 str = buf;
26297 }
26298 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26299 code[len] = font->driver->encode_char (font, str[len]);
26300 upper_len = (len + 1) / 2;
26301 font->driver->text_extents (font, code, upper_len,
26302 &metrics_upper);
26303 font->driver->text_extents (font, code + upper_len, len - upper_len,
26304 &metrics_lower);
26305
26306
26307
26308 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26309 width = max (metrics_upper.width, metrics_lower.width) + 4;
26310 upper_xoff = upper_yoff = 2; /* the typical case */
26311 if (base_width >= width)
26312 {
26313 /* Align the upper to the left, the lower to the right. */
26314 it->pixel_width = base_width;
26315 lower_xoff = base_width - 2 - metrics_lower.width;
26316 }
26317 else
26318 {
26319 /* Center the shorter one. */
26320 it->pixel_width = width;
26321 if (metrics_upper.width >= metrics_lower.width)
26322 lower_xoff = (width - metrics_lower.width) / 2;
26323 else
26324 {
26325 /* FIXME: This code doesn't look right. It formerly was
26326 missing the "lower_xoff = 0;", which couldn't have
26327 been right since it left lower_xoff uninitialized. */
26328 lower_xoff = 0;
26329 upper_xoff = (width - metrics_upper.width) / 2;
26330 }
26331 }
26332
26333 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26334 top, bottom, and between upper and lower strings. */
26335 height = (metrics_upper.ascent + metrics_upper.descent
26336 + metrics_lower.ascent + metrics_lower.descent) + 5;
26337 /* Center vertically.
26338 H:base_height, D:base_descent
26339 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26340
26341 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26342 descent = D - H/2 + h/2;
26343 lower_yoff = descent - 2 - ld;
26344 upper_yoff = lower_yoff - la - 1 - ud; */
26345 ascent = - (it->descent - (base_height + height + 1) / 2);
26346 descent = it->descent - (base_height - height) / 2;
26347 lower_yoff = descent - 2 - metrics_lower.descent;
26348 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26349 - metrics_upper.descent);
26350 /* Don't make the height shorter than the base height. */
26351 if (height > base_height)
26352 {
26353 it->ascent = ascent;
26354 it->descent = descent;
26355 }
26356 }
26357
26358 it->phys_ascent = it->ascent;
26359 it->phys_descent = it->descent;
26360 if (it->glyph_row)
26361 append_glyphless_glyph (it, face_id, for_no_font, len,
26362 upper_xoff, upper_yoff,
26363 lower_xoff, lower_yoff);
26364 it->nglyphs = 1;
26365 take_vertical_position_into_account (it);
26366 }
26367
26368
26369 /* RIF:
26370 Produce glyphs/get display metrics for the display element IT is
26371 loaded with. See the description of struct it in dispextern.h
26372 for an overview of struct it. */
26373
26374 void
26375 x_produce_glyphs (struct it *it)
26376 {
26377 int extra_line_spacing = it->extra_line_spacing;
26378
26379 it->glyph_not_available_p = false;
26380
26381 if (it->what == IT_CHARACTER)
26382 {
26383 XChar2b char2b;
26384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26385 struct font *font = face->font;
26386 struct font_metrics *pcm = NULL;
26387 int boff; /* Baseline offset. */
26388
26389 if (font == NULL)
26390 {
26391 /* When no suitable font is found, display this character by
26392 the method specified in the first extra slot of
26393 Vglyphless_char_display. */
26394 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26395
26396 eassert (it->what == IT_GLYPHLESS);
26397 produce_glyphless_glyph (it, true,
26398 STRINGP (acronym) ? acronym : Qnil);
26399 goto done;
26400 }
26401
26402 boff = font->baseline_offset;
26403 if (font->vertical_centering)
26404 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26405
26406 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26407 {
26408 it->nglyphs = 1;
26409
26410 if (it->override_ascent >= 0)
26411 {
26412 it->ascent = it->override_ascent;
26413 it->descent = it->override_descent;
26414 boff = it->override_boff;
26415 }
26416 else
26417 {
26418 it->ascent = FONT_BASE (font) + boff;
26419 it->descent = FONT_DESCENT (font) - boff;
26420 }
26421
26422 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26423 {
26424 pcm = get_per_char_metric (font, &char2b);
26425 if (pcm->width == 0
26426 && pcm->rbearing == 0 && pcm->lbearing == 0)
26427 pcm = NULL;
26428 }
26429
26430 if (pcm)
26431 {
26432 it->phys_ascent = pcm->ascent + boff;
26433 it->phys_descent = pcm->descent - boff;
26434 it->pixel_width = pcm->width;
26435 /* Don't use font-global values for ascent and descent
26436 if they result in an exceedingly large line height. */
26437 if (it->override_ascent < 0)
26438 {
26439 if (FONT_TOO_HIGH (font))
26440 {
26441 it->ascent = it->phys_ascent;
26442 it->descent = it->phys_descent;
26443 /* These limitations are enforced by an
26444 assertion near the end of this function. */
26445 if (it->ascent < 0)
26446 it->ascent = 0;
26447 if (it->descent < 0)
26448 it->descent = 0;
26449 }
26450 }
26451 }
26452 else
26453 {
26454 it->glyph_not_available_p = true;
26455 it->phys_ascent = it->ascent;
26456 it->phys_descent = it->descent;
26457 it->pixel_width = font->space_width;
26458 }
26459
26460 if (it->constrain_row_ascent_descent_p)
26461 {
26462 if (it->descent > it->max_descent)
26463 {
26464 it->ascent += it->descent - it->max_descent;
26465 it->descent = it->max_descent;
26466 }
26467 if (it->ascent > it->max_ascent)
26468 {
26469 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26470 it->ascent = it->max_ascent;
26471 }
26472 it->phys_ascent = min (it->phys_ascent, it->ascent);
26473 it->phys_descent = min (it->phys_descent, it->descent);
26474 extra_line_spacing = 0;
26475 }
26476
26477 /* If this is a space inside a region of text with
26478 `space-width' property, change its width. */
26479 bool stretched_p
26480 = it->char_to_display == ' ' && !NILP (it->space_width);
26481 if (stretched_p)
26482 it->pixel_width *= XFLOATINT (it->space_width);
26483
26484 /* If face has a box, add the box thickness to the character
26485 height. If character has a box line to the left and/or
26486 right, add the box line width to the character's width. */
26487 if (face->box != FACE_NO_BOX)
26488 {
26489 int thick = face->box_line_width;
26490
26491 if (thick > 0)
26492 {
26493 it->ascent += thick;
26494 it->descent += thick;
26495 }
26496 else
26497 thick = -thick;
26498
26499 if (it->start_of_box_run_p)
26500 it->pixel_width += thick;
26501 if (it->end_of_box_run_p)
26502 it->pixel_width += thick;
26503 }
26504
26505 /* If face has an overline, add the height of the overline
26506 (1 pixel) and a 1 pixel margin to the character height. */
26507 if (face->overline_p)
26508 it->ascent += overline_margin;
26509
26510 if (it->constrain_row_ascent_descent_p)
26511 {
26512 if (it->ascent > it->max_ascent)
26513 it->ascent = it->max_ascent;
26514 if (it->descent > it->max_descent)
26515 it->descent = it->max_descent;
26516 }
26517
26518 take_vertical_position_into_account (it);
26519
26520 /* If we have to actually produce glyphs, do it. */
26521 if (it->glyph_row)
26522 {
26523 if (stretched_p)
26524 {
26525 /* Translate a space with a `space-width' property
26526 into a stretch glyph. */
26527 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26528 / FONT_HEIGHT (font));
26529 append_stretch_glyph (it, it->object, it->pixel_width,
26530 it->ascent + it->descent, ascent);
26531 }
26532 else
26533 append_glyph (it);
26534
26535 /* If characters with lbearing or rbearing are displayed
26536 in this line, record that fact in a flag of the
26537 glyph row. This is used to optimize X output code. */
26538 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26539 it->glyph_row->contains_overlapping_glyphs_p = true;
26540 }
26541 if (! stretched_p && it->pixel_width == 0)
26542 /* We assure that all visible glyphs have at least 1-pixel
26543 width. */
26544 it->pixel_width = 1;
26545 }
26546 else if (it->char_to_display == '\n')
26547 {
26548 /* A newline has no width, but we need the height of the
26549 line. But if previous part of the line sets a height,
26550 don't increase that height. */
26551
26552 Lisp_Object height;
26553 Lisp_Object total_height = Qnil;
26554
26555 it->override_ascent = -1;
26556 it->pixel_width = 0;
26557 it->nglyphs = 0;
26558
26559 height = get_it_property (it, Qline_height);
26560 /* Split (line-height total-height) list. */
26561 if (CONSP (height)
26562 && CONSP (XCDR (height))
26563 && NILP (XCDR (XCDR (height))))
26564 {
26565 total_height = XCAR (XCDR (height));
26566 height = XCAR (height);
26567 }
26568 height = calc_line_height_property (it, height, font, boff, true);
26569
26570 if (it->override_ascent >= 0)
26571 {
26572 it->ascent = it->override_ascent;
26573 it->descent = it->override_descent;
26574 boff = it->override_boff;
26575 }
26576 else
26577 {
26578 if (FONT_TOO_HIGH (font))
26579 {
26580 it->ascent = font->pixel_size + boff - 1;
26581 it->descent = -boff + 1;
26582 if (it->descent < 0)
26583 it->descent = 0;
26584 }
26585 else
26586 {
26587 it->ascent = FONT_BASE (font) + boff;
26588 it->descent = FONT_DESCENT (font) - boff;
26589 }
26590 }
26591
26592 if (EQ (height, Qt))
26593 {
26594 if (it->descent > it->max_descent)
26595 {
26596 it->ascent += it->descent - it->max_descent;
26597 it->descent = it->max_descent;
26598 }
26599 if (it->ascent > it->max_ascent)
26600 {
26601 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26602 it->ascent = it->max_ascent;
26603 }
26604 it->phys_ascent = min (it->phys_ascent, it->ascent);
26605 it->phys_descent = min (it->phys_descent, it->descent);
26606 it->constrain_row_ascent_descent_p = true;
26607 extra_line_spacing = 0;
26608 }
26609 else
26610 {
26611 Lisp_Object spacing;
26612
26613 it->phys_ascent = it->ascent;
26614 it->phys_descent = it->descent;
26615
26616 if ((it->max_ascent > 0 || it->max_descent > 0)
26617 && face->box != FACE_NO_BOX
26618 && face->box_line_width > 0)
26619 {
26620 it->ascent += face->box_line_width;
26621 it->descent += face->box_line_width;
26622 }
26623 if (!NILP (height)
26624 && XINT (height) > it->ascent + it->descent)
26625 it->ascent = XINT (height) - it->descent;
26626
26627 if (!NILP (total_height))
26628 spacing = calc_line_height_property (it, total_height, font,
26629 boff, false);
26630 else
26631 {
26632 spacing = get_it_property (it, Qline_spacing);
26633 spacing = calc_line_height_property (it, spacing, font,
26634 boff, false);
26635 }
26636 if (INTEGERP (spacing))
26637 {
26638 extra_line_spacing = XINT (spacing);
26639 if (!NILP (total_height))
26640 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26641 }
26642 }
26643 }
26644 else /* i.e. (it->char_to_display == '\t') */
26645 {
26646 if (font->space_width > 0)
26647 {
26648 int tab_width = it->tab_width * font->space_width;
26649 int x = it->current_x + it->continuation_lines_width;
26650 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26651
26652 /* If the distance from the current position to the next tab
26653 stop is less than a space character width, use the
26654 tab stop after that. */
26655 if (next_tab_x - x < font->space_width)
26656 next_tab_x += tab_width;
26657
26658 it->pixel_width = next_tab_x - x;
26659 it->nglyphs = 1;
26660 if (FONT_TOO_HIGH (font))
26661 {
26662 if (get_char_glyph_code (' ', font, &char2b))
26663 {
26664 pcm = get_per_char_metric (font, &char2b);
26665 if (pcm->width == 0
26666 && pcm->rbearing == 0 && pcm->lbearing == 0)
26667 pcm = NULL;
26668 }
26669
26670 if (pcm)
26671 {
26672 it->ascent = pcm->ascent + boff;
26673 it->descent = pcm->descent - boff;
26674 }
26675 else
26676 {
26677 it->ascent = font->pixel_size + boff - 1;
26678 it->descent = -boff + 1;
26679 }
26680 if (it->ascent < 0)
26681 it->ascent = 0;
26682 if (it->descent < 0)
26683 it->descent = 0;
26684 }
26685 else
26686 {
26687 it->ascent = FONT_BASE (font) + boff;
26688 it->descent = FONT_DESCENT (font) - boff;
26689 }
26690 it->phys_ascent = it->ascent;
26691 it->phys_descent = it->descent;
26692
26693 if (it->glyph_row)
26694 {
26695 append_stretch_glyph (it, it->object, it->pixel_width,
26696 it->ascent + it->descent, it->ascent);
26697 }
26698 }
26699 else
26700 {
26701 it->pixel_width = 0;
26702 it->nglyphs = 1;
26703 }
26704 }
26705 }
26706 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26707 {
26708 /* A static composition.
26709
26710 Note: A composition is represented as one glyph in the
26711 glyph matrix. There are no padding glyphs.
26712
26713 Important note: pixel_width, ascent, and descent are the
26714 values of what is drawn by draw_glyphs (i.e. the values of
26715 the overall glyphs composed). */
26716 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26717 int boff; /* baseline offset */
26718 struct composition *cmp = composition_table[it->cmp_it.id];
26719 int glyph_len = cmp->glyph_len;
26720 struct font *font = face->font;
26721
26722 it->nglyphs = 1;
26723
26724 /* If we have not yet calculated pixel size data of glyphs of
26725 the composition for the current face font, calculate them
26726 now. Theoretically, we have to check all fonts for the
26727 glyphs, but that requires much time and memory space. So,
26728 here we check only the font of the first glyph. This may
26729 lead to incorrect display, but it's very rare, and C-l
26730 (recenter-top-bottom) can correct the display anyway. */
26731 if (! cmp->font || cmp->font != font)
26732 {
26733 /* Ascent and descent of the font of the first character
26734 of this composition (adjusted by baseline offset).
26735 Ascent and descent of overall glyphs should not be less
26736 than these, respectively. */
26737 int font_ascent, font_descent, font_height;
26738 /* Bounding box of the overall glyphs. */
26739 int leftmost, rightmost, lowest, highest;
26740 int lbearing, rbearing;
26741 int i, width, ascent, descent;
26742 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26743 XChar2b char2b;
26744 struct font_metrics *pcm;
26745 ptrdiff_t pos;
26746
26747 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26748 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26749 break;
26750 bool right_padded = glyph_len < cmp->glyph_len;
26751 for (i = 0; i < glyph_len; i++)
26752 {
26753 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26754 break;
26755 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26756 }
26757 bool left_padded = i > 0;
26758
26759 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26760 : IT_CHARPOS (*it));
26761 /* If no suitable font is found, use the default font. */
26762 bool font_not_found_p = font == NULL;
26763 if (font_not_found_p)
26764 {
26765 face = face->ascii_face;
26766 font = face->font;
26767 }
26768 boff = font->baseline_offset;
26769 if (font->vertical_centering)
26770 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26771 font_ascent = FONT_BASE (font) + boff;
26772 font_descent = FONT_DESCENT (font) - boff;
26773 font_height = FONT_HEIGHT (font);
26774
26775 cmp->font = font;
26776
26777 pcm = NULL;
26778 if (! font_not_found_p)
26779 {
26780 get_char_face_and_encoding (it->f, c, it->face_id,
26781 &char2b, false);
26782 pcm = get_per_char_metric (font, &char2b);
26783 }
26784
26785 /* Initialize the bounding box. */
26786 if (pcm)
26787 {
26788 width = cmp->glyph_len > 0 ? pcm->width : 0;
26789 ascent = pcm->ascent;
26790 descent = pcm->descent;
26791 lbearing = pcm->lbearing;
26792 rbearing = pcm->rbearing;
26793 }
26794 else
26795 {
26796 width = cmp->glyph_len > 0 ? font->space_width : 0;
26797 ascent = FONT_BASE (font);
26798 descent = FONT_DESCENT (font);
26799 lbearing = 0;
26800 rbearing = width;
26801 }
26802
26803 rightmost = width;
26804 leftmost = 0;
26805 lowest = - descent + boff;
26806 highest = ascent + boff;
26807
26808 if (! font_not_found_p
26809 && font->default_ascent
26810 && CHAR_TABLE_P (Vuse_default_ascent)
26811 && !NILP (Faref (Vuse_default_ascent,
26812 make_number (it->char_to_display))))
26813 highest = font->default_ascent + boff;
26814
26815 /* Draw the first glyph at the normal position. It may be
26816 shifted to right later if some other glyphs are drawn
26817 at the left. */
26818 cmp->offsets[i * 2] = 0;
26819 cmp->offsets[i * 2 + 1] = boff;
26820 cmp->lbearing = lbearing;
26821 cmp->rbearing = rbearing;
26822
26823 /* Set cmp->offsets for the remaining glyphs. */
26824 for (i++; i < glyph_len; i++)
26825 {
26826 int left, right, btm, top;
26827 int ch = COMPOSITION_GLYPH (cmp, i);
26828 int face_id;
26829 struct face *this_face;
26830
26831 if (ch == '\t')
26832 ch = ' ';
26833 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26834 this_face = FACE_FROM_ID (it->f, face_id);
26835 font = this_face->font;
26836
26837 if (font == NULL)
26838 pcm = NULL;
26839 else
26840 {
26841 get_char_face_and_encoding (it->f, ch, face_id,
26842 &char2b, false);
26843 pcm = get_per_char_metric (font, &char2b);
26844 }
26845 if (! pcm)
26846 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26847 else
26848 {
26849 width = pcm->width;
26850 ascent = pcm->ascent;
26851 descent = pcm->descent;
26852 lbearing = pcm->lbearing;
26853 rbearing = pcm->rbearing;
26854 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26855 {
26856 /* Relative composition with or without
26857 alternate chars. */
26858 left = (leftmost + rightmost - width) / 2;
26859 btm = - descent + boff;
26860 if (font->relative_compose
26861 && (! CHAR_TABLE_P (Vignore_relative_composition)
26862 || NILP (Faref (Vignore_relative_composition,
26863 make_number (ch)))))
26864 {
26865
26866 if (- descent >= font->relative_compose)
26867 /* One extra pixel between two glyphs. */
26868 btm = highest + 1;
26869 else if (ascent <= 0)
26870 /* One extra pixel between two glyphs. */
26871 btm = lowest - 1 - ascent - descent;
26872 }
26873 }
26874 else
26875 {
26876 /* A composition rule is specified by an integer
26877 value that encodes global and new reference
26878 points (GREF and NREF). GREF and NREF are
26879 specified by numbers as below:
26880
26881 0---1---2 -- ascent
26882 | |
26883 | |
26884 | |
26885 9--10--11 -- center
26886 | |
26887 ---3---4---5--- baseline
26888 | |
26889 6---7---8 -- descent
26890 */
26891 int rule = COMPOSITION_RULE (cmp, i);
26892 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26893
26894 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26895 grefx = gref % 3, nrefx = nref % 3;
26896 grefy = gref / 3, nrefy = nref / 3;
26897 if (xoff)
26898 xoff = font_height * (xoff - 128) / 256;
26899 if (yoff)
26900 yoff = font_height * (yoff - 128) / 256;
26901
26902 left = (leftmost
26903 + grefx * (rightmost - leftmost) / 2
26904 - nrefx * width / 2
26905 + xoff);
26906
26907 btm = ((grefy == 0 ? highest
26908 : grefy == 1 ? 0
26909 : grefy == 2 ? lowest
26910 : (highest + lowest) / 2)
26911 - (nrefy == 0 ? ascent + descent
26912 : nrefy == 1 ? descent - boff
26913 : nrefy == 2 ? 0
26914 : (ascent + descent) / 2)
26915 + yoff);
26916 }
26917
26918 cmp->offsets[i * 2] = left;
26919 cmp->offsets[i * 2 + 1] = btm + descent;
26920
26921 /* Update the bounding box of the overall glyphs. */
26922 if (width > 0)
26923 {
26924 right = left + width;
26925 if (left < leftmost)
26926 leftmost = left;
26927 if (right > rightmost)
26928 rightmost = right;
26929 }
26930 top = btm + descent + ascent;
26931 if (top > highest)
26932 highest = top;
26933 if (btm < lowest)
26934 lowest = btm;
26935
26936 if (cmp->lbearing > left + lbearing)
26937 cmp->lbearing = left + lbearing;
26938 if (cmp->rbearing < left + rbearing)
26939 cmp->rbearing = left + rbearing;
26940 }
26941 }
26942
26943 /* If there are glyphs whose x-offsets are negative,
26944 shift all glyphs to the right and make all x-offsets
26945 non-negative. */
26946 if (leftmost < 0)
26947 {
26948 for (i = 0; i < cmp->glyph_len; i++)
26949 cmp->offsets[i * 2] -= leftmost;
26950 rightmost -= leftmost;
26951 cmp->lbearing -= leftmost;
26952 cmp->rbearing -= leftmost;
26953 }
26954
26955 if (left_padded && cmp->lbearing < 0)
26956 {
26957 for (i = 0; i < cmp->glyph_len; i++)
26958 cmp->offsets[i * 2] -= cmp->lbearing;
26959 rightmost -= cmp->lbearing;
26960 cmp->rbearing -= cmp->lbearing;
26961 cmp->lbearing = 0;
26962 }
26963 if (right_padded && rightmost < cmp->rbearing)
26964 {
26965 rightmost = cmp->rbearing;
26966 }
26967
26968 cmp->pixel_width = rightmost;
26969 cmp->ascent = highest;
26970 cmp->descent = - lowest;
26971 if (cmp->ascent < font_ascent)
26972 cmp->ascent = font_ascent;
26973 if (cmp->descent < font_descent)
26974 cmp->descent = font_descent;
26975 }
26976
26977 if (it->glyph_row
26978 && (cmp->lbearing < 0
26979 || cmp->rbearing > cmp->pixel_width))
26980 it->glyph_row->contains_overlapping_glyphs_p = true;
26981
26982 it->pixel_width = cmp->pixel_width;
26983 it->ascent = it->phys_ascent = cmp->ascent;
26984 it->descent = it->phys_descent = cmp->descent;
26985 if (face->box != FACE_NO_BOX)
26986 {
26987 int thick = face->box_line_width;
26988
26989 if (thick > 0)
26990 {
26991 it->ascent += thick;
26992 it->descent += thick;
26993 }
26994 else
26995 thick = - thick;
26996
26997 if (it->start_of_box_run_p)
26998 it->pixel_width += thick;
26999 if (it->end_of_box_run_p)
27000 it->pixel_width += thick;
27001 }
27002
27003 /* If face has an overline, add the height of the overline
27004 (1 pixel) and a 1 pixel margin to the character height. */
27005 if (face->overline_p)
27006 it->ascent += overline_margin;
27007
27008 take_vertical_position_into_account (it);
27009 if (it->ascent < 0)
27010 it->ascent = 0;
27011 if (it->descent < 0)
27012 it->descent = 0;
27013
27014 if (it->glyph_row && cmp->glyph_len > 0)
27015 append_composite_glyph (it);
27016 }
27017 else if (it->what == IT_COMPOSITION)
27018 {
27019 /* A dynamic (automatic) composition. */
27020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27021 Lisp_Object gstring;
27022 struct font_metrics metrics;
27023
27024 it->nglyphs = 1;
27025
27026 gstring = composition_gstring_from_id (it->cmp_it.id);
27027 it->pixel_width
27028 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27029 &metrics);
27030 if (it->glyph_row
27031 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27032 it->glyph_row->contains_overlapping_glyphs_p = true;
27033 it->ascent = it->phys_ascent = metrics.ascent;
27034 it->descent = it->phys_descent = metrics.descent;
27035 if (face->box != FACE_NO_BOX)
27036 {
27037 int thick = face->box_line_width;
27038
27039 if (thick > 0)
27040 {
27041 it->ascent += thick;
27042 it->descent += thick;
27043 }
27044 else
27045 thick = - thick;
27046
27047 if (it->start_of_box_run_p)
27048 it->pixel_width += thick;
27049 if (it->end_of_box_run_p)
27050 it->pixel_width += thick;
27051 }
27052 /* If face has an overline, add the height of the overline
27053 (1 pixel) and a 1 pixel margin to the character height. */
27054 if (face->overline_p)
27055 it->ascent += overline_margin;
27056 take_vertical_position_into_account (it);
27057 if (it->ascent < 0)
27058 it->ascent = 0;
27059 if (it->descent < 0)
27060 it->descent = 0;
27061
27062 if (it->glyph_row)
27063 append_composite_glyph (it);
27064 }
27065 else if (it->what == IT_GLYPHLESS)
27066 produce_glyphless_glyph (it, false, Qnil);
27067 else if (it->what == IT_IMAGE)
27068 produce_image_glyph (it);
27069 else if (it->what == IT_STRETCH)
27070 produce_stretch_glyph (it);
27071
27072 done:
27073 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27074 because this isn't true for images with `:ascent 100'. */
27075 eassert (it->ascent >= 0 && it->descent >= 0);
27076 if (it->area == TEXT_AREA)
27077 it->current_x += it->pixel_width;
27078
27079 if (extra_line_spacing > 0)
27080 {
27081 it->descent += extra_line_spacing;
27082 if (extra_line_spacing > it->max_extra_line_spacing)
27083 it->max_extra_line_spacing = extra_line_spacing;
27084 }
27085
27086 it->max_ascent = max (it->max_ascent, it->ascent);
27087 it->max_descent = max (it->max_descent, it->descent);
27088 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27089 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27090 }
27091
27092 /* EXPORT for RIF:
27093 Output LEN glyphs starting at START at the nominal cursor position.
27094 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27095 being updated, and UPDATED_AREA is the area of that row being updated. */
27096
27097 void
27098 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27099 struct glyph *start, enum glyph_row_area updated_area, int len)
27100 {
27101 int x, hpos, chpos = w->phys_cursor.hpos;
27102
27103 eassert (updated_row);
27104 /* When the window is hscrolled, cursor hpos can legitimately be out
27105 of bounds, but we draw the cursor at the corresponding window
27106 margin in that case. */
27107 if (!updated_row->reversed_p && chpos < 0)
27108 chpos = 0;
27109 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27110 chpos = updated_row->used[TEXT_AREA] - 1;
27111
27112 block_input ();
27113
27114 /* Write glyphs. */
27115
27116 hpos = start - updated_row->glyphs[updated_area];
27117 x = draw_glyphs (w, w->output_cursor.x,
27118 updated_row, updated_area,
27119 hpos, hpos + len,
27120 DRAW_NORMAL_TEXT, 0);
27121
27122 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27123 if (updated_area == TEXT_AREA
27124 && w->phys_cursor_on_p
27125 && w->phys_cursor.vpos == w->output_cursor.vpos
27126 && chpos >= hpos
27127 && chpos < hpos + len)
27128 w->phys_cursor_on_p = false;
27129
27130 unblock_input ();
27131
27132 /* Advance the output cursor. */
27133 w->output_cursor.hpos += len;
27134 w->output_cursor.x = x;
27135 }
27136
27137
27138 /* EXPORT for RIF:
27139 Insert LEN glyphs from START at the nominal cursor position. */
27140
27141 void
27142 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27143 struct glyph *start, enum glyph_row_area updated_area, int len)
27144 {
27145 struct frame *f;
27146 int line_height, shift_by_width, shifted_region_width;
27147 struct glyph_row *row;
27148 struct glyph *glyph;
27149 int frame_x, frame_y;
27150 ptrdiff_t hpos;
27151
27152 eassert (updated_row);
27153 block_input ();
27154 f = XFRAME (WINDOW_FRAME (w));
27155
27156 /* Get the height of the line we are in. */
27157 row = updated_row;
27158 line_height = row->height;
27159
27160 /* Get the width of the glyphs to insert. */
27161 shift_by_width = 0;
27162 for (glyph = start; glyph < start + len; ++glyph)
27163 shift_by_width += glyph->pixel_width;
27164
27165 /* Get the width of the region to shift right. */
27166 shifted_region_width = (window_box_width (w, updated_area)
27167 - w->output_cursor.x
27168 - shift_by_width);
27169
27170 /* Shift right. */
27171 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27172 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27173
27174 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27175 line_height, shift_by_width);
27176
27177 /* Write the glyphs. */
27178 hpos = start - row->glyphs[updated_area];
27179 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27180 hpos, hpos + len,
27181 DRAW_NORMAL_TEXT, 0);
27182
27183 /* Advance the output cursor. */
27184 w->output_cursor.hpos += len;
27185 w->output_cursor.x += shift_by_width;
27186 unblock_input ();
27187 }
27188
27189
27190 /* EXPORT for RIF:
27191 Erase the current text line from the nominal cursor position
27192 (inclusive) to pixel column TO_X (exclusive). The idea is that
27193 everything from TO_X onward is already erased.
27194
27195 TO_X is a pixel position relative to UPDATED_AREA of currently
27196 updated window W. TO_X == -1 means clear to the end of this area. */
27197
27198 void
27199 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27200 enum glyph_row_area updated_area, int to_x)
27201 {
27202 struct frame *f;
27203 int max_x, min_y, max_y;
27204 int from_x, from_y, to_y;
27205
27206 eassert (updated_row);
27207 f = XFRAME (w->frame);
27208
27209 if (updated_row->full_width_p)
27210 max_x = (WINDOW_PIXEL_WIDTH (w)
27211 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27212 else
27213 max_x = window_box_width (w, updated_area);
27214 max_y = window_text_bottom_y (w);
27215
27216 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27217 of window. For TO_X > 0, truncate to end of drawing area. */
27218 if (to_x == 0)
27219 return;
27220 else if (to_x < 0)
27221 to_x = max_x;
27222 else
27223 to_x = min (to_x, max_x);
27224
27225 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27226
27227 /* Notice if the cursor will be cleared by this operation. */
27228 if (!updated_row->full_width_p)
27229 notice_overwritten_cursor (w, updated_area,
27230 w->output_cursor.x, -1,
27231 updated_row->y,
27232 MATRIX_ROW_BOTTOM_Y (updated_row));
27233
27234 from_x = w->output_cursor.x;
27235
27236 /* Translate to frame coordinates. */
27237 if (updated_row->full_width_p)
27238 {
27239 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27240 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27241 }
27242 else
27243 {
27244 int area_left = window_box_left (w, updated_area);
27245 from_x += area_left;
27246 to_x += area_left;
27247 }
27248
27249 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27250 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27251 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27252
27253 /* Prevent inadvertently clearing to end of the X window. */
27254 if (to_x > from_x && to_y > from_y)
27255 {
27256 block_input ();
27257 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27258 to_x - from_x, to_y - from_y);
27259 unblock_input ();
27260 }
27261 }
27262
27263 #endif /* HAVE_WINDOW_SYSTEM */
27264
27265
27266 \f
27267 /***********************************************************************
27268 Cursor types
27269 ***********************************************************************/
27270
27271 /* Value is the internal representation of the specified cursor type
27272 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27273 of the bar cursor. */
27274
27275 static enum text_cursor_kinds
27276 get_specified_cursor_type (Lisp_Object arg, int *width)
27277 {
27278 enum text_cursor_kinds type;
27279
27280 if (NILP (arg))
27281 return NO_CURSOR;
27282
27283 if (EQ (arg, Qbox))
27284 return FILLED_BOX_CURSOR;
27285
27286 if (EQ (arg, Qhollow))
27287 return HOLLOW_BOX_CURSOR;
27288
27289 if (EQ (arg, Qbar))
27290 {
27291 *width = 2;
27292 return BAR_CURSOR;
27293 }
27294
27295 if (CONSP (arg)
27296 && EQ (XCAR (arg), Qbar)
27297 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27298 {
27299 *width = XINT (XCDR (arg));
27300 return BAR_CURSOR;
27301 }
27302
27303 if (EQ (arg, Qhbar))
27304 {
27305 *width = 2;
27306 return HBAR_CURSOR;
27307 }
27308
27309 if (CONSP (arg)
27310 && EQ (XCAR (arg), Qhbar)
27311 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27312 {
27313 *width = XINT (XCDR (arg));
27314 return HBAR_CURSOR;
27315 }
27316
27317 /* Treat anything unknown as "hollow box cursor".
27318 It was bad to signal an error; people have trouble fixing
27319 .Xdefaults with Emacs, when it has something bad in it. */
27320 type = HOLLOW_BOX_CURSOR;
27321
27322 return type;
27323 }
27324
27325 /* Set the default cursor types for specified frame. */
27326 void
27327 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27328 {
27329 int width = 1;
27330 Lisp_Object tem;
27331
27332 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27333 FRAME_CURSOR_WIDTH (f) = width;
27334
27335 /* By default, set up the blink-off state depending on the on-state. */
27336
27337 tem = Fassoc (arg, Vblink_cursor_alist);
27338 if (!NILP (tem))
27339 {
27340 FRAME_BLINK_OFF_CURSOR (f)
27341 = get_specified_cursor_type (XCDR (tem), &width);
27342 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27343 }
27344 else
27345 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27346
27347 /* Make sure the cursor gets redrawn. */
27348 f->cursor_type_changed = true;
27349 }
27350
27351
27352 #ifdef HAVE_WINDOW_SYSTEM
27353
27354 /* Return the cursor we want to be displayed in window W. Return
27355 width of bar/hbar cursor through WIDTH arg. Return with
27356 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27357 (i.e. if the `system caret' should track this cursor).
27358
27359 In a mini-buffer window, we want the cursor only to appear if we
27360 are reading input from this window. For the selected window, we
27361 want the cursor type given by the frame parameter or buffer local
27362 setting of cursor-type. If explicitly marked off, draw no cursor.
27363 In all other cases, we want a hollow box cursor. */
27364
27365 static enum text_cursor_kinds
27366 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27367 bool *active_cursor)
27368 {
27369 struct frame *f = XFRAME (w->frame);
27370 struct buffer *b = XBUFFER (w->contents);
27371 int cursor_type = DEFAULT_CURSOR;
27372 Lisp_Object alt_cursor;
27373 bool non_selected = false;
27374
27375 *active_cursor = true;
27376
27377 /* Echo area */
27378 if (cursor_in_echo_area
27379 && FRAME_HAS_MINIBUF_P (f)
27380 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27381 {
27382 if (w == XWINDOW (echo_area_window))
27383 {
27384 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27385 {
27386 *width = FRAME_CURSOR_WIDTH (f);
27387 return FRAME_DESIRED_CURSOR (f);
27388 }
27389 else
27390 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27391 }
27392
27393 *active_cursor = false;
27394 non_selected = true;
27395 }
27396
27397 /* Detect a nonselected window or nonselected frame. */
27398 else if (w != XWINDOW (f->selected_window)
27399 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27400 {
27401 *active_cursor = false;
27402
27403 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27404 return NO_CURSOR;
27405
27406 non_selected = true;
27407 }
27408
27409 /* Never display a cursor in a window in which cursor-type is nil. */
27410 if (NILP (BVAR (b, cursor_type)))
27411 return NO_CURSOR;
27412
27413 /* Get the normal cursor type for this window. */
27414 if (EQ (BVAR (b, cursor_type), Qt))
27415 {
27416 cursor_type = FRAME_DESIRED_CURSOR (f);
27417 *width = FRAME_CURSOR_WIDTH (f);
27418 }
27419 else
27420 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27421
27422 /* Use cursor-in-non-selected-windows instead
27423 for non-selected window or frame. */
27424 if (non_selected)
27425 {
27426 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27427 if (!EQ (Qt, alt_cursor))
27428 return get_specified_cursor_type (alt_cursor, width);
27429 /* t means modify the normal cursor type. */
27430 if (cursor_type == FILLED_BOX_CURSOR)
27431 cursor_type = HOLLOW_BOX_CURSOR;
27432 else if (cursor_type == BAR_CURSOR && *width > 1)
27433 --*width;
27434 return cursor_type;
27435 }
27436
27437 /* Use normal cursor if not blinked off. */
27438 if (!w->cursor_off_p)
27439 {
27440 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27441 {
27442 if (cursor_type == FILLED_BOX_CURSOR)
27443 {
27444 /* Using a block cursor on large images can be very annoying.
27445 So use a hollow cursor for "large" images.
27446 If image is not transparent (no mask), also use hollow cursor. */
27447 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27448 if (img != NULL && IMAGEP (img->spec))
27449 {
27450 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27451 where N = size of default frame font size.
27452 This should cover most of the "tiny" icons people may use. */
27453 if (!img->mask
27454 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27455 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27456 cursor_type = HOLLOW_BOX_CURSOR;
27457 }
27458 }
27459 else if (cursor_type != NO_CURSOR)
27460 {
27461 /* Display current only supports BOX and HOLLOW cursors for images.
27462 So for now, unconditionally use a HOLLOW cursor when cursor is
27463 not a solid box cursor. */
27464 cursor_type = HOLLOW_BOX_CURSOR;
27465 }
27466 }
27467 return cursor_type;
27468 }
27469
27470 /* Cursor is blinked off, so determine how to "toggle" it. */
27471
27472 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27473 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27474 return get_specified_cursor_type (XCDR (alt_cursor), width);
27475
27476 /* Then see if frame has specified a specific blink off cursor type. */
27477 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27478 {
27479 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27480 return FRAME_BLINK_OFF_CURSOR (f);
27481 }
27482
27483 #if false
27484 /* Some people liked having a permanently visible blinking cursor,
27485 while others had very strong opinions against it. So it was
27486 decided to remove it. KFS 2003-09-03 */
27487
27488 /* Finally perform built-in cursor blinking:
27489 filled box <-> hollow box
27490 wide [h]bar <-> narrow [h]bar
27491 narrow [h]bar <-> no cursor
27492 other type <-> no cursor */
27493
27494 if (cursor_type == FILLED_BOX_CURSOR)
27495 return HOLLOW_BOX_CURSOR;
27496
27497 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27498 {
27499 *width = 1;
27500 return cursor_type;
27501 }
27502 #endif
27503
27504 return NO_CURSOR;
27505 }
27506
27507
27508 /* Notice when the text cursor of window W has been completely
27509 overwritten by a drawing operation that outputs glyphs in AREA
27510 starting at X0 and ending at X1 in the line starting at Y0 and
27511 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27512 the rest of the line after X0 has been written. Y coordinates
27513 are window-relative. */
27514
27515 static void
27516 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27517 int x0, int x1, int y0, int y1)
27518 {
27519 int cx0, cx1, cy0, cy1;
27520 struct glyph_row *row;
27521
27522 if (!w->phys_cursor_on_p)
27523 return;
27524 if (area != TEXT_AREA)
27525 return;
27526
27527 if (w->phys_cursor.vpos < 0
27528 || w->phys_cursor.vpos >= w->current_matrix->nrows
27529 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27530 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27531 return;
27532
27533 if (row->cursor_in_fringe_p)
27534 {
27535 row->cursor_in_fringe_p = false;
27536 draw_fringe_bitmap (w, row, row->reversed_p);
27537 w->phys_cursor_on_p = false;
27538 return;
27539 }
27540
27541 cx0 = w->phys_cursor.x;
27542 cx1 = cx0 + w->phys_cursor_width;
27543 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27544 return;
27545
27546 /* The cursor image will be completely removed from the
27547 screen if the output area intersects the cursor area in
27548 y-direction. When we draw in [y0 y1[, and some part of
27549 the cursor is at y < y0, that part must have been drawn
27550 before. When scrolling, the cursor is erased before
27551 actually scrolling, so we don't come here. When not
27552 scrolling, the rows above the old cursor row must have
27553 changed, and in this case these rows must have written
27554 over the cursor image.
27555
27556 Likewise if part of the cursor is below y1, with the
27557 exception of the cursor being in the first blank row at
27558 the buffer and window end because update_text_area
27559 doesn't draw that row. (Except when it does, but
27560 that's handled in update_text_area.) */
27561
27562 cy0 = w->phys_cursor.y;
27563 cy1 = cy0 + w->phys_cursor_height;
27564 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27565 return;
27566
27567 w->phys_cursor_on_p = false;
27568 }
27569
27570 #endif /* HAVE_WINDOW_SYSTEM */
27571
27572 \f
27573 /************************************************************************
27574 Mouse Face
27575 ************************************************************************/
27576
27577 #ifdef HAVE_WINDOW_SYSTEM
27578
27579 /* EXPORT for RIF:
27580 Fix the display of area AREA of overlapping row ROW in window W
27581 with respect to the overlapping part OVERLAPS. */
27582
27583 void
27584 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27585 enum glyph_row_area area, int overlaps)
27586 {
27587 int i, x;
27588
27589 block_input ();
27590
27591 x = 0;
27592 for (i = 0; i < row->used[area];)
27593 {
27594 if (row->glyphs[area][i].overlaps_vertically_p)
27595 {
27596 int start = i, start_x = x;
27597
27598 do
27599 {
27600 x += row->glyphs[area][i].pixel_width;
27601 ++i;
27602 }
27603 while (i < row->used[area]
27604 && row->glyphs[area][i].overlaps_vertically_p);
27605
27606 draw_glyphs (w, start_x, row, area,
27607 start, i,
27608 DRAW_NORMAL_TEXT, overlaps);
27609 }
27610 else
27611 {
27612 x += row->glyphs[area][i].pixel_width;
27613 ++i;
27614 }
27615 }
27616
27617 unblock_input ();
27618 }
27619
27620
27621 /* EXPORT:
27622 Draw the cursor glyph of window W in glyph row ROW. See the
27623 comment of draw_glyphs for the meaning of HL. */
27624
27625 void
27626 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27627 enum draw_glyphs_face hl)
27628 {
27629 /* If cursor hpos is out of bounds, don't draw garbage. This can
27630 happen in mini-buffer windows when switching between echo area
27631 glyphs and mini-buffer. */
27632 if ((row->reversed_p
27633 ? (w->phys_cursor.hpos >= 0)
27634 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27635 {
27636 bool on_p = w->phys_cursor_on_p;
27637 int x1;
27638 int hpos = w->phys_cursor.hpos;
27639
27640 /* When the window is hscrolled, cursor hpos can legitimately be
27641 out of bounds, but we draw the cursor at the corresponding
27642 window margin in that case. */
27643 if (!row->reversed_p && hpos < 0)
27644 hpos = 0;
27645 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27646 hpos = row->used[TEXT_AREA] - 1;
27647
27648 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27649 hl, 0);
27650 w->phys_cursor_on_p = on_p;
27651
27652 if (hl == DRAW_CURSOR)
27653 w->phys_cursor_width = x1 - w->phys_cursor.x;
27654 /* When we erase the cursor, and ROW is overlapped by other
27655 rows, make sure that these overlapping parts of other rows
27656 are redrawn. */
27657 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27658 {
27659 w->phys_cursor_width = x1 - w->phys_cursor.x;
27660
27661 if (row > w->current_matrix->rows
27662 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27663 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27664 OVERLAPS_ERASED_CURSOR);
27665
27666 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27667 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27668 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27669 OVERLAPS_ERASED_CURSOR);
27670 }
27671 }
27672 }
27673
27674
27675 /* Erase the image of a cursor of window W from the screen. */
27676
27677 void
27678 erase_phys_cursor (struct window *w)
27679 {
27680 struct frame *f = XFRAME (w->frame);
27681 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27682 int hpos = w->phys_cursor.hpos;
27683 int vpos = w->phys_cursor.vpos;
27684 bool mouse_face_here_p = false;
27685 struct glyph_matrix *active_glyphs = w->current_matrix;
27686 struct glyph_row *cursor_row;
27687 struct glyph *cursor_glyph;
27688 enum draw_glyphs_face hl;
27689
27690 /* No cursor displayed or row invalidated => nothing to do on the
27691 screen. */
27692 if (w->phys_cursor_type == NO_CURSOR)
27693 goto mark_cursor_off;
27694
27695 /* VPOS >= active_glyphs->nrows means that window has been resized.
27696 Don't bother to erase the cursor. */
27697 if (vpos >= active_glyphs->nrows)
27698 goto mark_cursor_off;
27699
27700 /* If row containing cursor is marked invalid, there is nothing we
27701 can do. */
27702 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27703 if (!cursor_row->enabled_p)
27704 goto mark_cursor_off;
27705
27706 /* If line spacing is > 0, old cursor may only be partially visible in
27707 window after split-window. So adjust visible height. */
27708 cursor_row->visible_height = min (cursor_row->visible_height,
27709 window_text_bottom_y (w) - cursor_row->y);
27710
27711 /* If row is completely invisible, don't attempt to delete a cursor which
27712 isn't there. This can happen if cursor is at top of a window, and
27713 we switch to a buffer with a header line in that window. */
27714 if (cursor_row->visible_height <= 0)
27715 goto mark_cursor_off;
27716
27717 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27718 if (cursor_row->cursor_in_fringe_p)
27719 {
27720 cursor_row->cursor_in_fringe_p = false;
27721 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27722 goto mark_cursor_off;
27723 }
27724
27725 /* This can happen when the new row is shorter than the old one.
27726 In this case, either draw_glyphs or clear_end_of_line
27727 should have cleared the cursor. Note that we wouldn't be
27728 able to erase the cursor in this case because we don't have a
27729 cursor glyph at hand. */
27730 if ((cursor_row->reversed_p
27731 ? (w->phys_cursor.hpos < 0)
27732 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27733 goto mark_cursor_off;
27734
27735 /* When the window is hscrolled, cursor hpos can legitimately be out
27736 of bounds, but we draw the cursor at the corresponding window
27737 margin in that case. */
27738 if (!cursor_row->reversed_p && hpos < 0)
27739 hpos = 0;
27740 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27741 hpos = cursor_row->used[TEXT_AREA] - 1;
27742
27743 /* If the cursor is in the mouse face area, redisplay that when
27744 we clear the cursor. */
27745 if (! NILP (hlinfo->mouse_face_window)
27746 && coords_in_mouse_face_p (w, hpos, vpos)
27747 /* Don't redraw the cursor's spot in mouse face if it is at the
27748 end of a line (on a newline). The cursor appears there, but
27749 mouse highlighting does not. */
27750 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27751 mouse_face_here_p = true;
27752
27753 /* Maybe clear the display under the cursor. */
27754 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27755 {
27756 int x, y;
27757 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27758 int width;
27759
27760 cursor_glyph = get_phys_cursor_glyph (w);
27761 if (cursor_glyph == NULL)
27762 goto mark_cursor_off;
27763
27764 width = cursor_glyph->pixel_width;
27765 x = w->phys_cursor.x;
27766 if (x < 0)
27767 {
27768 width += x;
27769 x = 0;
27770 }
27771 width = min (width, window_box_width (w, TEXT_AREA) - x);
27772 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27773 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27774
27775 if (width > 0)
27776 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27777 }
27778
27779 /* Erase the cursor by redrawing the character underneath it. */
27780 if (mouse_face_here_p)
27781 hl = DRAW_MOUSE_FACE;
27782 else
27783 hl = DRAW_NORMAL_TEXT;
27784 draw_phys_cursor_glyph (w, cursor_row, hl);
27785
27786 mark_cursor_off:
27787 w->phys_cursor_on_p = false;
27788 w->phys_cursor_type = NO_CURSOR;
27789 }
27790
27791
27792 /* Display or clear cursor of window W. If !ON, clear the cursor.
27793 If ON, display the cursor; where to put the cursor is specified by
27794 HPOS, VPOS, X and Y. */
27795
27796 void
27797 display_and_set_cursor (struct window *w, bool on,
27798 int hpos, int vpos, int x, int y)
27799 {
27800 struct frame *f = XFRAME (w->frame);
27801 int new_cursor_type;
27802 int new_cursor_width;
27803 bool active_cursor;
27804 struct glyph_row *glyph_row;
27805 struct glyph *glyph;
27806
27807 /* This is pointless on invisible frames, and dangerous on garbaged
27808 windows and frames; in the latter case, the frame or window may
27809 be in the midst of changing its size, and x and y may be off the
27810 window. */
27811 if (! FRAME_VISIBLE_P (f)
27812 || FRAME_GARBAGED_P (f)
27813 || vpos >= w->current_matrix->nrows
27814 || hpos >= w->current_matrix->matrix_w)
27815 return;
27816
27817 /* If cursor is off and we want it off, return quickly. */
27818 if (!on && !w->phys_cursor_on_p)
27819 return;
27820
27821 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27822 /* If cursor row is not enabled, we don't really know where to
27823 display the cursor. */
27824 if (!glyph_row->enabled_p)
27825 {
27826 w->phys_cursor_on_p = false;
27827 return;
27828 }
27829
27830 glyph = NULL;
27831 if (!glyph_row->exact_window_width_line_p
27832 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27833 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27834
27835 eassert (input_blocked_p ());
27836
27837 /* Set new_cursor_type to the cursor we want to be displayed. */
27838 new_cursor_type = get_window_cursor_type (w, glyph,
27839 &new_cursor_width, &active_cursor);
27840
27841 /* If cursor is currently being shown and we don't want it to be or
27842 it is in the wrong place, or the cursor type is not what we want,
27843 erase it. */
27844 if (w->phys_cursor_on_p
27845 && (!on
27846 || w->phys_cursor.x != x
27847 || w->phys_cursor.y != y
27848 /* HPOS can be negative in R2L rows whose
27849 exact_window_width_line_p flag is set (i.e. their newline
27850 would "overflow into the fringe"). */
27851 || hpos < 0
27852 || new_cursor_type != w->phys_cursor_type
27853 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27854 && new_cursor_width != w->phys_cursor_width)))
27855 erase_phys_cursor (w);
27856
27857 /* Don't check phys_cursor_on_p here because that flag is only set
27858 to false in some cases where we know that the cursor has been
27859 completely erased, to avoid the extra work of erasing the cursor
27860 twice. In other words, phys_cursor_on_p can be true and the cursor
27861 still not be visible, or it has only been partly erased. */
27862 if (on)
27863 {
27864 w->phys_cursor_ascent = glyph_row->ascent;
27865 w->phys_cursor_height = glyph_row->height;
27866
27867 /* Set phys_cursor_.* before x_draw_.* is called because some
27868 of them may need the information. */
27869 w->phys_cursor.x = x;
27870 w->phys_cursor.y = glyph_row->y;
27871 w->phys_cursor.hpos = hpos;
27872 w->phys_cursor.vpos = vpos;
27873 }
27874
27875 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27876 new_cursor_type, new_cursor_width,
27877 on, active_cursor);
27878 }
27879
27880
27881 /* Switch the display of W's cursor on or off, according to the value
27882 of ON. */
27883
27884 static void
27885 update_window_cursor (struct window *w, bool on)
27886 {
27887 /* Don't update cursor in windows whose frame is in the process
27888 of being deleted. */
27889 if (w->current_matrix)
27890 {
27891 int hpos = w->phys_cursor.hpos;
27892 int vpos = w->phys_cursor.vpos;
27893 struct glyph_row *row;
27894
27895 if (vpos >= w->current_matrix->nrows
27896 || hpos >= w->current_matrix->matrix_w)
27897 return;
27898
27899 row = MATRIX_ROW (w->current_matrix, vpos);
27900
27901 /* When the window is hscrolled, cursor hpos can legitimately be
27902 out of bounds, but we draw the cursor at the corresponding
27903 window margin in that case. */
27904 if (!row->reversed_p && hpos < 0)
27905 hpos = 0;
27906 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27907 hpos = row->used[TEXT_AREA] - 1;
27908
27909 block_input ();
27910 display_and_set_cursor (w, on, hpos, vpos,
27911 w->phys_cursor.x, w->phys_cursor.y);
27912 unblock_input ();
27913 }
27914 }
27915
27916
27917 /* Call update_window_cursor with parameter ON_P on all leaf windows
27918 in the window tree rooted at W. */
27919
27920 static void
27921 update_cursor_in_window_tree (struct window *w, bool on_p)
27922 {
27923 while (w)
27924 {
27925 if (WINDOWP (w->contents))
27926 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27927 else
27928 update_window_cursor (w, on_p);
27929
27930 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27931 }
27932 }
27933
27934
27935 /* EXPORT:
27936 Display the cursor on window W, or clear it, according to ON_P.
27937 Don't change the cursor's position. */
27938
27939 void
27940 x_update_cursor (struct frame *f, bool on_p)
27941 {
27942 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27943 }
27944
27945
27946 /* EXPORT:
27947 Clear the cursor of window W to background color, and mark the
27948 cursor as not shown. This is used when the text where the cursor
27949 is about to be rewritten. */
27950
27951 void
27952 x_clear_cursor (struct window *w)
27953 {
27954 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27955 update_window_cursor (w, false);
27956 }
27957
27958 #endif /* HAVE_WINDOW_SYSTEM */
27959
27960 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27961 and MSDOS. */
27962 static void
27963 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27964 int start_hpos, int end_hpos,
27965 enum draw_glyphs_face draw)
27966 {
27967 #ifdef HAVE_WINDOW_SYSTEM
27968 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27969 {
27970 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27971 return;
27972 }
27973 #endif
27974 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27975 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27976 #endif
27977 }
27978
27979 /* Display the active region described by mouse_face_* according to DRAW. */
27980
27981 static void
27982 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27983 {
27984 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27985 struct frame *f = XFRAME (WINDOW_FRAME (w));
27986
27987 if (/* If window is in the process of being destroyed, don't bother
27988 to do anything. */
27989 w->current_matrix != NULL
27990 /* Don't update mouse highlight if hidden. */
27991 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27992 /* Recognize when we are called to operate on rows that don't exist
27993 anymore. This can happen when a window is split. */
27994 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27995 {
27996 bool phys_cursor_on_p = w->phys_cursor_on_p;
27997 struct glyph_row *row, *first, *last;
27998
27999 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28000 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28001
28002 for (row = first; row <= last && row->enabled_p; ++row)
28003 {
28004 int start_hpos, end_hpos, start_x;
28005
28006 /* For all but the first row, the highlight starts at column 0. */
28007 if (row == first)
28008 {
28009 /* R2L rows have BEG and END in reversed order, but the
28010 screen drawing geometry is always left to right. So
28011 we need to mirror the beginning and end of the
28012 highlighted area in R2L rows. */
28013 if (!row->reversed_p)
28014 {
28015 start_hpos = hlinfo->mouse_face_beg_col;
28016 start_x = hlinfo->mouse_face_beg_x;
28017 }
28018 else if (row == last)
28019 {
28020 start_hpos = hlinfo->mouse_face_end_col;
28021 start_x = hlinfo->mouse_face_end_x;
28022 }
28023 else
28024 {
28025 start_hpos = 0;
28026 start_x = 0;
28027 }
28028 }
28029 else if (row->reversed_p && row == last)
28030 {
28031 start_hpos = hlinfo->mouse_face_end_col;
28032 start_x = hlinfo->mouse_face_end_x;
28033 }
28034 else
28035 {
28036 start_hpos = 0;
28037 start_x = 0;
28038 }
28039
28040 if (row == last)
28041 {
28042 if (!row->reversed_p)
28043 end_hpos = hlinfo->mouse_face_end_col;
28044 else if (row == first)
28045 end_hpos = hlinfo->mouse_face_beg_col;
28046 else
28047 {
28048 end_hpos = row->used[TEXT_AREA];
28049 if (draw == DRAW_NORMAL_TEXT)
28050 row->fill_line_p = true; /* Clear to end of line. */
28051 }
28052 }
28053 else if (row->reversed_p && row == first)
28054 end_hpos = hlinfo->mouse_face_beg_col;
28055 else
28056 {
28057 end_hpos = row->used[TEXT_AREA];
28058 if (draw == DRAW_NORMAL_TEXT)
28059 row->fill_line_p = true; /* Clear to end of line. */
28060 }
28061
28062 if (end_hpos > start_hpos)
28063 {
28064 draw_row_with_mouse_face (w, start_x, row,
28065 start_hpos, end_hpos, draw);
28066
28067 row->mouse_face_p
28068 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28069 }
28070 }
28071
28072 #ifdef HAVE_WINDOW_SYSTEM
28073 /* When we've written over the cursor, arrange for it to
28074 be displayed again. */
28075 if (FRAME_WINDOW_P (f)
28076 && phys_cursor_on_p && !w->phys_cursor_on_p)
28077 {
28078 int hpos = w->phys_cursor.hpos;
28079
28080 /* When the window is hscrolled, cursor hpos can legitimately be
28081 out of bounds, but we draw the cursor at the corresponding
28082 window margin in that case. */
28083 if (!row->reversed_p && hpos < 0)
28084 hpos = 0;
28085 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28086 hpos = row->used[TEXT_AREA] - 1;
28087
28088 block_input ();
28089 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28090 w->phys_cursor.x, w->phys_cursor.y);
28091 unblock_input ();
28092 }
28093 #endif /* HAVE_WINDOW_SYSTEM */
28094 }
28095
28096 #ifdef HAVE_WINDOW_SYSTEM
28097 /* Change the mouse cursor. */
28098 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28099 {
28100 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28101 if (draw == DRAW_NORMAL_TEXT
28102 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28103 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28104 else
28105 #endif
28106 if (draw == DRAW_MOUSE_FACE)
28107 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28108 else
28109 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28110 }
28111 #endif /* HAVE_WINDOW_SYSTEM */
28112 }
28113
28114 /* EXPORT:
28115 Clear out the mouse-highlighted active region.
28116 Redraw it un-highlighted first. Value is true if mouse
28117 face was actually drawn unhighlighted. */
28118
28119 bool
28120 clear_mouse_face (Mouse_HLInfo *hlinfo)
28121 {
28122 bool cleared
28123 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28124 if (cleared)
28125 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28126 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28127 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28128 hlinfo->mouse_face_window = Qnil;
28129 hlinfo->mouse_face_overlay = Qnil;
28130 return cleared;
28131 }
28132
28133 /* Return true if the coordinates HPOS and VPOS on windows W are
28134 within the mouse face on that window. */
28135 static bool
28136 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28137 {
28138 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28139
28140 /* Quickly resolve the easy cases. */
28141 if (!(WINDOWP (hlinfo->mouse_face_window)
28142 && XWINDOW (hlinfo->mouse_face_window) == w))
28143 return false;
28144 if (vpos < hlinfo->mouse_face_beg_row
28145 || vpos > hlinfo->mouse_face_end_row)
28146 return false;
28147 if (vpos > hlinfo->mouse_face_beg_row
28148 && vpos < hlinfo->mouse_face_end_row)
28149 return true;
28150
28151 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28152 {
28153 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28154 {
28155 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28156 return true;
28157 }
28158 else if ((vpos == hlinfo->mouse_face_beg_row
28159 && hpos >= hlinfo->mouse_face_beg_col)
28160 || (vpos == hlinfo->mouse_face_end_row
28161 && hpos < hlinfo->mouse_face_end_col))
28162 return true;
28163 }
28164 else
28165 {
28166 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28167 {
28168 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28169 return true;
28170 }
28171 else if ((vpos == hlinfo->mouse_face_beg_row
28172 && hpos <= hlinfo->mouse_face_beg_col)
28173 || (vpos == hlinfo->mouse_face_end_row
28174 && hpos > hlinfo->mouse_face_end_col))
28175 return true;
28176 }
28177 return false;
28178 }
28179
28180
28181 /* EXPORT:
28182 True if physical cursor of window W is within mouse face. */
28183
28184 bool
28185 cursor_in_mouse_face_p (struct window *w)
28186 {
28187 int hpos = w->phys_cursor.hpos;
28188 int vpos = w->phys_cursor.vpos;
28189 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28190
28191 /* When the window is hscrolled, cursor hpos can legitimately be out
28192 of bounds, but we draw the cursor at the corresponding window
28193 margin in that case. */
28194 if (!row->reversed_p && hpos < 0)
28195 hpos = 0;
28196 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28197 hpos = row->used[TEXT_AREA] - 1;
28198
28199 return coords_in_mouse_face_p (w, hpos, vpos);
28200 }
28201
28202
28203 \f
28204 /* Find the glyph rows START_ROW and END_ROW of window W that display
28205 characters between buffer positions START_CHARPOS and END_CHARPOS
28206 (excluding END_CHARPOS). DISP_STRING is a display string that
28207 covers these buffer positions. This is similar to
28208 row_containing_pos, but is more accurate when bidi reordering makes
28209 buffer positions change non-linearly with glyph rows. */
28210 static void
28211 rows_from_pos_range (struct window *w,
28212 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28213 Lisp_Object disp_string,
28214 struct glyph_row **start, struct glyph_row **end)
28215 {
28216 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28217 int last_y = window_text_bottom_y (w);
28218 struct glyph_row *row;
28219
28220 *start = NULL;
28221 *end = NULL;
28222
28223 while (!first->enabled_p
28224 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28225 first++;
28226
28227 /* Find the START row. */
28228 for (row = first;
28229 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28230 row++)
28231 {
28232 /* A row can potentially be the START row if the range of the
28233 characters it displays intersects the range
28234 [START_CHARPOS..END_CHARPOS). */
28235 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28236 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28237 /* See the commentary in row_containing_pos, for the
28238 explanation of the complicated way to check whether
28239 some position is beyond the end of the characters
28240 displayed by a row. */
28241 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28242 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28243 && !row->ends_at_zv_p
28244 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28245 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28246 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28247 && !row->ends_at_zv_p
28248 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28249 {
28250 /* Found a candidate row. Now make sure at least one of the
28251 glyphs it displays has a charpos from the range
28252 [START_CHARPOS..END_CHARPOS).
28253
28254 This is not obvious because bidi reordering could make
28255 buffer positions of a row be 1,2,3,102,101,100, and if we
28256 want to highlight characters in [50..60), we don't want
28257 this row, even though [50..60) does intersect [1..103),
28258 the range of character positions given by the row's start
28259 and end positions. */
28260 struct glyph *g = row->glyphs[TEXT_AREA];
28261 struct glyph *e = g + row->used[TEXT_AREA];
28262
28263 while (g < e)
28264 {
28265 if (((BUFFERP (g->object) || NILP (g->object))
28266 && start_charpos <= g->charpos && g->charpos < end_charpos)
28267 /* A glyph that comes from DISP_STRING is by
28268 definition to be highlighted. */
28269 || EQ (g->object, disp_string))
28270 *start = row;
28271 g++;
28272 }
28273 if (*start)
28274 break;
28275 }
28276 }
28277
28278 /* Find the END row. */
28279 if (!*start
28280 /* If the last row is partially visible, start looking for END
28281 from that row, instead of starting from FIRST. */
28282 && !(row->enabled_p
28283 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28284 row = first;
28285 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28286 {
28287 struct glyph_row *next = row + 1;
28288 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28289
28290 if (!next->enabled_p
28291 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28292 /* The first row >= START whose range of displayed characters
28293 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28294 is the row END + 1. */
28295 || (start_charpos < next_start
28296 && end_charpos < next_start)
28297 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28298 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28299 && !next->ends_at_zv_p
28300 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28301 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28302 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28303 && !next->ends_at_zv_p
28304 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28305 {
28306 *end = row;
28307 break;
28308 }
28309 else
28310 {
28311 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28312 but none of the characters it displays are in the range, it is
28313 also END + 1. */
28314 struct glyph *g = next->glyphs[TEXT_AREA];
28315 struct glyph *s = g;
28316 struct glyph *e = g + next->used[TEXT_AREA];
28317
28318 while (g < e)
28319 {
28320 if (((BUFFERP (g->object) || NILP (g->object))
28321 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28322 /* If the buffer position of the first glyph in
28323 the row is equal to END_CHARPOS, it means
28324 the last character to be highlighted is the
28325 newline of ROW, and we must consider NEXT as
28326 END, not END+1. */
28327 || (((!next->reversed_p && g == s)
28328 || (next->reversed_p && g == e - 1))
28329 && (g->charpos == end_charpos
28330 /* Special case for when NEXT is an
28331 empty line at ZV. */
28332 || (g->charpos == -1
28333 && !row->ends_at_zv_p
28334 && next_start == end_charpos)))))
28335 /* A glyph that comes from DISP_STRING is by
28336 definition to be highlighted. */
28337 || EQ (g->object, disp_string))
28338 break;
28339 g++;
28340 }
28341 if (g == e)
28342 {
28343 *end = row;
28344 break;
28345 }
28346 /* The first row that ends at ZV must be the last to be
28347 highlighted. */
28348 else if (next->ends_at_zv_p)
28349 {
28350 *end = next;
28351 break;
28352 }
28353 }
28354 }
28355 }
28356
28357 /* This function sets the mouse_face_* elements of HLINFO, assuming
28358 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28359 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28360 for the overlay or run of text properties specifying the mouse
28361 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28362 before-string and after-string that must also be highlighted.
28363 DISP_STRING, if non-nil, is a display string that may cover some
28364 or all of the highlighted text. */
28365
28366 static void
28367 mouse_face_from_buffer_pos (Lisp_Object window,
28368 Mouse_HLInfo *hlinfo,
28369 ptrdiff_t mouse_charpos,
28370 ptrdiff_t start_charpos,
28371 ptrdiff_t end_charpos,
28372 Lisp_Object before_string,
28373 Lisp_Object after_string,
28374 Lisp_Object disp_string)
28375 {
28376 struct window *w = XWINDOW (window);
28377 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28378 struct glyph_row *r1, *r2;
28379 struct glyph *glyph, *end;
28380 ptrdiff_t ignore, pos;
28381 int x;
28382
28383 eassert (NILP (disp_string) || STRINGP (disp_string));
28384 eassert (NILP (before_string) || STRINGP (before_string));
28385 eassert (NILP (after_string) || STRINGP (after_string));
28386
28387 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28388 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28389 if (r1 == NULL)
28390 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28391 /* If the before-string or display-string contains newlines,
28392 rows_from_pos_range skips to its last row. Move back. */
28393 if (!NILP (before_string) || !NILP (disp_string))
28394 {
28395 struct glyph_row *prev;
28396 while ((prev = r1 - 1, prev >= first)
28397 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28398 && prev->used[TEXT_AREA] > 0)
28399 {
28400 struct glyph *beg = prev->glyphs[TEXT_AREA];
28401 glyph = beg + prev->used[TEXT_AREA];
28402 while (--glyph >= beg && NILP (glyph->object));
28403 if (glyph < beg
28404 || !(EQ (glyph->object, before_string)
28405 || EQ (glyph->object, disp_string)))
28406 break;
28407 r1 = prev;
28408 }
28409 }
28410 if (r2 == NULL)
28411 {
28412 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28413 hlinfo->mouse_face_past_end = true;
28414 }
28415 else if (!NILP (after_string))
28416 {
28417 /* If the after-string has newlines, advance to its last row. */
28418 struct glyph_row *next;
28419 struct glyph_row *last
28420 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28421
28422 for (next = r2 + 1;
28423 next <= last
28424 && next->used[TEXT_AREA] > 0
28425 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28426 ++next)
28427 r2 = next;
28428 }
28429 /* The rest of the display engine assumes that mouse_face_beg_row is
28430 either above mouse_face_end_row or identical to it. But with
28431 bidi-reordered continued lines, the row for START_CHARPOS could
28432 be below the row for END_CHARPOS. If so, swap the rows and store
28433 them in correct order. */
28434 if (r1->y > r2->y)
28435 {
28436 struct glyph_row *tem = r2;
28437
28438 r2 = r1;
28439 r1 = tem;
28440 }
28441
28442 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28443 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28444
28445 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28446 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28447 could be anywhere in the row and in any order. The strategy
28448 below is to find the leftmost and the rightmost glyph that
28449 belongs to either of these 3 strings, or whose position is
28450 between START_CHARPOS and END_CHARPOS, and highlight all the
28451 glyphs between those two. This may cover more than just the text
28452 between START_CHARPOS and END_CHARPOS if the range of characters
28453 strides the bidi level boundary, e.g. if the beginning is in R2L
28454 text while the end is in L2R text or vice versa. */
28455 if (!r1->reversed_p)
28456 {
28457 /* This row is in a left to right paragraph. Scan it left to
28458 right. */
28459 glyph = r1->glyphs[TEXT_AREA];
28460 end = glyph + r1->used[TEXT_AREA];
28461 x = r1->x;
28462
28463 /* Skip truncation glyphs at the start of the glyph row. */
28464 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28465 for (; glyph < end
28466 && NILP (glyph->object)
28467 && glyph->charpos < 0;
28468 ++glyph)
28469 x += glyph->pixel_width;
28470
28471 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28472 or DISP_STRING, and the first glyph from buffer whose
28473 position is between START_CHARPOS and END_CHARPOS. */
28474 for (; glyph < end
28475 && !NILP (glyph->object)
28476 && !EQ (glyph->object, disp_string)
28477 && !(BUFFERP (glyph->object)
28478 && (glyph->charpos >= start_charpos
28479 && glyph->charpos < end_charpos));
28480 ++glyph)
28481 {
28482 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28483 are present at buffer positions between START_CHARPOS and
28484 END_CHARPOS, or if they come from an overlay. */
28485 if (EQ (glyph->object, before_string))
28486 {
28487 pos = string_buffer_position (before_string,
28488 start_charpos);
28489 /* If pos == 0, it means before_string came from an
28490 overlay, not from a buffer position. */
28491 if (!pos || (pos >= start_charpos && pos < end_charpos))
28492 break;
28493 }
28494 else if (EQ (glyph->object, after_string))
28495 {
28496 pos = string_buffer_position (after_string, end_charpos);
28497 if (!pos || (pos >= start_charpos && pos < end_charpos))
28498 break;
28499 }
28500 x += glyph->pixel_width;
28501 }
28502 hlinfo->mouse_face_beg_x = x;
28503 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28504 }
28505 else
28506 {
28507 /* This row is in a right to left paragraph. Scan it right to
28508 left. */
28509 struct glyph *g;
28510
28511 end = r1->glyphs[TEXT_AREA] - 1;
28512 glyph = end + r1->used[TEXT_AREA];
28513
28514 /* Skip truncation glyphs at the start of the glyph row. */
28515 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28516 for (; glyph > end
28517 && NILP (glyph->object)
28518 && glyph->charpos < 0;
28519 --glyph)
28520 ;
28521
28522 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28523 or DISP_STRING, and the first glyph from buffer whose
28524 position is between START_CHARPOS and END_CHARPOS. */
28525 for (; glyph > end
28526 && !NILP (glyph->object)
28527 && !EQ (glyph->object, disp_string)
28528 && !(BUFFERP (glyph->object)
28529 && (glyph->charpos >= start_charpos
28530 && glyph->charpos < end_charpos));
28531 --glyph)
28532 {
28533 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28534 are present at buffer positions between START_CHARPOS and
28535 END_CHARPOS, or if they come from an overlay. */
28536 if (EQ (glyph->object, before_string))
28537 {
28538 pos = string_buffer_position (before_string, start_charpos);
28539 /* If pos == 0, it means before_string came from an
28540 overlay, not from a buffer position. */
28541 if (!pos || (pos >= start_charpos && pos < end_charpos))
28542 break;
28543 }
28544 else if (EQ (glyph->object, after_string))
28545 {
28546 pos = string_buffer_position (after_string, end_charpos);
28547 if (!pos || (pos >= start_charpos && pos < end_charpos))
28548 break;
28549 }
28550 }
28551
28552 glyph++; /* first glyph to the right of the highlighted area */
28553 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28554 x += g->pixel_width;
28555 hlinfo->mouse_face_beg_x = x;
28556 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28557 }
28558
28559 /* If the highlight ends in a different row, compute GLYPH and END
28560 for the end row. Otherwise, reuse the values computed above for
28561 the row where the highlight begins. */
28562 if (r2 != r1)
28563 {
28564 if (!r2->reversed_p)
28565 {
28566 glyph = r2->glyphs[TEXT_AREA];
28567 end = glyph + r2->used[TEXT_AREA];
28568 x = r2->x;
28569 }
28570 else
28571 {
28572 end = r2->glyphs[TEXT_AREA] - 1;
28573 glyph = end + r2->used[TEXT_AREA];
28574 }
28575 }
28576
28577 if (!r2->reversed_p)
28578 {
28579 /* Skip truncation and continuation glyphs near the end of the
28580 row, and also blanks and stretch glyphs inserted by
28581 extend_face_to_end_of_line. */
28582 while (end > glyph
28583 && NILP ((end - 1)->object))
28584 --end;
28585 /* Scan the rest of the glyph row from the end, looking for the
28586 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28587 DISP_STRING, or whose position is between START_CHARPOS
28588 and END_CHARPOS */
28589 for (--end;
28590 end > glyph
28591 && !NILP (end->object)
28592 && !EQ (end->object, disp_string)
28593 && !(BUFFERP (end->object)
28594 && (end->charpos >= start_charpos
28595 && end->charpos < end_charpos));
28596 --end)
28597 {
28598 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28599 are present at buffer positions between START_CHARPOS and
28600 END_CHARPOS, or if they come from an overlay. */
28601 if (EQ (end->object, before_string))
28602 {
28603 pos = string_buffer_position (before_string, start_charpos);
28604 if (!pos || (pos >= start_charpos && pos < end_charpos))
28605 break;
28606 }
28607 else if (EQ (end->object, after_string))
28608 {
28609 pos = string_buffer_position (after_string, end_charpos);
28610 if (!pos || (pos >= start_charpos && pos < end_charpos))
28611 break;
28612 }
28613 }
28614 /* Find the X coordinate of the last glyph to be highlighted. */
28615 for (; glyph <= end; ++glyph)
28616 x += glyph->pixel_width;
28617
28618 hlinfo->mouse_face_end_x = x;
28619 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28620 }
28621 else
28622 {
28623 /* Skip truncation and continuation glyphs near the end of the
28624 row, and also blanks and stretch glyphs inserted by
28625 extend_face_to_end_of_line. */
28626 x = r2->x;
28627 end++;
28628 while (end < glyph
28629 && NILP (end->object))
28630 {
28631 x += end->pixel_width;
28632 ++end;
28633 }
28634 /* Scan the rest of the glyph row from the end, looking for the
28635 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28636 DISP_STRING, or whose position is between START_CHARPOS
28637 and END_CHARPOS */
28638 for ( ;
28639 end < glyph
28640 && !NILP (end->object)
28641 && !EQ (end->object, disp_string)
28642 && !(BUFFERP (end->object)
28643 && (end->charpos >= start_charpos
28644 && end->charpos < end_charpos));
28645 ++end)
28646 {
28647 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28648 are present at buffer positions between START_CHARPOS and
28649 END_CHARPOS, or if they come from an overlay. */
28650 if (EQ (end->object, before_string))
28651 {
28652 pos = string_buffer_position (before_string, start_charpos);
28653 if (!pos || (pos >= start_charpos && pos < end_charpos))
28654 break;
28655 }
28656 else if (EQ (end->object, after_string))
28657 {
28658 pos = string_buffer_position (after_string, end_charpos);
28659 if (!pos || (pos >= start_charpos && pos < end_charpos))
28660 break;
28661 }
28662 x += end->pixel_width;
28663 }
28664 /* If we exited the above loop because we arrived at the last
28665 glyph of the row, and its buffer position is still not in
28666 range, it means the last character in range is the preceding
28667 newline. Bump the end column and x values to get past the
28668 last glyph. */
28669 if (end == glyph
28670 && BUFFERP (end->object)
28671 && (end->charpos < start_charpos
28672 || end->charpos >= end_charpos))
28673 {
28674 x += end->pixel_width;
28675 ++end;
28676 }
28677 hlinfo->mouse_face_end_x = x;
28678 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28679 }
28680
28681 hlinfo->mouse_face_window = window;
28682 hlinfo->mouse_face_face_id
28683 = face_at_buffer_position (w, mouse_charpos, &ignore,
28684 mouse_charpos + 1,
28685 !hlinfo->mouse_face_hidden, -1);
28686 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28687 }
28688
28689 /* The following function is not used anymore (replaced with
28690 mouse_face_from_string_pos), but I leave it here for the time
28691 being, in case someone would. */
28692
28693 #if false /* not used */
28694
28695 /* Find the position of the glyph for position POS in OBJECT in
28696 window W's current matrix, and return in *X, *Y the pixel
28697 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28698
28699 RIGHT_P means return the position of the right edge of the glyph.
28700 !RIGHT_P means return the left edge position.
28701
28702 If no glyph for POS exists in the matrix, return the position of
28703 the glyph with the next smaller position that is in the matrix, if
28704 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28705 exists in the matrix, return the position of the glyph with the
28706 next larger position in OBJECT.
28707
28708 Value is true if a glyph was found. */
28709
28710 static bool
28711 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28712 int *hpos, int *vpos, int *x, int *y, bool right_p)
28713 {
28714 int yb = window_text_bottom_y (w);
28715 struct glyph_row *r;
28716 struct glyph *best_glyph = NULL;
28717 struct glyph_row *best_row = NULL;
28718 int best_x = 0;
28719
28720 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28721 r->enabled_p && r->y < yb;
28722 ++r)
28723 {
28724 struct glyph *g = r->glyphs[TEXT_AREA];
28725 struct glyph *e = g + r->used[TEXT_AREA];
28726 int gx;
28727
28728 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28729 if (EQ (g->object, object))
28730 {
28731 if (g->charpos == pos)
28732 {
28733 best_glyph = g;
28734 best_x = gx;
28735 best_row = r;
28736 goto found;
28737 }
28738 else if (best_glyph == NULL
28739 || ((eabs (g->charpos - pos)
28740 < eabs (best_glyph->charpos - pos))
28741 && (right_p
28742 ? g->charpos < pos
28743 : g->charpos > pos)))
28744 {
28745 best_glyph = g;
28746 best_x = gx;
28747 best_row = r;
28748 }
28749 }
28750 }
28751
28752 found:
28753
28754 if (best_glyph)
28755 {
28756 *x = best_x;
28757 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28758
28759 if (right_p)
28760 {
28761 *x += best_glyph->pixel_width;
28762 ++*hpos;
28763 }
28764
28765 *y = best_row->y;
28766 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28767 }
28768
28769 return best_glyph != NULL;
28770 }
28771 #endif /* not used */
28772
28773 /* Find the positions of the first and the last glyphs in window W's
28774 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28775 (assumed to be a string), and return in HLINFO's mouse_face_*
28776 members the pixel and column/row coordinates of those glyphs. */
28777
28778 static void
28779 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28780 Lisp_Object object,
28781 ptrdiff_t startpos, ptrdiff_t endpos)
28782 {
28783 int yb = window_text_bottom_y (w);
28784 struct glyph_row *r;
28785 struct glyph *g, *e;
28786 int gx;
28787 bool found = false;
28788
28789 /* Find the glyph row with at least one position in the range
28790 [STARTPOS..ENDPOS), and the first glyph in that row whose
28791 position belongs to that range. */
28792 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28793 r->enabled_p && r->y < yb;
28794 ++r)
28795 {
28796 if (!r->reversed_p)
28797 {
28798 g = r->glyphs[TEXT_AREA];
28799 e = g + r->used[TEXT_AREA];
28800 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28801 if (EQ (g->object, object)
28802 && startpos <= g->charpos && g->charpos < endpos)
28803 {
28804 hlinfo->mouse_face_beg_row
28805 = MATRIX_ROW_VPOS (r, w->current_matrix);
28806 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28807 hlinfo->mouse_face_beg_x = gx;
28808 found = true;
28809 break;
28810 }
28811 }
28812 else
28813 {
28814 struct glyph *g1;
28815
28816 e = r->glyphs[TEXT_AREA];
28817 g = e + r->used[TEXT_AREA];
28818 for ( ; g > e; --g)
28819 if (EQ ((g-1)->object, object)
28820 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28821 {
28822 hlinfo->mouse_face_beg_row
28823 = MATRIX_ROW_VPOS (r, w->current_matrix);
28824 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28825 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28826 gx += g1->pixel_width;
28827 hlinfo->mouse_face_beg_x = gx;
28828 found = true;
28829 break;
28830 }
28831 }
28832 if (found)
28833 break;
28834 }
28835
28836 if (!found)
28837 return;
28838
28839 /* Starting with the next row, look for the first row which does NOT
28840 include any glyphs whose positions are in the range. */
28841 for (++r; r->enabled_p && r->y < yb; ++r)
28842 {
28843 g = r->glyphs[TEXT_AREA];
28844 e = g + r->used[TEXT_AREA];
28845 found = false;
28846 for ( ; g < e; ++g)
28847 if (EQ (g->object, object)
28848 && startpos <= g->charpos && g->charpos < endpos)
28849 {
28850 found = true;
28851 break;
28852 }
28853 if (!found)
28854 break;
28855 }
28856
28857 /* The highlighted region ends on the previous row. */
28858 r--;
28859
28860 /* Set the end row. */
28861 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28862
28863 /* Compute and set the end column and the end column's horizontal
28864 pixel coordinate. */
28865 if (!r->reversed_p)
28866 {
28867 g = r->glyphs[TEXT_AREA];
28868 e = g + r->used[TEXT_AREA];
28869 for ( ; e > g; --e)
28870 if (EQ ((e-1)->object, object)
28871 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28872 break;
28873 hlinfo->mouse_face_end_col = e - g;
28874
28875 for (gx = r->x; g < e; ++g)
28876 gx += g->pixel_width;
28877 hlinfo->mouse_face_end_x = gx;
28878 }
28879 else
28880 {
28881 e = r->glyphs[TEXT_AREA];
28882 g = e + r->used[TEXT_AREA];
28883 for (gx = r->x ; e < g; ++e)
28884 {
28885 if (EQ (e->object, object)
28886 && startpos <= e->charpos && e->charpos < endpos)
28887 break;
28888 gx += e->pixel_width;
28889 }
28890 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28891 hlinfo->mouse_face_end_x = gx;
28892 }
28893 }
28894
28895 #ifdef HAVE_WINDOW_SYSTEM
28896
28897 /* See if position X, Y is within a hot-spot of an image. */
28898
28899 static bool
28900 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28901 {
28902 if (!CONSP (hot_spot))
28903 return false;
28904
28905 if (EQ (XCAR (hot_spot), Qrect))
28906 {
28907 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28908 Lisp_Object rect = XCDR (hot_spot);
28909 Lisp_Object tem;
28910 if (!CONSP (rect))
28911 return false;
28912 if (!CONSP (XCAR (rect)))
28913 return false;
28914 if (!CONSP (XCDR (rect)))
28915 return false;
28916 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28917 return false;
28918 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28919 return false;
28920 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28921 return false;
28922 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28923 return false;
28924 return true;
28925 }
28926 else if (EQ (XCAR (hot_spot), Qcircle))
28927 {
28928 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28929 Lisp_Object circ = XCDR (hot_spot);
28930 Lisp_Object lr, lx0, ly0;
28931 if (CONSP (circ)
28932 && CONSP (XCAR (circ))
28933 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28934 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28935 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28936 {
28937 double r = XFLOATINT (lr);
28938 double dx = XINT (lx0) - x;
28939 double dy = XINT (ly0) - y;
28940 return (dx * dx + dy * dy <= r * r);
28941 }
28942 }
28943 else if (EQ (XCAR (hot_spot), Qpoly))
28944 {
28945 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28946 if (VECTORP (XCDR (hot_spot)))
28947 {
28948 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28949 Lisp_Object *poly = v->contents;
28950 ptrdiff_t n = v->header.size;
28951 ptrdiff_t i;
28952 bool inside = false;
28953 Lisp_Object lx, ly;
28954 int x0, y0;
28955
28956 /* Need an even number of coordinates, and at least 3 edges. */
28957 if (n < 6 || n & 1)
28958 return false;
28959
28960 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28961 If count is odd, we are inside polygon. Pixels on edges
28962 may or may not be included depending on actual geometry of the
28963 polygon. */
28964 if ((lx = poly[n-2], !INTEGERP (lx))
28965 || (ly = poly[n-1], !INTEGERP (lx)))
28966 return false;
28967 x0 = XINT (lx), y0 = XINT (ly);
28968 for (i = 0; i < n; i += 2)
28969 {
28970 int x1 = x0, y1 = y0;
28971 if ((lx = poly[i], !INTEGERP (lx))
28972 || (ly = poly[i+1], !INTEGERP (ly)))
28973 return false;
28974 x0 = XINT (lx), y0 = XINT (ly);
28975
28976 /* Does this segment cross the X line? */
28977 if (x0 >= x)
28978 {
28979 if (x1 >= x)
28980 continue;
28981 }
28982 else if (x1 < x)
28983 continue;
28984 if (y > y0 && y > y1)
28985 continue;
28986 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28987 inside = !inside;
28988 }
28989 return inside;
28990 }
28991 }
28992 return false;
28993 }
28994
28995 Lisp_Object
28996 find_hot_spot (Lisp_Object map, int x, int y)
28997 {
28998 while (CONSP (map))
28999 {
29000 if (CONSP (XCAR (map))
29001 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29002 return XCAR (map);
29003 map = XCDR (map);
29004 }
29005
29006 return Qnil;
29007 }
29008
29009 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29010 3, 3, 0,
29011 doc: /* Lookup in image map MAP coordinates X and Y.
29012 An image map is an alist where each element has the format (AREA ID PLIST).
29013 An AREA is specified as either a rectangle, a circle, or a polygon:
29014 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29015 pixel coordinates of the upper left and bottom right corners.
29016 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29017 and the radius of the circle; r may be a float or integer.
29018 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29019 vector describes one corner in the polygon.
29020 Returns the alist element for the first matching AREA in MAP. */)
29021 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29022 {
29023 if (NILP (map))
29024 return Qnil;
29025
29026 CHECK_NUMBER (x);
29027 CHECK_NUMBER (y);
29028
29029 return find_hot_spot (map,
29030 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29031 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29032 }
29033
29034
29035 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29036 static void
29037 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29038 {
29039 /* Do not change cursor shape while dragging mouse. */
29040 if (!NILP (do_mouse_tracking))
29041 return;
29042
29043 if (!NILP (pointer))
29044 {
29045 if (EQ (pointer, Qarrow))
29046 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29047 else if (EQ (pointer, Qhand))
29048 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29049 else if (EQ (pointer, Qtext))
29050 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29051 else if (EQ (pointer, intern ("hdrag")))
29052 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29053 else if (EQ (pointer, intern ("nhdrag")))
29054 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29055 #ifdef HAVE_X_WINDOWS
29056 else if (EQ (pointer, intern ("vdrag")))
29057 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29058 #endif
29059 else if (EQ (pointer, intern ("hourglass")))
29060 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29061 else if (EQ (pointer, Qmodeline))
29062 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29063 else
29064 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29065 }
29066
29067 if (cursor != No_Cursor)
29068 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29069 }
29070
29071 #endif /* HAVE_WINDOW_SYSTEM */
29072
29073 /* Take proper action when mouse has moved to the mode or header line
29074 or marginal area AREA of window W, x-position X and y-position Y.
29075 X is relative to the start of the text display area of W, so the
29076 width of bitmap areas and scroll bars must be subtracted to get a
29077 position relative to the start of the mode line. */
29078
29079 static void
29080 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29081 enum window_part area)
29082 {
29083 struct window *w = XWINDOW (window);
29084 struct frame *f = XFRAME (w->frame);
29085 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29086 #ifdef HAVE_WINDOW_SYSTEM
29087 Display_Info *dpyinfo;
29088 #endif
29089 Cursor cursor = No_Cursor;
29090 Lisp_Object pointer = Qnil;
29091 int dx, dy, width, height;
29092 ptrdiff_t charpos;
29093 Lisp_Object string, object = Qnil;
29094 Lisp_Object pos IF_LINT (= Qnil), help;
29095
29096 Lisp_Object mouse_face;
29097 int original_x_pixel = x;
29098 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29099 struct glyph_row *row IF_LINT (= 0);
29100
29101 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29102 {
29103 int x0;
29104 struct glyph *end;
29105
29106 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29107 returns them in row/column units! */
29108 string = mode_line_string (w, area, &x, &y, &charpos,
29109 &object, &dx, &dy, &width, &height);
29110
29111 row = (area == ON_MODE_LINE
29112 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29113 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29114
29115 /* Find the glyph under the mouse pointer. */
29116 if (row->mode_line_p && row->enabled_p)
29117 {
29118 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29119 end = glyph + row->used[TEXT_AREA];
29120
29121 for (x0 = original_x_pixel;
29122 glyph < end && x0 >= glyph->pixel_width;
29123 ++glyph)
29124 x0 -= glyph->pixel_width;
29125
29126 if (glyph >= end)
29127 glyph = NULL;
29128 }
29129 }
29130 else
29131 {
29132 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29133 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29134 returns them in row/column units! */
29135 string = marginal_area_string (w, area, &x, &y, &charpos,
29136 &object, &dx, &dy, &width, &height);
29137 }
29138
29139 help = Qnil;
29140
29141 #ifdef HAVE_WINDOW_SYSTEM
29142 if (IMAGEP (object))
29143 {
29144 Lisp_Object image_map, hotspot;
29145 if ((image_map = Fplist_get (XCDR (object), QCmap),
29146 !NILP (image_map))
29147 && (hotspot = find_hot_spot (image_map, dx, dy),
29148 CONSP (hotspot))
29149 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29150 {
29151 Lisp_Object plist;
29152
29153 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29154 If so, we could look for mouse-enter, mouse-leave
29155 properties in PLIST (and do something...). */
29156 hotspot = XCDR (hotspot);
29157 if (CONSP (hotspot)
29158 && (plist = XCAR (hotspot), CONSP (plist)))
29159 {
29160 pointer = Fplist_get (plist, Qpointer);
29161 if (NILP (pointer))
29162 pointer = Qhand;
29163 help = Fplist_get (plist, Qhelp_echo);
29164 if (!NILP (help))
29165 {
29166 help_echo_string = help;
29167 XSETWINDOW (help_echo_window, w);
29168 help_echo_object = w->contents;
29169 help_echo_pos = charpos;
29170 }
29171 }
29172 }
29173 if (NILP (pointer))
29174 pointer = Fplist_get (XCDR (object), QCpointer);
29175 }
29176 #endif /* HAVE_WINDOW_SYSTEM */
29177
29178 if (STRINGP (string))
29179 pos = make_number (charpos);
29180
29181 /* Set the help text and mouse pointer. If the mouse is on a part
29182 of the mode line without any text (e.g. past the right edge of
29183 the mode line text), use the default help text and pointer. */
29184 if (STRINGP (string) || area == ON_MODE_LINE)
29185 {
29186 /* Arrange to display the help by setting the global variables
29187 help_echo_string, help_echo_object, and help_echo_pos. */
29188 if (NILP (help))
29189 {
29190 if (STRINGP (string))
29191 help = Fget_text_property (pos, Qhelp_echo, string);
29192
29193 if (!NILP (help))
29194 {
29195 help_echo_string = help;
29196 XSETWINDOW (help_echo_window, w);
29197 help_echo_object = string;
29198 help_echo_pos = charpos;
29199 }
29200 else if (area == ON_MODE_LINE)
29201 {
29202 Lisp_Object default_help
29203 = buffer_local_value (Qmode_line_default_help_echo,
29204 w->contents);
29205
29206 if (STRINGP (default_help))
29207 {
29208 help_echo_string = default_help;
29209 XSETWINDOW (help_echo_window, w);
29210 help_echo_object = Qnil;
29211 help_echo_pos = -1;
29212 }
29213 }
29214 }
29215
29216 #ifdef HAVE_WINDOW_SYSTEM
29217 /* Change the mouse pointer according to what is under it. */
29218 if (FRAME_WINDOW_P (f))
29219 {
29220 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29221 || minibuf_level
29222 || NILP (Vresize_mini_windows));
29223
29224 dpyinfo = FRAME_DISPLAY_INFO (f);
29225 if (STRINGP (string))
29226 {
29227 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29228
29229 if (NILP (pointer))
29230 pointer = Fget_text_property (pos, Qpointer, string);
29231
29232 /* Change the mouse pointer according to what is under X/Y. */
29233 if (NILP (pointer)
29234 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29235 {
29236 Lisp_Object map;
29237 map = Fget_text_property (pos, Qlocal_map, string);
29238 if (!KEYMAPP (map))
29239 map = Fget_text_property (pos, Qkeymap, string);
29240 if (!KEYMAPP (map) && draggable)
29241 cursor = dpyinfo->vertical_scroll_bar_cursor;
29242 }
29243 }
29244 else if (draggable)
29245 /* Default mode-line pointer. */
29246 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29247 }
29248 #endif
29249 }
29250
29251 /* Change the mouse face according to what is under X/Y. */
29252 bool mouse_face_shown = false;
29253 if (STRINGP (string))
29254 {
29255 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29256 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29257 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29258 && glyph)
29259 {
29260 Lisp_Object b, e;
29261
29262 struct glyph * tmp_glyph;
29263
29264 int gpos;
29265 int gseq_length;
29266 int total_pixel_width;
29267 ptrdiff_t begpos, endpos, ignore;
29268
29269 int vpos, hpos;
29270
29271 b = Fprevious_single_property_change (make_number (charpos + 1),
29272 Qmouse_face, string, Qnil);
29273 if (NILP (b))
29274 begpos = 0;
29275 else
29276 begpos = XINT (b);
29277
29278 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29279 if (NILP (e))
29280 endpos = SCHARS (string);
29281 else
29282 endpos = XINT (e);
29283
29284 /* Calculate the glyph position GPOS of GLYPH in the
29285 displayed string, relative to the beginning of the
29286 highlighted part of the string.
29287
29288 Note: GPOS is different from CHARPOS. CHARPOS is the
29289 position of GLYPH in the internal string object. A mode
29290 line string format has structures which are converted to
29291 a flattened string by the Emacs Lisp interpreter. The
29292 internal string is an element of those structures. The
29293 displayed string is the flattened string. */
29294 tmp_glyph = row_start_glyph;
29295 while (tmp_glyph < glyph
29296 && (!(EQ (tmp_glyph->object, glyph->object)
29297 && begpos <= tmp_glyph->charpos
29298 && tmp_glyph->charpos < endpos)))
29299 tmp_glyph++;
29300 gpos = glyph - tmp_glyph;
29301
29302 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29303 the highlighted part of the displayed string to which
29304 GLYPH belongs. Note: GSEQ_LENGTH is different from
29305 SCHARS (STRING), because the latter returns the length of
29306 the internal string. */
29307 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29308 tmp_glyph > glyph
29309 && (!(EQ (tmp_glyph->object, glyph->object)
29310 && begpos <= tmp_glyph->charpos
29311 && tmp_glyph->charpos < endpos));
29312 tmp_glyph--)
29313 ;
29314 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29315
29316 /* Calculate the total pixel width of all the glyphs between
29317 the beginning of the highlighted area and GLYPH. */
29318 total_pixel_width = 0;
29319 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29320 total_pixel_width += tmp_glyph->pixel_width;
29321
29322 /* Pre calculation of re-rendering position. Note: X is in
29323 column units here, after the call to mode_line_string or
29324 marginal_area_string. */
29325 hpos = x - gpos;
29326 vpos = (area == ON_MODE_LINE
29327 ? (w->current_matrix)->nrows - 1
29328 : 0);
29329
29330 /* If GLYPH's position is included in the region that is
29331 already drawn in mouse face, we have nothing to do. */
29332 if ( EQ (window, hlinfo->mouse_face_window)
29333 && (!row->reversed_p
29334 ? (hlinfo->mouse_face_beg_col <= hpos
29335 && hpos < hlinfo->mouse_face_end_col)
29336 /* In R2L rows we swap BEG and END, see below. */
29337 : (hlinfo->mouse_face_end_col <= hpos
29338 && hpos < hlinfo->mouse_face_beg_col))
29339 && hlinfo->mouse_face_beg_row == vpos )
29340 return;
29341
29342 if (clear_mouse_face (hlinfo))
29343 cursor = No_Cursor;
29344
29345 if (!row->reversed_p)
29346 {
29347 hlinfo->mouse_face_beg_col = hpos;
29348 hlinfo->mouse_face_beg_x = original_x_pixel
29349 - (total_pixel_width + dx);
29350 hlinfo->mouse_face_end_col = hpos + gseq_length;
29351 hlinfo->mouse_face_end_x = 0;
29352 }
29353 else
29354 {
29355 /* In R2L rows, show_mouse_face expects BEG and END
29356 coordinates to be swapped. */
29357 hlinfo->mouse_face_end_col = hpos;
29358 hlinfo->mouse_face_end_x = original_x_pixel
29359 - (total_pixel_width + dx);
29360 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29361 hlinfo->mouse_face_beg_x = 0;
29362 }
29363
29364 hlinfo->mouse_face_beg_row = vpos;
29365 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29366 hlinfo->mouse_face_past_end = false;
29367 hlinfo->mouse_face_window = window;
29368
29369 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29370 charpos,
29371 0, &ignore,
29372 glyph->face_id,
29373 true);
29374 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29375 mouse_face_shown = true;
29376
29377 if (NILP (pointer))
29378 pointer = Qhand;
29379 }
29380 }
29381
29382 /* If mouse-face doesn't need to be shown, clear any existing
29383 mouse-face. */
29384 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29385 clear_mouse_face (hlinfo);
29386
29387 #ifdef HAVE_WINDOW_SYSTEM
29388 if (FRAME_WINDOW_P (f))
29389 define_frame_cursor1 (f, cursor, pointer);
29390 #endif
29391 }
29392
29393
29394 /* EXPORT:
29395 Take proper action when the mouse has moved to position X, Y on
29396 frame F with regards to highlighting portions of display that have
29397 mouse-face properties. Also de-highlight portions of display where
29398 the mouse was before, set the mouse pointer shape as appropriate
29399 for the mouse coordinates, and activate help echo (tooltips).
29400 X and Y can be negative or out of range. */
29401
29402 void
29403 note_mouse_highlight (struct frame *f, int x, int y)
29404 {
29405 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29406 enum window_part part = ON_NOTHING;
29407 Lisp_Object window;
29408 struct window *w;
29409 Cursor cursor = No_Cursor;
29410 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29411 struct buffer *b;
29412
29413 /* When a menu is active, don't highlight because this looks odd. */
29414 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29415 if (popup_activated ())
29416 return;
29417 #endif
29418
29419 if (!f->glyphs_initialized_p
29420 || f->pointer_invisible)
29421 return;
29422
29423 hlinfo->mouse_face_mouse_x = x;
29424 hlinfo->mouse_face_mouse_y = y;
29425 hlinfo->mouse_face_mouse_frame = f;
29426
29427 if (hlinfo->mouse_face_defer)
29428 return;
29429
29430 /* Which window is that in? */
29431 window = window_from_coordinates (f, x, y, &part, true);
29432
29433 /* If displaying active text in another window, clear that. */
29434 if (! EQ (window, hlinfo->mouse_face_window)
29435 /* Also clear if we move out of text area in same window. */
29436 || (!NILP (hlinfo->mouse_face_window)
29437 && !NILP (window)
29438 && part != ON_TEXT
29439 && part != ON_MODE_LINE
29440 && part != ON_HEADER_LINE))
29441 clear_mouse_face (hlinfo);
29442
29443 /* Not on a window -> return. */
29444 if (!WINDOWP (window))
29445 return;
29446
29447 /* Reset help_echo_string. It will get recomputed below. */
29448 help_echo_string = Qnil;
29449
29450 /* Convert to window-relative pixel coordinates. */
29451 w = XWINDOW (window);
29452 frame_to_window_pixel_xy (w, &x, &y);
29453
29454 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29455 /* Handle tool-bar window differently since it doesn't display a
29456 buffer. */
29457 if (EQ (window, f->tool_bar_window))
29458 {
29459 note_tool_bar_highlight (f, x, y);
29460 return;
29461 }
29462 #endif
29463
29464 /* Mouse is on the mode, header line or margin? */
29465 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29466 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29467 {
29468 note_mode_line_or_margin_highlight (window, x, y, part);
29469
29470 #ifdef HAVE_WINDOW_SYSTEM
29471 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29472 {
29473 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29474 /* Show non-text cursor (Bug#16647). */
29475 goto set_cursor;
29476 }
29477 else
29478 #endif
29479 return;
29480 }
29481
29482 #ifdef HAVE_WINDOW_SYSTEM
29483 if (part == ON_VERTICAL_BORDER)
29484 {
29485 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29486 help_echo_string = build_string ("drag-mouse-1: resize");
29487 }
29488 else if (part == ON_RIGHT_DIVIDER)
29489 {
29490 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29491 help_echo_string = build_string ("drag-mouse-1: resize");
29492 }
29493 else if (part == ON_BOTTOM_DIVIDER)
29494 if (! WINDOW_BOTTOMMOST_P (w)
29495 || minibuf_level
29496 || NILP (Vresize_mini_windows))
29497 {
29498 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29499 help_echo_string = build_string ("drag-mouse-1: resize");
29500 }
29501 else
29502 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29503 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29504 || part == ON_VERTICAL_SCROLL_BAR
29505 || part == ON_HORIZONTAL_SCROLL_BAR)
29506 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29507 else
29508 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29509 #endif
29510
29511 /* Are we in a window whose display is up to date?
29512 And verify the buffer's text has not changed. */
29513 b = XBUFFER (w->contents);
29514 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29515 {
29516 int hpos, vpos, dx, dy, area = LAST_AREA;
29517 ptrdiff_t pos;
29518 struct glyph *glyph;
29519 Lisp_Object object;
29520 Lisp_Object mouse_face = Qnil, position;
29521 Lisp_Object *overlay_vec = NULL;
29522 ptrdiff_t i, noverlays;
29523 struct buffer *obuf;
29524 ptrdiff_t obegv, ozv;
29525 bool same_region;
29526
29527 /* Find the glyph under X/Y. */
29528 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29529
29530 #ifdef HAVE_WINDOW_SYSTEM
29531 /* Look for :pointer property on image. */
29532 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29533 {
29534 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29535 if (img != NULL && IMAGEP (img->spec))
29536 {
29537 Lisp_Object image_map, hotspot;
29538 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29539 !NILP (image_map))
29540 && (hotspot = find_hot_spot (image_map,
29541 glyph->slice.img.x + dx,
29542 glyph->slice.img.y + dy),
29543 CONSP (hotspot))
29544 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29545 {
29546 Lisp_Object plist;
29547
29548 /* Could check XCAR (hotspot) to see if we enter/leave
29549 this hot-spot.
29550 If so, we could look for mouse-enter, mouse-leave
29551 properties in PLIST (and do something...). */
29552 hotspot = XCDR (hotspot);
29553 if (CONSP (hotspot)
29554 && (plist = XCAR (hotspot), CONSP (plist)))
29555 {
29556 pointer = Fplist_get (plist, Qpointer);
29557 if (NILP (pointer))
29558 pointer = Qhand;
29559 help_echo_string = Fplist_get (plist, Qhelp_echo);
29560 if (!NILP (help_echo_string))
29561 {
29562 help_echo_window = window;
29563 help_echo_object = glyph->object;
29564 help_echo_pos = glyph->charpos;
29565 }
29566 }
29567 }
29568 if (NILP (pointer))
29569 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29570 }
29571 }
29572 #endif /* HAVE_WINDOW_SYSTEM */
29573
29574 /* Clear mouse face if X/Y not over text. */
29575 if (glyph == NULL
29576 || area != TEXT_AREA
29577 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29578 /* Glyph's OBJECT is nil for glyphs inserted by the
29579 display engine for its internal purposes, like truncation
29580 and continuation glyphs and blanks beyond the end of
29581 line's text on text terminals. If we are over such a
29582 glyph, we are not over any text. */
29583 || NILP (glyph->object)
29584 /* R2L rows have a stretch glyph at their front, which
29585 stands for no text, whereas L2R rows have no glyphs at
29586 all beyond the end of text. Treat such stretch glyphs
29587 like we do with NULL glyphs in L2R rows. */
29588 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29589 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29590 && glyph->type == STRETCH_GLYPH
29591 && glyph->avoid_cursor_p))
29592 {
29593 if (clear_mouse_face (hlinfo))
29594 cursor = No_Cursor;
29595 #ifdef HAVE_WINDOW_SYSTEM
29596 if (FRAME_WINDOW_P (f) && NILP (pointer))
29597 {
29598 if (area != TEXT_AREA)
29599 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29600 else
29601 pointer = Vvoid_text_area_pointer;
29602 }
29603 #endif
29604 goto set_cursor;
29605 }
29606
29607 pos = glyph->charpos;
29608 object = glyph->object;
29609 if (!STRINGP (object) && !BUFFERP (object))
29610 goto set_cursor;
29611
29612 /* If we get an out-of-range value, return now; avoid an error. */
29613 if (BUFFERP (object) && pos > BUF_Z (b))
29614 goto set_cursor;
29615
29616 /* Make the window's buffer temporarily current for
29617 overlays_at and compute_char_face. */
29618 obuf = current_buffer;
29619 current_buffer = b;
29620 obegv = BEGV;
29621 ozv = ZV;
29622 BEGV = BEG;
29623 ZV = Z;
29624
29625 /* Is this char mouse-active or does it have help-echo? */
29626 position = make_number (pos);
29627
29628 USE_SAFE_ALLOCA;
29629
29630 if (BUFFERP (object))
29631 {
29632 /* Put all the overlays we want in a vector in overlay_vec. */
29633 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29634 /* Sort overlays into increasing priority order. */
29635 noverlays = sort_overlays (overlay_vec, noverlays, w);
29636 }
29637 else
29638 noverlays = 0;
29639
29640 if (NILP (Vmouse_highlight))
29641 {
29642 clear_mouse_face (hlinfo);
29643 goto check_help_echo;
29644 }
29645
29646 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29647
29648 if (same_region)
29649 cursor = No_Cursor;
29650
29651 /* Check mouse-face highlighting. */
29652 if (! same_region
29653 /* If there exists an overlay with mouse-face overlapping
29654 the one we are currently highlighting, we have to
29655 check if we enter the overlapping overlay, and then
29656 highlight only that. */
29657 || (OVERLAYP (hlinfo->mouse_face_overlay)
29658 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29659 {
29660 /* Find the highest priority overlay with a mouse-face. */
29661 Lisp_Object overlay = Qnil;
29662 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29663 {
29664 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29665 if (!NILP (mouse_face))
29666 overlay = overlay_vec[i];
29667 }
29668
29669 /* If we're highlighting the same overlay as before, there's
29670 no need to do that again. */
29671 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29672 goto check_help_echo;
29673 hlinfo->mouse_face_overlay = overlay;
29674
29675 /* Clear the display of the old active region, if any. */
29676 if (clear_mouse_face (hlinfo))
29677 cursor = No_Cursor;
29678
29679 /* If no overlay applies, get a text property. */
29680 if (NILP (overlay))
29681 mouse_face = Fget_text_property (position, Qmouse_face, object);
29682
29683 /* Next, compute the bounds of the mouse highlighting and
29684 display it. */
29685 if (!NILP (mouse_face) && STRINGP (object))
29686 {
29687 /* The mouse-highlighting comes from a display string
29688 with a mouse-face. */
29689 Lisp_Object s, e;
29690 ptrdiff_t ignore;
29691
29692 s = Fprevious_single_property_change
29693 (make_number (pos + 1), Qmouse_face, object, Qnil);
29694 e = Fnext_single_property_change
29695 (position, Qmouse_face, object, Qnil);
29696 if (NILP (s))
29697 s = make_number (0);
29698 if (NILP (e))
29699 e = make_number (SCHARS (object));
29700 mouse_face_from_string_pos (w, hlinfo, object,
29701 XINT (s), XINT (e));
29702 hlinfo->mouse_face_past_end = false;
29703 hlinfo->mouse_face_window = window;
29704 hlinfo->mouse_face_face_id
29705 = face_at_string_position (w, object, pos, 0, &ignore,
29706 glyph->face_id, true);
29707 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29708 cursor = No_Cursor;
29709 }
29710 else
29711 {
29712 /* The mouse-highlighting, if any, comes from an overlay
29713 or text property in the buffer. */
29714 Lisp_Object buffer IF_LINT (= Qnil);
29715 Lisp_Object disp_string IF_LINT (= Qnil);
29716
29717 if (STRINGP (object))
29718 {
29719 /* If we are on a display string with no mouse-face,
29720 check if the text under it has one. */
29721 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29722 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29723 pos = string_buffer_position (object, start);
29724 if (pos > 0)
29725 {
29726 mouse_face = get_char_property_and_overlay
29727 (make_number (pos), Qmouse_face, w->contents, &overlay);
29728 buffer = w->contents;
29729 disp_string = object;
29730 }
29731 }
29732 else
29733 {
29734 buffer = object;
29735 disp_string = Qnil;
29736 }
29737
29738 if (!NILP (mouse_face))
29739 {
29740 Lisp_Object before, after;
29741 Lisp_Object before_string, after_string;
29742 /* To correctly find the limits of mouse highlight
29743 in a bidi-reordered buffer, we must not use the
29744 optimization of limiting the search in
29745 previous-single-property-change and
29746 next-single-property-change, because
29747 rows_from_pos_range needs the real start and end
29748 positions to DTRT in this case. That's because
29749 the first row visible in a window does not
29750 necessarily display the character whose position
29751 is the smallest. */
29752 Lisp_Object lim1
29753 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29754 ? Fmarker_position (w->start)
29755 : Qnil;
29756 Lisp_Object lim2
29757 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29758 ? make_number (BUF_Z (XBUFFER (buffer))
29759 - w->window_end_pos)
29760 : Qnil;
29761
29762 if (NILP (overlay))
29763 {
29764 /* Handle the text property case. */
29765 before = Fprevious_single_property_change
29766 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29767 after = Fnext_single_property_change
29768 (make_number (pos), Qmouse_face, buffer, lim2);
29769 before_string = after_string = Qnil;
29770 }
29771 else
29772 {
29773 /* Handle the overlay case. */
29774 before = Foverlay_start (overlay);
29775 after = Foverlay_end (overlay);
29776 before_string = Foverlay_get (overlay, Qbefore_string);
29777 after_string = Foverlay_get (overlay, Qafter_string);
29778
29779 if (!STRINGP (before_string)) before_string = Qnil;
29780 if (!STRINGP (after_string)) after_string = Qnil;
29781 }
29782
29783 mouse_face_from_buffer_pos (window, hlinfo, pos,
29784 NILP (before)
29785 ? 1
29786 : XFASTINT (before),
29787 NILP (after)
29788 ? BUF_Z (XBUFFER (buffer))
29789 : XFASTINT (after),
29790 before_string, after_string,
29791 disp_string);
29792 cursor = No_Cursor;
29793 }
29794 }
29795 }
29796
29797 check_help_echo:
29798
29799 /* Look for a `help-echo' property. */
29800 if (NILP (help_echo_string)) {
29801 Lisp_Object help, overlay;
29802
29803 /* Check overlays first. */
29804 help = overlay = Qnil;
29805 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29806 {
29807 overlay = overlay_vec[i];
29808 help = Foverlay_get (overlay, Qhelp_echo);
29809 }
29810
29811 if (!NILP (help))
29812 {
29813 help_echo_string = help;
29814 help_echo_window = window;
29815 help_echo_object = overlay;
29816 help_echo_pos = pos;
29817 }
29818 else
29819 {
29820 Lisp_Object obj = glyph->object;
29821 ptrdiff_t charpos = glyph->charpos;
29822
29823 /* Try text properties. */
29824 if (STRINGP (obj)
29825 && charpos >= 0
29826 && charpos < SCHARS (obj))
29827 {
29828 help = Fget_text_property (make_number (charpos),
29829 Qhelp_echo, obj);
29830 if (NILP (help))
29831 {
29832 /* If the string itself doesn't specify a help-echo,
29833 see if the buffer text ``under'' it does. */
29834 struct glyph_row *r
29835 = MATRIX_ROW (w->current_matrix, vpos);
29836 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29837 ptrdiff_t p = string_buffer_position (obj, start);
29838 if (p > 0)
29839 {
29840 help = Fget_char_property (make_number (p),
29841 Qhelp_echo, w->contents);
29842 if (!NILP (help))
29843 {
29844 charpos = p;
29845 obj = w->contents;
29846 }
29847 }
29848 }
29849 }
29850 else if (BUFFERP (obj)
29851 && charpos >= BEGV
29852 && charpos < ZV)
29853 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29854 obj);
29855
29856 if (!NILP (help))
29857 {
29858 help_echo_string = help;
29859 help_echo_window = window;
29860 help_echo_object = obj;
29861 help_echo_pos = charpos;
29862 }
29863 }
29864 }
29865
29866 #ifdef HAVE_WINDOW_SYSTEM
29867 /* Look for a `pointer' property. */
29868 if (FRAME_WINDOW_P (f) && NILP (pointer))
29869 {
29870 /* Check overlays first. */
29871 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29872 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29873
29874 if (NILP (pointer))
29875 {
29876 Lisp_Object obj = glyph->object;
29877 ptrdiff_t charpos = glyph->charpos;
29878
29879 /* Try text properties. */
29880 if (STRINGP (obj)
29881 && charpos >= 0
29882 && charpos < SCHARS (obj))
29883 {
29884 pointer = Fget_text_property (make_number (charpos),
29885 Qpointer, obj);
29886 if (NILP (pointer))
29887 {
29888 /* If the string itself doesn't specify a pointer,
29889 see if the buffer text ``under'' it does. */
29890 struct glyph_row *r
29891 = MATRIX_ROW (w->current_matrix, vpos);
29892 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29893 ptrdiff_t p = string_buffer_position (obj, start);
29894 if (p > 0)
29895 pointer = Fget_char_property (make_number (p),
29896 Qpointer, w->contents);
29897 }
29898 }
29899 else if (BUFFERP (obj)
29900 && charpos >= BEGV
29901 && charpos < ZV)
29902 pointer = Fget_text_property (make_number (charpos),
29903 Qpointer, obj);
29904 }
29905 }
29906 #endif /* HAVE_WINDOW_SYSTEM */
29907
29908 BEGV = obegv;
29909 ZV = ozv;
29910 current_buffer = obuf;
29911 SAFE_FREE ();
29912 }
29913
29914 set_cursor:
29915
29916 #ifdef HAVE_WINDOW_SYSTEM
29917 if (FRAME_WINDOW_P (f))
29918 define_frame_cursor1 (f, cursor, pointer);
29919 #else
29920 /* This is here to prevent a compiler error, about "label at end of
29921 compound statement". */
29922 return;
29923 #endif
29924 }
29925
29926
29927 /* EXPORT for RIF:
29928 Clear any mouse-face on window W. This function is part of the
29929 redisplay interface, and is called from try_window_id and similar
29930 functions to ensure the mouse-highlight is off. */
29931
29932 void
29933 x_clear_window_mouse_face (struct window *w)
29934 {
29935 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29936 Lisp_Object window;
29937
29938 block_input ();
29939 XSETWINDOW (window, w);
29940 if (EQ (window, hlinfo->mouse_face_window))
29941 clear_mouse_face (hlinfo);
29942 unblock_input ();
29943 }
29944
29945
29946 /* EXPORT:
29947 Just discard the mouse face information for frame F, if any.
29948 This is used when the size of F is changed. */
29949
29950 void
29951 cancel_mouse_face (struct frame *f)
29952 {
29953 Lisp_Object window;
29954 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29955
29956 window = hlinfo->mouse_face_window;
29957 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29958 reset_mouse_highlight (hlinfo);
29959 }
29960
29961
29962 \f
29963 /***********************************************************************
29964 Exposure Events
29965 ***********************************************************************/
29966
29967 #ifdef HAVE_WINDOW_SYSTEM
29968
29969 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29970 which intersects rectangle R. R is in window-relative coordinates. */
29971
29972 static void
29973 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29974 enum glyph_row_area area)
29975 {
29976 struct glyph *first = row->glyphs[area];
29977 struct glyph *end = row->glyphs[area] + row->used[area];
29978 struct glyph *last;
29979 int first_x, start_x, x;
29980
29981 if (area == TEXT_AREA && row->fill_line_p)
29982 /* If row extends face to end of line write the whole line. */
29983 draw_glyphs (w, 0, row, area,
29984 0, row->used[area],
29985 DRAW_NORMAL_TEXT, 0);
29986 else
29987 {
29988 /* Set START_X to the window-relative start position for drawing glyphs of
29989 AREA. The first glyph of the text area can be partially visible.
29990 The first glyphs of other areas cannot. */
29991 start_x = window_box_left_offset (w, area);
29992 x = start_x;
29993 if (area == TEXT_AREA)
29994 x += row->x;
29995
29996 /* Find the first glyph that must be redrawn. */
29997 while (first < end
29998 && x + first->pixel_width < r->x)
29999 {
30000 x += first->pixel_width;
30001 ++first;
30002 }
30003
30004 /* Find the last one. */
30005 last = first;
30006 first_x = x;
30007 while (last < end
30008 && x < r->x + r->width)
30009 {
30010 x += last->pixel_width;
30011 ++last;
30012 }
30013
30014 /* Repaint. */
30015 if (last > first)
30016 draw_glyphs (w, first_x - start_x, row, area,
30017 first - row->glyphs[area], last - row->glyphs[area],
30018 DRAW_NORMAL_TEXT, 0);
30019 }
30020 }
30021
30022
30023 /* Redraw the parts of the glyph row ROW on window W intersecting
30024 rectangle R. R is in window-relative coordinates. Value is
30025 true if mouse-face was overwritten. */
30026
30027 static bool
30028 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30029 {
30030 eassert (row->enabled_p);
30031
30032 if (row->mode_line_p || w->pseudo_window_p)
30033 draw_glyphs (w, 0, row, TEXT_AREA,
30034 0, row->used[TEXT_AREA],
30035 DRAW_NORMAL_TEXT, 0);
30036 else
30037 {
30038 if (row->used[LEFT_MARGIN_AREA])
30039 expose_area (w, row, r, LEFT_MARGIN_AREA);
30040 if (row->used[TEXT_AREA])
30041 expose_area (w, row, r, TEXT_AREA);
30042 if (row->used[RIGHT_MARGIN_AREA])
30043 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30044 draw_row_fringe_bitmaps (w, row);
30045 }
30046
30047 return row->mouse_face_p;
30048 }
30049
30050
30051 /* Redraw those parts of glyphs rows during expose event handling that
30052 overlap other rows. Redrawing of an exposed line writes over parts
30053 of lines overlapping that exposed line; this function fixes that.
30054
30055 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30056 row in W's current matrix that is exposed and overlaps other rows.
30057 LAST_OVERLAPPING_ROW is the last such row. */
30058
30059 static void
30060 expose_overlaps (struct window *w,
30061 struct glyph_row *first_overlapping_row,
30062 struct glyph_row *last_overlapping_row,
30063 XRectangle *r)
30064 {
30065 struct glyph_row *row;
30066
30067 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30068 if (row->overlapping_p)
30069 {
30070 eassert (row->enabled_p && !row->mode_line_p);
30071
30072 row->clip = r;
30073 if (row->used[LEFT_MARGIN_AREA])
30074 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30075
30076 if (row->used[TEXT_AREA])
30077 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30078
30079 if (row->used[RIGHT_MARGIN_AREA])
30080 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30081 row->clip = NULL;
30082 }
30083 }
30084
30085
30086 /* Return true if W's cursor intersects rectangle R. */
30087
30088 static bool
30089 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30090 {
30091 XRectangle cr, result;
30092 struct glyph *cursor_glyph;
30093 struct glyph_row *row;
30094
30095 if (w->phys_cursor.vpos >= 0
30096 && w->phys_cursor.vpos < w->current_matrix->nrows
30097 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30098 row->enabled_p)
30099 && row->cursor_in_fringe_p)
30100 {
30101 /* Cursor is in the fringe. */
30102 cr.x = window_box_right_offset (w,
30103 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30104 ? RIGHT_MARGIN_AREA
30105 : TEXT_AREA));
30106 cr.y = row->y;
30107 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30108 cr.height = row->height;
30109 return x_intersect_rectangles (&cr, r, &result);
30110 }
30111
30112 cursor_glyph = get_phys_cursor_glyph (w);
30113 if (cursor_glyph)
30114 {
30115 /* r is relative to W's box, but w->phys_cursor.x is relative
30116 to left edge of W's TEXT area. Adjust it. */
30117 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30118 cr.y = w->phys_cursor.y;
30119 cr.width = cursor_glyph->pixel_width;
30120 cr.height = w->phys_cursor_height;
30121 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30122 I assume the effect is the same -- and this is portable. */
30123 return x_intersect_rectangles (&cr, r, &result);
30124 }
30125 /* If we don't understand the format, pretend we're not in the hot-spot. */
30126 return false;
30127 }
30128
30129
30130 /* EXPORT:
30131 Draw a vertical window border to the right of window W if W doesn't
30132 have vertical scroll bars. */
30133
30134 void
30135 x_draw_vertical_border (struct window *w)
30136 {
30137 struct frame *f = XFRAME (WINDOW_FRAME (w));
30138
30139 /* We could do better, if we knew what type of scroll-bar the adjacent
30140 windows (on either side) have... But we don't :-(
30141 However, I think this works ok. ++KFS 2003-04-25 */
30142
30143 /* Redraw borders between horizontally adjacent windows. Don't
30144 do it for frames with vertical scroll bars because either the
30145 right scroll bar of a window, or the left scroll bar of its
30146 neighbor will suffice as a border. */
30147 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30148 return;
30149
30150 /* Note: It is necessary to redraw both the left and the right
30151 borders, for when only this single window W is being
30152 redisplayed. */
30153 if (!WINDOW_RIGHTMOST_P (w)
30154 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30155 {
30156 int x0, x1, y0, y1;
30157
30158 window_box_edges (w, &x0, &y0, &x1, &y1);
30159 y1 -= 1;
30160
30161 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30162 x1 -= 1;
30163
30164 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30165 }
30166
30167 if (!WINDOW_LEFTMOST_P (w)
30168 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30169 {
30170 int x0, x1, y0, y1;
30171
30172 window_box_edges (w, &x0, &y0, &x1, &y1);
30173 y1 -= 1;
30174
30175 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30176 x0 -= 1;
30177
30178 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30179 }
30180 }
30181
30182
30183 /* Draw window dividers for window W. */
30184
30185 void
30186 x_draw_right_divider (struct window *w)
30187 {
30188 struct frame *f = WINDOW_XFRAME (w);
30189
30190 if (w->mini || w->pseudo_window_p)
30191 return;
30192 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30193 {
30194 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30195 int x1 = WINDOW_RIGHT_EDGE_X (w);
30196 int y0 = WINDOW_TOP_EDGE_Y (w);
30197 /* The bottom divider prevails. */
30198 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30199
30200 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30201 }
30202 }
30203
30204 static void
30205 x_draw_bottom_divider (struct window *w)
30206 {
30207 struct frame *f = XFRAME (WINDOW_FRAME (w));
30208
30209 if (w->mini || w->pseudo_window_p)
30210 return;
30211 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30212 {
30213 int x0 = WINDOW_LEFT_EDGE_X (w);
30214 int x1 = WINDOW_RIGHT_EDGE_X (w);
30215 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30216 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30217
30218 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30219 }
30220 }
30221
30222 /* Redraw the part of window W intersection rectangle FR. Pixel
30223 coordinates in FR are frame-relative. Call this function with
30224 input blocked. Value is true if the exposure overwrites
30225 mouse-face. */
30226
30227 static bool
30228 expose_window (struct window *w, XRectangle *fr)
30229 {
30230 struct frame *f = XFRAME (w->frame);
30231 XRectangle wr, r;
30232 bool mouse_face_overwritten_p = false;
30233
30234 /* If window is not yet fully initialized, do nothing. This can
30235 happen when toolkit scroll bars are used and a window is split.
30236 Reconfiguring the scroll bar will generate an expose for a newly
30237 created window. */
30238 if (w->current_matrix == NULL)
30239 return false;
30240
30241 /* When we're currently updating the window, display and current
30242 matrix usually don't agree. Arrange for a thorough display
30243 later. */
30244 if (w->must_be_updated_p)
30245 {
30246 SET_FRAME_GARBAGED (f);
30247 return false;
30248 }
30249
30250 /* Frame-relative pixel rectangle of W. */
30251 wr.x = WINDOW_LEFT_EDGE_X (w);
30252 wr.y = WINDOW_TOP_EDGE_Y (w);
30253 wr.width = WINDOW_PIXEL_WIDTH (w);
30254 wr.height = WINDOW_PIXEL_HEIGHT (w);
30255
30256 if (x_intersect_rectangles (fr, &wr, &r))
30257 {
30258 int yb = window_text_bottom_y (w);
30259 struct glyph_row *row;
30260 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30261
30262 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30263 r.x, r.y, r.width, r.height));
30264
30265 /* Convert to window coordinates. */
30266 r.x -= WINDOW_LEFT_EDGE_X (w);
30267 r.y -= WINDOW_TOP_EDGE_Y (w);
30268
30269 /* Turn off the cursor. */
30270 bool cursor_cleared_p = (!w->pseudo_window_p
30271 && phys_cursor_in_rect_p (w, &r));
30272 if (cursor_cleared_p)
30273 x_clear_cursor (w);
30274
30275 /* If the row containing the cursor extends face to end of line,
30276 then expose_area might overwrite the cursor outside the
30277 rectangle and thus notice_overwritten_cursor might clear
30278 w->phys_cursor_on_p. We remember the original value and
30279 check later if it is changed. */
30280 bool phys_cursor_on_p = w->phys_cursor_on_p;
30281
30282 /* Update lines intersecting rectangle R. */
30283 first_overlapping_row = last_overlapping_row = NULL;
30284 for (row = w->current_matrix->rows;
30285 row->enabled_p;
30286 ++row)
30287 {
30288 int y0 = row->y;
30289 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30290
30291 if ((y0 >= r.y && y0 < r.y + r.height)
30292 || (y1 > r.y && y1 < r.y + r.height)
30293 || (r.y >= y0 && r.y < y1)
30294 || (r.y + r.height > y0 && r.y + r.height < y1))
30295 {
30296 /* A header line may be overlapping, but there is no need
30297 to fix overlapping areas for them. KFS 2005-02-12 */
30298 if (row->overlapping_p && !row->mode_line_p)
30299 {
30300 if (first_overlapping_row == NULL)
30301 first_overlapping_row = row;
30302 last_overlapping_row = row;
30303 }
30304
30305 row->clip = fr;
30306 if (expose_line (w, row, &r))
30307 mouse_face_overwritten_p = true;
30308 row->clip = NULL;
30309 }
30310 else if (row->overlapping_p)
30311 {
30312 /* We must redraw a row overlapping the exposed area. */
30313 if (y0 < r.y
30314 ? y0 + row->phys_height > r.y
30315 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30316 {
30317 if (first_overlapping_row == NULL)
30318 first_overlapping_row = row;
30319 last_overlapping_row = row;
30320 }
30321 }
30322
30323 if (y1 >= yb)
30324 break;
30325 }
30326
30327 /* Display the mode line if there is one. */
30328 if (WINDOW_WANTS_MODELINE_P (w)
30329 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30330 row->enabled_p)
30331 && row->y < r.y + r.height)
30332 {
30333 if (expose_line (w, row, &r))
30334 mouse_face_overwritten_p = true;
30335 }
30336
30337 if (!w->pseudo_window_p)
30338 {
30339 /* Fix the display of overlapping rows. */
30340 if (first_overlapping_row)
30341 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30342 fr);
30343
30344 /* Draw border between windows. */
30345 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30346 x_draw_right_divider (w);
30347 else
30348 x_draw_vertical_border (w);
30349
30350 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30351 x_draw_bottom_divider (w);
30352
30353 /* Turn the cursor on again. */
30354 if (cursor_cleared_p
30355 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30356 update_window_cursor (w, true);
30357 }
30358 }
30359
30360 return mouse_face_overwritten_p;
30361 }
30362
30363
30364
30365 /* Redraw (parts) of all windows in the window tree rooted at W that
30366 intersect R. R contains frame pixel coordinates. Value is
30367 true if the exposure overwrites mouse-face. */
30368
30369 static bool
30370 expose_window_tree (struct window *w, XRectangle *r)
30371 {
30372 struct frame *f = XFRAME (w->frame);
30373 bool mouse_face_overwritten_p = false;
30374
30375 while (w && !FRAME_GARBAGED_P (f))
30376 {
30377 mouse_face_overwritten_p
30378 |= (WINDOWP (w->contents)
30379 ? expose_window_tree (XWINDOW (w->contents), r)
30380 : expose_window (w, r));
30381
30382 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30383 }
30384
30385 return mouse_face_overwritten_p;
30386 }
30387
30388
30389 /* EXPORT:
30390 Redisplay an exposed area of frame F. X and Y are the upper-left
30391 corner of the exposed rectangle. W and H are width and height of
30392 the exposed area. All are pixel values. W or H zero means redraw
30393 the entire frame. */
30394
30395 void
30396 expose_frame (struct frame *f, int x, int y, int w, int h)
30397 {
30398 XRectangle r;
30399 bool mouse_face_overwritten_p = false;
30400
30401 TRACE ((stderr, "expose_frame "));
30402
30403 /* No need to redraw if frame will be redrawn soon. */
30404 if (FRAME_GARBAGED_P (f))
30405 {
30406 TRACE ((stderr, " garbaged\n"));
30407 return;
30408 }
30409
30410 /* If basic faces haven't been realized yet, there is no point in
30411 trying to redraw anything. This can happen when we get an expose
30412 event while Emacs is starting, e.g. by moving another window. */
30413 if (FRAME_FACE_CACHE (f) == NULL
30414 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30415 {
30416 TRACE ((stderr, " no faces\n"));
30417 return;
30418 }
30419
30420 if (w == 0 || h == 0)
30421 {
30422 r.x = r.y = 0;
30423 r.width = FRAME_TEXT_WIDTH (f);
30424 r.height = FRAME_TEXT_HEIGHT (f);
30425 }
30426 else
30427 {
30428 r.x = x;
30429 r.y = y;
30430 r.width = w;
30431 r.height = h;
30432 }
30433
30434 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30435 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30436
30437 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30438 if (WINDOWP (f->tool_bar_window))
30439 mouse_face_overwritten_p
30440 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30441 #endif
30442
30443 #ifdef HAVE_X_WINDOWS
30444 #ifndef MSDOS
30445 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30446 if (WINDOWP (f->menu_bar_window))
30447 mouse_face_overwritten_p
30448 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30449 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30450 #endif
30451 #endif
30452
30453 /* Some window managers support a focus-follows-mouse style with
30454 delayed raising of frames. Imagine a partially obscured frame,
30455 and moving the mouse into partially obscured mouse-face on that
30456 frame. The visible part of the mouse-face will be highlighted,
30457 then the WM raises the obscured frame. With at least one WM, KDE
30458 2.1, Emacs is not getting any event for the raising of the frame
30459 (even tried with SubstructureRedirectMask), only Expose events.
30460 These expose events will draw text normally, i.e. not
30461 highlighted. Which means we must redo the highlight here.
30462 Subsume it under ``we love X''. --gerd 2001-08-15 */
30463 /* Included in Windows version because Windows most likely does not
30464 do the right thing if any third party tool offers
30465 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30466 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30467 {
30468 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30469 if (f == hlinfo->mouse_face_mouse_frame)
30470 {
30471 int mouse_x = hlinfo->mouse_face_mouse_x;
30472 int mouse_y = hlinfo->mouse_face_mouse_y;
30473 clear_mouse_face (hlinfo);
30474 note_mouse_highlight (f, mouse_x, mouse_y);
30475 }
30476 }
30477 }
30478
30479
30480 /* EXPORT:
30481 Determine the intersection of two rectangles R1 and R2. Return
30482 the intersection in *RESULT. Value is true if RESULT is not
30483 empty. */
30484
30485 bool
30486 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30487 {
30488 XRectangle *left, *right;
30489 XRectangle *upper, *lower;
30490 bool intersection_p = false;
30491
30492 /* Rearrange so that R1 is the left-most rectangle. */
30493 if (r1->x < r2->x)
30494 left = r1, right = r2;
30495 else
30496 left = r2, right = r1;
30497
30498 /* X0 of the intersection is right.x0, if this is inside R1,
30499 otherwise there is no intersection. */
30500 if (right->x <= left->x + left->width)
30501 {
30502 result->x = right->x;
30503
30504 /* The right end of the intersection is the minimum of
30505 the right ends of left and right. */
30506 result->width = (min (left->x + left->width, right->x + right->width)
30507 - result->x);
30508
30509 /* Same game for Y. */
30510 if (r1->y < r2->y)
30511 upper = r1, lower = r2;
30512 else
30513 upper = r2, lower = r1;
30514
30515 /* The upper end of the intersection is lower.y0, if this is inside
30516 of upper. Otherwise, there is no intersection. */
30517 if (lower->y <= upper->y + upper->height)
30518 {
30519 result->y = lower->y;
30520
30521 /* The lower end of the intersection is the minimum of the lower
30522 ends of upper and lower. */
30523 result->height = (min (lower->y + lower->height,
30524 upper->y + upper->height)
30525 - result->y);
30526 intersection_p = true;
30527 }
30528 }
30529
30530 return intersection_p;
30531 }
30532
30533 #endif /* HAVE_WINDOW_SYSTEM */
30534
30535 \f
30536 /***********************************************************************
30537 Initialization
30538 ***********************************************************************/
30539
30540 void
30541 syms_of_xdisp (void)
30542 {
30543 Vwith_echo_area_save_vector = Qnil;
30544 staticpro (&Vwith_echo_area_save_vector);
30545
30546 Vmessage_stack = Qnil;
30547 staticpro (&Vmessage_stack);
30548
30549 /* Non-nil means don't actually do any redisplay. */
30550 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30551
30552 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30553
30554 DEFVAR_BOOL("inhibit-message", inhibit_message,
30555 doc: /* Non-nil means calls to `message' are not displayed.
30556 They are still logged to the *Messages* buffer. */);
30557 inhibit_message = 0;
30558
30559 message_dolog_marker1 = Fmake_marker ();
30560 staticpro (&message_dolog_marker1);
30561 message_dolog_marker2 = Fmake_marker ();
30562 staticpro (&message_dolog_marker2);
30563 message_dolog_marker3 = Fmake_marker ();
30564 staticpro (&message_dolog_marker3);
30565
30566 #ifdef GLYPH_DEBUG
30567 defsubr (&Sdump_frame_glyph_matrix);
30568 defsubr (&Sdump_glyph_matrix);
30569 defsubr (&Sdump_glyph_row);
30570 defsubr (&Sdump_tool_bar_row);
30571 defsubr (&Strace_redisplay);
30572 defsubr (&Strace_to_stderr);
30573 #endif
30574 #ifdef HAVE_WINDOW_SYSTEM
30575 defsubr (&Stool_bar_height);
30576 defsubr (&Slookup_image_map);
30577 #endif
30578 defsubr (&Sline_pixel_height);
30579 defsubr (&Sformat_mode_line);
30580 defsubr (&Sinvisible_p);
30581 defsubr (&Scurrent_bidi_paragraph_direction);
30582 defsubr (&Swindow_text_pixel_size);
30583 defsubr (&Smove_point_visually);
30584 defsubr (&Sbidi_find_overridden_directionality);
30585
30586 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30587 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30588 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30589 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30590 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30591 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30592 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30593 DEFSYM (Qeval, "eval");
30594 DEFSYM (QCdata, ":data");
30595
30596 /* Names of text properties relevant for redisplay. */
30597 DEFSYM (Qdisplay, "display");
30598 DEFSYM (Qspace_width, "space-width");
30599 DEFSYM (Qraise, "raise");
30600 DEFSYM (Qslice, "slice");
30601 DEFSYM (Qspace, "space");
30602 DEFSYM (Qmargin, "margin");
30603 DEFSYM (Qpointer, "pointer");
30604 DEFSYM (Qleft_margin, "left-margin");
30605 DEFSYM (Qright_margin, "right-margin");
30606 DEFSYM (Qcenter, "center");
30607 DEFSYM (Qline_height, "line-height");
30608 DEFSYM (QCalign_to, ":align-to");
30609 DEFSYM (QCrelative_width, ":relative-width");
30610 DEFSYM (QCrelative_height, ":relative-height");
30611 DEFSYM (QCeval, ":eval");
30612 DEFSYM (QCpropertize, ":propertize");
30613 DEFSYM (QCfile, ":file");
30614 DEFSYM (Qfontified, "fontified");
30615 DEFSYM (Qfontification_functions, "fontification-functions");
30616
30617 /* Name of the face used to highlight trailing whitespace. */
30618 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30619
30620 /* Name and number of the face used to highlight escape glyphs. */
30621 DEFSYM (Qescape_glyph, "escape-glyph");
30622
30623 /* Name and number of the face used to highlight non-breaking spaces. */
30624 DEFSYM (Qnobreak_space, "nobreak-space");
30625
30626 /* The symbol 'image' which is the car of the lists used to represent
30627 images in Lisp. Also a tool bar style. */
30628 DEFSYM (Qimage, "image");
30629
30630 /* Tool bar styles. */
30631 DEFSYM (Qtext, "text");
30632 DEFSYM (Qboth, "both");
30633 DEFSYM (Qboth_horiz, "both-horiz");
30634 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30635
30636 /* The image map types. */
30637 DEFSYM (QCmap, ":map");
30638 DEFSYM (QCpointer, ":pointer");
30639 DEFSYM (Qrect, "rect");
30640 DEFSYM (Qcircle, "circle");
30641 DEFSYM (Qpoly, "poly");
30642
30643 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30644 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30645 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30646
30647 DEFSYM (Qgrow_only, "grow-only");
30648 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30649 DEFSYM (Qposition, "position");
30650 DEFSYM (Qbuffer_position, "buffer-position");
30651 DEFSYM (Qobject, "object");
30652
30653 /* Cursor shapes. */
30654 DEFSYM (Qbar, "bar");
30655 DEFSYM (Qhbar, "hbar");
30656 DEFSYM (Qbox, "box");
30657 DEFSYM (Qhollow, "hollow");
30658
30659 /* Pointer shapes. */
30660 DEFSYM (Qhand, "hand");
30661 DEFSYM (Qarrow, "arrow");
30662 /* also Qtext */
30663
30664 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30665
30666 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30667 staticpro (&list_of_error);
30668
30669 /* Values of those variables at last redisplay are stored as
30670 properties on 'overlay-arrow-position' symbol. However, if
30671 Voverlay_arrow_position is a marker, last-arrow-position is its
30672 numerical position. */
30673 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30674 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30675
30676 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30677 properties on a symbol in overlay-arrow-variable-list. */
30678 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30679 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30680
30681 echo_buffer[0] = echo_buffer[1] = Qnil;
30682 staticpro (&echo_buffer[0]);
30683 staticpro (&echo_buffer[1]);
30684
30685 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30686 staticpro (&echo_area_buffer[0]);
30687 staticpro (&echo_area_buffer[1]);
30688
30689 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30690 staticpro (&Vmessages_buffer_name);
30691
30692 mode_line_proptrans_alist = Qnil;
30693 staticpro (&mode_line_proptrans_alist);
30694 mode_line_string_list = Qnil;
30695 staticpro (&mode_line_string_list);
30696 mode_line_string_face = Qnil;
30697 staticpro (&mode_line_string_face);
30698 mode_line_string_face_prop = Qnil;
30699 staticpro (&mode_line_string_face_prop);
30700 Vmode_line_unwind_vector = Qnil;
30701 staticpro (&Vmode_line_unwind_vector);
30702
30703 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30704
30705 help_echo_string = Qnil;
30706 staticpro (&help_echo_string);
30707 help_echo_object = Qnil;
30708 staticpro (&help_echo_object);
30709 help_echo_window = Qnil;
30710 staticpro (&help_echo_window);
30711 previous_help_echo_string = Qnil;
30712 staticpro (&previous_help_echo_string);
30713 help_echo_pos = -1;
30714
30715 DEFSYM (Qright_to_left, "right-to-left");
30716 DEFSYM (Qleft_to_right, "left-to-right");
30717 defsubr (&Sbidi_resolved_levels);
30718
30719 #ifdef HAVE_WINDOW_SYSTEM
30720 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30721 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30722 For example, if a block cursor is over a tab, it will be drawn as
30723 wide as that tab on the display. */);
30724 x_stretch_cursor_p = 0;
30725 #endif
30726
30727 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30728 doc: /* Non-nil means highlight trailing whitespace.
30729 The face used for trailing whitespace is `trailing-whitespace'. */);
30730 Vshow_trailing_whitespace = Qnil;
30731
30732 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30733 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30734 If the value is t, Emacs highlights non-ASCII chars which have the
30735 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30736 or `escape-glyph' face respectively.
30737
30738 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30739 U+2011 (non-breaking hyphen) are affected.
30740
30741 Any other non-nil value means to display these characters as a escape
30742 glyph followed by an ordinary space or hyphen.
30743
30744 A value of nil means no special handling of these characters. */);
30745 Vnobreak_char_display = Qt;
30746
30747 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30748 doc: /* The pointer shape to show in void text areas.
30749 A value of nil means to show the text pointer. Other options are
30750 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30751 `hourglass'. */);
30752 Vvoid_text_area_pointer = Qarrow;
30753
30754 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30755 doc: /* Non-nil means don't actually do any redisplay.
30756 This is used for internal purposes. */);
30757 Vinhibit_redisplay = Qnil;
30758
30759 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30760 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30761 Vglobal_mode_string = Qnil;
30762
30763 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30764 doc: /* Marker for where to display an arrow on top of the buffer text.
30765 This must be the beginning of a line in order to work.
30766 See also `overlay-arrow-string'. */);
30767 Voverlay_arrow_position = Qnil;
30768
30769 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30770 doc: /* String to display as an arrow in non-window frames.
30771 See also `overlay-arrow-position'. */);
30772 Voverlay_arrow_string = build_pure_c_string ("=>");
30773
30774 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30775 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30776 The symbols on this list are examined during redisplay to determine
30777 where to display overlay arrows. */);
30778 Voverlay_arrow_variable_list
30779 = list1 (intern_c_string ("overlay-arrow-position"));
30780
30781 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30782 doc: /* The number of lines to try scrolling a window by when point moves out.
30783 If that fails to bring point back on frame, point is centered instead.
30784 If this is zero, point is always centered after it moves off frame.
30785 If you want scrolling to always be a line at a time, you should set
30786 `scroll-conservatively' to a large value rather than set this to 1. */);
30787
30788 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30789 doc: /* Scroll up to this many lines, to bring point back on screen.
30790 If point moves off-screen, redisplay will scroll by up to
30791 `scroll-conservatively' lines in order to bring point just barely
30792 onto the screen again. If that cannot be done, then redisplay
30793 recenters point as usual.
30794
30795 If the value is greater than 100, redisplay will never recenter point,
30796 but will always scroll just enough text to bring point into view, even
30797 if you move far away.
30798
30799 A value of zero means always recenter point if it moves off screen. */);
30800 scroll_conservatively = 0;
30801
30802 DEFVAR_INT ("scroll-margin", scroll_margin,
30803 doc: /* Number of lines of margin at the top and bottom of a window.
30804 Recenter the window whenever point gets within this many lines
30805 of the top or bottom of the window. */);
30806 scroll_margin = 0;
30807
30808 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30809 doc: /* Pixels per inch value for non-window system displays.
30810 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30811 Vdisplay_pixels_per_inch = make_float (72.0);
30812
30813 #ifdef GLYPH_DEBUG
30814 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30815 #endif
30816
30817 DEFVAR_LISP ("truncate-partial-width-windows",
30818 Vtruncate_partial_width_windows,
30819 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30820 For an integer value, truncate lines in each window narrower than the
30821 full frame width, provided the window width is less than that integer;
30822 otherwise, respect the value of `truncate-lines'.
30823
30824 For any other non-nil value, truncate lines in all windows that do
30825 not span the full frame width.
30826
30827 A value of nil means to respect the value of `truncate-lines'.
30828
30829 If `word-wrap' is enabled, you might want to reduce this. */);
30830 Vtruncate_partial_width_windows = make_number (50);
30831
30832 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30833 doc: /* Maximum buffer size for which line number should be displayed.
30834 If the buffer is bigger than this, the line number does not appear
30835 in the mode line. A value of nil means no limit. */);
30836 Vline_number_display_limit = Qnil;
30837
30838 DEFVAR_INT ("line-number-display-limit-width",
30839 line_number_display_limit_width,
30840 doc: /* Maximum line width (in characters) for line number display.
30841 If the average length of the lines near point is bigger than this, then the
30842 line number may be omitted from the mode line. */);
30843 line_number_display_limit_width = 200;
30844
30845 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30846 doc: /* Non-nil means highlight region even in nonselected windows. */);
30847 highlight_nonselected_windows = false;
30848
30849 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30850 doc: /* Non-nil if more than one frame is visible on this display.
30851 Minibuffer-only frames don't count, but iconified frames do.
30852 This variable is not guaranteed to be accurate except while processing
30853 `frame-title-format' and `icon-title-format'. */);
30854
30855 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30856 doc: /* Template for displaying the title bar of visible frames.
30857 \(Assuming the window manager supports this feature.)
30858
30859 This variable has the same structure as `mode-line-format', except that
30860 the %c and %l constructs are ignored. It is used only on frames for
30861 which no explicit name has been set \(see `modify-frame-parameters'). */);
30862
30863 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30864 doc: /* Template for displaying the title bar of an iconified frame.
30865 \(Assuming the window manager supports this feature.)
30866 This variable has the same structure as `mode-line-format' (which see),
30867 and is used only on frames for which no explicit name has been set
30868 \(see `modify-frame-parameters'). */);
30869 Vicon_title_format
30870 = Vframe_title_format
30871 = listn (CONSTYPE_PURE, 3,
30872 intern_c_string ("multiple-frames"),
30873 build_pure_c_string ("%b"),
30874 listn (CONSTYPE_PURE, 4,
30875 empty_unibyte_string,
30876 intern_c_string ("invocation-name"),
30877 build_pure_c_string ("@"),
30878 intern_c_string ("system-name")));
30879
30880 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30881 doc: /* Maximum number of lines to keep in the message log buffer.
30882 If nil, disable message logging. If t, log messages but don't truncate
30883 the buffer when it becomes large. */);
30884 Vmessage_log_max = make_number (1000);
30885
30886 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30887 doc: /* Functions called before redisplay, if window sizes have changed.
30888 The value should be a list of functions that take one argument.
30889 Just before redisplay, for each frame, if any of its windows have changed
30890 size since the last redisplay, or have been split or deleted,
30891 all the functions in the list are called, with the frame as argument. */);
30892 Vwindow_size_change_functions = Qnil;
30893
30894 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30895 doc: /* List of functions to call before redisplaying a window with scrolling.
30896 Each function is called with two arguments, the window and its new
30897 display-start position.
30898 These functions are called whenever the `window-start' marker is modified,
30899 either to point into another buffer (e.g. via `set-window-buffer') or another
30900 place in the same buffer.
30901 Note that the value of `window-end' is not valid when these functions are
30902 called.
30903
30904 Warning: Do not use this feature to alter the way the window
30905 is scrolled. It is not designed for that, and such use probably won't
30906 work. */);
30907 Vwindow_scroll_functions = Qnil;
30908
30909 DEFVAR_LISP ("window-text-change-functions",
30910 Vwindow_text_change_functions,
30911 doc: /* Functions to call in redisplay when text in the window might change. */);
30912 Vwindow_text_change_functions = Qnil;
30913
30914 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30915 doc: /* Functions called when redisplay of a window reaches the end trigger.
30916 Each function is called with two arguments, the window and the end trigger value.
30917 See `set-window-redisplay-end-trigger'. */);
30918 Vredisplay_end_trigger_functions = Qnil;
30919
30920 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30921 doc: /* Non-nil means autoselect window with mouse pointer.
30922 If nil, do not autoselect windows.
30923 A positive number means delay autoselection by that many seconds: a
30924 window is autoselected only after the mouse has remained in that
30925 window for the duration of the delay.
30926 A negative number has a similar effect, but causes windows to be
30927 autoselected only after the mouse has stopped moving. \(Because of
30928 the way Emacs compares mouse events, you will occasionally wait twice
30929 that time before the window gets selected.\)
30930 Any other value means to autoselect window instantaneously when the
30931 mouse pointer enters it.
30932
30933 Autoselection selects the minibuffer only if it is active, and never
30934 unselects the minibuffer if it is active.
30935
30936 When customizing this variable make sure that the actual value of
30937 `focus-follows-mouse' matches the behavior of your window manager. */);
30938 Vmouse_autoselect_window = Qnil;
30939
30940 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30941 doc: /* Non-nil means automatically resize tool-bars.
30942 This dynamically changes the tool-bar's height to the minimum height
30943 that is needed to make all tool-bar items visible.
30944 If value is `grow-only', the tool-bar's height is only increased
30945 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30946 Vauto_resize_tool_bars = Qt;
30947
30948 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30949 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30950 auto_raise_tool_bar_buttons_p = true;
30951
30952 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30953 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30954 make_cursor_line_fully_visible_p = true;
30955
30956 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30957 doc: /* Border below tool-bar in pixels.
30958 If an integer, use it as the height of the border.
30959 If it is one of `internal-border-width' or `border-width', use the
30960 value of the corresponding frame parameter.
30961 Otherwise, no border is added below the tool-bar. */);
30962 Vtool_bar_border = Qinternal_border_width;
30963
30964 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30965 doc: /* Margin around tool-bar buttons in pixels.
30966 If an integer, use that for both horizontal and vertical margins.
30967 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30968 HORZ specifying the horizontal margin, and VERT specifying the
30969 vertical margin. */);
30970 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30971
30972 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30973 doc: /* Relief thickness of tool-bar buttons. */);
30974 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30975
30976 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30977 doc: /* Tool bar style to use.
30978 It can be one of
30979 image - show images only
30980 text - show text only
30981 both - show both, text below image
30982 both-horiz - show text to the right of the image
30983 text-image-horiz - show text to the left of the image
30984 any other - use system default or image if no system default.
30985
30986 This variable only affects the GTK+ toolkit version of Emacs. */);
30987 Vtool_bar_style = Qnil;
30988
30989 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30990 doc: /* Maximum number of characters a label can have to be shown.
30991 The tool bar style must also show labels for this to have any effect, see
30992 `tool-bar-style'. */);
30993 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30994
30995 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30996 doc: /* List of functions to call to fontify regions of text.
30997 Each function is called with one argument POS. Functions must
30998 fontify a region starting at POS in the current buffer, and give
30999 fontified regions the property `fontified'. */);
31000 Vfontification_functions = Qnil;
31001 Fmake_variable_buffer_local (Qfontification_functions);
31002
31003 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31004 unibyte_display_via_language_environment,
31005 doc: /* Non-nil means display unibyte text according to language environment.
31006 Specifically, this means that raw bytes in the range 160-255 decimal
31007 are displayed by converting them to the equivalent multibyte characters
31008 according to the current language environment. As a result, they are
31009 displayed according to the current fontset.
31010
31011 Note that this variable affects only how these bytes are displayed,
31012 but does not change the fact they are interpreted as raw bytes. */);
31013 unibyte_display_via_language_environment = false;
31014
31015 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31016 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31017 If a float, it specifies a fraction of the mini-window frame's height.
31018 If an integer, it specifies a number of lines. */);
31019 Vmax_mini_window_height = make_float (0.25);
31020
31021 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31022 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31023 A value of nil means don't automatically resize mini-windows.
31024 A value of t means resize them to fit the text displayed in them.
31025 A value of `grow-only', the default, means let mini-windows grow only;
31026 they return to their normal size when the minibuffer is closed, or the
31027 echo area becomes empty. */);
31028 Vresize_mini_windows = Qgrow_only;
31029
31030 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31031 doc: /* Alist specifying how to blink the cursor off.
31032 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31033 `cursor-type' frame-parameter or variable equals ON-STATE,
31034 comparing using `equal', Emacs uses OFF-STATE to specify
31035 how to blink it off. ON-STATE and OFF-STATE are values for
31036 the `cursor-type' frame parameter.
31037
31038 If a frame's ON-STATE has no entry in this list,
31039 the frame's other specifications determine how to blink the cursor off. */);
31040 Vblink_cursor_alist = Qnil;
31041
31042 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31043 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31044 If non-nil, windows are automatically scrolled horizontally to make
31045 point visible. */);
31046 automatic_hscrolling_p = true;
31047 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31048
31049 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31050 doc: /* How many columns away from the window edge point is allowed to get
31051 before automatic hscrolling will horizontally scroll the window. */);
31052 hscroll_margin = 5;
31053
31054 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31055 doc: /* How many columns to scroll the window when point gets too close to the edge.
31056 When point is less than `hscroll-margin' columns from the window
31057 edge, automatic hscrolling will scroll the window by the amount of columns
31058 determined by this variable. If its value is a positive integer, scroll that
31059 many columns. If it's a positive floating-point number, it specifies the
31060 fraction of the window's width to scroll. If it's nil or zero, point will be
31061 centered horizontally after the scroll. Any other value, including negative
31062 numbers, are treated as if the value were zero.
31063
31064 Automatic hscrolling always moves point outside the scroll margin, so if
31065 point was more than scroll step columns inside the margin, the window will
31066 scroll more than the value given by the scroll step.
31067
31068 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31069 and `scroll-right' overrides this variable's effect. */);
31070 Vhscroll_step = make_number (0);
31071
31072 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31073 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31074 Bind this around calls to `message' to let it take effect. */);
31075 message_truncate_lines = false;
31076
31077 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31078 doc: /* Normal hook run to update the menu bar definitions.
31079 Redisplay runs this hook before it redisplays the menu bar.
31080 This is used to update menus such as Buffers, whose contents depend on
31081 various data. */);
31082 Vmenu_bar_update_hook = Qnil;
31083
31084 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31085 doc: /* Frame for which we are updating a menu.
31086 The enable predicate for a menu binding should check this variable. */);
31087 Vmenu_updating_frame = Qnil;
31088
31089 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31090 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31091 inhibit_menubar_update = false;
31092
31093 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31094 doc: /* Prefix prepended to all continuation lines at display time.
31095 The value may be a string, an image, or a stretch-glyph; it is
31096 interpreted in the same way as the value of a `display' text property.
31097
31098 This variable is overridden by any `wrap-prefix' text or overlay
31099 property.
31100
31101 To add a prefix to non-continuation lines, use `line-prefix'. */);
31102 Vwrap_prefix = Qnil;
31103 DEFSYM (Qwrap_prefix, "wrap-prefix");
31104 Fmake_variable_buffer_local (Qwrap_prefix);
31105
31106 DEFVAR_LISP ("line-prefix", Vline_prefix,
31107 doc: /* Prefix prepended to all non-continuation lines at display time.
31108 The value may be a string, an image, or a stretch-glyph; it is
31109 interpreted in the same way as the value of a `display' text property.
31110
31111 This variable is overridden by any `line-prefix' text or overlay
31112 property.
31113
31114 To add a prefix to continuation lines, use `wrap-prefix'. */);
31115 Vline_prefix = Qnil;
31116 DEFSYM (Qline_prefix, "line-prefix");
31117 Fmake_variable_buffer_local (Qline_prefix);
31118
31119 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31120 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31121 inhibit_eval_during_redisplay = false;
31122
31123 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31124 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31125 inhibit_free_realized_faces = false;
31126
31127 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31128 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31129 Intended for use during debugging and for testing bidi display;
31130 see biditest.el in the test suite. */);
31131 inhibit_bidi_mirroring = false;
31132
31133 #ifdef GLYPH_DEBUG
31134 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31135 doc: /* Inhibit try_window_id display optimization. */);
31136 inhibit_try_window_id = false;
31137
31138 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31139 doc: /* Inhibit try_window_reusing display optimization. */);
31140 inhibit_try_window_reusing = false;
31141
31142 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31143 doc: /* Inhibit try_cursor_movement display optimization. */);
31144 inhibit_try_cursor_movement = false;
31145 #endif /* GLYPH_DEBUG */
31146
31147 DEFVAR_INT ("overline-margin", overline_margin,
31148 doc: /* Space between overline and text, in pixels.
31149 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31150 margin to the character height. */);
31151 overline_margin = 2;
31152
31153 DEFVAR_INT ("underline-minimum-offset",
31154 underline_minimum_offset,
31155 doc: /* Minimum distance between baseline and underline.
31156 This can improve legibility of underlined text at small font sizes,
31157 particularly when using variable `x-use-underline-position-properties'
31158 with fonts that specify an UNDERLINE_POSITION relatively close to the
31159 baseline. The default value is 1. */);
31160 underline_minimum_offset = 1;
31161
31162 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31163 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31164 This feature only works when on a window system that can change
31165 cursor shapes. */);
31166 display_hourglass_p = true;
31167
31168 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31169 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31170 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31171
31172 #ifdef HAVE_WINDOW_SYSTEM
31173 hourglass_atimer = NULL;
31174 hourglass_shown_p = false;
31175 #endif /* HAVE_WINDOW_SYSTEM */
31176
31177 /* Name of the face used to display glyphless characters. */
31178 DEFSYM (Qglyphless_char, "glyphless-char");
31179
31180 /* Method symbols for Vglyphless_char_display. */
31181 DEFSYM (Qhex_code, "hex-code");
31182 DEFSYM (Qempty_box, "empty-box");
31183 DEFSYM (Qthin_space, "thin-space");
31184 DEFSYM (Qzero_width, "zero-width");
31185
31186 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31187 doc: /* Function run just before redisplay.
31188 It is called with one argument, which is the set of windows that are to
31189 be redisplayed. This set can be nil (meaning, only the selected window),
31190 or t (meaning all windows). */);
31191 Vpre_redisplay_function = intern ("ignore");
31192
31193 /* Symbol for the purpose of Vglyphless_char_display. */
31194 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31195 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31196
31197 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31198 doc: /* Char-table defining glyphless characters.
31199 Each element, if non-nil, should be one of the following:
31200 an ASCII acronym string: display this string in a box
31201 `hex-code': display the hexadecimal code of a character in a box
31202 `empty-box': display as an empty box
31203 `thin-space': display as 1-pixel width space
31204 `zero-width': don't display
31205 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31206 display method for graphical terminals and text terminals respectively.
31207 GRAPHICAL and TEXT should each have one of the values listed above.
31208
31209 The char-table has one extra slot to control the display of a character for
31210 which no font is found. This slot only takes effect on graphical terminals.
31211 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31212 `thin-space'. The default is `empty-box'.
31213
31214 If a character has a non-nil entry in an active display table, the
31215 display table takes effect; in this case, Emacs does not consult
31216 `glyphless-char-display' at all. */);
31217 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31218 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31219 Qempty_box);
31220
31221 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31222 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31223 Vdebug_on_message = Qnil;
31224
31225 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31226 doc: /* */);
31227 Vredisplay__all_windows_cause
31228 = Fmake_vector (make_number (100), make_number (0));
31229
31230 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31231 doc: /* */);
31232 Vredisplay__mode_lines_cause
31233 = Fmake_vector (make_number (100), make_number (0));
31234 }
31235
31236
31237 /* Initialize this module when Emacs starts. */
31238
31239 void
31240 init_xdisp (void)
31241 {
31242 CHARPOS (this_line_start_pos) = 0;
31243
31244 if (!noninteractive)
31245 {
31246 struct window *m = XWINDOW (minibuf_window);
31247 Lisp_Object frame = m->frame;
31248 struct frame *f = XFRAME (frame);
31249 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31250 struct window *r = XWINDOW (root);
31251 int i;
31252
31253 echo_area_window = minibuf_window;
31254
31255 r->top_line = FRAME_TOP_MARGIN (f);
31256 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31257 r->total_cols = FRAME_COLS (f);
31258 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31259 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31260 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31261
31262 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31263 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31264 m->total_cols = FRAME_COLS (f);
31265 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31266 m->total_lines = 1;
31267 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31268
31269 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31270 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31271 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31272
31273 /* The default ellipsis glyphs `...'. */
31274 for (i = 0; i < 3; ++i)
31275 default_invis_vector[i] = make_number ('.');
31276 }
31277
31278 {
31279 /* Allocate the buffer for frame titles.
31280 Also used for `format-mode-line'. */
31281 int size = 100;
31282 mode_line_noprop_buf = xmalloc (size);
31283 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31284 mode_line_noprop_ptr = mode_line_noprop_buf;
31285 mode_line_target = MODE_LINE_DISPLAY;
31286 }
31287
31288 help_echo_showing_p = false;
31289 }
31290
31291 #ifdef HAVE_WINDOW_SYSTEM
31292
31293 /* Platform-independent portion of hourglass implementation. */
31294
31295 /* Timer function of hourglass_atimer. */
31296
31297 static void
31298 show_hourglass (struct atimer *timer)
31299 {
31300 /* The timer implementation will cancel this timer automatically
31301 after this function has run. Set hourglass_atimer to null
31302 so that we know the timer doesn't have to be canceled. */
31303 hourglass_atimer = NULL;
31304
31305 if (!hourglass_shown_p)
31306 {
31307 Lisp_Object tail, frame;
31308
31309 block_input ();
31310
31311 FOR_EACH_FRAME (tail, frame)
31312 {
31313 struct frame *f = XFRAME (frame);
31314
31315 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31316 && FRAME_RIF (f)->show_hourglass)
31317 FRAME_RIF (f)->show_hourglass (f);
31318 }
31319
31320 hourglass_shown_p = true;
31321 unblock_input ();
31322 }
31323 }
31324
31325 /* Cancel a currently active hourglass timer, and start a new one. */
31326
31327 void
31328 start_hourglass (void)
31329 {
31330 struct timespec delay;
31331
31332 cancel_hourglass ();
31333
31334 if (INTEGERP (Vhourglass_delay)
31335 && XINT (Vhourglass_delay) > 0)
31336 delay = make_timespec (min (XINT (Vhourglass_delay),
31337 TYPE_MAXIMUM (time_t)),
31338 0);
31339 else if (FLOATP (Vhourglass_delay)
31340 && XFLOAT_DATA (Vhourglass_delay) > 0)
31341 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31342 else
31343 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31344
31345 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31346 show_hourglass, NULL);
31347 }
31348
31349 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31350 shown. */
31351
31352 void
31353 cancel_hourglass (void)
31354 {
31355 if (hourglass_atimer)
31356 {
31357 cancel_atimer (hourglass_atimer);
31358 hourglass_atimer = NULL;
31359 }
31360
31361 if (hourglass_shown_p)
31362 {
31363 Lisp_Object tail, frame;
31364
31365 block_input ();
31366
31367 FOR_EACH_FRAME (tail, frame)
31368 {
31369 struct frame *f = XFRAME (frame);
31370
31371 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31372 && FRAME_RIF (f)->hide_hourglass)
31373 FRAME_RIF (f)->hide_hourglass (f);
31374 #ifdef HAVE_NTGUI
31375 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31376 else if (!FRAME_W32_P (f))
31377 w32_arrow_cursor ();
31378 #endif
31379 }
31380
31381 hourglass_shown_p = false;
31382 unblock_input ();
31383 }
31384 }
31385
31386 #endif /* HAVE_WINDOW_SYSTEM */