]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Fix display of stretch glyphs with large 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 int normal_char_height (struct font *);
837 static void append_stretch_glyph (struct it *, Lisp_Object,
838 int, int, int);
839
840
841 #endif /* HAVE_WINDOW_SYSTEM */
842
843 static void produce_special_glyphs (struct it *, enum display_element_type);
844 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
845 static bool coords_in_mouse_face_p (struct window *, int, int);
846
847
848 \f
849 /***********************************************************************
850 Window display dimensions
851 ***********************************************************************/
852
853 /* Return the bottom boundary y-position for text lines in window W.
854 This is the first y position at which a line cannot start.
855 It is relative to the top of the window.
856
857 This is the height of W minus the height of a mode line, if any. */
858
859 int
860 window_text_bottom_y (struct window *w)
861 {
862 int height = WINDOW_PIXEL_HEIGHT (w);
863
864 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
865
866 if (WINDOW_WANTS_MODELINE_P (w))
867 height -= CURRENT_MODE_LINE_HEIGHT (w);
868
869 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
870
871 return height;
872 }
873
874 /* Return the pixel width of display area AREA of window W.
875 ANY_AREA means return the total width of W, not including
876 fringes to the left and right of the window. */
877
878 int
879 window_box_width (struct window *w, enum glyph_row_area area)
880 {
881 int width = w->pixel_width;
882
883 if (!w->pseudo_window_p)
884 {
885 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
886 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
887
888 if (area == TEXT_AREA)
889 width -= (WINDOW_MARGINS_WIDTH (w)
890 + WINDOW_FRINGES_WIDTH (w));
891 else if (area == LEFT_MARGIN_AREA)
892 width = WINDOW_LEFT_MARGIN_WIDTH (w);
893 else if (area == RIGHT_MARGIN_AREA)
894 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
895 }
896
897 /* With wide margins, fringes, etc. we might end up with a negative
898 width, correct that here. */
899 return max (0, width);
900 }
901
902
903 /* Return the pixel height of the display area of window W, not
904 including mode lines of W, if any. */
905
906 int
907 window_box_height (struct window *w)
908 {
909 struct frame *f = XFRAME (w->frame);
910 int height = WINDOW_PIXEL_HEIGHT (w);
911
912 eassert (height >= 0);
913
914 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
915 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
916
917 /* Note: the code below that determines the mode-line/header-line
918 height is essentially the same as that contained in the macro
919 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
920 the appropriate glyph row has its `mode_line_p' flag set,
921 and if it doesn't, uses estimate_mode_line_height instead. */
922
923 if (WINDOW_WANTS_MODELINE_P (w))
924 {
925 struct glyph_row *ml_row
926 = (w->current_matrix && w->current_matrix->rows
927 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
928 : 0);
929 if (ml_row && ml_row->mode_line_p)
930 height -= ml_row->height;
931 else
932 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
933 }
934
935 if (WINDOW_WANTS_HEADER_LINE_P (w))
936 {
937 struct glyph_row *hl_row
938 = (w->current_matrix && w->current_matrix->rows
939 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
940 : 0);
941 if (hl_row && hl_row->mode_line_p)
942 height -= hl_row->height;
943 else
944 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
945 }
946
947 /* With a very small font and a mode-line that's taller than
948 default, we might end up with a negative height. */
949 return max (0, height);
950 }
951
952 /* Return the window-relative coordinate of the left edge of display
953 area AREA of window W. ANY_AREA means return the left edge of the
954 whole window, to the right of the left fringe of W. */
955
956 int
957 window_box_left_offset (struct window *w, enum glyph_row_area area)
958 {
959 int x;
960
961 if (w->pseudo_window_p)
962 return 0;
963
964 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
965
966 if (area == TEXT_AREA)
967 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
968 + window_box_width (w, LEFT_MARGIN_AREA));
969 else if (area == RIGHT_MARGIN_AREA)
970 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
971 + window_box_width (w, LEFT_MARGIN_AREA)
972 + window_box_width (w, TEXT_AREA)
973 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
974 ? 0
975 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
976 else if (area == LEFT_MARGIN_AREA
977 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
978 x += WINDOW_LEFT_FRINGE_WIDTH (w);
979
980 /* Don't return more than the window's pixel width. */
981 return min (x, w->pixel_width);
982 }
983
984
985 /* Return the window-relative coordinate of the right edge of display
986 area AREA of window W. ANY_AREA means return the right edge of the
987 whole window, to the left of the right fringe of W. */
988
989 static int
990 window_box_right_offset (struct window *w, enum glyph_row_area area)
991 {
992 /* Don't return more than the window's pixel width. */
993 return min (window_box_left_offset (w, area) + window_box_width (w, area),
994 w->pixel_width);
995 }
996
997 /* Return the frame-relative coordinate of the left edge of display
998 area AREA of window W. ANY_AREA means return the left edge of the
999 whole window, to the right of the left fringe of W. */
1000
1001 int
1002 window_box_left (struct window *w, enum glyph_row_area area)
1003 {
1004 struct frame *f = XFRAME (w->frame);
1005 int x;
1006
1007 if (w->pseudo_window_p)
1008 return FRAME_INTERNAL_BORDER_WIDTH (f);
1009
1010 x = (WINDOW_LEFT_EDGE_X (w)
1011 + window_box_left_offset (w, area));
1012
1013 return x;
1014 }
1015
1016
1017 /* Return the frame-relative coordinate of the right edge of display
1018 area AREA of window W. ANY_AREA means return the right edge of the
1019 whole window, to the left of the right fringe of W. */
1020
1021 int
1022 window_box_right (struct window *w, enum glyph_row_area area)
1023 {
1024 return window_box_left (w, area) + window_box_width (w, area);
1025 }
1026
1027 /* Get the bounding box of the display area AREA of window W, without
1028 mode lines, in frame-relative coordinates. ANY_AREA means the
1029 whole window, not including the left and right fringes of
1030 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1031 coordinates of the upper-left corner of the box. Return in
1032 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1033
1034 void
1035 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1036 int *box_y, int *box_width, int *box_height)
1037 {
1038 if (box_width)
1039 *box_width = window_box_width (w, area);
1040 if (box_height)
1041 *box_height = window_box_height (w);
1042 if (box_x)
1043 *box_x = window_box_left (w, area);
1044 if (box_y)
1045 {
1046 *box_y = WINDOW_TOP_EDGE_Y (w);
1047 if (WINDOW_WANTS_HEADER_LINE_P (w))
1048 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1049 }
1050 }
1051
1052 #ifdef HAVE_WINDOW_SYSTEM
1053
1054 /* Get the bounding box of the display area AREA of window W, without
1055 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1056 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1057 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1058 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1059 box. */
1060
1061 static void
1062 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1063 int *bottom_right_x, int *bottom_right_y)
1064 {
1065 window_box (w, ANY_AREA, top_left_x, top_left_y,
1066 bottom_right_x, bottom_right_y);
1067 *bottom_right_x += *top_left_x;
1068 *bottom_right_y += *top_left_y;
1069 }
1070
1071 #endif /* HAVE_WINDOW_SYSTEM */
1072
1073 /***********************************************************************
1074 Utilities
1075 ***********************************************************************/
1076
1077 /* Return the bottom y-position of the line the iterator IT is in.
1078 This can modify IT's settings. */
1079
1080 int
1081 line_bottom_y (struct it *it)
1082 {
1083 int line_height = it->max_ascent + it->max_descent;
1084 int line_top_y = it->current_y;
1085
1086 if (line_height == 0)
1087 {
1088 if (last_height)
1089 line_height = last_height;
1090 else if (IT_CHARPOS (*it) < ZV)
1091 {
1092 move_it_by_lines (it, 1);
1093 line_height = (it->max_ascent || it->max_descent
1094 ? it->max_ascent + it->max_descent
1095 : last_height);
1096 }
1097 else
1098 {
1099 struct glyph_row *row = it->glyph_row;
1100
1101 /* Use the default character height. */
1102 it->glyph_row = NULL;
1103 it->what = IT_CHARACTER;
1104 it->c = ' ';
1105 it->len = 1;
1106 PRODUCE_GLYPHS (it);
1107 line_height = it->ascent + it->descent;
1108 it->glyph_row = row;
1109 }
1110 }
1111
1112 return line_top_y + line_height;
1113 }
1114
1115 DEFUN ("line-pixel-height", Fline_pixel_height,
1116 Sline_pixel_height, 0, 0, 0,
1117 doc: /* Return height in pixels of text line in the selected window.
1118
1119 Value is the height in pixels of the line at point. */)
1120 (void)
1121 {
1122 struct it it;
1123 struct text_pos pt;
1124 struct window *w = XWINDOW (selected_window);
1125 struct buffer *old_buffer = NULL;
1126 Lisp_Object result;
1127
1128 if (XBUFFER (w->contents) != current_buffer)
1129 {
1130 old_buffer = current_buffer;
1131 set_buffer_internal_1 (XBUFFER (w->contents));
1132 }
1133 SET_TEXT_POS (pt, PT, PT_BYTE);
1134 start_display (&it, w, pt);
1135 it.vpos = it.current_y = 0;
1136 last_height = 0;
1137 result = make_number (line_bottom_y (&it));
1138 if (old_buffer)
1139 set_buffer_internal_1 (old_buffer);
1140
1141 return result;
1142 }
1143
1144 /* Return the default pixel height of text lines in window W. The
1145 value is the canonical height of the W frame's default font, plus
1146 any extra space required by the line-spacing variable or frame
1147 parameter.
1148
1149 Implementation note: this ignores any line-spacing text properties
1150 put on the newline characters. This is because those properties
1151 only affect the _screen_ line ending in the newline (i.e., in a
1152 continued line, only the last screen line will be affected), which
1153 means only a small number of lines in a buffer can ever use this
1154 feature. Since this function is used to compute the default pixel
1155 equivalent of text lines in a window, we can safely ignore those
1156 few lines. For the same reasons, we ignore the line-height
1157 properties. */
1158 int
1159 default_line_pixel_height (struct window *w)
1160 {
1161 struct frame *f = WINDOW_XFRAME (w);
1162 int height = FRAME_LINE_HEIGHT (f);
1163
1164 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1165 {
1166 struct buffer *b = XBUFFER (w->contents);
1167 Lisp_Object val = BVAR (b, extra_line_spacing);
1168
1169 if (NILP (val))
1170 val = BVAR (&buffer_defaults, extra_line_spacing);
1171 if (!NILP (val))
1172 {
1173 if (RANGED_INTEGERP (0, val, INT_MAX))
1174 height += XFASTINT (val);
1175 else if (FLOATP (val))
1176 {
1177 int addon = XFLOAT_DATA (val) * height + 0.5;
1178
1179 if (addon >= 0)
1180 height += addon;
1181 }
1182 }
1183 else
1184 height += f->extra_line_spacing;
1185 }
1186
1187 return height;
1188 }
1189
1190 /* Subroutine of pos_visible_p below. Extracts a display string, if
1191 any, from the display spec given as its argument. */
1192 static Lisp_Object
1193 string_from_display_spec (Lisp_Object spec)
1194 {
1195 if (CONSP (spec))
1196 {
1197 while (CONSP (spec))
1198 {
1199 if (STRINGP (XCAR (spec)))
1200 return XCAR (spec);
1201 spec = XCDR (spec);
1202 }
1203 }
1204 else if (VECTORP (spec))
1205 {
1206 ptrdiff_t i;
1207
1208 for (i = 0; i < ASIZE (spec); i++)
1209 {
1210 if (STRINGP (AREF (spec, i)))
1211 return AREF (spec, i);
1212 }
1213 return Qnil;
1214 }
1215
1216 return spec;
1217 }
1218
1219
1220 /* Limit insanely large values of W->hscroll on frame F to the largest
1221 value that will still prevent first_visible_x and last_visible_x of
1222 'struct it' from overflowing an int. */
1223 static int
1224 window_hscroll_limited (struct window *w, struct frame *f)
1225 {
1226 ptrdiff_t window_hscroll = w->hscroll;
1227 int window_text_width = window_box_width (w, TEXT_AREA);
1228 int colwidth = FRAME_COLUMN_WIDTH (f);
1229
1230 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1231 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1232
1233 return window_hscroll;
1234 }
1235
1236 /* Return true if position CHARPOS is visible in window W.
1237 CHARPOS < 0 means return info about WINDOW_END position.
1238 If visible, set *X and *Y to pixel coordinates of top left corner.
1239 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1240 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1241
1242 bool
1243 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1244 int *rtop, int *rbot, int *rowh, int *vpos)
1245 {
1246 struct it it;
1247 void *itdata = bidi_shelve_cache ();
1248 struct text_pos top;
1249 bool visible_p = false;
1250 struct buffer *old_buffer = NULL;
1251 bool r2l = false;
1252
1253 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1254 return visible_p;
1255
1256 if (XBUFFER (w->contents) != current_buffer)
1257 {
1258 old_buffer = current_buffer;
1259 set_buffer_internal_1 (XBUFFER (w->contents));
1260 }
1261
1262 SET_TEXT_POS_FROM_MARKER (top, w->start);
1263 /* Scrolling a minibuffer window via scroll bar when the echo area
1264 shows long text sometimes resets the minibuffer contents behind
1265 our backs. */
1266 if (CHARPOS (top) > ZV)
1267 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1268
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 w->mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 w->header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1279
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1292 {
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1302 int bottom_y;
1303 struct it save_it;
1304 void *save_it_data = NULL;
1305
1306 /* Calling line_bottom_y may change it.method, it.position, etc. */
1307 SAVE_IT (save_it, it, save_it_data);
1308 last_height = 0;
1309 bottom_y = line_bottom_y (&it);
1310 if (top_y < window_top_y)
1311 visible_p = bottom_y > window_top_y;
1312 else if (top_y < it.last_visible_y)
1313 visible_p = true;
1314 if (bottom_y >= it.last_visible_y
1315 && it.bidi_p && it.bidi_it.scan_dir == -1
1316 && IT_CHARPOS (it) < charpos)
1317 {
1318 /* When the last line of the window is scanned backwards
1319 under bidi iteration, we could be duped into thinking
1320 that we have passed CHARPOS, when in fact move_it_to
1321 simply stopped short of CHARPOS because it reached
1322 last_visible_y. To see if that's what happened, we call
1323 move_it_to again with a slightly larger vertical limit,
1324 and see if it actually moved vertically; if it did, we
1325 didn't really reach CHARPOS, which is beyond window end. */
1326 /* Why 10? because we don't know how many canonical lines
1327 will the height of the next line(s) be. So we guess. */
1328 int ten_more_lines = 10 * default_line_pixel_height (w);
1329
1330 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1331 MOVE_TO_POS | MOVE_TO_Y);
1332 if (it.current_y > top_y)
1333 visible_p = false;
1334
1335 }
1336 RESTORE_IT (&it, &save_it, save_it_data);
1337 if (visible_p)
1338 {
1339 if (it.method == GET_FROM_DISPLAY_VECTOR)
1340 {
1341 /* We stopped on the last glyph of a display vector.
1342 Try and recompute. Hack alert! */
1343 if (charpos < 2 || top.charpos >= charpos)
1344 top_x = it.glyph_row->x;
1345 else
1346 {
1347 struct it it2, it2_prev;
1348 /* The idea is to get to the previous buffer
1349 position, consume the character there, and use
1350 the pixel coordinates we get after that. But if
1351 the previous buffer position is also displayed
1352 from a display vector, we need to consume all of
1353 the glyphs from that display vector. */
1354 start_display (&it2, w, top);
1355 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1356 /* If we didn't get to CHARPOS - 1, there's some
1357 replacing display property at that position, and
1358 we stopped after it. That is exactly the place
1359 whose coordinates we want. */
1360 if (IT_CHARPOS (it2) != charpos - 1)
1361 it2_prev = it2;
1362 else
1363 {
1364 /* Iterate until we get out of the display
1365 vector that displays the character at
1366 CHARPOS - 1. */
1367 do {
1368 get_next_display_element (&it2);
1369 PRODUCE_GLYPHS (&it2);
1370 it2_prev = it2;
1371 set_iterator_to_next (&it2, true);
1372 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1373 && IT_CHARPOS (it2) < charpos);
1374 }
1375 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1376 || it2_prev.current_x > it2_prev.last_visible_x)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 top_x = it2_prev.current_x;
1381 top_y = it2_prev.current_y;
1382 }
1383 }
1384 }
1385 else if (IT_CHARPOS (it) != charpos)
1386 {
1387 Lisp_Object cpos = make_number (charpos);
1388 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1389 Lisp_Object string = string_from_display_spec (spec);
1390 struct text_pos tpos;
1391 bool newline_in_string
1392 = (STRINGP (string)
1393 && memchr (SDATA (string), '\n', SBYTES (string)));
1394
1395 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1396 bool replacing_spec_p
1397 = (!NILP (spec)
1398 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1399 charpos, FRAME_WINDOW_P (it.f)));
1400 /* The tricky code below is needed because there's a
1401 discrepancy between move_it_to and how we set cursor
1402 when PT is at the beginning of a portion of text
1403 covered by a display property or an overlay with a
1404 display property, or the display line ends in a
1405 newline from a display string. move_it_to will stop
1406 _after_ such display strings, whereas
1407 set_cursor_from_row conspires with cursor_row_p to
1408 place the cursor on the first glyph produced from the
1409 display string. */
1410
1411 /* We have overshoot PT because it is covered by a
1412 display property that replaces the text it covers.
1413 If the string includes embedded newlines, we are also
1414 in the wrong display line. Backtrack to the correct
1415 line, where the display property begins. */
1416 if (replacing_spec_p)
1417 {
1418 Lisp_Object startpos, endpos;
1419 EMACS_INT start, end;
1420 struct it it3;
1421
1422 /* Find the first and the last buffer positions
1423 covered by the display string. */
1424 endpos =
1425 Fnext_single_char_property_change (cpos, Qdisplay,
1426 Qnil, Qnil);
1427 startpos =
1428 Fprevious_single_char_property_change (endpos, Qdisplay,
1429 Qnil, Qnil);
1430 start = XFASTINT (startpos);
1431 end = XFASTINT (endpos);
1432 /* Move to the last buffer position before the
1433 display property. */
1434 start_display (&it3, w, top);
1435 if (start > CHARPOS (top))
1436 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* Move forward one more line if the position before
1438 the display string is a newline or if it is the
1439 rightmost character on a line that is
1440 continued or word-wrapped. */
1441 if (it3.method == GET_FROM_BUFFER
1442 && (it3.c == '\n'
1443 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1444 move_it_by_lines (&it3, 1);
1445 else if (move_it_in_display_line_to (&it3, -1,
1446 it3.current_x
1447 + it3.pixel_width,
1448 MOVE_TO_X)
1449 == MOVE_LINE_CONTINUED)
1450 {
1451 move_it_by_lines (&it3, 1);
1452 /* When we are under word-wrap, the #$@%!
1453 move_it_by_lines moves 2 lines, so we need to
1454 fix that up. */
1455 if (it3.line_wrap == WORD_WRAP)
1456 move_it_by_lines (&it3, -1);
1457 }
1458
1459 /* Record the vertical coordinate of the display
1460 line where we wound up. */
1461 top_y = it3.current_y;
1462 if (it3.bidi_p)
1463 {
1464 /* When characters are reordered for display,
1465 the character displayed to the left of the
1466 display string could be _after_ the display
1467 property in the logical order. Use the
1468 smallest vertical position of these two. */
1469 start_display (&it3, w, top);
1470 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1471 if (it3.current_y < top_y)
1472 top_y = it3.current_y;
1473 }
1474 /* Move from the top of the window to the beginning
1475 of the display line where the display string
1476 begins. */
1477 start_display (&it3, w, top);
1478 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1479 /* If it3_moved stays false after the 'while' loop
1480 below, that means we already were at a newline
1481 before the loop (e.g., the display string begins
1482 with a newline), so we don't need to (and cannot)
1483 inspect the glyphs of it3.glyph_row, because
1484 PRODUCE_GLYPHS will not produce anything for a
1485 newline, and thus it3.glyph_row stays at its
1486 stale content it got at top of the window. */
1487 bool it3_moved = false;
1488 /* Finally, advance the iterator until we hit the
1489 first display element whose character position is
1490 CHARPOS, or until the first newline from the
1491 display string, which signals the end of the
1492 display line. */
1493 while (get_next_display_element (&it3))
1494 {
1495 PRODUCE_GLYPHS (&it3);
1496 if (IT_CHARPOS (it3) == charpos
1497 || ITERATOR_AT_END_OF_LINE_P (&it3))
1498 break;
1499 it3_moved = true;
1500 set_iterator_to_next (&it3, false);
1501 }
1502 top_x = it3.current_x - it3.pixel_width;
1503 /* Normally, we would exit the above loop because we
1504 found the display element whose character
1505 position is CHARPOS. For the contingency that we
1506 didn't, and stopped at the first newline from the
1507 display string, move back over the glyphs
1508 produced from the string, until we find the
1509 rightmost glyph not from the string. */
1510 if (it3_moved
1511 && newline_in_string
1512 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1513 {
1514 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1515 + it3.glyph_row->used[TEXT_AREA];
1516
1517 while (EQ ((g - 1)->object, string))
1518 {
1519 --g;
1520 top_x -= g->pixel_width;
1521 }
1522 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1523 + it3.glyph_row->used[TEXT_AREA]);
1524 }
1525 }
1526 }
1527
1528 *x = top_x;
1529 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1530 *rtop = max (0, window_top_y - top_y);
1531 *rbot = max (0, bottom_y - it.last_visible_y);
1532 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1533 - max (top_y, window_top_y)));
1534 *vpos = it.vpos;
1535 if (it.bidi_it.paragraph_dir == R2L)
1536 r2l = true;
1537 }
1538 }
1539 else
1540 {
1541 /* Either we were asked to provide info about WINDOW_END, or
1542 CHARPOS is in the partially visible glyph row at end of
1543 window. */
1544 struct it it2;
1545 void *it2data = NULL;
1546
1547 SAVE_IT (it2, it, it2data);
1548 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1549 move_it_by_lines (&it, 1);
1550 if (charpos < IT_CHARPOS (it)
1551 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1552 {
1553 visible_p = true;
1554 RESTORE_IT (&it2, &it2, it2data);
1555 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1556 *x = it2.current_x;
1557 *y = it2.current_y + it2.max_ascent - it2.ascent;
1558 *rtop = max (0, -it2.current_y);
1559 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1560 - it.last_visible_y));
1561 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1562 it.last_visible_y)
1563 - max (it2.current_y,
1564 WINDOW_HEADER_LINE_HEIGHT (w))));
1565 *vpos = it2.vpos;
1566 if (it2.bidi_it.paragraph_dir == R2L)
1567 r2l = true;
1568 }
1569 else
1570 bidi_unshelve_cache (it2data, true);
1571 }
1572 bidi_unshelve_cache (itdata, false);
1573
1574 if (old_buffer)
1575 set_buffer_internal_1 (old_buffer);
1576
1577 if (visible_p)
1578 {
1579 if (w->hscroll > 0)
1580 *x -=
1581 window_hscroll_limited (w, WINDOW_XFRAME (w))
1582 * WINDOW_FRAME_COLUMN_WIDTH (w);
1583 /* For lines in an R2L paragraph, we need to mirror the X pixel
1584 coordinate wrt the text area. For the reasons, see the
1585 commentary in buffer_posn_from_coords and the explanation of
1586 the geometry used by the move_it_* functions at the end of
1587 the large commentary near the beginning of this file. */
1588 if (r2l)
1589 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1590 }
1591
1592 #if false
1593 /* Debugging code. */
1594 if (visible_p)
1595 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1596 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1597 else
1598 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1599 #endif
1600
1601 return visible_p;
1602 }
1603
1604
1605 /* Return the next character from STR. Return in *LEN the length of
1606 the character. This is like STRING_CHAR_AND_LENGTH but never
1607 returns an invalid character. If we find one, we return a `?', but
1608 with the length of the invalid character. */
1609
1610 static int
1611 string_char_and_length (const unsigned char *str, int *len)
1612 {
1613 int c;
1614
1615 c = STRING_CHAR_AND_LENGTH (str, *len);
1616 if (!CHAR_VALID_P (c))
1617 /* We may not change the length here because other places in Emacs
1618 don't use this function, i.e. they silently accept invalid
1619 characters. */
1620 c = '?';
1621
1622 return c;
1623 }
1624
1625
1626
1627 /* Given a position POS containing a valid character and byte position
1628 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1629
1630 static struct text_pos
1631 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1632 {
1633 eassert (STRINGP (string) && nchars >= 0);
1634
1635 if (STRING_MULTIBYTE (string))
1636 {
1637 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1638 int len;
1639
1640 while (nchars--)
1641 {
1642 string_char_and_length (p, &len);
1643 p += len;
1644 CHARPOS (pos) += 1;
1645 BYTEPOS (pos) += len;
1646 }
1647 }
1648 else
1649 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1650
1651 return pos;
1652 }
1653
1654
1655 /* Value is the text position, i.e. character and byte position,
1656 for character position CHARPOS in STRING. */
1657
1658 static struct text_pos
1659 string_pos (ptrdiff_t charpos, Lisp_Object string)
1660 {
1661 struct text_pos pos;
1662 eassert (STRINGP (string));
1663 eassert (charpos >= 0);
1664 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1665 return pos;
1666 }
1667
1668
1669 /* Value is a text position, i.e. character and byte position, for
1670 character position CHARPOS in C string S. MULTIBYTE_P
1671 means recognize multibyte characters. */
1672
1673 static struct text_pos
1674 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1675 {
1676 struct text_pos pos;
1677
1678 eassert (s != NULL);
1679 eassert (charpos >= 0);
1680
1681 if (multibyte_p)
1682 {
1683 int len;
1684
1685 SET_TEXT_POS (pos, 0, 0);
1686 while (charpos--)
1687 {
1688 string_char_and_length ((const unsigned char *) s, &len);
1689 s += len;
1690 CHARPOS (pos) += 1;
1691 BYTEPOS (pos) += len;
1692 }
1693 }
1694 else
1695 SET_TEXT_POS (pos, charpos, charpos);
1696
1697 return pos;
1698 }
1699
1700
1701 /* Value is the number of characters in C string S. MULTIBYTE_P
1702 means recognize multibyte characters. */
1703
1704 static ptrdiff_t
1705 number_of_chars (const char *s, bool multibyte_p)
1706 {
1707 ptrdiff_t nchars;
1708
1709 if (multibyte_p)
1710 {
1711 ptrdiff_t rest = strlen (s);
1712 int len;
1713 const unsigned char *p = (const unsigned char *) s;
1714
1715 for (nchars = 0; rest > 0; ++nchars)
1716 {
1717 string_char_and_length (p, &len);
1718 rest -= len, p += len;
1719 }
1720 }
1721 else
1722 nchars = strlen (s);
1723
1724 return nchars;
1725 }
1726
1727
1728 /* Compute byte position NEWPOS->bytepos corresponding to
1729 NEWPOS->charpos. POS is a known position in string STRING.
1730 NEWPOS->charpos must be >= POS.charpos. */
1731
1732 static void
1733 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1734 {
1735 eassert (STRINGP (string));
1736 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1737
1738 if (STRING_MULTIBYTE (string))
1739 *newpos = string_pos_nchars_ahead (pos, string,
1740 CHARPOS (*newpos) - CHARPOS (pos));
1741 else
1742 BYTEPOS (*newpos) = CHARPOS (*newpos);
1743 }
1744
1745 /* EXPORT:
1746 Return an estimation of the pixel height of mode or header lines on
1747 frame F. FACE_ID specifies what line's height to estimate. */
1748
1749 int
1750 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1751 {
1752 #ifdef HAVE_WINDOW_SYSTEM
1753 if (FRAME_WINDOW_P (f))
1754 {
1755 int height = FONT_HEIGHT (FRAME_FONT (f));
1756
1757 /* This function is called so early when Emacs starts that the face
1758 cache and mode line face are not yet initialized. */
1759 if (FRAME_FACE_CACHE (f))
1760 {
1761 struct face *face = FACE_FROM_ID (f, face_id);
1762 if (face)
1763 {
1764 if (face->font)
1765 height = FONT_HEIGHT (face->font);
1766 if (face->box_line_width > 0)
1767 height += 2 * face->box_line_width;
1768 }
1769 }
1770
1771 return height;
1772 }
1773 #endif
1774
1775 return 1;
1776 }
1777
1778 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1779 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1780 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1781 not force the value into range. */
1782
1783 void
1784 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1785 NativeRectangle *bounds, bool noclip)
1786 {
1787
1788 #ifdef HAVE_WINDOW_SYSTEM
1789 if (FRAME_WINDOW_P (f))
1790 {
1791 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1792 even for negative values. */
1793 if (pix_x < 0)
1794 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1795 if (pix_y < 0)
1796 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1797
1798 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1799 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1800
1801 if (bounds)
1802 STORE_NATIVE_RECT (*bounds,
1803 FRAME_COL_TO_PIXEL_X (f, pix_x),
1804 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1805 FRAME_COLUMN_WIDTH (f) - 1,
1806 FRAME_LINE_HEIGHT (f) - 1);
1807
1808 /* PXW: Should we clip pixels before converting to columns/lines? */
1809 if (!noclip)
1810 {
1811 if (pix_x < 0)
1812 pix_x = 0;
1813 else if (pix_x > FRAME_TOTAL_COLS (f))
1814 pix_x = FRAME_TOTAL_COLS (f);
1815
1816 if (pix_y < 0)
1817 pix_y = 0;
1818 else if (pix_y > FRAME_TOTAL_LINES (f))
1819 pix_y = FRAME_TOTAL_LINES (f);
1820 }
1821 }
1822 #endif
1823
1824 *x = pix_x;
1825 *y = pix_y;
1826 }
1827
1828
1829 /* Find the glyph under window-relative coordinates X/Y in window W.
1830 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1831 strings. Return in *HPOS and *VPOS the row and column number of
1832 the glyph found. Return in *AREA the glyph area containing X.
1833 Value is a pointer to the glyph found or null if X/Y is not on
1834 text, or we can't tell because W's current matrix is not up to
1835 date. */
1836
1837 static struct glyph *
1838 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1839 int *dx, int *dy, int *area)
1840 {
1841 struct glyph *glyph, *end;
1842 struct glyph_row *row = NULL;
1843 int x0, i;
1844
1845 /* Find row containing Y. Give up if some row is not enabled. */
1846 for (i = 0; i < w->current_matrix->nrows; ++i)
1847 {
1848 row = MATRIX_ROW (w->current_matrix, i);
1849 if (!row->enabled_p)
1850 return NULL;
1851 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1852 break;
1853 }
1854
1855 *vpos = i;
1856 *hpos = 0;
1857
1858 /* Give up if Y is not in the window. */
1859 if (i == w->current_matrix->nrows)
1860 return NULL;
1861
1862 /* Get the glyph area containing X. */
1863 if (w->pseudo_window_p)
1864 {
1865 *area = TEXT_AREA;
1866 x0 = 0;
1867 }
1868 else
1869 {
1870 if (x < window_box_left_offset (w, TEXT_AREA))
1871 {
1872 *area = LEFT_MARGIN_AREA;
1873 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1874 }
1875 else if (x < window_box_right_offset (w, TEXT_AREA))
1876 {
1877 *area = TEXT_AREA;
1878 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1879 }
1880 else
1881 {
1882 *area = RIGHT_MARGIN_AREA;
1883 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1884 }
1885 }
1886
1887 /* Find glyph containing X. */
1888 glyph = row->glyphs[*area];
1889 end = glyph + row->used[*area];
1890 x -= x0;
1891 while (glyph < end && x >= glyph->pixel_width)
1892 {
1893 x -= glyph->pixel_width;
1894 ++glyph;
1895 }
1896
1897 if (glyph == end)
1898 return NULL;
1899
1900 if (dx)
1901 {
1902 *dx = x;
1903 *dy = y - (row->y + row->ascent - glyph->ascent);
1904 }
1905
1906 *hpos = glyph - row->glyphs[*area];
1907 return glyph;
1908 }
1909
1910 /* Convert frame-relative x/y to coordinates relative to window W.
1911 Takes pseudo-windows into account. */
1912
1913 static void
1914 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1915 {
1916 if (w->pseudo_window_p)
1917 {
1918 /* A pseudo-window is always full-width, and starts at the
1919 left edge of the frame, plus a frame border. */
1920 struct frame *f = XFRAME (w->frame);
1921 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1923 }
1924 else
1925 {
1926 *x -= WINDOW_LEFT_EDGE_X (w);
1927 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1928 }
1929 }
1930
1931 #ifdef HAVE_WINDOW_SYSTEM
1932
1933 /* EXPORT:
1934 Return in RECTS[] at most N clipping rectangles for glyph string S.
1935 Return the number of stored rectangles. */
1936
1937 int
1938 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1939 {
1940 XRectangle r;
1941
1942 if (n <= 0)
1943 return 0;
1944
1945 if (s->row->full_width_p)
1946 {
1947 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1948 r.x = WINDOW_LEFT_EDGE_X (s->w);
1949 if (s->row->mode_line_p)
1950 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1951 else
1952 r.width = WINDOW_PIXEL_WIDTH (s->w);
1953
1954 /* Unless displaying a mode or menu bar line, which are always
1955 fully visible, clip to the visible part of the row. */
1956 if (s->w->pseudo_window_p)
1957 r.height = s->row->visible_height;
1958 else
1959 r.height = s->height;
1960 }
1961 else
1962 {
1963 /* This is a text line that may be partially visible. */
1964 r.x = window_box_left (s->w, s->area);
1965 r.width = window_box_width (s->w, s->area);
1966 r.height = s->row->visible_height;
1967 }
1968
1969 if (s->clip_head)
1970 if (r.x < s->clip_head->x)
1971 {
1972 if (r.width >= s->clip_head->x - r.x)
1973 r.width -= s->clip_head->x - r.x;
1974 else
1975 r.width = 0;
1976 r.x = s->clip_head->x;
1977 }
1978 if (s->clip_tail)
1979 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1980 {
1981 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1982 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1983 else
1984 r.width = 0;
1985 }
1986
1987 /* If S draws overlapping rows, it's sufficient to use the top and
1988 bottom of the window for clipping because this glyph string
1989 intentionally draws over other lines. */
1990 if (s->for_overlaps)
1991 {
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 r.height = window_text_bottom_y (s->w) - r.y;
1994
1995 /* Alas, the above simple strategy does not work for the
1996 environments with anti-aliased text: if the same text is
1997 drawn onto the same place multiple times, it gets thicker.
1998 If the overlap we are processing is for the erased cursor, we
1999 take the intersection with the rectangle of the cursor. */
2000 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2001 {
2002 XRectangle rc, r_save = r;
2003
2004 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2005 rc.y = s->w->phys_cursor.y;
2006 rc.width = s->w->phys_cursor_width;
2007 rc.height = s->w->phys_cursor_height;
2008
2009 x_intersect_rectangles (&r_save, &rc, &r);
2010 }
2011 }
2012 else
2013 {
2014 /* Don't use S->y for clipping because it doesn't take partially
2015 visible lines into account. For example, it can be negative for
2016 partially visible lines at the top of a window. */
2017 if (!s->row->full_width_p
2018 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2019 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2020 else
2021 r.y = max (0, s->row->y);
2022 }
2023
2024 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2025
2026 /* If drawing the cursor, don't let glyph draw outside its
2027 advertised boundaries. Cleartype does this under some circumstances. */
2028 if (s->hl == DRAW_CURSOR)
2029 {
2030 struct glyph *glyph = s->first_glyph;
2031 int height, max_y;
2032
2033 if (s->x > r.x)
2034 {
2035 if (r.width >= s->x - r.x)
2036 r.width -= s->x - r.x;
2037 else /* R2L hscrolled row with cursor outside text area */
2038 r.width = 0;
2039 r.x = s->x;
2040 }
2041 r.width = min (r.width, glyph->pixel_width);
2042
2043 /* If r.y is below window bottom, ensure that we still see a cursor. */
2044 height = min (glyph->ascent + glyph->descent,
2045 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2046 max_y = window_text_bottom_y (s->w) - height;
2047 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2048 if (s->ybase - glyph->ascent > max_y)
2049 {
2050 r.y = max_y;
2051 r.height = height;
2052 }
2053 else
2054 {
2055 /* Don't draw cursor glyph taller than our actual glyph. */
2056 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2057 if (height < r.height)
2058 {
2059 max_y = r.y + r.height;
2060 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2061 r.height = min (max_y - r.y, height);
2062 }
2063 }
2064 }
2065
2066 if (s->row->clip)
2067 {
2068 XRectangle r_save = r;
2069
2070 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2071 r.width = 0;
2072 }
2073
2074 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2075 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2076 {
2077 #ifdef CONVERT_FROM_XRECT
2078 CONVERT_FROM_XRECT (r, *rects);
2079 #else
2080 *rects = r;
2081 #endif
2082 return 1;
2083 }
2084 else
2085 {
2086 /* If we are processing overlapping and allowed to return
2087 multiple clipping rectangles, we exclude the row of the glyph
2088 string from the clipping rectangle. This is to avoid drawing
2089 the same text on the environment with anti-aliasing. */
2090 #ifdef CONVERT_FROM_XRECT
2091 XRectangle rs[2];
2092 #else
2093 XRectangle *rs = rects;
2094 #endif
2095 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2096
2097 if (s->for_overlaps & OVERLAPS_PRED)
2098 {
2099 rs[i] = r;
2100 if (r.y + r.height > row_y)
2101 {
2102 if (r.y < row_y)
2103 rs[i].height = row_y - r.y;
2104 else
2105 rs[i].height = 0;
2106 }
2107 i++;
2108 }
2109 if (s->for_overlaps & OVERLAPS_SUCC)
2110 {
2111 rs[i] = r;
2112 if (r.y < row_y + s->row->visible_height)
2113 {
2114 if (r.y + r.height > row_y + s->row->visible_height)
2115 {
2116 rs[i].y = row_y + s->row->visible_height;
2117 rs[i].height = r.y + r.height - rs[i].y;
2118 }
2119 else
2120 rs[i].height = 0;
2121 }
2122 i++;
2123 }
2124
2125 n = i;
2126 #ifdef CONVERT_FROM_XRECT
2127 for (i = 0; i < n; i++)
2128 CONVERT_FROM_XRECT (rs[i], rects[i]);
2129 #endif
2130 return n;
2131 }
2132 }
2133
2134 /* EXPORT:
2135 Return in *NR the clipping rectangle for glyph string S. */
2136
2137 void
2138 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2139 {
2140 get_glyph_string_clip_rects (s, nr, 1);
2141 }
2142
2143
2144 /* EXPORT:
2145 Return the position and height of the phys cursor in window W.
2146 Set w->phys_cursor_width to width of phys cursor.
2147 */
2148
2149 void
2150 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2151 struct glyph *glyph, int *xp, int *yp, int *heightp)
2152 {
2153 struct frame *f = XFRAME (WINDOW_FRAME (w));
2154 int x, y, wd, h, h0, y0, ascent;
2155
2156 /* Compute the width of the rectangle to draw. If on a stretch
2157 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2158 rectangle as wide as the glyph, but use a canonical character
2159 width instead. */
2160 wd = glyph->pixel_width;
2161
2162 x = w->phys_cursor.x;
2163 if (x < 0)
2164 {
2165 wd += x;
2166 x = 0;
2167 }
2168
2169 if (glyph->type == STRETCH_GLYPH
2170 && !x_stretch_cursor_p)
2171 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2172 w->phys_cursor_width = wd;
2173
2174 /* Don't let the hollow cursor glyph descend below the glyph row's
2175 ascent value, lest the hollow cursor looks funny. */
2176 y = w->phys_cursor.y;
2177 ascent = row->ascent;
2178 if (row->ascent < glyph->ascent)
2179 {
2180 y =- glyph->ascent - row->ascent;
2181 ascent = glyph->ascent;
2182 }
2183
2184 /* If y is below window bottom, ensure that we still see a cursor. */
2185 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2186
2187 h = max (h0, ascent + glyph->descent);
2188 h0 = min (h0, ascent + glyph->descent);
2189
2190 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2191 if (y < y0)
2192 {
2193 h = max (h - (y0 - y) + 1, h0);
2194 y = y0 - 1;
2195 }
2196 else
2197 {
2198 y0 = window_text_bottom_y (w) - h0;
2199 if (y > y0)
2200 {
2201 h += y - y0;
2202 y = y0;
2203 }
2204 }
2205
2206 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2207 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2208 *heightp = h;
2209 }
2210
2211 /*
2212 * Remember which glyph the mouse is over.
2213 */
2214
2215 void
2216 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2217 {
2218 Lisp_Object window;
2219 struct window *w;
2220 struct glyph_row *r, *gr, *end_row;
2221 enum window_part part;
2222 enum glyph_row_area area;
2223 int x, y, width, height;
2224
2225 /* Try to determine frame pixel position and size of the glyph under
2226 frame pixel coordinates X/Y on frame F. */
2227
2228 if (window_resize_pixelwise)
2229 {
2230 width = height = 1;
2231 goto virtual_glyph;
2232 }
2233 else if (!f->glyphs_initialized_p
2234 || (window = window_from_coordinates (f, gx, gy, &part, false),
2235 NILP (window)))
2236 {
2237 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2238 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2239 goto virtual_glyph;
2240 }
2241
2242 w = XWINDOW (window);
2243 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2244 height = WINDOW_FRAME_LINE_HEIGHT (w);
2245
2246 x = window_relative_x_coord (w, part, gx);
2247 y = gy - WINDOW_TOP_EDGE_Y (w);
2248
2249 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2250 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2251
2252 if (w->pseudo_window_p)
2253 {
2254 area = TEXT_AREA;
2255 part = ON_MODE_LINE; /* Don't adjust margin. */
2256 goto text_glyph;
2257 }
2258
2259 switch (part)
2260 {
2261 case ON_LEFT_MARGIN:
2262 area = LEFT_MARGIN_AREA;
2263 goto text_glyph;
2264
2265 case ON_RIGHT_MARGIN:
2266 area = RIGHT_MARGIN_AREA;
2267 goto text_glyph;
2268
2269 case ON_HEADER_LINE:
2270 case ON_MODE_LINE:
2271 gr = (part == ON_HEADER_LINE
2272 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2273 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2274 gy = gr->y;
2275 area = TEXT_AREA;
2276 goto text_glyph_row_found;
2277
2278 case ON_TEXT:
2279 area = TEXT_AREA;
2280
2281 text_glyph:
2282 gr = 0; gy = 0;
2283 for (; r <= end_row && r->enabled_p; ++r)
2284 if (r->y + r->height > y)
2285 {
2286 gr = r; gy = r->y;
2287 break;
2288 }
2289
2290 text_glyph_row_found:
2291 if (gr && gy <= y)
2292 {
2293 struct glyph *g = gr->glyphs[area];
2294 struct glyph *end = g + gr->used[area];
2295
2296 height = gr->height;
2297 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2298 if (gx + g->pixel_width > x)
2299 break;
2300
2301 if (g < end)
2302 {
2303 if (g->type == IMAGE_GLYPH)
2304 {
2305 /* Don't remember when mouse is over image, as
2306 image may have hot-spots. */
2307 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2308 return;
2309 }
2310 width = g->pixel_width;
2311 }
2312 else
2313 {
2314 /* Use nominal char spacing at end of line. */
2315 x -= gx;
2316 gx += (x / width) * width;
2317 }
2318
2319 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2320 {
2321 gx += window_box_left_offset (w, area);
2322 /* Don't expand over the modeline to make sure the vertical
2323 drag cursor is shown early enough. */
2324 height = min (height,
2325 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2326 }
2327 }
2328 else
2329 {
2330 /* Use nominal line height at end of window. */
2331 gx = (x / width) * width;
2332 y -= gy;
2333 gy += (y / height) * height;
2334 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2335 /* See comment above. */
2336 height = min (height,
2337 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2338 }
2339 break;
2340
2341 case ON_LEFT_FRINGE:
2342 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2343 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2344 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2345 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2346 goto row_glyph;
2347
2348 case ON_RIGHT_FRINGE:
2349 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2350 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2351 : window_box_right_offset (w, TEXT_AREA));
2352 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2353 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2354 && !WINDOW_RIGHTMOST_P (w))
2355 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2356 /* Make sure the vertical border can get her own glyph to the
2357 right of the one we build here. */
2358 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2359 else
2360 width = WINDOW_PIXEL_WIDTH (w) - gx;
2361 else
2362 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2363
2364 goto row_glyph;
2365
2366 case ON_VERTICAL_BORDER:
2367 gx = WINDOW_PIXEL_WIDTH (w) - width;
2368 goto row_glyph;
2369
2370 case ON_VERTICAL_SCROLL_BAR:
2371 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2372 ? 0
2373 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2374 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2375 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2376 : 0)));
2377 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2378
2379 row_glyph:
2380 gr = 0, gy = 0;
2381 for (; r <= end_row && r->enabled_p; ++r)
2382 if (r->y + r->height > y)
2383 {
2384 gr = r; gy = r->y;
2385 break;
2386 }
2387
2388 if (gr && gy <= y)
2389 height = gr->height;
2390 else
2391 {
2392 /* Use nominal line height at end of window. */
2393 y -= gy;
2394 gy += (y / height) * height;
2395 }
2396 break;
2397
2398 case ON_RIGHT_DIVIDER:
2399 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2400 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2401 gy = 0;
2402 /* The bottom divider prevails. */
2403 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2404 goto add_edge;
2405
2406 case ON_BOTTOM_DIVIDER:
2407 gx = 0;
2408 width = WINDOW_PIXEL_WIDTH (w);
2409 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2410 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2411 goto add_edge;
2412
2413 default:
2414 ;
2415 virtual_glyph:
2416 /* If there is no glyph under the mouse, then we divide the screen
2417 into a grid of the smallest glyph in the frame, and use that
2418 as our "glyph". */
2419
2420 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2421 round down even for negative values. */
2422 if (gx < 0)
2423 gx -= width - 1;
2424 if (gy < 0)
2425 gy -= height - 1;
2426
2427 gx = (gx / width) * width;
2428 gy = (gy / height) * height;
2429
2430 goto store_rect;
2431 }
2432
2433 add_edge:
2434 gx += WINDOW_LEFT_EDGE_X (w);
2435 gy += WINDOW_TOP_EDGE_Y (w);
2436
2437 store_rect:
2438 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2439
2440 /* Visible feedback for debugging. */
2441 #if false && defined HAVE_X_WINDOWS
2442 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2443 f->output_data.x->normal_gc,
2444 gx, gy, width, height);
2445 #endif
2446 }
2447
2448
2449 #endif /* HAVE_WINDOW_SYSTEM */
2450
2451 static void
2452 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2453 {
2454 eassert (w);
2455 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2456 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2457 w->window_end_vpos
2458 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2459 }
2460
2461 /***********************************************************************
2462 Lisp form evaluation
2463 ***********************************************************************/
2464
2465 /* Error handler for safe_eval and safe_call. */
2466
2467 static Lisp_Object
2468 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2469 {
2470 add_to_log ("Error during redisplay: %S signaled %S",
2471 Flist (nargs, args), arg);
2472 return Qnil;
2473 }
2474
2475 /* Call function FUNC with the rest of NARGS - 1 arguments
2476 following. Return the result, or nil if something went
2477 wrong. Prevent redisplay during the evaluation. */
2478
2479 static Lisp_Object
2480 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2481 {
2482 Lisp_Object val;
2483
2484 if (inhibit_eval_during_redisplay)
2485 val = Qnil;
2486 else
2487 {
2488 ptrdiff_t i;
2489 ptrdiff_t count = SPECPDL_INDEX ();
2490 Lisp_Object *args;
2491 USE_SAFE_ALLOCA;
2492 SAFE_ALLOCA_LISP (args, nargs);
2493
2494 args[0] = func;
2495 for (i = 1; i < nargs; i++)
2496 args[i] = va_arg (ap, Lisp_Object);
2497
2498 specbind (Qinhibit_redisplay, Qt);
2499 if (inhibit_quit)
2500 specbind (Qinhibit_quit, Qt);
2501 /* Use Qt to ensure debugger does not run,
2502 so there is no possibility of wanting to redisplay. */
2503 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2504 safe_eval_handler);
2505 SAFE_FREE ();
2506 val = unbind_to (count, val);
2507 }
2508
2509 return val;
2510 }
2511
2512 Lisp_Object
2513 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2514 {
2515 Lisp_Object retval;
2516 va_list ap;
2517
2518 va_start (ap, func);
2519 retval = safe__call (false, nargs, func, ap);
2520 va_end (ap);
2521 return retval;
2522 }
2523
2524 /* Call function FN with one argument ARG.
2525 Return the result, or nil if something went wrong. */
2526
2527 Lisp_Object
2528 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2529 {
2530 return safe_call (2, fn, arg);
2531 }
2532
2533 static Lisp_Object
2534 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2535 {
2536 Lisp_Object retval;
2537 va_list ap;
2538
2539 va_start (ap, fn);
2540 retval = safe__call (inhibit_quit, 2, fn, ap);
2541 va_end (ap);
2542 return retval;
2543 }
2544
2545 Lisp_Object
2546 safe_eval (Lisp_Object sexpr)
2547 {
2548 return safe__call1 (false, Qeval, sexpr);
2549 }
2550
2551 static Lisp_Object
2552 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2553 {
2554 return safe__call1 (inhibit_quit, Qeval, sexpr);
2555 }
2556
2557 /* Call function FN with two arguments ARG1 and ARG2.
2558 Return the result, or nil if something went wrong. */
2559
2560 Lisp_Object
2561 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2562 {
2563 return safe_call (3, fn, arg1, arg2);
2564 }
2565
2566
2567 \f
2568 /***********************************************************************
2569 Debugging
2570 ***********************************************************************/
2571
2572 /* Define CHECK_IT to perform sanity checks on iterators.
2573 This is for debugging. It is too slow to do unconditionally. */
2574
2575 static void
2576 CHECK_IT (struct it *it)
2577 {
2578 #if false
2579 if (it->method == GET_FROM_STRING)
2580 {
2581 eassert (STRINGP (it->string));
2582 eassert (IT_STRING_CHARPOS (*it) >= 0);
2583 }
2584 else
2585 {
2586 eassert (IT_STRING_CHARPOS (*it) < 0);
2587 if (it->method == GET_FROM_BUFFER)
2588 {
2589 /* Check that character and byte positions agree. */
2590 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2591 }
2592 }
2593
2594 if (it->dpvec)
2595 eassert (it->current.dpvec_index >= 0);
2596 else
2597 eassert (it->current.dpvec_index < 0);
2598 #endif
2599 }
2600
2601
2602 /* Check that the window end of window W is what we expect it
2603 to be---the last row in the current matrix displaying text. */
2604
2605 static void
2606 CHECK_WINDOW_END (struct window *w)
2607 {
2608 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2609 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2610 {
2611 struct glyph_row *row;
2612 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2613 !row->enabled_p
2614 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2615 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2616 }
2617 #endif
2618 }
2619
2620 /***********************************************************************
2621 Iterator initialization
2622 ***********************************************************************/
2623
2624 /* Initialize IT for displaying current_buffer in window W, starting
2625 at character position CHARPOS. CHARPOS < 0 means that no buffer
2626 position is specified which is useful when the iterator is assigned
2627 a position later. BYTEPOS is the byte position corresponding to
2628 CHARPOS.
2629
2630 If ROW is not null, calls to produce_glyphs with IT as parameter
2631 will produce glyphs in that row.
2632
2633 BASE_FACE_ID is the id of a base face to use. It must be one of
2634 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2635 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2636 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2637
2638 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2639 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2640 will be initialized to use the corresponding mode line glyph row of
2641 the desired matrix of W. */
2642
2643 void
2644 init_iterator (struct it *it, struct window *w,
2645 ptrdiff_t charpos, ptrdiff_t bytepos,
2646 struct glyph_row *row, enum face_id base_face_id)
2647 {
2648 enum face_id remapped_base_face_id = base_face_id;
2649
2650 /* Some precondition checks. */
2651 eassert (w != NULL && it != NULL);
2652 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2653 && charpos <= ZV));
2654
2655 /* If face attributes have been changed since the last redisplay,
2656 free realized faces now because they depend on face definitions
2657 that might have changed. Don't free faces while there might be
2658 desired matrices pending which reference these faces. */
2659 if (face_change && !inhibit_free_realized_faces)
2660 {
2661 face_change = false;
2662 free_all_realized_faces (Qnil);
2663 }
2664
2665 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2666 if (! NILP (Vface_remapping_alist))
2667 remapped_base_face_id
2668 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2669
2670 /* Use one of the mode line rows of W's desired matrix if
2671 appropriate. */
2672 if (row == NULL)
2673 {
2674 if (base_face_id == MODE_LINE_FACE_ID
2675 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2676 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2677 else if (base_face_id == HEADER_LINE_FACE_ID)
2678 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2679 }
2680
2681 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2682 Other parts of redisplay rely on that. */
2683 memclear (it, sizeof *it);
2684 it->current.overlay_string_index = -1;
2685 it->current.dpvec_index = -1;
2686 it->base_face_id = remapped_base_face_id;
2687 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2688 it->paragraph_embedding = L2R;
2689 it->bidi_it.w = w;
2690
2691 /* The window in which we iterate over current_buffer: */
2692 XSETWINDOW (it->window, w);
2693 it->w = w;
2694 it->f = XFRAME (w->frame);
2695
2696 it->cmp_it.id = -1;
2697
2698 /* Extra space between lines (on window systems only). */
2699 if (base_face_id == DEFAULT_FACE_ID
2700 && FRAME_WINDOW_P (it->f))
2701 {
2702 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2703 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2704 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2705 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2706 * FRAME_LINE_HEIGHT (it->f));
2707 else if (it->f->extra_line_spacing > 0)
2708 it->extra_line_spacing = it->f->extra_line_spacing;
2709 }
2710
2711 /* If realized faces have been removed, e.g. because of face
2712 attribute changes of named faces, recompute them. When running
2713 in batch mode, the face cache of the initial frame is null. If
2714 we happen to get called, make a dummy face cache. */
2715 if (FRAME_FACE_CACHE (it->f) == NULL)
2716 init_frame_faces (it->f);
2717 if (FRAME_FACE_CACHE (it->f)->used == 0)
2718 recompute_basic_faces (it->f);
2719
2720 it->override_ascent = -1;
2721
2722 /* Are control characters displayed as `^C'? */
2723 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2724
2725 /* -1 means everything between a CR and the following line end
2726 is invisible. >0 means lines indented more than this value are
2727 invisible. */
2728 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2729 ? (clip_to_bounds
2730 (-1, XINT (BVAR (current_buffer, selective_display)),
2731 PTRDIFF_MAX))
2732 : (!NILP (BVAR (current_buffer, selective_display))
2733 ? -1 : 0));
2734 it->selective_display_ellipsis_p
2735 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2736
2737 /* Display table to use. */
2738 it->dp = window_display_table (w);
2739
2740 /* Are multibyte characters enabled in current_buffer? */
2741 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2742
2743 /* Get the position at which the redisplay_end_trigger hook should
2744 be run, if it is to be run at all. */
2745 if (MARKERP (w->redisplay_end_trigger)
2746 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2747 it->redisplay_end_trigger_charpos
2748 = marker_position (w->redisplay_end_trigger);
2749 else if (INTEGERP (w->redisplay_end_trigger))
2750 it->redisplay_end_trigger_charpos
2751 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2752 PTRDIFF_MAX);
2753
2754 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2755
2756 /* Are lines in the display truncated? */
2757 if (TRUNCATE != 0)
2758 it->line_wrap = TRUNCATE;
2759 if (base_face_id == DEFAULT_FACE_ID
2760 && !it->w->hscroll
2761 && (WINDOW_FULL_WIDTH_P (it->w)
2762 || NILP (Vtruncate_partial_width_windows)
2763 || (INTEGERP (Vtruncate_partial_width_windows)
2764 /* PXW: Shall we do something about this? */
2765 && (XINT (Vtruncate_partial_width_windows)
2766 <= WINDOW_TOTAL_COLS (it->w))))
2767 && NILP (BVAR (current_buffer, truncate_lines)))
2768 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2769 ? WINDOW_WRAP : WORD_WRAP;
2770
2771 /* Get dimensions of truncation and continuation glyphs. These are
2772 displayed as fringe bitmaps under X, but we need them for such
2773 frames when the fringes are turned off. But leave the dimensions
2774 zero for tooltip frames, as these glyphs look ugly there and also
2775 sabotage calculations of tooltip dimensions in x-show-tip. */
2776 #ifdef HAVE_WINDOW_SYSTEM
2777 if (!(FRAME_WINDOW_P (it->f)
2778 && FRAMEP (tip_frame)
2779 && it->f == XFRAME (tip_frame)))
2780 #endif
2781 {
2782 if (it->line_wrap == TRUNCATE)
2783 {
2784 /* We will need the truncation glyph. */
2785 eassert (it->glyph_row == NULL);
2786 produce_special_glyphs (it, IT_TRUNCATION);
2787 it->truncation_pixel_width = it->pixel_width;
2788 }
2789 else
2790 {
2791 /* We will need the continuation glyph. */
2792 eassert (it->glyph_row == NULL);
2793 produce_special_glyphs (it, IT_CONTINUATION);
2794 it->continuation_pixel_width = it->pixel_width;
2795 }
2796 }
2797
2798 /* Reset these values to zero because the produce_special_glyphs
2799 above has changed them. */
2800 it->pixel_width = it->ascent = it->descent = 0;
2801 it->phys_ascent = it->phys_descent = 0;
2802
2803 /* Set this after getting the dimensions of truncation and
2804 continuation glyphs, so that we don't produce glyphs when calling
2805 produce_special_glyphs, above. */
2806 it->glyph_row = row;
2807 it->area = TEXT_AREA;
2808
2809 /* Get the dimensions of the display area. The display area
2810 consists of the visible window area plus a horizontally scrolled
2811 part to the left of the window. All x-values are relative to the
2812 start of this total display area. */
2813 if (base_face_id != DEFAULT_FACE_ID)
2814 {
2815 /* Mode lines, menu bar in terminal frames. */
2816 it->first_visible_x = 0;
2817 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2818 }
2819 else
2820 {
2821 it->first_visible_x
2822 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2823 it->last_visible_x = (it->first_visible_x
2824 + window_box_width (w, TEXT_AREA));
2825
2826 /* If we truncate lines, leave room for the truncation glyph(s) at
2827 the right margin. Otherwise, leave room for the continuation
2828 glyph(s). Done only if the window has no right fringe. */
2829 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2830 {
2831 if (it->line_wrap == TRUNCATE)
2832 it->last_visible_x -= it->truncation_pixel_width;
2833 else
2834 it->last_visible_x -= it->continuation_pixel_width;
2835 }
2836
2837 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2838 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2839 }
2840
2841 /* Leave room for a border glyph. */
2842 if (!FRAME_WINDOW_P (it->f)
2843 && !WINDOW_RIGHTMOST_P (it->w))
2844 it->last_visible_x -= 1;
2845
2846 it->last_visible_y = window_text_bottom_y (w);
2847
2848 /* For mode lines and alike, arrange for the first glyph having a
2849 left box line if the face specifies a box. */
2850 if (base_face_id != DEFAULT_FACE_ID)
2851 {
2852 struct face *face;
2853
2854 it->face_id = remapped_base_face_id;
2855
2856 /* If we have a boxed mode line, make the first character appear
2857 with a left box line. */
2858 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2859 if (face && face->box != FACE_NO_BOX)
2860 it->start_of_box_run_p = true;
2861 }
2862
2863 /* If a buffer position was specified, set the iterator there,
2864 getting overlays and face properties from that position. */
2865 if (charpos >= BUF_BEG (current_buffer))
2866 {
2867 it->stop_charpos = charpos;
2868 it->end_charpos = ZV;
2869 eassert (charpos == BYTE_TO_CHAR (bytepos));
2870 IT_CHARPOS (*it) = charpos;
2871 IT_BYTEPOS (*it) = bytepos;
2872
2873 /* We will rely on `reseat' to set this up properly, via
2874 handle_face_prop. */
2875 it->face_id = it->base_face_id;
2876
2877 it->start = it->current;
2878 /* Do we need to reorder bidirectional text? Not if this is a
2879 unibyte buffer: by definition, none of the single-byte
2880 characters are strong R2L, so no reordering is needed. And
2881 bidi.c doesn't support unibyte buffers anyway. Also, don't
2882 reorder while we are loading loadup.el, since the tables of
2883 character properties needed for reordering are not yet
2884 available. */
2885 it->bidi_p =
2886 NILP (Vpurify_flag)
2887 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2888 && it->multibyte_p;
2889
2890 /* If we are to reorder bidirectional text, init the bidi
2891 iterator. */
2892 if (it->bidi_p)
2893 {
2894 /* Since we don't know at this point whether there will be
2895 any R2L lines in the window, we reserve space for
2896 truncation/continuation glyphs even if only the left
2897 fringe is absent. */
2898 if (base_face_id == DEFAULT_FACE_ID
2899 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2900 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2901 {
2902 if (it->line_wrap == TRUNCATE)
2903 it->last_visible_x -= it->truncation_pixel_width;
2904 else
2905 it->last_visible_x -= it->continuation_pixel_width;
2906 }
2907 /* Note the paragraph direction that this buffer wants to
2908 use. */
2909 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2910 Qleft_to_right))
2911 it->paragraph_embedding = L2R;
2912 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2913 Qright_to_left))
2914 it->paragraph_embedding = R2L;
2915 else
2916 it->paragraph_embedding = NEUTRAL_DIR;
2917 bidi_unshelve_cache (NULL, false);
2918 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2919 &it->bidi_it);
2920 }
2921
2922 /* Compute faces etc. */
2923 reseat (it, it->current.pos, true);
2924 }
2925
2926 CHECK_IT (it);
2927 }
2928
2929
2930 /* Initialize IT for the display of window W with window start POS. */
2931
2932 void
2933 start_display (struct it *it, struct window *w, struct text_pos pos)
2934 {
2935 struct glyph_row *row;
2936 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2937
2938 row = w->desired_matrix->rows + first_vpos;
2939 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2940 it->first_vpos = first_vpos;
2941
2942 /* Don't reseat to previous visible line start if current start
2943 position is in a string or image. */
2944 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2945 {
2946 int first_y = it->current_y;
2947
2948 /* If window start is not at a line start, skip forward to POS to
2949 get the correct continuation lines width. */
2950 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
2951 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2952 if (!start_at_line_beg_p)
2953 {
2954 int new_x;
2955
2956 reseat_at_previous_visible_line_start (it);
2957 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2958
2959 new_x = it->current_x + it->pixel_width;
2960
2961 /* If lines are continued, this line may end in the middle
2962 of a multi-glyph character (e.g. a control character
2963 displayed as \003, or in the middle of an overlay
2964 string). In this case move_it_to above will not have
2965 taken us to the start of the continuation line but to the
2966 end of the continued line. */
2967 if (it->current_x > 0
2968 && it->line_wrap != TRUNCATE /* Lines are continued. */
2969 && (/* And glyph doesn't fit on the line. */
2970 new_x > it->last_visible_x
2971 /* Or it fits exactly and we're on a window
2972 system frame. */
2973 || (new_x == it->last_visible_x
2974 && FRAME_WINDOW_P (it->f)
2975 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2976 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2977 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2978 {
2979 if ((it->current.dpvec_index >= 0
2980 || it->current.overlay_string_index >= 0)
2981 /* If we are on a newline from a display vector or
2982 overlay string, then we are already at the end of
2983 a screen line; no need to go to the next line in
2984 that case, as this line is not really continued.
2985 (If we do go to the next line, C-e will not DTRT.) */
2986 && it->c != '\n')
2987 {
2988 set_iterator_to_next (it, true);
2989 move_it_in_display_line_to (it, -1, -1, 0);
2990 }
2991
2992 it->continuation_lines_width += it->current_x;
2993 }
2994 /* If the character at POS is displayed via a display
2995 vector, move_it_to above stops at the final glyph of
2996 IT->dpvec. To make the caller redisplay that character
2997 again (a.k.a. start at POS), we need to reset the
2998 dpvec_index to the beginning of IT->dpvec. */
2999 else if (it->current.dpvec_index >= 0)
3000 it->current.dpvec_index = 0;
3001
3002 /* We're starting a new display line, not affected by the
3003 height of the continued line, so clear the appropriate
3004 fields in the iterator structure. */
3005 it->max_ascent = it->max_descent = 0;
3006 it->max_phys_ascent = it->max_phys_descent = 0;
3007
3008 it->current_y = first_y;
3009 it->vpos = 0;
3010 it->current_x = it->hpos = 0;
3011 }
3012 }
3013 }
3014
3015
3016 /* Return true if POS is a position in ellipses displayed for invisible
3017 text. W is the window we display, for text property lookup. */
3018
3019 static bool
3020 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3021 {
3022 Lisp_Object prop, window;
3023 bool ellipses_p = false;
3024 ptrdiff_t charpos = CHARPOS (pos->pos);
3025
3026 /* If POS specifies a position in a display vector, this might
3027 be for an ellipsis displayed for invisible text. We won't
3028 get the iterator set up for delivering that ellipsis unless
3029 we make sure that it gets aware of the invisible text. */
3030 if (pos->dpvec_index >= 0
3031 && pos->overlay_string_index < 0
3032 && CHARPOS (pos->string_pos) < 0
3033 && charpos > BEGV
3034 && (XSETWINDOW (window, w),
3035 prop = Fget_char_property (make_number (charpos),
3036 Qinvisible, window),
3037 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3038 {
3039 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3040 window);
3041 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3042 }
3043
3044 return ellipses_p;
3045 }
3046
3047
3048 /* Initialize IT for stepping through current_buffer in window W,
3049 starting at position POS that includes overlay string and display
3050 vector/ control character translation position information. Value
3051 is false if there are overlay strings with newlines at POS. */
3052
3053 static bool
3054 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3055 {
3056 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3057 int i;
3058 bool overlay_strings_with_newlines = false;
3059
3060 /* If POS specifies a position in a display vector, this might
3061 be for an ellipsis displayed for invisible text. We won't
3062 get the iterator set up for delivering that ellipsis unless
3063 we make sure that it gets aware of the invisible text. */
3064 if (in_ellipses_for_invisible_text_p (pos, w))
3065 {
3066 --charpos;
3067 bytepos = 0;
3068 }
3069
3070 /* Keep in mind: the call to reseat in init_iterator skips invisible
3071 text, so we might end up at a position different from POS. This
3072 is only a problem when POS is a row start after a newline and an
3073 overlay starts there with an after-string, and the overlay has an
3074 invisible property. Since we don't skip invisible text in
3075 display_line and elsewhere immediately after consuming the
3076 newline before the row start, such a POS will not be in a string,
3077 but the call to init_iterator below will move us to the
3078 after-string. */
3079 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3080
3081 /* This only scans the current chunk -- it should scan all chunks.
3082 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3083 to 16 in 22.1 to make this a lesser problem. */
3084 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3085 {
3086 const char *s = SSDATA (it->overlay_strings[i]);
3087 const char *e = s + SBYTES (it->overlay_strings[i]);
3088
3089 while (s < e && *s != '\n')
3090 ++s;
3091
3092 if (s < e)
3093 {
3094 overlay_strings_with_newlines = true;
3095 break;
3096 }
3097 }
3098
3099 /* If position is within an overlay string, set up IT to the right
3100 overlay string. */
3101 if (pos->overlay_string_index >= 0)
3102 {
3103 int relative_index;
3104
3105 /* If the first overlay string happens to have a `display'
3106 property for an image, the iterator will be set up for that
3107 image, and we have to undo that setup first before we can
3108 correct the overlay string index. */
3109 if (it->method == GET_FROM_IMAGE)
3110 pop_it (it);
3111
3112 /* We already have the first chunk of overlay strings in
3113 IT->overlay_strings. Load more until the one for
3114 pos->overlay_string_index is in IT->overlay_strings. */
3115 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3116 {
3117 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3118 it->current.overlay_string_index = 0;
3119 while (n--)
3120 {
3121 load_overlay_strings (it, 0);
3122 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3123 }
3124 }
3125
3126 it->current.overlay_string_index = pos->overlay_string_index;
3127 relative_index = (it->current.overlay_string_index
3128 % OVERLAY_STRING_CHUNK_SIZE);
3129 it->string = it->overlay_strings[relative_index];
3130 eassert (STRINGP (it->string));
3131 it->current.string_pos = pos->string_pos;
3132 it->method = GET_FROM_STRING;
3133 it->end_charpos = SCHARS (it->string);
3134 /* Set up the bidi iterator for this overlay string. */
3135 if (it->bidi_p)
3136 {
3137 it->bidi_it.string.lstring = it->string;
3138 it->bidi_it.string.s = NULL;
3139 it->bidi_it.string.schars = SCHARS (it->string);
3140 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3141 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3142 it->bidi_it.string.unibyte = !it->multibyte_p;
3143 it->bidi_it.w = it->w;
3144 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3145 FRAME_WINDOW_P (it->f), &it->bidi_it);
3146
3147 /* Synchronize the state of the bidi iterator with
3148 pos->string_pos. For any string position other than
3149 zero, this will be done automagically when we resume
3150 iteration over the string and get_visually_first_element
3151 is called. But if string_pos is zero, and the string is
3152 to be reordered for display, we need to resync manually,
3153 since it could be that the iteration state recorded in
3154 pos ended at string_pos of 0 moving backwards in string. */
3155 if (CHARPOS (pos->string_pos) == 0)
3156 {
3157 get_visually_first_element (it);
3158 if (IT_STRING_CHARPOS (*it) != 0)
3159 do {
3160 /* Paranoia. */
3161 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3162 bidi_move_to_visually_next (&it->bidi_it);
3163 } while (it->bidi_it.charpos != 0);
3164 }
3165 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3166 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3167 }
3168 }
3169
3170 if (CHARPOS (pos->string_pos) >= 0)
3171 {
3172 /* Recorded position is not in an overlay string, but in another
3173 string. This can only be a string from a `display' property.
3174 IT should already be filled with that string. */
3175 it->current.string_pos = pos->string_pos;
3176 eassert (STRINGP (it->string));
3177 if (it->bidi_p)
3178 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3179 FRAME_WINDOW_P (it->f), &it->bidi_it);
3180 }
3181
3182 /* Restore position in display vector translations, control
3183 character translations or ellipses. */
3184 if (pos->dpvec_index >= 0)
3185 {
3186 if (it->dpvec == NULL)
3187 get_next_display_element (it);
3188 eassert (it->dpvec && it->current.dpvec_index == 0);
3189 it->current.dpvec_index = pos->dpvec_index;
3190 }
3191
3192 CHECK_IT (it);
3193 return !overlay_strings_with_newlines;
3194 }
3195
3196
3197 /* Initialize IT for stepping through current_buffer in window W
3198 starting at ROW->start. */
3199
3200 static void
3201 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3202 {
3203 init_from_display_pos (it, w, &row->start);
3204 it->start = row->start;
3205 it->continuation_lines_width = row->continuation_lines_width;
3206 CHECK_IT (it);
3207 }
3208
3209
3210 /* Initialize IT for stepping through current_buffer in window W
3211 starting in the line following ROW, i.e. starting at ROW->end.
3212 Value is false if there are overlay strings with newlines at ROW's
3213 end position. */
3214
3215 static bool
3216 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3217 {
3218 bool success = false;
3219
3220 if (init_from_display_pos (it, w, &row->end))
3221 {
3222 if (row->continued_p)
3223 it->continuation_lines_width
3224 = row->continuation_lines_width + row->pixel_width;
3225 CHECK_IT (it);
3226 success = true;
3227 }
3228
3229 return success;
3230 }
3231
3232
3233
3234 \f
3235 /***********************************************************************
3236 Text properties
3237 ***********************************************************************/
3238
3239 /* Called when IT reaches IT->stop_charpos. Handle text property and
3240 overlay changes. Set IT->stop_charpos to the next position where
3241 to stop. */
3242
3243 static void
3244 handle_stop (struct it *it)
3245 {
3246 enum prop_handled handled;
3247 bool handle_overlay_change_p;
3248 struct props *p;
3249
3250 it->dpvec = NULL;
3251 it->current.dpvec_index = -1;
3252 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3253 it->ellipsis_p = false;
3254
3255 /* Use face of preceding text for ellipsis (if invisible) */
3256 if (it->selective_display_ellipsis_p)
3257 it->saved_face_id = it->face_id;
3258
3259 /* Here's the description of the semantics of, and the logic behind,
3260 the various HANDLED_* statuses:
3261
3262 HANDLED_NORMALLY means the handler did its job, and the loop
3263 should proceed to calling the next handler in order.
3264
3265 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3266 change in the properties and overlays at current position, so the
3267 loop should be restarted, to re-invoke the handlers that were
3268 already called. This happens when fontification-functions were
3269 called by handle_fontified_prop, and actually fontified
3270 something. Another case where HANDLED_RECOMPUTE_PROPS is
3271 returned is when we discover overlay strings that need to be
3272 displayed right away. The loop below will continue for as long
3273 as the status is HANDLED_RECOMPUTE_PROPS.
3274
3275 HANDLED_RETURN means return immediately to the caller, to
3276 continue iteration without calling any further handlers. This is
3277 used when we need to act on some property right away, for example
3278 when we need to display the ellipsis or a replacing display
3279 property, such as display string or image.
3280
3281 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3282 consumed, and the handler switched to the next overlay string.
3283 This signals the loop below to refrain from looking for more
3284 overlays before all the overlay strings of the current overlay
3285 are processed.
3286
3287 Some of the handlers called by the loop push the iterator state
3288 onto the stack (see 'push_it'), and arrange for the iteration to
3289 continue with another object, such as an image, a display string,
3290 or an overlay string. In most such cases, it->stop_charpos is
3291 set to the first character of the string, so that when the
3292 iteration resumes, this function will immediately be called
3293 again, to examine the properties at the beginning of the string.
3294
3295 When a display or overlay string is exhausted, the iterator state
3296 is popped (see 'pop_it'), and iteration continues with the
3297 previous object. Again, in many such cases this function is
3298 called again to find the next position where properties might
3299 change. */
3300
3301 do
3302 {
3303 handled = HANDLED_NORMALLY;
3304
3305 /* Call text property handlers. */
3306 for (p = it_props; p->handler; ++p)
3307 {
3308 handled = p->handler (it);
3309
3310 if (handled == HANDLED_RECOMPUTE_PROPS)
3311 break;
3312 else if (handled == HANDLED_RETURN)
3313 {
3314 /* We still want to show before and after strings from
3315 overlays even if the actual buffer text is replaced. */
3316 if (!handle_overlay_change_p
3317 || it->sp > 1
3318 /* Don't call get_overlay_strings_1 if we already
3319 have overlay strings loaded, because doing so
3320 will load them again and push the iterator state
3321 onto the stack one more time, which is not
3322 expected by the rest of the code that processes
3323 overlay strings. */
3324 || (it->current.overlay_string_index < 0
3325 && !get_overlay_strings_1 (it, 0, false)))
3326 {
3327 if (it->ellipsis_p)
3328 setup_for_ellipsis (it, 0);
3329 /* When handling a display spec, we might load an
3330 empty string. In that case, discard it here. We
3331 used to discard it in handle_single_display_spec,
3332 but that causes get_overlay_strings_1, above, to
3333 ignore overlay strings that we must check. */
3334 if (STRINGP (it->string) && !SCHARS (it->string))
3335 pop_it (it);
3336 return;
3337 }
3338 else if (STRINGP (it->string) && !SCHARS (it->string))
3339 pop_it (it);
3340 else
3341 {
3342 it->string_from_display_prop_p = false;
3343 it->from_disp_prop_p = false;
3344 handle_overlay_change_p = false;
3345 }
3346 handled = HANDLED_RECOMPUTE_PROPS;
3347 break;
3348 }
3349 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3350 handle_overlay_change_p = false;
3351 }
3352
3353 if (handled != HANDLED_RECOMPUTE_PROPS)
3354 {
3355 /* Don't check for overlay strings below when set to deliver
3356 characters from a display vector. */
3357 if (it->method == GET_FROM_DISPLAY_VECTOR)
3358 handle_overlay_change_p = false;
3359
3360 /* Handle overlay changes.
3361 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3362 if it finds overlays. */
3363 if (handle_overlay_change_p)
3364 handled = handle_overlay_change (it);
3365 }
3366
3367 if (it->ellipsis_p)
3368 {
3369 setup_for_ellipsis (it, 0);
3370 break;
3371 }
3372 }
3373 while (handled == HANDLED_RECOMPUTE_PROPS);
3374
3375 /* Determine where to stop next. */
3376 if (handled == HANDLED_NORMALLY)
3377 compute_stop_pos (it);
3378 }
3379
3380
3381 /* Compute IT->stop_charpos from text property and overlay change
3382 information for IT's current position. */
3383
3384 static void
3385 compute_stop_pos (struct it *it)
3386 {
3387 register INTERVAL iv, next_iv;
3388 Lisp_Object object, limit, position;
3389 ptrdiff_t charpos, bytepos;
3390
3391 if (STRINGP (it->string))
3392 {
3393 /* Strings are usually short, so don't limit the search for
3394 properties. */
3395 it->stop_charpos = it->end_charpos;
3396 object = it->string;
3397 limit = Qnil;
3398 charpos = IT_STRING_CHARPOS (*it);
3399 bytepos = IT_STRING_BYTEPOS (*it);
3400 }
3401 else
3402 {
3403 ptrdiff_t pos;
3404
3405 /* If end_charpos is out of range for some reason, such as a
3406 misbehaving display function, rationalize it (Bug#5984). */
3407 if (it->end_charpos > ZV)
3408 it->end_charpos = ZV;
3409 it->stop_charpos = it->end_charpos;
3410
3411 /* If next overlay change is in front of the current stop pos
3412 (which is IT->end_charpos), stop there. Note: value of
3413 next_overlay_change is point-max if no overlay change
3414 follows. */
3415 charpos = IT_CHARPOS (*it);
3416 bytepos = IT_BYTEPOS (*it);
3417 pos = next_overlay_change (charpos);
3418 if (pos < it->stop_charpos)
3419 it->stop_charpos = pos;
3420
3421 /* Set up variables for computing the stop position from text
3422 property changes. */
3423 XSETBUFFER (object, current_buffer);
3424 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3425 }
3426
3427 /* Get the interval containing IT's position. Value is a null
3428 interval if there isn't such an interval. */
3429 position = make_number (charpos);
3430 iv = validate_interval_range (object, &position, &position, false);
3431 if (iv)
3432 {
3433 Lisp_Object values_here[LAST_PROP_IDX];
3434 struct props *p;
3435
3436 /* Get properties here. */
3437 for (p = it_props; p->handler; ++p)
3438 values_here[p->idx] = textget (iv->plist,
3439 builtin_lisp_symbol (p->name));
3440
3441 /* Look for an interval following iv that has different
3442 properties. */
3443 for (next_iv = next_interval (iv);
3444 (next_iv
3445 && (NILP (limit)
3446 || XFASTINT (limit) > next_iv->position));
3447 next_iv = next_interval (next_iv))
3448 {
3449 for (p = it_props; p->handler; ++p)
3450 {
3451 Lisp_Object new_value = textget (next_iv->plist,
3452 builtin_lisp_symbol (p->name));
3453 if (!EQ (values_here[p->idx], new_value))
3454 break;
3455 }
3456
3457 if (p->handler)
3458 break;
3459 }
3460
3461 if (next_iv)
3462 {
3463 if (INTEGERP (limit)
3464 && next_iv->position >= XFASTINT (limit))
3465 /* No text property change up to limit. */
3466 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3467 else
3468 /* Text properties change in next_iv. */
3469 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3470 }
3471 }
3472
3473 if (it->cmp_it.id < 0)
3474 {
3475 ptrdiff_t stoppos = it->end_charpos;
3476
3477 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3478 stoppos = -1;
3479 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3480 stoppos, it->string);
3481 }
3482
3483 eassert (STRINGP (it->string)
3484 || (it->stop_charpos >= BEGV
3485 && it->stop_charpos >= IT_CHARPOS (*it)));
3486 }
3487
3488
3489 /* Return the position of the next overlay change after POS in
3490 current_buffer. Value is point-max if no overlay change
3491 follows. This is like `next-overlay-change' but doesn't use
3492 xmalloc. */
3493
3494 static ptrdiff_t
3495 next_overlay_change (ptrdiff_t pos)
3496 {
3497 ptrdiff_t i, noverlays;
3498 ptrdiff_t endpos;
3499 Lisp_Object *overlays;
3500 USE_SAFE_ALLOCA;
3501
3502 /* Get all overlays at the given position. */
3503 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3504
3505 /* If any of these overlays ends before endpos,
3506 use its ending point instead. */
3507 for (i = 0; i < noverlays; ++i)
3508 {
3509 Lisp_Object oend;
3510 ptrdiff_t oendpos;
3511
3512 oend = OVERLAY_END (overlays[i]);
3513 oendpos = OVERLAY_POSITION (oend);
3514 endpos = min (endpos, oendpos);
3515 }
3516
3517 SAFE_FREE ();
3518 return endpos;
3519 }
3520
3521 /* How many characters forward to search for a display property or
3522 display string. Searching too far forward makes the bidi display
3523 sluggish, especially in small windows. */
3524 #define MAX_DISP_SCAN 250
3525
3526 /* Return the character position of a display string at or after
3527 position specified by POSITION. If no display string exists at or
3528 after POSITION, return ZV. A display string is either an overlay
3529 with `display' property whose value is a string, or a `display'
3530 text property whose value is a string. STRING is data about the
3531 string to iterate; if STRING->lstring is nil, we are iterating a
3532 buffer. FRAME_WINDOW_P is true when we are displaying a window
3533 on a GUI frame. DISP_PROP is set to zero if we searched
3534 MAX_DISP_SCAN characters forward without finding any display
3535 strings, non-zero otherwise. It is set to 2 if the display string
3536 uses any kind of `(space ...)' spec that will produce a stretch of
3537 white space in the text area. */
3538 ptrdiff_t
3539 compute_display_string_pos (struct text_pos *position,
3540 struct bidi_string_data *string,
3541 struct window *w,
3542 bool frame_window_p, int *disp_prop)
3543 {
3544 /* OBJECT = nil means current buffer. */
3545 Lisp_Object object, object1;
3546 Lisp_Object pos, spec, limpos;
3547 bool string_p = string && (STRINGP (string->lstring) || string->s);
3548 ptrdiff_t eob = string_p ? string->schars : ZV;
3549 ptrdiff_t begb = string_p ? 0 : BEGV;
3550 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3551 ptrdiff_t lim =
3552 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3553 struct text_pos tpos;
3554 int rv = 0;
3555
3556 if (string && STRINGP (string->lstring))
3557 object1 = object = string->lstring;
3558 else if (w && !string_p)
3559 {
3560 XSETWINDOW (object, w);
3561 object1 = Qnil;
3562 }
3563 else
3564 object1 = object = Qnil;
3565
3566 *disp_prop = 1;
3567
3568 if (charpos >= eob
3569 /* We don't support display properties whose values are strings
3570 that have display string properties. */
3571 || string->from_disp_str
3572 /* C strings cannot have display properties. */
3573 || (string->s && !STRINGP (object)))
3574 {
3575 *disp_prop = 0;
3576 return eob;
3577 }
3578
3579 /* If the character at CHARPOS is where the display string begins,
3580 return CHARPOS. */
3581 pos = make_number (charpos);
3582 if (STRINGP (object))
3583 bufpos = string->bufpos;
3584 else
3585 bufpos = charpos;
3586 tpos = *position;
3587 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3588 && (charpos <= begb
3589 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3590 object),
3591 spec))
3592 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3593 frame_window_p)))
3594 {
3595 if (rv == 2)
3596 *disp_prop = 2;
3597 return charpos;
3598 }
3599
3600 /* Look forward for the first character with a `display' property
3601 that will replace the underlying text when displayed. */
3602 limpos = make_number (lim);
3603 do {
3604 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3605 CHARPOS (tpos) = XFASTINT (pos);
3606 if (CHARPOS (tpos) >= lim)
3607 {
3608 *disp_prop = 0;
3609 break;
3610 }
3611 if (STRINGP (object))
3612 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3613 else
3614 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3615 spec = Fget_char_property (pos, Qdisplay, object);
3616 if (!STRINGP (object))
3617 bufpos = CHARPOS (tpos);
3618 } while (NILP (spec)
3619 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3620 bufpos, frame_window_p)));
3621 if (rv == 2)
3622 *disp_prop = 2;
3623
3624 return CHARPOS (tpos);
3625 }
3626
3627 /* Return the character position of the end of the display string that
3628 started at CHARPOS. If there's no display string at CHARPOS,
3629 return -1. A display string is either an overlay with `display'
3630 property whose value is a string or a `display' text property whose
3631 value is a string. */
3632 ptrdiff_t
3633 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3634 {
3635 /* OBJECT = nil means current buffer. */
3636 Lisp_Object object =
3637 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3638 Lisp_Object pos = make_number (charpos);
3639 ptrdiff_t eob =
3640 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3641
3642 if (charpos >= eob || (string->s && !STRINGP (object)))
3643 return eob;
3644
3645 /* It could happen that the display property or overlay was removed
3646 since we found it in compute_display_string_pos above. One way
3647 this can happen is if JIT font-lock was called (through
3648 handle_fontified_prop), and jit-lock-functions remove text
3649 properties or overlays from the portion of buffer that includes
3650 CHARPOS. Muse mode is known to do that, for example. In this
3651 case, we return -1 to the caller, to signal that no display
3652 string is actually present at CHARPOS. See bidi_fetch_char for
3653 how this is handled.
3654
3655 An alternative would be to never look for display properties past
3656 it->stop_charpos. But neither compute_display_string_pos nor
3657 bidi_fetch_char that calls it know or care where the next
3658 stop_charpos is. */
3659 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3660 return -1;
3661
3662 /* Look forward for the first character where the `display' property
3663 changes. */
3664 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3665
3666 return XFASTINT (pos);
3667 }
3668
3669
3670 \f
3671 /***********************************************************************
3672 Fontification
3673 ***********************************************************************/
3674
3675 /* Handle changes in the `fontified' property of the current buffer by
3676 calling hook functions from Qfontification_functions to fontify
3677 regions of text. */
3678
3679 static enum prop_handled
3680 handle_fontified_prop (struct it *it)
3681 {
3682 Lisp_Object prop, pos;
3683 enum prop_handled handled = HANDLED_NORMALLY;
3684
3685 if (!NILP (Vmemory_full))
3686 return handled;
3687
3688 /* Get the value of the `fontified' property at IT's current buffer
3689 position. (The `fontified' property doesn't have a special
3690 meaning in strings.) If the value is nil, call functions from
3691 Qfontification_functions. */
3692 if (!STRINGP (it->string)
3693 && it->s == NULL
3694 && !NILP (Vfontification_functions)
3695 && !NILP (Vrun_hooks)
3696 && (pos = make_number (IT_CHARPOS (*it)),
3697 prop = Fget_char_property (pos, Qfontified, Qnil),
3698 /* Ignore the special cased nil value always present at EOB since
3699 no amount of fontifying will be able to change it. */
3700 NILP (prop) && IT_CHARPOS (*it) < Z))
3701 {
3702 ptrdiff_t count = SPECPDL_INDEX ();
3703 Lisp_Object val;
3704 struct buffer *obuf = current_buffer;
3705 ptrdiff_t begv = BEGV, zv = ZV;
3706 bool old_clip_changed = current_buffer->clip_changed;
3707
3708 val = Vfontification_functions;
3709 specbind (Qfontification_functions, Qnil);
3710
3711 eassert (it->end_charpos == ZV);
3712
3713 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3714 safe_call1 (val, pos);
3715 else
3716 {
3717 Lisp_Object fns, fn;
3718 struct gcpro gcpro1, gcpro2;
3719
3720 fns = Qnil;
3721 GCPRO2 (val, fns);
3722
3723 for (; CONSP (val); val = XCDR (val))
3724 {
3725 fn = XCAR (val);
3726
3727 if (EQ (fn, Qt))
3728 {
3729 /* A value of t indicates this hook has a local
3730 binding; it means to run the global binding too.
3731 In a global value, t should not occur. If it
3732 does, we must ignore it to avoid an endless
3733 loop. */
3734 for (fns = Fdefault_value (Qfontification_functions);
3735 CONSP (fns);
3736 fns = XCDR (fns))
3737 {
3738 fn = XCAR (fns);
3739 if (!EQ (fn, Qt))
3740 safe_call1 (fn, pos);
3741 }
3742 }
3743 else
3744 safe_call1 (fn, pos);
3745 }
3746
3747 UNGCPRO;
3748 }
3749
3750 unbind_to (count, Qnil);
3751
3752 /* Fontification functions routinely call `save-restriction'.
3753 Normally, this tags clip_changed, which can confuse redisplay
3754 (see discussion in Bug#6671). Since we don't perform any
3755 special handling of fontification changes in the case where
3756 `save-restriction' isn't called, there's no point doing so in
3757 this case either. So, if the buffer's restrictions are
3758 actually left unchanged, reset clip_changed. */
3759 if (obuf == current_buffer)
3760 {
3761 if (begv == BEGV && zv == ZV)
3762 current_buffer->clip_changed = old_clip_changed;
3763 }
3764 /* There isn't much we can reasonably do to protect against
3765 misbehaving fontification, but here's a fig leaf. */
3766 else if (BUFFER_LIVE_P (obuf))
3767 set_buffer_internal_1 (obuf);
3768
3769 /* The fontification code may have added/removed text.
3770 It could do even a lot worse, but let's at least protect against
3771 the most obvious case where only the text past `pos' gets changed',
3772 as is/was done in grep.el where some escapes sequences are turned
3773 into face properties (bug#7876). */
3774 it->end_charpos = ZV;
3775
3776 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3777 something. This avoids an endless loop if they failed to
3778 fontify the text for which reason ever. */
3779 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3780 handled = HANDLED_RECOMPUTE_PROPS;
3781 }
3782
3783 return handled;
3784 }
3785
3786
3787 \f
3788 /***********************************************************************
3789 Faces
3790 ***********************************************************************/
3791
3792 /* Set up iterator IT from face properties at its current position.
3793 Called from handle_stop. */
3794
3795 static enum prop_handled
3796 handle_face_prop (struct it *it)
3797 {
3798 int new_face_id;
3799 ptrdiff_t next_stop;
3800
3801 if (!STRINGP (it->string))
3802 {
3803 new_face_id
3804 = face_at_buffer_position (it->w,
3805 IT_CHARPOS (*it),
3806 &next_stop,
3807 (IT_CHARPOS (*it)
3808 + TEXT_PROP_DISTANCE_LIMIT),
3809 false, it->base_face_id);
3810
3811 /* Is this a start of a run of characters with box face?
3812 Caveat: this can be called for a freshly initialized
3813 iterator; face_id is -1 in this case. We know that the new
3814 face will not change until limit, i.e. if the new face has a
3815 box, all characters up to limit will have one. But, as
3816 usual, we don't know whether limit is really the end. */
3817 if (new_face_id != it->face_id)
3818 {
3819 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3820 /* If it->face_id is -1, old_face below will be NULL, see
3821 the definition of FACE_FROM_ID. This will happen if this
3822 is the initial call that gets the face. */
3823 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3824
3825 /* If the value of face_id of the iterator is -1, we have to
3826 look in front of IT's position and see whether there is a
3827 face there that's different from new_face_id. */
3828 if (!old_face && IT_CHARPOS (*it) > BEG)
3829 {
3830 int prev_face_id = face_before_it_pos (it);
3831
3832 old_face = FACE_FROM_ID (it->f, prev_face_id);
3833 }
3834
3835 /* If the new face has a box, but the old face does not,
3836 this is the start of a run of characters with box face,
3837 i.e. this character has a shadow on the left side. */
3838 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3839 && (old_face == NULL || !old_face->box));
3840 it->face_box_p = new_face->box != FACE_NO_BOX;
3841 }
3842 }
3843 else
3844 {
3845 int base_face_id;
3846 ptrdiff_t bufpos;
3847 int i;
3848 Lisp_Object from_overlay
3849 = (it->current.overlay_string_index >= 0
3850 ? it->string_overlays[it->current.overlay_string_index
3851 % OVERLAY_STRING_CHUNK_SIZE]
3852 : Qnil);
3853
3854 /* See if we got to this string directly or indirectly from
3855 an overlay property. That includes the before-string or
3856 after-string of an overlay, strings in display properties
3857 provided by an overlay, their text properties, etc.
3858
3859 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3860 if (! NILP (from_overlay))
3861 for (i = it->sp - 1; i >= 0; i--)
3862 {
3863 if (it->stack[i].current.overlay_string_index >= 0)
3864 from_overlay
3865 = it->string_overlays[it->stack[i].current.overlay_string_index
3866 % OVERLAY_STRING_CHUNK_SIZE];
3867 else if (! NILP (it->stack[i].from_overlay))
3868 from_overlay = it->stack[i].from_overlay;
3869
3870 if (!NILP (from_overlay))
3871 break;
3872 }
3873
3874 if (! NILP (from_overlay))
3875 {
3876 bufpos = IT_CHARPOS (*it);
3877 /* For a string from an overlay, the base face depends
3878 only on text properties and ignores overlays. */
3879 base_face_id
3880 = face_for_overlay_string (it->w,
3881 IT_CHARPOS (*it),
3882 &next_stop,
3883 (IT_CHARPOS (*it)
3884 + TEXT_PROP_DISTANCE_LIMIT),
3885 false,
3886 from_overlay);
3887 }
3888 else
3889 {
3890 bufpos = 0;
3891
3892 /* For strings from a `display' property, use the face at
3893 IT's current buffer position as the base face to merge
3894 with, so that overlay strings appear in the same face as
3895 surrounding text, unless they specify their own faces.
3896 For strings from wrap-prefix and line-prefix properties,
3897 use the default face, possibly remapped via
3898 Vface_remapping_alist. */
3899 /* Note that the fact that we use the face at _buffer_
3900 position means that a 'display' property on an overlay
3901 string will not inherit the face of that overlay string,
3902 but will instead revert to the face of buffer text
3903 covered by the overlay. This is visible, e.g., when the
3904 overlay specifies a box face, but neither the buffer nor
3905 the display string do. This sounds like a design bug,
3906 but Emacs always did that since v21.1, so changing that
3907 might be a big deal. */
3908 base_face_id = it->string_from_prefix_prop_p
3909 ? (!NILP (Vface_remapping_alist)
3910 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3911 : DEFAULT_FACE_ID)
3912 : underlying_face_id (it);
3913 }
3914
3915 new_face_id = face_at_string_position (it->w,
3916 it->string,
3917 IT_STRING_CHARPOS (*it),
3918 bufpos,
3919 &next_stop,
3920 base_face_id, false);
3921
3922 /* Is this a start of a run of characters with box? Caveat:
3923 this can be called for a freshly allocated iterator; face_id
3924 is -1 is this case. We know that the new face will not
3925 change until the next check pos, i.e. if the new face has a
3926 box, all characters up to that position will have a
3927 box. But, as usual, we don't know whether that position
3928 is really the end. */
3929 if (new_face_id != it->face_id)
3930 {
3931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3932 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3933
3934 /* If new face has a box but old face hasn't, this is the
3935 start of a run of characters with box, i.e. it has a
3936 shadow on the left side. */
3937 it->start_of_box_run_p
3938 = new_face->box && (old_face == NULL || !old_face->box);
3939 it->face_box_p = new_face->box != FACE_NO_BOX;
3940 }
3941 }
3942
3943 it->face_id = new_face_id;
3944 return HANDLED_NORMALLY;
3945 }
3946
3947
3948 /* Return the ID of the face ``underlying'' IT's current position,
3949 which is in a string. If the iterator is associated with a
3950 buffer, return the face at IT's current buffer position.
3951 Otherwise, use the iterator's base_face_id. */
3952
3953 static int
3954 underlying_face_id (struct it *it)
3955 {
3956 int face_id = it->base_face_id, i;
3957
3958 eassert (STRINGP (it->string));
3959
3960 for (i = it->sp - 1; i >= 0; --i)
3961 if (NILP (it->stack[i].string))
3962 face_id = it->stack[i].face_id;
3963
3964 return face_id;
3965 }
3966
3967
3968 /* Compute the face one character before or after the current position
3969 of IT, in the visual order. BEFORE_P means get the face
3970 in front (to the left in L2R paragraphs, to the right in R2L
3971 paragraphs) of IT's screen position. Value is the ID of the face. */
3972
3973 static int
3974 face_before_or_after_it_pos (struct it *it, bool before_p)
3975 {
3976 int face_id, limit;
3977 ptrdiff_t next_check_charpos;
3978 struct it it_copy;
3979 void *it_copy_data = NULL;
3980
3981 eassert (it->s == NULL);
3982
3983 if (STRINGP (it->string))
3984 {
3985 ptrdiff_t bufpos, charpos;
3986 int base_face_id;
3987
3988 /* No face change past the end of the string (for the case
3989 we are padding with spaces). No face change before the
3990 string start. */
3991 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3992 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3993 return it->face_id;
3994
3995 if (!it->bidi_p)
3996 {
3997 /* Set charpos to the position before or after IT's current
3998 position, in the logical order, which in the non-bidi
3999 case is the same as the visual order. */
4000 if (before_p)
4001 charpos = IT_STRING_CHARPOS (*it) - 1;
4002 else if (it->what == IT_COMPOSITION)
4003 /* For composition, we must check the character after the
4004 composition. */
4005 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4006 else
4007 charpos = IT_STRING_CHARPOS (*it) + 1;
4008 }
4009 else
4010 {
4011 if (before_p)
4012 {
4013 /* With bidi iteration, the character before the current
4014 in the visual order cannot be found by simple
4015 iteration, because "reverse" reordering is not
4016 supported. Instead, we need to use the move_it_*
4017 family of functions. */
4018 /* Ignore face changes before the first visible
4019 character on this display line. */
4020 if (it->current_x <= it->first_visible_x)
4021 return it->face_id;
4022 SAVE_IT (it_copy, *it, it_copy_data);
4023 /* Implementation note: Since move_it_in_display_line
4024 works in the iterator geometry, and thinks the first
4025 character is always the leftmost, even in R2L lines,
4026 we don't need to distinguish between the R2L and L2R
4027 cases here. */
4028 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4029 it_copy.current_x - 1, MOVE_TO_X);
4030 charpos = IT_STRING_CHARPOS (it_copy);
4031 RESTORE_IT (it, it, it_copy_data);
4032 }
4033 else
4034 {
4035 /* Set charpos to the string position of the character
4036 that comes after IT's current position in the visual
4037 order. */
4038 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4039
4040 it_copy = *it;
4041 while (n--)
4042 bidi_move_to_visually_next (&it_copy.bidi_it);
4043
4044 charpos = it_copy.bidi_it.charpos;
4045 }
4046 }
4047 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4048
4049 if (it->current.overlay_string_index >= 0)
4050 bufpos = IT_CHARPOS (*it);
4051 else
4052 bufpos = 0;
4053
4054 base_face_id = underlying_face_id (it);
4055
4056 /* Get the face for ASCII, or unibyte. */
4057 face_id = face_at_string_position (it->w,
4058 it->string,
4059 charpos,
4060 bufpos,
4061 &next_check_charpos,
4062 base_face_id, false);
4063
4064 /* Correct the face for charsets different from ASCII. Do it
4065 for the multibyte case only. The face returned above is
4066 suitable for unibyte text if IT->string is unibyte. */
4067 if (STRING_MULTIBYTE (it->string))
4068 {
4069 struct text_pos pos1 = string_pos (charpos, it->string);
4070 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4071 int c, len;
4072 struct face *face = FACE_FROM_ID (it->f, face_id);
4073
4074 c = string_char_and_length (p, &len);
4075 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4076 }
4077 }
4078 else
4079 {
4080 struct text_pos pos;
4081
4082 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4083 || (IT_CHARPOS (*it) <= BEGV && before_p))
4084 return it->face_id;
4085
4086 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4087 pos = it->current.pos;
4088
4089 if (!it->bidi_p)
4090 {
4091 if (before_p)
4092 DEC_TEXT_POS (pos, it->multibyte_p);
4093 else
4094 {
4095 if (it->what == IT_COMPOSITION)
4096 {
4097 /* For composition, we must check the position after
4098 the composition. */
4099 pos.charpos += it->cmp_it.nchars;
4100 pos.bytepos += it->len;
4101 }
4102 else
4103 INC_TEXT_POS (pos, it->multibyte_p);
4104 }
4105 }
4106 else
4107 {
4108 if (before_p)
4109 {
4110 /* With bidi iteration, the character before the current
4111 in the visual order cannot be found by simple
4112 iteration, because "reverse" reordering is not
4113 supported. Instead, we need to use the move_it_*
4114 family of functions. */
4115 /* Ignore face changes before the first visible
4116 character on this display line. */
4117 if (it->current_x <= it->first_visible_x)
4118 return it->face_id;
4119 SAVE_IT (it_copy, *it, it_copy_data);
4120 /* Implementation note: Since move_it_in_display_line
4121 works in the iterator geometry, and thinks the first
4122 character is always the leftmost, even in R2L lines,
4123 we don't need to distinguish between the R2L and L2R
4124 cases here. */
4125 move_it_in_display_line (&it_copy, ZV,
4126 it_copy.current_x - 1, MOVE_TO_X);
4127 pos = it_copy.current.pos;
4128 RESTORE_IT (it, it, it_copy_data);
4129 }
4130 else
4131 {
4132 /* Set charpos to the buffer position of the character
4133 that comes after IT's current position in the visual
4134 order. */
4135 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4136
4137 it_copy = *it;
4138 while (n--)
4139 bidi_move_to_visually_next (&it_copy.bidi_it);
4140
4141 SET_TEXT_POS (pos,
4142 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4143 }
4144 }
4145 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4146
4147 /* Determine face for CHARSET_ASCII, or unibyte. */
4148 face_id = face_at_buffer_position (it->w,
4149 CHARPOS (pos),
4150 &next_check_charpos,
4151 limit, false, -1);
4152
4153 /* Correct the face for charsets different from ASCII. Do it
4154 for the multibyte case only. The face returned above is
4155 suitable for unibyte text if current_buffer is unibyte. */
4156 if (it->multibyte_p)
4157 {
4158 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4159 struct face *face = FACE_FROM_ID (it->f, face_id);
4160 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4161 }
4162 }
4163
4164 return face_id;
4165 }
4166
4167
4168 \f
4169 /***********************************************************************
4170 Invisible text
4171 ***********************************************************************/
4172
4173 /* Set up iterator IT from invisible properties at its current
4174 position. Called from handle_stop. */
4175
4176 static enum prop_handled
4177 handle_invisible_prop (struct it *it)
4178 {
4179 enum prop_handled handled = HANDLED_NORMALLY;
4180 int invis;
4181 Lisp_Object prop;
4182
4183 if (STRINGP (it->string))
4184 {
4185 Lisp_Object end_charpos, limit, charpos;
4186
4187 /* Get the value of the invisible text property at the
4188 current position. Value will be nil if there is no such
4189 property. */
4190 charpos = make_number (IT_STRING_CHARPOS (*it));
4191 prop = Fget_text_property (charpos, Qinvisible, it->string);
4192 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4193
4194 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4195 {
4196 /* Record whether we have to display an ellipsis for the
4197 invisible text. */
4198 bool display_ellipsis_p = (invis == 2);
4199 ptrdiff_t len, endpos;
4200
4201 handled = HANDLED_RECOMPUTE_PROPS;
4202
4203 /* Get the position at which the next visible text can be
4204 found in IT->string, if any. */
4205 endpos = len = SCHARS (it->string);
4206 XSETINT (limit, len);
4207 do
4208 {
4209 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4210 it->string, limit);
4211 if (INTEGERP (end_charpos))
4212 {
4213 endpos = XFASTINT (end_charpos);
4214 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4215 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4216 if (invis == 2)
4217 display_ellipsis_p = true;
4218 }
4219 }
4220 while (invis != 0 && endpos < len);
4221
4222 if (display_ellipsis_p)
4223 it->ellipsis_p = true;
4224
4225 if (endpos < len)
4226 {
4227 /* Text at END_CHARPOS is visible. Move IT there. */
4228 struct text_pos old;
4229 ptrdiff_t oldpos;
4230
4231 old = it->current.string_pos;
4232 oldpos = CHARPOS (old);
4233 if (it->bidi_p)
4234 {
4235 if (it->bidi_it.first_elt
4236 && it->bidi_it.charpos < SCHARS (it->string))
4237 bidi_paragraph_init (it->paragraph_embedding,
4238 &it->bidi_it, true);
4239 /* Bidi-iterate out of the invisible text. */
4240 do
4241 {
4242 bidi_move_to_visually_next (&it->bidi_it);
4243 }
4244 while (oldpos <= it->bidi_it.charpos
4245 && it->bidi_it.charpos < endpos);
4246
4247 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4249 if (IT_CHARPOS (*it) >= endpos)
4250 it->prev_stop = endpos;
4251 }
4252 else
4253 {
4254 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4255 compute_string_pos (&it->current.string_pos, old, it->string);
4256 }
4257 }
4258 else
4259 {
4260 /* The rest of the string is invisible. If this is an
4261 overlay string, proceed with the next overlay string
4262 or whatever comes and return a character from there. */
4263 if (it->current.overlay_string_index >= 0
4264 && !display_ellipsis_p)
4265 {
4266 next_overlay_string (it);
4267 /* Don't check for overlay strings when we just
4268 finished processing them. */
4269 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4270 }
4271 else
4272 {
4273 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4274 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4275 }
4276 }
4277 }
4278 }
4279 else
4280 {
4281 ptrdiff_t newpos, next_stop, start_charpos, tem;
4282 Lisp_Object pos, overlay;
4283
4284 /* First of all, is there invisible text at this position? */
4285 tem = start_charpos = IT_CHARPOS (*it);
4286 pos = make_number (tem);
4287 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4288 &overlay);
4289 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4290
4291 /* If we are on invisible text, skip over it. */
4292 if (invis != 0 && start_charpos < it->end_charpos)
4293 {
4294 /* Record whether we have to display an ellipsis for the
4295 invisible text. */
4296 bool display_ellipsis_p = invis == 2;
4297
4298 handled = HANDLED_RECOMPUTE_PROPS;
4299
4300 /* Loop skipping over invisible text. The loop is left at
4301 ZV or with IT on the first char being visible again. */
4302 do
4303 {
4304 /* Try to skip some invisible text. Return value is the
4305 position reached which can be equal to where we start
4306 if there is nothing invisible there. This skips both
4307 over invisible text properties and overlays with
4308 invisible property. */
4309 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4310
4311 /* If we skipped nothing at all we weren't at invisible
4312 text in the first place. If everything to the end of
4313 the buffer was skipped, end the loop. */
4314 if (newpos == tem || newpos >= ZV)
4315 invis = 0;
4316 else
4317 {
4318 /* We skipped some characters but not necessarily
4319 all there are. Check if we ended up on visible
4320 text. Fget_char_property returns the property of
4321 the char before the given position, i.e. if we
4322 get invis = 0, this means that the char at
4323 newpos is visible. */
4324 pos = make_number (newpos);
4325 prop = Fget_char_property (pos, Qinvisible, it->window);
4326 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4327 }
4328
4329 /* If we ended up on invisible text, proceed to
4330 skip starting with next_stop. */
4331 if (invis != 0)
4332 tem = next_stop;
4333
4334 /* If there are adjacent invisible texts, don't lose the
4335 second one's ellipsis. */
4336 if (invis == 2)
4337 display_ellipsis_p = true;
4338 }
4339 while (invis != 0);
4340
4341 /* The position newpos is now either ZV or on visible text. */
4342 if (it->bidi_p)
4343 {
4344 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4345 bool on_newline
4346 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4347 bool after_newline
4348 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4349
4350 /* If the invisible text ends on a newline or on a
4351 character after a newline, we can avoid the costly,
4352 character by character, bidi iteration to NEWPOS, and
4353 instead simply reseat the iterator there. That's
4354 because all bidi reordering information is tossed at
4355 the newline. This is a big win for modes that hide
4356 complete lines, like Outline, Org, etc. */
4357 if (on_newline || after_newline)
4358 {
4359 struct text_pos tpos;
4360 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4361
4362 SET_TEXT_POS (tpos, newpos, bpos);
4363 reseat_1 (it, tpos, false);
4364 /* If we reseat on a newline/ZV, we need to prep the
4365 bidi iterator for advancing to the next character
4366 after the newline/EOB, keeping the current paragraph
4367 direction (so that PRODUCE_GLYPHS does TRT wrt
4368 prepending/appending glyphs to a glyph row). */
4369 if (on_newline)
4370 {
4371 it->bidi_it.first_elt = false;
4372 it->bidi_it.paragraph_dir = pdir;
4373 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4374 it->bidi_it.nchars = 1;
4375 it->bidi_it.ch_len = 1;
4376 }
4377 }
4378 else /* Must use the slow method. */
4379 {
4380 /* With bidi iteration, the region of invisible text
4381 could start and/or end in the middle of a
4382 non-base embedding level. Therefore, we need to
4383 skip invisible text using the bidi iterator,
4384 starting at IT's current position, until we find
4385 ourselves outside of the invisible text.
4386 Skipping invisible text _after_ bidi iteration
4387 avoids affecting the visual order of the
4388 displayed text when invisible properties are
4389 added or removed. */
4390 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4391 {
4392 /* If we were `reseat'ed to a new paragraph,
4393 determine the paragraph base direction. We
4394 need to do it now because
4395 next_element_from_buffer may not have a
4396 chance to do it, if we are going to skip any
4397 text at the beginning, which resets the
4398 FIRST_ELT flag. */
4399 bidi_paragraph_init (it->paragraph_embedding,
4400 &it->bidi_it, true);
4401 }
4402 do
4403 {
4404 bidi_move_to_visually_next (&it->bidi_it);
4405 }
4406 while (it->stop_charpos <= it->bidi_it.charpos
4407 && it->bidi_it.charpos < newpos);
4408 IT_CHARPOS (*it) = it->bidi_it.charpos;
4409 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4410 /* If we overstepped NEWPOS, record its position in
4411 the iterator, so that we skip invisible text if
4412 later the bidi iteration lands us in the
4413 invisible region again. */
4414 if (IT_CHARPOS (*it) >= newpos)
4415 it->prev_stop = newpos;
4416 }
4417 }
4418 else
4419 {
4420 IT_CHARPOS (*it) = newpos;
4421 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4422 }
4423
4424 if (display_ellipsis_p)
4425 {
4426 /* Make sure that the glyphs of the ellipsis will get
4427 correct `charpos' values. If we would not update
4428 it->position here, the glyphs would belong to the
4429 last visible character _before_ the invisible
4430 text, which confuses `set_cursor_from_row'.
4431
4432 We use the last invisible position instead of the
4433 first because this way the cursor is always drawn on
4434 the first "." of the ellipsis, whenever PT is inside
4435 the invisible text. Otherwise the cursor would be
4436 placed _after_ the ellipsis when the point is after the
4437 first invisible character. */
4438 if (!STRINGP (it->object))
4439 {
4440 it->position.charpos = newpos - 1;
4441 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4442 }
4443 }
4444
4445 /* If there are before-strings at the start of invisible
4446 text, and the text is invisible because of a text
4447 property, arrange to show before-strings because 20.x did
4448 it that way. (If the text is invisible because of an
4449 overlay property instead of a text property, this is
4450 already handled in the overlay code.) */
4451 if (NILP (overlay)
4452 && get_overlay_strings (it, it->stop_charpos))
4453 {
4454 handled = HANDLED_RECOMPUTE_PROPS;
4455 if (it->sp > 0)
4456 {
4457 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4458 /* The call to get_overlay_strings above recomputes
4459 it->stop_charpos, but it only considers changes
4460 in properties and overlays beyond iterator's
4461 current position. This causes us to miss changes
4462 that happen exactly where the invisible property
4463 ended. So we play it safe here and force the
4464 iterator to check for potential stop positions
4465 immediately after the invisible text. Note that
4466 if get_overlay_strings returns true, it
4467 normally also pushed the iterator stack, so we
4468 need to update the stop position in the slot
4469 below the current one. */
4470 it->stack[it->sp - 1].stop_charpos
4471 = CHARPOS (it->stack[it->sp - 1].current.pos);
4472 }
4473 }
4474 else if (display_ellipsis_p)
4475 {
4476 it->ellipsis_p = true;
4477 /* Let the ellipsis display before
4478 considering any properties of the following char.
4479 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4480 handled = HANDLED_RETURN;
4481 }
4482 }
4483 }
4484
4485 return handled;
4486 }
4487
4488
4489 /* Make iterator IT return `...' next.
4490 Replaces LEN characters from buffer. */
4491
4492 static void
4493 setup_for_ellipsis (struct it *it, int len)
4494 {
4495 /* Use the display table definition for `...'. Invalid glyphs
4496 will be handled by the method returning elements from dpvec. */
4497 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4498 {
4499 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4500 it->dpvec = v->contents;
4501 it->dpend = v->contents + v->header.size;
4502 }
4503 else
4504 {
4505 /* Default `...'. */
4506 it->dpvec = default_invis_vector;
4507 it->dpend = default_invis_vector + 3;
4508 }
4509
4510 it->dpvec_char_len = len;
4511 it->current.dpvec_index = 0;
4512 it->dpvec_face_id = -1;
4513
4514 /* Remember the current face id in case glyphs specify faces.
4515 IT's face is restored in set_iterator_to_next.
4516 saved_face_id was set to preceding char's face in handle_stop. */
4517 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4518 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4519
4520 /* If the ellipsis represents buffer text, it means we advanced in
4521 the buffer, so we should no longer ignore overlay strings. */
4522 if (it->method == GET_FROM_BUFFER)
4523 it->ignore_overlay_strings_at_pos_p = false;
4524
4525 it->method = GET_FROM_DISPLAY_VECTOR;
4526 it->ellipsis_p = true;
4527 }
4528
4529
4530 \f
4531 /***********************************************************************
4532 'display' property
4533 ***********************************************************************/
4534
4535 /* Set up iterator IT from `display' property at its current position.
4536 Called from handle_stop.
4537 We return HANDLED_RETURN if some part of the display property
4538 overrides the display of the buffer text itself.
4539 Otherwise we return HANDLED_NORMALLY. */
4540
4541 static enum prop_handled
4542 handle_display_prop (struct it *it)
4543 {
4544 Lisp_Object propval, object, overlay;
4545 struct text_pos *position;
4546 ptrdiff_t bufpos;
4547 /* Nonzero if some property replaces the display of the text itself. */
4548 int display_replaced = 0;
4549
4550 if (STRINGP (it->string))
4551 {
4552 object = it->string;
4553 position = &it->current.string_pos;
4554 bufpos = CHARPOS (it->current.pos);
4555 }
4556 else
4557 {
4558 XSETWINDOW (object, it->w);
4559 position = &it->current.pos;
4560 bufpos = CHARPOS (*position);
4561 }
4562
4563 /* Reset those iterator values set from display property values. */
4564 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4565 it->space_width = Qnil;
4566 it->font_height = Qnil;
4567 it->voffset = 0;
4568
4569 /* We don't support recursive `display' properties, i.e. string
4570 values that have a string `display' property, that have a string
4571 `display' property etc. */
4572 if (!it->string_from_display_prop_p)
4573 it->area = TEXT_AREA;
4574
4575 propval = get_char_property_and_overlay (make_number (position->charpos),
4576 Qdisplay, object, &overlay);
4577 if (NILP (propval))
4578 return HANDLED_NORMALLY;
4579 /* Now OVERLAY is the overlay that gave us this property, or nil
4580 if it was a text property. */
4581
4582 if (!STRINGP (it->string))
4583 object = it->w->contents;
4584
4585 display_replaced = handle_display_spec (it, propval, object, overlay,
4586 position, bufpos,
4587 FRAME_WINDOW_P (it->f));
4588 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4589 }
4590
4591 /* Subroutine of handle_display_prop. Returns non-zero if the display
4592 specification in SPEC is a replacing specification, i.e. it would
4593 replace the text covered by `display' property with something else,
4594 such as an image or a display string. If SPEC includes any kind or
4595 `(space ...) specification, the value is 2; this is used by
4596 compute_display_string_pos, which see.
4597
4598 See handle_single_display_spec for documentation of arguments.
4599 FRAME_WINDOW_P is true if the window being redisplayed is on a
4600 GUI frame; this argument is used only if IT is NULL, see below.
4601
4602 IT can be NULL, if this is called by the bidi reordering code
4603 through compute_display_string_pos, which see. In that case, this
4604 function only examines SPEC, but does not otherwise "handle" it, in
4605 the sense that it doesn't set up members of IT from the display
4606 spec. */
4607 static int
4608 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4609 Lisp_Object overlay, struct text_pos *position,
4610 ptrdiff_t bufpos, bool frame_window_p)
4611 {
4612 int replacing = 0;
4613
4614 if (CONSP (spec)
4615 /* Simple specifications. */
4616 && !EQ (XCAR (spec), Qimage)
4617 && !EQ (XCAR (spec), Qspace)
4618 && !EQ (XCAR (spec), Qwhen)
4619 && !EQ (XCAR (spec), Qslice)
4620 && !EQ (XCAR (spec), Qspace_width)
4621 && !EQ (XCAR (spec), Qheight)
4622 && !EQ (XCAR (spec), Qraise)
4623 /* Marginal area specifications. */
4624 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4625 && !EQ (XCAR (spec), Qleft_fringe)
4626 && !EQ (XCAR (spec), Qright_fringe)
4627 && !NILP (XCAR (spec)))
4628 {
4629 for (; CONSP (spec); spec = XCDR (spec))
4630 {
4631 int rv = handle_single_display_spec (it, XCAR (spec), object,
4632 overlay, position, bufpos,
4633 replacing, frame_window_p);
4634 if (rv != 0)
4635 {
4636 replacing = rv;
4637 /* If some text in a string is replaced, `position' no
4638 longer points to the position of `object'. */
4639 if (!it || STRINGP (object))
4640 break;
4641 }
4642 }
4643 }
4644 else if (VECTORP (spec))
4645 {
4646 ptrdiff_t i;
4647 for (i = 0; i < ASIZE (spec); ++i)
4648 {
4649 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4650 overlay, position, bufpos,
4651 replacing, frame_window_p);
4652 if (rv != 0)
4653 {
4654 replacing = rv;
4655 /* If some text in a string is replaced, `position' no
4656 longer points to the position of `object'. */
4657 if (!it || STRINGP (object))
4658 break;
4659 }
4660 }
4661 }
4662 else
4663 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4664 bufpos, 0, frame_window_p);
4665 return replacing;
4666 }
4667
4668 /* Value is the position of the end of the `display' property starting
4669 at START_POS in OBJECT. */
4670
4671 static struct text_pos
4672 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4673 {
4674 Lisp_Object end;
4675 struct text_pos end_pos;
4676
4677 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4678 Qdisplay, object, Qnil);
4679 CHARPOS (end_pos) = XFASTINT (end);
4680 if (STRINGP (object))
4681 compute_string_pos (&end_pos, start_pos, it->string);
4682 else
4683 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4684
4685 return end_pos;
4686 }
4687
4688
4689 /* Set up IT from a single `display' property specification SPEC. OBJECT
4690 is the object in which the `display' property was found. *POSITION
4691 is the position in OBJECT at which the `display' property was found.
4692 BUFPOS is the buffer position of OBJECT (different from POSITION if
4693 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4694 previously saw a display specification which already replaced text
4695 display with something else, for example an image; we ignore such
4696 properties after the first one has been processed.
4697
4698 OVERLAY is the overlay this `display' property came from,
4699 or nil if it was a text property.
4700
4701 If SPEC is a `space' or `image' specification, and in some other
4702 cases too, set *POSITION to the position where the `display'
4703 property ends.
4704
4705 If IT is NULL, only examine the property specification in SPEC, but
4706 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4707 is intended to be displayed in a window on a GUI frame.
4708
4709 Value is non-zero if something was found which replaces the display
4710 of buffer or string text. */
4711
4712 static int
4713 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4714 Lisp_Object overlay, struct text_pos *position,
4715 ptrdiff_t bufpos, int display_replaced,
4716 bool frame_window_p)
4717 {
4718 Lisp_Object form;
4719 Lisp_Object location, value;
4720 struct text_pos start_pos = *position;
4721
4722 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4723 If the result is non-nil, use VALUE instead of SPEC. */
4724 form = Qt;
4725 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4726 {
4727 spec = XCDR (spec);
4728 if (!CONSP (spec))
4729 return 0;
4730 form = XCAR (spec);
4731 spec = XCDR (spec);
4732 }
4733
4734 if (!NILP (form) && !EQ (form, Qt))
4735 {
4736 ptrdiff_t count = SPECPDL_INDEX ();
4737 struct gcpro gcpro1;
4738
4739 /* Bind `object' to the object having the `display' property, a
4740 buffer or string. Bind `position' to the position in the
4741 object where the property was found, and `buffer-position'
4742 to the current position in the buffer. */
4743
4744 if (NILP (object))
4745 XSETBUFFER (object, current_buffer);
4746 specbind (Qobject, object);
4747 specbind (Qposition, make_number (CHARPOS (*position)));
4748 specbind (Qbuffer_position, make_number (bufpos));
4749 GCPRO1 (form);
4750 form = safe_eval (form);
4751 UNGCPRO;
4752 unbind_to (count, Qnil);
4753 }
4754
4755 if (NILP (form))
4756 return 0;
4757
4758 /* Handle `(height HEIGHT)' specifications. */
4759 if (CONSP (spec)
4760 && EQ (XCAR (spec), Qheight)
4761 && CONSP (XCDR (spec)))
4762 {
4763 if (it)
4764 {
4765 if (!FRAME_WINDOW_P (it->f))
4766 return 0;
4767
4768 it->font_height = XCAR (XCDR (spec));
4769 if (!NILP (it->font_height))
4770 {
4771 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4772 int new_height = -1;
4773
4774 if (CONSP (it->font_height)
4775 && (EQ (XCAR (it->font_height), Qplus)
4776 || EQ (XCAR (it->font_height), Qminus))
4777 && CONSP (XCDR (it->font_height))
4778 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4779 {
4780 /* `(+ N)' or `(- N)' where N is an integer. */
4781 int steps = XINT (XCAR (XCDR (it->font_height)));
4782 if (EQ (XCAR (it->font_height), Qplus))
4783 steps = - steps;
4784 it->face_id = smaller_face (it->f, it->face_id, steps);
4785 }
4786 else if (FUNCTIONP (it->font_height))
4787 {
4788 /* Call function with current height as argument.
4789 Value is the new height. */
4790 Lisp_Object height;
4791 height = safe_call1 (it->font_height,
4792 face->lface[LFACE_HEIGHT_INDEX]);
4793 if (NUMBERP (height))
4794 new_height = XFLOATINT (height);
4795 }
4796 else if (NUMBERP (it->font_height))
4797 {
4798 /* Value is a multiple of the canonical char height. */
4799 struct face *f;
4800
4801 f = FACE_FROM_ID (it->f,
4802 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4803 new_height = (XFLOATINT (it->font_height)
4804 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4805 }
4806 else
4807 {
4808 /* Evaluate IT->font_height with `height' bound to the
4809 current specified height to get the new height. */
4810 ptrdiff_t count = SPECPDL_INDEX ();
4811
4812 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4813 value = safe_eval (it->font_height);
4814 unbind_to (count, Qnil);
4815
4816 if (NUMBERP (value))
4817 new_height = XFLOATINT (value);
4818 }
4819
4820 if (new_height > 0)
4821 it->face_id = face_with_height (it->f, it->face_id, new_height);
4822 }
4823 }
4824
4825 return 0;
4826 }
4827
4828 /* Handle `(space-width WIDTH)'. */
4829 if (CONSP (spec)
4830 && EQ (XCAR (spec), Qspace_width)
4831 && CONSP (XCDR (spec)))
4832 {
4833 if (it)
4834 {
4835 if (!FRAME_WINDOW_P (it->f))
4836 return 0;
4837
4838 value = XCAR (XCDR (spec));
4839 if (NUMBERP (value) && XFLOATINT (value) > 0)
4840 it->space_width = value;
4841 }
4842
4843 return 0;
4844 }
4845
4846 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4847 if (CONSP (spec)
4848 && EQ (XCAR (spec), Qslice))
4849 {
4850 Lisp_Object tem;
4851
4852 if (it)
4853 {
4854 if (!FRAME_WINDOW_P (it->f))
4855 return 0;
4856
4857 if (tem = XCDR (spec), CONSP (tem))
4858 {
4859 it->slice.x = XCAR (tem);
4860 if (tem = XCDR (tem), CONSP (tem))
4861 {
4862 it->slice.y = XCAR (tem);
4863 if (tem = XCDR (tem), CONSP (tem))
4864 {
4865 it->slice.width = XCAR (tem);
4866 if (tem = XCDR (tem), CONSP (tem))
4867 it->slice.height = XCAR (tem);
4868 }
4869 }
4870 }
4871 }
4872
4873 return 0;
4874 }
4875
4876 /* Handle `(raise FACTOR)'. */
4877 if (CONSP (spec)
4878 && EQ (XCAR (spec), Qraise)
4879 && CONSP (XCDR (spec)))
4880 {
4881 if (it)
4882 {
4883 if (!FRAME_WINDOW_P (it->f))
4884 return 0;
4885
4886 #ifdef HAVE_WINDOW_SYSTEM
4887 value = XCAR (XCDR (spec));
4888 if (NUMBERP (value))
4889 {
4890 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4891 it->voffset = - (XFLOATINT (value)
4892 * (FONT_HEIGHT (face->font)));
4893 }
4894 #endif /* HAVE_WINDOW_SYSTEM */
4895 }
4896
4897 return 0;
4898 }
4899
4900 /* Don't handle the other kinds of display specifications
4901 inside a string that we got from a `display' property. */
4902 if (it && it->string_from_display_prop_p)
4903 return 0;
4904
4905 /* Characters having this form of property are not displayed, so
4906 we have to find the end of the property. */
4907 if (it)
4908 {
4909 start_pos = *position;
4910 *position = display_prop_end (it, object, start_pos);
4911 /* If the display property comes from an overlay, don't consider
4912 any potential stop_charpos values before the end of that
4913 overlay. Since display_prop_end will happily find another
4914 'display' property coming from some other overlay or text
4915 property on buffer positions before this overlay's end, we
4916 need to ignore them, or else we risk displaying this
4917 overlay's display string/image twice. */
4918 if (!NILP (overlay))
4919 {
4920 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4921
4922 if (ovendpos > CHARPOS (*position))
4923 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4924 }
4925 }
4926 value = Qnil;
4927
4928 /* Stop the scan at that end position--we assume that all
4929 text properties change there. */
4930 if (it)
4931 it->stop_charpos = position->charpos;
4932
4933 /* Handle `(left-fringe BITMAP [FACE])'
4934 and `(right-fringe BITMAP [FACE])'. */
4935 if (CONSP (spec)
4936 && (EQ (XCAR (spec), Qleft_fringe)
4937 || EQ (XCAR (spec), Qright_fringe))
4938 && CONSP (XCDR (spec)))
4939 {
4940 int fringe_bitmap;
4941
4942 if (it)
4943 {
4944 if (!FRAME_WINDOW_P (it->f))
4945 /* If we return here, POSITION has been advanced
4946 across the text with this property. */
4947 {
4948 /* Synchronize the bidi iterator with POSITION. This is
4949 needed because we are not going to push the iterator
4950 on behalf of this display property, so there will be
4951 no pop_it call to do this synchronization for us. */
4952 if (it->bidi_p)
4953 {
4954 it->position = *position;
4955 iterate_out_of_display_property (it);
4956 *position = it->position;
4957 }
4958 return 1;
4959 }
4960 }
4961 else if (!frame_window_p)
4962 return 1;
4963
4964 #ifdef HAVE_WINDOW_SYSTEM
4965 value = XCAR (XCDR (spec));
4966 if (!SYMBOLP (value)
4967 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4968 /* If we return here, POSITION has been advanced
4969 across the text with this property. */
4970 {
4971 if (it && it->bidi_p)
4972 {
4973 it->position = *position;
4974 iterate_out_of_display_property (it);
4975 *position = it->position;
4976 }
4977 return 1;
4978 }
4979
4980 if (it)
4981 {
4982 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4983
4984 if (CONSP (XCDR (XCDR (spec))))
4985 {
4986 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4987 int face_id2 = lookup_derived_face (it->f, face_name,
4988 FRINGE_FACE_ID, false);
4989 if (face_id2 >= 0)
4990 face_id = face_id2;
4991 }
4992
4993 /* Save current settings of IT so that we can restore them
4994 when we are finished with the glyph property value. */
4995 push_it (it, position);
4996
4997 it->area = TEXT_AREA;
4998 it->what = IT_IMAGE;
4999 it->image_id = -1; /* no image */
5000 it->position = start_pos;
5001 it->object = NILP (object) ? it->w->contents : object;
5002 it->method = GET_FROM_IMAGE;
5003 it->from_overlay = Qnil;
5004 it->face_id = face_id;
5005 it->from_disp_prop_p = true;
5006
5007 /* Say that we haven't consumed the characters with
5008 `display' property yet. The call to pop_it in
5009 set_iterator_to_next will clean this up. */
5010 *position = start_pos;
5011
5012 if (EQ (XCAR (spec), Qleft_fringe))
5013 {
5014 it->left_user_fringe_bitmap = fringe_bitmap;
5015 it->left_user_fringe_face_id = face_id;
5016 }
5017 else
5018 {
5019 it->right_user_fringe_bitmap = fringe_bitmap;
5020 it->right_user_fringe_face_id = face_id;
5021 }
5022 }
5023 #endif /* HAVE_WINDOW_SYSTEM */
5024 return 1;
5025 }
5026
5027 /* Prepare to handle `((margin left-margin) ...)',
5028 `((margin right-margin) ...)' and `((margin nil) ...)'
5029 prefixes for display specifications. */
5030 location = Qunbound;
5031 if (CONSP (spec) && CONSP (XCAR (spec)))
5032 {
5033 Lisp_Object tem;
5034
5035 value = XCDR (spec);
5036 if (CONSP (value))
5037 value = XCAR (value);
5038
5039 tem = XCAR (spec);
5040 if (EQ (XCAR (tem), Qmargin)
5041 && (tem = XCDR (tem),
5042 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5043 (NILP (tem)
5044 || EQ (tem, Qleft_margin)
5045 || EQ (tem, Qright_margin))))
5046 location = tem;
5047 }
5048
5049 if (EQ (location, Qunbound))
5050 {
5051 location = Qnil;
5052 value = spec;
5053 }
5054
5055 /* After this point, VALUE is the property after any
5056 margin prefix has been stripped. It must be a string,
5057 an image specification, or `(space ...)'.
5058
5059 LOCATION specifies where to display: `left-margin',
5060 `right-margin' or nil. */
5061
5062 bool valid_p = (STRINGP (value)
5063 #ifdef HAVE_WINDOW_SYSTEM
5064 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5065 && valid_image_p (value))
5066 #endif /* not HAVE_WINDOW_SYSTEM */
5067 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5068
5069 if (valid_p && display_replaced == 0)
5070 {
5071 int retval = 1;
5072
5073 if (!it)
5074 {
5075 /* Callers need to know whether the display spec is any kind
5076 of `(space ...)' spec that is about to affect text-area
5077 display. */
5078 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5079 retval = 2;
5080 return retval;
5081 }
5082
5083 /* Save current settings of IT so that we can restore them
5084 when we are finished with the glyph property value. */
5085 push_it (it, position);
5086 it->from_overlay = overlay;
5087 it->from_disp_prop_p = true;
5088
5089 if (NILP (location))
5090 it->area = TEXT_AREA;
5091 else if (EQ (location, Qleft_margin))
5092 it->area = LEFT_MARGIN_AREA;
5093 else
5094 it->area = RIGHT_MARGIN_AREA;
5095
5096 if (STRINGP (value))
5097 {
5098 it->string = value;
5099 it->multibyte_p = STRING_MULTIBYTE (it->string);
5100 it->current.overlay_string_index = -1;
5101 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5102 it->end_charpos = it->string_nchars = SCHARS (it->string);
5103 it->method = GET_FROM_STRING;
5104 it->stop_charpos = 0;
5105 it->prev_stop = 0;
5106 it->base_level_stop = 0;
5107 it->string_from_display_prop_p = true;
5108 /* Say that we haven't consumed the characters with
5109 `display' property yet. The call to pop_it in
5110 set_iterator_to_next will clean this up. */
5111 if (BUFFERP (object))
5112 *position = start_pos;
5113
5114 /* Force paragraph direction to be that of the parent
5115 object. If the parent object's paragraph direction is
5116 not yet determined, default to L2R. */
5117 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5118 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5119 else
5120 it->paragraph_embedding = L2R;
5121
5122 /* Set up the bidi iterator for this display string. */
5123 if (it->bidi_p)
5124 {
5125 it->bidi_it.string.lstring = it->string;
5126 it->bidi_it.string.s = NULL;
5127 it->bidi_it.string.schars = it->end_charpos;
5128 it->bidi_it.string.bufpos = bufpos;
5129 it->bidi_it.string.from_disp_str = true;
5130 it->bidi_it.string.unibyte = !it->multibyte_p;
5131 it->bidi_it.w = it->w;
5132 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5133 }
5134 }
5135 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5136 {
5137 it->method = GET_FROM_STRETCH;
5138 it->object = value;
5139 *position = it->position = start_pos;
5140 retval = 1 + (it->area == TEXT_AREA);
5141 }
5142 #ifdef HAVE_WINDOW_SYSTEM
5143 else
5144 {
5145 it->what = IT_IMAGE;
5146 it->image_id = lookup_image (it->f, value);
5147 it->position = start_pos;
5148 it->object = NILP (object) ? it->w->contents : object;
5149 it->method = GET_FROM_IMAGE;
5150
5151 /* Say that we haven't consumed the characters with
5152 `display' property yet. The call to pop_it in
5153 set_iterator_to_next will clean this up. */
5154 *position = start_pos;
5155 }
5156 #endif /* HAVE_WINDOW_SYSTEM */
5157
5158 return retval;
5159 }
5160
5161 /* Invalid property or property not supported. Restore
5162 POSITION to what it was before. */
5163 *position = start_pos;
5164 return 0;
5165 }
5166
5167 /* Check if PROP is a display property value whose text should be
5168 treated as intangible. OVERLAY is the overlay from which PROP
5169 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5170 specify the buffer position covered by PROP. */
5171
5172 bool
5173 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5174 ptrdiff_t charpos, ptrdiff_t bytepos)
5175 {
5176 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5177 struct text_pos position;
5178
5179 SET_TEXT_POS (position, charpos, bytepos);
5180 return (handle_display_spec (NULL, prop, Qnil, overlay,
5181 &position, charpos, frame_window_p)
5182 != 0);
5183 }
5184
5185
5186 /* Return true if PROP is a display sub-property value containing STRING.
5187
5188 Implementation note: this and the following function are really
5189 special cases of handle_display_spec and
5190 handle_single_display_spec, and should ideally use the same code.
5191 Until they do, these two pairs must be consistent and must be
5192 modified in sync. */
5193
5194 static bool
5195 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5196 {
5197 if (EQ (string, prop))
5198 return true;
5199
5200 /* Skip over `when FORM'. */
5201 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5202 {
5203 prop = XCDR (prop);
5204 if (!CONSP (prop))
5205 return false;
5206 /* Actually, the condition following `when' should be eval'ed,
5207 like handle_single_display_spec does, and we should return
5208 false if it evaluates to nil. However, this function is
5209 called only when the buffer was already displayed and some
5210 glyph in the glyph matrix was found to come from a display
5211 string. Therefore, the condition was already evaluated, and
5212 the result was non-nil, otherwise the display string wouldn't
5213 have been displayed and we would have never been called for
5214 this property. Thus, we can skip the evaluation and assume
5215 its result is non-nil. */
5216 prop = XCDR (prop);
5217 }
5218
5219 if (CONSP (prop))
5220 /* Skip over `margin LOCATION'. */
5221 if (EQ (XCAR (prop), Qmargin))
5222 {
5223 prop = XCDR (prop);
5224 if (!CONSP (prop))
5225 return false;
5226
5227 prop = XCDR (prop);
5228 if (!CONSP (prop))
5229 return false;
5230 }
5231
5232 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5233 }
5234
5235
5236 /* Return true if STRING appears in the `display' property PROP. */
5237
5238 static bool
5239 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5240 {
5241 if (CONSP (prop)
5242 && !EQ (XCAR (prop), Qwhen)
5243 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5244 {
5245 /* A list of sub-properties. */
5246 while (CONSP (prop))
5247 {
5248 if (single_display_spec_string_p (XCAR (prop), string))
5249 return true;
5250 prop = XCDR (prop);
5251 }
5252 }
5253 else if (VECTORP (prop))
5254 {
5255 /* A vector of sub-properties. */
5256 ptrdiff_t i;
5257 for (i = 0; i < ASIZE (prop); ++i)
5258 if (single_display_spec_string_p (AREF (prop, i), string))
5259 return true;
5260 }
5261 else
5262 return single_display_spec_string_p (prop, string);
5263
5264 return false;
5265 }
5266
5267 /* Look for STRING in overlays and text properties in the current
5268 buffer, between character positions FROM and TO (excluding TO).
5269 BACK_P means look back (in this case, TO is supposed to be
5270 less than FROM).
5271 Value is the first character position where STRING was found, or
5272 zero if it wasn't found before hitting TO.
5273
5274 This function may only use code that doesn't eval because it is
5275 called asynchronously from note_mouse_highlight. */
5276
5277 static ptrdiff_t
5278 string_buffer_position_lim (Lisp_Object string,
5279 ptrdiff_t from, ptrdiff_t to, bool back_p)
5280 {
5281 Lisp_Object limit, prop, pos;
5282 bool found = false;
5283
5284 pos = make_number (max (from, BEGV));
5285
5286 if (!back_p) /* looking forward */
5287 {
5288 limit = make_number (min (to, ZV));
5289 while (!found && !EQ (pos, limit))
5290 {
5291 prop = Fget_char_property (pos, Qdisplay, Qnil);
5292 if (!NILP (prop) && display_prop_string_p (prop, string))
5293 found = true;
5294 else
5295 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5296 limit);
5297 }
5298 }
5299 else /* looking back */
5300 {
5301 limit = make_number (max (to, BEGV));
5302 while (!found && !EQ (pos, limit))
5303 {
5304 prop = Fget_char_property (pos, Qdisplay, Qnil);
5305 if (!NILP (prop) && display_prop_string_p (prop, string))
5306 found = true;
5307 else
5308 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5309 limit);
5310 }
5311 }
5312
5313 return found ? XINT (pos) : 0;
5314 }
5315
5316 /* Determine which buffer position in current buffer STRING comes from.
5317 AROUND_CHARPOS is an approximate position where it could come from.
5318 Value is the buffer position or 0 if it couldn't be determined.
5319
5320 This function is necessary because we don't record buffer positions
5321 in glyphs generated from strings (to keep struct glyph small).
5322 This function may only use code that doesn't eval because it is
5323 called asynchronously from note_mouse_highlight. */
5324
5325 static ptrdiff_t
5326 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5327 {
5328 const int MAX_DISTANCE = 1000;
5329 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5330 around_charpos + MAX_DISTANCE,
5331 false);
5332
5333 if (!found)
5334 found = string_buffer_position_lim (string, around_charpos,
5335 around_charpos - MAX_DISTANCE, true);
5336 return found;
5337 }
5338
5339
5340 \f
5341 /***********************************************************************
5342 `composition' property
5343 ***********************************************************************/
5344
5345 /* Set up iterator IT from `composition' property at its current
5346 position. Called from handle_stop. */
5347
5348 static enum prop_handled
5349 handle_composition_prop (struct it *it)
5350 {
5351 Lisp_Object prop, string;
5352 ptrdiff_t pos, pos_byte, start, end;
5353
5354 if (STRINGP (it->string))
5355 {
5356 unsigned char *s;
5357
5358 pos = IT_STRING_CHARPOS (*it);
5359 pos_byte = IT_STRING_BYTEPOS (*it);
5360 string = it->string;
5361 s = SDATA (string) + pos_byte;
5362 it->c = STRING_CHAR (s);
5363 }
5364 else
5365 {
5366 pos = IT_CHARPOS (*it);
5367 pos_byte = IT_BYTEPOS (*it);
5368 string = Qnil;
5369 it->c = FETCH_CHAR (pos_byte);
5370 }
5371
5372 /* If there's a valid composition and point is not inside of the
5373 composition (in the case that the composition is from the current
5374 buffer), draw a glyph composed from the composition components. */
5375 if (find_composition (pos, -1, &start, &end, &prop, string)
5376 && composition_valid_p (start, end, prop)
5377 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5378 {
5379 if (start < pos)
5380 /* As we can't handle this situation (perhaps font-lock added
5381 a new composition), we just return here hoping that next
5382 redisplay will detect this composition much earlier. */
5383 return HANDLED_NORMALLY;
5384 if (start != pos)
5385 {
5386 if (STRINGP (it->string))
5387 pos_byte = string_char_to_byte (it->string, start);
5388 else
5389 pos_byte = CHAR_TO_BYTE (start);
5390 }
5391 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5392 prop, string);
5393
5394 if (it->cmp_it.id >= 0)
5395 {
5396 it->cmp_it.ch = -1;
5397 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5398 it->cmp_it.nglyphs = -1;
5399 }
5400 }
5401
5402 return HANDLED_NORMALLY;
5403 }
5404
5405
5406 \f
5407 /***********************************************************************
5408 Overlay strings
5409 ***********************************************************************/
5410
5411 /* The following structure is used to record overlay strings for
5412 later sorting in load_overlay_strings. */
5413
5414 struct overlay_entry
5415 {
5416 Lisp_Object overlay;
5417 Lisp_Object string;
5418 EMACS_INT priority;
5419 bool after_string_p;
5420 };
5421
5422
5423 /* Set up iterator IT from overlay strings at its current position.
5424 Called from handle_stop. */
5425
5426 static enum prop_handled
5427 handle_overlay_change (struct it *it)
5428 {
5429 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5430 return HANDLED_RECOMPUTE_PROPS;
5431 else
5432 return HANDLED_NORMALLY;
5433 }
5434
5435
5436 /* Set up the next overlay string for delivery by IT, if there is an
5437 overlay string to deliver. Called by set_iterator_to_next when the
5438 end of the current overlay string is reached. If there are more
5439 overlay strings to display, IT->string and
5440 IT->current.overlay_string_index are set appropriately here.
5441 Otherwise IT->string is set to nil. */
5442
5443 static void
5444 next_overlay_string (struct it *it)
5445 {
5446 ++it->current.overlay_string_index;
5447 if (it->current.overlay_string_index == it->n_overlay_strings)
5448 {
5449 /* No more overlay strings. Restore IT's settings to what
5450 they were before overlay strings were processed, and
5451 continue to deliver from current_buffer. */
5452
5453 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5454 pop_it (it);
5455 eassert (it->sp > 0
5456 || (NILP (it->string)
5457 && it->method == GET_FROM_BUFFER
5458 && it->stop_charpos >= BEGV
5459 && it->stop_charpos <= it->end_charpos));
5460 it->current.overlay_string_index = -1;
5461 it->n_overlay_strings = 0;
5462 /* If there's an empty display string on the stack, pop the
5463 stack, to resync the bidi iterator with IT's position. Such
5464 empty strings are pushed onto the stack in
5465 get_overlay_strings_1. */
5466 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5467 pop_it (it);
5468
5469 /* Since we've exhausted overlay strings at this buffer
5470 position, set the flag to ignore overlays until we move to
5471 another position. The flag is reset in
5472 next_element_from_buffer. */
5473 it->ignore_overlay_strings_at_pos_p = true;
5474
5475 /* If we're at the end of the buffer, record that we have
5476 processed the overlay strings there already, so that
5477 next_element_from_buffer doesn't try it again. */
5478 if (NILP (it->string)
5479 && IT_CHARPOS (*it) >= it->end_charpos
5480 && it->overlay_strings_charpos >= it->end_charpos)
5481 it->overlay_strings_at_end_processed_p = true;
5482 /* Note: we reset overlay_strings_charpos only here, to make
5483 sure the just-processed overlays were indeed at EOB.
5484 Otherwise, overlays on text with invisible text property,
5485 which are processed with IT's position past the invisible
5486 text, might fool us into thinking the overlays at EOB were
5487 already processed (linum-mode can cause this, for
5488 example). */
5489 it->overlay_strings_charpos = -1;
5490 }
5491 else
5492 {
5493 /* There are more overlay strings to process. If
5494 IT->current.overlay_string_index has advanced to a position
5495 where we must load IT->overlay_strings with more strings, do
5496 it. We must load at the IT->overlay_strings_charpos where
5497 IT->n_overlay_strings was originally computed; when invisible
5498 text is present, this might not be IT_CHARPOS (Bug#7016). */
5499 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5500
5501 if (it->current.overlay_string_index && i == 0)
5502 load_overlay_strings (it, it->overlay_strings_charpos);
5503
5504 /* Initialize IT to deliver display elements from the overlay
5505 string. */
5506 it->string = it->overlay_strings[i];
5507 it->multibyte_p = STRING_MULTIBYTE (it->string);
5508 SET_TEXT_POS (it->current.string_pos, 0, 0);
5509 it->method = GET_FROM_STRING;
5510 it->stop_charpos = 0;
5511 it->end_charpos = SCHARS (it->string);
5512 if (it->cmp_it.stop_pos >= 0)
5513 it->cmp_it.stop_pos = 0;
5514 it->prev_stop = 0;
5515 it->base_level_stop = 0;
5516
5517 /* Set up the bidi iterator for this overlay string. */
5518 if (it->bidi_p)
5519 {
5520 it->bidi_it.string.lstring = it->string;
5521 it->bidi_it.string.s = NULL;
5522 it->bidi_it.string.schars = SCHARS (it->string);
5523 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5524 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5525 it->bidi_it.string.unibyte = !it->multibyte_p;
5526 it->bidi_it.w = it->w;
5527 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5528 }
5529 }
5530
5531 CHECK_IT (it);
5532 }
5533
5534
5535 /* Compare two overlay_entry structures E1 and E2. Used as a
5536 comparison function for qsort in load_overlay_strings. Overlay
5537 strings for the same position are sorted so that
5538
5539 1. All after-strings come in front of before-strings, except
5540 when they come from the same overlay.
5541
5542 2. Within after-strings, strings are sorted so that overlay strings
5543 from overlays with higher priorities come first.
5544
5545 2. Within before-strings, strings are sorted so that overlay
5546 strings from overlays with higher priorities come last.
5547
5548 Value is analogous to strcmp. */
5549
5550
5551 static int
5552 compare_overlay_entries (const void *e1, const void *e2)
5553 {
5554 struct overlay_entry const *entry1 = e1;
5555 struct overlay_entry const *entry2 = e2;
5556 int result;
5557
5558 if (entry1->after_string_p != entry2->after_string_p)
5559 {
5560 /* Let after-strings appear in front of before-strings if
5561 they come from different overlays. */
5562 if (EQ (entry1->overlay, entry2->overlay))
5563 result = entry1->after_string_p ? 1 : -1;
5564 else
5565 result = entry1->after_string_p ? -1 : 1;
5566 }
5567 else if (entry1->priority != entry2->priority)
5568 {
5569 if (entry1->after_string_p)
5570 /* After-strings sorted in order of decreasing priority. */
5571 result = entry2->priority < entry1->priority ? -1 : 1;
5572 else
5573 /* Before-strings sorted in order of increasing priority. */
5574 result = entry1->priority < entry2->priority ? -1 : 1;
5575 }
5576 else
5577 result = 0;
5578
5579 return result;
5580 }
5581
5582
5583 /* Load the vector IT->overlay_strings with overlay strings from IT's
5584 current buffer position, or from CHARPOS if that is > 0. Set
5585 IT->n_overlays to the total number of overlay strings found.
5586
5587 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5588 a time. On entry into load_overlay_strings,
5589 IT->current.overlay_string_index gives the number of overlay
5590 strings that have already been loaded by previous calls to this
5591 function.
5592
5593 IT->add_overlay_start contains an additional overlay start
5594 position to consider for taking overlay strings from, if non-zero.
5595 This position comes into play when the overlay has an `invisible'
5596 property, and both before and after-strings. When we've skipped to
5597 the end of the overlay, because of its `invisible' property, we
5598 nevertheless want its before-string to appear.
5599 IT->add_overlay_start will contain the overlay start position
5600 in this case.
5601
5602 Overlay strings are sorted so that after-string strings come in
5603 front of before-string strings. Within before and after-strings,
5604 strings are sorted by overlay priority. See also function
5605 compare_overlay_entries. */
5606
5607 static void
5608 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5609 {
5610 Lisp_Object overlay, window, str, invisible;
5611 struct Lisp_Overlay *ov;
5612 ptrdiff_t start, end;
5613 ptrdiff_t n = 0, i, j;
5614 int invis;
5615 struct overlay_entry entriesbuf[20];
5616 ptrdiff_t size = ARRAYELTS (entriesbuf);
5617 struct overlay_entry *entries = entriesbuf;
5618 USE_SAFE_ALLOCA;
5619
5620 if (charpos <= 0)
5621 charpos = IT_CHARPOS (*it);
5622
5623 /* Append the overlay string STRING of overlay OVERLAY to vector
5624 `entries' which has size `size' and currently contains `n'
5625 elements. AFTER_P means STRING is an after-string of
5626 OVERLAY. */
5627 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5628 do \
5629 { \
5630 Lisp_Object priority; \
5631 \
5632 if (n == size) \
5633 { \
5634 struct overlay_entry *old = entries; \
5635 SAFE_NALLOCA (entries, 2, size); \
5636 memcpy (entries, old, size * sizeof *entries); \
5637 size *= 2; \
5638 } \
5639 \
5640 entries[n].string = (STRING); \
5641 entries[n].overlay = (OVERLAY); \
5642 priority = Foverlay_get ((OVERLAY), Qpriority); \
5643 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5644 entries[n].after_string_p = (AFTER_P); \
5645 ++n; \
5646 } \
5647 while (false)
5648
5649 /* Process overlay before the overlay center. */
5650 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5651 {
5652 XSETMISC (overlay, ov);
5653 eassert (OVERLAYP (overlay));
5654 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5655 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5656
5657 if (end < charpos)
5658 break;
5659
5660 /* Skip this overlay if it doesn't start or end at IT's current
5661 position. */
5662 if (end != charpos && start != charpos)
5663 continue;
5664
5665 /* Skip this overlay if it doesn't apply to IT->w. */
5666 window = Foverlay_get (overlay, Qwindow);
5667 if (WINDOWP (window) && XWINDOW (window) != it->w)
5668 continue;
5669
5670 /* If the text ``under'' the overlay is invisible, both before-
5671 and after-strings from this overlay are visible; start and
5672 end position are indistinguishable. */
5673 invisible = Foverlay_get (overlay, Qinvisible);
5674 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5675
5676 /* If overlay has a non-empty before-string, record it. */
5677 if ((start == charpos || (end == charpos && invis != 0))
5678 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5679 && SCHARS (str))
5680 RECORD_OVERLAY_STRING (overlay, str, false);
5681
5682 /* If overlay has a non-empty after-string, record it. */
5683 if ((end == charpos || (start == charpos && invis != 0))
5684 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5685 && SCHARS (str))
5686 RECORD_OVERLAY_STRING (overlay, str, true);
5687 }
5688
5689 /* Process overlays after the overlay center. */
5690 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5691 {
5692 XSETMISC (overlay, ov);
5693 eassert (OVERLAYP (overlay));
5694 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5695 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5696
5697 if (start > charpos)
5698 break;
5699
5700 /* Skip this overlay if it doesn't start or end at IT's current
5701 position. */
5702 if (end != charpos && start != charpos)
5703 continue;
5704
5705 /* Skip this overlay if it doesn't apply to IT->w. */
5706 window = Foverlay_get (overlay, Qwindow);
5707 if (WINDOWP (window) && XWINDOW (window) != it->w)
5708 continue;
5709
5710 /* If the text ``under'' the overlay is invisible, it has a zero
5711 dimension, and both before- and after-strings apply. */
5712 invisible = Foverlay_get (overlay, Qinvisible);
5713 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5714
5715 /* If overlay has a non-empty before-string, record it. */
5716 if ((start == charpos || (end == charpos && invis != 0))
5717 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5718 && SCHARS (str))
5719 RECORD_OVERLAY_STRING (overlay, str, false);
5720
5721 /* If overlay has a non-empty after-string, record it. */
5722 if ((end == charpos || (start == charpos && invis != 0))
5723 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5724 && SCHARS (str))
5725 RECORD_OVERLAY_STRING (overlay, str, true);
5726 }
5727
5728 #undef RECORD_OVERLAY_STRING
5729
5730 /* Sort entries. */
5731 if (n > 1)
5732 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5733
5734 /* Record number of overlay strings, and where we computed it. */
5735 it->n_overlay_strings = n;
5736 it->overlay_strings_charpos = charpos;
5737
5738 /* IT->current.overlay_string_index is the number of overlay strings
5739 that have already been consumed by IT. Copy some of the
5740 remaining overlay strings to IT->overlay_strings. */
5741 i = 0;
5742 j = it->current.overlay_string_index;
5743 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5744 {
5745 it->overlay_strings[i] = entries[j].string;
5746 it->string_overlays[i++] = entries[j++].overlay;
5747 }
5748
5749 CHECK_IT (it);
5750 SAFE_FREE ();
5751 }
5752
5753
5754 /* Get the first chunk of overlay strings at IT's current buffer
5755 position, or at CHARPOS if that is > 0. Value is true if at
5756 least one overlay string was found. */
5757
5758 static bool
5759 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5760 {
5761 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5762 process. This fills IT->overlay_strings with strings, and sets
5763 IT->n_overlay_strings to the total number of strings to process.
5764 IT->pos.overlay_string_index has to be set temporarily to zero
5765 because load_overlay_strings needs this; it must be set to -1
5766 when no overlay strings are found because a zero value would
5767 indicate a position in the first overlay string. */
5768 it->current.overlay_string_index = 0;
5769 load_overlay_strings (it, charpos);
5770
5771 /* If we found overlay strings, set up IT to deliver display
5772 elements from the first one. Otherwise set up IT to deliver
5773 from current_buffer. */
5774 if (it->n_overlay_strings)
5775 {
5776 /* Make sure we know settings in current_buffer, so that we can
5777 restore meaningful values when we're done with the overlay
5778 strings. */
5779 if (compute_stop_p)
5780 compute_stop_pos (it);
5781 eassert (it->face_id >= 0);
5782
5783 /* Save IT's settings. They are restored after all overlay
5784 strings have been processed. */
5785 eassert (!compute_stop_p || it->sp == 0);
5786
5787 /* When called from handle_stop, there might be an empty display
5788 string loaded. In that case, don't bother saving it. But
5789 don't use this optimization with the bidi iterator, since we
5790 need the corresponding pop_it call to resync the bidi
5791 iterator's position with IT's position, after we are done
5792 with the overlay strings. (The corresponding call to pop_it
5793 in case of an empty display string is in
5794 next_overlay_string.) */
5795 if (!(!it->bidi_p
5796 && STRINGP (it->string) && !SCHARS (it->string)))
5797 push_it (it, NULL);
5798
5799 /* Set up IT to deliver display elements from the first overlay
5800 string. */
5801 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5802 it->string = it->overlay_strings[0];
5803 it->from_overlay = Qnil;
5804 it->stop_charpos = 0;
5805 eassert (STRINGP (it->string));
5806 it->end_charpos = SCHARS (it->string);
5807 it->prev_stop = 0;
5808 it->base_level_stop = 0;
5809 it->multibyte_p = STRING_MULTIBYTE (it->string);
5810 it->method = GET_FROM_STRING;
5811 it->from_disp_prop_p = 0;
5812
5813 /* Force paragraph direction to be that of the parent
5814 buffer. */
5815 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5816 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5817 else
5818 it->paragraph_embedding = L2R;
5819
5820 /* Set up the bidi iterator for this overlay string. */
5821 if (it->bidi_p)
5822 {
5823 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5824
5825 it->bidi_it.string.lstring = it->string;
5826 it->bidi_it.string.s = NULL;
5827 it->bidi_it.string.schars = SCHARS (it->string);
5828 it->bidi_it.string.bufpos = pos;
5829 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5830 it->bidi_it.string.unibyte = !it->multibyte_p;
5831 it->bidi_it.w = it->w;
5832 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5833 }
5834 return true;
5835 }
5836
5837 it->current.overlay_string_index = -1;
5838 return false;
5839 }
5840
5841 static bool
5842 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5843 {
5844 it->string = Qnil;
5845 it->method = GET_FROM_BUFFER;
5846
5847 get_overlay_strings_1 (it, charpos, true);
5848
5849 CHECK_IT (it);
5850
5851 /* Value is true if we found at least one overlay string. */
5852 return STRINGP (it->string);
5853 }
5854
5855
5856 \f
5857 /***********************************************************************
5858 Saving and restoring state
5859 ***********************************************************************/
5860
5861 /* Save current settings of IT on IT->stack. Called, for example,
5862 before setting up IT for an overlay string, to be able to restore
5863 IT's settings to what they were after the overlay string has been
5864 processed. If POSITION is non-NULL, it is the position to save on
5865 the stack instead of IT->position. */
5866
5867 static void
5868 push_it (struct it *it, struct text_pos *position)
5869 {
5870 struct iterator_stack_entry *p;
5871
5872 eassert (it->sp < IT_STACK_SIZE);
5873 p = it->stack + it->sp;
5874
5875 p->stop_charpos = it->stop_charpos;
5876 p->prev_stop = it->prev_stop;
5877 p->base_level_stop = it->base_level_stop;
5878 p->cmp_it = it->cmp_it;
5879 eassert (it->face_id >= 0);
5880 p->face_id = it->face_id;
5881 p->string = it->string;
5882 p->method = it->method;
5883 p->from_overlay = it->from_overlay;
5884 switch (p->method)
5885 {
5886 case GET_FROM_IMAGE:
5887 p->u.image.object = it->object;
5888 p->u.image.image_id = it->image_id;
5889 p->u.image.slice = it->slice;
5890 break;
5891 case GET_FROM_STRETCH:
5892 p->u.stretch.object = it->object;
5893 break;
5894 }
5895 p->position = position ? *position : it->position;
5896 p->current = it->current;
5897 p->end_charpos = it->end_charpos;
5898 p->string_nchars = it->string_nchars;
5899 p->area = it->area;
5900 p->multibyte_p = it->multibyte_p;
5901 p->avoid_cursor_p = it->avoid_cursor_p;
5902 p->space_width = it->space_width;
5903 p->font_height = it->font_height;
5904 p->voffset = it->voffset;
5905 p->string_from_display_prop_p = it->string_from_display_prop_p;
5906 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5907 p->display_ellipsis_p = false;
5908 p->line_wrap = it->line_wrap;
5909 p->bidi_p = it->bidi_p;
5910 p->paragraph_embedding = it->paragraph_embedding;
5911 p->from_disp_prop_p = it->from_disp_prop_p;
5912 ++it->sp;
5913
5914 /* Save the state of the bidi iterator as well. */
5915 if (it->bidi_p)
5916 bidi_push_it (&it->bidi_it);
5917 }
5918
5919 static void
5920 iterate_out_of_display_property (struct it *it)
5921 {
5922 bool buffer_p = !STRINGP (it->string);
5923 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5924 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5925
5926 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5927
5928 /* Maybe initialize paragraph direction. If we are at the beginning
5929 of a new paragraph, next_element_from_buffer may not have a
5930 chance to do that. */
5931 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5932 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5933 /* prev_stop can be zero, so check against BEGV as well. */
5934 while (it->bidi_it.charpos >= bob
5935 && it->prev_stop <= it->bidi_it.charpos
5936 && it->bidi_it.charpos < CHARPOS (it->position)
5937 && it->bidi_it.charpos < eob)
5938 bidi_move_to_visually_next (&it->bidi_it);
5939 /* Record the stop_pos we just crossed, for when we cross it
5940 back, maybe. */
5941 if (it->bidi_it.charpos > CHARPOS (it->position))
5942 it->prev_stop = CHARPOS (it->position);
5943 /* If we ended up not where pop_it put us, resync IT's
5944 positional members with the bidi iterator. */
5945 if (it->bidi_it.charpos != CHARPOS (it->position))
5946 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5947 if (buffer_p)
5948 it->current.pos = it->position;
5949 else
5950 it->current.string_pos = it->position;
5951 }
5952
5953 /* Restore IT's settings from IT->stack. Called, for example, when no
5954 more overlay strings must be processed, and we return to delivering
5955 display elements from a buffer, or when the end of a string from a
5956 `display' property is reached and we return to delivering display
5957 elements from an overlay string, or from a buffer. */
5958
5959 static void
5960 pop_it (struct it *it)
5961 {
5962 struct iterator_stack_entry *p;
5963 bool from_display_prop = it->from_disp_prop_p;
5964
5965 eassert (it->sp > 0);
5966 --it->sp;
5967 p = it->stack + it->sp;
5968 it->stop_charpos = p->stop_charpos;
5969 it->prev_stop = p->prev_stop;
5970 it->base_level_stop = p->base_level_stop;
5971 it->cmp_it = p->cmp_it;
5972 it->face_id = p->face_id;
5973 it->current = p->current;
5974 it->position = p->position;
5975 it->string = p->string;
5976 it->from_overlay = p->from_overlay;
5977 if (NILP (it->string))
5978 SET_TEXT_POS (it->current.string_pos, -1, -1);
5979 it->method = p->method;
5980 switch (it->method)
5981 {
5982 case GET_FROM_IMAGE:
5983 it->image_id = p->u.image.image_id;
5984 it->object = p->u.image.object;
5985 it->slice = p->u.image.slice;
5986 break;
5987 case GET_FROM_STRETCH:
5988 it->object = p->u.stretch.object;
5989 break;
5990 case GET_FROM_BUFFER:
5991 it->object = it->w->contents;
5992 break;
5993 case GET_FROM_STRING:
5994 {
5995 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5996
5997 /* Restore the face_box_p flag, since it could have been
5998 overwritten by the face of the object that we just finished
5999 displaying. */
6000 if (face)
6001 it->face_box_p = face->box != FACE_NO_BOX;
6002 it->object = it->string;
6003 }
6004 break;
6005 case GET_FROM_DISPLAY_VECTOR:
6006 if (it->s)
6007 it->method = GET_FROM_C_STRING;
6008 else if (STRINGP (it->string))
6009 it->method = GET_FROM_STRING;
6010 else
6011 {
6012 it->method = GET_FROM_BUFFER;
6013 it->object = it->w->contents;
6014 }
6015 }
6016 it->end_charpos = p->end_charpos;
6017 it->string_nchars = p->string_nchars;
6018 it->area = p->area;
6019 it->multibyte_p = p->multibyte_p;
6020 it->avoid_cursor_p = p->avoid_cursor_p;
6021 it->space_width = p->space_width;
6022 it->font_height = p->font_height;
6023 it->voffset = p->voffset;
6024 it->string_from_display_prop_p = p->string_from_display_prop_p;
6025 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6026 it->line_wrap = p->line_wrap;
6027 it->bidi_p = p->bidi_p;
6028 it->paragraph_embedding = p->paragraph_embedding;
6029 it->from_disp_prop_p = p->from_disp_prop_p;
6030 if (it->bidi_p)
6031 {
6032 bidi_pop_it (&it->bidi_it);
6033 /* Bidi-iterate until we get out of the portion of text, if any,
6034 covered by a `display' text property or by an overlay with
6035 `display' property. (We cannot just jump there, because the
6036 internal coherency of the bidi iterator state can not be
6037 preserved across such jumps.) We also must determine the
6038 paragraph base direction if the overlay we just processed is
6039 at the beginning of a new paragraph. */
6040 if (from_display_prop
6041 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6042 iterate_out_of_display_property (it);
6043
6044 eassert ((BUFFERP (it->object)
6045 && IT_CHARPOS (*it) == it->bidi_it.charpos
6046 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6047 || (STRINGP (it->object)
6048 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6049 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6050 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6051 }
6052 }
6053
6054
6055 \f
6056 /***********************************************************************
6057 Moving over lines
6058 ***********************************************************************/
6059
6060 /* Set IT's current position to the previous line start. */
6061
6062 static void
6063 back_to_previous_line_start (struct it *it)
6064 {
6065 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6066
6067 DEC_BOTH (cp, bp);
6068 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6069 }
6070
6071
6072 /* Move IT to the next line start.
6073
6074 Value is true if a newline was found. Set *SKIPPED_P to true if
6075 we skipped over part of the text (as opposed to moving the iterator
6076 continuously over the text). Otherwise, don't change the value
6077 of *SKIPPED_P.
6078
6079 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6080 iterator on the newline, if it was found.
6081
6082 Newlines may come from buffer text, overlay strings, or strings
6083 displayed via the `display' property. That's the reason we can't
6084 simply use find_newline_no_quit.
6085
6086 Note that this function may not skip over invisible text that is so
6087 because of text properties and immediately follows a newline. If
6088 it would, function reseat_at_next_visible_line_start, when called
6089 from set_iterator_to_next, would effectively make invisible
6090 characters following a newline part of the wrong glyph row, which
6091 leads to wrong cursor motion. */
6092
6093 static bool
6094 forward_to_next_line_start (struct it *it, bool *skipped_p,
6095 struct bidi_it *bidi_it_prev)
6096 {
6097 ptrdiff_t old_selective;
6098 bool newline_found_p = false;
6099 int n;
6100 const int MAX_NEWLINE_DISTANCE = 500;
6101
6102 /* If already on a newline, just consume it to avoid unintended
6103 skipping over invisible text below. */
6104 if (it->what == IT_CHARACTER
6105 && it->c == '\n'
6106 && CHARPOS (it->position) == IT_CHARPOS (*it))
6107 {
6108 if (it->bidi_p && bidi_it_prev)
6109 *bidi_it_prev = it->bidi_it;
6110 set_iterator_to_next (it, false);
6111 it->c = 0;
6112 return true;
6113 }
6114
6115 /* Don't handle selective display in the following. It's (a)
6116 unnecessary because it's done by the caller, and (b) leads to an
6117 infinite recursion because next_element_from_ellipsis indirectly
6118 calls this function. */
6119 old_selective = it->selective;
6120 it->selective = 0;
6121
6122 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6123 from buffer text. */
6124 for (n = 0;
6125 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6126 n += !STRINGP (it->string))
6127 {
6128 if (!get_next_display_element (it))
6129 return false;
6130 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6131 if (newline_found_p && it->bidi_p && bidi_it_prev)
6132 *bidi_it_prev = it->bidi_it;
6133 set_iterator_to_next (it, false);
6134 }
6135
6136 /* If we didn't find a newline near enough, see if we can use a
6137 short-cut. */
6138 if (!newline_found_p)
6139 {
6140 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6141 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6142 1, &bytepos);
6143 Lisp_Object pos;
6144
6145 eassert (!STRINGP (it->string));
6146
6147 /* If there isn't any `display' property in sight, and no
6148 overlays, we can just use the position of the newline in
6149 buffer text. */
6150 if (it->stop_charpos >= limit
6151 || ((pos = Fnext_single_property_change (make_number (start),
6152 Qdisplay, Qnil,
6153 make_number (limit)),
6154 NILP (pos))
6155 && next_overlay_change (start) == ZV))
6156 {
6157 if (!it->bidi_p)
6158 {
6159 IT_CHARPOS (*it) = limit;
6160 IT_BYTEPOS (*it) = bytepos;
6161 }
6162 else
6163 {
6164 struct bidi_it bprev;
6165
6166 /* Help bidi.c avoid expensive searches for display
6167 properties and overlays, by telling it that there are
6168 none up to `limit'. */
6169 if (it->bidi_it.disp_pos < limit)
6170 {
6171 it->bidi_it.disp_pos = limit;
6172 it->bidi_it.disp_prop = 0;
6173 }
6174 do {
6175 bprev = it->bidi_it;
6176 bidi_move_to_visually_next (&it->bidi_it);
6177 } while (it->bidi_it.charpos != limit);
6178 IT_CHARPOS (*it) = limit;
6179 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6180 if (bidi_it_prev)
6181 *bidi_it_prev = bprev;
6182 }
6183 *skipped_p = newline_found_p = true;
6184 }
6185 else
6186 {
6187 while (get_next_display_element (it)
6188 && !newline_found_p)
6189 {
6190 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6191 if (newline_found_p && it->bidi_p && bidi_it_prev)
6192 *bidi_it_prev = it->bidi_it;
6193 set_iterator_to_next (it, false);
6194 }
6195 }
6196 }
6197
6198 it->selective = old_selective;
6199 return newline_found_p;
6200 }
6201
6202
6203 /* Set IT's current position to the previous visible line start. Skip
6204 invisible text that is so either due to text properties or due to
6205 selective display. Caution: this does not change IT->current_x and
6206 IT->hpos. */
6207
6208 static void
6209 back_to_previous_visible_line_start (struct it *it)
6210 {
6211 while (IT_CHARPOS (*it) > BEGV)
6212 {
6213 back_to_previous_line_start (it);
6214
6215 if (IT_CHARPOS (*it) <= BEGV)
6216 break;
6217
6218 /* If selective > 0, then lines indented more than its value are
6219 invisible. */
6220 if (it->selective > 0
6221 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6222 it->selective))
6223 continue;
6224
6225 /* Check the newline before point for invisibility. */
6226 {
6227 Lisp_Object prop;
6228 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6229 Qinvisible, it->window);
6230 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6231 continue;
6232 }
6233
6234 if (IT_CHARPOS (*it) <= BEGV)
6235 break;
6236
6237 {
6238 struct it it2;
6239 void *it2data = NULL;
6240 ptrdiff_t pos;
6241 ptrdiff_t beg, end;
6242 Lisp_Object val, overlay;
6243
6244 SAVE_IT (it2, *it, it2data);
6245
6246 /* If newline is part of a composition, continue from start of composition */
6247 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6248 && beg < IT_CHARPOS (*it))
6249 goto replaced;
6250
6251 /* If newline is replaced by a display property, find start of overlay
6252 or interval and continue search from that point. */
6253 pos = --IT_CHARPOS (it2);
6254 --IT_BYTEPOS (it2);
6255 it2.sp = 0;
6256 bidi_unshelve_cache (NULL, false);
6257 it2.string_from_display_prop_p = false;
6258 it2.from_disp_prop_p = false;
6259 if (handle_display_prop (&it2) == HANDLED_RETURN
6260 && !NILP (val = get_char_property_and_overlay
6261 (make_number (pos), Qdisplay, Qnil, &overlay))
6262 && (OVERLAYP (overlay)
6263 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6264 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6265 {
6266 RESTORE_IT (it, it, it2data);
6267 goto replaced;
6268 }
6269
6270 /* Newline is not replaced by anything -- so we are done. */
6271 RESTORE_IT (it, it, it2data);
6272 break;
6273
6274 replaced:
6275 if (beg < BEGV)
6276 beg = BEGV;
6277 IT_CHARPOS (*it) = beg;
6278 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6279 }
6280 }
6281
6282 it->continuation_lines_width = 0;
6283
6284 eassert (IT_CHARPOS (*it) >= BEGV);
6285 eassert (IT_CHARPOS (*it) == BEGV
6286 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6287 CHECK_IT (it);
6288 }
6289
6290
6291 /* Reseat iterator IT at the previous visible line start. Skip
6292 invisible text that is so either due to text properties or due to
6293 selective display. At the end, update IT's overlay information,
6294 face information etc. */
6295
6296 void
6297 reseat_at_previous_visible_line_start (struct it *it)
6298 {
6299 back_to_previous_visible_line_start (it);
6300 reseat (it, it->current.pos, true);
6301 CHECK_IT (it);
6302 }
6303
6304
6305 /* Reseat iterator IT on the next visible line start in the current
6306 buffer. ON_NEWLINE_P means position IT on the newline
6307 preceding the line start. Skip over invisible text that is so
6308 because of selective display. Compute faces, overlays etc at the
6309 new position. Note that this function does not skip over text that
6310 is invisible because of text properties. */
6311
6312 static void
6313 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6314 {
6315 bool skipped_p = false;
6316 struct bidi_it bidi_it_prev;
6317 bool newline_found_p
6318 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6319
6320 /* Skip over lines that are invisible because they are indented
6321 more than the value of IT->selective. */
6322 if (it->selective > 0)
6323 while (IT_CHARPOS (*it) < ZV
6324 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6325 it->selective))
6326 {
6327 eassert (IT_BYTEPOS (*it) == BEGV
6328 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6329 newline_found_p =
6330 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6331 }
6332
6333 /* Position on the newline if that's what's requested. */
6334 if (on_newline_p && newline_found_p)
6335 {
6336 if (STRINGP (it->string))
6337 {
6338 if (IT_STRING_CHARPOS (*it) > 0)
6339 {
6340 if (!it->bidi_p)
6341 {
6342 --IT_STRING_CHARPOS (*it);
6343 --IT_STRING_BYTEPOS (*it);
6344 }
6345 else
6346 {
6347 /* We need to restore the bidi iterator to the state
6348 it had on the newline, and resync the IT's
6349 position with that. */
6350 it->bidi_it = bidi_it_prev;
6351 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6352 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6353 }
6354 }
6355 }
6356 else if (IT_CHARPOS (*it) > BEGV)
6357 {
6358 if (!it->bidi_p)
6359 {
6360 --IT_CHARPOS (*it);
6361 --IT_BYTEPOS (*it);
6362 }
6363 else
6364 {
6365 /* We need to restore the bidi iterator to the state it
6366 had on the newline and resync IT with that. */
6367 it->bidi_it = bidi_it_prev;
6368 IT_CHARPOS (*it) = it->bidi_it.charpos;
6369 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6370 }
6371 reseat (it, it->current.pos, false);
6372 }
6373 }
6374 else if (skipped_p)
6375 reseat (it, it->current.pos, false);
6376
6377 CHECK_IT (it);
6378 }
6379
6380
6381 \f
6382 /***********************************************************************
6383 Changing an iterator's position
6384 ***********************************************************************/
6385
6386 /* Change IT's current position to POS in current_buffer.
6387 If FORCE_P, always check for text properties at the new position.
6388 Otherwise, text properties are only looked up if POS >=
6389 IT->check_charpos of a property. */
6390
6391 static void
6392 reseat (struct it *it, struct text_pos pos, bool force_p)
6393 {
6394 ptrdiff_t original_pos = IT_CHARPOS (*it);
6395
6396 reseat_1 (it, pos, false);
6397
6398 /* Determine where to check text properties. Avoid doing it
6399 where possible because text property lookup is very expensive. */
6400 if (force_p
6401 || CHARPOS (pos) > it->stop_charpos
6402 || CHARPOS (pos) < original_pos)
6403 {
6404 if (it->bidi_p)
6405 {
6406 /* For bidi iteration, we need to prime prev_stop and
6407 base_level_stop with our best estimations. */
6408 /* Implementation note: Of course, POS is not necessarily a
6409 stop position, so assigning prev_pos to it is a lie; we
6410 should have called compute_stop_backwards. However, if
6411 the current buffer does not include any R2L characters,
6412 that call would be a waste of cycles, because the
6413 iterator will never move back, and thus never cross this
6414 "fake" stop position. So we delay that backward search
6415 until the time we really need it, in next_element_from_buffer. */
6416 if (CHARPOS (pos) != it->prev_stop)
6417 it->prev_stop = CHARPOS (pos);
6418 if (CHARPOS (pos) < it->base_level_stop)
6419 it->base_level_stop = 0; /* meaning it's unknown */
6420 handle_stop (it);
6421 }
6422 else
6423 {
6424 handle_stop (it);
6425 it->prev_stop = it->base_level_stop = 0;
6426 }
6427
6428 }
6429
6430 CHECK_IT (it);
6431 }
6432
6433
6434 /* Change IT's buffer position to POS. SET_STOP_P means set
6435 IT->stop_pos to POS, also. */
6436
6437 static void
6438 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6439 {
6440 /* Don't call this function when scanning a C string. */
6441 eassert (it->s == NULL);
6442
6443 /* POS must be a reasonable value. */
6444 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6445
6446 it->current.pos = it->position = pos;
6447 it->end_charpos = ZV;
6448 it->dpvec = NULL;
6449 it->current.dpvec_index = -1;
6450 it->current.overlay_string_index = -1;
6451 IT_STRING_CHARPOS (*it) = -1;
6452 IT_STRING_BYTEPOS (*it) = -1;
6453 it->string = Qnil;
6454 it->method = GET_FROM_BUFFER;
6455 it->object = it->w->contents;
6456 it->area = TEXT_AREA;
6457 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6458 it->sp = 0;
6459 it->string_from_display_prop_p = false;
6460 it->string_from_prefix_prop_p = false;
6461
6462 it->from_disp_prop_p = false;
6463 it->face_before_selective_p = false;
6464 if (it->bidi_p)
6465 {
6466 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6467 &it->bidi_it);
6468 bidi_unshelve_cache (NULL, false);
6469 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6470 it->bidi_it.string.s = NULL;
6471 it->bidi_it.string.lstring = Qnil;
6472 it->bidi_it.string.bufpos = 0;
6473 it->bidi_it.string.from_disp_str = false;
6474 it->bidi_it.string.unibyte = false;
6475 it->bidi_it.w = it->w;
6476 }
6477
6478 if (set_stop_p)
6479 {
6480 it->stop_charpos = CHARPOS (pos);
6481 it->base_level_stop = CHARPOS (pos);
6482 }
6483 /* This make the information stored in it->cmp_it invalidate. */
6484 it->cmp_it.id = -1;
6485 }
6486
6487
6488 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6489 If S is non-null, it is a C string to iterate over. Otherwise,
6490 STRING gives a Lisp string to iterate over.
6491
6492 If PRECISION > 0, don't return more then PRECISION number of
6493 characters from the string.
6494
6495 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6496 characters have been returned. FIELD_WIDTH < 0 means an infinite
6497 field width.
6498
6499 MULTIBYTE = 0 means disable processing of multibyte characters,
6500 MULTIBYTE > 0 means enable it,
6501 MULTIBYTE < 0 means use IT->multibyte_p.
6502
6503 IT must be initialized via a prior call to init_iterator before
6504 calling this function. */
6505
6506 static void
6507 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6508 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6509 int multibyte)
6510 {
6511 /* No text property checks performed by default, but see below. */
6512 it->stop_charpos = -1;
6513
6514 /* Set iterator position and end position. */
6515 memset (&it->current, 0, sizeof it->current);
6516 it->current.overlay_string_index = -1;
6517 it->current.dpvec_index = -1;
6518 eassert (charpos >= 0);
6519
6520 /* If STRING is specified, use its multibyteness, otherwise use the
6521 setting of MULTIBYTE, if specified. */
6522 if (multibyte >= 0)
6523 it->multibyte_p = multibyte > 0;
6524
6525 /* Bidirectional reordering of strings is controlled by the default
6526 value of bidi-display-reordering. Don't try to reorder while
6527 loading loadup.el, as the necessary character property tables are
6528 not yet available. */
6529 it->bidi_p =
6530 NILP (Vpurify_flag)
6531 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6532
6533 if (s == NULL)
6534 {
6535 eassert (STRINGP (string));
6536 it->string = string;
6537 it->s = NULL;
6538 it->end_charpos = it->string_nchars = SCHARS (string);
6539 it->method = GET_FROM_STRING;
6540 it->current.string_pos = string_pos (charpos, string);
6541
6542 if (it->bidi_p)
6543 {
6544 it->bidi_it.string.lstring = string;
6545 it->bidi_it.string.s = NULL;
6546 it->bidi_it.string.schars = it->end_charpos;
6547 it->bidi_it.string.bufpos = 0;
6548 it->bidi_it.string.from_disp_str = false;
6549 it->bidi_it.string.unibyte = !it->multibyte_p;
6550 it->bidi_it.w = it->w;
6551 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6552 FRAME_WINDOW_P (it->f), &it->bidi_it);
6553 }
6554 }
6555 else
6556 {
6557 it->s = (const unsigned char *) s;
6558 it->string = Qnil;
6559
6560 /* Note that we use IT->current.pos, not it->current.string_pos,
6561 for displaying C strings. */
6562 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6563 if (it->multibyte_p)
6564 {
6565 it->current.pos = c_string_pos (charpos, s, true);
6566 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6567 }
6568 else
6569 {
6570 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6571 it->end_charpos = it->string_nchars = strlen (s);
6572 }
6573
6574 if (it->bidi_p)
6575 {
6576 it->bidi_it.string.lstring = Qnil;
6577 it->bidi_it.string.s = (const unsigned char *) s;
6578 it->bidi_it.string.schars = it->end_charpos;
6579 it->bidi_it.string.bufpos = 0;
6580 it->bidi_it.string.from_disp_str = false;
6581 it->bidi_it.string.unibyte = !it->multibyte_p;
6582 it->bidi_it.w = it->w;
6583 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6584 &it->bidi_it);
6585 }
6586 it->method = GET_FROM_C_STRING;
6587 }
6588
6589 /* PRECISION > 0 means don't return more than PRECISION characters
6590 from the string. */
6591 if (precision > 0 && it->end_charpos - charpos > precision)
6592 {
6593 it->end_charpos = it->string_nchars = charpos + precision;
6594 if (it->bidi_p)
6595 it->bidi_it.string.schars = it->end_charpos;
6596 }
6597
6598 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6599 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6600 FIELD_WIDTH < 0 means infinite field width. This is useful for
6601 padding with `-' at the end of a mode line. */
6602 if (field_width < 0)
6603 field_width = INFINITY;
6604 /* Implementation note: We deliberately don't enlarge
6605 it->bidi_it.string.schars here to fit it->end_charpos, because
6606 the bidi iterator cannot produce characters out of thin air. */
6607 if (field_width > it->end_charpos - charpos)
6608 it->end_charpos = charpos + field_width;
6609
6610 /* Use the standard display table for displaying strings. */
6611 if (DISP_TABLE_P (Vstandard_display_table))
6612 it->dp = XCHAR_TABLE (Vstandard_display_table);
6613
6614 it->stop_charpos = charpos;
6615 it->prev_stop = charpos;
6616 it->base_level_stop = 0;
6617 if (it->bidi_p)
6618 {
6619 it->bidi_it.first_elt = true;
6620 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6621 it->bidi_it.disp_pos = -1;
6622 }
6623 if (s == NULL && it->multibyte_p)
6624 {
6625 ptrdiff_t endpos = SCHARS (it->string);
6626 if (endpos > it->end_charpos)
6627 endpos = it->end_charpos;
6628 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6629 it->string);
6630 }
6631 CHECK_IT (it);
6632 }
6633
6634
6635 \f
6636 /***********************************************************************
6637 Iteration
6638 ***********************************************************************/
6639
6640 /* Map enum it_method value to corresponding next_element_from_* function. */
6641
6642 typedef bool (*next_element_function) (struct it *);
6643
6644 static next_element_function const get_next_element[NUM_IT_METHODS] =
6645 {
6646 next_element_from_buffer,
6647 next_element_from_display_vector,
6648 next_element_from_string,
6649 next_element_from_c_string,
6650 next_element_from_image,
6651 next_element_from_stretch
6652 };
6653
6654 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6655
6656
6657 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6658 (possibly with the following characters). */
6659
6660 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6661 ((IT)->cmp_it.id >= 0 \
6662 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6663 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6664 END_CHARPOS, (IT)->w, \
6665 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6666 (IT)->string)))
6667
6668
6669 /* Lookup the char-table Vglyphless_char_display for character C (-1
6670 if we want information for no-font case), and return the display
6671 method symbol. By side-effect, update it->what and
6672 it->glyphless_method. This function is called from
6673 get_next_display_element for each character element, and from
6674 x_produce_glyphs when no suitable font was found. */
6675
6676 Lisp_Object
6677 lookup_glyphless_char_display (int c, struct it *it)
6678 {
6679 Lisp_Object glyphless_method = Qnil;
6680
6681 if (CHAR_TABLE_P (Vglyphless_char_display)
6682 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6683 {
6684 if (c >= 0)
6685 {
6686 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6687 if (CONSP (glyphless_method))
6688 glyphless_method = FRAME_WINDOW_P (it->f)
6689 ? XCAR (glyphless_method)
6690 : XCDR (glyphless_method);
6691 }
6692 else
6693 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6694 }
6695
6696 retry:
6697 if (NILP (glyphless_method))
6698 {
6699 if (c >= 0)
6700 /* The default is to display the character by a proper font. */
6701 return Qnil;
6702 /* The default for the no-font case is to display an empty box. */
6703 glyphless_method = Qempty_box;
6704 }
6705 if (EQ (glyphless_method, Qzero_width))
6706 {
6707 if (c >= 0)
6708 return glyphless_method;
6709 /* This method can't be used for the no-font case. */
6710 glyphless_method = Qempty_box;
6711 }
6712 if (EQ (glyphless_method, Qthin_space))
6713 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6714 else if (EQ (glyphless_method, Qempty_box))
6715 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6716 else if (EQ (glyphless_method, Qhex_code))
6717 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6718 else if (STRINGP (glyphless_method))
6719 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6720 else
6721 {
6722 /* Invalid value. We use the default method. */
6723 glyphless_method = Qnil;
6724 goto retry;
6725 }
6726 it->what = IT_GLYPHLESS;
6727 return glyphless_method;
6728 }
6729
6730 /* Merge escape glyph face and cache the result. */
6731
6732 static struct frame *last_escape_glyph_frame = NULL;
6733 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6734 static int last_escape_glyph_merged_face_id = 0;
6735
6736 static int
6737 merge_escape_glyph_face (struct it *it)
6738 {
6739 int face_id;
6740
6741 if (it->f == last_escape_glyph_frame
6742 && it->face_id == last_escape_glyph_face_id)
6743 face_id = last_escape_glyph_merged_face_id;
6744 else
6745 {
6746 /* Merge the `escape-glyph' face into the current face. */
6747 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6748 last_escape_glyph_frame = it->f;
6749 last_escape_glyph_face_id = it->face_id;
6750 last_escape_glyph_merged_face_id = face_id;
6751 }
6752 return face_id;
6753 }
6754
6755 /* Likewise for glyphless glyph face. */
6756
6757 static struct frame *last_glyphless_glyph_frame = NULL;
6758 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6759 static int last_glyphless_glyph_merged_face_id = 0;
6760
6761 int
6762 merge_glyphless_glyph_face (struct it *it)
6763 {
6764 int face_id;
6765
6766 if (it->f == last_glyphless_glyph_frame
6767 && it->face_id == last_glyphless_glyph_face_id)
6768 face_id = last_glyphless_glyph_merged_face_id;
6769 else
6770 {
6771 /* Merge the `glyphless-char' face into the current face. */
6772 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6773 last_glyphless_glyph_frame = it->f;
6774 last_glyphless_glyph_face_id = it->face_id;
6775 last_glyphless_glyph_merged_face_id = face_id;
6776 }
6777 return face_id;
6778 }
6779
6780 /* Load IT's display element fields with information about the next
6781 display element from the current position of IT. Value is false if
6782 end of buffer (or C string) is reached. */
6783
6784 static bool
6785 get_next_display_element (struct it *it)
6786 {
6787 /* True means that we found a display element. False means that
6788 we hit the end of what we iterate over. Performance note: the
6789 function pointer `method' used here turns out to be faster than
6790 using a sequence of if-statements. */
6791 bool success_p;
6792
6793 get_next:
6794 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6795
6796 if (it->what == IT_CHARACTER)
6797 {
6798 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6799 and only if (a) the resolved directionality of that character
6800 is R..." */
6801 /* FIXME: Do we need an exception for characters from display
6802 tables? */
6803 if (it->bidi_p && it->bidi_it.type == STRONG_R
6804 && !inhibit_bidi_mirroring)
6805 it->c = bidi_mirror_char (it->c);
6806 /* Map via display table or translate control characters.
6807 IT->c, IT->len etc. have been set to the next character by
6808 the function call above. If we have a display table, and it
6809 contains an entry for IT->c, translate it. Don't do this if
6810 IT->c itself comes from a display table, otherwise we could
6811 end up in an infinite recursion. (An alternative could be to
6812 count the recursion depth of this function and signal an
6813 error when a certain maximum depth is reached.) Is it worth
6814 it? */
6815 if (success_p && it->dpvec == NULL)
6816 {
6817 Lisp_Object dv;
6818 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6819 bool nonascii_space_p = false;
6820 bool nonascii_hyphen_p = false;
6821 int c = it->c; /* This is the character to display. */
6822
6823 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6824 {
6825 eassert (SINGLE_BYTE_CHAR_P (c));
6826 if (unibyte_display_via_language_environment)
6827 {
6828 c = DECODE_CHAR (unibyte, c);
6829 if (c < 0)
6830 c = BYTE8_TO_CHAR (it->c);
6831 }
6832 else
6833 c = BYTE8_TO_CHAR (it->c);
6834 }
6835
6836 if (it->dp
6837 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6838 VECTORP (dv)))
6839 {
6840 struct Lisp_Vector *v = XVECTOR (dv);
6841
6842 /* Return the first character from the display table
6843 entry, if not empty. If empty, don't display the
6844 current character. */
6845 if (v->header.size)
6846 {
6847 it->dpvec_char_len = it->len;
6848 it->dpvec = v->contents;
6849 it->dpend = v->contents + v->header.size;
6850 it->current.dpvec_index = 0;
6851 it->dpvec_face_id = -1;
6852 it->saved_face_id = it->face_id;
6853 it->method = GET_FROM_DISPLAY_VECTOR;
6854 it->ellipsis_p = false;
6855 }
6856 else
6857 {
6858 set_iterator_to_next (it, false);
6859 }
6860 goto get_next;
6861 }
6862
6863 if (! NILP (lookup_glyphless_char_display (c, it)))
6864 {
6865 if (it->what == IT_GLYPHLESS)
6866 goto done;
6867 /* Don't display this character. */
6868 set_iterator_to_next (it, false);
6869 goto get_next;
6870 }
6871
6872 /* If `nobreak-char-display' is non-nil, we display
6873 non-ASCII spaces and hyphens specially. */
6874 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6875 {
6876 if (c == 0xA0)
6877 nonascii_space_p = true;
6878 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6879 nonascii_hyphen_p = true;
6880 }
6881
6882 /* Translate control characters into `\003' or `^C' form.
6883 Control characters coming from a display table entry are
6884 currently not translated because we use IT->dpvec to hold
6885 the translation. This could easily be changed but I
6886 don't believe that it is worth doing.
6887
6888 The characters handled by `nobreak-char-display' must be
6889 translated too.
6890
6891 Non-printable characters and raw-byte characters are also
6892 translated to octal form. */
6893 if (((c < ' ' || c == 127) /* ASCII control chars. */
6894 ? (it->area != TEXT_AREA
6895 /* In mode line, treat \n, \t like other crl chars. */
6896 || (c != '\t'
6897 && it->glyph_row
6898 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6899 || (c != '\n' && c != '\t'))
6900 : (nonascii_space_p
6901 || nonascii_hyphen_p
6902 || CHAR_BYTE8_P (c)
6903 || ! CHAR_PRINTABLE_P (c))))
6904 {
6905 /* C is a control character, non-ASCII space/hyphen,
6906 raw-byte, or a non-printable character which must be
6907 displayed either as '\003' or as `^C' where the '\\'
6908 and '^' can be defined in the display table. Fill
6909 IT->ctl_chars with glyphs for what we have to
6910 display. Then, set IT->dpvec to these glyphs. */
6911 Lisp_Object gc;
6912 int ctl_len;
6913 int face_id;
6914 int lface_id = 0;
6915 int escape_glyph;
6916
6917 /* Handle control characters with ^. */
6918
6919 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6920 {
6921 int g;
6922
6923 g = '^'; /* default glyph for Control */
6924 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6925 if (it->dp
6926 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6927 {
6928 g = GLYPH_CODE_CHAR (gc);
6929 lface_id = GLYPH_CODE_FACE (gc);
6930 }
6931
6932 face_id = (lface_id
6933 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6934 : merge_escape_glyph_face (it));
6935
6936 XSETINT (it->ctl_chars[0], g);
6937 XSETINT (it->ctl_chars[1], c ^ 0100);
6938 ctl_len = 2;
6939 goto display_control;
6940 }
6941
6942 /* Handle non-ascii space in the mode where it only gets
6943 highlighting. */
6944
6945 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6946 {
6947 /* Merge `nobreak-space' into the current face. */
6948 face_id = merge_faces (it->f, Qnobreak_space, 0,
6949 it->face_id);
6950 XSETINT (it->ctl_chars[0], ' ');
6951 ctl_len = 1;
6952 goto display_control;
6953 }
6954
6955 /* Handle sequences that start with the "escape glyph". */
6956
6957 /* the default escape glyph is \. */
6958 escape_glyph = '\\';
6959
6960 if (it->dp
6961 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6962 {
6963 escape_glyph = GLYPH_CODE_CHAR (gc);
6964 lface_id = GLYPH_CODE_FACE (gc);
6965 }
6966
6967 face_id = (lface_id
6968 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6969 : merge_escape_glyph_face (it));
6970
6971 /* Draw non-ASCII hyphen with just highlighting: */
6972
6973 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6974 {
6975 XSETINT (it->ctl_chars[0], '-');
6976 ctl_len = 1;
6977 goto display_control;
6978 }
6979
6980 /* Draw non-ASCII space/hyphen with escape glyph: */
6981
6982 if (nonascii_space_p || nonascii_hyphen_p)
6983 {
6984 XSETINT (it->ctl_chars[0], escape_glyph);
6985 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6986 ctl_len = 2;
6987 goto display_control;
6988 }
6989
6990 {
6991 char str[10];
6992 int len, i;
6993
6994 if (CHAR_BYTE8_P (c))
6995 /* Display \200 instead of \17777600. */
6996 c = CHAR_TO_BYTE8 (c);
6997 len = sprintf (str, "%03o", c + 0u);
6998
6999 XSETINT (it->ctl_chars[0], escape_glyph);
7000 for (i = 0; i < len; i++)
7001 XSETINT (it->ctl_chars[i + 1], str[i]);
7002 ctl_len = len + 1;
7003 }
7004
7005 display_control:
7006 /* Set up IT->dpvec and return first character from it. */
7007 it->dpvec_char_len = it->len;
7008 it->dpvec = it->ctl_chars;
7009 it->dpend = it->dpvec + ctl_len;
7010 it->current.dpvec_index = 0;
7011 it->dpvec_face_id = face_id;
7012 it->saved_face_id = it->face_id;
7013 it->method = GET_FROM_DISPLAY_VECTOR;
7014 it->ellipsis_p = false;
7015 goto get_next;
7016 }
7017 it->char_to_display = c;
7018 }
7019 else if (success_p)
7020 {
7021 it->char_to_display = it->c;
7022 }
7023 }
7024
7025 #ifdef HAVE_WINDOW_SYSTEM
7026 /* Adjust face id for a multibyte character. There are no multibyte
7027 character in unibyte text. */
7028 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7029 && it->multibyte_p
7030 && success_p
7031 && FRAME_WINDOW_P (it->f))
7032 {
7033 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7034
7035 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7036 {
7037 /* Automatic composition with glyph-string. */
7038 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7039
7040 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7041 }
7042 else
7043 {
7044 ptrdiff_t pos = (it->s ? -1
7045 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7046 : IT_CHARPOS (*it));
7047 int c;
7048
7049 if (it->what == IT_CHARACTER)
7050 c = it->char_to_display;
7051 else
7052 {
7053 struct composition *cmp = composition_table[it->cmp_it.id];
7054 int i;
7055
7056 c = ' ';
7057 for (i = 0; i < cmp->glyph_len; i++)
7058 /* TAB in a composition means display glyphs with
7059 padding space on the left or right. */
7060 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7061 break;
7062 }
7063 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7064 }
7065 }
7066 #endif /* HAVE_WINDOW_SYSTEM */
7067
7068 done:
7069 /* Is this character the last one of a run of characters with
7070 box? If yes, set IT->end_of_box_run_p to true. */
7071 if (it->face_box_p
7072 && it->s == NULL)
7073 {
7074 if (it->method == GET_FROM_STRING && it->sp)
7075 {
7076 int face_id = underlying_face_id (it);
7077 struct face *face = FACE_FROM_ID (it->f, face_id);
7078
7079 if (face)
7080 {
7081 if (face->box == FACE_NO_BOX)
7082 {
7083 /* If the box comes from face properties in a
7084 display string, check faces in that string. */
7085 int string_face_id = face_after_it_pos (it);
7086 it->end_of_box_run_p
7087 = (FACE_FROM_ID (it->f, string_face_id)->box
7088 == FACE_NO_BOX);
7089 }
7090 /* Otherwise, the box comes from the underlying face.
7091 If this is the last string character displayed, check
7092 the next buffer location. */
7093 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7094 /* n_overlay_strings is unreliable unless
7095 overlay_string_index is non-negative. */
7096 && ((it->current.overlay_string_index >= 0
7097 && (it->current.overlay_string_index
7098 == it->n_overlay_strings - 1))
7099 /* A string from display property. */
7100 || it->from_disp_prop_p))
7101 {
7102 ptrdiff_t ignore;
7103 int next_face_id;
7104 struct text_pos pos = it->current.pos;
7105
7106 /* For a string from a display property, the next
7107 buffer position is stored in the 'position'
7108 member of the iteration stack slot below the
7109 current one, see handle_single_display_spec. By
7110 contrast, it->current.pos was is not yet updated
7111 to point to that buffer position; that will
7112 happen in pop_it, after we finish displaying the
7113 current string. Note that we already checked
7114 above that it->sp is positive, so subtracting one
7115 from it is safe. */
7116 if (it->from_disp_prop_p)
7117 pos = (it->stack + it->sp - 1)->position;
7118 else
7119 INC_TEXT_POS (pos, it->multibyte_p);
7120
7121 if (CHARPOS (pos) >= ZV)
7122 it->end_of_box_run_p = true;
7123 else
7124 {
7125 next_face_id = face_at_buffer_position
7126 (it->w, CHARPOS (pos), &ignore,
7127 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7128 it->end_of_box_run_p
7129 = (FACE_FROM_ID (it->f, next_face_id)->box
7130 == FACE_NO_BOX);
7131 }
7132 }
7133 }
7134 }
7135 /* next_element_from_display_vector sets this flag according to
7136 faces of the display vector glyphs, see there. */
7137 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7138 {
7139 int face_id = face_after_it_pos (it);
7140 it->end_of_box_run_p
7141 = (face_id != it->face_id
7142 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7143 }
7144 }
7145 /* If we reached the end of the object we've been iterating (e.g., a
7146 display string or an overlay string), and there's something on
7147 IT->stack, proceed with what's on the stack. It doesn't make
7148 sense to return false if there's unprocessed stuff on the stack,
7149 because otherwise that stuff will never be displayed. */
7150 if (!success_p && it->sp > 0)
7151 {
7152 set_iterator_to_next (it, false);
7153 success_p = get_next_display_element (it);
7154 }
7155
7156 /* Value is false if end of buffer or string reached. */
7157 return success_p;
7158 }
7159
7160
7161 /* Move IT to the next display element.
7162
7163 RESEAT_P means if called on a newline in buffer text,
7164 skip to the next visible line start.
7165
7166 Functions get_next_display_element and set_iterator_to_next are
7167 separate because I find this arrangement easier to handle than a
7168 get_next_display_element function that also increments IT's
7169 position. The way it is we can first look at an iterator's current
7170 display element, decide whether it fits on a line, and if it does,
7171 increment the iterator position. The other way around we probably
7172 would either need a flag indicating whether the iterator has to be
7173 incremented the next time, or we would have to implement a
7174 decrement position function which would not be easy to write. */
7175
7176 void
7177 set_iterator_to_next (struct it *it, bool reseat_p)
7178 {
7179 /* Reset flags indicating start and end of a sequence of characters
7180 with box. Reset them at the start of this function because
7181 moving the iterator to a new position might set them. */
7182 it->start_of_box_run_p = it->end_of_box_run_p = false;
7183
7184 switch (it->method)
7185 {
7186 case GET_FROM_BUFFER:
7187 /* The current display element of IT is a character from
7188 current_buffer. Advance in the buffer, and maybe skip over
7189 invisible lines that are so because of selective display. */
7190 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7191 reseat_at_next_visible_line_start (it, false);
7192 else if (it->cmp_it.id >= 0)
7193 {
7194 /* We are currently getting glyphs from a composition. */
7195 if (! it->bidi_p)
7196 {
7197 IT_CHARPOS (*it) += it->cmp_it.nchars;
7198 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7199 }
7200 else
7201 {
7202 int i;
7203
7204 /* Update IT's char/byte positions to point to the first
7205 character of the next grapheme cluster, or to the
7206 character visually after the current composition. */
7207 for (i = 0; i < it->cmp_it.nchars; i++)
7208 bidi_move_to_visually_next (&it->bidi_it);
7209 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7210 IT_CHARPOS (*it) = it->bidi_it.charpos;
7211 }
7212
7213 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7214 && it->cmp_it.to < it->cmp_it.nglyphs)
7215 {
7216 /* Composition created while scanning forward. Proceed
7217 to the next grapheme cluster. */
7218 it->cmp_it.from = it->cmp_it.to;
7219 }
7220 else if ((it->bidi_p && it->cmp_it.reversed_p)
7221 && it->cmp_it.from > 0)
7222 {
7223 /* Composition created while scanning backward. Proceed
7224 to the previous grapheme cluster. */
7225 it->cmp_it.to = it->cmp_it.from;
7226 }
7227 else
7228 {
7229 /* No more grapheme clusters in this composition.
7230 Find the next stop position. */
7231 ptrdiff_t stop = it->end_charpos;
7232
7233 if (it->bidi_it.scan_dir < 0)
7234 /* Now we are scanning backward and don't know
7235 where to stop. */
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7238 IT_BYTEPOS (*it), stop, Qnil);
7239 }
7240 }
7241 else
7242 {
7243 eassert (it->len != 0);
7244
7245 if (!it->bidi_p)
7246 {
7247 IT_BYTEPOS (*it) += it->len;
7248 IT_CHARPOS (*it) += 1;
7249 }
7250 else
7251 {
7252 int prev_scan_dir = it->bidi_it.scan_dir;
7253 /* If this is a new paragraph, determine its base
7254 direction (a.k.a. its base embedding level). */
7255 if (it->bidi_it.new_paragraph)
7256 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7257 false);
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_CHARPOS (*it) = it->bidi_it.charpos;
7261 if (prev_scan_dir != it->bidi_it.scan_dir)
7262 {
7263 /* As the scan direction was changed, we must
7264 re-compute the stop position for composition. */
7265 ptrdiff_t stop = it->end_charpos;
7266 if (it->bidi_it.scan_dir < 0)
7267 stop = -1;
7268 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7269 IT_BYTEPOS (*it), stop, Qnil);
7270 }
7271 }
7272 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7273 }
7274 break;
7275
7276 case GET_FROM_C_STRING:
7277 /* Current display element of IT is from a C string. */
7278 if (!it->bidi_p
7279 /* If the string position is beyond string's end, it means
7280 next_element_from_c_string is padding the string with
7281 blanks, in which case we bypass the bidi iterator,
7282 because it cannot deal with such virtual characters. */
7283 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7284 {
7285 IT_BYTEPOS (*it) += it->len;
7286 IT_CHARPOS (*it) += 1;
7287 }
7288 else
7289 {
7290 bidi_move_to_visually_next (&it->bidi_it);
7291 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7292 IT_CHARPOS (*it) = it->bidi_it.charpos;
7293 }
7294 break;
7295
7296 case GET_FROM_DISPLAY_VECTOR:
7297 /* Current display element of IT is from a display table entry.
7298 Advance in the display table definition. Reset it to null if
7299 end reached, and continue with characters from buffers/
7300 strings. */
7301 ++it->current.dpvec_index;
7302
7303 /* Restore face of the iterator to what they were before the
7304 display vector entry (these entries may contain faces). */
7305 it->face_id = it->saved_face_id;
7306
7307 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7308 {
7309 bool recheck_faces = it->ellipsis_p;
7310
7311 if (it->s)
7312 it->method = GET_FROM_C_STRING;
7313 else if (STRINGP (it->string))
7314 it->method = GET_FROM_STRING;
7315 else
7316 {
7317 it->method = GET_FROM_BUFFER;
7318 it->object = it->w->contents;
7319 }
7320
7321 it->dpvec = NULL;
7322 it->current.dpvec_index = -1;
7323
7324 /* Skip over characters which were displayed via IT->dpvec. */
7325 if (it->dpvec_char_len < 0)
7326 reseat_at_next_visible_line_start (it, true);
7327 else if (it->dpvec_char_len > 0)
7328 {
7329 it->len = it->dpvec_char_len;
7330 set_iterator_to_next (it, reseat_p);
7331 }
7332
7333 /* Maybe recheck faces after display vector. */
7334 if (recheck_faces)
7335 {
7336 if (it->method == GET_FROM_STRING)
7337 it->stop_charpos = IT_STRING_CHARPOS (*it);
7338 else
7339 it->stop_charpos = IT_CHARPOS (*it);
7340 }
7341 }
7342 break;
7343
7344 case GET_FROM_STRING:
7345 /* Current display element is a character from a Lisp string. */
7346 eassert (it->s == NULL && STRINGP (it->string));
7347 /* Don't advance past string end. These conditions are true
7348 when set_iterator_to_next is called at the end of
7349 get_next_display_element, in which case the Lisp string is
7350 already exhausted, and all we want is pop the iterator
7351 stack. */
7352 if (it->current.overlay_string_index >= 0)
7353 {
7354 /* This is an overlay string, so there's no padding with
7355 spaces, and the number of characters in the string is
7356 where the string ends. */
7357 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7358 goto consider_string_end;
7359 }
7360 else
7361 {
7362 /* Not an overlay string. There could be padding, so test
7363 against it->end_charpos. */
7364 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7365 goto consider_string_end;
7366 }
7367 if (it->cmp_it.id >= 0)
7368 {
7369 /* We are delivering display elements from a composition.
7370 Update the string position past the grapheme cluster
7371 we've just processed. */
7372 if (! it->bidi_p)
7373 {
7374 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7375 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7376 }
7377 else
7378 {
7379 int i;
7380
7381 for (i = 0; i < it->cmp_it.nchars; i++)
7382 bidi_move_to_visually_next (&it->bidi_it);
7383 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7384 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7385 }
7386
7387 /* Did we exhaust all the grapheme clusters of this
7388 composition? */
7389 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7390 && (it->cmp_it.to < it->cmp_it.nglyphs))
7391 {
7392 /* Not all the grapheme clusters were processed yet;
7393 advance to the next cluster. */
7394 it->cmp_it.from = it->cmp_it.to;
7395 }
7396 else if ((it->bidi_p && it->cmp_it.reversed_p)
7397 && it->cmp_it.from > 0)
7398 {
7399 /* Likewise: advance to the next cluster, but going in
7400 the reverse direction. */
7401 it->cmp_it.to = it->cmp_it.from;
7402 }
7403 else
7404 {
7405 /* This composition was fully processed; find the next
7406 candidate place for checking for composed
7407 characters. */
7408 /* Always limit string searches to the string length;
7409 any padding spaces are not part of the string, and
7410 there cannot be any compositions in that padding. */
7411 ptrdiff_t stop = SCHARS (it->string);
7412
7413 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7414 stop = -1;
7415 else if (it->end_charpos < stop)
7416 {
7417 /* Cf. PRECISION in reseat_to_string: we might be
7418 limited in how many of the string characters we
7419 need to deliver. */
7420 stop = it->end_charpos;
7421 }
7422 composition_compute_stop_pos (&it->cmp_it,
7423 IT_STRING_CHARPOS (*it),
7424 IT_STRING_BYTEPOS (*it), stop,
7425 it->string);
7426 }
7427 }
7428 else
7429 {
7430 if (!it->bidi_p
7431 /* If the string position is beyond string's end, it
7432 means next_element_from_string is padding the string
7433 with blanks, in which case we bypass the bidi
7434 iterator, because it cannot deal with such virtual
7435 characters. */
7436 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7437 {
7438 IT_STRING_BYTEPOS (*it) += it->len;
7439 IT_STRING_CHARPOS (*it) += 1;
7440 }
7441 else
7442 {
7443 int prev_scan_dir = it->bidi_it.scan_dir;
7444
7445 bidi_move_to_visually_next (&it->bidi_it);
7446 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7447 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7448 /* If the scan direction changes, we may need to update
7449 the place where to check for composed characters. */
7450 if (prev_scan_dir != it->bidi_it.scan_dir)
7451 {
7452 ptrdiff_t stop = SCHARS (it->string);
7453
7454 if (it->bidi_it.scan_dir < 0)
7455 stop = -1;
7456 else if (it->end_charpos < stop)
7457 stop = it->end_charpos;
7458
7459 composition_compute_stop_pos (&it->cmp_it,
7460 IT_STRING_CHARPOS (*it),
7461 IT_STRING_BYTEPOS (*it), stop,
7462 it->string);
7463 }
7464 }
7465 }
7466
7467 consider_string_end:
7468
7469 if (it->current.overlay_string_index >= 0)
7470 {
7471 /* IT->string is an overlay string. Advance to the
7472 next, if there is one. */
7473 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7474 {
7475 it->ellipsis_p = false;
7476 next_overlay_string (it);
7477 if (it->ellipsis_p)
7478 setup_for_ellipsis (it, 0);
7479 }
7480 }
7481 else
7482 {
7483 /* IT->string is not an overlay string. If we reached
7484 its end, and there is something on IT->stack, proceed
7485 with what is on the stack. This can be either another
7486 string, this time an overlay string, or a buffer. */
7487 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7488 && it->sp > 0)
7489 {
7490 pop_it (it);
7491 if (it->method == GET_FROM_STRING)
7492 goto consider_string_end;
7493 }
7494 }
7495 break;
7496
7497 case GET_FROM_IMAGE:
7498 case GET_FROM_STRETCH:
7499 /* The position etc with which we have to proceed are on
7500 the stack. The position may be at the end of a string,
7501 if the `display' property takes up the whole string. */
7502 eassert (it->sp > 0);
7503 pop_it (it);
7504 if (it->method == GET_FROM_STRING)
7505 goto consider_string_end;
7506 break;
7507
7508 default:
7509 /* There are no other methods defined, so this should be a bug. */
7510 emacs_abort ();
7511 }
7512
7513 eassert (it->method != GET_FROM_STRING
7514 || (STRINGP (it->string)
7515 && IT_STRING_CHARPOS (*it) >= 0));
7516 }
7517
7518 /* Load IT's display element fields with information about the next
7519 display element which comes from a display table entry or from the
7520 result of translating a control character to one of the forms `^C'
7521 or `\003'.
7522
7523 IT->dpvec holds the glyphs to return as characters.
7524 IT->saved_face_id holds the face id before the display vector--it
7525 is restored into IT->face_id in set_iterator_to_next. */
7526
7527 static bool
7528 next_element_from_display_vector (struct it *it)
7529 {
7530 Lisp_Object gc;
7531 int prev_face_id = it->face_id;
7532 int next_face_id;
7533
7534 /* Precondition. */
7535 eassert (it->dpvec && it->current.dpvec_index >= 0);
7536
7537 it->face_id = it->saved_face_id;
7538
7539 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7540 That seemed totally bogus - so I changed it... */
7541 gc = it->dpvec[it->current.dpvec_index];
7542
7543 if (GLYPH_CODE_P (gc))
7544 {
7545 struct face *this_face, *prev_face, *next_face;
7546
7547 it->c = GLYPH_CODE_CHAR (gc);
7548 it->len = CHAR_BYTES (it->c);
7549
7550 /* The entry may contain a face id to use. Such a face id is
7551 the id of a Lisp face, not a realized face. A face id of
7552 zero means no face is specified. */
7553 if (it->dpvec_face_id >= 0)
7554 it->face_id = it->dpvec_face_id;
7555 else
7556 {
7557 int lface_id = GLYPH_CODE_FACE (gc);
7558 if (lface_id > 0)
7559 it->face_id = merge_faces (it->f, Qt, lface_id,
7560 it->saved_face_id);
7561 }
7562
7563 /* Glyphs in the display vector could have the box face, so we
7564 need to set the related flags in the iterator, as
7565 appropriate. */
7566 this_face = FACE_FROM_ID (it->f, it->face_id);
7567 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7568
7569 /* Is this character the first character of a box-face run? */
7570 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7571 && (!prev_face
7572 || prev_face->box == FACE_NO_BOX));
7573
7574 /* For the last character of the box-face run, we need to look
7575 either at the next glyph from the display vector, or at the
7576 face we saw before the display vector. */
7577 next_face_id = it->saved_face_id;
7578 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7579 {
7580 if (it->dpvec_face_id >= 0)
7581 next_face_id = it->dpvec_face_id;
7582 else
7583 {
7584 int lface_id =
7585 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7586
7587 if (lface_id > 0)
7588 next_face_id = merge_faces (it->f, Qt, lface_id,
7589 it->saved_face_id);
7590 }
7591 }
7592 next_face = FACE_FROM_ID (it->f, next_face_id);
7593 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7594 && (!next_face
7595 || next_face->box == FACE_NO_BOX));
7596 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7597 }
7598 else
7599 /* Display table entry is invalid. Return a space. */
7600 it->c = ' ', it->len = 1;
7601
7602 /* Don't change position and object of the iterator here. They are
7603 still the values of the character that had this display table
7604 entry or was translated, and that's what we want. */
7605 it->what = IT_CHARACTER;
7606 return true;
7607 }
7608
7609 /* Get the first element of string/buffer in the visual order, after
7610 being reseated to a new position in a string or a buffer. */
7611 static void
7612 get_visually_first_element (struct it *it)
7613 {
7614 bool string_p = STRINGP (it->string) || it->s;
7615 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7616 ptrdiff_t bob = (string_p ? 0 : BEGV);
7617
7618 if (STRINGP (it->string))
7619 {
7620 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7621 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7622 }
7623 else
7624 {
7625 it->bidi_it.charpos = IT_CHARPOS (*it);
7626 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7627 }
7628
7629 if (it->bidi_it.charpos == eob)
7630 {
7631 /* Nothing to do, but reset the FIRST_ELT flag, like
7632 bidi_paragraph_init does, because we are not going to
7633 call it. */
7634 it->bidi_it.first_elt = false;
7635 }
7636 else if (it->bidi_it.charpos == bob
7637 || (!string_p
7638 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7639 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7640 {
7641 /* If we are at the beginning of a line/string, we can produce
7642 the next element right away. */
7643 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7644 bidi_move_to_visually_next (&it->bidi_it);
7645 }
7646 else
7647 {
7648 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7649
7650 /* We need to prime the bidi iterator starting at the line's or
7651 string's beginning, before we will be able to produce the
7652 next element. */
7653 if (string_p)
7654 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7655 else
7656 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7657 IT_BYTEPOS (*it), -1,
7658 &it->bidi_it.bytepos);
7659 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7660 do
7661 {
7662 /* Now return to buffer/string position where we were asked
7663 to get the next display element, and produce that. */
7664 bidi_move_to_visually_next (&it->bidi_it);
7665 }
7666 while (it->bidi_it.bytepos != orig_bytepos
7667 && it->bidi_it.charpos < eob);
7668 }
7669
7670 /* Adjust IT's position information to where we ended up. */
7671 if (STRINGP (it->string))
7672 {
7673 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7674 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7675 }
7676 else
7677 {
7678 IT_CHARPOS (*it) = it->bidi_it.charpos;
7679 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7680 }
7681
7682 if (STRINGP (it->string) || !it->s)
7683 {
7684 ptrdiff_t stop, charpos, bytepos;
7685
7686 if (STRINGP (it->string))
7687 {
7688 eassert (!it->s);
7689 stop = SCHARS (it->string);
7690 if (stop > it->end_charpos)
7691 stop = it->end_charpos;
7692 charpos = IT_STRING_CHARPOS (*it);
7693 bytepos = IT_STRING_BYTEPOS (*it);
7694 }
7695 else
7696 {
7697 stop = it->end_charpos;
7698 charpos = IT_CHARPOS (*it);
7699 bytepos = IT_BYTEPOS (*it);
7700 }
7701 if (it->bidi_it.scan_dir < 0)
7702 stop = -1;
7703 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7704 it->string);
7705 }
7706 }
7707
7708 /* Load IT with the next display element from Lisp string IT->string.
7709 IT->current.string_pos is the current position within the string.
7710 If IT->current.overlay_string_index >= 0, the Lisp string is an
7711 overlay string. */
7712
7713 static bool
7714 next_element_from_string (struct it *it)
7715 {
7716 struct text_pos position;
7717
7718 eassert (STRINGP (it->string));
7719 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7720 eassert (IT_STRING_CHARPOS (*it) >= 0);
7721 position = it->current.string_pos;
7722
7723 /* With bidi reordering, the character to display might not be the
7724 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7725 that we were reseat()ed to a new string, whose paragraph
7726 direction is not known. */
7727 if (it->bidi_p && it->bidi_it.first_elt)
7728 {
7729 get_visually_first_element (it);
7730 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7731 }
7732
7733 /* Time to check for invisible text? */
7734 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7735 {
7736 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7737 {
7738 if (!(!it->bidi_p
7739 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7740 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7741 {
7742 /* With bidi non-linear iteration, we could find
7743 ourselves far beyond the last computed stop_charpos,
7744 with several other stop positions in between that we
7745 missed. Scan them all now, in buffer's logical
7746 order, until we find and handle the last stop_charpos
7747 that precedes our current position. */
7748 handle_stop_backwards (it, it->stop_charpos);
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7750 }
7751 else
7752 {
7753 if (it->bidi_p)
7754 {
7755 /* Take note of the stop position we just moved
7756 across, for when we will move back across it. */
7757 it->prev_stop = it->stop_charpos;
7758 /* If we are at base paragraph embedding level, take
7759 note of the last stop position seen at this
7760 level. */
7761 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7762 it->base_level_stop = it->stop_charpos;
7763 }
7764 handle_stop (it);
7765
7766 /* Since a handler may have changed IT->method, we must
7767 recurse here. */
7768 return GET_NEXT_DISPLAY_ELEMENT (it);
7769 }
7770 }
7771 else if (it->bidi_p
7772 /* If we are before prev_stop, we may have overstepped
7773 on our way backwards a stop_pos, and if so, we need
7774 to handle that stop_pos. */
7775 && IT_STRING_CHARPOS (*it) < it->prev_stop
7776 /* We can sometimes back up for reasons that have nothing
7777 to do with bidi reordering. E.g., compositions. The
7778 code below is only needed when we are above the base
7779 embedding level, so test for that explicitly. */
7780 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7781 {
7782 /* If we lost track of base_level_stop, we have no better
7783 place for handle_stop_backwards to start from than string
7784 beginning. This happens, e.g., when we were reseated to
7785 the previous screenful of text by vertical-motion. */
7786 if (it->base_level_stop <= 0
7787 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7788 it->base_level_stop = 0;
7789 handle_stop_backwards (it, it->base_level_stop);
7790 return GET_NEXT_DISPLAY_ELEMENT (it);
7791 }
7792 }
7793
7794 if (it->current.overlay_string_index >= 0)
7795 {
7796 /* Get the next character from an overlay string. In overlay
7797 strings, there is no field width or padding with spaces to
7798 do. */
7799 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7800 {
7801 it->what = IT_EOB;
7802 return false;
7803 }
7804 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7805 IT_STRING_BYTEPOS (*it),
7806 it->bidi_it.scan_dir < 0
7807 ? -1
7808 : SCHARS (it->string))
7809 && next_element_from_composition (it))
7810 {
7811 return true;
7812 }
7813 else if (STRING_MULTIBYTE (it->string))
7814 {
7815 const unsigned char *s = (SDATA (it->string)
7816 + IT_STRING_BYTEPOS (*it));
7817 it->c = string_char_and_length (s, &it->len);
7818 }
7819 else
7820 {
7821 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7822 it->len = 1;
7823 }
7824 }
7825 else
7826 {
7827 /* Get the next character from a Lisp string that is not an
7828 overlay string. Such strings come from the mode line, for
7829 example. We may have to pad with spaces, or truncate the
7830 string. See also next_element_from_c_string. */
7831 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7832 {
7833 it->what = IT_EOB;
7834 return false;
7835 }
7836 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7837 {
7838 /* Pad with spaces. */
7839 it->c = ' ', it->len = 1;
7840 CHARPOS (position) = BYTEPOS (position) = -1;
7841 }
7842 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7843 IT_STRING_BYTEPOS (*it),
7844 it->bidi_it.scan_dir < 0
7845 ? -1
7846 : it->string_nchars)
7847 && next_element_from_composition (it))
7848 {
7849 return true;
7850 }
7851 else if (STRING_MULTIBYTE (it->string))
7852 {
7853 const unsigned char *s = (SDATA (it->string)
7854 + IT_STRING_BYTEPOS (*it));
7855 it->c = string_char_and_length (s, &it->len);
7856 }
7857 else
7858 {
7859 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7860 it->len = 1;
7861 }
7862 }
7863
7864 /* Record what we have and where it came from. */
7865 it->what = IT_CHARACTER;
7866 it->object = it->string;
7867 it->position = position;
7868 return true;
7869 }
7870
7871
7872 /* Load IT with next display element from C string IT->s.
7873 IT->string_nchars is the maximum number of characters to return
7874 from the string. IT->end_charpos may be greater than
7875 IT->string_nchars when this function is called, in which case we
7876 may have to return padding spaces. Value is false if end of string
7877 reached, including padding spaces. */
7878
7879 static bool
7880 next_element_from_c_string (struct it *it)
7881 {
7882 bool success_p = true;
7883
7884 eassert (it->s);
7885 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7886 it->what = IT_CHARACTER;
7887 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7888 it->object = make_number (0);
7889
7890 /* With bidi reordering, the character to display might not be the
7891 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7892 we were reseated to a new string, whose paragraph direction is
7893 not known. */
7894 if (it->bidi_p && it->bidi_it.first_elt)
7895 get_visually_first_element (it);
7896
7897 /* IT's position can be greater than IT->string_nchars in case a
7898 field width or precision has been specified when the iterator was
7899 initialized. */
7900 if (IT_CHARPOS (*it) >= it->end_charpos)
7901 {
7902 /* End of the game. */
7903 it->what = IT_EOB;
7904 success_p = false;
7905 }
7906 else if (IT_CHARPOS (*it) >= it->string_nchars)
7907 {
7908 /* Pad with spaces. */
7909 it->c = ' ', it->len = 1;
7910 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7911 }
7912 else if (it->multibyte_p)
7913 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7914 else
7915 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7916
7917 return success_p;
7918 }
7919
7920
7921 /* Set up IT to return characters from an ellipsis, if appropriate.
7922 The definition of the ellipsis glyphs may come from a display table
7923 entry. This function fills IT with the first glyph from the
7924 ellipsis if an ellipsis is to be displayed. */
7925
7926 static bool
7927 next_element_from_ellipsis (struct it *it)
7928 {
7929 if (it->selective_display_ellipsis_p)
7930 setup_for_ellipsis (it, it->len);
7931 else
7932 {
7933 /* The face at the current position may be different from the
7934 face we find after the invisible text. Remember what it
7935 was in IT->saved_face_id, and signal that it's there by
7936 setting face_before_selective_p. */
7937 it->saved_face_id = it->face_id;
7938 it->method = GET_FROM_BUFFER;
7939 it->object = it->w->contents;
7940 reseat_at_next_visible_line_start (it, true);
7941 it->face_before_selective_p = true;
7942 }
7943
7944 return GET_NEXT_DISPLAY_ELEMENT (it);
7945 }
7946
7947
7948 /* Deliver an image display element. The iterator IT is already
7949 filled with image information (done in handle_display_prop). Value
7950 is always true. */
7951
7952
7953 static bool
7954 next_element_from_image (struct it *it)
7955 {
7956 it->what = IT_IMAGE;
7957 return true;
7958 }
7959
7960
7961 /* Fill iterator IT with next display element from a stretch glyph
7962 property. IT->object is the value of the text property. Value is
7963 always true. */
7964
7965 static bool
7966 next_element_from_stretch (struct it *it)
7967 {
7968 it->what = IT_STRETCH;
7969 return true;
7970 }
7971
7972 /* Scan backwards from IT's current position until we find a stop
7973 position, or until BEGV. This is called when we find ourself
7974 before both the last known prev_stop and base_level_stop while
7975 reordering bidirectional text. */
7976
7977 static void
7978 compute_stop_pos_backwards (struct it *it)
7979 {
7980 const int SCAN_BACK_LIMIT = 1000;
7981 struct text_pos pos;
7982 struct display_pos save_current = it->current;
7983 struct text_pos save_position = it->position;
7984 ptrdiff_t charpos = IT_CHARPOS (*it);
7985 ptrdiff_t where_we_are = charpos;
7986 ptrdiff_t save_stop_pos = it->stop_charpos;
7987 ptrdiff_t save_end_pos = it->end_charpos;
7988
7989 eassert (NILP (it->string) && !it->s);
7990 eassert (it->bidi_p);
7991 it->bidi_p = false;
7992 do
7993 {
7994 it->end_charpos = min (charpos + 1, ZV);
7995 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7996 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7997 reseat_1 (it, pos, false);
7998 compute_stop_pos (it);
7999 /* We must advance forward, right? */
8000 if (it->stop_charpos <= charpos)
8001 emacs_abort ();
8002 }
8003 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8004
8005 if (it->stop_charpos <= where_we_are)
8006 it->prev_stop = it->stop_charpos;
8007 else
8008 it->prev_stop = BEGV;
8009 it->bidi_p = true;
8010 it->current = save_current;
8011 it->position = save_position;
8012 it->stop_charpos = save_stop_pos;
8013 it->end_charpos = save_end_pos;
8014 }
8015
8016 /* Scan forward from CHARPOS in the current buffer/string, until we
8017 find a stop position > current IT's position. Then handle the stop
8018 position before that. This is called when we bump into a stop
8019 position while reordering bidirectional text. CHARPOS should be
8020 the last previously processed stop_pos (or BEGV/0, if none were
8021 processed yet) whose position is less that IT's current
8022 position. */
8023
8024 static void
8025 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8026 {
8027 bool bufp = !STRINGP (it->string);
8028 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8029 struct display_pos save_current = it->current;
8030 struct text_pos save_position = it->position;
8031 struct text_pos pos1;
8032 ptrdiff_t next_stop;
8033
8034 /* Scan in strict logical order. */
8035 eassert (it->bidi_p);
8036 it->bidi_p = false;
8037 do
8038 {
8039 it->prev_stop = charpos;
8040 if (bufp)
8041 {
8042 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8043 reseat_1 (it, pos1, false);
8044 }
8045 else
8046 it->current.string_pos = string_pos (charpos, it->string);
8047 compute_stop_pos (it);
8048 /* We must advance forward, right? */
8049 if (it->stop_charpos <= it->prev_stop)
8050 emacs_abort ();
8051 charpos = it->stop_charpos;
8052 }
8053 while (charpos <= where_we_are);
8054
8055 it->bidi_p = true;
8056 it->current = save_current;
8057 it->position = save_position;
8058 next_stop = it->stop_charpos;
8059 it->stop_charpos = it->prev_stop;
8060 handle_stop (it);
8061 it->stop_charpos = next_stop;
8062 }
8063
8064 /* Load IT with the next display element from current_buffer. Value
8065 is false if end of buffer reached. IT->stop_charpos is the next
8066 position at which to stop and check for text properties or buffer
8067 end. */
8068
8069 static bool
8070 next_element_from_buffer (struct it *it)
8071 {
8072 bool success_p = true;
8073
8074 eassert (IT_CHARPOS (*it) >= BEGV);
8075 eassert (NILP (it->string) && !it->s);
8076 eassert (!it->bidi_p
8077 || (EQ (it->bidi_it.string.lstring, Qnil)
8078 && it->bidi_it.string.s == NULL));
8079
8080 /* With bidi reordering, the character to display might not be the
8081 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8082 we were reseat()ed to a new buffer position, which is potentially
8083 a different paragraph. */
8084 if (it->bidi_p && it->bidi_it.first_elt)
8085 {
8086 get_visually_first_element (it);
8087 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8088 }
8089
8090 if (IT_CHARPOS (*it) >= it->stop_charpos)
8091 {
8092 if (IT_CHARPOS (*it) >= it->end_charpos)
8093 {
8094 bool overlay_strings_follow_p;
8095
8096 /* End of the game, except when overlay strings follow that
8097 haven't been returned yet. */
8098 if (it->overlay_strings_at_end_processed_p)
8099 overlay_strings_follow_p = false;
8100 else
8101 {
8102 it->overlay_strings_at_end_processed_p = true;
8103 overlay_strings_follow_p = get_overlay_strings (it, 0);
8104 }
8105
8106 if (overlay_strings_follow_p)
8107 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8108 else
8109 {
8110 it->what = IT_EOB;
8111 it->position = it->current.pos;
8112 success_p = false;
8113 }
8114 }
8115 else if (!(!it->bidi_p
8116 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8117 || IT_CHARPOS (*it) == it->stop_charpos))
8118 {
8119 /* With bidi non-linear iteration, we could find ourselves
8120 far beyond the last computed stop_charpos, with several
8121 other stop positions in between that we missed. Scan
8122 them all now, in buffer's logical order, until we find
8123 and handle the last stop_charpos that precedes our
8124 current position. */
8125 handle_stop_backwards (it, it->stop_charpos);
8126 it->ignore_overlay_strings_at_pos_p = false;
8127 return GET_NEXT_DISPLAY_ELEMENT (it);
8128 }
8129 else
8130 {
8131 if (it->bidi_p)
8132 {
8133 /* Take note of the stop position we just moved across,
8134 for when we will move back across it. */
8135 it->prev_stop = it->stop_charpos;
8136 /* If we are at base paragraph embedding level, take
8137 note of the last stop position seen at this
8138 level. */
8139 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8140 it->base_level_stop = it->stop_charpos;
8141 }
8142 handle_stop (it);
8143 it->ignore_overlay_strings_at_pos_p = false;
8144 return GET_NEXT_DISPLAY_ELEMENT (it);
8145 }
8146 }
8147 else if (it->bidi_p
8148 /* If we are before prev_stop, we may have overstepped on
8149 our way backwards a stop_pos, and if so, we need to
8150 handle that stop_pos. */
8151 && IT_CHARPOS (*it) < it->prev_stop
8152 /* We can sometimes back up for reasons that have nothing
8153 to do with bidi reordering. E.g., compositions. The
8154 code below is only needed when we are above the base
8155 embedding level, so test for that explicitly. */
8156 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8157 {
8158 if (it->base_level_stop <= 0
8159 || IT_CHARPOS (*it) < it->base_level_stop)
8160 {
8161 /* If we lost track of base_level_stop, we need to find
8162 prev_stop by looking backwards. This happens, e.g., when
8163 we were reseated to the previous screenful of text by
8164 vertical-motion. */
8165 it->base_level_stop = BEGV;
8166 compute_stop_pos_backwards (it);
8167 handle_stop_backwards (it, it->prev_stop);
8168 }
8169 else
8170 handle_stop_backwards (it, it->base_level_stop);
8171 it->ignore_overlay_strings_at_pos_p = false;
8172 return GET_NEXT_DISPLAY_ELEMENT (it);
8173 }
8174 else
8175 {
8176 /* No face changes, overlays etc. in sight, so just return a
8177 character from current_buffer. */
8178 unsigned char *p;
8179 ptrdiff_t stop;
8180
8181 /* We moved to the next buffer position, so any info about
8182 previously seen overlays is no longer valid. */
8183 it->ignore_overlay_strings_at_pos_p = false;
8184
8185 /* Maybe run the redisplay end trigger hook. Performance note:
8186 This doesn't seem to cost measurable time. */
8187 if (it->redisplay_end_trigger_charpos
8188 && it->glyph_row
8189 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8190 run_redisplay_end_trigger_hook (it);
8191
8192 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8193 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8194 stop)
8195 && next_element_from_composition (it))
8196 {
8197 return true;
8198 }
8199
8200 /* Get the next character, maybe multibyte. */
8201 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8202 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8203 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8204 else
8205 it->c = *p, it->len = 1;
8206
8207 /* Record what we have and where it came from. */
8208 it->what = IT_CHARACTER;
8209 it->object = it->w->contents;
8210 it->position = it->current.pos;
8211
8212 /* Normally we return the character found above, except when we
8213 really want to return an ellipsis for selective display. */
8214 if (it->selective)
8215 {
8216 if (it->c == '\n')
8217 {
8218 /* A value of selective > 0 means hide lines indented more
8219 than that number of columns. */
8220 if (it->selective > 0
8221 && IT_CHARPOS (*it) + 1 < ZV
8222 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8223 IT_BYTEPOS (*it) + 1,
8224 it->selective))
8225 {
8226 success_p = next_element_from_ellipsis (it);
8227 it->dpvec_char_len = -1;
8228 }
8229 }
8230 else if (it->c == '\r' && it->selective == -1)
8231 {
8232 /* A value of selective == -1 means that everything from the
8233 CR to the end of the line is invisible, with maybe an
8234 ellipsis displayed for it. */
8235 success_p = next_element_from_ellipsis (it);
8236 it->dpvec_char_len = -1;
8237 }
8238 }
8239 }
8240
8241 /* Value is false if end of buffer reached. */
8242 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8243 return success_p;
8244 }
8245
8246
8247 /* Run the redisplay end trigger hook for IT. */
8248
8249 static void
8250 run_redisplay_end_trigger_hook (struct it *it)
8251 {
8252 /* IT->glyph_row should be non-null, i.e. we should be actually
8253 displaying something, or otherwise we should not run the hook. */
8254 eassert (it->glyph_row);
8255
8256 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8257 it->redisplay_end_trigger_charpos = 0;
8258
8259 /* Since we are *trying* to run these functions, don't try to run
8260 them again, even if they get an error. */
8261 wset_redisplay_end_trigger (it->w, Qnil);
8262 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8263 make_number (charpos));
8264
8265 /* Notice if it changed the face of the character we are on. */
8266 handle_face_prop (it);
8267 }
8268
8269
8270 /* Deliver a composition display element. Unlike the other
8271 next_element_from_XXX, this function is not registered in the array
8272 get_next_element[]. It is called from next_element_from_buffer and
8273 next_element_from_string when necessary. */
8274
8275 static bool
8276 next_element_from_composition (struct it *it)
8277 {
8278 it->what = IT_COMPOSITION;
8279 it->len = it->cmp_it.nbytes;
8280 if (STRINGP (it->string))
8281 {
8282 if (it->c < 0)
8283 {
8284 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8285 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8286 return false;
8287 }
8288 it->position = it->current.string_pos;
8289 it->object = it->string;
8290 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8291 IT_STRING_BYTEPOS (*it), it->string);
8292 }
8293 else
8294 {
8295 if (it->c < 0)
8296 {
8297 IT_CHARPOS (*it) += it->cmp_it.nchars;
8298 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8299 if (it->bidi_p)
8300 {
8301 if (it->bidi_it.new_paragraph)
8302 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8303 false);
8304 /* Resync the bidi iterator with IT's new position.
8305 FIXME: this doesn't support bidirectional text. */
8306 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8307 bidi_move_to_visually_next (&it->bidi_it);
8308 }
8309 return false;
8310 }
8311 it->position = it->current.pos;
8312 it->object = it->w->contents;
8313 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8314 IT_BYTEPOS (*it), Qnil);
8315 }
8316 return true;
8317 }
8318
8319
8320 \f
8321 /***********************************************************************
8322 Moving an iterator without producing glyphs
8323 ***********************************************************************/
8324
8325 /* Check if iterator is at a position corresponding to a valid buffer
8326 position after some move_it_ call. */
8327
8328 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8329 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8330
8331
8332 /* Move iterator IT to a specified buffer or X position within one
8333 line on the display without producing glyphs.
8334
8335 OP should be a bit mask including some or all of these bits:
8336 MOVE_TO_X: Stop upon reaching x-position TO_X.
8337 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8338 Regardless of OP's value, stop upon reaching the end of the display line.
8339
8340 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8341 This means, in particular, that TO_X includes window's horizontal
8342 scroll amount.
8343
8344 The return value has several possible values that
8345 say what condition caused the scan to stop:
8346
8347 MOVE_POS_MATCH_OR_ZV
8348 - when TO_POS or ZV was reached.
8349
8350 MOVE_X_REACHED
8351 -when TO_X was reached before TO_POS or ZV were reached.
8352
8353 MOVE_LINE_CONTINUED
8354 - when we reached the end of the display area and the line must
8355 be continued.
8356
8357 MOVE_LINE_TRUNCATED
8358 - when we reached the end of the display area and the line is
8359 truncated.
8360
8361 MOVE_NEWLINE_OR_CR
8362 - when we stopped at a line end, i.e. a newline or a CR and selective
8363 display is on. */
8364
8365 static enum move_it_result
8366 move_it_in_display_line_to (struct it *it,
8367 ptrdiff_t to_charpos, int to_x,
8368 enum move_operation_enum op)
8369 {
8370 enum move_it_result result = MOVE_UNDEFINED;
8371 struct glyph_row *saved_glyph_row;
8372 struct it wrap_it, atpos_it, atx_it, ppos_it;
8373 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8374 void *ppos_data = NULL;
8375 bool may_wrap = false;
8376 enum it_method prev_method = it->method;
8377 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8378 bool saw_smaller_pos = prev_pos < to_charpos;
8379
8380 /* Don't produce glyphs in produce_glyphs. */
8381 saved_glyph_row = it->glyph_row;
8382 it->glyph_row = NULL;
8383
8384 /* Use wrap_it to save a copy of IT wherever a word wrap could
8385 occur. Use atpos_it to save a copy of IT at the desired buffer
8386 position, if found, so that we can scan ahead and check if the
8387 word later overshoots the window edge. Use atx_it similarly, for
8388 pixel positions. */
8389 wrap_it.sp = -1;
8390 atpos_it.sp = -1;
8391 atx_it.sp = -1;
8392
8393 /* Use ppos_it under bidi reordering to save a copy of IT for the
8394 initial position. We restore that position in IT when we have
8395 scanned the entire display line without finding a match for
8396 TO_CHARPOS and all the character positions are greater than
8397 TO_CHARPOS. We then restart the scan from the initial position,
8398 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8399 the closest to TO_CHARPOS. */
8400 if (it->bidi_p)
8401 {
8402 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8403 {
8404 SAVE_IT (ppos_it, *it, ppos_data);
8405 closest_pos = IT_CHARPOS (*it);
8406 }
8407 else
8408 closest_pos = ZV;
8409 }
8410
8411 #define BUFFER_POS_REACHED_P() \
8412 ((op & MOVE_TO_POS) != 0 \
8413 && BUFFERP (it->object) \
8414 && (IT_CHARPOS (*it) == to_charpos \
8415 || ((!it->bidi_p \
8416 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8417 && IT_CHARPOS (*it) > to_charpos) \
8418 || (it->what == IT_COMPOSITION \
8419 && ((IT_CHARPOS (*it) > to_charpos \
8420 && to_charpos >= it->cmp_it.charpos) \
8421 || (IT_CHARPOS (*it) < to_charpos \
8422 && to_charpos <= it->cmp_it.charpos)))) \
8423 && (it->method == GET_FROM_BUFFER \
8424 || (it->method == GET_FROM_DISPLAY_VECTOR \
8425 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8426
8427 /* If there's a line-/wrap-prefix, handle it. */
8428 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8429 && it->current_y < it->last_visible_y)
8430 handle_line_prefix (it);
8431
8432 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8433 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8434
8435 while (true)
8436 {
8437 int x, i, ascent = 0, descent = 0;
8438
8439 /* Utility macro to reset an iterator with x, ascent, and descent. */
8440 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8441 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8442 (IT)->max_descent = descent)
8443
8444 /* Stop if we move beyond TO_CHARPOS (after an image or a
8445 display string or stretch glyph). */
8446 if ((op & MOVE_TO_POS) != 0
8447 && BUFFERP (it->object)
8448 && it->method == GET_FROM_BUFFER
8449 && (((!it->bidi_p
8450 /* When the iterator is at base embedding level, we
8451 are guaranteed that characters are delivered for
8452 display in strictly increasing order of their
8453 buffer positions. */
8454 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8455 && IT_CHARPOS (*it) > to_charpos)
8456 || (it->bidi_p
8457 && (prev_method == GET_FROM_IMAGE
8458 || prev_method == GET_FROM_STRETCH
8459 || prev_method == GET_FROM_STRING)
8460 /* Passed TO_CHARPOS from left to right. */
8461 && ((prev_pos < to_charpos
8462 && IT_CHARPOS (*it) > to_charpos)
8463 /* Passed TO_CHARPOS from right to left. */
8464 || (prev_pos > to_charpos
8465 && IT_CHARPOS (*it) < to_charpos)))))
8466 {
8467 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8468 {
8469 result = MOVE_POS_MATCH_OR_ZV;
8470 break;
8471 }
8472 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8473 /* If wrap_it is valid, the current position might be in a
8474 word that is wrapped. So, save the iterator in
8475 atpos_it and continue to see if wrapping happens. */
8476 SAVE_IT (atpos_it, *it, atpos_data);
8477 }
8478
8479 /* Stop when ZV reached.
8480 We used to stop here when TO_CHARPOS reached as well, but that is
8481 too soon if this glyph does not fit on this line. So we handle it
8482 explicitly below. */
8483 if (!get_next_display_element (it))
8484 {
8485 result = MOVE_POS_MATCH_OR_ZV;
8486 break;
8487 }
8488
8489 if (it->line_wrap == TRUNCATE)
8490 {
8491 if (BUFFER_POS_REACHED_P ())
8492 {
8493 result = MOVE_POS_MATCH_OR_ZV;
8494 break;
8495 }
8496 }
8497 else
8498 {
8499 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8500 {
8501 if (IT_DISPLAYING_WHITESPACE (it))
8502 may_wrap = true;
8503 else if (may_wrap)
8504 {
8505 /* We have reached a glyph that follows one or more
8506 whitespace characters. If the position is
8507 already found, we are done. */
8508 if (atpos_it.sp >= 0)
8509 {
8510 RESTORE_IT (it, &atpos_it, atpos_data);
8511 result = MOVE_POS_MATCH_OR_ZV;
8512 goto done;
8513 }
8514 if (atx_it.sp >= 0)
8515 {
8516 RESTORE_IT (it, &atx_it, atx_data);
8517 result = MOVE_X_REACHED;
8518 goto done;
8519 }
8520 /* Otherwise, we can wrap here. */
8521 SAVE_IT (wrap_it, *it, wrap_data);
8522 may_wrap = false;
8523 }
8524 }
8525 }
8526
8527 /* Remember the line height for the current line, in case
8528 the next element doesn't fit on the line. */
8529 ascent = it->max_ascent;
8530 descent = it->max_descent;
8531
8532 /* The call to produce_glyphs will get the metrics of the
8533 display element IT is loaded with. Record the x-position
8534 before this display element, in case it doesn't fit on the
8535 line. */
8536 x = it->current_x;
8537
8538 PRODUCE_GLYPHS (it);
8539
8540 if (it->area != TEXT_AREA)
8541 {
8542 prev_method = it->method;
8543 if (it->method == GET_FROM_BUFFER)
8544 prev_pos = IT_CHARPOS (*it);
8545 set_iterator_to_next (it, true);
8546 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8547 SET_TEXT_POS (this_line_min_pos,
8548 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8549 if (it->bidi_p
8550 && (op & MOVE_TO_POS)
8551 && IT_CHARPOS (*it) > to_charpos
8552 && IT_CHARPOS (*it) < closest_pos)
8553 closest_pos = IT_CHARPOS (*it);
8554 continue;
8555 }
8556
8557 /* The number of glyphs we get back in IT->nglyphs will normally
8558 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8559 character on a terminal frame, or (iii) a line end. For the
8560 second case, IT->nglyphs - 1 padding glyphs will be present.
8561 (On X frames, there is only one glyph produced for a
8562 composite character.)
8563
8564 The behavior implemented below means, for continuation lines,
8565 that as many spaces of a TAB as fit on the current line are
8566 displayed there. For terminal frames, as many glyphs of a
8567 multi-glyph character are displayed in the current line, too.
8568 This is what the old redisplay code did, and we keep it that
8569 way. Under X, the whole shape of a complex character must
8570 fit on the line or it will be completely displayed in the
8571 next line.
8572
8573 Note that both for tabs and padding glyphs, all glyphs have
8574 the same width. */
8575 if (it->nglyphs)
8576 {
8577 /* More than one glyph or glyph doesn't fit on line. All
8578 glyphs have the same width. */
8579 int single_glyph_width = it->pixel_width / it->nglyphs;
8580 int new_x;
8581 int x_before_this_char = x;
8582 int hpos_before_this_char = it->hpos;
8583
8584 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8585 {
8586 new_x = x + single_glyph_width;
8587
8588 /* We want to leave anything reaching TO_X to the caller. */
8589 if ((op & MOVE_TO_X) && new_x > to_x)
8590 {
8591 if (BUFFER_POS_REACHED_P ())
8592 {
8593 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8594 goto buffer_pos_reached;
8595 if (atpos_it.sp < 0)
8596 {
8597 SAVE_IT (atpos_it, *it, atpos_data);
8598 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8599 }
8600 }
8601 else
8602 {
8603 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8604 {
8605 it->current_x = x;
8606 result = MOVE_X_REACHED;
8607 break;
8608 }
8609 if (atx_it.sp < 0)
8610 {
8611 SAVE_IT (atx_it, *it, atx_data);
8612 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8613 }
8614 }
8615 }
8616
8617 if (/* Lines are continued. */
8618 it->line_wrap != TRUNCATE
8619 && (/* And glyph doesn't fit on the line. */
8620 new_x > it->last_visible_x
8621 /* Or it fits exactly and we're on a window
8622 system frame. */
8623 || (new_x == it->last_visible_x
8624 && FRAME_WINDOW_P (it->f)
8625 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8626 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8627 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8628 {
8629 if (/* IT->hpos == 0 means the very first glyph
8630 doesn't fit on the line, e.g. a wide image. */
8631 it->hpos == 0
8632 || (new_x == it->last_visible_x
8633 && FRAME_WINDOW_P (it->f)))
8634 {
8635 ++it->hpos;
8636 it->current_x = new_x;
8637
8638 /* The character's last glyph just barely fits
8639 in this row. */
8640 if (i == it->nglyphs - 1)
8641 {
8642 /* If this is the destination position,
8643 return a position *before* it in this row,
8644 now that we know it fits in this row. */
8645 if (BUFFER_POS_REACHED_P ())
8646 {
8647 if (it->line_wrap != WORD_WRAP
8648 || wrap_it.sp < 0
8649 /* If we've just found whitespace to
8650 wrap, effectively ignore the
8651 previous wrap point -- it is no
8652 longer relevant, but we won't
8653 have an opportunity to update it,
8654 since we've reached the edge of
8655 this screen line. */
8656 || (may_wrap
8657 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8658 {
8659 it->hpos = hpos_before_this_char;
8660 it->current_x = x_before_this_char;
8661 result = MOVE_POS_MATCH_OR_ZV;
8662 break;
8663 }
8664 if (it->line_wrap == WORD_WRAP
8665 && atpos_it.sp < 0)
8666 {
8667 SAVE_IT (atpos_it, *it, atpos_data);
8668 atpos_it.current_x = x_before_this_char;
8669 atpos_it.hpos = hpos_before_this_char;
8670 }
8671 }
8672
8673 prev_method = it->method;
8674 if (it->method == GET_FROM_BUFFER)
8675 prev_pos = IT_CHARPOS (*it);
8676 set_iterator_to_next (it, true);
8677 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8678 SET_TEXT_POS (this_line_min_pos,
8679 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8680 /* On graphical terminals, newlines may
8681 "overflow" into the fringe if
8682 overflow-newline-into-fringe is non-nil.
8683 On text terminals, and on graphical
8684 terminals with no right margin, newlines
8685 may overflow into the last glyph on the
8686 display line.*/
8687 if (!FRAME_WINDOW_P (it->f)
8688 || ((it->bidi_p
8689 && it->bidi_it.paragraph_dir == R2L)
8690 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8691 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8692 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8693 {
8694 if (!get_next_display_element (it))
8695 {
8696 result = MOVE_POS_MATCH_OR_ZV;
8697 break;
8698 }
8699 if (BUFFER_POS_REACHED_P ())
8700 {
8701 if (ITERATOR_AT_END_OF_LINE_P (it))
8702 result = MOVE_POS_MATCH_OR_ZV;
8703 else
8704 result = MOVE_LINE_CONTINUED;
8705 break;
8706 }
8707 if (ITERATOR_AT_END_OF_LINE_P (it)
8708 && (it->line_wrap != WORD_WRAP
8709 || wrap_it.sp < 0
8710 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8711 {
8712 result = MOVE_NEWLINE_OR_CR;
8713 break;
8714 }
8715 }
8716 }
8717 }
8718 else
8719 IT_RESET_X_ASCENT_DESCENT (it);
8720
8721 /* If the screen line ends with whitespace, and we
8722 are under word-wrap, don't use wrap_it: it is no
8723 longer relevant, but we won't have an opportunity
8724 to update it, since we are done with this screen
8725 line. */
8726 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8727 {
8728 /* If we've found TO_X, go back there, as we now
8729 know the last word fits on this screen line. */
8730 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8731 && atx_it.sp >= 0)
8732 {
8733 RESTORE_IT (it, &atx_it, atx_data);
8734 atpos_it.sp = -1;
8735 atx_it.sp = -1;
8736 result = MOVE_X_REACHED;
8737 break;
8738 }
8739 }
8740 else if (wrap_it.sp >= 0)
8741 {
8742 RESTORE_IT (it, &wrap_it, wrap_data);
8743 atpos_it.sp = -1;
8744 atx_it.sp = -1;
8745 }
8746
8747 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8748 IT_CHARPOS (*it)));
8749 result = MOVE_LINE_CONTINUED;
8750 break;
8751 }
8752
8753 if (BUFFER_POS_REACHED_P ())
8754 {
8755 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8756 goto buffer_pos_reached;
8757 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8758 {
8759 SAVE_IT (atpos_it, *it, atpos_data);
8760 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8761 }
8762 }
8763
8764 if (new_x > it->first_visible_x)
8765 {
8766 /* Glyph is visible. Increment number of glyphs that
8767 would be displayed. */
8768 ++it->hpos;
8769 }
8770 }
8771
8772 if (result != MOVE_UNDEFINED)
8773 break;
8774 }
8775 else if (BUFFER_POS_REACHED_P ())
8776 {
8777 buffer_pos_reached:
8778 IT_RESET_X_ASCENT_DESCENT (it);
8779 result = MOVE_POS_MATCH_OR_ZV;
8780 break;
8781 }
8782 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8783 {
8784 /* Stop when TO_X specified and reached. This check is
8785 necessary here because of lines consisting of a line end,
8786 only. The line end will not produce any glyphs and we
8787 would never get MOVE_X_REACHED. */
8788 eassert (it->nglyphs == 0);
8789 result = MOVE_X_REACHED;
8790 break;
8791 }
8792
8793 /* Is this a line end? If yes, we're done. */
8794 if (ITERATOR_AT_END_OF_LINE_P (it))
8795 {
8796 /* If we are past TO_CHARPOS, but never saw any character
8797 positions smaller than TO_CHARPOS, return
8798 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8799 did. */
8800 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8801 {
8802 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8803 {
8804 if (closest_pos < ZV)
8805 {
8806 RESTORE_IT (it, &ppos_it, ppos_data);
8807 /* Don't recurse if closest_pos is equal to
8808 to_charpos, since we have just tried that. */
8809 if (closest_pos != to_charpos)
8810 move_it_in_display_line_to (it, closest_pos, -1,
8811 MOVE_TO_POS);
8812 result = MOVE_POS_MATCH_OR_ZV;
8813 }
8814 else
8815 goto buffer_pos_reached;
8816 }
8817 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8818 && IT_CHARPOS (*it) > to_charpos)
8819 goto buffer_pos_reached;
8820 else
8821 result = MOVE_NEWLINE_OR_CR;
8822 }
8823 else
8824 result = MOVE_NEWLINE_OR_CR;
8825 break;
8826 }
8827
8828 prev_method = it->method;
8829 if (it->method == GET_FROM_BUFFER)
8830 prev_pos = IT_CHARPOS (*it);
8831 /* The current display element has been consumed. Advance
8832 to the next. */
8833 set_iterator_to_next (it, true);
8834 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8835 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8836 if (IT_CHARPOS (*it) < to_charpos)
8837 saw_smaller_pos = true;
8838 if (it->bidi_p
8839 && (op & MOVE_TO_POS)
8840 && IT_CHARPOS (*it) >= to_charpos
8841 && IT_CHARPOS (*it) < closest_pos)
8842 closest_pos = IT_CHARPOS (*it);
8843
8844 /* Stop if lines are truncated and IT's current x-position is
8845 past the right edge of the window now. */
8846 if (it->line_wrap == TRUNCATE
8847 && it->current_x >= it->last_visible_x)
8848 {
8849 if (!FRAME_WINDOW_P (it->f)
8850 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8851 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8852 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8853 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8854 {
8855 bool at_eob_p = false;
8856
8857 if ((at_eob_p = !get_next_display_element (it))
8858 || BUFFER_POS_REACHED_P ()
8859 /* If we are past TO_CHARPOS, but never saw any
8860 character positions smaller than TO_CHARPOS,
8861 return MOVE_POS_MATCH_OR_ZV, like the
8862 unidirectional display did. */
8863 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8864 && !saw_smaller_pos
8865 && IT_CHARPOS (*it) > to_charpos))
8866 {
8867 if (it->bidi_p
8868 && !BUFFER_POS_REACHED_P ()
8869 && !at_eob_p && closest_pos < ZV)
8870 {
8871 RESTORE_IT (it, &ppos_it, ppos_data);
8872 if (closest_pos != to_charpos)
8873 move_it_in_display_line_to (it, closest_pos, -1,
8874 MOVE_TO_POS);
8875 }
8876 result = MOVE_POS_MATCH_OR_ZV;
8877 break;
8878 }
8879 if (ITERATOR_AT_END_OF_LINE_P (it))
8880 {
8881 result = MOVE_NEWLINE_OR_CR;
8882 break;
8883 }
8884 }
8885 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8886 && !saw_smaller_pos
8887 && IT_CHARPOS (*it) > to_charpos)
8888 {
8889 if (closest_pos < ZV)
8890 {
8891 RESTORE_IT (it, &ppos_it, ppos_data);
8892 if (closest_pos != to_charpos)
8893 move_it_in_display_line_to (it, closest_pos, -1,
8894 MOVE_TO_POS);
8895 }
8896 result = MOVE_POS_MATCH_OR_ZV;
8897 break;
8898 }
8899 result = MOVE_LINE_TRUNCATED;
8900 break;
8901 }
8902 #undef IT_RESET_X_ASCENT_DESCENT
8903 }
8904
8905 #undef BUFFER_POS_REACHED_P
8906
8907 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8908 restore the saved iterator. */
8909 if (atpos_it.sp >= 0)
8910 RESTORE_IT (it, &atpos_it, atpos_data);
8911 else if (atx_it.sp >= 0)
8912 RESTORE_IT (it, &atx_it, atx_data);
8913
8914 done:
8915
8916 if (atpos_data)
8917 bidi_unshelve_cache (atpos_data, true);
8918 if (atx_data)
8919 bidi_unshelve_cache (atx_data, true);
8920 if (wrap_data)
8921 bidi_unshelve_cache (wrap_data, true);
8922 if (ppos_data)
8923 bidi_unshelve_cache (ppos_data, true);
8924
8925 /* Restore the iterator settings altered at the beginning of this
8926 function. */
8927 it->glyph_row = saved_glyph_row;
8928 return result;
8929 }
8930
8931 /* For external use. */
8932 void
8933 move_it_in_display_line (struct it *it,
8934 ptrdiff_t to_charpos, int to_x,
8935 enum move_operation_enum op)
8936 {
8937 if (it->line_wrap == WORD_WRAP
8938 && (op & MOVE_TO_X))
8939 {
8940 struct it save_it;
8941 void *save_data = NULL;
8942 int skip;
8943
8944 SAVE_IT (save_it, *it, save_data);
8945 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8946 /* When word-wrap is on, TO_X may lie past the end
8947 of a wrapped line. Then it->current is the
8948 character on the next line, so backtrack to the
8949 space before the wrap point. */
8950 if (skip == MOVE_LINE_CONTINUED)
8951 {
8952 int prev_x = max (it->current_x - 1, 0);
8953 RESTORE_IT (it, &save_it, save_data);
8954 move_it_in_display_line_to
8955 (it, -1, prev_x, MOVE_TO_X);
8956 }
8957 else
8958 bidi_unshelve_cache (save_data, true);
8959 }
8960 else
8961 move_it_in_display_line_to (it, to_charpos, to_x, op);
8962 }
8963
8964
8965 /* Move IT forward until it satisfies one or more of the criteria in
8966 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8967
8968 OP is a bit-mask that specifies where to stop, and in particular,
8969 which of those four position arguments makes a difference. See the
8970 description of enum move_operation_enum.
8971
8972 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8973 screen line, this function will set IT to the next position that is
8974 displayed to the right of TO_CHARPOS on the screen.
8975
8976 Return the maximum pixel length of any line scanned but never more
8977 than it.last_visible_x. */
8978
8979 int
8980 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8981 {
8982 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8983 int line_height, line_start_x = 0, reached = 0;
8984 int max_current_x = 0;
8985 void *backup_data = NULL;
8986
8987 for (;;)
8988 {
8989 if (op & MOVE_TO_VPOS)
8990 {
8991 /* If no TO_CHARPOS and no TO_X specified, stop at the
8992 start of the line TO_VPOS. */
8993 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8994 {
8995 if (it->vpos == to_vpos)
8996 {
8997 reached = 1;
8998 break;
8999 }
9000 else
9001 skip = move_it_in_display_line_to (it, -1, -1, 0);
9002 }
9003 else
9004 {
9005 /* TO_VPOS >= 0 means stop at TO_X in the line at
9006 TO_VPOS, or at TO_POS, whichever comes first. */
9007 if (it->vpos == to_vpos)
9008 {
9009 reached = 2;
9010 break;
9011 }
9012
9013 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9014
9015 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9016 {
9017 reached = 3;
9018 break;
9019 }
9020 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9021 {
9022 /* We have reached TO_X but not in the line we want. */
9023 skip = move_it_in_display_line_to (it, to_charpos,
9024 -1, MOVE_TO_POS);
9025 if (skip == MOVE_POS_MATCH_OR_ZV)
9026 {
9027 reached = 4;
9028 break;
9029 }
9030 }
9031 }
9032 }
9033 else if (op & MOVE_TO_Y)
9034 {
9035 struct it it_backup;
9036
9037 if (it->line_wrap == WORD_WRAP)
9038 SAVE_IT (it_backup, *it, backup_data);
9039
9040 /* TO_Y specified means stop at TO_X in the line containing
9041 TO_Y---or at TO_CHARPOS if this is reached first. The
9042 problem is that we can't really tell whether the line
9043 contains TO_Y before we have completely scanned it, and
9044 this may skip past TO_X. What we do is to first scan to
9045 TO_X.
9046
9047 If TO_X is not specified, use a TO_X of zero. The reason
9048 is to make the outcome of this function more predictable.
9049 If we didn't use TO_X == 0, we would stop at the end of
9050 the line which is probably not what a caller would expect
9051 to happen. */
9052 skip = move_it_in_display_line_to
9053 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9054 (MOVE_TO_X | (op & MOVE_TO_POS)));
9055
9056 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9057 if (skip == MOVE_POS_MATCH_OR_ZV)
9058 reached = 5;
9059 else if (skip == MOVE_X_REACHED)
9060 {
9061 /* If TO_X was reached, we want to know whether TO_Y is
9062 in the line. We know this is the case if the already
9063 scanned glyphs make the line tall enough. Otherwise,
9064 we must check by scanning the rest of the line. */
9065 line_height = it->max_ascent + it->max_descent;
9066 if (to_y >= it->current_y
9067 && to_y < it->current_y + line_height)
9068 {
9069 reached = 6;
9070 break;
9071 }
9072 SAVE_IT (it_backup, *it, backup_data);
9073 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9074 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9075 op & MOVE_TO_POS);
9076 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9077 line_height = it->max_ascent + it->max_descent;
9078 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9079
9080 if (to_y >= it->current_y
9081 && to_y < it->current_y + line_height)
9082 {
9083 /* If TO_Y is in this line and TO_X was reached
9084 above, we scanned too far. We have to restore
9085 IT's settings to the ones before skipping. But
9086 keep the more accurate values of max_ascent and
9087 max_descent we've found while skipping the rest
9088 of the line, for the sake of callers, such as
9089 pos_visible_p, that need to know the line
9090 height. */
9091 int max_ascent = it->max_ascent;
9092 int max_descent = it->max_descent;
9093
9094 RESTORE_IT (it, &it_backup, backup_data);
9095 it->max_ascent = max_ascent;
9096 it->max_descent = max_descent;
9097 reached = 6;
9098 }
9099 else
9100 {
9101 skip = skip2;
9102 if (skip == MOVE_POS_MATCH_OR_ZV)
9103 reached = 7;
9104 }
9105 }
9106 else
9107 {
9108 /* Check whether TO_Y is in this line. */
9109 line_height = it->max_ascent + it->max_descent;
9110 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9111
9112 if (to_y >= it->current_y
9113 && to_y < it->current_y + line_height)
9114 {
9115 if (to_y > it->current_y)
9116 max_current_x = max (it->current_x, max_current_x);
9117
9118 /* When word-wrap is on, TO_X may lie past the end
9119 of a wrapped line. Then it->current is the
9120 character on the next line, so backtrack to the
9121 space before the wrap point. */
9122 if (skip == MOVE_LINE_CONTINUED
9123 && it->line_wrap == WORD_WRAP)
9124 {
9125 int prev_x = max (it->current_x - 1, 0);
9126 RESTORE_IT (it, &it_backup, backup_data);
9127 skip = move_it_in_display_line_to
9128 (it, -1, prev_x, MOVE_TO_X);
9129 }
9130
9131 reached = 6;
9132 }
9133 }
9134
9135 if (reached)
9136 {
9137 max_current_x = max (it->current_x, max_current_x);
9138 break;
9139 }
9140 }
9141 else if (BUFFERP (it->object)
9142 && (it->method == GET_FROM_BUFFER
9143 || it->method == GET_FROM_STRETCH)
9144 && IT_CHARPOS (*it) >= to_charpos
9145 /* Under bidi iteration, a call to set_iterator_to_next
9146 can scan far beyond to_charpos if the initial
9147 portion of the next line needs to be reordered. In
9148 that case, give move_it_in_display_line_to another
9149 chance below. */
9150 && !(it->bidi_p
9151 && it->bidi_it.scan_dir == -1))
9152 skip = MOVE_POS_MATCH_OR_ZV;
9153 else
9154 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9155
9156 switch (skip)
9157 {
9158 case MOVE_POS_MATCH_OR_ZV:
9159 max_current_x = max (it->current_x, max_current_x);
9160 reached = 8;
9161 goto out;
9162
9163 case MOVE_NEWLINE_OR_CR:
9164 max_current_x = max (it->current_x, max_current_x);
9165 set_iterator_to_next (it, true);
9166 it->continuation_lines_width = 0;
9167 break;
9168
9169 case MOVE_LINE_TRUNCATED:
9170 max_current_x = it->last_visible_x;
9171 it->continuation_lines_width = 0;
9172 reseat_at_next_visible_line_start (it, false);
9173 if ((op & MOVE_TO_POS) != 0
9174 && IT_CHARPOS (*it) > to_charpos)
9175 {
9176 reached = 9;
9177 goto out;
9178 }
9179 break;
9180
9181 case MOVE_LINE_CONTINUED:
9182 max_current_x = it->last_visible_x;
9183 /* For continued lines ending in a tab, some of the glyphs
9184 associated with the tab are displayed on the current
9185 line. Since it->current_x does not include these glyphs,
9186 we use it->last_visible_x instead. */
9187 if (it->c == '\t')
9188 {
9189 it->continuation_lines_width += it->last_visible_x;
9190 /* When moving by vpos, ensure that the iterator really
9191 advances to the next line (bug#847, bug#969). Fixme:
9192 do we need to do this in other circumstances? */
9193 if (it->current_x != it->last_visible_x
9194 && (op & MOVE_TO_VPOS)
9195 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9196 {
9197 line_start_x = it->current_x + it->pixel_width
9198 - it->last_visible_x;
9199 if (FRAME_WINDOW_P (it->f))
9200 {
9201 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9202 struct font *face_font = face->font;
9203
9204 /* When display_line produces a continued line
9205 that ends in a TAB, it skips a tab stop that
9206 is closer than the font's space character
9207 width (see x_produce_glyphs where it produces
9208 the stretch glyph which represents a TAB).
9209 We need to reproduce the same logic here. */
9210 eassert (face_font);
9211 if (face_font)
9212 {
9213 if (line_start_x < face_font->space_width)
9214 line_start_x
9215 += it->tab_width * face_font->space_width;
9216 }
9217 }
9218 set_iterator_to_next (it, false);
9219 }
9220 }
9221 else
9222 it->continuation_lines_width += it->current_x;
9223 break;
9224
9225 default:
9226 emacs_abort ();
9227 }
9228
9229 /* Reset/increment for the next run. */
9230 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9231 it->current_x = line_start_x;
9232 line_start_x = 0;
9233 it->hpos = 0;
9234 it->current_y += it->max_ascent + it->max_descent;
9235 ++it->vpos;
9236 last_height = it->max_ascent + it->max_descent;
9237 it->max_ascent = it->max_descent = 0;
9238 }
9239
9240 out:
9241
9242 /* On text terminals, we may stop at the end of a line in the middle
9243 of a multi-character glyph. If the glyph itself is continued,
9244 i.e. it is actually displayed on the next line, don't treat this
9245 stopping point as valid; move to the next line instead (unless
9246 that brings us offscreen). */
9247 if (!FRAME_WINDOW_P (it->f)
9248 && op & MOVE_TO_POS
9249 && IT_CHARPOS (*it) == to_charpos
9250 && it->what == IT_CHARACTER
9251 && it->nglyphs > 1
9252 && it->line_wrap == WINDOW_WRAP
9253 && it->current_x == it->last_visible_x - 1
9254 && it->c != '\n'
9255 && it->c != '\t'
9256 && it->w->window_end_valid
9257 && it->vpos < it->w->window_end_vpos)
9258 {
9259 it->continuation_lines_width += it->current_x;
9260 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9261 it->current_y += it->max_ascent + it->max_descent;
9262 ++it->vpos;
9263 last_height = it->max_ascent + it->max_descent;
9264 }
9265
9266 if (backup_data)
9267 bidi_unshelve_cache (backup_data, true);
9268
9269 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9270
9271 return max_current_x;
9272 }
9273
9274
9275 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9276
9277 If DY > 0, move IT backward at least that many pixels. DY = 0
9278 means move IT backward to the preceding line start or BEGV. This
9279 function may move over more than DY pixels if IT->current_y - DY
9280 ends up in the middle of a line; in this case IT->current_y will be
9281 set to the top of the line moved to. */
9282
9283 void
9284 move_it_vertically_backward (struct it *it, int dy)
9285 {
9286 int nlines, h;
9287 struct it it2, it3;
9288 void *it2data = NULL, *it3data = NULL;
9289 ptrdiff_t start_pos;
9290 int nchars_per_row
9291 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9292 ptrdiff_t pos_limit;
9293
9294 move_further_back:
9295 eassert (dy >= 0);
9296
9297 start_pos = IT_CHARPOS (*it);
9298
9299 /* Estimate how many newlines we must move back. */
9300 nlines = max (1, dy / default_line_pixel_height (it->w));
9301 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9302 pos_limit = BEGV;
9303 else
9304 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9305
9306 /* Set the iterator's position that many lines back. But don't go
9307 back more than NLINES full screen lines -- this wins a day with
9308 buffers which have very long lines. */
9309 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9310 back_to_previous_visible_line_start (it);
9311
9312 /* Reseat the iterator here. When moving backward, we don't want
9313 reseat to skip forward over invisible text, set up the iterator
9314 to deliver from overlay strings at the new position etc. So,
9315 use reseat_1 here. */
9316 reseat_1 (it, it->current.pos, true);
9317
9318 /* We are now surely at a line start. */
9319 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9320 reordering is in effect. */
9321 it->continuation_lines_width = 0;
9322
9323 /* Move forward and see what y-distance we moved. First move to the
9324 start of the next line so that we get its height. We need this
9325 height to be able to tell whether we reached the specified
9326 y-distance. */
9327 SAVE_IT (it2, *it, it2data);
9328 it2.max_ascent = it2.max_descent = 0;
9329 do
9330 {
9331 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9332 MOVE_TO_POS | MOVE_TO_VPOS);
9333 }
9334 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9335 /* If we are in a display string which starts at START_POS,
9336 and that display string includes a newline, and we are
9337 right after that newline (i.e. at the beginning of a
9338 display line), exit the loop, because otherwise we will
9339 infloop, since move_it_to will see that it is already at
9340 START_POS and will not move. */
9341 || (it2.method == GET_FROM_STRING
9342 && IT_CHARPOS (it2) == start_pos
9343 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9344 eassert (IT_CHARPOS (*it) >= BEGV);
9345 SAVE_IT (it3, it2, it3data);
9346
9347 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9348 eassert (IT_CHARPOS (*it) >= BEGV);
9349 /* H is the actual vertical distance from the position in *IT
9350 and the starting position. */
9351 h = it2.current_y - it->current_y;
9352 /* NLINES is the distance in number of lines. */
9353 nlines = it2.vpos - it->vpos;
9354
9355 /* Correct IT's y and vpos position
9356 so that they are relative to the starting point. */
9357 it->vpos -= nlines;
9358 it->current_y -= h;
9359
9360 if (dy == 0)
9361 {
9362 /* DY == 0 means move to the start of the screen line. The
9363 value of nlines is > 0 if continuation lines were involved,
9364 or if the original IT position was at start of a line. */
9365 RESTORE_IT (it, it, it2data);
9366 if (nlines > 0)
9367 move_it_by_lines (it, nlines);
9368 /* The above code moves us to some position NLINES down,
9369 usually to its first glyph (leftmost in an L2R line), but
9370 that's not necessarily the start of the line, under bidi
9371 reordering. We want to get to the character position
9372 that is immediately after the newline of the previous
9373 line. */
9374 if (it->bidi_p
9375 && !it->continuation_lines_width
9376 && !STRINGP (it->string)
9377 && IT_CHARPOS (*it) > BEGV
9378 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9379 {
9380 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9381
9382 DEC_BOTH (cp, bp);
9383 cp = find_newline_no_quit (cp, bp, -1, NULL);
9384 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9385 }
9386 bidi_unshelve_cache (it3data, true);
9387 }
9388 else
9389 {
9390 /* The y-position we try to reach, relative to *IT.
9391 Note that H has been subtracted in front of the if-statement. */
9392 int target_y = it->current_y + h - dy;
9393 int y0 = it3.current_y;
9394 int y1;
9395 int line_height;
9396
9397 RESTORE_IT (&it3, &it3, it3data);
9398 y1 = line_bottom_y (&it3);
9399 line_height = y1 - y0;
9400 RESTORE_IT (it, it, it2data);
9401 /* If we did not reach target_y, try to move further backward if
9402 we can. If we moved too far backward, try to move forward. */
9403 if (target_y < it->current_y
9404 /* This is heuristic. In a window that's 3 lines high, with
9405 a line height of 13 pixels each, recentering with point
9406 on the bottom line will try to move -39/2 = 19 pixels
9407 backward. Try to avoid moving into the first line. */
9408 && (it->current_y - target_y
9409 > min (window_box_height (it->w), line_height * 2 / 3))
9410 && IT_CHARPOS (*it) > BEGV)
9411 {
9412 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9413 target_y - it->current_y));
9414 dy = it->current_y - target_y;
9415 goto move_further_back;
9416 }
9417 else if (target_y >= it->current_y + line_height
9418 && IT_CHARPOS (*it) < ZV)
9419 {
9420 /* Should move forward by at least one line, maybe more.
9421
9422 Note: Calling move_it_by_lines can be expensive on
9423 terminal frames, where compute_motion is used (via
9424 vmotion) to do the job, when there are very long lines
9425 and truncate-lines is nil. That's the reason for
9426 treating terminal frames specially here. */
9427
9428 if (!FRAME_WINDOW_P (it->f))
9429 move_it_vertically (it, target_y - (it->current_y + line_height));
9430 else
9431 {
9432 do
9433 {
9434 move_it_by_lines (it, 1);
9435 }
9436 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9437 }
9438 }
9439 }
9440 }
9441
9442
9443 /* Move IT by a specified amount of pixel lines DY. DY negative means
9444 move backwards. DY = 0 means move to start of screen line. At the
9445 end, IT will be on the start of a screen line. */
9446
9447 void
9448 move_it_vertically (struct it *it, int dy)
9449 {
9450 if (dy <= 0)
9451 move_it_vertically_backward (it, -dy);
9452 else
9453 {
9454 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9455 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9456 MOVE_TO_POS | MOVE_TO_Y);
9457 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9458
9459 /* If buffer ends in ZV without a newline, move to the start of
9460 the line to satisfy the post-condition. */
9461 if (IT_CHARPOS (*it) == ZV
9462 && ZV > BEGV
9463 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9464 move_it_by_lines (it, 0);
9465 }
9466 }
9467
9468
9469 /* Move iterator IT past the end of the text line it is in. */
9470
9471 void
9472 move_it_past_eol (struct it *it)
9473 {
9474 enum move_it_result rc;
9475
9476 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9477 if (rc == MOVE_NEWLINE_OR_CR)
9478 set_iterator_to_next (it, false);
9479 }
9480
9481
9482 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9483 negative means move up. DVPOS == 0 means move to the start of the
9484 screen line.
9485
9486 Optimization idea: If we would know that IT->f doesn't use
9487 a face with proportional font, we could be faster for
9488 truncate-lines nil. */
9489
9490 void
9491 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9492 {
9493
9494 /* The commented-out optimization uses vmotion on terminals. This
9495 gives bad results, because elements like it->what, on which
9496 callers such as pos_visible_p rely, aren't updated. */
9497 /* struct position pos;
9498 if (!FRAME_WINDOW_P (it->f))
9499 {
9500 struct text_pos textpos;
9501
9502 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9503 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9504 reseat (it, textpos, true);
9505 it->vpos += pos.vpos;
9506 it->current_y += pos.vpos;
9507 }
9508 else */
9509
9510 if (dvpos == 0)
9511 {
9512 /* DVPOS == 0 means move to the start of the screen line. */
9513 move_it_vertically_backward (it, 0);
9514 /* Let next call to line_bottom_y calculate real line height. */
9515 last_height = 0;
9516 }
9517 else if (dvpos > 0)
9518 {
9519 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9520 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9521 {
9522 /* Only move to the next buffer position if we ended up in a
9523 string from display property, not in an overlay string
9524 (before-string or after-string). That is because the
9525 latter don't conceal the underlying buffer position, so
9526 we can ask to move the iterator to the exact position we
9527 are interested in. Note that, even if we are already at
9528 IT_CHARPOS (*it), the call below is not a no-op, as it
9529 will detect that we are at the end of the string, pop the
9530 iterator, and compute it->current_x and it->hpos
9531 correctly. */
9532 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9533 -1, -1, -1, MOVE_TO_POS);
9534 }
9535 }
9536 else
9537 {
9538 struct it it2;
9539 void *it2data = NULL;
9540 ptrdiff_t start_charpos, i;
9541 int nchars_per_row
9542 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9543 bool hit_pos_limit = false;
9544 ptrdiff_t pos_limit;
9545
9546 /* Start at the beginning of the screen line containing IT's
9547 position. This may actually move vertically backwards,
9548 in case of overlays, so adjust dvpos accordingly. */
9549 dvpos += it->vpos;
9550 move_it_vertically_backward (it, 0);
9551 dvpos -= it->vpos;
9552
9553 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9554 screen lines, and reseat the iterator there. */
9555 start_charpos = IT_CHARPOS (*it);
9556 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9557 pos_limit = BEGV;
9558 else
9559 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9560
9561 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9562 back_to_previous_visible_line_start (it);
9563 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9564 hit_pos_limit = true;
9565 reseat (it, it->current.pos, true);
9566
9567 /* Move further back if we end up in a string or an image. */
9568 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9569 {
9570 /* First try to move to start of display line. */
9571 dvpos += it->vpos;
9572 move_it_vertically_backward (it, 0);
9573 dvpos -= it->vpos;
9574 if (IT_POS_VALID_AFTER_MOVE_P (it))
9575 break;
9576 /* If start of line is still in string or image,
9577 move further back. */
9578 back_to_previous_visible_line_start (it);
9579 reseat (it, it->current.pos, true);
9580 dvpos--;
9581 }
9582
9583 it->current_x = it->hpos = 0;
9584
9585 /* Above call may have moved too far if continuation lines
9586 are involved. Scan forward and see if it did. */
9587 SAVE_IT (it2, *it, it2data);
9588 it2.vpos = it2.current_y = 0;
9589 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9590 it->vpos -= it2.vpos;
9591 it->current_y -= it2.current_y;
9592 it->current_x = it->hpos = 0;
9593
9594 /* If we moved too far back, move IT some lines forward. */
9595 if (it2.vpos > -dvpos)
9596 {
9597 int delta = it2.vpos + dvpos;
9598
9599 RESTORE_IT (&it2, &it2, it2data);
9600 SAVE_IT (it2, *it, it2data);
9601 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9602 /* Move back again if we got too far ahead. */
9603 if (IT_CHARPOS (*it) >= start_charpos)
9604 RESTORE_IT (it, &it2, it2data);
9605 else
9606 bidi_unshelve_cache (it2data, true);
9607 }
9608 else if (hit_pos_limit && pos_limit > BEGV
9609 && dvpos < 0 && it2.vpos < -dvpos)
9610 {
9611 /* If we hit the limit, but still didn't make it far enough
9612 back, that means there's a display string with a newline
9613 covering a large chunk of text, and that caused
9614 back_to_previous_visible_line_start try to go too far.
9615 Punish those who commit such atrocities by going back
9616 until we've reached DVPOS, after lifting the limit, which
9617 could make it slow for very long lines. "If it hurts,
9618 don't do that!" */
9619 dvpos += it2.vpos;
9620 RESTORE_IT (it, it, it2data);
9621 for (i = -dvpos; i > 0; --i)
9622 {
9623 back_to_previous_visible_line_start (it);
9624 it->vpos--;
9625 }
9626 reseat_1 (it, it->current.pos, true);
9627 }
9628 else
9629 RESTORE_IT (it, it, it2data);
9630 }
9631 }
9632
9633 /* Return true if IT points into the middle of a display vector. */
9634
9635 bool
9636 in_display_vector_p (struct it *it)
9637 {
9638 return (it->method == GET_FROM_DISPLAY_VECTOR
9639 && it->current.dpvec_index > 0
9640 && it->dpvec + it->current.dpvec_index != it->dpend);
9641 }
9642
9643 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9644 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9645 WINDOW must be a live window and defaults to the selected one. The
9646 return value is a cons of the maximum pixel-width of any text line and
9647 the maximum pixel-height of all text lines.
9648
9649 The optional argument FROM, if non-nil, specifies the first text
9650 position and defaults to the minimum accessible position of the buffer.
9651 If FROM is t, use the minimum accessible position that is not a newline
9652 character. TO, if non-nil, specifies the last text position and
9653 defaults to the maximum accessible position of the buffer. If TO is t,
9654 use the maximum accessible position that is not a newline character.
9655
9656 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9657 width that can be returned. X-LIMIT nil or omitted, means to use the
9658 pixel-width of WINDOW's body; use this if you do not intend to change
9659 the width of WINDOW. Use the maximum width WINDOW may assume if you
9660 intend to change WINDOW's width. In any case, text whose x-coordinate
9661 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9662 can take some time, it's always a good idea to make this argument as
9663 small as possible; in particular, if the buffer contains long lines that
9664 shall be truncated anyway.
9665
9666 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9667 height that can be returned. Text lines whose y-coordinate is beyond
9668 Y-LIMIT are ignored. Since calculating the text height of a large
9669 buffer can take some time, it makes sense to specify this argument if
9670 the size of the buffer is unknown.
9671
9672 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9673 include the height of the mode- or header-line of WINDOW in the return
9674 value. If it is either the symbol `mode-line' or `header-line', include
9675 only the height of that line, if present, in the return value. If t,
9676 include the height of both, if present, in the return value. */)
9677 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9678 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9679 {
9680 struct window *w = decode_live_window (window);
9681 Lisp_Object buffer = w->contents;
9682 struct buffer *b;
9683 struct it it;
9684 struct buffer *old_b = NULL;
9685 ptrdiff_t start, end, pos;
9686 struct text_pos startp;
9687 void *itdata = NULL;
9688 int c, max_y = -1, x = 0, y = 0;
9689
9690 CHECK_BUFFER (buffer);
9691 b = XBUFFER (buffer);
9692
9693 if (b != current_buffer)
9694 {
9695 old_b = current_buffer;
9696 set_buffer_internal (b);
9697 }
9698
9699 if (NILP (from))
9700 start = BEGV;
9701 else if (EQ (from, Qt))
9702 {
9703 start = pos = BEGV;
9704 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9705 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9706 start = pos;
9707 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9708 start = pos;
9709 }
9710 else
9711 {
9712 CHECK_NUMBER_COERCE_MARKER (from);
9713 start = min (max (XINT (from), BEGV), ZV);
9714 }
9715
9716 if (NILP (to))
9717 end = ZV;
9718 else if (EQ (to, Qt))
9719 {
9720 end = pos = ZV;
9721 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9722 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9723 end = pos;
9724 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9725 end = pos;
9726 }
9727 else
9728 {
9729 CHECK_NUMBER_COERCE_MARKER (to);
9730 end = max (start, min (XINT (to), ZV));
9731 }
9732
9733 if (!NILP (y_limit))
9734 {
9735 CHECK_NUMBER (y_limit);
9736 max_y = min (XINT (y_limit), INT_MAX);
9737 }
9738
9739 itdata = bidi_shelve_cache ();
9740 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9741 start_display (&it, w, startp);
9742
9743 if (NILP (x_limit))
9744 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9745 else
9746 {
9747 CHECK_NUMBER (x_limit);
9748 it.last_visible_x = min (XINT (x_limit), INFINITY);
9749 /* Actually, we never want move_it_to stop at to_x. But to make
9750 sure that move_it_in_display_line_to always moves far enough,
9751 we set it to INT_MAX and specify MOVE_TO_X. */
9752 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9753 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9754 }
9755
9756 y = it.current_y + it.max_ascent + it.max_descent;
9757
9758 if (!EQ (mode_and_header_line, Qheader_line)
9759 && !EQ (mode_and_header_line, Qt))
9760 /* Do not count the header-line which was counted automatically by
9761 start_display. */
9762 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9763
9764 if (EQ (mode_and_header_line, Qmode_line)
9765 || EQ (mode_and_header_line, Qt))
9766 /* Do count the mode-line which is not included automatically by
9767 start_display. */
9768 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9769
9770 bidi_unshelve_cache (itdata, false);
9771
9772 if (old_b)
9773 set_buffer_internal (old_b);
9774
9775 return Fcons (make_number (x), make_number (y));
9776 }
9777 \f
9778 /***********************************************************************
9779 Messages
9780 ***********************************************************************/
9781
9782
9783 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9784 to *Messages*. */
9785
9786 void
9787 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9788 {
9789 Lisp_Object msg, fmt;
9790 char *buffer;
9791 ptrdiff_t len;
9792 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9793 USE_SAFE_ALLOCA;
9794
9795 fmt = msg = Qnil;
9796 GCPRO4 (fmt, msg, arg1, arg2);
9797
9798 fmt = build_string (format);
9799 msg = CALLN (Fformat, fmt, arg1, arg2);
9800
9801 len = SBYTES (msg) + 1;
9802 buffer = SAFE_ALLOCA (len);
9803 memcpy (buffer, SDATA (msg), len);
9804
9805 message_dolog (buffer, len - 1, true, false);
9806 SAFE_FREE ();
9807
9808 UNGCPRO;
9809 }
9810
9811
9812 /* Output a newline in the *Messages* buffer if "needs" one. */
9813
9814 void
9815 message_log_maybe_newline (void)
9816 {
9817 if (message_log_need_newline)
9818 message_dolog ("", 0, true, false);
9819 }
9820
9821
9822 /* Add a string M of length NBYTES to the message log, optionally
9823 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9824 true, means interpret the contents of M as multibyte. This
9825 function calls low-level routines in order to bypass text property
9826 hooks, etc. which might not be safe to run.
9827
9828 This may GC (insert may run before/after change hooks),
9829 so the buffer M must NOT point to a Lisp string. */
9830
9831 void
9832 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9833 {
9834 const unsigned char *msg = (const unsigned char *) m;
9835
9836 if (!NILP (Vmemory_full))
9837 return;
9838
9839 if (!NILP (Vmessage_log_max))
9840 {
9841 struct buffer *oldbuf;
9842 Lisp_Object oldpoint, oldbegv, oldzv;
9843 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9844 ptrdiff_t point_at_end = 0;
9845 ptrdiff_t zv_at_end = 0;
9846 Lisp_Object old_deactivate_mark;
9847 struct gcpro gcpro1;
9848
9849 old_deactivate_mark = Vdeactivate_mark;
9850 oldbuf = current_buffer;
9851
9852 /* Ensure the Messages buffer exists, and switch to it.
9853 If we created it, set the major-mode. */
9854 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9855 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9856 if (newbuffer
9857 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9858 call0 (intern ("messages-buffer-mode"));
9859
9860 bset_undo_list (current_buffer, Qt);
9861 bset_cache_long_scans (current_buffer, Qnil);
9862
9863 oldpoint = message_dolog_marker1;
9864 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9865 oldbegv = message_dolog_marker2;
9866 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9867 oldzv = message_dolog_marker3;
9868 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9869 GCPRO1 (old_deactivate_mark);
9870
9871 if (PT == Z)
9872 point_at_end = 1;
9873 if (ZV == Z)
9874 zv_at_end = 1;
9875
9876 BEGV = BEG;
9877 BEGV_BYTE = BEG_BYTE;
9878 ZV = Z;
9879 ZV_BYTE = Z_BYTE;
9880 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9881
9882 /* Insert the string--maybe converting multibyte to single byte
9883 or vice versa, so that all the text fits the buffer. */
9884 if (multibyte
9885 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9886 {
9887 ptrdiff_t i;
9888 int c, char_bytes;
9889 char work[1];
9890
9891 /* Convert a multibyte string to single-byte
9892 for the *Message* buffer. */
9893 for (i = 0; i < nbytes; i += char_bytes)
9894 {
9895 c = string_char_and_length (msg + i, &char_bytes);
9896 work[0] = CHAR_TO_BYTE8 (c);
9897 insert_1_both (work, 1, 1, true, false, false);
9898 }
9899 }
9900 else if (! multibyte
9901 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9902 {
9903 ptrdiff_t i;
9904 int c, char_bytes;
9905 unsigned char str[MAX_MULTIBYTE_LENGTH];
9906 /* Convert a single-byte string to multibyte
9907 for the *Message* buffer. */
9908 for (i = 0; i < nbytes; i++)
9909 {
9910 c = msg[i];
9911 MAKE_CHAR_MULTIBYTE (c);
9912 char_bytes = CHAR_STRING (c, str);
9913 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
9914 }
9915 }
9916 else if (nbytes)
9917 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
9918 true, false, false);
9919
9920 if (nlflag)
9921 {
9922 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9923 printmax_t dups;
9924
9925 insert_1_both ("\n", 1, 1, true, false, false);
9926
9927 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
9928 this_bol = PT;
9929 this_bol_byte = PT_BYTE;
9930
9931 /* See if this line duplicates the previous one.
9932 If so, combine duplicates. */
9933 if (this_bol > BEG)
9934 {
9935 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
9936 prev_bol = PT;
9937 prev_bol_byte = PT_BYTE;
9938
9939 dups = message_log_check_duplicate (prev_bol_byte,
9940 this_bol_byte);
9941 if (dups)
9942 {
9943 del_range_both (prev_bol, prev_bol_byte,
9944 this_bol, this_bol_byte, false);
9945 if (dups > 1)
9946 {
9947 char dupstr[sizeof " [ times]"
9948 + INT_STRLEN_BOUND (printmax_t)];
9949
9950 /* If you change this format, don't forget to also
9951 change message_log_check_duplicate. */
9952 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9953 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9954 insert_1_both (dupstr, duplen, duplen,
9955 true, false, true);
9956 }
9957 }
9958 }
9959
9960 /* If we have more than the desired maximum number of lines
9961 in the *Messages* buffer now, delete the oldest ones.
9962 This is safe because we don't have undo in this buffer. */
9963
9964 if (NATNUMP (Vmessage_log_max))
9965 {
9966 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9967 -XFASTINT (Vmessage_log_max) - 1, false);
9968 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
9969 }
9970 }
9971 BEGV = marker_position (oldbegv);
9972 BEGV_BYTE = marker_byte_position (oldbegv);
9973
9974 if (zv_at_end)
9975 {
9976 ZV = Z;
9977 ZV_BYTE = Z_BYTE;
9978 }
9979 else
9980 {
9981 ZV = marker_position (oldzv);
9982 ZV_BYTE = marker_byte_position (oldzv);
9983 }
9984
9985 if (point_at_end)
9986 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9987 else
9988 /* We can't do Fgoto_char (oldpoint) because it will run some
9989 Lisp code. */
9990 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9991 marker_byte_position (oldpoint));
9992
9993 UNGCPRO;
9994 unchain_marker (XMARKER (oldpoint));
9995 unchain_marker (XMARKER (oldbegv));
9996 unchain_marker (XMARKER (oldzv));
9997
9998 /* We called insert_1_both above with its 5th argument (PREPARE)
9999 false, which prevents insert_1_both from calling
10000 prepare_to_modify_buffer, which in turns prevents us from
10001 incrementing windows_or_buffers_changed even if *Messages* is
10002 shown in some window. So we must manually set
10003 windows_or_buffers_changed here to make up for that. */
10004 windows_or_buffers_changed = old_windows_or_buffers_changed;
10005 bset_redisplay (current_buffer);
10006
10007 set_buffer_internal (oldbuf);
10008
10009 message_log_need_newline = !nlflag;
10010 Vdeactivate_mark = old_deactivate_mark;
10011 }
10012 }
10013
10014
10015 /* We are at the end of the buffer after just having inserted a newline.
10016 (Note: We depend on the fact we won't be crossing the gap.)
10017 Check to see if the most recent message looks a lot like the previous one.
10018 Return 0 if different, 1 if the new one should just replace it, or a
10019 value N > 1 if we should also append " [N times]". */
10020
10021 static intmax_t
10022 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10023 {
10024 ptrdiff_t i;
10025 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10026 bool seen_dots = false;
10027 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10028 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10029
10030 for (i = 0; i < len; i++)
10031 {
10032 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10033 seen_dots = true;
10034 if (p1[i] != p2[i])
10035 return seen_dots;
10036 }
10037 p1 += len;
10038 if (*p1 == '\n')
10039 return 2;
10040 if (*p1++ == ' ' && *p1++ == '[')
10041 {
10042 char *pend;
10043 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10044 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10045 return n + 1;
10046 }
10047 return 0;
10048 }
10049 \f
10050
10051 /* Display an echo area message M with a specified length of NBYTES
10052 bytes. The string may include null characters. If M is not a
10053 string, clear out any existing message, and let the mini-buffer
10054 text show through.
10055
10056 This function cancels echoing. */
10057
10058 void
10059 message3 (Lisp_Object m)
10060 {
10061 struct gcpro gcpro1;
10062
10063 GCPRO1 (m);
10064 clear_message (true, true);
10065 cancel_echoing ();
10066
10067 /* First flush out any partial line written with print. */
10068 message_log_maybe_newline ();
10069 if (STRINGP (m))
10070 {
10071 ptrdiff_t nbytes = SBYTES (m);
10072 bool multibyte = STRING_MULTIBYTE (m);
10073 char *buffer;
10074 USE_SAFE_ALLOCA;
10075 SAFE_ALLOCA_STRING (buffer, m);
10076 message_dolog (buffer, nbytes, true, multibyte);
10077 SAFE_FREE ();
10078 }
10079 if (! inhibit_message)
10080 message3_nolog (m);
10081 UNGCPRO;
10082 }
10083
10084
10085 /* The non-logging version of message3.
10086 This does not cancel echoing, because it is used for echoing.
10087 Perhaps we need to make a separate function for echoing
10088 and make this cancel echoing. */
10089
10090 void
10091 message3_nolog (Lisp_Object m)
10092 {
10093 struct frame *sf = SELECTED_FRAME ();
10094
10095 if (FRAME_INITIAL_P (sf))
10096 {
10097 if (noninteractive_need_newline)
10098 putc ('\n', stderr);
10099 noninteractive_need_newline = false;
10100 if (STRINGP (m))
10101 {
10102 Lisp_Object s = ENCODE_SYSTEM (m);
10103
10104 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10105 }
10106 if (!cursor_in_echo_area)
10107 fprintf (stderr, "\n");
10108 fflush (stderr);
10109 }
10110 /* Error messages get reported properly by cmd_error, so this must be just an
10111 informative message; if the frame hasn't really been initialized yet, just
10112 toss it. */
10113 else if (INTERACTIVE && sf->glyphs_initialized_p)
10114 {
10115 /* Get the frame containing the mini-buffer
10116 that the selected frame is using. */
10117 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10118 Lisp_Object frame = XWINDOW (mini_window)->frame;
10119 struct frame *f = XFRAME (frame);
10120
10121 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10122 Fmake_frame_visible (frame);
10123
10124 if (STRINGP (m) && SCHARS (m) > 0)
10125 {
10126 set_message (m);
10127 if (minibuffer_auto_raise)
10128 Fraise_frame (frame);
10129 /* Assume we are not echoing.
10130 (If we are, echo_now will override this.) */
10131 echo_message_buffer = Qnil;
10132 }
10133 else
10134 clear_message (true, true);
10135
10136 do_pending_window_change (false);
10137 echo_area_display (true);
10138 do_pending_window_change (false);
10139 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10140 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10141 }
10142 }
10143
10144
10145 /* Display a null-terminated echo area message M. If M is 0, clear
10146 out any existing message, and let the mini-buffer text show through.
10147
10148 The buffer M must continue to exist until after the echo area gets
10149 cleared or some other message gets displayed there. Do not pass
10150 text that is stored in a Lisp string. Do not pass text in a buffer
10151 that was alloca'd. */
10152
10153 void
10154 message1 (const char *m)
10155 {
10156 message3 (m ? build_unibyte_string (m) : Qnil);
10157 }
10158
10159
10160 /* The non-logging counterpart of message1. */
10161
10162 void
10163 message1_nolog (const char *m)
10164 {
10165 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10166 }
10167
10168 /* Display a message M which contains a single %s
10169 which gets replaced with STRING. */
10170
10171 void
10172 message_with_string (const char *m, Lisp_Object string, bool log)
10173 {
10174 CHECK_STRING (string);
10175
10176 if (noninteractive)
10177 {
10178 if (m)
10179 {
10180 /* ENCODE_SYSTEM below can GC and/or relocate the
10181 Lisp data, so make sure we don't use it here. */
10182 eassert (relocatable_string_data_p (m) != 1);
10183
10184 if (noninteractive_need_newline)
10185 putc ('\n', stderr);
10186 noninteractive_need_newline = false;
10187 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10188 if (!cursor_in_echo_area)
10189 fprintf (stderr, "\n");
10190 fflush (stderr);
10191 }
10192 }
10193 else if (INTERACTIVE)
10194 {
10195 /* The frame whose minibuffer we're going to display the message on.
10196 It may be larger than the selected frame, so we need
10197 to use its buffer, not the selected frame's buffer. */
10198 Lisp_Object mini_window;
10199 struct frame *f, *sf = SELECTED_FRAME ();
10200
10201 /* Get the frame containing the minibuffer
10202 that the selected frame is using. */
10203 mini_window = FRAME_MINIBUF_WINDOW (sf);
10204 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10205
10206 /* Error messages get reported properly by cmd_error, so this must be
10207 just an informative message; if the frame hasn't really been
10208 initialized yet, just toss it. */
10209 if (f->glyphs_initialized_p)
10210 {
10211 struct gcpro gcpro1, gcpro2;
10212
10213 Lisp_Object fmt = build_string (m);
10214 Lisp_Object msg = string;
10215 GCPRO2 (fmt, msg);
10216
10217 msg = CALLN (Fformat, fmt, msg);
10218
10219 if (log)
10220 message3 (msg);
10221 else
10222 message3_nolog (msg);
10223
10224 UNGCPRO;
10225
10226 /* Print should start at the beginning of the message
10227 buffer next time. */
10228 message_buf_print = false;
10229 }
10230 }
10231 }
10232
10233
10234 /* Dump an informative message to the minibuf. If M is 0, clear out
10235 any existing message, and let the mini-buffer text show through. */
10236
10237 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10238 vmessage (const char *m, va_list ap)
10239 {
10240 if (noninteractive)
10241 {
10242 if (m)
10243 {
10244 if (noninteractive_need_newline)
10245 putc ('\n', stderr);
10246 noninteractive_need_newline = false;
10247 vfprintf (stderr, m, ap);
10248 if (!cursor_in_echo_area)
10249 fprintf (stderr, "\n");
10250 fflush (stderr);
10251 }
10252 }
10253 else if (INTERACTIVE)
10254 {
10255 /* The frame whose mini-buffer we're going to display the message
10256 on. It may be larger than the selected frame, so we need to
10257 use its buffer, not the selected frame's buffer. */
10258 Lisp_Object mini_window;
10259 struct frame *f, *sf = SELECTED_FRAME ();
10260
10261 /* Get the frame containing the mini-buffer
10262 that the selected frame is using. */
10263 mini_window = FRAME_MINIBUF_WINDOW (sf);
10264 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10265
10266 /* Error messages get reported properly by cmd_error, so this must be
10267 just an informative message; if the frame hasn't really been
10268 initialized yet, just toss it. */
10269 if (f->glyphs_initialized_p)
10270 {
10271 if (m)
10272 {
10273 ptrdiff_t len;
10274 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10275 USE_SAFE_ALLOCA;
10276 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10277
10278 len = doprnt (message_buf, maxsize, m, 0, ap);
10279
10280 message3 (make_string (message_buf, len));
10281 SAFE_FREE ();
10282 }
10283 else
10284 message1 (0);
10285
10286 /* Print should start at the beginning of the message
10287 buffer next time. */
10288 message_buf_print = false;
10289 }
10290 }
10291 }
10292
10293 void
10294 message (const char *m, ...)
10295 {
10296 va_list ap;
10297 va_start (ap, m);
10298 vmessage (m, ap);
10299 va_end (ap);
10300 }
10301
10302
10303 #if false
10304 /* The non-logging version of message. */
10305
10306 void
10307 message_nolog (const char *m, ...)
10308 {
10309 Lisp_Object old_log_max;
10310 va_list ap;
10311 va_start (ap, m);
10312 old_log_max = Vmessage_log_max;
10313 Vmessage_log_max = Qnil;
10314 vmessage (m, ap);
10315 Vmessage_log_max = old_log_max;
10316 va_end (ap);
10317 }
10318 #endif
10319
10320
10321 /* Display the current message in the current mini-buffer. This is
10322 only called from error handlers in process.c, and is not time
10323 critical. */
10324
10325 void
10326 update_echo_area (void)
10327 {
10328 if (!NILP (echo_area_buffer[0]))
10329 {
10330 Lisp_Object string;
10331 string = Fcurrent_message ();
10332 message3 (string);
10333 }
10334 }
10335
10336
10337 /* Make sure echo area buffers in `echo_buffers' are live.
10338 If they aren't, make new ones. */
10339
10340 static void
10341 ensure_echo_area_buffers (void)
10342 {
10343 int i;
10344
10345 for (i = 0; i < 2; ++i)
10346 if (!BUFFERP (echo_buffer[i])
10347 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10348 {
10349 char name[30];
10350 Lisp_Object old_buffer;
10351 int j;
10352
10353 old_buffer = echo_buffer[i];
10354 echo_buffer[i] = Fget_buffer_create
10355 (make_formatted_string (name, " *Echo Area %d*", i));
10356 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10357 /* to force word wrap in echo area -
10358 it was decided to postpone this*/
10359 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10360
10361 for (j = 0; j < 2; ++j)
10362 if (EQ (old_buffer, echo_area_buffer[j]))
10363 echo_area_buffer[j] = echo_buffer[i];
10364 }
10365 }
10366
10367
10368 /* Call FN with args A1..A2 with either the current or last displayed
10369 echo_area_buffer as current buffer.
10370
10371 WHICH zero means use the current message buffer
10372 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10373 from echo_buffer[] and clear it.
10374
10375 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10376 suitable buffer from echo_buffer[] and clear it.
10377
10378 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10379 that the current message becomes the last displayed one, make
10380 choose a suitable buffer for echo_area_buffer[0], and clear it.
10381
10382 Value is what FN returns. */
10383
10384 static bool
10385 with_echo_area_buffer (struct window *w, int which,
10386 bool (*fn) (ptrdiff_t, Lisp_Object),
10387 ptrdiff_t a1, Lisp_Object a2)
10388 {
10389 Lisp_Object buffer;
10390 bool this_one, the_other, clear_buffer_p, rc;
10391 ptrdiff_t count = SPECPDL_INDEX ();
10392
10393 /* If buffers aren't live, make new ones. */
10394 ensure_echo_area_buffers ();
10395
10396 clear_buffer_p = false;
10397
10398 if (which == 0)
10399 this_one = false, the_other = true;
10400 else if (which > 0)
10401 this_one = true, the_other = false;
10402 else
10403 {
10404 this_one = false, the_other = true;
10405 clear_buffer_p = true;
10406
10407 /* We need a fresh one in case the current echo buffer equals
10408 the one containing the last displayed echo area message. */
10409 if (!NILP (echo_area_buffer[this_one])
10410 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10411 echo_area_buffer[this_one] = Qnil;
10412 }
10413
10414 /* Choose a suitable buffer from echo_buffer[] is we don't
10415 have one. */
10416 if (NILP (echo_area_buffer[this_one]))
10417 {
10418 echo_area_buffer[this_one]
10419 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10420 ? echo_buffer[the_other]
10421 : echo_buffer[this_one]);
10422 clear_buffer_p = true;
10423 }
10424
10425 buffer = echo_area_buffer[this_one];
10426
10427 /* Don't get confused by reusing the buffer used for echoing
10428 for a different purpose. */
10429 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10430 cancel_echoing ();
10431
10432 record_unwind_protect (unwind_with_echo_area_buffer,
10433 with_echo_area_buffer_unwind_data (w));
10434
10435 /* Make the echo area buffer current. Note that for display
10436 purposes, it is not necessary that the displayed window's buffer
10437 == current_buffer, except for text property lookup. So, let's
10438 only set that buffer temporarily here without doing a full
10439 Fset_window_buffer. We must also change w->pointm, though,
10440 because otherwise an assertions in unshow_buffer fails, and Emacs
10441 aborts. */
10442 set_buffer_internal_1 (XBUFFER (buffer));
10443 if (w)
10444 {
10445 wset_buffer (w, buffer);
10446 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10447 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10448 }
10449
10450 bset_undo_list (current_buffer, Qt);
10451 bset_read_only (current_buffer, Qnil);
10452 specbind (Qinhibit_read_only, Qt);
10453 specbind (Qinhibit_modification_hooks, Qt);
10454
10455 if (clear_buffer_p && Z > BEG)
10456 del_range (BEG, Z);
10457
10458 eassert (BEGV >= BEG);
10459 eassert (ZV <= Z && ZV >= BEGV);
10460
10461 rc = fn (a1, a2);
10462
10463 eassert (BEGV >= BEG);
10464 eassert (ZV <= Z && ZV >= BEGV);
10465
10466 unbind_to (count, Qnil);
10467 return rc;
10468 }
10469
10470
10471 /* Save state that should be preserved around the call to the function
10472 FN called in with_echo_area_buffer. */
10473
10474 static Lisp_Object
10475 with_echo_area_buffer_unwind_data (struct window *w)
10476 {
10477 int i = 0;
10478 Lisp_Object vector, tmp;
10479
10480 /* Reduce consing by keeping one vector in
10481 Vwith_echo_area_save_vector. */
10482 vector = Vwith_echo_area_save_vector;
10483 Vwith_echo_area_save_vector = Qnil;
10484
10485 if (NILP (vector))
10486 vector = Fmake_vector (make_number (11), Qnil);
10487
10488 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10489 ASET (vector, i, Vdeactivate_mark); ++i;
10490 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10491
10492 if (w)
10493 {
10494 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10495 ASET (vector, i, w->contents); ++i;
10496 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10497 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10498 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10499 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10500 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10501 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10502 }
10503 else
10504 {
10505 int end = i + 8;
10506 for (; i < end; ++i)
10507 ASET (vector, i, Qnil);
10508 }
10509
10510 eassert (i == ASIZE (vector));
10511 return vector;
10512 }
10513
10514
10515 /* Restore global state from VECTOR which was created by
10516 with_echo_area_buffer_unwind_data. */
10517
10518 static void
10519 unwind_with_echo_area_buffer (Lisp_Object vector)
10520 {
10521 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10522 Vdeactivate_mark = AREF (vector, 1);
10523 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10524
10525 if (WINDOWP (AREF (vector, 3)))
10526 {
10527 struct window *w;
10528 Lisp_Object buffer;
10529
10530 w = XWINDOW (AREF (vector, 3));
10531 buffer = AREF (vector, 4);
10532
10533 wset_buffer (w, buffer);
10534 set_marker_both (w->pointm, buffer,
10535 XFASTINT (AREF (vector, 5)),
10536 XFASTINT (AREF (vector, 6)));
10537 set_marker_both (w->old_pointm, buffer,
10538 XFASTINT (AREF (vector, 7)),
10539 XFASTINT (AREF (vector, 8)));
10540 set_marker_both (w->start, buffer,
10541 XFASTINT (AREF (vector, 9)),
10542 XFASTINT (AREF (vector, 10)));
10543 }
10544
10545 Vwith_echo_area_save_vector = vector;
10546 }
10547
10548
10549 /* Set up the echo area for use by print functions. MULTIBYTE_P
10550 means we will print multibyte. */
10551
10552 void
10553 setup_echo_area_for_printing (bool multibyte_p)
10554 {
10555 /* If we can't find an echo area any more, exit. */
10556 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10557 Fkill_emacs (Qnil);
10558
10559 ensure_echo_area_buffers ();
10560
10561 if (!message_buf_print)
10562 {
10563 /* A message has been output since the last time we printed.
10564 Choose a fresh echo area buffer. */
10565 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10566 echo_area_buffer[0] = echo_buffer[1];
10567 else
10568 echo_area_buffer[0] = echo_buffer[0];
10569
10570 /* Switch to that buffer and clear it. */
10571 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10572 bset_truncate_lines (current_buffer, Qnil);
10573
10574 if (Z > BEG)
10575 {
10576 ptrdiff_t count = SPECPDL_INDEX ();
10577 specbind (Qinhibit_read_only, Qt);
10578 /* Note that undo recording is always disabled. */
10579 del_range (BEG, Z);
10580 unbind_to (count, Qnil);
10581 }
10582 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10583
10584 /* Set up the buffer for the multibyteness we need. */
10585 if (multibyte_p
10586 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10587 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10588
10589 /* Raise the frame containing the echo area. */
10590 if (minibuffer_auto_raise)
10591 {
10592 struct frame *sf = SELECTED_FRAME ();
10593 Lisp_Object mini_window;
10594 mini_window = FRAME_MINIBUF_WINDOW (sf);
10595 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10596 }
10597
10598 message_log_maybe_newline ();
10599 message_buf_print = true;
10600 }
10601 else
10602 {
10603 if (NILP (echo_area_buffer[0]))
10604 {
10605 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10606 echo_area_buffer[0] = echo_buffer[1];
10607 else
10608 echo_area_buffer[0] = echo_buffer[0];
10609 }
10610
10611 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10612 {
10613 /* Someone switched buffers between print requests. */
10614 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10615 bset_truncate_lines (current_buffer, Qnil);
10616 }
10617 }
10618 }
10619
10620
10621 /* Display an echo area message in window W. Value is true if W's
10622 height is changed. If display_last_displayed_message_p,
10623 display the message that was last displayed, otherwise
10624 display the current message. */
10625
10626 static bool
10627 display_echo_area (struct window *w)
10628 {
10629 bool no_message_p, window_height_changed_p;
10630
10631 /* Temporarily disable garbage collections while displaying the echo
10632 area. This is done because a GC can print a message itself.
10633 That message would modify the echo area buffer's contents while a
10634 redisplay of the buffer is going on, and seriously confuse
10635 redisplay. */
10636 ptrdiff_t count = inhibit_garbage_collection ();
10637
10638 /* If there is no message, we must call display_echo_area_1
10639 nevertheless because it resizes the window. But we will have to
10640 reset the echo_area_buffer in question to nil at the end because
10641 with_echo_area_buffer will sets it to an empty buffer. */
10642 bool i = display_last_displayed_message_p;
10643 no_message_p = NILP (echo_area_buffer[i]);
10644
10645 window_height_changed_p
10646 = with_echo_area_buffer (w, display_last_displayed_message_p,
10647 display_echo_area_1,
10648 (intptr_t) w, Qnil);
10649
10650 if (no_message_p)
10651 echo_area_buffer[i] = Qnil;
10652
10653 unbind_to (count, Qnil);
10654 return window_height_changed_p;
10655 }
10656
10657
10658 /* Helper for display_echo_area. Display the current buffer which
10659 contains the current echo area message in window W, a mini-window,
10660 a pointer to which is passed in A1. A2..A4 are currently not used.
10661 Change the height of W so that all of the message is displayed.
10662 Value is true if height of W was changed. */
10663
10664 static bool
10665 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10666 {
10667 intptr_t i1 = a1;
10668 struct window *w = (struct window *) i1;
10669 Lisp_Object window;
10670 struct text_pos start;
10671
10672 /* Do this before displaying, so that we have a large enough glyph
10673 matrix for the display. If we can't get enough space for the
10674 whole text, display the last N lines. That works by setting w->start. */
10675 bool window_height_changed_p = resize_mini_window (w, false);
10676
10677 /* Use the starting position chosen by resize_mini_window. */
10678 SET_TEXT_POS_FROM_MARKER (start, w->start);
10679
10680 /* Display. */
10681 clear_glyph_matrix (w->desired_matrix);
10682 XSETWINDOW (window, w);
10683 try_window (window, start, 0);
10684
10685 return window_height_changed_p;
10686 }
10687
10688
10689 /* Resize the echo area window to exactly the size needed for the
10690 currently displayed message, if there is one. If a mini-buffer
10691 is active, don't shrink it. */
10692
10693 void
10694 resize_echo_area_exactly (void)
10695 {
10696 if (BUFFERP (echo_area_buffer[0])
10697 && WINDOWP (echo_area_window))
10698 {
10699 struct window *w = XWINDOW (echo_area_window);
10700 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10701 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10702 (intptr_t) w, resize_exactly);
10703 if (resized_p)
10704 {
10705 windows_or_buffers_changed = 42;
10706 update_mode_lines = 30;
10707 redisplay_internal ();
10708 }
10709 }
10710 }
10711
10712
10713 /* Callback function for with_echo_area_buffer, when used from
10714 resize_echo_area_exactly. A1 contains a pointer to the window to
10715 resize, EXACTLY non-nil means resize the mini-window exactly to the
10716 size of the text displayed. A3 and A4 are not used. Value is what
10717 resize_mini_window returns. */
10718
10719 static bool
10720 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10721 {
10722 intptr_t i1 = a1;
10723 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10724 }
10725
10726
10727 /* Resize mini-window W to fit the size of its contents. EXACT_P
10728 means size the window exactly to the size needed. Otherwise, it's
10729 only enlarged until W's buffer is empty.
10730
10731 Set W->start to the right place to begin display. If the whole
10732 contents fit, start at the beginning. Otherwise, start so as
10733 to make the end of the contents appear. This is particularly
10734 important for y-or-n-p, but seems desirable generally.
10735
10736 Value is true if the window height has been changed. */
10737
10738 bool
10739 resize_mini_window (struct window *w, bool exact_p)
10740 {
10741 struct frame *f = XFRAME (w->frame);
10742 bool window_height_changed_p = false;
10743
10744 eassert (MINI_WINDOW_P (w));
10745
10746 /* By default, start display at the beginning. */
10747 set_marker_both (w->start, w->contents,
10748 BUF_BEGV (XBUFFER (w->contents)),
10749 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10750
10751 /* Don't resize windows while redisplaying a window; it would
10752 confuse redisplay functions when the size of the window they are
10753 displaying changes from under them. Such a resizing can happen,
10754 for instance, when which-func prints a long message while
10755 we are running fontification-functions. We're running these
10756 functions with safe_call which binds inhibit-redisplay to t. */
10757 if (!NILP (Vinhibit_redisplay))
10758 return false;
10759
10760 /* Nil means don't try to resize. */
10761 if (NILP (Vresize_mini_windows)
10762 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10763 return false;
10764
10765 if (!FRAME_MINIBUF_ONLY_P (f))
10766 {
10767 struct it it;
10768 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10769 + WINDOW_PIXEL_HEIGHT (w));
10770 int unit = FRAME_LINE_HEIGHT (f);
10771 int height, max_height;
10772 struct text_pos start;
10773 struct buffer *old_current_buffer = NULL;
10774
10775 if (current_buffer != XBUFFER (w->contents))
10776 {
10777 old_current_buffer = current_buffer;
10778 set_buffer_internal (XBUFFER (w->contents));
10779 }
10780
10781 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10782
10783 /* Compute the max. number of lines specified by the user. */
10784 if (FLOATP (Vmax_mini_window_height))
10785 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10786 else if (INTEGERP (Vmax_mini_window_height))
10787 max_height = XINT (Vmax_mini_window_height) * unit;
10788 else
10789 max_height = total_height / 4;
10790
10791 /* Correct that max. height if it's bogus. */
10792 max_height = clip_to_bounds (unit, max_height, total_height);
10793
10794 /* Find out the height of the text in the window. */
10795 if (it.line_wrap == TRUNCATE)
10796 height = unit;
10797 else
10798 {
10799 last_height = 0;
10800 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10801 if (it.max_ascent == 0 && it.max_descent == 0)
10802 height = it.current_y + last_height;
10803 else
10804 height = it.current_y + it.max_ascent + it.max_descent;
10805 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10806 }
10807
10808 /* Compute a suitable window start. */
10809 if (height > max_height)
10810 {
10811 height = (max_height / unit) * unit;
10812 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10813 move_it_vertically_backward (&it, height - unit);
10814 start = it.current.pos;
10815 }
10816 else
10817 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10818 SET_MARKER_FROM_TEXT_POS (w->start, start);
10819
10820 if (EQ (Vresize_mini_windows, Qgrow_only))
10821 {
10822 /* Let it grow only, until we display an empty message, in which
10823 case the window shrinks again. */
10824 if (height > WINDOW_PIXEL_HEIGHT (w))
10825 {
10826 int old_height = WINDOW_PIXEL_HEIGHT (w);
10827
10828 FRAME_WINDOWS_FROZEN (f) = true;
10829 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10830 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10831 }
10832 else if (height < WINDOW_PIXEL_HEIGHT (w)
10833 && (exact_p || BEGV == ZV))
10834 {
10835 int old_height = WINDOW_PIXEL_HEIGHT (w);
10836
10837 FRAME_WINDOWS_FROZEN (f) = false;
10838 shrink_mini_window (w, true);
10839 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10840 }
10841 }
10842 else
10843 {
10844 /* Always resize to exact size needed. */
10845 if (height > WINDOW_PIXEL_HEIGHT (w))
10846 {
10847 int old_height = WINDOW_PIXEL_HEIGHT (w);
10848
10849 FRAME_WINDOWS_FROZEN (f) = true;
10850 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10851 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10852 }
10853 else if (height < WINDOW_PIXEL_HEIGHT (w))
10854 {
10855 int old_height = WINDOW_PIXEL_HEIGHT (w);
10856
10857 FRAME_WINDOWS_FROZEN (f) = false;
10858 shrink_mini_window (w, true);
10859
10860 if (height)
10861 {
10862 FRAME_WINDOWS_FROZEN (f) = true;
10863 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10864 }
10865
10866 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10867 }
10868 }
10869
10870 if (old_current_buffer)
10871 set_buffer_internal (old_current_buffer);
10872 }
10873
10874 return window_height_changed_p;
10875 }
10876
10877
10878 /* Value is the current message, a string, or nil if there is no
10879 current message. */
10880
10881 Lisp_Object
10882 current_message (void)
10883 {
10884 Lisp_Object msg;
10885
10886 if (!BUFFERP (echo_area_buffer[0]))
10887 msg = Qnil;
10888 else
10889 {
10890 with_echo_area_buffer (0, 0, current_message_1,
10891 (intptr_t) &msg, Qnil);
10892 if (NILP (msg))
10893 echo_area_buffer[0] = Qnil;
10894 }
10895
10896 return msg;
10897 }
10898
10899
10900 static bool
10901 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10902 {
10903 intptr_t i1 = a1;
10904 Lisp_Object *msg = (Lisp_Object *) i1;
10905
10906 if (Z > BEG)
10907 *msg = make_buffer_string (BEG, Z, true);
10908 else
10909 *msg = Qnil;
10910 return false;
10911 }
10912
10913
10914 /* Push the current message on Vmessage_stack for later restoration
10915 by restore_message. Value is true if the current message isn't
10916 empty. This is a relatively infrequent operation, so it's not
10917 worth optimizing. */
10918
10919 bool
10920 push_message (void)
10921 {
10922 Lisp_Object msg = current_message ();
10923 Vmessage_stack = Fcons (msg, Vmessage_stack);
10924 return STRINGP (msg);
10925 }
10926
10927
10928 /* Restore message display from the top of Vmessage_stack. */
10929
10930 void
10931 restore_message (void)
10932 {
10933 eassert (CONSP (Vmessage_stack));
10934 message3_nolog (XCAR (Vmessage_stack));
10935 }
10936
10937
10938 /* Handler for unwind-protect calling pop_message. */
10939
10940 void
10941 pop_message_unwind (void)
10942 {
10943 /* Pop the top-most entry off Vmessage_stack. */
10944 eassert (CONSP (Vmessage_stack));
10945 Vmessage_stack = XCDR (Vmessage_stack);
10946 }
10947
10948
10949 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10950 exits. If the stack is not empty, we have a missing pop_message
10951 somewhere. */
10952
10953 void
10954 check_message_stack (void)
10955 {
10956 if (!NILP (Vmessage_stack))
10957 emacs_abort ();
10958 }
10959
10960
10961 /* Truncate to NCHARS what will be displayed in the echo area the next
10962 time we display it---but don't redisplay it now. */
10963
10964 void
10965 truncate_echo_area (ptrdiff_t nchars)
10966 {
10967 if (nchars == 0)
10968 echo_area_buffer[0] = Qnil;
10969 else if (!noninteractive
10970 && INTERACTIVE
10971 && !NILP (echo_area_buffer[0]))
10972 {
10973 struct frame *sf = SELECTED_FRAME ();
10974 /* Error messages get reported properly by cmd_error, so this must be
10975 just an informative message; if the frame hasn't really been
10976 initialized yet, just toss it. */
10977 if (sf->glyphs_initialized_p)
10978 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10979 }
10980 }
10981
10982
10983 /* Helper function for truncate_echo_area. Truncate the current
10984 message to at most NCHARS characters. */
10985
10986 static bool
10987 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10988 {
10989 if (BEG + nchars < Z)
10990 del_range (BEG + nchars, Z);
10991 if (Z == BEG)
10992 echo_area_buffer[0] = Qnil;
10993 return false;
10994 }
10995
10996 /* Set the current message to STRING. */
10997
10998 static void
10999 set_message (Lisp_Object string)
11000 {
11001 eassert (STRINGP (string));
11002
11003 message_enable_multibyte = STRING_MULTIBYTE (string);
11004
11005 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11006 message_buf_print = false;
11007 help_echo_showing_p = false;
11008
11009 if (STRINGP (Vdebug_on_message)
11010 && STRINGP (string)
11011 && fast_string_match (Vdebug_on_message, string) >= 0)
11012 call_debugger (list2 (Qerror, string));
11013 }
11014
11015
11016 /* Helper function for set_message. First argument is ignored and second
11017 argument has the same meaning as for set_message.
11018 This function is called with the echo area buffer being current. */
11019
11020 static bool
11021 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11022 {
11023 eassert (STRINGP (string));
11024
11025 /* Change multibyteness of the echo buffer appropriately. */
11026 if (message_enable_multibyte
11027 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11028 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11029
11030 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11031 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11032 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11033
11034 /* Insert new message at BEG. */
11035 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11036
11037 /* This function takes care of single/multibyte conversion.
11038 We just have to ensure that the echo area buffer has the right
11039 setting of enable_multibyte_characters. */
11040 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11041
11042 return false;
11043 }
11044
11045
11046 /* Clear messages. CURRENT_P means clear the current message.
11047 LAST_DISPLAYED_P means clear the message last displayed. */
11048
11049 void
11050 clear_message (bool current_p, bool last_displayed_p)
11051 {
11052 if (current_p)
11053 {
11054 echo_area_buffer[0] = Qnil;
11055 message_cleared_p = true;
11056 }
11057
11058 if (last_displayed_p)
11059 echo_area_buffer[1] = Qnil;
11060
11061 message_buf_print = false;
11062 }
11063
11064 /* Clear garbaged frames.
11065
11066 This function is used where the old redisplay called
11067 redraw_garbaged_frames which in turn called redraw_frame which in
11068 turn called clear_frame. The call to clear_frame was a source of
11069 flickering. I believe a clear_frame is not necessary. It should
11070 suffice in the new redisplay to invalidate all current matrices,
11071 and ensure a complete redisplay of all windows. */
11072
11073 static void
11074 clear_garbaged_frames (void)
11075 {
11076 if (frame_garbaged)
11077 {
11078 Lisp_Object tail, frame;
11079
11080 FOR_EACH_FRAME (tail, frame)
11081 {
11082 struct frame *f = XFRAME (frame);
11083
11084 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11085 {
11086 if (f->resized_p)
11087 redraw_frame (f);
11088 else
11089 clear_current_matrices (f);
11090 fset_redisplay (f);
11091 f->garbaged = false;
11092 f->resized_p = false;
11093 }
11094 }
11095
11096 frame_garbaged = false;
11097 }
11098 }
11099
11100
11101 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P,
11102 update selected_frame. Value is true if the mini-windows height
11103 has been changed. */
11104
11105 static bool
11106 echo_area_display (bool update_frame_p)
11107 {
11108 Lisp_Object mini_window;
11109 struct window *w;
11110 struct frame *f;
11111 bool window_height_changed_p = false;
11112 struct frame *sf = SELECTED_FRAME ();
11113
11114 mini_window = FRAME_MINIBUF_WINDOW (sf);
11115 w = XWINDOW (mini_window);
11116 f = XFRAME (WINDOW_FRAME (w));
11117
11118 /* Don't display if frame is invisible or not yet initialized. */
11119 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11120 return false;
11121
11122 #ifdef HAVE_WINDOW_SYSTEM
11123 /* When Emacs starts, selected_frame may be the initial terminal
11124 frame. If we let this through, a message would be displayed on
11125 the terminal. */
11126 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11127 return false;
11128 #endif /* HAVE_WINDOW_SYSTEM */
11129
11130 /* Redraw garbaged frames. */
11131 clear_garbaged_frames ();
11132
11133 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11134 {
11135 echo_area_window = mini_window;
11136 window_height_changed_p = display_echo_area (w);
11137 w->must_be_updated_p = true;
11138
11139 /* Update the display, unless called from redisplay_internal.
11140 Also don't update the screen during redisplay itself. The
11141 update will happen at the end of redisplay, and an update
11142 here could cause confusion. */
11143 if (update_frame_p && !redisplaying_p)
11144 {
11145 int n = 0;
11146
11147 /* If the display update has been interrupted by pending
11148 input, update mode lines in the frame. Due to the
11149 pending input, it might have been that redisplay hasn't
11150 been called, so that mode lines above the echo area are
11151 garbaged. This looks odd, so we prevent it here. */
11152 if (!display_completed)
11153 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11154
11155 if (window_height_changed_p
11156 /* Don't do this if Emacs is shutting down. Redisplay
11157 needs to run hooks. */
11158 && !NILP (Vrun_hooks))
11159 {
11160 /* Must update other windows. Likewise as in other
11161 cases, don't let this update be interrupted by
11162 pending input. */
11163 ptrdiff_t count = SPECPDL_INDEX ();
11164 specbind (Qredisplay_dont_pause, Qt);
11165 windows_or_buffers_changed = 44;
11166 redisplay_internal ();
11167 unbind_to (count, Qnil);
11168 }
11169 else if (FRAME_WINDOW_P (f) && n == 0)
11170 {
11171 /* Window configuration is the same as before.
11172 Can do with a display update of the echo area,
11173 unless we displayed some mode lines. */
11174 update_single_window (w);
11175 flush_frame (f);
11176 }
11177 else
11178 update_frame (f, true, true);
11179
11180 /* If cursor is in the echo area, make sure that the next
11181 redisplay displays the minibuffer, so that the cursor will
11182 be replaced with what the minibuffer wants. */
11183 if (cursor_in_echo_area)
11184 wset_redisplay (XWINDOW (mini_window));
11185 }
11186 }
11187 else if (!EQ (mini_window, selected_window))
11188 wset_redisplay (XWINDOW (mini_window));
11189
11190 /* Last displayed message is now the current message. */
11191 echo_area_buffer[1] = echo_area_buffer[0];
11192 /* Inform read_char that we're not echoing. */
11193 echo_message_buffer = Qnil;
11194
11195 /* Prevent redisplay optimization in redisplay_internal by resetting
11196 this_line_start_pos. This is done because the mini-buffer now
11197 displays the message instead of its buffer text. */
11198 if (EQ (mini_window, selected_window))
11199 CHARPOS (this_line_start_pos) = 0;
11200
11201 return window_height_changed_p;
11202 }
11203
11204 /* True if W's buffer was changed but not saved. */
11205
11206 static bool
11207 window_buffer_changed (struct window *w)
11208 {
11209 struct buffer *b = XBUFFER (w->contents);
11210
11211 eassert (BUFFER_LIVE_P (b));
11212
11213 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11214 }
11215
11216 /* True if W has %c in its mode line and mode line should be updated. */
11217
11218 static bool
11219 mode_line_update_needed (struct window *w)
11220 {
11221 return (w->column_number_displayed != -1
11222 && !(PT == w->last_point && !window_outdated (w))
11223 && (w->column_number_displayed != current_column ()));
11224 }
11225
11226 /* True if window start of W is frozen and may not be changed during
11227 redisplay. */
11228
11229 static bool
11230 window_frozen_p (struct window *w)
11231 {
11232 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11233 {
11234 Lisp_Object window;
11235
11236 XSETWINDOW (window, w);
11237 if (MINI_WINDOW_P (w))
11238 return false;
11239 else if (EQ (window, selected_window))
11240 return false;
11241 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11242 && EQ (window, Vminibuf_scroll_window))
11243 /* This special window can't be frozen too. */
11244 return false;
11245 else
11246 return true;
11247 }
11248 return false;
11249 }
11250
11251 /***********************************************************************
11252 Mode Lines and Frame Titles
11253 ***********************************************************************/
11254
11255 /* A buffer for constructing non-propertized mode-line strings and
11256 frame titles in it; allocated from the heap in init_xdisp and
11257 resized as needed in store_mode_line_noprop_char. */
11258
11259 static char *mode_line_noprop_buf;
11260
11261 /* The buffer's end, and a current output position in it. */
11262
11263 static char *mode_line_noprop_buf_end;
11264 static char *mode_line_noprop_ptr;
11265
11266 #define MODE_LINE_NOPROP_LEN(start) \
11267 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11268
11269 static enum {
11270 MODE_LINE_DISPLAY = 0,
11271 MODE_LINE_TITLE,
11272 MODE_LINE_NOPROP,
11273 MODE_LINE_STRING
11274 } mode_line_target;
11275
11276 /* Alist that caches the results of :propertize.
11277 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11278 static Lisp_Object mode_line_proptrans_alist;
11279
11280 /* List of strings making up the mode-line. */
11281 static Lisp_Object mode_line_string_list;
11282
11283 /* Base face property when building propertized mode line string. */
11284 static Lisp_Object mode_line_string_face;
11285 static Lisp_Object mode_line_string_face_prop;
11286
11287
11288 /* Unwind data for mode line strings */
11289
11290 static Lisp_Object Vmode_line_unwind_vector;
11291
11292 static Lisp_Object
11293 format_mode_line_unwind_data (struct frame *target_frame,
11294 struct buffer *obuf,
11295 Lisp_Object owin,
11296 bool save_proptrans)
11297 {
11298 Lisp_Object vector, tmp;
11299
11300 /* Reduce consing by keeping one vector in
11301 Vwith_echo_area_save_vector. */
11302 vector = Vmode_line_unwind_vector;
11303 Vmode_line_unwind_vector = Qnil;
11304
11305 if (NILP (vector))
11306 vector = Fmake_vector (make_number (10), Qnil);
11307
11308 ASET (vector, 0, make_number (mode_line_target));
11309 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11310 ASET (vector, 2, mode_line_string_list);
11311 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11312 ASET (vector, 4, mode_line_string_face);
11313 ASET (vector, 5, mode_line_string_face_prop);
11314
11315 if (obuf)
11316 XSETBUFFER (tmp, obuf);
11317 else
11318 tmp = Qnil;
11319 ASET (vector, 6, tmp);
11320 ASET (vector, 7, owin);
11321 if (target_frame)
11322 {
11323 /* Similarly to `with-selected-window', if the operation selects
11324 a window on another frame, we must restore that frame's
11325 selected window, and (for a tty) the top-frame. */
11326 ASET (vector, 8, target_frame->selected_window);
11327 if (FRAME_TERMCAP_P (target_frame))
11328 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11329 }
11330
11331 return vector;
11332 }
11333
11334 static void
11335 unwind_format_mode_line (Lisp_Object vector)
11336 {
11337 Lisp_Object old_window = AREF (vector, 7);
11338 Lisp_Object target_frame_window = AREF (vector, 8);
11339 Lisp_Object old_top_frame = AREF (vector, 9);
11340
11341 mode_line_target = XINT (AREF (vector, 0));
11342 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11343 mode_line_string_list = AREF (vector, 2);
11344 if (! EQ (AREF (vector, 3), Qt))
11345 mode_line_proptrans_alist = AREF (vector, 3);
11346 mode_line_string_face = AREF (vector, 4);
11347 mode_line_string_face_prop = AREF (vector, 5);
11348
11349 /* Select window before buffer, since it may change the buffer. */
11350 if (!NILP (old_window))
11351 {
11352 /* If the operation that we are unwinding had selected a window
11353 on a different frame, reset its frame-selected-window. For a
11354 text terminal, reset its top-frame if necessary. */
11355 if (!NILP (target_frame_window))
11356 {
11357 Lisp_Object frame
11358 = WINDOW_FRAME (XWINDOW (target_frame_window));
11359
11360 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11361 Fselect_window (target_frame_window, Qt);
11362
11363 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11364 Fselect_frame (old_top_frame, Qt);
11365 }
11366
11367 Fselect_window (old_window, Qt);
11368 }
11369
11370 if (!NILP (AREF (vector, 6)))
11371 {
11372 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11373 ASET (vector, 6, Qnil);
11374 }
11375
11376 Vmode_line_unwind_vector = vector;
11377 }
11378
11379
11380 /* Store a single character C for the frame title in mode_line_noprop_buf.
11381 Re-allocate mode_line_noprop_buf if necessary. */
11382
11383 static void
11384 store_mode_line_noprop_char (char c)
11385 {
11386 /* If output position has reached the end of the allocated buffer,
11387 increase the buffer's size. */
11388 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11389 {
11390 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11391 ptrdiff_t size = len;
11392 mode_line_noprop_buf =
11393 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11394 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11395 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11396 }
11397
11398 *mode_line_noprop_ptr++ = c;
11399 }
11400
11401
11402 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11403 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11404 characters that yield more columns than PRECISION; PRECISION <= 0
11405 means copy the whole string. Pad with spaces until FIELD_WIDTH
11406 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11407 pad. Called from display_mode_element when it is used to build a
11408 frame title. */
11409
11410 static int
11411 store_mode_line_noprop (const char *string, int field_width, int precision)
11412 {
11413 const unsigned char *str = (const unsigned char *) string;
11414 int n = 0;
11415 ptrdiff_t dummy, nbytes;
11416
11417 /* Copy at most PRECISION chars from STR. */
11418 nbytes = strlen (string);
11419 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11420 while (nbytes--)
11421 store_mode_line_noprop_char (*str++);
11422
11423 /* Fill up with spaces until FIELD_WIDTH reached. */
11424 while (field_width > 0
11425 && n < field_width)
11426 {
11427 store_mode_line_noprop_char (' ');
11428 ++n;
11429 }
11430
11431 return n;
11432 }
11433
11434 /***********************************************************************
11435 Frame Titles
11436 ***********************************************************************/
11437
11438 #ifdef HAVE_WINDOW_SYSTEM
11439
11440 /* Set the title of FRAME, if it has changed. The title format is
11441 Vicon_title_format if FRAME is iconified, otherwise it is
11442 frame_title_format. */
11443
11444 static void
11445 x_consider_frame_title (Lisp_Object frame)
11446 {
11447 struct frame *f = XFRAME (frame);
11448
11449 if (FRAME_WINDOW_P (f)
11450 || FRAME_MINIBUF_ONLY_P (f)
11451 || f->explicit_name)
11452 {
11453 /* Do we have more than one visible frame on this X display? */
11454 Lisp_Object tail, other_frame, fmt;
11455 ptrdiff_t title_start;
11456 char *title;
11457 ptrdiff_t len;
11458 struct it it;
11459 ptrdiff_t count = SPECPDL_INDEX ();
11460
11461 FOR_EACH_FRAME (tail, other_frame)
11462 {
11463 struct frame *tf = XFRAME (other_frame);
11464
11465 if (tf != f
11466 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11467 && !FRAME_MINIBUF_ONLY_P (tf)
11468 && !EQ (other_frame, tip_frame)
11469 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11470 break;
11471 }
11472
11473 /* Set global variable indicating that multiple frames exist. */
11474 multiple_frames = CONSP (tail);
11475
11476 /* Switch to the buffer of selected window of the frame. Set up
11477 mode_line_target so that display_mode_element will output into
11478 mode_line_noprop_buf; then display the title. */
11479 record_unwind_protect (unwind_format_mode_line,
11480 format_mode_line_unwind_data
11481 (f, current_buffer, selected_window, false));
11482
11483 Fselect_window (f->selected_window, Qt);
11484 set_buffer_internal_1
11485 (XBUFFER (XWINDOW (f->selected_window)->contents));
11486 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11487
11488 mode_line_target = MODE_LINE_TITLE;
11489 title_start = MODE_LINE_NOPROP_LEN (0);
11490 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11491 NULL, DEFAULT_FACE_ID);
11492 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11493 len = MODE_LINE_NOPROP_LEN (title_start);
11494 title = mode_line_noprop_buf + title_start;
11495 unbind_to (count, Qnil);
11496
11497 /* Set the title only if it's changed. This avoids consing in
11498 the common case where it hasn't. (If it turns out that we've
11499 already wasted too much time by walking through the list with
11500 display_mode_element, then we might need to optimize at a
11501 higher level than this.) */
11502 if (! STRINGP (f->name)
11503 || SBYTES (f->name) != len
11504 || memcmp (title, SDATA (f->name), len) != 0)
11505 x_implicitly_set_name (f, make_string (title, len), Qnil);
11506 }
11507 }
11508
11509 #endif /* not HAVE_WINDOW_SYSTEM */
11510
11511 \f
11512 /***********************************************************************
11513 Menu Bars
11514 ***********************************************************************/
11515
11516 /* True if we will not redisplay all visible windows. */
11517 #define REDISPLAY_SOME_P() \
11518 ((windows_or_buffers_changed == 0 \
11519 || windows_or_buffers_changed == REDISPLAY_SOME) \
11520 && (update_mode_lines == 0 \
11521 || update_mode_lines == REDISPLAY_SOME))
11522
11523 /* Prepare for redisplay by updating menu-bar item lists when
11524 appropriate. This can call eval. */
11525
11526 static void
11527 prepare_menu_bars (void)
11528 {
11529 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11530 bool some_windows = REDISPLAY_SOME_P ();
11531 struct gcpro gcpro1, gcpro2;
11532 Lisp_Object tooltip_frame;
11533
11534 #ifdef HAVE_WINDOW_SYSTEM
11535 tooltip_frame = tip_frame;
11536 #else
11537 tooltip_frame = Qnil;
11538 #endif
11539
11540 if (FUNCTIONP (Vpre_redisplay_function))
11541 {
11542 Lisp_Object windows = all_windows ? Qt : Qnil;
11543 if (all_windows && some_windows)
11544 {
11545 Lisp_Object ws = window_list ();
11546 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11547 {
11548 Lisp_Object this = XCAR (ws);
11549 struct window *w = XWINDOW (this);
11550 if (w->redisplay
11551 || XFRAME (w->frame)->redisplay
11552 || XBUFFER (w->contents)->text->redisplay)
11553 {
11554 windows = Fcons (this, windows);
11555 }
11556 }
11557 }
11558 safe__call1 (true, Vpre_redisplay_function, windows);
11559 }
11560
11561 /* Update all frame titles based on their buffer names, etc. We do
11562 this before the menu bars so that the buffer-menu will show the
11563 up-to-date frame titles. */
11564 #ifdef HAVE_WINDOW_SYSTEM
11565 if (all_windows)
11566 {
11567 Lisp_Object tail, frame;
11568
11569 FOR_EACH_FRAME (tail, frame)
11570 {
11571 struct frame *f = XFRAME (frame);
11572 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11573 if (some_windows
11574 && !f->redisplay
11575 && !w->redisplay
11576 && !XBUFFER (w->contents)->text->redisplay)
11577 continue;
11578
11579 if (!EQ (frame, tooltip_frame)
11580 && (FRAME_ICONIFIED_P (f)
11581 || FRAME_VISIBLE_P (f) == 1
11582 /* Exclude TTY frames that are obscured because they
11583 are not the top frame on their console. This is
11584 because x_consider_frame_title actually switches
11585 to the frame, which for TTY frames means it is
11586 marked as garbaged, and will be completely
11587 redrawn on the next redisplay cycle. This causes
11588 TTY frames to be completely redrawn, when there
11589 are more than one of them, even though nothing
11590 should be changed on display. */
11591 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11592 x_consider_frame_title (frame);
11593 }
11594 }
11595 #endif /* HAVE_WINDOW_SYSTEM */
11596
11597 /* Update the menu bar item lists, if appropriate. This has to be
11598 done before any actual redisplay or generation of display lines. */
11599
11600 if (all_windows)
11601 {
11602 Lisp_Object tail, frame;
11603 ptrdiff_t count = SPECPDL_INDEX ();
11604 /* True means that update_menu_bar has run its hooks
11605 so any further calls to update_menu_bar shouldn't do so again. */
11606 bool menu_bar_hooks_run = false;
11607
11608 record_unwind_save_match_data ();
11609
11610 FOR_EACH_FRAME (tail, frame)
11611 {
11612 struct frame *f = XFRAME (frame);
11613 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11614
11615 /* Ignore tooltip frame. */
11616 if (EQ (frame, tooltip_frame))
11617 continue;
11618
11619 if (some_windows
11620 && !f->redisplay
11621 && !w->redisplay
11622 && !XBUFFER (w->contents)->text->redisplay)
11623 continue;
11624
11625 /* If a window on this frame changed size, report that to
11626 the user and clear the size-change flag. */
11627 if (FRAME_WINDOW_SIZES_CHANGED (f))
11628 {
11629 Lisp_Object functions;
11630
11631 /* Clear flag first in case we get an error below. */
11632 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11633 functions = Vwindow_size_change_functions;
11634 GCPRO2 (tail, functions);
11635
11636 while (CONSP (functions))
11637 {
11638 if (!EQ (XCAR (functions), Qt))
11639 call1 (XCAR (functions), frame);
11640 functions = XCDR (functions);
11641 }
11642 UNGCPRO;
11643 }
11644
11645 GCPRO1 (tail);
11646 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11647 #ifdef HAVE_WINDOW_SYSTEM
11648 update_tool_bar (f, false);
11649 #endif
11650 UNGCPRO;
11651 }
11652
11653 unbind_to (count, Qnil);
11654 }
11655 else
11656 {
11657 struct frame *sf = SELECTED_FRAME ();
11658 update_menu_bar (sf, true, false);
11659 #ifdef HAVE_WINDOW_SYSTEM
11660 update_tool_bar (sf, true);
11661 #endif
11662 }
11663 }
11664
11665
11666 /* Update the menu bar item list for frame F. This has to be done
11667 before we start to fill in any display lines, because it can call
11668 eval.
11669
11670 If SAVE_MATCH_DATA, we must save and restore it here.
11671
11672 If HOOKS_RUN, a previous call to update_menu_bar
11673 already ran the menu bar hooks for this redisplay, so there
11674 is no need to run them again. The return value is the
11675 updated value of this flag, to pass to the next call. */
11676
11677 static bool
11678 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11679 {
11680 Lisp_Object window;
11681 struct window *w;
11682
11683 /* If called recursively during a menu update, do nothing. This can
11684 happen when, for instance, an activate-menubar-hook causes a
11685 redisplay. */
11686 if (inhibit_menubar_update)
11687 return hooks_run;
11688
11689 window = FRAME_SELECTED_WINDOW (f);
11690 w = XWINDOW (window);
11691
11692 if (FRAME_WINDOW_P (f)
11693 ?
11694 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11695 || defined (HAVE_NS) || defined (USE_GTK)
11696 FRAME_EXTERNAL_MENU_BAR (f)
11697 #else
11698 FRAME_MENU_BAR_LINES (f) > 0
11699 #endif
11700 : FRAME_MENU_BAR_LINES (f) > 0)
11701 {
11702 /* If the user has switched buffers or windows, we need to
11703 recompute to reflect the new bindings. But we'll
11704 recompute when update_mode_lines is set too; that means
11705 that people can use force-mode-line-update to request
11706 that the menu bar be recomputed. The adverse effect on
11707 the rest of the redisplay algorithm is about the same as
11708 windows_or_buffers_changed anyway. */
11709 if (windows_or_buffers_changed
11710 /* This used to test w->update_mode_line, but we believe
11711 there is no need to recompute the menu in that case. */
11712 || update_mode_lines
11713 || window_buffer_changed (w))
11714 {
11715 struct buffer *prev = current_buffer;
11716 ptrdiff_t count = SPECPDL_INDEX ();
11717
11718 specbind (Qinhibit_menubar_update, Qt);
11719
11720 set_buffer_internal_1 (XBUFFER (w->contents));
11721 if (save_match_data)
11722 record_unwind_save_match_data ();
11723 if (NILP (Voverriding_local_map_menu_flag))
11724 {
11725 specbind (Qoverriding_terminal_local_map, Qnil);
11726 specbind (Qoverriding_local_map, Qnil);
11727 }
11728
11729 if (!hooks_run)
11730 {
11731 /* Run the Lucid hook. */
11732 safe_run_hooks (Qactivate_menubar_hook);
11733
11734 /* If it has changed current-menubar from previous value,
11735 really recompute the menu-bar from the value. */
11736 if (! NILP (Vlucid_menu_bar_dirty_flag))
11737 call0 (Qrecompute_lucid_menubar);
11738
11739 safe_run_hooks (Qmenu_bar_update_hook);
11740
11741 hooks_run = true;
11742 }
11743
11744 XSETFRAME (Vmenu_updating_frame, f);
11745 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11746
11747 /* Redisplay the menu bar in case we changed it. */
11748 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11749 || defined (HAVE_NS) || defined (USE_GTK)
11750 if (FRAME_WINDOW_P (f))
11751 {
11752 #if defined (HAVE_NS)
11753 /* All frames on Mac OS share the same menubar. So only
11754 the selected frame should be allowed to set it. */
11755 if (f == SELECTED_FRAME ())
11756 #endif
11757 set_frame_menubar (f, false, false);
11758 }
11759 else
11760 /* On a terminal screen, the menu bar is an ordinary screen
11761 line, and this makes it get updated. */
11762 w->update_mode_line = true;
11763 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11764 /* In the non-toolkit version, the menu bar is an ordinary screen
11765 line, and this makes it get updated. */
11766 w->update_mode_line = true;
11767 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11768
11769 unbind_to (count, Qnil);
11770 set_buffer_internal_1 (prev);
11771 }
11772 }
11773
11774 return hooks_run;
11775 }
11776
11777 /***********************************************************************
11778 Tool-bars
11779 ***********************************************************************/
11780
11781 #ifdef HAVE_WINDOW_SYSTEM
11782
11783 /* Select `frame' temporarily without running all the code in
11784 do_switch_frame.
11785 FIXME: Maybe do_switch_frame should be trimmed down similarly
11786 when `norecord' is set. */
11787 static void
11788 fast_set_selected_frame (Lisp_Object frame)
11789 {
11790 if (!EQ (selected_frame, frame))
11791 {
11792 selected_frame = frame;
11793 selected_window = XFRAME (frame)->selected_window;
11794 }
11795 }
11796
11797 /* Update the tool-bar item list for frame F. This has to be done
11798 before we start to fill in any display lines. Called from
11799 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11800 and restore it here. */
11801
11802 static void
11803 update_tool_bar (struct frame *f, bool save_match_data)
11804 {
11805 #if defined (USE_GTK) || defined (HAVE_NS)
11806 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11807 #else
11808 bool do_update = (WINDOWP (f->tool_bar_window)
11809 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11810 #endif
11811
11812 if (do_update)
11813 {
11814 Lisp_Object window;
11815 struct window *w;
11816
11817 window = FRAME_SELECTED_WINDOW (f);
11818 w = XWINDOW (window);
11819
11820 /* If the user has switched buffers or windows, we need to
11821 recompute to reflect the new bindings. But we'll
11822 recompute when update_mode_lines is set too; that means
11823 that people can use force-mode-line-update to request
11824 that the menu bar be recomputed. The adverse effect on
11825 the rest of the redisplay algorithm is about the same as
11826 windows_or_buffers_changed anyway. */
11827 if (windows_or_buffers_changed
11828 || w->update_mode_line
11829 || update_mode_lines
11830 || window_buffer_changed (w))
11831 {
11832 struct buffer *prev = current_buffer;
11833 ptrdiff_t count = SPECPDL_INDEX ();
11834 Lisp_Object frame, new_tool_bar;
11835 int new_n_tool_bar;
11836 struct gcpro gcpro1;
11837
11838 /* Set current_buffer to the buffer of the selected
11839 window of the frame, so that we get the right local
11840 keymaps. */
11841 set_buffer_internal_1 (XBUFFER (w->contents));
11842
11843 /* Save match data, if we must. */
11844 if (save_match_data)
11845 record_unwind_save_match_data ();
11846
11847 /* Make sure that we don't accidentally use bogus keymaps. */
11848 if (NILP (Voverriding_local_map_menu_flag))
11849 {
11850 specbind (Qoverriding_terminal_local_map, Qnil);
11851 specbind (Qoverriding_local_map, Qnil);
11852 }
11853
11854 GCPRO1 (new_tool_bar);
11855
11856 /* We must temporarily set the selected frame to this frame
11857 before calling tool_bar_items, because the calculation of
11858 the tool-bar keymap uses the selected frame (see
11859 `tool-bar-make-keymap' in tool-bar.el). */
11860 eassert (EQ (selected_window,
11861 /* Since we only explicitly preserve selected_frame,
11862 check that selected_window would be redundant. */
11863 XFRAME (selected_frame)->selected_window));
11864 record_unwind_protect (fast_set_selected_frame, selected_frame);
11865 XSETFRAME (frame, f);
11866 fast_set_selected_frame (frame);
11867
11868 /* Build desired tool-bar items from keymaps. */
11869 new_tool_bar
11870 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11871 &new_n_tool_bar);
11872
11873 /* Redisplay the tool-bar if we changed it. */
11874 if (new_n_tool_bar != f->n_tool_bar_items
11875 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11876 {
11877 /* Redisplay that happens asynchronously due to an expose event
11878 may access f->tool_bar_items. Make sure we update both
11879 variables within BLOCK_INPUT so no such event interrupts. */
11880 block_input ();
11881 fset_tool_bar_items (f, new_tool_bar);
11882 f->n_tool_bar_items = new_n_tool_bar;
11883 w->update_mode_line = true;
11884 unblock_input ();
11885 }
11886
11887 UNGCPRO;
11888
11889 unbind_to (count, Qnil);
11890 set_buffer_internal_1 (prev);
11891 }
11892 }
11893 }
11894
11895 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11896
11897 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11898 F's desired tool-bar contents. F->tool_bar_items must have
11899 been set up previously by calling prepare_menu_bars. */
11900
11901 static void
11902 build_desired_tool_bar_string (struct frame *f)
11903 {
11904 int i, size, size_needed;
11905 struct gcpro gcpro1, gcpro2;
11906 Lisp_Object image, plist;
11907
11908 image = plist = Qnil;
11909 GCPRO2 (image, plist);
11910
11911 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11912 Otherwise, make a new string. */
11913
11914 /* The size of the string we might be able to reuse. */
11915 size = (STRINGP (f->desired_tool_bar_string)
11916 ? SCHARS (f->desired_tool_bar_string)
11917 : 0);
11918
11919 /* We need one space in the string for each image. */
11920 size_needed = f->n_tool_bar_items;
11921
11922 /* Reuse f->desired_tool_bar_string, if possible. */
11923 if (size < size_needed || NILP (f->desired_tool_bar_string))
11924 fset_desired_tool_bar_string
11925 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11926 else
11927 {
11928 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11929 struct gcpro gcpro1;
11930 GCPRO1 (props);
11931 Fremove_text_properties (make_number (0), make_number (size),
11932 props, f->desired_tool_bar_string);
11933 UNGCPRO;
11934 }
11935
11936 /* Put a `display' property on the string for the images to display,
11937 put a `menu_item' property on tool-bar items with a value that
11938 is the index of the item in F's tool-bar item vector. */
11939 for (i = 0; i < f->n_tool_bar_items; ++i)
11940 {
11941 #define PROP(IDX) \
11942 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11943
11944 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11945 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11946 int hmargin, vmargin, relief, idx, end;
11947
11948 /* If image is a vector, choose the image according to the
11949 button state. */
11950 image = PROP (TOOL_BAR_ITEM_IMAGES);
11951 if (VECTORP (image))
11952 {
11953 if (enabled_p)
11954 idx = (selected_p
11955 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11956 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11957 else
11958 idx = (selected_p
11959 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11960 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11961
11962 eassert (ASIZE (image) >= idx);
11963 image = AREF (image, idx);
11964 }
11965 else
11966 idx = -1;
11967
11968 /* Ignore invalid image specifications. */
11969 if (!valid_image_p (image))
11970 continue;
11971
11972 /* Display the tool-bar button pressed, or depressed. */
11973 plist = Fcopy_sequence (XCDR (image));
11974
11975 /* Compute margin and relief to draw. */
11976 relief = (tool_bar_button_relief >= 0
11977 ? tool_bar_button_relief
11978 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11979 hmargin = vmargin = relief;
11980
11981 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11982 INT_MAX - max (hmargin, vmargin)))
11983 {
11984 hmargin += XFASTINT (Vtool_bar_button_margin);
11985 vmargin += XFASTINT (Vtool_bar_button_margin);
11986 }
11987 else if (CONSP (Vtool_bar_button_margin))
11988 {
11989 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11990 INT_MAX - hmargin))
11991 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11992
11993 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11994 INT_MAX - vmargin))
11995 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11996 }
11997
11998 if (auto_raise_tool_bar_buttons_p)
11999 {
12000 /* Add a `:relief' property to the image spec if the item is
12001 selected. */
12002 if (selected_p)
12003 {
12004 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12005 hmargin -= relief;
12006 vmargin -= relief;
12007 }
12008 }
12009 else
12010 {
12011 /* If image is selected, display it pressed, i.e. with a
12012 negative relief. If it's not selected, display it with a
12013 raised relief. */
12014 plist = Fplist_put (plist, QCrelief,
12015 (selected_p
12016 ? make_number (-relief)
12017 : make_number (relief)));
12018 hmargin -= relief;
12019 vmargin -= relief;
12020 }
12021
12022 /* Put a margin around the image. */
12023 if (hmargin || vmargin)
12024 {
12025 if (hmargin == vmargin)
12026 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12027 else
12028 plist = Fplist_put (plist, QCmargin,
12029 Fcons (make_number (hmargin),
12030 make_number (vmargin)));
12031 }
12032
12033 /* If button is not enabled, and we don't have special images
12034 for the disabled state, make the image appear disabled by
12035 applying an appropriate algorithm to it. */
12036 if (!enabled_p && idx < 0)
12037 plist = Fplist_put (plist, QCconversion, Qdisabled);
12038
12039 /* Put a `display' text property on the string for the image to
12040 display. Put a `menu-item' property on the string that gives
12041 the start of this item's properties in the tool-bar items
12042 vector. */
12043 image = Fcons (Qimage, plist);
12044 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12045 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12046 struct gcpro gcpro1;
12047 GCPRO1 (props);
12048
12049 /* Let the last image hide all remaining spaces in the tool bar
12050 string. The string can be longer than needed when we reuse a
12051 previous string. */
12052 if (i + 1 == f->n_tool_bar_items)
12053 end = SCHARS (f->desired_tool_bar_string);
12054 else
12055 end = i + 1;
12056 Fadd_text_properties (make_number (i), make_number (end),
12057 props, f->desired_tool_bar_string);
12058 UNGCPRO;
12059 #undef PROP
12060 }
12061
12062 UNGCPRO;
12063 }
12064
12065
12066 /* Display one line of the tool-bar of frame IT->f.
12067
12068 HEIGHT specifies the desired height of the tool-bar line.
12069 If the actual height of the glyph row is less than HEIGHT, the
12070 row's height is increased to HEIGHT, and the icons are centered
12071 vertically in the new height.
12072
12073 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12074 count a final empty row in case the tool-bar width exactly matches
12075 the window width.
12076 */
12077
12078 static void
12079 display_tool_bar_line (struct it *it, int height)
12080 {
12081 struct glyph_row *row = it->glyph_row;
12082 int max_x = it->last_visible_x;
12083 struct glyph *last;
12084
12085 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12086 clear_glyph_row (row);
12087 row->enabled_p = true;
12088 row->y = it->current_y;
12089
12090 /* Note that this isn't made use of if the face hasn't a box,
12091 so there's no need to check the face here. */
12092 it->start_of_box_run_p = true;
12093
12094 while (it->current_x < max_x)
12095 {
12096 int x, n_glyphs_before, i, nglyphs;
12097 struct it it_before;
12098
12099 /* Get the next display element. */
12100 if (!get_next_display_element (it))
12101 {
12102 /* Don't count empty row if we are counting needed tool-bar lines. */
12103 if (height < 0 && !it->hpos)
12104 return;
12105 break;
12106 }
12107
12108 /* Produce glyphs. */
12109 n_glyphs_before = row->used[TEXT_AREA];
12110 it_before = *it;
12111
12112 PRODUCE_GLYPHS (it);
12113
12114 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12115 i = 0;
12116 x = it_before.current_x;
12117 while (i < nglyphs)
12118 {
12119 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12120
12121 if (x + glyph->pixel_width > max_x)
12122 {
12123 /* Glyph doesn't fit on line. Backtrack. */
12124 row->used[TEXT_AREA] = n_glyphs_before;
12125 *it = it_before;
12126 /* If this is the only glyph on this line, it will never fit on the
12127 tool-bar, so skip it. But ensure there is at least one glyph,
12128 so we don't accidentally disable the tool-bar. */
12129 if (n_glyphs_before == 0
12130 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12131 break;
12132 goto out;
12133 }
12134
12135 ++it->hpos;
12136 x += glyph->pixel_width;
12137 ++i;
12138 }
12139
12140 /* Stop at line end. */
12141 if (ITERATOR_AT_END_OF_LINE_P (it))
12142 break;
12143
12144 set_iterator_to_next (it, true);
12145 }
12146
12147 out:;
12148
12149 row->displays_text_p = row->used[TEXT_AREA] != 0;
12150
12151 /* Use default face for the border below the tool bar.
12152
12153 FIXME: When auto-resize-tool-bars is grow-only, there is
12154 no additional border below the possibly empty tool-bar lines.
12155 So to make the extra empty lines look "normal", we have to
12156 use the tool-bar face for the border too. */
12157 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12158 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12159 it->face_id = DEFAULT_FACE_ID;
12160
12161 extend_face_to_end_of_line (it);
12162 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12163 last->right_box_line_p = true;
12164 if (last == row->glyphs[TEXT_AREA])
12165 last->left_box_line_p = true;
12166
12167 /* Make line the desired height and center it vertically. */
12168 if ((height -= it->max_ascent + it->max_descent) > 0)
12169 {
12170 /* Don't add more than one line height. */
12171 height %= FRAME_LINE_HEIGHT (it->f);
12172 it->max_ascent += height / 2;
12173 it->max_descent += (height + 1) / 2;
12174 }
12175
12176 compute_line_metrics (it);
12177
12178 /* If line is empty, make it occupy the rest of the tool-bar. */
12179 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12180 {
12181 row->height = row->phys_height = it->last_visible_y - row->y;
12182 row->visible_height = row->height;
12183 row->ascent = row->phys_ascent = 0;
12184 row->extra_line_spacing = 0;
12185 }
12186
12187 row->full_width_p = true;
12188 row->continued_p = false;
12189 row->truncated_on_left_p = false;
12190 row->truncated_on_right_p = false;
12191
12192 it->current_x = it->hpos = 0;
12193 it->current_y += row->height;
12194 ++it->vpos;
12195 ++it->glyph_row;
12196 }
12197
12198
12199 /* Value is the number of pixels needed to make all tool-bar items of
12200 frame F visible. The actual number of glyph rows needed is
12201 returned in *N_ROWS if non-NULL. */
12202 static int
12203 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12204 {
12205 struct window *w = XWINDOW (f->tool_bar_window);
12206 struct it it;
12207 /* tool_bar_height is called from redisplay_tool_bar after building
12208 the desired matrix, so use (unused) mode-line row as temporary row to
12209 avoid destroying the first tool-bar row. */
12210 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12211
12212 /* Initialize an iterator for iteration over
12213 F->desired_tool_bar_string in the tool-bar window of frame F. */
12214 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12215 temp_row->reversed_p = false;
12216 it.first_visible_x = 0;
12217 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12218 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12219 it.paragraph_embedding = L2R;
12220
12221 while (!ITERATOR_AT_END_P (&it))
12222 {
12223 clear_glyph_row (temp_row);
12224 it.glyph_row = temp_row;
12225 display_tool_bar_line (&it, -1);
12226 }
12227 clear_glyph_row (temp_row);
12228
12229 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12230 if (n_rows)
12231 *n_rows = it.vpos > 0 ? it.vpos : -1;
12232
12233 if (pixelwise)
12234 return it.current_y;
12235 else
12236 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12237 }
12238
12239 #endif /* !USE_GTK && !HAVE_NS */
12240
12241 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12242 0, 2, 0,
12243 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12244 If FRAME is nil or omitted, use the selected frame. Optional argument
12245 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12246 (Lisp_Object frame, Lisp_Object pixelwise)
12247 {
12248 int height = 0;
12249
12250 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12251 struct frame *f = decode_any_frame (frame);
12252
12253 if (WINDOWP (f->tool_bar_window)
12254 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12255 {
12256 update_tool_bar (f, true);
12257 if (f->n_tool_bar_items)
12258 {
12259 build_desired_tool_bar_string (f);
12260 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12261 }
12262 }
12263 #endif
12264
12265 return make_number (height);
12266 }
12267
12268
12269 /* Display the tool-bar of frame F. Value is true if tool-bar's
12270 height should be changed. */
12271 static bool
12272 redisplay_tool_bar (struct frame *f)
12273 {
12274 #if defined (USE_GTK) || defined (HAVE_NS)
12275
12276 if (FRAME_EXTERNAL_TOOL_BAR (f))
12277 update_frame_tool_bar (f);
12278 return false;
12279
12280 #else /* !USE_GTK && !HAVE_NS */
12281
12282 struct window *w;
12283 struct it it;
12284 struct glyph_row *row;
12285
12286 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12287 do anything. This means you must start with tool-bar-lines
12288 non-zero to get the auto-sizing effect. Or in other words, you
12289 can turn off tool-bars by specifying tool-bar-lines zero. */
12290 if (!WINDOWP (f->tool_bar_window)
12291 || (w = XWINDOW (f->tool_bar_window),
12292 WINDOW_TOTAL_LINES (w) == 0))
12293 return false;
12294
12295 /* Set up an iterator for the tool-bar window. */
12296 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12297 it.first_visible_x = 0;
12298 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12299 row = it.glyph_row;
12300 row->reversed_p = false;
12301
12302 /* Build a string that represents the contents of the tool-bar. */
12303 build_desired_tool_bar_string (f);
12304 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12305 /* FIXME: This should be controlled by a user option. But it
12306 doesn't make sense to have an R2L tool bar if the menu bar cannot
12307 be drawn also R2L, and making the menu bar R2L is tricky due
12308 toolkit-specific code that implements it. If an R2L tool bar is
12309 ever supported, display_tool_bar_line should also be augmented to
12310 call unproduce_glyphs like display_line and display_string
12311 do. */
12312 it.paragraph_embedding = L2R;
12313
12314 if (f->n_tool_bar_rows == 0)
12315 {
12316 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12317
12318 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12319 {
12320 x_change_tool_bar_height (f, new_height);
12321 frame_default_tool_bar_height = new_height;
12322 /* Always do that now. */
12323 clear_glyph_matrix (w->desired_matrix);
12324 f->fonts_changed = true;
12325 return true;
12326 }
12327 }
12328
12329 /* Display as many lines as needed to display all tool-bar items. */
12330
12331 if (f->n_tool_bar_rows > 0)
12332 {
12333 int border, rows, height, extra;
12334
12335 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12336 border = XINT (Vtool_bar_border);
12337 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12338 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12339 else if (EQ (Vtool_bar_border, Qborder_width))
12340 border = f->border_width;
12341 else
12342 border = 0;
12343 if (border < 0)
12344 border = 0;
12345
12346 rows = f->n_tool_bar_rows;
12347 height = max (1, (it.last_visible_y - border) / rows);
12348 extra = it.last_visible_y - border - height * rows;
12349
12350 while (it.current_y < it.last_visible_y)
12351 {
12352 int h = 0;
12353 if (extra > 0 && rows-- > 0)
12354 {
12355 h = (extra + rows - 1) / rows;
12356 extra -= h;
12357 }
12358 display_tool_bar_line (&it, height + h);
12359 }
12360 }
12361 else
12362 {
12363 while (it.current_y < it.last_visible_y)
12364 display_tool_bar_line (&it, 0);
12365 }
12366
12367 /* It doesn't make much sense to try scrolling in the tool-bar
12368 window, so don't do it. */
12369 w->desired_matrix->no_scrolling_p = true;
12370 w->must_be_updated_p = true;
12371
12372 if (!NILP (Vauto_resize_tool_bars))
12373 {
12374 bool change_height_p = true;
12375
12376 /* If we couldn't display everything, change the tool-bar's
12377 height if there is room for more. */
12378 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12379 change_height_p = true;
12380
12381 /* We subtract 1 because display_tool_bar_line advances the
12382 glyph_row pointer before returning to its caller. We want to
12383 examine the last glyph row produced by
12384 display_tool_bar_line. */
12385 row = it.glyph_row - 1;
12386
12387 /* If there are blank lines at the end, except for a partially
12388 visible blank line at the end that is smaller than
12389 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12390 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12391 && row->height >= FRAME_LINE_HEIGHT (f))
12392 change_height_p = true;
12393
12394 /* If row displays tool-bar items, but is partially visible,
12395 change the tool-bar's height. */
12396 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12397 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12398 change_height_p = true;
12399
12400 /* Resize windows as needed by changing the `tool-bar-lines'
12401 frame parameter. */
12402 if (change_height_p)
12403 {
12404 int nrows;
12405 int new_height = tool_bar_height (f, &nrows, true);
12406
12407 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12408 && !f->minimize_tool_bar_window_p)
12409 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12410 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12411 f->minimize_tool_bar_window_p = false;
12412
12413 if (change_height_p)
12414 {
12415 x_change_tool_bar_height (f, new_height);
12416 frame_default_tool_bar_height = new_height;
12417 clear_glyph_matrix (w->desired_matrix);
12418 f->n_tool_bar_rows = nrows;
12419 f->fonts_changed = true;
12420
12421 return true;
12422 }
12423 }
12424 }
12425
12426 f->minimize_tool_bar_window_p = false;
12427 return false;
12428
12429 #endif /* USE_GTK || HAVE_NS */
12430 }
12431
12432 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12433
12434 /* Get information about the tool-bar item which is displayed in GLYPH
12435 on frame F. Return in *PROP_IDX the index where tool-bar item
12436 properties start in F->tool_bar_items. Value is false if
12437 GLYPH doesn't display a tool-bar item. */
12438
12439 static bool
12440 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12441 {
12442 Lisp_Object prop;
12443 int charpos;
12444
12445 /* This function can be called asynchronously, which means we must
12446 exclude any possibility that Fget_text_property signals an
12447 error. */
12448 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12449 charpos = max (0, charpos);
12450
12451 /* Get the text property `menu-item' at pos. The value of that
12452 property is the start index of this item's properties in
12453 F->tool_bar_items. */
12454 prop = Fget_text_property (make_number (charpos),
12455 Qmenu_item, f->current_tool_bar_string);
12456 if (! INTEGERP (prop))
12457 return false;
12458 *prop_idx = XINT (prop);
12459 return true;
12460 }
12461
12462 \f
12463 /* Get information about the tool-bar item at position X/Y on frame F.
12464 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12465 the current matrix of the tool-bar window of F, or NULL if not
12466 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12467 item in F->tool_bar_items. Value is
12468
12469 -1 if X/Y is not on a tool-bar item
12470 0 if X/Y is on the same item that was highlighted before.
12471 1 otherwise. */
12472
12473 static int
12474 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12475 int *hpos, int *vpos, int *prop_idx)
12476 {
12477 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12478 struct window *w = XWINDOW (f->tool_bar_window);
12479 int area;
12480
12481 /* Find the glyph under X/Y. */
12482 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12483 if (*glyph == NULL)
12484 return -1;
12485
12486 /* Get the start of this tool-bar item's properties in
12487 f->tool_bar_items. */
12488 if (!tool_bar_item_info (f, *glyph, prop_idx))
12489 return -1;
12490
12491 /* Is mouse on the highlighted item? */
12492 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12493 && *vpos >= hlinfo->mouse_face_beg_row
12494 && *vpos <= hlinfo->mouse_face_end_row
12495 && (*vpos > hlinfo->mouse_face_beg_row
12496 || *hpos >= hlinfo->mouse_face_beg_col)
12497 && (*vpos < hlinfo->mouse_face_end_row
12498 || *hpos < hlinfo->mouse_face_end_col
12499 || hlinfo->mouse_face_past_end))
12500 return 0;
12501
12502 return 1;
12503 }
12504
12505
12506 /* EXPORT:
12507 Handle mouse button event on the tool-bar of frame F, at
12508 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12509 false for button release. MODIFIERS is event modifiers for button
12510 release. */
12511
12512 void
12513 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12514 int modifiers)
12515 {
12516 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12517 struct window *w = XWINDOW (f->tool_bar_window);
12518 int hpos, vpos, prop_idx;
12519 struct glyph *glyph;
12520 Lisp_Object enabled_p;
12521 int ts;
12522
12523 /* If not on the highlighted tool-bar item, and mouse-highlight is
12524 non-nil, return. This is so we generate the tool-bar button
12525 click only when the mouse button is released on the same item as
12526 where it was pressed. However, when mouse-highlight is disabled,
12527 generate the click when the button is released regardless of the
12528 highlight, since tool-bar items are not highlighted in that
12529 case. */
12530 frame_to_window_pixel_xy (w, &x, &y);
12531 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12532 if (ts == -1
12533 || (ts != 0 && !NILP (Vmouse_highlight)))
12534 return;
12535
12536 /* When mouse-highlight is off, generate the click for the item
12537 where the button was pressed, disregarding where it was
12538 released. */
12539 if (NILP (Vmouse_highlight) && !down_p)
12540 prop_idx = f->last_tool_bar_item;
12541
12542 /* If item is disabled, do nothing. */
12543 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12544 if (NILP (enabled_p))
12545 return;
12546
12547 if (down_p)
12548 {
12549 /* Show item in pressed state. */
12550 if (!NILP (Vmouse_highlight))
12551 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12552 f->last_tool_bar_item = prop_idx;
12553 }
12554 else
12555 {
12556 Lisp_Object key, frame;
12557 struct input_event event;
12558 EVENT_INIT (event);
12559
12560 /* Show item in released state. */
12561 if (!NILP (Vmouse_highlight))
12562 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12563
12564 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12565
12566 XSETFRAME (frame, f);
12567 event.kind = TOOL_BAR_EVENT;
12568 event.frame_or_window = frame;
12569 event.arg = frame;
12570 kbd_buffer_store_event (&event);
12571
12572 event.kind = TOOL_BAR_EVENT;
12573 event.frame_or_window = frame;
12574 event.arg = key;
12575 event.modifiers = modifiers;
12576 kbd_buffer_store_event (&event);
12577 f->last_tool_bar_item = -1;
12578 }
12579 }
12580
12581
12582 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12583 tool-bar window-relative coordinates X/Y. Called from
12584 note_mouse_highlight. */
12585
12586 static void
12587 note_tool_bar_highlight (struct frame *f, int x, int y)
12588 {
12589 Lisp_Object window = f->tool_bar_window;
12590 struct window *w = XWINDOW (window);
12591 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12593 int hpos, vpos;
12594 struct glyph *glyph;
12595 struct glyph_row *row;
12596 int i;
12597 Lisp_Object enabled_p;
12598 int prop_idx;
12599 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12600 bool mouse_down_p;
12601 int rc;
12602
12603 /* Function note_mouse_highlight is called with negative X/Y
12604 values when mouse moves outside of the frame. */
12605 if (x <= 0 || y <= 0)
12606 {
12607 clear_mouse_face (hlinfo);
12608 return;
12609 }
12610
12611 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12612 if (rc < 0)
12613 {
12614 /* Not on tool-bar item. */
12615 clear_mouse_face (hlinfo);
12616 return;
12617 }
12618 else if (rc == 0)
12619 /* On same tool-bar item as before. */
12620 goto set_help_echo;
12621
12622 clear_mouse_face (hlinfo);
12623
12624 /* Mouse is down, but on different tool-bar item? */
12625 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12626 && f == dpyinfo->last_mouse_frame);
12627
12628 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12629 return;
12630
12631 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12632
12633 /* If tool-bar item is not enabled, don't highlight it. */
12634 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12635 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12636 {
12637 /* Compute the x-position of the glyph. In front and past the
12638 image is a space. We include this in the highlighted area. */
12639 row = MATRIX_ROW (w->current_matrix, vpos);
12640 for (i = x = 0; i < hpos; ++i)
12641 x += row->glyphs[TEXT_AREA][i].pixel_width;
12642
12643 /* Record this as the current active region. */
12644 hlinfo->mouse_face_beg_col = hpos;
12645 hlinfo->mouse_face_beg_row = vpos;
12646 hlinfo->mouse_face_beg_x = x;
12647 hlinfo->mouse_face_past_end = false;
12648
12649 hlinfo->mouse_face_end_col = hpos + 1;
12650 hlinfo->mouse_face_end_row = vpos;
12651 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12652 hlinfo->mouse_face_window = window;
12653 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12654
12655 /* Display it as active. */
12656 show_mouse_face (hlinfo, draw);
12657 }
12658
12659 set_help_echo:
12660
12661 /* Set help_echo_string to a help string to display for this tool-bar item.
12662 XTread_socket does the rest. */
12663 help_echo_object = help_echo_window = Qnil;
12664 help_echo_pos = -1;
12665 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12666 if (NILP (help_echo_string))
12667 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12668 }
12669
12670 #endif /* !USE_GTK && !HAVE_NS */
12671
12672 #endif /* HAVE_WINDOW_SYSTEM */
12673
12674
12675 \f
12676 /************************************************************************
12677 Horizontal scrolling
12678 ************************************************************************/
12679
12680 /* For all leaf windows in the window tree rooted at WINDOW, set their
12681 hscroll value so that PT is (i) visible in the window, and (ii) so
12682 that it is not within a certain margin at the window's left and
12683 right border. Value is true if any window's hscroll has been
12684 changed. */
12685
12686 static bool
12687 hscroll_window_tree (Lisp_Object window)
12688 {
12689 bool hscrolled_p = false;
12690 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12691 int hscroll_step_abs = 0;
12692 double hscroll_step_rel = 0;
12693
12694 if (hscroll_relative_p)
12695 {
12696 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12697 if (hscroll_step_rel < 0)
12698 {
12699 hscroll_relative_p = false;
12700 hscroll_step_abs = 0;
12701 }
12702 }
12703 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12704 {
12705 hscroll_step_abs = XINT (Vhscroll_step);
12706 if (hscroll_step_abs < 0)
12707 hscroll_step_abs = 0;
12708 }
12709 else
12710 hscroll_step_abs = 0;
12711
12712 while (WINDOWP (window))
12713 {
12714 struct window *w = XWINDOW (window);
12715
12716 if (WINDOWP (w->contents))
12717 hscrolled_p |= hscroll_window_tree (w->contents);
12718 else if (w->cursor.vpos >= 0)
12719 {
12720 int h_margin;
12721 int text_area_width;
12722 struct glyph_row *cursor_row;
12723 struct glyph_row *bottom_row;
12724
12725 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12726 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12727 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12728 else
12729 cursor_row = bottom_row - 1;
12730
12731 if (!cursor_row->enabled_p)
12732 {
12733 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12734 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12735 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12736 else
12737 cursor_row = bottom_row - 1;
12738 }
12739 bool row_r2l_p = cursor_row->reversed_p;
12740
12741 text_area_width = window_box_width (w, TEXT_AREA);
12742
12743 /* Scroll when cursor is inside this scroll margin. */
12744 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12745
12746 /* If the position of this window's point has explicitly
12747 changed, no more suspend auto hscrolling. */
12748 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12749 w->suspend_auto_hscroll = false;
12750
12751 /* Remember window point. */
12752 Fset_marker (w->old_pointm,
12753 ((w == XWINDOW (selected_window))
12754 ? make_number (BUF_PT (XBUFFER (w->contents)))
12755 : Fmarker_position (w->pointm)),
12756 w->contents);
12757
12758 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12759 && !w->suspend_auto_hscroll
12760 /* In some pathological cases, like restoring a window
12761 configuration into a frame that is much smaller than
12762 the one from which the configuration was saved, we
12763 get glyph rows whose start and end have zero buffer
12764 positions, which we cannot handle below. Just skip
12765 such windows. */
12766 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12767 /* For left-to-right rows, hscroll when cursor is either
12768 (i) inside the right hscroll margin, or (ii) if it is
12769 inside the left margin and the window is already
12770 hscrolled. */
12771 && ((!row_r2l_p
12772 && ((w->hscroll && w->cursor.x <= h_margin)
12773 || (cursor_row->enabled_p
12774 && cursor_row->truncated_on_right_p
12775 && (w->cursor.x >= text_area_width - h_margin))))
12776 /* For right-to-left rows, the logic is similar,
12777 except that rules for scrolling to left and right
12778 are reversed. E.g., if cursor.x <= h_margin, we
12779 need to hscroll "to the right" unconditionally,
12780 and that will scroll the screen to the left so as
12781 to reveal the next portion of the row. */
12782 || (row_r2l_p
12783 && ((cursor_row->enabled_p
12784 /* FIXME: It is confusing to set the
12785 truncated_on_right_p flag when R2L rows
12786 are actually truncated on the left. */
12787 && cursor_row->truncated_on_right_p
12788 && w->cursor.x <= h_margin)
12789 || (w->hscroll
12790 && (w->cursor.x >= text_area_width - h_margin))))))
12791 {
12792 struct it it;
12793 ptrdiff_t hscroll;
12794 struct buffer *saved_current_buffer;
12795 ptrdiff_t pt;
12796 int wanted_x;
12797
12798 /* Find point in a display of infinite width. */
12799 saved_current_buffer = current_buffer;
12800 current_buffer = XBUFFER (w->contents);
12801
12802 if (w == XWINDOW (selected_window))
12803 pt = PT;
12804 else
12805 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12806
12807 /* Move iterator to pt starting at cursor_row->start in
12808 a line with infinite width. */
12809 init_to_row_start (&it, w, cursor_row);
12810 it.last_visible_x = INFINITY;
12811 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12812 current_buffer = saved_current_buffer;
12813
12814 /* Position cursor in window. */
12815 if (!hscroll_relative_p && hscroll_step_abs == 0)
12816 hscroll = max (0, (it.current_x
12817 - (ITERATOR_AT_END_OF_LINE_P (&it)
12818 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12819 : (text_area_width / 2))))
12820 / FRAME_COLUMN_WIDTH (it.f);
12821 else if ((!row_r2l_p
12822 && w->cursor.x >= text_area_width - h_margin)
12823 || (row_r2l_p && w->cursor.x <= h_margin))
12824 {
12825 if (hscroll_relative_p)
12826 wanted_x = text_area_width * (1 - hscroll_step_rel)
12827 - h_margin;
12828 else
12829 wanted_x = text_area_width
12830 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12831 - h_margin;
12832 hscroll
12833 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12834 }
12835 else
12836 {
12837 if (hscroll_relative_p)
12838 wanted_x = text_area_width * hscroll_step_rel
12839 + h_margin;
12840 else
12841 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12842 + h_margin;
12843 hscroll
12844 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12845 }
12846 hscroll = max (hscroll, w->min_hscroll);
12847
12848 /* Don't prevent redisplay optimizations if hscroll
12849 hasn't changed, as it will unnecessarily slow down
12850 redisplay. */
12851 if (w->hscroll != hscroll)
12852 {
12853 struct buffer *b = XBUFFER (w->contents);
12854 b->prevent_redisplay_optimizations_p = true;
12855 w->hscroll = hscroll;
12856 hscrolled_p = true;
12857 }
12858 }
12859 }
12860
12861 window = w->next;
12862 }
12863
12864 /* Value is true if hscroll of any leaf window has been changed. */
12865 return hscrolled_p;
12866 }
12867
12868
12869 /* Set hscroll so that cursor is visible and not inside horizontal
12870 scroll margins for all windows in the tree rooted at WINDOW. See
12871 also hscroll_window_tree above. Value is true if any window's
12872 hscroll has been changed. If it has, desired matrices on the frame
12873 of WINDOW are cleared. */
12874
12875 static bool
12876 hscroll_windows (Lisp_Object window)
12877 {
12878 bool hscrolled_p = hscroll_window_tree (window);
12879 if (hscrolled_p)
12880 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12881 return hscrolled_p;
12882 }
12883
12884
12885 \f
12886 /************************************************************************
12887 Redisplay
12888 ************************************************************************/
12889
12890 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12891 This is sometimes handy to have in a debugger session. */
12892
12893 #ifdef GLYPH_DEBUG
12894
12895 /* First and last unchanged row for try_window_id. */
12896
12897 static int debug_first_unchanged_at_end_vpos;
12898 static int debug_last_unchanged_at_beg_vpos;
12899
12900 /* Delta vpos and y. */
12901
12902 static int debug_dvpos, debug_dy;
12903
12904 /* Delta in characters and bytes for try_window_id. */
12905
12906 static ptrdiff_t debug_delta, debug_delta_bytes;
12907
12908 /* Values of window_end_pos and window_end_vpos at the end of
12909 try_window_id. */
12910
12911 static ptrdiff_t debug_end_vpos;
12912
12913 /* Append a string to W->desired_matrix->method. FMT is a printf
12914 format string. If trace_redisplay_p is true also printf the
12915 resulting string to stderr. */
12916
12917 static void debug_method_add (struct window *, char const *, ...)
12918 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12919
12920 static void
12921 debug_method_add (struct window *w, char const *fmt, ...)
12922 {
12923 void *ptr = w;
12924 char *method = w->desired_matrix->method;
12925 int len = strlen (method);
12926 int size = sizeof w->desired_matrix->method;
12927 int remaining = size - len - 1;
12928 va_list ap;
12929
12930 if (len && remaining)
12931 {
12932 method[len] = '|';
12933 --remaining, ++len;
12934 }
12935
12936 va_start (ap, fmt);
12937 vsnprintf (method + len, remaining + 1, fmt, ap);
12938 va_end (ap);
12939
12940 if (trace_redisplay_p)
12941 fprintf (stderr, "%p (%s): %s\n",
12942 ptr,
12943 ((BUFFERP (w->contents)
12944 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12945 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12946 : "no buffer"),
12947 method + len);
12948 }
12949
12950 #endif /* GLYPH_DEBUG */
12951
12952
12953 /* Value is true if all changes in window W, which displays
12954 current_buffer, are in the text between START and END. START is a
12955 buffer position, END is given as a distance from Z. Used in
12956 redisplay_internal for display optimization. */
12957
12958 static bool
12959 text_outside_line_unchanged_p (struct window *w,
12960 ptrdiff_t start, ptrdiff_t end)
12961 {
12962 bool unchanged_p = true;
12963
12964 /* If text or overlays have changed, see where. */
12965 if (window_outdated (w))
12966 {
12967 /* Gap in the line? */
12968 if (GPT < start || Z - GPT < end)
12969 unchanged_p = false;
12970
12971 /* Changes start in front of the line, or end after it? */
12972 if (unchanged_p
12973 && (BEG_UNCHANGED < start - 1
12974 || END_UNCHANGED < end))
12975 unchanged_p = false;
12976
12977 /* If selective display, can't optimize if changes start at the
12978 beginning of the line. */
12979 if (unchanged_p
12980 && INTEGERP (BVAR (current_buffer, selective_display))
12981 && XINT (BVAR (current_buffer, selective_display)) > 0
12982 && (BEG_UNCHANGED < start || GPT <= start))
12983 unchanged_p = false;
12984
12985 /* If there are overlays at the start or end of the line, these
12986 may have overlay strings with newlines in them. A change at
12987 START, for instance, may actually concern the display of such
12988 overlay strings as well, and they are displayed on different
12989 lines. So, quickly rule out this case. (For the future, it
12990 might be desirable to implement something more telling than
12991 just BEG/END_UNCHANGED.) */
12992 if (unchanged_p)
12993 {
12994 if (BEG + BEG_UNCHANGED == start
12995 && overlay_touches_p (start))
12996 unchanged_p = false;
12997 if (END_UNCHANGED == end
12998 && overlay_touches_p (Z - end))
12999 unchanged_p = false;
13000 }
13001
13002 /* Under bidi reordering, adding or deleting a character in the
13003 beginning of a paragraph, before the first strong directional
13004 character, can change the base direction of the paragraph (unless
13005 the buffer specifies a fixed paragraph direction), which will
13006 require to redisplay the whole paragraph. It might be worthwhile
13007 to find the paragraph limits and widen the range of redisplayed
13008 lines to that, but for now just give up this optimization. */
13009 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13010 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13011 unchanged_p = false;
13012 }
13013
13014 return unchanged_p;
13015 }
13016
13017
13018 /* Do a frame update, taking possible shortcuts into account. This is
13019 the main external entry point for redisplay.
13020
13021 If the last redisplay displayed an echo area message and that message
13022 is no longer requested, we clear the echo area or bring back the
13023 mini-buffer if that is in use. */
13024
13025 void
13026 redisplay (void)
13027 {
13028 redisplay_internal ();
13029 }
13030
13031
13032 static Lisp_Object
13033 overlay_arrow_string_or_property (Lisp_Object var)
13034 {
13035 Lisp_Object val;
13036
13037 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13038 return val;
13039
13040 return Voverlay_arrow_string;
13041 }
13042
13043 /* Return true if there are any overlay-arrows in current_buffer. */
13044 static bool
13045 overlay_arrow_in_current_buffer_p (void)
13046 {
13047 Lisp_Object vlist;
13048
13049 for (vlist = Voverlay_arrow_variable_list;
13050 CONSP (vlist);
13051 vlist = XCDR (vlist))
13052 {
13053 Lisp_Object var = XCAR (vlist);
13054 Lisp_Object val;
13055
13056 if (!SYMBOLP (var))
13057 continue;
13058 val = find_symbol_value (var);
13059 if (MARKERP (val)
13060 && current_buffer == XMARKER (val)->buffer)
13061 return true;
13062 }
13063 return false;
13064 }
13065
13066
13067 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13068 has changed. */
13069
13070 static bool
13071 overlay_arrows_changed_p (void)
13072 {
13073 Lisp_Object vlist;
13074
13075 for (vlist = Voverlay_arrow_variable_list;
13076 CONSP (vlist);
13077 vlist = XCDR (vlist))
13078 {
13079 Lisp_Object var = XCAR (vlist);
13080 Lisp_Object val, pstr;
13081
13082 if (!SYMBOLP (var))
13083 continue;
13084 val = find_symbol_value (var);
13085 if (!MARKERP (val))
13086 continue;
13087 if (! EQ (COERCE_MARKER (val),
13088 Fget (var, Qlast_arrow_position))
13089 || ! (pstr = overlay_arrow_string_or_property (var),
13090 EQ (pstr, Fget (var, Qlast_arrow_string))))
13091 return true;
13092 }
13093 return false;
13094 }
13095
13096 /* Mark overlay arrows to be updated on next redisplay. */
13097
13098 static void
13099 update_overlay_arrows (int up_to_date)
13100 {
13101 Lisp_Object vlist;
13102
13103 for (vlist = Voverlay_arrow_variable_list;
13104 CONSP (vlist);
13105 vlist = XCDR (vlist))
13106 {
13107 Lisp_Object var = XCAR (vlist);
13108
13109 if (!SYMBOLP (var))
13110 continue;
13111
13112 if (up_to_date > 0)
13113 {
13114 Lisp_Object val = find_symbol_value (var);
13115 Fput (var, Qlast_arrow_position,
13116 COERCE_MARKER (val));
13117 Fput (var, Qlast_arrow_string,
13118 overlay_arrow_string_or_property (var));
13119 }
13120 else if (up_to_date < 0
13121 || !NILP (Fget (var, Qlast_arrow_position)))
13122 {
13123 Fput (var, Qlast_arrow_position, Qt);
13124 Fput (var, Qlast_arrow_string, Qt);
13125 }
13126 }
13127 }
13128
13129
13130 /* Return overlay arrow string to display at row.
13131 Return integer (bitmap number) for arrow bitmap in left fringe.
13132 Return nil if no overlay arrow. */
13133
13134 static Lisp_Object
13135 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13136 {
13137 Lisp_Object vlist;
13138
13139 for (vlist = Voverlay_arrow_variable_list;
13140 CONSP (vlist);
13141 vlist = XCDR (vlist))
13142 {
13143 Lisp_Object var = XCAR (vlist);
13144 Lisp_Object val;
13145
13146 if (!SYMBOLP (var))
13147 continue;
13148
13149 val = find_symbol_value (var);
13150
13151 if (MARKERP (val)
13152 && current_buffer == XMARKER (val)->buffer
13153 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13154 {
13155 if (FRAME_WINDOW_P (it->f)
13156 /* FIXME: if ROW->reversed_p is set, this should test
13157 the right fringe, not the left one. */
13158 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13159 {
13160 #ifdef HAVE_WINDOW_SYSTEM
13161 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13162 {
13163 int fringe_bitmap = lookup_fringe_bitmap (val);
13164 if (fringe_bitmap != 0)
13165 return make_number (fringe_bitmap);
13166 }
13167 #endif
13168 return make_number (-1); /* Use default arrow bitmap. */
13169 }
13170 return overlay_arrow_string_or_property (var);
13171 }
13172 }
13173
13174 return Qnil;
13175 }
13176
13177 /* Return true if point moved out of or into a composition. Otherwise
13178 return false. PREV_BUF and PREV_PT are the last point buffer and
13179 position. BUF and PT are the current point buffer and position. */
13180
13181 static bool
13182 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13183 struct buffer *buf, ptrdiff_t pt)
13184 {
13185 ptrdiff_t start, end;
13186 Lisp_Object prop;
13187 Lisp_Object buffer;
13188
13189 XSETBUFFER (buffer, buf);
13190 /* Check a composition at the last point if point moved within the
13191 same buffer. */
13192 if (prev_buf == buf)
13193 {
13194 if (prev_pt == pt)
13195 /* Point didn't move. */
13196 return false;
13197
13198 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13199 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13200 && composition_valid_p (start, end, prop)
13201 && start < prev_pt && end > prev_pt)
13202 /* The last point was within the composition. Return true iff
13203 point moved out of the composition. */
13204 return (pt <= start || pt >= end);
13205 }
13206
13207 /* Check a composition at the current point. */
13208 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13209 && find_composition (pt, -1, &start, &end, &prop, buffer)
13210 && composition_valid_p (start, end, prop)
13211 && start < pt && end > pt);
13212 }
13213
13214 /* Reconsider the clip changes of buffer which is displayed in W. */
13215
13216 static void
13217 reconsider_clip_changes (struct window *w)
13218 {
13219 struct buffer *b = XBUFFER (w->contents);
13220
13221 if (b->clip_changed
13222 && w->window_end_valid
13223 && w->current_matrix->buffer == b
13224 && w->current_matrix->zv == BUF_ZV (b)
13225 && w->current_matrix->begv == BUF_BEGV (b))
13226 b->clip_changed = false;
13227
13228 /* If display wasn't paused, and W is not a tool bar window, see if
13229 point has been moved into or out of a composition. In that case,
13230 set b->clip_changed to force updating the screen. If
13231 b->clip_changed has already been set, skip this check. */
13232 if (!b->clip_changed && w->window_end_valid)
13233 {
13234 ptrdiff_t pt = (w == XWINDOW (selected_window)
13235 ? PT : marker_position (w->pointm));
13236
13237 if ((w->current_matrix->buffer != b || pt != w->last_point)
13238 && check_point_in_composition (w->current_matrix->buffer,
13239 w->last_point, b, pt))
13240 b->clip_changed = true;
13241 }
13242 }
13243
13244 static void
13245 propagate_buffer_redisplay (void)
13246 { /* Resetting b->text->redisplay is problematic!
13247 We can't just reset it in the case that some window that displays
13248 it has not been redisplayed; and such a window can stay
13249 unredisplayed for a long time if it's currently invisible.
13250 But we do want to reset it at the end of redisplay otherwise
13251 its displayed windows will keep being redisplayed over and over
13252 again.
13253 So we copy all b->text->redisplay flags up to their windows here,
13254 such that mark_window_display_accurate can safely reset
13255 b->text->redisplay. */
13256 Lisp_Object ws = window_list ();
13257 for (; CONSP (ws); ws = XCDR (ws))
13258 {
13259 struct window *thisw = XWINDOW (XCAR (ws));
13260 struct buffer *thisb = XBUFFER (thisw->contents);
13261 if (thisb->text->redisplay)
13262 thisw->redisplay = true;
13263 }
13264 }
13265
13266 #define STOP_POLLING \
13267 do { if (! polling_stopped_here) stop_polling (); \
13268 polling_stopped_here = true; } while (false)
13269
13270 #define RESUME_POLLING \
13271 do { if (polling_stopped_here) start_polling (); \
13272 polling_stopped_here = false; } while (false)
13273
13274
13275 /* Perhaps in the future avoid recentering windows if it
13276 is not necessary; currently that causes some problems. */
13277
13278 static void
13279 redisplay_internal (void)
13280 {
13281 struct window *w = XWINDOW (selected_window);
13282 struct window *sw;
13283 struct frame *fr;
13284 bool pending;
13285 bool must_finish = false, match_p;
13286 struct text_pos tlbufpos, tlendpos;
13287 int number_of_visible_frames;
13288 ptrdiff_t count;
13289 struct frame *sf;
13290 bool polling_stopped_here = false;
13291 Lisp_Object tail, frame;
13292
13293 /* True means redisplay has to consider all windows on all
13294 frames. False, only selected_window is considered. */
13295 bool consider_all_windows_p;
13296
13297 /* True means redisplay has to redisplay the miniwindow. */
13298 bool update_miniwindow_p = false;
13299
13300 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13301
13302 /* No redisplay if running in batch mode or frame is not yet fully
13303 initialized, or redisplay is explicitly turned off by setting
13304 Vinhibit_redisplay. */
13305 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13306 || !NILP (Vinhibit_redisplay))
13307 return;
13308
13309 /* Don't examine these until after testing Vinhibit_redisplay.
13310 When Emacs is shutting down, perhaps because its connection to
13311 X has dropped, we should not look at them at all. */
13312 fr = XFRAME (w->frame);
13313 sf = SELECTED_FRAME ();
13314
13315 if (!fr->glyphs_initialized_p)
13316 return;
13317
13318 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13319 if (popup_activated ())
13320 return;
13321 #endif
13322
13323 /* I don't think this happens but let's be paranoid. */
13324 if (redisplaying_p)
13325 return;
13326
13327 /* Record a function that clears redisplaying_p
13328 when we leave this function. */
13329 count = SPECPDL_INDEX ();
13330 record_unwind_protect_void (unwind_redisplay);
13331 redisplaying_p = true;
13332 specbind (Qinhibit_free_realized_faces, Qnil);
13333
13334 /* Record this function, so it appears on the profiler's backtraces. */
13335 record_in_backtrace (Qredisplay_internal, 0, 0);
13336
13337 FOR_EACH_FRAME (tail, frame)
13338 XFRAME (frame)->already_hscrolled_p = false;
13339
13340 retry:
13341 /* Remember the currently selected window. */
13342 sw = w;
13343
13344 pending = false;
13345 last_escape_glyph_frame = NULL;
13346 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13347 last_glyphless_glyph_frame = NULL;
13348 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13349
13350 /* If face_change, init_iterator will free all realized faces, which
13351 includes the faces referenced from current matrices. So, we
13352 can't reuse current matrices in this case. */
13353 if (face_change)
13354 windows_or_buffers_changed = 47;
13355
13356 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13357 && FRAME_TTY (sf)->previous_frame != sf)
13358 {
13359 /* Since frames on a single ASCII terminal share the same
13360 display area, displaying a different frame means redisplay
13361 the whole thing. */
13362 SET_FRAME_GARBAGED (sf);
13363 #ifndef DOS_NT
13364 set_tty_color_mode (FRAME_TTY (sf), sf);
13365 #endif
13366 FRAME_TTY (sf)->previous_frame = sf;
13367 }
13368
13369 /* Set the visible flags for all frames. Do this before checking for
13370 resized or garbaged frames; they want to know if their frames are
13371 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13372 number_of_visible_frames = 0;
13373
13374 FOR_EACH_FRAME (tail, frame)
13375 {
13376 struct frame *f = XFRAME (frame);
13377
13378 if (FRAME_VISIBLE_P (f))
13379 {
13380 ++number_of_visible_frames;
13381 /* Adjust matrices for visible frames only. */
13382 if (f->fonts_changed)
13383 {
13384 adjust_frame_glyphs (f);
13385 /* Disable all redisplay optimizations for this frame.
13386 This is because adjust_frame_glyphs resets the
13387 enabled_p flag for all glyph rows of all windows, so
13388 many optimizations will fail anyway, and some might
13389 fail to test that flag and do bogus things as
13390 result. */
13391 SET_FRAME_GARBAGED (f);
13392 f->fonts_changed = false;
13393 }
13394 /* If cursor type has been changed on the frame
13395 other than selected, consider all frames. */
13396 if (f != sf && f->cursor_type_changed)
13397 update_mode_lines = 31;
13398 }
13399 clear_desired_matrices (f);
13400 }
13401
13402 /* Notice any pending interrupt request to change frame size. */
13403 do_pending_window_change (true);
13404
13405 /* do_pending_window_change could change the selected_window due to
13406 frame resizing which makes the selected window too small. */
13407 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13408 sw = w;
13409
13410 /* Clear frames marked as garbaged. */
13411 clear_garbaged_frames ();
13412
13413 /* Build menubar and tool-bar items. */
13414 if (NILP (Vmemory_full))
13415 prepare_menu_bars ();
13416
13417 reconsider_clip_changes (w);
13418
13419 /* In most cases selected window displays current buffer. */
13420 match_p = XBUFFER (w->contents) == current_buffer;
13421 if (match_p)
13422 {
13423 /* Detect case that we need to write or remove a star in the mode line. */
13424 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13425 w->update_mode_line = true;
13426
13427 if (mode_line_update_needed (w))
13428 w->update_mode_line = true;
13429
13430 /* If reconsider_clip_changes above decided that the narrowing
13431 in the current buffer changed, make sure all other windows
13432 showing that buffer will be redisplayed. */
13433 if (current_buffer->clip_changed)
13434 bset_update_mode_line (current_buffer);
13435 }
13436
13437 /* Normally the message* functions will have already displayed and
13438 updated the echo area, but the frame may have been trashed, or
13439 the update may have been preempted, so display the echo area
13440 again here. Checking message_cleared_p captures the case that
13441 the echo area should be cleared. */
13442 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13443 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13444 || (message_cleared_p
13445 && minibuf_level == 0
13446 /* If the mini-window is currently selected, this means the
13447 echo-area doesn't show through. */
13448 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13449 {
13450 bool window_height_changed_p = echo_area_display (false);
13451
13452 if (message_cleared_p)
13453 update_miniwindow_p = true;
13454
13455 must_finish = true;
13456
13457 /* If we don't display the current message, don't clear the
13458 message_cleared_p flag, because, if we did, we wouldn't clear
13459 the echo area in the next redisplay which doesn't preserve
13460 the echo area. */
13461 if (!display_last_displayed_message_p)
13462 message_cleared_p = false;
13463
13464 if (window_height_changed_p)
13465 {
13466 windows_or_buffers_changed = 50;
13467
13468 /* If window configuration was changed, frames may have been
13469 marked garbaged. Clear them or we will experience
13470 surprises wrt scrolling. */
13471 clear_garbaged_frames ();
13472 }
13473 }
13474 else if (EQ (selected_window, minibuf_window)
13475 && (current_buffer->clip_changed || window_outdated (w))
13476 && resize_mini_window (w, false))
13477 {
13478 /* Resized active mini-window to fit the size of what it is
13479 showing if its contents might have changed. */
13480 must_finish = true;
13481
13482 /* If window configuration was changed, frames may have been
13483 marked garbaged. Clear them or we will experience
13484 surprises wrt scrolling. */
13485 clear_garbaged_frames ();
13486 }
13487
13488 if (windows_or_buffers_changed && !update_mode_lines)
13489 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13490 only the windows's contents needs to be refreshed, or whether the
13491 mode-lines also need a refresh. */
13492 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13493 ? REDISPLAY_SOME : 32);
13494
13495 /* If specs for an arrow have changed, do thorough redisplay
13496 to ensure we remove any arrow that should no longer exist. */
13497 if (overlay_arrows_changed_p ())
13498 /* Apparently, this is the only case where we update other windows,
13499 without updating other mode-lines. */
13500 windows_or_buffers_changed = 49;
13501
13502 consider_all_windows_p = (update_mode_lines
13503 || windows_or_buffers_changed);
13504
13505 #define AINC(a,i) \
13506 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13507 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13508
13509 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13510 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13511
13512 /* Optimize the case that only the line containing the cursor in the
13513 selected window has changed. Variables starting with this_ are
13514 set in display_line and record information about the line
13515 containing the cursor. */
13516 tlbufpos = this_line_start_pos;
13517 tlendpos = this_line_end_pos;
13518 if (!consider_all_windows_p
13519 && CHARPOS (tlbufpos) > 0
13520 && !w->update_mode_line
13521 && !current_buffer->clip_changed
13522 && !current_buffer->prevent_redisplay_optimizations_p
13523 && FRAME_VISIBLE_P (XFRAME (w->frame))
13524 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13525 && !XFRAME (w->frame)->cursor_type_changed
13526 /* Make sure recorded data applies to current buffer, etc. */
13527 && this_line_buffer == current_buffer
13528 && match_p
13529 && !w->force_start
13530 && !w->optional_new_start
13531 /* Point must be on the line that we have info recorded about. */
13532 && PT >= CHARPOS (tlbufpos)
13533 && PT <= Z - CHARPOS (tlendpos)
13534 /* All text outside that line, including its final newline,
13535 must be unchanged. */
13536 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13537 CHARPOS (tlendpos)))
13538 {
13539 if (CHARPOS (tlbufpos) > BEGV
13540 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13541 && (CHARPOS (tlbufpos) == ZV
13542 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13543 /* Former continuation line has disappeared by becoming empty. */
13544 goto cancel;
13545 else if (window_outdated (w) || MINI_WINDOW_P (w))
13546 {
13547 /* We have to handle the case of continuation around a
13548 wide-column character (see the comment in indent.c around
13549 line 1340).
13550
13551 For instance, in the following case:
13552
13553 -------- Insert --------
13554 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13555 J_I_ ==> J_I_ `^^' are cursors.
13556 ^^ ^^
13557 -------- --------
13558
13559 As we have to redraw the line above, we cannot use this
13560 optimization. */
13561
13562 struct it it;
13563 int line_height_before = this_line_pixel_height;
13564
13565 /* Note that start_display will handle the case that the
13566 line starting at tlbufpos is a continuation line. */
13567 start_display (&it, w, tlbufpos);
13568
13569 /* Implementation note: It this still necessary? */
13570 if (it.current_x != this_line_start_x)
13571 goto cancel;
13572
13573 TRACE ((stderr, "trying display optimization 1\n"));
13574 w->cursor.vpos = -1;
13575 overlay_arrow_seen = false;
13576 it.vpos = this_line_vpos;
13577 it.current_y = this_line_y;
13578 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13579 display_line (&it);
13580
13581 /* If line contains point, is not continued,
13582 and ends at same distance from eob as before, we win. */
13583 if (w->cursor.vpos >= 0
13584 /* Line is not continued, otherwise this_line_start_pos
13585 would have been set to 0 in display_line. */
13586 && CHARPOS (this_line_start_pos)
13587 /* Line ends as before. */
13588 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13589 /* Line has same height as before. Otherwise other lines
13590 would have to be shifted up or down. */
13591 && this_line_pixel_height == line_height_before)
13592 {
13593 /* If this is not the window's last line, we must adjust
13594 the charstarts of the lines below. */
13595 if (it.current_y < it.last_visible_y)
13596 {
13597 struct glyph_row *row
13598 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13599 ptrdiff_t delta, delta_bytes;
13600
13601 /* We used to distinguish between two cases here,
13602 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13603 when the line ends in a newline or the end of the
13604 buffer's accessible portion. But both cases did
13605 the same, so they were collapsed. */
13606 delta = (Z
13607 - CHARPOS (tlendpos)
13608 - MATRIX_ROW_START_CHARPOS (row));
13609 delta_bytes = (Z_BYTE
13610 - BYTEPOS (tlendpos)
13611 - MATRIX_ROW_START_BYTEPOS (row));
13612
13613 increment_matrix_positions (w->current_matrix,
13614 this_line_vpos + 1,
13615 w->current_matrix->nrows,
13616 delta, delta_bytes);
13617 }
13618
13619 /* If this row displays text now but previously didn't,
13620 or vice versa, w->window_end_vpos may have to be
13621 adjusted. */
13622 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13623 {
13624 if (w->window_end_vpos < this_line_vpos)
13625 w->window_end_vpos = this_line_vpos;
13626 }
13627 else if (w->window_end_vpos == this_line_vpos
13628 && this_line_vpos > 0)
13629 w->window_end_vpos = this_line_vpos - 1;
13630 w->window_end_valid = false;
13631
13632 /* Update hint: No need to try to scroll in update_window. */
13633 w->desired_matrix->no_scrolling_p = true;
13634
13635 #ifdef GLYPH_DEBUG
13636 *w->desired_matrix->method = 0;
13637 debug_method_add (w, "optimization 1");
13638 #endif
13639 #ifdef HAVE_WINDOW_SYSTEM
13640 update_window_fringes (w, false);
13641 #endif
13642 goto update;
13643 }
13644 else
13645 goto cancel;
13646 }
13647 else if (/* Cursor position hasn't changed. */
13648 PT == w->last_point
13649 /* Make sure the cursor was last displayed
13650 in this window. Otherwise we have to reposition it. */
13651
13652 /* PXW: Must be converted to pixels, probably. */
13653 && 0 <= w->cursor.vpos
13654 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13655 {
13656 if (!must_finish)
13657 {
13658 do_pending_window_change (true);
13659 /* If selected_window changed, redisplay again. */
13660 if (WINDOWP (selected_window)
13661 && (w = XWINDOW (selected_window)) != sw)
13662 goto retry;
13663
13664 /* We used to always goto end_of_redisplay here, but this
13665 isn't enough if we have a blinking cursor. */
13666 if (w->cursor_off_p == w->last_cursor_off_p)
13667 goto end_of_redisplay;
13668 }
13669 goto update;
13670 }
13671 /* If highlighting the region, or if the cursor is in the echo area,
13672 then we can't just move the cursor. */
13673 else if (NILP (Vshow_trailing_whitespace)
13674 && !cursor_in_echo_area)
13675 {
13676 struct it it;
13677 struct glyph_row *row;
13678
13679 /* Skip from tlbufpos to PT and see where it is. Note that
13680 PT may be in invisible text. If so, we will end at the
13681 next visible position. */
13682 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13683 NULL, DEFAULT_FACE_ID);
13684 it.current_x = this_line_start_x;
13685 it.current_y = this_line_y;
13686 it.vpos = this_line_vpos;
13687
13688 /* The call to move_it_to stops in front of PT, but
13689 moves over before-strings. */
13690 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13691
13692 if (it.vpos == this_line_vpos
13693 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13694 row->enabled_p))
13695 {
13696 eassert (this_line_vpos == it.vpos);
13697 eassert (this_line_y == it.current_y);
13698 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13699 #ifdef GLYPH_DEBUG
13700 *w->desired_matrix->method = 0;
13701 debug_method_add (w, "optimization 3");
13702 #endif
13703 goto update;
13704 }
13705 else
13706 goto cancel;
13707 }
13708
13709 cancel:
13710 /* Text changed drastically or point moved off of line. */
13711 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13712 }
13713
13714 CHARPOS (this_line_start_pos) = 0;
13715 ++clear_face_cache_count;
13716 #ifdef HAVE_WINDOW_SYSTEM
13717 ++clear_image_cache_count;
13718 #endif
13719
13720 /* Build desired matrices, and update the display. If
13721 consider_all_windows_p, do it for all windows on all frames.
13722 Otherwise do it for selected_window, only. */
13723
13724 if (consider_all_windows_p)
13725 {
13726 FOR_EACH_FRAME (tail, frame)
13727 XFRAME (frame)->updated_p = false;
13728
13729 propagate_buffer_redisplay ();
13730
13731 FOR_EACH_FRAME (tail, frame)
13732 {
13733 struct frame *f = XFRAME (frame);
13734
13735 /* We don't have to do anything for unselected terminal
13736 frames. */
13737 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13738 && !EQ (FRAME_TTY (f)->top_frame, frame))
13739 continue;
13740
13741 retry_frame:
13742
13743 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13744 /* Redisplay internal tool bar if this is the first time so we
13745 can adjust the frame height right now, if necessary. */
13746 if (!f->tool_bar_redisplayed_once)
13747 {
13748 if (redisplay_tool_bar (f))
13749 adjust_frame_glyphs (f);
13750 f->tool_bar_redisplayed_once = true;
13751 }
13752 #endif
13753
13754 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13755 {
13756 bool gcscrollbars
13757 /* Only GC scrollbars when we redisplay the whole frame. */
13758 = f->redisplay || !REDISPLAY_SOME_P ();
13759 /* Mark all the scroll bars to be removed; we'll redeem
13760 the ones we want when we redisplay their windows. */
13761 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13762 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13763
13764 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13765 redisplay_windows (FRAME_ROOT_WINDOW (f));
13766 /* Remember that the invisible frames need to be redisplayed next
13767 time they're visible. */
13768 else if (!REDISPLAY_SOME_P ())
13769 f->redisplay = true;
13770
13771 /* The X error handler may have deleted that frame. */
13772 if (!FRAME_LIVE_P (f))
13773 continue;
13774
13775 /* Any scroll bars which redisplay_windows should have
13776 nuked should now go away. */
13777 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13778 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13779
13780 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13781 {
13782 /* If fonts changed on visible frame, display again. */
13783 if (f->fonts_changed)
13784 {
13785 adjust_frame_glyphs (f);
13786 /* Disable all redisplay optimizations for this
13787 frame. For the reasons, see the comment near
13788 the previous call to adjust_frame_glyphs above. */
13789 SET_FRAME_GARBAGED (f);
13790 f->fonts_changed = false;
13791 goto retry_frame;
13792 }
13793
13794 /* See if we have to hscroll. */
13795 if (!f->already_hscrolled_p)
13796 {
13797 f->already_hscrolled_p = true;
13798 if (hscroll_windows (f->root_window))
13799 goto retry_frame;
13800 }
13801
13802 /* Prevent various kinds of signals during display
13803 update. stdio is not robust about handling
13804 signals, which can cause an apparent I/O error. */
13805 if (interrupt_input)
13806 unrequest_sigio ();
13807 STOP_POLLING;
13808
13809 pending |= update_frame (f, false, false);
13810 f->cursor_type_changed = false;
13811 f->updated_p = true;
13812 }
13813 }
13814 }
13815
13816 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13817
13818 if (!pending)
13819 {
13820 /* Do the mark_window_display_accurate after all windows have
13821 been redisplayed because this call resets flags in buffers
13822 which are needed for proper redisplay. */
13823 FOR_EACH_FRAME (tail, frame)
13824 {
13825 struct frame *f = XFRAME (frame);
13826 if (f->updated_p)
13827 {
13828 f->redisplay = false;
13829 mark_window_display_accurate (f->root_window, true);
13830 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13831 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13832 }
13833 }
13834 }
13835 }
13836 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13837 {
13838 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13839 struct frame *mini_frame;
13840
13841 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13842 /* Use list_of_error, not Qerror, so that
13843 we catch only errors and don't run the debugger. */
13844 internal_condition_case_1 (redisplay_window_1, selected_window,
13845 list_of_error,
13846 redisplay_window_error);
13847 if (update_miniwindow_p)
13848 internal_condition_case_1 (redisplay_window_1, mini_window,
13849 list_of_error,
13850 redisplay_window_error);
13851
13852 /* Compare desired and current matrices, perform output. */
13853
13854 update:
13855 /* If fonts changed, display again. */
13856 if (sf->fonts_changed)
13857 goto retry;
13858
13859 /* Prevent various kinds of signals during display update.
13860 stdio is not robust about handling signals,
13861 which can cause an apparent I/O error. */
13862 if (interrupt_input)
13863 unrequest_sigio ();
13864 STOP_POLLING;
13865
13866 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13867 {
13868 if (hscroll_windows (selected_window))
13869 goto retry;
13870
13871 XWINDOW (selected_window)->must_be_updated_p = true;
13872 pending = update_frame (sf, false, false);
13873 sf->cursor_type_changed = false;
13874 }
13875
13876 /* We may have called echo_area_display at the top of this
13877 function. If the echo area is on another frame, that may
13878 have put text on a frame other than the selected one, so the
13879 above call to update_frame would not have caught it. Catch
13880 it here. */
13881 mini_window = FRAME_MINIBUF_WINDOW (sf);
13882 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13883
13884 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13885 {
13886 XWINDOW (mini_window)->must_be_updated_p = true;
13887 pending |= update_frame (mini_frame, false, false);
13888 mini_frame->cursor_type_changed = false;
13889 if (!pending && hscroll_windows (mini_window))
13890 goto retry;
13891 }
13892 }
13893
13894 /* If display was paused because of pending input, make sure we do a
13895 thorough update the next time. */
13896 if (pending)
13897 {
13898 /* Prevent the optimization at the beginning of
13899 redisplay_internal that tries a single-line update of the
13900 line containing the cursor in the selected window. */
13901 CHARPOS (this_line_start_pos) = 0;
13902
13903 /* Let the overlay arrow be updated the next time. */
13904 update_overlay_arrows (0);
13905
13906 /* If we pause after scrolling, some rows in the current
13907 matrices of some windows are not valid. */
13908 if (!WINDOW_FULL_WIDTH_P (w)
13909 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13910 update_mode_lines = 36;
13911 }
13912 else
13913 {
13914 if (!consider_all_windows_p)
13915 {
13916 /* This has already been done above if
13917 consider_all_windows_p is set. */
13918 if (XBUFFER (w->contents)->text->redisplay
13919 && buffer_window_count (XBUFFER (w->contents)) > 1)
13920 /* This can happen if b->text->redisplay was set during
13921 jit-lock. */
13922 propagate_buffer_redisplay ();
13923 mark_window_display_accurate_1 (w, true);
13924
13925 /* Say overlay arrows are up to date. */
13926 update_overlay_arrows (1);
13927
13928 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13929 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13930 }
13931
13932 update_mode_lines = 0;
13933 windows_or_buffers_changed = 0;
13934 }
13935
13936 /* Start SIGIO interrupts coming again. Having them off during the
13937 code above makes it less likely one will discard output, but not
13938 impossible, since there might be stuff in the system buffer here.
13939 But it is much hairier to try to do anything about that. */
13940 if (interrupt_input)
13941 request_sigio ();
13942 RESUME_POLLING;
13943
13944 /* If a frame has become visible which was not before, redisplay
13945 again, so that we display it. Expose events for such a frame
13946 (which it gets when becoming visible) don't call the parts of
13947 redisplay constructing glyphs, so simply exposing a frame won't
13948 display anything in this case. So, we have to display these
13949 frames here explicitly. */
13950 if (!pending)
13951 {
13952 int new_count = 0;
13953
13954 FOR_EACH_FRAME (tail, frame)
13955 {
13956 if (XFRAME (frame)->visible)
13957 new_count++;
13958 }
13959
13960 if (new_count != number_of_visible_frames)
13961 windows_or_buffers_changed = 52;
13962 }
13963
13964 /* Change frame size now if a change is pending. */
13965 do_pending_window_change (true);
13966
13967 /* If we just did a pending size change, or have additional
13968 visible frames, or selected_window changed, redisplay again. */
13969 if ((windows_or_buffers_changed && !pending)
13970 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13971 goto retry;
13972
13973 /* Clear the face and image caches.
13974
13975 We used to do this only if consider_all_windows_p. But the cache
13976 needs to be cleared if a timer creates images in the current
13977 buffer (e.g. the test case in Bug#6230). */
13978
13979 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13980 {
13981 clear_face_cache (false);
13982 clear_face_cache_count = 0;
13983 }
13984
13985 #ifdef HAVE_WINDOW_SYSTEM
13986 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13987 {
13988 clear_image_caches (Qnil);
13989 clear_image_cache_count = 0;
13990 }
13991 #endif /* HAVE_WINDOW_SYSTEM */
13992
13993 end_of_redisplay:
13994 #ifdef HAVE_NS
13995 ns_set_doc_edited ();
13996 #endif
13997 if (interrupt_input && interrupts_deferred)
13998 request_sigio ();
13999
14000 unbind_to (count, Qnil);
14001 RESUME_POLLING;
14002 }
14003
14004
14005 /* Redisplay, but leave alone any recent echo area message unless
14006 another message has been requested in its place.
14007
14008 This is useful in situations where you need to redisplay but no
14009 user action has occurred, making it inappropriate for the message
14010 area to be cleared. See tracking_off and
14011 wait_reading_process_output for examples of these situations.
14012
14013 FROM_WHERE is an integer saying from where this function was
14014 called. This is useful for debugging. */
14015
14016 void
14017 redisplay_preserve_echo_area (int from_where)
14018 {
14019 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14020
14021 if (!NILP (echo_area_buffer[1]))
14022 {
14023 /* We have a previously displayed message, but no current
14024 message. Redisplay the previous message. */
14025 display_last_displayed_message_p = true;
14026 redisplay_internal ();
14027 display_last_displayed_message_p = false;
14028 }
14029 else
14030 redisplay_internal ();
14031
14032 flush_frame (SELECTED_FRAME ());
14033 }
14034
14035
14036 /* Function registered with record_unwind_protect in redisplay_internal. */
14037
14038 static void
14039 unwind_redisplay (void)
14040 {
14041 redisplaying_p = false;
14042 }
14043
14044
14045 /* Mark the display of leaf window W as accurate or inaccurate.
14046 If ACCURATE_P, mark display of W as accurate.
14047 If !ACCURATE_P, arrange for W to be redisplayed the next
14048 time redisplay_internal is called. */
14049
14050 static void
14051 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14052 {
14053 struct buffer *b = XBUFFER (w->contents);
14054
14055 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14056 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14057 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14058
14059 if (accurate_p)
14060 {
14061 b->clip_changed = false;
14062 b->prevent_redisplay_optimizations_p = false;
14063 eassert (buffer_window_count (b) > 0);
14064 /* Resetting b->text->redisplay is problematic!
14065 In order to make it safer to do it here, redisplay_internal must
14066 have copied all b->text->redisplay to their respective windows. */
14067 b->text->redisplay = false;
14068
14069 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14070 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14071 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14072 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14073
14074 w->current_matrix->buffer = b;
14075 w->current_matrix->begv = BUF_BEGV (b);
14076 w->current_matrix->zv = BUF_ZV (b);
14077
14078 w->last_cursor_vpos = w->cursor.vpos;
14079 w->last_cursor_off_p = w->cursor_off_p;
14080
14081 if (w == XWINDOW (selected_window))
14082 w->last_point = BUF_PT (b);
14083 else
14084 w->last_point = marker_position (w->pointm);
14085
14086 w->window_end_valid = true;
14087 w->update_mode_line = false;
14088 }
14089
14090 w->redisplay = !accurate_p;
14091 }
14092
14093
14094 /* Mark the display of windows in the window tree rooted at WINDOW as
14095 accurate or inaccurate. If ACCURATE_P, mark display of
14096 windows as accurate. If !ACCURATE_P, arrange for windows to
14097 be redisplayed the next time redisplay_internal is called. */
14098
14099 void
14100 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14101 {
14102 struct window *w;
14103
14104 for (; !NILP (window); window = w->next)
14105 {
14106 w = XWINDOW (window);
14107 if (WINDOWP (w->contents))
14108 mark_window_display_accurate (w->contents, accurate_p);
14109 else
14110 mark_window_display_accurate_1 (w, accurate_p);
14111 }
14112
14113 if (accurate_p)
14114 update_overlay_arrows (1);
14115 else
14116 /* Force a thorough redisplay the next time by setting
14117 last_arrow_position and last_arrow_string to t, which is
14118 unequal to any useful value of Voverlay_arrow_... */
14119 update_overlay_arrows (-1);
14120 }
14121
14122
14123 /* Return value in display table DP (Lisp_Char_Table *) for character
14124 C. Since a display table doesn't have any parent, we don't have to
14125 follow parent. Do not call this function directly but use the
14126 macro DISP_CHAR_VECTOR. */
14127
14128 Lisp_Object
14129 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14130 {
14131 Lisp_Object val;
14132
14133 if (ASCII_CHAR_P (c))
14134 {
14135 val = dp->ascii;
14136 if (SUB_CHAR_TABLE_P (val))
14137 val = XSUB_CHAR_TABLE (val)->contents[c];
14138 }
14139 else
14140 {
14141 Lisp_Object table;
14142
14143 XSETCHAR_TABLE (table, dp);
14144 val = char_table_ref (table, c);
14145 }
14146 if (NILP (val))
14147 val = dp->defalt;
14148 return val;
14149 }
14150
14151
14152 \f
14153 /***********************************************************************
14154 Window Redisplay
14155 ***********************************************************************/
14156
14157 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14158
14159 static void
14160 redisplay_windows (Lisp_Object window)
14161 {
14162 while (!NILP (window))
14163 {
14164 struct window *w = XWINDOW (window);
14165
14166 if (WINDOWP (w->contents))
14167 redisplay_windows (w->contents);
14168 else if (BUFFERP (w->contents))
14169 {
14170 displayed_buffer = XBUFFER (w->contents);
14171 /* Use list_of_error, not Qerror, so that
14172 we catch only errors and don't run the debugger. */
14173 internal_condition_case_1 (redisplay_window_0, window,
14174 list_of_error,
14175 redisplay_window_error);
14176 }
14177
14178 window = w->next;
14179 }
14180 }
14181
14182 static Lisp_Object
14183 redisplay_window_error (Lisp_Object ignore)
14184 {
14185 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14186 return Qnil;
14187 }
14188
14189 static Lisp_Object
14190 redisplay_window_0 (Lisp_Object window)
14191 {
14192 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14193 redisplay_window (window, false);
14194 return Qnil;
14195 }
14196
14197 static Lisp_Object
14198 redisplay_window_1 (Lisp_Object window)
14199 {
14200 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14201 redisplay_window (window, true);
14202 return Qnil;
14203 }
14204 \f
14205
14206 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14207 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14208 which positions recorded in ROW differ from current buffer
14209 positions.
14210
14211 Return true iff cursor is on this row. */
14212
14213 static bool
14214 set_cursor_from_row (struct window *w, struct glyph_row *row,
14215 struct glyph_matrix *matrix,
14216 ptrdiff_t delta, ptrdiff_t delta_bytes,
14217 int dy, int dvpos)
14218 {
14219 struct glyph *glyph = row->glyphs[TEXT_AREA];
14220 struct glyph *end = glyph + row->used[TEXT_AREA];
14221 struct glyph *cursor = NULL;
14222 /* The last known character position in row. */
14223 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14224 int x = row->x;
14225 ptrdiff_t pt_old = PT - delta;
14226 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14227 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14228 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14229 /* A glyph beyond the edge of TEXT_AREA which we should never
14230 touch. */
14231 struct glyph *glyphs_end = end;
14232 /* True means we've found a match for cursor position, but that
14233 glyph has the avoid_cursor_p flag set. */
14234 bool match_with_avoid_cursor = false;
14235 /* True means we've seen at least one glyph that came from a
14236 display string. */
14237 bool string_seen = false;
14238 /* Largest and smallest buffer positions seen so far during scan of
14239 glyph row. */
14240 ptrdiff_t bpos_max = pos_before;
14241 ptrdiff_t bpos_min = pos_after;
14242 /* Last buffer position covered by an overlay string with an integer
14243 `cursor' property. */
14244 ptrdiff_t bpos_covered = 0;
14245 /* True means the display string on which to display the cursor
14246 comes from a text property, not from an overlay. */
14247 bool string_from_text_prop = false;
14248
14249 /* Don't even try doing anything if called for a mode-line or
14250 header-line row, since the rest of the code isn't prepared to
14251 deal with such calamities. */
14252 eassert (!row->mode_line_p);
14253 if (row->mode_line_p)
14254 return false;
14255
14256 /* Skip over glyphs not having an object at the start and the end of
14257 the row. These are special glyphs like truncation marks on
14258 terminal frames. */
14259 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14260 {
14261 if (!row->reversed_p)
14262 {
14263 while (glyph < end
14264 && NILP (glyph->object)
14265 && glyph->charpos < 0)
14266 {
14267 x += glyph->pixel_width;
14268 ++glyph;
14269 }
14270 while (end > glyph
14271 && NILP ((end - 1)->object)
14272 /* CHARPOS is zero for blanks and stretch glyphs
14273 inserted by extend_face_to_end_of_line. */
14274 && (end - 1)->charpos <= 0)
14275 --end;
14276 glyph_before = glyph - 1;
14277 glyph_after = end;
14278 }
14279 else
14280 {
14281 struct glyph *g;
14282
14283 /* If the glyph row is reversed, we need to process it from back
14284 to front, so swap the edge pointers. */
14285 glyphs_end = end = glyph - 1;
14286 glyph += row->used[TEXT_AREA] - 1;
14287
14288 while (glyph > end + 1
14289 && NILP (glyph->object)
14290 && glyph->charpos < 0)
14291 {
14292 --glyph;
14293 x -= glyph->pixel_width;
14294 }
14295 if (NILP (glyph->object) && glyph->charpos < 0)
14296 --glyph;
14297 /* By default, in reversed rows we put the cursor on the
14298 rightmost (first in the reading order) glyph. */
14299 for (g = end + 1; g < glyph; g++)
14300 x += g->pixel_width;
14301 while (end < glyph
14302 && NILP ((end + 1)->object)
14303 && (end + 1)->charpos <= 0)
14304 ++end;
14305 glyph_before = glyph + 1;
14306 glyph_after = end;
14307 }
14308 }
14309 else if (row->reversed_p)
14310 {
14311 /* In R2L rows that don't display text, put the cursor on the
14312 rightmost glyph. Case in point: an empty last line that is
14313 part of an R2L paragraph. */
14314 cursor = end - 1;
14315 /* Avoid placing the cursor on the last glyph of the row, where
14316 on terminal frames we hold the vertical border between
14317 adjacent windows. */
14318 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14319 && !WINDOW_RIGHTMOST_P (w)
14320 && cursor == row->glyphs[LAST_AREA] - 1)
14321 cursor--;
14322 x = -1; /* will be computed below, at label compute_x */
14323 }
14324
14325 /* Step 1: Try to find the glyph whose character position
14326 corresponds to point. If that's not possible, find 2 glyphs
14327 whose character positions are the closest to point, one before
14328 point, the other after it. */
14329 if (!row->reversed_p)
14330 while (/* not marched to end of glyph row */
14331 glyph < end
14332 /* glyph was not inserted by redisplay for internal purposes */
14333 && !NILP (glyph->object))
14334 {
14335 if (BUFFERP (glyph->object))
14336 {
14337 ptrdiff_t dpos = glyph->charpos - pt_old;
14338
14339 if (glyph->charpos > bpos_max)
14340 bpos_max = glyph->charpos;
14341 if (glyph->charpos < bpos_min)
14342 bpos_min = glyph->charpos;
14343 if (!glyph->avoid_cursor_p)
14344 {
14345 /* If we hit point, we've found the glyph on which to
14346 display the cursor. */
14347 if (dpos == 0)
14348 {
14349 match_with_avoid_cursor = false;
14350 break;
14351 }
14352 /* See if we've found a better approximation to
14353 POS_BEFORE or to POS_AFTER. */
14354 if (0 > dpos && dpos > pos_before - pt_old)
14355 {
14356 pos_before = glyph->charpos;
14357 glyph_before = glyph;
14358 }
14359 else if (0 < dpos && dpos < pos_after - pt_old)
14360 {
14361 pos_after = glyph->charpos;
14362 glyph_after = glyph;
14363 }
14364 }
14365 else if (dpos == 0)
14366 match_with_avoid_cursor = true;
14367 }
14368 else if (STRINGP (glyph->object))
14369 {
14370 Lisp_Object chprop;
14371 ptrdiff_t glyph_pos = glyph->charpos;
14372
14373 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14374 glyph->object);
14375 if (!NILP (chprop))
14376 {
14377 /* If the string came from a `display' text property,
14378 look up the buffer position of that property and
14379 use that position to update bpos_max, as if we
14380 actually saw such a position in one of the row's
14381 glyphs. This helps with supporting integer values
14382 of `cursor' property on the display string in
14383 situations where most or all of the row's buffer
14384 text is completely covered by display properties,
14385 so that no glyph with valid buffer positions is
14386 ever seen in the row. */
14387 ptrdiff_t prop_pos =
14388 string_buffer_position_lim (glyph->object, pos_before,
14389 pos_after, false);
14390
14391 if (prop_pos >= pos_before)
14392 bpos_max = prop_pos;
14393 }
14394 if (INTEGERP (chprop))
14395 {
14396 bpos_covered = bpos_max + XINT (chprop);
14397 /* If the `cursor' property covers buffer positions up
14398 to and including point, we should display cursor on
14399 this glyph. Note that, if a `cursor' property on one
14400 of the string's characters has an integer value, we
14401 will break out of the loop below _before_ we get to
14402 the position match above. IOW, integer values of
14403 the `cursor' property override the "exact match for
14404 point" strategy of positioning the cursor. */
14405 /* Implementation note: bpos_max == pt_old when, e.g.,
14406 we are in an empty line, where bpos_max is set to
14407 MATRIX_ROW_START_CHARPOS, see above. */
14408 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14409 {
14410 cursor = glyph;
14411 break;
14412 }
14413 }
14414
14415 string_seen = true;
14416 }
14417 x += glyph->pixel_width;
14418 ++glyph;
14419 }
14420 else if (glyph > end) /* row is reversed */
14421 while (!NILP (glyph->object))
14422 {
14423 if (BUFFERP (glyph->object))
14424 {
14425 ptrdiff_t dpos = glyph->charpos - pt_old;
14426
14427 if (glyph->charpos > bpos_max)
14428 bpos_max = glyph->charpos;
14429 if (glyph->charpos < bpos_min)
14430 bpos_min = glyph->charpos;
14431 if (!glyph->avoid_cursor_p)
14432 {
14433 if (dpos == 0)
14434 {
14435 match_with_avoid_cursor = false;
14436 break;
14437 }
14438 if (0 > dpos && dpos > pos_before - pt_old)
14439 {
14440 pos_before = glyph->charpos;
14441 glyph_before = glyph;
14442 }
14443 else if (0 < dpos && dpos < pos_after - pt_old)
14444 {
14445 pos_after = glyph->charpos;
14446 glyph_after = glyph;
14447 }
14448 }
14449 else if (dpos == 0)
14450 match_with_avoid_cursor = true;
14451 }
14452 else if (STRINGP (glyph->object))
14453 {
14454 Lisp_Object chprop;
14455 ptrdiff_t glyph_pos = glyph->charpos;
14456
14457 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14458 glyph->object);
14459 if (!NILP (chprop))
14460 {
14461 ptrdiff_t prop_pos =
14462 string_buffer_position_lim (glyph->object, pos_before,
14463 pos_after, false);
14464
14465 if (prop_pos >= pos_before)
14466 bpos_max = prop_pos;
14467 }
14468 if (INTEGERP (chprop))
14469 {
14470 bpos_covered = bpos_max + XINT (chprop);
14471 /* If the `cursor' property covers buffer positions up
14472 to and including point, we should display cursor on
14473 this glyph. */
14474 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14475 {
14476 cursor = glyph;
14477 break;
14478 }
14479 }
14480 string_seen = true;
14481 }
14482 --glyph;
14483 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14484 {
14485 x--; /* can't use any pixel_width */
14486 break;
14487 }
14488 x -= glyph->pixel_width;
14489 }
14490
14491 /* Step 2: If we didn't find an exact match for point, we need to
14492 look for a proper place to put the cursor among glyphs between
14493 GLYPH_BEFORE and GLYPH_AFTER. */
14494 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14495 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14496 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14497 {
14498 /* An empty line has a single glyph whose OBJECT is nil and
14499 whose CHARPOS is the position of a newline on that line.
14500 Note that on a TTY, there are more glyphs after that, which
14501 were produced by extend_face_to_end_of_line, but their
14502 CHARPOS is zero or negative. */
14503 bool empty_line_p =
14504 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14505 && NILP (glyph->object) && glyph->charpos > 0
14506 /* On a TTY, continued and truncated rows also have a glyph at
14507 their end whose OBJECT is nil and whose CHARPOS is
14508 positive (the continuation and truncation glyphs), but such
14509 rows are obviously not "empty". */
14510 && !(row->continued_p || row->truncated_on_right_p));
14511
14512 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14513 {
14514 ptrdiff_t ellipsis_pos;
14515
14516 /* Scan back over the ellipsis glyphs. */
14517 if (!row->reversed_p)
14518 {
14519 ellipsis_pos = (glyph - 1)->charpos;
14520 while (glyph > row->glyphs[TEXT_AREA]
14521 && (glyph - 1)->charpos == ellipsis_pos)
14522 glyph--, x -= glyph->pixel_width;
14523 /* That loop always goes one position too far, including
14524 the glyph before the ellipsis. So scan forward over
14525 that one. */
14526 x += glyph->pixel_width;
14527 glyph++;
14528 }
14529 else /* row is reversed */
14530 {
14531 ellipsis_pos = (glyph + 1)->charpos;
14532 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14533 && (glyph + 1)->charpos == ellipsis_pos)
14534 glyph++, x += glyph->pixel_width;
14535 x -= glyph->pixel_width;
14536 glyph--;
14537 }
14538 }
14539 else if (match_with_avoid_cursor)
14540 {
14541 cursor = glyph_after;
14542 x = -1;
14543 }
14544 else if (string_seen)
14545 {
14546 int incr = row->reversed_p ? -1 : +1;
14547
14548 /* Need to find the glyph that came out of a string which is
14549 present at point. That glyph is somewhere between
14550 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14551 positioned between POS_BEFORE and POS_AFTER in the
14552 buffer. */
14553 struct glyph *start, *stop;
14554 ptrdiff_t pos = pos_before;
14555
14556 x = -1;
14557
14558 /* If the row ends in a newline from a display string,
14559 reordering could have moved the glyphs belonging to the
14560 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14561 in this case we extend the search to the last glyph in
14562 the row that was not inserted by redisplay. */
14563 if (row->ends_in_newline_from_string_p)
14564 {
14565 glyph_after = end;
14566 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14567 }
14568
14569 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14570 correspond to POS_BEFORE and POS_AFTER, respectively. We
14571 need START and STOP in the order that corresponds to the
14572 row's direction as given by its reversed_p flag. If the
14573 directionality of characters between POS_BEFORE and
14574 POS_AFTER is the opposite of the row's base direction,
14575 these characters will have been reordered for display,
14576 and we need to reverse START and STOP. */
14577 if (!row->reversed_p)
14578 {
14579 start = min (glyph_before, glyph_after);
14580 stop = max (glyph_before, glyph_after);
14581 }
14582 else
14583 {
14584 start = max (glyph_before, glyph_after);
14585 stop = min (glyph_before, glyph_after);
14586 }
14587 for (glyph = start + incr;
14588 row->reversed_p ? glyph > stop : glyph < stop; )
14589 {
14590
14591 /* Any glyphs that come from the buffer are here because
14592 of bidi reordering. Skip them, and only pay
14593 attention to glyphs that came from some string. */
14594 if (STRINGP (glyph->object))
14595 {
14596 Lisp_Object str;
14597 ptrdiff_t tem;
14598 /* If the display property covers the newline, we
14599 need to search for it one position farther. */
14600 ptrdiff_t lim = pos_after
14601 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14602
14603 string_from_text_prop = false;
14604 str = glyph->object;
14605 tem = string_buffer_position_lim (str, pos, lim, false);
14606 if (tem == 0 /* from overlay */
14607 || pos <= tem)
14608 {
14609 /* If the string from which this glyph came is
14610 found in the buffer at point, or at position
14611 that is closer to point than pos_after, then
14612 we've found the glyph we've been looking for.
14613 If it comes from an overlay (tem == 0), and
14614 it has the `cursor' property on one of its
14615 glyphs, record that glyph as a candidate for
14616 displaying the cursor. (As in the
14617 unidirectional version, we will display the
14618 cursor on the last candidate we find.) */
14619 if (tem == 0
14620 || tem == pt_old
14621 || (tem - pt_old > 0 && tem < pos_after))
14622 {
14623 /* The glyphs from this string could have
14624 been reordered. Find the one with the
14625 smallest string position. Or there could
14626 be a character in the string with the
14627 `cursor' property, which means display
14628 cursor on that character's glyph. */
14629 ptrdiff_t strpos = glyph->charpos;
14630
14631 if (tem)
14632 {
14633 cursor = glyph;
14634 string_from_text_prop = true;
14635 }
14636 for ( ;
14637 (row->reversed_p ? glyph > stop : glyph < stop)
14638 && EQ (glyph->object, str);
14639 glyph += incr)
14640 {
14641 Lisp_Object cprop;
14642 ptrdiff_t gpos = glyph->charpos;
14643
14644 cprop = Fget_char_property (make_number (gpos),
14645 Qcursor,
14646 glyph->object);
14647 if (!NILP (cprop))
14648 {
14649 cursor = glyph;
14650 break;
14651 }
14652 if (tem && glyph->charpos < strpos)
14653 {
14654 strpos = glyph->charpos;
14655 cursor = glyph;
14656 }
14657 }
14658
14659 if (tem == pt_old
14660 || (tem - pt_old > 0 && tem < pos_after))
14661 goto compute_x;
14662 }
14663 if (tem)
14664 pos = tem + 1; /* don't find previous instances */
14665 }
14666 /* This string is not what we want; skip all of the
14667 glyphs that came from it. */
14668 while ((row->reversed_p ? glyph > stop : glyph < stop)
14669 && EQ (glyph->object, str))
14670 glyph += incr;
14671 }
14672 else
14673 glyph += incr;
14674 }
14675
14676 /* If we reached the end of the line, and END was from a string,
14677 the cursor is not on this line. */
14678 if (cursor == NULL
14679 && (row->reversed_p ? glyph <= end : glyph >= end)
14680 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14681 && STRINGP (end->object)
14682 && row->continued_p)
14683 return false;
14684 }
14685 /* A truncated row may not include PT among its character positions.
14686 Setting the cursor inside the scroll margin will trigger
14687 recalculation of hscroll in hscroll_window_tree. But if a
14688 display string covers point, defer to the string-handling
14689 code below to figure this out. */
14690 else if (row->truncated_on_left_p && pt_old < bpos_min)
14691 {
14692 cursor = glyph_before;
14693 x = -1;
14694 }
14695 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14696 /* Zero-width characters produce no glyphs. */
14697 || (!empty_line_p
14698 && (row->reversed_p
14699 ? glyph_after > glyphs_end
14700 : glyph_after < glyphs_end)))
14701 {
14702 cursor = glyph_after;
14703 x = -1;
14704 }
14705 }
14706
14707 compute_x:
14708 if (cursor != NULL)
14709 glyph = cursor;
14710 else if (glyph == glyphs_end
14711 && pos_before == pos_after
14712 && STRINGP ((row->reversed_p
14713 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14714 : row->glyphs[TEXT_AREA])->object))
14715 {
14716 /* If all the glyphs of this row came from strings, put the
14717 cursor on the first glyph of the row. This avoids having the
14718 cursor outside of the text area in this very rare and hard
14719 use case. */
14720 glyph =
14721 row->reversed_p
14722 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14723 : row->glyphs[TEXT_AREA];
14724 }
14725 if (x < 0)
14726 {
14727 struct glyph *g;
14728
14729 /* Need to compute x that corresponds to GLYPH. */
14730 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14731 {
14732 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14733 emacs_abort ();
14734 x += g->pixel_width;
14735 }
14736 }
14737
14738 /* ROW could be part of a continued line, which, under bidi
14739 reordering, might have other rows whose start and end charpos
14740 occlude point. Only set w->cursor if we found a better
14741 approximation to the cursor position than we have from previously
14742 examined candidate rows belonging to the same continued line. */
14743 if (/* We already have a candidate row. */
14744 w->cursor.vpos >= 0
14745 /* That candidate is not the row we are processing. */
14746 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14747 /* Make sure cursor.vpos specifies a row whose start and end
14748 charpos occlude point, and it is valid candidate for being a
14749 cursor-row. This is because some callers of this function
14750 leave cursor.vpos at the row where the cursor was displayed
14751 during the last redisplay cycle. */
14752 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14753 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14754 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14755 {
14756 struct glyph *g1
14757 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14758
14759 /* Don't consider glyphs that are outside TEXT_AREA. */
14760 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14761 return false;
14762 /* Keep the candidate whose buffer position is the closest to
14763 point or has the `cursor' property. */
14764 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14765 w->cursor.hpos >= 0
14766 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14767 && ((BUFFERP (g1->object)
14768 && (g1->charpos == pt_old /* An exact match always wins. */
14769 || (BUFFERP (glyph->object)
14770 && eabs (g1->charpos - pt_old)
14771 < eabs (glyph->charpos - pt_old))))
14772 /* Previous candidate is a glyph from a string that has
14773 a non-nil `cursor' property. */
14774 || (STRINGP (g1->object)
14775 && (!NILP (Fget_char_property (make_number (g1->charpos),
14776 Qcursor, g1->object))
14777 /* Previous candidate is from the same display
14778 string as this one, and the display string
14779 came from a text property. */
14780 || (EQ (g1->object, glyph->object)
14781 && string_from_text_prop)
14782 /* this candidate is from newline and its
14783 position is not an exact match */
14784 || (NILP (glyph->object)
14785 && glyph->charpos != pt_old)))))
14786 return false;
14787 /* If this candidate gives an exact match, use that. */
14788 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14789 /* If this candidate is a glyph created for the
14790 terminating newline of a line, and point is on that
14791 newline, it wins because it's an exact match. */
14792 || (!row->continued_p
14793 && NILP (glyph->object)
14794 && glyph->charpos == 0
14795 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14796 /* Otherwise, keep the candidate that comes from a row
14797 spanning less buffer positions. This may win when one or
14798 both candidate positions are on glyphs that came from
14799 display strings, for which we cannot compare buffer
14800 positions. */
14801 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14802 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14803 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14804 return false;
14805 }
14806 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14807 w->cursor.x = x;
14808 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14809 w->cursor.y = row->y + dy;
14810
14811 if (w == XWINDOW (selected_window))
14812 {
14813 if (!row->continued_p
14814 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14815 && row->x == 0)
14816 {
14817 this_line_buffer = XBUFFER (w->contents);
14818
14819 CHARPOS (this_line_start_pos)
14820 = MATRIX_ROW_START_CHARPOS (row) + delta;
14821 BYTEPOS (this_line_start_pos)
14822 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14823
14824 CHARPOS (this_line_end_pos)
14825 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14826 BYTEPOS (this_line_end_pos)
14827 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14828
14829 this_line_y = w->cursor.y;
14830 this_line_pixel_height = row->height;
14831 this_line_vpos = w->cursor.vpos;
14832 this_line_start_x = row->x;
14833 }
14834 else
14835 CHARPOS (this_line_start_pos) = 0;
14836 }
14837
14838 return true;
14839 }
14840
14841
14842 /* Run window scroll functions, if any, for WINDOW with new window
14843 start STARTP. Sets the window start of WINDOW to that position.
14844
14845 We assume that the window's buffer is really current. */
14846
14847 static struct text_pos
14848 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14849 {
14850 struct window *w = XWINDOW (window);
14851 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14852
14853 eassert (current_buffer == XBUFFER (w->contents));
14854
14855 if (!NILP (Vwindow_scroll_functions))
14856 {
14857 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14858 make_number (CHARPOS (startp)));
14859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14860 /* In case the hook functions switch buffers. */
14861 set_buffer_internal (XBUFFER (w->contents));
14862 }
14863
14864 return startp;
14865 }
14866
14867
14868 /* Make sure the line containing the cursor is fully visible.
14869 A value of true means there is nothing to be done.
14870 (Either the line is fully visible, or it cannot be made so,
14871 or we cannot tell.)
14872
14873 If FORCE_P, return false even if partial visible cursor row
14874 is higher than window.
14875
14876 If CURRENT_MATRIX_P, use the information from the
14877 window's current glyph matrix; otherwise use the desired glyph
14878 matrix.
14879
14880 A value of false means the caller should do scrolling
14881 as if point had gone off the screen. */
14882
14883 static bool
14884 cursor_row_fully_visible_p (struct window *w, bool force_p,
14885 bool current_matrix_p)
14886 {
14887 struct glyph_matrix *matrix;
14888 struct glyph_row *row;
14889 int window_height;
14890
14891 if (!make_cursor_line_fully_visible_p)
14892 return true;
14893
14894 /* It's not always possible to find the cursor, e.g, when a window
14895 is full of overlay strings. Don't do anything in that case. */
14896 if (w->cursor.vpos < 0)
14897 return true;
14898
14899 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14900 row = MATRIX_ROW (matrix, w->cursor.vpos);
14901
14902 /* If the cursor row is not partially visible, there's nothing to do. */
14903 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14904 return true;
14905
14906 /* If the row the cursor is in is taller than the window's height,
14907 it's not clear what to do, so do nothing. */
14908 window_height = window_box_height (w);
14909 if (row->height >= window_height)
14910 {
14911 if (!force_p || MINI_WINDOW_P (w)
14912 || w->vscroll || w->cursor.vpos == 0)
14913 return true;
14914 }
14915 return false;
14916 }
14917
14918
14919 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14920 means only WINDOW is redisplayed in redisplay_internal.
14921 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14922 in redisplay_window to bring a partially visible line into view in
14923 the case that only the cursor has moved.
14924
14925 LAST_LINE_MISFIT should be true if we're scrolling because the
14926 last screen line's vertical height extends past the end of the screen.
14927
14928 Value is
14929
14930 1 if scrolling succeeded
14931
14932 0 if scrolling didn't find point.
14933
14934 -1 if new fonts have been loaded so that we must interrupt
14935 redisplay, adjust glyph matrices, and try again. */
14936
14937 enum
14938 {
14939 SCROLLING_SUCCESS,
14940 SCROLLING_FAILED,
14941 SCROLLING_NEED_LARGER_MATRICES
14942 };
14943
14944 /* If scroll-conservatively is more than this, never recenter.
14945
14946 If you change this, don't forget to update the doc string of
14947 `scroll-conservatively' and the Emacs manual. */
14948 #define SCROLL_LIMIT 100
14949
14950 static int
14951 try_scrolling (Lisp_Object window, bool just_this_one_p,
14952 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14953 bool temp_scroll_step, bool last_line_misfit)
14954 {
14955 struct window *w = XWINDOW (window);
14956 struct frame *f = XFRAME (w->frame);
14957 struct text_pos pos, startp;
14958 struct it it;
14959 int this_scroll_margin, scroll_max, rc, height;
14960 int dy = 0, amount_to_scroll = 0;
14961 bool scroll_down_p = false;
14962 int extra_scroll_margin_lines = last_line_misfit;
14963 Lisp_Object aggressive;
14964 /* We will never try scrolling more than this number of lines. */
14965 int scroll_limit = SCROLL_LIMIT;
14966 int frame_line_height = default_line_pixel_height (w);
14967 int window_total_lines
14968 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14969
14970 #ifdef GLYPH_DEBUG
14971 debug_method_add (w, "try_scrolling");
14972 #endif
14973
14974 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14975
14976 /* Compute scroll margin height in pixels. We scroll when point is
14977 within this distance from the top or bottom of the window. */
14978 if (scroll_margin > 0)
14979 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14980 * frame_line_height;
14981 else
14982 this_scroll_margin = 0;
14983
14984 /* Force arg_scroll_conservatively to have a reasonable value, to
14985 avoid scrolling too far away with slow move_it_* functions. Note
14986 that the user can supply scroll-conservatively equal to
14987 `most-positive-fixnum', which can be larger than INT_MAX. */
14988 if (arg_scroll_conservatively > scroll_limit)
14989 {
14990 arg_scroll_conservatively = scroll_limit + 1;
14991 scroll_max = scroll_limit * frame_line_height;
14992 }
14993 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14994 /* Compute how much we should try to scroll maximally to bring
14995 point into view. */
14996 scroll_max = (max (scroll_step,
14997 max (arg_scroll_conservatively, temp_scroll_step))
14998 * frame_line_height);
14999 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15000 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15001 /* We're trying to scroll because of aggressive scrolling but no
15002 scroll_step is set. Choose an arbitrary one. */
15003 scroll_max = 10 * frame_line_height;
15004 else
15005 scroll_max = 0;
15006
15007 too_near_end:
15008
15009 /* Decide whether to scroll down. */
15010 if (PT > CHARPOS (startp))
15011 {
15012 int scroll_margin_y;
15013
15014 /* Compute the pixel ypos of the scroll margin, then move IT to
15015 either that ypos or PT, whichever comes first. */
15016 start_display (&it, w, startp);
15017 scroll_margin_y = it.last_visible_y - this_scroll_margin
15018 - frame_line_height * extra_scroll_margin_lines;
15019 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15020 (MOVE_TO_POS | MOVE_TO_Y));
15021
15022 if (PT > CHARPOS (it.current.pos))
15023 {
15024 int y0 = line_bottom_y (&it);
15025 /* Compute how many pixels below window bottom to stop searching
15026 for PT. This avoids costly search for PT that is far away if
15027 the user limited scrolling by a small number of lines, but
15028 always finds PT if scroll_conservatively is set to a large
15029 number, such as most-positive-fixnum. */
15030 int slack = max (scroll_max, 10 * frame_line_height);
15031 int y_to_move = it.last_visible_y + slack;
15032
15033 /* Compute the distance from the scroll margin to PT or to
15034 the scroll limit, whichever comes first. This should
15035 include the height of the cursor line, to make that line
15036 fully visible. */
15037 move_it_to (&it, PT, -1, y_to_move,
15038 -1, MOVE_TO_POS | MOVE_TO_Y);
15039 dy = line_bottom_y (&it) - y0;
15040
15041 if (dy > scroll_max)
15042 return SCROLLING_FAILED;
15043
15044 if (dy > 0)
15045 scroll_down_p = true;
15046 }
15047 }
15048
15049 if (scroll_down_p)
15050 {
15051 /* Point is in or below the bottom scroll margin, so move the
15052 window start down. If scrolling conservatively, move it just
15053 enough down to make point visible. If scroll_step is set,
15054 move it down by scroll_step. */
15055 if (arg_scroll_conservatively)
15056 amount_to_scroll
15057 = min (max (dy, frame_line_height),
15058 frame_line_height * arg_scroll_conservatively);
15059 else if (scroll_step || temp_scroll_step)
15060 amount_to_scroll = scroll_max;
15061 else
15062 {
15063 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15064 height = WINDOW_BOX_TEXT_HEIGHT (w);
15065 if (NUMBERP (aggressive))
15066 {
15067 double float_amount = XFLOATINT (aggressive) * height;
15068 int aggressive_scroll = float_amount;
15069 if (aggressive_scroll == 0 && float_amount > 0)
15070 aggressive_scroll = 1;
15071 /* Don't let point enter the scroll margin near top of
15072 the window. This could happen if the value of
15073 scroll_up_aggressively is too large and there are
15074 non-zero margins, because scroll_up_aggressively
15075 means put point that fraction of window height
15076 _from_the_bottom_margin_. */
15077 if (aggressive_scroll + 2 * this_scroll_margin > height)
15078 aggressive_scroll = height - 2 * this_scroll_margin;
15079 amount_to_scroll = dy + aggressive_scroll;
15080 }
15081 }
15082
15083 if (amount_to_scroll <= 0)
15084 return SCROLLING_FAILED;
15085
15086 start_display (&it, w, startp);
15087 if (arg_scroll_conservatively <= scroll_limit)
15088 move_it_vertically (&it, amount_to_scroll);
15089 else
15090 {
15091 /* Extra precision for users who set scroll-conservatively
15092 to a large number: make sure the amount we scroll
15093 the window start is never less than amount_to_scroll,
15094 which was computed as distance from window bottom to
15095 point. This matters when lines at window top and lines
15096 below window bottom have different height. */
15097 struct it it1;
15098 void *it1data = NULL;
15099 /* We use a temporary it1 because line_bottom_y can modify
15100 its argument, if it moves one line down; see there. */
15101 int start_y;
15102
15103 SAVE_IT (it1, it, it1data);
15104 start_y = line_bottom_y (&it1);
15105 do {
15106 RESTORE_IT (&it, &it, it1data);
15107 move_it_by_lines (&it, 1);
15108 SAVE_IT (it1, it, it1data);
15109 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15110 }
15111
15112 /* If STARTP is unchanged, move it down another screen line. */
15113 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15114 move_it_by_lines (&it, 1);
15115 startp = it.current.pos;
15116 }
15117 else
15118 {
15119 struct text_pos scroll_margin_pos = startp;
15120 int y_offset = 0;
15121
15122 /* See if point is inside the scroll margin at the top of the
15123 window. */
15124 if (this_scroll_margin)
15125 {
15126 int y_start;
15127
15128 start_display (&it, w, startp);
15129 y_start = it.current_y;
15130 move_it_vertically (&it, this_scroll_margin);
15131 scroll_margin_pos = it.current.pos;
15132 /* If we didn't move enough before hitting ZV, request
15133 additional amount of scroll, to move point out of the
15134 scroll margin. */
15135 if (IT_CHARPOS (it) == ZV
15136 && it.current_y - y_start < this_scroll_margin)
15137 y_offset = this_scroll_margin - (it.current_y - y_start);
15138 }
15139
15140 if (PT < CHARPOS (scroll_margin_pos))
15141 {
15142 /* Point is in the scroll margin at the top of the window or
15143 above what is displayed in the window. */
15144 int y0, y_to_move;
15145
15146 /* Compute the vertical distance from PT to the scroll
15147 margin position. Move as far as scroll_max allows, or
15148 one screenful, or 10 screen lines, whichever is largest.
15149 Give up if distance is greater than scroll_max or if we
15150 didn't reach the scroll margin position. */
15151 SET_TEXT_POS (pos, PT, PT_BYTE);
15152 start_display (&it, w, pos);
15153 y0 = it.current_y;
15154 y_to_move = max (it.last_visible_y,
15155 max (scroll_max, 10 * frame_line_height));
15156 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15157 y_to_move, -1,
15158 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15159 dy = it.current_y - y0;
15160 if (dy > scroll_max
15161 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15162 return SCROLLING_FAILED;
15163
15164 /* Additional scroll for when ZV was too close to point. */
15165 dy += y_offset;
15166
15167 /* Compute new window start. */
15168 start_display (&it, w, startp);
15169
15170 if (arg_scroll_conservatively)
15171 amount_to_scroll = max (dy, frame_line_height
15172 * max (scroll_step, temp_scroll_step));
15173 else if (scroll_step || temp_scroll_step)
15174 amount_to_scroll = scroll_max;
15175 else
15176 {
15177 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15178 height = WINDOW_BOX_TEXT_HEIGHT (w);
15179 if (NUMBERP (aggressive))
15180 {
15181 double float_amount = XFLOATINT (aggressive) * height;
15182 int aggressive_scroll = float_amount;
15183 if (aggressive_scroll == 0 && float_amount > 0)
15184 aggressive_scroll = 1;
15185 /* Don't let point enter the scroll margin near
15186 bottom of the window, if the value of
15187 scroll_down_aggressively happens to be too
15188 large. */
15189 if (aggressive_scroll + 2 * this_scroll_margin > height)
15190 aggressive_scroll = height - 2 * this_scroll_margin;
15191 amount_to_scroll = dy + aggressive_scroll;
15192 }
15193 }
15194
15195 if (amount_to_scroll <= 0)
15196 return SCROLLING_FAILED;
15197
15198 move_it_vertically_backward (&it, amount_to_scroll);
15199 startp = it.current.pos;
15200 }
15201 }
15202
15203 /* Run window scroll functions. */
15204 startp = run_window_scroll_functions (window, startp);
15205
15206 /* Display the window. Give up if new fonts are loaded, or if point
15207 doesn't appear. */
15208 if (!try_window (window, startp, 0))
15209 rc = SCROLLING_NEED_LARGER_MATRICES;
15210 else if (w->cursor.vpos < 0)
15211 {
15212 clear_glyph_matrix (w->desired_matrix);
15213 rc = SCROLLING_FAILED;
15214 }
15215 else
15216 {
15217 /* Maybe forget recorded base line for line number display. */
15218 if (!just_this_one_p
15219 || current_buffer->clip_changed
15220 || BEG_UNCHANGED < CHARPOS (startp))
15221 w->base_line_number = 0;
15222
15223 /* If cursor ends up on a partially visible line,
15224 treat that as being off the bottom of the screen. */
15225 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15226 false)
15227 /* It's possible that the cursor is on the first line of the
15228 buffer, which is partially obscured due to a vscroll
15229 (Bug#7537). In that case, avoid looping forever. */
15230 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15231 {
15232 clear_glyph_matrix (w->desired_matrix);
15233 ++extra_scroll_margin_lines;
15234 goto too_near_end;
15235 }
15236 rc = SCROLLING_SUCCESS;
15237 }
15238
15239 return rc;
15240 }
15241
15242
15243 /* Compute a suitable window start for window W if display of W starts
15244 on a continuation line. Value is true if a new window start
15245 was computed.
15246
15247 The new window start will be computed, based on W's width, starting
15248 from the start of the continued line. It is the start of the
15249 screen line with the minimum distance from the old start W->start. */
15250
15251 static bool
15252 compute_window_start_on_continuation_line (struct window *w)
15253 {
15254 struct text_pos pos, start_pos;
15255 bool window_start_changed_p = false;
15256
15257 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15258
15259 /* If window start is on a continuation line... Window start may be
15260 < BEGV in case there's invisible text at the start of the
15261 buffer (M-x rmail, for example). */
15262 if (CHARPOS (start_pos) > BEGV
15263 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15264 {
15265 struct it it;
15266 struct glyph_row *row;
15267
15268 /* Handle the case that the window start is out of range. */
15269 if (CHARPOS (start_pos) < BEGV)
15270 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15271 else if (CHARPOS (start_pos) > ZV)
15272 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15273
15274 /* Find the start of the continued line. This should be fast
15275 because find_newline is fast (newline cache). */
15276 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15277 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15278 row, DEFAULT_FACE_ID);
15279 reseat_at_previous_visible_line_start (&it);
15280
15281 /* If the line start is "too far" away from the window start,
15282 say it takes too much time to compute a new window start. */
15283 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15284 /* PXW: Do we need upper bounds here? */
15285 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15286 {
15287 int min_distance, distance;
15288
15289 /* Move forward by display lines to find the new window
15290 start. If window width was enlarged, the new start can
15291 be expected to be > the old start. If window width was
15292 decreased, the new window start will be < the old start.
15293 So, we're looking for the display line start with the
15294 minimum distance from the old window start. */
15295 pos = it.current.pos;
15296 min_distance = INFINITY;
15297 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15298 distance < min_distance)
15299 {
15300 min_distance = distance;
15301 pos = it.current.pos;
15302 if (it.line_wrap == WORD_WRAP)
15303 {
15304 /* Under WORD_WRAP, move_it_by_lines is likely to
15305 overshoot and stop not at the first, but the
15306 second character from the left margin. So in
15307 that case, we need a more tight control on the X
15308 coordinate of the iterator than move_it_by_lines
15309 promises in its contract. The method is to first
15310 go to the last (rightmost) visible character of a
15311 line, then move to the leftmost character on the
15312 next line in a separate call. */
15313 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15314 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15315 move_it_to (&it, ZV, 0,
15316 it.current_y + it.max_ascent + it.max_descent, -1,
15317 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15318 }
15319 else
15320 move_it_by_lines (&it, 1);
15321 }
15322
15323 /* Set the window start there. */
15324 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15325 window_start_changed_p = true;
15326 }
15327 }
15328
15329 return window_start_changed_p;
15330 }
15331
15332
15333 /* Try cursor movement in case text has not changed in window WINDOW,
15334 with window start STARTP. Value is
15335
15336 CURSOR_MOVEMENT_SUCCESS if successful
15337
15338 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15339
15340 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15341 display. *SCROLL_STEP is set to true, under certain circumstances, if
15342 we want to scroll as if scroll-step were set to 1. See the code.
15343
15344 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15345 which case we have to abort this redisplay, and adjust matrices
15346 first. */
15347
15348 enum
15349 {
15350 CURSOR_MOVEMENT_SUCCESS,
15351 CURSOR_MOVEMENT_CANNOT_BE_USED,
15352 CURSOR_MOVEMENT_MUST_SCROLL,
15353 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15354 };
15355
15356 static int
15357 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15358 bool *scroll_step)
15359 {
15360 struct window *w = XWINDOW (window);
15361 struct frame *f = XFRAME (w->frame);
15362 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15363
15364 #ifdef GLYPH_DEBUG
15365 if (inhibit_try_cursor_movement)
15366 return rc;
15367 #endif
15368
15369 /* Previously, there was a check for Lisp integer in the
15370 if-statement below. Now, this field is converted to
15371 ptrdiff_t, thus zero means invalid position in a buffer. */
15372 eassert (w->last_point > 0);
15373 /* Likewise there was a check whether window_end_vpos is nil or larger
15374 than the window. Now window_end_vpos is int and so never nil, but
15375 let's leave eassert to check whether it fits in the window. */
15376 eassert (!w->window_end_valid
15377 || w->window_end_vpos < w->current_matrix->nrows);
15378
15379 /* Handle case where text has not changed, only point, and it has
15380 not moved off the frame. */
15381 if (/* Point may be in this window. */
15382 PT >= CHARPOS (startp)
15383 /* Selective display hasn't changed. */
15384 && !current_buffer->clip_changed
15385 /* Function force-mode-line-update is used to force a thorough
15386 redisplay. It sets either windows_or_buffers_changed or
15387 update_mode_lines. So don't take a shortcut here for these
15388 cases. */
15389 && !update_mode_lines
15390 && !windows_or_buffers_changed
15391 && !f->cursor_type_changed
15392 && NILP (Vshow_trailing_whitespace)
15393 /* This code is not used for mini-buffer for the sake of the case
15394 of redisplaying to replace an echo area message; since in
15395 that case the mini-buffer contents per se are usually
15396 unchanged. This code is of no real use in the mini-buffer
15397 since the handling of this_line_start_pos, etc., in redisplay
15398 handles the same cases. */
15399 && !EQ (window, minibuf_window)
15400 && (FRAME_WINDOW_P (f)
15401 || !overlay_arrow_in_current_buffer_p ()))
15402 {
15403 int this_scroll_margin, top_scroll_margin;
15404 struct glyph_row *row = NULL;
15405 int frame_line_height = default_line_pixel_height (w);
15406 int window_total_lines
15407 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15408
15409 #ifdef GLYPH_DEBUG
15410 debug_method_add (w, "cursor movement");
15411 #endif
15412
15413 /* Scroll if point within this distance from the top or bottom
15414 of the window. This is a pixel value. */
15415 if (scroll_margin > 0)
15416 {
15417 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15418 this_scroll_margin *= frame_line_height;
15419 }
15420 else
15421 this_scroll_margin = 0;
15422
15423 top_scroll_margin = this_scroll_margin;
15424 if (WINDOW_WANTS_HEADER_LINE_P (w))
15425 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15426
15427 /* Start with the row the cursor was displayed during the last
15428 not paused redisplay. Give up if that row is not valid. */
15429 if (w->last_cursor_vpos < 0
15430 || w->last_cursor_vpos >= w->current_matrix->nrows)
15431 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15432 else
15433 {
15434 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15435 if (row->mode_line_p)
15436 ++row;
15437 if (!row->enabled_p)
15438 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15439 }
15440
15441 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15442 {
15443 bool scroll_p = false, must_scroll = false;
15444 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15445
15446 if (PT > w->last_point)
15447 {
15448 /* Point has moved forward. */
15449 while (MATRIX_ROW_END_CHARPOS (row) < PT
15450 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15451 {
15452 eassert (row->enabled_p);
15453 ++row;
15454 }
15455
15456 /* If the end position of a row equals the start
15457 position of the next row, and PT is at that position,
15458 we would rather display cursor in the next line. */
15459 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15460 && MATRIX_ROW_END_CHARPOS (row) == PT
15461 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15462 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15463 && !cursor_row_p (row))
15464 ++row;
15465
15466 /* If within the scroll margin, scroll. Note that
15467 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15468 the next line would be drawn, and that
15469 this_scroll_margin can be zero. */
15470 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15471 || PT > MATRIX_ROW_END_CHARPOS (row)
15472 /* Line is completely visible last line in window
15473 and PT is to be set in the next line. */
15474 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15475 && PT == MATRIX_ROW_END_CHARPOS (row)
15476 && !row->ends_at_zv_p
15477 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15478 scroll_p = true;
15479 }
15480 else if (PT < w->last_point)
15481 {
15482 /* Cursor has to be moved backward. Note that PT >=
15483 CHARPOS (startp) because of the outer if-statement. */
15484 while (!row->mode_line_p
15485 && (MATRIX_ROW_START_CHARPOS (row) > PT
15486 || (MATRIX_ROW_START_CHARPOS (row) == PT
15487 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15488 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15489 row > w->current_matrix->rows
15490 && (row-1)->ends_in_newline_from_string_p))))
15491 && (row->y > top_scroll_margin
15492 || CHARPOS (startp) == BEGV))
15493 {
15494 eassert (row->enabled_p);
15495 --row;
15496 }
15497
15498 /* Consider the following case: Window starts at BEGV,
15499 there is invisible, intangible text at BEGV, so that
15500 display starts at some point START > BEGV. It can
15501 happen that we are called with PT somewhere between
15502 BEGV and START. Try to handle that case. */
15503 if (row < w->current_matrix->rows
15504 || row->mode_line_p)
15505 {
15506 row = w->current_matrix->rows;
15507 if (row->mode_line_p)
15508 ++row;
15509 }
15510
15511 /* Due to newlines in overlay strings, we may have to
15512 skip forward over overlay strings. */
15513 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15514 && MATRIX_ROW_END_CHARPOS (row) == PT
15515 && !cursor_row_p (row))
15516 ++row;
15517
15518 /* If within the scroll margin, scroll. */
15519 if (row->y < top_scroll_margin
15520 && CHARPOS (startp) != BEGV)
15521 scroll_p = true;
15522 }
15523 else
15524 {
15525 /* Cursor did not move. So don't scroll even if cursor line
15526 is partially visible, as it was so before. */
15527 rc = CURSOR_MOVEMENT_SUCCESS;
15528 }
15529
15530 if (PT < MATRIX_ROW_START_CHARPOS (row)
15531 || PT > MATRIX_ROW_END_CHARPOS (row))
15532 {
15533 /* if PT is not in the glyph row, give up. */
15534 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15535 must_scroll = true;
15536 }
15537 else if (rc != CURSOR_MOVEMENT_SUCCESS
15538 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15539 {
15540 struct glyph_row *row1;
15541
15542 /* If rows are bidi-reordered and point moved, back up
15543 until we find a row that does not belong to a
15544 continuation line. This is because we must consider
15545 all rows of a continued line as candidates for the
15546 new cursor positioning, since row start and end
15547 positions change non-linearly with vertical position
15548 in such rows. */
15549 /* FIXME: Revisit this when glyph ``spilling'' in
15550 continuation lines' rows is implemented for
15551 bidi-reordered rows. */
15552 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15553 MATRIX_ROW_CONTINUATION_LINE_P (row);
15554 --row)
15555 {
15556 /* If we hit the beginning of the displayed portion
15557 without finding the first row of a continued
15558 line, give up. */
15559 if (row <= row1)
15560 {
15561 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15562 break;
15563 }
15564 eassert (row->enabled_p);
15565 }
15566 }
15567 if (must_scroll)
15568 ;
15569 else if (rc != CURSOR_MOVEMENT_SUCCESS
15570 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15571 /* Make sure this isn't a header line by any chance, since
15572 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15573 && !row->mode_line_p
15574 && make_cursor_line_fully_visible_p)
15575 {
15576 if (PT == MATRIX_ROW_END_CHARPOS (row)
15577 && !row->ends_at_zv_p
15578 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15579 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15580 else if (row->height > window_box_height (w))
15581 {
15582 /* If we end up in a partially visible line, let's
15583 make it fully visible, except when it's taller
15584 than the window, in which case we can't do much
15585 about it. */
15586 *scroll_step = true;
15587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15588 }
15589 else
15590 {
15591 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15592 if (!cursor_row_fully_visible_p (w, false, true))
15593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15594 else
15595 rc = CURSOR_MOVEMENT_SUCCESS;
15596 }
15597 }
15598 else if (scroll_p)
15599 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15600 else if (rc != CURSOR_MOVEMENT_SUCCESS
15601 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15602 {
15603 /* With bidi-reordered rows, there could be more than
15604 one candidate row whose start and end positions
15605 occlude point. We need to let set_cursor_from_row
15606 find the best candidate. */
15607 /* FIXME: Revisit this when glyph ``spilling'' in
15608 continuation lines' rows is implemented for
15609 bidi-reordered rows. */
15610 bool rv = false;
15611
15612 do
15613 {
15614 bool at_zv_p = false, exact_match_p = false;
15615
15616 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15617 && PT <= MATRIX_ROW_END_CHARPOS (row)
15618 && cursor_row_p (row))
15619 rv |= set_cursor_from_row (w, row, w->current_matrix,
15620 0, 0, 0, 0);
15621 /* As soon as we've found the exact match for point,
15622 or the first suitable row whose ends_at_zv_p flag
15623 is set, we are done. */
15624 if (rv)
15625 {
15626 at_zv_p = MATRIX_ROW (w->current_matrix,
15627 w->cursor.vpos)->ends_at_zv_p;
15628 if (!at_zv_p
15629 && w->cursor.hpos >= 0
15630 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15631 w->cursor.vpos))
15632 {
15633 struct glyph_row *candidate =
15634 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15635 struct glyph *g =
15636 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15637 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15638
15639 exact_match_p =
15640 (BUFFERP (g->object) && g->charpos == PT)
15641 || (NILP (g->object)
15642 && (g->charpos == PT
15643 || (g->charpos == 0 && endpos - 1 == PT)));
15644 }
15645 if (at_zv_p || exact_match_p)
15646 {
15647 rc = CURSOR_MOVEMENT_SUCCESS;
15648 break;
15649 }
15650 }
15651 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15652 break;
15653 ++row;
15654 }
15655 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15656 || row->continued_p)
15657 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15658 || (MATRIX_ROW_START_CHARPOS (row) == PT
15659 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15660 /* If we didn't find any candidate rows, or exited the
15661 loop before all the candidates were examined, signal
15662 to the caller that this method failed. */
15663 if (rc != CURSOR_MOVEMENT_SUCCESS
15664 && !(rv
15665 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15666 && !row->continued_p))
15667 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15668 else if (rv)
15669 rc = CURSOR_MOVEMENT_SUCCESS;
15670 }
15671 else
15672 {
15673 do
15674 {
15675 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15676 {
15677 rc = CURSOR_MOVEMENT_SUCCESS;
15678 break;
15679 }
15680 ++row;
15681 }
15682 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15683 && MATRIX_ROW_START_CHARPOS (row) == PT
15684 && cursor_row_p (row));
15685 }
15686 }
15687 }
15688
15689 return rc;
15690 }
15691
15692
15693 void
15694 set_vertical_scroll_bar (struct window *w)
15695 {
15696 ptrdiff_t start, end, whole;
15697
15698 /* Calculate the start and end positions for the current window.
15699 At some point, it would be nice to choose between scrollbars
15700 which reflect the whole buffer size, with special markers
15701 indicating narrowing, and scrollbars which reflect only the
15702 visible region.
15703
15704 Note that mini-buffers sometimes aren't displaying any text. */
15705 if (!MINI_WINDOW_P (w)
15706 || (w == XWINDOW (minibuf_window)
15707 && NILP (echo_area_buffer[0])))
15708 {
15709 struct buffer *buf = XBUFFER (w->contents);
15710 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15711 start = marker_position (w->start) - BUF_BEGV (buf);
15712 /* I don't think this is guaranteed to be right. For the
15713 moment, we'll pretend it is. */
15714 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15715
15716 if (end < start)
15717 end = start;
15718 if (whole < (end - start))
15719 whole = end - start;
15720 }
15721 else
15722 start = end = whole = 0;
15723
15724 /* Indicate what this scroll bar ought to be displaying now. */
15725 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15726 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15727 (w, end - start, whole, start);
15728 }
15729
15730
15731 void
15732 set_horizontal_scroll_bar (struct window *w)
15733 {
15734 int start, end, whole, portion;
15735
15736 if (!MINI_WINDOW_P (w)
15737 || (w == XWINDOW (minibuf_window)
15738 && NILP (echo_area_buffer[0])))
15739 {
15740 struct buffer *b = XBUFFER (w->contents);
15741 struct buffer *old_buffer = NULL;
15742 struct it it;
15743 struct text_pos startp;
15744
15745 if (b != current_buffer)
15746 {
15747 old_buffer = current_buffer;
15748 set_buffer_internal (b);
15749 }
15750
15751 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15752 start_display (&it, w, startp);
15753 it.last_visible_x = INT_MAX;
15754 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15755 MOVE_TO_X | MOVE_TO_Y);
15756 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15757 window_box_height (w), -1,
15758 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15759
15760 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15761 end = start + window_box_width (w, TEXT_AREA);
15762 portion = end - start;
15763 /* After enlarging a horizontally scrolled window such that it
15764 gets at least as wide as the text it contains, make sure that
15765 the thumb doesn't fill the entire scroll bar so we can still
15766 drag it back to see the entire text. */
15767 whole = max (whole, end);
15768
15769 if (it.bidi_p)
15770 {
15771 Lisp_Object pdir;
15772
15773 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15774 if (EQ (pdir, Qright_to_left))
15775 {
15776 start = whole - end;
15777 end = start + portion;
15778 }
15779 }
15780
15781 if (old_buffer)
15782 set_buffer_internal (old_buffer);
15783 }
15784 else
15785 start = end = whole = portion = 0;
15786
15787 w->hscroll_whole = whole;
15788
15789 /* Indicate what this scroll bar ought to be displaying now. */
15790 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15791 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15792 (w, portion, whole, start);
15793 }
15794
15795
15796 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15797 selected_window is redisplayed.
15798
15799 We can return without actually redisplaying the window if fonts has been
15800 changed on window's frame. In that case, redisplay_internal will retry.
15801
15802 As one of the important parts of redisplaying a window, we need to
15803 decide whether the previous window-start position (stored in the
15804 window's w->start marker position) is still valid, and if it isn't,
15805 recompute it. Some details about that:
15806
15807 . The previous window-start could be in a continuation line, in
15808 which case we need to recompute it when the window width
15809 changes. See compute_window_start_on_continuation_line and its
15810 call below.
15811
15812 . The text that changed since last redisplay could include the
15813 previous window-start position. In that case, we try to salvage
15814 what we can from the current glyph matrix by calling
15815 try_scrolling, which see.
15816
15817 . Some Emacs command could force us to use a specific window-start
15818 position by setting the window's force_start flag, or gently
15819 propose doing that by setting the window's optional_new_start
15820 flag. In these cases, we try using the specified start point if
15821 that succeeds (i.e. the window desired matrix is successfully
15822 recomputed, and point location is within the window). In case
15823 of optional_new_start, we first check if the specified start
15824 position is feasible, i.e. if it will allow point to be
15825 displayed in the window. If using the specified start point
15826 fails, e.g., if new fonts are needed to be loaded, we abort the
15827 redisplay cycle and leave it up to the next cycle to figure out
15828 things.
15829
15830 . Note that the window's force_start flag is sometimes set by
15831 redisplay itself, when it decides that the previous window start
15832 point is fine and should be kept. Search for "goto force_start"
15833 below to see the details. Like the values of window-start
15834 specified outside of redisplay, these internally-deduced values
15835 are tested for feasibility, and ignored if found to be
15836 unfeasible.
15837
15838 . Note that the function try_window, used to completely redisplay
15839 a window, accepts the window's start point as its argument.
15840 This is used several times in the redisplay code to control
15841 where the window start will be, according to user options such
15842 as scroll-conservatively, and also to ensure the screen line
15843 showing point will be fully (as opposed to partially) visible on
15844 display. */
15845
15846 static void
15847 redisplay_window (Lisp_Object window, bool just_this_one_p)
15848 {
15849 struct window *w = XWINDOW (window);
15850 struct frame *f = XFRAME (w->frame);
15851 struct buffer *buffer = XBUFFER (w->contents);
15852 struct buffer *old = current_buffer;
15853 struct text_pos lpoint, opoint, startp;
15854 bool update_mode_line;
15855 int tem;
15856 struct it it;
15857 /* Record it now because it's overwritten. */
15858 bool current_matrix_up_to_date_p = false;
15859 bool used_current_matrix_p = false;
15860 /* This is less strict than current_matrix_up_to_date_p.
15861 It indicates that the buffer contents and narrowing are unchanged. */
15862 bool buffer_unchanged_p = false;
15863 bool temp_scroll_step = false;
15864 ptrdiff_t count = SPECPDL_INDEX ();
15865 int rc;
15866 int centering_position = -1;
15867 bool last_line_misfit = false;
15868 ptrdiff_t beg_unchanged, end_unchanged;
15869 int frame_line_height;
15870
15871 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15872 opoint = lpoint;
15873
15874 #ifdef GLYPH_DEBUG
15875 *w->desired_matrix->method = 0;
15876 #endif
15877
15878 if (!just_this_one_p
15879 && REDISPLAY_SOME_P ()
15880 && !w->redisplay
15881 && !w->update_mode_line
15882 && !f->redisplay
15883 && !buffer->text->redisplay
15884 && BUF_PT (buffer) == w->last_point)
15885 return;
15886
15887 /* Make sure that both W's markers are valid. */
15888 eassert (XMARKER (w->start)->buffer == buffer);
15889 eassert (XMARKER (w->pointm)->buffer == buffer);
15890
15891 /* We come here again if we need to run window-text-change-functions
15892 below. */
15893 restart:
15894 reconsider_clip_changes (w);
15895 frame_line_height = default_line_pixel_height (w);
15896
15897 /* Has the mode line to be updated? */
15898 update_mode_line = (w->update_mode_line
15899 || update_mode_lines
15900 || buffer->clip_changed
15901 || buffer->prevent_redisplay_optimizations_p);
15902
15903 if (!just_this_one_p)
15904 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15905 cleverly elsewhere. */
15906 w->must_be_updated_p = true;
15907
15908 if (MINI_WINDOW_P (w))
15909 {
15910 if (w == XWINDOW (echo_area_window)
15911 && !NILP (echo_area_buffer[0]))
15912 {
15913 if (update_mode_line)
15914 /* We may have to update a tty frame's menu bar or a
15915 tool-bar. Example `M-x C-h C-h C-g'. */
15916 goto finish_menu_bars;
15917 else
15918 /* We've already displayed the echo area glyphs in this window. */
15919 goto finish_scroll_bars;
15920 }
15921 else if ((w != XWINDOW (minibuf_window)
15922 || minibuf_level == 0)
15923 /* When buffer is nonempty, redisplay window normally. */
15924 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15925 /* Quail displays non-mini buffers in minibuffer window.
15926 In that case, redisplay the window normally. */
15927 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15928 {
15929 /* W is a mini-buffer window, but it's not active, so clear
15930 it. */
15931 int yb = window_text_bottom_y (w);
15932 struct glyph_row *row;
15933 int y;
15934
15935 for (y = 0, row = w->desired_matrix->rows;
15936 y < yb;
15937 y += row->height, ++row)
15938 blank_row (w, row, y);
15939 goto finish_scroll_bars;
15940 }
15941
15942 clear_glyph_matrix (w->desired_matrix);
15943 }
15944
15945 /* Otherwise set up data on this window; select its buffer and point
15946 value. */
15947 /* Really select the buffer, for the sake of buffer-local
15948 variables. */
15949 set_buffer_internal_1 (XBUFFER (w->contents));
15950
15951 current_matrix_up_to_date_p
15952 = (w->window_end_valid
15953 && !current_buffer->clip_changed
15954 && !current_buffer->prevent_redisplay_optimizations_p
15955 && !window_outdated (w));
15956
15957 /* Run the window-text-change-functions
15958 if it is possible that the text on the screen has changed
15959 (either due to modification of the text, or any other reason). */
15960 if (!current_matrix_up_to_date_p
15961 && !NILP (Vwindow_text_change_functions))
15962 {
15963 safe_run_hooks (Qwindow_text_change_functions);
15964 goto restart;
15965 }
15966
15967 beg_unchanged = BEG_UNCHANGED;
15968 end_unchanged = END_UNCHANGED;
15969
15970 SET_TEXT_POS (opoint, PT, PT_BYTE);
15971
15972 specbind (Qinhibit_point_motion_hooks, Qt);
15973
15974 buffer_unchanged_p
15975 = (w->window_end_valid
15976 && !current_buffer->clip_changed
15977 && !window_outdated (w));
15978
15979 /* When windows_or_buffers_changed is non-zero, we can't rely
15980 on the window end being valid, so set it to zero there. */
15981 if (windows_or_buffers_changed)
15982 {
15983 /* If window starts on a continuation line, maybe adjust the
15984 window start in case the window's width changed. */
15985 if (XMARKER (w->start)->buffer == current_buffer)
15986 compute_window_start_on_continuation_line (w);
15987
15988 w->window_end_valid = false;
15989 /* If so, we also can't rely on current matrix
15990 and should not fool try_cursor_movement below. */
15991 current_matrix_up_to_date_p = false;
15992 }
15993
15994 /* Some sanity checks. */
15995 CHECK_WINDOW_END (w);
15996 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15997 emacs_abort ();
15998 if (BYTEPOS (opoint) < CHARPOS (opoint))
15999 emacs_abort ();
16000
16001 if (mode_line_update_needed (w))
16002 update_mode_line = true;
16003
16004 /* Point refers normally to the selected window. For any other
16005 window, set up appropriate value. */
16006 if (!EQ (window, selected_window))
16007 {
16008 ptrdiff_t new_pt = marker_position (w->pointm);
16009 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16010
16011 if (new_pt < BEGV)
16012 {
16013 new_pt = BEGV;
16014 new_pt_byte = BEGV_BYTE;
16015 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16016 }
16017 else if (new_pt > (ZV - 1))
16018 {
16019 new_pt = ZV;
16020 new_pt_byte = ZV_BYTE;
16021 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16022 }
16023
16024 /* We don't use SET_PT so that the point-motion hooks don't run. */
16025 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16026 }
16027
16028 /* If any of the character widths specified in the display table
16029 have changed, invalidate the width run cache. It's true that
16030 this may be a bit late to catch such changes, but the rest of
16031 redisplay goes (non-fatally) haywire when the display table is
16032 changed, so why should we worry about doing any better? */
16033 if (current_buffer->width_run_cache
16034 || (current_buffer->base_buffer
16035 && current_buffer->base_buffer->width_run_cache))
16036 {
16037 struct Lisp_Char_Table *disptab = buffer_display_table ();
16038
16039 if (! disptab_matches_widthtab
16040 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16041 {
16042 struct buffer *buf = current_buffer;
16043
16044 if (buf->base_buffer)
16045 buf = buf->base_buffer;
16046 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16047 recompute_width_table (current_buffer, disptab);
16048 }
16049 }
16050
16051 /* If window-start is screwed up, choose a new one. */
16052 if (XMARKER (w->start)->buffer != current_buffer)
16053 goto recenter;
16054
16055 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16056
16057 /* If someone specified a new starting point but did not insist,
16058 check whether it can be used. */
16059 if ((w->optional_new_start || window_frozen_p (w))
16060 && CHARPOS (startp) >= BEGV
16061 && CHARPOS (startp) <= ZV)
16062 {
16063 ptrdiff_t it_charpos;
16064
16065 w->optional_new_start = false;
16066 start_display (&it, w, startp);
16067 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16068 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16069 /* Record IT's position now, since line_bottom_y might change
16070 that. */
16071 it_charpos = IT_CHARPOS (it);
16072 /* Make sure we set the force_start flag only if the cursor row
16073 will be fully visible. Otherwise, the code under force_start
16074 label below will try to move point back into view, which is
16075 not what the code which sets optional_new_start wants. */
16076 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16077 && !w->force_start)
16078 {
16079 if (it_charpos == PT)
16080 w->force_start = true;
16081 /* IT may overshoot PT if text at PT is invisible. */
16082 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16083 w->force_start = true;
16084 #ifdef GLYPH_DEBUG
16085 if (w->force_start)
16086 {
16087 if (window_frozen_p (w))
16088 debug_method_add (w, "set force_start from frozen window start");
16089 else
16090 debug_method_add (w, "set force_start from optional_new_start");
16091 }
16092 #endif
16093 }
16094 }
16095
16096 force_start:
16097
16098 /* Handle case where place to start displaying has been specified,
16099 unless the specified location is outside the accessible range. */
16100 if (w->force_start)
16101 {
16102 /* We set this later on if we have to adjust point. */
16103 int new_vpos = -1;
16104
16105 w->force_start = false;
16106 w->vscroll = 0;
16107 w->window_end_valid = false;
16108
16109 /* Forget any recorded base line for line number display. */
16110 if (!buffer_unchanged_p)
16111 w->base_line_number = 0;
16112
16113 /* Redisplay the mode line. Select the buffer properly for that.
16114 Also, run the hook window-scroll-functions
16115 because we have scrolled. */
16116 /* Note, we do this after clearing force_start because
16117 if there's an error, it is better to forget about force_start
16118 than to get into an infinite loop calling the hook functions
16119 and having them get more errors. */
16120 if (!update_mode_line
16121 || ! NILP (Vwindow_scroll_functions))
16122 {
16123 update_mode_line = true;
16124 w->update_mode_line = true;
16125 startp = run_window_scroll_functions (window, startp);
16126 }
16127
16128 if (CHARPOS (startp) < BEGV)
16129 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16130 else if (CHARPOS (startp) > ZV)
16131 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16132
16133 /* Redisplay, then check if cursor has been set during the
16134 redisplay. Give up if new fonts were loaded. */
16135 /* We used to issue a CHECK_MARGINS argument to try_window here,
16136 but this causes scrolling to fail when point begins inside
16137 the scroll margin (bug#148) -- cyd */
16138 if (!try_window (window, startp, 0))
16139 {
16140 w->force_start = true;
16141 clear_glyph_matrix (w->desired_matrix);
16142 goto need_larger_matrices;
16143 }
16144
16145 if (w->cursor.vpos < 0)
16146 {
16147 /* If point does not appear, try to move point so it does
16148 appear. The desired matrix has been built above, so we
16149 can use it here. */
16150 new_vpos = window_box_height (w) / 2;
16151 }
16152
16153 if (!cursor_row_fully_visible_p (w, false, false))
16154 {
16155 /* Point does appear, but on a line partly visible at end of window.
16156 Move it back to a fully-visible line. */
16157 new_vpos = window_box_height (w);
16158 /* But if window_box_height suggests a Y coordinate that is
16159 not less than we already have, that line will clearly not
16160 be fully visible, so give up and scroll the display.
16161 This can happen when the default face uses a font whose
16162 dimensions are different from the frame's default
16163 font. */
16164 if (new_vpos >= w->cursor.y)
16165 {
16166 w->cursor.vpos = -1;
16167 clear_glyph_matrix (w->desired_matrix);
16168 goto try_to_scroll;
16169 }
16170 }
16171 else if (w->cursor.vpos >= 0)
16172 {
16173 /* Some people insist on not letting point enter the scroll
16174 margin, even though this part handles windows that didn't
16175 scroll at all. */
16176 int window_total_lines
16177 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16178 int margin = min (scroll_margin, window_total_lines / 4);
16179 int pixel_margin = margin * frame_line_height;
16180 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16181
16182 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16183 below, which finds the row to move point to, advances by
16184 the Y coordinate of the _next_ row, see the definition of
16185 MATRIX_ROW_BOTTOM_Y. */
16186 if (w->cursor.vpos < margin + header_line)
16187 {
16188 w->cursor.vpos = -1;
16189 clear_glyph_matrix (w->desired_matrix);
16190 goto try_to_scroll;
16191 }
16192 else
16193 {
16194 int window_height = window_box_height (w);
16195
16196 if (header_line)
16197 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16198 if (w->cursor.y >= window_height - pixel_margin)
16199 {
16200 w->cursor.vpos = -1;
16201 clear_glyph_matrix (w->desired_matrix);
16202 goto try_to_scroll;
16203 }
16204 }
16205 }
16206
16207 /* If we need to move point for either of the above reasons,
16208 now actually do it. */
16209 if (new_vpos >= 0)
16210 {
16211 struct glyph_row *row;
16212
16213 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16214 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16215 ++row;
16216
16217 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16218 MATRIX_ROW_START_BYTEPOS (row));
16219
16220 if (w != XWINDOW (selected_window))
16221 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16222 else if (current_buffer == old)
16223 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16224
16225 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16226
16227 /* Re-run pre-redisplay-function so it can update the region
16228 according to the new position of point. */
16229 /* Other than the cursor, w's redisplay is done so we can set its
16230 redisplay to false. Also the buffer's redisplay can be set to
16231 false, since propagate_buffer_redisplay should have already
16232 propagated its info to `w' anyway. */
16233 w->redisplay = false;
16234 XBUFFER (w->contents)->text->redisplay = false;
16235 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16236
16237 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16238 {
16239 /* pre-redisplay-function made changes (e.g. move the region)
16240 that require another round of redisplay. */
16241 clear_glyph_matrix (w->desired_matrix);
16242 if (!try_window (window, startp, 0))
16243 goto need_larger_matrices;
16244 }
16245 }
16246 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16247 {
16248 clear_glyph_matrix (w->desired_matrix);
16249 goto try_to_scroll;
16250 }
16251
16252 #ifdef GLYPH_DEBUG
16253 debug_method_add (w, "forced window start");
16254 #endif
16255 goto done;
16256 }
16257
16258 /* Handle case where text has not changed, only point, and it has
16259 not moved off the frame, and we are not retrying after hscroll.
16260 (current_matrix_up_to_date_p is true when retrying.) */
16261 if (current_matrix_up_to_date_p
16262 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16263 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16264 {
16265 switch (rc)
16266 {
16267 case CURSOR_MOVEMENT_SUCCESS:
16268 used_current_matrix_p = true;
16269 goto done;
16270
16271 case CURSOR_MOVEMENT_MUST_SCROLL:
16272 goto try_to_scroll;
16273
16274 default:
16275 emacs_abort ();
16276 }
16277 }
16278 /* If current starting point was originally the beginning of a line
16279 but no longer is, find a new starting point. */
16280 else if (w->start_at_line_beg
16281 && !(CHARPOS (startp) <= BEGV
16282 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16283 {
16284 #ifdef GLYPH_DEBUG
16285 debug_method_add (w, "recenter 1");
16286 #endif
16287 goto recenter;
16288 }
16289
16290 /* Try scrolling with try_window_id. Value is > 0 if update has
16291 been done, it is -1 if we know that the same window start will
16292 not work. It is 0 if unsuccessful for some other reason. */
16293 else if ((tem = try_window_id (w)) != 0)
16294 {
16295 #ifdef GLYPH_DEBUG
16296 debug_method_add (w, "try_window_id %d", tem);
16297 #endif
16298
16299 if (f->fonts_changed)
16300 goto need_larger_matrices;
16301 if (tem > 0)
16302 goto done;
16303
16304 /* Otherwise try_window_id has returned -1 which means that we
16305 don't want the alternative below this comment to execute. */
16306 }
16307 else if (CHARPOS (startp) >= BEGV
16308 && CHARPOS (startp) <= ZV
16309 && PT >= CHARPOS (startp)
16310 && (CHARPOS (startp) < ZV
16311 /* Avoid starting at end of buffer. */
16312 || CHARPOS (startp) == BEGV
16313 || !window_outdated (w)))
16314 {
16315 int d1, d2, d5, d6;
16316 int rtop, rbot;
16317
16318 /* If first window line is a continuation line, and window start
16319 is inside the modified region, but the first change is before
16320 current window start, we must select a new window start.
16321
16322 However, if this is the result of a down-mouse event (e.g. by
16323 extending the mouse-drag-overlay), we don't want to select a
16324 new window start, since that would change the position under
16325 the mouse, resulting in an unwanted mouse-movement rather
16326 than a simple mouse-click. */
16327 if (!w->start_at_line_beg
16328 && NILP (do_mouse_tracking)
16329 && CHARPOS (startp) > BEGV
16330 && CHARPOS (startp) > BEG + beg_unchanged
16331 && CHARPOS (startp) <= Z - end_unchanged
16332 /* Even if w->start_at_line_beg is nil, a new window may
16333 start at a line_beg, since that's how set_buffer_window
16334 sets it. So, we need to check the return value of
16335 compute_window_start_on_continuation_line. (See also
16336 bug#197). */
16337 && XMARKER (w->start)->buffer == current_buffer
16338 && compute_window_start_on_continuation_line (w)
16339 /* It doesn't make sense to force the window start like we
16340 do at label force_start if it is already known that point
16341 will not be fully visible in the resulting window, because
16342 doing so will move point from its correct position
16343 instead of scrolling the window to bring point into view.
16344 See bug#9324. */
16345 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16346 /* A very tall row could need more than the window height,
16347 in which case we accept that it is partially visible. */
16348 && (rtop != 0) == (rbot != 0))
16349 {
16350 w->force_start = true;
16351 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16352 #ifdef GLYPH_DEBUG
16353 debug_method_add (w, "recomputed window start in continuation line");
16354 #endif
16355 goto force_start;
16356 }
16357
16358 #ifdef GLYPH_DEBUG
16359 debug_method_add (w, "same window start");
16360 #endif
16361
16362 /* Try to redisplay starting at same place as before.
16363 If point has not moved off frame, accept the results. */
16364 if (!current_matrix_up_to_date_p
16365 /* Don't use try_window_reusing_current_matrix in this case
16366 because a window scroll function can have changed the
16367 buffer. */
16368 || !NILP (Vwindow_scroll_functions)
16369 || MINI_WINDOW_P (w)
16370 || !(used_current_matrix_p
16371 = try_window_reusing_current_matrix (w)))
16372 {
16373 IF_DEBUG (debug_method_add (w, "1"));
16374 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16375 /* -1 means we need to scroll.
16376 0 means we need new matrices, but fonts_changed
16377 is set in that case, so we will detect it below. */
16378 goto try_to_scroll;
16379 }
16380
16381 if (f->fonts_changed)
16382 goto need_larger_matrices;
16383
16384 if (w->cursor.vpos >= 0)
16385 {
16386 if (!just_this_one_p
16387 || current_buffer->clip_changed
16388 || BEG_UNCHANGED < CHARPOS (startp))
16389 /* Forget any recorded base line for line number display. */
16390 w->base_line_number = 0;
16391
16392 if (!cursor_row_fully_visible_p (w, true, false))
16393 {
16394 clear_glyph_matrix (w->desired_matrix);
16395 last_line_misfit = true;
16396 }
16397 /* Drop through and scroll. */
16398 else
16399 goto done;
16400 }
16401 else
16402 clear_glyph_matrix (w->desired_matrix);
16403 }
16404
16405 try_to_scroll:
16406
16407 /* Redisplay the mode line. Select the buffer properly for that. */
16408 if (!update_mode_line)
16409 {
16410 update_mode_line = true;
16411 w->update_mode_line = true;
16412 }
16413
16414 /* Try to scroll by specified few lines. */
16415 if ((scroll_conservatively
16416 || emacs_scroll_step
16417 || temp_scroll_step
16418 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16419 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16420 && CHARPOS (startp) >= BEGV
16421 && CHARPOS (startp) <= ZV)
16422 {
16423 /* The function returns -1 if new fonts were loaded, 1 if
16424 successful, 0 if not successful. */
16425 int ss = try_scrolling (window, just_this_one_p,
16426 scroll_conservatively,
16427 emacs_scroll_step,
16428 temp_scroll_step, last_line_misfit);
16429 switch (ss)
16430 {
16431 case SCROLLING_SUCCESS:
16432 goto done;
16433
16434 case SCROLLING_NEED_LARGER_MATRICES:
16435 goto need_larger_matrices;
16436
16437 case SCROLLING_FAILED:
16438 break;
16439
16440 default:
16441 emacs_abort ();
16442 }
16443 }
16444
16445 /* Finally, just choose a place to start which positions point
16446 according to user preferences. */
16447
16448 recenter:
16449
16450 #ifdef GLYPH_DEBUG
16451 debug_method_add (w, "recenter");
16452 #endif
16453
16454 /* Forget any previously recorded base line for line number display. */
16455 if (!buffer_unchanged_p)
16456 w->base_line_number = 0;
16457
16458 /* Determine the window start relative to point. */
16459 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16460 it.current_y = it.last_visible_y;
16461 if (centering_position < 0)
16462 {
16463 int window_total_lines
16464 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16465 int margin
16466 = scroll_margin > 0
16467 ? min (scroll_margin, window_total_lines / 4)
16468 : 0;
16469 ptrdiff_t margin_pos = CHARPOS (startp);
16470 Lisp_Object aggressive;
16471 bool scrolling_up;
16472
16473 /* If there is a scroll margin at the top of the window, find
16474 its character position. */
16475 if (margin
16476 /* Cannot call start_display if startp is not in the
16477 accessible region of the buffer. This can happen when we
16478 have just switched to a different buffer and/or changed
16479 its restriction. In that case, startp is initialized to
16480 the character position 1 (BEGV) because we did not yet
16481 have chance to display the buffer even once. */
16482 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16483 {
16484 struct it it1;
16485 void *it1data = NULL;
16486
16487 SAVE_IT (it1, it, it1data);
16488 start_display (&it1, w, startp);
16489 move_it_vertically (&it1, margin * frame_line_height);
16490 margin_pos = IT_CHARPOS (it1);
16491 RESTORE_IT (&it, &it, it1data);
16492 }
16493 scrolling_up = PT > margin_pos;
16494 aggressive =
16495 scrolling_up
16496 ? BVAR (current_buffer, scroll_up_aggressively)
16497 : BVAR (current_buffer, scroll_down_aggressively);
16498
16499 if (!MINI_WINDOW_P (w)
16500 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16501 {
16502 int pt_offset = 0;
16503
16504 /* Setting scroll-conservatively overrides
16505 scroll-*-aggressively. */
16506 if (!scroll_conservatively && NUMBERP (aggressive))
16507 {
16508 double float_amount = XFLOATINT (aggressive);
16509
16510 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16511 if (pt_offset == 0 && float_amount > 0)
16512 pt_offset = 1;
16513 if (pt_offset && margin > 0)
16514 margin -= 1;
16515 }
16516 /* Compute how much to move the window start backward from
16517 point so that point will be displayed where the user
16518 wants it. */
16519 if (scrolling_up)
16520 {
16521 centering_position = it.last_visible_y;
16522 if (pt_offset)
16523 centering_position -= pt_offset;
16524 centering_position -=
16525 (frame_line_height * (1 + margin + last_line_misfit)
16526 + WINDOW_HEADER_LINE_HEIGHT (w));
16527 /* Don't let point enter the scroll margin near top of
16528 the window. */
16529 if (centering_position < margin * frame_line_height)
16530 centering_position = margin * frame_line_height;
16531 }
16532 else
16533 centering_position = margin * frame_line_height + pt_offset;
16534 }
16535 else
16536 /* Set the window start half the height of the window backward
16537 from point. */
16538 centering_position = window_box_height (w) / 2;
16539 }
16540 move_it_vertically_backward (&it, centering_position);
16541
16542 eassert (IT_CHARPOS (it) >= BEGV);
16543
16544 /* The function move_it_vertically_backward may move over more
16545 than the specified y-distance. If it->w is small, e.g. a
16546 mini-buffer window, we may end up in front of the window's
16547 display area. Start displaying at the start of the line
16548 containing PT in this case. */
16549 if (it.current_y <= 0)
16550 {
16551 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16552 move_it_vertically_backward (&it, 0);
16553 it.current_y = 0;
16554 }
16555
16556 it.current_x = it.hpos = 0;
16557
16558 /* Set the window start position here explicitly, to avoid an
16559 infinite loop in case the functions in window-scroll-functions
16560 get errors. */
16561 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16562
16563 /* Run scroll hooks. */
16564 startp = run_window_scroll_functions (window, it.current.pos);
16565
16566 /* Redisplay the window. */
16567 if (!current_matrix_up_to_date_p
16568 || windows_or_buffers_changed
16569 || f->cursor_type_changed
16570 /* Don't use try_window_reusing_current_matrix in this case
16571 because it can have changed the buffer. */
16572 || !NILP (Vwindow_scroll_functions)
16573 || !just_this_one_p
16574 || MINI_WINDOW_P (w)
16575 || !(used_current_matrix_p
16576 = try_window_reusing_current_matrix (w)))
16577 try_window (window, startp, 0);
16578
16579 /* If new fonts have been loaded (due to fontsets), give up. We
16580 have to start a new redisplay since we need to re-adjust glyph
16581 matrices. */
16582 if (f->fonts_changed)
16583 goto need_larger_matrices;
16584
16585 /* If cursor did not appear assume that the middle of the window is
16586 in the first line of the window. Do it again with the next line.
16587 (Imagine a window of height 100, displaying two lines of height
16588 60. Moving back 50 from it->last_visible_y will end in the first
16589 line.) */
16590 if (w->cursor.vpos < 0)
16591 {
16592 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16593 {
16594 clear_glyph_matrix (w->desired_matrix);
16595 move_it_by_lines (&it, 1);
16596 try_window (window, it.current.pos, 0);
16597 }
16598 else if (PT < IT_CHARPOS (it))
16599 {
16600 clear_glyph_matrix (w->desired_matrix);
16601 move_it_by_lines (&it, -1);
16602 try_window (window, it.current.pos, 0);
16603 }
16604 else
16605 {
16606 /* Not much we can do about it. */
16607 }
16608 }
16609
16610 /* Consider the following case: Window starts at BEGV, there is
16611 invisible, intangible text at BEGV, so that display starts at
16612 some point START > BEGV. It can happen that we are called with
16613 PT somewhere between BEGV and START. Try to handle that case,
16614 and similar ones. */
16615 if (w->cursor.vpos < 0)
16616 {
16617 /* First, try locating the proper glyph row for PT. */
16618 struct glyph_row *row =
16619 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16620
16621 /* Sometimes point is at the beginning of invisible text that is
16622 before the 1st character displayed in the row. In that case,
16623 row_containing_pos fails to find the row, because no glyphs
16624 with appropriate buffer positions are present in the row.
16625 Therefore, we next try to find the row which shows the 1st
16626 position after the invisible text. */
16627 if (!row)
16628 {
16629 Lisp_Object val =
16630 get_char_property_and_overlay (make_number (PT), Qinvisible,
16631 Qnil, NULL);
16632
16633 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16634 {
16635 ptrdiff_t alt_pos;
16636 Lisp_Object invis_end =
16637 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16638 Qnil, Qnil);
16639
16640 if (NATNUMP (invis_end))
16641 alt_pos = XFASTINT (invis_end);
16642 else
16643 alt_pos = ZV;
16644 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16645 NULL, 0);
16646 }
16647 }
16648 /* Finally, fall back on the first row of the window after the
16649 header line (if any). This is slightly better than not
16650 displaying the cursor at all. */
16651 if (!row)
16652 {
16653 row = w->current_matrix->rows;
16654 if (row->mode_line_p)
16655 ++row;
16656 }
16657 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16658 }
16659
16660 if (!cursor_row_fully_visible_p (w, false, false))
16661 {
16662 /* If vscroll is enabled, disable it and try again. */
16663 if (w->vscroll)
16664 {
16665 w->vscroll = 0;
16666 clear_glyph_matrix (w->desired_matrix);
16667 goto recenter;
16668 }
16669
16670 /* Users who set scroll-conservatively to a large number want
16671 point just above/below the scroll margin. If we ended up
16672 with point's row partially visible, move the window start to
16673 make that row fully visible and out of the margin. */
16674 if (scroll_conservatively > SCROLL_LIMIT)
16675 {
16676 int window_total_lines
16677 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16678 int margin =
16679 scroll_margin > 0
16680 ? min (scroll_margin, window_total_lines / 4)
16681 : 0;
16682 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16683
16684 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16685 clear_glyph_matrix (w->desired_matrix);
16686 if (1 == try_window (window, it.current.pos,
16687 TRY_WINDOW_CHECK_MARGINS))
16688 goto done;
16689 }
16690
16691 /* If centering point failed to make the whole line visible,
16692 put point at the top instead. That has to make the whole line
16693 visible, if it can be done. */
16694 if (centering_position == 0)
16695 goto done;
16696
16697 clear_glyph_matrix (w->desired_matrix);
16698 centering_position = 0;
16699 goto recenter;
16700 }
16701
16702 done:
16703
16704 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16705 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16706 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16707
16708 /* Display the mode line, if we must. */
16709 if ((update_mode_line
16710 /* If window not full width, must redo its mode line
16711 if (a) the window to its side is being redone and
16712 (b) we do a frame-based redisplay. This is a consequence
16713 of how inverted lines are drawn in frame-based redisplay. */
16714 || (!just_this_one_p
16715 && !FRAME_WINDOW_P (f)
16716 && !WINDOW_FULL_WIDTH_P (w))
16717 /* Line number to display. */
16718 || w->base_line_pos > 0
16719 /* Column number is displayed and different from the one displayed. */
16720 || (w->column_number_displayed != -1
16721 && (w->column_number_displayed != current_column ())))
16722 /* This means that the window has a mode line. */
16723 && (WINDOW_WANTS_MODELINE_P (w)
16724 || WINDOW_WANTS_HEADER_LINE_P (w)))
16725 {
16726
16727 display_mode_lines (w);
16728
16729 /* If mode line height has changed, arrange for a thorough
16730 immediate redisplay using the correct mode line height. */
16731 if (WINDOW_WANTS_MODELINE_P (w)
16732 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16733 {
16734 f->fonts_changed = true;
16735 w->mode_line_height = -1;
16736 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16737 = DESIRED_MODE_LINE_HEIGHT (w);
16738 }
16739
16740 /* If header line height has changed, arrange for a thorough
16741 immediate redisplay using the correct header line height. */
16742 if (WINDOW_WANTS_HEADER_LINE_P (w)
16743 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16744 {
16745 f->fonts_changed = true;
16746 w->header_line_height = -1;
16747 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16748 = DESIRED_HEADER_LINE_HEIGHT (w);
16749 }
16750
16751 if (f->fonts_changed)
16752 goto need_larger_matrices;
16753 }
16754
16755 if (!line_number_displayed && w->base_line_pos != -1)
16756 {
16757 w->base_line_pos = 0;
16758 w->base_line_number = 0;
16759 }
16760
16761 finish_menu_bars:
16762
16763 /* When we reach a frame's selected window, redo the frame's menu bar. */
16764 if (update_mode_line
16765 && EQ (FRAME_SELECTED_WINDOW (f), window))
16766 {
16767 bool redisplay_menu_p;
16768
16769 if (FRAME_WINDOW_P (f))
16770 {
16771 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16772 || defined (HAVE_NS) || defined (USE_GTK)
16773 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16774 #else
16775 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16776 #endif
16777 }
16778 else
16779 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16780
16781 if (redisplay_menu_p)
16782 display_menu_bar (w);
16783
16784 #ifdef HAVE_WINDOW_SYSTEM
16785 if (FRAME_WINDOW_P (f))
16786 {
16787 #if defined (USE_GTK) || defined (HAVE_NS)
16788 if (FRAME_EXTERNAL_TOOL_BAR (f))
16789 redisplay_tool_bar (f);
16790 #else
16791 if (WINDOWP (f->tool_bar_window)
16792 && (FRAME_TOOL_BAR_LINES (f) > 0
16793 || !NILP (Vauto_resize_tool_bars))
16794 && redisplay_tool_bar (f))
16795 ignore_mouse_drag_p = true;
16796 #endif
16797 }
16798 #endif
16799 }
16800
16801 #ifdef HAVE_WINDOW_SYSTEM
16802 if (FRAME_WINDOW_P (f)
16803 && update_window_fringes (w, (just_this_one_p
16804 || (!used_current_matrix_p && !overlay_arrow_seen)
16805 || w->pseudo_window_p)))
16806 {
16807 update_begin (f);
16808 block_input ();
16809 if (draw_window_fringes (w, true))
16810 {
16811 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16812 x_draw_right_divider (w);
16813 else
16814 x_draw_vertical_border (w);
16815 }
16816 unblock_input ();
16817 update_end (f);
16818 }
16819
16820 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16821 x_draw_bottom_divider (w);
16822 #endif /* HAVE_WINDOW_SYSTEM */
16823
16824 /* We go to this label, with fonts_changed set, if it is
16825 necessary to try again using larger glyph matrices.
16826 We have to redeem the scroll bar even in this case,
16827 because the loop in redisplay_internal expects that. */
16828 need_larger_matrices:
16829 ;
16830 finish_scroll_bars:
16831
16832 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16833 {
16834 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16835 /* Set the thumb's position and size. */
16836 set_vertical_scroll_bar (w);
16837
16838 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16839 /* Set the thumb's position and size. */
16840 set_horizontal_scroll_bar (w);
16841
16842 /* Note that we actually used the scroll bar attached to this
16843 window, so it shouldn't be deleted at the end of redisplay. */
16844 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16845 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16846 }
16847
16848 /* Restore current_buffer and value of point in it. The window
16849 update may have changed the buffer, so first make sure `opoint'
16850 is still valid (Bug#6177). */
16851 if (CHARPOS (opoint) < BEGV)
16852 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16853 else if (CHARPOS (opoint) > ZV)
16854 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16855 else
16856 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16857
16858 set_buffer_internal_1 (old);
16859 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16860 shorter. This can be caused by log truncation in *Messages*. */
16861 if (CHARPOS (lpoint) <= ZV)
16862 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16863
16864 unbind_to (count, Qnil);
16865 }
16866
16867
16868 /* Build the complete desired matrix of WINDOW with a window start
16869 buffer position POS.
16870
16871 Value is 1 if successful. It is zero if fonts were loaded during
16872 redisplay which makes re-adjusting glyph matrices necessary, and -1
16873 if point would appear in the scroll margins.
16874 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16875 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16876 set in FLAGS.) */
16877
16878 int
16879 try_window (Lisp_Object window, struct text_pos pos, int flags)
16880 {
16881 struct window *w = XWINDOW (window);
16882 struct it it;
16883 struct glyph_row *last_text_row = NULL;
16884 struct frame *f = XFRAME (w->frame);
16885 int frame_line_height = default_line_pixel_height (w);
16886
16887 /* Make POS the new window start. */
16888 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16889
16890 /* Mark cursor position as unknown. No overlay arrow seen. */
16891 w->cursor.vpos = -1;
16892 overlay_arrow_seen = false;
16893
16894 /* Initialize iterator and info to start at POS. */
16895 start_display (&it, w, pos);
16896 it.glyph_row->reversed_p = false;
16897
16898 /* Display all lines of W. */
16899 while (it.current_y < it.last_visible_y)
16900 {
16901 if (display_line (&it))
16902 last_text_row = it.glyph_row - 1;
16903 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16904 return 0;
16905 }
16906
16907 /* Don't let the cursor end in the scroll margins. */
16908 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16909 && !MINI_WINDOW_P (w))
16910 {
16911 int this_scroll_margin;
16912 int window_total_lines
16913 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16914
16915 if (scroll_margin > 0)
16916 {
16917 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16918 this_scroll_margin *= frame_line_height;
16919 }
16920 else
16921 this_scroll_margin = 0;
16922
16923 if ((w->cursor.y >= 0 /* not vscrolled */
16924 && w->cursor.y < this_scroll_margin
16925 && CHARPOS (pos) > BEGV
16926 && IT_CHARPOS (it) < ZV)
16927 /* rms: considering make_cursor_line_fully_visible_p here
16928 seems to give wrong results. We don't want to recenter
16929 when the last line is partly visible, we want to allow
16930 that case to be handled in the usual way. */
16931 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16932 {
16933 w->cursor.vpos = -1;
16934 clear_glyph_matrix (w->desired_matrix);
16935 return -1;
16936 }
16937 }
16938
16939 /* If bottom moved off end of frame, change mode line percentage. */
16940 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16941 w->update_mode_line = true;
16942
16943 /* Set window_end_pos to the offset of the last character displayed
16944 on the window from the end of current_buffer. Set
16945 window_end_vpos to its row number. */
16946 if (last_text_row)
16947 {
16948 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16949 adjust_window_ends (w, last_text_row, false);
16950 eassert
16951 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16952 w->window_end_vpos)));
16953 }
16954 else
16955 {
16956 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16957 w->window_end_pos = Z - ZV;
16958 w->window_end_vpos = 0;
16959 }
16960
16961 /* But that is not valid info until redisplay finishes. */
16962 w->window_end_valid = false;
16963 return 1;
16964 }
16965
16966
16967 \f
16968 /************************************************************************
16969 Window redisplay reusing current matrix when buffer has not changed
16970 ************************************************************************/
16971
16972 /* Try redisplay of window W showing an unchanged buffer with a
16973 different window start than the last time it was displayed by
16974 reusing its current matrix. Value is true if successful.
16975 W->start is the new window start. */
16976
16977 static bool
16978 try_window_reusing_current_matrix (struct window *w)
16979 {
16980 struct frame *f = XFRAME (w->frame);
16981 struct glyph_row *bottom_row;
16982 struct it it;
16983 struct run run;
16984 struct text_pos start, new_start;
16985 int nrows_scrolled, i;
16986 struct glyph_row *last_text_row;
16987 struct glyph_row *last_reused_text_row;
16988 struct glyph_row *start_row;
16989 int start_vpos, min_y, max_y;
16990
16991 #ifdef GLYPH_DEBUG
16992 if (inhibit_try_window_reusing)
16993 return false;
16994 #endif
16995
16996 if (/* This function doesn't handle terminal frames. */
16997 !FRAME_WINDOW_P (f)
16998 /* Don't try to reuse the display if windows have been split
16999 or such. */
17000 || windows_or_buffers_changed
17001 || f->cursor_type_changed)
17002 return false;
17003
17004 /* Can't do this if showing trailing whitespace. */
17005 if (!NILP (Vshow_trailing_whitespace))
17006 return false;
17007
17008 /* If top-line visibility has changed, give up. */
17009 if (WINDOW_WANTS_HEADER_LINE_P (w)
17010 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17011 return false;
17012
17013 /* Give up if old or new display is scrolled vertically. We could
17014 make this function handle this, but right now it doesn't. */
17015 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17016 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17017 return false;
17018
17019 /* The variable new_start now holds the new window start. The old
17020 start `start' can be determined from the current matrix. */
17021 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17022 start = start_row->minpos;
17023 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17024
17025 /* Clear the desired matrix for the display below. */
17026 clear_glyph_matrix (w->desired_matrix);
17027
17028 if (CHARPOS (new_start) <= CHARPOS (start))
17029 {
17030 /* Don't use this method if the display starts with an ellipsis
17031 displayed for invisible text. It's not easy to handle that case
17032 below, and it's certainly not worth the effort since this is
17033 not a frequent case. */
17034 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17035 return false;
17036
17037 IF_DEBUG (debug_method_add (w, "twu1"));
17038
17039 /* Display up to a row that can be reused. The variable
17040 last_text_row is set to the last row displayed that displays
17041 text. Note that it.vpos == 0 if or if not there is a
17042 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17043 start_display (&it, w, new_start);
17044 w->cursor.vpos = -1;
17045 last_text_row = last_reused_text_row = NULL;
17046
17047 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17048 {
17049 /* If we have reached into the characters in the START row,
17050 that means the line boundaries have changed. So we
17051 can't start copying with the row START. Maybe it will
17052 work to start copying with the following row. */
17053 while (IT_CHARPOS (it) > CHARPOS (start))
17054 {
17055 /* Advance to the next row as the "start". */
17056 start_row++;
17057 start = start_row->minpos;
17058 /* If there are no more rows to try, or just one, give up. */
17059 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17060 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17061 || CHARPOS (start) == ZV)
17062 {
17063 clear_glyph_matrix (w->desired_matrix);
17064 return false;
17065 }
17066
17067 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17068 }
17069 /* If we have reached alignment, we can copy the rest of the
17070 rows. */
17071 if (IT_CHARPOS (it) == CHARPOS (start)
17072 /* Don't accept "alignment" inside a display vector,
17073 since start_row could have started in the middle of
17074 that same display vector (thus their character
17075 positions match), and we have no way of telling if
17076 that is the case. */
17077 && it.current.dpvec_index < 0)
17078 break;
17079
17080 it.glyph_row->reversed_p = false;
17081 if (display_line (&it))
17082 last_text_row = it.glyph_row - 1;
17083
17084 }
17085
17086 /* A value of current_y < last_visible_y means that we stopped
17087 at the previous window start, which in turn means that we
17088 have at least one reusable row. */
17089 if (it.current_y < it.last_visible_y)
17090 {
17091 struct glyph_row *row;
17092
17093 /* IT.vpos always starts from 0; it counts text lines. */
17094 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17095
17096 /* Find PT if not already found in the lines displayed. */
17097 if (w->cursor.vpos < 0)
17098 {
17099 int dy = it.current_y - start_row->y;
17100
17101 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17102 row = row_containing_pos (w, PT, row, NULL, dy);
17103 if (row)
17104 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17105 dy, nrows_scrolled);
17106 else
17107 {
17108 clear_glyph_matrix (w->desired_matrix);
17109 return false;
17110 }
17111 }
17112
17113 /* Scroll the display. Do it before the current matrix is
17114 changed. The problem here is that update has not yet
17115 run, i.e. part of the current matrix is not up to date.
17116 scroll_run_hook will clear the cursor, and use the
17117 current matrix to get the height of the row the cursor is
17118 in. */
17119 run.current_y = start_row->y;
17120 run.desired_y = it.current_y;
17121 run.height = it.last_visible_y - it.current_y;
17122
17123 if (run.height > 0 && run.current_y != run.desired_y)
17124 {
17125 update_begin (f);
17126 FRAME_RIF (f)->update_window_begin_hook (w);
17127 FRAME_RIF (f)->clear_window_mouse_face (w);
17128 FRAME_RIF (f)->scroll_run_hook (w, &run);
17129 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17130 update_end (f);
17131 }
17132
17133 /* Shift current matrix down by nrows_scrolled lines. */
17134 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17135 rotate_matrix (w->current_matrix,
17136 start_vpos,
17137 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17138 nrows_scrolled);
17139
17140 /* Disable lines that must be updated. */
17141 for (i = 0; i < nrows_scrolled; ++i)
17142 (start_row + i)->enabled_p = false;
17143
17144 /* Re-compute Y positions. */
17145 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17146 max_y = it.last_visible_y;
17147 for (row = start_row + nrows_scrolled;
17148 row < bottom_row;
17149 ++row)
17150 {
17151 row->y = it.current_y;
17152 row->visible_height = row->height;
17153
17154 if (row->y < min_y)
17155 row->visible_height -= min_y - row->y;
17156 if (row->y + row->height > max_y)
17157 row->visible_height -= row->y + row->height - max_y;
17158 if (row->fringe_bitmap_periodic_p)
17159 row->redraw_fringe_bitmaps_p = true;
17160
17161 it.current_y += row->height;
17162
17163 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17164 last_reused_text_row = row;
17165 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17166 break;
17167 }
17168
17169 /* Disable lines in the current matrix which are now
17170 below the window. */
17171 for (++row; row < bottom_row; ++row)
17172 row->enabled_p = row->mode_line_p = false;
17173 }
17174
17175 /* Update window_end_pos etc.; last_reused_text_row is the last
17176 reused row from the current matrix containing text, if any.
17177 The value of last_text_row is the last displayed line
17178 containing text. */
17179 if (last_reused_text_row)
17180 adjust_window_ends (w, last_reused_text_row, true);
17181 else if (last_text_row)
17182 adjust_window_ends (w, last_text_row, false);
17183 else
17184 {
17185 /* This window must be completely empty. */
17186 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17187 w->window_end_pos = Z - ZV;
17188 w->window_end_vpos = 0;
17189 }
17190 w->window_end_valid = false;
17191
17192 /* Update hint: don't try scrolling again in update_window. */
17193 w->desired_matrix->no_scrolling_p = true;
17194
17195 #ifdef GLYPH_DEBUG
17196 debug_method_add (w, "try_window_reusing_current_matrix 1");
17197 #endif
17198 return true;
17199 }
17200 else if (CHARPOS (new_start) > CHARPOS (start))
17201 {
17202 struct glyph_row *pt_row, *row;
17203 struct glyph_row *first_reusable_row;
17204 struct glyph_row *first_row_to_display;
17205 int dy;
17206 int yb = window_text_bottom_y (w);
17207
17208 /* Find the row starting at new_start, if there is one. Don't
17209 reuse a partially visible line at the end. */
17210 first_reusable_row = start_row;
17211 while (first_reusable_row->enabled_p
17212 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17213 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17214 < CHARPOS (new_start)))
17215 ++first_reusable_row;
17216
17217 /* Give up if there is no row to reuse. */
17218 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17219 || !first_reusable_row->enabled_p
17220 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17221 != CHARPOS (new_start)))
17222 return false;
17223
17224 /* We can reuse fully visible rows beginning with
17225 first_reusable_row to the end of the window. Set
17226 first_row_to_display to the first row that cannot be reused.
17227 Set pt_row to the row containing point, if there is any. */
17228 pt_row = NULL;
17229 for (first_row_to_display = first_reusable_row;
17230 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17231 ++first_row_to_display)
17232 {
17233 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17234 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17235 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17236 && first_row_to_display->ends_at_zv_p
17237 && pt_row == NULL)))
17238 pt_row = first_row_to_display;
17239 }
17240
17241 /* Start displaying at the start of first_row_to_display. */
17242 eassert (first_row_to_display->y < yb);
17243 init_to_row_start (&it, w, first_row_to_display);
17244
17245 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17246 - start_vpos);
17247 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17248 - nrows_scrolled);
17249 it.current_y = (first_row_to_display->y - first_reusable_row->y
17250 + WINDOW_HEADER_LINE_HEIGHT (w));
17251
17252 /* Display lines beginning with first_row_to_display in the
17253 desired matrix. Set last_text_row to the last row displayed
17254 that displays text. */
17255 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17256 if (pt_row == NULL)
17257 w->cursor.vpos = -1;
17258 last_text_row = NULL;
17259 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17260 if (display_line (&it))
17261 last_text_row = it.glyph_row - 1;
17262
17263 /* If point is in a reused row, adjust y and vpos of the cursor
17264 position. */
17265 if (pt_row)
17266 {
17267 w->cursor.vpos -= nrows_scrolled;
17268 w->cursor.y -= first_reusable_row->y - start_row->y;
17269 }
17270
17271 /* Give up if point isn't in a row displayed or reused. (This
17272 also handles the case where w->cursor.vpos < nrows_scrolled
17273 after the calls to display_line, which can happen with scroll
17274 margins. See bug#1295.) */
17275 if (w->cursor.vpos < 0)
17276 {
17277 clear_glyph_matrix (w->desired_matrix);
17278 return false;
17279 }
17280
17281 /* Scroll the display. */
17282 run.current_y = first_reusable_row->y;
17283 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17284 run.height = it.last_visible_y - run.current_y;
17285 dy = run.current_y - run.desired_y;
17286
17287 if (run.height)
17288 {
17289 update_begin (f);
17290 FRAME_RIF (f)->update_window_begin_hook (w);
17291 FRAME_RIF (f)->clear_window_mouse_face (w);
17292 FRAME_RIF (f)->scroll_run_hook (w, &run);
17293 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17294 update_end (f);
17295 }
17296
17297 /* Adjust Y positions of reused rows. */
17298 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17299 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17300 max_y = it.last_visible_y;
17301 for (row = first_reusable_row; row < first_row_to_display; ++row)
17302 {
17303 row->y -= dy;
17304 row->visible_height = row->height;
17305 if (row->y < min_y)
17306 row->visible_height -= min_y - row->y;
17307 if (row->y + row->height > max_y)
17308 row->visible_height -= row->y + row->height - max_y;
17309 if (row->fringe_bitmap_periodic_p)
17310 row->redraw_fringe_bitmaps_p = true;
17311 }
17312
17313 /* Scroll the current matrix. */
17314 eassert (nrows_scrolled > 0);
17315 rotate_matrix (w->current_matrix,
17316 start_vpos,
17317 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17318 -nrows_scrolled);
17319
17320 /* Disable rows not reused. */
17321 for (row -= nrows_scrolled; row < bottom_row; ++row)
17322 row->enabled_p = false;
17323
17324 /* Point may have moved to a different line, so we cannot assume that
17325 the previous cursor position is valid; locate the correct row. */
17326 if (pt_row)
17327 {
17328 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17329 row < bottom_row
17330 && PT >= MATRIX_ROW_END_CHARPOS (row)
17331 && !row->ends_at_zv_p;
17332 row++)
17333 {
17334 w->cursor.vpos++;
17335 w->cursor.y = row->y;
17336 }
17337 if (row < bottom_row)
17338 {
17339 /* Can't simply scan the row for point with
17340 bidi-reordered glyph rows. Let set_cursor_from_row
17341 figure out where to put the cursor, and if it fails,
17342 give up. */
17343 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17344 {
17345 if (!set_cursor_from_row (w, row, w->current_matrix,
17346 0, 0, 0, 0))
17347 {
17348 clear_glyph_matrix (w->desired_matrix);
17349 return false;
17350 }
17351 }
17352 else
17353 {
17354 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17355 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17356
17357 for (; glyph < end
17358 && (!BUFFERP (glyph->object)
17359 || glyph->charpos < PT);
17360 glyph++)
17361 {
17362 w->cursor.hpos++;
17363 w->cursor.x += glyph->pixel_width;
17364 }
17365 }
17366 }
17367 }
17368
17369 /* Adjust window end. A null value of last_text_row means that
17370 the window end is in reused rows which in turn means that
17371 only its vpos can have changed. */
17372 if (last_text_row)
17373 adjust_window_ends (w, last_text_row, false);
17374 else
17375 w->window_end_vpos -= nrows_scrolled;
17376
17377 w->window_end_valid = false;
17378 w->desired_matrix->no_scrolling_p = true;
17379
17380 #ifdef GLYPH_DEBUG
17381 debug_method_add (w, "try_window_reusing_current_matrix 2");
17382 #endif
17383 return true;
17384 }
17385
17386 return false;
17387 }
17388
17389
17390 \f
17391 /************************************************************************
17392 Window redisplay reusing current matrix when buffer has changed
17393 ************************************************************************/
17394
17395 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17396 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17397 ptrdiff_t *, ptrdiff_t *);
17398 static struct glyph_row *
17399 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17400 struct glyph_row *);
17401
17402
17403 /* Return the last row in MATRIX displaying text. If row START is
17404 non-null, start searching with that row. IT gives the dimensions
17405 of the display. Value is null if matrix is empty; otherwise it is
17406 a pointer to the row found. */
17407
17408 static struct glyph_row *
17409 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17410 struct glyph_row *start)
17411 {
17412 struct glyph_row *row, *row_found;
17413
17414 /* Set row_found to the last row in IT->w's current matrix
17415 displaying text. The loop looks funny but think of partially
17416 visible lines. */
17417 row_found = NULL;
17418 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17419 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17420 {
17421 eassert (row->enabled_p);
17422 row_found = row;
17423 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17424 break;
17425 ++row;
17426 }
17427
17428 return row_found;
17429 }
17430
17431
17432 /* Return the last row in the current matrix of W that is not affected
17433 by changes at the start of current_buffer that occurred since W's
17434 current matrix was built. Value is null if no such row exists.
17435
17436 BEG_UNCHANGED us the number of characters unchanged at the start of
17437 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17438 first changed character in current_buffer. Characters at positions <
17439 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17440 when the current matrix was built. */
17441
17442 static struct glyph_row *
17443 find_last_unchanged_at_beg_row (struct window *w)
17444 {
17445 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17446 struct glyph_row *row;
17447 struct glyph_row *row_found = NULL;
17448 int yb = window_text_bottom_y (w);
17449
17450 /* Find the last row displaying unchanged text. */
17451 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17452 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17453 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17454 ++row)
17455 {
17456 if (/* If row ends before first_changed_pos, it is unchanged,
17457 except in some case. */
17458 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17459 /* When row ends in ZV and we write at ZV it is not
17460 unchanged. */
17461 && !row->ends_at_zv_p
17462 /* When first_changed_pos is the end of a continued line,
17463 row is not unchanged because it may be no longer
17464 continued. */
17465 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17466 && (row->continued_p
17467 || row->exact_window_width_line_p))
17468 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17469 needs to be recomputed, so don't consider this row as
17470 unchanged. This happens when the last line was
17471 bidi-reordered and was killed immediately before this
17472 redisplay cycle. In that case, ROW->end stores the
17473 buffer position of the first visual-order character of
17474 the killed text, which is now beyond ZV. */
17475 && CHARPOS (row->end.pos) <= ZV)
17476 row_found = row;
17477
17478 /* Stop if last visible row. */
17479 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17480 break;
17481 }
17482
17483 return row_found;
17484 }
17485
17486
17487 /* Find the first glyph row in the current matrix of W that is not
17488 affected by changes at the end of current_buffer since the
17489 time W's current matrix was built.
17490
17491 Return in *DELTA the number of chars by which buffer positions in
17492 unchanged text at the end of current_buffer must be adjusted.
17493
17494 Return in *DELTA_BYTES the corresponding number of bytes.
17495
17496 Value is null if no such row exists, i.e. all rows are affected by
17497 changes. */
17498
17499 static struct glyph_row *
17500 find_first_unchanged_at_end_row (struct window *w,
17501 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17502 {
17503 struct glyph_row *row;
17504 struct glyph_row *row_found = NULL;
17505
17506 *delta = *delta_bytes = 0;
17507
17508 /* Display must not have been paused, otherwise the current matrix
17509 is not up to date. */
17510 eassert (w->window_end_valid);
17511
17512 /* A value of window_end_pos >= END_UNCHANGED means that the window
17513 end is in the range of changed text. If so, there is no
17514 unchanged row at the end of W's current matrix. */
17515 if (w->window_end_pos >= END_UNCHANGED)
17516 return NULL;
17517
17518 /* Set row to the last row in W's current matrix displaying text. */
17519 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17520
17521 /* If matrix is entirely empty, no unchanged row exists. */
17522 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17523 {
17524 /* The value of row is the last glyph row in the matrix having a
17525 meaningful buffer position in it. The end position of row
17526 corresponds to window_end_pos. This allows us to translate
17527 buffer positions in the current matrix to current buffer
17528 positions for characters not in changed text. */
17529 ptrdiff_t Z_old =
17530 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17531 ptrdiff_t Z_BYTE_old =
17532 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17533 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17534 struct glyph_row *first_text_row
17535 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17536
17537 *delta = Z - Z_old;
17538 *delta_bytes = Z_BYTE - Z_BYTE_old;
17539
17540 /* Set last_unchanged_pos to the buffer position of the last
17541 character in the buffer that has not been changed. Z is the
17542 index + 1 of the last character in current_buffer, i.e. by
17543 subtracting END_UNCHANGED we get the index of the last
17544 unchanged character, and we have to add BEG to get its buffer
17545 position. */
17546 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17547 last_unchanged_pos_old = last_unchanged_pos - *delta;
17548
17549 /* Search backward from ROW for a row displaying a line that
17550 starts at a minimum position >= last_unchanged_pos_old. */
17551 for (; row > first_text_row; --row)
17552 {
17553 /* This used to abort, but it can happen.
17554 It is ok to just stop the search instead here. KFS. */
17555 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17556 break;
17557
17558 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17559 row_found = row;
17560 }
17561 }
17562
17563 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17564
17565 return row_found;
17566 }
17567
17568
17569 /* Make sure that glyph rows in the current matrix of window W
17570 reference the same glyph memory as corresponding rows in the
17571 frame's frame matrix. This function is called after scrolling W's
17572 current matrix on a terminal frame in try_window_id and
17573 try_window_reusing_current_matrix. */
17574
17575 static void
17576 sync_frame_with_window_matrix_rows (struct window *w)
17577 {
17578 struct frame *f = XFRAME (w->frame);
17579 struct glyph_row *window_row, *window_row_end, *frame_row;
17580
17581 /* Preconditions: W must be a leaf window and full-width. Its frame
17582 must have a frame matrix. */
17583 eassert (BUFFERP (w->contents));
17584 eassert (WINDOW_FULL_WIDTH_P (w));
17585 eassert (!FRAME_WINDOW_P (f));
17586
17587 /* If W is a full-width window, glyph pointers in W's current matrix
17588 have, by definition, to be the same as glyph pointers in the
17589 corresponding frame matrix. Note that frame matrices have no
17590 marginal areas (see build_frame_matrix). */
17591 window_row = w->current_matrix->rows;
17592 window_row_end = window_row + w->current_matrix->nrows;
17593 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17594 while (window_row < window_row_end)
17595 {
17596 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17597 struct glyph *end = window_row->glyphs[LAST_AREA];
17598
17599 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17600 frame_row->glyphs[TEXT_AREA] = start;
17601 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17602 frame_row->glyphs[LAST_AREA] = end;
17603
17604 /* Disable frame rows whose corresponding window rows have
17605 been disabled in try_window_id. */
17606 if (!window_row->enabled_p)
17607 frame_row->enabled_p = false;
17608
17609 ++window_row, ++frame_row;
17610 }
17611 }
17612
17613
17614 /* Find the glyph row in window W containing CHARPOS. Consider all
17615 rows between START and END (not inclusive). END null means search
17616 all rows to the end of the display area of W. Value is the row
17617 containing CHARPOS or null. */
17618
17619 struct glyph_row *
17620 row_containing_pos (struct window *w, ptrdiff_t charpos,
17621 struct glyph_row *start, struct glyph_row *end, int dy)
17622 {
17623 struct glyph_row *row = start;
17624 struct glyph_row *best_row = NULL;
17625 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17626 int last_y;
17627
17628 /* If we happen to start on a header-line, skip that. */
17629 if (row->mode_line_p)
17630 ++row;
17631
17632 if ((end && row >= end) || !row->enabled_p)
17633 return NULL;
17634
17635 last_y = window_text_bottom_y (w) - dy;
17636
17637 while (true)
17638 {
17639 /* Give up if we have gone too far. */
17640 if (end && row >= end)
17641 return NULL;
17642 /* This formerly returned if they were equal.
17643 I think that both quantities are of a "last plus one" type;
17644 if so, when they are equal, the row is within the screen. -- rms. */
17645 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17646 return NULL;
17647
17648 /* If it is in this row, return this row. */
17649 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17650 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17651 /* The end position of a row equals the start
17652 position of the next row. If CHARPOS is there, we
17653 would rather consider it displayed in the next
17654 line, except when this line ends in ZV. */
17655 && !row_for_charpos_p (row, charpos)))
17656 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17657 {
17658 struct glyph *g;
17659
17660 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17661 || (!best_row && !row->continued_p))
17662 return row;
17663 /* In bidi-reordered rows, there could be several rows whose
17664 edges surround CHARPOS, all of these rows belonging to
17665 the same continued line. We need to find the row which
17666 fits CHARPOS the best. */
17667 for (g = row->glyphs[TEXT_AREA];
17668 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17669 g++)
17670 {
17671 if (!STRINGP (g->object))
17672 {
17673 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17674 {
17675 mindif = eabs (g->charpos - charpos);
17676 best_row = row;
17677 /* Exact match always wins. */
17678 if (mindif == 0)
17679 return best_row;
17680 }
17681 }
17682 }
17683 }
17684 else if (best_row && !row->continued_p)
17685 return best_row;
17686 ++row;
17687 }
17688 }
17689
17690
17691 /* Try to redisplay window W by reusing its existing display. W's
17692 current matrix must be up to date when this function is called,
17693 i.e., window_end_valid must be true.
17694
17695 Value is
17696
17697 >= 1 if successful, i.e. display has been updated
17698 specifically:
17699 1 means the changes were in front of a newline that precedes
17700 the window start, and the whole current matrix was reused
17701 2 means the changes were after the last position displayed
17702 in the window, and the whole current matrix was reused
17703 3 means portions of the current matrix were reused, while
17704 some of the screen lines were redrawn
17705 -1 if redisplay with same window start is known not to succeed
17706 0 if otherwise unsuccessful
17707
17708 The following steps are performed:
17709
17710 1. Find the last row in the current matrix of W that is not
17711 affected by changes at the start of current_buffer. If no such row
17712 is found, give up.
17713
17714 2. Find the first row in W's current matrix that is not affected by
17715 changes at the end of current_buffer. Maybe there is no such row.
17716
17717 3. Display lines beginning with the row + 1 found in step 1 to the
17718 row found in step 2 or, if step 2 didn't find a row, to the end of
17719 the window.
17720
17721 4. If cursor is not known to appear on the window, give up.
17722
17723 5. If display stopped at the row found in step 2, scroll the
17724 display and current matrix as needed.
17725
17726 6. Maybe display some lines at the end of W, if we must. This can
17727 happen under various circumstances, like a partially visible line
17728 becoming fully visible, or because newly displayed lines are displayed
17729 in smaller font sizes.
17730
17731 7. Update W's window end information. */
17732
17733 static int
17734 try_window_id (struct window *w)
17735 {
17736 struct frame *f = XFRAME (w->frame);
17737 struct glyph_matrix *current_matrix = w->current_matrix;
17738 struct glyph_matrix *desired_matrix = w->desired_matrix;
17739 struct glyph_row *last_unchanged_at_beg_row;
17740 struct glyph_row *first_unchanged_at_end_row;
17741 struct glyph_row *row;
17742 struct glyph_row *bottom_row;
17743 int bottom_vpos;
17744 struct it it;
17745 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17746 int dvpos, dy;
17747 struct text_pos start_pos;
17748 struct run run;
17749 int first_unchanged_at_end_vpos = 0;
17750 struct glyph_row *last_text_row, *last_text_row_at_end;
17751 struct text_pos start;
17752 ptrdiff_t first_changed_charpos, last_changed_charpos;
17753
17754 #ifdef GLYPH_DEBUG
17755 if (inhibit_try_window_id)
17756 return 0;
17757 #endif
17758
17759 /* This is handy for debugging. */
17760 #if false
17761 #define GIVE_UP(X) \
17762 do { \
17763 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17764 return 0; \
17765 } while (false)
17766 #else
17767 #define GIVE_UP(X) return 0
17768 #endif
17769
17770 SET_TEXT_POS_FROM_MARKER (start, w->start);
17771
17772 /* Don't use this for mini-windows because these can show
17773 messages and mini-buffers, and we don't handle that here. */
17774 if (MINI_WINDOW_P (w))
17775 GIVE_UP (1);
17776
17777 /* This flag is used to prevent redisplay optimizations. */
17778 if (windows_or_buffers_changed || f->cursor_type_changed)
17779 GIVE_UP (2);
17780
17781 /* This function's optimizations cannot be used if overlays have
17782 changed in the buffer displayed by the window, so give up if they
17783 have. */
17784 if (w->last_overlay_modified != OVERLAY_MODIFF)
17785 GIVE_UP (21);
17786
17787 /* Verify that narrowing has not changed.
17788 Also verify that we were not told to prevent redisplay optimizations.
17789 It would be nice to further
17790 reduce the number of cases where this prevents try_window_id. */
17791 if (current_buffer->clip_changed
17792 || current_buffer->prevent_redisplay_optimizations_p)
17793 GIVE_UP (3);
17794
17795 /* Window must either use window-based redisplay or be full width. */
17796 if (!FRAME_WINDOW_P (f)
17797 && (!FRAME_LINE_INS_DEL_OK (f)
17798 || !WINDOW_FULL_WIDTH_P (w)))
17799 GIVE_UP (4);
17800
17801 /* Give up if point is known NOT to appear in W. */
17802 if (PT < CHARPOS (start))
17803 GIVE_UP (5);
17804
17805 /* Another way to prevent redisplay optimizations. */
17806 if (w->last_modified == 0)
17807 GIVE_UP (6);
17808
17809 /* Verify that window is not hscrolled. */
17810 if (w->hscroll != 0)
17811 GIVE_UP (7);
17812
17813 /* Verify that display wasn't paused. */
17814 if (!w->window_end_valid)
17815 GIVE_UP (8);
17816
17817 /* Likewise if highlighting trailing whitespace. */
17818 if (!NILP (Vshow_trailing_whitespace))
17819 GIVE_UP (11);
17820
17821 /* Can't use this if overlay arrow position and/or string have
17822 changed. */
17823 if (overlay_arrows_changed_p ())
17824 GIVE_UP (12);
17825
17826 /* When word-wrap is on, adding a space to the first word of a
17827 wrapped line can change the wrap position, altering the line
17828 above it. It might be worthwhile to handle this more
17829 intelligently, but for now just redisplay from scratch. */
17830 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17831 GIVE_UP (21);
17832
17833 /* Under bidi reordering, adding or deleting a character in the
17834 beginning of a paragraph, before the first strong directional
17835 character, can change the base direction of the paragraph (unless
17836 the buffer specifies a fixed paragraph direction), which will
17837 require to redisplay the whole paragraph. It might be worthwhile
17838 to find the paragraph limits and widen the range of redisplayed
17839 lines to that, but for now just give up this optimization and
17840 redisplay from scratch. */
17841 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17842 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17843 GIVE_UP (22);
17844
17845 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17846 only if buffer has really changed. The reason is that the gap is
17847 initially at Z for freshly visited files. The code below would
17848 set end_unchanged to 0 in that case. */
17849 if (MODIFF > SAVE_MODIFF
17850 /* This seems to happen sometimes after saving a buffer. */
17851 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17852 {
17853 if (GPT - BEG < BEG_UNCHANGED)
17854 BEG_UNCHANGED = GPT - BEG;
17855 if (Z - GPT < END_UNCHANGED)
17856 END_UNCHANGED = Z - GPT;
17857 }
17858
17859 /* The position of the first and last character that has been changed. */
17860 first_changed_charpos = BEG + BEG_UNCHANGED;
17861 last_changed_charpos = Z - END_UNCHANGED;
17862
17863 /* If window starts after a line end, and the last change is in
17864 front of that newline, then changes don't affect the display.
17865 This case happens with stealth-fontification. Note that although
17866 the display is unchanged, glyph positions in the matrix have to
17867 be adjusted, of course. */
17868 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17869 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17870 && ((last_changed_charpos < CHARPOS (start)
17871 && CHARPOS (start) == BEGV)
17872 || (last_changed_charpos < CHARPOS (start) - 1
17873 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17874 {
17875 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17876 struct glyph_row *r0;
17877
17878 /* Compute how many chars/bytes have been added to or removed
17879 from the buffer. */
17880 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17881 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17882 Z_delta = Z - Z_old;
17883 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17884
17885 /* Give up if PT is not in the window. Note that it already has
17886 been checked at the start of try_window_id that PT is not in
17887 front of the window start. */
17888 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17889 GIVE_UP (13);
17890
17891 /* If window start is unchanged, we can reuse the whole matrix
17892 as is, after adjusting glyph positions. No need to compute
17893 the window end again, since its offset from Z hasn't changed. */
17894 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17895 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17896 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17897 /* PT must not be in a partially visible line. */
17898 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17899 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17900 {
17901 /* Adjust positions in the glyph matrix. */
17902 if (Z_delta || Z_delta_bytes)
17903 {
17904 struct glyph_row *r1
17905 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17906 increment_matrix_positions (w->current_matrix,
17907 MATRIX_ROW_VPOS (r0, current_matrix),
17908 MATRIX_ROW_VPOS (r1, current_matrix),
17909 Z_delta, Z_delta_bytes);
17910 }
17911
17912 /* Set the cursor. */
17913 row = row_containing_pos (w, PT, r0, NULL, 0);
17914 if (row)
17915 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17916 return 1;
17917 }
17918 }
17919
17920 /* Handle the case that changes are all below what is displayed in
17921 the window, and that PT is in the window. This shortcut cannot
17922 be taken if ZV is visible in the window, and text has been added
17923 there that is visible in the window. */
17924 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17925 /* ZV is not visible in the window, or there are no
17926 changes at ZV, actually. */
17927 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17928 || first_changed_charpos == last_changed_charpos))
17929 {
17930 struct glyph_row *r0;
17931
17932 /* Give up if PT is not in the window. Note that it already has
17933 been checked at the start of try_window_id that PT is not in
17934 front of the window start. */
17935 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17936 GIVE_UP (14);
17937
17938 /* If window start is unchanged, we can reuse the whole matrix
17939 as is, without changing glyph positions since no text has
17940 been added/removed in front of the window end. */
17941 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17942 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17943 /* PT must not be in a partially visible line. */
17944 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17945 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17946 {
17947 /* We have to compute the window end anew since text
17948 could have been added/removed after it. */
17949 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17950 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17951
17952 /* Set the cursor. */
17953 row = row_containing_pos (w, PT, r0, NULL, 0);
17954 if (row)
17955 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17956 return 2;
17957 }
17958 }
17959
17960 /* Give up if window start is in the changed area.
17961
17962 The condition used to read
17963
17964 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17965
17966 but why that was tested escapes me at the moment. */
17967 if (CHARPOS (start) >= first_changed_charpos
17968 && CHARPOS (start) <= last_changed_charpos)
17969 GIVE_UP (15);
17970
17971 /* Check that window start agrees with the start of the first glyph
17972 row in its current matrix. Check this after we know the window
17973 start is not in changed text, otherwise positions would not be
17974 comparable. */
17975 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17976 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17977 GIVE_UP (16);
17978
17979 /* Give up if the window ends in strings. Overlay strings
17980 at the end are difficult to handle, so don't try. */
17981 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17982 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17983 GIVE_UP (20);
17984
17985 /* Compute the position at which we have to start displaying new
17986 lines. Some of the lines at the top of the window might be
17987 reusable because they are not displaying changed text. Find the
17988 last row in W's current matrix not affected by changes at the
17989 start of current_buffer. Value is null if changes start in the
17990 first line of window. */
17991 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17992 if (last_unchanged_at_beg_row)
17993 {
17994 /* Avoid starting to display in the middle of a character, a TAB
17995 for instance. This is easier than to set up the iterator
17996 exactly, and it's not a frequent case, so the additional
17997 effort wouldn't really pay off. */
17998 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17999 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18000 && last_unchanged_at_beg_row > w->current_matrix->rows)
18001 --last_unchanged_at_beg_row;
18002
18003 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18004 GIVE_UP (17);
18005
18006 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18007 GIVE_UP (18);
18008 start_pos = it.current.pos;
18009
18010 /* Start displaying new lines in the desired matrix at the same
18011 vpos we would use in the current matrix, i.e. below
18012 last_unchanged_at_beg_row. */
18013 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18014 current_matrix);
18015 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18016 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18017
18018 eassert (it.hpos == 0 && it.current_x == 0);
18019 }
18020 else
18021 {
18022 /* There are no reusable lines at the start of the window.
18023 Start displaying in the first text line. */
18024 start_display (&it, w, start);
18025 it.vpos = it.first_vpos;
18026 start_pos = it.current.pos;
18027 }
18028
18029 /* Find the first row that is not affected by changes at the end of
18030 the buffer. Value will be null if there is no unchanged row, in
18031 which case we must redisplay to the end of the window. delta
18032 will be set to the value by which buffer positions beginning with
18033 first_unchanged_at_end_row have to be adjusted due to text
18034 changes. */
18035 first_unchanged_at_end_row
18036 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18037 IF_DEBUG (debug_delta = delta);
18038 IF_DEBUG (debug_delta_bytes = delta_bytes);
18039
18040 /* Set stop_pos to the buffer position up to which we will have to
18041 display new lines. If first_unchanged_at_end_row != NULL, this
18042 is the buffer position of the start of the line displayed in that
18043 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18044 that we don't stop at a buffer position. */
18045 stop_pos = 0;
18046 if (first_unchanged_at_end_row)
18047 {
18048 eassert (last_unchanged_at_beg_row == NULL
18049 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18050
18051 /* If this is a continuation line, move forward to the next one
18052 that isn't. Changes in lines above affect this line.
18053 Caution: this may move first_unchanged_at_end_row to a row
18054 not displaying text. */
18055 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18056 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18057 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18058 < it.last_visible_y))
18059 ++first_unchanged_at_end_row;
18060
18061 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18062 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18063 >= it.last_visible_y))
18064 first_unchanged_at_end_row = NULL;
18065 else
18066 {
18067 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18068 + delta);
18069 first_unchanged_at_end_vpos
18070 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18071 eassert (stop_pos >= Z - END_UNCHANGED);
18072 }
18073 }
18074 else if (last_unchanged_at_beg_row == NULL)
18075 GIVE_UP (19);
18076
18077
18078 #ifdef GLYPH_DEBUG
18079
18080 /* Either there is no unchanged row at the end, or the one we have
18081 now displays text. This is a necessary condition for the window
18082 end pos calculation at the end of this function. */
18083 eassert (first_unchanged_at_end_row == NULL
18084 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18085
18086 debug_last_unchanged_at_beg_vpos
18087 = (last_unchanged_at_beg_row
18088 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18089 : -1);
18090 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18091
18092 #endif /* GLYPH_DEBUG */
18093
18094
18095 /* Display new lines. Set last_text_row to the last new line
18096 displayed which has text on it, i.e. might end up as being the
18097 line where the window_end_vpos is. */
18098 w->cursor.vpos = -1;
18099 last_text_row = NULL;
18100 overlay_arrow_seen = false;
18101 if (it.current_y < it.last_visible_y
18102 && !f->fonts_changed
18103 && (first_unchanged_at_end_row == NULL
18104 || IT_CHARPOS (it) < stop_pos))
18105 it.glyph_row->reversed_p = false;
18106 while (it.current_y < it.last_visible_y
18107 && !f->fonts_changed
18108 && (first_unchanged_at_end_row == NULL
18109 || IT_CHARPOS (it) < stop_pos))
18110 {
18111 if (display_line (&it))
18112 last_text_row = it.glyph_row - 1;
18113 }
18114
18115 if (f->fonts_changed)
18116 return -1;
18117
18118 /* The redisplay iterations in display_line above could have
18119 triggered font-lock, which could have done something that
18120 invalidates IT->w window's end-point information, on which we
18121 rely below. E.g., one package, which will remain unnamed, used
18122 to install a font-lock-fontify-region-function that called
18123 bury-buffer, whose side effect is to switch the buffer displayed
18124 by IT->w, and that predictably resets IT->w's window_end_valid
18125 flag, which we already tested at the entry to this function.
18126 Amply punish such packages/modes by giving up on this
18127 optimization in those cases. */
18128 if (!w->window_end_valid)
18129 {
18130 clear_glyph_matrix (w->desired_matrix);
18131 return -1;
18132 }
18133
18134 /* Compute differences in buffer positions, y-positions etc. for
18135 lines reused at the bottom of the window. Compute what we can
18136 scroll. */
18137 if (first_unchanged_at_end_row
18138 /* No lines reused because we displayed everything up to the
18139 bottom of the window. */
18140 && it.current_y < it.last_visible_y)
18141 {
18142 dvpos = (it.vpos
18143 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18144 current_matrix));
18145 dy = it.current_y - first_unchanged_at_end_row->y;
18146 run.current_y = first_unchanged_at_end_row->y;
18147 run.desired_y = run.current_y + dy;
18148 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18149 }
18150 else
18151 {
18152 delta = delta_bytes = dvpos = dy
18153 = run.current_y = run.desired_y = run.height = 0;
18154 first_unchanged_at_end_row = NULL;
18155 }
18156 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18157
18158
18159 /* Find the cursor if not already found. We have to decide whether
18160 PT will appear on this window (it sometimes doesn't, but this is
18161 not a very frequent case.) This decision has to be made before
18162 the current matrix is altered. A value of cursor.vpos < 0 means
18163 that PT is either in one of the lines beginning at
18164 first_unchanged_at_end_row or below the window. Don't care for
18165 lines that might be displayed later at the window end; as
18166 mentioned, this is not a frequent case. */
18167 if (w->cursor.vpos < 0)
18168 {
18169 /* Cursor in unchanged rows at the top? */
18170 if (PT < CHARPOS (start_pos)
18171 && last_unchanged_at_beg_row)
18172 {
18173 row = row_containing_pos (w, PT,
18174 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18175 last_unchanged_at_beg_row + 1, 0);
18176 if (row)
18177 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18178 }
18179
18180 /* Start from first_unchanged_at_end_row looking for PT. */
18181 else if (first_unchanged_at_end_row)
18182 {
18183 row = row_containing_pos (w, PT - delta,
18184 first_unchanged_at_end_row, NULL, 0);
18185 if (row)
18186 set_cursor_from_row (w, row, w->current_matrix, delta,
18187 delta_bytes, dy, dvpos);
18188 }
18189
18190 /* Give up if cursor was not found. */
18191 if (w->cursor.vpos < 0)
18192 {
18193 clear_glyph_matrix (w->desired_matrix);
18194 return -1;
18195 }
18196 }
18197
18198 /* Don't let the cursor end in the scroll margins. */
18199 {
18200 int this_scroll_margin, cursor_height;
18201 int frame_line_height = default_line_pixel_height (w);
18202 int window_total_lines
18203 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18204
18205 this_scroll_margin =
18206 max (0, min (scroll_margin, window_total_lines / 4));
18207 this_scroll_margin *= frame_line_height;
18208 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18209
18210 if ((w->cursor.y < this_scroll_margin
18211 && CHARPOS (start) > BEGV)
18212 /* Old redisplay didn't take scroll margin into account at the bottom,
18213 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18214 || (w->cursor.y + (make_cursor_line_fully_visible_p
18215 ? cursor_height + this_scroll_margin
18216 : 1)) > it.last_visible_y)
18217 {
18218 w->cursor.vpos = -1;
18219 clear_glyph_matrix (w->desired_matrix);
18220 return -1;
18221 }
18222 }
18223
18224 /* Scroll the display. Do it before changing the current matrix so
18225 that xterm.c doesn't get confused about where the cursor glyph is
18226 found. */
18227 if (dy && run.height)
18228 {
18229 update_begin (f);
18230
18231 if (FRAME_WINDOW_P (f))
18232 {
18233 FRAME_RIF (f)->update_window_begin_hook (w);
18234 FRAME_RIF (f)->clear_window_mouse_face (w);
18235 FRAME_RIF (f)->scroll_run_hook (w, &run);
18236 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18237 }
18238 else
18239 {
18240 /* Terminal frame. In this case, dvpos gives the number of
18241 lines to scroll by; dvpos < 0 means scroll up. */
18242 int from_vpos
18243 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18244 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18245 int end = (WINDOW_TOP_EDGE_LINE (w)
18246 + WINDOW_WANTS_HEADER_LINE_P (w)
18247 + window_internal_height (w));
18248
18249 #if defined (HAVE_GPM) || defined (MSDOS)
18250 x_clear_window_mouse_face (w);
18251 #endif
18252 /* Perform the operation on the screen. */
18253 if (dvpos > 0)
18254 {
18255 /* Scroll last_unchanged_at_beg_row to the end of the
18256 window down dvpos lines. */
18257 set_terminal_window (f, end);
18258
18259 /* On dumb terminals delete dvpos lines at the end
18260 before inserting dvpos empty lines. */
18261 if (!FRAME_SCROLL_REGION_OK (f))
18262 ins_del_lines (f, end - dvpos, -dvpos);
18263
18264 /* Insert dvpos empty lines in front of
18265 last_unchanged_at_beg_row. */
18266 ins_del_lines (f, from, dvpos);
18267 }
18268 else if (dvpos < 0)
18269 {
18270 /* Scroll up last_unchanged_at_beg_vpos to the end of
18271 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18272 set_terminal_window (f, end);
18273
18274 /* Delete dvpos lines in front of
18275 last_unchanged_at_beg_vpos. ins_del_lines will set
18276 the cursor to the given vpos and emit |dvpos| delete
18277 line sequences. */
18278 ins_del_lines (f, from + dvpos, dvpos);
18279
18280 /* On a dumb terminal insert dvpos empty lines at the
18281 end. */
18282 if (!FRAME_SCROLL_REGION_OK (f))
18283 ins_del_lines (f, end + dvpos, -dvpos);
18284 }
18285
18286 set_terminal_window (f, 0);
18287 }
18288
18289 update_end (f);
18290 }
18291
18292 /* Shift reused rows of the current matrix to the right position.
18293 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18294 text. */
18295 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18296 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18297 if (dvpos < 0)
18298 {
18299 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18300 bottom_vpos, dvpos);
18301 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18302 bottom_vpos);
18303 }
18304 else if (dvpos > 0)
18305 {
18306 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18307 bottom_vpos, dvpos);
18308 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18309 first_unchanged_at_end_vpos + dvpos);
18310 }
18311
18312 /* For frame-based redisplay, make sure that current frame and window
18313 matrix are in sync with respect to glyph memory. */
18314 if (!FRAME_WINDOW_P (f))
18315 sync_frame_with_window_matrix_rows (w);
18316
18317 /* Adjust buffer positions in reused rows. */
18318 if (delta || delta_bytes)
18319 increment_matrix_positions (current_matrix,
18320 first_unchanged_at_end_vpos + dvpos,
18321 bottom_vpos, delta, delta_bytes);
18322
18323 /* Adjust Y positions. */
18324 if (dy)
18325 shift_glyph_matrix (w, current_matrix,
18326 first_unchanged_at_end_vpos + dvpos,
18327 bottom_vpos, dy);
18328
18329 if (first_unchanged_at_end_row)
18330 {
18331 first_unchanged_at_end_row += dvpos;
18332 if (first_unchanged_at_end_row->y >= it.last_visible_y
18333 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18334 first_unchanged_at_end_row = NULL;
18335 }
18336
18337 /* If scrolling up, there may be some lines to display at the end of
18338 the window. */
18339 last_text_row_at_end = NULL;
18340 if (dy < 0)
18341 {
18342 /* Scrolling up can leave for example a partially visible line
18343 at the end of the window to be redisplayed. */
18344 /* Set last_row to the glyph row in the current matrix where the
18345 window end line is found. It has been moved up or down in
18346 the matrix by dvpos. */
18347 int last_vpos = w->window_end_vpos + dvpos;
18348 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18349
18350 /* If last_row is the window end line, it should display text. */
18351 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18352
18353 /* If window end line was partially visible before, begin
18354 displaying at that line. Otherwise begin displaying with the
18355 line following it. */
18356 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18357 {
18358 init_to_row_start (&it, w, last_row);
18359 it.vpos = last_vpos;
18360 it.current_y = last_row->y;
18361 }
18362 else
18363 {
18364 init_to_row_end (&it, w, last_row);
18365 it.vpos = 1 + last_vpos;
18366 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18367 ++last_row;
18368 }
18369
18370 /* We may start in a continuation line. If so, we have to
18371 get the right continuation_lines_width and current_x. */
18372 it.continuation_lines_width = last_row->continuation_lines_width;
18373 it.hpos = it.current_x = 0;
18374
18375 /* Display the rest of the lines at the window end. */
18376 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18377 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18378 {
18379 /* Is it always sure that the display agrees with lines in
18380 the current matrix? I don't think so, so we mark rows
18381 displayed invalid in the current matrix by setting their
18382 enabled_p flag to false. */
18383 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18384 if (display_line (&it))
18385 last_text_row_at_end = it.glyph_row - 1;
18386 }
18387 }
18388
18389 /* Update window_end_pos and window_end_vpos. */
18390 if (first_unchanged_at_end_row && !last_text_row_at_end)
18391 {
18392 /* Window end line if one of the preserved rows from the current
18393 matrix. Set row to the last row displaying text in current
18394 matrix starting at first_unchanged_at_end_row, after
18395 scrolling. */
18396 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18397 row = find_last_row_displaying_text (w->current_matrix, &it,
18398 first_unchanged_at_end_row);
18399 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18400 adjust_window_ends (w, row, true);
18401 eassert (w->window_end_bytepos >= 0);
18402 IF_DEBUG (debug_method_add (w, "A"));
18403 }
18404 else if (last_text_row_at_end)
18405 {
18406 adjust_window_ends (w, last_text_row_at_end, false);
18407 eassert (w->window_end_bytepos >= 0);
18408 IF_DEBUG (debug_method_add (w, "B"));
18409 }
18410 else if (last_text_row)
18411 {
18412 /* We have displayed either to the end of the window or at the
18413 end of the window, i.e. the last row with text is to be found
18414 in the desired matrix. */
18415 adjust_window_ends (w, last_text_row, false);
18416 eassert (w->window_end_bytepos >= 0);
18417 }
18418 else if (first_unchanged_at_end_row == NULL
18419 && last_text_row == NULL
18420 && last_text_row_at_end == NULL)
18421 {
18422 /* Displayed to end of window, but no line containing text was
18423 displayed. Lines were deleted at the end of the window. */
18424 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18425 int vpos = w->window_end_vpos;
18426 struct glyph_row *current_row = current_matrix->rows + vpos;
18427 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18428
18429 for (row = NULL;
18430 row == NULL && vpos >= first_vpos;
18431 --vpos, --current_row, --desired_row)
18432 {
18433 if (desired_row->enabled_p)
18434 {
18435 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18436 row = desired_row;
18437 }
18438 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18439 row = current_row;
18440 }
18441
18442 eassert (row != NULL);
18443 w->window_end_vpos = vpos + 1;
18444 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18445 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18446 eassert (w->window_end_bytepos >= 0);
18447 IF_DEBUG (debug_method_add (w, "C"));
18448 }
18449 else
18450 emacs_abort ();
18451
18452 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18453 debug_end_vpos = w->window_end_vpos));
18454
18455 /* Record that display has not been completed. */
18456 w->window_end_valid = false;
18457 w->desired_matrix->no_scrolling_p = true;
18458 return 3;
18459
18460 #undef GIVE_UP
18461 }
18462
18463
18464 \f
18465 /***********************************************************************
18466 More debugging support
18467 ***********************************************************************/
18468
18469 #ifdef GLYPH_DEBUG
18470
18471 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18472 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18473 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18474
18475
18476 /* Dump the contents of glyph matrix MATRIX on stderr.
18477
18478 GLYPHS 0 means don't show glyph contents.
18479 GLYPHS 1 means show glyphs in short form
18480 GLYPHS > 1 means show glyphs in long form. */
18481
18482 void
18483 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18484 {
18485 int i;
18486 for (i = 0; i < matrix->nrows; ++i)
18487 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18488 }
18489
18490
18491 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18492 the glyph row and area where the glyph comes from. */
18493
18494 void
18495 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18496 {
18497 if (glyph->type == CHAR_GLYPH
18498 || glyph->type == GLYPHLESS_GLYPH)
18499 {
18500 fprintf (stderr,
18501 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18502 glyph - row->glyphs[TEXT_AREA],
18503 (glyph->type == CHAR_GLYPH
18504 ? 'C'
18505 : 'G'),
18506 glyph->charpos,
18507 (BUFFERP (glyph->object)
18508 ? 'B'
18509 : (STRINGP (glyph->object)
18510 ? 'S'
18511 : (NILP (glyph->object)
18512 ? '0'
18513 : '-'))),
18514 glyph->pixel_width,
18515 glyph->u.ch,
18516 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18517 ? glyph->u.ch
18518 : '.'),
18519 glyph->face_id,
18520 glyph->left_box_line_p,
18521 glyph->right_box_line_p);
18522 }
18523 else if (glyph->type == STRETCH_GLYPH)
18524 {
18525 fprintf (stderr,
18526 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18527 glyph - row->glyphs[TEXT_AREA],
18528 'S',
18529 glyph->charpos,
18530 (BUFFERP (glyph->object)
18531 ? 'B'
18532 : (STRINGP (glyph->object)
18533 ? 'S'
18534 : (NILP (glyph->object)
18535 ? '0'
18536 : '-'))),
18537 glyph->pixel_width,
18538 0,
18539 ' ',
18540 glyph->face_id,
18541 glyph->left_box_line_p,
18542 glyph->right_box_line_p);
18543 }
18544 else if (glyph->type == IMAGE_GLYPH)
18545 {
18546 fprintf (stderr,
18547 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18548 glyph - row->glyphs[TEXT_AREA],
18549 'I',
18550 glyph->charpos,
18551 (BUFFERP (glyph->object)
18552 ? 'B'
18553 : (STRINGP (glyph->object)
18554 ? 'S'
18555 : (NILP (glyph->object)
18556 ? '0'
18557 : '-'))),
18558 glyph->pixel_width,
18559 glyph->u.img_id,
18560 '.',
18561 glyph->face_id,
18562 glyph->left_box_line_p,
18563 glyph->right_box_line_p);
18564 }
18565 else if (glyph->type == COMPOSITE_GLYPH)
18566 {
18567 fprintf (stderr,
18568 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18569 glyph - row->glyphs[TEXT_AREA],
18570 '+',
18571 glyph->charpos,
18572 (BUFFERP (glyph->object)
18573 ? 'B'
18574 : (STRINGP (glyph->object)
18575 ? 'S'
18576 : (NILP (glyph->object)
18577 ? '0'
18578 : '-'))),
18579 glyph->pixel_width,
18580 glyph->u.cmp.id);
18581 if (glyph->u.cmp.automatic)
18582 fprintf (stderr,
18583 "[%d-%d]",
18584 glyph->slice.cmp.from, glyph->slice.cmp.to);
18585 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18586 glyph->face_id,
18587 glyph->left_box_line_p,
18588 glyph->right_box_line_p);
18589 }
18590 }
18591
18592
18593 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18594 GLYPHS 0 means don't show glyph contents.
18595 GLYPHS 1 means show glyphs in short form
18596 GLYPHS > 1 means show glyphs in long form. */
18597
18598 void
18599 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18600 {
18601 if (glyphs != 1)
18602 {
18603 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18604 fprintf (stderr, "==============================================================================\n");
18605
18606 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18607 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18608 vpos,
18609 MATRIX_ROW_START_CHARPOS (row),
18610 MATRIX_ROW_END_CHARPOS (row),
18611 row->used[TEXT_AREA],
18612 row->contains_overlapping_glyphs_p,
18613 row->enabled_p,
18614 row->truncated_on_left_p,
18615 row->truncated_on_right_p,
18616 row->continued_p,
18617 MATRIX_ROW_CONTINUATION_LINE_P (row),
18618 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18619 row->ends_at_zv_p,
18620 row->fill_line_p,
18621 row->ends_in_middle_of_char_p,
18622 row->starts_in_middle_of_char_p,
18623 row->mouse_face_p,
18624 row->x,
18625 row->y,
18626 row->pixel_width,
18627 row->height,
18628 row->visible_height,
18629 row->ascent,
18630 row->phys_ascent);
18631 /* The next 3 lines should align to "Start" in the header. */
18632 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18633 row->end.overlay_string_index,
18634 row->continuation_lines_width);
18635 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18636 CHARPOS (row->start.string_pos),
18637 CHARPOS (row->end.string_pos));
18638 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18639 row->end.dpvec_index);
18640 }
18641
18642 if (glyphs > 1)
18643 {
18644 int area;
18645
18646 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18647 {
18648 struct glyph *glyph = row->glyphs[area];
18649 struct glyph *glyph_end = glyph + row->used[area];
18650
18651 /* Glyph for a line end in text. */
18652 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18653 ++glyph_end;
18654
18655 if (glyph < glyph_end)
18656 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18657
18658 for (; glyph < glyph_end; ++glyph)
18659 dump_glyph (row, glyph, area);
18660 }
18661 }
18662 else if (glyphs == 1)
18663 {
18664 int area;
18665 char s[SHRT_MAX + 4];
18666
18667 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18668 {
18669 int i;
18670
18671 for (i = 0; i < row->used[area]; ++i)
18672 {
18673 struct glyph *glyph = row->glyphs[area] + i;
18674 if (i == row->used[area] - 1
18675 && area == TEXT_AREA
18676 && NILP (glyph->object)
18677 && glyph->type == CHAR_GLYPH
18678 && glyph->u.ch == ' ')
18679 {
18680 strcpy (&s[i], "[\\n]");
18681 i += 4;
18682 }
18683 else if (glyph->type == CHAR_GLYPH
18684 && glyph->u.ch < 0x80
18685 && glyph->u.ch >= ' ')
18686 s[i] = glyph->u.ch;
18687 else
18688 s[i] = '.';
18689 }
18690
18691 s[i] = '\0';
18692 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18693 }
18694 }
18695 }
18696
18697
18698 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18699 Sdump_glyph_matrix, 0, 1, "p",
18700 doc: /* Dump the current matrix of the selected window to stderr.
18701 Shows contents of glyph row structures. With non-nil
18702 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18703 glyphs in short form, otherwise show glyphs in long form.
18704
18705 Interactively, no argument means show glyphs in short form;
18706 with numeric argument, its value is passed as the GLYPHS flag. */)
18707 (Lisp_Object glyphs)
18708 {
18709 struct window *w = XWINDOW (selected_window);
18710 struct buffer *buffer = XBUFFER (w->contents);
18711
18712 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18713 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18714 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18715 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18716 fprintf (stderr, "=============================================\n");
18717 dump_glyph_matrix (w->current_matrix,
18718 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18719 return Qnil;
18720 }
18721
18722
18723 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18724 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18725 Only text-mode frames have frame glyph matrices. */)
18726 (void)
18727 {
18728 struct frame *f = XFRAME (selected_frame);
18729
18730 if (f->current_matrix)
18731 dump_glyph_matrix (f->current_matrix, 1);
18732 else
18733 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18734 return Qnil;
18735 }
18736
18737
18738 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18739 doc: /* Dump glyph row ROW to stderr.
18740 GLYPH 0 means don't dump glyphs.
18741 GLYPH 1 means dump glyphs in short form.
18742 GLYPH > 1 or omitted means dump glyphs in long form. */)
18743 (Lisp_Object row, Lisp_Object glyphs)
18744 {
18745 struct glyph_matrix *matrix;
18746 EMACS_INT vpos;
18747
18748 CHECK_NUMBER (row);
18749 matrix = XWINDOW (selected_window)->current_matrix;
18750 vpos = XINT (row);
18751 if (vpos >= 0 && vpos < matrix->nrows)
18752 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18753 vpos,
18754 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18755 return Qnil;
18756 }
18757
18758
18759 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18760 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18761 GLYPH 0 means don't dump glyphs.
18762 GLYPH 1 means dump glyphs in short form.
18763 GLYPH > 1 or omitted means dump glyphs in long form.
18764
18765 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18766 do nothing. */)
18767 (Lisp_Object row, Lisp_Object glyphs)
18768 {
18769 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18770 struct frame *sf = SELECTED_FRAME ();
18771 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18772 EMACS_INT vpos;
18773
18774 CHECK_NUMBER (row);
18775 vpos = XINT (row);
18776 if (vpos >= 0 && vpos < m->nrows)
18777 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18778 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18779 #endif
18780 return Qnil;
18781 }
18782
18783
18784 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18785 doc: /* Toggle tracing of redisplay.
18786 With ARG, turn tracing on if and only if ARG is positive. */)
18787 (Lisp_Object arg)
18788 {
18789 if (NILP (arg))
18790 trace_redisplay_p = !trace_redisplay_p;
18791 else
18792 {
18793 arg = Fprefix_numeric_value (arg);
18794 trace_redisplay_p = XINT (arg) > 0;
18795 }
18796
18797 return Qnil;
18798 }
18799
18800
18801 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18802 doc: /* Like `format', but print result to stderr.
18803 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18804 (ptrdiff_t nargs, Lisp_Object *args)
18805 {
18806 Lisp_Object s = Fformat (nargs, args);
18807 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18808 return Qnil;
18809 }
18810
18811 #endif /* GLYPH_DEBUG */
18812
18813
18814 \f
18815 /***********************************************************************
18816 Building Desired Matrix Rows
18817 ***********************************************************************/
18818
18819 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18820 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18821
18822 static struct glyph_row *
18823 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18824 {
18825 struct frame *f = XFRAME (WINDOW_FRAME (w));
18826 struct buffer *buffer = XBUFFER (w->contents);
18827 struct buffer *old = current_buffer;
18828 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18829 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18830 const unsigned char *arrow_end = arrow_string + arrow_len;
18831 const unsigned char *p;
18832 struct it it;
18833 bool multibyte_p;
18834 int n_glyphs_before;
18835
18836 set_buffer_temp (buffer);
18837 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18838 scratch_glyph_row.reversed_p = false;
18839 it.glyph_row->used[TEXT_AREA] = 0;
18840 SET_TEXT_POS (it.position, 0, 0);
18841
18842 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18843 p = arrow_string;
18844 while (p < arrow_end)
18845 {
18846 Lisp_Object face, ilisp;
18847
18848 /* Get the next character. */
18849 if (multibyte_p)
18850 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18851 else
18852 {
18853 it.c = it.char_to_display = *p, it.len = 1;
18854 if (! ASCII_CHAR_P (it.c))
18855 it.char_to_display = BYTE8_TO_CHAR (it.c);
18856 }
18857 p += it.len;
18858
18859 /* Get its face. */
18860 ilisp = make_number (p - arrow_string);
18861 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18862 it.face_id = compute_char_face (f, it.char_to_display, face);
18863
18864 /* Compute its width, get its glyphs. */
18865 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18866 SET_TEXT_POS (it.position, -1, -1);
18867 PRODUCE_GLYPHS (&it);
18868
18869 /* If this character doesn't fit any more in the line, we have
18870 to remove some glyphs. */
18871 if (it.current_x > it.last_visible_x)
18872 {
18873 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18874 break;
18875 }
18876 }
18877
18878 set_buffer_temp (old);
18879 return it.glyph_row;
18880 }
18881
18882
18883 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18884 glyphs to insert is determined by produce_special_glyphs. */
18885
18886 static void
18887 insert_left_trunc_glyphs (struct it *it)
18888 {
18889 struct it truncate_it;
18890 struct glyph *from, *end, *to, *toend;
18891
18892 eassert (!FRAME_WINDOW_P (it->f)
18893 || (!it->glyph_row->reversed_p
18894 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18895 || (it->glyph_row->reversed_p
18896 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18897
18898 /* Get the truncation glyphs. */
18899 truncate_it = *it;
18900 truncate_it.current_x = 0;
18901 truncate_it.face_id = DEFAULT_FACE_ID;
18902 truncate_it.glyph_row = &scratch_glyph_row;
18903 truncate_it.area = TEXT_AREA;
18904 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18905 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18906 truncate_it.object = Qnil;
18907 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18908
18909 /* Overwrite glyphs from IT with truncation glyphs. */
18910 if (!it->glyph_row->reversed_p)
18911 {
18912 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18913
18914 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18915 end = from + tused;
18916 to = it->glyph_row->glyphs[TEXT_AREA];
18917 toend = to + it->glyph_row->used[TEXT_AREA];
18918 if (FRAME_WINDOW_P (it->f))
18919 {
18920 /* On GUI frames, when variable-size fonts are displayed,
18921 the truncation glyphs may need more pixels than the row's
18922 glyphs they overwrite. We overwrite more glyphs to free
18923 enough screen real estate, and enlarge the stretch glyph
18924 on the right (see display_line), if there is one, to
18925 preserve the screen position of the truncation glyphs on
18926 the right. */
18927 int w = 0;
18928 struct glyph *g = to;
18929 short used;
18930
18931 /* The first glyph could be partially visible, in which case
18932 it->glyph_row->x will be negative. But we want the left
18933 truncation glyphs to be aligned at the left margin of the
18934 window, so we override the x coordinate at which the row
18935 will begin. */
18936 it->glyph_row->x = 0;
18937 while (g < toend && w < it->truncation_pixel_width)
18938 {
18939 w += g->pixel_width;
18940 ++g;
18941 }
18942 if (g - to - tused > 0)
18943 {
18944 memmove (to + tused, g, (toend - g) * sizeof(*g));
18945 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18946 }
18947 used = it->glyph_row->used[TEXT_AREA];
18948 if (it->glyph_row->truncated_on_right_p
18949 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18950 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18951 == STRETCH_GLYPH)
18952 {
18953 int extra = w - it->truncation_pixel_width;
18954
18955 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18956 }
18957 }
18958
18959 while (from < end)
18960 *to++ = *from++;
18961
18962 /* There may be padding glyphs left over. Overwrite them too. */
18963 if (!FRAME_WINDOW_P (it->f))
18964 {
18965 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18966 {
18967 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18968 while (from < end)
18969 *to++ = *from++;
18970 }
18971 }
18972
18973 if (to > toend)
18974 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18975 }
18976 else
18977 {
18978 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18979
18980 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18981 that back to front. */
18982 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18983 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18984 toend = it->glyph_row->glyphs[TEXT_AREA];
18985 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18986 if (FRAME_WINDOW_P (it->f))
18987 {
18988 int w = 0;
18989 struct glyph *g = to;
18990
18991 while (g >= toend && w < it->truncation_pixel_width)
18992 {
18993 w += g->pixel_width;
18994 --g;
18995 }
18996 if (to - g - tused > 0)
18997 to = g + tused;
18998 if (it->glyph_row->truncated_on_right_p
18999 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19000 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19001 {
19002 int extra = w - it->truncation_pixel_width;
19003
19004 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19005 }
19006 }
19007
19008 while (from >= end && to >= toend)
19009 *to-- = *from--;
19010 if (!FRAME_WINDOW_P (it->f))
19011 {
19012 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19013 {
19014 from =
19015 truncate_it.glyph_row->glyphs[TEXT_AREA]
19016 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19017 while (from >= end && to >= toend)
19018 *to-- = *from--;
19019 }
19020 }
19021 if (from >= end)
19022 {
19023 /* Need to free some room before prepending additional
19024 glyphs. */
19025 int move_by = from - end + 1;
19026 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19027 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19028
19029 for ( ; g >= g0; g--)
19030 g[move_by] = *g;
19031 while (from >= end)
19032 *to-- = *from--;
19033 it->glyph_row->used[TEXT_AREA] += move_by;
19034 }
19035 }
19036 }
19037
19038 /* Compute the hash code for ROW. */
19039 unsigned
19040 row_hash (struct glyph_row *row)
19041 {
19042 int area, k;
19043 unsigned hashval = 0;
19044
19045 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19046 for (k = 0; k < row->used[area]; ++k)
19047 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19048 + row->glyphs[area][k].u.val
19049 + row->glyphs[area][k].face_id
19050 + row->glyphs[area][k].padding_p
19051 + (row->glyphs[area][k].type << 2));
19052
19053 return hashval;
19054 }
19055
19056 /* Compute the pixel height and width of IT->glyph_row.
19057
19058 Most of the time, ascent and height of a display line will be equal
19059 to the max_ascent and max_height values of the display iterator
19060 structure. This is not the case if
19061
19062 1. We hit ZV without displaying anything. In this case, max_ascent
19063 and max_height will be zero.
19064
19065 2. We have some glyphs that don't contribute to the line height.
19066 (The glyph row flag contributes_to_line_height_p is for future
19067 pixmap extensions).
19068
19069 The first case is easily covered by using default values because in
19070 these cases, the line height does not really matter, except that it
19071 must not be zero. */
19072
19073 static void
19074 compute_line_metrics (struct it *it)
19075 {
19076 struct glyph_row *row = it->glyph_row;
19077
19078 if (FRAME_WINDOW_P (it->f))
19079 {
19080 int i, min_y, max_y;
19081
19082 /* The line may consist of one space only, that was added to
19083 place the cursor on it. If so, the row's height hasn't been
19084 computed yet. */
19085 if (row->height == 0)
19086 {
19087 if (it->max_ascent + it->max_descent == 0)
19088 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19089 row->ascent = it->max_ascent;
19090 row->height = it->max_ascent + it->max_descent;
19091 row->phys_ascent = it->max_phys_ascent;
19092 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19093 row->extra_line_spacing = it->max_extra_line_spacing;
19094 }
19095
19096 /* Compute the width of this line. */
19097 row->pixel_width = row->x;
19098 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19099 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19100
19101 eassert (row->pixel_width >= 0);
19102 eassert (row->ascent >= 0 && row->height > 0);
19103
19104 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19105 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19106
19107 /* If first line's physical ascent is larger than its logical
19108 ascent, use the physical ascent, and make the row taller.
19109 This makes accented characters fully visible. */
19110 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19111 && row->phys_ascent > row->ascent)
19112 {
19113 row->height += row->phys_ascent - row->ascent;
19114 row->ascent = row->phys_ascent;
19115 }
19116
19117 /* Compute how much of the line is visible. */
19118 row->visible_height = row->height;
19119
19120 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19121 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19122
19123 if (row->y < min_y)
19124 row->visible_height -= min_y - row->y;
19125 if (row->y + row->height > max_y)
19126 row->visible_height -= row->y + row->height - max_y;
19127 }
19128 else
19129 {
19130 row->pixel_width = row->used[TEXT_AREA];
19131 if (row->continued_p)
19132 row->pixel_width -= it->continuation_pixel_width;
19133 else if (row->truncated_on_right_p)
19134 row->pixel_width -= it->truncation_pixel_width;
19135 row->ascent = row->phys_ascent = 0;
19136 row->height = row->phys_height = row->visible_height = 1;
19137 row->extra_line_spacing = 0;
19138 }
19139
19140 /* Compute a hash code for this row. */
19141 row->hash = row_hash (row);
19142
19143 it->max_ascent = it->max_descent = 0;
19144 it->max_phys_ascent = it->max_phys_descent = 0;
19145 }
19146
19147
19148 /* Append one space to the glyph row of iterator IT if doing a
19149 window-based redisplay. The space has the same face as
19150 IT->face_id. Value is true if a space was added.
19151
19152 This function is called to make sure that there is always one glyph
19153 at the end of a glyph row that the cursor can be set on under
19154 window-systems. (If there weren't such a glyph we would not know
19155 how wide and tall a box cursor should be displayed).
19156
19157 At the same time this space let's a nicely handle clearing to the
19158 end of the line if the row ends in italic text. */
19159
19160 static bool
19161 append_space_for_newline (struct it *it, bool default_face_p)
19162 {
19163 if (FRAME_WINDOW_P (it->f))
19164 {
19165 int n = it->glyph_row->used[TEXT_AREA];
19166
19167 if (it->glyph_row->glyphs[TEXT_AREA] + n
19168 < it->glyph_row->glyphs[1 + TEXT_AREA])
19169 {
19170 /* Save some values that must not be changed.
19171 Must save IT->c and IT->len because otherwise
19172 ITERATOR_AT_END_P wouldn't work anymore after
19173 append_space_for_newline has been called. */
19174 enum display_element_type saved_what = it->what;
19175 int saved_c = it->c, saved_len = it->len;
19176 int saved_char_to_display = it->char_to_display;
19177 int saved_x = it->current_x;
19178 int saved_face_id = it->face_id;
19179 bool saved_box_end = it->end_of_box_run_p;
19180 struct text_pos saved_pos;
19181 Lisp_Object saved_object;
19182 struct face *face;
19183 struct glyph *g;
19184
19185 saved_object = it->object;
19186 saved_pos = it->position;
19187
19188 it->what = IT_CHARACTER;
19189 memset (&it->position, 0, sizeof it->position);
19190 it->object = Qnil;
19191 it->c = it->char_to_display = ' ';
19192 it->len = 1;
19193
19194 /* If the default face was remapped, be sure to use the
19195 remapped face for the appended newline. */
19196 if (default_face_p)
19197 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19198 else if (it->face_before_selective_p)
19199 it->face_id = it->saved_face_id;
19200 face = FACE_FROM_ID (it->f, it->face_id);
19201 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19202 /* In R2L rows, we will prepend a stretch glyph that will
19203 have the end_of_box_run_p flag set for it, so there's no
19204 need for the appended newline glyph to have that flag
19205 set. */
19206 if (it->glyph_row->reversed_p
19207 /* But if the appended newline glyph goes all the way to
19208 the end of the row, there will be no stretch glyph,
19209 so leave the box flag set. */
19210 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19211 it->end_of_box_run_p = false;
19212
19213 PRODUCE_GLYPHS (it);
19214
19215 /* Make sure this space glyph has the right ascent value, or
19216 else hollow cursor at end of line will look funny. */
19217 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19218 g->ascent = it->glyph_row->ascent;
19219
19220 it->override_ascent = -1;
19221 it->constrain_row_ascent_descent_p = false;
19222 it->current_x = saved_x;
19223 it->object = saved_object;
19224 it->position = saved_pos;
19225 it->what = saved_what;
19226 it->face_id = saved_face_id;
19227 it->len = saved_len;
19228 it->c = saved_c;
19229 it->char_to_display = saved_char_to_display;
19230 it->end_of_box_run_p = saved_box_end;
19231 return true;
19232 }
19233 }
19234
19235 return false;
19236 }
19237
19238
19239 /* Extend the face of the last glyph in the text area of IT->glyph_row
19240 to the end of the display line. Called from display_line. If the
19241 glyph row is empty, add a space glyph to it so that we know the
19242 face to draw. Set the glyph row flag fill_line_p. If the glyph
19243 row is R2L, prepend a stretch glyph to cover the empty space to the
19244 left of the leftmost glyph. */
19245
19246 static void
19247 extend_face_to_end_of_line (struct it *it)
19248 {
19249 struct face *face, *default_face;
19250 struct frame *f = it->f;
19251
19252 /* If line is already filled, do nothing. Non window-system frames
19253 get a grace of one more ``pixel'' because their characters are
19254 1-``pixel'' wide, so they hit the equality too early. This grace
19255 is needed only for R2L rows that are not continued, to produce
19256 one extra blank where we could display the cursor. */
19257 if ((it->current_x >= it->last_visible_x
19258 + (!FRAME_WINDOW_P (f)
19259 && it->glyph_row->reversed_p
19260 && !it->glyph_row->continued_p))
19261 /* If the window has display margins, we will need to extend
19262 their face even if the text area is filled. */
19263 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19264 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19265 return;
19266
19267 /* The default face, possibly remapped. */
19268 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19269
19270 /* Face extension extends the background and box of IT->face_id
19271 to the end of the line. If the background equals the background
19272 of the frame, we don't have to do anything. */
19273 if (it->face_before_selective_p)
19274 face = FACE_FROM_ID (f, it->saved_face_id);
19275 else
19276 face = FACE_FROM_ID (f, it->face_id);
19277
19278 if (FRAME_WINDOW_P (f)
19279 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19280 && face->box == FACE_NO_BOX
19281 && face->background == FRAME_BACKGROUND_PIXEL (f)
19282 #ifdef HAVE_WINDOW_SYSTEM
19283 && !face->stipple
19284 #endif
19285 && !it->glyph_row->reversed_p)
19286 return;
19287
19288 /* Set the glyph row flag indicating that the face of the last glyph
19289 in the text area has to be drawn to the end of the text area. */
19290 it->glyph_row->fill_line_p = true;
19291
19292 /* If current character of IT is not ASCII, make sure we have the
19293 ASCII face. This will be automatically undone the next time
19294 get_next_display_element returns a multibyte character. Note
19295 that the character will always be single byte in unibyte
19296 text. */
19297 if (!ASCII_CHAR_P (it->c))
19298 {
19299 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19300 }
19301
19302 if (FRAME_WINDOW_P (f))
19303 {
19304 /* If the row is empty, add a space with the current face of IT,
19305 so that we know which face to draw. */
19306 if (it->glyph_row->used[TEXT_AREA] == 0)
19307 {
19308 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19309 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19310 it->glyph_row->used[TEXT_AREA] = 1;
19311 }
19312 /* Mode line and the header line don't have margins, and
19313 likewise the frame's tool-bar window, if there is any. */
19314 if (!(it->glyph_row->mode_line_p
19315 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19316 || (WINDOWP (f->tool_bar_window)
19317 && it->w == XWINDOW (f->tool_bar_window))
19318 #endif
19319 ))
19320 {
19321 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19322 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19323 {
19324 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19325 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19326 default_face->id;
19327 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19328 }
19329 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19330 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19331 {
19332 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19333 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19334 default_face->id;
19335 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19336 }
19337 }
19338 #ifdef HAVE_WINDOW_SYSTEM
19339 if (it->glyph_row->reversed_p)
19340 {
19341 /* Prepend a stretch glyph to the row, such that the
19342 rightmost glyph will be drawn flushed all the way to the
19343 right margin of the window. The stretch glyph that will
19344 occupy the empty space, if any, to the left of the
19345 glyphs. */
19346 struct font *font = face->font ? face->font : FRAME_FONT (f);
19347 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19348 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19349 struct glyph *g;
19350 int row_width, stretch_ascent, stretch_width;
19351 struct text_pos saved_pos;
19352 int saved_face_id;
19353 bool saved_avoid_cursor, saved_box_start;
19354
19355 for (row_width = 0, g = row_start; g < row_end; g++)
19356 row_width += g->pixel_width;
19357
19358 /* FIXME: There are various minor display glitches in R2L
19359 rows when only one of the fringes is missing. The
19360 strange condition below produces the least bad effect. */
19361 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19362 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19363 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19364 stretch_width = window_box_width (it->w, TEXT_AREA);
19365 else
19366 stretch_width = it->last_visible_x - it->first_visible_x;
19367 stretch_width -= row_width;
19368
19369 if (stretch_width > 0)
19370 {
19371 stretch_ascent =
19372 (((it->ascent + it->descent)
19373 * FONT_BASE (font)) / FONT_HEIGHT (font));
19374 saved_pos = it->position;
19375 memset (&it->position, 0, sizeof it->position);
19376 saved_avoid_cursor = it->avoid_cursor_p;
19377 it->avoid_cursor_p = true;
19378 saved_face_id = it->face_id;
19379 saved_box_start = it->start_of_box_run_p;
19380 /* The last row's stretch glyph should get the default
19381 face, to avoid painting the rest of the window with
19382 the region face, if the region ends at ZV. */
19383 if (it->glyph_row->ends_at_zv_p)
19384 it->face_id = default_face->id;
19385 else
19386 it->face_id = face->id;
19387 it->start_of_box_run_p = false;
19388 append_stretch_glyph (it, Qnil, stretch_width,
19389 it->ascent + it->descent, stretch_ascent);
19390 it->position = saved_pos;
19391 it->avoid_cursor_p = saved_avoid_cursor;
19392 it->face_id = saved_face_id;
19393 it->start_of_box_run_p = saved_box_start;
19394 }
19395 /* If stretch_width comes out negative, it means that the
19396 last glyph is only partially visible. In R2L rows, we
19397 want the leftmost glyph to be partially visible, so we
19398 need to give the row the corresponding left offset. */
19399 if (stretch_width < 0)
19400 it->glyph_row->x = stretch_width;
19401 }
19402 #endif /* HAVE_WINDOW_SYSTEM */
19403 }
19404 else
19405 {
19406 /* Save some values that must not be changed. */
19407 int saved_x = it->current_x;
19408 struct text_pos saved_pos;
19409 Lisp_Object saved_object;
19410 enum display_element_type saved_what = it->what;
19411 int saved_face_id = it->face_id;
19412
19413 saved_object = it->object;
19414 saved_pos = it->position;
19415
19416 it->what = IT_CHARACTER;
19417 memset (&it->position, 0, sizeof it->position);
19418 it->object = Qnil;
19419 it->c = it->char_to_display = ' ';
19420 it->len = 1;
19421
19422 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19423 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19424 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19425 && !it->glyph_row->mode_line_p
19426 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19427 {
19428 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19429 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19430
19431 for (it->current_x = 0; g < e; g++)
19432 it->current_x += g->pixel_width;
19433
19434 it->area = LEFT_MARGIN_AREA;
19435 it->face_id = default_face->id;
19436 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19437 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19438 {
19439 PRODUCE_GLYPHS (it);
19440 /* term.c:produce_glyphs advances it->current_x only for
19441 TEXT_AREA. */
19442 it->current_x += it->pixel_width;
19443 }
19444
19445 it->current_x = saved_x;
19446 it->area = TEXT_AREA;
19447 }
19448
19449 /* The last row's blank glyphs should get the default face, to
19450 avoid painting the rest of the window with the region face,
19451 if the region ends at ZV. */
19452 if (it->glyph_row->ends_at_zv_p)
19453 it->face_id = default_face->id;
19454 else
19455 it->face_id = face->id;
19456 PRODUCE_GLYPHS (it);
19457
19458 while (it->current_x <= it->last_visible_x)
19459 PRODUCE_GLYPHS (it);
19460
19461 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19462 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19463 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19464 && !it->glyph_row->mode_line_p
19465 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19466 {
19467 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19468 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19469
19470 for ( ; g < e; g++)
19471 it->current_x += g->pixel_width;
19472
19473 it->area = RIGHT_MARGIN_AREA;
19474 it->face_id = default_face->id;
19475 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19476 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19477 {
19478 PRODUCE_GLYPHS (it);
19479 it->current_x += it->pixel_width;
19480 }
19481
19482 it->area = TEXT_AREA;
19483 }
19484
19485 /* Don't count these blanks really. It would let us insert a left
19486 truncation glyph below and make us set the cursor on them, maybe. */
19487 it->current_x = saved_x;
19488 it->object = saved_object;
19489 it->position = saved_pos;
19490 it->what = saved_what;
19491 it->face_id = saved_face_id;
19492 }
19493 }
19494
19495
19496 /* Value is true if text starting at CHARPOS in current_buffer is
19497 trailing whitespace. */
19498
19499 static bool
19500 trailing_whitespace_p (ptrdiff_t charpos)
19501 {
19502 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19503 int c = 0;
19504
19505 while (bytepos < ZV_BYTE
19506 && (c = FETCH_CHAR (bytepos),
19507 c == ' ' || c == '\t'))
19508 ++bytepos;
19509
19510 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19511 {
19512 if (bytepos != PT_BYTE)
19513 return true;
19514 }
19515 return false;
19516 }
19517
19518
19519 /* Highlight trailing whitespace, if any, in ROW. */
19520
19521 static void
19522 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19523 {
19524 int used = row->used[TEXT_AREA];
19525
19526 if (used)
19527 {
19528 struct glyph *start = row->glyphs[TEXT_AREA];
19529 struct glyph *glyph = start + used - 1;
19530
19531 if (row->reversed_p)
19532 {
19533 /* Right-to-left rows need to be processed in the opposite
19534 direction, so swap the edge pointers. */
19535 glyph = start;
19536 start = row->glyphs[TEXT_AREA] + used - 1;
19537 }
19538
19539 /* Skip over glyphs inserted to display the cursor at the
19540 end of a line, for extending the face of the last glyph
19541 to the end of the line on terminals, and for truncation
19542 and continuation glyphs. */
19543 if (!row->reversed_p)
19544 {
19545 while (glyph >= start
19546 && glyph->type == CHAR_GLYPH
19547 && NILP (glyph->object))
19548 --glyph;
19549 }
19550 else
19551 {
19552 while (glyph <= start
19553 && glyph->type == CHAR_GLYPH
19554 && NILP (glyph->object))
19555 ++glyph;
19556 }
19557
19558 /* If last glyph is a space or stretch, and it's trailing
19559 whitespace, set the face of all trailing whitespace glyphs in
19560 IT->glyph_row to `trailing-whitespace'. */
19561 if ((row->reversed_p ? glyph <= start : glyph >= start)
19562 && BUFFERP (glyph->object)
19563 && (glyph->type == STRETCH_GLYPH
19564 || (glyph->type == CHAR_GLYPH
19565 && glyph->u.ch == ' '))
19566 && trailing_whitespace_p (glyph->charpos))
19567 {
19568 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19569 if (face_id < 0)
19570 return;
19571
19572 if (!row->reversed_p)
19573 {
19574 while (glyph >= start
19575 && BUFFERP (glyph->object)
19576 && (glyph->type == STRETCH_GLYPH
19577 || (glyph->type == CHAR_GLYPH
19578 && glyph->u.ch == ' ')))
19579 (glyph--)->face_id = face_id;
19580 }
19581 else
19582 {
19583 while (glyph <= start
19584 && BUFFERP (glyph->object)
19585 && (glyph->type == STRETCH_GLYPH
19586 || (glyph->type == CHAR_GLYPH
19587 && glyph->u.ch == ' ')))
19588 (glyph++)->face_id = face_id;
19589 }
19590 }
19591 }
19592 }
19593
19594
19595 /* Value is true if glyph row ROW should be
19596 considered to hold the buffer position CHARPOS. */
19597
19598 static bool
19599 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19600 {
19601 bool result = true;
19602
19603 if (charpos == CHARPOS (row->end.pos)
19604 || charpos == MATRIX_ROW_END_CHARPOS (row))
19605 {
19606 /* Suppose the row ends on a string.
19607 Unless the row is continued, that means it ends on a newline
19608 in the string. If it's anything other than a display string
19609 (e.g., a before-string from an overlay), we don't want the
19610 cursor there. (This heuristic seems to give the optimal
19611 behavior for the various types of multi-line strings.)
19612 One exception: if the string has `cursor' property on one of
19613 its characters, we _do_ want the cursor there. */
19614 if (CHARPOS (row->end.string_pos) >= 0)
19615 {
19616 if (row->continued_p)
19617 result = true;
19618 else
19619 {
19620 /* Check for `display' property. */
19621 struct glyph *beg = row->glyphs[TEXT_AREA];
19622 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19623 struct glyph *glyph;
19624
19625 result = false;
19626 for (glyph = end; glyph >= beg; --glyph)
19627 if (STRINGP (glyph->object))
19628 {
19629 Lisp_Object prop
19630 = Fget_char_property (make_number (charpos),
19631 Qdisplay, Qnil);
19632 result =
19633 (!NILP (prop)
19634 && display_prop_string_p (prop, glyph->object));
19635 /* If there's a `cursor' property on one of the
19636 string's characters, this row is a cursor row,
19637 even though this is not a display string. */
19638 if (!result)
19639 {
19640 Lisp_Object s = glyph->object;
19641
19642 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19643 {
19644 ptrdiff_t gpos = glyph->charpos;
19645
19646 if (!NILP (Fget_char_property (make_number (gpos),
19647 Qcursor, s)))
19648 {
19649 result = true;
19650 break;
19651 }
19652 }
19653 }
19654 break;
19655 }
19656 }
19657 }
19658 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19659 {
19660 /* If the row ends in middle of a real character,
19661 and the line is continued, we want the cursor here.
19662 That's because CHARPOS (ROW->end.pos) would equal
19663 PT if PT is before the character. */
19664 if (!row->ends_in_ellipsis_p)
19665 result = row->continued_p;
19666 else
19667 /* If the row ends in an ellipsis, then
19668 CHARPOS (ROW->end.pos) will equal point after the
19669 invisible text. We want that position to be displayed
19670 after the ellipsis. */
19671 result = false;
19672 }
19673 /* If the row ends at ZV, display the cursor at the end of that
19674 row instead of at the start of the row below. */
19675 else
19676 result = row->ends_at_zv_p;
19677 }
19678
19679 return result;
19680 }
19681
19682 /* Value is true if glyph row ROW should be
19683 used to hold the cursor. */
19684
19685 static bool
19686 cursor_row_p (struct glyph_row *row)
19687 {
19688 return row_for_charpos_p (row, PT);
19689 }
19690
19691 \f
19692
19693 /* Push the property PROP so that it will be rendered at the current
19694 position in IT. Return true if PROP was successfully pushed, false
19695 otherwise. Called from handle_line_prefix to handle the
19696 `line-prefix' and `wrap-prefix' properties. */
19697
19698 static bool
19699 push_prefix_prop (struct it *it, Lisp_Object prop)
19700 {
19701 struct text_pos pos =
19702 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19703
19704 eassert (it->method == GET_FROM_BUFFER
19705 || it->method == GET_FROM_DISPLAY_VECTOR
19706 || it->method == GET_FROM_STRING);
19707
19708 /* We need to save the current buffer/string position, so it will be
19709 restored by pop_it, because iterate_out_of_display_property
19710 depends on that being set correctly, but some situations leave
19711 it->position not yet set when this function is called. */
19712 push_it (it, &pos);
19713
19714 if (STRINGP (prop))
19715 {
19716 if (SCHARS (prop) == 0)
19717 {
19718 pop_it (it);
19719 return false;
19720 }
19721
19722 it->string = prop;
19723 it->string_from_prefix_prop_p = true;
19724 it->multibyte_p = STRING_MULTIBYTE (it->string);
19725 it->current.overlay_string_index = -1;
19726 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19727 it->end_charpos = it->string_nchars = SCHARS (it->string);
19728 it->method = GET_FROM_STRING;
19729 it->stop_charpos = 0;
19730 it->prev_stop = 0;
19731 it->base_level_stop = 0;
19732
19733 /* Force paragraph direction to be that of the parent
19734 buffer/string. */
19735 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19736 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19737 else
19738 it->paragraph_embedding = L2R;
19739
19740 /* Set up the bidi iterator for this display string. */
19741 if (it->bidi_p)
19742 {
19743 it->bidi_it.string.lstring = it->string;
19744 it->bidi_it.string.s = NULL;
19745 it->bidi_it.string.schars = it->end_charpos;
19746 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19747 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19748 it->bidi_it.string.unibyte = !it->multibyte_p;
19749 it->bidi_it.w = it->w;
19750 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19751 }
19752 }
19753 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19754 {
19755 it->method = GET_FROM_STRETCH;
19756 it->object = prop;
19757 }
19758 #ifdef HAVE_WINDOW_SYSTEM
19759 else if (IMAGEP (prop))
19760 {
19761 it->what = IT_IMAGE;
19762 it->image_id = lookup_image (it->f, prop);
19763 it->method = GET_FROM_IMAGE;
19764 }
19765 #endif /* HAVE_WINDOW_SYSTEM */
19766 else
19767 {
19768 pop_it (it); /* bogus display property, give up */
19769 return false;
19770 }
19771
19772 return true;
19773 }
19774
19775 /* Return the character-property PROP at the current position in IT. */
19776
19777 static Lisp_Object
19778 get_it_property (struct it *it, Lisp_Object prop)
19779 {
19780 Lisp_Object position, object = it->object;
19781
19782 if (STRINGP (object))
19783 position = make_number (IT_STRING_CHARPOS (*it));
19784 else if (BUFFERP (object))
19785 {
19786 position = make_number (IT_CHARPOS (*it));
19787 object = it->window;
19788 }
19789 else
19790 return Qnil;
19791
19792 return Fget_char_property (position, prop, object);
19793 }
19794
19795 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19796
19797 static void
19798 handle_line_prefix (struct it *it)
19799 {
19800 Lisp_Object prefix;
19801
19802 if (it->continuation_lines_width > 0)
19803 {
19804 prefix = get_it_property (it, Qwrap_prefix);
19805 if (NILP (prefix))
19806 prefix = Vwrap_prefix;
19807 }
19808 else
19809 {
19810 prefix = get_it_property (it, Qline_prefix);
19811 if (NILP (prefix))
19812 prefix = Vline_prefix;
19813 }
19814 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19815 {
19816 /* If the prefix is wider than the window, and we try to wrap
19817 it, it would acquire its own wrap prefix, and so on till the
19818 iterator stack overflows. So, don't wrap the prefix. */
19819 it->line_wrap = TRUNCATE;
19820 it->avoid_cursor_p = true;
19821 }
19822 }
19823
19824 \f
19825
19826 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19827 only for R2L lines from display_line and display_string, when they
19828 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19829 the line/string needs to be continued on the next glyph row. */
19830 static void
19831 unproduce_glyphs (struct it *it, int n)
19832 {
19833 struct glyph *glyph, *end;
19834
19835 eassert (it->glyph_row);
19836 eassert (it->glyph_row->reversed_p);
19837 eassert (it->area == TEXT_AREA);
19838 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19839
19840 if (n > it->glyph_row->used[TEXT_AREA])
19841 n = it->glyph_row->used[TEXT_AREA];
19842 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19843 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19844 for ( ; glyph < end; glyph++)
19845 glyph[-n] = *glyph;
19846 }
19847
19848 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19849 and ROW->maxpos. */
19850 static void
19851 find_row_edges (struct it *it, struct glyph_row *row,
19852 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19853 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19854 {
19855 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19856 lines' rows is implemented for bidi-reordered rows. */
19857
19858 /* ROW->minpos is the value of min_pos, the minimal buffer position
19859 we have in ROW, or ROW->start.pos if that is smaller. */
19860 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19861 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19862 else
19863 /* We didn't find buffer positions smaller than ROW->start, or
19864 didn't find _any_ valid buffer positions in any of the glyphs,
19865 so we must trust the iterator's computed positions. */
19866 row->minpos = row->start.pos;
19867 if (max_pos <= 0)
19868 {
19869 max_pos = CHARPOS (it->current.pos);
19870 max_bpos = BYTEPOS (it->current.pos);
19871 }
19872
19873 /* Here are the various use-cases for ending the row, and the
19874 corresponding values for ROW->maxpos:
19875
19876 Line ends in a newline from buffer eol_pos + 1
19877 Line is continued from buffer max_pos + 1
19878 Line is truncated on right it->current.pos
19879 Line ends in a newline from string max_pos + 1(*)
19880 (*) + 1 only when line ends in a forward scan
19881 Line is continued from string max_pos
19882 Line is continued from display vector max_pos
19883 Line is entirely from a string min_pos == max_pos
19884 Line is entirely from a display vector min_pos == max_pos
19885 Line that ends at ZV ZV
19886
19887 If you discover other use-cases, please add them here as
19888 appropriate. */
19889 if (row->ends_at_zv_p)
19890 row->maxpos = it->current.pos;
19891 else if (row->used[TEXT_AREA])
19892 {
19893 bool seen_this_string = false;
19894 struct glyph_row *r1 = row - 1;
19895
19896 /* Did we see the same display string on the previous row? */
19897 if (STRINGP (it->object)
19898 /* this is not the first row */
19899 && row > it->w->desired_matrix->rows
19900 /* previous row is not the header line */
19901 && !r1->mode_line_p
19902 /* previous row also ends in a newline from a string */
19903 && r1->ends_in_newline_from_string_p)
19904 {
19905 struct glyph *start, *end;
19906
19907 /* Search for the last glyph of the previous row that came
19908 from buffer or string. Depending on whether the row is
19909 L2R or R2L, we need to process it front to back or the
19910 other way round. */
19911 if (!r1->reversed_p)
19912 {
19913 start = r1->glyphs[TEXT_AREA];
19914 end = start + r1->used[TEXT_AREA];
19915 /* Glyphs inserted by redisplay have nil as their object. */
19916 while (end > start
19917 && NILP ((end - 1)->object)
19918 && (end - 1)->charpos <= 0)
19919 --end;
19920 if (end > start)
19921 {
19922 if (EQ ((end - 1)->object, it->object))
19923 seen_this_string = true;
19924 }
19925 else
19926 /* If all the glyphs of the previous row were inserted
19927 by redisplay, it means the previous row was
19928 produced from a single newline, which is only
19929 possible if that newline came from the same string
19930 as the one which produced this ROW. */
19931 seen_this_string = true;
19932 }
19933 else
19934 {
19935 end = r1->glyphs[TEXT_AREA] - 1;
19936 start = end + r1->used[TEXT_AREA];
19937 while (end < start
19938 && NILP ((end + 1)->object)
19939 && (end + 1)->charpos <= 0)
19940 ++end;
19941 if (end < start)
19942 {
19943 if (EQ ((end + 1)->object, it->object))
19944 seen_this_string = true;
19945 }
19946 else
19947 seen_this_string = true;
19948 }
19949 }
19950 /* Take note of each display string that covers a newline only
19951 once, the first time we see it. This is for when a display
19952 string includes more than one newline in it. */
19953 if (row->ends_in_newline_from_string_p && !seen_this_string)
19954 {
19955 /* If we were scanning the buffer forward when we displayed
19956 the string, we want to account for at least one buffer
19957 position that belongs to this row (position covered by
19958 the display string), so that cursor positioning will
19959 consider this row as a candidate when point is at the end
19960 of the visual line represented by this row. This is not
19961 required when scanning back, because max_pos will already
19962 have a much larger value. */
19963 if (CHARPOS (row->end.pos) > max_pos)
19964 INC_BOTH (max_pos, max_bpos);
19965 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19966 }
19967 else if (CHARPOS (it->eol_pos) > 0)
19968 SET_TEXT_POS (row->maxpos,
19969 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19970 else if (row->continued_p)
19971 {
19972 /* If max_pos is different from IT's current position, it
19973 means IT->method does not belong to the display element
19974 at max_pos. However, it also means that the display
19975 element at max_pos was displayed in its entirety on this
19976 line, which is equivalent to saying that the next line
19977 starts at the next buffer position. */
19978 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19979 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19980 else
19981 {
19982 INC_BOTH (max_pos, max_bpos);
19983 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19984 }
19985 }
19986 else if (row->truncated_on_right_p)
19987 /* display_line already called reseat_at_next_visible_line_start,
19988 which puts the iterator at the beginning of the next line, in
19989 the logical order. */
19990 row->maxpos = it->current.pos;
19991 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19992 /* A line that is entirely from a string/image/stretch... */
19993 row->maxpos = row->minpos;
19994 else
19995 emacs_abort ();
19996 }
19997 else
19998 row->maxpos = it->current.pos;
19999 }
20000
20001 /* Construct the glyph row IT->glyph_row in the desired matrix of
20002 IT->w from text at the current position of IT. See dispextern.h
20003 for an overview of struct it. Value is true if
20004 IT->glyph_row displays text, as opposed to a line displaying ZV
20005 only. */
20006
20007 static bool
20008 display_line (struct it *it)
20009 {
20010 struct glyph_row *row = it->glyph_row;
20011 Lisp_Object overlay_arrow_string;
20012 struct it wrap_it;
20013 void *wrap_data = NULL;
20014 bool may_wrap = false;
20015 int wrap_x IF_LINT (= 0);
20016 int wrap_row_used = -1;
20017 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20018 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20019 int wrap_row_extra_line_spacing IF_LINT (= 0);
20020 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20021 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20022 int cvpos;
20023 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20024 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20025 bool pending_handle_line_prefix = false;
20026
20027 /* We always start displaying at hpos zero even if hscrolled. */
20028 eassert (it->hpos == 0 && it->current_x == 0);
20029
20030 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20031 >= it->w->desired_matrix->nrows)
20032 {
20033 it->w->nrows_scale_factor++;
20034 it->f->fonts_changed = true;
20035 return false;
20036 }
20037
20038 /* Clear the result glyph row and enable it. */
20039 prepare_desired_row (it->w, row, false);
20040
20041 row->y = it->current_y;
20042 row->start = it->start;
20043 row->continuation_lines_width = it->continuation_lines_width;
20044 row->displays_text_p = true;
20045 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20046 it->starts_in_middle_of_char_p = false;
20047
20048 /* Arrange the overlays nicely for our purposes. Usually, we call
20049 display_line on only one line at a time, in which case this
20050 can't really hurt too much, or we call it on lines which appear
20051 one after another in the buffer, in which case all calls to
20052 recenter_overlay_lists but the first will be pretty cheap. */
20053 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20054
20055 /* Move over display elements that are not visible because we are
20056 hscrolled. This may stop at an x-position < IT->first_visible_x
20057 if the first glyph is partially visible or if we hit a line end. */
20058 if (it->current_x < it->first_visible_x)
20059 {
20060 enum move_it_result move_result;
20061
20062 this_line_min_pos = row->start.pos;
20063 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20064 MOVE_TO_POS | MOVE_TO_X);
20065 /* If we are under a large hscroll, move_it_in_display_line_to
20066 could hit the end of the line without reaching
20067 it->first_visible_x. Pretend that we did reach it. This is
20068 especially important on a TTY, where we will call
20069 extend_face_to_end_of_line, which needs to know how many
20070 blank glyphs to produce. */
20071 if (it->current_x < it->first_visible_x
20072 && (move_result == MOVE_NEWLINE_OR_CR
20073 || move_result == MOVE_POS_MATCH_OR_ZV))
20074 it->current_x = it->first_visible_x;
20075
20076 /* Record the smallest positions seen while we moved over
20077 display elements that are not visible. This is needed by
20078 redisplay_internal for optimizing the case where the cursor
20079 stays inside the same line. The rest of this function only
20080 considers positions that are actually displayed, so
20081 RECORD_MAX_MIN_POS will not otherwise record positions that
20082 are hscrolled to the left of the left edge of the window. */
20083 min_pos = CHARPOS (this_line_min_pos);
20084 min_bpos = BYTEPOS (this_line_min_pos);
20085 }
20086 else if (it->area == TEXT_AREA)
20087 {
20088 /* We only do this when not calling move_it_in_display_line_to
20089 above, because that function calls itself handle_line_prefix. */
20090 handle_line_prefix (it);
20091 }
20092 else
20093 {
20094 /* Line-prefix and wrap-prefix are always displayed in the text
20095 area. But if this is the first call to display_line after
20096 init_iterator, the iterator might have been set up to write
20097 into a marginal area, e.g. if the line begins with some
20098 display property that writes to the margins. So we need to
20099 wait with the call to handle_line_prefix until whatever
20100 writes to the margin has done its job. */
20101 pending_handle_line_prefix = true;
20102 }
20103
20104 /* Get the initial row height. This is either the height of the
20105 text hscrolled, if there is any, or zero. */
20106 row->ascent = it->max_ascent;
20107 row->height = it->max_ascent + it->max_descent;
20108 row->phys_ascent = it->max_phys_ascent;
20109 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20110 row->extra_line_spacing = it->max_extra_line_spacing;
20111
20112 /* Utility macro to record max and min buffer positions seen until now. */
20113 #define RECORD_MAX_MIN_POS(IT) \
20114 do \
20115 { \
20116 bool composition_p \
20117 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20118 ptrdiff_t current_pos = \
20119 composition_p ? (IT)->cmp_it.charpos \
20120 : IT_CHARPOS (*(IT)); \
20121 ptrdiff_t current_bpos = \
20122 composition_p ? CHAR_TO_BYTE (current_pos) \
20123 : IT_BYTEPOS (*(IT)); \
20124 if (current_pos < min_pos) \
20125 { \
20126 min_pos = current_pos; \
20127 min_bpos = current_bpos; \
20128 } \
20129 if (IT_CHARPOS (*it) > max_pos) \
20130 { \
20131 max_pos = IT_CHARPOS (*it); \
20132 max_bpos = IT_BYTEPOS (*it); \
20133 } \
20134 } \
20135 while (false)
20136
20137 /* Loop generating characters. The loop is left with IT on the next
20138 character to display. */
20139 while (true)
20140 {
20141 int n_glyphs_before, hpos_before, x_before;
20142 int x, nglyphs;
20143 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20144
20145 /* Retrieve the next thing to display. Value is false if end of
20146 buffer reached. */
20147 if (!get_next_display_element (it))
20148 {
20149 /* Maybe add a space at the end of this line that is used to
20150 display the cursor there under X. Set the charpos of the
20151 first glyph of blank lines not corresponding to any text
20152 to -1. */
20153 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20154 row->exact_window_width_line_p = true;
20155 else if ((append_space_for_newline (it, true)
20156 && row->used[TEXT_AREA] == 1)
20157 || row->used[TEXT_AREA] == 0)
20158 {
20159 row->glyphs[TEXT_AREA]->charpos = -1;
20160 row->displays_text_p = false;
20161
20162 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20163 && (!MINI_WINDOW_P (it->w)
20164 || (minibuf_level && EQ (it->window, minibuf_window))))
20165 row->indicate_empty_line_p = true;
20166 }
20167
20168 it->continuation_lines_width = 0;
20169 row->ends_at_zv_p = true;
20170 /* A row that displays right-to-left text must always have
20171 its last face extended all the way to the end of line,
20172 even if this row ends in ZV, because we still write to
20173 the screen left to right. We also need to extend the
20174 last face if the default face is remapped to some
20175 different face, otherwise the functions that clear
20176 portions of the screen will clear with the default face's
20177 background color. */
20178 if (row->reversed_p
20179 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20180 extend_face_to_end_of_line (it);
20181 break;
20182 }
20183
20184 /* Now, get the metrics of what we want to display. This also
20185 generates glyphs in `row' (which is IT->glyph_row). */
20186 n_glyphs_before = row->used[TEXT_AREA];
20187 x = it->current_x;
20188
20189 /* Remember the line height so far in case the next element doesn't
20190 fit on the line. */
20191 if (it->line_wrap != TRUNCATE)
20192 {
20193 ascent = it->max_ascent;
20194 descent = it->max_descent;
20195 phys_ascent = it->max_phys_ascent;
20196 phys_descent = it->max_phys_descent;
20197
20198 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20199 {
20200 if (IT_DISPLAYING_WHITESPACE (it))
20201 may_wrap = true;
20202 else if (may_wrap)
20203 {
20204 SAVE_IT (wrap_it, *it, wrap_data);
20205 wrap_x = x;
20206 wrap_row_used = row->used[TEXT_AREA];
20207 wrap_row_ascent = row->ascent;
20208 wrap_row_height = row->height;
20209 wrap_row_phys_ascent = row->phys_ascent;
20210 wrap_row_phys_height = row->phys_height;
20211 wrap_row_extra_line_spacing = row->extra_line_spacing;
20212 wrap_row_min_pos = min_pos;
20213 wrap_row_min_bpos = min_bpos;
20214 wrap_row_max_pos = max_pos;
20215 wrap_row_max_bpos = max_bpos;
20216 may_wrap = false;
20217 }
20218 }
20219 }
20220
20221 PRODUCE_GLYPHS (it);
20222
20223 /* If this display element was in marginal areas, continue with
20224 the next one. */
20225 if (it->area != TEXT_AREA)
20226 {
20227 row->ascent = max (row->ascent, it->max_ascent);
20228 row->height = max (row->height, it->max_ascent + it->max_descent);
20229 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20230 row->phys_height = max (row->phys_height,
20231 it->max_phys_ascent + it->max_phys_descent);
20232 row->extra_line_spacing = max (row->extra_line_spacing,
20233 it->max_extra_line_spacing);
20234 set_iterator_to_next (it, true);
20235 /* If we didn't handle the line/wrap prefix above, and the
20236 call to set_iterator_to_next just switched to TEXT_AREA,
20237 process the prefix now. */
20238 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20239 {
20240 pending_handle_line_prefix = false;
20241 handle_line_prefix (it);
20242 }
20243 continue;
20244 }
20245
20246 /* Does the display element fit on the line? If we truncate
20247 lines, we should draw past the right edge of the window. If
20248 we don't truncate, we want to stop so that we can display the
20249 continuation glyph before the right margin. If lines are
20250 continued, there are two possible strategies for characters
20251 resulting in more than 1 glyph (e.g. tabs): Display as many
20252 glyphs as possible in this line and leave the rest for the
20253 continuation line, or display the whole element in the next
20254 line. Original redisplay did the former, so we do it also. */
20255 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20256 hpos_before = it->hpos;
20257 x_before = x;
20258
20259 if (/* Not a newline. */
20260 nglyphs > 0
20261 /* Glyphs produced fit entirely in the line. */
20262 && it->current_x < it->last_visible_x)
20263 {
20264 it->hpos += nglyphs;
20265 row->ascent = max (row->ascent, it->max_ascent);
20266 row->height = max (row->height, it->max_ascent + it->max_descent);
20267 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20268 row->phys_height = max (row->phys_height,
20269 it->max_phys_ascent + it->max_phys_descent);
20270 row->extra_line_spacing = max (row->extra_line_spacing,
20271 it->max_extra_line_spacing);
20272 if (it->current_x - it->pixel_width < it->first_visible_x
20273 /* In R2L rows, we arrange in extend_face_to_end_of_line
20274 to add a right offset to the line, by a suitable
20275 change to the stretch glyph that is the leftmost
20276 glyph of the line. */
20277 && !row->reversed_p)
20278 row->x = x - it->first_visible_x;
20279 /* Record the maximum and minimum buffer positions seen so
20280 far in glyphs that will be displayed by this row. */
20281 if (it->bidi_p)
20282 RECORD_MAX_MIN_POS (it);
20283 }
20284 else
20285 {
20286 int i, new_x;
20287 struct glyph *glyph;
20288
20289 for (i = 0; i < nglyphs; ++i, x = new_x)
20290 {
20291 /* Identify the glyphs added by the last call to
20292 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20293 the previous glyphs. */
20294 if (!row->reversed_p)
20295 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20296 else
20297 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20298 new_x = x + glyph->pixel_width;
20299
20300 if (/* Lines are continued. */
20301 it->line_wrap != TRUNCATE
20302 && (/* Glyph doesn't fit on the line. */
20303 new_x > it->last_visible_x
20304 /* Or it fits exactly on a window system frame. */
20305 || (new_x == it->last_visible_x
20306 && FRAME_WINDOW_P (it->f)
20307 && (row->reversed_p
20308 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20309 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20310 {
20311 /* End of a continued line. */
20312
20313 if (it->hpos == 0
20314 || (new_x == it->last_visible_x
20315 && FRAME_WINDOW_P (it->f)
20316 && (row->reversed_p
20317 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20318 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20319 {
20320 /* Current glyph is the only one on the line or
20321 fits exactly on the line. We must continue
20322 the line because we can't draw the cursor
20323 after the glyph. */
20324 row->continued_p = true;
20325 it->current_x = new_x;
20326 it->continuation_lines_width += new_x;
20327 ++it->hpos;
20328 if (i == nglyphs - 1)
20329 {
20330 /* If line-wrap is on, check if a previous
20331 wrap point was found. */
20332 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20333 && wrap_row_used > 0
20334 /* Even if there is a previous wrap
20335 point, continue the line here as
20336 usual, if (i) the previous character
20337 was a space or tab AND (ii) the
20338 current character is not. */
20339 && (!may_wrap
20340 || IT_DISPLAYING_WHITESPACE (it)))
20341 goto back_to_wrap;
20342
20343 /* Record the maximum and minimum buffer
20344 positions seen so far in glyphs that will be
20345 displayed by this row. */
20346 if (it->bidi_p)
20347 RECORD_MAX_MIN_POS (it);
20348 set_iterator_to_next (it, true);
20349 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20350 {
20351 if (!get_next_display_element (it))
20352 {
20353 row->exact_window_width_line_p = true;
20354 it->continuation_lines_width = 0;
20355 row->continued_p = false;
20356 row->ends_at_zv_p = true;
20357 }
20358 else if (ITERATOR_AT_END_OF_LINE_P (it))
20359 {
20360 row->continued_p = false;
20361 row->exact_window_width_line_p = true;
20362 }
20363 /* If line-wrap is on, check if a
20364 previous wrap point was found. */
20365 else if (wrap_row_used > 0
20366 /* Even if there is a previous wrap
20367 point, continue the line here as
20368 usual, if (i) the previous character
20369 was a space or tab AND (ii) the
20370 current character is not. */
20371 && (!may_wrap
20372 || IT_DISPLAYING_WHITESPACE (it)))
20373 goto back_to_wrap;
20374
20375 }
20376 }
20377 else if (it->bidi_p)
20378 RECORD_MAX_MIN_POS (it);
20379 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20380 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20381 extend_face_to_end_of_line (it);
20382 }
20383 else if (CHAR_GLYPH_PADDING_P (*glyph)
20384 && !FRAME_WINDOW_P (it->f))
20385 {
20386 /* A padding glyph that doesn't fit on this line.
20387 This means the whole character doesn't fit
20388 on the line. */
20389 if (row->reversed_p)
20390 unproduce_glyphs (it, row->used[TEXT_AREA]
20391 - n_glyphs_before);
20392 row->used[TEXT_AREA] = n_glyphs_before;
20393
20394 /* Fill the rest of the row with continuation
20395 glyphs like in 20.x. */
20396 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20397 < row->glyphs[1 + TEXT_AREA])
20398 produce_special_glyphs (it, IT_CONTINUATION);
20399
20400 row->continued_p = true;
20401 it->current_x = x_before;
20402 it->continuation_lines_width += x_before;
20403
20404 /* Restore the height to what it was before the
20405 element not fitting on the line. */
20406 it->max_ascent = ascent;
20407 it->max_descent = descent;
20408 it->max_phys_ascent = phys_ascent;
20409 it->max_phys_descent = phys_descent;
20410 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20411 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20412 extend_face_to_end_of_line (it);
20413 }
20414 else if (wrap_row_used > 0)
20415 {
20416 back_to_wrap:
20417 if (row->reversed_p)
20418 unproduce_glyphs (it,
20419 row->used[TEXT_AREA] - wrap_row_used);
20420 RESTORE_IT (it, &wrap_it, wrap_data);
20421 it->continuation_lines_width += wrap_x;
20422 row->used[TEXT_AREA] = wrap_row_used;
20423 row->ascent = wrap_row_ascent;
20424 row->height = wrap_row_height;
20425 row->phys_ascent = wrap_row_phys_ascent;
20426 row->phys_height = wrap_row_phys_height;
20427 row->extra_line_spacing = wrap_row_extra_line_spacing;
20428 min_pos = wrap_row_min_pos;
20429 min_bpos = wrap_row_min_bpos;
20430 max_pos = wrap_row_max_pos;
20431 max_bpos = wrap_row_max_bpos;
20432 row->continued_p = true;
20433 row->ends_at_zv_p = false;
20434 row->exact_window_width_line_p = false;
20435 it->continuation_lines_width += x;
20436
20437 /* Make sure that a non-default face is extended
20438 up to the right margin of the window. */
20439 extend_face_to_end_of_line (it);
20440 }
20441 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20442 {
20443 /* A TAB that extends past the right edge of the
20444 window. This produces a single glyph on
20445 window system frames. We leave the glyph in
20446 this row and let it fill the row, but don't
20447 consume the TAB. */
20448 if ((row->reversed_p
20449 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20450 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20451 produce_special_glyphs (it, IT_CONTINUATION);
20452 it->continuation_lines_width += it->last_visible_x;
20453 row->ends_in_middle_of_char_p = true;
20454 row->continued_p = true;
20455 glyph->pixel_width = it->last_visible_x - x;
20456 it->starts_in_middle_of_char_p = true;
20457 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20458 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20459 extend_face_to_end_of_line (it);
20460 }
20461 else
20462 {
20463 /* Something other than a TAB that draws past
20464 the right edge of the window. Restore
20465 positions to values before the element. */
20466 if (row->reversed_p)
20467 unproduce_glyphs (it, row->used[TEXT_AREA]
20468 - (n_glyphs_before + i));
20469 row->used[TEXT_AREA] = n_glyphs_before + i;
20470
20471 /* Display continuation glyphs. */
20472 it->current_x = x_before;
20473 it->continuation_lines_width += x;
20474 if (!FRAME_WINDOW_P (it->f)
20475 || (row->reversed_p
20476 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20477 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20478 produce_special_glyphs (it, IT_CONTINUATION);
20479 row->continued_p = true;
20480
20481 extend_face_to_end_of_line (it);
20482
20483 if (nglyphs > 1 && i > 0)
20484 {
20485 row->ends_in_middle_of_char_p = true;
20486 it->starts_in_middle_of_char_p = true;
20487 }
20488
20489 /* Restore the height to what it was before the
20490 element not fitting on the line. */
20491 it->max_ascent = ascent;
20492 it->max_descent = descent;
20493 it->max_phys_ascent = phys_ascent;
20494 it->max_phys_descent = phys_descent;
20495 }
20496
20497 break;
20498 }
20499 else if (new_x > it->first_visible_x)
20500 {
20501 /* Increment number of glyphs actually displayed. */
20502 ++it->hpos;
20503
20504 /* Record the maximum and minimum buffer positions
20505 seen so far in glyphs that will be displayed by
20506 this row. */
20507 if (it->bidi_p)
20508 RECORD_MAX_MIN_POS (it);
20509
20510 if (x < it->first_visible_x && !row->reversed_p)
20511 /* Glyph is partially visible, i.e. row starts at
20512 negative X position. Don't do that in R2L
20513 rows, where we arrange to add a right offset to
20514 the line in extend_face_to_end_of_line, by a
20515 suitable change to the stretch glyph that is
20516 the leftmost glyph of the line. */
20517 row->x = x - it->first_visible_x;
20518 /* When the last glyph of an R2L row only fits
20519 partially on the line, we need to set row->x to a
20520 negative offset, so that the leftmost glyph is
20521 the one that is partially visible. But if we are
20522 going to produce the truncation glyph, this will
20523 be taken care of in produce_special_glyphs. */
20524 if (row->reversed_p
20525 && new_x > it->last_visible_x
20526 && !(it->line_wrap == TRUNCATE
20527 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20528 {
20529 eassert (FRAME_WINDOW_P (it->f));
20530 row->x = it->last_visible_x - new_x;
20531 }
20532 }
20533 else
20534 {
20535 /* Glyph is completely off the left margin of the
20536 window. This should not happen because of the
20537 move_it_in_display_line at the start of this
20538 function, unless the text display area of the
20539 window is empty. */
20540 eassert (it->first_visible_x <= it->last_visible_x);
20541 }
20542 }
20543 /* Even if this display element produced no glyphs at all,
20544 we want to record its position. */
20545 if (it->bidi_p && nglyphs == 0)
20546 RECORD_MAX_MIN_POS (it);
20547
20548 row->ascent = max (row->ascent, it->max_ascent);
20549 row->height = max (row->height, it->max_ascent + it->max_descent);
20550 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20551 row->phys_height = max (row->phys_height,
20552 it->max_phys_ascent + it->max_phys_descent);
20553 row->extra_line_spacing = max (row->extra_line_spacing,
20554 it->max_extra_line_spacing);
20555
20556 /* End of this display line if row is continued. */
20557 if (row->continued_p || row->ends_at_zv_p)
20558 break;
20559 }
20560
20561 at_end_of_line:
20562 /* Is this a line end? If yes, we're also done, after making
20563 sure that a non-default face is extended up to the right
20564 margin of the window. */
20565 if (ITERATOR_AT_END_OF_LINE_P (it))
20566 {
20567 int used_before = row->used[TEXT_AREA];
20568
20569 row->ends_in_newline_from_string_p = STRINGP (it->object);
20570
20571 /* Add a space at the end of the line that is used to
20572 display the cursor there. */
20573 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20574 append_space_for_newline (it, false);
20575
20576 /* Extend the face to the end of the line. */
20577 extend_face_to_end_of_line (it);
20578
20579 /* Make sure we have the position. */
20580 if (used_before == 0)
20581 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20582
20583 /* Record the position of the newline, for use in
20584 find_row_edges. */
20585 it->eol_pos = it->current.pos;
20586
20587 /* Consume the line end. This skips over invisible lines. */
20588 set_iterator_to_next (it, true);
20589 it->continuation_lines_width = 0;
20590 break;
20591 }
20592
20593 /* Proceed with next display element. Note that this skips
20594 over lines invisible because of selective display. */
20595 set_iterator_to_next (it, true);
20596
20597 /* If we truncate lines, we are done when the last displayed
20598 glyphs reach past the right margin of the window. */
20599 if (it->line_wrap == TRUNCATE
20600 && ((FRAME_WINDOW_P (it->f)
20601 /* Images are preprocessed in produce_image_glyph such
20602 that they are cropped at the right edge of the
20603 window, so an image glyph will always end exactly at
20604 last_visible_x, even if there's no right fringe. */
20605 && ((row->reversed_p
20606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20608 || it->what == IT_IMAGE))
20609 ? (it->current_x >= it->last_visible_x)
20610 : (it->current_x > it->last_visible_x)))
20611 {
20612 /* Maybe add truncation glyphs. */
20613 if (!FRAME_WINDOW_P (it->f)
20614 || (row->reversed_p
20615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20617 {
20618 int i, n;
20619
20620 if (!row->reversed_p)
20621 {
20622 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20623 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20624 break;
20625 }
20626 else
20627 {
20628 for (i = 0; i < row->used[TEXT_AREA]; i++)
20629 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20630 break;
20631 /* Remove any padding glyphs at the front of ROW, to
20632 make room for the truncation glyphs we will be
20633 adding below. The loop below always inserts at
20634 least one truncation glyph, so also remove the
20635 last glyph added to ROW. */
20636 unproduce_glyphs (it, i + 1);
20637 /* Adjust i for the loop below. */
20638 i = row->used[TEXT_AREA] - (i + 1);
20639 }
20640
20641 /* produce_special_glyphs overwrites the last glyph, so
20642 we don't want that if we want to keep that last
20643 glyph, which means it's an image. */
20644 if (it->current_x > it->last_visible_x)
20645 {
20646 it->current_x = x_before;
20647 if (!FRAME_WINDOW_P (it->f))
20648 {
20649 for (n = row->used[TEXT_AREA]; i < n; ++i)
20650 {
20651 row->used[TEXT_AREA] = i;
20652 produce_special_glyphs (it, IT_TRUNCATION);
20653 }
20654 }
20655 else
20656 {
20657 row->used[TEXT_AREA] = i;
20658 produce_special_glyphs (it, IT_TRUNCATION);
20659 }
20660 it->hpos = hpos_before;
20661 }
20662 }
20663 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20664 {
20665 /* Don't truncate if we can overflow newline into fringe. */
20666 if (!get_next_display_element (it))
20667 {
20668 it->continuation_lines_width = 0;
20669 row->ends_at_zv_p = true;
20670 row->exact_window_width_line_p = true;
20671 break;
20672 }
20673 if (ITERATOR_AT_END_OF_LINE_P (it))
20674 {
20675 row->exact_window_width_line_p = true;
20676 goto at_end_of_line;
20677 }
20678 it->current_x = x_before;
20679 it->hpos = hpos_before;
20680 }
20681
20682 row->truncated_on_right_p = true;
20683 it->continuation_lines_width = 0;
20684 reseat_at_next_visible_line_start (it, false);
20685 /* We insist below that IT's position be at ZV because in
20686 bidi-reordered lines the character at visible line start
20687 might not be the character that follows the newline in
20688 the logical order. */
20689 if (IT_BYTEPOS (*it) > BEG_BYTE)
20690 row->ends_at_zv_p =
20691 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20692 else
20693 row->ends_at_zv_p = false;
20694 break;
20695 }
20696 }
20697
20698 if (wrap_data)
20699 bidi_unshelve_cache (wrap_data, true);
20700
20701 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20702 at the left window margin. */
20703 if (it->first_visible_x
20704 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20705 {
20706 if (!FRAME_WINDOW_P (it->f)
20707 || (((row->reversed_p
20708 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20709 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20710 /* Don't let insert_left_trunc_glyphs overwrite the
20711 first glyph of the row if it is an image. */
20712 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20713 insert_left_trunc_glyphs (it);
20714 row->truncated_on_left_p = true;
20715 }
20716
20717 /* Remember the position at which this line ends.
20718
20719 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20720 cannot be before the call to find_row_edges below, since that is
20721 where these positions are determined. */
20722 row->end = it->current;
20723 if (!it->bidi_p)
20724 {
20725 row->minpos = row->start.pos;
20726 row->maxpos = row->end.pos;
20727 }
20728 else
20729 {
20730 /* ROW->minpos and ROW->maxpos must be the smallest and
20731 `1 + the largest' buffer positions in ROW. But if ROW was
20732 bidi-reordered, these two positions can be anywhere in the
20733 row, so we must determine them now. */
20734 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20735 }
20736
20737 /* If the start of this line is the overlay arrow-position, then
20738 mark this glyph row as the one containing the overlay arrow.
20739 This is clearly a mess with variable size fonts. It would be
20740 better to let it be displayed like cursors under X. */
20741 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20742 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20743 !NILP (overlay_arrow_string)))
20744 {
20745 /* Overlay arrow in window redisplay is a fringe bitmap. */
20746 if (STRINGP (overlay_arrow_string))
20747 {
20748 struct glyph_row *arrow_row
20749 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20750 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20751 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20752 struct glyph *p = row->glyphs[TEXT_AREA];
20753 struct glyph *p2, *end;
20754
20755 /* Copy the arrow glyphs. */
20756 while (glyph < arrow_end)
20757 *p++ = *glyph++;
20758
20759 /* Throw away padding glyphs. */
20760 p2 = p;
20761 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20762 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20763 ++p2;
20764 if (p2 > p)
20765 {
20766 while (p2 < end)
20767 *p++ = *p2++;
20768 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20769 }
20770 }
20771 else
20772 {
20773 eassert (INTEGERP (overlay_arrow_string));
20774 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20775 }
20776 overlay_arrow_seen = true;
20777 }
20778
20779 /* Highlight trailing whitespace. */
20780 if (!NILP (Vshow_trailing_whitespace))
20781 highlight_trailing_whitespace (it->f, it->glyph_row);
20782
20783 /* Compute pixel dimensions of this line. */
20784 compute_line_metrics (it);
20785
20786 /* Implementation note: No changes in the glyphs of ROW or in their
20787 faces can be done past this point, because compute_line_metrics
20788 computes ROW's hash value and stores it within the glyph_row
20789 structure. */
20790
20791 /* Record whether this row ends inside an ellipsis. */
20792 row->ends_in_ellipsis_p
20793 = (it->method == GET_FROM_DISPLAY_VECTOR
20794 && it->ellipsis_p);
20795
20796 /* Save fringe bitmaps in this row. */
20797 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20798 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20799 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20800 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20801
20802 it->left_user_fringe_bitmap = 0;
20803 it->left_user_fringe_face_id = 0;
20804 it->right_user_fringe_bitmap = 0;
20805 it->right_user_fringe_face_id = 0;
20806
20807 /* Maybe set the cursor. */
20808 cvpos = it->w->cursor.vpos;
20809 if ((cvpos < 0
20810 /* In bidi-reordered rows, keep checking for proper cursor
20811 position even if one has been found already, because buffer
20812 positions in such rows change non-linearly with ROW->VPOS,
20813 when a line is continued. One exception: when we are at ZV,
20814 display cursor on the first suitable glyph row, since all
20815 the empty rows after that also have their position set to ZV. */
20816 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20817 lines' rows is implemented for bidi-reordered rows. */
20818 || (it->bidi_p
20819 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20820 && PT >= MATRIX_ROW_START_CHARPOS (row)
20821 && PT <= MATRIX_ROW_END_CHARPOS (row)
20822 && cursor_row_p (row))
20823 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20824
20825 /* Prepare for the next line. This line starts horizontally at (X
20826 HPOS) = (0 0). Vertical positions are incremented. As a
20827 convenience for the caller, IT->glyph_row is set to the next
20828 row to be used. */
20829 it->current_x = it->hpos = 0;
20830 it->current_y += row->height;
20831 SET_TEXT_POS (it->eol_pos, 0, 0);
20832 ++it->vpos;
20833 ++it->glyph_row;
20834 /* The next row should by default use the same value of the
20835 reversed_p flag as this one. set_iterator_to_next decides when
20836 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20837 the flag accordingly. */
20838 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20839 it->glyph_row->reversed_p = row->reversed_p;
20840 it->start = row->end;
20841 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20842
20843 #undef RECORD_MAX_MIN_POS
20844 }
20845
20846 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20847 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20848 doc: /* Return paragraph direction at point in BUFFER.
20849 Value is either `left-to-right' or `right-to-left'.
20850 If BUFFER is omitted or nil, it defaults to the current buffer.
20851
20852 Paragraph direction determines how the text in the paragraph is displayed.
20853 In left-to-right paragraphs, text begins at the left margin of the window
20854 and the reading direction is generally left to right. In right-to-left
20855 paragraphs, text begins at the right margin and is read from right to left.
20856
20857 See also `bidi-paragraph-direction'. */)
20858 (Lisp_Object buffer)
20859 {
20860 struct buffer *buf = current_buffer;
20861 struct buffer *old = buf;
20862
20863 if (! NILP (buffer))
20864 {
20865 CHECK_BUFFER (buffer);
20866 buf = XBUFFER (buffer);
20867 }
20868
20869 if (NILP (BVAR (buf, bidi_display_reordering))
20870 || NILP (BVAR (buf, enable_multibyte_characters))
20871 /* When we are loading loadup.el, the character property tables
20872 needed for bidi iteration are not yet available. */
20873 || !NILP (Vpurify_flag))
20874 return Qleft_to_right;
20875 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20876 return BVAR (buf, bidi_paragraph_direction);
20877 else
20878 {
20879 /* Determine the direction from buffer text. We could try to
20880 use current_matrix if it is up to date, but this seems fast
20881 enough as it is. */
20882 struct bidi_it itb;
20883 ptrdiff_t pos = BUF_PT (buf);
20884 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20885 int c;
20886 void *itb_data = bidi_shelve_cache ();
20887
20888 set_buffer_temp (buf);
20889 /* bidi_paragraph_init finds the base direction of the paragraph
20890 by searching forward from paragraph start. We need the base
20891 direction of the current or _previous_ paragraph, so we need
20892 to make sure we are within that paragraph. To that end, find
20893 the previous non-empty line. */
20894 if (pos >= ZV && pos > BEGV)
20895 DEC_BOTH (pos, bytepos);
20896 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20897 if (fast_looking_at (trailing_white_space,
20898 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20899 {
20900 while ((c = FETCH_BYTE (bytepos)) == '\n'
20901 || c == ' ' || c == '\t' || c == '\f')
20902 {
20903 if (bytepos <= BEGV_BYTE)
20904 break;
20905 bytepos--;
20906 pos--;
20907 }
20908 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20909 bytepos--;
20910 }
20911 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20912 itb.paragraph_dir = NEUTRAL_DIR;
20913 itb.string.s = NULL;
20914 itb.string.lstring = Qnil;
20915 itb.string.bufpos = 0;
20916 itb.string.from_disp_str = false;
20917 itb.string.unibyte = false;
20918 /* We have no window to use here for ignoring window-specific
20919 overlays. Using NULL for window pointer will cause
20920 compute_display_string_pos to use the current buffer. */
20921 itb.w = NULL;
20922 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
20923 bidi_unshelve_cache (itb_data, false);
20924 set_buffer_temp (old);
20925 switch (itb.paragraph_dir)
20926 {
20927 case L2R:
20928 return Qleft_to_right;
20929 break;
20930 case R2L:
20931 return Qright_to_left;
20932 break;
20933 default:
20934 emacs_abort ();
20935 }
20936 }
20937 }
20938
20939 DEFUN ("bidi-find-overridden-directionality",
20940 Fbidi_find_overridden_directionality,
20941 Sbidi_find_overridden_directionality, 2, 3, 0,
20942 doc: /* Return position between FROM and TO where directionality was overridden.
20943
20944 This function returns the first character position in the specified
20945 region of OBJECT where there is a character whose `bidi-class' property
20946 is `L', but which was forced to display as `R' by a directional
20947 override, and likewise with characters whose `bidi-class' is `R'
20948 or `AL' that were forced to display as `L'.
20949
20950 If no such character is found, the function returns nil.
20951
20952 OBJECT is a Lisp string or buffer to search for overridden
20953 directionality, and defaults to the current buffer if nil or omitted.
20954 OBJECT can also be a window, in which case the function will search
20955 the buffer displayed in that window. Passing the window instead of
20956 a buffer is preferable when the buffer is displayed in some window,
20957 because this function will then be able to correctly account for
20958 window-specific overlays, which can affect the results.
20959
20960 Strong directional characters `L', `R', and `AL' can have their
20961 intrinsic directionality overridden by directional override
20962 control characters RLO \(u+202e) and LRO \(u+202d). See the
20963 function `get-char-code-property' for a way to inquire about
20964 the `bidi-class' property of a character. */)
20965 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20966 {
20967 struct buffer *buf = current_buffer;
20968 struct buffer *old = buf;
20969 struct window *w = NULL;
20970 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20971 struct bidi_it itb;
20972 ptrdiff_t from_pos, to_pos, from_bpos;
20973 void *itb_data;
20974
20975 if (!NILP (object))
20976 {
20977 if (BUFFERP (object))
20978 buf = XBUFFER (object);
20979 else if (WINDOWP (object))
20980 {
20981 w = decode_live_window (object);
20982 buf = XBUFFER (w->contents);
20983 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20984 }
20985 else
20986 CHECK_STRING (object);
20987 }
20988
20989 if (STRINGP (object))
20990 {
20991 /* Characters in unibyte strings are always treated by bidi.c as
20992 strong LTR. */
20993 if (!STRING_MULTIBYTE (object)
20994 /* When we are loading loadup.el, the character property
20995 tables needed for bidi iteration are not yet
20996 available. */
20997 || !NILP (Vpurify_flag))
20998 return Qnil;
20999
21000 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21001 if (from_pos >= SCHARS (object))
21002 return Qnil;
21003
21004 /* Set up the bidi iterator. */
21005 itb_data = bidi_shelve_cache ();
21006 itb.paragraph_dir = NEUTRAL_DIR;
21007 itb.string.lstring = object;
21008 itb.string.s = NULL;
21009 itb.string.schars = SCHARS (object);
21010 itb.string.bufpos = 0;
21011 itb.string.from_disp_str = false;
21012 itb.string.unibyte = false;
21013 itb.w = w;
21014 bidi_init_it (0, 0, frame_window_p, &itb);
21015 }
21016 else
21017 {
21018 /* Nothing this fancy can happen in unibyte buffers, or in a
21019 buffer that disabled reordering, or if FROM is at EOB. */
21020 if (NILP (BVAR (buf, bidi_display_reordering))
21021 || NILP (BVAR (buf, enable_multibyte_characters))
21022 /* When we are loading loadup.el, the character property
21023 tables needed for bidi iteration are not yet
21024 available. */
21025 || !NILP (Vpurify_flag))
21026 return Qnil;
21027
21028 set_buffer_temp (buf);
21029 validate_region (&from, &to);
21030 from_pos = XINT (from);
21031 to_pos = XINT (to);
21032 if (from_pos >= ZV)
21033 return Qnil;
21034
21035 /* Set up the bidi iterator. */
21036 itb_data = bidi_shelve_cache ();
21037 from_bpos = CHAR_TO_BYTE (from_pos);
21038 if (from_pos == BEGV)
21039 {
21040 itb.charpos = BEGV;
21041 itb.bytepos = BEGV_BYTE;
21042 }
21043 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21044 {
21045 itb.charpos = from_pos;
21046 itb.bytepos = from_bpos;
21047 }
21048 else
21049 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21050 -1, &itb.bytepos);
21051 itb.paragraph_dir = NEUTRAL_DIR;
21052 itb.string.s = NULL;
21053 itb.string.lstring = Qnil;
21054 itb.string.bufpos = 0;
21055 itb.string.from_disp_str = false;
21056 itb.string.unibyte = false;
21057 itb.w = w;
21058 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21059 }
21060
21061 ptrdiff_t found;
21062 do {
21063 /* For the purposes of this function, the actual base direction of
21064 the paragraph doesn't matter, so just set it to L2R. */
21065 bidi_paragraph_init (L2R, &itb, false);
21066 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21067 ;
21068 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21069
21070 bidi_unshelve_cache (itb_data, false);
21071 set_buffer_temp (old);
21072
21073 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21074 }
21075
21076 DEFUN ("move-point-visually", Fmove_point_visually,
21077 Smove_point_visually, 1, 1, 0,
21078 doc: /* Move point in the visual order in the specified DIRECTION.
21079 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21080 left.
21081
21082 Value is the new character position of point. */)
21083 (Lisp_Object direction)
21084 {
21085 struct window *w = XWINDOW (selected_window);
21086 struct buffer *b = XBUFFER (w->contents);
21087 struct glyph_row *row;
21088 int dir;
21089 Lisp_Object paragraph_dir;
21090
21091 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21092 (!(ROW)->continued_p \
21093 && NILP ((GLYPH)->object) \
21094 && (GLYPH)->type == CHAR_GLYPH \
21095 && (GLYPH)->u.ch == ' ' \
21096 && (GLYPH)->charpos >= 0 \
21097 && !(GLYPH)->avoid_cursor_p)
21098
21099 CHECK_NUMBER (direction);
21100 dir = XINT (direction);
21101 if (dir > 0)
21102 dir = 1;
21103 else
21104 dir = -1;
21105
21106 /* If current matrix is up-to-date, we can use the information
21107 recorded in the glyphs, at least as long as the goal is on the
21108 screen. */
21109 if (w->window_end_valid
21110 && !windows_or_buffers_changed
21111 && b
21112 && !b->clip_changed
21113 && !b->prevent_redisplay_optimizations_p
21114 && !window_outdated (w)
21115 /* We rely below on the cursor coordinates to be up to date, but
21116 we cannot trust them if some command moved point since the
21117 last complete redisplay. */
21118 && w->last_point == BUF_PT (b)
21119 && w->cursor.vpos >= 0
21120 && w->cursor.vpos < w->current_matrix->nrows
21121 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21122 {
21123 struct glyph *g = row->glyphs[TEXT_AREA];
21124 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21125 struct glyph *gpt = g + w->cursor.hpos;
21126
21127 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21128 {
21129 if (BUFFERP (g->object) && g->charpos != PT)
21130 {
21131 SET_PT (g->charpos);
21132 w->cursor.vpos = -1;
21133 return make_number (PT);
21134 }
21135 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21136 {
21137 ptrdiff_t new_pos;
21138
21139 if (BUFFERP (gpt->object))
21140 {
21141 new_pos = PT;
21142 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21143 new_pos += (row->reversed_p ? -dir : dir);
21144 else
21145 new_pos -= (row->reversed_p ? -dir : dir);
21146 }
21147 else if (BUFFERP (g->object))
21148 new_pos = g->charpos;
21149 else
21150 break;
21151 SET_PT (new_pos);
21152 w->cursor.vpos = -1;
21153 return make_number (PT);
21154 }
21155 else if (ROW_GLYPH_NEWLINE_P (row, g))
21156 {
21157 /* Glyphs inserted at the end of a non-empty line for
21158 positioning the cursor have zero charpos, so we must
21159 deduce the value of point by other means. */
21160 if (g->charpos > 0)
21161 SET_PT (g->charpos);
21162 else if (row->ends_at_zv_p && PT != ZV)
21163 SET_PT (ZV);
21164 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21165 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21166 else
21167 break;
21168 w->cursor.vpos = -1;
21169 return make_number (PT);
21170 }
21171 }
21172 if (g == e || NILP (g->object))
21173 {
21174 if (row->truncated_on_left_p || row->truncated_on_right_p)
21175 goto simulate_display;
21176 if (!row->reversed_p)
21177 row += dir;
21178 else
21179 row -= dir;
21180 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21181 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21182 goto simulate_display;
21183
21184 if (dir > 0)
21185 {
21186 if (row->reversed_p && !row->continued_p)
21187 {
21188 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21189 w->cursor.vpos = -1;
21190 return make_number (PT);
21191 }
21192 g = row->glyphs[TEXT_AREA];
21193 e = g + row->used[TEXT_AREA];
21194 for ( ; g < e; g++)
21195 {
21196 if (BUFFERP (g->object)
21197 /* Empty lines have only one glyph, which stands
21198 for the newline, and whose charpos is the
21199 buffer position of the newline. */
21200 || ROW_GLYPH_NEWLINE_P (row, g)
21201 /* When the buffer ends in a newline, the line at
21202 EOB also has one glyph, but its charpos is -1. */
21203 || (row->ends_at_zv_p
21204 && !row->reversed_p
21205 && NILP (g->object)
21206 && g->type == CHAR_GLYPH
21207 && g->u.ch == ' '))
21208 {
21209 if (g->charpos > 0)
21210 SET_PT (g->charpos);
21211 else if (!row->reversed_p
21212 && row->ends_at_zv_p
21213 && PT != ZV)
21214 SET_PT (ZV);
21215 else
21216 continue;
21217 w->cursor.vpos = -1;
21218 return make_number (PT);
21219 }
21220 }
21221 }
21222 else
21223 {
21224 if (!row->reversed_p && !row->continued_p)
21225 {
21226 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21227 w->cursor.vpos = -1;
21228 return make_number (PT);
21229 }
21230 e = row->glyphs[TEXT_AREA];
21231 g = e + row->used[TEXT_AREA] - 1;
21232 for ( ; g >= e; g--)
21233 {
21234 if (BUFFERP (g->object)
21235 || (ROW_GLYPH_NEWLINE_P (row, g)
21236 && g->charpos > 0)
21237 /* Empty R2L lines on GUI frames have the buffer
21238 position of the newline stored in the stretch
21239 glyph. */
21240 || g->type == STRETCH_GLYPH
21241 || (row->ends_at_zv_p
21242 && row->reversed_p
21243 && NILP (g->object)
21244 && g->type == CHAR_GLYPH
21245 && g->u.ch == ' '))
21246 {
21247 if (g->charpos > 0)
21248 SET_PT (g->charpos);
21249 else if (row->reversed_p
21250 && row->ends_at_zv_p
21251 && PT != ZV)
21252 SET_PT (ZV);
21253 else
21254 continue;
21255 w->cursor.vpos = -1;
21256 return make_number (PT);
21257 }
21258 }
21259 }
21260 }
21261 }
21262
21263 simulate_display:
21264
21265 /* If we wind up here, we failed to move by using the glyphs, so we
21266 need to simulate display instead. */
21267
21268 if (b)
21269 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21270 else
21271 paragraph_dir = Qleft_to_right;
21272 if (EQ (paragraph_dir, Qright_to_left))
21273 dir = -dir;
21274 if (PT <= BEGV && dir < 0)
21275 xsignal0 (Qbeginning_of_buffer);
21276 else if (PT >= ZV && dir > 0)
21277 xsignal0 (Qend_of_buffer);
21278 else
21279 {
21280 struct text_pos pt;
21281 struct it it;
21282 int pt_x, target_x, pixel_width, pt_vpos;
21283 bool at_eol_p;
21284 bool overshoot_expected = false;
21285 bool target_is_eol_p = false;
21286
21287 /* Setup the arena. */
21288 SET_TEXT_POS (pt, PT, PT_BYTE);
21289 start_display (&it, w, pt);
21290
21291 if (it.cmp_it.id < 0
21292 && it.method == GET_FROM_STRING
21293 && it.area == TEXT_AREA
21294 && it.string_from_display_prop_p
21295 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21296 overshoot_expected = true;
21297
21298 /* Find the X coordinate of point. We start from the beginning
21299 of this or previous line to make sure we are before point in
21300 the logical order (since the move_it_* functions can only
21301 move forward). */
21302 reseat:
21303 reseat_at_previous_visible_line_start (&it);
21304 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21305 if (IT_CHARPOS (it) != PT)
21306 {
21307 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21308 -1, -1, -1, MOVE_TO_POS);
21309 /* If we missed point because the character there is
21310 displayed out of a display vector that has more than one
21311 glyph, retry expecting overshoot. */
21312 if (it.method == GET_FROM_DISPLAY_VECTOR
21313 && it.current.dpvec_index > 0
21314 && !overshoot_expected)
21315 {
21316 overshoot_expected = true;
21317 goto reseat;
21318 }
21319 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21320 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21321 }
21322 pt_x = it.current_x;
21323 pt_vpos = it.vpos;
21324 if (dir > 0 || overshoot_expected)
21325 {
21326 struct glyph_row *row = it.glyph_row;
21327
21328 /* When point is at beginning of line, we don't have
21329 information about the glyph there loaded into struct
21330 it. Calling get_next_display_element fixes that. */
21331 if (pt_x == 0)
21332 get_next_display_element (&it);
21333 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21334 it.glyph_row = NULL;
21335 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21336 it.glyph_row = row;
21337 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21338 it, lest it will become out of sync with it's buffer
21339 position. */
21340 it.current_x = pt_x;
21341 }
21342 else
21343 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21344 pixel_width = it.pixel_width;
21345 if (overshoot_expected && at_eol_p)
21346 pixel_width = 0;
21347 else if (pixel_width <= 0)
21348 pixel_width = 1;
21349
21350 /* If there's a display string (or something similar) at point,
21351 we are actually at the glyph to the left of point, so we need
21352 to correct the X coordinate. */
21353 if (overshoot_expected)
21354 {
21355 if (it.bidi_p)
21356 pt_x += pixel_width * it.bidi_it.scan_dir;
21357 else
21358 pt_x += pixel_width;
21359 }
21360
21361 /* Compute target X coordinate, either to the left or to the
21362 right of point. On TTY frames, all characters have the same
21363 pixel width of 1, so we can use that. On GUI frames we don't
21364 have an easy way of getting at the pixel width of the
21365 character to the left of point, so we use a different method
21366 of getting to that place. */
21367 if (dir > 0)
21368 target_x = pt_x + pixel_width;
21369 else
21370 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21371
21372 /* Target X coordinate could be one line above or below the line
21373 of point, in which case we need to adjust the target X
21374 coordinate. Also, if moving to the left, we need to begin at
21375 the left edge of the point's screen line. */
21376 if (dir < 0)
21377 {
21378 if (pt_x > 0)
21379 {
21380 start_display (&it, w, pt);
21381 reseat_at_previous_visible_line_start (&it);
21382 it.current_x = it.current_y = it.hpos = 0;
21383 if (pt_vpos != 0)
21384 move_it_by_lines (&it, pt_vpos);
21385 }
21386 else
21387 {
21388 move_it_by_lines (&it, -1);
21389 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21390 target_is_eol_p = true;
21391 /* Under word-wrap, we don't know the x coordinate of
21392 the last character displayed on the previous line,
21393 which immediately precedes the wrap point. To find
21394 out its x coordinate, we try moving to the right
21395 margin of the window, which will stop at the wrap
21396 point, and then reset target_x to point at the
21397 character that precedes the wrap point. This is not
21398 needed on GUI frames, because (see below) there we
21399 move from the left margin one grapheme cluster at a
21400 time, and stop when we hit the wrap point. */
21401 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21402 {
21403 void *it_data = NULL;
21404 struct it it2;
21405
21406 SAVE_IT (it2, it, it_data);
21407 move_it_in_display_line_to (&it, ZV, target_x,
21408 MOVE_TO_POS | MOVE_TO_X);
21409 /* If we arrived at target_x, that _is_ the last
21410 character on the previous line. */
21411 if (it.current_x != target_x)
21412 target_x = it.current_x - 1;
21413 RESTORE_IT (&it, &it2, it_data);
21414 }
21415 }
21416 }
21417 else
21418 {
21419 if (at_eol_p
21420 || (target_x >= it.last_visible_x
21421 && it.line_wrap != TRUNCATE))
21422 {
21423 if (pt_x > 0)
21424 move_it_by_lines (&it, 0);
21425 move_it_by_lines (&it, 1);
21426 target_x = 0;
21427 }
21428 }
21429
21430 /* Move to the target X coordinate. */
21431 #ifdef HAVE_WINDOW_SYSTEM
21432 /* On GUI frames, as we don't know the X coordinate of the
21433 character to the left of point, moving point to the left
21434 requires walking, one grapheme cluster at a time, until we
21435 find ourself at a place immediately to the left of the
21436 character at point. */
21437 if (FRAME_WINDOW_P (it.f) && dir < 0)
21438 {
21439 struct text_pos new_pos;
21440 enum move_it_result rc = MOVE_X_REACHED;
21441
21442 if (it.current_x == 0)
21443 get_next_display_element (&it);
21444 if (it.what == IT_COMPOSITION)
21445 {
21446 new_pos.charpos = it.cmp_it.charpos;
21447 new_pos.bytepos = -1;
21448 }
21449 else
21450 new_pos = it.current.pos;
21451
21452 while (it.current_x + it.pixel_width <= target_x
21453 && (rc == MOVE_X_REACHED
21454 /* Under word-wrap, move_it_in_display_line_to
21455 stops at correct coordinates, but sometimes
21456 returns MOVE_POS_MATCH_OR_ZV. */
21457 || (it.line_wrap == WORD_WRAP
21458 && rc == MOVE_POS_MATCH_OR_ZV)))
21459 {
21460 int new_x = it.current_x + it.pixel_width;
21461
21462 /* For composed characters, we want the position of the
21463 first character in the grapheme cluster (usually, the
21464 composition's base character), whereas it.current
21465 might give us the position of the _last_ one, e.g. if
21466 the composition is rendered in reverse due to bidi
21467 reordering. */
21468 if (it.what == IT_COMPOSITION)
21469 {
21470 new_pos.charpos = it.cmp_it.charpos;
21471 new_pos.bytepos = -1;
21472 }
21473 else
21474 new_pos = it.current.pos;
21475 if (new_x == it.current_x)
21476 new_x++;
21477 rc = move_it_in_display_line_to (&it, ZV, new_x,
21478 MOVE_TO_POS | MOVE_TO_X);
21479 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21480 break;
21481 }
21482 /* The previous position we saw in the loop is the one we
21483 want. */
21484 if (new_pos.bytepos == -1)
21485 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21486 it.current.pos = new_pos;
21487 }
21488 else
21489 #endif
21490 if (it.current_x != target_x)
21491 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21492
21493 /* When lines are truncated, the above loop will stop at the
21494 window edge. But we want to get to the end of line, even if
21495 it is beyond the window edge; automatic hscroll will then
21496 scroll the window to show point as appropriate. */
21497 if (target_is_eol_p && it.line_wrap == TRUNCATE
21498 && get_next_display_element (&it))
21499 {
21500 struct text_pos new_pos = it.current.pos;
21501
21502 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21503 {
21504 set_iterator_to_next (&it, false);
21505 if (it.method == GET_FROM_BUFFER)
21506 new_pos = it.current.pos;
21507 if (!get_next_display_element (&it))
21508 break;
21509 }
21510
21511 it.current.pos = new_pos;
21512 }
21513
21514 /* If we ended up in a display string that covers point, move to
21515 buffer position to the right in the visual order. */
21516 if (dir > 0)
21517 {
21518 while (IT_CHARPOS (it) == PT)
21519 {
21520 set_iterator_to_next (&it, false);
21521 if (!get_next_display_element (&it))
21522 break;
21523 }
21524 }
21525
21526 /* Move point to that position. */
21527 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21528 }
21529
21530 return make_number (PT);
21531
21532 #undef ROW_GLYPH_NEWLINE_P
21533 }
21534
21535 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21536 Sbidi_resolved_levels, 0, 1, 0,
21537 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21538
21539 The resolved levels are produced by the Emacs bidi reordering engine
21540 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21541 read the Unicode Standard Annex 9 (UAX#9) for background information
21542 about these levels.
21543
21544 VPOS is the zero-based number of the current window's screen line
21545 for which to produce the resolved levels. If VPOS is nil or omitted,
21546 it defaults to the screen line of point. If the window displays a
21547 header line, VPOS of zero will report on the header line, and first
21548 line of text in the window will have VPOS of 1.
21549
21550 Value is an array of resolved levels, indexed by glyph number.
21551 Glyphs are numbered from zero starting from the beginning of the
21552 screen line, i.e. the left edge of the window for left-to-right lines
21553 and from the right edge for right-to-left lines. The resolved levels
21554 are produced only for the window's text area; text in display margins
21555 is not included.
21556
21557 If the selected window's display is not up-to-date, or if the specified
21558 screen line does not display text, this function returns nil. It is
21559 highly recommended to bind this function to some simple key, like F8,
21560 in order to avoid these problems.
21561
21562 This function exists mainly for testing the correctness of the
21563 Emacs UBA implementation, in particular with the test suite. */)
21564 (Lisp_Object vpos)
21565 {
21566 struct window *w = XWINDOW (selected_window);
21567 struct buffer *b = XBUFFER (w->contents);
21568 int nrow;
21569 struct glyph_row *row;
21570
21571 if (NILP (vpos))
21572 {
21573 int d1, d2, d3, d4, d5;
21574
21575 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21576 }
21577 else
21578 {
21579 CHECK_NUMBER_COERCE_MARKER (vpos);
21580 nrow = XINT (vpos);
21581 }
21582
21583 /* We require up-to-date glyph matrix for this window. */
21584 if (w->window_end_valid
21585 && !windows_or_buffers_changed
21586 && b
21587 && !b->clip_changed
21588 && !b->prevent_redisplay_optimizations_p
21589 && !window_outdated (w)
21590 && nrow >= 0
21591 && nrow < w->current_matrix->nrows
21592 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21593 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21594 {
21595 struct glyph *g, *e, *g1;
21596 int nglyphs, i;
21597 Lisp_Object levels;
21598
21599 if (!row->reversed_p) /* Left-to-right glyph row. */
21600 {
21601 g = g1 = row->glyphs[TEXT_AREA];
21602 e = g + row->used[TEXT_AREA];
21603
21604 /* Skip over glyphs at the start of the row that was
21605 generated by redisplay for its own needs. */
21606 while (g < e
21607 && NILP (g->object)
21608 && g->charpos < 0)
21609 g++;
21610 g1 = g;
21611
21612 /* Count the "interesting" glyphs in this row. */
21613 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21614 nglyphs++;
21615
21616 /* Create and fill the array. */
21617 levels = make_uninit_vector (nglyphs);
21618 for (i = 0; g1 < g; i++, g1++)
21619 ASET (levels, i, make_number (g1->resolved_level));
21620 }
21621 else /* Right-to-left glyph row. */
21622 {
21623 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21624 e = row->glyphs[TEXT_AREA] - 1;
21625 while (g > e
21626 && NILP (g->object)
21627 && g->charpos < 0)
21628 g--;
21629 g1 = g;
21630 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21631 nglyphs++;
21632 levels = make_uninit_vector (nglyphs);
21633 for (i = 0; g1 > g; i++, g1--)
21634 ASET (levels, i, make_number (g1->resolved_level));
21635 }
21636 return levels;
21637 }
21638 else
21639 return Qnil;
21640 }
21641
21642
21643 \f
21644 /***********************************************************************
21645 Menu Bar
21646 ***********************************************************************/
21647
21648 /* Redisplay the menu bar in the frame for window W.
21649
21650 The menu bar of X frames that don't have X toolkit support is
21651 displayed in a special window W->frame->menu_bar_window.
21652
21653 The menu bar of terminal frames is treated specially as far as
21654 glyph matrices are concerned. Menu bar lines are not part of
21655 windows, so the update is done directly on the frame matrix rows
21656 for the menu bar. */
21657
21658 static void
21659 display_menu_bar (struct window *w)
21660 {
21661 struct frame *f = XFRAME (WINDOW_FRAME (w));
21662 struct it it;
21663 Lisp_Object items;
21664 int i;
21665
21666 /* Don't do all this for graphical frames. */
21667 #ifdef HAVE_NTGUI
21668 if (FRAME_W32_P (f))
21669 return;
21670 #endif
21671 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21672 if (FRAME_X_P (f))
21673 return;
21674 #endif
21675
21676 #ifdef HAVE_NS
21677 if (FRAME_NS_P (f))
21678 return;
21679 #endif /* HAVE_NS */
21680
21681 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21682 eassert (!FRAME_WINDOW_P (f));
21683 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21684 it.first_visible_x = 0;
21685 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21686 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21687 if (FRAME_WINDOW_P (f))
21688 {
21689 /* Menu bar lines are displayed in the desired matrix of the
21690 dummy window menu_bar_window. */
21691 struct window *menu_w;
21692 menu_w = XWINDOW (f->menu_bar_window);
21693 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21694 MENU_FACE_ID);
21695 it.first_visible_x = 0;
21696 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21697 }
21698 else
21699 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21700 {
21701 /* This is a TTY frame, i.e. character hpos/vpos are used as
21702 pixel x/y. */
21703 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21704 MENU_FACE_ID);
21705 it.first_visible_x = 0;
21706 it.last_visible_x = FRAME_COLS (f);
21707 }
21708
21709 /* FIXME: This should be controlled by a user option. See the
21710 comments in redisplay_tool_bar and display_mode_line about
21711 this. */
21712 it.paragraph_embedding = L2R;
21713
21714 /* Clear all rows of the menu bar. */
21715 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21716 {
21717 struct glyph_row *row = it.glyph_row + i;
21718 clear_glyph_row (row);
21719 row->enabled_p = true;
21720 row->full_width_p = true;
21721 row->reversed_p = false;
21722 }
21723
21724 /* Display all items of the menu bar. */
21725 items = FRAME_MENU_BAR_ITEMS (it.f);
21726 for (i = 0; i < ASIZE (items); i += 4)
21727 {
21728 Lisp_Object string;
21729
21730 /* Stop at nil string. */
21731 string = AREF (items, i + 1);
21732 if (NILP (string))
21733 break;
21734
21735 /* Remember where item was displayed. */
21736 ASET (items, i + 3, make_number (it.hpos));
21737
21738 /* Display the item, pad with one space. */
21739 if (it.current_x < it.last_visible_x)
21740 display_string (NULL, string, Qnil, 0, 0, &it,
21741 SCHARS (string) + 1, 0, 0, -1);
21742 }
21743
21744 /* Fill out the line with spaces. */
21745 if (it.current_x < it.last_visible_x)
21746 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21747
21748 /* Compute the total height of the lines. */
21749 compute_line_metrics (&it);
21750 }
21751
21752 /* Deep copy of a glyph row, including the glyphs. */
21753 static void
21754 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21755 {
21756 struct glyph *pointers[1 + LAST_AREA];
21757 int to_used = to->used[TEXT_AREA];
21758
21759 /* Save glyph pointers of TO. */
21760 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21761
21762 /* Do a structure assignment. */
21763 *to = *from;
21764
21765 /* Restore original glyph pointers of TO. */
21766 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21767
21768 /* Copy the glyphs. */
21769 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21770 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21771
21772 /* If we filled only part of the TO row, fill the rest with
21773 space_glyph (which will display as empty space). */
21774 if (to_used > from->used[TEXT_AREA])
21775 fill_up_frame_row_with_spaces (to, to_used);
21776 }
21777
21778 /* Display one menu item on a TTY, by overwriting the glyphs in the
21779 frame F's desired glyph matrix with glyphs produced from the menu
21780 item text. Called from term.c to display TTY drop-down menus one
21781 item at a time.
21782
21783 ITEM_TEXT is the menu item text as a C string.
21784
21785 FACE_ID is the face ID to be used for this menu item. FACE_ID
21786 could specify one of 3 faces: a face for an enabled item, a face
21787 for a disabled item, or a face for a selected item.
21788
21789 X and Y are coordinates of the first glyph in the frame's desired
21790 matrix to be overwritten by the menu item. Since this is a TTY, Y
21791 is the zero-based number of the glyph row and X is the zero-based
21792 glyph number in the row, starting from left, where to start
21793 displaying the item.
21794
21795 SUBMENU means this menu item drops down a submenu, which
21796 should be indicated by displaying a proper visual cue after the
21797 item text. */
21798
21799 void
21800 display_tty_menu_item (const char *item_text, int width, int face_id,
21801 int x, int y, bool submenu)
21802 {
21803 struct it it;
21804 struct frame *f = SELECTED_FRAME ();
21805 struct window *w = XWINDOW (f->selected_window);
21806 struct glyph_row *row;
21807 size_t item_len = strlen (item_text);
21808
21809 eassert (FRAME_TERMCAP_P (f));
21810
21811 /* Don't write beyond the matrix's last row. This can happen for
21812 TTY screens that are not high enough to show the entire menu.
21813 (This is actually a bit of defensive programming, as
21814 tty_menu_display already limits the number of menu items to one
21815 less than the number of screen lines.) */
21816 if (y >= f->desired_matrix->nrows)
21817 return;
21818
21819 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21820 it.first_visible_x = 0;
21821 it.last_visible_x = FRAME_COLS (f) - 1;
21822 row = it.glyph_row;
21823 /* Start with the row contents from the current matrix. */
21824 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21825 bool saved_width = row->full_width_p;
21826 row->full_width_p = true;
21827 bool saved_reversed = row->reversed_p;
21828 row->reversed_p = false;
21829 row->enabled_p = true;
21830
21831 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21832 desired face. */
21833 eassert (x < f->desired_matrix->matrix_w);
21834 it.current_x = it.hpos = x;
21835 it.current_y = it.vpos = y;
21836 int saved_used = row->used[TEXT_AREA];
21837 bool saved_truncated = row->truncated_on_right_p;
21838 row->used[TEXT_AREA] = x;
21839 it.face_id = face_id;
21840 it.line_wrap = TRUNCATE;
21841
21842 /* FIXME: This should be controlled by a user option. See the
21843 comments in redisplay_tool_bar and display_mode_line about this.
21844 Also, if paragraph_embedding could ever be R2L, changes will be
21845 needed to avoid shifting to the right the row characters in
21846 term.c:append_glyph. */
21847 it.paragraph_embedding = L2R;
21848
21849 /* Pad with a space on the left. */
21850 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21851 width--;
21852 /* Display the menu item, pad with spaces to WIDTH. */
21853 if (submenu)
21854 {
21855 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21856 item_len, 0, FRAME_COLS (f) - 1, -1);
21857 width -= item_len;
21858 /* Indicate with " >" that there's a submenu. */
21859 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21860 FRAME_COLS (f) - 1, -1);
21861 }
21862 else
21863 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21864 width, 0, FRAME_COLS (f) - 1, -1);
21865
21866 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21867 row->truncated_on_right_p = saved_truncated;
21868 row->hash = row_hash (row);
21869 row->full_width_p = saved_width;
21870 row->reversed_p = saved_reversed;
21871 }
21872 \f
21873 /***********************************************************************
21874 Mode Line
21875 ***********************************************************************/
21876
21877 /* Redisplay mode lines in the window tree whose root is WINDOW.
21878 If FORCE, redisplay mode lines unconditionally.
21879 Otherwise, redisplay only mode lines that are garbaged. Value is
21880 the number of windows whose mode lines were redisplayed. */
21881
21882 static int
21883 redisplay_mode_lines (Lisp_Object window, bool force)
21884 {
21885 int nwindows = 0;
21886
21887 while (!NILP (window))
21888 {
21889 struct window *w = XWINDOW (window);
21890
21891 if (WINDOWP (w->contents))
21892 nwindows += redisplay_mode_lines (w->contents, force);
21893 else if (force
21894 || FRAME_GARBAGED_P (XFRAME (w->frame))
21895 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21896 {
21897 struct text_pos lpoint;
21898 struct buffer *old = current_buffer;
21899
21900 /* Set the window's buffer for the mode line display. */
21901 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21902 set_buffer_internal_1 (XBUFFER (w->contents));
21903
21904 /* Point refers normally to the selected window. For any
21905 other window, set up appropriate value. */
21906 if (!EQ (window, selected_window))
21907 {
21908 struct text_pos pt;
21909
21910 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21911 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21912 }
21913
21914 /* Display mode lines. */
21915 clear_glyph_matrix (w->desired_matrix);
21916 if (display_mode_lines (w))
21917 ++nwindows;
21918
21919 /* Restore old settings. */
21920 set_buffer_internal_1 (old);
21921 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21922 }
21923
21924 window = w->next;
21925 }
21926
21927 return nwindows;
21928 }
21929
21930
21931 /* Display the mode and/or header line of window W. Value is the
21932 sum number of mode lines and header lines displayed. */
21933
21934 static int
21935 display_mode_lines (struct window *w)
21936 {
21937 Lisp_Object old_selected_window = selected_window;
21938 Lisp_Object old_selected_frame = selected_frame;
21939 Lisp_Object new_frame = w->frame;
21940 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21941 int n = 0;
21942
21943 selected_frame = new_frame;
21944 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21945 or window's point, then we'd need select_window_1 here as well. */
21946 XSETWINDOW (selected_window, w);
21947 XFRAME (new_frame)->selected_window = selected_window;
21948
21949 /* These will be set while the mode line specs are processed. */
21950 line_number_displayed = false;
21951 w->column_number_displayed = -1;
21952
21953 if (WINDOW_WANTS_MODELINE_P (w))
21954 {
21955 struct window *sel_w = XWINDOW (old_selected_window);
21956
21957 /* Select mode line face based on the real selected window. */
21958 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21959 BVAR (current_buffer, mode_line_format));
21960 ++n;
21961 }
21962
21963 if (WINDOW_WANTS_HEADER_LINE_P (w))
21964 {
21965 display_mode_line (w, HEADER_LINE_FACE_ID,
21966 BVAR (current_buffer, header_line_format));
21967 ++n;
21968 }
21969
21970 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21971 selected_frame = old_selected_frame;
21972 selected_window = old_selected_window;
21973 if (n > 0)
21974 w->must_be_updated_p = true;
21975 return n;
21976 }
21977
21978
21979 /* Display mode or header line of window W. FACE_ID specifies which
21980 line to display; it is either MODE_LINE_FACE_ID or
21981 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21982 display. Value is the pixel height of the mode/header line
21983 displayed. */
21984
21985 static int
21986 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21987 {
21988 struct it it;
21989 struct face *face;
21990 ptrdiff_t count = SPECPDL_INDEX ();
21991
21992 init_iterator (&it, w, -1, -1, NULL, face_id);
21993 /* Don't extend on a previously drawn mode-line.
21994 This may happen if called from pos_visible_p. */
21995 it.glyph_row->enabled_p = false;
21996 prepare_desired_row (w, it.glyph_row, true);
21997
21998 it.glyph_row->mode_line_p = true;
21999
22000 /* FIXME: This should be controlled by a user option. But
22001 supporting such an option is not trivial, since the mode line is
22002 made up of many separate strings. */
22003 it.paragraph_embedding = L2R;
22004
22005 record_unwind_protect (unwind_format_mode_line,
22006 format_mode_line_unwind_data (NULL, NULL,
22007 Qnil, false));
22008
22009 mode_line_target = MODE_LINE_DISPLAY;
22010
22011 /* Temporarily make frame's keyboard the current kboard so that
22012 kboard-local variables in the mode_line_format will get the right
22013 values. */
22014 push_kboard (FRAME_KBOARD (it.f));
22015 record_unwind_save_match_data ();
22016 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22017 pop_kboard ();
22018
22019 unbind_to (count, Qnil);
22020
22021 /* Fill up with spaces. */
22022 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22023
22024 compute_line_metrics (&it);
22025 it.glyph_row->full_width_p = true;
22026 it.glyph_row->continued_p = false;
22027 it.glyph_row->truncated_on_left_p = false;
22028 it.glyph_row->truncated_on_right_p = false;
22029
22030 /* Make a 3D mode-line have a shadow at its right end. */
22031 face = FACE_FROM_ID (it.f, face_id);
22032 extend_face_to_end_of_line (&it);
22033 if (face->box != FACE_NO_BOX)
22034 {
22035 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22036 + it.glyph_row->used[TEXT_AREA] - 1);
22037 last->right_box_line_p = true;
22038 }
22039
22040 return it.glyph_row->height;
22041 }
22042
22043 /* Move element ELT in LIST to the front of LIST.
22044 Return the updated list. */
22045
22046 static Lisp_Object
22047 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22048 {
22049 register Lisp_Object tail, prev;
22050 register Lisp_Object tem;
22051
22052 tail = list;
22053 prev = Qnil;
22054 while (CONSP (tail))
22055 {
22056 tem = XCAR (tail);
22057
22058 if (EQ (elt, tem))
22059 {
22060 /* Splice out the link TAIL. */
22061 if (NILP (prev))
22062 list = XCDR (tail);
22063 else
22064 Fsetcdr (prev, XCDR (tail));
22065
22066 /* Now make it the first. */
22067 Fsetcdr (tail, list);
22068 return tail;
22069 }
22070 else
22071 prev = tail;
22072 tail = XCDR (tail);
22073 QUIT;
22074 }
22075
22076 /* Not found--return unchanged LIST. */
22077 return list;
22078 }
22079
22080 /* Contribute ELT to the mode line for window IT->w. How it
22081 translates into text depends on its data type.
22082
22083 IT describes the display environment in which we display, as usual.
22084
22085 DEPTH is the depth in recursion. It is used to prevent
22086 infinite recursion here.
22087
22088 FIELD_WIDTH is the number of characters the display of ELT should
22089 occupy in the mode line, and PRECISION is the maximum number of
22090 characters to display from ELT's representation. See
22091 display_string for details.
22092
22093 Returns the hpos of the end of the text generated by ELT.
22094
22095 PROPS is a property list to add to any string we encounter.
22096
22097 If RISKY, remove (disregard) any properties in any string
22098 we encounter, and ignore :eval and :propertize.
22099
22100 The global variable `mode_line_target' determines whether the
22101 output is passed to `store_mode_line_noprop',
22102 `store_mode_line_string', or `display_string'. */
22103
22104 static int
22105 display_mode_element (struct it *it, int depth, int field_width, int precision,
22106 Lisp_Object elt, Lisp_Object props, bool risky)
22107 {
22108 int n = 0, field, prec;
22109 bool literal = false;
22110
22111 tail_recurse:
22112 if (depth > 100)
22113 elt = build_string ("*too-deep*");
22114
22115 depth++;
22116
22117 switch (XTYPE (elt))
22118 {
22119 case Lisp_String:
22120 {
22121 /* A string: output it and check for %-constructs within it. */
22122 unsigned char c;
22123 ptrdiff_t offset = 0;
22124
22125 if (SCHARS (elt) > 0
22126 && (!NILP (props) || risky))
22127 {
22128 Lisp_Object oprops, aelt;
22129 oprops = Ftext_properties_at (make_number (0), elt);
22130
22131 /* If the starting string's properties are not what
22132 we want, translate the string. Also, if the string
22133 is risky, do that anyway. */
22134
22135 if (NILP (Fequal (props, oprops)) || risky)
22136 {
22137 /* If the starting string has properties,
22138 merge the specified ones onto the existing ones. */
22139 if (! NILP (oprops) && !risky)
22140 {
22141 Lisp_Object tem;
22142
22143 oprops = Fcopy_sequence (oprops);
22144 tem = props;
22145 while (CONSP (tem))
22146 {
22147 oprops = Fplist_put (oprops, XCAR (tem),
22148 XCAR (XCDR (tem)));
22149 tem = XCDR (XCDR (tem));
22150 }
22151 props = oprops;
22152 }
22153
22154 aelt = Fassoc (elt, mode_line_proptrans_alist);
22155 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22156 {
22157 /* AELT is what we want. Move it to the front
22158 without consing. */
22159 elt = XCAR (aelt);
22160 mode_line_proptrans_alist
22161 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22162 }
22163 else
22164 {
22165 Lisp_Object tem;
22166
22167 /* If AELT has the wrong props, it is useless.
22168 so get rid of it. */
22169 if (! NILP (aelt))
22170 mode_line_proptrans_alist
22171 = Fdelq (aelt, mode_line_proptrans_alist);
22172
22173 elt = Fcopy_sequence (elt);
22174 Fset_text_properties (make_number (0), Flength (elt),
22175 props, elt);
22176 /* Add this item to mode_line_proptrans_alist. */
22177 mode_line_proptrans_alist
22178 = Fcons (Fcons (elt, props),
22179 mode_line_proptrans_alist);
22180 /* Truncate mode_line_proptrans_alist
22181 to at most 50 elements. */
22182 tem = Fnthcdr (make_number (50),
22183 mode_line_proptrans_alist);
22184 if (! NILP (tem))
22185 XSETCDR (tem, Qnil);
22186 }
22187 }
22188 }
22189
22190 offset = 0;
22191
22192 if (literal)
22193 {
22194 prec = precision - n;
22195 switch (mode_line_target)
22196 {
22197 case MODE_LINE_NOPROP:
22198 case MODE_LINE_TITLE:
22199 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22200 break;
22201 case MODE_LINE_STRING:
22202 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22203 break;
22204 case MODE_LINE_DISPLAY:
22205 n += display_string (NULL, elt, Qnil, 0, 0, it,
22206 0, prec, 0, STRING_MULTIBYTE (elt));
22207 break;
22208 }
22209
22210 break;
22211 }
22212
22213 /* Handle the non-literal case. */
22214
22215 while ((precision <= 0 || n < precision)
22216 && SREF (elt, offset) != 0
22217 && (mode_line_target != MODE_LINE_DISPLAY
22218 || it->current_x < it->last_visible_x))
22219 {
22220 ptrdiff_t last_offset = offset;
22221
22222 /* Advance to end of string or next format specifier. */
22223 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22224 ;
22225
22226 if (offset - 1 != last_offset)
22227 {
22228 ptrdiff_t nchars, nbytes;
22229
22230 /* Output to end of string or up to '%'. Field width
22231 is length of string. Don't output more than
22232 PRECISION allows us. */
22233 offset--;
22234
22235 prec = c_string_width (SDATA (elt) + last_offset,
22236 offset - last_offset, precision - n,
22237 &nchars, &nbytes);
22238
22239 switch (mode_line_target)
22240 {
22241 case MODE_LINE_NOPROP:
22242 case MODE_LINE_TITLE:
22243 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22244 break;
22245 case MODE_LINE_STRING:
22246 {
22247 ptrdiff_t bytepos = last_offset;
22248 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22249 ptrdiff_t endpos = (precision <= 0
22250 ? string_byte_to_char (elt, offset)
22251 : charpos + nchars);
22252 Lisp_Object mode_string
22253 = Fsubstring (elt, make_number (charpos),
22254 make_number (endpos));
22255 n += store_mode_line_string (NULL, mode_string, false,
22256 0, 0, Qnil);
22257 }
22258 break;
22259 case MODE_LINE_DISPLAY:
22260 {
22261 ptrdiff_t bytepos = last_offset;
22262 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22263
22264 if (precision <= 0)
22265 nchars = string_byte_to_char (elt, offset) - charpos;
22266 n += display_string (NULL, elt, Qnil, 0, charpos,
22267 it, 0, nchars, 0,
22268 STRING_MULTIBYTE (elt));
22269 }
22270 break;
22271 }
22272 }
22273 else /* c == '%' */
22274 {
22275 ptrdiff_t percent_position = offset;
22276
22277 /* Get the specified minimum width. Zero means
22278 don't pad. */
22279 field = 0;
22280 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22281 field = field * 10 + c - '0';
22282
22283 /* Don't pad beyond the total padding allowed. */
22284 if (field_width - n > 0 && field > field_width - n)
22285 field = field_width - n;
22286
22287 /* Note that either PRECISION <= 0 or N < PRECISION. */
22288 prec = precision - n;
22289
22290 if (c == 'M')
22291 n += display_mode_element (it, depth, field, prec,
22292 Vglobal_mode_string, props,
22293 risky);
22294 else if (c != 0)
22295 {
22296 bool multibyte;
22297 ptrdiff_t bytepos, charpos;
22298 const char *spec;
22299 Lisp_Object string;
22300
22301 bytepos = percent_position;
22302 charpos = (STRING_MULTIBYTE (elt)
22303 ? string_byte_to_char (elt, bytepos)
22304 : bytepos);
22305 spec = decode_mode_spec (it->w, c, field, &string);
22306 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22307
22308 switch (mode_line_target)
22309 {
22310 case MODE_LINE_NOPROP:
22311 case MODE_LINE_TITLE:
22312 n += store_mode_line_noprop (spec, field, prec);
22313 break;
22314 case MODE_LINE_STRING:
22315 {
22316 Lisp_Object tem = build_string (spec);
22317 props = Ftext_properties_at (make_number (charpos), elt);
22318 /* Should only keep face property in props */
22319 n += store_mode_line_string (NULL, tem, false,
22320 field, prec, props);
22321 }
22322 break;
22323 case MODE_LINE_DISPLAY:
22324 {
22325 int nglyphs_before, nwritten;
22326
22327 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22328 nwritten = display_string (spec, string, elt,
22329 charpos, 0, it,
22330 field, prec, 0,
22331 multibyte);
22332
22333 /* Assign to the glyphs written above the
22334 string where the `%x' came from, position
22335 of the `%'. */
22336 if (nwritten > 0)
22337 {
22338 struct glyph *glyph
22339 = (it->glyph_row->glyphs[TEXT_AREA]
22340 + nglyphs_before);
22341 int i;
22342
22343 for (i = 0; i < nwritten; ++i)
22344 {
22345 glyph[i].object = elt;
22346 glyph[i].charpos = charpos;
22347 }
22348
22349 n += nwritten;
22350 }
22351 }
22352 break;
22353 }
22354 }
22355 else /* c == 0 */
22356 break;
22357 }
22358 }
22359 }
22360 break;
22361
22362 case Lisp_Symbol:
22363 /* A symbol: process the value of the symbol recursively
22364 as if it appeared here directly. Avoid error if symbol void.
22365 Special case: if value of symbol is a string, output the string
22366 literally. */
22367 {
22368 register Lisp_Object tem;
22369
22370 /* If the variable is not marked as risky to set
22371 then its contents are risky to use. */
22372 if (NILP (Fget (elt, Qrisky_local_variable)))
22373 risky = true;
22374
22375 tem = Fboundp (elt);
22376 if (!NILP (tem))
22377 {
22378 tem = Fsymbol_value (elt);
22379 /* If value is a string, output that string literally:
22380 don't check for % within it. */
22381 if (STRINGP (tem))
22382 literal = true;
22383
22384 if (!EQ (tem, elt))
22385 {
22386 /* Give up right away for nil or t. */
22387 elt = tem;
22388 goto tail_recurse;
22389 }
22390 }
22391 }
22392 break;
22393
22394 case Lisp_Cons:
22395 {
22396 register Lisp_Object car, tem;
22397
22398 /* A cons cell: five distinct cases.
22399 If first element is :eval or :propertize, do something special.
22400 If first element is a string or a cons, process all the elements
22401 and effectively concatenate them.
22402 If first element is a negative number, truncate displaying cdr to
22403 at most that many characters. If positive, pad (with spaces)
22404 to at least that many characters.
22405 If first element is a symbol, process the cadr or caddr recursively
22406 according to whether the symbol's value is non-nil or nil. */
22407 car = XCAR (elt);
22408 if (EQ (car, QCeval))
22409 {
22410 /* An element of the form (:eval FORM) means evaluate FORM
22411 and use the result as mode line elements. */
22412
22413 if (risky)
22414 break;
22415
22416 if (CONSP (XCDR (elt)))
22417 {
22418 Lisp_Object spec;
22419 spec = safe__eval (true, XCAR (XCDR (elt)));
22420 n += display_mode_element (it, depth, field_width - n,
22421 precision - n, spec, props,
22422 risky);
22423 }
22424 }
22425 else if (EQ (car, QCpropertize))
22426 {
22427 /* An element of the form (:propertize ELT PROPS...)
22428 means display ELT but applying properties PROPS. */
22429
22430 if (risky)
22431 break;
22432
22433 if (CONSP (XCDR (elt)))
22434 n += display_mode_element (it, depth, field_width - n,
22435 precision - n, XCAR (XCDR (elt)),
22436 XCDR (XCDR (elt)), risky);
22437 }
22438 else if (SYMBOLP (car))
22439 {
22440 tem = Fboundp (car);
22441 elt = XCDR (elt);
22442 if (!CONSP (elt))
22443 goto invalid;
22444 /* elt is now the cdr, and we know it is a cons cell.
22445 Use its car if CAR has a non-nil value. */
22446 if (!NILP (tem))
22447 {
22448 tem = Fsymbol_value (car);
22449 if (!NILP (tem))
22450 {
22451 elt = XCAR (elt);
22452 goto tail_recurse;
22453 }
22454 }
22455 /* Symbol's value is nil (or symbol is unbound)
22456 Get the cddr of the original list
22457 and if possible find the caddr and use that. */
22458 elt = XCDR (elt);
22459 if (NILP (elt))
22460 break;
22461 else if (!CONSP (elt))
22462 goto invalid;
22463 elt = XCAR (elt);
22464 goto tail_recurse;
22465 }
22466 else if (INTEGERP (car))
22467 {
22468 register int lim = XINT (car);
22469 elt = XCDR (elt);
22470 if (lim < 0)
22471 {
22472 /* Negative int means reduce maximum width. */
22473 if (precision <= 0)
22474 precision = -lim;
22475 else
22476 precision = min (precision, -lim);
22477 }
22478 else if (lim > 0)
22479 {
22480 /* Padding specified. Don't let it be more than
22481 current maximum. */
22482 if (precision > 0)
22483 lim = min (precision, lim);
22484
22485 /* If that's more padding than already wanted, queue it.
22486 But don't reduce padding already specified even if
22487 that is beyond the current truncation point. */
22488 field_width = max (lim, field_width);
22489 }
22490 goto tail_recurse;
22491 }
22492 else if (STRINGP (car) || CONSP (car))
22493 {
22494 Lisp_Object halftail = elt;
22495 int len = 0;
22496
22497 while (CONSP (elt)
22498 && (precision <= 0 || n < precision))
22499 {
22500 n += display_mode_element (it, depth,
22501 /* Do padding only after the last
22502 element in the list. */
22503 (! CONSP (XCDR (elt))
22504 ? field_width - n
22505 : 0),
22506 precision - n, XCAR (elt),
22507 props, risky);
22508 elt = XCDR (elt);
22509 len++;
22510 if ((len & 1) == 0)
22511 halftail = XCDR (halftail);
22512 /* Check for cycle. */
22513 if (EQ (halftail, elt))
22514 break;
22515 }
22516 }
22517 }
22518 break;
22519
22520 default:
22521 invalid:
22522 elt = build_string ("*invalid*");
22523 goto tail_recurse;
22524 }
22525
22526 /* Pad to FIELD_WIDTH. */
22527 if (field_width > 0 && n < field_width)
22528 {
22529 switch (mode_line_target)
22530 {
22531 case MODE_LINE_NOPROP:
22532 case MODE_LINE_TITLE:
22533 n += store_mode_line_noprop ("", field_width - n, 0);
22534 break;
22535 case MODE_LINE_STRING:
22536 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22537 Qnil);
22538 break;
22539 case MODE_LINE_DISPLAY:
22540 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22541 0, 0, 0);
22542 break;
22543 }
22544 }
22545
22546 return n;
22547 }
22548
22549 /* Store a mode-line string element in mode_line_string_list.
22550
22551 If STRING is non-null, display that C string. Otherwise, the Lisp
22552 string LISP_STRING is displayed.
22553
22554 FIELD_WIDTH is the minimum number of output glyphs to produce.
22555 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22556 with spaces. FIELD_WIDTH <= 0 means don't pad.
22557
22558 PRECISION is the maximum number of characters to output from
22559 STRING. PRECISION <= 0 means don't truncate the string.
22560
22561 If COPY_STRING, make a copy of LISP_STRING before adding
22562 properties to the string.
22563
22564 PROPS are the properties to add to the string.
22565 The mode_line_string_face face property is always added to the string.
22566 */
22567
22568 static int
22569 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22570 bool copy_string,
22571 int field_width, int precision, Lisp_Object props)
22572 {
22573 ptrdiff_t len;
22574 int n = 0;
22575
22576 if (string != NULL)
22577 {
22578 len = strlen (string);
22579 if (precision > 0 && len > precision)
22580 len = precision;
22581 lisp_string = make_string (string, len);
22582 if (NILP (props))
22583 props = mode_line_string_face_prop;
22584 else if (!NILP (mode_line_string_face))
22585 {
22586 Lisp_Object face = Fplist_get (props, Qface);
22587 props = Fcopy_sequence (props);
22588 if (NILP (face))
22589 face = mode_line_string_face;
22590 else
22591 face = list2 (face, mode_line_string_face);
22592 props = Fplist_put (props, Qface, face);
22593 }
22594 Fadd_text_properties (make_number (0), make_number (len),
22595 props, lisp_string);
22596 }
22597 else
22598 {
22599 len = XFASTINT (Flength (lisp_string));
22600 if (precision > 0 && len > precision)
22601 {
22602 len = precision;
22603 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22604 precision = -1;
22605 }
22606 if (!NILP (mode_line_string_face))
22607 {
22608 Lisp_Object face;
22609 if (NILP (props))
22610 props = Ftext_properties_at (make_number (0), lisp_string);
22611 face = Fplist_get (props, Qface);
22612 if (NILP (face))
22613 face = mode_line_string_face;
22614 else
22615 face = list2 (face, mode_line_string_face);
22616 props = list2 (Qface, face);
22617 if (copy_string)
22618 lisp_string = Fcopy_sequence (lisp_string);
22619 }
22620 if (!NILP (props))
22621 Fadd_text_properties (make_number (0), make_number (len),
22622 props, lisp_string);
22623 }
22624
22625 if (len > 0)
22626 {
22627 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22628 n += len;
22629 }
22630
22631 if (field_width > len)
22632 {
22633 field_width -= len;
22634 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22635 if (!NILP (props))
22636 Fadd_text_properties (make_number (0), make_number (field_width),
22637 props, lisp_string);
22638 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22639 n += field_width;
22640 }
22641
22642 return n;
22643 }
22644
22645
22646 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22647 1, 4, 0,
22648 doc: /* Format a string out of a mode line format specification.
22649 First arg FORMAT specifies the mode line format (see `mode-line-format'
22650 for details) to use.
22651
22652 By default, the format is evaluated for the currently selected window.
22653
22654 Optional second arg FACE specifies the face property to put on all
22655 characters for which no face is specified. The value nil means the
22656 default face. The value t means whatever face the window's mode line
22657 currently uses (either `mode-line' or `mode-line-inactive',
22658 depending on whether the window is the selected window or not).
22659 An integer value means the value string has no text
22660 properties.
22661
22662 Optional third and fourth args WINDOW and BUFFER specify the window
22663 and buffer to use as the context for the formatting (defaults
22664 are the selected window and the WINDOW's buffer). */)
22665 (Lisp_Object format, Lisp_Object face,
22666 Lisp_Object window, Lisp_Object buffer)
22667 {
22668 struct it it;
22669 int len;
22670 struct window *w;
22671 struct buffer *old_buffer = NULL;
22672 int face_id;
22673 bool no_props = INTEGERP (face);
22674 ptrdiff_t count = SPECPDL_INDEX ();
22675 Lisp_Object str;
22676 int string_start = 0;
22677
22678 w = decode_any_window (window);
22679 XSETWINDOW (window, w);
22680
22681 if (NILP (buffer))
22682 buffer = w->contents;
22683 CHECK_BUFFER (buffer);
22684
22685 /* Make formatting the modeline a non-op when noninteractive, otherwise
22686 there will be problems later caused by a partially initialized frame. */
22687 if (NILP (format) || noninteractive)
22688 return empty_unibyte_string;
22689
22690 if (no_props)
22691 face = Qnil;
22692
22693 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22694 : EQ (face, Qt) ? (EQ (window, selected_window)
22695 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22696 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22697 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22698 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22699 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22700 : DEFAULT_FACE_ID;
22701
22702 old_buffer = current_buffer;
22703
22704 /* Save things including mode_line_proptrans_alist,
22705 and set that to nil so that we don't alter the outer value. */
22706 record_unwind_protect (unwind_format_mode_line,
22707 format_mode_line_unwind_data
22708 (XFRAME (WINDOW_FRAME (w)),
22709 old_buffer, selected_window, true));
22710 mode_line_proptrans_alist = Qnil;
22711
22712 Fselect_window (window, Qt);
22713 set_buffer_internal_1 (XBUFFER (buffer));
22714
22715 init_iterator (&it, w, -1, -1, NULL, face_id);
22716
22717 if (no_props)
22718 {
22719 mode_line_target = MODE_LINE_NOPROP;
22720 mode_line_string_face_prop = Qnil;
22721 mode_line_string_list = Qnil;
22722 string_start = MODE_LINE_NOPROP_LEN (0);
22723 }
22724 else
22725 {
22726 mode_line_target = MODE_LINE_STRING;
22727 mode_line_string_list = Qnil;
22728 mode_line_string_face = face;
22729 mode_line_string_face_prop
22730 = NILP (face) ? Qnil : list2 (Qface, face);
22731 }
22732
22733 push_kboard (FRAME_KBOARD (it.f));
22734 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22735 pop_kboard ();
22736
22737 if (no_props)
22738 {
22739 len = MODE_LINE_NOPROP_LEN (string_start);
22740 str = make_string (mode_line_noprop_buf + string_start, len);
22741 }
22742 else
22743 {
22744 mode_line_string_list = Fnreverse (mode_line_string_list);
22745 str = Fmapconcat (Qidentity, mode_line_string_list,
22746 empty_unibyte_string);
22747 }
22748
22749 unbind_to (count, Qnil);
22750 return str;
22751 }
22752
22753 /* Write a null-terminated, right justified decimal representation of
22754 the positive integer D to BUF using a minimal field width WIDTH. */
22755
22756 static void
22757 pint2str (register char *buf, register int width, register ptrdiff_t d)
22758 {
22759 register char *p = buf;
22760
22761 if (d <= 0)
22762 *p++ = '0';
22763 else
22764 {
22765 while (d > 0)
22766 {
22767 *p++ = d % 10 + '0';
22768 d /= 10;
22769 }
22770 }
22771
22772 for (width -= (int) (p - buf); width > 0; --width)
22773 *p++ = ' ';
22774 *p-- = '\0';
22775 while (p > buf)
22776 {
22777 d = *buf;
22778 *buf++ = *p;
22779 *p-- = d;
22780 }
22781 }
22782
22783 /* Write a null-terminated, right justified decimal and "human
22784 readable" representation of the nonnegative integer D to BUF using
22785 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22786
22787 static const char power_letter[] =
22788 {
22789 0, /* no letter */
22790 'k', /* kilo */
22791 'M', /* mega */
22792 'G', /* giga */
22793 'T', /* tera */
22794 'P', /* peta */
22795 'E', /* exa */
22796 'Z', /* zetta */
22797 'Y' /* yotta */
22798 };
22799
22800 static void
22801 pint2hrstr (char *buf, int width, ptrdiff_t d)
22802 {
22803 /* We aim to represent the nonnegative integer D as
22804 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22805 ptrdiff_t quotient = d;
22806 int remainder = 0;
22807 /* -1 means: do not use TENTHS. */
22808 int tenths = -1;
22809 int exponent = 0;
22810
22811 /* Length of QUOTIENT.TENTHS as a string. */
22812 int length;
22813
22814 char * psuffix;
22815 char * p;
22816
22817 if (quotient >= 1000)
22818 {
22819 /* Scale to the appropriate EXPONENT. */
22820 do
22821 {
22822 remainder = quotient % 1000;
22823 quotient /= 1000;
22824 exponent++;
22825 }
22826 while (quotient >= 1000);
22827
22828 /* Round to nearest and decide whether to use TENTHS or not. */
22829 if (quotient <= 9)
22830 {
22831 tenths = remainder / 100;
22832 if (remainder % 100 >= 50)
22833 {
22834 if (tenths < 9)
22835 tenths++;
22836 else
22837 {
22838 quotient++;
22839 if (quotient == 10)
22840 tenths = -1;
22841 else
22842 tenths = 0;
22843 }
22844 }
22845 }
22846 else
22847 if (remainder >= 500)
22848 {
22849 if (quotient < 999)
22850 quotient++;
22851 else
22852 {
22853 quotient = 1;
22854 exponent++;
22855 tenths = 0;
22856 }
22857 }
22858 }
22859
22860 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22861 if (tenths == -1 && quotient <= 99)
22862 if (quotient <= 9)
22863 length = 1;
22864 else
22865 length = 2;
22866 else
22867 length = 3;
22868 p = psuffix = buf + max (width, length);
22869
22870 /* Print EXPONENT. */
22871 *psuffix++ = power_letter[exponent];
22872 *psuffix = '\0';
22873
22874 /* Print TENTHS. */
22875 if (tenths >= 0)
22876 {
22877 *--p = '0' + tenths;
22878 *--p = '.';
22879 }
22880
22881 /* Print QUOTIENT. */
22882 do
22883 {
22884 int digit = quotient % 10;
22885 *--p = '0' + digit;
22886 }
22887 while ((quotient /= 10) != 0);
22888
22889 /* Print leading spaces. */
22890 while (buf < p)
22891 *--p = ' ';
22892 }
22893
22894 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22895 If EOL_FLAG, set also a mnemonic character for end-of-line
22896 type of CODING_SYSTEM. Return updated pointer into BUF. */
22897
22898 static unsigned char invalid_eol_type[] = "(*invalid*)";
22899
22900 static char *
22901 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
22902 {
22903 Lisp_Object val;
22904 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22905 const unsigned char *eol_str;
22906 int eol_str_len;
22907 /* The EOL conversion we are using. */
22908 Lisp_Object eoltype;
22909
22910 val = CODING_SYSTEM_SPEC (coding_system);
22911 eoltype = Qnil;
22912
22913 if (!VECTORP (val)) /* Not yet decided. */
22914 {
22915 *buf++ = multibyte ? '-' : ' ';
22916 if (eol_flag)
22917 eoltype = eol_mnemonic_undecided;
22918 /* Don't mention EOL conversion if it isn't decided. */
22919 }
22920 else
22921 {
22922 Lisp_Object attrs;
22923 Lisp_Object eolvalue;
22924
22925 attrs = AREF (val, 0);
22926 eolvalue = AREF (val, 2);
22927
22928 *buf++ = multibyte
22929 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22930 : ' ';
22931
22932 if (eol_flag)
22933 {
22934 /* The EOL conversion that is normal on this system. */
22935
22936 if (NILP (eolvalue)) /* Not yet decided. */
22937 eoltype = eol_mnemonic_undecided;
22938 else if (VECTORP (eolvalue)) /* Not yet decided. */
22939 eoltype = eol_mnemonic_undecided;
22940 else /* eolvalue is Qunix, Qdos, or Qmac. */
22941 eoltype = (EQ (eolvalue, Qunix)
22942 ? eol_mnemonic_unix
22943 : EQ (eolvalue, Qdos)
22944 ? eol_mnemonic_dos : eol_mnemonic_mac);
22945 }
22946 }
22947
22948 if (eol_flag)
22949 {
22950 /* Mention the EOL conversion if it is not the usual one. */
22951 if (STRINGP (eoltype))
22952 {
22953 eol_str = SDATA (eoltype);
22954 eol_str_len = SBYTES (eoltype);
22955 }
22956 else if (CHARACTERP (eoltype))
22957 {
22958 int c = XFASTINT (eoltype);
22959 return buf + CHAR_STRING (c, (unsigned char *) buf);
22960 }
22961 else
22962 {
22963 eol_str = invalid_eol_type;
22964 eol_str_len = sizeof (invalid_eol_type) - 1;
22965 }
22966 memcpy (buf, eol_str, eol_str_len);
22967 buf += eol_str_len;
22968 }
22969
22970 return buf;
22971 }
22972
22973 /* Return a string for the output of a mode line %-spec for window W,
22974 generated by character C. FIELD_WIDTH > 0 means pad the string
22975 returned with spaces to that value. Return a Lisp string in
22976 *STRING if the resulting string is taken from that Lisp string.
22977
22978 Note we operate on the current buffer for most purposes. */
22979
22980 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22981
22982 static const char *
22983 decode_mode_spec (struct window *w, register int c, int field_width,
22984 Lisp_Object *string)
22985 {
22986 Lisp_Object obj;
22987 struct frame *f = XFRAME (WINDOW_FRAME (w));
22988 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22989 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22990 produce strings from numerical values, so limit preposterously
22991 large values of FIELD_WIDTH to avoid overrunning the buffer's
22992 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22993 bytes plus the terminating null. */
22994 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22995 struct buffer *b = current_buffer;
22996
22997 obj = Qnil;
22998 *string = Qnil;
22999
23000 switch (c)
23001 {
23002 case '*':
23003 if (!NILP (BVAR (b, read_only)))
23004 return "%";
23005 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23006 return "*";
23007 return "-";
23008
23009 case '+':
23010 /* This differs from %* only for a modified read-only buffer. */
23011 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23012 return "*";
23013 if (!NILP (BVAR (b, read_only)))
23014 return "%";
23015 return "-";
23016
23017 case '&':
23018 /* This differs from %* in ignoring read-only-ness. */
23019 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23020 return "*";
23021 return "-";
23022
23023 case '%':
23024 return "%";
23025
23026 case '[':
23027 {
23028 int i;
23029 char *p;
23030
23031 if (command_loop_level > 5)
23032 return "[[[... ";
23033 p = decode_mode_spec_buf;
23034 for (i = 0; i < command_loop_level; i++)
23035 *p++ = '[';
23036 *p = 0;
23037 return decode_mode_spec_buf;
23038 }
23039
23040 case ']':
23041 {
23042 int i;
23043 char *p;
23044
23045 if (command_loop_level > 5)
23046 return " ...]]]";
23047 p = decode_mode_spec_buf;
23048 for (i = 0; i < command_loop_level; i++)
23049 *p++ = ']';
23050 *p = 0;
23051 return decode_mode_spec_buf;
23052 }
23053
23054 case '-':
23055 {
23056 register int i;
23057
23058 /* Let lots_of_dashes be a string of infinite length. */
23059 if (mode_line_target == MODE_LINE_NOPROP
23060 || mode_line_target == MODE_LINE_STRING)
23061 return "--";
23062 if (field_width <= 0
23063 || field_width > sizeof (lots_of_dashes))
23064 {
23065 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23066 decode_mode_spec_buf[i] = '-';
23067 decode_mode_spec_buf[i] = '\0';
23068 return decode_mode_spec_buf;
23069 }
23070 else
23071 return lots_of_dashes;
23072 }
23073
23074 case 'b':
23075 obj = BVAR (b, name);
23076 break;
23077
23078 case 'c':
23079 /* %c and %l are ignored in `frame-title-format'.
23080 (In redisplay_internal, the frame title is drawn _before_ the
23081 windows are updated, so the stuff which depends on actual
23082 window contents (such as %l) may fail to render properly, or
23083 even crash emacs.) */
23084 if (mode_line_target == MODE_LINE_TITLE)
23085 return "";
23086 else
23087 {
23088 ptrdiff_t col = current_column ();
23089 w->column_number_displayed = col;
23090 pint2str (decode_mode_spec_buf, width, col);
23091 return decode_mode_spec_buf;
23092 }
23093
23094 case 'e':
23095 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23096 {
23097 if (NILP (Vmemory_full))
23098 return "";
23099 else
23100 return "!MEM FULL! ";
23101 }
23102 #else
23103 return "";
23104 #endif
23105
23106 case 'F':
23107 /* %F displays the frame name. */
23108 if (!NILP (f->title))
23109 return SSDATA (f->title);
23110 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23111 return SSDATA (f->name);
23112 return "Emacs";
23113
23114 case 'f':
23115 obj = BVAR (b, filename);
23116 break;
23117
23118 case 'i':
23119 {
23120 ptrdiff_t size = ZV - BEGV;
23121 pint2str (decode_mode_spec_buf, width, size);
23122 return decode_mode_spec_buf;
23123 }
23124
23125 case 'I':
23126 {
23127 ptrdiff_t size = ZV - BEGV;
23128 pint2hrstr (decode_mode_spec_buf, width, size);
23129 return decode_mode_spec_buf;
23130 }
23131
23132 case 'l':
23133 {
23134 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23135 ptrdiff_t topline, nlines, height;
23136 ptrdiff_t junk;
23137
23138 /* %c and %l are ignored in `frame-title-format'. */
23139 if (mode_line_target == MODE_LINE_TITLE)
23140 return "";
23141
23142 startpos = marker_position (w->start);
23143 startpos_byte = marker_byte_position (w->start);
23144 height = WINDOW_TOTAL_LINES (w);
23145
23146 /* If we decided that this buffer isn't suitable for line numbers,
23147 don't forget that too fast. */
23148 if (w->base_line_pos == -1)
23149 goto no_value;
23150
23151 /* If the buffer is very big, don't waste time. */
23152 if (INTEGERP (Vline_number_display_limit)
23153 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23154 {
23155 w->base_line_pos = 0;
23156 w->base_line_number = 0;
23157 goto no_value;
23158 }
23159
23160 if (w->base_line_number > 0
23161 && w->base_line_pos > 0
23162 && w->base_line_pos <= startpos)
23163 {
23164 line = w->base_line_number;
23165 linepos = w->base_line_pos;
23166 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23167 }
23168 else
23169 {
23170 line = 1;
23171 linepos = BUF_BEGV (b);
23172 linepos_byte = BUF_BEGV_BYTE (b);
23173 }
23174
23175 /* Count lines from base line to window start position. */
23176 nlines = display_count_lines (linepos_byte,
23177 startpos_byte,
23178 startpos, &junk);
23179
23180 topline = nlines + line;
23181
23182 /* Determine a new base line, if the old one is too close
23183 or too far away, or if we did not have one.
23184 "Too close" means it's plausible a scroll-down would
23185 go back past it. */
23186 if (startpos == BUF_BEGV (b))
23187 {
23188 w->base_line_number = topline;
23189 w->base_line_pos = BUF_BEGV (b);
23190 }
23191 else if (nlines < height + 25 || nlines > height * 3 + 50
23192 || linepos == BUF_BEGV (b))
23193 {
23194 ptrdiff_t limit = BUF_BEGV (b);
23195 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23196 ptrdiff_t position;
23197 ptrdiff_t distance =
23198 (height * 2 + 30) * line_number_display_limit_width;
23199
23200 if (startpos - distance > limit)
23201 {
23202 limit = startpos - distance;
23203 limit_byte = CHAR_TO_BYTE (limit);
23204 }
23205
23206 nlines = display_count_lines (startpos_byte,
23207 limit_byte,
23208 - (height * 2 + 30),
23209 &position);
23210 /* If we couldn't find the lines we wanted within
23211 line_number_display_limit_width chars per line,
23212 give up on line numbers for this window. */
23213 if (position == limit_byte && limit == startpos - distance)
23214 {
23215 w->base_line_pos = -1;
23216 w->base_line_number = 0;
23217 goto no_value;
23218 }
23219
23220 w->base_line_number = topline - nlines;
23221 w->base_line_pos = BYTE_TO_CHAR (position);
23222 }
23223
23224 /* Now count lines from the start pos to point. */
23225 nlines = display_count_lines (startpos_byte,
23226 PT_BYTE, PT, &junk);
23227
23228 /* Record that we did display the line number. */
23229 line_number_displayed = true;
23230
23231 /* Make the string to show. */
23232 pint2str (decode_mode_spec_buf, width, topline + nlines);
23233 return decode_mode_spec_buf;
23234 no_value:
23235 {
23236 char *p = decode_mode_spec_buf;
23237 int pad = width - 2;
23238 while (pad-- > 0)
23239 *p++ = ' ';
23240 *p++ = '?';
23241 *p++ = '?';
23242 *p = '\0';
23243 return decode_mode_spec_buf;
23244 }
23245 }
23246 break;
23247
23248 case 'm':
23249 obj = BVAR (b, mode_name);
23250 break;
23251
23252 case 'n':
23253 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23254 return " Narrow";
23255 break;
23256
23257 case 'p':
23258 {
23259 ptrdiff_t pos = marker_position (w->start);
23260 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23261
23262 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23263 {
23264 if (pos <= BUF_BEGV (b))
23265 return "All";
23266 else
23267 return "Bottom";
23268 }
23269 else if (pos <= BUF_BEGV (b))
23270 return "Top";
23271 else
23272 {
23273 if (total > 1000000)
23274 /* Do it differently for a large value, to avoid overflow. */
23275 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23276 else
23277 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23278 /* We can't normally display a 3-digit number,
23279 so get us a 2-digit number that is close. */
23280 if (total == 100)
23281 total = 99;
23282 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23283 return decode_mode_spec_buf;
23284 }
23285 }
23286
23287 /* Display percentage of size above the bottom of the screen. */
23288 case 'P':
23289 {
23290 ptrdiff_t toppos = marker_position (w->start);
23291 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23292 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23293
23294 if (botpos >= BUF_ZV (b))
23295 {
23296 if (toppos <= BUF_BEGV (b))
23297 return "All";
23298 else
23299 return "Bottom";
23300 }
23301 else
23302 {
23303 if (total > 1000000)
23304 /* Do it differently for a large value, to avoid overflow. */
23305 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23306 else
23307 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23308 /* We can't normally display a 3-digit number,
23309 so get us a 2-digit number that is close. */
23310 if (total == 100)
23311 total = 99;
23312 if (toppos <= BUF_BEGV (b))
23313 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23314 else
23315 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23316 return decode_mode_spec_buf;
23317 }
23318 }
23319
23320 case 's':
23321 /* status of process */
23322 obj = Fget_buffer_process (Fcurrent_buffer ());
23323 if (NILP (obj))
23324 return "no process";
23325 #ifndef MSDOS
23326 obj = Fsymbol_name (Fprocess_status (obj));
23327 #endif
23328 break;
23329
23330 case '@':
23331 {
23332 ptrdiff_t count = inhibit_garbage_collection ();
23333 Lisp_Object curdir = BVAR (current_buffer, directory);
23334 Lisp_Object val = Qnil;
23335
23336 if (STRINGP (curdir))
23337 val = call1 (intern ("file-remote-p"), curdir);
23338
23339 unbind_to (count, Qnil);
23340
23341 if (NILP (val))
23342 return "-";
23343 else
23344 return "@";
23345 }
23346
23347 case 'z':
23348 /* coding-system (not including end-of-line format) */
23349 case 'Z':
23350 /* coding-system (including end-of-line type) */
23351 {
23352 bool eol_flag = (c == 'Z');
23353 char *p = decode_mode_spec_buf;
23354
23355 if (! FRAME_WINDOW_P (f))
23356 {
23357 /* No need to mention EOL here--the terminal never needs
23358 to do EOL conversion. */
23359 p = decode_mode_spec_coding (CODING_ID_NAME
23360 (FRAME_KEYBOARD_CODING (f)->id),
23361 p, false);
23362 p = decode_mode_spec_coding (CODING_ID_NAME
23363 (FRAME_TERMINAL_CODING (f)->id),
23364 p, false);
23365 }
23366 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23367 p, eol_flag);
23368
23369 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23370 #ifdef subprocesses
23371 obj = Fget_buffer_process (Fcurrent_buffer ());
23372 if (PROCESSP (obj))
23373 {
23374 p = decode_mode_spec_coding
23375 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23376 p = decode_mode_spec_coding
23377 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23378 }
23379 #endif /* subprocesses */
23380 #endif /* false */
23381 *p = 0;
23382 return decode_mode_spec_buf;
23383 }
23384 }
23385
23386 if (STRINGP (obj))
23387 {
23388 *string = obj;
23389 return SSDATA (obj);
23390 }
23391 else
23392 return "";
23393 }
23394
23395
23396 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23397 means count lines back from START_BYTE. But don't go beyond
23398 LIMIT_BYTE. Return the number of lines thus found (always
23399 nonnegative).
23400
23401 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23402 either the position COUNT lines after/before START_BYTE, if we
23403 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23404 COUNT lines. */
23405
23406 static ptrdiff_t
23407 display_count_lines (ptrdiff_t start_byte,
23408 ptrdiff_t limit_byte, ptrdiff_t count,
23409 ptrdiff_t *byte_pos_ptr)
23410 {
23411 register unsigned char *cursor;
23412 unsigned char *base;
23413
23414 register ptrdiff_t ceiling;
23415 register unsigned char *ceiling_addr;
23416 ptrdiff_t orig_count = count;
23417
23418 /* If we are not in selective display mode,
23419 check only for newlines. */
23420 bool selective_display
23421 = (!NILP (BVAR (current_buffer, selective_display))
23422 && !INTEGERP (BVAR (current_buffer, selective_display)));
23423
23424 if (count > 0)
23425 {
23426 while (start_byte < limit_byte)
23427 {
23428 ceiling = BUFFER_CEILING_OF (start_byte);
23429 ceiling = min (limit_byte - 1, ceiling);
23430 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23431 base = (cursor = BYTE_POS_ADDR (start_byte));
23432
23433 do
23434 {
23435 if (selective_display)
23436 {
23437 while (*cursor != '\n' && *cursor != 015
23438 && ++cursor != ceiling_addr)
23439 continue;
23440 if (cursor == ceiling_addr)
23441 break;
23442 }
23443 else
23444 {
23445 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23446 if (! cursor)
23447 break;
23448 }
23449
23450 cursor++;
23451
23452 if (--count == 0)
23453 {
23454 start_byte += cursor - base;
23455 *byte_pos_ptr = start_byte;
23456 return orig_count;
23457 }
23458 }
23459 while (cursor < ceiling_addr);
23460
23461 start_byte += ceiling_addr - base;
23462 }
23463 }
23464 else
23465 {
23466 while (start_byte > limit_byte)
23467 {
23468 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23469 ceiling = max (limit_byte, ceiling);
23470 ceiling_addr = BYTE_POS_ADDR (ceiling);
23471 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23472 while (true)
23473 {
23474 if (selective_display)
23475 {
23476 while (--cursor >= ceiling_addr
23477 && *cursor != '\n' && *cursor != 015)
23478 continue;
23479 if (cursor < ceiling_addr)
23480 break;
23481 }
23482 else
23483 {
23484 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23485 if (! cursor)
23486 break;
23487 }
23488
23489 if (++count == 0)
23490 {
23491 start_byte += cursor - base + 1;
23492 *byte_pos_ptr = start_byte;
23493 /* When scanning backwards, we should
23494 not count the newline posterior to which we stop. */
23495 return - orig_count - 1;
23496 }
23497 }
23498 start_byte += ceiling_addr - base;
23499 }
23500 }
23501
23502 *byte_pos_ptr = limit_byte;
23503
23504 if (count < 0)
23505 return - orig_count + count;
23506 return orig_count - count;
23507
23508 }
23509
23510
23511 \f
23512 /***********************************************************************
23513 Displaying strings
23514 ***********************************************************************/
23515
23516 /* Display a NUL-terminated string, starting with index START.
23517
23518 If STRING is non-null, display that C string. Otherwise, the Lisp
23519 string LISP_STRING is displayed. There's a case that STRING is
23520 non-null and LISP_STRING is not nil. It means STRING is a string
23521 data of LISP_STRING. In that case, we display LISP_STRING while
23522 ignoring its text properties.
23523
23524 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23525 FACE_STRING. Display STRING or LISP_STRING with the face at
23526 FACE_STRING_POS in FACE_STRING:
23527
23528 Display the string in the environment given by IT, but use the
23529 standard display table, temporarily.
23530
23531 FIELD_WIDTH is the minimum number of output glyphs to produce.
23532 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23533 with spaces. If STRING has more characters, more than FIELD_WIDTH
23534 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23535
23536 PRECISION is the maximum number of characters to output from
23537 STRING. PRECISION < 0 means don't truncate the string.
23538
23539 This is roughly equivalent to printf format specifiers:
23540
23541 FIELD_WIDTH PRECISION PRINTF
23542 ----------------------------------------
23543 -1 -1 %s
23544 -1 10 %.10s
23545 10 -1 %10s
23546 20 10 %20.10s
23547
23548 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23549 display them, and < 0 means obey the current buffer's value of
23550 enable_multibyte_characters.
23551
23552 Value is the number of columns displayed. */
23553
23554 static int
23555 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23556 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23557 int field_width, int precision, int max_x, int multibyte)
23558 {
23559 int hpos_at_start = it->hpos;
23560 int saved_face_id = it->face_id;
23561 struct glyph_row *row = it->glyph_row;
23562 ptrdiff_t it_charpos;
23563
23564 /* Initialize the iterator IT for iteration over STRING beginning
23565 with index START. */
23566 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23567 precision, field_width, multibyte);
23568 if (string && STRINGP (lisp_string))
23569 /* LISP_STRING is the one returned by decode_mode_spec. We should
23570 ignore its text properties. */
23571 it->stop_charpos = it->end_charpos;
23572
23573 /* If displaying STRING, set up the face of the iterator from
23574 FACE_STRING, if that's given. */
23575 if (STRINGP (face_string))
23576 {
23577 ptrdiff_t endptr;
23578 struct face *face;
23579
23580 it->face_id
23581 = face_at_string_position (it->w, face_string, face_string_pos,
23582 0, &endptr, it->base_face_id, false);
23583 face = FACE_FROM_ID (it->f, it->face_id);
23584 it->face_box_p = face->box != FACE_NO_BOX;
23585 }
23586
23587 /* Set max_x to the maximum allowed X position. Don't let it go
23588 beyond the right edge of the window. */
23589 if (max_x <= 0)
23590 max_x = it->last_visible_x;
23591 else
23592 max_x = min (max_x, it->last_visible_x);
23593
23594 /* Skip over display elements that are not visible. because IT->w is
23595 hscrolled. */
23596 if (it->current_x < it->first_visible_x)
23597 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23598 MOVE_TO_POS | MOVE_TO_X);
23599
23600 row->ascent = it->max_ascent;
23601 row->height = it->max_ascent + it->max_descent;
23602 row->phys_ascent = it->max_phys_ascent;
23603 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23604 row->extra_line_spacing = it->max_extra_line_spacing;
23605
23606 if (STRINGP (it->string))
23607 it_charpos = IT_STRING_CHARPOS (*it);
23608 else
23609 it_charpos = IT_CHARPOS (*it);
23610
23611 /* This condition is for the case that we are called with current_x
23612 past last_visible_x. */
23613 while (it->current_x < max_x)
23614 {
23615 int x_before, x, n_glyphs_before, i, nglyphs;
23616
23617 /* Get the next display element. */
23618 if (!get_next_display_element (it))
23619 break;
23620
23621 /* Produce glyphs. */
23622 x_before = it->current_x;
23623 n_glyphs_before = row->used[TEXT_AREA];
23624 PRODUCE_GLYPHS (it);
23625
23626 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23627 i = 0;
23628 x = x_before;
23629 while (i < nglyphs)
23630 {
23631 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23632
23633 if (it->line_wrap != TRUNCATE
23634 && x + glyph->pixel_width > max_x)
23635 {
23636 /* End of continued line or max_x reached. */
23637 if (CHAR_GLYPH_PADDING_P (*glyph))
23638 {
23639 /* A wide character is unbreakable. */
23640 if (row->reversed_p)
23641 unproduce_glyphs (it, row->used[TEXT_AREA]
23642 - n_glyphs_before);
23643 row->used[TEXT_AREA] = n_glyphs_before;
23644 it->current_x = x_before;
23645 }
23646 else
23647 {
23648 if (row->reversed_p)
23649 unproduce_glyphs (it, row->used[TEXT_AREA]
23650 - (n_glyphs_before + i));
23651 row->used[TEXT_AREA] = n_glyphs_before + i;
23652 it->current_x = x;
23653 }
23654 break;
23655 }
23656 else if (x + glyph->pixel_width >= it->first_visible_x)
23657 {
23658 /* Glyph is at least partially visible. */
23659 ++it->hpos;
23660 if (x < it->first_visible_x)
23661 row->x = x - it->first_visible_x;
23662 }
23663 else
23664 {
23665 /* Glyph is off the left margin of the display area.
23666 Should not happen. */
23667 emacs_abort ();
23668 }
23669
23670 row->ascent = max (row->ascent, it->max_ascent);
23671 row->height = max (row->height, it->max_ascent + it->max_descent);
23672 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23673 row->phys_height = max (row->phys_height,
23674 it->max_phys_ascent + it->max_phys_descent);
23675 row->extra_line_spacing = max (row->extra_line_spacing,
23676 it->max_extra_line_spacing);
23677 x += glyph->pixel_width;
23678 ++i;
23679 }
23680
23681 /* Stop if max_x reached. */
23682 if (i < nglyphs)
23683 break;
23684
23685 /* Stop at line ends. */
23686 if (ITERATOR_AT_END_OF_LINE_P (it))
23687 {
23688 it->continuation_lines_width = 0;
23689 break;
23690 }
23691
23692 set_iterator_to_next (it, true);
23693 if (STRINGP (it->string))
23694 it_charpos = IT_STRING_CHARPOS (*it);
23695 else
23696 it_charpos = IT_CHARPOS (*it);
23697
23698 /* Stop if truncating at the right edge. */
23699 if (it->line_wrap == TRUNCATE
23700 && it->current_x >= it->last_visible_x)
23701 {
23702 /* Add truncation mark, but don't do it if the line is
23703 truncated at a padding space. */
23704 if (it_charpos < it->string_nchars)
23705 {
23706 if (!FRAME_WINDOW_P (it->f))
23707 {
23708 int ii, n;
23709
23710 if (it->current_x > it->last_visible_x)
23711 {
23712 if (!row->reversed_p)
23713 {
23714 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23715 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23716 break;
23717 }
23718 else
23719 {
23720 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23721 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23722 break;
23723 unproduce_glyphs (it, ii + 1);
23724 ii = row->used[TEXT_AREA] - (ii + 1);
23725 }
23726 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23727 {
23728 row->used[TEXT_AREA] = ii;
23729 produce_special_glyphs (it, IT_TRUNCATION);
23730 }
23731 }
23732 produce_special_glyphs (it, IT_TRUNCATION);
23733 }
23734 row->truncated_on_right_p = true;
23735 }
23736 break;
23737 }
23738 }
23739
23740 /* Maybe insert a truncation at the left. */
23741 if (it->first_visible_x
23742 && it_charpos > 0)
23743 {
23744 if (!FRAME_WINDOW_P (it->f)
23745 || (row->reversed_p
23746 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23747 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23748 insert_left_trunc_glyphs (it);
23749 row->truncated_on_left_p = true;
23750 }
23751
23752 it->face_id = saved_face_id;
23753
23754 /* Value is number of columns displayed. */
23755 return it->hpos - hpos_at_start;
23756 }
23757
23758
23759 \f
23760 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23761 appears as an element of LIST or as the car of an element of LIST.
23762 If PROPVAL is a list, compare each element against LIST in that
23763 way, and return 1/2 if any element of PROPVAL is found in LIST.
23764 Otherwise return 0. This function cannot quit.
23765 The return value is 2 if the text is invisible but with an ellipsis
23766 and 1 if it's invisible and without an ellipsis. */
23767
23768 int
23769 invisible_prop (Lisp_Object propval, Lisp_Object list)
23770 {
23771 Lisp_Object tail, proptail;
23772
23773 for (tail = list; CONSP (tail); tail = XCDR (tail))
23774 {
23775 register Lisp_Object tem;
23776 tem = XCAR (tail);
23777 if (EQ (propval, tem))
23778 return 1;
23779 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23780 return NILP (XCDR (tem)) ? 1 : 2;
23781 }
23782
23783 if (CONSP (propval))
23784 {
23785 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23786 {
23787 Lisp_Object propelt;
23788 propelt = XCAR (proptail);
23789 for (tail = list; CONSP (tail); tail = XCDR (tail))
23790 {
23791 register Lisp_Object tem;
23792 tem = XCAR (tail);
23793 if (EQ (propelt, tem))
23794 return 1;
23795 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23796 return NILP (XCDR (tem)) ? 1 : 2;
23797 }
23798 }
23799 }
23800
23801 return 0;
23802 }
23803
23804 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23805 doc: /* Non-nil if the property makes the text invisible.
23806 POS-OR-PROP can be a marker or number, in which case it is taken to be
23807 a position in the current buffer and the value of the `invisible' property
23808 is checked; or it can be some other value, which is then presumed to be the
23809 value of the `invisible' property of the text of interest.
23810 The non-nil value returned can be t for truly invisible text or something
23811 else if the text is replaced by an ellipsis. */)
23812 (Lisp_Object pos_or_prop)
23813 {
23814 Lisp_Object prop
23815 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23816 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23817 : pos_or_prop);
23818 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23819 return (invis == 0 ? Qnil
23820 : invis == 1 ? Qt
23821 : make_number (invis));
23822 }
23823
23824 /* Calculate a width or height in pixels from a specification using
23825 the following elements:
23826
23827 SPEC ::=
23828 NUM - a (fractional) multiple of the default font width/height
23829 (NUM) - specifies exactly NUM pixels
23830 UNIT - a fixed number of pixels, see below.
23831 ELEMENT - size of a display element in pixels, see below.
23832 (NUM . SPEC) - equals NUM * SPEC
23833 (+ SPEC SPEC ...) - add pixel values
23834 (- SPEC SPEC ...) - subtract pixel values
23835 (- SPEC) - negate pixel value
23836
23837 NUM ::=
23838 INT or FLOAT - a number constant
23839 SYMBOL - use symbol's (buffer local) variable binding.
23840
23841 UNIT ::=
23842 in - pixels per inch *)
23843 mm - pixels per 1/1000 meter *)
23844 cm - pixels per 1/100 meter *)
23845 width - width of current font in pixels.
23846 height - height of current font in pixels.
23847
23848 *) using the ratio(s) defined in display-pixels-per-inch.
23849
23850 ELEMENT ::=
23851
23852 left-fringe - left fringe width in pixels
23853 right-fringe - right fringe width in pixels
23854
23855 left-margin - left margin width in pixels
23856 right-margin - right margin width in pixels
23857
23858 scroll-bar - scroll-bar area width in pixels
23859
23860 Examples:
23861
23862 Pixels corresponding to 5 inches:
23863 (5 . in)
23864
23865 Total width of non-text areas on left side of window (if scroll-bar is on left):
23866 '(space :width (+ left-fringe left-margin scroll-bar))
23867
23868 Align to first text column (in header line):
23869 '(space :align-to 0)
23870
23871 Align to middle of text area minus half the width of variable `my-image'
23872 containing a loaded image:
23873 '(space :align-to (0.5 . (- text my-image)))
23874
23875 Width of left margin minus width of 1 character in the default font:
23876 '(space :width (- left-margin 1))
23877
23878 Width of left margin minus width of 2 characters in the current font:
23879 '(space :width (- left-margin (2 . width)))
23880
23881 Center 1 character over left-margin (in header line):
23882 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23883
23884 Different ways to express width of left fringe plus left margin minus one pixel:
23885 '(space :width (- (+ left-fringe left-margin) (1)))
23886 '(space :width (+ left-fringe left-margin (- (1))))
23887 '(space :width (+ left-fringe left-margin (-1)))
23888
23889 */
23890
23891 static bool
23892 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23893 struct font *font, bool width_p, int *align_to)
23894 {
23895 double pixels;
23896
23897 # define OK_PIXELS(val) (*res = (val), true)
23898 # define OK_ALIGN_TO(val) (*align_to = (val), true)
23899
23900 if (NILP (prop))
23901 return OK_PIXELS (0);
23902
23903 eassert (FRAME_LIVE_P (it->f));
23904
23905 if (SYMBOLP (prop))
23906 {
23907 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23908 {
23909 char *unit = SSDATA (SYMBOL_NAME (prop));
23910
23911 if (unit[0] == 'i' && unit[1] == 'n')
23912 pixels = 1.0;
23913 else if (unit[0] == 'm' && unit[1] == 'm')
23914 pixels = 25.4;
23915 else if (unit[0] == 'c' && unit[1] == 'm')
23916 pixels = 2.54;
23917 else
23918 pixels = 0;
23919 if (pixels > 0)
23920 {
23921 double ppi = (width_p ? FRAME_RES_X (it->f)
23922 : FRAME_RES_Y (it->f));
23923
23924 if (ppi > 0)
23925 return OK_PIXELS (ppi / pixels);
23926 return false;
23927 }
23928 }
23929
23930 #ifdef HAVE_WINDOW_SYSTEM
23931 if (EQ (prop, Qheight))
23932 return OK_PIXELS (font
23933 ? normal_char_height (font)
23934 : FRAME_LINE_HEIGHT (it->f));
23935 if (EQ (prop, Qwidth))
23936 return OK_PIXELS (font
23937 ? FONT_WIDTH (font)
23938 : FRAME_COLUMN_WIDTH (it->f));
23939 #else
23940 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23941 return OK_PIXELS (1);
23942 #endif
23943
23944 if (EQ (prop, Qtext))
23945 return OK_PIXELS (width_p
23946 ? window_box_width (it->w, TEXT_AREA)
23947 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23948
23949 if (align_to && *align_to < 0)
23950 {
23951 *res = 0;
23952 if (EQ (prop, Qleft))
23953 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23954 if (EQ (prop, Qright))
23955 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23956 if (EQ (prop, Qcenter))
23957 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23958 + window_box_width (it->w, TEXT_AREA) / 2);
23959 if (EQ (prop, Qleft_fringe))
23960 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23961 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23962 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23963 if (EQ (prop, Qright_fringe))
23964 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23965 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23966 : window_box_right_offset (it->w, TEXT_AREA));
23967 if (EQ (prop, Qleft_margin))
23968 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23969 if (EQ (prop, Qright_margin))
23970 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23971 if (EQ (prop, Qscroll_bar))
23972 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23973 ? 0
23974 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23975 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23976 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23977 : 0)));
23978 }
23979 else
23980 {
23981 if (EQ (prop, Qleft_fringe))
23982 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23983 if (EQ (prop, Qright_fringe))
23984 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23985 if (EQ (prop, Qleft_margin))
23986 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23987 if (EQ (prop, Qright_margin))
23988 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23989 if (EQ (prop, Qscroll_bar))
23990 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23991 }
23992
23993 prop = buffer_local_value (prop, it->w->contents);
23994 if (EQ (prop, Qunbound))
23995 prop = Qnil;
23996 }
23997
23998 if (INTEGERP (prop) || FLOATP (prop))
23999 {
24000 int base_unit = (width_p
24001 ? FRAME_COLUMN_WIDTH (it->f)
24002 : FRAME_LINE_HEIGHT (it->f));
24003 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24004 }
24005
24006 if (CONSP (prop))
24007 {
24008 Lisp_Object car = XCAR (prop);
24009 Lisp_Object cdr = XCDR (prop);
24010
24011 if (SYMBOLP (car))
24012 {
24013 #ifdef HAVE_WINDOW_SYSTEM
24014 if (FRAME_WINDOW_P (it->f)
24015 && valid_image_p (prop))
24016 {
24017 ptrdiff_t id = lookup_image (it->f, prop);
24018 struct image *img = IMAGE_FROM_ID (it->f, id);
24019
24020 return OK_PIXELS (width_p ? img->width : img->height);
24021 }
24022 #endif
24023 if (EQ (car, Qplus) || EQ (car, Qminus))
24024 {
24025 bool first = true;
24026 double px;
24027
24028 pixels = 0;
24029 while (CONSP (cdr))
24030 {
24031 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24032 font, width_p, align_to))
24033 return false;
24034 if (first)
24035 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24036 else
24037 pixels += px;
24038 cdr = XCDR (cdr);
24039 }
24040 if (EQ (car, Qminus))
24041 pixels = -pixels;
24042 return OK_PIXELS (pixels);
24043 }
24044
24045 car = buffer_local_value (car, it->w->contents);
24046 if (EQ (car, Qunbound))
24047 car = Qnil;
24048 }
24049
24050 if (INTEGERP (car) || FLOATP (car))
24051 {
24052 double fact;
24053 pixels = XFLOATINT (car);
24054 if (NILP (cdr))
24055 return OK_PIXELS (pixels);
24056 if (calc_pixel_width_or_height (&fact, it, cdr,
24057 font, width_p, align_to))
24058 return OK_PIXELS (pixels * fact);
24059 return false;
24060 }
24061
24062 return false;
24063 }
24064
24065 return false;
24066 }
24067
24068 \f
24069 /***********************************************************************
24070 Glyph Display
24071 ***********************************************************************/
24072
24073 #ifdef HAVE_WINDOW_SYSTEM
24074
24075 #ifdef GLYPH_DEBUG
24076
24077 void
24078 dump_glyph_string (struct glyph_string *s)
24079 {
24080 fprintf (stderr, "glyph string\n");
24081 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24082 s->x, s->y, s->width, s->height);
24083 fprintf (stderr, " ybase = %d\n", s->ybase);
24084 fprintf (stderr, " hl = %d\n", s->hl);
24085 fprintf (stderr, " left overhang = %d, right = %d\n",
24086 s->left_overhang, s->right_overhang);
24087 fprintf (stderr, " nchars = %d\n", s->nchars);
24088 fprintf (stderr, " extends to end of line = %d\n",
24089 s->extends_to_end_of_line_p);
24090 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24091 fprintf (stderr, " bg width = %d\n", s->background_width);
24092 }
24093
24094 #endif /* GLYPH_DEBUG */
24095
24096 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24097 of XChar2b structures for S; it can't be allocated in
24098 init_glyph_string because it must be allocated via `alloca'. W
24099 is the window on which S is drawn. ROW and AREA are the glyph row
24100 and area within the row from which S is constructed. START is the
24101 index of the first glyph structure covered by S. HL is a
24102 face-override for drawing S. */
24103
24104 #ifdef HAVE_NTGUI
24105 #define OPTIONAL_HDC(hdc) HDC hdc,
24106 #define DECLARE_HDC(hdc) HDC hdc;
24107 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24108 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24109 #endif
24110
24111 #ifndef OPTIONAL_HDC
24112 #define OPTIONAL_HDC(hdc)
24113 #define DECLARE_HDC(hdc)
24114 #define ALLOCATE_HDC(hdc, f)
24115 #define RELEASE_HDC(hdc, f)
24116 #endif
24117
24118 static void
24119 init_glyph_string (struct glyph_string *s,
24120 OPTIONAL_HDC (hdc)
24121 XChar2b *char2b, struct window *w, struct glyph_row *row,
24122 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24123 {
24124 memset (s, 0, sizeof *s);
24125 s->w = w;
24126 s->f = XFRAME (w->frame);
24127 #ifdef HAVE_NTGUI
24128 s->hdc = hdc;
24129 #endif
24130 s->display = FRAME_X_DISPLAY (s->f);
24131 s->window = FRAME_X_WINDOW (s->f);
24132 s->char2b = char2b;
24133 s->hl = hl;
24134 s->row = row;
24135 s->area = area;
24136 s->first_glyph = row->glyphs[area] + start;
24137 s->height = row->height;
24138 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24139 s->ybase = s->y + row->ascent;
24140 }
24141
24142
24143 /* Append the list of glyph strings with head H and tail T to the list
24144 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24145
24146 static void
24147 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24148 struct glyph_string *h, struct glyph_string *t)
24149 {
24150 if (h)
24151 {
24152 if (*head)
24153 (*tail)->next = h;
24154 else
24155 *head = h;
24156 h->prev = *tail;
24157 *tail = t;
24158 }
24159 }
24160
24161
24162 /* Prepend the list of glyph strings with head H and tail T to the
24163 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24164 result. */
24165
24166 static void
24167 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24168 struct glyph_string *h, struct glyph_string *t)
24169 {
24170 if (h)
24171 {
24172 if (*head)
24173 (*head)->prev = t;
24174 else
24175 *tail = t;
24176 t->next = *head;
24177 *head = h;
24178 }
24179 }
24180
24181
24182 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24183 Set *HEAD and *TAIL to the resulting list. */
24184
24185 static void
24186 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24187 struct glyph_string *s)
24188 {
24189 s->next = s->prev = NULL;
24190 append_glyph_string_lists (head, tail, s, s);
24191 }
24192
24193
24194 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24195 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24196 make sure that X resources for the face returned are allocated.
24197 Value is a pointer to a realized face that is ready for display if
24198 DISPLAY_P. */
24199
24200 static struct face *
24201 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24202 XChar2b *char2b, bool display_p)
24203 {
24204 struct face *face = FACE_FROM_ID (f, face_id);
24205 unsigned code = 0;
24206
24207 if (face->font)
24208 {
24209 code = face->font->driver->encode_char (face->font, c);
24210
24211 if (code == FONT_INVALID_CODE)
24212 code = 0;
24213 }
24214 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24215
24216 /* Make sure X resources of the face are allocated. */
24217 #ifdef HAVE_X_WINDOWS
24218 if (display_p)
24219 #endif
24220 {
24221 eassert (face != NULL);
24222 prepare_face_for_display (f, face);
24223 }
24224
24225 return face;
24226 }
24227
24228
24229 /* Get face and two-byte form of character glyph GLYPH on frame F.
24230 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24231 a pointer to a realized face that is ready for display. */
24232
24233 static struct face *
24234 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24235 XChar2b *char2b)
24236 {
24237 struct face *face;
24238 unsigned code = 0;
24239
24240 eassert (glyph->type == CHAR_GLYPH);
24241 face = FACE_FROM_ID (f, glyph->face_id);
24242
24243 /* Make sure X resources of the face are allocated. */
24244 eassert (face != NULL);
24245 prepare_face_for_display (f, face);
24246
24247 if (face->font)
24248 {
24249 if (CHAR_BYTE8_P (glyph->u.ch))
24250 code = CHAR_TO_BYTE8 (glyph->u.ch);
24251 else
24252 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24253
24254 if (code == FONT_INVALID_CODE)
24255 code = 0;
24256 }
24257
24258 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24259 return face;
24260 }
24261
24262
24263 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24264 Return true iff FONT has a glyph for C. */
24265
24266 static bool
24267 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24268 {
24269 unsigned code;
24270
24271 if (CHAR_BYTE8_P (c))
24272 code = CHAR_TO_BYTE8 (c);
24273 else
24274 code = font->driver->encode_char (font, c);
24275
24276 if (code == FONT_INVALID_CODE)
24277 return false;
24278 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24279 return true;
24280 }
24281
24282
24283 /* Fill glyph string S with composition components specified by S->cmp.
24284
24285 BASE_FACE is the base face of the composition.
24286 S->cmp_from is the index of the first component for S.
24287
24288 OVERLAPS non-zero means S should draw the foreground only, and use
24289 its physical height for clipping. See also draw_glyphs.
24290
24291 Value is the index of a component not in S. */
24292
24293 static int
24294 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24295 int overlaps)
24296 {
24297 int i;
24298 /* For all glyphs of this composition, starting at the offset
24299 S->cmp_from, until we reach the end of the definition or encounter a
24300 glyph that requires the different face, add it to S. */
24301 struct face *face;
24302
24303 eassert (s);
24304
24305 s->for_overlaps = overlaps;
24306 s->face = NULL;
24307 s->font = NULL;
24308 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24309 {
24310 int c = COMPOSITION_GLYPH (s->cmp, i);
24311
24312 /* TAB in a composition means display glyphs with padding space
24313 on the left or right. */
24314 if (c != '\t')
24315 {
24316 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24317 -1, Qnil);
24318
24319 face = get_char_face_and_encoding (s->f, c, face_id,
24320 s->char2b + i, true);
24321 if (face)
24322 {
24323 if (! s->face)
24324 {
24325 s->face = face;
24326 s->font = s->face->font;
24327 }
24328 else if (s->face != face)
24329 break;
24330 }
24331 }
24332 ++s->nchars;
24333 }
24334 s->cmp_to = i;
24335
24336 if (s->face == NULL)
24337 {
24338 s->face = base_face->ascii_face;
24339 s->font = s->face->font;
24340 }
24341
24342 /* All glyph strings for the same composition has the same width,
24343 i.e. the width set for the first component of the composition. */
24344 s->width = s->first_glyph->pixel_width;
24345
24346 /* If the specified font could not be loaded, use the frame's
24347 default font, but record the fact that we couldn't load it in
24348 the glyph string so that we can draw rectangles for the
24349 characters of the glyph string. */
24350 if (s->font == NULL)
24351 {
24352 s->font_not_found_p = true;
24353 s->font = FRAME_FONT (s->f);
24354 }
24355
24356 /* Adjust base line for subscript/superscript text. */
24357 s->ybase += s->first_glyph->voffset;
24358
24359 return s->cmp_to;
24360 }
24361
24362 static int
24363 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24364 int start, int end, int overlaps)
24365 {
24366 struct glyph *glyph, *last;
24367 Lisp_Object lgstring;
24368 int i;
24369
24370 s->for_overlaps = overlaps;
24371 glyph = s->row->glyphs[s->area] + start;
24372 last = s->row->glyphs[s->area] + end;
24373 s->cmp_id = glyph->u.cmp.id;
24374 s->cmp_from = glyph->slice.cmp.from;
24375 s->cmp_to = glyph->slice.cmp.to + 1;
24376 s->face = FACE_FROM_ID (s->f, face_id);
24377 lgstring = composition_gstring_from_id (s->cmp_id);
24378 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24379 glyph++;
24380 while (glyph < last
24381 && glyph->u.cmp.automatic
24382 && glyph->u.cmp.id == s->cmp_id
24383 && s->cmp_to == glyph->slice.cmp.from)
24384 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24385
24386 for (i = s->cmp_from; i < s->cmp_to; i++)
24387 {
24388 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24389 unsigned code = LGLYPH_CODE (lglyph);
24390
24391 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24392 }
24393 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24394 return glyph - s->row->glyphs[s->area];
24395 }
24396
24397
24398 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24399 See the comment of fill_glyph_string for arguments.
24400 Value is the index of the first glyph not in S. */
24401
24402
24403 static int
24404 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24405 int start, int end, int overlaps)
24406 {
24407 struct glyph *glyph, *last;
24408 int voffset;
24409
24410 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24411 s->for_overlaps = overlaps;
24412 glyph = s->row->glyphs[s->area] + start;
24413 last = s->row->glyphs[s->area] + end;
24414 voffset = glyph->voffset;
24415 s->face = FACE_FROM_ID (s->f, face_id);
24416 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24417 s->nchars = 1;
24418 s->width = glyph->pixel_width;
24419 glyph++;
24420 while (glyph < last
24421 && glyph->type == GLYPHLESS_GLYPH
24422 && glyph->voffset == voffset
24423 && glyph->face_id == face_id)
24424 {
24425 s->nchars++;
24426 s->width += glyph->pixel_width;
24427 glyph++;
24428 }
24429 s->ybase += voffset;
24430 return glyph - s->row->glyphs[s->area];
24431 }
24432
24433
24434 /* Fill glyph string S from a sequence of character glyphs.
24435
24436 FACE_ID is the face id of the string. START is the index of the
24437 first glyph to consider, END is the index of the last + 1.
24438 OVERLAPS non-zero means S should draw the foreground only, and use
24439 its physical height for clipping. See also draw_glyphs.
24440
24441 Value is the index of the first glyph not in S. */
24442
24443 static int
24444 fill_glyph_string (struct glyph_string *s, int face_id,
24445 int start, int end, int overlaps)
24446 {
24447 struct glyph *glyph, *last;
24448 int voffset;
24449 bool glyph_not_available_p;
24450
24451 eassert (s->f == XFRAME (s->w->frame));
24452 eassert (s->nchars == 0);
24453 eassert (start >= 0 && end > start);
24454
24455 s->for_overlaps = overlaps;
24456 glyph = s->row->glyphs[s->area] + start;
24457 last = s->row->glyphs[s->area] + end;
24458 voffset = glyph->voffset;
24459 s->padding_p = glyph->padding_p;
24460 glyph_not_available_p = glyph->glyph_not_available_p;
24461
24462 while (glyph < last
24463 && glyph->type == CHAR_GLYPH
24464 && glyph->voffset == voffset
24465 /* Same face id implies same font, nowadays. */
24466 && glyph->face_id == face_id
24467 && glyph->glyph_not_available_p == glyph_not_available_p)
24468 {
24469 s->face = get_glyph_face_and_encoding (s->f, glyph,
24470 s->char2b + s->nchars);
24471 ++s->nchars;
24472 eassert (s->nchars <= end - start);
24473 s->width += glyph->pixel_width;
24474 if (glyph++->padding_p != s->padding_p)
24475 break;
24476 }
24477
24478 s->font = s->face->font;
24479
24480 /* If the specified font could not be loaded, use the frame's font,
24481 but record the fact that we couldn't load it in
24482 S->font_not_found_p so that we can draw rectangles for the
24483 characters of the glyph string. */
24484 if (s->font == NULL || glyph_not_available_p)
24485 {
24486 s->font_not_found_p = true;
24487 s->font = FRAME_FONT (s->f);
24488 }
24489
24490 /* Adjust base line for subscript/superscript text. */
24491 s->ybase += voffset;
24492
24493 eassert (s->face && s->face->gc);
24494 return glyph - s->row->glyphs[s->area];
24495 }
24496
24497
24498 /* Fill glyph string S from image glyph S->first_glyph. */
24499
24500 static void
24501 fill_image_glyph_string (struct glyph_string *s)
24502 {
24503 eassert (s->first_glyph->type == IMAGE_GLYPH);
24504 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24505 eassert (s->img);
24506 s->slice = s->first_glyph->slice.img;
24507 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24508 s->font = s->face->font;
24509 s->width = s->first_glyph->pixel_width;
24510
24511 /* Adjust base line for subscript/superscript text. */
24512 s->ybase += s->first_glyph->voffset;
24513 }
24514
24515
24516 /* Fill glyph string S from a sequence of stretch glyphs.
24517
24518 START is the index of the first glyph to consider,
24519 END is the index of the last + 1.
24520
24521 Value is the index of the first glyph not in S. */
24522
24523 static int
24524 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24525 {
24526 struct glyph *glyph, *last;
24527 int voffset, face_id;
24528
24529 eassert (s->first_glyph->type == STRETCH_GLYPH);
24530
24531 glyph = s->row->glyphs[s->area] + start;
24532 last = s->row->glyphs[s->area] + end;
24533 face_id = glyph->face_id;
24534 s->face = FACE_FROM_ID (s->f, face_id);
24535 s->font = s->face->font;
24536 s->width = glyph->pixel_width;
24537 s->nchars = 1;
24538 voffset = glyph->voffset;
24539
24540 for (++glyph;
24541 (glyph < last
24542 && glyph->type == STRETCH_GLYPH
24543 && glyph->voffset == voffset
24544 && glyph->face_id == face_id);
24545 ++glyph)
24546 s->width += glyph->pixel_width;
24547
24548 /* Adjust base line for subscript/superscript text. */
24549 s->ybase += voffset;
24550
24551 /* The case that face->gc == 0 is handled when drawing the glyph
24552 string by calling prepare_face_for_display. */
24553 eassert (s->face);
24554 return glyph - s->row->glyphs[s->area];
24555 }
24556
24557 static struct font_metrics *
24558 get_per_char_metric (struct font *font, XChar2b *char2b)
24559 {
24560 static struct font_metrics metrics;
24561 unsigned code;
24562
24563 if (! font)
24564 return NULL;
24565 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24566 if (code == FONT_INVALID_CODE)
24567 return NULL;
24568 font->driver->text_extents (font, &code, 1, &metrics);
24569 return &metrics;
24570 }
24571
24572 /* A subroutine that computes a reasonable "normal character height"
24573 for fonts that claim preposterously large vertical dimensions, but
24574 whose glyphs are actually reasonably sized. */
24575 static int
24576 normal_char_height (struct font *font)
24577 {
24578 int default_height = FONT_HEIGHT (font);
24579
24580 /* If the font claims too large height, use the metrics of the SPC
24581 character instead. Note that this could still fail to produce a
24582 better value if the font or the font driver don't support the
24583 functionality required by get_per_char_metric. */
24584 if (FONT_TOO_HIGH (font))
24585 {
24586 XChar2b char2b;
24587
24588 /* Get metrics of the SPC character. */
24589 if (get_char_glyph_code (' ', font, &char2b))
24590 {
24591 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24592
24593 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24594 default_height = pcm->ascent + pcm->descent;
24595 }
24596 }
24597 return default_height;
24598 }
24599
24600 /* EXPORT for RIF:
24601 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24602 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24603 assumed to be zero. */
24604
24605 void
24606 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24607 {
24608 *left = *right = 0;
24609
24610 if (glyph->type == CHAR_GLYPH)
24611 {
24612 XChar2b char2b;
24613 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24614 if (face->font)
24615 {
24616 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24617 if (pcm)
24618 {
24619 if (pcm->rbearing > pcm->width)
24620 *right = pcm->rbearing - pcm->width;
24621 if (pcm->lbearing < 0)
24622 *left = -pcm->lbearing;
24623 }
24624 }
24625 }
24626 else if (glyph->type == COMPOSITE_GLYPH)
24627 {
24628 if (! glyph->u.cmp.automatic)
24629 {
24630 struct composition *cmp = composition_table[glyph->u.cmp.id];
24631
24632 if (cmp->rbearing > cmp->pixel_width)
24633 *right = cmp->rbearing - cmp->pixel_width;
24634 if (cmp->lbearing < 0)
24635 *left = - cmp->lbearing;
24636 }
24637 else
24638 {
24639 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24640 struct font_metrics metrics;
24641
24642 composition_gstring_width (gstring, glyph->slice.cmp.from,
24643 glyph->slice.cmp.to + 1, &metrics);
24644 if (metrics.rbearing > metrics.width)
24645 *right = metrics.rbearing - metrics.width;
24646 if (metrics.lbearing < 0)
24647 *left = - metrics.lbearing;
24648 }
24649 }
24650 }
24651
24652
24653 /* Return the index of the first glyph preceding glyph string S that
24654 is overwritten by S because of S's left overhang. Value is -1
24655 if no glyphs are overwritten. */
24656
24657 static int
24658 left_overwritten (struct glyph_string *s)
24659 {
24660 int k;
24661
24662 if (s->left_overhang)
24663 {
24664 int x = 0, i;
24665 struct glyph *glyphs = s->row->glyphs[s->area];
24666 int first = s->first_glyph - glyphs;
24667
24668 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24669 x -= glyphs[i].pixel_width;
24670
24671 k = i + 1;
24672 }
24673 else
24674 k = -1;
24675
24676 return k;
24677 }
24678
24679
24680 /* Return the index of the first glyph preceding glyph string S that
24681 is overwriting S because of its right overhang. Value is -1 if no
24682 glyph in front of S overwrites S. */
24683
24684 static int
24685 left_overwriting (struct glyph_string *s)
24686 {
24687 int i, k, x;
24688 struct glyph *glyphs = s->row->glyphs[s->area];
24689 int first = s->first_glyph - glyphs;
24690
24691 k = -1;
24692 x = 0;
24693 for (i = first - 1; i >= 0; --i)
24694 {
24695 int left, right;
24696 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24697 if (x + right > 0)
24698 k = i;
24699 x -= glyphs[i].pixel_width;
24700 }
24701
24702 return k;
24703 }
24704
24705
24706 /* Return the index of the last glyph following glyph string S that is
24707 overwritten by S because of S's right overhang. Value is -1 if
24708 no such glyph is found. */
24709
24710 static int
24711 right_overwritten (struct glyph_string *s)
24712 {
24713 int k = -1;
24714
24715 if (s->right_overhang)
24716 {
24717 int x = 0, i;
24718 struct glyph *glyphs = s->row->glyphs[s->area];
24719 int first = (s->first_glyph - glyphs
24720 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24721 int end = s->row->used[s->area];
24722
24723 for (i = first; i < end && s->right_overhang > x; ++i)
24724 x += glyphs[i].pixel_width;
24725
24726 k = i;
24727 }
24728
24729 return k;
24730 }
24731
24732
24733 /* Return the index of the last glyph following glyph string S that
24734 overwrites S because of its left overhang. Value is negative
24735 if no such glyph is found. */
24736
24737 static int
24738 right_overwriting (struct glyph_string *s)
24739 {
24740 int i, k, x;
24741 int end = s->row->used[s->area];
24742 struct glyph *glyphs = s->row->glyphs[s->area];
24743 int first = (s->first_glyph - glyphs
24744 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24745
24746 k = -1;
24747 x = 0;
24748 for (i = first; i < end; ++i)
24749 {
24750 int left, right;
24751 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24752 if (x - left < 0)
24753 k = i;
24754 x += glyphs[i].pixel_width;
24755 }
24756
24757 return k;
24758 }
24759
24760
24761 /* Set background width of glyph string S. START is the index of the
24762 first glyph following S. LAST_X is the right-most x-position + 1
24763 in the drawing area. */
24764
24765 static void
24766 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24767 {
24768 /* If the face of this glyph string has to be drawn to the end of
24769 the drawing area, set S->extends_to_end_of_line_p. */
24770
24771 if (start == s->row->used[s->area]
24772 && ((s->row->fill_line_p
24773 && (s->hl == DRAW_NORMAL_TEXT
24774 || s->hl == DRAW_IMAGE_RAISED
24775 || s->hl == DRAW_IMAGE_SUNKEN))
24776 || s->hl == DRAW_MOUSE_FACE))
24777 s->extends_to_end_of_line_p = true;
24778
24779 /* If S extends its face to the end of the line, set its
24780 background_width to the distance to the right edge of the drawing
24781 area. */
24782 if (s->extends_to_end_of_line_p)
24783 s->background_width = last_x - s->x + 1;
24784 else
24785 s->background_width = s->width;
24786 }
24787
24788
24789 /* Compute overhangs and x-positions for glyph string S and its
24790 predecessors, or successors. X is the starting x-position for S.
24791 BACKWARD_P means process predecessors. */
24792
24793 static void
24794 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24795 {
24796 if (backward_p)
24797 {
24798 while (s)
24799 {
24800 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24801 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24802 x -= s->width;
24803 s->x = x;
24804 s = s->prev;
24805 }
24806 }
24807 else
24808 {
24809 while (s)
24810 {
24811 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24812 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24813 s->x = x;
24814 x += s->width;
24815 s = s->next;
24816 }
24817 }
24818 }
24819
24820
24821
24822 /* The following macros are only called from draw_glyphs below.
24823 They reference the following parameters of that function directly:
24824 `w', `row', `area', and `overlap_p'
24825 as well as the following local variables:
24826 `s', `f', and `hdc' (in W32) */
24827
24828 #ifdef HAVE_NTGUI
24829 /* On W32, silently add local `hdc' variable to argument list of
24830 init_glyph_string. */
24831 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24832 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24833 #else
24834 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24835 init_glyph_string (s, char2b, w, row, area, start, hl)
24836 #endif
24837
24838 /* Add a glyph string for a stretch glyph to the list of strings
24839 between HEAD and TAIL. START is the index of the stretch glyph in
24840 row area AREA of glyph row ROW. END is the index of the last glyph
24841 in that glyph row area. X is the current output position assigned
24842 to the new glyph string constructed. HL overrides that face of the
24843 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24844 is the right-most x-position of the drawing area. */
24845
24846 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24847 and below -- keep them on one line. */
24848 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24849 do \
24850 { \
24851 s = alloca (sizeof *s); \
24852 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24853 START = fill_stretch_glyph_string (s, START, END); \
24854 append_glyph_string (&HEAD, &TAIL, s); \
24855 s->x = (X); \
24856 } \
24857 while (false)
24858
24859
24860 /* Add a glyph string for an image glyph to the list of strings
24861 between HEAD and TAIL. START is the index of the image glyph in
24862 row area AREA of glyph row ROW. END is the index of the last glyph
24863 in that glyph row area. X is the current output position assigned
24864 to the new glyph string constructed. HL overrides that face of the
24865 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24866 is the right-most x-position of the drawing area. */
24867
24868 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24869 do \
24870 { \
24871 s = alloca (sizeof *s); \
24872 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24873 fill_image_glyph_string (s); \
24874 append_glyph_string (&HEAD, &TAIL, s); \
24875 ++START; \
24876 s->x = (X); \
24877 } \
24878 while (false)
24879
24880
24881 /* Add a glyph string for a sequence of character glyphs to the list
24882 of strings between HEAD and TAIL. START is the index of the first
24883 glyph in row area AREA of glyph row ROW that is part of the new
24884 glyph string. END is the index of the last glyph in that glyph row
24885 area. X is the current output position assigned to the new glyph
24886 string constructed. HL overrides that face of the glyph; e.g. it
24887 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24888 right-most x-position of the drawing area. */
24889
24890 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24891 do \
24892 { \
24893 int face_id; \
24894 XChar2b *char2b; \
24895 \
24896 face_id = (row)->glyphs[area][START].face_id; \
24897 \
24898 s = alloca (sizeof *s); \
24899 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24900 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24901 append_glyph_string (&HEAD, &TAIL, s); \
24902 s->x = (X); \
24903 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24904 } \
24905 while (false)
24906
24907
24908 /* Add a glyph string for a composite sequence to the list of strings
24909 between HEAD and TAIL. START is the index of the first glyph in
24910 row area AREA of glyph row ROW that is part of the new glyph
24911 string. END is the index of the last glyph in that glyph row area.
24912 X is the current output position assigned to the new glyph string
24913 constructed. HL overrides that face of the glyph; e.g. it is
24914 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24915 x-position of the drawing area. */
24916
24917 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24918 do { \
24919 int face_id = (row)->glyphs[area][START].face_id; \
24920 struct face *base_face = FACE_FROM_ID (f, face_id); \
24921 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24922 struct composition *cmp = composition_table[cmp_id]; \
24923 XChar2b *char2b; \
24924 struct glyph_string *first_s = NULL; \
24925 int n; \
24926 \
24927 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24928 \
24929 /* Make glyph_strings for each glyph sequence that is drawable by \
24930 the same face, and append them to HEAD/TAIL. */ \
24931 for (n = 0; n < cmp->glyph_len;) \
24932 { \
24933 s = alloca (sizeof *s); \
24934 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24935 append_glyph_string (&(HEAD), &(TAIL), s); \
24936 s->cmp = cmp; \
24937 s->cmp_from = n; \
24938 s->x = (X); \
24939 if (n == 0) \
24940 first_s = s; \
24941 n = fill_composite_glyph_string (s, base_face, overlaps); \
24942 } \
24943 \
24944 ++START; \
24945 s = first_s; \
24946 } while (false)
24947
24948
24949 /* Add a glyph string for a glyph-string sequence to the list of strings
24950 between HEAD and TAIL. */
24951
24952 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24953 do { \
24954 int face_id; \
24955 XChar2b *char2b; \
24956 Lisp_Object gstring; \
24957 \
24958 face_id = (row)->glyphs[area][START].face_id; \
24959 gstring = (composition_gstring_from_id \
24960 ((row)->glyphs[area][START].u.cmp.id)); \
24961 s = alloca (sizeof *s); \
24962 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24963 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24964 append_glyph_string (&(HEAD), &(TAIL), s); \
24965 s->x = (X); \
24966 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24967 } while (false)
24968
24969
24970 /* Add a glyph string for a sequence of glyphless character's glyphs
24971 to the list of strings between HEAD and TAIL. The meanings of
24972 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24973
24974 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24975 do \
24976 { \
24977 int face_id; \
24978 \
24979 face_id = (row)->glyphs[area][START].face_id; \
24980 \
24981 s = alloca (sizeof *s); \
24982 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24983 append_glyph_string (&HEAD, &TAIL, s); \
24984 s->x = (X); \
24985 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24986 overlaps); \
24987 } \
24988 while (false)
24989
24990
24991 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24992 of AREA of glyph row ROW on window W between indices START and END.
24993 HL overrides the face for drawing glyph strings, e.g. it is
24994 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24995 x-positions of the drawing area.
24996
24997 This is an ugly monster macro construct because we must use alloca
24998 to allocate glyph strings (because draw_glyphs can be called
24999 asynchronously). */
25000
25001 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25002 do \
25003 { \
25004 HEAD = TAIL = NULL; \
25005 while (START < END) \
25006 { \
25007 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25008 switch (first_glyph->type) \
25009 { \
25010 case CHAR_GLYPH: \
25011 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25012 HL, X, LAST_X); \
25013 break; \
25014 \
25015 case COMPOSITE_GLYPH: \
25016 if (first_glyph->u.cmp.automatic) \
25017 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25018 HL, X, LAST_X); \
25019 else \
25020 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25021 HL, X, LAST_X); \
25022 break; \
25023 \
25024 case STRETCH_GLYPH: \
25025 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25026 HL, X, LAST_X); \
25027 break; \
25028 \
25029 case IMAGE_GLYPH: \
25030 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25031 HL, X, LAST_X); \
25032 break; \
25033 \
25034 case GLYPHLESS_GLYPH: \
25035 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25036 HL, X, LAST_X); \
25037 break; \
25038 \
25039 default: \
25040 emacs_abort (); \
25041 } \
25042 \
25043 if (s) \
25044 { \
25045 set_glyph_string_background_width (s, START, LAST_X); \
25046 (X) += s->width; \
25047 } \
25048 } \
25049 } while (false)
25050
25051
25052 /* Draw glyphs between START and END in AREA of ROW on window W,
25053 starting at x-position X. X is relative to AREA in W. HL is a
25054 face-override with the following meaning:
25055
25056 DRAW_NORMAL_TEXT draw normally
25057 DRAW_CURSOR draw in cursor face
25058 DRAW_MOUSE_FACE draw in mouse face.
25059 DRAW_INVERSE_VIDEO draw in mode line face
25060 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25061 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25062
25063 If OVERLAPS is non-zero, draw only the foreground of characters and
25064 clip to the physical height of ROW. Non-zero value also defines
25065 the overlapping part to be drawn:
25066
25067 OVERLAPS_PRED overlap with preceding rows
25068 OVERLAPS_SUCC overlap with succeeding rows
25069 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25070 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25071
25072 Value is the x-position reached, relative to AREA of W. */
25073
25074 static int
25075 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25076 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25077 enum draw_glyphs_face hl, int overlaps)
25078 {
25079 struct glyph_string *head, *tail;
25080 struct glyph_string *s;
25081 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25082 int i, j, x_reached, last_x, area_left = 0;
25083 struct frame *f = XFRAME (WINDOW_FRAME (w));
25084 DECLARE_HDC (hdc);
25085
25086 ALLOCATE_HDC (hdc, f);
25087
25088 /* Let's rather be paranoid than getting a SEGV. */
25089 end = min (end, row->used[area]);
25090 start = clip_to_bounds (0, start, end);
25091
25092 /* Translate X to frame coordinates. Set last_x to the right
25093 end of the drawing area. */
25094 if (row->full_width_p)
25095 {
25096 /* X is relative to the left edge of W, without scroll bars
25097 or fringes. */
25098 area_left = WINDOW_LEFT_EDGE_X (w);
25099 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25100 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25101 }
25102 else
25103 {
25104 area_left = window_box_left (w, area);
25105 last_x = area_left + window_box_width (w, area);
25106 }
25107 x += area_left;
25108
25109 /* Build a doubly-linked list of glyph_string structures between
25110 head and tail from what we have to draw. Note that the macro
25111 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25112 the reason we use a separate variable `i'. */
25113 i = start;
25114 USE_SAFE_ALLOCA;
25115 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25116 if (tail)
25117 x_reached = tail->x + tail->background_width;
25118 else
25119 x_reached = x;
25120
25121 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25122 the row, redraw some glyphs in front or following the glyph
25123 strings built above. */
25124 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25125 {
25126 struct glyph_string *h, *t;
25127 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25128 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25129 bool check_mouse_face = false;
25130 int dummy_x = 0;
25131
25132 /* If mouse highlighting is on, we may need to draw adjacent
25133 glyphs using mouse-face highlighting. */
25134 if (area == TEXT_AREA && row->mouse_face_p
25135 && hlinfo->mouse_face_beg_row >= 0
25136 && hlinfo->mouse_face_end_row >= 0)
25137 {
25138 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25139
25140 if (row_vpos >= hlinfo->mouse_face_beg_row
25141 && row_vpos <= hlinfo->mouse_face_end_row)
25142 {
25143 check_mouse_face = true;
25144 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25145 ? hlinfo->mouse_face_beg_col : 0;
25146 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25147 ? hlinfo->mouse_face_end_col
25148 : row->used[TEXT_AREA];
25149 }
25150 }
25151
25152 /* Compute overhangs for all glyph strings. */
25153 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25154 for (s = head; s; s = s->next)
25155 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25156
25157 /* Prepend glyph strings for glyphs in front of the first glyph
25158 string that are overwritten because of the first glyph
25159 string's left overhang. The background of all strings
25160 prepended must be drawn because the first glyph string
25161 draws over it. */
25162 i = left_overwritten (head);
25163 if (i >= 0)
25164 {
25165 enum draw_glyphs_face overlap_hl;
25166
25167 /* If this row contains mouse highlighting, attempt to draw
25168 the overlapped glyphs with the correct highlight. This
25169 code fails if the overlap encompasses more than one glyph
25170 and mouse-highlight spans only some of these glyphs.
25171 However, making it work perfectly involves a lot more
25172 code, and I don't know if the pathological case occurs in
25173 practice, so we'll stick to this for now. --- cyd */
25174 if (check_mouse_face
25175 && mouse_beg_col < start && mouse_end_col > i)
25176 overlap_hl = DRAW_MOUSE_FACE;
25177 else
25178 overlap_hl = DRAW_NORMAL_TEXT;
25179
25180 if (hl != overlap_hl)
25181 clip_head = head;
25182 j = i;
25183 BUILD_GLYPH_STRINGS (j, start, h, t,
25184 overlap_hl, dummy_x, last_x);
25185 start = i;
25186 compute_overhangs_and_x (t, head->x, true);
25187 prepend_glyph_string_lists (&head, &tail, h, t);
25188 if (clip_head == NULL)
25189 clip_head = head;
25190 }
25191
25192 /* Prepend glyph strings for glyphs in front of the first glyph
25193 string that overwrite that glyph string because of their
25194 right overhang. For these strings, only the foreground must
25195 be drawn, because it draws over the glyph string at `head'.
25196 The background must not be drawn because this would overwrite
25197 right overhangs of preceding glyphs for which no glyph
25198 strings exist. */
25199 i = left_overwriting (head);
25200 if (i >= 0)
25201 {
25202 enum draw_glyphs_face overlap_hl;
25203
25204 if (check_mouse_face
25205 && mouse_beg_col < start && mouse_end_col > i)
25206 overlap_hl = DRAW_MOUSE_FACE;
25207 else
25208 overlap_hl = DRAW_NORMAL_TEXT;
25209
25210 if (hl == overlap_hl || clip_head == NULL)
25211 clip_head = head;
25212 BUILD_GLYPH_STRINGS (i, start, h, t,
25213 overlap_hl, dummy_x, last_x);
25214 for (s = h; s; s = s->next)
25215 s->background_filled_p = true;
25216 compute_overhangs_and_x (t, head->x, true);
25217 prepend_glyph_string_lists (&head, &tail, h, t);
25218 }
25219
25220 /* Append glyphs strings for glyphs following the last glyph
25221 string tail that are overwritten by tail. The background of
25222 these strings has to be drawn because tail's foreground draws
25223 over it. */
25224 i = right_overwritten (tail);
25225 if (i >= 0)
25226 {
25227 enum draw_glyphs_face overlap_hl;
25228
25229 if (check_mouse_face
25230 && mouse_beg_col < i && mouse_end_col > end)
25231 overlap_hl = DRAW_MOUSE_FACE;
25232 else
25233 overlap_hl = DRAW_NORMAL_TEXT;
25234
25235 if (hl != overlap_hl)
25236 clip_tail = tail;
25237 BUILD_GLYPH_STRINGS (end, i, h, t,
25238 overlap_hl, x, last_x);
25239 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25240 we don't have `end = i;' here. */
25241 compute_overhangs_and_x (h, tail->x + tail->width, false);
25242 append_glyph_string_lists (&head, &tail, h, t);
25243 if (clip_tail == NULL)
25244 clip_tail = tail;
25245 }
25246
25247 /* Append glyph strings for glyphs following the last glyph
25248 string tail that overwrite tail. The foreground of such
25249 glyphs has to be drawn because it writes into the background
25250 of tail. The background must not be drawn because it could
25251 paint over the foreground of following glyphs. */
25252 i = right_overwriting (tail);
25253 if (i >= 0)
25254 {
25255 enum draw_glyphs_face overlap_hl;
25256 if (check_mouse_face
25257 && mouse_beg_col < i && mouse_end_col > end)
25258 overlap_hl = DRAW_MOUSE_FACE;
25259 else
25260 overlap_hl = DRAW_NORMAL_TEXT;
25261
25262 if (hl == overlap_hl || clip_tail == NULL)
25263 clip_tail = tail;
25264 i++; /* We must include the Ith glyph. */
25265 BUILD_GLYPH_STRINGS (end, i, h, t,
25266 overlap_hl, x, last_x);
25267 for (s = h; s; s = s->next)
25268 s->background_filled_p = true;
25269 compute_overhangs_and_x (h, tail->x + tail->width, false);
25270 append_glyph_string_lists (&head, &tail, h, t);
25271 }
25272 if (clip_head || clip_tail)
25273 for (s = head; s; s = s->next)
25274 {
25275 s->clip_head = clip_head;
25276 s->clip_tail = clip_tail;
25277 }
25278 }
25279
25280 /* Draw all strings. */
25281 for (s = head; s; s = s->next)
25282 FRAME_RIF (f)->draw_glyph_string (s);
25283
25284 #ifndef HAVE_NS
25285 /* When focus a sole frame and move horizontally, this clears on_p
25286 causing a failure to erase prev cursor position. */
25287 if (area == TEXT_AREA
25288 && !row->full_width_p
25289 /* When drawing overlapping rows, only the glyph strings'
25290 foreground is drawn, which doesn't erase a cursor
25291 completely. */
25292 && !overlaps)
25293 {
25294 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25295 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25296 : (tail ? tail->x + tail->background_width : x));
25297 x0 -= area_left;
25298 x1 -= area_left;
25299
25300 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25301 row->y, MATRIX_ROW_BOTTOM_Y (row));
25302 }
25303 #endif
25304
25305 /* Value is the x-position up to which drawn, relative to AREA of W.
25306 This doesn't include parts drawn because of overhangs. */
25307 if (row->full_width_p)
25308 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25309 else
25310 x_reached -= area_left;
25311
25312 RELEASE_HDC (hdc, f);
25313
25314 SAFE_FREE ();
25315 return x_reached;
25316 }
25317
25318 /* Expand row matrix if too narrow. Don't expand if area
25319 is not present. */
25320
25321 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25322 { \
25323 if (!it->f->fonts_changed \
25324 && (it->glyph_row->glyphs[area] \
25325 < it->glyph_row->glyphs[area + 1])) \
25326 { \
25327 it->w->ncols_scale_factor++; \
25328 it->f->fonts_changed = true; \
25329 } \
25330 }
25331
25332 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25333 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25334
25335 static void
25336 append_glyph (struct it *it)
25337 {
25338 struct glyph *glyph;
25339 enum glyph_row_area area = it->area;
25340
25341 eassert (it->glyph_row);
25342 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25343
25344 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25345 if (glyph < it->glyph_row->glyphs[area + 1])
25346 {
25347 /* If the glyph row is reversed, we need to prepend the glyph
25348 rather than append it. */
25349 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25350 {
25351 struct glyph *g;
25352
25353 /* Make room for the additional glyph. */
25354 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25355 g[1] = *g;
25356 glyph = it->glyph_row->glyphs[area];
25357 }
25358 glyph->charpos = CHARPOS (it->position);
25359 glyph->object = it->object;
25360 if (it->pixel_width > 0)
25361 {
25362 glyph->pixel_width = it->pixel_width;
25363 glyph->padding_p = false;
25364 }
25365 else
25366 {
25367 /* Assure at least 1-pixel width. Otherwise, cursor can't
25368 be displayed correctly. */
25369 glyph->pixel_width = 1;
25370 glyph->padding_p = true;
25371 }
25372 glyph->ascent = it->ascent;
25373 glyph->descent = it->descent;
25374 glyph->voffset = it->voffset;
25375 glyph->type = CHAR_GLYPH;
25376 glyph->avoid_cursor_p = it->avoid_cursor_p;
25377 glyph->multibyte_p = it->multibyte_p;
25378 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25379 {
25380 /* In R2L rows, the left and the right box edges need to be
25381 drawn in reverse direction. */
25382 glyph->right_box_line_p = it->start_of_box_run_p;
25383 glyph->left_box_line_p = it->end_of_box_run_p;
25384 }
25385 else
25386 {
25387 glyph->left_box_line_p = it->start_of_box_run_p;
25388 glyph->right_box_line_p = it->end_of_box_run_p;
25389 }
25390 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25391 || it->phys_descent > it->descent);
25392 glyph->glyph_not_available_p = it->glyph_not_available_p;
25393 glyph->face_id = it->face_id;
25394 glyph->u.ch = it->char_to_display;
25395 glyph->slice.img = null_glyph_slice;
25396 glyph->font_type = FONT_TYPE_UNKNOWN;
25397 if (it->bidi_p)
25398 {
25399 glyph->resolved_level = it->bidi_it.resolved_level;
25400 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25401 glyph->bidi_type = it->bidi_it.type;
25402 }
25403 else
25404 {
25405 glyph->resolved_level = 0;
25406 glyph->bidi_type = UNKNOWN_BT;
25407 }
25408 ++it->glyph_row->used[area];
25409 }
25410 else
25411 IT_EXPAND_MATRIX_WIDTH (it, area);
25412 }
25413
25414 /* Store one glyph for the composition IT->cmp_it.id in
25415 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25416 non-null. */
25417
25418 static void
25419 append_composite_glyph (struct it *it)
25420 {
25421 struct glyph *glyph;
25422 enum glyph_row_area area = it->area;
25423
25424 eassert (it->glyph_row);
25425
25426 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25427 if (glyph < it->glyph_row->glyphs[area + 1])
25428 {
25429 /* If the glyph row is reversed, we need to prepend the glyph
25430 rather than append it. */
25431 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25432 {
25433 struct glyph *g;
25434
25435 /* Make room for the new glyph. */
25436 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25437 g[1] = *g;
25438 glyph = it->glyph_row->glyphs[it->area];
25439 }
25440 glyph->charpos = it->cmp_it.charpos;
25441 glyph->object = it->object;
25442 glyph->pixel_width = it->pixel_width;
25443 glyph->ascent = it->ascent;
25444 glyph->descent = it->descent;
25445 glyph->voffset = it->voffset;
25446 glyph->type = COMPOSITE_GLYPH;
25447 if (it->cmp_it.ch < 0)
25448 {
25449 glyph->u.cmp.automatic = false;
25450 glyph->u.cmp.id = it->cmp_it.id;
25451 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25452 }
25453 else
25454 {
25455 glyph->u.cmp.automatic = true;
25456 glyph->u.cmp.id = it->cmp_it.id;
25457 glyph->slice.cmp.from = it->cmp_it.from;
25458 glyph->slice.cmp.to = it->cmp_it.to - 1;
25459 }
25460 glyph->avoid_cursor_p = it->avoid_cursor_p;
25461 glyph->multibyte_p = it->multibyte_p;
25462 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25463 {
25464 /* In R2L rows, the left and the right box edges need to be
25465 drawn in reverse direction. */
25466 glyph->right_box_line_p = it->start_of_box_run_p;
25467 glyph->left_box_line_p = it->end_of_box_run_p;
25468 }
25469 else
25470 {
25471 glyph->left_box_line_p = it->start_of_box_run_p;
25472 glyph->right_box_line_p = it->end_of_box_run_p;
25473 }
25474 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25475 || it->phys_descent > it->descent);
25476 glyph->padding_p = false;
25477 glyph->glyph_not_available_p = false;
25478 glyph->face_id = it->face_id;
25479 glyph->font_type = FONT_TYPE_UNKNOWN;
25480 if (it->bidi_p)
25481 {
25482 glyph->resolved_level = it->bidi_it.resolved_level;
25483 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25484 glyph->bidi_type = it->bidi_it.type;
25485 }
25486 ++it->glyph_row->used[area];
25487 }
25488 else
25489 IT_EXPAND_MATRIX_WIDTH (it, area);
25490 }
25491
25492
25493 /* Change IT->ascent and IT->height according to the setting of
25494 IT->voffset. */
25495
25496 static void
25497 take_vertical_position_into_account (struct it *it)
25498 {
25499 if (it->voffset)
25500 {
25501 if (it->voffset < 0)
25502 /* Increase the ascent so that we can display the text higher
25503 in the line. */
25504 it->ascent -= it->voffset;
25505 else
25506 /* Increase the descent so that we can display the text lower
25507 in the line. */
25508 it->descent += it->voffset;
25509 }
25510 }
25511
25512
25513 /* Produce glyphs/get display metrics for the image IT is loaded with.
25514 See the description of struct display_iterator in dispextern.h for
25515 an overview of struct display_iterator. */
25516
25517 static void
25518 produce_image_glyph (struct it *it)
25519 {
25520 struct image *img;
25521 struct face *face;
25522 int glyph_ascent, crop;
25523 struct glyph_slice slice;
25524
25525 eassert (it->what == IT_IMAGE);
25526
25527 face = FACE_FROM_ID (it->f, it->face_id);
25528 eassert (face);
25529 /* Make sure X resources of the face is loaded. */
25530 prepare_face_for_display (it->f, face);
25531
25532 if (it->image_id < 0)
25533 {
25534 /* Fringe bitmap. */
25535 it->ascent = it->phys_ascent = 0;
25536 it->descent = it->phys_descent = 0;
25537 it->pixel_width = 0;
25538 it->nglyphs = 0;
25539 return;
25540 }
25541
25542 img = IMAGE_FROM_ID (it->f, it->image_id);
25543 eassert (img);
25544 /* Make sure X resources of the image is loaded. */
25545 prepare_image_for_display (it->f, img);
25546
25547 slice.x = slice.y = 0;
25548 slice.width = img->width;
25549 slice.height = img->height;
25550
25551 if (INTEGERP (it->slice.x))
25552 slice.x = XINT (it->slice.x);
25553 else if (FLOATP (it->slice.x))
25554 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25555
25556 if (INTEGERP (it->slice.y))
25557 slice.y = XINT (it->slice.y);
25558 else if (FLOATP (it->slice.y))
25559 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25560
25561 if (INTEGERP (it->slice.width))
25562 slice.width = XINT (it->slice.width);
25563 else if (FLOATP (it->slice.width))
25564 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25565
25566 if (INTEGERP (it->slice.height))
25567 slice.height = XINT (it->slice.height);
25568 else if (FLOATP (it->slice.height))
25569 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25570
25571 if (slice.x >= img->width)
25572 slice.x = img->width;
25573 if (slice.y >= img->height)
25574 slice.y = img->height;
25575 if (slice.x + slice.width >= img->width)
25576 slice.width = img->width - slice.x;
25577 if (slice.y + slice.height > img->height)
25578 slice.height = img->height - slice.y;
25579
25580 if (slice.width == 0 || slice.height == 0)
25581 return;
25582
25583 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25584
25585 it->descent = slice.height - glyph_ascent;
25586 if (slice.y == 0)
25587 it->descent += img->vmargin;
25588 if (slice.y + slice.height == img->height)
25589 it->descent += img->vmargin;
25590 it->phys_descent = it->descent;
25591
25592 it->pixel_width = slice.width;
25593 if (slice.x == 0)
25594 it->pixel_width += img->hmargin;
25595 if (slice.x + slice.width == img->width)
25596 it->pixel_width += img->hmargin;
25597
25598 /* It's quite possible for images to have an ascent greater than
25599 their height, so don't get confused in that case. */
25600 if (it->descent < 0)
25601 it->descent = 0;
25602
25603 it->nglyphs = 1;
25604
25605 if (face->box != FACE_NO_BOX)
25606 {
25607 if (face->box_line_width > 0)
25608 {
25609 if (slice.y == 0)
25610 it->ascent += face->box_line_width;
25611 if (slice.y + slice.height == img->height)
25612 it->descent += face->box_line_width;
25613 }
25614
25615 if (it->start_of_box_run_p && slice.x == 0)
25616 it->pixel_width += eabs (face->box_line_width);
25617 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25618 it->pixel_width += eabs (face->box_line_width);
25619 }
25620
25621 take_vertical_position_into_account (it);
25622
25623 /* Automatically crop wide image glyphs at right edge so we can
25624 draw the cursor on same display row. */
25625 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25626 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25627 {
25628 it->pixel_width -= crop;
25629 slice.width -= crop;
25630 }
25631
25632 if (it->glyph_row)
25633 {
25634 struct glyph *glyph;
25635 enum glyph_row_area area = it->area;
25636
25637 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25638 if (it->glyph_row->reversed_p)
25639 {
25640 struct glyph *g;
25641
25642 /* Make room for the new glyph. */
25643 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25644 g[1] = *g;
25645 glyph = it->glyph_row->glyphs[it->area];
25646 }
25647 if (glyph < it->glyph_row->glyphs[area + 1])
25648 {
25649 glyph->charpos = CHARPOS (it->position);
25650 glyph->object = it->object;
25651 glyph->pixel_width = it->pixel_width;
25652 glyph->ascent = glyph_ascent;
25653 glyph->descent = it->descent;
25654 glyph->voffset = it->voffset;
25655 glyph->type = IMAGE_GLYPH;
25656 glyph->avoid_cursor_p = it->avoid_cursor_p;
25657 glyph->multibyte_p = it->multibyte_p;
25658 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25659 {
25660 /* In R2L rows, the left and the right box edges need to be
25661 drawn in reverse direction. */
25662 glyph->right_box_line_p = it->start_of_box_run_p;
25663 glyph->left_box_line_p = it->end_of_box_run_p;
25664 }
25665 else
25666 {
25667 glyph->left_box_line_p = it->start_of_box_run_p;
25668 glyph->right_box_line_p = it->end_of_box_run_p;
25669 }
25670 glyph->overlaps_vertically_p = false;
25671 glyph->padding_p = false;
25672 glyph->glyph_not_available_p = false;
25673 glyph->face_id = it->face_id;
25674 glyph->u.img_id = img->id;
25675 glyph->slice.img = slice;
25676 glyph->font_type = FONT_TYPE_UNKNOWN;
25677 if (it->bidi_p)
25678 {
25679 glyph->resolved_level = it->bidi_it.resolved_level;
25680 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25681 glyph->bidi_type = it->bidi_it.type;
25682 }
25683 ++it->glyph_row->used[area];
25684 }
25685 else
25686 IT_EXPAND_MATRIX_WIDTH (it, area);
25687 }
25688 }
25689
25690
25691 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25692 of the glyph, WIDTH and HEIGHT are the width and height of the
25693 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25694
25695 static void
25696 append_stretch_glyph (struct it *it, Lisp_Object object,
25697 int width, int height, int ascent)
25698 {
25699 struct glyph *glyph;
25700 enum glyph_row_area area = it->area;
25701
25702 eassert (ascent >= 0 && ascent <= height);
25703
25704 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25705 if (glyph < it->glyph_row->glyphs[area + 1])
25706 {
25707 /* If the glyph row is reversed, we need to prepend the glyph
25708 rather than append it. */
25709 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25710 {
25711 struct glyph *g;
25712
25713 /* Make room for the additional glyph. */
25714 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25715 g[1] = *g;
25716 glyph = it->glyph_row->glyphs[area];
25717
25718 /* Decrease the width of the first glyph of the row that
25719 begins before first_visible_x (e.g., due to hscroll).
25720 This is so the overall width of the row becomes smaller
25721 by the scroll amount, and the stretch glyph appended by
25722 extend_face_to_end_of_line will be wider, to shift the
25723 row glyphs to the right. (In L2R rows, the corresponding
25724 left-shift effect is accomplished by setting row->x to a
25725 negative value, which won't work with R2L rows.)
25726
25727 This must leave us with a positive value of WIDTH, since
25728 otherwise the call to move_it_in_display_line_to at the
25729 beginning of display_line would have got past the entire
25730 first glyph, and then it->current_x would have been
25731 greater or equal to it->first_visible_x. */
25732 if (it->current_x < it->first_visible_x)
25733 width -= it->first_visible_x - it->current_x;
25734 eassert (width > 0);
25735 }
25736 glyph->charpos = CHARPOS (it->position);
25737 glyph->object = object;
25738 glyph->pixel_width = width;
25739 glyph->ascent = ascent;
25740 glyph->descent = height - ascent;
25741 glyph->voffset = it->voffset;
25742 glyph->type = STRETCH_GLYPH;
25743 glyph->avoid_cursor_p = it->avoid_cursor_p;
25744 glyph->multibyte_p = it->multibyte_p;
25745 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25746 {
25747 /* In R2L rows, the left and the right box edges need to be
25748 drawn in reverse direction. */
25749 glyph->right_box_line_p = it->start_of_box_run_p;
25750 glyph->left_box_line_p = it->end_of_box_run_p;
25751 }
25752 else
25753 {
25754 glyph->left_box_line_p = it->start_of_box_run_p;
25755 glyph->right_box_line_p = it->end_of_box_run_p;
25756 }
25757 glyph->overlaps_vertically_p = false;
25758 glyph->padding_p = false;
25759 glyph->glyph_not_available_p = false;
25760 glyph->face_id = it->face_id;
25761 glyph->u.stretch.ascent = ascent;
25762 glyph->u.stretch.height = height;
25763 glyph->slice.img = null_glyph_slice;
25764 glyph->font_type = FONT_TYPE_UNKNOWN;
25765 if (it->bidi_p)
25766 {
25767 glyph->resolved_level = it->bidi_it.resolved_level;
25768 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25769 glyph->bidi_type = it->bidi_it.type;
25770 }
25771 else
25772 {
25773 glyph->resolved_level = 0;
25774 glyph->bidi_type = UNKNOWN_BT;
25775 }
25776 ++it->glyph_row->used[area];
25777 }
25778 else
25779 IT_EXPAND_MATRIX_WIDTH (it, area);
25780 }
25781
25782 #endif /* HAVE_WINDOW_SYSTEM */
25783
25784 /* Produce a stretch glyph for iterator IT. IT->object is the value
25785 of the glyph property displayed. The value must be a list
25786 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25787 being recognized:
25788
25789 1. `:width WIDTH' specifies that the space should be WIDTH *
25790 canonical char width wide. WIDTH may be an integer or floating
25791 point number.
25792
25793 2. `:relative-width FACTOR' specifies that the width of the stretch
25794 should be computed from the width of the first character having the
25795 `glyph' property, and should be FACTOR times that width.
25796
25797 3. `:align-to HPOS' specifies that the space should be wide enough
25798 to reach HPOS, a value in canonical character units.
25799
25800 Exactly one of the above pairs must be present.
25801
25802 4. `:height HEIGHT' specifies that the height of the stretch produced
25803 should be HEIGHT, measured in canonical character units.
25804
25805 5. `:relative-height FACTOR' specifies that the height of the
25806 stretch should be FACTOR times the height of the characters having
25807 the glyph property.
25808
25809 Either none or exactly one of 4 or 5 must be present.
25810
25811 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25812 of the stretch should be used for the ascent of the stretch.
25813 ASCENT must be in the range 0 <= ASCENT <= 100. */
25814
25815 void
25816 produce_stretch_glyph (struct it *it)
25817 {
25818 /* (space :width WIDTH :height HEIGHT ...) */
25819 Lisp_Object prop, plist;
25820 int width = 0, height = 0, align_to = -1;
25821 bool zero_width_ok_p = false;
25822 double tem;
25823 struct font *font = NULL;
25824
25825 #ifdef HAVE_WINDOW_SYSTEM
25826 int ascent = 0;
25827 bool zero_height_ok_p = false;
25828
25829 if (FRAME_WINDOW_P (it->f))
25830 {
25831 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25832 font = face->font ? face->font : FRAME_FONT (it->f);
25833 prepare_face_for_display (it->f, face);
25834 }
25835 #endif
25836
25837 /* List should start with `space'. */
25838 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25839 plist = XCDR (it->object);
25840
25841 /* Compute the width of the stretch. */
25842 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25843 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
25844 {
25845 /* Absolute width `:width WIDTH' specified and valid. */
25846 zero_width_ok_p = true;
25847 width = (int)tem;
25848 }
25849 #ifdef HAVE_WINDOW_SYSTEM
25850 else if (FRAME_WINDOW_P (it->f)
25851 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25852 {
25853 /* Relative width `:relative-width FACTOR' specified and valid.
25854 Compute the width of the characters having the `glyph'
25855 property. */
25856 struct it it2;
25857 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25858
25859 it2 = *it;
25860 if (it->multibyte_p)
25861 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25862 else
25863 {
25864 it2.c = it2.char_to_display = *p, it2.len = 1;
25865 if (! ASCII_CHAR_P (it2.c))
25866 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25867 }
25868
25869 it2.glyph_row = NULL;
25870 it2.what = IT_CHARACTER;
25871 x_produce_glyphs (&it2);
25872 width = NUMVAL (prop) * it2.pixel_width;
25873 }
25874 #endif /* HAVE_WINDOW_SYSTEM */
25875 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25876 && calc_pixel_width_or_height (&tem, it, prop, font, true,
25877 &align_to))
25878 {
25879 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25880 align_to = (align_to < 0
25881 ? 0
25882 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25883 else if (align_to < 0)
25884 align_to = window_box_left_offset (it->w, TEXT_AREA);
25885 width = max (0, (int)tem + align_to - it->current_x);
25886 zero_width_ok_p = true;
25887 }
25888 else
25889 /* Nothing specified -> width defaults to canonical char width. */
25890 width = FRAME_COLUMN_WIDTH (it->f);
25891
25892 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25893 width = 1;
25894
25895 #ifdef HAVE_WINDOW_SYSTEM
25896 /* Compute height. */
25897 if (FRAME_WINDOW_P (it->f))
25898 {
25899 int default_height = normal_char_height (font);
25900
25901 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25902 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25903 {
25904 height = (int)tem;
25905 zero_height_ok_p = true;
25906 }
25907 else if (prop = Fplist_get (plist, QCrelative_height),
25908 NUMVAL (prop) > 0)
25909 height = default_height * NUMVAL (prop);
25910 else
25911 height = default_height;
25912
25913 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25914 height = 1;
25915
25916 /* Compute percentage of height used for ascent. If
25917 `:ascent ASCENT' is present and valid, use that. Otherwise,
25918 derive the ascent from the font in use. */
25919 if (prop = Fplist_get (plist, QCascent),
25920 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25921 ascent = height * NUMVAL (prop) / 100.0;
25922 else if (!NILP (prop)
25923 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
25924 ascent = min (max (0, (int)tem), height);
25925 else
25926 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25927 }
25928 else
25929 #endif /* HAVE_WINDOW_SYSTEM */
25930 height = 1;
25931
25932 if (width > 0 && it->line_wrap != TRUNCATE
25933 && it->current_x + width > it->last_visible_x)
25934 {
25935 width = it->last_visible_x - it->current_x;
25936 #ifdef HAVE_WINDOW_SYSTEM
25937 /* Subtract one more pixel from the stretch width, but only on
25938 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25939 width -= FRAME_WINDOW_P (it->f);
25940 #endif
25941 }
25942
25943 if (width > 0 && height > 0 && it->glyph_row)
25944 {
25945 Lisp_Object o_object = it->object;
25946 Lisp_Object object = it->stack[it->sp - 1].string;
25947 int n = width;
25948
25949 if (!STRINGP (object))
25950 object = it->w->contents;
25951 #ifdef HAVE_WINDOW_SYSTEM
25952 if (FRAME_WINDOW_P (it->f))
25953 append_stretch_glyph (it, object, width, height, ascent);
25954 else
25955 #endif
25956 {
25957 it->object = object;
25958 it->char_to_display = ' ';
25959 it->pixel_width = it->len = 1;
25960 while (n--)
25961 tty_append_glyph (it);
25962 it->object = o_object;
25963 }
25964 }
25965
25966 it->pixel_width = width;
25967 #ifdef HAVE_WINDOW_SYSTEM
25968 if (FRAME_WINDOW_P (it->f))
25969 {
25970 it->ascent = it->phys_ascent = ascent;
25971 it->descent = it->phys_descent = height - it->ascent;
25972 it->nglyphs = width > 0 && height > 0;
25973 take_vertical_position_into_account (it);
25974 }
25975 else
25976 #endif
25977 it->nglyphs = width;
25978 }
25979
25980 /* Get information about special display element WHAT in an
25981 environment described by IT. WHAT is one of IT_TRUNCATION or
25982 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25983 non-null glyph_row member. This function ensures that fields like
25984 face_id, c, len of IT are left untouched. */
25985
25986 static void
25987 produce_special_glyphs (struct it *it, enum display_element_type what)
25988 {
25989 struct it temp_it;
25990 Lisp_Object gc;
25991 GLYPH glyph;
25992
25993 temp_it = *it;
25994 temp_it.object = Qnil;
25995 memset (&temp_it.current, 0, sizeof temp_it.current);
25996
25997 if (what == IT_CONTINUATION)
25998 {
25999 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26000 if (it->bidi_it.paragraph_dir == R2L)
26001 SET_GLYPH_FROM_CHAR (glyph, '/');
26002 else
26003 SET_GLYPH_FROM_CHAR (glyph, '\\');
26004 if (it->dp
26005 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26006 {
26007 /* FIXME: Should we mirror GC for R2L lines? */
26008 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26009 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26010 }
26011 }
26012 else if (what == IT_TRUNCATION)
26013 {
26014 /* Truncation glyph. */
26015 SET_GLYPH_FROM_CHAR (glyph, '$');
26016 if (it->dp
26017 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26018 {
26019 /* FIXME: Should we mirror GC for R2L lines? */
26020 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26021 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26022 }
26023 }
26024 else
26025 emacs_abort ();
26026
26027 #ifdef HAVE_WINDOW_SYSTEM
26028 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26029 is turned off, we precede the truncation/continuation glyphs by a
26030 stretch glyph whose width is computed such that these special
26031 glyphs are aligned at the window margin, even when very different
26032 fonts are used in different glyph rows. */
26033 if (FRAME_WINDOW_P (temp_it.f)
26034 /* init_iterator calls this with it->glyph_row == NULL, and it
26035 wants only the pixel width of the truncation/continuation
26036 glyphs. */
26037 && temp_it.glyph_row
26038 /* insert_left_trunc_glyphs calls us at the beginning of the
26039 row, and it has its own calculation of the stretch glyph
26040 width. */
26041 && temp_it.glyph_row->used[TEXT_AREA] > 0
26042 && (temp_it.glyph_row->reversed_p
26043 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26044 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26045 {
26046 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26047
26048 if (stretch_width > 0)
26049 {
26050 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26051 struct font *font =
26052 face->font ? face->font : FRAME_FONT (temp_it.f);
26053 int stretch_ascent =
26054 (((temp_it.ascent + temp_it.descent)
26055 * FONT_BASE (font)) / FONT_HEIGHT (font));
26056
26057 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26058 temp_it.ascent + temp_it.descent,
26059 stretch_ascent);
26060 }
26061 }
26062 #endif
26063
26064 temp_it.dp = NULL;
26065 temp_it.what = IT_CHARACTER;
26066 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26067 temp_it.face_id = GLYPH_FACE (glyph);
26068 temp_it.len = CHAR_BYTES (temp_it.c);
26069
26070 PRODUCE_GLYPHS (&temp_it);
26071 it->pixel_width = temp_it.pixel_width;
26072 it->nglyphs = temp_it.nglyphs;
26073 }
26074
26075 #ifdef HAVE_WINDOW_SYSTEM
26076
26077 /* Calculate line-height and line-spacing properties.
26078 An integer value specifies explicit pixel value.
26079 A float value specifies relative value to current face height.
26080 A cons (float . face-name) specifies relative value to
26081 height of specified face font.
26082
26083 Returns height in pixels, or nil. */
26084
26085 static Lisp_Object
26086 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26087 int boff, bool override)
26088 {
26089 Lisp_Object face_name = Qnil;
26090 int ascent, descent, height;
26091
26092 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26093 return val;
26094
26095 if (CONSP (val))
26096 {
26097 face_name = XCAR (val);
26098 val = XCDR (val);
26099 if (!NUMBERP (val))
26100 val = make_number (1);
26101 if (NILP (face_name))
26102 {
26103 height = it->ascent + it->descent;
26104 goto scale;
26105 }
26106 }
26107
26108 if (NILP (face_name))
26109 {
26110 font = FRAME_FONT (it->f);
26111 boff = FRAME_BASELINE_OFFSET (it->f);
26112 }
26113 else if (EQ (face_name, Qt))
26114 {
26115 override = false;
26116 }
26117 else
26118 {
26119 int face_id;
26120 struct face *face;
26121
26122 face_id = lookup_named_face (it->f, face_name, false);
26123 if (face_id < 0)
26124 return make_number (-1);
26125
26126 face = FACE_FROM_ID (it->f, face_id);
26127 font = face->font;
26128 if (font == NULL)
26129 return make_number (-1);
26130 boff = font->baseline_offset;
26131 if (font->vertical_centering)
26132 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26133 }
26134
26135 ascent = FONT_BASE (font) + boff;
26136 descent = FONT_DESCENT (font) - boff;
26137
26138 if (override)
26139 {
26140 it->override_ascent = ascent;
26141 it->override_descent = descent;
26142 it->override_boff = boff;
26143 }
26144
26145 height = ascent + descent;
26146
26147 scale:
26148 if (FLOATP (val))
26149 height = (int)(XFLOAT_DATA (val) * height);
26150 else if (INTEGERP (val))
26151 height *= XINT (val);
26152
26153 return make_number (height);
26154 }
26155
26156
26157 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26158 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26159 and only if this is for a character for which no font was found.
26160
26161 If the display method (it->glyphless_method) is
26162 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26163 length of the acronym or the hexadecimal string, UPPER_XOFF and
26164 UPPER_YOFF are pixel offsets for the upper part of the string,
26165 LOWER_XOFF and LOWER_YOFF are for the lower part.
26166
26167 For the other display methods, LEN through LOWER_YOFF are zero. */
26168
26169 static void
26170 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26171 short upper_xoff, short upper_yoff,
26172 short lower_xoff, short lower_yoff)
26173 {
26174 struct glyph *glyph;
26175 enum glyph_row_area area = it->area;
26176
26177 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26178 if (glyph < it->glyph_row->glyphs[area + 1])
26179 {
26180 /* If the glyph row is reversed, we need to prepend the glyph
26181 rather than append it. */
26182 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26183 {
26184 struct glyph *g;
26185
26186 /* Make room for the additional glyph. */
26187 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26188 g[1] = *g;
26189 glyph = it->glyph_row->glyphs[area];
26190 }
26191 glyph->charpos = CHARPOS (it->position);
26192 glyph->object = it->object;
26193 glyph->pixel_width = it->pixel_width;
26194 glyph->ascent = it->ascent;
26195 glyph->descent = it->descent;
26196 glyph->voffset = it->voffset;
26197 glyph->type = GLYPHLESS_GLYPH;
26198 glyph->u.glyphless.method = it->glyphless_method;
26199 glyph->u.glyphless.for_no_font = for_no_font;
26200 glyph->u.glyphless.len = len;
26201 glyph->u.glyphless.ch = it->c;
26202 glyph->slice.glyphless.upper_xoff = upper_xoff;
26203 glyph->slice.glyphless.upper_yoff = upper_yoff;
26204 glyph->slice.glyphless.lower_xoff = lower_xoff;
26205 glyph->slice.glyphless.lower_yoff = lower_yoff;
26206 glyph->avoid_cursor_p = it->avoid_cursor_p;
26207 glyph->multibyte_p = it->multibyte_p;
26208 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26209 {
26210 /* In R2L rows, the left and the right box edges need to be
26211 drawn in reverse direction. */
26212 glyph->right_box_line_p = it->start_of_box_run_p;
26213 glyph->left_box_line_p = it->end_of_box_run_p;
26214 }
26215 else
26216 {
26217 glyph->left_box_line_p = it->start_of_box_run_p;
26218 glyph->right_box_line_p = it->end_of_box_run_p;
26219 }
26220 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26221 || it->phys_descent > it->descent);
26222 glyph->padding_p = false;
26223 glyph->glyph_not_available_p = false;
26224 glyph->face_id = face_id;
26225 glyph->font_type = FONT_TYPE_UNKNOWN;
26226 if (it->bidi_p)
26227 {
26228 glyph->resolved_level = it->bidi_it.resolved_level;
26229 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26230 glyph->bidi_type = it->bidi_it.type;
26231 }
26232 ++it->glyph_row->used[area];
26233 }
26234 else
26235 IT_EXPAND_MATRIX_WIDTH (it, area);
26236 }
26237
26238
26239 /* Produce a glyph for a glyphless character for iterator IT.
26240 IT->glyphless_method specifies which method to use for displaying
26241 the character. See the description of enum
26242 glyphless_display_method in dispextern.h for the detail.
26243
26244 FOR_NO_FONT is true if and only if this is for a character for
26245 which no font was found. ACRONYM, if non-nil, is an acronym string
26246 for the character. */
26247
26248 static void
26249 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26250 {
26251 int face_id;
26252 struct face *face;
26253 struct font *font;
26254 int base_width, base_height, width, height;
26255 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26256 int len;
26257
26258 /* Get the metrics of the base font. We always refer to the current
26259 ASCII face. */
26260 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26261 font = face->font ? face->font : FRAME_FONT (it->f);
26262 it->ascent = FONT_BASE (font);
26263 it->descent = FONT_DESCENT (font);
26264 /* Attempt to fix box height for fonts that claim preposterously
26265 large height. */
26266 if (FONT_TOO_HIGH (font))
26267 {
26268 XChar2b char2b;
26269
26270 /* Get metrics of a reasonably sized ASCII character. */
26271 if (get_char_glyph_code ('{', font, &char2b))
26272 {
26273 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
26274
26275 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
26276 {
26277 it->ascent = pcm->ascent;
26278 it->descent = pcm->descent;
26279 }
26280 }
26281 }
26282 it->ascent += font->baseline_offset;
26283 it->descent -= font->baseline_offset;
26284 base_height = it->ascent + it->descent;
26285 base_width = font->average_width;
26286
26287 face_id = merge_glyphless_glyph_face (it);
26288
26289 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26290 {
26291 it->pixel_width = THIN_SPACE_WIDTH;
26292 len = 0;
26293 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26294 }
26295 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26296 {
26297 width = CHAR_WIDTH (it->c);
26298 if (width == 0)
26299 width = 1;
26300 else if (width > 4)
26301 width = 4;
26302 it->pixel_width = base_width * width;
26303 len = 0;
26304 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26305 }
26306 else
26307 {
26308 char buf[7];
26309 const char *str;
26310 unsigned int code[6];
26311 int upper_len;
26312 int ascent, descent;
26313 struct font_metrics metrics_upper, metrics_lower;
26314
26315 face = FACE_FROM_ID (it->f, face_id);
26316 font = face->font ? face->font : FRAME_FONT (it->f);
26317 prepare_face_for_display (it->f, face);
26318
26319 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26320 {
26321 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26322 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26323 if (CONSP (acronym))
26324 acronym = XCAR (acronym);
26325 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26326 }
26327 else
26328 {
26329 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26330 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26331 str = buf;
26332 }
26333 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26334 code[len] = font->driver->encode_char (font, str[len]);
26335 upper_len = (len + 1) / 2;
26336 font->driver->text_extents (font, code, upper_len,
26337 &metrics_upper);
26338 font->driver->text_extents (font, code + upper_len, len - upper_len,
26339 &metrics_lower);
26340
26341
26342
26343 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26344 width = max (metrics_upper.width, metrics_lower.width) + 4;
26345 upper_xoff = upper_yoff = 2; /* the typical case */
26346 if (base_width >= width)
26347 {
26348 /* Align the upper to the left, the lower to the right. */
26349 it->pixel_width = base_width;
26350 lower_xoff = base_width - 2 - metrics_lower.width;
26351 }
26352 else
26353 {
26354 /* Center the shorter one. */
26355 it->pixel_width = width;
26356 if (metrics_upper.width >= metrics_lower.width)
26357 lower_xoff = (width - metrics_lower.width) / 2;
26358 else
26359 {
26360 /* FIXME: This code doesn't look right. It formerly was
26361 missing the "lower_xoff = 0;", which couldn't have
26362 been right since it left lower_xoff uninitialized. */
26363 lower_xoff = 0;
26364 upper_xoff = (width - metrics_upper.width) / 2;
26365 }
26366 }
26367
26368 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26369 top, bottom, and between upper and lower strings. */
26370 height = (metrics_upper.ascent + metrics_upper.descent
26371 + metrics_lower.ascent + metrics_lower.descent) + 5;
26372 /* Center vertically.
26373 H:base_height, D:base_descent
26374 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26375
26376 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26377 descent = D - H/2 + h/2;
26378 lower_yoff = descent - 2 - ld;
26379 upper_yoff = lower_yoff - la - 1 - ud; */
26380 ascent = - (it->descent - (base_height + height + 1) / 2);
26381 descent = it->descent - (base_height - height) / 2;
26382 lower_yoff = descent - 2 - metrics_lower.descent;
26383 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26384 - metrics_upper.descent);
26385 /* Don't make the height shorter than the base height. */
26386 if (height > base_height)
26387 {
26388 it->ascent = ascent;
26389 it->descent = descent;
26390 }
26391 }
26392
26393 it->phys_ascent = it->ascent;
26394 it->phys_descent = it->descent;
26395 if (it->glyph_row)
26396 append_glyphless_glyph (it, face_id, for_no_font, len,
26397 upper_xoff, upper_yoff,
26398 lower_xoff, lower_yoff);
26399 it->nglyphs = 1;
26400 take_vertical_position_into_account (it);
26401 }
26402
26403
26404 /* RIF:
26405 Produce glyphs/get display metrics for the display element IT is
26406 loaded with. See the description of struct it in dispextern.h
26407 for an overview of struct it. */
26408
26409 void
26410 x_produce_glyphs (struct it *it)
26411 {
26412 int extra_line_spacing = it->extra_line_spacing;
26413
26414 it->glyph_not_available_p = false;
26415
26416 if (it->what == IT_CHARACTER)
26417 {
26418 XChar2b char2b;
26419 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26420 struct font *font = face->font;
26421 struct font_metrics *pcm = NULL;
26422 int boff; /* Baseline offset. */
26423
26424 if (font == NULL)
26425 {
26426 /* When no suitable font is found, display this character by
26427 the method specified in the first extra slot of
26428 Vglyphless_char_display. */
26429 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26430
26431 eassert (it->what == IT_GLYPHLESS);
26432 produce_glyphless_glyph (it, true,
26433 STRINGP (acronym) ? acronym : Qnil);
26434 goto done;
26435 }
26436
26437 boff = font->baseline_offset;
26438 if (font->vertical_centering)
26439 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26440
26441 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26442 {
26443 it->nglyphs = 1;
26444
26445 if (it->override_ascent >= 0)
26446 {
26447 it->ascent = it->override_ascent;
26448 it->descent = it->override_descent;
26449 boff = it->override_boff;
26450 }
26451 else
26452 {
26453 it->ascent = FONT_BASE (font) + boff;
26454 it->descent = FONT_DESCENT (font) - boff;
26455 }
26456
26457 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26458 {
26459 pcm = get_per_char_metric (font, &char2b);
26460 if (pcm->width == 0
26461 && pcm->rbearing == 0 && pcm->lbearing == 0)
26462 pcm = NULL;
26463 }
26464
26465 if (pcm)
26466 {
26467 it->phys_ascent = pcm->ascent + boff;
26468 it->phys_descent = pcm->descent - boff;
26469 it->pixel_width = pcm->width;
26470 /* Don't use font-global values for ascent and descent
26471 if they result in an exceedingly large line height. */
26472 if (it->override_ascent < 0)
26473 {
26474 if (FONT_TOO_HIGH (font))
26475 {
26476 it->ascent = it->phys_ascent;
26477 it->descent = it->phys_descent;
26478 /* These limitations are enforced by an
26479 assertion near the end of this function. */
26480 if (it->ascent < 0)
26481 it->ascent = 0;
26482 if (it->descent < 0)
26483 it->descent = 0;
26484 }
26485 }
26486 }
26487 else
26488 {
26489 it->glyph_not_available_p = true;
26490 it->phys_ascent = it->ascent;
26491 it->phys_descent = it->descent;
26492 it->pixel_width = font->space_width;
26493 }
26494
26495 if (it->constrain_row_ascent_descent_p)
26496 {
26497 if (it->descent > it->max_descent)
26498 {
26499 it->ascent += it->descent - it->max_descent;
26500 it->descent = it->max_descent;
26501 }
26502 if (it->ascent > it->max_ascent)
26503 {
26504 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26505 it->ascent = it->max_ascent;
26506 }
26507 it->phys_ascent = min (it->phys_ascent, it->ascent);
26508 it->phys_descent = min (it->phys_descent, it->descent);
26509 extra_line_spacing = 0;
26510 }
26511
26512 /* If this is a space inside a region of text with
26513 `space-width' property, change its width. */
26514 bool stretched_p
26515 = it->char_to_display == ' ' && !NILP (it->space_width);
26516 if (stretched_p)
26517 it->pixel_width *= XFLOATINT (it->space_width);
26518
26519 /* If face has a box, add the box thickness to the character
26520 height. If character has a box line to the left and/or
26521 right, add the box line width to the character's width. */
26522 if (face->box != FACE_NO_BOX)
26523 {
26524 int thick = face->box_line_width;
26525
26526 if (thick > 0)
26527 {
26528 it->ascent += thick;
26529 it->descent += thick;
26530 }
26531 else
26532 thick = -thick;
26533
26534 if (it->start_of_box_run_p)
26535 it->pixel_width += thick;
26536 if (it->end_of_box_run_p)
26537 it->pixel_width += thick;
26538 }
26539
26540 /* If face has an overline, add the height of the overline
26541 (1 pixel) and a 1 pixel margin to the character height. */
26542 if (face->overline_p)
26543 it->ascent += overline_margin;
26544
26545 if (it->constrain_row_ascent_descent_p)
26546 {
26547 if (it->ascent > it->max_ascent)
26548 it->ascent = it->max_ascent;
26549 if (it->descent > it->max_descent)
26550 it->descent = it->max_descent;
26551 }
26552
26553 take_vertical_position_into_account (it);
26554
26555 /* If we have to actually produce glyphs, do it. */
26556 if (it->glyph_row)
26557 {
26558 if (stretched_p)
26559 {
26560 /* Translate a space with a `space-width' property
26561 into a stretch glyph. */
26562 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26563 / FONT_HEIGHT (font));
26564 append_stretch_glyph (it, it->object, it->pixel_width,
26565 it->ascent + it->descent, ascent);
26566 }
26567 else
26568 append_glyph (it);
26569
26570 /* If characters with lbearing or rbearing are displayed
26571 in this line, record that fact in a flag of the
26572 glyph row. This is used to optimize X output code. */
26573 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26574 it->glyph_row->contains_overlapping_glyphs_p = true;
26575 }
26576 if (! stretched_p && it->pixel_width == 0)
26577 /* We assure that all visible glyphs have at least 1-pixel
26578 width. */
26579 it->pixel_width = 1;
26580 }
26581 else if (it->char_to_display == '\n')
26582 {
26583 /* A newline has no width, but we need the height of the
26584 line. But if previous part of the line sets a height,
26585 don't increase that height. */
26586
26587 Lisp_Object height;
26588 Lisp_Object total_height = Qnil;
26589
26590 it->override_ascent = -1;
26591 it->pixel_width = 0;
26592 it->nglyphs = 0;
26593
26594 height = get_it_property (it, Qline_height);
26595 /* Split (line-height total-height) list. */
26596 if (CONSP (height)
26597 && CONSP (XCDR (height))
26598 && NILP (XCDR (XCDR (height))))
26599 {
26600 total_height = XCAR (XCDR (height));
26601 height = XCAR (height);
26602 }
26603 height = calc_line_height_property (it, height, font, boff, true);
26604
26605 if (it->override_ascent >= 0)
26606 {
26607 it->ascent = it->override_ascent;
26608 it->descent = it->override_descent;
26609 boff = it->override_boff;
26610 }
26611 else
26612 {
26613 if (FONT_TOO_HIGH (font))
26614 {
26615 it->ascent = font->pixel_size + boff - 1;
26616 it->descent = -boff + 1;
26617 if (it->descent < 0)
26618 it->descent = 0;
26619 }
26620 else
26621 {
26622 it->ascent = FONT_BASE (font) + boff;
26623 it->descent = FONT_DESCENT (font) - boff;
26624 }
26625 }
26626
26627 if (EQ (height, Qt))
26628 {
26629 if (it->descent > it->max_descent)
26630 {
26631 it->ascent += it->descent - it->max_descent;
26632 it->descent = it->max_descent;
26633 }
26634 if (it->ascent > it->max_ascent)
26635 {
26636 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26637 it->ascent = it->max_ascent;
26638 }
26639 it->phys_ascent = min (it->phys_ascent, it->ascent);
26640 it->phys_descent = min (it->phys_descent, it->descent);
26641 it->constrain_row_ascent_descent_p = true;
26642 extra_line_spacing = 0;
26643 }
26644 else
26645 {
26646 Lisp_Object spacing;
26647
26648 it->phys_ascent = it->ascent;
26649 it->phys_descent = it->descent;
26650
26651 if ((it->max_ascent > 0 || it->max_descent > 0)
26652 && face->box != FACE_NO_BOX
26653 && face->box_line_width > 0)
26654 {
26655 it->ascent += face->box_line_width;
26656 it->descent += face->box_line_width;
26657 }
26658 if (!NILP (height)
26659 && XINT (height) > it->ascent + it->descent)
26660 it->ascent = XINT (height) - it->descent;
26661
26662 if (!NILP (total_height))
26663 spacing = calc_line_height_property (it, total_height, font,
26664 boff, false);
26665 else
26666 {
26667 spacing = get_it_property (it, Qline_spacing);
26668 spacing = calc_line_height_property (it, spacing, font,
26669 boff, false);
26670 }
26671 if (INTEGERP (spacing))
26672 {
26673 extra_line_spacing = XINT (spacing);
26674 if (!NILP (total_height))
26675 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26676 }
26677 }
26678 }
26679 else /* i.e. (it->char_to_display == '\t') */
26680 {
26681 if (font->space_width > 0)
26682 {
26683 int tab_width = it->tab_width * font->space_width;
26684 int x = it->current_x + it->continuation_lines_width;
26685 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26686
26687 /* If the distance from the current position to the next tab
26688 stop is less than a space character width, use the
26689 tab stop after that. */
26690 if (next_tab_x - x < font->space_width)
26691 next_tab_x += tab_width;
26692
26693 it->pixel_width = next_tab_x - x;
26694 it->nglyphs = 1;
26695 if (FONT_TOO_HIGH (font))
26696 {
26697 if (get_char_glyph_code (' ', font, &char2b))
26698 {
26699 pcm = get_per_char_metric (font, &char2b);
26700 if (pcm->width == 0
26701 && pcm->rbearing == 0 && pcm->lbearing == 0)
26702 pcm = NULL;
26703 }
26704
26705 if (pcm)
26706 {
26707 it->ascent = pcm->ascent + boff;
26708 it->descent = pcm->descent - boff;
26709 }
26710 else
26711 {
26712 it->ascent = font->pixel_size + boff - 1;
26713 it->descent = -boff + 1;
26714 }
26715 if (it->ascent < 0)
26716 it->ascent = 0;
26717 if (it->descent < 0)
26718 it->descent = 0;
26719 }
26720 else
26721 {
26722 it->ascent = FONT_BASE (font) + boff;
26723 it->descent = FONT_DESCENT (font) - boff;
26724 }
26725 it->phys_ascent = it->ascent;
26726 it->phys_descent = it->descent;
26727
26728 if (it->glyph_row)
26729 {
26730 append_stretch_glyph (it, it->object, it->pixel_width,
26731 it->ascent + it->descent, it->ascent);
26732 }
26733 }
26734 else
26735 {
26736 it->pixel_width = 0;
26737 it->nglyphs = 1;
26738 }
26739 }
26740 }
26741 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26742 {
26743 /* A static composition.
26744
26745 Note: A composition is represented as one glyph in the
26746 glyph matrix. There are no padding glyphs.
26747
26748 Important note: pixel_width, ascent, and descent are the
26749 values of what is drawn by draw_glyphs (i.e. the values of
26750 the overall glyphs composed). */
26751 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26752 int boff; /* baseline offset */
26753 struct composition *cmp = composition_table[it->cmp_it.id];
26754 int glyph_len = cmp->glyph_len;
26755 struct font *font = face->font;
26756
26757 it->nglyphs = 1;
26758
26759 /* If we have not yet calculated pixel size data of glyphs of
26760 the composition for the current face font, calculate them
26761 now. Theoretically, we have to check all fonts for the
26762 glyphs, but that requires much time and memory space. So,
26763 here we check only the font of the first glyph. This may
26764 lead to incorrect display, but it's very rare, and C-l
26765 (recenter-top-bottom) can correct the display anyway. */
26766 if (! cmp->font || cmp->font != font)
26767 {
26768 /* Ascent and descent of the font of the first character
26769 of this composition (adjusted by baseline offset).
26770 Ascent and descent of overall glyphs should not be less
26771 than these, respectively. */
26772 int font_ascent, font_descent, font_height;
26773 /* Bounding box of the overall glyphs. */
26774 int leftmost, rightmost, lowest, highest;
26775 int lbearing, rbearing;
26776 int i, width, ascent, descent;
26777 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26778 XChar2b char2b;
26779 struct font_metrics *pcm;
26780 ptrdiff_t pos;
26781
26782 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26783 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26784 break;
26785 bool right_padded = glyph_len < cmp->glyph_len;
26786 for (i = 0; i < glyph_len; i++)
26787 {
26788 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26789 break;
26790 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26791 }
26792 bool left_padded = i > 0;
26793
26794 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26795 : IT_CHARPOS (*it));
26796 /* If no suitable font is found, use the default font. */
26797 bool font_not_found_p = font == NULL;
26798 if (font_not_found_p)
26799 {
26800 face = face->ascii_face;
26801 font = face->font;
26802 }
26803 boff = font->baseline_offset;
26804 if (font->vertical_centering)
26805 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26806 font_ascent = FONT_BASE (font) + boff;
26807 font_descent = FONT_DESCENT (font) - boff;
26808 font_height = FONT_HEIGHT (font);
26809
26810 cmp->font = font;
26811
26812 pcm = NULL;
26813 if (! font_not_found_p)
26814 {
26815 get_char_face_and_encoding (it->f, c, it->face_id,
26816 &char2b, false);
26817 pcm = get_per_char_metric (font, &char2b);
26818 }
26819
26820 /* Initialize the bounding box. */
26821 if (pcm)
26822 {
26823 width = cmp->glyph_len > 0 ? pcm->width : 0;
26824 ascent = pcm->ascent;
26825 descent = pcm->descent;
26826 lbearing = pcm->lbearing;
26827 rbearing = pcm->rbearing;
26828 }
26829 else
26830 {
26831 width = cmp->glyph_len > 0 ? font->space_width : 0;
26832 ascent = FONT_BASE (font);
26833 descent = FONT_DESCENT (font);
26834 lbearing = 0;
26835 rbearing = width;
26836 }
26837
26838 rightmost = width;
26839 leftmost = 0;
26840 lowest = - descent + boff;
26841 highest = ascent + boff;
26842
26843 if (! font_not_found_p
26844 && font->default_ascent
26845 && CHAR_TABLE_P (Vuse_default_ascent)
26846 && !NILP (Faref (Vuse_default_ascent,
26847 make_number (it->char_to_display))))
26848 highest = font->default_ascent + boff;
26849
26850 /* Draw the first glyph at the normal position. It may be
26851 shifted to right later if some other glyphs are drawn
26852 at the left. */
26853 cmp->offsets[i * 2] = 0;
26854 cmp->offsets[i * 2 + 1] = boff;
26855 cmp->lbearing = lbearing;
26856 cmp->rbearing = rbearing;
26857
26858 /* Set cmp->offsets for the remaining glyphs. */
26859 for (i++; i < glyph_len; i++)
26860 {
26861 int left, right, btm, top;
26862 int ch = COMPOSITION_GLYPH (cmp, i);
26863 int face_id;
26864 struct face *this_face;
26865
26866 if (ch == '\t')
26867 ch = ' ';
26868 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26869 this_face = FACE_FROM_ID (it->f, face_id);
26870 font = this_face->font;
26871
26872 if (font == NULL)
26873 pcm = NULL;
26874 else
26875 {
26876 get_char_face_and_encoding (it->f, ch, face_id,
26877 &char2b, false);
26878 pcm = get_per_char_metric (font, &char2b);
26879 }
26880 if (! pcm)
26881 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26882 else
26883 {
26884 width = pcm->width;
26885 ascent = pcm->ascent;
26886 descent = pcm->descent;
26887 lbearing = pcm->lbearing;
26888 rbearing = pcm->rbearing;
26889 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26890 {
26891 /* Relative composition with or without
26892 alternate chars. */
26893 left = (leftmost + rightmost - width) / 2;
26894 btm = - descent + boff;
26895 if (font->relative_compose
26896 && (! CHAR_TABLE_P (Vignore_relative_composition)
26897 || NILP (Faref (Vignore_relative_composition,
26898 make_number (ch)))))
26899 {
26900
26901 if (- descent >= font->relative_compose)
26902 /* One extra pixel between two glyphs. */
26903 btm = highest + 1;
26904 else if (ascent <= 0)
26905 /* One extra pixel between two glyphs. */
26906 btm = lowest - 1 - ascent - descent;
26907 }
26908 }
26909 else
26910 {
26911 /* A composition rule is specified by an integer
26912 value that encodes global and new reference
26913 points (GREF and NREF). GREF and NREF are
26914 specified by numbers as below:
26915
26916 0---1---2 -- ascent
26917 | |
26918 | |
26919 | |
26920 9--10--11 -- center
26921 | |
26922 ---3---4---5--- baseline
26923 | |
26924 6---7---8 -- descent
26925 */
26926 int rule = COMPOSITION_RULE (cmp, i);
26927 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26928
26929 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26930 grefx = gref % 3, nrefx = nref % 3;
26931 grefy = gref / 3, nrefy = nref / 3;
26932 if (xoff)
26933 xoff = font_height * (xoff - 128) / 256;
26934 if (yoff)
26935 yoff = font_height * (yoff - 128) / 256;
26936
26937 left = (leftmost
26938 + grefx * (rightmost - leftmost) / 2
26939 - nrefx * width / 2
26940 + xoff);
26941
26942 btm = ((grefy == 0 ? highest
26943 : grefy == 1 ? 0
26944 : grefy == 2 ? lowest
26945 : (highest + lowest) / 2)
26946 - (nrefy == 0 ? ascent + descent
26947 : nrefy == 1 ? descent - boff
26948 : nrefy == 2 ? 0
26949 : (ascent + descent) / 2)
26950 + yoff);
26951 }
26952
26953 cmp->offsets[i * 2] = left;
26954 cmp->offsets[i * 2 + 1] = btm + descent;
26955
26956 /* Update the bounding box of the overall glyphs. */
26957 if (width > 0)
26958 {
26959 right = left + width;
26960 if (left < leftmost)
26961 leftmost = left;
26962 if (right > rightmost)
26963 rightmost = right;
26964 }
26965 top = btm + descent + ascent;
26966 if (top > highest)
26967 highest = top;
26968 if (btm < lowest)
26969 lowest = btm;
26970
26971 if (cmp->lbearing > left + lbearing)
26972 cmp->lbearing = left + lbearing;
26973 if (cmp->rbearing < left + rbearing)
26974 cmp->rbearing = left + rbearing;
26975 }
26976 }
26977
26978 /* If there are glyphs whose x-offsets are negative,
26979 shift all glyphs to the right and make all x-offsets
26980 non-negative. */
26981 if (leftmost < 0)
26982 {
26983 for (i = 0; i < cmp->glyph_len; i++)
26984 cmp->offsets[i * 2] -= leftmost;
26985 rightmost -= leftmost;
26986 cmp->lbearing -= leftmost;
26987 cmp->rbearing -= leftmost;
26988 }
26989
26990 if (left_padded && cmp->lbearing < 0)
26991 {
26992 for (i = 0; i < cmp->glyph_len; i++)
26993 cmp->offsets[i * 2] -= cmp->lbearing;
26994 rightmost -= cmp->lbearing;
26995 cmp->rbearing -= cmp->lbearing;
26996 cmp->lbearing = 0;
26997 }
26998 if (right_padded && rightmost < cmp->rbearing)
26999 {
27000 rightmost = cmp->rbearing;
27001 }
27002
27003 cmp->pixel_width = rightmost;
27004 cmp->ascent = highest;
27005 cmp->descent = - lowest;
27006 if (cmp->ascent < font_ascent)
27007 cmp->ascent = font_ascent;
27008 if (cmp->descent < font_descent)
27009 cmp->descent = font_descent;
27010 }
27011
27012 if (it->glyph_row
27013 && (cmp->lbearing < 0
27014 || cmp->rbearing > cmp->pixel_width))
27015 it->glyph_row->contains_overlapping_glyphs_p = true;
27016
27017 it->pixel_width = cmp->pixel_width;
27018 it->ascent = it->phys_ascent = cmp->ascent;
27019 it->descent = it->phys_descent = cmp->descent;
27020 if (face->box != FACE_NO_BOX)
27021 {
27022 int thick = face->box_line_width;
27023
27024 if (thick > 0)
27025 {
27026 it->ascent += thick;
27027 it->descent += thick;
27028 }
27029 else
27030 thick = - thick;
27031
27032 if (it->start_of_box_run_p)
27033 it->pixel_width += thick;
27034 if (it->end_of_box_run_p)
27035 it->pixel_width += thick;
27036 }
27037
27038 /* If face has an overline, add the height of the overline
27039 (1 pixel) and a 1 pixel margin to the character height. */
27040 if (face->overline_p)
27041 it->ascent += overline_margin;
27042
27043 take_vertical_position_into_account (it);
27044 if (it->ascent < 0)
27045 it->ascent = 0;
27046 if (it->descent < 0)
27047 it->descent = 0;
27048
27049 if (it->glyph_row && cmp->glyph_len > 0)
27050 append_composite_glyph (it);
27051 }
27052 else if (it->what == IT_COMPOSITION)
27053 {
27054 /* A dynamic (automatic) composition. */
27055 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27056 Lisp_Object gstring;
27057 struct font_metrics metrics;
27058
27059 it->nglyphs = 1;
27060
27061 gstring = composition_gstring_from_id (it->cmp_it.id);
27062 it->pixel_width
27063 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27064 &metrics);
27065 if (it->glyph_row
27066 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27067 it->glyph_row->contains_overlapping_glyphs_p = true;
27068 it->ascent = it->phys_ascent = metrics.ascent;
27069 it->descent = it->phys_descent = metrics.descent;
27070 if (face->box != FACE_NO_BOX)
27071 {
27072 int thick = face->box_line_width;
27073
27074 if (thick > 0)
27075 {
27076 it->ascent += thick;
27077 it->descent += thick;
27078 }
27079 else
27080 thick = - thick;
27081
27082 if (it->start_of_box_run_p)
27083 it->pixel_width += thick;
27084 if (it->end_of_box_run_p)
27085 it->pixel_width += thick;
27086 }
27087 /* If face has an overline, add the height of the overline
27088 (1 pixel) and a 1 pixel margin to the character height. */
27089 if (face->overline_p)
27090 it->ascent += overline_margin;
27091 take_vertical_position_into_account (it);
27092 if (it->ascent < 0)
27093 it->ascent = 0;
27094 if (it->descent < 0)
27095 it->descent = 0;
27096
27097 if (it->glyph_row)
27098 append_composite_glyph (it);
27099 }
27100 else if (it->what == IT_GLYPHLESS)
27101 produce_glyphless_glyph (it, false, Qnil);
27102 else if (it->what == IT_IMAGE)
27103 produce_image_glyph (it);
27104 else if (it->what == IT_STRETCH)
27105 produce_stretch_glyph (it);
27106
27107 done:
27108 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27109 because this isn't true for images with `:ascent 100'. */
27110 eassert (it->ascent >= 0 && it->descent >= 0);
27111 if (it->area == TEXT_AREA)
27112 it->current_x += it->pixel_width;
27113
27114 if (extra_line_spacing > 0)
27115 {
27116 it->descent += extra_line_spacing;
27117 if (extra_line_spacing > it->max_extra_line_spacing)
27118 it->max_extra_line_spacing = extra_line_spacing;
27119 }
27120
27121 it->max_ascent = max (it->max_ascent, it->ascent);
27122 it->max_descent = max (it->max_descent, it->descent);
27123 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27124 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27125 }
27126
27127 /* EXPORT for RIF:
27128 Output LEN glyphs starting at START at the nominal cursor position.
27129 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27130 being updated, and UPDATED_AREA is the area of that row being updated. */
27131
27132 void
27133 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27134 struct glyph *start, enum glyph_row_area updated_area, int len)
27135 {
27136 int x, hpos, chpos = w->phys_cursor.hpos;
27137
27138 eassert (updated_row);
27139 /* When the window is hscrolled, cursor hpos can legitimately be out
27140 of bounds, but we draw the cursor at the corresponding window
27141 margin in that case. */
27142 if (!updated_row->reversed_p && chpos < 0)
27143 chpos = 0;
27144 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27145 chpos = updated_row->used[TEXT_AREA] - 1;
27146
27147 block_input ();
27148
27149 /* Write glyphs. */
27150
27151 hpos = start - updated_row->glyphs[updated_area];
27152 x = draw_glyphs (w, w->output_cursor.x,
27153 updated_row, updated_area,
27154 hpos, hpos + len,
27155 DRAW_NORMAL_TEXT, 0);
27156
27157 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27158 if (updated_area == TEXT_AREA
27159 && w->phys_cursor_on_p
27160 && w->phys_cursor.vpos == w->output_cursor.vpos
27161 && chpos >= hpos
27162 && chpos < hpos + len)
27163 w->phys_cursor_on_p = false;
27164
27165 unblock_input ();
27166
27167 /* Advance the output cursor. */
27168 w->output_cursor.hpos += len;
27169 w->output_cursor.x = x;
27170 }
27171
27172
27173 /* EXPORT for RIF:
27174 Insert LEN glyphs from START at the nominal cursor position. */
27175
27176 void
27177 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27178 struct glyph *start, enum glyph_row_area updated_area, int len)
27179 {
27180 struct frame *f;
27181 int line_height, shift_by_width, shifted_region_width;
27182 struct glyph_row *row;
27183 struct glyph *glyph;
27184 int frame_x, frame_y;
27185 ptrdiff_t hpos;
27186
27187 eassert (updated_row);
27188 block_input ();
27189 f = XFRAME (WINDOW_FRAME (w));
27190
27191 /* Get the height of the line we are in. */
27192 row = updated_row;
27193 line_height = row->height;
27194
27195 /* Get the width of the glyphs to insert. */
27196 shift_by_width = 0;
27197 for (glyph = start; glyph < start + len; ++glyph)
27198 shift_by_width += glyph->pixel_width;
27199
27200 /* Get the width of the region to shift right. */
27201 shifted_region_width = (window_box_width (w, updated_area)
27202 - w->output_cursor.x
27203 - shift_by_width);
27204
27205 /* Shift right. */
27206 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27207 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27208
27209 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27210 line_height, shift_by_width);
27211
27212 /* Write the glyphs. */
27213 hpos = start - row->glyphs[updated_area];
27214 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27215 hpos, hpos + len,
27216 DRAW_NORMAL_TEXT, 0);
27217
27218 /* Advance the output cursor. */
27219 w->output_cursor.hpos += len;
27220 w->output_cursor.x += shift_by_width;
27221 unblock_input ();
27222 }
27223
27224
27225 /* EXPORT for RIF:
27226 Erase the current text line from the nominal cursor position
27227 (inclusive) to pixel column TO_X (exclusive). The idea is that
27228 everything from TO_X onward is already erased.
27229
27230 TO_X is a pixel position relative to UPDATED_AREA of currently
27231 updated window W. TO_X == -1 means clear to the end of this area. */
27232
27233 void
27234 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27235 enum glyph_row_area updated_area, int to_x)
27236 {
27237 struct frame *f;
27238 int max_x, min_y, max_y;
27239 int from_x, from_y, to_y;
27240
27241 eassert (updated_row);
27242 f = XFRAME (w->frame);
27243
27244 if (updated_row->full_width_p)
27245 max_x = (WINDOW_PIXEL_WIDTH (w)
27246 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27247 else
27248 max_x = window_box_width (w, updated_area);
27249 max_y = window_text_bottom_y (w);
27250
27251 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27252 of window. For TO_X > 0, truncate to end of drawing area. */
27253 if (to_x == 0)
27254 return;
27255 else if (to_x < 0)
27256 to_x = max_x;
27257 else
27258 to_x = min (to_x, max_x);
27259
27260 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27261
27262 /* Notice if the cursor will be cleared by this operation. */
27263 if (!updated_row->full_width_p)
27264 notice_overwritten_cursor (w, updated_area,
27265 w->output_cursor.x, -1,
27266 updated_row->y,
27267 MATRIX_ROW_BOTTOM_Y (updated_row));
27268
27269 from_x = w->output_cursor.x;
27270
27271 /* Translate to frame coordinates. */
27272 if (updated_row->full_width_p)
27273 {
27274 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27275 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27276 }
27277 else
27278 {
27279 int area_left = window_box_left (w, updated_area);
27280 from_x += area_left;
27281 to_x += area_left;
27282 }
27283
27284 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27285 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27286 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27287
27288 /* Prevent inadvertently clearing to end of the X window. */
27289 if (to_x > from_x && to_y > from_y)
27290 {
27291 block_input ();
27292 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27293 to_x - from_x, to_y - from_y);
27294 unblock_input ();
27295 }
27296 }
27297
27298 #endif /* HAVE_WINDOW_SYSTEM */
27299
27300
27301 \f
27302 /***********************************************************************
27303 Cursor types
27304 ***********************************************************************/
27305
27306 /* Value is the internal representation of the specified cursor type
27307 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27308 of the bar cursor. */
27309
27310 static enum text_cursor_kinds
27311 get_specified_cursor_type (Lisp_Object arg, int *width)
27312 {
27313 enum text_cursor_kinds type;
27314
27315 if (NILP (arg))
27316 return NO_CURSOR;
27317
27318 if (EQ (arg, Qbox))
27319 return FILLED_BOX_CURSOR;
27320
27321 if (EQ (arg, Qhollow))
27322 return HOLLOW_BOX_CURSOR;
27323
27324 if (EQ (arg, Qbar))
27325 {
27326 *width = 2;
27327 return BAR_CURSOR;
27328 }
27329
27330 if (CONSP (arg)
27331 && EQ (XCAR (arg), Qbar)
27332 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27333 {
27334 *width = XINT (XCDR (arg));
27335 return BAR_CURSOR;
27336 }
27337
27338 if (EQ (arg, Qhbar))
27339 {
27340 *width = 2;
27341 return HBAR_CURSOR;
27342 }
27343
27344 if (CONSP (arg)
27345 && EQ (XCAR (arg), Qhbar)
27346 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27347 {
27348 *width = XINT (XCDR (arg));
27349 return HBAR_CURSOR;
27350 }
27351
27352 /* Treat anything unknown as "hollow box cursor".
27353 It was bad to signal an error; people have trouble fixing
27354 .Xdefaults with Emacs, when it has something bad in it. */
27355 type = HOLLOW_BOX_CURSOR;
27356
27357 return type;
27358 }
27359
27360 /* Set the default cursor types for specified frame. */
27361 void
27362 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27363 {
27364 int width = 1;
27365 Lisp_Object tem;
27366
27367 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27368 FRAME_CURSOR_WIDTH (f) = width;
27369
27370 /* By default, set up the blink-off state depending on the on-state. */
27371
27372 tem = Fassoc (arg, Vblink_cursor_alist);
27373 if (!NILP (tem))
27374 {
27375 FRAME_BLINK_OFF_CURSOR (f)
27376 = get_specified_cursor_type (XCDR (tem), &width);
27377 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27378 }
27379 else
27380 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27381
27382 /* Make sure the cursor gets redrawn. */
27383 f->cursor_type_changed = true;
27384 }
27385
27386
27387 #ifdef HAVE_WINDOW_SYSTEM
27388
27389 /* Return the cursor we want to be displayed in window W. Return
27390 width of bar/hbar cursor through WIDTH arg. Return with
27391 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27392 (i.e. if the `system caret' should track this cursor).
27393
27394 In a mini-buffer window, we want the cursor only to appear if we
27395 are reading input from this window. For the selected window, we
27396 want the cursor type given by the frame parameter or buffer local
27397 setting of cursor-type. If explicitly marked off, draw no cursor.
27398 In all other cases, we want a hollow box cursor. */
27399
27400 static enum text_cursor_kinds
27401 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27402 bool *active_cursor)
27403 {
27404 struct frame *f = XFRAME (w->frame);
27405 struct buffer *b = XBUFFER (w->contents);
27406 int cursor_type = DEFAULT_CURSOR;
27407 Lisp_Object alt_cursor;
27408 bool non_selected = false;
27409
27410 *active_cursor = true;
27411
27412 /* Echo area */
27413 if (cursor_in_echo_area
27414 && FRAME_HAS_MINIBUF_P (f)
27415 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27416 {
27417 if (w == XWINDOW (echo_area_window))
27418 {
27419 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27420 {
27421 *width = FRAME_CURSOR_WIDTH (f);
27422 return FRAME_DESIRED_CURSOR (f);
27423 }
27424 else
27425 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27426 }
27427
27428 *active_cursor = false;
27429 non_selected = true;
27430 }
27431
27432 /* Detect a nonselected window or nonselected frame. */
27433 else if (w != XWINDOW (f->selected_window)
27434 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27435 {
27436 *active_cursor = false;
27437
27438 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27439 return NO_CURSOR;
27440
27441 non_selected = true;
27442 }
27443
27444 /* Never display a cursor in a window in which cursor-type is nil. */
27445 if (NILP (BVAR (b, cursor_type)))
27446 return NO_CURSOR;
27447
27448 /* Get the normal cursor type for this window. */
27449 if (EQ (BVAR (b, cursor_type), Qt))
27450 {
27451 cursor_type = FRAME_DESIRED_CURSOR (f);
27452 *width = FRAME_CURSOR_WIDTH (f);
27453 }
27454 else
27455 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27456
27457 /* Use cursor-in-non-selected-windows instead
27458 for non-selected window or frame. */
27459 if (non_selected)
27460 {
27461 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27462 if (!EQ (Qt, alt_cursor))
27463 return get_specified_cursor_type (alt_cursor, width);
27464 /* t means modify the normal cursor type. */
27465 if (cursor_type == FILLED_BOX_CURSOR)
27466 cursor_type = HOLLOW_BOX_CURSOR;
27467 else if (cursor_type == BAR_CURSOR && *width > 1)
27468 --*width;
27469 return cursor_type;
27470 }
27471
27472 /* Use normal cursor if not blinked off. */
27473 if (!w->cursor_off_p)
27474 {
27475 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27476 {
27477 if (cursor_type == FILLED_BOX_CURSOR)
27478 {
27479 /* Using a block cursor on large images can be very annoying.
27480 So use a hollow cursor for "large" images.
27481 If image is not transparent (no mask), also use hollow cursor. */
27482 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27483 if (img != NULL && IMAGEP (img->spec))
27484 {
27485 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27486 where N = size of default frame font size.
27487 This should cover most of the "tiny" icons people may use. */
27488 if (!img->mask
27489 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27490 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27491 cursor_type = HOLLOW_BOX_CURSOR;
27492 }
27493 }
27494 else if (cursor_type != NO_CURSOR)
27495 {
27496 /* Display current only supports BOX and HOLLOW cursors for images.
27497 So for now, unconditionally use a HOLLOW cursor when cursor is
27498 not a solid box cursor. */
27499 cursor_type = HOLLOW_BOX_CURSOR;
27500 }
27501 }
27502 return cursor_type;
27503 }
27504
27505 /* Cursor is blinked off, so determine how to "toggle" it. */
27506
27507 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27508 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27509 return get_specified_cursor_type (XCDR (alt_cursor), width);
27510
27511 /* Then see if frame has specified a specific blink off cursor type. */
27512 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27513 {
27514 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27515 return FRAME_BLINK_OFF_CURSOR (f);
27516 }
27517
27518 #if false
27519 /* Some people liked having a permanently visible blinking cursor,
27520 while others had very strong opinions against it. So it was
27521 decided to remove it. KFS 2003-09-03 */
27522
27523 /* Finally perform built-in cursor blinking:
27524 filled box <-> hollow box
27525 wide [h]bar <-> narrow [h]bar
27526 narrow [h]bar <-> no cursor
27527 other type <-> no cursor */
27528
27529 if (cursor_type == FILLED_BOX_CURSOR)
27530 return HOLLOW_BOX_CURSOR;
27531
27532 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27533 {
27534 *width = 1;
27535 return cursor_type;
27536 }
27537 #endif
27538
27539 return NO_CURSOR;
27540 }
27541
27542
27543 /* Notice when the text cursor of window W has been completely
27544 overwritten by a drawing operation that outputs glyphs in AREA
27545 starting at X0 and ending at X1 in the line starting at Y0 and
27546 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27547 the rest of the line after X0 has been written. Y coordinates
27548 are window-relative. */
27549
27550 static void
27551 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27552 int x0, int x1, int y0, int y1)
27553 {
27554 int cx0, cx1, cy0, cy1;
27555 struct glyph_row *row;
27556
27557 if (!w->phys_cursor_on_p)
27558 return;
27559 if (area != TEXT_AREA)
27560 return;
27561
27562 if (w->phys_cursor.vpos < 0
27563 || w->phys_cursor.vpos >= w->current_matrix->nrows
27564 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27565 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27566 return;
27567
27568 if (row->cursor_in_fringe_p)
27569 {
27570 row->cursor_in_fringe_p = false;
27571 draw_fringe_bitmap (w, row, row->reversed_p);
27572 w->phys_cursor_on_p = false;
27573 return;
27574 }
27575
27576 cx0 = w->phys_cursor.x;
27577 cx1 = cx0 + w->phys_cursor_width;
27578 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27579 return;
27580
27581 /* The cursor image will be completely removed from the
27582 screen if the output area intersects the cursor area in
27583 y-direction. When we draw in [y0 y1[, and some part of
27584 the cursor is at y < y0, that part must have been drawn
27585 before. When scrolling, the cursor is erased before
27586 actually scrolling, so we don't come here. When not
27587 scrolling, the rows above the old cursor row must have
27588 changed, and in this case these rows must have written
27589 over the cursor image.
27590
27591 Likewise if part of the cursor is below y1, with the
27592 exception of the cursor being in the first blank row at
27593 the buffer and window end because update_text_area
27594 doesn't draw that row. (Except when it does, but
27595 that's handled in update_text_area.) */
27596
27597 cy0 = w->phys_cursor.y;
27598 cy1 = cy0 + w->phys_cursor_height;
27599 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27600 return;
27601
27602 w->phys_cursor_on_p = false;
27603 }
27604
27605 #endif /* HAVE_WINDOW_SYSTEM */
27606
27607 \f
27608 /************************************************************************
27609 Mouse Face
27610 ************************************************************************/
27611
27612 #ifdef HAVE_WINDOW_SYSTEM
27613
27614 /* EXPORT for RIF:
27615 Fix the display of area AREA of overlapping row ROW in window W
27616 with respect to the overlapping part OVERLAPS. */
27617
27618 void
27619 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27620 enum glyph_row_area area, int overlaps)
27621 {
27622 int i, x;
27623
27624 block_input ();
27625
27626 x = 0;
27627 for (i = 0; i < row->used[area];)
27628 {
27629 if (row->glyphs[area][i].overlaps_vertically_p)
27630 {
27631 int start = i, start_x = x;
27632
27633 do
27634 {
27635 x += row->glyphs[area][i].pixel_width;
27636 ++i;
27637 }
27638 while (i < row->used[area]
27639 && row->glyphs[area][i].overlaps_vertically_p);
27640
27641 draw_glyphs (w, start_x, row, area,
27642 start, i,
27643 DRAW_NORMAL_TEXT, overlaps);
27644 }
27645 else
27646 {
27647 x += row->glyphs[area][i].pixel_width;
27648 ++i;
27649 }
27650 }
27651
27652 unblock_input ();
27653 }
27654
27655
27656 /* EXPORT:
27657 Draw the cursor glyph of window W in glyph row ROW. See the
27658 comment of draw_glyphs for the meaning of HL. */
27659
27660 void
27661 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27662 enum draw_glyphs_face hl)
27663 {
27664 /* If cursor hpos is out of bounds, don't draw garbage. This can
27665 happen in mini-buffer windows when switching between echo area
27666 glyphs and mini-buffer. */
27667 if ((row->reversed_p
27668 ? (w->phys_cursor.hpos >= 0)
27669 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27670 {
27671 bool on_p = w->phys_cursor_on_p;
27672 int x1;
27673 int hpos = w->phys_cursor.hpos;
27674
27675 /* When the window is hscrolled, cursor hpos can legitimately be
27676 out of bounds, but we draw the cursor at the corresponding
27677 window margin in that case. */
27678 if (!row->reversed_p && hpos < 0)
27679 hpos = 0;
27680 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27681 hpos = row->used[TEXT_AREA] - 1;
27682
27683 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27684 hl, 0);
27685 w->phys_cursor_on_p = on_p;
27686
27687 if (hl == DRAW_CURSOR)
27688 w->phys_cursor_width = x1 - w->phys_cursor.x;
27689 /* When we erase the cursor, and ROW is overlapped by other
27690 rows, make sure that these overlapping parts of other rows
27691 are redrawn. */
27692 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27693 {
27694 w->phys_cursor_width = x1 - w->phys_cursor.x;
27695
27696 if (row > w->current_matrix->rows
27697 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27698 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27699 OVERLAPS_ERASED_CURSOR);
27700
27701 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27702 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27703 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27704 OVERLAPS_ERASED_CURSOR);
27705 }
27706 }
27707 }
27708
27709
27710 /* Erase the image of a cursor of window W from the screen. */
27711
27712 void
27713 erase_phys_cursor (struct window *w)
27714 {
27715 struct frame *f = XFRAME (w->frame);
27716 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27717 int hpos = w->phys_cursor.hpos;
27718 int vpos = w->phys_cursor.vpos;
27719 bool mouse_face_here_p = false;
27720 struct glyph_matrix *active_glyphs = w->current_matrix;
27721 struct glyph_row *cursor_row;
27722 struct glyph *cursor_glyph;
27723 enum draw_glyphs_face hl;
27724
27725 /* No cursor displayed or row invalidated => nothing to do on the
27726 screen. */
27727 if (w->phys_cursor_type == NO_CURSOR)
27728 goto mark_cursor_off;
27729
27730 /* VPOS >= active_glyphs->nrows means that window has been resized.
27731 Don't bother to erase the cursor. */
27732 if (vpos >= active_glyphs->nrows)
27733 goto mark_cursor_off;
27734
27735 /* If row containing cursor is marked invalid, there is nothing we
27736 can do. */
27737 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27738 if (!cursor_row->enabled_p)
27739 goto mark_cursor_off;
27740
27741 /* If line spacing is > 0, old cursor may only be partially visible in
27742 window after split-window. So adjust visible height. */
27743 cursor_row->visible_height = min (cursor_row->visible_height,
27744 window_text_bottom_y (w) - cursor_row->y);
27745
27746 /* If row is completely invisible, don't attempt to delete a cursor which
27747 isn't there. This can happen if cursor is at top of a window, and
27748 we switch to a buffer with a header line in that window. */
27749 if (cursor_row->visible_height <= 0)
27750 goto mark_cursor_off;
27751
27752 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27753 if (cursor_row->cursor_in_fringe_p)
27754 {
27755 cursor_row->cursor_in_fringe_p = false;
27756 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27757 goto mark_cursor_off;
27758 }
27759
27760 /* This can happen when the new row is shorter than the old one.
27761 In this case, either draw_glyphs or clear_end_of_line
27762 should have cleared the cursor. Note that we wouldn't be
27763 able to erase the cursor in this case because we don't have a
27764 cursor glyph at hand. */
27765 if ((cursor_row->reversed_p
27766 ? (w->phys_cursor.hpos < 0)
27767 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27768 goto mark_cursor_off;
27769
27770 /* When the window is hscrolled, cursor hpos can legitimately be out
27771 of bounds, but we draw the cursor at the corresponding window
27772 margin in that case. */
27773 if (!cursor_row->reversed_p && hpos < 0)
27774 hpos = 0;
27775 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27776 hpos = cursor_row->used[TEXT_AREA] - 1;
27777
27778 /* If the cursor is in the mouse face area, redisplay that when
27779 we clear the cursor. */
27780 if (! NILP (hlinfo->mouse_face_window)
27781 && coords_in_mouse_face_p (w, hpos, vpos)
27782 /* Don't redraw the cursor's spot in mouse face if it is at the
27783 end of a line (on a newline). The cursor appears there, but
27784 mouse highlighting does not. */
27785 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27786 mouse_face_here_p = true;
27787
27788 /* Maybe clear the display under the cursor. */
27789 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27790 {
27791 int x, y;
27792 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27793 int width;
27794
27795 cursor_glyph = get_phys_cursor_glyph (w);
27796 if (cursor_glyph == NULL)
27797 goto mark_cursor_off;
27798
27799 width = cursor_glyph->pixel_width;
27800 x = w->phys_cursor.x;
27801 if (x < 0)
27802 {
27803 width += x;
27804 x = 0;
27805 }
27806 width = min (width, window_box_width (w, TEXT_AREA) - x);
27807 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27808 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27809
27810 if (width > 0)
27811 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27812 }
27813
27814 /* Erase the cursor by redrawing the character underneath it. */
27815 if (mouse_face_here_p)
27816 hl = DRAW_MOUSE_FACE;
27817 else
27818 hl = DRAW_NORMAL_TEXT;
27819 draw_phys_cursor_glyph (w, cursor_row, hl);
27820
27821 mark_cursor_off:
27822 w->phys_cursor_on_p = false;
27823 w->phys_cursor_type = NO_CURSOR;
27824 }
27825
27826
27827 /* Display or clear cursor of window W. If !ON, clear the cursor.
27828 If ON, display the cursor; where to put the cursor is specified by
27829 HPOS, VPOS, X and Y. */
27830
27831 void
27832 display_and_set_cursor (struct window *w, bool on,
27833 int hpos, int vpos, int x, int y)
27834 {
27835 struct frame *f = XFRAME (w->frame);
27836 int new_cursor_type;
27837 int new_cursor_width;
27838 bool active_cursor;
27839 struct glyph_row *glyph_row;
27840 struct glyph *glyph;
27841
27842 /* This is pointless on invisible frames, and dangerous on garbaged
27843 windows and frames; in the latter case, the frame or window may
27844 be in the midst of changing its size, and x and y may be off the
27845 window. */
27846 if (! FRAME_VISIBLE_P (f)
27847 || FRAME_GARBAGED_P (f)
27848 || vpos >= w->current_matrix->nrows
27849 || hpos >= w->current_matrix->matrix_w)
27850 return;
27851
27852 /* If cursor is off and we want it off, return quickly. */
27853 if (!on && !w->phys_cursor_on_p)
27854 return;
27855
27856 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27857 /* If cursor row is not enabled, we don't really know where to
27858 display the cursor. */
27859 if (!glyph_row->enabled_p)
27860 {
27861 w->phys_cursor_on_p = false;
27862 return;
27863 }
27864
27865 glyph = NULL;
27866 if (!glyph_row->exact_window_width_line_p
27867 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27868 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27869
27870 eassert (input_blocked_p ());
27871
27872 /* Set new_cursor_type to the cursor we want to be displayed. */
27873 new_cursor_type = get_window_cursor_type (w, glyph,
27874 &new_cursor_width, &active_cursor);
27875
27876 /* If cursor is currently being shown and we don't want it to be or
27877 it is in the wrong place, or the cursor type is not what we want,
27878 erase it. */
27879 if (w->phys_cursor_on_p
27880 && (!on
27881 || w->phys_cursor.x != x
27882 || w->phys_cursor.y != y
27883 /* HPOS can be negative in R2L rows whose
27884 exact_window_width_line_p flag is set (i.e. their newline
27885 would "overflow into the fringe"). */
27886 || hpos < 0
27887 || new_cursor_type != w->phys_cursor_type
27888 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27889 && new_cursor_width != w->phys_cursor_width)))
27890 erase_phys_cursor (w);
27891
27892 /* Don't check phys_cursor_on_p here because that flag is only set
27893 to false in some cases where we know that the cursor has been
27894 completely erased, to avoid the extra work of erasing the cursor
27895 twice. In other words, phys_cursor_on_p can be true and the cursor
27896 still not be visible, or it has only been partly erased. */
27897 if (on)
27898 {
27899 w->phys_cursor_ascent = glyph_row->ascent;
27900 w->phys_cursor_height = glyph_row->height;
27901
27902 /* Set phys_cursor_.* before x_draw_.* is called because some
27903 of them may need the information. */
27904 w->phys_cursor.x = x;
27905 w->phys_cursor.y = glyph_row->y;
27906 w->phys_cursor.hpos = hpos;
27907 w->phys_cursor.vpos = vpos;
27908 }
27909
27910 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27911 new_cursor_type, new_cursor_width,
27912 on, active_cursor);
27913 }
27914
27915
27916 /* Switch the display of W's cursor on or off, according to the value
27917 of ON. */
27918
27919 static void
27920 update_window_cursor (struct window *w, bool on)
27921 {
27922 /* Don't update cursor in windows whose frame is in the process
27923 of being deleted. */
27924 if (w->current_matrix)
27925 {
27926 int hpos = w->phys_cursor.hpos;
27927 int vpos = w->phys_cursor.vpos;
27928 struct glyph_row *row;
27929
27930 if (vpos >= w->current_matrix->nrows
27931 || hpos >= w->current_matrix->matrix_w)
27932 return;
27933
27934 row = MATRIX_ROW (w->current_matrix, vpos);
27935
27936 /* When the window is hscrolled, cursor hpos can legitimately be
27937 out of bounds, but we draw the cursor at the corresponding
27938 window margin in that case. */
27939 if (!row->reversed_p && hpos < 0)
27940 hpos = 0;
27941 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27942 hpos = row->used[TEXT_AREA] - 1;
27943
27944 block_input ();
27945 display_and_set_cursor (w, on, hpos, vpos,
27946 w->phys_cursor.x, w->phys_cursor.y);
27947 unblock_input ();
27948 }
27949 }
27950
27951
27952 /* Call update_window_cursor with parameter ON_P on all leaf windows
27953 in the window tree rooted at W. */
27954
27955 static void
27956 update_cursor_in_window_tree (struct window *w, bool on_p)
27957 {
27958 while (w)
27959 {
27960 if (WINDOWP (w->contents))
27961 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27962 else
27963 update_window_cursor (w, on_p);
27964
27965 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27966 }
27967 }
27968
27969
27970 /* EXPORT:
27971 Display the cursor on window W, or clear it, according to ON_P.
27972 Don't change the cursor's position. */
27973
27974 void
27975 x_update_cursor (struct frame *f, bool on_p)
27976 {
27977 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27978 }
27979
27980
27981 /* EXPORT:
27982 Clear the cursor of window W to background color, and mark the
27983 cursor as not shown. This is used when the text where the cursor
27984 is about to be rewritten. */
27985
27986 void
27987 x_clear_cursor (struct window *w)
27988 {
27989 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27990 update_window_cursor (w, false);
27991 }
27992
27993 #endif /* HAVE_WINDOW_SYSTEM */
27994
27995 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27996 and MSDOS. */
27997 static void
27998 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27999 int start_hpos, int end_hpos,
28000 enum draw_glyphs_face draw)
28001 {
28002 #ifdef HAVE_WINDOW_SYSTEM
28003 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28004 {
28005 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28006 return;
28007 }
28008 #endif
28009 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28010 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28011 #endif
28012 }
28013
28014 /* Display the active region described by mouse_face_* according to DRAW. */
28015
28016 static void
28017 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28018 {
28019 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28020 struct frame *f = XFRAME (WINDOW_FRAME (w));
28021
28022 if (/* If window is in the process of being destroyed, don't bother
28023 to do anything. */
28024 w->current_matrix != NULL
28025 /* Don't update mouse highlight if hidden. */
28026 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28027 /* Recognize when we are called to operate on rows that don't exist
28028 anymore. This can happen when a window is split. */
28029 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28030 {
28031 bool phys_cursor_on_p = w->phys_cursor_on_p;
28032 struct glyph_row *row, *first, *last;
28033
28034 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28035 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28036
28037 for (row = first; row <= last && row->enabled_p; ++row)
28038 {
28039 int start_hpos, end_hpos, start_x;
28040
28041 /* For all but the first row, the highlight starts at column 0. */
28042 if (row == first)
28043 {
28044 /* R2L rows have BEG and END in reversed order, but the
28045 screen drawing geometry is always left to right. So
28046 we need to mirror the beginning and end of the
28047 highlighted area in R2L rows. */
28048 if (!row->reversed_p)
28049 {
28050 start_hpos = hlinfo->mouse_face_beg_col;
28051 start_x = hlinfo->mouse_face_beg_x;
28052 }
28053 else if (row == last)
28054 {
28055 start_hpos = hlinfo->mouse_face_end_col;
28056 start_x = hlinfo->mouse_face_end_x;
28057 }
28058 else
28059 {
28060 start_hpos = 0;
28061 start_x = 0;
28062 }
28063 }
28064 else if (row->reversed_p && row == last)
28065 {
28066 start_hpos = hlinfo->mouse_face_end_col;
28067 start_x = hlinfo->mouse_face_end_x;
28068 }
28069 else
28070 {
28071 start_hpos = 0;
28072 start_x = 0;
28073 }
28074
28075 if (row == last)
28076 {
28077 if (!row->reversed_p)
28078 end_hpos = hlinfo->mouse_face_end_col;
28079 else if (row == first)
28080 end_hpos = hlinfo->mouse_face_beg_col;
28081 else
28082 {
28083 end_hpos = row->used[TEXT_AREA];
28084 if (draw == DRAW_NORMAL_TEXT)
28085 row->fill_line_p = true; /* Clear to end of line. */
28086 }
28087 }
28088 else if (row->reversed_p && row == first)
28089 end_hpos = hlinfo->mouse_face_beg_col;
28090 else
28091 {
28092 end_hpos = row->used[TEXT_AREA];
28093 if (draw == DRAW_NORMAL_TEXT)
28094 row->fill_line_p = true; /* Clear to end of line. */
28095 }
28096
28097 if (end_hpos > start_hpos)
28098 {
28099 draw_row_with_mouse_face (w, start_x, row,
28100 start_hpos, end_hpos, draw);
28101
28102 row->mouse_face_p
28103 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28104 }
28105 }
28106
28107 #ifdef HAVE_WINDOW_SYSTEM
28108 /* When we've written over the cursor, arrange for it to
28109 be displayed again. */
28110 if (FRAME_WINDOW_P (f)
28111 && phys_cursor_on_p && !w->phys_cursor_on_p)
28112 {
28113 int hpos = w->phys_cursor.hpos;
28114
28115 /* When the window is hscrolled, cursor hpos can legitimately be
28116 out of bounds, but we draw the cursor at the corresponding
28117 window margin in that case. */
28118 if (!row->reversed_p && hpos < 0)
28119 hpos = 0;
28120 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28121 hpos = row->used[TEXT_AREA] - 1;
28122
28123 block_input ();
28124 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28125 w->phys_cursor.x, w->phys_cursor.y);
28126 unblock_input ();
28127 }
28128 #endif /* HAVE_WINDOW_SYSTEM */
28129 }
28130
28131 #ifdef HAVE_WINDOW_SYSTEM
28132 /* Change the mouse cursor. */
28133 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28134 {
28135 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28136 if (draw == DRAW_NORMAL_TEXT
28137 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28138 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28139 else
28140 #endif
28141 if (draw == DRAW_MOUSE_FACE)
28142 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28143 else
28144 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28145 }
28146 #endif /* HAVE_WINDOW_SYSTEM */
28147 }
28148
28149 /* EXPORT:
28150 Clear out the mouse-highlighted active region.
28151 Redraw it un-highlighted first. Value is true if mouse
28152 face was actually drawn unhighlighted. */
28153
28154 bool
28155 clear_mouse_face (Mouse_HLInfo *hlinfo)
28156 {
28157 bool cleared
28158 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28159 if (cleared)
28160 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28161 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28162 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28163 hlinfo->mouse_face_window = Qnil;
28164 hlinfo->mouse_face_overlay = Qnil;
28165 return cleared;
28166 }
28167
28168 /* Return true if the coordinates HPOS and VPOS on windows W are
28169 within the mouse face on that window. */
28170 static bool
28171 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28172 {
28173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28174
28175 /* Quickly resolve the easy cases. */
28176 if (!(WINDOWP (hlinfo->mouse_face_window)
28177 && XWINDOW (hlinfo->mouse_face_window) == w))
28178 return false;
28179 if (vpos < hlinfo->mouse_face_beg_row
28180 || vpos > hlinfo->mouse_face_end_row)
28181 return false;
28182 if (vpos > hlinfo->mouse_face_beg_row
28183 && vpos < hlinfo->mouse_face_end_row)
28184 return true;
28185
28186 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28187 {
28188 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28189 {
28190 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28191 return true;
28192 }
28193 else if ((vpos == hlinfo->mouse_face_beg_row
28194 && hpos >= hlinfo->mouse_face_beg_col)
28195 || (vpos == hlinfo->mouse_face_end_row
28196 && hpos < hlinfo->mouse_face_end_col))
28197 return true;
28198 }
28199 else
28200 {
28201 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28202 {
28203 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28204 return true;
28205 }
28206 else if ((vpos == hlinfo->mouse_face_beg_row
28207 && hpos <= hlinfo->mouse_face_beg_col)
28208 || (vpos == hlinfo->mouse_face_end_row
28209 && hpos > hlinfo->mouse_face_end_col))
28210 return true;
28211 }
28212 return false;
28213 }
28214
28215
28216 /* EXPORT:
28217 True if physical cursor of window W is within mouse face. */
28218
28219 bool
28220 cursor_in_mouse_face_p (struct window *w)
28221 {
28222 int hpos = w->phys_cursor.hpos;
28223 int vpos = w->phys_cursor.vpos;
28224 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28225
28226 /* When the window is hscrolled, cursor hpos can legitimately be out
28227 of bounds, but we draw the cursor at the corresponding window
28228 margin in that case. */
28229 if (!row->reversed_p && hpos < 0)
28230 hpos = 0;
28231 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28232 hpos = row->used[TEXT_AREA] - 1;
28233
28234 return coords_in_mouse_face_p (w, hpos, vpos);
28235 }
28236
28237
28238 \f
28239 /* Find the glyph rows START_ROW and END_ROW of window W that display
28240 characters between buffer positions START_CHARPOS and END_CHARPOS
28241 (excluding END_CHARPOS). DISP_STRING is a display string that
28242 covers these buffer positions. This is similar to
28243 row_containing_pos, but is more accurate when bidi reordering makes
28244 buffer positions change non-linearly with glyph rows. */
28245 static void
28246 rows_from_pos_range (struct window *w,
28247 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28248 Lisp_Object disp_string,
28249 struct glyph_row **start, struct glyph_row **end)
28250 {
28251 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28252 int last_y = window_text_bottom_y (w);
28253 struct glyph_row *row;
28254
28255 *start = NULL;
28256 *end = NULL;
28257
28258 while (!first->enabled_p
28259 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28260 first++;
28261
28262 /* Find the START row. */
28263 for (row = first;
28264 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28265 row++)
28266 {
28267 /* A row can potentially be the START row if the range of the
28268 characters it displays intersects the range
28269 [START_CHARPOS..END_CHARPOS). */
28270 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28271 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28272 /* See the commentary in row_containing_pos, for the
28273 explanation of the complicated way to check whether
28274 some position is beyond the end of the characters
28275 displayed by a row. */
28276 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28277 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28278 && !row->ends_at_zv_p
28279 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28280 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28281 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28282 && !row->ends_at_zv_p
28283 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28284 {
28285 /* Found a candidate row. Now make sure at least one of the
28286 glyphs it displays has a charpos from the range
28287 [START_CHARPOS..END_CHARPOS).
28288
28289 This is not obvious because bidi reordering could make
28290 buffer positions of a row be 1,2,3,102,101,100, and if we
28291 want to highlight characters in [50..60), we don't want
28292 this row, even though [50..60) does intersect [1..103),
28293 the range of character positions given by the row's start
28294 and end positions. */
28295 struct glyph *g = row->glyphs[TEXT_AREA];
28296 struct glyph *e = g + row->used[TEXT_AREA];
28297
28298 while (g < e)
28299 {
28300 if (((BUFFERP (g->object) || NILP (g->object))
28301 && start_charpos <= g->charpos && g->charpos < end_charpos)
28302 /* A glyph that comes from DISP_STRING is by
28303 definition to be highlighted. */
28304 || EQ (g->object, disp_string))
28305 *start = row;
28306 g++;
28307 }
28308 if (*start)
28309 break;
28310 }
28311 }
28312
28313 /* Find the END row. */
28314 if (!*start
28315 /* If the last row is partially visible, start looking for END
28316 from that row, instead of starting from FIRST. */
28317 && !(row->enabled_p
28318 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28319 row = first;
28320 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28321 {
28322 struct glyph_row *next = row + 1;
28323 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28324
28325 if (!next->enabled_p
28326 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28327 /* The first row >= START whose range of displayed characters
28328 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28329 is the row END + 1. */
28330 || (start_charpos < next_start
28331 && end_charpos < next_start)
28332 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28333 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28334 && !next->ends_at_zv_p
28335 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28336 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28337 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28338 && !next->ends_at_zv_p
28339 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28340 {
28341 *end = row;
28342 break;
28343 }
28344 else
28345 {
28346 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28347 but none of the characters it displays are in the range, it is
28348 also END + 1. */
28349 struct glyph *g = next->glyphs[TEXT_AREA];
28350 struct glyph *s = g;
28351 struct glyph *e = g + next->used[TEXT_AREA];
28352
28353 while (g < e)
28354 {
28355 if (((BUFFERP (g->object) || NILP (g->object))
28356 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28357 /* If the buffer position of the first glyph in
28358 the row is equal to END_CHARPOS, it means
28359 the last character to be highlighted is the
28360 newline of ROW, and we must consider NEXT as
28361 END, not END+1. */
28362 || (((!next->reversed_p && g == s)
28363 || (next->reversed_p && g == e - 1))
28364 && (g->charpos == end_charpos
28365 /* Special case for when NEXT is an
28366 empty line at ZV. */
28367 || (g->charpos == -1
28368 && !row->ends_at_zv_p
28369 && next_start == end_charpos)))))
28370 /* A glyph that comes from DISP_STRING is by
28371 definition to be highlighted. */
28372 || EQ (g->object, disp_string))
28373 break;
28374 g++;
28375 }
28376 if (g == e)
28377 {
28378 *end = row;
28379 break;
28380 }
28381 /* The first row that ends at ZV must be the last to be
28382 highlighted. */
28383 else if (next->ends_at_zv_p)
28384 {
28385 *end = next;
28386 break;
28387 }
28388 }
28389 }
28390 }
28391
28392 /* This function sets the mouse_face_* elements of HLINFO, assuming
28393 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28394 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28395 for the overlay or run of text properties specifying the mouse
28396 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28397 before-string and after-string that must also be highlighted.
28398 DISP_STRING, if non-nil, is a display string that may cover some
28399 or all of the highlighted text. */
28400
28401 static void
28402 mouse_face_from_buffer_pos (Lisp_Object window,
28403 Mouse_HLInfo *hlinfo,
28404 ptrdiff_t mouse_charpos,
28405 ptrdiff_t start_charpos,
28406 ptrdiff_t end_charpos,
28407 Lisp_Object before_string,
28408 Lisp_Object after_string,
28409 Lisp_Object disp_string)
28410 {
28411 struct window *w = XWINDOW (window);
28412 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28413 struct glyph_row *r1, *r2;
28414 struct glyph *glyph, *end;
28415 ptrdiff_t ignore, pos;
28416 int x;
28417
28418 eassert (NILP (disp_string) || STRINGP (disp_string));
28419 eassert (NILP (before_string) || STRINGP (before_string));
28420 eassert (NILP (after_string) || STRINGP (after_string));
28421
28422 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28423 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28424 if (r1 == NULL)
28425 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28426 /* If the before-string or display-string contains newlines,
28427 rows_from_pos_range skips to its last row. Move back. */
28428 if (!NILP (before_string) || !NILP (disp_string))
28429 {
28430 struct glyph_row *prev;
28431 while ((prev = r1 - 1, prev >= first)
28432 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28433 && prev->used[TEXT_AREA] > 0)
28434 {
28435 struct glyph *beg = prev->glyphs[TEXT_AREA];
28436 glyph = beg + prev->used[TEXT_AREA];
28437 while (--glyph >= beg && NILP (glyph->object));
28438 if (glyph < beg
28439 || !(EQ (glyph->object, before_string)
28440 || EQ (glyph->object, disp_string)))
28441 break;
28442 r1 = prev;
28443 }
28444 }
28445 if (r2 == NULL)
28446 {
28447 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28448 hlinfo->mouse_face_past_end = true;
28449 }
28450 else if (!NILP (after_string))
28451 {
28452 /* If the after-string has newlines, advance to its last row. */
28453 struct glyph_row *next;
28454 struct glyph_row *last
28455 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28456
28457 for (next = r2 + 1;
28458 next <= last
28459 && next->used[TEXT_AREA] > 0
28460 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28461 ++next)
28462 r2 = next;
28463 }
28464 /* The rest of the display engine assumes that mouse_face_beg_row is
28465 either above mouse_face_end_row or identical to it. But with
28466 bidi-reordered continued lines, the row for START_CHARPOS could
28467 be below the row for END_CHARPOS. If so, swap the rows and store
28468 them in correct order. */
28469 if (r1->y > r2->y)
28470 {
28471 struct glyph_row *tem = r2;
28472
28473 r2 = r1;
28474 r1 = tem;
28475 }
28476
28477 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28478 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28479
28480 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28481 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28482 could be anywhere in the row and in any order. The strategy
28483 below is to find the leftmost and the rightmost glyph that
28484 belongs to either of these 3 strings, or whose position is
28485 between START_CHARPOS and END_CHARPOS, and highlight all the
28486 glyphs between those two. This may cover more than just the text
28487 between START_CHARPOS and END_CHARPOS if the range of characters
28488 strides the bidi level boundary, e.g. if the beginning is in R2L
28489 text while the end is in L2R text or vice versa. */
28490 if (!r1->reversed_p)
28491 {
28492 /* This row is in a left to right paragraph. Scan it left to
28493 right. */
28494 glyph = r1->glyphs[TEXT_AREA];
28495 end = glyph + r1->used[TEXT_AREA];
28496 x = r1->x;
28497
28498 /* Skip truncation glyphs at the start of the glyph row. */
28499 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28500 for (; glyph < end
28501 && NILP (glyph->object)
28502 && glyph->charpos < 0;
28503 ++glyph)
28504 x += glyph->pixel_width;
28505
28506 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28507 or DISP_STRING, and the first glyph from buffer whose
28508 position is between START_CHARPOS and END_CHARPOS. */
28509 for (; glyph < end
28510 && !NILP (glyph->object)
28511 && !EQ (glyph->object, disp_string)
28512 && !(BUFFERP (glyph->object)
28513 && (glyph->charpos >= start_charpos
28514 && glyph->charpos < end_charpos));
28515 ++glyph)
28516 {
28517 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28518 are present at buffer positions between START_CHARPOS and
28519 END_CHARPOS, or if they come from an overlay. */
28520 if (EQ (glyph->object, before_string))
28521 {
28522 pos = string_buffer_position (before_string,
28523 start_charpos);
28524 /* If pos == 0, it means before_string came from an
28525 overlay, not from a buffer position. */
28526 if (!pos || (pos >= start_charpos && pos < end_charpos))
28527 break;
28528 }
28529 else if (EQ (glyph->object, after_string))
28530 {
28531 pos = string_buffer_position (after_string, end_charpos);
28532 if (!pos || (pos >= start_charpos && pos < end_charpos))
28533 break;
28534 }
28535 x += glyph->pixel_width;
28536 }
28537 hlinfo->mouse_face_beg_x = x;
28538 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28539 }
28540 else
28541 {
28542 /* This row is in a right to left paragraph. Scan it right to
28543 left. */
28544 struct glyph *g;
28545
28546 end = r1->glyphs[TEXT_AREA] - 1;
28547 glyph = end + r1->used[TEXT_AREA];
28548
28549 /* Skip truncation glyphs at the start of the glyph row. */
28550 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28551 for (; glyph > end
28552 && NILP (glyph->object)
28553 && glyph->charpos < 0;
28554 --glyph)
28555 ;
28556
28557 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28558 or DISP_STRING, and the first glyph from buffer whose
28559 position is between START_CHARPOS and END_CHARPOS. */
28560 for (; glyph > end
28561 && !NILP (glyph->object)
28562 && !EQ (glyph->object, disp_string)
28563 && !(BUFFERP (glyph->object)
28564 && (glyph->charpos >= start_charpos
28565 && glyph->charpos < end_charpos));
28566 --glyph)
28567 {
28568 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28569 are present at buffer positions between START_CHARPOS and
28570 END_CHARPOS, or if they come from an overlay. */
28571 if (EQ (glyph->object, before_string))
28572 {
28573 pos = string_buffer_position (before_string, start_charpos);
28574 /* If pos == 0, it means before_string came from an
28575 overlay, not from a buffer position. */
28576 if (!pos || (pos >= start_charpos && pos < end_charpos))
28577 break;
28578 }
28579 else if (EQ (glyph->object, after_string))
28580 {
28581 pos = string_buffer_position (after_string, end_charpos);
28582 if (!pos || (pos >= start_charpos && pos < end_charpos))
28583 break;
28584 }
28585 }
28586
28587 glyph++; /* first glyph to the right of the highlighted area */
28588 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28589 x += g->pixel_width;
28590 hlinfo->mouse_face_beg_x = x;
28591 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28592 }
28593
28594 /* If the highlight ends in a different row, compute GLYPH and END
28595 for the end row. Otherwise, reuse the values computed above for
28596 the row where the highlight begins. */
28597 if (r2 != r1)
28598 {
28599 if (!r2->reversed_p)
28600 {
28601 glyph = r2->glyphs[TEXT_AREA];
28602 end = glyph + r2->used[TEXT_AREA];
28603 x = r2->x;
28604 }
28605 else
28606 {
28607 end = r2->glyphs[TEXT_AREA] - 1;
28608 glyph = end + r2->used[TEXT_AREA];
28609 }
28610 }
28611
28612 if (!r2->reversed_p)
28613 {
28614 /* Skip truncation and continuation glyphs near the end of the
28615 row, and also blanks and stretch glyphs inserted by
28616 extend_face_to_end_of_line. */
28617 while (end > glyph
28618 && NILP ((end - 1)->object))
28619 --end;
28620 /* Scan the rest of the glyph row from the end, looking for the
28621 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28622 DISP_STRING, or whose position is between START_CHARPOS
28623 and END_CHARPOS */
28624 for (--end;
28625 end > glyph
28626 && !NILP (end->object)
28627 && !EQ (end->object, disp_string)
28628 && !(BUFFERP (end->object)
28629 && (end->charpos >= start_charpos
28630 && end->charpos < end_charpos));
28631 --end)
28632 {
28633 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28634 are present at buffer positions between START_CHARPOS and
28635 END_CHARPOS, or if they come from an overlay. */
28636 if (EQ (end->object, before_string))
28637 {
28638 pos = string_buffer_position (before_string, start_charpos);
28639 if (!pos || (pos >= start_charpos && pos < end_charpos))
28640 break;
28641 }
28642 else if (EQ (end->object, after_string))
28643 {
28644 pos = string_buffer_position (after_string, end_charpos);
28645 if (!pos || (pos >= start_charpos && pos < end_charpos))
28646 break;
28647 }
28648 }
28649 /* Find the X coordinate of the last glyph to be highlighted. */
28650 for (; glyph <= end; ++glyph)
28651 x += glyph->pixel_width;
28652
28653 hlinfo->mouse_face_end_x = x;
28654 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28655 }
28656 else
28657 {
28658 /* Skip truncation and continuation glyphs near the end of the
28659 row, and also blanks and stretch glyphs inserted by
28660 extend_face_to_end_of_line. */
28661 x = r2->x;
28662 end++;
28663 while (end < glyph
28664 && NILP (end->object))
28665 {
28666 x += end->pixel_width;
28667 ++end;
28668 }
28669 /* Scan the rest of the glyph row from the end, looking for the
28670 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28671 DISP_STRING, or whose position is between START_CHARPOS
28672 and END_CHARPOS */
28673 for ( ;
28674 end < glyph
28675 && !NILP (end->object)
28676 && !EQ (end->object, disp_string)
28677 && !(BUFFERP (end->object)
28678 && (end->charpos >= start_charpos
28679 && end->charpos < end_charpos));
28680 ++end)
28681 {
28682 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28683 are present at buffer positions between START_CHARPOS and
28684 END_CHARPOS, or if they come from an overlay. */
28685 if (EQ (end->object, before_string))
28686 {
28687 pos = string_buffer_position (before_string, start_charpos);
28688 if (!pos || (pos >= start_charpos && pos < end_charpos))
28689 break;
28690 }
28691 else if (EQ (end->object, after_string))
28692 {
28693 pos = string_buffer_position (after_string, end_charpos);
28694 if (!pos || (pos >= start_charpos && pos < end_charpos))
28695 break;
28696 }
28697 x += end->pixel_width;
28698 }
28699 /* If we exited the above loop because we arrived at the last
28700 glyph of the row, and its buffer position is still not in
28701 range, it means the last character in range is the preceding
28702 newline. Bump the end column and x values to get past the
28703 last glyph. */
28704 if (end == glyph
28705 && BUFFERP (end->object)
28706 && (end->charpos < start_charpos
28707 || end->charpos >= end_charpos))
28708 {
28709 x += end->pixel_width;
28710 ++end;
28711 }
28712 hlinfo->mouse_face_end_x = x;
28713 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28714 }
28715
28716 hlinfo->mouse_face_window = window;
28717 hlinfo->mouse_face_face_id
28718 = face_at_buffer_position (w, mouse_charpos, &ignore,
28719 mouse_charpos + 1,
28720 !hlinfo->mouse_face_hidden, -1);
28721 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28722 }
28723
28724 /* The following function is not used anymore (replaced with
28725 mouse_face_from_string_pos), but I leave it here for the time
28726 being, in case someone would. */
28727
28728 #if false /* not used */
28729
28730 /* Find the position of the glyph for position POS in OBJECT in
28731 window W's current matrix, and return in *X, *Y the pixel
28732 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28733
28734 RIGHT_P means return the position of the right edge of the glyph.
28735 !RIGHT_P means return the left edge position.
28736
28737 If no glyph for POS exists in the matrix, return the position of
28738 the glyph with the next smaller position that is in the matrix, if
28739 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28740 exists in the matrix, return the position of the glyph with the
28741 next larger position in OBJECT.
28742
28743 Value is true if a glyph was found. */
28744
28745 static bool
28746 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28747 int *hpos, int *vpos, int *x, int *y, bool right_p)
28748 {
28749 int yb = window_text_bottom_y (w);
28750 struct glyph_row *r;
28751 struct glyph *best_glyph = NULL;
28752 struct glyph_row *best_row = NULL;
28753 int best_x = 0;
28754
28755 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28756 r->enabled_p && r->y < yb;
28757 ++r)
28758 {
28759 struct glyph *g = r->glyphs[TEXT_AREA];
28760 struct glyph *e = g + r->used[TEXT_AREA];
28761 int gx;
28762
28763 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28764 if (EQ (g->object, object))
28765 {
28766 if (g->charpos == pos)
28767 {
28768 best_glyph = g;
28769 best_x = gx;
28770 best_row = r;
28771 goto found;
28772 }
28773 else if (best_glyph == NULL
28774 || ((eabs (g->charpos - pos)
28775 < eabs (best_glyph->charpos - pos))
28776 && (right_p
28777 ? g->charpos < pos
28778 : g->charpos > pos)))
28779 {
28780 best_glyph = g;
28781 best_x = gx;
28782 best_row = r;
28783 }
28784 }
28785 }
28786
28787 found:
28788
28789 if (best_glyph)
28790 {
28791 *x = best_x;
28792 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28793
28794 if (right_p)
28795 {
28796 *x += best_glyph->pixel_width;
28797 ++*hpos;
28798 }
28799
28800 *y = best_row->y;
28801 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28802 }
28803
28804 return best_glyph != NULL;
28805 }
28806 #endif /* not used */
28807
28808 /* Find the positions of the first and the last glyphs in window W's
28809 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28810 (assumed to be a string), and return in HLINFO's mouse_face_*
28811 members the pixel and column/row coordinates of those glyphs. */
28812
28813 static void
28814 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28815 Lisp_Object object,
28816 ptrdiff_t startpos, ptrdiff_t endpos)
28817 {
28818 int yb = window_text_bottom_y (w);
28819 struct glyph_row *r;
28820 struct glyph *g, *e;
28821 int gx;
28822 bool found = false;
28823
28824 /* Find the glyph row with at least one position in the range
28825 [STARTPOS..ENDPOS), and the first glyph in that row whose
28826 position belongs to that range. */
28827 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28828 r->enabled_p && r->y < yb;
28829 ++r)
28830 {
28831 if (!r->reversed_p)
28832 {
28833 g = r->glyphs[TEXT_AREA];
28834 e = g + r->used[TEXT_AREA];
28835 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28836 if (EQ (g->object, object)
28837 && startpos <= g->charpos && g->charpos < endpos)
28838 {
28839 hlinfo->mouse_face_beg_row
28840 = MATRIX_ROW_VPOS (r, w->current_matrix);
28841 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28842 hlinfo->mouse_face_beg_x = gx;
28843 found = true;
28844 break;
28845 }
28846 }
28847 else
28848 {
28849 struct glyph *g1;
28850
28851 e = r->glyphs[TEXT_AREA];
28852 g = e + r->used[TEXT_AREA];
28853 for ( ; g > e; --g)
28854 if (EQ ((g-1)->object, object)
28855 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28856 {
28857 hlinfo->mouse_face_beg_row
28858 = MATRIX_ROW_VPOS (r, w->current_matrix);
28859 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28860 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28861 gx += g1->pixel_width;
28862 hlinfo->mouse_face_beg_x = gx;
28863 found = true;
28864 break;
28865 }
28866 }
28867 if (found)
28868 break;
28869 }
28870
28871 if (!found)
28872 return;
28873
28874 /* Starting with the next row, look for the first row which does NOT
28875 include any glyphs whose positions are in the range. */
28876 for (++r; r->enabled_p && r->y < yb; ++r)
28877 {
28878 g = r->glyphs[TEXT_AREA];
28879 e = g + r->used[TEXT_AREA];
28880 found = false;
28881 for ( ; g < e; ++g)
28882 if (EQ (g->object, object)
28883 && startpos <= g->charpos && g->charpos < endpos)
28884 {
28885 found = true;
28886 break;
28887 }
28888 if (!found)
28889 break;
28890 }
28891
28892 /* The highlighted region ends on the previous row. */
28893 r--;
28894
28895 /* Set the end row. */
28896 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28897
28898 /* Compute and set the end column and the end column's horizontal
28899 pixel coordinate. */
28900 if (!r->reversed_p)
28901 {
28902 g = r->glyphs[TEXT_AREA];
28903 e = g + r->used[TEXT_AREA];
28904 for ( ; e > g; --e)
28905 if (EQ ((e-1)->object, object)
28906 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28907 break;
28908 hlinfo->mouse_face_end_col = e - g;
28909
28910 for (gx = r->x; g < e; ++g)
28911 gx += g->pixel_width;
28912 hlinfo->mouse_face_end_x = gx;
28913 }
28914 else
28915 {
28916 e = r->glyphs[TEXT_AREA];
28917 g = e + r->used[TEXT_AREA];
28918 for (gx = r->x ; e < g; ++e)
28919 {
28920 if (EQ (e->object, object)
28921 && startpos <= e->charpos && e->charpos < endpos)
28922 break;
28923 gx += e->pixel_width;
28924 }
28925 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28926 hlinfo->mouse_face_end_x = gx;
28927 }
28928 }
28929
28930 #ifdef HAVE_WINDOW_SYSTEM
28931
28932 /* See if position X, Y is within a hot-spot of an image. */
28933
28934 static bool
28935 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28936 {
28937 if (!CONSP (hot_spot))
28938 return false;
28939
28940 if (EQ (XCAR (hot_spot), Qrect))
28941 {
28942 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28943 Lisp_Object rect = XCDR (hot_spot);
28944 Lisp_Object tem;
28945 if (!CONSP (rect))
28946 return false;
28947 if (!CONSP (XCAR (rect)))
28948 return false;
28949 if (!CONSP (XCDR (rect)))
28950 return false;
28951 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28952 return false;
28953 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28954 return false;
28955 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28956 return false;
28957 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28958 return false;
28959 return true;
28960 }
28961 else if (EQ (XCAR (hot_spot), Qcircle))
28962 {
28963 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28964 Lisp_Object circ = XCDR (hot_spot);
28965 Lisp_Object lr, lx0, ly0;
28966 if (CONSP (circ)
28967 && CONSP (XCAR (circ))
28968 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28969 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28970 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28971 {
28972 double r = XFLOATINT (lr);
28973 double dx = XINT (lx0) - x;
28974 double dy = XINT (ly0) - y;
28975 return (dx * dx + dy * dy <= r * r);
28976 }
28977 }
28978 else if (EQ (XCAR (hot_spot), Qpoly))
28979 {
28980 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28981 if (VECTORP (XCDR (hot_spot)))
28982 {
28983 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28984 Lisp_Object *poly = v->contents;
28985 ptrdiff_t n = v->header.size;
28986 ptrdiff_t i;
28987 bool inside = false;
28988 Lisp_Object lx, ly;
28989 int x0, y0;
28990
28991 /* Need an even number of coordinates, and at least 3 edges. */
28992 if (n < 6 || n & 1)
28993 return false;
28994
28995 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28996 If count is odd, we are inside polygon. Pixels on edges
28997 may or may not be included depending on actual geometry of the
28998 polygon. */
28999 if ((lx = poly[n-2], !INTEGERP (lx))
29000 || (ly = poly[n-1], !INTEGERP (lx)))
29001 return false;
29002 x0 = XINT (lx), y0 = XINT (ly);
29003 for (i = 0; i < n; i += 2)
29004 {
29005 int x1 = x0, y1 = y0;
29006 if ((lx = poly[i], !INTEGERP (lx))
29007 || (ly = poly[i+1], !INTEGERP (ly)))
29008 return false;
29009 x0 = XINT (lx), y0 = XINT (ly);
29010
29011 /* Does this segment cross the X line? */
29012 if (x0 >= x)
29013 {
29014 if (x1 >= x)
29015 continue;
29016 }
29017 else if (x1 < x)
29018 continue;
29019 if (y > y0 && y > y1)
29020 continue;
29021 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29022 inside = !inside;
29023 }
29024 return inside;
29025 }
29026 }
29027 return false;
29028 }
29029
29030 Lisp_Object
29031 find_hot_spot (Lisp_Object map, int x, int y)
29032 {
29033 while (CONSP (map))
29034 {
29035 if (CONSP (XCAR (map))
29036 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29037 return XCAR (map);
29038 map = XCDR (map);
29039 }
29040
29041 return Qnil;
29042 }
29043
29044 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29045 3, 3, 0,
29046 doc: /* Lookup in image map MAP coordinates X and Y.
29047 An image map is an alist where each element has the format (AREA ID PLIST).
29048 An AREA is specified as either a rectangle, a circle, or a polygon:
29049 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29050 pixel coordinates of the upper left and bottom right corners.
29051 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29052 and the radius of the circle; r may be a float or integer.
29053 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29054 vector describes one corner in the polygon.
29055 Returns the alist element for the first matching AREA in MAP. */)
29056 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29057 {
29058 if (NILP (map))
29059 return Qnil;
29060
29061 CHECK_NUMBER (x);
29062 CHECK_NUMBER (y);
29063
29064 return find_hot_spot (map,
29065 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29066 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29067 }
29068
29069
29070 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29071 static void
29072 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29073 {
29074 /* Do not change cursor shape while dragging mouse. */
29075 if (!NILP (do_mouse_tracking))
29076 return;
29077
29078 if (!NILP (pointer))
29079 {
29080 if (EQ (pointer, Qarrow))
29081 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29082 else if (EQ (pointer, Qhand))
29083 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29084 else if (EQ (pointer, Qtext))
29085 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29086 else if (EQ (pointer, intern ("hdrag")))
29087 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29088 else if (EQ (pointer, intern ("nhdrag")))
29089 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29090 #ifdef HAVE_X_WINDOWS
29091 else if (EQ (pointer, intern ("vdrag")))
29092 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29093 #endif
29094 else if (EQ (pointer, intern ("hourglass")))
29095 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29096 else if (EQ (pointer, Qmodeline))
29097 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29098 else
29099 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29100 }
29101
29102 if (cursor != No_Cursor)
29103 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29104 }
29105
29106 #endif /* HAVE_WINDOW_SYSTEM */
29107
29108 /* Take proper action when mouse has moved to the mode or header line
29109 or marginal area AREA of window W, x-position X and y-position Y.
29110 X is relative to the start of the text display area of W, so the
29111 width of bitmap areas and scroll bars must be subtracted to get a
29112 position relative to the start of the mode line. */
29113
29114 static void
29115 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29116 enum window_part area)
29117 {
29118 struct window *w = XWINDOW (window);
29119 struct frame *f = XFRAME (w->frame);
29120 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29121 #ifdef HAVE_WINDOW_SYSTEM
29122 Display_Info *dpyinfo;
29123 #endif
29124 Cursor cursor = No_Cursor;
29125 Lisp_Object pointer = Qnil;
29126 int dx, dy, width, height;
29127 ptrdiff_t charpos;
29128 Lisp_Object string, object = Qnil;
29129 Lisp_Object pos IF_LINT (= Qnil), help;
29130
29131 Lisp_Object mouse_face;
29132 int original_x_pixel = x;
29133 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29134 struct glyph_row *row IF_LINT (= 0);
29135
29136 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29137 {
29138 int x0;
29139 struct glyph *end;
29140
29141 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29142 returns them in row/column units! */
29143 string = mode_line_string (w, area, &x, &y, &charpos,
29144 &object, &dx, &dy, &width, &height);
29145
29146 row = (area == ON_MODE_LINE
29147 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29148 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29149
29150 /* Find the glyph under the mouse pointer. */
29151 if (row->mode_line_p && row->enabled_p)
29152 {
29153 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29154 end = glyph + row->used[TEXT_AREA];
29155
29156 for (x0 = original_x_pixel;
29157 glyph < end && x0 >= glyph->pixel_width;
29158 ++glyph)
29159 x0 -= glyph->pixel_width;
29160
29161 if (glyph >= end)
29162 glyph = NULL;
29163 }
29164 }
29165 else
29166 {
29167 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29168 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29169 returns them in row/column units! */
29170 string = marginal_area_string (w, area, &x, &y, &charpos,
29171 &object, &dx, &dy, &width, &height);
29172 }
29173
29174 help = Qnil;
29175
29176 #ifdef HAVE_WINDOW_SYSTEM
29177 if (IMAGEP (object))
29178 {
29179 Lisp_Object image_map, hotspot;
29180 if ((image_map = Fplist_get (XCDR (object), QCmap),
29181 !NILP (image_map))
29182 && (hotspot = find_hot_spot (image_map, dx, dy),
29183 CONSP (hotspot))
29184 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29185 {
29186 Lisp_Object plist;
29187
29188 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29189 If so, we could look for mouse-enter, mouse-leave
29190 properties in PLIST (and do something...). */
29191 hotspot = XCDR (hotspot);
29192 if (CONSP (hotspot)
29193 && (plist = XCAR (hotspot), CONSP (plist)))
29194 {
29195 pointer = Fplist_get (plist, Qpointer);
29196 if (NILP (pointer))
29197 pointer = Qhand;
29198 help = Fplist_get (plist, Qhelp_echo);
29199 if (!NILP (help))
29200 {
29201 help_echo_string = help;
29202 XSETWINDOW (help_echo_window, w);
29203 help_echo_object = w->contents;
29204 help_echo_pos = charpos;
29205 }
29206 }
29207 }
29208 if (NILP (pointer))
29209 pointer = Fplist_get (XCDR (object), QCpointer);
29210 }
29211 #endif /* HAVE_WINDOW_SYSTEM */
29212
29213 if (STRINGP (string))
29214 pos = make_number (charpos);
29215
29216 /* Set the help text and mouse pointer. If the mouse is on a part
29217 of the mode line without any text (e.g. past the right edge of
29218 the mode line text), use the default help text and pointer. */
29219 if (STRINGP (string) || area == ON_MODE_LINE)
29220 {
29221 /* Arrange to display the help by setting the global variables
29222 help_echo_string, help_echo_object, and help_echo_pos. */
29223 if (NILP (help))
29224 {
29225 if (STRINGP (string))
29226 help = Fget_text_property (pos, Qhelp_echo, string);
29227
29228 if (!NILP (help))
29229 {
29230 help_echo_string = help;
29231 XSETWINDOW (help_echo_window, w);
29232 help_echo_object = string;
29233 help_echo_pos = charpos;
29234 }
29235 else if (area == ON_MODE_LINE)
29236 {
29237 Lisp_Object default_help
29238 = buffer_local_value (Qmode_line_default_help_echo,
29239 w->contents);
29240
29241 if (STRINGP (default_help))
29242 {
29243 help_echo_string = default_help;
29244 XSETWINDOW (help_echo_window, w);
29245 help_echo_object = Qnil;
29246 help_echo_pos = -1;
29247 }
29248 }
29249 }
29250
29251 #ifdef HAVE_WINDOW_SYSTEM
29252 /* Change the mouse pointer according to what is under it. */
29253 if (FRAME_WINDOW_P (f))
29254 {
29255 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29256 || minibuf_level
29257 || NILP (Vresize_mini_windows));
29258
29259 dpyinfo = FRAME_DISPLAY_INFO (f);
29260 if (STRINGP (string))
29261 {
29262 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29263
29264 if (NILP (pointer))
29265 pointer = Fget_text_property (pos, Qpointer, string);
29266
29267 /* Change the mouse pointer according to what is under X/Y. */
29268 if (NILP (pointer)
29269 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29270 {
29271 Lisp_Object map;
29272 map = Fget_text_property (pos, Qlocal_map, string);
29273 if (!KEYMAPP (map))
29274 map = Fget_text_property (pos, Qkeymap, string);
29275 if (!KEYMAPP (map) && draggable)
29276 cursor = dpyinfo->vertical_scroll_bar_cursor;
29277 }
29278 }
29279 else if (draggable)
29280 /* Default mode-line pointer. */
29281 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29282 }
29283 #endif
29284 }
29285
29286 /* Change the mouse face according to what is under X/Y. */
29287 bool mouse_face_shown = false;
29288 if (STRINGP (string))
29289 {
29290 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29291 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29292 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29293 && glyph)
29294 {
29295 Lisp_Object b, e;
29296
29297 struct glyph * tmp_glyph;
29298
29299 int gpos;
29300 int gseq_length;
29301 int total_pixel_width;
29302 ptrdiff_t begpos, endpos, ignore;
29303
29304 int vpos, hpos;
29305
29306 b = Fprevious_single_property_change (make_number (charpos + 1),
29307 Qmouse_face, string, Qnil);
29308 if (NILP (b))
29309 begpos = 0;
29310 else
29311 begpos = XINT (b);
29312
29313 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29314 if (NILP (e))
29315 endpos = SCHARS (string);
29316 else
29317 endpos = XINT (e);
29318
29319 /* Calculate the glyph position GPOS of GLYPH in the
29320 displayed string, relative to the beginning of the
29321 highlighted part of the string.
29322
29323 Note: GPOS is different from CHARPOS. CHARPOS is the
29324 position of GLYPH in the internal string object. A mode
29325 line string format has structures which are converted to
29326 a flattened string by the Emacs Lisp interpreter. The
29327 internal string is an element of those structures. The
29328 displayed string is the flattened string. */
29329 tmp_glyph = row_start_glyph;
29330 while (tmp_glyph < glyph
29331 && (!(EQ (tmp_glyph->object, glyph->object)
29332 && begpos <= tmp_glyph->charpos
29333 && tmp_glyph->charpos < endpos)))
29334 tmp_glyph++;
29335 gpos = glyph - tmp_glyph;
29336
29337 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29338 the highlighted part of the displayed string to which
29339 GLYPH belongs. Note: GSEQ_LENGTH is different from
29340 SCHARS (STRING), because the latter returns the length of
29341 the internal string. */
29342 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29343 tmp_glyph > glyph
29344 && (!(EQ (tmp_glyph->object, glyph->object)
29345 && begpos <= tmp_glyph->charpos
29346 && tmp_glyph->charpos < endpos));
29347 tmp_glyph--)
29348 ;
29349 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29350
29351 /* Calculate the total pixel width of all the glyphs between
29352 the beginning of the highlighted area and GLYPH. */
29353 total_pixel_width = 0;
29354 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29355 total_pixel_width += tmp_glyph->pixel_width;
29356
29357 /* Pre calculation of re-rendering position. Note: X is in
29358 column units here, after the call to mode_line_string or
29359 marginal_area_string. */
29360 hpos = x - gpos;
29361 vpos = (area == ON_MODE_LINE
29362 ? (w->current_matrix)->nrows - 1
29363 : 0);
29364
29365 /* If GLYPH's position is included in the region that is
29366 already drawn in mouse face, we have nothing to do. */
29367 if ( EQ (window, hlinfo->mouse_face_window)
29368 && (!row->reversed_p
29369 ? (hlinfo->mouse_face_beg_col <= hpos
29370 && hpos < hlinfo->mouse_face_end_col)
29371 /* In R2L rows we swap BEG and END, see below. */
29372 : (hlinfo->mouse_face_end_col <= hpos
29373 && hpos < hlinfo->mouse_face_beg_col))
29374 && hlinfo->mouse_face_beg_row == vpos )
29375 return;
29376
29377 if (clear_mouse_face (hlinfo))
29378 cursor = No_Cursor;
29379
29380 if (!row->reversed_p)
29381 {
29382 hlinfo->mouse_face_beg_col = hpos;
29383 hlinfo->mouse_face_beg_x = original_x_pixel
29384 - (total_pixel_width + dx);
29385 hlinfo->mouse_face_end_col = hpos + gseq_length;
29386 hlinfo->mouse_face_end_x = 0;
29387 }
29388 else
29389 {
29390 /* In R2L rows, show_mouse_face expects BEG and END
29391 coordinates to be swapped. */
29392 hlinfo->mouse_face_end_col = hpos;
29393 hlinfo->mouse_face_end_x = original_x_pixel
29394 - (total_pixel_width + dx);
29395 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29396 hlinfo->mouse_face_beg_x = 0;
29397 }
29398
29399 hlinfo->mouse_face_beg_row = vpos;
29400 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29401 hlinfo->mouse_face_past_end = false;
29402 hlinfo->mouse_face_window = window;
29403
29404 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29405 charpos,
29406 0, &ignore,
29407 glyph->face_id,
29408 true);
29409 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29410 mouse_face_shown = true;
29411
29412 if (NILP (pointer))
29413 pointer = Qhand;
29414 }
29415 }
29416
29417 /* If mouse-face doesn't need to be shown, clear any existing
29418 mouse-face. */
29419 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29420 clear_mouse_face (hlinfo);
29421
29422 #ifdef HAVE_WINDOW_SYSTEM
29423 if (FRAME_WINDOW_P (f))
29424 define_frame_cursor1 (f, cursor, pointer);
29425 #endif
29426 }
29427
29428
29429 /* EXPORT:
29430 Take proper action when the mouse has moved to position X, Y on
29431 frame F with regards to highlighting portions of display that have
29432 mouse-face properties. Also de-highlight portions of display where
29433 the mouse was before, set the mouse pointer shape as appropriate
29434 for the mouse coordinates, and activate help echo (tooltips).
29435 X and Y can be negative or out of range. */
29436
29437 void
29438 note_mouse_highlight (struct frame *f, int x, int y)
29439 {
29440 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29441 enum window_part part = ON_NOTHING;
29442 Lisp_Object window;
29443 struct window *w;
29444 Cursor cursor = No_Cursor;
29445 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29446 struct buffer *b;
29447
29448 /* When a menu is active, don't highlight because this looks odd. */
29449 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29450 if (popup_activated ())
29451 return;
29452 #endif
29453
29454 if (!f->glyphs_initialized_p
29455 || f->pointer_invisible)
29456 return;
29457
29458 hlinfo->mouse_face_mouse_x = x;
29459 hlinfo->mouse_face_mouse_y = y;
29460 hlinfo->mouse_face_mouse_frame = f;
29461
29462 if (hlinfo->mouse_face_defer)
29463 return;
29464
29465 /* Which window is that in? */
29466 window = window_from_coordinates (f, x, y, &part, true);
29467
29468 /* If displaying active text in another window, clear that. */
29469 if (! EQ (window, hlinfo->mouse_face_window)
29470 /* Also clear if we move out of text area in same window. */
29471 || (!NILP (hlinfo->mouse_face_window)
29472 && !NILP (window)
29473 && part != ON_TEXT
29474 && part != ON_MODE_LINE
29475 && part != ON_HEADER_LINE))
29476 clear_mouse_face (hlinfo);
29477
29478 /* Not on a window -> return. */
29479 if (!WINDOWP (window))
29480 return;
29481
29482 /* Reset help_echo_string. It will get recomputed below. */
29483 help_echo_string = Qnil;
29484
29485 /* Convert to window-relative pixel coordinates. */
29486 w = XWINDOW (window);
29487 frame_to_window_pixel_xy (w, &x, &y);
29488
29489 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29490 /* Handle tool-bar window differently since it doesn't display a
29491 buffer. */
29492 if (EQ (window, f->tool_bar_window))
29493 {
29494 note_tool_bar_highlight (f, x, y);
29495 return;
29496 }
29497 #endif
29498
29499 /* Mouse is on the mode, header line or margin? */
29500 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29501 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29502 {
29503 note_mode_line_or_margin_highlight (window, x, y, part);
29504
29505 #ifdef HAVE_WINDOW_SYSTEM
29506 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29507 {
29508 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29509 /* Show non-text cursor (Bug#16647). */
29510 goto set_cursor;
29511 }
29512 else
29513 #endif
29514 return;
29515 }
29516
29517 #ifdef HAVE_WINDOW_SYSTEM
29518 if (part == ON_VERTICAL_BORDER)
29519 {
29520 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29521 help_echo_string = build_string ("drag-mouse-1: resize");
29522 }
29523 else if (part == ON_RIGHT_DIVIDER)
29524 {
29525 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29526 help_echo_string = build_string ("drag-mouse-1: resize");
29527 }
29528 else if (part == ON_BOTTOM_DIVIDER)
29529 if (! WINDOW_BOTTOMMOST_P (w)
29530 || minibuf_level
29531 || NILP (Vresize_mini_windows))
29532 {
29533 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29534 help_echo_string = build_string ("drag-mouse-1: resize");
29535 }
29536 else
29537 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29538 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29539 || part == ON_VERTICAL_SCROLL_BAR
29540 || part == ON_HORIZONTAL_SCROLL_BAR)
29541 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29542 else
29543 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29544 #endif
29545
29546 /* Are we in a window whose display is up to date?
29547 And verify the buffer's text has not changed. */
29548 b = XBUFFER (w->contents);
29549 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29550 {
29551 int hpos, vpos, dx, dy, area = LAST_AREA;
29552 ptrdiff_t pos;
29553 struct glyph *glyph;
29554 Lisp_Object object;
29555 Lisp_Object mouse_face = Qnil, position;
29556 Lisp_Object *overlay_vec = NULL;
29557 ptrdiff_t i, noverlays;
29558 struct buffer *obuf;
29559 ptrdiff_t obegv, ozv;
29560 bool same_region;
29561
29562 /* Find the glyph under X/Y. */
29563 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29564
29565 #ifdef HAVE_WINDOW_SYSTEM
29566 /* Look for :pointer property on image. */
29567 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29568 {
29569 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29570 if (img != NULL && IMAGEP (img->spec))
29571 {
29572 Lisp_Object image_map, hotspot;
29573 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29574 !NILP (image_map))
29575 && (hotspot = find_hot_spot (image_map,
29576 glyph->slice.img.x + dx,
29577 glyph->slice.img.y + dy),
29578 CONSP (hotspot))
29579 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29580 {
29581 Lisp_Object plist;
29582
29583 /* Could check XCAR (hotspot) to see if we enter/leave
29584 this hot-spot.
29585 If so, we could look for mouse-enter, mouse-leave
29586 properties in PLIST (and do something...). */
29587 hotspot = XCDR (hotspot);
29588 if (CONSP (hotspot)
29589 && (plist = XCAR (hotspot), CONSP (plist)))
29590 {
29591 pointer = Fplist_get (plist, Qpointer);
29592 if (NILP (pointer))
29593 pointer = Qhand;
29594 help_echo_string = Fplist_get (plist, Qhelp_echo);
29595 if (!NILP (help_echo_string))
29596 {
29597 help_echo_window = window;
29598 help_echo_object = glyph->object;
29599 help_echo_pos = glyph->charpos;
29600 }
29601 }
29602 }
29603 if (NILP (pointer))
29604 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29605 }
29606 }
29607 #endif /* HAVE_WINDOW_SYSTEM */
29608
29609 /* Clear mouse face if X/Y not over text. */
29610 if (glyph == NULL
29611 || area != TEXT_AREA
29612 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29613 /* Glyph's OBJECT is nil for glyphs inserted by the
29614 display engine for its internal purposes, like truncation
29615 and continuation glyphs and blanks beyond the end of
29616 line's text on text terminals. If we are over such a
29617 glyph, we are not over any text. */
29618 || NILP (glyph->object)
29619 /* R2L rows have a stretch glyph at their front, which
29620 stands for no text, whereas L2R rows have no glyphs at
29621 all beyond the end of text. Treat such stretch glyphs
29622 like we do with NULL glyphs in L2R rows. */
29623 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29624 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29625 && glyph->type == STRETCH_GLYPH
29626 && glyph->avoid_cursor_p))
29627 {
29628 if (clear_mouse_face (hlinfo))
29629 cursor = No_Cursor;
29630 #ifdef HAVE_WINDOW_SYSTEM
29631 if (FRAME_WINDOW_P (f) && NILP (pointer))
29632 {
29633 if (area != TEXT_AREA)
29634 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29635 else
29636 pointer = Vvoid_text_area_pointer;
29637 }
29638 #endif
29639 goto set_cursor;
29640 }
29641
29642 pos = glyph->charpos;
29643 object = glyph->object;
29644 if (!STRINGP (object) && !BUFFERP (object))
29645 goto set_cursor;
29646
29647 /* If we get an out-of-range value, return now; avoid an error. */
29648 if (BUFFERP (object) && pos > BUF_Z (b))
29649 goto set_cursor;
29650
29651 /* Make the window's buffer temporarily current for
29652 overlays_at and compute_char_face. */
29653 obuf = current_buffer;
29654 current_buffer = b;
29655 obegv = BEGV;
29656 ozv = ZV;
29657 BEGV = BEG;
29658 ZV = Z;
29659
29660 /* Is this char mouse-active or does it have help-echo? */
29661 position = make_number (pos);
29662
29663 USE_SAFE_ALLOCA;
29664
29665 if (BUFFERP (object))
29666 {
29667 /* Put all the overlays we want in a vector in overlay_vec. */
29668 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29669 /* Sort overlays into increasing priority order. */
29670 noverlays = sort_overlays (overlay_vec, noverlays, w);
29671 }
29672 else
29673 noverlays = 0;
29674
29675 if (NILP (Vmouse_highlight))
29676 {
29677 clear_mouse_face (hlinfo);
29678 goto check_help_echo;
29679 }
29680
29681 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29682
29683 if (same_region)
29684 cursor = No_Cursor;
29685
29686 /* Check mouse-face highlighting. */
29687 if (! same_region
29688 /* If there exists an overlay with mouse-face overlapping
29689 the one we are currently highlighting, we have to
29690 check if we enter the overlapping overlay, and then
29691 highlight only that. */
29692 || (OVERLAYP (hlinfo->mouse_face_overlay)
29693 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29694 {
29695 /* Find the highest priority overlay with a mouse-face. */
29696 Lisp_Object overlay = Qnil;
29697 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29698 {
29699 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29700 if (!NILP (mouse_face))
29701 overlay = overlay_vec[i];
29702 }
29703
29704 /* If we're highlighting the same overlay as before, there's
29705 no need to do that again. */
29706 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29707 goto check_help_echo;
29708 hlinfo->mouse_face_overlay = overlay;
29709
29710 /* Clear the display of the old active region, if any. */
29711 if (clear_mouse_face (hlinfo))
29712 cursor = No_Cursor;
29713
29714 /* If no overlay applies, get a text property. */
29715 if (NILP (overlay))
29716 mouse_face = Fget_text_property (position, Qmouse_face, object);
29717
29718 /* Next, compute the bounds of the mouse highlighting and
29719 display it. */
29720 if (!NILP (mouse_face) && STRINGP (object))
29721 {
29722 /* The mouse-highlighting comes from a display string
29723 with a mouse-face. */
29724 Lisp_Object s, e;
29725 ptrdiff_t ignore;
29726
29727 s = Fprevious_single_property_change
29728 (make_number (pos + 1), Qmouse_face, object, Qnil);
29729 e = Fnext_single_property_change
29730 (position, Qmouse_face, object, Qnil);
29731 if (NILP (s))
29732 s = make_number (0);
29733 if (NILP (e))
29734 e = make_number (SCHARS (object));
29735 mouse_face_from_string_pos (w, hlinfo, object,
29736 XINT (s), XINT (e));
29737 hlinfo->mouse_face_past_end = false;
29738 hlinfo->mouse_face_window = window;
29739 hlinfo->mouse_face_face_id
29740 = face_at_string_position (w, object, pos, 0, &ignore,
29741 glyph->face_id, true);
29742 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29743 cursor = No_Cursor;
29744 }
29745 else
29746 {
29747 /* The mouse-highlighting, if any, comes from an overlay
29748 or text property in the buffer. */
29749 Lisp_Object buffer IF_LINT (= Qnil);
29750 Lisp_Object disp_string IF_LINT (= Qnil);
29751
29752 if (STRINGP (object))
29753 {
29754 /* If we are on a display string with no mouse-face,
29755 check if the text under it has one. */
29756 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29757 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29758 pos = string_buffer_position (object, start);
29759 if (pos > 0)
29760 {
29761 mouse_face = get_char_property_and_overlay
29762 (make_number (pos), Qmouse_face, w->contents, &overlay);
29763 buffer = w->contents;
29764 disp_string = object;
29765 }
29766 }
29767 else
29768 {
29769 buffer = object;
29770 disp_string = Qnil;
29771 }
29772
29773 if (!NILP (mouse_face))
29774 {
29775 Lisp_Object before, after;
29776 Lisp_Object before_string, after_string;
29777 /* To correctly find the limits of mouse highlight
29778 in a bidi-reordered buffer, we must not use the
29779 optimization of limiting the search in
29780 previous-single-property-change and
29781 next-single-property-change, because
29782 rows_from_pos_range needs the real start and end
29783 positions to DTRT in this case. That's because
29784 the first row visible in a window does not
29785 necessarily display the character whose position
29786 is the smallest. */
29787 Lisp_Object lim1
29788 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29789 ? Fmarker_position (w->start)
29790 : Qnil;
29791 Lisp_Object lim2
29792 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29793 ? make_number (BUF_Z (XBUFFER (buffer))
29794 - w->window_end_pos)
29795 : Qnil;
29796
29797 if (NILP (overlay))
29798 {
29799 /* Handle the text property case. */
29800 before = Fprevious_single_property_change
29801 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29802 after = Fnext_single_property_change
29803 (make_number (pos), Qmouse_face, buffer, lim2);
29804 before_string = after_string = Qnil;
29805 }
29806 else
29807 {
29808 /* Handle the overlay case. */
29809 before = Foverlay_start (overlay);
29810 after = Foverlay_end (overlay);
29811 before_string = Foverlay_get (overlay, Qbefore_string);
29812 after_string = Foverlay_get (overlay, Qafter_string);
29813
29814 if (!STRINGP (before_string)) before_string = Qnil;
29815 if (!STRINGP (after_string)) after_string = Qnil;
29816 }
29817
29818 mouse_face_from_buffer_pos (window, hlinfo, pos,
29819 NILP (before)
29820 ? 1
29821 : XFASTINT (before),
29822 NILP (after)
29823 ? BUF_Z (XBUFFER (buffer))
29824 : XFASTINT (after),
29825 before_string, after_string,
29826 disp_string);
29827 cursor = No_Cursor;
29828 }
29829 }
29830 }
29831
29832 check_help_echo:
29833
29834 /* Look for a `help-echo' property. */
29835 if (NILP (help_echo_string)) {
29836 Lisp_Object help, overlay;
29837
29838 /* Check overlays first. */
29839 help = overlay = Qnil;
29840 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29841 {
29842 overlay = overlay_vec[i];
29843 help = Foverlay_get (overlay, Qhelp_echo);
29844 }
29845
29846 if (!NILP (help))
29847 {
29848 help_echo_string = help;
29849 help_echo_window = window;
29850 help_echo_object = overlay;
29851 help_echo_pos = pos;
29852 }
29853 else
29854 {
29855 Lisp_Object obj = glyph->object;
29856 ptrdiff_t charpos = glyph->charpos;
29857
29858 /* Try text properties. */
29859 if (STRINGP (obj)
29860 && charpos >= 0
29861 && charpos < SCHARS (obj))
29862 {
29863 help = Fget_text_property (make_number (charpos),
29864 Qhelp_echo, obj);
29865 if (NILP (help))
29866 {
29867 /* If the string itself doesn't specify a help-echo,
29868 see if the buffer text ``under'' it does. */
29869 struct glyph_row *r
29870 = MATRIX_ROW (w->current_matrix, vpos);
29871 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29872 ptrdiff_t p = string_buffer_position (obj, start);
29873 if (p > 0)
29874 {
29875 help = Fget_char_property (make_number (p),
29876 Qhelp_echo, w->contents);
29877 if (!NILP (help))
29878 {
29879 charpos = p;
29880 obj = w->contents;
29881 }
29882 }
29883 }
29884 }
29885 else if (BUFFERP (obj)
29886 && charpos >= BEGV
29887 && charpos < ZV)
29888 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29889 obj);
29890
29891 if (!NILP (help))
29892 {
29893 help_echo_string = help;
29894 help_echo_window = window;
29895 help_echo_object = obj;
29896 help_echo_pos = charpos;
29897 }
29898 }
29899 }
29900
29901 #ifdef HAVE_WINDOW_SYSTEM
29902 /* Look for a `pointer' property. */
29903 if (FRAME_WINDOW_P (f) && NILP (pointer))
29904 {
29905 /* Check overlays first. */
29906 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29907 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29908
29909 if (NILP (pointer))
29910 {
29911 Lisp_Object obj = glyph->object;
29912 ptrdiff_t charpos = glyph->charpos;
29913
29914 /* Try text properties. */
29915 if (STRINGP (obj)
29916 && charpos >= 0
29917 && charpos < SCHARS (obj))
29918 {
29919 pointer = Fget_text_property (make_number (charpos),
29920 Qpointer, obj);
29921 if (NILP (pointer))
29922 {
29923 /* If the string itself doesn't specify a pointer,
29924 see if the buffer text ``under'' it does. */
29925 struct glyph_row *r
29926 = MATRIX_ROW (w->current_matrix, vpos);
29927 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29928 ptrdiff_t p = string_buffer_position (obj, start);
29929 if (p > 0)
29930 pointer = Fget_char_property (make_number (p),
29931 Qpointer, w->contents);
29932 }
29933 }
29934 else if (BUFFERP (obj)
29935 && charpos >= BEGV
29936 && charpos < ZV)
29937 pointer = Fget_text_property (make_number (charpos),
29938 Qpointer, obj);
29939 }
29940 }
29941 #endif /* HAVE_WINDOW_SYSTEM */
29942
29943 BEGV = obegv;
29944 ZV = ozv;
29945 current_buffer = obuf;
29946 SAFE_FREE ();
29947 }
29948
29949 set_cursor:
29950
29951 #ifdef HAVE_WINDOW_SYSTEM
29952 if (FRAME_WINDOW_P (f))
29953 define_frame_cursor1 (f, cursor, pointer);
29954 #else
29955 /* This is here to prevent a compiler error, about "label at end of
29956 compound statement". */
29957 return;
29958 #endif
29959 }
29960
29961
29962 /* EXPORT for RIF:
29963 Clear any mouse-face on window W. This function is part of the
29964 redisplay interface, and is called from try_window_id and similar
29965 functions to ensure the mouse-highlight is off. */
29966
29967 void
29968 x_clear_window_mouse_face (struct window *w)
29969 {
29970 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29971 Lisp_Object window;
29972
29973 block_input ();
29974 XSETWINDOW (window, w);
29975 if (EQ (window, hlinfo->mouse_face_window))
29976 clear_mouse_face (hlinfo);
29977 unblock_input ();
29978 }
29979
29980
29981 /* EXPORT:
29982 Just discard the mouse face information for frame F, if any.
29983 This is used when the size of F is changed. */
29984
29985 void
29986 cancel_mouse_face (struct frame *f)
29987 {
29988 Lisp_Object window;
29989 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29990
29991 window = hlinfo->mouse_face_window;
29992 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29993 reset_mouse_highlight (hlinfo);
29994 }
29995
29996
29997 \f
29998 /***********************************************************************
29999 Exposure Events
30000 ***********************************************************************/
30001
30002 #ifdef HAVE_WINDOW_SYSTEM
30003
30004 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30005 which intersects rectangle R. R is in window-relative coordinates. */
30006
30007 static void
30008 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30009 enum glyph_row_area area)
30010 {
30011 struct glyph *first = row->glyphs[area];
30012 struct glyph *end = row->glyphs[area] + row->used[area];
30013 struct glyph *last;
30014 int first_x, start_x, x;
30015
30016 if (area == TEXT_AREA && row->fill_line_p)
30017 /* If row extends face to end of line write the whole line. */
30018 draw_glyphs (w, 0, row, area,
30019 0, row->used[area],
30020 DRAW_NORMAL_TEXT, 0);
30021 else
30022 {
30023 /* Set START_X to the window-relative start position for drawing glyphs of
30024 AREA. The first glyph of the text area can be partially visible.
30025 The first glyphs of other areas cannot. */
30026 start_x = window_box_left_offset (w, area);
30027 x = start_x;
30028 if (area == TEXT_AREA)
30029 x += row->x;
30030
30031 /* Find the first glyph that must be redrawn. */
30032 while (first < end
30033 && x + first->pixel_width < r->x)
30034 {
30035 x += first->pixel_width;
30036 ++first;
30037 }
30038
30039 /* Find the last one. */
30040 last = first;
30041 first_x = x;
30042 while (last < end
30043 && x < r->x + r->width)
30044 {
30045 x += last->pixel_width;
30046 ++last;
30047 }
30048
30049 /* Repaint. */
30050 if (last > first)
30051 draw_glyphs (w, first_x - start_x, row, area,
30052 first - row->glyphs[area], last - row->glyphs[area],
30053 DRAW_NORMAL_TEXT, 0);
30054 }
30055 }
30056
30057
30058 /* Redraw the parts of the glyph row ROW on window W intersecting
30059 rectangle R. R is in window-relative coordinates. Value is
30060 true if mouse-face was overwritten. */
30061
30062 static bool
30063 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30064 {
30065 eassert (row->enabled_p);
30066
30067 if (row->mode_line_p || w->pseudo_window_p)
30068 draw_glyphs (w, 0, row, TEXT_AREA,
30069 0, row->used[TEXT_AREA],
30070 DRAW_NORMAL_TEXT, 0);
30071 else
30072 {
30073 if (row->used[LEFT_MARGIN_AREA])
30074 expose_area (w, row, r, LEFT_MARGIN_AREA);
30075 if (row->used[TEXT_AREA])
30076 expose_area (w, row, r, TEXT_AREA);
30077 if (row->used[RIGHT_MARGIN_AREA])
30078 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30079 draw_row_fringe_bitmaps (w, row);
30080 }
30081
30082 return row->mouse_face_p;
30083 }
30084
30085
30086 /* Redraw those parts of glyphs rows during expose event handling that
30087 overlap other rows. Redrawing of an exposed line writes over parts
30088 of lines overlapping that exposed line; this function fixes that.
30089
30090 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30091 row in W's current matrix that is exposed and overlaps other rows.
30092 LAST_OVERLAPPING_ROW is the last such row. */
30093
30094 static void
30095 expose_overlaps (struct window *w,
30096 struct glyph_row *first_overlapping_row,
30097 struct glyph_row *last_overlapping_row,
30098 XRectangle *r)
30099 {
30100 struct glyph_row *row;
30101
30102 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30103 if (row->overlapping_p)
30104 {
30105 eassert (row->enabled_p && !row->mode_line_p);
30106
30107 row->clip = r;
30108 if (row->used[LEFT_MARGIN_AREA])
30109 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30110
30111 if (row->used[TEXT_AREA])
30112 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30113
30114 if (row->used[RIGHT_MARGIN_AREA])
30115 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30116 row->clip = NULL;
30117 }
30118 }
30119
30120
30121 /* Return true if W's cursor intersects rectangle R. */
30122
30123 static bool
30124 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30125 {
30126 XRectangle cr, result;
30127 struct glyph *cursor_glyph;
30128 struct glyph_row *row;
30129
30130 if (w->phys_cursor.vpos >= 0
30131 && w->phys_cursor.vpos < w->current_matrix->nrows
30132 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30133 row->enabled_p)
30134 && row->cursor_in_fringe_p)
30135 {
30136 /* Cursor is in the fringe. */
30137 cr.x = window_box_right_offset (w,
30138 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30139 ? RIGHT_MARGIN_AREA
30140 : TEXT_AREA));
30141 cr.y = row->y;
30142 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30143 cr.height = row->height;
30144 return x_intersect_rectangles (&cr, r, &result);
30145 }
30146
30147 cursor_glyph = get_phys_cursor_glyph (w);
30148 if (cursor_glyph)
30149 {
30150 /* r is relative to W's box, but w->phys_cursor.x is relative
30151 to left edge of W's TEXT area. Adjust it. */
30152 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30153 cr.y = w->phys_cursor.y;
30154 cr.width = cursor_glyph->pixel_width;
30155 cr.height = w->phys_cursor_height;
30156 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30157 I assume the effect is the same -- and this is portable. */
30158 return x_intersect_rectangles (&cr, r, &result);
30159 }
30160 /* If we don't understand the format, pretend we're not in the hot-spot. */
30161 return false;
30162 }
30163
30164
30165 /* EXPORT:
30166 Draw a vertical window border to the right of window W if W doesn't
30167 have vertical scroll bars. */
30168
30169 void
30170 x_draw_vertical_border (struct window *w)
30171 {
30172 struct frame *f = XFRAME (WINDOW_FRAME (w));
30173
30174 /* We could do better, if we knew what type of scroll-bar the adjacent
30175 windows (on either side) have... But we don't :-(
30176 However, I think this works ok. ++KFS 2003-04-25 */
30177
30178 /* Redraw borders between horizontally adjacent windows. Don't
30179 do it for frames with vertical scroll bars because either the
30180 right scroll bar of a window, or the left scroll bar of its
30181 neighbor will suffice as a border. */
30182 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30183 return;
30184
30185 /* Note: It is necessary to redraw both the left and the right
30186 borders, for when only this single window W is being
30187 redisplayed. */
30188 if (!WINDOW_RIGHTMOST_P (w)
30189 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30190 {
30191 int x0, x1, y0, y1;
30192
30193 window_box_edges (w, &x0, &y0, &x1, &y1);
30194 y1 -= 1;
30195
30196 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30197 x1 -= 1;
30198
30199 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30200 }
30201
30202 if (!WINDOW_LEFTMOST_P (w)
30203 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30204 {
30205 int x0, x1, y0, y1;
30206
30207 window_box_edges (w, &x0, &y0, &x1, &y1);
30208 y1 -= 1;
30209
30210 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30211 x0 -= 1;
30212
30213 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30214 }
30215 }
30216
30217
30218 /* Draw window dividers for window W. */
30219
30220 void
30221 x_draw_right_divider (struct window *w)
30222 {
30223 struct frame *f = WINDOW_XFRAME (w);
30224
30225 if (w->mini || w->pseudo_window_p)
30226 return;
30227 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30228 {
30229 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30230 int x1 = WINDOW_RIGHT_EDGE_X (w);
30231 int y0 = WINDOW_TOP_EDGE_Y (w);
30232 /* The bottom divider prevails. */
30233 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30234
30235 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30236 }
30237 }
30238
30239 static void
30240 x_draw_bottom_divider (struct window *w)
30241 {
30242 struct frame *f = XFRAME (WINDOW_FRAME (w));
30243
30244 if (w->mini || w->pseudo_window_p)
30245 return;
30246 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30247 {
30248 int x0 = WINDOW_LEFT_EDGE_X (w);
30249 int x1 = WINDOW_RIGHT_EDGE_X (w);
30250 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30251 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30252
30253 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30254 }
30255 }
30256
30257 /* Redraw the part of window W intersection rectangle FR. Pixel
30258 coordinates in FR are frame-relative. Call this function with
30259 input blocked. Value is true if the exposure overwrites
30260 mouse-face. */
30261
30262 static bool
30263 expose_window (struct window *w, XRectangle *fr)
30264 {
30265 struct frame *f = XFRAME (w->frame);
30266 XRectangle wr, r;
30267 bool mouse_face_overwritten_p = false;
30268
30269 /* If window is not yet fully initialized, do nothing. This can
30270 happen when toolkit scroll bars are used and a window is split.
30271 Reconfiguring the scroll bar will generate an expose for a newly
30272 created window. */
30273 if (w->current_matrix == NULL)
30274 return false;
30275
30276 /* When we're currently updating the window, display and current
30277 matrix usually don't agree. Arrange for a thorough display
30278 later. */
30279 if (w->must_be_updated_p)
30280 {
30281 SET_FRAME_GARBAGED (f);
30282 return false;
30283 }
30284
30285 /* Frame-relative pixel rectangle of W. */
30286 wr.x = WINDOW_LEFT_EDGE_X (w);
30287 wr.y = WINDOW_TOP_EDGE_Y (w);
30288 wr.width = WINDOW_PIXEL_WIDTH (w);
30289 wr.height = WINDOW_PIXEL_HEIGHT (w);
30290
30291 if (x_intersect_rectangles (fr, &wr, &r))
30292 {
30293 int yb = window_text_bottom_y (w);
30294 struct glyph_row *row;
30295 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30296
30297 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30298 r.x, r.y, r.width, r.height));
30299
30300 /* Convert to window coordinates. */
30301 r.x -= WINDOW_LEFT_EDGE_X (w);
30302 r.y -= WINDOW_TOP_EDGE_Y (w);
30303
30304 /* Turn off the cursor. */
30305 bool cursor_cleared_p = (!w->pseudo_window_p
30306 && phys_cursor_in_rect_p (w, &r));
30307 if (cursor_cleared_p)
30308 x_clear_cursor (w);
30309
30310 /* If the row containing the cursor extends face to end of line,
30311 then expose_area might overwrite the cursor outside the
30312 rectangle and thus notice_overwritten_cursor might clear
30313 w->phys_cursor_on_p. We remember the original value and
30314 check later if it is changed. */
30315 bool phys_cursor_on_p = w->phys_cursor_on_p;
30316
30317 /* Update lines intersecting rectangle R. */
30318 first_overlapping_row = last_overlapping_row = NULL;
30319 for (row = w->current_matrix->rows;
30320 row->enabled_p;
30321 ++row)
30322 {
30323 int y0 = row->y;
30324 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30325
30326 if ((y0 >= r.y && y0 < r.y + r.height)
30327 || (y1 > r.y && y1 < r.y + r.height)
30328 || (r.y >= y0 && r.y < y1)
30329 || (r.y + r.height > y0 && r.y + r.height < y1))
30330 {
30331 /* A header line may be overlapping, but there is no need
30332 to fix overlapping areas for them. KFS 2005-02-12 */
30333 if (row->overlapping_p && !row->mode_line_p)
30334 {
30335 if (first_overlapping_row == NULL)
30336 first_overlapping_row = row;
30337 last_overlapping_row = row;
30338 }
30339
30340 row->clip = fr;
30341 if (expose_line (w, row, &r))
30342 mouse_face_overwritten_p = true;
30343 row->clip = NULL;
30344 }
30345 else if (row->overlapping_p)
30346 {
30347 /* We must redraw a row overlapping the exposed area. */
30348 if (y0 < r.y
30349 ? y0 + row->phys_height > r.y
30350 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30351 {
30352 if (first_overlapping_row == NULL)
30353 first_overlapping_row = row;
30354 last_overlapping_row = row;
30355 }
30356 }
30357
30358 if (y1 >= yb)
30359 break;
30360 }
30361
30362 /* Display the mode line if there is one. */
30363 if (WINDOW_WANTS_MODELINE_P (w)
30364 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30365 row->enabled_p)
30366 && row->y < r.y + r.height)
30367 {
30368 if (expose_line (w, row, &r))
30369 mouse_face_overwritten_p = true;
30370 }
30371
30372 if (!w->pseudo_window_p)
30373 {
30374 /* Fix the display of overlapping rows. */
30375 if (first_overlapping_row)
30376 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30377 fr);
30378
30379 /* Draw border between windows. */
30380 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30381 x_draw_right_divider (w);
30382 else
30383 x_draw_vertical_border (w);
30384
30385 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30386 x_draw_bottom_divider (w);
30387
30388 /* Turn the cursor on again. */
30389 if (cursor_cleared_p
30390 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30391 update_window_cursor (w, true);
30392 }
30393 }
30394
30395 return mouse_face_overwritten_p;
30396 }
30397
30398
30399
30400 /* Redraw (parts) of all windows in the window tree rooted at W that
30401 intersect R. R contains frame pixel coordinates. Value is
30402 true if the exposure overwrites mouse-face. */
30403
30404 static bool
30405 expose_window_tree (struct window *w, XRectangle *r)
30406 {
30407 struct frame *f = XFRAME (w->frame);
30408 bool mouse_face_overwritten_p = false;
30409
30410 while (w && !FRAME_GARBAGED_P (f))
30411 {
30412 mouse_face_overwritten_p
30413 |= (WINDOWP (w->contents)
30414 ? expose_window_tree (XWINDOW (w->contents), r)
30415 : expose_window (w, r));
30416
30417 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30418 }
30419
30420 return mouse_face_overwritten_p;
30421 }
30422
30423
30424 /* EXPORT:
30425 Redisplay an exposed area of frame F. X and Y are the upper-left
30426 corner of the exposed rectangle. W and H are width and height of
30427 the exposed area. All are pixel values. W or H zero means redraw
30428 the entire frame. */
30429
30430 void
30431 expose_frame (struct frame *f, int x, int y, int w, int h)
30432 {
30433 XRectangle r;
30434 bool mouse_face_overwritten_p = false;
30435
30436 TRACE ((stderr, "expose_frame "));
30437
30438 /* No need to redraw if frame will be redrawn soon. */
30439 if (FRAME_GARBAGED_P (f))
30440 {
30441 TRACE ((stderr, " garbaged\n"));
30442 return;
30443 }
30444
30445 /* If basic faces haven't been realized yet, there is no point in
30446 trying to redraw anything. This can happen when we get an expose
30447 event while Emacs is starting, e.g. by moving another window. */
30448 if (FRAME_FACE_CACHE (f) == NULL
30449 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30450 {
30451 TRACE ((stderr, " no faces\n"));
30452 return;
30453 }
30454
30455 if (w == 0 || h == 0)
30456 {
30457 r.x = r.y = 0;
30458 r.width = FRAME_TEXT_WIDTH (f);
30459 r.height = FRAME_TEXT_HEIGHT (f);
30460 }
30461 else
30462 {
30463 r.x = x;
30464 r.y = y;
30465 r.width = w;
30466 r.height = h;
30467 }
30468
30469 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30470 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30471
30472 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30473 if (WINDOWP (f->tool_bar_window))
30474 mouse_face_overwritten_p
30475 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30476 #endif
30477
30478 #ifdef HAVE_X_WINDOWS
30479 #ifndef MSDOS
30480 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30481 if (WINDOWP (f->menu_bar_window))
30482 mouse_face_overwritten_p
30483 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30484 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30485 #endif
30486 #endif
30487
30488 /* Some window managers support a focus-follows-mouse style with
30489 delayed raising of frames. Imagine a partially obscured frame,
30490 and moving the mouse into partially obscured mouse-face on that
30491 frame. The visible part of the mouse-face will be highlighted,
30492 then the WM raises the obscured frame. With at least one WM, KDE
30493 2.1, Emacs is not getting any event for the raising of the frame
30494 (even tried with SubstructureRedirectMask), only Expose events.
30495 These expose events will draw text normally, i.e. not
30496 highlighted. Which means we must redo the highlight here.
30497 Subsume it under ``we love X''. --gerd 2001-08-15 */
30498 /* Included in Windows version because Windows most likely does not
30499 do the right thing if any third party tool offers
30500 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30501 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30502 {
30503 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30504 if (f == hlinfo->mouse_face_mouse_frame)
30505 {
30506 int mouse_x = hlinfo->mouse_face_mouse_x;
30507 int mouse_y = hlinfo->mouse_face_mouse_y;
30508 clear_mouse_face (hlinfo);
30509 note_mouse_highlight (f, mouse_x, mouse_y);
30510 }
30511 }
30512 }
30513
30514
30515 /* EXPORT:
30516 Determine the intersection of two rectangles R1 and R2. Return
30517 the intersection in *RESULT. Value is true if RESULT is not
30518 empty. */
30519
30520 bool
30521 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30522 {
30523 XRectangle *left, *right;
30524 XRectangle *upper, *lower;
30525 bool intersection_p = false;
30526
30527 /* Rearrange so that R1 is the left-most rectangle. */
30528 if (r1->x < r2->x)
30529 left = r1, right = r2;
30530 else
30531 left = r2, right = r1;
30532
30533 /* X0 of the intersection is right.x0, if this is inside R1,
30534 otherwise there is no intersection. */
30535 if (right->x <= left->x + left->width)
30536 {
30537 result->x = right->x;
30538
30539 /* The right end of the intersection is the minimum of
30540 the right ends of left and right. */
30541 result->width = (min (left->x + left->width, right->x + right->width)
30542 - result->x);
30543
30544 /* Same game for Y. */
30545 if (r1->y < r2->y)
30546 upper = r1, lower = r2;
30547 else
30548 upper = r2, lower = r1;
30549
30550 /* The upper end of the intersection is lower.y0, if this is inside
30551 of upper. Otherwise, there is no intersection. */
30552 if (lower->y <= upper->y + upper->height)
30553 {
30554 result->y = lower->y;
30555
30556 /* The lower end of the intersection is the minimum of the lower
30557 ends of upper and lower. */
30558 result->height = (min (lower->y + lower->height,
30559 upper->y + upper->height)
30560 - result->y);
30561 intersection_p = true;
30562 }
30563 }
30564
30565 return intersection_p;
30566 }
30567
30568 #endif /* HAVE_WINDOW_SYSTEM */
30569
30570 \f
30571 /***********************************************************************
30572 Initialization
30573 ***********************************************************************/
30574
30575 void
30576 syms_of_xdisp (void)
30577 {
30578 Vwith_echo_area_save_vector = Qnil;
30579 staticpro (&Vwith_echo_area_save_vector);
30580
30581 Vmessage_stack = Qnil;
30582 staticpro (&Vmessage_stack);
30583
30584 /* Non-nil means don't actually do any redisplay. */
30585 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30586
30587 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30588
30589 DEFVAR_BOOL("inhibit-message", inhibit_message,
30590 doc: /* Non-nil means calls to `message' are not displayed.
30591 They are still logged to the *Messages* buffer. */);
30592 inhibit_message = 0;
30593
30594 message_dolog_marker1 = Fmake_marker ();
30595 staticpro (&message_dolog_marker1);
30596 message_dolog_marker2 = Fmake_marker ();
30597 staticpro (&message_dolog_marker2);
30598 message_dolog_marker3 = Fmake_marker ();
30599 staticpro (&message_dolog_marker3);
30600
30601 #ifdef GLYPH_DEBUG
30602 defsubr (&Sdump_frame_glyph_matrix);
30603 defsubr (&Sdump_glyph_matrix);
30604 defsubr (&Sdump_glyph_row);
30605 defsubr (&Sdump_tool_bar_row);
30606 defsubr (&Strace_redisplay);
30607 defsubr (&Strace_to_stderr);
30608 #endif
30609 #ifdef HAVE_WINDOW_SYSTEM
30610 defsubr (&Stool_bar_height);
30611 defsubr (&Slookup_image_map);
30612 #endif
30613 defsubr (&Sline_pixel_height);
30614 defsubr (&Sformat_mode_line);
30615 defsubr (&Sinvisible_p);
30616 defsubr (&Scurrent_bidi_paragraph_direction);
30617 defsubr (&Swindow_text_pixel_size);
30618 defsubr (&Smove_point_visually);
30619 defsubr (&Sbidi_find_overridden_directionality);
30620
30621 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30622 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30623 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30624 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30625 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30626 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30627 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30628 DEFSYM (Qeval, "eval");
30629 DEFSYM (QCdata, ":data");
30630
30631 /* Names of text properties relevant for redisplay. */
30632 DEFSYM (Qdisplay, "display");
30633 DEFSYM (Qspace_width, "space-width");
30634 DEFSYM (Qraise, "raise");
30635 DEFSYM (Qslice, "slice");
30636 DEFSYM (Qspace, "space");
30637 DEFSYM (Qmargin, "margin");
30638 DEFSYM (Qpointer, "pointer");
30639 DEFSYM (Qleft_margin, "left-margin");
30640 DEFSYM (Qright_margin, "right-margin");
30641 DEFSYM (Qcenter, "center");
30642 DEFSYM (Qline_height, "line-height");
30643 DEFSYM (QCalign_to, ":align-to");
30644 DEFSYM (QCrelative_width, ":relative-width");
30645 DEFSYM (QCrelative_height, ":relative-height");
30646 DEFSYM (QCeval, ":eval");
30647 DEFSYM (QCpropertize, ":propertize");
30648 DEFSYM (QCfile, ":file");
30649 DEFSYM (Qfontified, "fontified");
30650 DEFSYM (Qfontification_functions, "fontification-functions");
30651
30652 /* Name of the face used to highlight trailing whitespace. */
30653 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30654
30655 /* Name and number of the face used to highlight escape glyphs. */
30656 DEFSYM (Qescape_glyph, "escape-glyph");
30657
30658 /* Name and number of the face used to highlight non-breaking spaces. */
30659 DEFSYM (Qnobreak_space, "nobreak-space");
30660
30661 /* The symbol 'image' which is the car of the lists used to represent
30662 images in Lisp. Also a tool bar style. */
30663 DEFSYM (Qimage, "image");
30664
30665 /* Tool bar styles. */
30666 DEFSYM (Qtext, "text");
30667 DEFSYM (Qboth, "both");
30668 DEFSYM (Qboth_horiz, "both-horiz");
30669 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30670
30671 /* The image map types. */
30672 DEFSYM (QCmap, ":map");
30673 DEFSYM (QCpointer, ":pointer");
30674 DEFSYM (Qrect, "rect");
30675 DEFSYM (Qcircle, "circle");
30676 DEFSYM (Qpoly, "poly");
30677
30678 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30679 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30680 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30681
30682 DEFSYM (Qgrow_only, "grow-only");
30683 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30684 DEFSYM (Qposition, "position");
30685 DEFSYM (Qbuffer_position, "buffer-position");
30686 DEFSYM (Qobject, "object");
30687
30688 /* Cursor shapes. */
30689 DEFSYM (Qbar, "bar");
30690 DEFSYM (Qhbar, "hbar");
30691 DEFSYM (Qbox, "box");
30692 DEFSYM (Qhollow, "hollow");
30693
30694 /* Pointer shapes. */
30695 DEFSYM (Qhand, "hand");
30696 DEFSYM (Qarrow, "arrow");
30697 /* also Qtext */
30698
30699 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30700
30701 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30702 staticpro (&list_of_error);
30703
30704 /* Values of those variables at last redisplay are stored as
30705 properties on 'overlay-arrow-position' symbol. However, if
30706 Voverlay_arrow_position is a marker, last-arrow-position is its
30707 numerical position. */
30708 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30709 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30710
30711 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30712 properties on a symbol in overlay-arrow-variable-list. */
30713 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30714 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30715
30716 echo_buffer[0] = echo_buffer[1] = Qnil;
30717 staticpro (&echo_buffer[0]);
30718 staticpro (&echo_buffer[1]);
30719
30720 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30721 staticpro (&echo_area_buffer[0]);
30722 staticpro (&echo_area_buffer[1]);
30723
30724 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30725 staticpro (&Vmessages_buffer_name);
30726
30727 mode_line_proptrans_alist = Qnil;
30728 staticpro (&mode_line_proptrans_alist);
30729 mode_line_string_list = Qnil;
30730 staticpro (&mode_line_string_list);
30731 mode_line_string_face = Qnil;
30732 staticpro (&mode_line_string_face);
30733 mode_line_string_face_prop = Qnil;
30734 staticpro (&mode_line_string_face_prop);
30735 Vmode_line_unwind_vector = Qnil;
30736 staticpro (&Vmode_line_unwind_vector);
30737
30738 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30739
30740 help_echo_string = Qnil;
30741 staticpro (&help_echo_string);
30742 help_echo_object = Qnil;
30743 staticpro (&help_echo_object);
30744 help_echo_window = Qnil;
30745 staticpro (&help_echo_window);
30746 previous_help_echo_string = Qnil;
30747 staticpro (&previous_help_echo_string);
30748 help_echo_pos = -1;
30749
30750 DEFSYM (Qright_to_left, "right-to-left");
30751 DEFSYM (Qleft_to_right, "left-to-right");
30752 defsubr (&Sbidi_resolved_levels);
30753
30754 #ifdef HAVE_WINDOW_SYSTEM
30755 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30756 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30757 For example, if a block cursor is over a tab, it will be drawn as
30758 wide as that tab on the display. */);
30759 x_stretch_cursor_p = 0;
30760 #endif
30761
30762 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30763 doc: /* Non-nil means highlight trailing whitespace.
30764 The face used for trailing whitespace is `trailing-whitespace'. */);
30765 Vshow_trailing_whitespace = Qnil;
30766
30767 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30768 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30769 If the value is t, Emacs highlights non-ASCII chars which have the
30770 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30771 or `escape-glyph' face respectively.
30772
30773 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30774 U+2011 (non-breaking hyphen) are affected.
30775
30776 Any other non-nil value means to display these characters as a escape
30777 glyph followed by an ordinary space or hyphen.
30778
30779 A value of nil means no special handling of these characters. */);
30780 Vnobreak_char_display = Qt;
30781
30782 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30783 doc: /* The pointer shape to show in void text areas.
30784 A value of nil means to show the text pointer. Other options are
30785 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30786 `hourglass'. */);
30787 Vvoid_text_area_pointer = Qarrow;
30788
30789 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30790 doc: /* Non-nil means don't actually do any redisplay.
30791 This is used for internal purposes. */);
30792 Vinhibit_redisplay = Qnil;
30793
30794 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30795 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30796 Vglobal_mode_string = Qnil;
30797
30798 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30799 doc: /* Marker for where to display an arrow on top of the buffer text.
30800 This must be the beginning of a line in order to work.
30801 See also `overlay-arrow-string'. */);
30802 Voverlay_arrow_position = Qnil;
30803
30804 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30805 doc: /* String to display as an arrow in non-window frames.
30806 See also `overlay-arrow-position'. */);
30807 Voverlay_arrow_string = build_pure_c_string ("=>");
30808
30809 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30810 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30811 The symbols on this list are examined during redisplay to determine
30812 where to display overlay arrows. */);
30813 Voverlay_arrow_variable_list
30814 = list1 (intern_c_string ("overlay-arrow-position"));
30815
30816 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30817 doc: /* The number of lines to try scrolling a window by when point moves out.
30818 If that fails to bring point back on frame, point is centered instead.
30819 If this is zero, point is always centered after it moves off frame.
30820 If you want scrolling to always be a line at a time, you should set
30821 `scroll-conservatively' to a large value rather than set this to 1. */);
30822
30823 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30824 doc: /* Scroll up to this many lines, to bring point back on screen.
30825 If point moves off-screen, redisplay will scroll by up to
30826 `scroll-conservatively' lines in order to bring point just barely
30827 onto the screen again. If that cannot be done, then redisplay
30828 recenters point as usual.
30829
30830 If the value is greater than 100, redisplay will never recenter point,
30831 but will always scroll just enough text to bring point into view, even
30832 if you move far away.
30833
30834 A value of zero means always recenter point if it moves off screen. */);
30835 scroll_conservatively = 0;
30836
30837 DEFVAR_INT ("scroll-margin", scroll_margin,
30838 doc: /* Number of lines of margin at the top and bottom of a window.
30839 Recenter the window whenever point gets within this many lines
30840 of the top or bottom of the window. */);
30841 scroll_margin = 0;
30842
30843 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30844 doc: /* Pixels per inch value for non-window system displays.
30845 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30846 Vdisplay_pixels_per_inch = make_float (72.0);
30847
30848 #ifdef GLYPH_DEBUG
30849 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30850 #endif
30851
30852 DEFVAR_LISP ("truncate-partial-width-windows",
30853 Vtruncate_partial_width_windows,
30854 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30855 For an integer value, truncate lines in each window narrower than the
30856 full frame width, provided the window width is less than that integer;
30857 otherwise, respect the value of `truncate-lines'.
30858
30859 For any other non-nil value, truncate lines in all windows that do
30860 not span the full frame width.
30861
30862 A value of nil means to respect the value of `truncate-lines'.
30863
30864 If `word-wrap' is enabled, you might want to reduce this. */);
30865 Vtruncate_partial_width_windows = make_number (50);
30866
30867 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30868 doc: /* Maximum buffer size for which line number should be displayed.
30869 If the buffer is bigger than this, the line number does not appear
30870 in the mode line. A value of nil means no limit. */);
30871 Vline_number_display_limit = Qnil;
30872
30873 DEFVAR_INT ("line-number-display-limit-width",
30874 line_number_display_limit_width,
30875 doc: /* Maximum line width (in characters) for line number display.
30876 If the average length of the lines near point is bigger than this, then the
30877 line number may be omitted from the mode line. */);
30878 line_number_display_limit_width = 200;
30879
30880 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30881 doc: /* Non-nil means highlight region even in nonselected windows. */);
30882 highlight_nonselected_windows = false;
30883
30884 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30885 doc: /* Non-nil if more than one frame is visible on this display.
30886 Minibuffer-only frames don't count, but iconified frames do.
30887 This variable is not guaranteed to be accurate except while processing
30888 `frame-title-format' and `icon-title-format'. */);
30889
30890 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30891 doc: /* Template for displaying the title bar of visible frames.
30892 \(Assuming the window manager supports this feature.)
30893
30894 This variable has the same structure as `mode-line-format', except that
30895 the %c and %l constructs are ignored. It is used only on frames for
30896 which no explicit name has been set \(see `modify-frame-parameters'). */);
30897
30898 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30899 doc: /* Template for displaying the title bar of an iconified frame.
30900 \(Assuming the window manager supports this feature.)
30901 This variable has the same structure as `mode-line-format' (which see),
30902 and is used only on frames for which no explicit name has been set
30903 \(see `modify-frame-parameters'). */);
30904 Vicon_title_format
30905 = Vframe_title_format
30906 = listn (CONSTYPE_PURE, 3,
30907 intern_c_string ("multiple-frames"),
30908 build_pure_c_string ("%b"),
30909 listn (CONSTYPE_PURE, 4,
30910 empty_unibyte_string,
30911 intern_c_string ("invocation-name"),
30912 build_pure_c_string ("@"),
30913 intern_c_string ("system-name")));
30914
30915 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30916 doc: /* Maximum number of lines to keep in the message log buffer.
30917 If nil, disable message logging. If t, log messages but don't truncate
30918 the buffer when it becomes large. */);
30919 Vmessage_log_max = make_number (1000);
30920
30921 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30922 doc: /* Functions called before redisplay, if window sizes have changed.
30923 The value should be a list of functions that take one argument.
30924 Just before redisplay, for each frame, if any of its windows have changed
30925 size since the last redisplay, or have been split or deleted,
30926 all the functions in the list are called, with the frame as argument. */);
30927 Vwindow_size_change_functions = Qnil;
30928
30929 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30930 doc: /* List of functions to call before redisplaying a window with scrolling.
30931 Each function is called with two arguments, the window and its new
30932 display-start position.
30933 These functions are called whenever the `window-start' marker is modified,
30934 either to point into another buffer (e.g. via `set-window-buffer') or another
30935 place in the same buffer.
30936 Note that the value of `window-end' is not valid when these functions are
30937 called.
30938
30939 Warning: Do not use this feature to alter the way the window
30940 is scrolled. It is not designed for that, and such use probably won't
30941 work. */);
30942 Vwindow_scroll_functions = Qnil;
30943
30944 DEFVAR_LISP ("window-text-change-functions",
30945 Vwindow_text_change_functions,
30946 doc: /* Functions to call in redisplay when text in the window might change. */);
30947 Vwindow_text_change_functions = Qnil;
30948
30949 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30950 doc: /* Functions called when redisplay of a window reaches the end trigger.
30951 Each function is called with two arguments, the window and the end trigger value.
30952 See `set-window-redisplay-end-trigger'. */);
30953 Vredisplay_end_trigger_functions = Qnil;
30954
30955 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30956 doc: /* Non-nil means autoselect window with mouse pointer.
30957 If nil, do not autoselect windows.
30958 A positive number means delay autoselection by that many seconds: a
30959 window is autoselected only after the mouse has remained in that
30960 window for the duration of the delay.
30961 A negative number has a similar effect, but causes windows to be
30962 autoselected only after the mouse has stopped moving. \(Because of
30963 the way Emacs compares mouse events, you will occasionally wait twice
30964 that time before the window gets selected.\)
30965 Any other value means to autoselect window instantaneously when the
30966 mouse pointer enters it.
30967
30968 Autoselection selects the minibuffer only if it is active, and never
30969 unselects the minibuffer if it is active.
30970
30971 When customizing this variable make sure that the actual value of
30972 `focus-follows-mouse' matches the behavior of your window manager. */);
30973 Vmouse_autoselect_window = Qnil;
30974
30975 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30976 doc: /* Non-nil means automatically resize tool-bars.
30977 This dynamically changes the tool-bar's height to the minimum height
30978 that is needed to make all tool-bar items visible.
30979 If value is `grow-only', the tool-bar's height is only increased
30980 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30981 Vauto_resize_tool_bars = Qt;
30982
30983 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30984 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30985 auto_raise_tool_bar_buttons_p = true;
30986
30987 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30988 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30989 make_cursor_line_fully_visible_p = true;
30990
30991 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30992 doc: /* Border below tool-bar in pixels.
30993 If an integer, use it as the height of the border.
30994 If it is one of `internal-border-width' or `border-width', use the
30995 value of the corresponding frame parameter.
30996 Otherwise, no border is added below the tool-bar. */);
30997 Vtool_bar_border = Qinternal_border_width;
30998
30999 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31000 doc: /* Margin around tool-bar buttons in pixels.
31001 If an integer, use that for both horizontal and vertical margins.
31002 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31003 HORZ specifying the horizontal margin, and VERT specifying the
31004 vertical margin. */);
31005 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31006
31007 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31008 doc: /* Relief thickness of tool-bar buttons. */);
31009 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31010
31011 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31012 doc: /* Tool bar style to use.
31013 It can be one of
31014 image - show images only
31015 text - show text only
31016 both - show both, text below image
31017 both-horiz - show text to the right of the image
31018 text-image-horiz - show text to the left of the image
31019 any other - use system default or image if no system default.
31020
31021 This variable only affects the GTK+ toolkit version of Emacs. */);
31022 Vtool_bar_style = Qnil;
31023
31024 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31025 doc: /* Maximum number of characters a label can have to be shown.
31026 The tool bar style must also show labels for this to have any effect, see
31027 `tool-bar-style'. */);
31028 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31029
31030 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31031 doc: /* List of functions to call to fontify regions of text.
31032 Each function is called with one argument POS. Functions must
31033 fontify a region starting at POS in the current buffer, and give
31034 fontified regions the property `fontified'. */);
31035 Vfontification_functions = Qnil;
31036 Fmake_variable_buffer_local (Qfontification_functions);
31037
31038 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31039 unibyte_display_via_language_environment,
31040 doc: /* Non-nil means display unibyte text according to language environment.
31041 Specifically, this means that raw bytes in the range 160-255 decimal
31042 are displayed by converting them to the equivalent multibyte characters
31043 according to the current language environment. As a result, they are
31044 displayed according to the current fontset.
31045
31046 Note that this variable affects only how these bytes are displayed,
31047 but does not change the fact they are interpreted as raw bytes. */);
31048 unibyte_display_via_language_environment = false;
31049
31050 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31051 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31052 If a float, it specifies a fraction of the mini-window frame's height.
31053 If an integer, it specifies a number of lines. */);
31054 Vmax_mini_window_height = make_float (0.25);
31055
31056 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31057 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31058 A value of nil means don't automatically resize mini-windows.
31059 A value of t means resize them to fit the text displayed in them.
31060 A value of `grow-only', the default, means let mini-windows grow only;
31061 they return to their normal size when the minibuffer is closed, or the
31062 echo area becomes empty. */);
31063 Vresize_mini_windows = Qgrow_only;
31064
31065 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31066 doc: /* Alist specifying how to blink the cursor off.
31067 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31068 `cursor-type' frame-parameter or variable equals ON-STATE,
31069 comparing using `equal', Emacs uses OFF-STATE to specify
31070 how to blink it off. ON-STATE and OFF-STATE are values for
31071 the `cursor-type' frame parameter.
31072
31073 If a frame's ON-STATE has no entry in this list,
31074 the frame's other specifications determine how to blink the cursor off. */);
31075 Vblink_cursor_alist = Qnil;
31076
31077 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31078 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31079 If non-nil, windows are automatically scrolled horizontally to make
31080 point visible. */);
31081 automatic_hscrolling_p = true;
31082 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31083
31084 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31085 doc: /* How many columns away from the window edge point is allowed to get
31086 before automatic hscrolling will horizontally scroll the window. */);
31087 hscroll_margin = 5;
31088
31089 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31090 doc: /* How many columns to scroll the window when point gets too close to the edge.
31091 When point is less than `hscroll-margin' columns from the window
31092 edge, automatic hscrolling will scroll the window by the amount of columns
31093 determined by this variable. If its value is a positive integer, scroll that
31094 many columns. If it's a positive floating-point number, it specifies the
31095 fraction of the window's width to scroll. If it's nil or zero, point will be
31096 centered horizontally after the scroll. Any other value, including negative
31097 numbers, are treated as if the value were zero.
31098
31099 Automatic hscrolling always moves point outside the scroll margin, so if
31100 point was more than scroll step columns inside the margin, the window will
31101 scroll more than the value given by the scroll step.
31102
31103 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31104 and `scroll-right' overrides this variable's effect. */);
31105 Vhscroll_step = make_number (0);
31106
31107 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31108 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31109 Bind this around calls to `message' to let it take effect. */);
31110 message_truncate_lines = false;
31111
31112 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31113 doc: /* Normal hook run to update the menu bar definitions.
31114 Redisplay runs this hook before it redisplays the menu bar.
31115 This is used to update menus such as Buffers, whose contents depend on
31116 various data. */);
31117 Vmenu_bar_update_hook = Qnil;
31118
31119 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31120 doc: /* Frame for which we are updating a menu.
31121 The enable predicate for a menu binding should check this variable. */);
31122 Vmenu_updating_frame = Qnil;
31123
31124 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31125 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31126 inhibit_menubar_update = false;
31127
31128 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31129 doc: /* Prefix prepended to all continuation lines at display time.
31130 The value may be a string, an image, or a stretch-glyph; it is
31131 interpreted in the same way as the value of a `display' text property.
31132
31133 This variable is overridden by any `wrap-prefix' text or overlay
31134 property.
31135
31136 To add a prefix to non-continuation lines, use `line-prefix'. */);
31137 Vwrap_prefix = Qnil;
31138 DEFSYM (Qwrap_prefix, "wrap-prefix");
31139 Fmake_variable_buffer_local (Qwrap_prefix);
31140
31141 DEFVAR_LISP ("line-prefix", Vline_prefix,
31142 doc: /* Prefix prepended to all non-continuation lines at display time.
31143 The value may be a string, an image, or a stretch-glyph; it is
31144 interpreted in the same way as the value of a `display' text property.
31145
31146 This variable is overridden by any `line-prefix' text or overlay
31147 property.
31148
31149 To add a prefix to continuation lines, use `wrap-prefix'. */);
31150 Vline_prefix = Qnil;
31151 DEFSYM (Qline_prefix, "line-prefix");
31152 Fmake_variable_buffer_local (Qline_prefix);
31153
31154 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31155 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31156 inhibit_eval_during_redisplay = false;
31157
31158 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31159 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31160 inhibit_free_realized_faces = false;
31161
31162 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31163 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31164 Intended for use during debugging and for testing bidi display;
31165 see biditest.el in the test suite. */);
31166 inhibit_bidi_mirroring = false;
31167
31168 #ifdef GLYPH_DEBUG
31169 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31170 doc: /* Inhibit try_window_id display optimization. */);
31171 inhibit_try_window_id = false;
31172
31173 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31174 doc: /* Inhibit try_window_reusing display optimization. */);
31175 inhibit_try_window_reusing = false;
31176
31177 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31178 doc: /* Inhibit try_cursor_movement display optimization. */);
31179 inhibit_try_cursor_movement = false;
31180 #endif /* GLYPH_DEBUG */
31181
31182 DEFVAR_INT ("overline-margin", overline_margin,
31183 doc: /* Space between overline and text, in pixels.
31184 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31185 margin to the character height. */);
31186 overline_margin = 2;
31187
31188 DEFVAR_INT ("underline-minimum-offset",
31189 underline_minimum_offset,
31190 doc: /* Minimum distance between baseline and underline.
31191 This can improve legibility of underlined text at small font sizes,
31192 particularly when using variable `x-use-underline-position-properties'
31193 with fonts that specify an UNDERLINE_POSITION relatively close to the
31194 baseline. The default value is 1. */);
31195 underline_minimum_offset = 1;
31196
31197 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31198 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31199 This feature only works when on a window system that can change
31200 cursor shapes. */);
31201 display_hourglass_p = true;
31202
31203 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31204 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31205 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31206
31207 #ifdef HAVE_WINDOW_SYSTEM
31208 hourglass_atimer = NULL;
31209 hourglass_shown_p = false;
31210 #endif /* HAVE_WINDOW_SYSTEM */
31211
31212 /* Name of the face used to display glyphless characters. */
31213 DEFSYM (Qglyphless_char, "glyphless-char");
31214
31215 /* Method symbols for Vglyphless_char_display. */
31216 DEFSYM (Qhex_code, "hex-code");
31217 DEFSYM (Qempty_box, "empty-box");
31218 DEFSYM (Qthin_space, "thin-space");
31219 DEFSYM (Qzero_width, "zero-width");
31220
31221 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31222 doc: /* Function run just before redisplay.
31223 It is called with one argument, which is the set of windows that are to
31224 be redisplayed. This set can be nil (meaning, only the selected window),
31225 or t (meaning all windows). */);
31226 Vpre_redisplay_function = intern ("ignore");
31227
31228 /* Symbol for the purpose of Vglyphless_char_display. */
31229 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31230 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31231
31232 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31233 doc: /* Char-table defining glyphless characters.
31234 Each element, if non-nil, should be one of the following:
31235 an ASCII acronym string: display this string in a box
31236 `hex-code': display the hexadecimal code of a character in a box
31237 `empty-box': display as an empty box
31238 `thin-space': display as 1-pixel width space
31239 `zero-width': don't display
31240 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31241 display method for graphical terminals and text terminals respectively.
31242 GRAPHICAL and TEXT should each have one of the values listed above.
31243
31244 The char-table has one extra slot to control the display of a character for
31245 which no font is found. This slot only takes effect on graphical terminals.
31246 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31247 `thin-space'. The default is `empty-box'.
31248
31249 If a character has a non-nil entry in an active display table, the
31250 display table takes effect; in this case, Emacs does not consult
31251 `glyphless-char-display' at all. */);
31252 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31253 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31254 Qempty_box);
31255
31256 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31257 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31258 Vdebug_on_message = Qnil;
31259
31260 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31261 doc: /* */);
31262 Vredisplay__all_windows_cause
31263 = Fmake_vector (make_number (100), make_number (0));
31264
31265 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31266 doc: /* */);
31267 Vredisplay__mode_lines_cause
31268 = Fmake_vector (make_number (100), make_number (0));
31269 }
31270
31271
31272 /* Initialize this module when Emacs starts. */
31273
31274 void
31275 init_xdisp (void)
31276 {
31277 CHARPOS (this_line_start_pos) = 0;
31278
31279 if (!noninteractive)
31280 {
31281 struct window *m = XWINDOW (minibuf_window);
31282 Lisp_Object frame = m->frame;
31283 struct frame *f = XFRAME (frame);
31284 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31285 struct window *r = XWINDOW (root);
31286 int i;
31287
31288 echo_area_window = minibuf_window;
31289
31290 r->top_line = FRAME_TOP_MARGIN (f);
31291 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31292 r->total_cols = FRAME_COLS (f);
31293 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31294 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31295 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31296
31297 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31298 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31299 m->total_cols = FRAME_COLS (f);
31300 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31301 m->total_lines = 1;
31302 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31303
31304 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31305 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31306 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31307
31308 /* The default ellipsis glyphs `...'. */
31309 for (i = 0; i < 3; ++i)
31310 default_invis_vector[i] = make_number ('.');
31311 }
31312
31313 {
31314 /* Allocate the buffer for frame titles.
31315 Also used for `format-mode-line'. */
31316 int size = 100;
31317 mode_line_noprop_buf = xmalloc (size);
31318 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31319 mode_line_noprop_ptr = mode_line_noprop_buf;
31320 mode_line_target = MODE_LINE_DISPLAY;
31321 }
31322
31323 help_echo_showing_p = false;
31324 }
31325
31326 #ifdef HAVE_WINDOW_SYSTEM
31327
31328 /* Platform-independent portion of hourglass implementation. */
31329
31330 /* Timer function of hourglass_atimer. */
31331
31332 static void
31333 show_hourglass (struct atimer *timer)
31334 {
31335 /* The timer implementation will cancel this timer automatically
31336 after this function has run. Set hourglass_atimer to null
31337 so that we know the timer doesn't have to be canceled. */
31338 hourglass_atimer = NULL;
31339
31340 if (!hourglass_shown_p)
31341 {
31342 Lisp_Object tail, frame;
31343
31344 block_input ();
31345
31346 FOR_EACH_FRAME (tail, frame)
31347 {
31348 struct frame *f = XFRAME (frame);
31349
31350 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31351 && FRAME_RIF (f)->show_hourglass)
31352 FRAME_RIF (f)->show_hourglass (f);
31353 }
31354
31355 hourglass_shown_p = true;
31356 unblock_input ();
31357 }
31358 }
31359
31360 /* Cancel a currently active hourglass timer, and start a new one. */
31361
31362 void
31363 start_hourglass (void)
31364 {
31365 struct timespec delay;
31366
31367 cancel_hourglass ();
31368
31369 if (INTEGERP (Vhourglass_delay)
31370 && XINT (Vhourglass_delay) > 0)
31371 delay = make_timespec (min (XINT (Vhourglass_delay),
31372 TYPE_MAXIMUM (time_t)),
31373 0);
31374 else if (FLOATP (Vhourglass_delay)
31375 && XFLOAT_DATA (Vhourglass_delay) > 0)
31376 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31377 else
31378 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31379
31380 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31381 show_hourglass, NULL);
31382 }
31383
31384 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31385 shown. */
31386
31387 void
31388 cancel_hourglass (void)
31389 {
31390 if (hourglass_atimer)
31391 {
31392 cancel_atimer (hourglass_atimer);
31393 hourglass_atimer = NULL;
31394 }
31395
31396 if (hourglass_shown_p)
31397 {
31398 Lisp_Object tail, frame;
31399
31400 block_input ();
31401
31402 FOR_EACH_FRAME (tail, frame)
31403 {
31404 struct frame *f = XFRAME (frame);
31405
31406 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31407 && FRAME_RIF (f)->hide_hourglass)
31408 FRAME_RIF (f)->hide_hourglass (f);
31409 #ifdef HAVE_NTGUI
31410 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31411 else if (!FRAME_W32_P (f))
31412 w32_arrow_cursor ();
31413 #endif
31414 }
31415
31416 hourglass_shown_p = false;
31417 unblock_input ();
31418 }
31419 }
31420
31421 #endif /* HAVE_WINDOW_SYSTEM */