]> code.delx.au - gnu-emacs/blob - src/xdisp.c
; * src/xdisp.c (redisplay_internal): Improve commentary.
[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 "composite.h"
296 #include "keyboard.h"
297 #include "systime.h"
298 #include "frame.h"
299 #include "window.h"
300 #include "termchar.h"
301 #include "dispextern.h"
302 #include "character.h"
303 #include "buffer.h"
304 #include "charset.h"
305 #include "indent.h"
306 #include "commands.h"
307 #include "keymap.h"
308 #include "disptab.h"
309 #include "termhooks.h"
310 #include "termopts.h"
311 #include "intervals.h"
312 #include "coding.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 /* At each redisplay cycle, we should refresh everything there is to refresh.
438 To do that efficiently, we use many optimizations that try to make sure we
439 don't waste too much time updating things that haven't changed.
440 The coarsest such optimization is that, in the most common cases, we only
441 look at the selected-window.
442
443 To know whether other windows should be considered for redisplay, we use the
444 variable windows_or_buffers_changed: as long as it is 0, it means that we
445 have not noticed anything that should require updating anything else than
446 the selected-window. If it is set to REDISPLAY_SOME, it means that since
447 last redisplay, some changes have been made which could impact other
448 windows. To know which ones need redisplay, every buffer, window, and frame
449 has a `redisplay' bit, which (if true) means that this object needs to be
450 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
451 looking for those `redisplay' bits (actually, there might be some such bits
452 set, but then only on objects which aren't displayed anyway).
453
454 OTOH if it's non-zero we wil have to loop through all windows and then check
455 the `redisplay' bit of the corresponding window, frame, and buffer, in order
456 to decide whether that window needs attention or not. Note that we can't
457 just look at the frame's redisplay bit to decide that the whole frame can be
458 skipped, since even if the frame's redisplay bit is unset, some of its
459 windows's redisplay bits may be set.
460
461 Mostly for historical reasons, windows_or_buffers_changed can also take
462 other non-zero values. In that case, the precise value doesn't matter (it
463 encodes the cause of the setting but is only used for debugging purposes),
464 and what it means is that we shouldn't pay attention to any `redisplay' bits
465 and we should simply try and redisplay every window out there. */
466
467 int windows_or_buffers_changed;
468
469 /* Nonzero if we should redraw the mode lines on the next redisplay.
470 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
471 then only redisplay the mode lines in those buffers/windows/frames where the
472 `redisplay' bit has been set.
473 For any other value, redisplay all mode lines (the number used is then only
474 used to track down the cause for this full-redisplay).
475
476 Since the frame title uses the same %-constructs as the mode line
477 (except %c and %l), if this variable is non-zero, we also consider
478 redisplaying the title of each frame, see x_consider_frame_title.
479
480 The `redisplay' bits are the same as those used for
481 windows_or_buffers_changed, and setting windows_or_buffers_changed also
482 causes recomputation of the mode lines of all those windows. IOW this
483 variable only has an effect if windows_or_buffers_changed is zero, in which
484 case we should only need to redisplay the mode-line of those objects with
485 a `redisplay' bit set but not the window's text content (tho we may still
486 need to refresh the text content of the selected-window). */
487
488 int update_mode_lines;
489
490 /* True after display_mode_line if %l was used and it displayed a
491 line number. */
492
493 static bool line_number_displayed;
494
495 /* The name of the *Messages* buffer, a string. */
496
497 static Lisp_Object Vmessages_buffer_name;
498
499 /* Current, index 0, and last displayed echo area message. Either
500 buffers from echo_buffers, or nil to indicate no message. */
501
502 Lisp_Object echo_area_buffer[2];
503
504 /* The buffers referenced from echo_area_buffer. */
505
506 static Lisp_Object echo_buffer[2];
507
508 /* A vector saved used in with_area_buffer to reduce consing. */
509
510 static Lisp_Object Vwith_echo_area_save_vector;
511
512 /* True means display_echo_area should display the last echo area
513 message again. Set by redisplay_preserve_echo_area. */
514
515 static bool display_last_displayed_message_p;
516
517 /* True if echo area is being used by print; false if being used by
518 message. */
519
520 static bool message_buf_print;
521
522 /* Set to true in clear_message to make redisplay_internal aware
523 of an emptied echo area. */
524
525 static bool message_cleared_p;
526
527 /* A scratch glyph row with contents used for generating truncation
528 glyphs. Also used in direct_output_for_insert. */
529
530 #define MAX_SCRATCH_GLYPHS 100
531 static struct glyph_row scratch_glyph_row;
532 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
533
534 /* Ascent and height of the last line processed by move_it_to. */
535
536 static int last_height;
537
538 /* True if there's a help-echo in the echo area. */
539
540 bool help_echo_showing_p;
541
542 /* The maximum distance to look ahead for text properties. Values
543 that are too small let us call compute_char_face and similar
544 functions too often which is expensive. Values that are too large
545 let us call compute_char_face and alike too often because we
546 might not be interested in text properties that far away. */
547
548 #define TEXT_PROP_DISTANCE_LIMIT 100
549
550 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
551 iterator state and later restore it. This is needed because the
552 bidi iterator on bidi.c keeps a stacked cache of its states, which
553 is really a singleton. When we use scratch iterator objects to
554 move around the buffer, we can cause the bidi cache to be pushed or
555 popped, and therefore we need to restore the cache state when we
556 return to the original iterator. */
557 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
558 do { \
559 if (CACHE) \
560 bidi_unshelve_cache (CACHE, true); \
561 ITCOPY = ITORIG; \
562 CACHE = bidi_shelve_cache (); \
563 } while (false)
564
565 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
566 do { \
567 if (pITORIG != pITCOPY) \
568 *(pITORIG) = *(pITCOPY); \
569 bidi_unshelve_cache (CACHE, false); \
570 CACHE = NULL; \
571 } while (false)
572
573 /* Functions to mark elements as needing redisplay. */
574 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
575
576 void
577 redisplay_other_windows (void)
578 {
579 if (!windows_or_buffers_changed)
580 windows_or_buffers_changed = REDISPLAY_SOME;
581 }
582
583 void
584 wset_redisplay (struct window *w)
585 {
586 /* Beware: selected_window can be nil during early stages. */
587 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
588 redisplay_other_windows ();
589 w->redisplay = true;
590 }
591
592 void
593 fset_redisplay (struct frame *f)
594 {
595 redisplay_other_windows ();
596 f->redisplay = true;
597 }
598
599 void
600 bset_redisplay (struct buffer *b)
601 {
602 int count = buffer_window_count (b);
603 if (count > 0)
604 {
605 /* ... it's visible in other window than selected, */
606 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
607 redisplay_other_windows ();
608 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
609 so that if we later set windows_or_buffers_changed, this buffer will
610 not be omitted. */
611 b->text->redisplay = true;
612 }
613 }
614
615 void
616 bset_update_mode_line (struct buffer *b)
617 {
618 if (!update_mode_lines)
619 update_mode_lines = REDISPLAY_SOME;
620 b->text->redisplay = true;
621 }
622
623 #ifdef GLYPH_DEBUG
624
625 /* True means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 bool trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* True means trace with TRACE_MOVE to stderr. */
634 static bool trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 /* Buffer being redisplayed -- for redisplay_window_error. */
642
643 static struct buffer *displayed_buffer;
644
645 /* Value returned from text property handlers (see below). */
646
647 enum prop_handled
648 {
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
653 };
654
655 /* A description of text properties that redisplay is interested
656 in. */
657
658 struct props
659 {
660 /* The symbol index of the name of the property. */
661 short name;
662
663 /* A unique index for the property. */
664 enum prop_idx idx;
665
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) (struct it *it);
669 };
670
671 static enum prop_handled handle_face_prop (struct it *);
672 static enum prop_handled handle_invisible_prop (struct it *);
673 static enum prop_handled handle_display_prop (struct it *);
674 static enum prop_handled handle_composition_prop (struct it *);
675 static enum prop_handled handle_overlay_change (struct it *);
676 static enum prop_handled handle_fontified_prop (struct it *);
677
678 /* Properties handled by iterators. */
679
680 static struct props it_props[] =
681 {
682 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
686 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
687 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
689 {0, 0, NULL}
690 };
691
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
694
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
696
697 /* Enumeration returned by some move_it_.* functions internally. */
698
699 enum move_it_result
700 {
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
703
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
706
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
709
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
713
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
717
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
720 };
721
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
726
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
729
730 /* Similarly for the image cache. */
731
732 #ifdef HAVE_WINDOW_SYSTEM
733 #define CLEAR_IMAGE_CACHE_COUNT 101
734 static int clear_image_cache_count;
735
736 /* Null glyph slice */
737 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
738 #endif
739
740 /* True while redisplay_internal is in progress. */
741
742 bool redisplaying_p;
743
744 /* If a string, XTread_socket generates an event to display that string.
745 (The display is done in read_char.) */
746
747 Lisp_Object help_echo_string;
748 Lisp_Object help_echo_window;
749 Lisp_Object help_echo_object;
750 ptrdiff_t help_echo_pos;
751
752 /* Temporary variable for XTread_socket. */
753
754 Lisp_Object previous_help_echo_string;
755
756 /* Platform-independent portion of hourglass implementation. */
757
758 #ifdef HAVE_WINDOW_SYSTEM
759
760 /* True means an hourglass cursor is currently shown. */
761 static bool hourglass_shown_p;
762
763 /* If non-null, an asynchronous timer that, when it expires, displays
764 an hourglass cursor on all frames. */
765 static struct atimer *hourglass_atimer;
766
767 #endif /* HAVE_WINDOW_SYSTEM */
768
769 /* Default number of seconds to wait before displaying an hourglass
770 cursor. */
771 #define DEFAULT_HOURGLASS_DELAY 1
772
773 #ifdef HAVE_WINDOW_SYSTEM
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 #endif /* HAVE_WINDOW_SYSTEM */
779
780 /* Function prototypes. */
781
782 static void setup_for_ellipsis (struct it *, int);
783 static void set_iterator_to_next (struct it *, bool);
784 static void mark_window_display_accurate_1 (struct window *, bool);
785 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
786 static bool cursor_row_p (struct glyph_row *);
787 static int redisplay_mode_lines (Lisp_Object, bool);
788
789 static void handle_line_prefix (struct it *);
790
791 static void handle_stop_backwards (struct it *, ptrdiff_t);
792 static void unwind_with_echo_area_buffer (Lisp_Object);
793 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
794 static bool current_message_1 (ptrdiff_t, Lisp_Object);
795 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
796 static void set_message (Lisp_Object);
797 static bool set_message_1 (ptrdiff_t, Lisp_Object);
798 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
799 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
800 static void unwind_redisplay (void);
801 static void extend_face_to_end_of_line (struct it *);
802 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
803 static void push_it (struct it *, struct text_pos *);
804 static void iterate_out_of_display_property (struct it *);
805 static void pop_it (struct it *);
806 static void redisplay_internal (void);
807 static void echo_area_display (bool);
808 static void redisplay_windows (Lisp_Object);
809 static void redisplay_window (Lisp_Object, bool);
810 static Lisp_Object redisplay_window_error (Lisp_Object);
811 static Lisp_Object redisplay_window_0 (Lisp_Object);
812 static Lisp_Object redisplay_window_1 (Lisp_Object);
813 static bool set_cursor_from_row (struct window *, struct glyph_row *,
814 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
815 int, int);
816 static bool update_menu_bar (struct frame *, bool, bool);
817 static bool try_window_reusing_current_matrix (struct window *);
818 static int try_window_id (struct window *);
819 static bool display_line (struct it *);
820 static int display_mode_lines (struct window *);
821 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
822 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
823 Lisp_Object, bool);
824 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
825 Lisp_Object);
826 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
827 static void display_menu_bar (struct window *);
828 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
829 ptrdiff_t *);
830 static int display_string (const char *, Lisp_Object, Lisp_Object,
831 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
832 static void compute_line_metrics (struct it *);
833 static void run_redisplay_end_trigger_hook (struct it *);
834 static bool get_overlay_strings (struct it *, ptrdiff_t);
835 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
836 static void next_overlay_string (struct it *);
837 static void reseat (struct it *, struct text_pos, bool);
838 static void reseat_1 (struct it *, struct text_pos, bool);
839 static bool next_element_from_display_vector (struct it *);
840 static bool next_element_from_string (struct it *);
841 static bool next_element_from_c_string (struct it *);
842 static bool next_element_from_buffer (struct it *);
843 static bool next_element_from_composition (struct it *);
844 static bool next_element_from_image (struct it *);
845 static bool next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static bool get_next_display_element (struct it *);
848 static enum move_it_result
849 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
850 enum move_operation_enum);
851 static void get_visually_first_element (struct it *);
852 static void compute_stop_pos (struct it *);
853 static int face_before_or_after_it_pos (struct it *, bool);
854 static ptrdiff_t next_overlay_change (ptrdiff_t);
855 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
856 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
857 static int handle_single_display_spec (struct it *, Lisp_Object,
858 Lisp_Object, Lisp_Object,
859 struct text_pos *, ptrdiff_t, int, bool);
860 static int underlying_face_id (struct it *);
861
862 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
863 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
864
865 #ifdef HAVE_WINDOW_SYSTEM
866
867 static void update_tool_bar (struct frame *, bool);
868 static void x_draw_bottom_divider (struct window *w);
869 static void notice_overwritten_cursor (struct window *,
870 enum glyph_row_area,
871 int, int, int, int);
872 static int normal_char_height (struct font *, int);
873 static void normal_char_ascent_descent (struct font *, int, int *, int *);
874
875 static void append_stretch_glyph (struct it *, Lisp_Object,
876 int, int, int);
877
878 static Lisp_Object get_it_property (struct it *, Lisp_Object);
879 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
880 struct font *, int, bool);
881
882 #endif /* HAVE_WINDOW_SYSTEM */
883
884 static void produce_special_glyphs (struct it *, enum display_element_type);
885 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
886 static bool coords_in_mouse_face_p (struct window *, int, int);
887
888
889 \f
890 /***********************************************************************
891 Window display dimensions
892 ***********************************************************************/
893
894 /* Return the bottom boundary y-position for text lines in window W.
895 This is the first y position at which a line cannot start.
896 It is relative to the top of the window.
897
898 This is the height of W minus the height of a mode line, if any. */
899
900 int
901 window_text_bottom_y (struct window *w)
902 {
903 int height = WINDOW_PIXEL_HEIGHT (w);
904
905 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
906
907 if (WINDOW_WANTS_MODELINE_P (w))
908 height -= CURRENT_MODE_LINE_HEIGHT (w);
909
910 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
911
912 return height;
913 }
914
915 /* Return the pixel width of display area AREA of window W.
916 ANY_AREA means return the total width of W, not including
917 fringes to the left and right of the window. */
918
919 int
920 window_box_width (struct window *w, enum glyph_row_area area)
921 {
922 int width = w->pixel_width;
923
924 if (!w->pseudo_window_p)
925 {
926 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
927 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
928
929 if (area == TEXT_AREA)
930 width -= (WINDOW_MARGINS_WIDTH (w)
931 + WINDOW_FRINGES_WIDTH (w));
932 else if (area == LEFT_MARGIN_AREA)
933 width = WINDOW_LEFT_MARGIN_WIDTH (w);
934 else if (area == RIGHT_MARGIN_AREA)
935 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
936 }
937
938 /* With wide margins, fringes, etc. we might end up with a negative
939 width, correct that here. */
940 return max (0, width);
941 }
942
943
944 /* Return the pixel height of the display area of window W, not
945 including mode lines of W, if any. */
946
947 int
948 window_box_height (struct window *w)
949 {
950 struct frame *f = XFRAME (w->frame);
951 int height = WINDOW_PIXEL_HEIGHT (w);
952
953 eassert (height >= 0);
954
955 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
956 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
957
958 /* Note: the code below that determines the mode-line/header-line
959 height is essentially the same as that contained in the macro
960 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
961 the appropriate glyph row has its `mode_line_p' flag set,
962 and if it doesn't, uses estimate_mode_line_height instead. */
963
964 if (WINDOW_WANTS_MODELINE_P (w))
965 {
966 struct glyph_row *ml_row
967 = (w->current_matrix && w->current_matrix->rows
968 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
969 : 0);
970 if (ml_row && ml_row->mode_line_p)
971 height -= ml_row->height;
972 else
973 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
974 }
975
976 if (WINDOW_WANTS_HEADER_LINE_P (w))
977 {
978 struct glyph_row *hl_row
979 = (w->current_matrix && w->current_matrix->rows
980 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
981 : 0);
982 if (hl_row && hl_row->mode_line_p)
983 height -= hl_row->height;
984 else
985 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
986 }
987
988 /* With a very small font and a mode-line that's taller than
989 default, we might end up with a negative height. */
990 return max (0, height);
991 }
992
993 /* Return the window-relative coordinate of the left edge of display
994 area AREA of window W. ANY_AREA means return the left edge of the
995 whole window, to the right of the left fringe of W. */
996
997 int
998 window_box_left_offset (struct window *w, enum glyph_row_area area)
999 {
1000 int x;
1001
1002 if (w->pseudo_window_p)
1003 return 0;
1004
1005 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1006
1007 if (area == TEXT_AREA)
1008 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1009 + window_box_width (w, LEFT_MARGIN_AREA));
1010 else if (area == RIGHT_MARGIN_AREA)
1011 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1012 + window_box_width (w, LEFT_MARGIN_AREA)
1013 + window_box_width (w, TEXT_AREA)
1014 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1015 ? 0
1016 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1017 else if (area == LEFT_MARGIN_AREA
1018 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1019 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1020
1021 /* Don't return more than the window's pixel width. */
1022 return min (x, w->pixel_width);
1023 }
1024
1025
1026 /* Return the window-relative coordinate of the right edge of display
1027 area AREA of window W. ANY_AREA means return the right edge of the
1028 whole window, to the left of the right fringe of W. */
1029
1030 static int
1031 window_box_right_offset (struct window *w, enum glyph_row_area area)
1032 {
1033 /* Don't return more than the window's pixel width. */
1034 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1035 w->pixel_width);
1036 }
1037
1038 /* Return the frame-relative coordinate of the left edge of display
1039 area AREA of window W. ANY_AREA means return the left edge of the
1040 whole window, to the right of the left fringe of W. */
1041
1042 int
1043 window_box_left (struct window *w, enum glyph_row_area area)
1044 {
1045 struct frame *f = XFRAME (w->frame);
1046 int x;
1047
1048 if (w->pseudo_window_p)
1049 return FRAME_INTERNAL_BORDER_WIDTH (f);
1050
1051 x = (WINDOW_LEFT_EDGE_X (w)
1052 + window_box_left_offset (w, area));
1053
1054 return x;
1055 }
1056
1057
1058 /* Return the frame-relative coordinate of the right edge of display
1059 area AREA of window W. ANY_AREA means return the right edge of the
1060 whole window, to the left of the right fringe of W. */
1061
1062 int
1063 window_box_right (struct window *w, enum glyph_row_area area)
1064 {
1065 return window_box_left (w, area) + window_box_width (w, area);
1066 }
1067
1068 /* Get the bounding box of the display area AREA of window W, without
1069 mode lines, in frame-relative coordinates. ANY_AREA means the
1070 whole window, not including the left and right fringes of
1071 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1072 coordinates of the upper-left corner of the box. Return in
1073 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1074
1075 void
1076 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1077 int *box_y, int *box_width, int *box_height)
1078 {
1079 if (box_width)
1080 *box_width = window_box_width (w, area);
1081 if (box_height)
1082 *box_height = window_box_height (w);
1083 if (box_x)
1084 *box_x = window_box_left (w, area);
1085 if (box_y)
1086 {
1087 *box_y = WINDOW_TOP_EDGE_Y (w);
1088 if (WINDOW_WANTS_HEADER_LINE_P (w))
1089 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1090 }
1091 }
1092
1093 #ifdef HAVE_WINDOW_SYSTEM
1094
1095 /* Get the bounding box of the display area AREA of window W, without
1096 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1097 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1098 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1099 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1100 box. */
1101
1102 static void
1103 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1104 int *bottom_right_x, int *bottom_right_y)
1105 {
1106 window_box (w, ANY_AREA, top_left_x, top_left_y,
1107 bottom_right_x, bottom_right_y);
1108 *bottom_right_x += *top_left_x;
1109 *bottom_right_y += *top_left_y;
1110 }
1111
1112 #endif /* HAVE_WINDOW_SYSTEM */
1113
1114 /***********************************************************************
1115 Utilities
1116 ***********************************************************************/
1117
1118 /* Return the bottom y-position of the line the iterator IT is in.
1119 This can modify IT's settings. */
1120
1121 int
1122 line_bottom_y (struct it *it)
1123 {
1124 int line_height = it->max_ascent + it->max_descent;
1125 int line_top_y = it->current_y;
1126
1127 if (line_height == 0)
1128 {
1129 if (last_height)
1130 line_height = last_height;
1131 else if (IT_CHARPOS (*it) < ZV)
1132 {
1133 move_it_by_lines (it, 1);
1134 line_height = (it->max_ascent || it->max_descent
1135 ? it->max_ascent + it->max_descent
1136 : last_height);
1137 }
1138 else
1139 {
1140 struct glyph_row *row = it->glyph_row;
1141
1142 /* Use the default character height. */
1143 it->glyph_row = NULL;
1144 it->what = IT_CHARACTER;
1145 it->c = ' ';
1146 it->len = 1;
1147 PRODUCE_GLYPHS (it);
1148 line_height = it->ascent + it->descent;
1149 it->glyph_row = row;
1150 }
1151 }
1152
1153 return line_top_y + line_height;
1154 }
1155
1156 DEFUN ("line-pixel-height", Fline_pixel_height,
1157 Sline_pixel_height, 0, 0, 0,
1158 doc: /* Return height in pixels of text line in the selected window.
1159
1160 Value is the height in pixels of the line at point. */)
1161 (void)
1162 {
1163 struct it it;
1164 struct text_pos pt;
1165 struct window *w = XWINDOW (selected_window);
1166 struct buffer *old_buffer = NULL;
1167 Lisp_Object result;
1168
1169 if (XBUFFER (w->contents) != current_buffer)
1170 {
1171 old_buffer = current_buffer;
1172 set_buffer_internal_1 (XBUFFER (w->contents));
1173 }
1174 SET_TEXT_POS (pt, PT, PT_BYTE);
1175 start_display (&it, w, pt);
1176 it.vpos = it.current_y = 0;
1177 last_height = 0;
1178 result = make_number (line_bottom_y (&it));
1179 if (old_buffer)
1180 set_buffer_internal_1 (old_buffer);
1181
1182 return result;
1183 }
1184
1185 /* Return the default pixel height of text lines in window W. The
1186 value is the canonical height of the W frame's default font, plus
1187 any extra space required by the line-spacing variable or frame
1188 parameter.
1189
1190 Implementation note: this ignores any line-spacing text properties
1191 put on the newline characters. This is because those properties
1192 only affect the _screen_ line ending in the newline (i.e., in a
1193 continued line, only the last screen line will be affected), which
1194 means only a small number of lines in a buffer can ever use this
1195 feature. Since this function is used to compute the default pixel
1196 equivalent of text lines in a window, we can safely ignore those
1197 few lines. For the same reasons, we ignore the line-height
1198 properties. */
1199 int
1200 default_line_pixel_height (struct window *w)
1201 {
1202 struct frame *f = WINDOW_XFRAME (w);
1203 int height = FRAME_LINE_HEIGHT (f);
1204
1205 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1206 {
1207 struct buffer *b = XBUFFER (w->contents);
1208 Lisp_Object val = BVAR (b, extra_line_spacing);
1209
1210 if (NILP (val))
1211 val = BVAR (&buffer_defaults, extra_line_spacing);
1212 if (!NILP (val))
1213 {
1214 if (RANGED_INTEGERP (0, val, INT_MAX))
1215 height += XFASTINT (val);
1216 else if (FLOATP (val))
1217 {
1218 int addon = XFLOAT_DATA (val) * height + 0.5;
1219
1220 if (addon >= 0)
1221 height += addon;
1222 }
1223 }
1224 else
1225 height += f->extra_line_spacing;
1226 }
1227
1228 return height;
1229 }
1230
1231 /* Subroutine of pos_visible_p below. Extracts a display string, if
1232 any, from the display spec given as its argument. */
1233 static Lisp_Object
1234 string_from_display_spec (Lisp_Object spec)
1235 {
1236 if (CONSP (spec))
1237 {
1238 while (CONSP (spec))
1239 {
1240 if (STRINGP (XCAR (spec)))
1241 return XCAR (spec);
1242 spec = XCDR (spec);
1243 }
1244 }
1245 else if (VECTORP (spec))
1246 {
1247 ptrdiff_t i;
1248
1249 for (i = 0; i < ASIZE (spec); i++)
1250 {
1251 if (STRINGP (AREF (spec, i)))
1252 return AREF (spec, i);
1253 }
1254 return Qnil;
1255 }
1256
1257 return spec;
1258 }
1259
1260
1261 /* Limit insanely large values of W->hscroll on frame F to the largest
1262 value that will still prevent first_visible_x and last_visible_x of
1263 'struct it' from overflowing an int. */
1264 static int
1265 window_hscroll_limited (struct window *w, struct frame *f)
1266 {
1267 ptrdiff_t window_hscroll = w->hscroll;
1268 int window_text_width = window_box_width (w, TEXT_AREA);
1269 int colwidth = FRAME_COLUMN_WIDTH (f);
1270
1271 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1272 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1273
1274 return window_hscroll;
1275 }
1276
1277 /* Return true if position CHARPOS is visible in window W.
1278 CHARPOS < 0 means return info about WINDOW_END position.
1279 If visible, set *X and *Y to pixel coordinates of top left corner.
1280 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1281 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1282
1283 bool
1284 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1285 int *rtop, int *rbot, int *rowh, int *vpos)
1286 {
1287 struct it it;
1288 void *itdata = bidi_shelve_cache ();
1289 struct text_pos top;
1290 bool visible_p = false;
1291 struct buffer *old_buffer = NULL;
1292 bool r2l = false;
1293
1294 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1295 return visible_p;
1296
1297 if (XBUFFER (w->contents) != current_buffer)
1298 {
1299 old_buffer = current_buffer;
1300 set_buffer_internal_1 (XBUFFER (w->contents));
1301 }
1302
1303 SET_TEXT_POS_FROM_MARKER (top, w->start);
1304 /* Scrolling a minibuffer window via scroll bar when the echo area
1305 shows long text sometimes resets the minibuffer contents behind
1306 our backs. */
1307 if (CHARPOS (top) > ZV)
1308 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1309
1310 /* Compute exact mode line heights. */
1311 if (WINDOW_WANTS_MODELINE_P (w))
1312 w->mode_line_height
1313 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1314 BVAR (current_buffer, mode_line_format));
1315
1316 if (WINDOW_WANTS_HEADER_LINE_P (w))
1317 w->header_line_height
1318 = display_mode_line (w, HEADER_LINE_FACE_ID,
1319 BVAR (current_buffer, header_line_format));
1320
1321 start_display (&it, w, top);
1322 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1323 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1324
1325 if (charpos >= 0
1326 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1327 && IT_CHARPOS (it) >= charpos)
1328 /* When scanning backwards under bidi iteration, move_it_to
1329 stops at or _before_ CHARPOS, because it stops at or to
1330 the _right_ of the character at CHARPOS. */
1331 || (it.bidi_p && it.bidi_it.scan_dir == -1
1332 && IT_CHARPOS (it) <= charpos)))
1333 {
1334 /* We have reached CHARPOS, or passed it. How the call to
1335 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1336 or covered by a display property, move_it_to stops at the end
1337 of the invisible text, to the right of CHARPOS. (ii) If
1338 CHARPOS is in a display vector, move_it_to stops on its last
1339 glyph. */
1340 int top_x = it.current_x;
1341 int top_y = it.current_y;
1342 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1343 int bottom_y;
1344 struct it save_it;
1345 void *save_it_data = NULL;
1346
1347 /* Calling line_bottom_y may change it.method, it.position, etc. */
1348 SAVE_IT (save_it, it, save_it_data);
1349 last_height = 0;
1350 bottom_y = line_bottom_y (&it);
1351 if (top_y < window_top_y)
1352 visible_p = bottom_y > window_top_y;
1353 else if (top_y < it.last_visible_y)
1354 visible_p = true;
1355 if (bottom_y >= it.last_visible_y
1356 && it.bidi_p && it.bidi_it.scan_dir == -1
1357 && IT_CHARPOS (it) < charpos)
1358 {
1359 /* When the last line of the window is scanned backwards
1360 under bidi iteration, we could be duped into thinking
1361 that we have passed CHARPOS, when in fact move_it_to
1362 simply stopped short of CHARPOS because it reached
1363 last_visible_y. To see if that's what happened, we call
1364 move_it_to again with a slightly larger vertical limit,
1365 and see if it actually moved vertically; if it did, we
1366 didn't really reach CHARPOS, which is beyond window end. */
1367 /* Why 10? because we don't know how many canonical lines
1368 will the height of the next line(s) be. So we guess. */
1369 int ten_more_lines = 10 * default_line_pixel_height (w);
1370
1371 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1372 MOVE_TO_POS | MOVE_TO_Y);
1373 if (it.current_y > top_y)
1374 visible_p = false;
1375
1376 }
1377 RESTORE_IT (&it, &save_it, save_it_data);
1378 if (visible_p)
1379 {
1380 if (it.method == GET_FROM_DISPLAY_VECTOR)
1381 {
1382 /* We stopped on the last glyph of a display vector.
1383 Try and recompute. Hack alert! */
1384 if (charpos < 2 || top.charpos >= charpos)
1385 top_x = it.glyph_row->x;
1386 else
1387 {
1388 struct it it2, it2_prev;
1389 /* The idea is to get to the previous buffer
1390 position, consume the character there, and use
1391 the pixel coordinates we get after that. But if
1392 the previous buffer position is also displayed
1393 from a display vector, we need to consume all of
1394 the glyphs from that display vector. */
1395 start_display (&it2, w, top);
1396 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1397 /* If we didn't get to CHARPOS - 1, there's some
1398 replacing display property at that position, and
1399 we stopped after it. That is exactly the place
1400 whose coordinates we want. */
1401 if (IT_CHARPOS (it2) != charpos - 1)
1402 it2_prev = it2;
1403 else
1404 {
1405 /* Iterate until we get out of the display
1406 vector that displays the character at
1407 CHARPOS - 1. */
1408 do {
1409 get_next_display_element (&it2);
1410 PRODUCE_GLYPHS (&it2);
1411 it2_prev = it2;
1412 set_iterator_to_next (&it2, true);
1413 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1414 && IT_CHARPOS (it2) < charpos);
1415 }
1416 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1417 || it2_prev.current_x > it2_prev.last_visible_x)
1418 top_x = it.glyph_row->x;
1419 else
1420 {
1421 top_x = it2_prev.current_x;
1422 top_y = it2_prev.current_y;
1423 }
1424 }
1425 }
1426 else if (IT_CHARPOS (it) != charpos)
1427 {
1428 Lisp_Object cpos = make_number (charpos);
1429 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1430 Lisp_Object string = string_from_display_spec (spec);
1431 struct text_pos tpos;
1432 bool newline_in_string
1433 = (STRINGP (string)
1434 && memchr (SDATA (string), '\n', SBYTES (string)));
1435
1436 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1437 bool replacing_spec_p
1438 = (!NILP (spec)
1439 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1440 charpos, FRAME_WINDOW_P (it.f)));
1441 /* The tricky code below is needed because there's a
1442 discrepancy between move_it_to and how we set cursor
1443 when PT is at the beginning of a portion of text
1444 covered by a display property or an overlay with a
1445 display property, or the display line ends in a
1446 newline from a display string. move_it_to will stop
1447 _after_ such display strings, whereas
1448 set_cursor_from_row conspires with cursor_row_p to
1449 place the cursor on the first glyph produced from the
1450 display string. */
1451
1452 /* We have overshoot PT because it is covered by a
1453 display property that replaces the text it covers.
1454 If the string includes embedded newlines, we are also
1455 in the wrong display line. Backtrack to the correct
1456 line, where the display property begins. */
1457 if (replacing_spec_p)
1458 {
1459 Lisp_Object startpos, endpos;
1460 EMACS_INT start, end;
1461 struct it it3;
1462
1463 /* Find the first and the last buffer positions
1464 covered by the display string. */
1465 endpos =
1466 Fnext_single_char_property_change (cpos, Qdisplay,
1467 Qnil, Qnil);
1468 startpos =
1469 Fprevious_single_char_property_change (endpos, Qdisplay,
1470 Qnil, Qnil);
1471 start = XFASTINT (startpos);
1472 end = XFASTINT (endpos);
1473 /* Move to the last buffer position before the
1474 display property. */
1475 start_display (&it3, w, top);
1476 if (start > CHARPOS (top))
1477 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1478 /* Move forward one more line if the position before
1479 the display string is a newline or if it is the
1480 rightmost character on a line that is
1481 continued or word-wrapped. */
1482 if (it3.method == GET_FROM_BUFFER
1483 && (it3.c == '\n'
1484 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1485 move_it_by_lines (&it3, 1);
1486 else if (move_it_in_display_line_to (&it3, -1,
1487 it3.current_x
1488 + it3.pixel_width,
1489 MOVE_TO_X)
1490 == MOVE_LINE_CONTINUED)
1491 {
1492 move_it_by_lines (&it3, 1);
1493 /* When we are under word-wrap, the #$@%!
1494 move_it_by_lines moves 2 lines, so we need to
1495 fix that up. */
1496 if (it3.line_wrap == WORD_WRAP)
1497 move_it_by_lines (&it3, -1);
1498 }
1499
1500 /* Record the vertical coordinate of the display
1501 line where we wound up. */
1502 top_y = it3.current_y;
1503 if (it3.bidi_p)
1504 {
1505 /* When characters are reordered for display,
1506 the character displayed to the left of the
1507 display string could be _after_ the display
1508 property in the logical order. Use the
1509 smallest vertical position of these two. */
1510 start_display (&it3, w, top);
1511 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1512 if (it3.current_y < top_y)
1513 top_y = it3.current_y;
1514 }
1515 /* Move from the top of the window to the beginning
1516 of the display line where the display string
1517 begins. */
1518 start_display (&it3, w, top);
1519 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1520 /* If it3_moved stays false after the 'while' loop
1521 below, that means we already were at a newline
1522 before the loop (e.g., the display string begins
1523 with a newline), so we don't need to (and cannot)
1524 inspect the glyphs of it3.glyph_row, because
1525 PRODUCE_GLYPHS will not produce anything for a
1526 newline, and thus it3.glyph_row stays at its
1527 stale content it got at top of the window. */
1528 bool it3_moved = false;
1529 /* Finally, advance the iterator until we hit the
1530 first display element whose character position is
1531 CHARPOS, or until the first newline from the
1532 display string, which signals the end of the
1533 display line. */
1534 while (get_next_display_element (&it3))
1535 {
1536 PRODUCE_GLYPHS (&it3);
1537 if (IT_CHARPOS (it3) == charpos
1538 || ITERATOR_AT_END_OF_LINE_P (&it3))
1539 break;
1540 it3_moved = true;
1541 set_iterator_to_next (&it3, false);
1542 }
1543 top_x = it3.current_x - it3.pixel_width;
1544 /* Normally, we would exit the above loop because we
1545 found the display element whose character
1546 position is CHARPOS. For the contingency that we
1547 didn't, and stopped at the first newline from the
1548 display string, move back over the glyphs
1549 produced from the string, until we find the
1550 rightmost glyph not from the string. */
1551 if (it3_moved
1552 && newline_in_string
1553 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1554 {
1555 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1556 + it3.glyph_row->used[TEXT_AREA];
1557
1558 while (EQ ((g - 1)->object, string))
1559 {
1560 --g;
1561 top_x -= g->pixel_width;
1562 }
1563 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1564 + it3.glyph_row->used[TEXT_AREA]);
1565 }
1566 }
1567 }
1568
1569 *x = top_x;
1570 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1571 *rtop = max (0, window_top_y - top_y);
1572 *rbot = max (0, bottom_y - it.last_visible_y);
1573 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1574 - max (top_y, window_top_y)));
1575 *vpos = it.vpos;
1576 if (it.bidi_it.paragraph_dir == R2L)
1577 r2l = true;
1578 }
1579 }
1580 else
1581 {
1582 /* Either we were asked to provide info about WINDOW_END, or
1583 CHARPOS is in the partially visible glyph row at end of
1584 window. */
1585 struct it it2;
1586 void *it2data = NULL;
1587
1588 SAVE_IT (it2, it, it2data);
1589 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1590 move_it_by_lines (&it, 1);
1591 if (charpos < IT_CHARPOS (it)
1592 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1593 {
1594 visible_p = true;
1595 RESTORE_IT (&it2, &it2, it2data);
1596 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1597 *x = it2.current_x;
1598 *y = it2.current_y + it2.max_ascent - it2.ascent;
1599 *rtop = max (0, -it2.current_y);
1600 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1601 - it.last_visible_y));
1602 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1603 it.last_visible_y)
1604 - max (it2.current_y,
1605 WINDOW_HEADER_LINE_HEIGHT (w))));
1606 *vpos = it2.vpos;
1607 if (it2.bidi_it.paragraph_dir == R2L)
1608 r2l = true;
1609 }
1610 else
1611 bidi_unshelve_cache (it2data, true);
1612 }
1613 bidi_unshelve_cache (itdata, false);
1614
1615 if (old_buffer)
1616 set_buffer_internal_1 (old_buffer);
1617
1618 if (visible_p)
1619 {
1620 if (w->hscroll > 0)
1621 *x -=
1622 window_hscroll_limited (w, WINDOW_XFRAME (w))
1623 * WINDOW_FRAME_COLUMN_WIDTH (w);
1624 /* For lines in an R2L paragraph, we need to mirror the X pixel
1625 coordinate wrt the text area. For the reasons, see the
1626 commentary in buffer_posn_from_coords and the explanation of
1627 the geometry used by the move_it_* functions at the end of
1628 the large commentary near the beginning of this file. */
1629 if (r2l)
1630 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1631 }
1632
1633 #if false
1634 /* Debugging code. */
1635 if (visible_p)
1636 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1637 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1638 else
1639 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1640 #endif
1641
1642 return visible_p;
1643 }
1644
1645
1646 /* Return the next character from STR. Return in *LEN the length of
1647 the character. This is like STRING_CHAR_AND_LENGTH but never
1648 returns an invalid character. If we find one, we return a `?', but
1649 with the length of the invalid character. */
1650
1651 static int
1652 string_char_and_length (const unsigned char *str, int *len)
1653 {
1654 int c;
1655
1656 c = STRING_CHAR_AND_LENGTH (str, *len);
1657 if (!CHAR_VALID_P (c))
1658 /* We may not change the length here because other places in Emacs
1659 don't use this function, i.e. they silently accept invalid
1660 characters. */
1661 c = '?';
1662
1663 return c;
1664 }
1665
1666
1667
1668 /* Given a position POS containing a valid character and byte position
1669 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1670
1671 static struct text_pos
1672 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1673 {
1674 eassert (STRINGP (string) && nchars >= 0);
1675
1676 if (STRING_MULTIBYTE (string))
1677 {
1678 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1679 int len;
1680
1681 while (nchars--)
1682 {
1683 string_char_and_length (p, &len);
1684 p += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1687 }
1688 }
1689 else
1690 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1691
1692 return pos;
1693 }
1694
1695
1696 /* Value is the text position, i.e. character and byte position,
1697 for character position CHARPOS in STRING. */
1698
1699 static struct text_pos
1700 string_pos (ptrdiff_t charpos, Lisp_Object string)
1701 {
1702 struct text_pos pos;
1703 eassert (STRINGP (string));
1704 eassert (charpos >= 0);
1705 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1706 return pos;
1707 }
1708
1709
1710 /* Value is a text position, i.e. character and byte position, for
1711 character position CHARPOS in C string S. MULTIBYTE_P
1712 means recognize multibyte characters. */
1713
1714 static struct text_pos
1715 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1716 {
1717 struct text_pos pos;
1718
1719 eassert (s != NULL);
1720 eassert (charpos >= 0);
1721
1722 if (multibyte_p)
1723 {
1724 int len;
1725
1726 SET_TEXT_POS (pos, 0, 0);
1727 while (charpos--)
1728 {
1729 string_char_and_length ((const unsigned char *) s, &len);
1730 s += len;
1731 CHARPOS (pos) += 1;
1732 BYTEPOS (pos) += len;
1733 }
1734 }
1735 else
1736 SET_TEXT_POS (pos, charpos, charpos);
1737
1738 return pos;
1739 }
1740
1741
1742 /* Value is the number of characters in C string S. MULTIBYTE_P
1743 means recognize multibyte characters. */
1744
1745 static ptrdiff_t
1746 number_of_chars (const char *s, bool multibyte_p)
1747 {
1748 ptrdiff_t nchars;
1749
1750 if (multibyte_p)
1751 {
1752 ptrdiff_t rest = strlen (s);
1753 int len;
1754 const unsigned char *p = (const unsigned char *) s;
1755
1756 for (nchars = 0; rest > 0; ++nchars)
1757 {
1758 string_char_and_length (p, &len);
1759 rest -= len, p += len;
1760 }
1761 }
1762 else
1763 nchars = strlen (s);
1764
1765 return nchars;
1766 }
1767
1768
1769 /* Compute byte position NEWPOS->bytepos corresponding to
1770 NEWPOS->charpos. POS is a known position in string STRING.
1771 NEWPOS->charpos must be >= POS.charpos. */
1772
1773 static void
1774 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1775 {
1776 eassert (STRINGP (string));
1777 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1778
1779 if (STRING_MULTIBYTE (string))
1780 *newpos = string_pos_nchars_ahead (pos, string,
1781 CHARPOS (*newpos) - CHARPOS (pos));
1782 else
1783 BYTEPOS (*newpos) = CHARPOS (*newpos);
1784 }
1785
1786 /* EXPORT:
1787 Return an estimation of the pixel height of mode or header lines on
1788 frame F. FACE_ID specifies what line's height to estimate. */
1789
1790 int
1791 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1792 {
1793 #ifdef HAVE_WINDOW_SYSTEM
1794 if (FRAME_WINDOW_P (f))
1795 {
1796 int height = FONT_HEIGHT (FRAME_FONT (f));
1797
1798 /* This function is called so early when Emacs starts that the face
1799 cache and mode line face are not yet initialized. */
1800 if (FRAME_FACE_CACHE (f))
1801 {
1802 struct face *face = FACE_FROM_ID (f, face_id);
1803 if (face)
1804 {
1805 if (face->font)
1806 height = normal_char_height (face->font, -1);
1807 if (face->box_line_width > 0)
1808 height += 2 * face->box_line_width;
1809 }
1810 }
1811
1812 return height;
1813 }
1814 #endif
1815
1816 return 1;
1817 }
1818
1819 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1820 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1821 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1822 not force the value into range. */
1823
1824 void
1825 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1826 NativeRectangle *bounds, bool noclip)
1827 {
1828
1829 #ifdef HAVE_WINDOW_SYSTEM
1830 if (FRAME_WINDOW_P (f))
1831 {
1832 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1833 even for negative values. */
1834 if (pix_x < 0)
1835 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1836 if (pix_y < 0)
1837 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1838
1839 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1840 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1841
1842 if (bounds)
1843 STORE_NATIVE_RECT (*bounds,
1844 FRAME_COL_TO_PIXEL_X (f, pix_x),
1845 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1846 FRAME_COLUMN_WIDTH (f) - 1,
1847 FRAME_LINE_HEIGHT (f) - 1);
1848
1849 /* PXW: Should we clip pixels before converting to columns/lines? */
1850 if (!noclip)
1851 {
1852 if (pix_x < 0)
1853 pix_x = 0;
1854 else if (pix_x > FRAME_TOTAL_COLS (f))
1855 pix_x = FRAME_TOTAL_COLS (f);
1856
1857 if (pix_y < 0)
1858 pix_y = 0;
1859 else if (pix_y > FRAME_TOTAL_LINES (f))
1860 pix_y = FRAME_TOTAL_LINES (f);
1861 }
1862 }
1863 #endif
1864
1865 *x = pix_x;
1866 *y = pix_y;
1867 }
1868
1869
1870 /* Find the glyph under window-relative coordinates X/Y in window W.
1871 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1872 strings. Return in *HPOS and *VPOS the row and column number of
1873 the glyph found. Return in *AREA the glyph area containing X.
1874 Value is a pointer to the glyph found or null if X/Y is not on
1875 text, or we can't tell because W's current matrix is not up to
1876 date. */
1877
1878 static struct glyph *
1879 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1880 int *dx, int *dy, int *area)
1881 {
1882 struct glyph *glyph, *end;
1883 struct glyph_row *row = NULL;
1884 int x0, i;
1885
1886 /* Find row containing Y. Give up if some row is not enabled. */
1887 for (i = 0; i < w->current_matrix->nrows; ++i)
1888 {
1889 row = MATRIX_ROW (w->current_matrix, i);
1890 if (!row->enabled_p)
1891 return NULL;
1892 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1893 break;
1894 }
1895
1896 *vpos = i;
1897 *hpos = 0;
1898
1899 /* Give up if Y is not in the window. */
1900 if (i == w->current_matrix->nrows)
1901 return NULL;
1902
1903 /* Get the glyph area containing X. */
1904 if (w->pseudo_window_p)
1905 {
1906 *area = TEXT_AREA;
1907 x0 = 0;
1908 }
1909 else
1910 {
1911 if (x < window_box_left_offset (w, TEXT_AREA))
1912 {
1913 *area = LEFT_MARGIN_AREA;
1914 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1915 }
1916 else if (x < window_box_right_offset (w, TEXT_AREA))
1917 {
1918 *area = TEXT_AREA;
1919 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1920 }
1921 else
1922 {
1923 *area = RIGHT_MARGIN_AREA;
1924 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1925 }
1926 }
1927
1928 /* Find glyph containing X. */
1929 glyph = row->glyphs[*area];
1930 end = glyph + row->used[*area];
1931 x -= x0;
1932 while (glyph < end && x >= glyph->pixel_width)
1933 {
1934 x -= glyph->pixel_width;
1935 ++glyph;
1936 }
1937
1938 if (glyph == end)
1939 return NULL;
1940
1941 if (dx)
1942 {
1943 *dx = x;
1944 *dy = y - (row->y + row->ascent - glyph->ascent);
1945 }
1946
1947 *hpos = glyph - row->glyphs[*area];
1948 return glyph;
1949 }
1950
1951 /* Convert frame-relative x/y to coordinates relative to window W.
1952 Takes pseudo-windows into account. */
1953
1954 static void
1955 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1956 {
1957 if (w->pseudo_window_p)
1958 {
1959 /* A pseudo-window is always full-width, and starts at the
1960 left edge of the frame, plus a frame border. */
1961 struct frame *f = XFRAME (w->frame);
1962 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1963 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1964 }
1965 else
1966 {
1967 *x -= WINDOW_LEFT_EDGE_X (w);
1968 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1969 }
1970 }
1971
1972 #ifdef HAVE_WINDOW_SYSTEM
1973
1974 /* EXPORT:
1975 Return in RECTS[] at most N clipping rectangles for glyph string S.
1976 Return the number of stored rectangles. */
1977
1978 int
1979 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1980 {
1981 XRectangle r;
1982
1983 if (n <= 0)
1984 return 0;
1985
1986 if (s->row->full_width_p)
1987 {
1988 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1989 r.x = WINDOW_LEFT_EDGE_X (s->w);
1990 if (s->row->mode_line_p)
1991 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
1992 else
1993 r.width = WINDOW_PIXEL_WIDTH (s->w);
1994
1995 /* Unless displaying a mode or menu bar line, which are always
1996 fully visible, clip to the visible part of the row. */
1997 if (s->w->pseudo_window_p)
1998 r.height = s->row->visible_height;
1999 else
2000 r.height = s->height;
2001 }
2002 else
2003 {
2004 /* This is a text line that may be partially visible. */
2005 r.x = window_box_left (s->w, s->area);
2006 r.width = window_box_width (s->w, s->area);
2007 r.height = s->row->visible_height;
2008 }
2009
2010 if (s->clip_head)
2011 if (r.x < s->clip_head->x)
2012 {
2013 if (r.width >= s->clip_head->x - r.x)
2014 r.width -= s->clip_head->x - r.x;
2015 else
2016 r.width = 0;
2017 r.x = s->clip_head->x;
2018 }
2019 if (s->clip_tail)
2020 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2021 {
2022 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2023 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2024 else
2025 r.width = 0;
2026 }
2027
2028 /* If S draws overlapping rows, it's sufficient to use the top and
2029 bottom of the window for clipping because this glyph string
2030 intentionally draws over other lines. */
2031 if (s->for_overlaps)
2032 {
2033 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2034 r.height = window_text_bottom_y (s->w) - r.y;
2035
2036 /* Alas, the above simple strategy does not work for the
2037 environments with anti-aliased text: if the same text is
2038 drawn onto the same place multiple times, it gets thicker.
2039 If the overlap we are processing is for the erased cursor, we
2040 take the intersection with the rectangle of the cursor. */
2041 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2042 {
2043 XRectangle rc, r_save = r;
2044
2045 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2046 rc.y = s->w->phys_cursor.y;
2047 rc.width = s->w->phys_cursor_width;
2048 rc.height = s->w->phys_cursor_height;
2049
2050 x_intersect_rectangles (&r_save, &rc, &r);
2051 }
2052 }
2053 else
2054 {
2055 /* Don't use S->y for clipping because it doesn't take partially
2056 visible lines into account. For example, it can be negative for
2057 partially visible lines at the top of a window. */
2058 if (!s->row->full_width_p
2059 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2060 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2061 else
2062 r.y = max (0, s->row->y);
2063 }
2064
2065 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2066
2067 /* If drawing the cursor, don't let glyph draw outside its
2068 advertised boundaries. Cleartype does this under some circumstances. */
2069 if (s->hl == DRAW_CURSOR)
2070 {
2071 struct glyph *glyph = s->first_glyph;
2072 int height, max_y;
2073
2074 if (s->x > r.x)
2075 {
2076 if (r.width >= s->x - r.x)
2077 r.width -= s->x - r.x;
2078 else /* R2L hscrolled row with cursor outside text area */
2079 r.width = 0;
2080 r.x = s->x;
2081 }
2082 r.width = min (r.width, glyph->pixel_width);
2083
2084 /* If r.y is below window bottom, ensure that we still see a cursor. */
2085 height = min (glyph->ascent + glyph->descent,
2086 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2087 max_y = window_text_bottom_y (s->w) - height;
2088 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2089 if (s->ybase - glyph->ascent > max_y)
2090 {
2091 r.y = max_y;
2092 r.height = height;
2093 }
2094 else
2095 {
2096 /* Don't draw cursor glyph taller than our actual glyph. */
2097 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2098 if (height < r.height)
2099 {
2100 max_y = r.y + r.height;
2101 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2102 r.height = min (max_y - r.y, height);
2103 }
2104 }
2105 }
2106
2107 if (s->row->clip)
2108 {
2109 XRectangle r_save = r;
2110
2111 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2112 r.width = 0;
2113 }
2114
2115 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2116 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2117 {
2118 #ifdef CONVERT_FROM_XRECT
2119 CONVERT_FROM_XRECT (r, *rects);
2120 #else
2121 *rects = r;
2122 #endif
2123 return 1;
2124 }
2125 else
2126 {
2127 /* If we are processing overlapping and allowed to return
2128 multiple clipping rectangles, we exclude the row of the glyph
2129 string from the clipping rectangle. This is to avoid drawing
2130 the same text on the environment with anti-aliasing. */
2131 #ifdef CONVERT_FROM_XRECT
2132 XRectangle rs[2];
2133 #else
2134 XRectangle *rs = rects;
2135 #endif
2136 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2137
2138 if (s->for_overlaps & OVERLAPS_PRED)
2139 {
2140 rs[i] = r;
2141 if (r.y + r.height > row_y)
2142 {
2143 if (r.y < row_y)
2144 rs[i].height = row_y - r.y;
2145 else
2146 rs[i].height = 0;
2147 }
2148 i++;
2149 }
2150 if (s->for_overlaps & OVERLAPS_SUCC)
2151 {
2152 rs[i] = r;
2153 if (r.y < row_y + s->row->visible_height)
2154 {
2155 if (r.y + r.height > row_y + s->row->visible_height)
2156 {
2157 rs[i].y = row_y + s->row->visible_height;
2158 rs[i].height = r.y + r.height - rs[i].y;
2159 }
2160 else
2161 rs[i].height = 0;
2162 }
2163 i++;
2164 }
2165
2166 n = i;
2167 #ifdef CONVERT_FROM_XRECT
2168 for (i = 0; i < n; i++)
2169 CONVERT_FROM_XRECT (rs[i], rects[i]);
2170 #endif
2171 return n;
2172 }
2173 }
2174
2175 /* EXPORT:
2176 Return in *NR the clipping rectangle for glyph string S. */
2177
2178 void
2179 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2180 {
2181 get_glyph_string_clip_rects (s, nr, 1);
2182 }
2183
2184
2185 /* EXPORT:
2186 Return the position and height of the phys cursor in window W.
2187 Set w->phys_cursor_width to width of phys cursor.
2188 */
2189
2190 void
2191 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2192 struct glyph *glyph, int *xp, int *yp, int *heightp)
2193 {
2194 struct frame *f = XFRAME (WINDOW_FRAME (w));
2195 int x, y, wd, h, h0, y0, ascent;
2196
2197 /* Compute the width of the rectangle to draw. If on a stretch
2198 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2199 rectangle as wide as the glyph, but use a canonical character
2200 width instead. */
2201 wd = glyph->pixel_width;
2202
2203 x = w->phys_cursor.x;
2204 if (x < 0)
2205 {
2206 wd += x;
2207 x = 0;
2208 }
2209
2210 if (glyph->type == STRETCH_GLYPH
2211 && !x_stretch_cursor_p)
2212 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2213 w->phys_cursor_width = wd;
2214
2215 /* Don't let the hollow cursor glyph descend below the glyph row's
2216 ascent value, lest the hollow cursor looks funny. */
2217 y = w->phys_cursor.y;
2218 ascent = row->ascent;
2219 if (row->ascent < glyph->ascent)
2220 {
2221 y =- glyph->ascent - row->ascent;
2222 ascent = glyph->ascent;
2223 }
2224
2225 /* If y is below window bottom, ensure that we still see a cursor. */
2226 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2227
2228 h = max (h0, ascent + glyph->descent);
2229 h0 = min (h0, ascent + glyph->descent);
2230
2231 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2232 if (y < y0)
2233 {
2234 h = max (h - (y0 - y) + 1, h0);
2235 y = y0 - 1;
2236 }
2237 else
2238 {
2239 y0 = window_text_bottom_y (w) - h0;
2240 if (y > y0)
2241 {
2242 h += y - y0;
2243 y = y0;
2244 }
2245 }
2246
2247 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2248 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2249 *heightp = h;
2250 }
2251
2252 /*
2253 * Remember which glyph the mouse is over.
2254 */
2255
2256 void
2257 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2258 {
2259 Lisp_Object window;
2260 struct window *w;
2261 struct glyph_row *r, *gr, *end_row;
2262 enum window_part part;
2263 enum glyph_row_area area;
2264 int x, y, width, height;
2265
2266 /* Try to determine frame pixel position and size of the glyph under
2267 frame pixel coordinates X/Y on frame F. */
2268
2269 if (window_resize_pixelwise)
2270 {
2271 width = height = 1;
2272 goto virtual_glyph;
2273 }
2274 else if (!f->glyphs_initialized_p
2275 || (window = window_from_coordinates (f, gx, gy, &part, false),
2276 NILP (window)))
2277 {
2278 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2279 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2280 goto virtual_glyph;
2281 }
2282
2283 w = XWINDOW (window);
2284 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2285 height = WINDOW_FRAME_LINE_HEIGHT (w);
2286
2287 x = window_relative_x_coord (w, part, gx);
2288 y = gy - WINDOW_TOP_EDGE_Y (w);
2289
2290 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2291 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2292
2293 if (w->pseudo_window_p)
2294 {
2295 area = TEXT_AREA;
2296 part = ON_MODE_LINE; /* Don't adjust margin. */
2297 goto text_glyph;
2298 }
2299
2300 switch (part)
2301 {
2302 case ON_LEFT_MARGIN:
2303 area = LEFT_MARGIN_AREA;
2304 goto text_glyph;
2305
2306 case ON_RIGHT_MARGIN:
2307 area = RIGHT_MARGIN_AREA;
2308 goto text_glyph;
2309
2310 case ON_HEADER_LINE:
2311 case ON_MODE_LINE:
2312 gr = (part == ON_HEADER_LINE
2313 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2314 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2315 gy = gr->y;
2316 area = TEXT_AREA;
2317 goto text_glyph_row_found;
2318
2319 case ON_TEXT:
2320 area = TEXT_AREA;
2321
2322 text_glyph:
2323 gr = 0; gy = 0;
2324 for (; r <= end_row && r->enabled_p; ++r)
2325 if (r->y + r->height > y)
2326 {
2327 gr = r; gy = r->y;
2328 break;
2329 }
2330
2331 text_glyph_row_found:
2332 if (gr && gy <= y)
2333 {
2334 struct glyph *g = gr->glyphs[area];
2335 struct glyph *end = g + gr->used[area];
2336
2337 height = gr->height;
2338 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2339 if (gx + g->pixel_width > x)
2340 break;
2341
2342 if (g < end)
2343 {
2344 if (g->type == IMAGE_GLYPH)
2345 {
2346 /* Don't remember when mouse is over image, as
2347 image may have hot-spots. */
2348 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2349 return;
2350 }
2351 width = g->pixel_width;
2352 }
2353 else
2354 {
2355 /* Use nominal char spacing at end of line. */
2356 x -= gx;
2357 gx += (x / width) * width;
2358 }
2359
2360 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2361 {
2362 gx += window_box_left_offset (w, area);
2363 /* Don't expand over the modeline to make sure the vertical
2364 drag cursor is shown early enough. */
2365 height = min (height,
2366 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2367 }
2368 }
2369 else
2370 {
2371 /* Use nominal line height at end of window. */
2372 gx = (x / width) * width;
2373 y -= gy;
2374 gy += (y / height) * height;
2375 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2376 /* See comment above. */
2377 height = min (height,
2378 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2379 }
2380 break;
2381
2382 case ON_LEFT_FRINGE:
2383 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2384 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2385 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2386 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2387 goto row_glyph;
2388
2389 case ON_RIGHT_FRINGE:
2390 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2392 : window_box_right_offset (w, TEXT_AREA));
2393 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2394 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2395 && !WINDOW_RIGHTMOST_P (w))
2396 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2397 /* Make sure the vertical border can get her own glyph to the
2398 right of the one we build here. */
2399 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2400 else
2401 width = WINDOW_PIXEL_WIDTH (w) - gx;
2402 else
2403 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2404
2405 goto row_glyph;
2406
2407 case ON_VERTICAL_BORDER:
2408 gx = WINDOW_PIXEL_WIDTH (w) - width;
2409 goto row_glyph;
2410
2411 case ON_VERTICAL_SCROLL_BAR:
2412 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2413 ? 0
2414 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2415 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2416 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2417 : 0)));
2418 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2419
2420 row_glyph:
2421 gr = 0, gy = 0;
2422 for (; r <= end_row && r->enabled_p; ++r)
2423 if (r->y + r->height > y)
2424 {
2425 gr = r; gy = r->y;
2426 break;
2427 }
2428
2429 if (gr && gy <= y)
2430 height = gr->height;
2431 else
2432 {
2433 /* Use nominal line height at end of window. */
2434 y -= gy;
2435 gy += (y / height) * height;
2436 }
2437 break;
2438
2439 case ON_RIGHT_DIVIDER:
2440 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2441 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2442 gy = 0;
2443 /* The bottom divider prevails. */
2444 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2445 goto add_edge;
2446
2447 case ON_BOTTOM_DIVIDER:
2448 gx = 0;
2449 width = WINDOW_PIXEL_WIDTH (w);
2450 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2451 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2452 goto add_edge;
2453
2454 default:
2455 ;
2456 virtual_glyph:
2457 /* If there is no glyph under the mouse, then we divide the screen
2458 into a grid of the smallest glyph in the frame, and use that
2459 as our "glyph". */
2460
2461 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2462 round down even for negative values. */
2463 if (gx < 0)
2464 gx -= width - 1;
2465 if (gy < 0)
2466 gy -= height - 1;
2467
2468 gx = (gx / width) * width;
2469 gy = (gy / height) * height;
2470
2471 goto store_rect;
2472 }
2473
2474 add_edge:
2475 gx += WINDOW_LEFT_EDGE_X (w);
2476 gy += WINDOW_TOP_EDGE_Y (w);
2477
2478 store_rect:
2479 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2480
2481 /* Visible feedback for debugging. */
2482 #if false && defined HAVE_X_WINDOWS
2483 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2484 f->output_data.x->normal_gc,
2485 gx, gy, width, height);
2486 #endif
2487 }
2488
2489
2490 #endif /* HAVE_WINDOW_SYSTEM */
2491
2492 static void
2493 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2494 {
2495 eassert (w);
2496 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2497 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2498 w->window_end_vpos
2499 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2500 }
2501
2502 /***********************************************************************
2503 Lisp form evaluation
2504 ***********************************************************************/
2505
2506 /* Error handler for safe_eval and safe_call. */
2507
2508 static Lisp_Object
2509 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2510 {
2511 add_to_log ("Error during redisplay: %S signaled %S",
2512 Flist (nargs, args), arg);
2513 return Qnil;
2514 }
2515
2516 /* Call function FUNC with the rest of NARGS - 1 arguments
2517 following. Return the result, or nil if something went
2518 wrong. Prevent redisplay during the evaluation. */
2519
2520 static Lisp_Object
2521 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2522 {
2523 Lisp_Object val;
2524
2525 if (inhibit_eval_during_redisplay)
2526 val = Qnil;
2527 else
2528 {
2529 ptrdiff_t i;
2530 ptrdiff_t count = SPECPDL_INDEX ();
2531 Lisp_Object *args;
2532 USE_SAFE_ALLOCA;
2533 SAFE_ALLOCA_LISP (args, nargs);
2534
2535 args[0] = func;
2536 for (i = 1; i < nargs; i++)
2537 args[i] = va_arg (ap, Lisp_Object);
2538
2539 specbind (Qinhibit_redisplay, Qt);
2540 if (inhibit_quit)
2541 specbind (Qinhibit_quit, Qt);
2542 /* Use Qt to ensure debugger does not run,
2543 so there is no possibility of wanting to redisplay. */
2544 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2545 safe_eval_handler);
2546 SAFE_FREE ();
2547 val = unbind_to (count, val);
2548 }
2549
2550 return val;
2551 }
2552
2553 Lisp_Object
2554 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2555 {
2556 Lisp_Object retval;
2557 va_list ap;
2558
2559 va_start (ap, func);
2560 retval = safe__call (false, nargs, func, ap);
2561 va_end (ap);
2562 return retval;
2563 }
2564
2565 /* Call function FN with one argument ARG.
2566 Return the result, or nil if something went wrong. */
2567
2568 Lisp_Object
2569 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2570 {
2571 return safe_call (2, fn, arg);
2572 }
2573
2574 static Lisp_Object
2575 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2576 {
2577 Lisp_Object retval;
2578 va_list ap;
2579
2580 va_start (ap, fn);
2581 retval = safe__call (inhibit_quit, 2, fn, ap);
2582 va_end (ap);
2583 return retval;
2584 }
2585
2586 Lisp_Object
2587 safe_eval (Lisp_Object sexpr)
2588 {
2589 return safe__call1 (false, Qeval, sexpr);
2590 }
2591
2592 static Lisp_Object
2593 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2594 {
2595 return safe__call1 (inhibit_quit, Qeval, sexpr);
2596 }
2597
2598 /* Call function FN with two arguments ARG1 and ARG2.
2599 Return the result, or nil if something went wrong. */
2600
2601 Lisp_Object
2602 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2603 {
2604 return safe_call (3, fn, arg1, arg2);
2605 }
2606
2607
2608 \f
2609 /***********************************************************************
2610 Debugging
2611 ***********************************************************************/
2612
2613 /* Define CHECK_IT to perform sanity checks on iterators.
2614 This is for debugging. It is too slow to do unconditionally. */
2615
2616 static void
2617 CHECK_IT (struct it *it)
2618 {
2619 #if false
2620 if (it->method == GET_FROM_STRING)
2621 {
2622 eassert (STRINGP (it->string));
2623 eassert (IT_STRING_CHARPOS (*it) >= 0);
2624 }
2625 else
2626 {
2627 eassert (IT_STRING_CHARPOS (*it) < 0);
2628 if (it->method == GET_FROM_BUFFER)
2629 {
2630 /* Check that character and byte positions agree. */
2631 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2632 }
2633 }
2634
2635 if (it->dpvec)
2636 eassert (it->current.dpvec_index >= 0);
2637 else
2638 eassert (it->current.dpvec_index < 0);
2639 #endif
2640 }
2641
2642
2643 /* Check that the window end of window W is what we expect it
2644 to be---the last row in the current matrix displaying text. */
2645
2646 static void
2647 CHECK_WINDOW_END (struct window *w)
2648 {
2649 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2650 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2651 {
2652 struct glyph_row *row;
2653 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2654 !row->enabled_p
2655 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2656 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2657 }
2658 #endif
2659 }
2660
2661 /***********************************************************************
2662 Iterator initialization
2663 ***********************************************************************/
2664
2665 /* Initialize IT for displaying current_buffer in window W, starting
2666 at character position CHARPOS. CHARPOS < 0 means that no buffer
2667 position is specified which is useful when the iterator is assigned
2668 a position later. BYTEPOS is the byte position corresponding to
2669 CHARPOS.
2670
2671 If ROW is not null, calls to produce_glyphs with IT as parameter
2672 will produce glyphs in that row.
2673
2674 BASE_FACE_ID is the id of a base face to use. It must be one of
2675 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2676 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2677 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2678
2679 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2680 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2681 will be initialized to use the corresponding mode line glyph row of
2682 the desired matrix of W. */
2683
2684 void
2685 init_iterator (struct it *it, struct window *w,
2686 ptrdiff_t charpos, ptrdiff_t bytepos,
2687 struct glyph_row *row, enum face_id base_face_id)
2688 {
2689 enum face_id remapped_base_face_id = base_face_id;
2690
2691 /* Some precondition checks. */
2692 eassert (w != NULL && it != NULL);
2693 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2694 && charpos <= ZV));
2695
2696 /* If face attributes have been changed since the last redisplay,
2697 free realized faces now because they depend on face definitions
2698 that might have changed. Don't free faces while there might be
2699 desired matrices pending which reference these faces. */
2700 if (!inhibit_free_realized_faces)
2701 {
2702 if (face_change)
2703 {
2704 face_change = false;
2705 free_all_realized_faces (Qnil);
2706 }
2707 else if (XFRAME (w->frame)->face_change)
2708 {
2709 XFRAME (w->frame)->face_change = 0;
2710 free_all_realized_faces (w->frame);
2711 }
2712 }
2713
2714 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2715 if (! NILP (Vface_remapping_alist))
2716 remapped_base_face_id
2717 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2718
2719 /* Use one of the mode line rows of W's desired matrix if
2720 appropriate. */
2721 if (row == NULL)
2722 {
2723 if (base_face_id == MODE_LINE_FACE_ID
2724 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2725 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2726 else if (base_face_id == HEADER_LINE_FACE_ID)
2727 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2728 }
2729
2730 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2731 Other parts of redisplay rely on that. */
2732 memclear (it, sizeof *it);
2733 it->current.overlay_string_index = -1;
2734 it->current.dpvec_index = -1;
2735 it->base_face_id = remapped_base_face_id;
2736 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2737 it->paragraph_embedding = L2R;
2738 it->bidi_it.w = w;
2739
2740 /* The window in which we iterate over current_buffer: */
2741 XSETWINDOW (it->window, w);
2742 it->w = w;
2743 it->f = XFRAME (w->frame);
2744
2745 it->cmp_it.id = -1;
2746
2747 /* Extra space between lines (on window systems only). */
2748 if (base_face_id == DEFAULT_FACE_ID
2749 && FRAME_WINDOW_P (it->f))
2750 {
2751 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2752 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2753 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2754 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2755 * FRAME_LINE_HEIGHT (it->f));
2756 else if (it->f->extra_line_spacing > 0)
2757 it->extra_line_spacing = it->f->extra_line_spacing;
2758 }
2759
2760 /* If realized faces have been removed, e.g. because of face
2761 attribute changes of named faces, recompute them. When running
2762 in batch mode, the face cache of the initial frame is null. If
2763 we happen to get called, make a dummy face cache. */
2764 if (FRAME_FACE_CACHE (it->f) == NULL)
2765 init_frame_faces (it->f);
2766 if (FRAME_FACE_CACHE (it->f)->used == 0)
2767 recompute_basic_faces (it->f);
2768
2769 it->override_ascent = -1;
2770
2771 /* Are control characters displayed as `^C'? */
2772 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2773
2774 /* -1 means everything between a CR and the following line end
2775 is invisible. >0 means lines indented more than this value are
2776 invisible. */
2777 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2778 ? (clip_to_bounds
2779 (-1, XINT (BVAR (current_buffer, selective_display)),
2780 PTRDIFF_MAX))
2781 : (!NILP (BVAR (current_buffer, selective_display))
2782 ? -1 : 0));
2783 it->selective_display_ellipsis_p
2784 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2785
2786 /* Display table to use. */
2787 it->dp = window_display_table (w);
2788
2789 /* Are multibyte characters enabled in current_buffer? */
2790 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2791
2792 /* Get the position at which the redisplay_end_trigger hook should
2793 be run, if it is to be run at all. */
2794 if (MARKERP (w->redisplay_end_trigger)
2795 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2796 it->redisplay_end_trigger_charpos
2797 = marker_position (w->redisplay_end_trigger);
2798 else if (INTEGERP (w->redisplay_end_trigger))
2799 it->redisplay_end_trigger_charpos
2800 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2801 PTRDIFF_MAX);
2802
2803 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2804
2805 /* Are lines in the display truncated? */
2806 if (TRUNCATE != 0)
2807 it->line_wrap = TRUNCATE;
2808 if (base_face_id == DEFAULT_FACE_ID
2809 && !it->w->hscroll
2810 && (WINDOW_FULL_WIDTH_P (it->w)
2811 || NILP (Vtruncate_partial_width_windows)
2812 || (INTEGERP (Vtruncate_partial_width_windows)
2813 /* PXW: Shall we do something about this? */
2814 && (XINT (Vtruncate_partial_width_windows)
2815 <= WINDOW_TOTAL_COLS (it->w))))
2816 && NILP (BVAR (current_buffer, truncate_lines)))
2817 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2818 ? WINDOW_WRAP : WORD_WRAP;
2819
2820 /* Get dimensions of truncation and continuation glyphs. These are
2821 displayed as fringe bitmaps under X, but we need them for such
2822 frames when the fringes are turned off. But leave the dimensions
2823 zero for tooltip frames, as these glyphs look ugly there and also
2824 sabotage calculations of tooltip dimensions in x-show-tip. */
2825 #ifdef HAVE_WINDOW_SYSTEM
2826 if (!(FRAME_WINDOW_P (it->f)
2827 && FRAMEP (tip_frame)
2828 && it->f == XFRAME (tip_frame)))
2829 #endif
2830 {
2831 if (it->line_wrap == TRUNCATE)
2832 {
2833 /* We will need the truncation glyph. */
2834 eassert (it->glyph_row == NULL);
2835 produce_special_glyphs (it, IT_TRUNCATION);
2836 it->truncation_pixel_width = it->pixel_width;
2837 }
2838 else
2839 {
2840 /* We will need the continuation glyph. */
2841 eassert (it->glyph_row == NULL);
2842 produce_special_glyphs (it, IT_CONTINUATION);
2843 it->continuation_pixel_width = it->pixel_width;
2844 }
2845 }
2846
2847 /* Reset these values to zero because the produce_special_glyphs
2848 above has changed them. */
2849 it->pixel_width = it->ascent = it->descent = 0;
2850 it->phys_ascent = it->phys_descent = 0;
2851
2852 /* Set this after getting the dimensions of truncation and
2853 continuation glyphs, so that we don't produce glyphs when calling
2854 produce_special_glyphs, above. */
2855 it->glyph_row = row;
2856 it->area = TEXT_AREA;
2857
2858 /* Get the dimensions of the display area. The display area
2859 consists of the visible window area plus a horizontally scrolled
2860 part to the left of the window. All x-values are relative to the
2861 start of this total display area. */
2862 if (base_face_id != DEFAULT_FACE_ID)
2863 {
2864 /* Mode lines, menu bar in terminal frames. */
2865 it->first_visible_x = 0;
2866 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2867 }
2868 else
2869 {
2870 it->first_visible_x
2871 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2872 it->last_visible_x = (it->first_visible_x
2873 + window_box_width (w, TEXT_AREA));
2874
2875 /* If we truncate lines, leave room for the truncation glyph(s) at
2876 the right margin. Otherwise, leave room for the continuation
2877 glyph(s). Done only if the window has no right fringe. */
2878 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2879 {
2880 if (it->line_wrap == TRUNCATE)
2881 it->last_visible_x -= it->truncation_pixel_width;
2882 else
2883 it->last_visible_x -= it->continuation_pixel_width;
2884 }
2885
2886 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2887 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2888 }
2889
2890 /* Leave room for a border glyph. */
2891 if (!FRAME_WINDOW_P (it->f)
2892 && !WINDOW_RIGHTMOST_P (it->w))
2893 it->last_visible_x -= 1;
2894
2895 it->last_visible_y = window_text_bottom_y (w);
2896
2897 /* For mode lines and alike, arrange for the first glyph having a
2898 left box line if the face specifies a box. */
2899 if (base_face_id != DEFAULT_FACE_ID)
2900 {
2901 struct face *face;
2902
2903 it->face_id = remapped_base_face_id;
2904
2905 /* If we have a boxed mode line, make the first character appear
2906 with a left box line. */
2907 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2908 if (face && face->box != FACE_NO_BOX)
2909 it->start_of_box_run_p = true;
2910 }
2911
2912 /* If a buffer position was specified, set the iterator there,
2913 getting overlays and face properties from that position. */
2914 if (charpos >= BUF_BEG (current_buffer))
2915 {
2916 it->stop_charpos = charpos;
2917 it->end_charpos = ZV;
2918 eassert (charpos == BYTE_TO_CHAR (bytepos));
2919 IT_CHARPOS (*it) = charpos;
2920 IT_BYTEPOS (*it) = bytepos;
2921
2922 /* We will rely on `reseat' to set this up properly, via
2923 handle_face_prop. */
2924 it->face_id = it->base_face_id;
2925
2926 it->start = it->current;
2927 /* Do we need to reorder bidirectional text? Not if this is a
2928 unibyte buffer: by definition, none of the single-byte
2929 characters are strong R2L, so no reordering is needed. And
2930 bidi.c doesn't support unibyte buffers anyway. Also, don't
2931 reorder while we are loading loadup.el, since the tables of
2932 character properties needed for reordering are not yet
2933 available. */
2934 it->bidi_p =
2935 NILP (Vpurify_flag)
2936 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2937 && it->multibyte_p;
2938
2939 /* If we are to reorder bidirectional text, init the bidi
2940 iterator. */
2941 if (it->bidi_p)
2942 {
2943 /* Since we don't know at this point whether there will be
2944 any R2L lines in the window, we reserve space for
2945 truncation/continuation glyphs even if only the left
2946 fringe is absent. */
2947 if (base_face_id == DEFAULT_FACE_ID
2948 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2949 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2950 {
2951 if (it->line_wrap == TRUNCATE)
2952 it->last_visible_x -= it->truncation_pixel_width;
2953 else
2954 it->last_visible_x -= it->continuation_pixel_width;
2955 }
2956 /* Note the paragraph direction that this buffer wants to
2957 use. */
2958 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2959 Qleft_to_right))
2960 it->paragraph_embedding = L2R;
2961 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2962 Qright_to_left))
2963 it->paragraph_embedding = R2L;
2964 else
2965 it->paragraph_embedding = NEUTRAL_DIR;
2966 bidi_unshelve_cache (NULL, false);
2967 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2968 &it->bidi_it);
2969 }
2970
2971 /* Compute faces etc. */
2972 reseat (it, it->current.pos, true);
2973 }
2974
2975 CHECK_IT (it);
2976 }
2977
2978
2979 /* Initialize IT for the display of window W with window start POS. */
2980
2981 void
2982 start_display (struct it *it, struct window *w, struct text_pos pos)
2983 {
2984 struct glyph_row *row;
2985 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
2986
2987 row = w->desired_matrix->rows + first_vpos;
2988 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2989 it->first_vpos = first_vpos;
2990
2991 /* Don't reseat to previous visible line start if current start
2992 position is in a string or image. */
2993 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2994 {
2995 int first_y = it->current_y;
2996
2997 /* If window start is not at a line start, skip forward to POS to
2998 get the correct continuation lines width. */
2999 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3000 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3001 if (!start_at_line_beg_p)
3002 {
3003 int new_x;
3004
3005 reseat_at_previous_visible_line_start (it);
3006 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3007
3008 new_x = it->current_x + it->pixel_width;
3009
3010 /* If lines are continued, this line may end in the middle
3011 of a multi-glyph character (e.g. a control character
3012 displayed as \003, or in the middle of an overlay
3013 string). In this case move_it_to above will not have
3014 taken us to the start of the continuation line but to the
3015 end of the continued line. */
3016 if (it->current_x > 0
3017 && it->line_wrap != TRUNCATE /* Lines are continued. */
3018 && (/* And glyph doesn't fit on the line. */
3019 new_x > it->last_visible_x
3020 /* Or it fits exactly and we're on a window
3021 system frame. */
3022 || (new_x == it->last_visible_x
3023 && FRAME_WINDOW_P (it->f)
3024 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3025 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3026 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3027 {
3028 if ((it->current.dpvec_index >= 0
3029 || it->current.overlay_string_index >= 0)
3030 /* If we are on a newline from a display vector or
3031 overlay string, then we are already at the end of
3032 a screen line; no need to go to the next line in
3033 that case, as this line is not really continued.
3034 (If we do go to the next line, C-e will not DTRT.) */
3035 && it->c != '\n')
3036 {
3037 set_iterator_to_next (it, true);
3038 move_it_in_display_line_to (it, -1, -1, 0);
3039 }
3040
3041 it->continuation_lines_width += it->current_x;
3042 }
3043 /* If the character at POS is displayed via a display
3044 vector, move_it_to above stops at the final glyph of
3045 IT->dpvec. To make the caller redisplay that character
3046 again (a.k.a. start at POS), we need to reset the
3047 dpvec_index to the beginning of IT->dpvec. */
3048 else if (it->current.dpvec_index >= 0)
3049 it->current.dpvec_index = 0;
3050
3051 /* We're starting a new display line, not affected by the
3052 height of the continued line, so clear the appropriate
3053 fields in the iterator structure. */
3054 it->max_ascent = it->max_descent = 0;
3055 it->max_phys_ascent = it->max_phys_descent = 0;
3056
3057 it->current_y = first_y;
3058 it->vpos = 0;
3059 it->current_x = it->hpos = 0;
3060 }
3061 }
3062 }
3063
3064
3065 /* Return true if POS is a position in ellipses displayed for invisible
3066 text. W is the window we display, for text property lookup. */
3067
3068 static bool
3069 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3070 {
3071 Lisp_Object prop, window;
3072 bool ellipses_p = false;
3073 ptrdiff_t charpos = CHARPOS (pos->pos);
3074
3075 /* If POS specifies a position in a display vector, this might
3076 be for an ellipsis displayed for invisible text. We won't
3077 get the iterator set up for delivering that ellipsis unless
3078 we make sure that it gets aware of the invisible text. */
3079 if (pos->dpvec_index >= 0
3080 && pos->overlay_string_index < 0
3081 && CHARPOS (pos->string_pos) < 0
3082 && charpos > BEGV
3083 && (XSETWINDOW (window, w),
3084 prop = Fget_char_property (make_number (charpos),
3085 Qinvisible, window),
3086 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3087 {
3088 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3089 window);
3090 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3091 }
3092
3093 return ellipses_p;
3094 }
3095
3096
3097 /* Initialize IT for stepping through current_buffer in window W,
3098 starting at position POS that includes overlay string and display
3099 vector/ control character translation position information. Value
3100 is false if there are overlay strings with newlines at POS. */
3101
3102 static bool
3103 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3104 {
3105 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3106 int i;
3107 bool overlay_strings_with_newlines = false;
3108
3109 /* If POS specifies a position in a display vector, this might
3110 be for an ellipsis displayed for invisible text. We won't
3111 get the iterator set up for delivering that ellipsis unless
3112 we make sure that it gets aware of the invisible text. */
3113 if (in_ellipses_for_invisible_text_p (pos, w))
3114 {
3115 --charpos;
3116 bytepos = 0;
3117 }
3118
3119 /* Keep in mind: the call to reseat in init_iterator skips invisible
3120 text, so we might end up at a position different from POS. This
3121 is only a problem when POS is a row start after a newline and an
3122 overlay starts there with an after-string, and the overlay has an
3123 invisible property. Since we don't skip invisible text in
3124 display_line and elsewhere immediately after consuming the
3125 newline before the row start, such a POS will not be in a string,
3126 but the call to init_iterator below will move us to the
3127 after-string. */
3128 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3129
3130 /* This only scans the current chunk -- it should scan all chunks.
3131 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3132 to 16 in 22.1 to make this a lesser problem. */
3133 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3134 {
3135 const char *s = SSDATA (it->overlay_strings[i]);
3136 const char *e = s + SBYTES (it->overlay_strings[i]);
3137
3138 while (s < e && *s != '\n')
3139 ++s;
3140
3141 if (s < e)
3142 {
3143 overlay_strings_with_newlines = true;
3144 break;
3145 }
3146 }
3147
3148 /* If position is within an overlay string, set up IT to the right
3149 overlay string. */
3150 if (pos->overlay_string_index >= 0)
3151 {
3152 int relative_index;
3153
3154 /* If the first overlay string happens to have a `display'
3155 property for an image, the iterator will be set up for that
3156 image, and we have to undo that setup first before we can
3157 correct the overlay string index. */
3158 if (it->method == GET_FROM_IMAGE)
3159 pop_it (it);
3160
3161 /* We already have the first chunk of overlay strings in
3162 IT->overlay_strings. Load more until the one for
3163 pos->overlay_string_index is in IT->overlay_strings. */
3164 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3165 {
3166 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3167 it->current.overlay_string_index = 0;
3168 while (n--)
3169 {
3170 load_overlay_strings (it, 0);
3171 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3172 }
3173 }
3174
3175 it->current.overlay_string_index = pos->overlay_string_index;
3176 relative_index = (it->current.overlay_string_index
3177 % OVERLAY_STRING_CHUNK_SIZE);
3178 it->string = it->overlay_strings[relative_index];
3179 eassert (STRINGP (it->string));
3180 it->current.string_pos = pos->string_pos;
3181 it->method = GET_FROM_STRING;
3182 it->end_charpos = SCHARS (it->string);
3183 /* Set up the bidi iterator for this overlay string. */
3184 if (it->bidi_p)
3185 {
3186 it->bidi_it.string.lstring = it->string;
3187 it->bidi_it.string.s = NULL;
3188 it->bidi_it.string.schars = SCHARS (it->string);
3189 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3190 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3191 it->bidi_it.string.unibyte = !it->multibyte_p;
3192 it->bidi_it.w = it->w;
3193 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3194 FRAME_WINDOW_P (it->f), &it->bidi_it);
3195
3196 /* Synchronize the state of the bidi iterator with
3197 pos->string_pos. For any string position other than
3198 zero, this will be done automagically when we resume
3199 iteration over the string and get_visually_first_element
3200 is called. But if string_pos is zero, and the string is
3201 to be reordered for display, we need to resync manually,
3202 since it could be that the iteration state recorded in
3203 pos ended at string_pos of 0 moving backwards in string. */
3204 if (CHARPOS (pos->string_pos) == 0)
3205 {
3206 get_visually_first_element (it);
3207 if (IT_STRING_CHARPOS (*it) != 0)
3208 do {
3209 /* Paranoia. */
3210 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3211 bidi_move_to_visually_next (&it->bidi_it);
3212 } while (it->bidi_it.charpos != 0);
3213 }
3214 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3215 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3216 }
3217 }
3218
3219 if (CHARPOS (pos->string_pos) >= 0)
3220 {
3221 /* Recorded position is not in an overlay string, but in another
3222 string. This can only be a string from a `display' property.
3223 IT should already be filled with that string. */
3224 it->current.string_pos = pos->string_pos;
3225 eassert (STRINGP (it->string));
3226 if (it->bidi_p)
3227 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3228 FRAME_WINDOW_P (it->f), &it->bidi_it);
3229 }
3230
3231 /* Restore position in display vector translations, control
3232 character translations or ellipses. */
3233 if (pos->dpvec_index >= 0)
3234 {
3235 if (it->dpvec == NULL)
3236 get_next_display_element (it);
3237 eassert (it->dpvec && it->current.dpvec_index == 0);
3238 it->current.dpvec_index = pos->dpvec_index;
3239 }
3240
3241 CHECK_IT (it);
3242 return !overlay_strings_with_newlines;
3243 }
3244
3245
3246 /* Initialize IT for stepping through current_buffer in window W
3247 starting at ROW->start. */
3248
3249 static void
3250 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3251 {
3252 init_from_display_pos (it, w, &row->start);
3253 it->start = row->start;
3254 it->continuation_lines_width = row->continuation_lines_width;
3255 CHECK_IT (it);
3256 }
3257
3258
3259 /* Initialize IT for stepping through current_buffer in window W
3260 starting in the line following ROW, i.e. starting at ROW->end.
3261 Value is false if there are overlay strings with newlines at ROW's
3262 end position. */
3263
3264 static bool
3265 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3266 {
3267 bool success = false;
3268
3269 if (init_from_display_pos (it, w, &row->end))
3270 {
3271 if (row->continued_p)
3272 it->continuation_lines_width
3273 = row->continuation_lines_width + row->pixel_width;
3274 CHECK_IT (it);
3275 success = true;
3276 }
3277
3278 return success;
3279 }
3280
3281
3282
3283 \f
3284 /***********************************************************************
3285 Text properties
3286 ***********************************************************************/
3287
3288 /* Called when IT reaches IT->stop_charpos. Handle text property and
3289 overlay changes. Set IT->stop_charpos to the next position where
3290 to stop. */
3291
3292 static void
3293 handle_stop (struct it *it)
3294 {
3295 enum prop_handled handled;
3296 bool handle_overlay_change_p;
3297 struct props *p;
3298
3299 it->dpvec = NULL;
3300 it->current.dpvec_index = -1;
3301 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3302 it->ellipsis_p = false;
3303
3304 /* Use face of preceding text for ellipsis (if invisible) */
3305 if (it->selective_display_ellipsis_p)
3306 it->saved_face_id = it->face_id;
3307
3308 /* Here's the description of the semantics of, and the logic behind,
3309 the various HANDLED_* statuses:
3310
3311 HANDLED_NORMALLY means the handler did its job, and the loop
3312 should proceed to calling the next handler in order.
3313
3314 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3315 change in the properties and overlays at current position, so the
3316 loop should be restarted, to re-invoke the handlers that were
3317 already called. This happens when fontification-functions were
3318 called by handle_fontified_prop, and actually fontified
3319 something. Another case where HANDLED_RECOMPUTE_PROPS is
3320 returned is when we discover overlay strings that need to be
3321 displayed right away. The loop below will continue for as long
3322 as the status is HANDLED_RECOMPUTE_PROPS.
3323
3324 HANDLED_RETURN means return immediately to the caller, to
3325 continue iteration without calling any further handlers. This is
3326 used when we need to act on some property right away, for example
3327 when we need to display the ellipsis or a replacing display
3328 property, such as display string or image.
3329
3330 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3331 consumed, and the handler switched to the next overlay string.
3332 This signals the loop below to refrain from looking for more
3333 overlays before all the overlay strings of the current overlay
3334 are processed.
3335
3336 Some of the handlers called by the loop push the iterator state
3337 onto the stack (see 'push_it'), and arrange for the iteration to
3338 continue with another object, such as an image, a display string,
3339 or an overlay string. In most such cases, it->stop_charpos is
3340 set to the first character of the string, so that when the
3341 iteration resumes, this function will immediately be called
3342 again, to examine the properties at the beginning of the string.
3343
3344 When a display or overlay string is exhausted, the iterator state
3345 is popped (see 'pop_it'), and iteration continues with the
3346 previous object. Again, in many such cases this function is
3347 called again to find the next position where properties might
3348 change. */
3349
3350 do
3351 {
3352 handled = HANDLED_NORMALLY;
3353
3354 /* Call text property handlers. */
3355 for (p = it_props; p->handler; ++p)
3356 {
3357 handled = p->handler (it);
3358
3359 if (handled == HANDLED_RECOMPUTE_PROPS)
3360 break;
3361 else if (handled == HANDLED_RETURN)
3362 {
3363 /* We still want to show before and after strings from
3364 overlays even if the actual buffer text is replaced. */
3365 if (!handle_overlay_change_p
3366 || it->sp > 1
3367 /* Don't call get_overlay_strings_1 if we already
3368 have overlay strings loaded, because doing so
3369 will load them again and push the iterator state
3370 onto the stack one more time, which is not
3371 expected by the rest of the code that processes
3372 overlay strings. */
3373 || (it->current.overlay_string_index < 0
3374 && !get_overlay_strings_1 (it, 0, false)))
3375 {
3376 if (it->ellipsis_p)
3377 setup_for_ellipsis (it, 0);
3378 /* When handling a display spec, we might load an
3379 empty string. In that case, discard it here. We
3380 used to discard it in handle_single_display_spec,
3381 but that causes get_overlay_strings_1, above, to
3382 ignore overlay strings that we must check. */
3383 if (STRINGP (it->string) && !SCHARS (it->string))
3384 pop_it (it);
3385 return;
3386 }
3387 else if (STRINGP (it->string) && !SCHARS (it->string))
3388 pop_it (it);
3389 else
3390 {
3391 it->string_from_display_prop_p = false;
3392 it->from_disp_prop_p = false;
3393 handle_overlay_change_p = false;
3394 }
3395 handled = HANDLED_RECOMPUTE_PROPS;
3396 break;
3397 }
3398 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3399 handle_overlay_change_p = false;
3400 }
3401
3402 if (handled != HANDLED_RECOMPUTE_PROPS)
3403 {
3404 /* Don't check for overlay strings below when set to deliver
3405 characters from a display vector. */
3406 if (it->method == GET_FROM_DISPLAY_VECTOR)
3407 handle_overlay_change_p = false;
3408
3409 /* Handle overlay changes.
3410 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3411 if it finds overlays. */
3412 if (handle_overlay_change_p)
3413 handled = handle_overlay_change (it);
3414 }
3415
3416 if (it->ellipsis_p)
3417 {
3418 setup_for_ellipsis (it, 0);
3419 break;
3420 }
3421 }
3422 while (handled == HANDLED_RECOMPUTE_PROPS);
3423
3424 /* Determine where to stop next. */
3425 if (handled == HANDLED_NORMALLY)
3426 compute_stop_pos (it);
3427 }
3428
3429
3430 /* Compute IT->stop_charpos from text property and overlay change
3431 information for IT's current position. */
3432
3433 static void
3434 compute_stop_pos (struct it *it)
3435 {
3436 register INTERVAL iv, next_iv;
3437 Lisp_Object object, limit, position;
3438 ptrdiff_t charpos, bytepos;
3439
3440 if (STRINGP (it->string))
3441 {
3442 /* Strings are usually short, so don't limit the search for
3443 properties. */
3444 it->stop_charpos = it->end_charpos;
3445 object = it->string;
3446 limit = Qnil;
3447 charpos = IT_STRING_CHARPOS (*it);
3448 bytepos = IT_STRING_BYTEPOS (*it);
3449 }
3450 else
3451 {
3452 ptrdiff_t pos;
3453
3454 /* If end_charpos is out of range for some reason, such as a
3455 misbehaving display function, rationalize it (Bug#5984). */
3456 if (it->end_charpos > ZV)
3457 it->end_charpos = ZV;
3458 it->stop_charpos = it->end_charpos;
3459
3460 /* If next overlay change is in front of the current stop pos
3461 (which is IT->end_charpos), stop there. Note: value of
3462 next_overlay_change is point-max if no overlay change
3463 follows. */
3464 charpos = IT_CHARPOS (*it);
3465 bytepos = IT_BYTEPOS (*it);
3466 pos = next_overlay_change (charpos);
3467 if (pos < it->stop_charpos)
3468 it->stop_charpos = pos;
3469
3470 /* Set up variables for computing the stop position from text
3471 property changes. */
3472 XSETBUFFER (object, current_buffer);
3473 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3474 }
3475
3476 /* Get the interval containing IT's position. Value is a null
3477 interval if there isn't such an interval. */
3478 position = make_number (charpos);
3479 iv = validate_interval_range (object, &position, &position, false);
3480 if (iv)
3481 {
3482 Lisp_Object values_here[LAST_PROP_IDX];
3483 struct props *p;
3484
3485 /* Get properties here. */
3486 for (p = it_props; p->handler; ++p)
3487 values_here[p->idx] = textget (iv->plist,
3488 builtin_lisp_symbol (p->name));
3489
3490 /* Look for an interval following iv that has different
3491 properties. */
3492 for (next_iv = next_interval (iv);
3493 (next_iv
3494 && (NILP (limit)
3495 || XFASTINT (limit) > next_iv->position));
3496 next_iv = next_interval (next_iv))
3497 {
3498 for (p = it_props; p->handler; ++p)
3499 {
3500 Lisp_Object new_value = textget (next_iv->plist,
3501 builtin_lisp_symbol (p->name));
3502 if (!EQ (values_here[p->idx], new_value))
3503 break;
3504 }
3505
3506 if (p->handler)
3507 break;
3508 }
3509
3510 if (next_iv)
3511 {
3512 if (INTEGERP (limit)
3513 && next_iv->position >= XFASTINT (limit))
3514 /* No text property change up to limit. */
3515 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3516 else
3517 /* Text properties change in next_iv. */
3518 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3519 }
3520 }
3521
3522 if (it->cmp_it.id < 0)
3523 {
3524 ptrdiff_t stoppos = it->end_charpos;
3525
3526 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3527 stoppos = -1;
3528 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3529 stoppos, it->string);
3530 }
3531
3532 eassert (STRINGP (it->string)
3533 || (it->stop_charpos >= BEGV
3534 && it->stop_charpos >= IT_CHARPOS (*it)));
3535 }
3536
3537
3538 /* Return the position of the next overlay change after POS in
3539 current_buffer. Value is point-max if no overlay change
3540 follows. This is like `next-overlay-change' but doesn't use
3541 xmalloc. */
3542
3543 static ptrdiff_t
3544 next_overlay_change (ptrdiff_t pos)
3545 {
3546 ptrdiff_t i, noverlays;
3547 ptrdiff_t endpos;
3548 Lisp_Object *overlays;
3549 USE_SAFE_ALLOCA;
3550
3551 /* Get all overlays at the given position. */
3552 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3553
3554 /* If any of these overlays ends before endpos,
3555 use its ending point instead. */
3556 for (i = 0; i < noverlays; ++i)
3557 {
3558 Lisp_Object oend;
3559 ptrdiff_t oendpos;
3560
3561 oend = OVERLAY_END (overlays[i]);
3562 oendpos = OVERLAY_POSITION (oend);
3563 endpos = min (endpos, oendpos);
3564 }
3565
3566 SAFE_FREE ();
3567 return endpos;
3568 }
3569
3570 /* How many characters forward to search for a display property or
3571 display string. Searching too far forward makes the bidi display
3572 sluggish, especially in small windows. */
3573 #define MAX_DISP_SCAN 250
3574
3575 /* Return the character position of a display string at or after
3576 position specified by POSITION. If no display string exists at or
3577 after POSITION, return ZV. A display string is either an overlay
3578 with `display' property whose value is a string, or a `display'
3579 text property whose value is a string. STRING is data about the
3580 string to iterate; if STRING->lstring is nil, we are iterating a
3581 buffer. FRAME_WINDOW_P is true when we are displaying a window
3582 on a GUI frame. DISP_PROP is set to zero if we searched
3583 MAX_DISP_SCAN characters forward without finding any display
3584 strings, non-zero otherwise. It is set to 2 if the display string
3585 uses any kind of `(space ...)' spec that will produce a stretch of
3586 white space in the text area. */
3587 ptrdiff_t
3588 compute_display_string_pos (struct text_pos *position,
3589 struct bidi_string_data *string,
3590 struct window *w,
3591 bool frame_window_p, int *disp_prop)
3592 {
3593 /* OBJECT = nil means current buffer. */
3594 Lisp_Object object, object1;
3595 Lisp_Object pos, spec, limpos;
3596 bool string_p = string && (STRINGP (string->lstring) || string->s);
3597 ptrdiff_t eob = string_p ? string->schars : ZV;
3598 ptrdiff_t begb = string_p ? 0 : BEGV;
3599 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3600 ptrdiff_t lim =
3601 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3602 struct text_pos tpos;
3603 int rv = 0;
3604
3605 if (string && STRINGP (string->lstring))
3606 object1 = object = string->lstring;
3607 else if (w && !string_p)
3608 {
3609 XSETWINDOW (object, w);
3610 object1 = Qnil;
3611 }
3612 else
3613 object1 = object = Qnil;
3614
3615 *disp_prop = 1;
3616
3617 if (charpos >= eob
3618 /* We don't support display properties whose values are strings
3619 that have display string properties. */
3620 || string->from_disp_str
3621 /* C strings cannot have display properties. */
3622 || (string->s && !STRINGP (object)))
3623 {
3624 *disp_prop = 0;
3625 return eob;
3626 }
3627
3628 /* If the character at CHARPOS is where the display string begins,
3629 return CHARPOS. */
3630 pos = make_number (charpos);
3631 if (STRINGP (object))
3632 bufpos = string->bufpos;
3633 else
3634 bufpos = charpos;
3635 tpos = *position;
3636 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3637 && (charpos <= begb
3638 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3639 object),
3640 spec))
3641 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3642 frame_window_p)))
3643 {
3644 if (rv == 2)
3645 *disp_prop = 2;
3646 return charpos;
3647 }
3648
3649 /* Look forward for the first character with a `display' property
3650 that will replace the underlying text when displayed. */
3651 limpos = make_number (lim);
3652 do {
3653 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3654 CHARPOS (tpos) = XFASTINT (pos);
3655 if (CHARPOS (tpos) >= lim)
3656 {
3657 *disp_prop = 0;
3658 break;
3659 }
3660 if (STRINGP (object))
3661 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3662 else
3663 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3664 spec = Fget_char_property (pos, Qdisplay, object);
3665 if (!STRINGP (object))
3666 bufpos = CHARPOS (tpos);
3667 } while (NILP (spec)
3668 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3669 bufpos, frame_window_p)));
3670 if (rv == 2)
3671 *disp_prop = 2;
3672
3673 return CHARPOS (tpos);
3674 }
3675
3676 /* Return the character position of the end of the display string that
3677 started at CHARPOS. If there's no display string at CHARPOS,
3678 return -1. A display string is either an overlay with `display'
3679 property whose value is a string or a `display' text property whose
3680 value is a string. */
3681 ptrdiff_t
3682 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3683 {
3684 /* OBJECT = nil means current buffer. */
3685 Lisp_Object object =
3686 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3687 Lisp_Object pos = make_number (charpos);
3688 ptrdiff_t eob =
3689 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3690
3691 if (charpos >= eob || (string->s && !STRINGP (object)))
3692 return eob;
3693
3694 /* It could happen that the display property or overlay was removed
3695 since we found it in compute_display_string_pos above. One way
3696 this can happen is if JIT font-lock was called (through
3697 handle_fontified_prop), and jit-lock-functions remove text
3698 properties or overlays from the portion of buffer that includes
3699 CHARPOS. Muse mode is known to do that, for example. In this
3700 case, we return -1 to the caller, to signal that no display
3701 string is actually present at CHARPOS. See bidi_fetch_char for
3702 how this is handled.
3703
3704 An alternative would be to never look for display properties past
3705 it->stop_charpos. But neither compute_display_string_pos nor
3706 bidi_fetch_char that calls it know or care where the next
3707 stop_charpos is. */
3708 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3709 return -1;
3710
3711 /* Look forward for the first character where the `display' property
3712 changes. */
3713 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3714
3715 return XFASTINT (pos);
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Fontification
3722 ***********************************************************************/
3723
3724 /* Handle changes in the `fontified' property of the current buffer by
3725 calling hook functions from Qfontification_functions to fontify
3726 regions of text. */
3727
3728 static enum prop_handled
3729 handle_fontified_prop (struct it *it)
3730 {
3731 Lisp_Object prop, pos;
3732 enum prop_handled handled = HANDLED_NORMALLY;
3733
3734 if (!NILP (Vmemory_full))
3735 return handled;
3736
3737 /* Get the value of the `fontified' property at IT's current buffer
3738 position. (The `fontified' property doesn't have a special
3739 meaning in strings.) If the value is nil, call functions from
3740 Qfontification_functions. */
3741 if (!STRINGP (it->string)
3742 && it->s == NULL
3743 && !NILP (Vfontification_functions)
3744 && !NILP (Vrun_hooks)
3745 && (pos = make_number (IT_CHARPOS (*it)),
3746 prop = Fget_char_property (pos, Qfontified, Qnil),
3747 /* Ignore the special cased nil value always present at EOB since
3748 no amount of fontifying will be able to change it. */
3749 NILP (prop) && IT_CHARPOS (*it) < Z))
3750 {
3751 ptrdiff_t count = SPECPDL_INDEX ();
3752 Lisp_Object val;
3753 struct buffer *obuf = current_buffer;
3754 ptrdiff_t begv = BEGV, zv = ZV;
3755 bool old_clip_changed = current_buffer->clip_changed;
3756
3757 val = Vfontification_functions;
3758 specbind (Qfontification_functions, Qnil);
3759
3760 eassert (it->end_charpos == ZV);
3761
3762 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3763 safe_call1 (val, pos);
3764 else
3765 {
3766 Lisp_Object fns, fn;
3767
3768 fns = Qnil;
3769
3770 for (; CONSP (val); val = XCDR (val))
3771 {
3772 fn = XCAR (val);
3773
3774 if (EQ (fn, Qt))
3775 {
3776 /* A value of t indicates this hook has a local
3777 binding; it means to run the global binding too.
3778 In a global value, t should not occur. If it
3779 does, we must ignore it to avoid an endless
3780 loop. */
3781 for (fns = Fdefault_value (Qfontification_functions);
3782 CONSP (fns);
3783 fns = XCDR (fns))
3784 {
3785 fn = XCAR (fns);
3786 if (!EQ (fn, Qt))
3787 safe_call1 (fn, pos);
3788 }
3789 }
3790 else
3791 safe_call1 (fn, pos);
3792 }
3793 }
3794
3795 unbind_to (count, Qnil);
3796
3797 /* Fontification functions routinely call `save-restriction'.
3798 Normally, this tags clip_changed, which can confuse redisplay
3799 (see discussion in Bug#6671). Since we don't perform any
3800 special handling of fontification changes in the case where
3801 `save-restriction' isn't called, there's no point doing so in
3802 this case either. So, if the buffer's restrictions are
3803 actually left unchanged, reset clip_changed. */
3804 if (obuf == current_buffer)
3805 {
3806 if (begv == BEGV && zv == ZV)
3807 current_buffer->clip_changed = old_clip_changed;
3808 }
3809 /* There isn't much we can reasonably do to protect against
3810 misbehaving fontification, but here's a fig leaf. */
3811 else if (BUFFER_LIVE_P (obuf))
3812 set_buffer_internal_1 (obuf);
3813
3814 /* The fontification code may have added/removed text.
3815 It could do even a lot worse, but let's at least protect against
3816 the most obvious case where only the text past `pos' gets changed',
3817 as is/was done in grep.el where some escapes sequences are turned
3818 into face properties (bug#7876). */
3819 it->end_charpos = ZV;
3820
3821 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3822 something. This avoids an endless loop if they failed to
3823 fontify the text for which reason ever. */
3824 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3825 handled = HANDLED_RECOMPUTE_PROPS;
3826 }
3827
3828 return handled;
3829 }
3830
3831
3832 \f
3833 /***********************************************************************
3834 Faces
3835 ***********************************************************************/
3836
3837 /* Set up iterator IT from face properties at its current position.
3838 Called from handle_stop. */
3839
3840 static enum prop_handled
3841 handle_face_prop (struct it *it)
3842 {
3843 int new_face_id;
3844 ptrdiff_t next_stop;
3845
3846 if (!STRINGP (it->string))
3847 {
3848 new_face_id
3849 = face_at_buffer_position (it->w,
3850 IT_CHARPOS (*it),
3851 &next_stop,
3852 (IT_CHARPOS (*it)
3853 + TEXT_PROP_DISTANCE_LIMIT),
3854 false, it->base_face_id);
3855
3856 /* Is this a start of a run of characters with box face?
3857 Caveat: this can be called for a freshly initialized
3858 iterator; face_id is -1 in this case. We know that the new
3859 face will not change until limit, i.e. if the new face has a
3860 box, all characters up to limit will have one. But, as
3861 usual, we don't know whether limit is really the end. */
3862 if (new_face_id != it->face_id)
3863 {
3864 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3865 /* If it->face_id is -1, old_face below will be NULL, see
3866 the definition of FACE_FROM_ID. This will happen if this
3867 is the initial call that gets the face. */
3868 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3869
3870 /* If the value of face_id of the iterator is -1, we have to
3871 look in front of IT's position and see whether there is a
3872 face there that's different from new_face_id. */
3873 if (!old_face && IT_CHARPOS (*it) > BEG)
3874 {
3875 int prev_face_id = face_before_it_pos (it);
3876
3877 old_face = FACE_FROM_ID (it->f, prev_face_id);
3878 }
3879
3880 /* If the new face has a box, but the old face does not,
3881 this is the start of a run of characters with box face,
3882 i.e. this character has a shadow on the left side. */
3883 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3884 && (old_face == NULL || !old_face->box));
3885 it->face_box_p = new_face->box != FACE_NO_BOX;
3886 }
3887 }
3888 else
3889 {
3890 int base_face_id;
3891 ptrdiff_t bufpos;
3892 int i;
3893 Lisp_Object from_overlay
3894 = (it->current.overlay_string_index >= 0
3895 ? it->string_overlays[it->current.overlay_string_index
3896 % OVERLAY_STRING_CHUNK_SIZE]
3897 : Qnil);
3898
3899 /* See if we got to this string directly or indirectly from
3900 an overlay property. That includes the before-string or
3901 after-string of an overlay, strings in display properties
3902 provided by an overlay, their text properties, etc.
3903
3904 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3905 if (! NILP (from_overlay))
3906 for (i = it->sp - 1; i >= 0; i--)
3907 {
3908 if (it->stack[i].current.overlay_string_index >= 0)
3909 from_overlay
3910 = it->string_overlays[it->stack[i].current.overlay_string_index
3911 % OVERLAY_STRING_CHUNK_SIZE];
3912 else if (! NILP (it->stack[i].from_overlay))
3913 from_overlay = it->stack[i].from_overlay;
3914
3915 if (!NILP (from_overlay))
3916 break;
3917 }
3918
3919 if (! NILP (from_overlay))
3920 {
3921 bufpos = IT_CHARPOS (*it);
3922 /* For a string from an overlay, the base face depends
3923 only on text properties and ignores overlays. */
3924 base_face_id
3925 = face_for_overlay_string (it->w,
3926 IT_CHARPOS (*it),
3927 &next_stop,
3928 (IT_CHARPOS (*it)
3929 + TEXT_PROP_DISTANCE_LIMIT),
3930 false,
3931 from_overlay);
3932 }
3933 else
3934 {
3935 bufpos = 0;
3936
3937 /* For strings from a `display' property, use the face at
3938 IT's current buffer position as the base face to merge
3939 with, so that overlay strings appear in the same face as
3940 surrounding text, unless they specify their own faces.
3941 For strings from wrap-prefix and line-prefix properties,
3942 use the default face, possibly remapped via
3943 Vface_remapping_alist. */
3944 /* Note that the fact that we use the face at _buffer_
3945 position means that a 'display' property on an overlay
3946 string will not inherit the face of that overlay string,
3947 but will instead revert to the face of buffer text
3948 covered by the overlay. This is visible, e.g., when the
3949 overlay specifies a box face, but neither the buffer nor
3950 the display string do. This sounds like a design bug,
3951 but Emacs always did that since v21.1, so changing that
3952 might be a big deal. */
3953 base_face_id = it->string_from_prefix_prop_p
3954 ? (!NILP (Vface_remapping_alist)
3955 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3956 : DEFAULT_FACE_ID)
3957 : underlying_face_id (it);
3958 }
3959
3960 new_face_id = face_at_string_position (it->w,
3961 it->string,
3962 IT_STRING_CHARPOS (*it),
3963 bufpos,
3964 &next_stop,
3965 base_face_id, false);
3966
3967 /* Is this a start of a run of characters with box? Caveat:
3968 this can be called for a freshly allocated iterator; face_id
3969 is -1 is this case. We know that the new face will not
3970 change until the next check pos, i.e. if the new face has a
3971 box, all characters up to that position will have a
3972 box. But, as usual, we don't know whether that position
3973 is really the end. */
3974 if (new_face_id != it->face_id)
3975 {
3976 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3977 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3978
3979 /* If new face has a box but old face hasn't, this is the
3980 start of a run of characters with box, i.e. it has a
3981 shadow on the left side. */
3982 it->start_of_box_run_p
3983 = new_face->box && (old_face == NULL || !old_face->box);
3984 it->face_box_p = new_face->box != FACE_NO_BOX;
3985 }
3986 }
3987
3988 it->face_id = new_face_id;
3989 return HANDLED_NORMALLY;
3990 }
3991
3992
3993 /* Return the ID of the face ``underlying'' IT's current position,
3994 which is in a string. If the iterator is associated with a
3995 buffer, return the face at IT's current buffer position.
3996 Otherwise, use the iterator's base_face_id. */
3997
3998 static int
3999 underlying_face_id (struct it *it)
4000 {
4001 int face_id = it->base_face_id, i;
4002
4003 eassert (STRINGP (it->string));
4004
4005 for (i = it->sp - 1; i >= 0; --i)
4006 if (NILP (it->stack[i].string))
4007 face_id = it->stack[i].face_id;
4008
4009 return face_id;
4010 }
4011
4012
4013 /* Compute the face one character before or after the current position
4014 of IT, in the visual order. BEFORE_P means get the face
4015 in front (to the left in L2R paragraphs, to the right in R2L
4016 paragraphs) of IT's screen position. Value is the ID of the face. */
4017
4018 static int
4019 face_before_or_after_it_pos (struct it *it, bool before_p)
4020 {
4021 int face_id, limit;
4022 ptrdiff_t next_check_charpos;
4023 struct it it_copy;
4024 void *it_copy_data = NULL;
4025
4026 eassert (it->s == NULL);
4027
4028 if (STRINGP (it->string))
4029 {
4030 ptrdiff_t bufpos, charpos;
4031 int base_face_id;
4032
4033 /* No face change past the end of the string (for the case
4034 we are padding with spaces). No face change before the
4035 string start. */
4036 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4037 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4038 return it->face_id;
4039
4040 if (!it->bidi_p)
4041 {
4042 /* Set charpos to the position before or after IT's current
4043 position, in the logical order, which in the non-bidi
4044 case is the same as the visual order. */
4045 if (before_p)
4046 charpos = IT_STRING_CHARPOS (*it) - 1;
4047 else if (it->what == IT_COMPOSITION)
4048 /* For composition, we must check the character after the
4049 composition. */
4050 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4051 else
4052 charpos = IT_STRING_CHARPOS (*it) + 1;
4053 }
4054 else
4055 {
4056 if (before_p)
4057 {
4058 /* With bidi iteration, the character before the current
4059 in the visual order cannot be found by simple
4060 iteration, because "reverse" reordering is not
4061 supported. Instead, we need to start from the string
4062 beginning and go all the way to the current string
4063 position, remembering the previous position. */
4064 /* Ignore face changes before the first visible
4065 character on this display line. */
4066 if (it->current_x <= it->first_visible_x)
4067 return it->face_id;
4068 SAVE_IT (it_copy, *it, it_copy_data);
4069 IT_STRING_CHARPOS (it_copy) = 0;
4070 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4071
4072 do
4073 {
4074 charpos = IT_STRING_CHARPOS (it_copy);
4075 if (charpos >= SCHARS (it->string))
4076 break;
4077 bidi_move_to_visually_next (&it_copy.bidi_it);
4078 }
4079 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4080
4081 RESTORE_IT (it, it, it_copy_data);
4082 }
4083 else
4084 {
4085 /* Set charpos to the string position of the character
4086 that comes after IT's current position in the visual
4087 order. */
4088 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4089
4090 it_copy = *it;
4091 while (n--)
4092 bidi_move_to_visually_next (&it_copy.bidi_it);
4093
4094 charpos = it_copy.bidi_it.charpos;
4095 }
4096 }
4097 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4098
4099 if (it->current.overlay_string_index >= 0)
4100 bufpos = IT_CHARPOS (*it);
4101 else
4102 bufpos = 0;
4103
4104 base_face_id = underlying_face_id (it);
4105
4106 /* Get the face for ASCII, or unibyte. */
4107 face_id = face_at_string_position (it->w,
4108 it->string,
4109 charpos,
4110 bufpos,
4111 &next_check_charpos,
4112 base_face_id, false);
4113
4114 /* Correct the face for charsets different from ASCII. Do it
4115 for the multibyte case only. The face returned above is
4116 suitable for unibyte text if IT->string is unibyte. */
4117 if (STRING_MULTIBYTE (it->string))
4118 {
4119 struct text_pos pos1 = string_pos (charpos, it->string);
4120 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4121 int c, len;
4122 struct face *face = FACE_FROM_ID (it->f, face_id);
4123
4124 c = string_char_and_length (p, &len);
4125 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4126 }
4127 }
4128 else
4129 {
4130 struct text_pos pos;
4131
4132 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4133 || (IT_CHARPOS (*it) <= BEGV && before_p))
4134 return it->face_id;
4135
4136 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4137 pos = it->current.pos;
4138
4139 if (!it->bidi_p)
4140 {
4141 if (before_p)
4142 DEC_TEXT_POS (pos, it->multibyte_p);
4143 else
4144 {
4145 if (it->what == IT_COMPOSITION)
4146 {
4147 /* For composition, we must check the position after
4148 the composition. */
4149 pos.charpos += it->cmp_it.nchars;
4150 pos.bytepos += it->len;
4151 }
4152 else
4153 INC_TEXT_POS (pos, it->multibyte_p);
4154 }
4155 }
4156 else
4157 {
4158 if (before_p)
4159 {
4160 int current_x;
4161
4162 /* With bidi iteration, the character before the current
4163 in the visual order cannot be found by simple
4164 iteration, because "reverse" reordering is not
4165 supported. Instead, we need to use the move_it_*
4166 family of functions, and move to the previous
4167 character starting from the beginning of the visual
4168 line. */
4169 /* Ignore face changes before the first visible
4170 character on this display line. */
4171 if (it->current_x <= it->first_visible_x)
4172 return it->face_id;
4173 SAVE_IT (it_copy, *it, it_copy_data);
4174 /* Implementation note: Since move_it_in_display_line
4175 works in the iterator geometry, and thinks the first
4176 character is always the leftmost, even in R2L lines,
4177 we don't need to distinguish between the R2L and L2R
4178 cases here. */
4179 current_x = it_copy.current_x;
4180 move_it_vertically_backward (&it_copy, 0);
4181 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4182 pos = it_copy.current.pos;
4183 RESTORE_IT (it, it, it_copy_data);
4184 }
4185 else
4186 {
4187 /* Set charpos to the buffer position of the character
4188 that comes after IT's current position in the visual
4189 order. */
4190 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4191
4192 it_copy = *it;
4193 while (n--)
4194 bidi_move_to_visually_next (&it_copy.bidi_it);
4195
4196 SET_TEXT_POS (pos,
4197 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4198 }
4199 }
4200 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4201
4202 /* Determine face for CHARSET_ASCII, or unibyte. */
4203 face_id = face_at_buffer_position (it->w,
4204 CHARPOS (pos),
4205 &next_check_charpos,
4206 limit, false, -1);
4207
4208 /* Correct the face for charsets different from ASCII. Do it
4209 for the multibyte case only. The face returned above is
4210 suitable for unibyte text if current_buffer is unibyte. */
4211 if (it->multibyte_p)
4212 {
4213 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4214 struct face *face = FACE_FROM_ID (it->f, face_id);
4215 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4216 }
4217 }
4218
4219 return face_id;
4220 }
4221
4222
4223 \f
4224 /***********************************************************************
4225 Invisible text
4226 ***********************************************************************/
4227
4228 /* Set up iterator IT from invisible properties at its current
4229 position. Called from handle_stop. */
4230
4231 static enum prop_handled
4232 handle_invisible_prop (struct it *it)
4233 {
4234 enum prop_handled handled = HANDLED_NORMALLY;
4235 int invis;
4236 Lisp_Object prop;
4237
4238 if (STRINGP (it->string))
4239 {
4240 Lisp_Object end_charpos, limit;
4241
4242 /* Get the value of the invisible text property at the
4243 current position. Value will be nil if there is no such
4244 property. */
4245 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4246 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4247 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4248
4249 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4250 {
4251 /* Record whether we have to display an ellipsis for the
4252 invisible text. */
4253 bool display_ellipsis_p = (invis == 2);
4254 ptrdiff_t len, endpos;
4255
4256 handled = HANDLED_RECOMPUTE_PROPS;
4257
4258 /* Get the position at which the next visible text can be
4259 found in IT->string, if any. */
4260 endpos = len = SCHARS (it->string);
4261 XSETINT (limit, len);
4262 do
4263 {
4264 end_charpos
4265 = Fnext_single_property_change (end_charpos, Qinvisible,
4266 it->string, limit);
4267 /* Since LIMIT is always an integer, so should be the
4268 value returned by Fnext_single_property_change. */
4269 eassert (INTEGERP (end_charpos));
4270 if (INTEGERP (end_charpos))
4271 {
4272 endpos = XFASTINT (end_charpos);
4273 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4274 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4275 if (invis == 2)
4276 display_ellipsis_p = true;
4277 }
4278 else /* Should never happen; but if it does, exit the loop. */
4279 endpos = len;
4280 }
4281 while (invis != 0 && endpos < len);
4282
4283 if (display_ellipsis_p)
4284 it->ellipsis_p = true;
4285
4286 if (endpos < len)
4287 {
4288 /* Text at END_CHARPOS is visible. Move IT there. */
4289 struct text_pos old;
4290 ptrdiff_t oldpos;
4291
4292 old = it->current.string_pos;
4293 oldpos = CHARPOS (old);
4294 if (it->bidi_p)
4295 {
4296 if (it->bidi_it.first_elt
4297 && it->bidi_it.charpos < SCHARS (it->string))
4298 bidi_paragraph_init (it->paragraph_embedding,
4299 &it->bidi_it, true);
4300 /* Bidi-iterate out of the invisible text. */
4301 do
4302 {
4303 bidi_move_to_visually_next (&it->bidi_it);
4304 }
4305 while (oldpos <= it->bidi_it.charpos
4306 && it->bidi_it.charpos < endpos);
4307
4308 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4309 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4310 if (IT_CHARPOS (*it) >= endpos)
4311 it->prev_stop = endpos;
4312 }
4313 else
4314 {
4315 IT_STRING_CHARPOS (*it) = endpos;
4316 compute_string_pos (&it->current.string_pos, old, it->string);
4317 }
4318 }
4319 else
4320 {
4321 /* The rest of the string is invisible. If this is an
4322 overlay string, proceed with the next overlay string
4323 or whatever comes and return a character from there. */
4324 if (it->current.overlay_string_index >= 0
4325 && !display_ellipsis_p)
4326 {
4327 next_overlay_string (it);
4328 /* Don't check for overlay strings when we just
4329 finished processing them. */
4330 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4331 }
4332 else
4333 {
4334 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4335 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4336 }
4337 }
4338 }
4339 }
4340 else
4341 {
4342 ptrdiff_t newpos, next_stop, start_charpos, tem;
4343 Lisp_Object pos, overlay;
4344
4345 /* First of all, is there invisible text at this position? */
4346 tem = start_charpos = IT_CHARPOS (*it);
4347 pos = make_number (tem);
4348 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4349 &overlay);
4350 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4351
4352 /* If we are on invisible text, skip over it. */
4353 if (invis != 0 && start_charpos < it->end_charpos)
4354 {
4355 /* Record whether we have to display an ellipsis for the
4356 invisible text. */
4357 bool display_ellipsis_p = invis == 2;
4358
4359 handled = HANDLED_RECOMPUTE_PROPS;
4360
4361 /* Loop skipping over invisible text. The loop is left at
4362 ZV or with IT on the first char being visible again. */
4363 do
4364 {
4365 /* Try to skip some invisible text. Return value is the
4366 position reached which can be equal to where we start
4367 if there is nothing invisible there. This skips both
4368 over invisible text properties and overlays with
4369 invisible property. */
4370 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4371
4372 /* If we skipped nothing at all we weren't at invisible
4373 text in the first place. If everything to the end of
4374 the buffer was skipped, end the loop. */
4375 if (newpos == tem || newpos >= ZV)
4376 invis = 0;
4377 else
4378 {
4379 /* We skipped some characters but not necessarily
4380 all there are. Check if we ended up on visible
4381 text. Fget_char_property returns the property of
4382 the char before the given position, i.e. if we
4383 get invis = 0, this means that the char at
4384 newpos is visible. */
4385 pos = make_number (newpos);
4386 prop = Fget_char_property (pos, Qinvisible, it->window);
4387 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4388 }
4389
4390 /* If we ended up on invisible text, proceed to
4391 skip starting with next_stop. */
4392 if (invis != 0)
4393 tem = next_stop;
4394
4395 /* If there are adjacent invisible texts, don't lose the
4396 second one's ellipsis. */
4397 if (invis == 2)
4398 display_ellipsis_p = true;
4399 }
4400 while (invis != 0);
4401
4402 /* The position newpos is now either ZV or on visible text. */
4403 if (it->bidi_p)
4404 {
4405 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4406 bool on_newline
4407 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4408 bool after_newline
4409 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4410
4411 /* If the invisible text ends on a newline or on a
4412 character after a newline, we can avoid the costly,
4413 character by character, bidi iteration to NEWPOS, and
4414 instead simply reseat the iterator there. That's
4415 because all bidi reordering information is tossed at
4416 the newline. This is a big win for modes that hide
4417 complete lines, like Outline, Org, etc. */
4418 if (on_newline || after_newline)
4419 {
4420 struct text_pos tpos;
4421 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4422
4423 SET_TEXT_POS (tpos, newpos, bpos);
4424 reseat_1 (it, tpos, false);
4425 /* If we reseat on a newline/ZV, we need to prep the
4426 bidi iterator for advancing to the next character
4427 after the newline/EOB, keeping the current paragraph
4428 direction (so that PRODUCE_GLYPHS does TRT wrt
4429 prepending/appending glyphs to a glyph row). */
4430 if (on_newline)
4431 {
4432 it->bidi_it.first_elt = false;
4433 it->bidi_it.paragraph_dir = pdir;
4434 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4435 it->bidi_it.nchars = 1;
4436 it->bidi_it.ch_len = 1;
4437 }
4438 }
4439 else /* Must use the slow method. */
4440 {
4441 /* With bidi iteration, the region of invisible text
4442 could start and/or end in the middle of a
4443 non-base embedding level. Therefore, we need to
4444 skip invisible text using the bidi iterator,
4445 starting at IT's current position, until we find
4446 ourselves outside of the invisible text.
4447 Skipping invisible text _after_ bidi iteration
4448 avoids affecting the visual order of the
4449 displayed text when invisible properties are
4450 added or removed. */
4451 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4452 {
4453 /* If we were `reseat'ed to a new paragraph,
4454 determine the paragraph base direction. We
4455 need to do it now because
4456 next_element_from_buffer may not have a
4457 chance to do it, if we are going to skip any
4458 text at the beginning, which resets the
4459 FIRST_ELT flag. */
4460 bidi_paragraph_init (it->paragraph_embedding,
4461 &it->bidi_it, true);
4462 }
4463 do
4464 {
4465 bidi_move_to_visually_next (&it->bidi_it);
4466 }
4467 while (it->stop_charpos <= it->bidi_it.charpos
4468 && it->bidi_it.charpos < newpos);
4469 IT_CHARPOS (*it) = it->bidi_it.charpos;
4470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4471 /* If we overstepped NEWPOS, record its position in
4472 the iterator, so that we skip invisible text if
4473 later the bidi iteration lands us in the
4474 invisible region again. */
4475 if (IT_CHARPOS (*it) >= newpos)
4476 it->prev_stop = newpos;
4477 }
4478 }
4479 else
4480 {
4481 IT_CHARPOS (*it) = newpos;
4482 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4483 }
4484
4485 if (display_ellipsis_p)
4486 {
4487 /* Make sure that the glyphs of the ellipsis will get
4488 correct `charpos' values. If we would not update
4489 it->position here, the glyphs would belong to the
4490 last visible character _before_ the invisible
4491 text, which confuses `set_cursor_from_row'.
4492
4493 We use the last invisible position instead of the
4494 first because this way the cursor is always drawn on
4495 the first "." of the ellipsis, whenever PT is inside
4496 the invisible text. Otherwise the cursor would be
4497 placed _after_ the ellipsis when the point is after the
4498 first invisible character. */
4499 if (!STRINGP (it->object))
4500 {
4501 it->position.charpos = newpos - 1;
4502 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4503 }
4504 }
4505
4506 /* If there are before-strings at the start of invisible
4507 text, and the text is invisible because of a text
4508 property, arrange to show before-strings because 20.x did
4509 it that way. (If the text is invisible because of an
4510 overlay property instead of a text property, this is
4511 already handled in the overlay code.) */
4512 if (NILP (overlay)
4513 && get_overlay_strings (it, it->stop_charpos))
4514 {
4515 handled = HANDLED_RECOMPUTE_PROPS;
4516 if (it->sp > 0)
4517 {
4518 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4519 /* The call to get_overlay_strings above recomputes
4520 it->stop_charpos, but it only considers changes
4521 in properties and overlays beyond iterator's
4522 current position. This causes us to miss changes
4523 that happen exactly where the invisible property
4524 ended. So we play it safe here and force the
4525 iterator to check for potential stop positions
4526 immediately after the invisible text. Note that
4527 if get_overlay_strings returns true, it
4528 normally also pushed the iterator stack, so we
4529 need to update the stop position in the slot
4530 below the current one. */
4531 it->stack[it->sp - 1].stop_charpos
4532 = CHARPOS (it->stack[it->sp - 1].current.pos);
4533 }
4534 }
4535 else if (display_ellipsis_p)
4536 {
4537 it->ellipsis_p = true;
4538 /* Let the ellipsis display before
4539 considering any properties of the following char.
4540 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4541 handled = HANDLED_RETURN;
4542 }
4543 }
4544 }
4545
4546 return handled;
4547 }
4548
4549
4550 /* Make iterator IT return `...' next.
4551 Replaces LEN characters from buffer. */
4552
4553 static void
4554 setup_for_ellipsis (struct it *it, int len)
4555 {
4556 /* Use the display table definition for `...'. Invalid glyphs
4557 will be handled by the method returning elements from dpvec. */
4558 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4559 {
4560 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4561 it->dpvec = v->contents;
4562 it->dpend = v->contents + v->header.size;
4563 }
4564 else
4565 {
4566 /* Default `...'. */
4567 it->dpvec = default_invis_vector;
4568 it->dpend = default_invis_vector + 3;
4569 }
4570
4571 it->dpvec_char_len = len;
4572 it->current.dpvec_index = 0;
4573 it->dpvec_face_id = -1;
4574
4575 /* Remember the current face id in case glyphs specify faces.
4576 IT's face is restored in set_iterator_to_next.
4577 saved_face_id was set to preceding char's face in handle_stop. */
4578 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4579 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4580
4581 /* If the ellipsis represents buffer text, it means we advanced in
4582 the buffer, so we should no longer ignore overlay strings. */
4583 if (it->method == GET_FROM_BUFFER)
4584 it->ignore_overlay_strings_at_pos_p = false;
4585
4586 it->method = GET_FROM_DISPLAY_VECTOR;
4587 it->ellipsis_p = true;
4588 }
4589
4590
4591 \f
4592 /***********************************************************************
4593 'display' property
4594 ***********************************************************************/
4595
4596 /* Set up iterator IT from `display' property at its current position.
4597 Called from handle_stop.
4598 We return HANDLED_RETURN if some part of the display property
4599 overrides the display of the buffer text itself.
4600 Otherwise we return HANDLED_NORMALLY. */
4601
4602 static enum prop_handled
4603 handle_display_prop (struct it *it)
4604 {
4605 Lisp_Object propval, object, overlay;
4606 struct text_pos *position;
4607 ptrdiff_t bufpos;
4608 /* Nonzero if some property replaces the display of the text itself. */
4609 int display_replaced = 0;
4610
4611 if (STRINGP (it->string))
4612 {
4613 object = it->string;
4614 position = &it->current.string_pos;
4615 bufpos = CHARPOS (it->current.pos);
4616 }
4617 else
4618 {
4619 XSETWINDOW (object, it->w);
4620 position = &it->current.pos;
4621 bufpos = CHARPOS (*position);
4622 }
4623
4624 /* Reset those iterator values set from display property values. */
4625 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4626 it->space_width = Qnil;
4627 it->font_height = Qnil;
4628 it->voffset = 0;
4629
4630 /* We don't support recursive `display' properties, i.e. string
4631 values that have a string `display' property, that have a string
4632 `display' property etc. */
4633 if (!it->string_from_display_prop_p)
4634 it->area = TEXT_AREA;
4635
4636 propval = get_char_property_and_overlay (make_number (position->charpos),
4637 Qdisplay, object, &overlay);
4638 if (NILP (propval))
4639 return HANDLED_NORMALLY;
4640 /* Now OVERLAY is the overlay that gave us this property, or nil
4641 if it was a text property. */
4642
4643 if (!STRINGP (it->string))
4644 object = it->w->contents;
4645
4646 display_replaced = handle_display_spec (it, propval, object, overlay,
4647 position, bufpos,
4648 FRAME_WINDOW_P (it->f));
4649 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4650 }
4651
4652 /* Subroutine of handle_display_prop. Returns non-zero if the display
4653 specification in SPEC is a replacing specification, i.e. it would
4654 replace the text covered by `display' property with something else,
4655 such as an image or a display string. If SPEC includes any kind or
4656 `(space ...) specification, the value is 2; this is used by
4657 compute_display_string_pos, which see.
4658
4659 See handle_single_display_spec for documentation of arguments.
4660 FRAME_WINDOW_P is true if the window being redisplayed is on a
4661 GUI frame; this argument is used only if IT is NULL, see below.
4662
4663 IT can be NULL, if this is called by the bidi reordering code
4664 through compute_display_string_pos, which see. In that case, this
4665 function only examines SPEC, but does not otherwise "handle" it, in
4666 the sense that it doesn't set up members of IT from the display
4667 spec. */
4668 static int
4669 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4670 Lisp_Object overlay, struct text_pos *position,
4671 ptrdiff_t bufpos, bool frame_window_p)
4672 {
4673 int replacing = 0;
4674
4675 if (CONSP (spec)
4676 /* Simple specifications. */
4677 && !EQ (XCAR (spec), Qimage)
4678 && !EQ (XCAR (spec), Qspace)
4679 && !EQ (XCAR (spec), Qwhen)
4680 && !EQ (XCAR (spec), Qslice)
4681 && !EQ (XCAR (spec), Qspace_width)
4682 && !EQ (XCAR (spec), Qheight)
4683 && !EQ (XCAR (spec), Qraise)
4684 /* Marginal area specifications. */
4685 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4686 && !EQ (XCAR (spec), Qleft_fringe)
4687 && !EQ (XCAR (spec), Qright_fringe)
4688 && !NILP (XCAR (spec)))
4689 {
4690 for (; CONSP (spec); spec = XCDR (spec))
4691 {
4692 int rv = handle_single_display_spec (it, XCAR (spec), object,
4693 overlay, position, bufpos,
4694 replacing, frame_window_p);
4695 if (rv != 0)
4696 {
4697 replacing = rv;
4698 /* If some text in a string is replaced, `position' no
4699 longer points to the position of `object'. */
4700 if (!it || STRINGP (object))
4701 break;
4702 }
4703 }
4704 }
4705 else if (VECTORP (spec))
4706 {
4707 ptrdiff_t i;
4708 for (i = 0; i < ASIZE (spec); ++i)
4709 {
4710 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4711 overlay, position, bufpos,
4712 replacing, frame_window_p);
4713 if (rv != 0)
4714 {
4715 replacing = rv;
4716 /* If some text in a string is replaced, `position' no
4717 longer points to the position of `object'. */
4718 if (!it || STRINGP (object))
4719 break;
4720 }
4721 }
4722 }
4723 else
4724 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4725 bufpos, 0, frame_window_p);
4726 return replacing;
4727 }
4728
4729 /* Value is the position of the end of the `display' property starting
4730 at START_POS in OBJECT. */
4731
4732 static struct text_pos
4733 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4734 {
4735 Lisp_Object end;
4736 struct text_pos end_pos;
4737
4738 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4739 Qdisplay, object, Qnil);
4740 CHARPOS (end_pos) = XFASTINT (end);
4741 if (STRINGP (object))
4742 compute_string_pos (&end_pos, start_pos, it->string);
4743 else
4744 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4745
4746 return end_pos;
4747 }
4748
4749
4750 /* Set up IT from a single `display' property specification SPEC. OBJECT
4751 is the object in which the `display' property was found. *POSITION
4752 is the position in OBJECT at which the `display' property was found.
4753 BUFPOS is the buffer position of OBJECT (different from POSITION if
4754 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4755 previously saw a display specification which already replaced text
4756 display with something else, for example an image; we ignore such
4757 properties after the first one has been processed.
4758
4759 OVERLAY is the overlay this `display' property came from,
4760 or nil if it was a text property.
4761
4762 If SPEC is a `space' or `image' specification, and in some other
4763 cases too, set *POSITION to the position where the `display'
4764 property ends.
4765
4766 If IT is NULL, only examine the property specification in SPEC, but
4767 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4768 is intended to be displayed in a window on a GUI frame.
4769
4770 Value is non-zero if something was found which replaces the display
4771 of buffer or string text. */
4772
4773 static int
4774 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4775 Lisp_Object overlay, struct text_pos *position,
4776 ptrdiff_t bufpos, int display_replaced,
4777 bool frame_window_p)
4778 {
4779 Lisp_Object form;
4780 Lisp_Object location, value;
4781 struct text_pos start_pos = *position;
4782
4783 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4784 If the result is non-nil, use VALUE instead of SPEC. */
4785 form = Qt;
4786 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4787 {
4788 spec = XCDR (spec);
4789 if (!CONSP (spec))
4790 return 0;
4791 form = XCAR (spec);
4792 spec = XCDR (spec);
4793 }
4794
4795 if (!NILP (form) && !EQ (form, Qt))
4796 {
4797 ptrdiff_t count = SPECPDL_INDEX ();
4798
4799 /* Bind `object' to the object having the `display' property, a
4800 buffer or string. Bind `position' to the position in the
4801 object where the property was found, and `buffer-position'
4802 to the current position in the buffer. */
4803
4804 if (NILP (object))
4805 XSETBUFFER (object, current_buffer);
4806 specbind (Qobject, object);
4807 specbind (Qposition, make_number (CHARPOS (*position)));
4808 specbind (Qbuffer_position, make_number (bufpos));
4809 form = safe_eval (form);
4810 unbind_to (count, Qnil);
4811 }
4812
4813 if (NILP (form))
4814 return 0;
4815
4816 /* Handle `(height HEIGHT)' specifications. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qheight)
4819 && CONSP (XCDR (spec)))
4820 {
4821 if (it)
4822 {
4823 if (!FRAME_WINDOW_P (it->f))
4824 return 0;
4825
4826 it->font_height = XCAR (XCDR (spec));
4827 if (!NILP (it->font_height))
4828 {
4829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4830 int new_height = -1;
4831
4832 if (CONSP (it->font_height)
4833 && (EQ (XCAR (it->font_height), Qplus)
4834 || EQ (XCAR (it->font_height), Qminus))
4835 && CONSP (XCDR (it->font_height))
4836 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4837 {
4838 /* `(+ N)' or `(- N)' where N is an integer. */
4839 int steps = XINT (XCAR (XCDR (it->font_height)));
4840 if (EQ (XCAR (it->font_height), Qplus))
4841 steps = - steps;
4842 it->face_id = smaller_face (it->f, it->face_id, steps);
4843 }
4844 else if (FUNCTIONP (it->font_height))
4845 {
4846 /* Call function with current height as argument.
4847 Value is the new height. */
4848 Lisp_Object height;
4849 height = safe_call1 (it->font_height,
4850 face->lface[LFACE_HEIGHT_INDEX]);
4851 if (NUMBERP (height))
4852 new_height = XFLOATINT (height);
4853 }
4854 else if (NUMBERP (it->font_height))
4855 {
4856 /* Value is a multiple of the canonical char height. */
4857 struct face *f;
4858
4859 f = FACE_FROM_ID (it->f,
4860 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4861 new_height = (XFLOATINT (it->font_height)
4862 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4863 }
4864 else
4865 {
4866 /* Evaluate IT->font_height with `height' bound to the
4867 current specified height to get the new height. */
4868 ptrdiff_t count = SPECPDL_INDEX ();
4869
4870 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4871 value = safe_eval (it->font_height);
4872 unbind_to (count, Qnil);
4873
4874 if (NUMBERP (value))
4875 new_height = XFLOATINT (value);
4876 }
4877
4878 if (new_height > 0)
4879 it->face_id = face_with_height (it->f, it->face_id, new_height);
4880 }
4881 }
4882
4883 return 0;
4884 }
4885
4886 /* Handle `(space-width WIDTH)'. */
4887 if (CONSP (spec)
4888 && EQ (XCAR (spec), Qspace_width)
4889 && CONSP (XCDR (spec)))
4890 {
4891 if (it)
4892 {
4893 if (!FRAME_WINDOW_P (it->f))
4894 return 0;
4895
4896 value = XCAR (XCDR (spec));
4897 if (NUMBERP (value) && XFLOATINT (value) > 0)
4898 it->space_width = value;
4899 }
4900
4901 return 0;
4902 }
4903
4904 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4905 if (CONSP (spec)
4906 && EQ (XCAR (spec), Qslice))
4907 {
4908 Lisp_Object tem;
4909
4910 if (it)
4911 {
4912 if (!FRAME_WINDOW_P (it->f))
4913 return 0;
4914
4915 if (tem = XCDR (spec), CONSP (tem))
4916 {
4917 it->slice.x = XCAR (tem);
4918 if (tem = XCDR (tem), CONSP (tem))
4919 {
4920 it->slice.y = XCAR (tem);
4921 if (tem = XCDR (tem), CONSP (tem))
4922 {
4923 it->slice.width = XCAR (tem);
4924 if (tem = XCDR (tem), CONSP (tem))
4925 it->slice.height = XCAR (tem);
4926 }
4927 }
4928 }
4929 }
4930
4931 return 0;
4932 }
4933
4934 /* Handle `(raise FACTOR)'. */
4935 if (CONSP (spec)
4936 && EQ (XCAR (spec), Qraise)
4937 && CONSP (XCDR (spec)))
4938 {
4939 if (it)
4940 {
4941 if (!FRAME_WINDOW_P (it->f))
4942 return 0;
4943
4944 #ifdef HAVE_WINDOW_SYSTEM
4945 value = XCAR (XCDR (spec));
4946 if (NUMBERP (value))
4947 {
4948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4949 it->voffset = - (XFLOATINT (value)
4950 * (normal_char_height (face->font, -1)));
4951 }
4952 #endif /* HAVE_WINDOW_SYSTEM */
4953 }
4954
4955 return 0;
4956 }
4957
4958 /* Don't handle the other kinds of display specifications
4959 inside a string that we got from a `display' property. */
4960 if (it && it->string_from_display_prop_p)
4961 return 0;
4962
4963 /* Characters having this form of property are not displayed, so
4964 we have to find the end of the property. */
4965 if (it)
4966 {
4967 start_pos = *position;
4968 *position = display_prop_end (it, object, start_pos);
4969 /* If the display property comes from an overlay, don't consider
4970 any potential stop_charpos values before the end of that
4971 overlay. Since display_prop_end will happily find another
4972 'display' property coming from some other overlay or text
4973 property on buffer positions before this overlay's end, we
4974 need to ignore them, or else we risk displaying this
4975 overlay's display string/image twice. */
4976 if (!NILP (overlay))
4977 {
4978 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
4979
4980 if (ovendpos > CHARPOS (*position))
4981 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
4982 }
4983 }
4984 value = Qnil;
4985
4986 /* Stop the scan at that end position--we assume that all
4987 text properties change there. */
4988 if (it)
4989 it->stop_charpos = position->charpos;
4990
4991 /* Handle `(left-fringe BITMAP [FACE])'
4992 and `(right-fringe BITMAP [FACE])'. */
4993 if (CONSP (spec)
4994 && (EQ (XCAR (spec), Qleft_fringe)
4995 || EQ (XCAR (spec), Qright_fringe))
4996 && CONSP (XCDR (spec)))
4997 {
4998 int fringe_bitmap;
4999
5000 if (it)
5001 {
5002 if (!FRAME_WINDOW_P (it->f))
5003 /* If we return here, POSITION has been advanced
5004 across the text with this property. */
5005 {
5006 /* Synchronize the bidi iterator with POSITION. This is
5007 needed because we are not going to push the iterator
5008 on behalf of this display property, so there will be
5009 no pop_it call to do this synchronization for us. */
5010 if (it->bidi_p)
5011 {
5012 it->position = *position;
5013 iterate_out_of_display_property (it);
5014 *position = it->position;
5015 }
5016 return 1;
5017 }
5018 }
5019 else if (!frame_window_p)
5020 return 1;
5021
5022 #ifdef HAVE_WINDOW_SYSTEM
5023 value = XCAR (XCDR (spec));
5024 if (!SYMBOLP (value)
5025 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5026 /* If we return here, POSITION has been advanced
5027 across the text with this property. */
5028 {
5029 if (it && it->bidi_p)
5030 {
5031 it->position = *position;
5032 iterate_out_of_display_property (it);
5033 *position = it->position;
5034 }
5035 return 1;
5036 }
5037
5038 if (it)
5039 {
5040 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5041
5042 if (CONSP (XCDR (XCDR (spec))))
5043 {
5044 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5045 int face_id2 = lookup_derived_face (it->f, face_name,
5046 FRINGE_FACE_ID, false);
5047 if (face_id2 >= 0)
5048 face_id = face_id2;
5049 }
5050
5051 /* Save current settings of IT so that we can restore them
5052 when we are finished with the glyph property value. */
5053 push_it (it, position);
5054
5055 it->area = TEXT_AREA;
5056 it->what = IT_IMAGE;
5057 it->image_id = -1; /* no image */
5058 it->position = start_pos;
5059 it->object = NILP (object) ? it->w->contents : object;
5060 it->method = GET_FROM_IMAGE;
5061 it->from_overlay = Qnil;
5062 it->face_id = face_id;
5063 it->from_disp_prop_p = true;
5064
5065 /* Say that we haven't consumed the characters with
5066 `display' property yet. The call to pop_it in
5067 set_iterator_to_next will clean this up. */
5068 *position = start_pos;
5069
5070 if (EQ (XCAR (spec), Qleft_fringe))
5071 {
5072 it->left_user_fringe_bitmap = fringe_bitmap;
5073 it->left_user_fringe_face_id = face_id;
5074 }
5075 else
5076 {
5077 it->right_user_fringe_bitmap = fringe_bitmap;
5078 it->right_user_fringe_face_id = face_id;
5079 }
5080 }
5081 #endif /* HAVE_WINDOW_SYSTEM */
5082 return 1;
5083 }
5084
5085 /* Prepare to handle `((margin left-margin) ...)',
5086 `((margin right-margin) ...)' and `((margin nil) ...)'
5087 prefixes for display specifications. */
5088 location = Qunbound;
5089 if (CONSP (spec) && CONSP (XCAR (spec)))
5090 {
5091 Lisp_Object tem;
5092
5093 value = XCDR (spec);
5094 if (CONSP (value))
5095 value = XCAR (value);
5096
5097 tem = XCAR (spec);
5098 if (EQ (XCAR (tem), Qmargin)
5099 && (tem = XCDR (tem),
5100 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5101 (NILP (tem)
5102 || EQ (tem, Qleft_margin)
5103 || EQ (tem, Qright_margin))))
5104 location = tem;
5105 }
5106
5107 if (EQ (location, Qunbound))
5108 {
5109 location = Qnil;
5110 value = spec;
5111 }
5112
5113 /* After this point, VALUE is the property after any
5114 margin prefix has been stripped. It must be a string,
5115 an image specification, or `(space ...)'.
5116
5117 LOCATION specifies where to display: `left-margin',
5118 `right-margin' or nil. */
5119
5120 bool valid_p = (STRINGP (value)
5121 #ifdef HAVE_WINDOW_SYSTEM
5122 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5123 && valid_image_p (value))
5124 #endif /* not HAVE_WINDOW_SYSTEM */
5125 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5126
5127 if (valid_p && display_replaced == 0)
5128 {
5129 int retval = 1;
5130
5131 if (!it)
5132 {
5133 /* Callers need to know whether the display spec is any kind
5134 of `(space ...)' spec that is about to affect text-area
5135 display. */
5136 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5137 retval = 2;
5138 return retval;
5139 }
5140
5141 /* Save current settings of IT so that we can restore them
5142 when we are finished with the glyph property value. */
5143 push_it (it, position);
5144 it->from_overlay = overlay;
5145 it->from_disp_prop_p = true;
5146
5147 if (NILP (location))
5148 it->area = TEXT_AREA;
5149 else if (EQ (location, Qleft_margin))
5150 it->area = LEFT_MARGIN_AREA;
5151 else
5152 it->area = RIGHT_MARGIN_AREA;
5153
5154 if (STRINGP (value))
5155 {
5156 it->string = value;
5157 it->multibyte_p = STRING_MULTIBYTE (it->string);
5158 it->current.overlay_string_index = -1;
5159 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5160 it->end_charpos = it->string_nchars = SCHARS (it->string);
5161 it->method = GET_FROM_STRING;
5162 it->stop_charpos = 0;
5163 it->prev_stop = 0;
5164 it->base_level_stop = 0;
5165 it->string_from_display_prop_p = true;
5166 /* Say that we haven't consumed the characters with
5167 `display' property yet. The call to pop_it in
5168 set_iterator_to_next will clean this up. */
5169 if (BUFFERP (object))
5170 *position = start_pos;
5171
5172 /* Force paragraph direction to be that of the parent
5173 object. If the parent object's paragraph direction is
5174 not yet determined, default to L2R. */
5175 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5176 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5177 else
5178 it->paragraph_embedding = L2R;
5179
5180 /* Set up the bidi iterator for this display string. */
5181 if (it->bidi_p)
5182 {
5183 it->bidi_it.string.lstring = it->string;
5184 it->bidi_it.string.s = NULL;
5185 it->bidi_it.string.schars = it->end_charpos;
5186 it->bidi_it.string.bufpos = bufpos;
5187 it->bidi_it.string.from_disp_str = true;
5188 it->bidi_it.string.unibyte = !it->multibyte_p;
5189 it->bidi_it.w = it->w;
5190 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5191 }
5192 }
5193 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5194 {
5195 it->method = GET_FROM_STRETCH;
5196 it->object = value;
5197 *position = it->position = start_pos;
5198 retval = 1 + (it->area == TEXT_AREA);
5199 }
5200 #ifdef HAVE_WINDOW_SYSTEM
5201 else
5202 {
5203 it->what = IT_IMAGE;
5204 it->image_id = lookup_image (it->f, value);
5205 it->position = start_pos;
5206 it->object = NILP (object) ? it->w->contents : object;
5207 it->method = GET_FROM_IMAGE;
5208
5209 /* Say that we haven't consumed the characters with
5210 `display' property yet. The call to pop_it in
5211 set_iterator_to_next will clean this up. */
5212 *position = start_pos;
5213 }
5214 #endif /* HAVE_WINDOW_SYSTEM */
5215
5216 return retval;
5217 }
5218
5219 /* Invalid property or property not supported. Restore
5220 POSITION to what it was before. */
5221 *position = start_pos;
5222 return 0;
5223 }
5224
5225 /* Check if PROP is a display property value whose text should be
5226 treated as intangible. OVERLAY is the overlay from which PROP
5227 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5228 specify the buffer position covered by PROP. */
5229
5230 bool
5231 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5232 ptrdiff_t charpos, ptrdiff_t bytepos)
5233 {
5234 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5235 struct text_pos position;
5236
5237 SET_TEXT_POS (position, charpos, bytepos);
5238 return (handle_display_spec (NULL, prop, Qnil, overlay,
5239 &position, charpos, frame_window_p)
5240 != 0);
5241 }
5242
5243
5244 /* Return true if PROP is a display sub-property value containing STRING.
5245
5246 Implementation note: this and the following function are really
5247 special cases of handle_display_spec and
5248 handle_single_display_spec, and should ideally use the same code.
5249 Until they do, these two pairs must be consistent and must be
5250 modified in sync. */
5251
5252 static bool
5253 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5254 {
5255 if (EQ (string, prop))
5256 return true;
5257
5258 /* Skip over `when FORM'. */
5259 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5260 {
5261 prop = XCDR (prop);
5262 if (!CONSP (prop))
5263 return false;
5264 /* Actually, the condition following `when' should be eval'ed,
5265 like handle_single_display_spec does, and we should return
5266 false if it evaluates to nil. However, this function is
5267 called only when the buffer was already displayed and some
5268 glyph in the glyph matrix was found to come from a display
5269 string. Therefore, the condition was already evaluated, and
5270 the result was non-nil, otherwise the display string wouldn't
5271 have been displayed and we would have never been called for
5272 this property. Thus, we can skip the evaluation and assume
5273 its result is non-nil. */
5274 prop = XCDR (prop);
5275 }
5276
5277 if (CONSP (prop))
5278 /* Skip over `margin LOCATION'. */
5279 if (EQ (XCAR (prop), Qmargin))
5280 {
5281 prop = XCDR (prop);
5282 if (!CONSP (prop))
5283 return false;
5284
5285 prop = XCDR (prop);
5286 if (!CONSP (prop))
5287 return false;
5288 }
5289
5290 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5291 }
5292
5293
5294 /* Return true if STRING appears in the `display' property PROP. */
5295
5296 static bool
5297 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5298 {
5299 if (CONSP (prop)
5300 && !EQ (XCAR (prop), Qwhen)
5301 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5302 {
5303 /* A list of sub-properties. */
5304 while (CONSP (prop))
5305 {
5306 if (single_display_spec_string_p (XCAR (prop), string))
5307 return true;
5308 prop = XCDR (prop);
5309 }
5310 }
5311 else if (VECTORP (prop))
5312 {
5313 /* A vector of sub-properties. */
5314 ptrdiff_t i;
5315 for (i = 0; i < ASIZE (prop); ++i)
5316 if (single_display_spec_string_p (AREF (prop, i), string))
5317 return true;
5318 }
5319 else
5320 return single_display_spec_string_p (prop, string);
5321
5322 return false;
5323 }
5324
5325 /* Look for STRING in overlays and text properties in the current
5326 buffer, between character positions FROM and TO (excluding TO).
5327 BACK_P means look back (in this case, TO is supposed to be
5328 less than FROM).
5329 Value is the first character position where STRING was found, or
5330 zero if it wasn't found before hitting TO.
5331
5332 This function may only use code that doesn't eval because it is
5333 called asynchronously from note_mouse_highlight. */
5334
5335 static ptrdiff_t
5336 string_buffer_position_lim (Lisp_Object string,
5337 ptrdiff_t from, ptrdiff_t to, bool back_p)
5338 {
5339 Lisp_Object limit, prop, pos;
5340 bool found = false;
5341
5342 pos = make_number (max (from, BEGV));
5343
5344 if (!back_p) /* looking forward */
5345 {
5346 limit = make_number (min (to, ZV));
5347 while (!found && !EQ (pos, limit))
5348 {
5349 prop = Fget_char_property (pos, Qdisplay, Qnil);
5350 if (!NILP (prop) && display_prop_string_p (prop, string))
5351 found = true;
5352 else
5353 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5354 limit);
5355 }
5356 }
5357 else /* looking back */
5358 {
5359 limit = make_number (max (to, BEGV));
5360 while (!found && !EQ (pos, limit))
5361 {
5362 prop = Fget_char_property (pos, Qdisplay, Qnil);
5363 if (!NILP (prop) && display_prop_string_p (prop, string))
5364 found = true;
5365 else
5366 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5367 limit);
5368 }
5369 }
5370
5371 return found ? XINT (pos) : 0;
5372 }
5373
5374 /* Determine which buffer position in current buffer STRING comes from.
5375 AROUND_CHARPOS is an approximate position where it could come from.
5376 Value is the buffer position or 0 if it couldn't be determined.
5377
5378 This function is necessary because we don't record buffer positions
5379 in glyphs generated from strings (to keep struct glyph small).
5380 This function may only use code that doesn't eval because it is
5381 called asynchronously from note_mouse_highlight. */
5382
5383 static ptrdiff_t
5384 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5385 {
5386 const int MAX_DISTANCE = 1000;
5387 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5388 around_charpos + MAX_DISTANCE,
5389 false);
5390
5391 if (!found)
5392 found = string_buffer_position_lim (string, around_charpos,
5393 around_charpos - MAX_DISTANCE, true);
5394 return found;
5395 }
5396
5397
5398 \f
5399 /***********************************************************************
5400 `composition' property
5401 ***********************************************************************/
5402
5403 /* Set up iterator IT from `composition' property at its current
5404 position. Called from handle_stop. */
5405
5406 static enum prop_handled
5407 handle_composition_prop (struct it *it)
5408 {
5409 Lisp_Object prop, string;
5410 ptrdiff_t pos, pos_byte, start, end;
5411
5412 if (STRINGP (it->string))
5413 {
5414 unsigned char *s;
5415
5416 pos = IT_STRING_CHARPOS (*it);
5417 pos_byte = IT_STRING_BYTEPOS (*it);
5418 string = it->string;
5419 s = SDATA (string) + pos_byte;
5420 it->c = STRING_CHAR (s);
5421 }
5422 else
5423 {
5424 pos = IT_CHARPOS (*it);
5425 pos_byte = IT_BYTEPOS (*it);
5426 string = Qnil;
5427 it->c = FETCH_CHAR (pos_byte);
5428 }
5429
5430 /* If there's a valid composition and point is not inside of the
5431 composition (in the case that the composition is from the current
5432 buffer), draw a glyph composed from the composition components. */
5433 if (find_composition (pos, -1, &start, &end, &prop, string)
5434 && composition_valid_p (start, end, prop)
5435 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5436 {
5437 if (start < pos)
5438 /* As we can't handle this situation (perhaps font-lock added
5439 a new composition), we just return here hoping that next
5440 redisplay will detect this composition much earlier. */
5441 return HANDLED_NORMALLY;
5442 if (start != pos)
5443 {
5444 if (STRINGP (it->string))
5445 pos_byte = string_char_to_byte (it->string, start);
5446 else
5447 pos_byte = CHAR_TO_BYTE (start);
5448 }
5449 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5450 prop, string);
5451
5452 if (it->cmp_it.id >= 0)
5453 {
5454 it->cmp_it.ch = -1;
5455 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5456 it->cmp_it.nglyphs = -1;
5457 }
5458 }
5459
5460 return HANDLED_NORMALLY;
5461 }
5462
5463
5464 \f
5465 /***********************************************************************
5466 Overlay strings
5467 ***********************************************************************/
5468
5469 /* The following structure is used to record overlay strings for
5470 later sorting in load_overlay_strings. */
5471
5472 struct overlay_entry
5473 {
5474 Lisp_Object overlay;
5475 Lisp_Object string;
5476 EMACS_INT priority;
5477 bool after_string_p;
5478 };
5479
5480
5481 /* Set up iterator IT from overlay strings at its current position.
5482 Called from handle_stop. */
5483
5484 static enum prop_handled
5485 handle_overlay_change (struct it *it)
5486 {
5487 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5488 return HANDLED_RECOMPUTE_PROPS;
5489 else
5490 return HANDLED_NORMALLY;
5491 }
5492
5493
5494 /* Set up the next overlay string for delivery by IT, if there is an
5495 overlay string to deliver. Called by set_iterator_to_next when the
5496 end of the current overlay string is reached. If there are more
5497 overlay strings to display, IT->string and
5498 IT->current.overlay_string_index are set appropriately here.
5499 Otherwise IT->string is set to nil. */
5500
5501 static void
5502 next_overlay_string (struct it *it)
5503 {
5504 ++it->current.overlay_string_index;
5505 if (it->current.overlay_string_index == it->n_overlay_strings)
5506 {
5507 /* No more overlay strings. Restore IT's settings to what
5508 they were before overlay strings were processed, and
5509 continue to deliver from current_buffer. */
5510
5511 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5512 pop_it (it);
5513 eassert (it->sp > 0
5514 || (NILP (it->string)
5515 && it->method == GET_FROM_BUFFER
5516 && it->stop_charpos >= BEGV
5517 && it->stop_charpos <= it->end_charpos));
5518 it->current.overlay_string_index = -1;
5519 it->n_overlay_strings = 0;
5520 /* If there's an empty display string on the stack, pop the
5521 stack, to resync the bidi iterator with IT's position. Such
5522 empty strings are pushed onto the stack in
5523 get_overlay_strings_1. */
5524 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5525 pop_it (it);
5526
5527 /* Since we've exhausted overlay strings at this buffer
5528 position, set the flag to ignore overlays until we move to
5529 another position. The flag is reset in
5530 next_element_from_buffer. */
5531 it->ignore_overlay_strings_at_pos_p = true;
5532
5533 /* If we're at the end of the buffer, record that we have
5534 processed the overlay strings there already, so that
5535 next_element_from_buffer doesn't try it again. */
5536 if (NILP (it->string)
5537 && IT_CHARPOS (*it) >= it->end_charpos
5538 && it->overlay_strings_charpos >= it->end_charpos)
5539 it->overlay_strings_at_end_processed_p = true;
5540 /* Note: we reset overlay_strings_charpos only here, to make
5541 sure the just-processed overlays were indeed at EOB.
5542 Otherwise, overlays on text with invisible text property,
5543 which are processed with IT's position past the invisible
5544 text, might fool us into thinking the overlays at EOB were
5545 already processed (linum-mode can cause this, for
5546 example). */
5547 it->overlay_strings_charpos = -1;
5548 }
5549 else
5550 {
5551 /* There are more overlay strings to process. If
5552 IT->current.overlay_string_index has advanced to a position
5553 where we must load IT->overlay_strings with more strings, do
5554 it. We must load at the IT->overlay_strings_charpos where
5555 IT->n_overlay_strings was originally computed; when invisible
5556 text is present, this might not be IT_CHARPOS (Bug#7016). */
5557 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5558
5559 if (it->current.overlay_string_index && i == 0)
5560 load_overlay_strings (it, it->overlay_strings_charpos);
5561
5562 /* Initialize IT to deliver display elements from the overlay
5563 string. */
5564 it->string = it->overlay_strings[i];
5565 it->multibyte_p = STRING_MULTIBYTE (it->string);
5566 SET_TEXT_POS (it->current.string_pos, 0, 0);
5567 it->method = GET_FROM_STRING;
5568 it->stop_charpos = 0;
5569 it->end_charpos = SCHARS (it->string);
5570 if (it->cmp_it.stop_pos >= 0)
5571 it->cmp_it.stop_pos = 0;
5572 it->prev_stop = 0;
5573 it->base_level_stop = 0;
5574
5575 /* Set up the bidi iterator for this overlay string. */
5576 if (it->bidi_p)
5577 {
5578 it->bidi_it.string.lstring = it->string;
5579 it->bidi_it.string.s = NULL;
5580 it->bidi_it.string.schars = SCHARS (it->string);
5581 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5582 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5583 it->bidi_it.string.unibyte = !it->multibyte_p;
5584 it->bidi_it.w = it->w;
5585 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5586 }
5587 }
5588
5589 CHECK_IT (it);
5590 }
5591
5592
5593 /* Compare two overlay_entry structures E1 and E2. Used as a
5594 comparison function for qsort in load_overlay_strings. Overlay
5595 strings for the same position are sorted so that
5596
5597 1. All after-strings come in front of before-strings, except
5598 when they come from the same overlay.
5599
5600 2. Within after-strings, strings are sorted so that overlay strings
5601 from overlays with higher priorities come first.
5602
5603 2. Within before-strings, strings are sorted so that overlay
5604 strings from overlays with higher priorities come last.
5605
5606 Value is analogous to strcmp. */
5607
5608
5609 static int
5610 compare_overlay_entries (const void *e1, const void *e2)
5611 {
5612 struct overlay_entry const *entry1 = e1;
5613 struct overlay_entry const *entry2 = e2;
5614 int result;
5615
5616 if (entry1->after_string_p != entry2->after_string_p)
5617 {
5618 /* Let after-strings appear in front of before-strings if
5619 they come from different overlays. */
5620 if (EQ (entry1->overlay, entry2->overlay))
5621 result = entry1->after_string_p ? 1 : -1;
5622 else
5623 result = entry1->after_string_p ? -1 : 1;
5624 }
5625 else if (entry1->priority != entry2->priority)
5626 {
5627 if (entry1->after_string_p)
5628 /* After-strings sorted in order of decreasing priority. */
5629 result = entry2->priority < entry1->priority ? -1 : 1;
5630 else
5631 /* Before-strings sorted in order of increasing priority. */
5632 result = entry1->priority < entry2->priority ? -1 : 1;
5633 }
5634 else
5635 result = 0;
5636
5637 return result;
5638 }
5639
5640
5641 /* Load the vector IT->overlay_strings with overlay strings from IT's
5642 current buffer position, or from CHARPOS if that is > 0. Set
5643 IT->n_overlays to the total number of overlay strings found.
5644
5645 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5646 a time. On entry into load_overlay_strings,
5647 IT->current.overlay_string_index gives the number of overlay
5648 strings that have already been loaded by previous calls to this
5649 function.
5650
5651 IT->add_overlay_start contains an additional overlay start
5652 position to consider for taking overlay strings from, if non-zero.
5653 This position comes into play when the overlay has an `invisible'
5654 property, and both before and after-strings. When we've skipped to
5655 the end of the overlay, because of its `invisible' property, we
5656 nevertheless want its before-string to appear.
5657 IT->add_overlay_start will contain the overlay start position
5658 in this case.
5659
5660 Overlay strings are sorted so that after-string strings come in
5661 front of before-string strings. Within before and after-strings,
5662 strings are sorted by overlay priority. See also function
5663 compare_overlay_entries. */
5664
5665 static void
5666 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5667 {
5668 Lisp_Object overlay, window, str, invisible;
5669 struct Lisp_Overlay *ov;
5670 ptrdiff_t start, end;
5671 ptrdiff_t n = 0, i, j;
5672 int invis;
5673 struct overlay_entry entriesbuf[20];
5674 ptrdiff_t size = ARRAYELTS (entriesbuf);
5675 struct overlay_entry *entries = entriesbuf;
5676 USE_SAFE_ALLOCA;
5677
5678 if (charpos <= 0)
5679 charpos = IT_CHARPOS (*it);
5680
5681 /* Append the overlay string STRING of overlay OVERLAY to vector
5682 `entries' which has size `size' and currently contains `n'
5683 elements. AFTER_P means STRING is an after-string of
5684 OVERLAY. */
5685 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5686 do \
5687 { \
5688 Lisp_Object priority; \
5689 \
5690 if (n == size) \
5691 { \
5692 struct overlay_entry *old = entries; \
5693 SAFE_NALLOCA (entries, 2, size); \
5694 memcpy (entries, old, size * sizeof *entries); \
5695 size *= 2; \
5696 } \
5697 \
5698 entries[n].string = (STRING); \
5699 entries[n].overlay = (OVERLAY); \
5700 priority = Foverlay_get ((OVERLAY), Qpriority); \
5701 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5702 entries[n].after_string_p = (AFTER_P); \
5703 ++n; \
5704 } \
5705 while (false)
5706
5707 /* Process overlay before the overlay center. */
5708 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5709 {
5710 XSETMISC (overlay, ov);
5711 eassert (OVERLAYP (overlay));
5712 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5713 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5714
5715 if (end < charpos)
5716 break;
5717
5718 /* Skip this overlay if it doesn't start or end at IT's current
5719 position. */
5720 if (end != charpos && start != charpos)
5721 continue;
5722
5723 /* Skip this overlay if it doesn't apply to IT->w. */
5724 window = Foverlay_get (overlay, Qwindow);
5725 if (WINDOWP (window) && XWINDOW (window) != it->w)
5726 continue;
5727
5728 /* If the text ``under'' the overlay is invisible, both before-
5729 and after-strings from this overlay are visible; start and
5730 end position are indistinguishable. */
5731 invisible = Foverlay_get (overlay, Qinvisible);
5732 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5733
5734 /* If overlay has a non-empty before-string, record it. */
5735 if ((start == charpos || (end == charpos && invis != 0))
5736 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5737 && SCHARS (str))
5738 RECORD_OVERLAY_STRING (overlay, str, false);
5739
5740 /* If overlay has a non-empty after-string, record it. */
5741 if ((end == charpos || (start == charpos && invis != 0))
5742 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5743 && SCHARS (str))
5744 RECORD_OVERLAY_STRING (overlay, str, true);
5745 }
5746
5747 /* Process overlays after the overlay center. */
5748 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5749 {
5750 XSETMISC (overlay, ov);
5751 eassert (OVERLAYP (overlay));
5752 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5753 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5754
5755 if (start > charpos)
5756 break;
5757
5758 /* Skip this overlay if it doesn't start or end at IT's current
5759 position. */
5760 if (end != charpos && start != charpos)
5761 continue;
5762
5763 /* Skip this overlay if it doesn't apply to IT->w. */
5764 window = Foverlay_get (overlay, Qwindow);
5765 if (WINDOWP (window) && XWINDOW (window) != it->w)
5766 continue;
5767
5768 /* If the text ``under'' the overlay is invisible, it has a zero
5769 dimension, and both before- and after-strings apply. */
5770 invisible = Foverlay_get (overlay, Qinvisible);
5771 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5772
5773 /* If overlay has a non-empty before-string, record it. */
5774 if ((start == charpos || (end == charpos && invis != 0))
5775 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5776 && SCHARS (str))
5777 RECORD_OVERLAY_STRING (overlay, str, false);
5778
5779 /* If overlay has a non-empty after-string, record it. */
5780 if ((end == charpos || (start == charpos && invis != 0))
5781 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5782 && SCHARS (str))
5783 RECORD_OVERLAY_STRING (overlay, str, true);
5784 }
5785
5786 #undef RECORD_OVERLAY_STRING
5787
5788 /* Sort entries. */
5789 if (n > 1)
5790 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5791
5792 /* Record number of overlay strings, and where we computed it. */
5793 it->n_overlay_strings = n;
5794 it->overlay_strings_charpos = charpos;
5795
5796 /* IT->current.overlay_string_index is the number of overlay strings
5797 that have already been consumed by IT. Copy some of the
5798 remaining overlay strings to IT->overlay_strings. */
5799 i = 0;
5800 j = it->current.overlay_string_index;
5801 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5802 {
5803 it->overlay_strings[i] = entries[j].string;
5804 it->string_overlays[i++] = entries[j++].overlay;
5805 }
5806
5807 CHECK_IT (it);
5808 SAFE_FREE ();
5809 }
5810
5811
5812 /* Get the first chunk of overlay strings at IT's current buffer
5813 position, or at CHARPOS if that is > 0. Value is true if at
5814 least one overlay string was found. */
5815
5816 static bool
5817 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5818 {
5819 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5820 process. This fills IT->overlay_strings with strings, and sets
5821 IT->n_overlay_strings to the total number of strings to process.
5822 IT->pos.overlay_string_index has to be set temporarily to zero
5823 because load_overlay_strings needs this; it must be set to -1
5824 when no overlay strings are found because a zero value would
5825 indicate a position in the first overlay string. */
5826 it->current.overlay_string_index = 0;
5827 load_overlay_strings (it, charpos);
5828
5829 /* If we found overlay strings, set up IT to deliver display
5830 elements from the first one. Otherwise set up IT to deliver
5831 from current_buffer. */
5832 if (it->n_overlay_strings)
5833 {
5834 /* Make sure we know settings in current_buffer, so that we can
5835 restore meaningful values when we're done with the overlay
5836 strings. */
5837 if (compute_stop_p)
5838 compute_stop_pos (it);
5839 eassert (it->face_id >= 0);
5840
5841 /* Save IT's settings. They are restored after all overlay
5842 strings have been processed. */
5843 eassert (!compute_stop_p || it->sp == 0);
5844
5845 /* When called from handle_stop, there might be an empty display
5846 string loaded. In that case, don't bother saving it. But
5847 don't use this optimization with the bidi iterator, since we
5848 need the corresponding pop_it call to resync the bidi
5849 iterator's position with IT's position, after we are done
5850 with the overlay strings. (The corresponding call to pop_it
5851 in case of an empty display string is in
5852 next_overlay_string.) */
5853 if (!(!it->bidi_p
5854 && STRINGP (it->string) && !SCHARS (it->string)))
5855 push_it (it, NULL);
5856
5857 /* Set up IT to deliver display elements from the first overlay
5858 string. */
5859 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5860 it->string = it->overlay_strings[0];
5861 it->from_overlay = Qnil;
5862 it->stop_charpos = 0;
5863 eassert (STRINGP (it->string));
5864 it->end_charpos = SCHARS (it->string);
5865 it->prev_stop = 0;
5866 it->base_level_stop = 0;
5867 it->multibyte_p = STRING_MULTIBYTE (it->string);
5868 it->method = GET_FROM_STRING;
5869 it->from_disp_prop_p = 0;
5870
5871 /* Force paragraph direction to be that of the parent
5872 buffer. */
5873 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5874 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5875 else
5876 it->paragraph_embedding = L2R;
5877
5878 /* Set up the bidi iterator for this overlay string. */
5879 if (it->bidi_p)
5880 {
5881 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5882
5883 it->bidi_it.string.lstring = it->string;
5884 it->bidi_it.string.s = NULL;
5885 it->bidi_it.string.schars = SCHARS (it->string);
5886 it->bidi_it.string.bufpos = pos;
5887 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5888 it->bidi_it.string.unibyte = !it->multibyte_p;
5889 it->bidi_it.w = it->w;
5890 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5891 }
5892 return true;
5893 }
5894
5895 it->current.overlay_string_index = -1;
5896 return false;
5897 }
5898
5899 static bool
5900 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5901 {
5902 it->string = Qnil;
5903 it->method = GET_FROM_BUFFER;
5904
5905 get_overlay_strings_1 (it, charpos, true);
5906
5907 CHECK_IT (it);
5908
5909 /* Value is true if we found at least one overlay string. */
5910 return STRINGP (it->string);
5911 }
5912
5913
5914 \f
5915 /***********************************************************************
5916 Saving and restoring state
5917 ***********************************************************************/
5918
5919 /* Save current settings of IT on IT->stack. Called, for example,
5920 before setting up IT for an overlay string, to be able to restore
5921 IT's settings to what they were after the overlay string has been
5922 processed. If POSITION is non-NULL, it is the position to save on
5923 the stack instead of IT->position. */
5924
5925 static void
5926 push_it (struct it *it, struct text_pos *position)
5927 {
5928 struct iterator_stack_entry *p;
5929
5930 eassert (it->sp < IT_STACK_SIZE);
5931 p = it->stack + it->sp;
5932
5933 p->stop_charpos = it->stop_charpos;
5934 p->prev_stop = it->prev_stop;
5935 p->base_level_stop = it->base_level_stop;
5936 p->cmp_it = it->cmp_it;
5937 eassert (it->face_id >= 0);
5938 p->face_id = it->face_id;
5939 p->string = it->string;
5940 p->method = it->method;
5941 p->from_overlay = it->from_overlay;
5942 switch (p->method)
5943 {
5944 case GET_FROM_IMAGE:
5945 p->u.image.object = it->object;
5946 p->u.image.image_id = it->image_id;
5947 p->u.image.slice = it->slice;
5948 break;
5949 case GET_FROM_STRETCH:
5950 p->u.stretch.object = it->object;
5951 break;
5952 case GET_FROM_BUFFER:
5953 case GET_FROM_DISPLAY_VECTOR:
5954 case GET_FROM_STRING:
5955 case GET_FROM_C_STRING:
5956 break;
5957 default:
5958 emacs_abort ();
5959 }
5960 p->position = position ? *position : it->position;
5961 p->current = it->current;
5962 p->end_charpos = it->end_charpos;
5963 p->string_nchars = it->string_nchars;
5964 p->area = it->area;
5965 p->multibyte_p = it->multibyte_p;
5966 p->avoid_cursor_p = it->avoid_cursor_p;
5967 p->space_width = it->space_width;
5968 p->font_height = it->font_height;
5969 p->voffset = it->voffset;
5970 p->string_from_display_prop_p = it->string_from_display_prop_p;
5971 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5972 p->display_ellipsis_p = false;
5973 p->line_wrap = it->line_wrap;
5974 p->bidi_p = it->bidi_p;
5975 p->paragraph_embedding = it->paragraph_embedding;
5976 p->from_disp_prop_p = it->from_disp_prop_p;
5977 ++it->sp;
5978
5979 /* Save the state of the bidi iterator as well. */
5980 if (it->bidi_p)
5981 bidi_push_it (&it->bidi_it);
5982 }
5983
5984 static void
5985 iterate_out_of_display_property (struct it *it)
5986 {
5987 bool buffer_p = !STRINGP (it->string);
5988 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5989 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5990
5991 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5992
5993 /* Maybe initialize paragraph direction. If we are at the beginning
5994 of a new paragraph, next_element_from_buffer may not have a
5995 chance to do that. */
5996 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5997 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
5998 /* prev_stop can be zero, so check against BEGV as well. */
5999 while (it->bidi_it.charpos >= bob
6000 && it->prev_stop <= it->bidi_it.charpos
6001 && it->bidi_it.charpos < CHARPOS (it->position)
6002 && it->bidi_it.charpos < eob)
6003 bidi_move_to_visually_next (&it->bidi_it);
6004 /* Record the stop_pos we just crossed, for when we cross it
6005 back, maybe. */
6006 if (it->bidi_it.charpos > CHARPOS (it->position))
6007 it->prev_stop = CHARPOS (it->position);
6008 /* If we ended up not where pop_it put us, resync IT's
6009 positional members with the bidi iterator. */
6010 if (it->bidi_it.charpos != CHARPOS (it->position))
6011 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6012 if (buffer_p)
6013 it->current.pos = it->position;
6014 else
6015 it->current.string_pos = it->position;
6016 }
6017
6018 /* Restore IT's settings from IT->stack. Called, for example, when no
6019 more overlay strings must be processed, and we return to delivering
6020 display elements from a buffer, or when the end of a string from a
6021 `display' property is reached and we return to delivering display
6022 elements from an overlay string, or from a buffer. */
6023
6024 static void
6025 pop_it (struct it *it)
6026 {
6027 struct iterator_stack_entry *p;
6028 bool from_display_prop = it->from_disp_prop_p;
6029 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6030
6031 eassert (it->sp > 0);
6032 --it->sp;
6033 p = it->stack + it->sp;
6034 it->stop_charpos = p->stop_charpos;
6035 it->prev_stop = p->prev_stop;
6036 it->base_level_stop = p->base_level_stop;
6037 it->cmp_it = p->cmp_it;
6038 it->face_id = p->face_id;
6039 it->current = p->current;
6040 it->position = p->position;
6041 it->string = p->string;
6042 it->from_overlay = p->from_overlay;
6043 if (NILP (it->string))
6044 SET_TEXT_POS (it->current.string_pos, -1, -1);
6045 it->method = p->method;
6046 switch (it->method)
6047 {
6048 case GET_FROM_IMAGE:
6049 it->image_id = p->u.image.image_id;
6050 it->object = p->u.image.object;
6051 it->slice = p->u.image.slice;
6052 break;
6053 case GET_FROM_STRETCH:
6054 it->object = p->u.stretch.object;
6055 break;
6056 case GET_FROM_BUFFER:
6057 it->object = it->w->contents;
6058 break;
6059 case GET_FROM_STRING:
6060 {
6061 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6062
6063 /* Restore the face_box_p flag, since it could have been
6064 overwritten by the face of the object that we just finished
6065 displaying. */
6066 if (face)
6067 it->face_box_p = face->box != FACE_NO_BOX;
6068 it->object = it->string;
6069 }
6070 break;
6071 case GET_FROM_DISPLAY_VECTOR:
6072 if (it->s)
6073 it->method = GET_FROM_C_STRING;
6074 else if (STRINGP (it->string))
6075 it->method = GET_FROM_STRING;
6076 else
6077 {
6078 it->method = GET_FROM_BUFFER;
6079 it->object = it->w->contents;
6080 }
6081 break;
6082 case GET_FROM_C_STRING:
6083 break;
6084 default:
6085 emacs_abort ();
6086 }
6087 it->end_charpos = p->end_charpos;
6088 it->string_nchars = p->string_nchars;
6089 it->area = p->area;
6090 it->multibyte_p = p->multibyte_p;
6091 it->avoid_cursor_p = p->avoid_cursor_p;
6092 it->space_width = p->space_width;
6093 it->font_height = p->font_height;
6094 it->voffset = p->voffset;
6095 it->string_from_display_prop_p = p->string_from_display_prop_p;
6096 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6097 it->line_wrap = p->line_wrap;
6098 it->bidi_p = p->bidi_p;
6099 it->paragraph_embedding = p->paragraph_embedding;
6100 it->from_disp_prop_p = p->from_disp_prop_p;
6101 if (it->bidi_p)
6102 {
6103 bidi_pop_it (&it->bidi_it);
6104 /* Bidi-iterate until we get out of the portion of text, if any,
6105 covered by a `display' text property or by an overlay with
6106 `display' property. (We cannot just jump there, because the
6107 internal coherency of the bidi iterator state can not be
6108 preserved across such jumps.) We also must determine the
6109 paragraph base direction if the overlay we just processed is
6110 at the beginning of a new paragraph. */
6111 if (from_display_prop
6112 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6113 iterate_out_of_display_property (it);
6114
6115 eassert ((BUFFERP (it->object)
6116 && IT_CHARPOS (*it) == it->bidi_it.charpos
6117 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6118 || (STRINGP (it->object)
6119 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6120 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6121 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6122 }
6123 /* If we move the iterator over text covered by a display property
6124 to a new buffer position, any info about previously seen overlays
6125 is no longer valid. */
6126 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6127 it->ignore_overlay_strings_at_pos_p = false;
6128 }
6129
6130
6131 \f
6132 /***********************************************************************
6133 Moving over lines
6134 ***********************************************************************/
6135
6136 /* Set IT's current position to the previous line start. */
6137
6138 static void
6139 back_to_previous_line_start (struct it *it)
6140 {
6141 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6142
6143 DEC_BOTH (cp, bp);
6144 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6145 }
6146
6147
6148 /* Move IT to the next line start.
6149
6150 Value is true if a newline was found. Set *SKIPPED_P to true if
6151 we skipped over part of the text (as opposed to moving the iterator
6152 continuously over the text). Otherwise, don't change the value
6153 of *SKIPPED_P.
6154
6155 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6156 iterator on the newline, if it was found.
6157
6158 Newlines may come from buffer text, overlay strings, or strings
6159 displayed via the `display' property. That's the reason we can't
6160 simply use find_newline_no_quit.
6161
6162 Note that this function may not skip over invisible text that is so
6163 because of text properties and immediately follows a newline. If
6164 it would, function reseat_at_next_visible_line_start, when called
6165 from set_iterator_to_next, would effectively make invisible
6166 characters following a newline part of the wrong glyph row, which
6167 leads to wrong cursor motion. */
6168
6169 static bool
6170 forward_to_next_line_start (struct it *it, bool *skipped_p,
6171 struct bidi_it *bidi_it_prev)
6172 {
6173 ptrdiff_t old_selective;
6174 bool newline_found_p = false;
6175 int n;
6176 const int MAX_NEWLINE_DISTANCE = 500;
6177
6178 /* If already on a newline, just consume it to avoid unintended
6179 skipping over invisible text below. */
6180 if (it->what == IT_CHARACTER
6181 && it->c == '\n'
6182 && CHARPOS (it->position) == IT_CHARPOS (*it))
6183 {
6184 if (it->bidi_p && bidi_it_prev)
6185 *bidi_it_prev = it->bidi_it;
6186 set_iterator_to_next (it, false);
6187 it->c = 0;
6188 return true;
6189 }
6190
6191 /* Don't handle selective display in the following. It's (a)
6192 unnecessary because it's done by the caller, and (b) leads to an
6193 infinite recursion because next_element_from_ellipsis indirectly
6194 calls this function. */
6195 old_selective = it->selective;
6196 it->selective = 0;
6197
6198 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6199 from buffer text. */
6200 for (n = 0;
6201 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6202 n += !STRINGP (it->string))
6203 {
6204 if (!get_next_display_element (it))
6205 return false;
6206 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6207 if (newline_found_p && it->bidi_p && bidi_it_prev)
6208 *bidi_it_prev = it->bidi_it;
6209 set_iterator_to_next (it, false);
6210 }
6211
6212 /* If we didn't find a newline near enough, see if we can use a
6213 short-cut. */
6214 if (!newline_found_p)
6215 {
6216 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6217 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6218 1, &bytepos);
6219 Lisp_Object pos;
6220
6221 eassert (!STRINGP (it->string));
6222
6223 /* If there isn't any `display' property in sight, and no
6224 overlays, we can just use the position of the newline in
6225 buffer text. */
6226 if (it->stop_charpos >= limit
6227 || ((pos = Fnext_single_property_change (make_number (start),
6228 Qdisplay, Qnil,
6229 make_number (limit)),
6230 NILP (pos))
6231 && next_overlay_change (start) == ZV))
6232 {
6233 if (!it->bidi_p)
6234 {
6235 IT_CHARPOS (*it) = limit;
6236 IT_BYTEPOS (*it) = bytepos;
6237 }
6238 else
6239 {
6240 struct bidi_it bprev;
6241
6242 /* Help bidi.c avoid expensive searches for display
6243 properties and overlays, by telling it that there are
6244 none up to `limit'. */
6245 if (it->bidi_it.disp_pos < limit)
6246 {
6247 it->bidi_it.disp_pos = limit;
6248 it->bidi_it.disp_prop = 0;
6249 }
6250 do {
6251 bprev = it->bidi_it;
6252 bidi_move_to_visually_next (&it->bidi_it);
6253 } while (it->bidi_it.charpos != limit);
6254 IT_CHARPOS (*it) = limit;
6255 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6256 if (bidi_it_prev)
6257 *bidi_it_prev = bprev;
6258 }
6259 *skipped_p = newline_found_p = true;
6260 }
6261 else
6262 {
6263 while (get_next_display_element (it)
6264 && !newline_found_p)
6265 {
6266 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6267 if (newline_found_p && it->bidi_p && bidi_it_prev)
6268 *bidi_it_prev = it->bidi_it;
6269 set_iterator_to_next (it, false);
6270 }
6271 }
6272 }
6273
6274 it->selective = old_selective;
6275 return newline_found_p;
6276 }
6277
6278
6279 /* Set IT's current position to the previous visible line start. Skip
6280 invisible text that is so either due to text properties or due to
6281 selective display. Caution: this does not change IT->current_x and
6282 IT->hpos. */
6283
6284 static void
6285 back_to_previous_visible_line_start (struct it *it)
6286 {
6287 while (IT_CHARPOS (*it) > BEGV)
6288 {
6289 back_to_previous_line_start (it);
6290
6291 if (IT_CHARPOS (*it) <= BEGV)
6292 break;
6293
6294 /* If selective > 0, then lines indented more than its value are
6295 invisible. */
6296 if (it->selective > 0
6297 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6298 it->selective))
6299 continue;
6300
6301 /* Check the newline before point for invisibility. */
6302 {
6303 Lisp_Object prop;
6304 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6305 Qinvisible, it->window);
6306 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6307 continue;
6308 }
6309
6310 if (IT_CHARPOS (*it) <= BEGV)
6311 break;
6312
6313 {
6314 struct it it2;
6315 void *it2data = NULL;
6316 ptrdiff_t pos;
6317 ptrdiff_t beg, end;
6318 Lisp_Object val, overlay;
6319
6320 SAVE_IT (it2, *it, it2data);
6321
6322 /* If newline is part of a composition, continue from start of composition */
6323 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6324 && beg < IT_CHARPOS (*it))
6325 goto replaced;
6326
6327 /* If newline is replaced by a display property, find start of overlay
6328 or interval and continue search from that point. */
6329 pos = --IT_CHARPOS (it2);
6330 --IT_BYTEPOS (it2);
6331 it2.sp = 0;
6332 bidi_unshelve_cache (NULL, false);
6333 it2.string_from_display_prop_p = false;
6334 it2.from_disp_prop_p = false;
6335 if (handle_display_prop (&it2) == HANDLED_RETURN
6336 && !NILP (val = get_char_property_and_overlay
6337 (make_number (pos), Qdisplay, Qnil, &overlay))
6338 && (OVERLAYP (overlay)
6339 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6340 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6341 {
6342 RESTORE_IT (it, it, it2data);
6343 goto replaced;
6344 }
6345
6346 /* Newline is not replaced by anything -- so we are done. */
6347 RESTORE_IT (it, it, it2data);
6348 break;
6349
6350 replaced:
6351 if (beg < BEGV)
6352 beg = BEGV;
6353 IT_CHARPOS (*it) = beg;
6354 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6355 }
6356 }
6357
6358 it->continuation_lines_width = 0;
6359
6360 eassert (IT_CHARPOS (*it) >= BEGV);
6361 eassert (IT_CHARPOS (*it) == BEGV
6362 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6363 CHECK_IT (it);
6364 }
6365
6366
6367 /* Reseat iterator IT at the previous visible line start. Skip
6368 invisible text that is so either due to text properties or due to
6369 selective display. At the end, update IT's overlay information,
6370 face information etc. */
6371
6372 void
6373 reseat_at_previous_visible_line_start (struct it *it)
6374 {
6375 back_to_previous_visible_line_start (it);
6376 reseat (it, it->current.pos, true);
6377 CHECK_IT (it);
6378 }
6379
6380
6381 /* Reseat iterator IT on the next visible line start in the current
6382 buffer. ON_NEWLINE_P means position IT on the newline
6383 preceding the line start. Skip over invisible text that is so
6384 because of selective display. Compute faces, overlays etc at the
6385 new position. Note that this function does not skip over text that
6386 is invisible because of text properties. */
6387
6388 static void
6389 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6390 {
6391 bool skipped_p = false;
6392 struct bidi_it bidi_it_prev;
6393 bool newline_found_p
6394 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6395
6396 /* Skip over lines that are invisible because they are indented
6397 more than the value of IT->selective. */
6398 if (it->selective > 0)
6399 while (IT_CHARPOS (*it) < ZV
6400 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6401 it->selective))
6402 {
6403 eassert (IT_BYTEPOS (*it) == BEGV
6404 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6405 newline_found_p =
6406 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6407 }
6408
6409 /* Position on the newline if that's what's requested. */
6410 if (on_newline_p && newline_found_p)
6411 {
6412 if (STRINGP (it->string))
6413 {
6414 if (IT_STRING_CHARPOS (*it) > 0)
6415 {
6416 if (!it->bidi_p)
6417 {
6418 --IT_STRING_CHARPOS (*it);
6419 --IT_STRING_BYTEPOS (*it);
6420 }
6421 else
6422 {
6423 /* We need to restore the bidi iterator to the state
6424 it had on the newline, and resync the IT's
6425 position with that. */
6426 it->bidi_it = bidi_it_prev;
6427 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6428 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6429 }
6430 }
6431 }
6432 else if (IT_CHARPOS (*it) > BEGV)
6433 {
6434 if (!it->bidi_p)
6435 {
6436 --IT_CHARPOS (*it);
6437 --IT_BYTEPOS (*it);
6438 }
6439 else
6440 {
6441 /* We need to restore the bidi iterator to the state it
6442 had on the newline and resync IT with that. */
6443 it->bidi_it = bidi_it_prev;
6444 IT_CHARPOS (*it) = it->bidi_it.charpos;
6445 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6446 }
6447 reseat (it, it->current.pos, false);
6448 }
6449 }
6450 else if (skipped_p)
6451 reseat (it, it->current.pos, false);
6452
6453 CHECK_IT (it);
6454 }
6455
6456
6457 \f
6458 /***********************************************************************
6459 Changing an iterator's position
6460 ***********************************************************************/
6461
6462 /* Change IT's current position to POS in current_buffer.
6463 If FORCE_P, always check for text properties at the new position.
6464 Otherwise, text properties are only looked up if POS >=
6465 IT->check_charpos of a property. */
6466
6467 static void
6468 reseat (struct it *it, struct text_pos pos, bool force_p)
6469 {
6470 ptrdiff_t original_pos = IT_CHARPOS (*it);
6471
6472 reseat_1 (it, pos, false);
6473
6474 /* Determine where to check text properties. Avoid doing it
6475 where possible because text property lookup is very expensive. */
6476 if (force_p
6477 || CHARPOS (pos) > it->stop_charpos
6478 || CHARPOS (pos) < original_pos)
6479 {
6480 if (it->bidi_p)
6481 {
6482 /* For bidi iteration, we need to prime prev_stop and
6483 base_level_stop with our best estimations. */
6484 /* Implementation note: Of course, POS is not necessarily a
6485 stop position, so assigning prev_pos to it is a lie; we
6486 should have called compute_stop_backwards. However, if
6487 the current buffer does not include any R2L characters,
6488 that call would be a waste of cycles, because the
6489 iterator will never move back, and thus never cross this
6490 "fake" stop position. So we delay that backward search
6491 until the time we really need it, in next_element_from_buffer. */
6492 if (CHARPOS (pos) != it->prev_stop)
6493 it->prev_stop = CHARPOS (pos);
6494 if (CHARPOS (pos) < it->base_level_stop)
6495 it->base_level_stop = 0; /* meaning it's unknown */
6496 handle_stop (it);
6497 }
6498 else
6499 {
6500 handle_stop (it);
6501 it->prev_stop = it->base_level_stop = 0;
6502 }
6503
6504 }
6505
6506 CHECK_IT (it);
6507 }
6508
6509
6510 /* Change IT's buffer position to POS. SET_STOP_P means set
6511 IT->stop_pos to POS, also. */
6512
6513 static void
6514 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6515 {
6516 /* Don't call this function when scanning a C string. */
6517 eassert (it->s == NULL);
6518
6519 /* POS must be a reasonable value. */
6520 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6521
6522 it->current.pos = it->position = pos;
6523 it->end_charpos = ZV;
6524 it->dpvec = NULL;
6525 it->current.dpvec_index = -1;
6526 it->current.overlay_string_index = -1;
6527 IT_STRING_CHARPOS (*it) = -1;
6528 IT_STRING_BYTEPOS (*it) = -1;
6529 it->string = Qnil;
6530 it->method = GET_FROM_BUFFER;
6531 it->object = it->w->contents;
6532 it->area = TEXT_AREA;
6533 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6534 it->sp = 0;
6535 it->string_from_display_prop_p = false;
6536 it->string_from_prefix_prop_p = false;
6537
6538 it->from_disp_prop_p = false;
6539 it->face_before_selective_p = false;
6540 if (it->bidi_p)
6541 {
6542 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6543 &it->bidi_it);
6544 bidi_unshelve_cache (NULL, false);
6545 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6546 it->bidi_it.string.s = NULL;
6547 it->bidi_it.string.lstring = Qnil;
6548 it->bidi_it.string.bufpos = 0;
6549 it->bidi_it.string.from_disp_str = false;
6550 it->bidi_it.string.unibyte = false;
6551 it->bidi_it.w = it->w;
6552 }
6553
6554 if (set_stop_p)
6555 {
6556 it->stop_charpos = CHARPOS (pos);
6557 it->base_level_stop = CHARPOS (pos);
6558 }
6559 /* This make the information stored in it->cmp_it invalidate. */
6560 it->cmp_it.id = -1;
6561 }
6562
6563
6564 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6565 If S is non-null, it is a C string to iterate over. Otherwise,
6566 STRING gives a Lisp string to iterate over.
6567
6568 If PRECISION > 0, don't return more then PRECISION number of
6569 characters from the string.
6570
6571 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6572 characters have been returned. FIELD_WIDTH < 0 means an infinite
6573 field width.
6574
6575 MULTIBYTE = 0 means disable processing of multibyte characters,
6576 MULTIBYTE > 0 means enable it,
6577 MULTIBYTE < 0 means use IT->multibyte_p.
6578
6579 IT must be initialized via a prior call to init_iterator before
6580 calling this function. */
6581
6582 static void
6583 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6584 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6585 int multibyte)
6586 {
6587 /* No text property checks performed by default, but see below. */
6588 it->stop_charpos = -1;
6589
6590 /* Set iterator position and end position. */
6591 memset (&it->current, 0, sizeof it->current);
6592 it->current.overlay_string_index = -1;
6593 it->current.dpvec_index = -1;
6594 eassert (charpos >= 0);
6595
6596 /* If STRING is specified, use its multibyteness, otherwise use the
6597 setting of MULTIBYTE, if specified. */
6598 if (multibyte >= 0)
6599 it->multibyte_p = multibyte > 0;
6600
6601 /* Bidirectional reordering of strings is controlled by the default
6602 value of bidi-display-reordering. Don't try to reorder while
6603 loading loadup.el, as the necessary character property tables are
6604 not yet available. */
6605 it->bidi_p =
6606 NILP (Vpurify_flag)
6607 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6608
6609 if (s == NULL)
6610 {
6611 eassert (STRINGP (string));
6612 it->string = string;
6613 it->s = NULL;
6614 it->end_charpos = it->string_nchars = SCHARS (string);
6615 it->method = GET_FROM_STRING;
6616 it->current.string_pos = string_pos (charpos, string);
6617
6618 if (it->bidi_p)
6619 {
6620 it->bidi_it.string.lstring = string;
6621 it->bidi_it.string.s = NULL;
6622 it->bidi_it.string.schars = it->end_charpos;
6623 it->bidi_it.string.bufpos = 0;
6624 it->bidi_it.string.from_disp_str = false;
6625 it->bidi_it.string.unibyte = !it->multibyte_p;
6626 it->bidi_it.w = it->w;
6627 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6628 FRAME_WINDOW_P (it->f), &it->bidi_it);
6629 }
6630 }
6631 else
6632 {
6633 it->s = (const unsigned char *) s;
6634 it->string = Qnil;
6635
6636 /* Note that we use IT->current.pos, not it->current.string_pos,
6637 for displaying C strings. */
6638 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6639 if (it->multibyte_p)
6640 {
6641 it->current.pos = c_string_pos (charpos, s, true);
6642 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6643 }
6644 else
6645 {
6646 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6647 it->end_charpos = it->string_nchars = strlen (s);
6648 }
6649
6650 if (it->bidi_p)
6651 {
6652 it->bidi_it.string.lstring = Qnil;
6653 it->bidi_it.string.s = (const unsigned char *) s;
6654 it->bidi_it.string.schars = it->end_charpos;
6655 it->bidi_it.string.bufpos = 0;
6656 it->bidi_it.string.from_disp_str = false;
6657 it->bidi_it.string.unibyte = !it->multibyte_p;
6658 it->bidi_it.w = it->w;
6659 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6660 &it->bidi_it);
6661 }
6662 it->method = GET_FROM_C_STRING;
6663 }
6664
6665 /* PRECISION > 0 means don't return more than PRECISION characters
6666 from the string. */
6667 if (precision > 0 && it->end_charpos - charpos > precision)
6668 {
6669 it->end_charpos = it->string_nchars = charpos + precision;
6670 if (it->bidi_p)
6671 it->bidi_it.string.schars = it->end_charpos;
6672 }
6673
6674 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6675 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6676 FIELD_WIDTH < 0 means infinite field width. This is useful for
6677 padding with `-' at the end of a mode line. */
6678 if (field_width < 0)
6679 field_width = INFINITY;
6680 /* Implementation note: We deliberately don't enlarge
6681 it->bidi_it.string.schars here to fit it->end_charpos, because
6682 the bidi iterator cannot produce characters out of thin air. */
6683 if (field_width > it->end_charpos - charpos)
6684 it->end_charpos = charpos + field_width;
6685
6686 /* Use the standard display table for displaying strings. */
6687 if (DISP_TABLE_P (Vstandard_display_table))
6688 it->dp = XCHAR_TABLE (Vstandard_display_table);
6689
6690 it->stop_charpos = charpos;
6691 it->prev_stop = charpos;
6692 it->base_level_stop = 0;
6693 if (it->bidi_p)
6694 {
6695 it->bidi_it.first_elt = true;
6696 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6697 it->bidi_it.disp_pos = -1;
6698 }
6699 if (s == NULL && it->multibyte_p)
6700 {
6701 ptrdiff_t endpos = SCHARS (it->string);
6702 if (endpos > it->end_charpos)
6703 endpos = it->end_charpos;
6704 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6705 it->string);
6706 }
6707 CHECK_IT (it);
6708 }
6709
6710
6711 \f
6712 /***********************************************************************
6713 Iteration
6714 ***********************************************************************/
6715
6716 /* Map enum it_method value to corresponding next_element_from_* function. */
6717
6718 typedef bool (*next_element_function) (struct it *);
6719
6720 static next_element_function const get_next_element[NUM_IT_METHODS] =
6721 {
6722 next_element_from_buffer,
6723 next_element_from_display_vector,
6724 next_element_from_string,
6725 next_element_from_c_string,
6726 next_element_from_image,
6727 next_element_from_stretch
6728 };
6729
6730 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6731
6732
6733 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6734 (possibly with the following characters). */
6735
6736 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6737 ((IT)->cmp_it.id >= 0 \
6738 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6739 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6740 END_CHARPOS, (IT)->w, \
6741 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6742 (IT)->string)))
6743
6744
6745 /* Lookup the char-table Vglyphless_char_display for character C (-1
6746 if we want information for no-font case), and return the display
6747 method symbol. By side-effect, update it->what and
6748 it->glyphless_method. This function is called from
6749 get_next_display_element for each character element, and from
6750 x_produce_glyphs when no suitable font was found. */
6751
6752 Lisp_Object
6753 lookup_glyphless_char_display (int c, struct it *it)
6754 {
6755 Lisp_Object glyphless_method = Qnil;
6756
6757 if (CHAR_TABLE_P (Vglyphless_char_display)
6758 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6759 {
6760 if (c >= 0)
6761 {
6762 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6763 if (CONSP (glyphless_method))
6764 glyphless_method = FRAME_WINDOW_P (it->f)
6765 ? XCAR (glyphless_method)
6766 : XCDR (glyphless_method);
6767 }
6768 else
6769 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6770 }
6771
6772 retry:
6773 if (NILP (glyphless_method))
6774 {
6775 if (c >= 0)
6776 /* The default is to display the character by a proper font. */
6777 return Qnil;
6778 /* The default for the no-font case is to display an empty box. */
6779 glyphless_method = Qempty_box;
6780 }
6781 if (EQ (glyphless_method, Qzero_width))
6782 {
6783 if (c >= 0)
6784 return glyphless_method;
6785 /* This method can't be used for the no-font case. */
6786 glyphless_method = Qempty_box;
6787 }
6788 if (EQ (glyphless_method, Qthin_space))
6789 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6790 else if (EQ (glyphless_method, Qempty_box))
6791 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6792 else if (EQ (glyphless_method, Qhex_code))
6793 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6794 else if (STRINGP (glyphless_method))
6795 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6796 else
6797 {
6798 /* Invalid value. We use the default method. */
6799 glyphless_method = Qnil;
6800 goto retry;
6801 }
6802 it->what = IT_GLYPHLESS;
6803 return glyphless_method;
6804 }
6805
6806 /* Merge escape glyph face and cache the result. */
6807
6808 static struct frame *last_escape_glyph_frame = NULL;
6809 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6810 static int last_escape_glyph_merged_face_id = 0;
6811
6812 static int
6813 merge_escape_glyph_face (struct it *it)
6814 {
6815 int face_id;
6816
6817 if (it->f == last_escape_glyph_frame
6818 && it->face_id == last_escape_glyph_face_id)
6819 face_id = last_escape_glyph_merged_face_id;
6820 else
6821 {
6822 /* Merge the `escape-glyph' face into the current face. */
6823 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6824 last_escape_glyph_frame = it->f;
6825 last_escape_glyph_face_id = it->face_id;
6826 last_escape_glyph_merged_face_id = face_id;
6827 }
6828 return face_id;
6829 }
6830
6831 /* Likewise for glyphless glyph face. */
6832
6833 static struct frame *last_glyphless_glyph_frame = NULL;
6834 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6835 static int last_glyphless_glyph_merged_face_id = 0;
6836
6837 int
6838 merge_glyphless_glyph_face (struct it *it)
6839 {
6840 int face_id;
6841
6842 if (it->f == last_glyphless_glyph_frame
6843 && it->face_id == last_glyphless_glyph_face_id)
6844 face_id = last_glyphless_glyph_merged_face_id;
6845 else
6846 {
6847 /* Merge the `glyphless-char' face into the current face. */
6848 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6849 last_glyphless_glyph_frame = it->f;
6850 last_glyphless_glyph_face_id = it->face_id;
6851 last_glyphless_glyph_merged_face_id = face_id;
6852 }
6853 return face_id;
6854 }
6855
6856 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6857 be called before redisplaying windows, and when the frame's face
6858 cache is freed. */
6859 void
6860 forget_escape_and_glyphless_faces (void)
6861 {
6862 last_escape_glyph_frame = NULL;
6863 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6864 last_glyphless_glyph_frame = NULL;
6865 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6866 }
6867
6868 /* Load IT's display element fields with information about the next
6869 display element from the current position of IT. Value is false if
6870 end of buffer (or C string) is reached. */
6871
6872 static bool
6873 get_next_display_element (struct it *it)
6874 {
6875 /* True means that we found a display element. False means that
6876 we hit the end of what we iterate over. Performance note: the
6877 function pointer `method' used here turns out to be faster than
6878 using a sequence of if-statements. */
6879 bool success_p;
6880
6881 get_next:
6882 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6883
6884 if (it->what == IT_CHARACTER)
6885 {
6886 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6887 and only if (a) the resolved directionality of that character
6888 is R..." */
6889 /* FIXME: Do we need an exception for characters from display
6890 tables? */
6891 if (it->bidi_p && it->bidi_it.type == STRONG_R
6892 && !inhibit_bidi_mirroring)
6893 it->c = bidi_mirror_char (it->c);
6894 /* Map via display table or translate control characters.
6895 IT->c, IT->len etc. have been set to the next character by
6896 the function call above. If we have a display table, and it
6897 contains an entry for IT->c, translate it. Don't do this if
6898 IT->c itself comes from a display table, otherwise we could
6899 end up in an infinite recursion. (An alternative could be to
6900 count the recursion depth of this function and signal an
6901 error when a certain maximum depth is reached.) Is it worth
6902 it? */
6903 if (success_p && it->dpvec == NULL)
6904 {
6905 Lisp_Object dv;
6906 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6907 bool nonascii_space_p = false;
6908 bool nonascii_hyphen_p = false;
6909 int c = it->c; /* This is the character to display. */
6910
6911 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6912 {
6913 eassert (SINGLE_BYTE_CHAR_P (c));
6914 if (unibyte_display_via_language_environment)
6915 {
6916 c = DECODE_CHAR (unibyte, c);
6917 if (c < 0)
6918 c = BYTE8_TO_CHAR (it->c);
6919 }
6920 else
6921 c = BYTE8_TO_CHAR (it->c);
6922 }
6923
6924 if (it->dp
6925 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6926 VECTORP (dv)))
6927 {
6928 struct Lisp_Vector *v = XVECTOR (dv);
6929
6930 /* Return the first character from the display table
6931 entry, if not empty. If empty, don't display the
6932 current character. */
6933 if (v->header.size)
6934 {
6935 it->dpvec_char_len = it->len;
6936 it->dpvec = v->contents;
6937 it->dpend = v->contents + v->header.size;
6938 it->current.dpvec_index = 0;
6939 it->dpvec_face_id = -1;
6940 it->saved_face_id = it->face_id;
6941 it->method = GET_FROM_DISPLAY_VECTOR;
6942 it->ellipsis_p = false;
6943 }
6944 else
6945 {
6946 set_iterator_to_next (it, false);
6947 }
6948 goto get_next;
6949 }
6950
6951 if (! NILP (lookup_glyphless_char_display (c, it)))
6952 {
6953 if (it->what == IT_GLYPHLESS)
6954 goto done;
6955 /* Don't display this character. */
6956 set_iterator_to_next (it, false);
6957 goto get_next;
6958 }
6959
6960 /* If `nobreak-char-display' is non-nil, we display
6961 non-ASCII spaces and hyphens specially. */
6962 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6963 {
6964 if (c == NO_BREAK_SPACE)
6965 nonascii_space_p = true;
6966 else if (c == SOFT_HYPHEN || c == HYPHEN
6967 || c == NON_BREAKING_HYPHEN)
6968 nonascii_hyphen_p = true;
6969 }
6970
6971 /* Translate control characters into `\003' or `^C' form.
6972 Control characters coming from a display table entry are
6973 currently not translated because we use IT->dpvec to hold
6974 the translation. This could easily be changed but I
6975 don't believe that it is worth doing.
6976
6977 The characters handled by `nobreak-char-display' must be
6978 translated too.
6979
6980 Non-printable characters and raw-byte characters are also
6981 translated to octal form. */
6982 if (((c < ' ' || c == 127) /* ASCII control chars. */
6983 ? (it->area != TEXT_AREA
6984 /* In mode line, treat \n, \t like other crl chars. */
6985 || (c != '\t'
6986 && it->glyph_row
6987 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6988 || (c != '\n' && c != '\t'))
6989 : (nonascii_space_p
6990 || nonascii_hyphen_p
6991 || CHAR_BYTE8_P (c)
6992 || ! CHAR_PRINTABLE_P (c))))
6993 {
6994 /* C is a control character, non-ASCII space/hyphen,
6995 raw-byte, or a non-printable character which must be
6996 displayed either as '\003' or as `^C' where the '\\'
6997 and '^' can be defined in the display table. Fill
6998 IT->ctl_chars with glyphs for what we have to
6999 display. Then, set IT->dpvec to these glyphs. */
7000 Lisp_Object gc;
7001 int ctl_len;
7002 int face_id;
7003 int lface_id = 0;
7004 int escape_glyph;
7005
7006 /* Handle control characters with ^. */
7007
7008 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7009 {
7010 int g;
7011
7012 g = '^'; /* default glyph for Control */
7013 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7014 if (it->dp
7015 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7016 {
7017 g = GLYPH_CODE_CHAR (gc);
7018 lface_id = GLYPH_CODE_FACE (gc);
7019 }
7020
7021 face_id = (lface_id
7022 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7023 : merge_escape_glyph_face (it));
7024
7025 XSETINT (it->ctl_chars[0], g);
7026 XSETINT (it->ctl_chars[1], c ^ 0100);
7027 ctl_len = 2;
7028 goto display_control;
7029 }
7030
7031 /* Handle non-ascii space in the mode where it only gets
7032 highlighting. */
7033
7034 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7035 {
7036 /* Merge `nobreak-space' into the current face. */
7037 face_id = merge_faces (it->f, Qnobreak_space, 0,
7038 it->face_id);
7039 XSETINT (it->ctl_chars[0], ' ');
7040 ctl_len = 1;
7041 goto display_control;
7042 }
7043
7044 /* Handle sequences that start with the "escape glyph". */
7045
7046 /* the default escape glyph is \. */
7047 escape_glyph = '\\';
7048
7049 if (it->dp
7050 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7051 {
7052 escape_glyph = GLYPH_CODE_CHAR (gc);
7053 lface_id = GLYPH_CODE_FACE (gc);
7054 }
7055
7056 face_id = (lface_id
7057 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7058 : merge_escape_glyph_face (it));
7059
7060 /* Draw non-ASCII hyphen with just highlighting: */
7061
7062 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7063 {
7064 XSETINT (it->ctl_chars[0], '-');
7065 ctl_len = 1;
7066 goto display_control;
7067 }
7068
7069 /* Draw non-ASCII space/hyphen with escape glyph: */
7070
7071 if (nonascii_space_p || nonascii_hyphen_p)
7072 {
7073 XSETINT (it->ctl_chars[0], escape_glyph);
7074 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7075 ctl_len = 2;
7076 goto display_control;
7077 }
7078
7079 {
7080 char str[10];
7081 int len, i;
7082
7083 if (CHAR_BYTE8_P (c))
7084 /* Display \200 instead of \17777600. */
7085 c = CHAR_TO_BYTE8 (c);
7086 len = sprintf (str, "%03o", c + 0u);
7087
7088 XSETINT (it->ctl_chars[0], escape_glyph);
7089 for (i = 0; i < len; i++)
7090 XSETINT (it->ctl_chars[i + 1], str[i]);
7091 ctl_len = len + 1;
7092 }
7093
7094 display_control:
7095 /* Set up IT->dpvec and return first character from it. */
7096 it->dpvec_char_len = it->len;
7097 it->dpvec = it->ctl_chars;
7098 it->dpend = it->dpvec + ctl_len;
7099 it->current.dpvec_index = 0;
7100 it->dpvec_face_id = face_id;
7101 it->saved_face_id = it->face_id;
7102 it->method = GET_FROM_DISPLAY_VECTOR;
7103 it->ellipsis_p = false;
7104 goto get_next;
7105 }
7106 it->char_to_display = c;
7107 }
7108 else if (success_p)
7109 {
7110 it->char_to_display = it->c;
7111 }
7112 }
7113
7114 #ifdef HAVE_WINDOW_SYSTEM
7115 /* Adjust face id for a multibyte character. There are no multibyte
7116 character in unibyte text. */
7117 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7118 && it->multibyte_p
7119 && success_p
7120 && FRAME_WINDOW_P (it->f))
7121 {
7122 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7123
7124 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7125 {
7126 /* Automatic composition with glyph-string. */
7127 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7128
7129 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7130 }
7131 else
7132 {
7133 ptrdiff_t pos = (it->s ? -1
7134 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7135 : IT_CHARPOS (*it));
7136 int c;
7137
7138 if (it->what == IT_CHARACTER)
7139 c = it->char_to_display;
7140 else
7141 {
7142 struct composition *cmp = composition_table[it->cmp_it.id];
7143 int i;
7144
7145 c = ' ';
7146 for (i = 0; i < cmp->glyph_len; i++)
7147 /* TAB in a composition means display glyphs with
7148 padding space on the left or right. */
7149 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7150 break;
7151 }
7152 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7153 }
7154 }
7155 #endif /* HAVE_WINDOW_SYSTEM */
7156
7157 done:
7158 /* Is this character the last one of a run of characters with
7159 box? If yes, set IT->end_of_box_run_p to true. */
7160 if (it->face_box_p
7161 && it->s == NULL)
7162 {
7163 if (it->method == GET_FROM_STRING && it->sp)
7164 {
7165 int face_id = underlying_face_id (it);
7166 struct face *face = FACE_FROM_ID (it->f, face_id);
7167
7168 if (face)
7169 {
7170 if (face->box == FACE_NO_BOX)
7171 {
7172 /* If the box comes from face properties in a
7173 display string, check faces in that string. */
7174 int string_face_id = face_after_it_pos (it);
7175 it->end_of_box_run_p
7176 = (FACE_FROM_ID (it->f, string_face_id)->box
7177 == FACE_NO_BOX);
7178 }
7179 /* Otherwise, the box comes from the underlying face.
7180 If this is the last string character displayed, check
7181 the next buffer location. */
7182 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7183 /* n_overlay_strings is unreliable unless
7184 overlay_string_index is non-negative. */
7185 && ((it->current.overlay_string_index >= 0
7186 && (it->current.overlay_string_index
7187 == it->n_overlay_strings - 1))
7188 /* A string from display property. */
7189 || it->from_disp_prop_p))
7190 {
7191 ptrdiff_t ignore;
7192 int next_face_id;
7193 struct text_pos pos = it->current.pos;
7194
7195 /* For a string from a display property, the next
7196 buffer position is stored in the 'position'
7197 member of the iteration stack slot below the
7198 current one, see handle_single_display_spec. By
7199 contrast, it->current.pos was is not yet updated
7200 to point to that buffer position; that will
7201 happen in pop_it, after we finish displaying the
7202 current string. Note that we already checked
7203 above that it->sp is positive, so subtracting one
7204 from it is safe. */
7205 if (it->from_disp_prop_p)
7206 pos = (it->stack + it->sp - 1)->position;
7207 else
7208 INC_TEXT_POS (pos, it->multibyte_p);
7209
7210 if (CHARPOS (pos) >= ZV)
7211 it->end_of_box_run_p = true;
7212 else
7213 {
7214 next_face_id = face_at_buffer_position
7215 (it->w, CHARPOS (pos), &ignore,
7216 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7217 it->end_of_box_run_p
7218 = (FACE_FROM_ID (it->f, next_face_id)->box
7219 == FACE_NO_BOX);
7220 }
7221 }
7222 }
7223 }
7224 /* next_element_from_display_vector sets this flag according to
7225 faces of the display vector glyphs, see there. */
7226 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7227 {
7228 int face_id = face_after_it_pos (it);
7229 it->end_of_box_run_p
7230 = (face_id != it->face_id
7231 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7232 }
7233 }
7234 /* If we reached the end of the object we've been iterating (e.g., a
7235 display string or an overlay string), and there's something on
7236 IT->stack, proceed with what's on the stack. It doesn't make
7237 sense to return false if there's unprocessed stuff on the stack,
7238 because otherwise that stuff will never be displayed. */
7239 if (!success_p && it->sp > 0)
7240 {
7241 set_iterator_to_next (it, false);
7242 success_p = get_next_display_element (it);
7243 }
7244
7245 /* Value is false if end of buffer or string reached. */
7246 return success_p;
7247 }
7248
7249
7250 /* Move IT to the next display element.
7251
7252 RESEAT_P means if called on a newline in buffer text,
7253 skip to the next visible line start.
7254
7255 Functions get_next_display_element and set_iterator_to_next are
7256 separate because I find this arrangement easier to handle than a
7257 get_next_display_element function that also increments IT's
7258 position. The way it is we can first look at an iterator's current
7259 display element, decide whether it fits on a line, and if it does,
7260 increment the iterator position. The other way around we probably
7261 would either need a flag indicating whether the iterator has to be
7262 incremented the next time, or we would have to implement a
7263 decrement position function which would not be easy to write. */
7264
7265 void
7266 set_iterator_to_next (struct it *it, bool reseat_p)
7267 {
7268 /* Reset flags indicating start and end of a sequence of characters
7269 with box. Reset them at the start of this function because
7270 moving the iterator to a new position might set them. */
7271 it->start_of_box_run_p = it->end_of_box_run_p = false;
7272
7273 switch (it->method)
7274 {
7275 case GET_FROM_BUFFER:
7276 /* The current display element of IT is a character from
7277 current_buffer. Advance in the buffer, and maybe skip over
7278 invisible lines that are so because of selective display. */
7279 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7280 reseat_at_next_visible_line_start (it, false);
7281 else if (it->cmp_it.id >= 0)
7282 {
7283 /* We are currently getting glyphs from a composition. */
7284 if (! it->bidi_p)
7285 {
7286 IT_CHARPOS (*it) += it->cmp_it.nchars;
7287 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7288 }
7289 else
7290 {
7291 int i;
7292
7293 /* Update IT's char/byte positions to point to the first
7294 character of the next grapheme cluster, or to the
7295 character visually after the current composition. */
7296 for (i = 0; i < it->cmp_it.nchars; i++)
7297 bidi_move_to_visually_next (&it->bidi_it);
7298 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7299 IT_CHARPOS (*it) = it->bidi_it.charpos;
7300 }
7301
7302 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7303 && it->cmp_it.to < it->cmp_it.nglyphs)
7304 {
7305 /* Composition created while scanning forward. Proceed
7306 to the next grapheme cluster. */
7307 it->cmp_it.from = it->cmp_it.to;
7308 }
7309 else if ((it->bidi_p && it->cmp_it.reversed_p)
7310 && it->cmp_it.from > 0)
7311 {
7312 /* Composition created while scanning backward. Proceed
7313 to the previous grapheme cluster. */
7314 it->cmp_it.to = it->cmp_it.from;
7315 }
7316 else
7317 {
7318 /* No more grapheme clusters in this composition.
7319 Find the next stop position. */
7320 ptrdiff_t stop = it->end_charpos;
7321
7322 if (it->bidi_it.scan_dir < 0)
7323 /* Now we are scanning backward and don't know
7324 where to stop. */
7325 stop = -1;
7326 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7327 IT_BYTEPOS (*it), stop, Qnil);
7328 }
7329 }
7330 else
7331 {
7332 eassert (it->len != 0);
7333
7334 if (!it->bidi_p)
7335 {
7336 IT_BYTEPOS (*it) += it->len;
7337 IT_CHARPOS (*it) += 1;
7338 }
7339 else
7340 {
7341 int prev_scan_dir = it->bidi_it.scan_dir;
7342 /* If this is a new paragraph, determine its base
7343 direction (a.k.a. its base embedding level). */
7344 if (it->bidi_it.new_paragraph)
7345 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7346 false);
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (prev_scan_dir != it->bidi_it.scan_dir)
7351 {
7352 /* As the scan direction was changed, we must
7353 re-compute the stop position for composition. */
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7358 IT_BYTEPOS (*it), stop, Qnil);
7359 }
7360 }
7361 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7362 }
7363 break;
7364
7365 case GET_FROM_C_STRING:
7366 /* Current display element of IT is from a C string. */
7367 if (!it->bidi_p
7368 /* If the string position is beyond string's end, it means
7369 next_element_from_c_string is padding the string with
7370 blanks, in which case we bypass the bidi iterator,
7371 because it cannot deal with such virtual characters. */
7372 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 {
7374 IT_BYTEPOS (*it) += it->len;
7375 IT_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 bidi_move_to_visually_next (&it->bidi_it);
7380 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7381 IT_CHARPOS (*it) = it->bidi_it.charpos;
7382 }
7383 break;
7384
7385 case GET_FROM_DISPLAY_VECTOR:
7386 /* Current display element of IT is from a display table entry.
7387 Advance in the display table definition. Reset it to null if
7388 end reached, and continue with characters from buffers/
7389 strings. */
7390 ++it->current.dpvec_index;
7391
7392 /* Restore face of the iterator to what they were before the
7393 display vector entry (these entries may contain faces). */
7394 it->face_id = it->saved_face_id;
7395
7396 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7397 {
7398 bool recheck_faces = it->ellipsis_p;
7399
7400 if (it->s)
7401 it->method = GET_FROM_C_STRING;
7402 else if (STRINGP (it->string))
7403 it->method = GET_FROM_STRING;
7404 else
7405 {
7406 it->method = GET_FROM_BUFFER;
7407 it->object = it->w->contents;
7408 }
7409
7410 it->dpvec = NULL;
7411 it->current.dpvec_index = -1;
7412
7413 /* Skip over characters which were displayed via IT->dpvec. */
7414 if (it->dpvec_char_len < 0)
7415 reseat_at_next_visible_line_start (it, true);
7416 else if (it->dpvec_char_len > 0)
7417 {
7418 it->len = it->dpvec_char_len;
7419 set_iterator_to_next (it, reseat_p);
7420 }
7421
7422 /* Maybe recheck faces after display vector. */
7423 if (recheck_faces)
7424 {
7425 if (it->method == GET_FROM_STRING)
7426 it->stop_charpos = IT_STRING_CHARPOS (*it);
7427 else
7428 it->stop_charpos = IT_CHARPOS (*it);
7429 }
7430 }
7431 break;
7432
7433 case GET_FROM_STRING:
7434 /* Current display element is a character from a Lisp string. */
7435 eassert (it->s == NULL && STRINGP (it->string));
7436 /* Don't advance past string end. These conditions are true
7437 when set_iterator_to_next is called at the end of
7438 get_next_display_element, in which case the Lisp string is
7439 already exhausted, and all we want is pop the iterator
7440 stack. */
7441 if (it->current.overlay_string_index >= 0)
7442 {
7443 /* This is an overlay string, so there's no padding with
7444 spaces, and the number of characters in the string is
7445 where the string ends. */
7446 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7447 goto consider_string_end;
7448 }
7449 else
7450 {
7451 /* Not an overlay string. There could be padding, so test
7452 against it->end_charpos. */
7453 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7454 goto consider_string_end;
7455 }
7456 if (it->cmp_it.id >= 0)
7457 {
7458 /* We are delivering display elements from a composition.
7459 Update the string position past the grapheme cluster
7460 we've just processed. */
7461 if (! it->bidi_p)
7462 {
7463 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7464 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7465 }
7466 else
7467 {
7468 int i;
7469
7470 for (i = 0; i < it->cmp_it.nchars; i++)
7471 bidi_move_to_visually_next (&it->bidi_it);
7472 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7473 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7474 }
7475
7476 /* Did we exhaust all the grapheme clusters of this
7477 composition? */
7478 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7479 && (it->cmp_it.to < it->cmp_it.nglyphs))
7480 {
7481 /* Not all the grapheme clusters were processed yet;
7482 advance to the next cluster. */
7483 it->cmp_it.from = it->cmp_it.to;
7484 }
7485 else if ((it->bidi_p && it->cmp_it.reversed_p)
7486 && it->cmp_it.from > 0)
7487 {
7488 /* Likewise: advance to the next cluster, but going in
7489 the reverse direction. */
7490 it->cmp_it.to = it->cmp_it.from;
7491 }
7492 else
7493 {
7494 /* This composition was fully processed; find the next
7495 candidate place for checking for composed
7496 characters. */
7497 /* Always limit string searches to the string length;
7498 any padding spaces are not part of the string, and
7499 there cannot be any compositions in that padding. */
7500 ptrdiff_t stop = SCHARS (it->string);
7501
7502 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7503 stop = -1;
7504 else if (it->end_charpos < stop)
7505 {
7506 /* Cf. PRECISION in reseat_to_string: we might be
7507 limited in how many of the string characters we
7508 need to deliver. */
7509 stop = it->end_charpos;
7510 }
7511 composition_compute_stop_pos (&it->cmp_it,
7512 IT_STRING_CHARPOS (*it),
7513 IT_STRING_BYTEPOS (*it), stop,
7514 it->string);
7515 }
7516 }
7517 else
7518 {
7519 if (!it->bidi_p
7520 /* If the string position is beyond string's end, it
7521 means next_element_from_string is padding the string
7522 with blanks, in which case we bypass the bidi
7523 iterator, because it cannot deal with such virtual
7524 characters. */
7525 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7526 {
7527 IT_STRING_BYTEPOS (*it) += it->len;
7528 IT_STRING_CHARPOS (*it) += 1;
7529 }
7530 else
7531 {
7532 int prev_scan_dir = it->bidi_it.scan_dir;
7533
7534 bidi_move_to_visually_next (&it->bidi_it);
7535 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7536 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7537 /* If the scan direction changes, we may need to update
7538 the place where to check for composed characters. */
7539 if (prev_scan_dir != it->bidi_it.scan_dir)
7540 {
7541 ptrdiff_t stop = SCHARS (it->string);
7542
7543 if (it->bidi_it.scan_dir < 0)
7544 stop = -1;
7545 else if (it->end_charpos < stop)
7546 stop = it->end_charpos;
7547
7548 composition_compute_stop_pos (&it->cmp_it,
7549 IT_STRING_CHARPOS (*it),
7550 IT_STRING_BYTEPOS (*it), stop,
7551 it->string);
7552 }
7553 }
7554 }
7555
7556 consider_string_end:
7557
7558 if (it->current.overlay_string_index >= 0)
7559 {
7560 /* IT->string is an overlay string. Advance to the
7561 next, if there is one. */
7562 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7563 {
7564 it->ellipsis_p = false;
7565 next_overlay_string (it);
7566 if (it->ellipsis_p)
7567 setup_for_ellipsis (it, 0);
7568 }
7569 }
7570 else
7571 {
7572 /* IT->string is not an overlay string. If we reached
7573 its end, and there is something on IT->stack, proceed
7574 with what is on the stack. This can be either another
7575 string, this time an overlay string, or a buffer. */
7576 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7577 && it->sp > 0)
7578 {
7579 pop_it (it);
7580 if (it->method == GET_FROM_STRING)
7581 goto consider_string_end;
7582 }
7583 }
7584 break;
7585
7586 case GET_FROM_IMAGE:
7587 case GET_FROM_STRETCH:
7588 /* The position etc with which we have to proceed are on
7589 the stack. The position may be at the end of a string,
7590 if the `display' property takes up the whole string. */
7591 eassert (it->sp > 0);
7592 pop_it (it);
7593 if (it->method == GET_FROM_STRING)
7594 goto consider_string_end;
7595 break;
7596
7597 default:
7598 /* There are no other methods defined, so this should be a bug. */
7599 emacs_abort ();
7600 }
7601
7602 eassert (it->method != GET_FROM_STRING
7603 || (STRINGP (it->string)
7604 && IT_STRING_CHARPOS (*it) >= 0));
7605 }
7606
7607 /* Load IT's display element fields with information about the next
7608 display element which comes from a display table entry or from the
7609 result of translating a control character to one of the forms `^C'
7610 or `\003'.
7611
7612 IT->dpvec holds the glyphs to return as characters.
7613 IT->saved_face_id holds the face id before the display vector--it
7614 is restored into IT->face_id in set_iterator_to_next. */
7615
7616 static bool
7617 next_element_from_display_vector (struct it *it)
7618 {
7619 Lisp_Object gc;
7620 int prev_face_id = it->face_id;
7621 int next_face_id;
7622
7623 /* Precondition. */
7624 eassert (it->dpvec && it->current.dpvec_index >= 0);
7625
7626 it->face_id = it->saved_face_id;
7627
7628 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7629 That seemed totally bogus - so I changed it... */
7630 gc = it->dpvec[it->current.dpvec_index];
7631
7632 if (GLYPH_CODE_P (gc))
7633 {
7634 struct face *this_face, *prev_face, *next_face;
7635
7636 it->c = GLYPH_CODE_CHAR (gc);
7637 it->len = CHAR_BYTES (it->c);
7638
7639 /* The entry may contain a face id to use. Such a face id is
7640 the id of a Lisp face, not a realized face. A face id of
7641 zero means no face is specified. */
7642 if (it->dpvec_face_id >= 0)
7643 it->face_id = it->dpvec_face_id;
7644 else
7645 {
7646 int lface_id = GLYPH_CODE_FACE (gc);
7647 if (lface_id > 0)
7648 it->face_id = merge_faces (it->f, Qt, lface_id,
7649 it->saved_face_id);
7650 }
7651
7652 /* Glyphs in the display vector could have the box face, so we
7653 need to set the related flags in the iterator, as
7654 appropriate. */
7655 this_face = FACE_FROM_ID (it->f, it->face_id);
7656 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7657
7658 /* Is this character the first character of a box-face run? */
7659 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7660 && (!prev_face
7661 || prev_face->box == FACE_NO_BOX));
7662
7663 /* For the last character of the box-face run, we need to look
7664 either at the next glyph from the display vector, or at the
7665 face we saw before the display vector. */
7666 next_face_id = it->saved_face_id;
7667 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7668 {
7669 if (it->dpvec_face_id >= 0)
7670 next_face_id = it->dpvec_face_id;
7671 else
7672 {
7673 int lface_id =
7674 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7675
7676 if (lface_id > 0)
7677 next_face_id = merge_faces (it->f, Qt, lface_id,
7678 it->saved_face_id);
7679 }
7680 }
7681 next_face = FACE_FROM_ID (it->f, next_face_id);
7682 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7683 && (!next_face
7684 || next_face->box == FACE_NO_BOX));
7685 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7686 }
7687 else
7688 /* Display table entry is invalid. Return a space. */
7689 it->c = ' ', it->len = 1;
7690
7691 /* Don't change position and object of the iterator here. They are
7692 still the values of the character that had this display table
7693 entry or was translated, and that's what we want. */
7694 it->what = IT_CHARACTER;
7695 return true;
7696 }
7697
7698 /* Get the first element of string/buffer in the visual order, after
7699 being reseated to a new position in a string or a buffer. */
7700 static void
7701 get_visually_first_element (struct it *it)
7702 {
7703 bool string_p = STRINGP (it->string) || it->s;
7704 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7705 ptrdiff_t bob = (string_p ? 0 : BEGV);
7706
7707 if (STRINGP (it->string))
7708 {
7709 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7710 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7711 }
7712 else
7713 {
7714 it->bidi_it.charpos = IT_CHARPOS (*it);
7715 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7716 }
7717
7718 if (it->bidi_it.charpos == eob)
7719 {
7720 /* Nothing to do, but reset the FIRST_ELT flag, like
7721 bidi_paragraph_init does, because we are not going to
7722 call it. */
7723 it->bidi_it.first_elt = false;
7724 }
7725 else if (it->bidi_it.charpos == bob
7726 || (!string_p
7727 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7728 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7729 {
7730 /* If we are at the beginning of a line/string, we can produce
7731 the next element right away. */
7732 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7733 bidi_move_to_visually_next (&it->bidi_it);
7734 }
7735 else
7736 {
7737 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7738
7739 /* We need to prime the bidi iterator starting at the line's or
7740 string's beginning, before we will be able to produce the
7741 next element. */
7742 if (string_p)
7743 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7744 else
7745 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7746 IT_BYTEPOS (*it), -1,
7747 &it->bidi_it.bytepos);
7748 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7749 do
7750 {
7751 /* Now return to buffer/string position where we were asked
7752 to get the next display element, and produce that. */
7753 bidi_move_to_visually_next (&it->bidi_it);
7754 }
7755 while (it->bidi_it.bytepos != orig_bytepos
7756 && it->bidi_it.charpos < eob);
7757 }
7758
7759 /* Adjust IT's position information to where we ended up. */
7760 if (STRINGP (it->string))
7761 {
7762 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7763 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7764 }
7765 else
7766 {
7767 IT_CHARPOS (*it) = it->bidi_it.charpos;
7768 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7769 }
7770
7771 if (STRINGP (it->string) || !it->s)
7772 {
7773 ptrdiff_t stop, charpos, bytepos;
7774
7775 if (STRINGP (it->string))
7776 {
7777 eassert (!it->s);
7778 stop = SCHARS (it->string);
7779 if (stop > it->end_charpos)
7780 stop = it->end_charpos;
7781 charpos = IT_STRING_CHARPOS (*it);
7782 bytepos = IT_STRING_BYTEPOS (*it);
7783 }
7784 else
7785 {
7786 stop = it->end_charpos;
7787 charpos = IT_CHARPOS (*it);
7788 bytepos = IT_BYTEPOS (*it);
7789 }
7790 if (it->bidi_it.scan_dir < 0)
7791 stop = -1;
7792 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7793 it->string);
7794 }
7795 }
7796
7797 /* Load IT with the next display element from Lisp string IT->string.
7798 IT->current.string_pos is the current position within the string.
7799 If IT->current.overlay_string_index >= 0, the Lisp string is an
7800 overlay string. */
7801
7802 static bool
7803 next_element_from_string (struct it *it)
7804 {
7805 struct text_pos position;
7806
7807 eassert (STRINGP (it->string));
7808 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7809 eassert (IT_STRING_CHARPOS (*it) >= 0);
7810 position = it->current.string_pos;
7811
7812 /* With bidi reordering, the character to display might not be the
7813 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7814 that we were reseat()ed to a new string, whose paragraph
7815 direction is not known. */
7816 if (it->bidi_p && it->bidi_it.first_elt)
7817 {
7818 get_visually_first_element (it);
7819 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7820 }
7821
7822 /* Time to check for invisible text? */
7823 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7824 {
7825 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7826 {
7827 if (!(!it->bidi_p
7828 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7829 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7830 {
7831 /* With bidi non-linear iteration, we could find
7832 ourselves far beyond the last computed stop_charpos,
7833 with several other stop positions in between that we
7834 missed. Scan them all now, in buffer's logical
7835 order, until we find and handle the last stop_charpos
7836 that precedes our current position. */
7837 handle_stop_backwards (it, it->stop_charpos);
7838 return GET_NEXT_DISPLAY_ELEMENT (it);
7839 }
7840 else
7841 {
7842 if (it->bidi_p)
7843 {
7844 /* Take note of the stop position we just moved
7845 across, for when we will move back across it. */
7846 it->prev_stop = it->stop_charpos;
7847 /* If we are at base paragraph embedding level, take
7848 note of the last stop position seen at this
7849 level. */
7850 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7851 it->base_level_stop = it->stop_charpos;
7852 }
7853 handle_stop (it);
7854
7855 /* Since a handler may have changed IT->method, we must
7856 recurse here. */
7857 return GET_NEXT_DISPLAY_ELEMENT (it);
7858 }
7859 }
7860 else if (it->bidi_p
7861 /* If we are before prev_stop, we may have overstepped
7862 on our way backwards a stop_pos, and if so, we need
7863 to handle that stop_pos. */
7864 && IT_STRING_CHARPOS (*it) < it->prev_stop
7865 /* We can sometimes back up for reasons that have nothing
7866 to do with bidi reordering. E.g., compositions. The
7867 code below is only needed when we are above the base
7868 embedding level, so test for that explicitly. */
7869 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7870 {
7871 /* If we lost track of base_level_stop, we have no better
7872 place for handle_stop_backwards to start from than string
7873 beginning. This happens, e.g., when we were reseated to
7874 the previous screenful of text by vertical-motion. */
7875 if (it->base_level_stop <= 0
7876 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7877 it->base_level_stop = 0;
7878 handle_stop_backwards (it, it->base_level_stop);
7879 return GET_NEXT_DISPLAY_ELEMENT (it);
7880 }
7881 }
7882
7883 if (it->current.overlay_string_index >= 0)
7884 {
7885 /* Get the next character from an overlay string. In overlay
7886 strings, there is no field width or padding with spaces to
7887 do. */
7888 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7889 {
7890 it->what = IT_EOB;
7891 return false;
7892 }
7893 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7894 IT_STRING_BYTEPOS (*it),
7895 it->bidi_it.scan_dir < 0
7896 ? -1
7897 : SCHARS (it->string))
7898 && next_element_from_composition (it))
7899 {
7900 return true;
7901 }
7902 else if (STRING_MULTIBYTE (it->string))
7903 {
7904 const unsigned char *s = (SDATA (it->string)
7905 + IT_STRING_BYTEPOS (*it));
7906 it->c = string_char_and_length (s, &it->len);
7907 }
7908 else
7909 {
7910 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7911 it->len = 1;
7912 }
7913 }
7914 else
7915 {
7916 /* Get the next character from a Lisp string that is not an
7917 overlay string. Such strings come from the mode line, for
7918 example. We may have to pad with spaces, or truncate the
7919 string. See also next_element_from_c_string. */
7920 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7921 {
7922 it->what = IT_EOB;
7923 return false;
7924 }
7925 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7926 {
7927 /* Pad with spaces. */
7928 it->c = ' ', it->len = 1;
7929 CHARPOS (position) = BYTEPOS (position) = -1;
7930 }
7931 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7932 IT_STRING_BYTEPOS (*it),
7933 it->bidi_it.scan_dir < 0
7934 ? -1
7935 : it->string_nchars)
7936 && next_element_from_composition (it))
7937 {
7938 return true;
7939 }
7940 else if (STRING_MULTIBYTE (it->string))
7941 {
7942 const unsigned char *s = (SDATA (it->string)
7943 + IT_STRING_BYTEPOS (*it));
7944 it->c = string_char_and_length (s, &it->len);
7945 }
7946 else
7947 {
7948 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7949 it->len = 1;
7950 }
7951 }
7952
7953 /* Record what we have and where it came from. */
7954 it->what = IT_CHARACTER;
7955 it->object = it->string;
7956 it->position = position;
7957 return true;
7958 }
7959
7960
7961 /* Load IT with next display element from C string IT->s.
7962 IT->string_nchars is the maximum number of characters to return
7963 from the string. IT->end_charpos may be greater than
7964 IT->string_nchars when this function is called, in which case we
7965 may have to return padding spaces. Value is false if end of string
7966 reached, including padding spaces. */
7967
7968 static bool
7969 next_element_from_c_string (struct it *it)
7970 {
7971 bool success_p = true;
7972
7973 eassert (it->s);
7974 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7975 it->what = IT_CHARACTER;
7976 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7977 it->object = make_number (0);
7978
7979 /* With bidi reordering, the character to display might not be the
7980 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
7981 we were reseated to a new string, whose paragraph direction is
7982 not known. */
7983 if (it->bidi_p && it->bidi_it.first_elt)
7984 get_visually_first_element (it);
7985
7986 /* IT's position can be greater than IT->string_nchars in case a
7987 field width or precision has been specified when the iterator was
7988 initialized. */
7989 if (IT_CHARPOS (*it) >= it->end_charpos)
7990 {
7991 /* End of the game. */
7992 it->what = IT_EOB;
7993 success_p = false;
7994 }
7995 else if (IT_CHARPOS (*it) >= it->string_nchars)
7996 {
7997 /* Pad with spaces. */
7998 it->c = ' ', it->len = 1;
7999 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8000 }
8001 else if (it->multibyte_p)
8002 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8003 else
8004 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8005
8006 return success_p;
8007 }
8008
8009
8010 /* Set up IT to return characters from an ellipsis, if appropriate.
8011 The definition of the ellipsis glyphs may come from a display table
8012 entry. This function fills IT with the first glyph from the
8013 ellipsis if an ellipsis is to be displayed. */
8014
8015 static bool
8016 next_element_from_ellipsis (struct it *it)
8017 {
8018 if (it->selective_display_ellipsis_p)
8019 setup_for_ellipsis (it, it->len);
8020 else
8021 {
8022 /* The face at the current position may be different from the
8023 face we find after the invisible text. Remember what it
8024 was in IT->saved_face_id, and signal that it's there by
8025 setting face_before_selective_p. */
8026 it->saved_face_id = it->face_id;
8027 it->method = GET_FROM_BUFFER;
8028 it->object = it->w->contents;
8029 reseat_at_next_visible_line_start (it, true);
8030 it->face_before_selective_p = true;
8031 }
8032
8033 return GET_NEXT_DISPLAY_ELEMENT (it);
8034 }
8035
8036
8037 /* Deliver an image display element. The iterator IT is already
8038 filled with image information (done in handle_display_prop). Value
8039 is always true. */
8040
8041
8042 static bool
8043 next_element_from_image (struct it *it)
8044 {
8045 it->what = IT_IMAGE;
8046 return true;
8047 }
8048
8049
8050 /* Fill iterator IT with next display element from a stretch glyph
8051 property. IT->object is the value of the text property. Value is
8052 always true. */
8053
8054 static bool
8055 next_element_from_stretch (struct it *it)
8056 {
8057 it->what = IT_STRETCH;
8058 return true;
8059 }
8060
8061 /* Scan backwards from IT's current position until we find a stop
8062 position, or until BEGV. This is called when we find ourself
8063 before both the last known prev_stop and base_level_stop while
8064 reordering bidirectional text. */
8065
8066 static void
8067 compute_stop_pos_backwards (struct it *it)
8068 {
8069 const int SCAN_BACK_LIMIT = 1000;
8070 struct text_pos pos;
8071 struct display_pos save_current = it->current;
8072 struct text_pos save_position = it->position;
8073 ptrdiff_t charpos = IT_CHARPOS (*it);
8074 ptrdiff_t where_we_are = charpos;
8075 ptrdiff_t save_stop_pos = it->stop_charpos;
8076 ptrdiff_t save_end_pos = it->end_charpos;
8077
8078 eassert (NILP (it->string) && !it->s);
8079 eassert (it->bidi_p);
8080 it->bidi_p = false;
8081 do
8082 {
8083 it->end_charpos = min (charpos + 1, ZV);
8084 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8085 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8086 reseat_1 (it, pos, false);
8087 compute_stop_pos (it);
8088 /* We must advance forward, right? */
8089 if (it->stop_charpos <= charpos)
8090 emacs_abort ();
8091 }
8092 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8093
8094 if (it->stop_charpos <= where_we_are)
8095 it->prev_stop = it->stop_charpos;
8096 else
8097 it->prev_stop = BEGV;
8098 it->bidi_p = true;
8099 it->current = save_current;
8100 it->position = save_position;
8101 it->stop_charpos = save_stop_pos;
8102 it->end_charpos = save_end_pos;
8103 }
8104
8105 /* Scan forward from CHARPOS in the current buffer/string, until we
8106 find a stop position > current IT's position. Then handle the stop
8107 position before that. This is called when we bump into a stop
8108 position while reordering bidirectional text. CHARPOS should be
8109 the last previously processed stop_pos (or BEGV/0, if none were
8110 processed yet) whose position is less that IT's current
8111 position. */
8112
8113 static void
8114 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8115 {
8116 bool bufp = !STRINGP (it->string);
8117 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8118 struct display_pos save_current = it->current;
8119 struct text_pos save_position = it->position;
8120 struct text_pos pos1;
8121 ptrdiff_t next_stop;
8122
8123 /* Scan in strict logical order. */
8124 eassert (it->bidi_p);
8125 it->bidi_p = false;
8126 do
8127 {
8128 it->prev_stop = charpos;
8129 if (bufp)
8130 {
8131 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8132 reseat_1 (it, pos1, false);
8133 }
8134 else
8135 it->current.string_pos = string_pos (charpos, it->string);
8136 compute_stop_pos (it);
8137 /* We must advance forward, right? */
8138 if (it->stop_charpos <= it->prev_stop)
8139 emacs_abort ();
8140 charpos = it->stop_charpos;
8141 }
8142 while (charpos <= where_we_are);
8143
8144 it->bidi_p = true;
8145 it->current = save_current;
8146 it->position = save_position;
8147 next_stop = it->stop_charpos;
8148 it->stop_charpos = it->prev_stop;
8149 handle_stop (it);
8150 it->stop_charpos = next_stop;
8151 }
8152
8153 /* Load IT with the next display element from current_buffer. Value
8154 is false if end of buffer reached. IT->stop_charpos is the next
8155 position at which to stop and check for text properties or buffer
8156 end. */
8157
8158 static bool
8159 next_element_from_buffer (struct it *it)
8160 {
8161 bool success_p = true;
8162
8163 eassert (IT_CHARPOS (*it) >= BEGV);
8164 eassert (NILP (it->string) && !it->s);
8165 eassert (!it->bidi_p
8166 || (EQ (it->bidi_it.string.lstring, Qnil)
8167 && it->bidi_it.string.s == NULL));
8168
8169 /* With bidi reordering, the character to display might not be the
8170 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8171 we were reseat()ed to a new buffer position, which is potentially
8172 a different paragraph. */
8173 if (it->bidi_p && it->bidi_it.first_elt)
8174 {
8175 get_visually_first_element (it);
8176 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8177 }
8178
8179 if (IT_CHARPOS (*it) >= it->stop_charpos)
8180 {
8181 if (IT_CHARPOS (*it) >= it->end_charpos)
8182 {
8183 bool overlay_strings_follow_p;
8184
8185 /* End of the game, except when overlay strings follow that
8186 haven't been returned yet. */
8187 if (it->overlay_strings_at_end_processed_p)
8188 overlay_strings_follow_p = false;
8189 else
8190 {
8191 it->overlay_strings_at_end_processed_p = true;
8192 overlay_strings_follow_p = get_overlay_strings (it, 0);
8193 }
8194
8195 if (overlay_strings_follow_p)
8196 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8197 else
8198 {
8199 it->what = IT_EOB;
8200 it->position = it->current.pos;
8201 success_p = false;
8202 }
8203 }
8204 else if (!(!it->bidi_p
8205 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8206 || IT_CHARPOS (*it) == it->stop_charpos))
8207 {
8208 /* With bidi non-linear iteration, we could find ourselves
8209 far beyond the last computed stop_charpos, with several
8210 other stop positions in between that we missed. Scan
8211 them all now, in buffer's logical order, until we find
8212 and handle the last stop_charpos that precedes our
8213 current position. */
8214 handle_stop_backwards (it, it->stop_charpos);
8215 it->ignore_overlay_strings_at_pos_p = false;
8216 return GET_NEXT_DISPLAY_ELEMENT (it);
8217 }
8218 else
8219 {
8220 if (it->bidi_p)
8221 {
8222 /* Take note of the stop position we just moved across,
8223 for when we will move back across it. */
8224 it->prev_stop = it->stop_charpos;
8225 /* If we are at base paragraph embedding level, take
8226 note of the last stop position seen at this
8227 level. */
8228 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8229 it->base_level_stop = it->stop_charpos;
8230 }
8231 handle_stop (it);
8232 it->ignore_overlay_strings_at_pos_p = false;
8233 return GET_NEXT_DISPLAY_ELEMENT (it);
8234 }
8235 }
8236 else if (it->bidi_p
8237 /* If we are before prev_stop, we may have overstepped on
8238 our way backwards a stop_pos, and if so, we need to
8239 handle that stop_pos. */
8240 && IT_CHARPOS (*it) < it->prev_stop
8241 /* We can sometimes back up for reasons that have nothing
8242 to do with bidi reordering. E.g., compositions. The
8243 code below is only needed when we are above the base
8244 embedding level, so test for that explicitly. */
8245 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8246 {
8247 if (it->base_level_stop <= 0
8248 || IT_CHARPOS (*it) < it->base_level_stop)
8249 {
8250 /* If we lost track of base_level_stop, we need to find
8251 prev_stop by looking backwards. This happens, e.g., when
8252 we were reseated to the previous screenful of text by
8253 vertical-motion. */
8254 it->base_level_stop = BEGV;
8255 compute_stop_pos_backwards (it);
8256 handle_stop_backwards (it, it->prev_stop);
8257 }
8258 else
8259 handle_stop_backwards (it, it->base_level_stop);
8260 it->ignore_overlay_strings_at_pos_p = false;
8261 return GET_NEXT_DISPLAY_ELEMENT (it);
8262 }
8263 else
8264 {
8265 /* No face changes, overlays etc. in sight, so just return a
8266 character from current_buffer. */
8267 unsigned char *p;
8268 ptrdiff_t stop;
8269
8270 /* We moved to the next buffer position, so any info about
8271 previously seen overlays is no longer valid. */
8272 it->ignore_overlay_strings_at_pos_p = false;
8273
8274 /* Maybe run the redisplay end trigger hook. Performance note:
8275 This doesn't seem to cost measurable time. */
8276 if (it->redisplay_end_trigger_charpos
8277 && it->glyph_row
8278 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8279 run_redisplay_end_trigger_hook (it);
8280
8281 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8282 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8283 stop)
8284 && next_element_from_composition (it))
8285 {
8286 return true;
8287 }
8288
8289 /* Get the next character, maybe multibyte. */
8290 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8291 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8292 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8293 else
8294 it->c = *p, it->len = 1;
8295
8296 /* Record what we have and where it came from. */
8297 it->what = IT_CHARACTER;
8298 it->object = it->w->contents;
8299 it->position = it->current.pos;
8300
8301 /* Normally we return the character found above, except when we
8302 really want to return an ellipsis for selective display. */
8303 if (it->selective)
8304 {
8305 if (it->c == '\n')
8306 {
8307 /* A value of selective > 0 means hide lines indented more
8308 than that number of columns. */
8309 if (it->selective > 0
8310 && IT_CHARPOS (*it) + 1 < ZV
8311 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8312 IT_BYTEPOS (*it) + 1,
8313 it->selective))
8314 {
8315 success_p = next_element_from_ellipsis (it);
8316 it->dpvec_char_len = -1;
8317 }
8318 }
8319 else if (it->c == '\r' && it->selective == -1)
8320 {
8321 /* A value of selective == -1 means that everything from the
8322 CR to the end of the line is invisible, with maybe an
8323 ellipsis displayed for it. */
8324 success_p = next_element_from_ellipsis (it);
8325 it->dpvec_char_len = -1;
8326 }
8327 }
8328 }
8329
8330 /* Value is false if end of buffer reached. */
8331 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8332 return success_p;
8333 }
8334
8335
8336 /* Run the redisplay end trigger hook for IT. */
8337
8338 static void
8339 run_redisplay_end_trigger_hook (struct it *it)
8340 {
8341 /* IT->glyph_row should be non-null, i.e. we should be actually
8342 displaying something, or otherwise we should not run the hook. */
8343 eassert (it->glyph_row);
8344
8345 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8346 it->redisplay_end_trigger_charpos = 0;
8347
8348 /* Since we are *trying* to run these functions, don't try to run
8349 them again, even if they get an error. */
8350 wset_redisplay_end_trigger (it->w, Qnil);
8351 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8352 make_number (charpos));
8353
8354 /* Notice if it changed the face of the character we are on. */
8355 handle_face_prop (it);
8356 }
8357
8358
8359 /* Deliver a composition display element. Unlike the other
8360 next_element_from_XXX, this function is not registered in the array
8361 get_next_element[]. It is called from next_element_from_buffer and
8362 next_element_from_string when necessary. */
8363
8364 static bool
8365 next_element_from_composition (struct it *it)
8366 {
8367 it->what = IT_COMPOSITION;
8368 it->len = it->cmp_it.nbytes;
8369 if (STRINGP (it->string))
8370 {
8371 if (it->c < 0)
8372 {
8373 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8374 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8375 return false;
8376 }
8377 it->position = it->current.string_pos;
8378 it->object = it->string;
8379 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8380 IT_STRING_BYTEPOS (*it), it->string);
8381 }
8382 else
8383 {
8384 if (it->c < 0)
8385 {
8386 IT_CHARPOS (*it) += it->cmp_it.nchars;
8387 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8388 if (it->bidi_p)
8389 {
8390 if (it->bidi_it.new_paragraph)
8391 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8392 false);
8393 /* Resync the bidi iterator with IT's new position.
8394 FIXME: this doesn't support bidirectional text. */
8395 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8396 bidi_move_to_visually_next (&it->bidi_it);
8397 }
8398 return false;
8399 }
8400 it->position = it->current.pos;
8401 it->object = it->w->contents;
8402 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8403 IT_BYTEPOS (*it), Qnil);
8404 }
8405 return true;
8406 }
8407
8408
8409 \f
8410 /***********************************************************************
8411 Moving an iterator without producing glyphs
8412 ***********************************************************************/
8413
8414 /* Check if iterator is at a position corresponding to a valid buffer
8415 position after some move_it_ call. */
8416
8417 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8418 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8419
8420
8421 /* Move iterator IT to a specified buffer or X position within one
8422 line on the display without producing glyphs.
8423
8424 OP should be a bit mask including some or all of these bits:
8425 MOVE_TO_X: Stop upon reaching x-position TO_X.
8426 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8427 Regardless of OP's value, stop upon reaching the end of the display line.
8428
8429 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8430 This means, in particular, that TO_X includes window's horizontal
8431 scroll amount.
8432
8433 The return value has several possible values that
8434 say what condition caused the scan to stop:
8435
8436 MOVE_POS_MATCH_OR_ZV
8437 - when TO_POS or ZV was reached.
8438
8439 MOVE_X_REACHED
8440 -when TO_X was reached before TO_POS or ZV were reached.
8441
8442 MOVE_LINE_CONTINUED
8443 - when we reached the end of the display area and the line must
8444 be continued.
8445
8446 MOVE_LINE_TRUNCATED
8447 - when we reached the end of the display area and the line is
8448 truncated.
8449
8450 MOVE_NEWLINE_OR_CR
8451 - when we stopped at a line end, i.e. a newline or a CR and selective
8452 display is on. */
8453
8454 static enum move_it_result
8455 move_it_in_display_line_to (struct it *it,
8456 ptrdiff_t to_charpos, int to_x,
8457 enum move_operation_enum op)
8458 {
8459 enum move_it_result result = MOVE_UNDEFINED;
8460 struct glyph_row *saved_glyph_row;
8461 struct it wrap_it, atpos_it, atx_it, ppos_it;
8462 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8463 void *ppos_data = NULL;
8464 bool may_wrap = false;
8465 enum it_method prev_method = it->method;
8466 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8467 bool saw_smaller_pos = prev_pos < to_charpos;
8468
8469 /* Don't produce glyphs in produce_glyphs. */
8470 saved_glyph_row = it->glyph_row;
8471 it->glyph_row = NULL;
8472
8473 /* Use wrap_it to save a copy of IT wherever a word wrap could
8474 occur. Use atpos_it to save a copy of IT at the desired buffer
8475 position, if found, so that we can scan ahead and check if the
8476 word later overshoots the window edge. Use atx_it similarly, for
8477 pixel positions. */
8478 wrap_it.sp = -1;
8479 atpos_it.sp = -1;
8480 atx_it.sp = -1;
8481
8482 /* Use ppos_it under bidi reordering to save a copy of IT for the
8483 initial position. We restore that position in IT when we have
8484 scanned the entire display line without finding a match for
8485 TO_CHARPOS and all the character positions are greater than
8486 TO_CHARPOS. We then restart the scan from the initial position,
8487 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8488 the closest to TO_CHARPOS. */
8489 if (it->bidi_p)
8490 {
8491 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8492 {
8493 SAVE_IT (ppos_it, *it, ppos_data);
8494 closest_pos = IT_CHARPOS (*it);
8495 }
8496 else
8497 closest_pos = ZV;
8498 }
8499
8500 #define BUFFER_POS_REACHED_P() \
8501 ((op & MOVE_TO_POS) != 0 \
8502 && BUFFERP (it->object) \
8503 && (IT_CHARPOS (*it) == to_charpos \
8504 || ((!it->bidi_p \
8505 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8506 && IT_CHARPOS (*it) > to_charpos) \
8507 || (it->what == IT_COMPOSITION \
8508 && ((IT_CHARPOS (*it) > to_charpos \
8509 && to_charpos >= it->cmp_it.charpos) \
8510 || (IT_CHARPOS (*it) < to_charpos \
8511 && to_charpos <= it->cmp_it.charpos)))) \
8512 && (it->method == GET_FROM_BUFFER \
8513 || (it->method == GET_FROM_DISPLAY_VECTOR \
8514 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8515
8516 /* If there's a line-/wrap-prefix, handle it. */
8517 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8518 && it->current_y < it->last_visible_y)
8519 handle_line_prefix (it);
8520
8521 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8522 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8523
8524 while (true)
8525 {
8526 int x, i, ascent = 0, descent = 0;
8527
8528 /* Utility macro to reset an iterator with x, ascent, and descent. */
8529 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8530 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8531 (IT)->max_descent = descent)
8532
8533 /* Stop if we move beyond TO_CHARPOS (after an image or a
8534 display string or stretch glyph). */
8535 if ((op & MOVE_TO_POS) != 0
8536 && BUFFERP (it->object)
8537 && it->method == GET_FROM_BUFFER
8538 && (((!it->bidi_p
8539 /* When the iterator is at base embedding level, we
8540 are guaranteed that characters are delivered for
8541 display in strictly increasing order of their
8542 buffer positions. */
8543 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8544 && IT_CHARPOS (*it) > to_charpos)
8545 || (it->bidi_p
8546 && (prev_method == GET_FROM_IMAGE
8547 || prev_method == GET_FROM_STRETCH
8548 || prev_method == GET_FROM_STRING)
8549 /* Passed TO_CHARPOS from left to right. */
8550 && ((prev_pos < to_charpos
8551 && IT_CHARPOS (*it) > to_charpos)
8552 /* Passed TO_CHARPOS from right to left. */
8553 || (prev_pos > to_charpos
8554 && IT_CHARPOS (*it) < to_charpos)))))
8555 {
8556 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8557 {
8558 result = MOVE_POS_MATCH_OR_ZV;
8559 break;
8560 }
8561 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8562 /* If wrap_it is valid, the current position might be in a
8563 word that is wrapped. So, save the iterator in
8564 atpos_it and continue to see if wrapping happens. */
8565 SAVE_IT (atpos_it, *it, atpos_data);
8566 }
8567
8568 /* Stop when ZV reached.
8569 We used to stop here when TO_CHARPOS reached as well, but that is
8570 too soon if this glyph does not fit on this line. So we handle it
8571 explicitly below. */
8572 if (!get_next_display_element (it))
8573 {
8574 result = MOVE_POS_MATCH_OR_ZV;
8575 break;
8576 }
8577
8578 if (it->line_wrap == TRUNCATE)
8579 {
8580 if (BUFFER_POS_REACHED_P ())
8581 {
8582 result = MOVE_POS_MATCH_OR_ZV;
8583 break;
8584 }
8585 }
8586 else
8587 {
8588 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8589 {
8590 if (IT_DISPLAYING_WHITESPACE (it))
8591 may_wrap = true;
8592 else if (may_wrap)
8593 {
8594 /* We have reached a glyph that follows one or more
8595 whitespace characters. If the position is
8596 already found, we are done. */
8597 if (atpos_it.sp >= 0)
8598 {
8599 RESTORE_IT (it, &atpos_it, atpos_data);
8600 result = MOVE_POS_MATCH_OR_ZV;
8601 goto done;
8602 }
8603 if (atx_it.sp >= 0)
8604 {
8605 RESTORE_IT (it, &atx_it, atx_data);
8606 result = MOVE_X_REACHED;
8607 goto done;
8608 }
8609 /* Otherwise, we can wrap here. */
8610 SAVE_IT (wrap_it, *it, wrap_data);
8611 may_wrap = false;
8612 }
8613 }
8614 }
8615
8616 /* Remember the line height for the current line, in case
8617 the next element doesn't fit on the line. */
8618 ascent = it->max_ascent;
8619 descent = it->max_descent;
8620
8621 /* The call to produce_glyphs will get the metrics of the
8622 display element IT is loaded with. Record the x-position
8623 before this display element, in case it doesn't fit on the
8624 line. */
8625 x = it->current_x;
8626
8627 PRODUCE_GLYPHS (it);
8628
8629 if (it->area != TEXT_AREA)
8630 {
8631 prev_method = it->method;
8632 if (it->method == GET_FROM_BUFFER)
8633 prev_pos = IT_CHARPOS (*it);
8634 set_iterator_to_next (it, true);
8635 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8636 SET_TEXT_POS (this_line_min_pos,
8637 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8638 if (it->bidi_p
8639 && (op & MOVE_TO_POS)
8640 && IT_CHARPOS (*it) > to_charpos
8641 && IT_CHARPOS (*it) < closest_pos)
8642 closest_pos = IT_CHARPOS (*it);
8643 continue;
8644 }
8645
8646 /* The number of glyphs we get back in IT->nglyphs will normally
8647 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8648 character on a terminal frame, or (iii) a line end. For the
8649 second case, IT->nglyphs - 1 padding glyphs will be present.
8650 (On X frames, there is only one glyph produced for a
8651 composite character.)
8652
8653 The behavior implemented below means, for continuation lines,
8654 that as many spaces of a TAB as fit on the current line are
8655 displayed there. For terminal frames, as many glyphs of a
8656 multi-glyph character are displayed in the current line, too.
8657 This is what the old redisplay code did, and we keep it that
8658 way. Under X, the whole shape of a complex character must
8659 fit on the line or it will be completely displayed in the
8660 next line.
8661
8662 Note that both for tabs and padding glyphs, all glyphs have
8663 the same width. */
8664 if (it->nglyphs)
8665 {
8666 /* More than one glyph or glyph doesn't fit on line. All
8667 glyphs have the same width. */
8668 int single_glyph_width = it->pixel_width / it->nglyphs;
8669 int new_x;
8670 int x_before_this_char = x;
8671 int hpos_before_this_char = it->hpos;
8672
8673 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8674 {
8675 new_x = x + single_glyph_width;
8676
8677 /* We want to leave anything reaching TO_X to the caller. */
8678 if ((op & MOVE_TO_X) && new_x > to_x)
8679 {
8680 if (BUFFER_POS_REACHED_P ())
8681 {
8682 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8683 goto buffer_pos_reached;
8684 if (atpos_it.sp < 0)
8685 {
8686 SAVE_IT (atpos_it, *it, atpos_data);
8687 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8688 }
8689 }
8690 else
8691 {
8692 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8693 {
8694 it->current_x = x;
8695 result = MOVE_X_REACHED;
8696 break;
8697 }
8698 if (atx_it.sp < 0)
8699 {
8700 SAVE_IT (atx_it, *it, atx_data);
8701 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8702 }
8703 }
8704 }
8705
8706 if (/* Lines are continued. */
8707 it->line_wrap != TRUNCATE
8708 && (/* And glyph doesn't fit on the line. */
8709 new_x > it->last_visible_x
8710 /* Or it fits exactly and we're on a window
8711 system frame. */
8712 || (new_x == it->last_visible_x
8713 && FRAME_WINDOW_P (it->f)
8714 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8715 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8716 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8717 {
8718 if (/* IT->hpos == 0 means the very first glyph
8719 doesn't fit on the line, e.g. a wide image. */
8720 it->hpos == 0
8721 || (new_x == it->last_visible_x
8722 && FRAME_WINDOW_P (it->f)))
8723 {
8724 ++it->hpos;
8725 it->current_x = new_x;
8726
8727 /* The character's last glyph just barely fits
8728 in this row. */
8729 if (i == it->nglyphs - 1)
8730 {
8731 /* If this is the destination position,
8732 return a position *before* it in this row,
8733 now that we know it fits in this row. */
8734 if (BUFFER_POS_REACHED_P ())
8735 {
8736 if (it->line_wrap != WORD_WRAP
8737 || wrap_it.sp < 0
8738 /* If we've just found whitespace to
8739 wrap, effectively ignore the
8740 previous wrap point -- it is no
8741 longer relevant, but we won't
8742 have an opportunity to update it,
8743 since we've reached the edge of
8744 this screen line. */
8745 || (may_wrap
8746 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8747 {
8748 it->hpos = hpos_before_this_char;
8749 it->current_x = x_before_this_char;
8750 result = MOVE_POS_MATCH_OR_ZV;
8751 break;
8752 }
8753 if (it->line_wrap == WORD_WRAP
8754 && atpos_it.sp < 0)
8755 {
8756 SAVE_IT (atpos_it, *it, atpos_data);
8757 atpos_it.current_x = x_before_this_char;
8758 atpos_it.hpos = hpos_before_this_char;
8759 }
8760 }
8761
8762 prev_method = it->method;
8763 if (it->method == GET_FROM_BUFFER)
8764 prev_pos = IT_CHARPOS (*it);
8765 set_iterator_to_next (it, true);
8766 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8767 SET_TEXT_POS (this_line_min_pos,
8768 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8769 /* On graphical terminals, newlines may
8770 "overflow" into the fringe if
8771 overflow-newline-into-fringe is non-nil.
8772 On text terminals, and on graphical
8773 terminals with no right margin, newlines
8774 may overflow into the last glyph on the
8775 display line.*/
8776 if (!FRAME_WINDOW_P (it->f)
8777 || ((it->bidi_p
8778 && it->bidi_it.paragraph_dir == R2L)
8779 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8780 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8781 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8782 {
8783 if (!get_next_display_element (it))
8784 {
8785 result = MOVE_POS_MATCH_OR_ZV;
8786 break;
8787 }
8788 if (BUFFER_POS_REACHED_P ())
8789 {
8790 if (ITERATOR_AT_END_OF_LINE_P (it))
8791 result = MOVE_POS_MATCH_OR_ZV;
8792 else
8793 result = MOVE_LINE_CONTINUED;
8794 break;
8795 }
8796 if (ITERATOR_AT_END_OF_LINE_P (it)
8797 && (it->line_wrap != WORD_WRAP
8798 || wrap_it.sp < 0
8799 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8800 {
8801 result = MOVE_NEWLINE_OR_CR;
8802 break;
8803 }
8804 }
8805 }
8806 }
8807 else
8808 IT_RESET_X_ASCENT_DESCENT (it);
8809
8810 /* If the screen line ends with whitespace, and we
8811 are under word-wrap, don't use wrap_it: it is no
8812 longer relevant, but we won't have an opportunity
8813 to update it, since we are done with this screen
8814 line. */
8815 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8816 {
8817 /* If we've found TO_X, go back there, as we now
8818 know the last word fits on this screen line. */
8819 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8820 && atx_it.sp >= 0)
8821 {
8822 RESTORE_IT (it, &atx_it, atx_data);
8823 atpos_it.sp = -1;
8824 atx_it.sp = -1;
8825 result = MOVE_X_REACHED;
8826 break;
8827 }
8828 }
8829 else if (wrap_it.sp >= 0)
8830 {
8831 RESTORE_IT (it, &wrap_it, wrap_data);
8832 atpos_it.sp = -1;
8833 atx_it.sp = -1;
8834 }
8835
8836 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8837 IT_CHARPOS (*it)));
8838 result = MOVE_LINE_CONTINUED;
8839 break;
8840 }
8841
8842 if (BUFFER_POS_REACHED_P ())
8843 {
8844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8845 goto buffer_pos_reached;
8846 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8847 {
8848 SAVE_IT (atpos_it, *it, atpos_data);
8849 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8850 }
8851 }
8852
8853 if (new_x > it->first_visible_x)
8854 {
8855 /* Glyph is visible. Increment number of glyphs that
8856 would be displayed. */
8857 ++it->hpos;
8858 }
8859 }
8860
8861 if (result != MOVE_UNDEFINED)
8862 break;
8863 }
8864 else if (BUFFER_POS_REACHED_P ())
8865 {
8866 buffer_pos_reached:
8867 IT_RESET_X_ASCENT_DESCENT (it);
8868 result = MOVE_POS_MATCH_OR_ZV;
8869 break;
8870 }
8871 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8872 {
8873 /* Stop when TO_X specified and reached. This check is
8874 necessary here because of lines consisting of a line end,
8875 only. The line end will not produce any glyphs and we
8876 would never get MOVE_X_REACHED. */
8877 eassert (it->nglyphs == 0);
8878 result = MOVE_X_REACHED;
8879 break;
8880 }
8881
8882 /* Is this a line end? If yes, we're done. */
8883 if (ITERATOR_AT_END_OF_LINE_P (it))
8884 {
8885 /* If we are past TO_CHARPOS, but never saw any character
8886 positions smaller than TO_CHARPOS, return
8887 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8888 did. */
8889 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8890 {
8891 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8892 {
8893 if (closest_pos < ZV)
8894 {
8895 RESTORE_IT (it, &ppos_it, ppos_data);
8896 /* Don't recurse if closest_pos is equal to
8897 to_charpos, since we have just tried that. */
8898 if (closest_pos != to_charpos)
8899 move_it_in_display_line_to (it, closest_pos, -1,
8900 MOVE_TO_POS);
8901 result = MOVE_POS_MATCH_OR_ZV;
8902 }
8903 else
8904 goto buffer_pos_reached;
8905 }
8906 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8907 && IT_CHARPOS (*it) > to_charpos)
8908 goto buffer_pos_reached;
8909 else
8910 result = MOVE_NEWLINE_OR_CR;
8911 }
8912 else
8913 result = MOVE_NEWLINE_OR_CR;
8914 break;
8915 }
8916
8917 prev_method = it->method;
8918 if (it->method == GET_FROM_BUFFER)
8919 prev_pos = IT_CHARPOS (*it);
8920 /* The current display element has been consumed. Advance
8921 to the next. */
8922 set_iterator_to_next (it, true);
8923 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8924 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8925 if (IT_CHARPOS (*it) < to_charpos)
8926 saw_smaller_pos = true;
8927 if (it->bidi_p
8928 && (op & MOVE_TO_POS)
8929 && IT_CHARPOS (*it) >= to_charpos
8930 && IT_CHARPOS (*it) < closest_pos)
8931 closest_pos = IT_CHARPOS (*it);
8932
8933 /* Stop if lines are truncated and IT's current x-position is
8934 past the right edge of the window now. */
8935 if (it->line_wrap == TRUNCATE
8936 && it->current_x >= it->last_visible_x)
8937 {
8938 if (!FRAME_WINDOW_P (it->f)
8939 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8940 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8941 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8942 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8943 {
8944 bool at_eob_p = false;
8945
8946 if ((at_eob_p = !get_next_display_element (it))
8947 || BUFFER_POS_REACHED_P ()
8948 /* If we are past TO_CHARPOS, but never saw any
8949 character positions smaller than TO_CHARPOS,
8950 return MOVE_POS_MATCH_OR_ZV, like the
8951 unidirectional display did. */
8952 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8953 && !saw_smaller_pos
8954 && IT_CHARPOS (*it) > to_charpos))
8955 {
8956 if (it->bidi_p
8957 && !BUFFER_POS_REACHED_P ()
8958 && !at_eob_p && closest_pos < ZV)
8959 {
8960 RESTORE_IT (it, &ppos_it, ppos_data);
8961 if (closest_pos != to_charpos)
8962 move_it_in_display_line_to (it, closest_pos, -1,
8963 MOVE_TO_POS);
8964 }
8965 result = MOVE_POS_MATCH_OR_ZV;
8966 break;
8967 }
8968 if (ITERATOR_AT_END_OF_LINE_P (it))
8969 {
8970 result = MOVE_NEWLINE_OR_CR;
8971 break;
8972 }
8973 }
8974 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8975 && !saw_smaller_pos
8976 && IT_CHARPOS (*it) > to_charpos)
8977 {
8978 if (closest_pos < ZV)
8979 {
8980 RESTORE_IT (it, &ppos_it, ppos_data);
8981 if (closest_pos != to_charpos)
8982 move_it_in_display_line_to (it, closest_pos, -1,
8983 MOVE_TO_POS);
8984 }
8985 result = MOVE_POS_MATCH_OR_ZV;
8986 break;
8987 }
8988 result = MOVE_LINE_TRUNCATED;
8989 break;
8990 }
8991 #undef IT_RESET_X_ASCENT_DESCENT
8992 }
8993
8994 #undef BUFFER_POS_REACHED_P
8995
8996 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8997 restore the saved iterator. */
8998 if (atpos_it.sp >= 0)
8999 RESTORE_IT (it, &atpos_it, atpos_data);
9000 else if (atx_it.sp >= 0)
9001 RESTORE_IT (it, &atx_it, atx_data);
9002
9003 done:
9004
9005 if (atpos_data)
9006 bidi_unshelve_cache (atpos_data, true);
9007 if (atx_data)
9008 bidi_unshelve_cache (atx_data, true);
9009 if (wrap_data)
9010 bidi_unshelve_cache (wrap_data, true);
9011 if (ppos_data)
9012 bidi_unshelve_cache (ppos_data, true);
9013
9014 /* Restore the iterator settings altered at the beginning of this
9015 function. */
9016 it->glyph_row = saved_glyph_row;
9017 return result;
9018 }
9019
9020 /* For external use. */
9021 void
9022 move_it_in_display_line (struct it *it,
9023 ptrdiff_t to_charpos, int to_x,
9024 enum move_operation_enum op)
9025 {
9026 if (it->line_wrap == WORD_WRAP
9027 && (op & MOVE_TO_X))
9028 {
9029 struct it save_it;
9030 void *save_data = NULL;
9031 int skip;
9032
9033 SAVE_IT (save_it, *it, save_data);
9034 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9035 /* When word-wrap is on, TO_X may lie past the end
9036 of a wrapped line. Then it->current is the
9037 character on the next line, so backtrack to the
9038 space before the wrap point. */
9039 if (skip == MOVE_LINE_CONTINUED)
9040 {
9041 int prev_x = max (it->current_x - 1, 0);
9042 RESTORE_IT (it, &save_it, save_data);
9043 move_it_in_display_line_to
9044 (it, -1, prev_x, MOVE_TO_X);
9045 }
9046 else
9047 bidi_unshelve_cache (save_data, true);
9048 }
9049 else
9050 move_it_in_display_line_to (it, to_charpos, to_x, op);
9051 }
9052
9053
9054 /* Move IT forward until it satisfies one or more of the criteria in
9055 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9056
9057 OP is a bit-mask that specifies where to stop, and in particular,
9058 which of those four position arguments makes a difference. See the
9059 description of enum move_operation_enum.
9060
9061 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9062 screen line, this function will set IT to the next position that is
9063 displayed to the right of TO_CHARPOS on the screen.
9064
9065 Return the maximum pixel length of any line scanned but never more
9066 than it.last_visible_x. */
9067
9068 int
9069 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9070 {
9071 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9072 int line_height, line_start_x = 0, reached = 0;
9073 int max_current_x = 0;
9074 void *backup_data = NULL;
9075
9076 for (;;)
9077 {
9078 if (op & MOVE_TO_VPOS)
9079 {
9080 /* If no TO_CHARPOS and no TO_X specified, stop at the
9081 start of the line TO_VPOS. */
9082 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9083 {
9084 if (it->vpos == to_vpos)
9085 {
9086 reached = 1;
9087 break;
9088 }
9089 else
9090 skip = move_it_in_display_line_to (it, -1, -1, 0);
9091 }
9092 else
9093 {
9094 /* TO_VPOS >= 0 means stop at TO_X in the line at
9095 TO_VPOS, or at TO_POS, whichever comes first. */
9096 if (it->vpos == to_vpos)
9097 {
9098 reached = 2;
9099 break;
9100 }
9101
9102 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9103
9104 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9105 {
9106 reached = 3;
9107 break;
9108 }
9109 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9110 {
9111 /* We have reached TO_X but not in the line we want. */
9112 skip = move_it_in_display_line_to (it, to_charpos,
9113 -1, MOVE_TO_POS);
9114 if (skip == MOVE_POS_MATCH_OR_ZV)
9115 {
9116 reached = 4;
9117 break;
9118 }
9119 }
9120 }
9121 }
9122 else if (op & MOVE_TO_Y)
9123 {
9124 struct it it_backup;
9125
9126 if (it->line_wrap == WORD_WRAP)
9127 SAVE_IT (it_backup, *it, backup_data);
9128
9129 /* TO_Y specified means stop at TO_X in the line containing
9130 TO_Y---or at TO_CHARPOS if this is reached first. The
9131 problem is that we can't really tell whether the line
9132 contains TO_Y before we have completely scanned it, and
9133 this may skip past TO_X. What we do is to first scan to
9134 TO_X.
9135
9136 If TO_X is not specified, use a TO_X of zero. The reason
9137 is to make the outcome of this function more predictable.
9138 If we didn't use TO_X == 0, we would stop at the end of
9139 the line which is probably not what a caller would expect
9140 to happen. */
9141 skip = move_it_in_display_line_to
9142 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9143 (MOVE_TO_X | (op & MOVE_TO_POS)));
9144
9145 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9146 if (skip == MOVE_POS_MATCH_OR_ZV)
9147 reached = 5;
9148 else if (skip == MOVE_X_REACHED)
9149 {
9150 /* If TO_X was reached, we want to know whether TO_Y is
9151 in the line. We know this is the case if the already
9152 scanned glyphs make the line tall enough. Otherwise,
9153 we must check by scanning the rest of the line. */
9154 line_height = it->max_ascent + it->max_descent;
9155 if (to_y >= it->current_y
9156 && to_y < it->current_y + line_height)
9157 {
9158 reached = 6;
9159 break;
9160 }
9161 SAVE_IT (it_backup, *it, backup_data);
9162 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9163 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9164 op & MOVE_TO_POS);
9165 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9166 line_height = it->max_ascent + it->max_descent;
9167 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9168
9169 if (to_y >= it->current_y
9170 && to_y < it->current_y + line_height)
9171 {
9172 /* If TO_Y is in this line and TO_X was reached
9173 above, we scanned too far. We have to restore
9174 IT's settings to the ones before skipping. But
9175 keep the more accurate values of max_ascent and
9176 max_descent we've found while skipping the rest
9177 of the line, for the sake of callers, such as
9178 pos_visible_p, that need to know the line
9179 height. */
9180 int max_ascent = it->max_ascent;
9181 int max_descent = it->max_descent;
9182
9183 RESTORE_IT (it, &it_backup, backup_data);
9184 it->max_ascent = max_ascent;
9185 it->max_descent = max_descent;
9186 reached = 6;
9187 }
9188 else
9189 {
9190 skip = skip2;
9191 if (skip == MOVE_POS_MATCH_OR_ZV)
9192 reached = 7;
9193 }
9194 }
9195 else
9196 {
9197 /* Check whether TO_Y is in this line. */
9198 line_height = it->max_ascent + it->max_descent;
9199 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9200
9201 if (to_y >= it->current_y
9202 && to_y < it->current_y + line_height)
9203 {
9204 if (to_y > it->current_y)
9205 max_current_x = max (it->current_x, max_current_x);
9206
9207 /* When word-wrap is on, TO_X may lie past the end
9208 of a wrapped line. Then it->current is the
9209 character on the next line, so backtrack to the
9210 space before the wrap point. */
9211 if (skip == MOVE_LINE_CONTINUED
9212 && it->line_wrap == WORD_WRAP)
9213 {
9214 int prev_x = max (it->current_x - 1, 0);
9215 RESTORE_IT (it, &it_backup, backup_data);
9216 skip = move_it_in_display_line_to
9217 (it, -1, prev_x, MOVE_TO_X);
9218 }
9219
9220 reached = 6;
9221 }
9222 }
9223
9224 if (reached)
9225 {
9226 max_current_x = max (it->current_x, max_current_x);
9227 break;
9228 }
9229 }
9230 else if (BUFFERP (it->object)
9231 && (it->method == GET_FROM_BUFFER
9232 || it->method == GET_FROM_STRETCH)
9233 && IT_CHARPOS (*it) >= to_charpos
9234 /* Under bidi iteration, a call to set_iterator_to_next
9235 can scan far beyond to_charpos if the initial
9236 portion of the next line needs to be reordered. In
9237 that case, give move_it_in_display_line_to another
9238 chance below. */
9239 && !(it->bidi_p
9240 && it->bidi_it.scan_dir == -1))
9241 skip = MOVE_POS_MATCH_OR_ZV;
9242 else
9243 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9244
9245 switch (skip)
9246 {
9247 case MOVE_POS_MATCH_OR_ZV:
9248 max_current_x = max (it->current_x, max_current_x);
9249 reached = 8;
9250 goto out;
9251
9252 case MOVE_NEWLINE_OR_CR:
9253 max_current_x = max (it->current_x, max_current_x);
9254 set_iterator_to_next (it, true);
9255 it->continuation_lines_width = 0;
9256 break;
9257
9258 case MOVE_LINE_TRUNCATED:
9259 max_current_x = it->last_visible_x;
9260 it->continuation_lines_width = 0;
9261 reseat_at_next_visible_line_start (it, false);
9262 if ((op & MOVE_TO_POS) != 0
9263 && IT_CHARPOS (*it) > to_charpos)
9264 {
9265 reached = 9;
9266 goto out;
9267 }
9268 break;
9269
9270 case MOVE_LINE_CONTINUED:
9271 max_current_x = it->last_visible_x;
9272 /* For continued lines ending in a tab, some of the glyphs
9273 associated with the tab are displayed on the current
9274 line. Since it->current_x does not include these glyphs,
9275 we use it->last_visible_x instead. */
9276 if (it->c == '\t')
9277 {
9278 it->continuation_lines_width += it->last_visible_x;
9279 /* When moving by vpos, ensure that the iterator really
9280 advances to the next line (bug#847, bug#969). Fixme:
9281 do we need to do this in other circumstances? */
9282 if (it->current_x != it->last_visible_x
9283 && (op & MOVE_TO_VPOS)
9284 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9285 {
9286 line_start_x = it->current_x + it->pixel_width
9287 - it->last_visible_x;
9288 if (FRAME_WINDOW_P (it->f))
9289 {
9290 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9291 struct font *face_font = face->font;
9292
9293 /* When display_line produces a continued line
9294 that ends in a TAB, it skips a tab stop that
9295 is closer than the font's space character
9296 width (see x_produce_glyphs where it produces
9297 the stretch glyph which represents a TAB).
9298 We need to reproduce the same logic here. */
9299 eassert (face_font);
9300 if (face_font)
9301 {
9302 if (line_start_x < face_font->space_width)
9303 line_start_x
9304 += it->tab_width * face_font->space_width;
9305 }
9306 }
9307 set_iterator_to_next (it, false);
9308 }
9309 }
9310 else
9311 it->continuation_lines_width += it->current_x;
9312 break;
9313
9314 default:
9315 emacs_abort ();
9316 }
9317
9318 /* Reset/increment for the next run. */
9319 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9320 it->current_x = line_start_x;
9321 line_start_x = 0;
9322 it->hpos = 0;
9323 it->current_y += it->max_ascent + it->max_descent;
9324 ++it->vpos;
9325 last_height = it->max_ascent + it->max_descent;
9326 it->max_ascent = it->max_descent = 0;
9327 }
9328
9329 out:
9330
9331 /* On text terminals, we may stop at the end of a line in the middle
9332 of a multi-character glyph. If the glyph itself is continued,
9333 i.e. it is actually displayed on the next line, don't treat this
9334 stopping point as valid; move to the next line instead (unless
9335 that brings us offscreen). */
9336 if (!FRAME_WINDOW_P (it->f)
9337 && op & MOVE_TO_POS
9338 && IT_CHARPOS (*it) == to_charpos
9339 && it->what == IT_CHARACTER
9340 && it->nglyphs > 1
9341 && it->line_wrap == WINDOW_WRAP
9342 && it->current_x == it->last_visible_x - 1
9343 && it->c != '\n'
9344 && it->c != '\t'
9345 && it->w->window_end_valid
9346 && it->vpos < it->w->window_end_vpos)
9347 {
9348 it->continuation_lines_width += it->current_x;
9349 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9350 it->current_y += it->max_ascent + it->max_descent;
9351 ++it->vpos;
9352 last_height = it->max_ascent + it->max_descent;
9353 }
9354
9355 if (backup_data)
9356 bidi_unshelve_cache (backup_data, true);
9357
9358 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9359
9360 return max_current_x;
9361 }
9362
9363
9364 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9365
9366 If DY > 0, move IT backward at least that many pixels. DY = 0
9367 means move IT backward to the preceding line start or BEGV. This
9368 function may move over more than DY pixels if IT->current_y - DY
9369 ends up in the middle of a line; in this case IT->current_y will be
9370 set to the top of the line moved to. */
9371
9372 void
9373 move_it_vertically_backward (struct it *it, int dy)
9374 {
9375 int nlines, h;
9376 struct it it2, it3;
9377 void *it2data = NULL, *it3data = NULL;
9378 ptrdiff_t start_pos;
9379 int nchars_per_row
9380 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9381 ptrdiff_t pos_limit;
9382
9383 move_further_back:
9384 eassert (dy >= 0);
9385
9386 start_pos = IT_CHARPOS (*it);
9387
9388 /* Estimate how many newlines we must move back. */
9389 nlines = max (1, dy / default_line_pixel_height (it->w));
9390 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9391 pos_limit = BEGV;
9392 else
9393 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9394
9395 /* Set the iterator's position that many lines back. But don't go
9396 back more than NLINES full screen lines -- this wins a day with
9397 buffers which have very long lines. */
9398 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9399 back_to_previous_visible_line_start (it);
9400
9401 /* Reseat the iterator here. When moving backward, we don't want
9402 reseat to skip forward over invisible text, set up the iterator
9403 to deliver from overlay strings at the new position etc. So,
9404 use reseat_1 here. */
9405 reseat_1 (it, it->current.pos, true);
9406
9407 /* We are now surely at a line start. */
9408 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9409 reordering is in effect. */
9410 it->continuation_lines_width = 0;
9411
9412 /* Move forward and see what y-distance we moved. First move to the
9413 start of the next line so that we get its height. We need this
9414 height to be able to tell whether we reached the specified
9415 y-distance. */
9416 SAVE_IT (it2, *it, it2data);
9417 it2.max_ascent = it2.max_descent = 0;
9418 do
9419 {
9420 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9421 MOVE_TO_POS | MOVE_TO_VPOS);
9422 }
9423 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9424 /* If we are in a display string which starts at START_POS,
9425 and that display string includes a newline, and we are
9426 right after that newline (i.e. at the beginning of a
9427 display line), exit the loop, because otherwise we will
9428 infloop, since move_it_to will see that it is already at
9429 START_POS and will not move. */
9430 || (it2.method == GET_FROM_STRING
9431 && IT_CHARPOS (it2) == start_pos
9432 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9433 eassert (IT_CHARPOS (*it) >= BEGV);
9434 SAVE_IT (it3, it2, it3data);
9435
9436 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9437 eassert (IT_CHARPOS (*it) >= BEGV);
9438 /* H is the actual vertical distance from the position in *IT
9439 and the starting position. */
9440 h = it2.current_y - it->current_y;
9441 /* NLINES is the distance in number of lines. */
9442 nlines = it2.vpos - it->vpos;
9443
9444 /* Correct IT's y and vpos position
9445 so that they are relative to the starting point. */
9446 it->vpos -= nlines;
9447 it->current_y -= h;
9448
9449 if (dy == 0)
9450 {
9451 /* DY == 0 means move to the start of the screen line. The
9452 value of nlines is > 0 if continuation lines were involved,
9453 or if the original IT position was at start of a line. */
9454 RESTORE_IT (it, it, it2data);
9455 if (nlines > 0)
9456 move_it_by_lines (it, nlines);
9457 /* The above code moves us to some position NLINES down,
9458 usually to its first glyph (leftmost in an L2R line), but
9459 that's not necessarily the start of the line, under bidi
9460 reordering. We want to get to the character position
9461 that is immediately after the newline of the previous
9462 line. */
9463 if (it->bidi_p
9464 && !it->continuation_lines_width
9465 && !STRINGP (it->string)
9466 && IT_CHARPOS (*it) > BEGV
9467 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9468 {
9469 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9470
9471 DEC_BOTH (cp, bp);
9472 cp = find_newline_no_quit (cp, bp, -1, NULL);
9473 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9474 }
9475 bidi_unshelve_cache (it3data, true);
9476 }
9477 else
9478 {
9479 /* The y-position we try to reach, relative to *IT.
9480 Note that H has been subtracted in front of the if-statement. */
9481 int target_y = it->current_y + h - dy;
9482 int y0 = it3.current_y;
9483 int y1;
9484 int line_height;
9485
9486 RESTORE_IT (&it3, &it3, it3data);
9487 y1 = line_bottom_y (&it3);
9488 line_height = y1 - y0;
9489 RESTORE_IT (it, it, it2data);
9490 /* If we did not reach target_y, try to move further backward if
9491 we can. If we moved too far backward, try to move forward. */
9492 if (target_y < it->current_y
9493 /* This is heuristic. In a window that's 3 lines high, with
9494 a line height of 13 pixels each, recentering with point
9495 on the bottom line will try to move -39/2 = 19 pixels
9496 backward. Try to avoid moving into the first line. */
9497 && (it->current_y - target_y
9498 > min (window_box_height (it->w), line_height * 2 / 3))
9499 && IT_CHARPOS (*it) > BEGV)
9500 {
9501 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9502 target_y - it->current_y));
9503 dy = it->current_y - target_y;
9504 goto move_further_back;
9505 }
9506 else if (target_y >= it->current_y + line_height
9507 && IT_CHARPOS (*it) < ZV)
9508 {
9509 /* Should move forward by at least one line, maybe more.
9510
9511 Note: Calling move_it_by_lines can be expensive on
9512 terminal frames, where compute_motion is used (via
9513 vmotion) to do the job, when there are very long lines
9514 and truncate-lines is nil. That's the reason for
9515 treating terminal frames specially here. */
9516
9517 if (!FRAME_WINDOW_P (it->f))
9518 move_it_vertically (it, target_y - it->current_y);
9519 else
9520 {
9521 do
9522 {
9523 move_it_by_lines (it, 1);
9524 }
9525 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9526 }
9527 }
9528 }
9529 }
9530
9531
9532 /* Move IT by a specified amount of pixel lines DY. DY negative means
9533 move backwards. DY = 0 means move to start of screen line. At the
9534 end, IT will be on the start of a screen line. */
9535
9536 void
9537 move_it_vertically (struct it *it, int dy)
9538 {
9539 if (dy <= 0)
9540 move_it_vertically_backward (it, -dy);
9541 else
9542 {
9543 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9544 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9545 MOVE_TO_POS | MOVE_TO_Y);
9546 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9547
9548 /* If buffer ends in ZV without a newline, move to the start of
9549 the line to satisfy the post-condition. */
9550 if (IT_CHARPOS (*it) == ZV
9551 && ZV > BEGV
9552 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9553 move_it_by_lines (it, 0);
9554 }
9555 }
9556
9557
9558 /* Move iterator IT past the end of the text line it is in. */
9559
9560 void
9561 move_it_past_eol (struct it *it)
9562 {
9563 enum move_it_result rc;
9564
9565 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9566 if (rc == MOVE_NEWLINE_OR_CR)
9567 set_iterator_to_next (it, false);
9568 }
9569
9570
9571 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9572 negative means move up. DVPOS == 0 means move to the start of the
9573 screen line.
9574
9575 Optimization idea: If we would know that IT->f doesn't use
9576 a face with proportional font, we could be faster for
9577 truncate-lines nil. */
9578
9579 void
9580 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9581 {
9582
9583 /* The commented-out optimization uses vmotion on terminals. This
9584 gives bad results, because elements like it->what, on which
9585 callers such as pos_visible_p rely, aren't updated. */
9586 /* struct position pos;
9587 if (!FRAME_WINDOW_P (it->f))
9588 {
9589 struct text_pos textpos;
9590
9591 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9592 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9593 reseat (it, textpos, true);
9594 it->vpos += pos.vpos;
9595 it->current_y += pos.vpos;
9596 }
9597 else */
9598
9599 if (dvpos == 0)
9600 {
9601 /* DVPOS == 0 means move to the start of the screen line. */
9602 move_it_vertically_backward (it, 0);
9603 /* Let next call to line_bottom_y calculate real line height. */
9604 last_height = 0;
9605 }
9606 else if (dvpos > 0)
9607 {
9608 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9609 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9610 {
9611 /* Only move to the next buffer position if we ended up in a
9612 string from display property, not in an overlay string
9613 (before-string or after-string). That is because the
9614 latter don't conceal the underlying buffer position, so
9615 we can ask to move the iterator to the exact position we
9616 are interested in. Note that, even if we are already at
9617 IT_CHARPOS (*it), the call below is not a no-op, as it
9618 will detect that we are at the end of the string, pop the
9619 iterator, and compute it->current_x and it->hpos
9620 correctly. */
9621 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9622 -1, -1, -1, MOVE_TO_POS);
9623 }
9624 }
9625 else
9626 {
9627 struct it it2;
9628 void *it2data = NULL;
9629 ptrdiff_t start_charpos, i;
9630 int nchars_per_row
9631 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9632 bool hit_pos_limit = false;
9633 ptrdiff_t pos_limit;
9634
9635 /* Start at the beginning of the screen line containing IT's
9636 position. This may actually move vertically backwards,
9637 in case of overlays, so adjust dvpos accordingly. */
9638 dvpos += it->vpos;
9639 move_it_vertically_backward (it, 0);
9640 dvpos -= it->vpos;
9641
9642 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9643 screen lines, and reseat the iterator there. */
9644 start_charpos = IT_CHARPOS (*it);
9645 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9646 pos_limit = BEGV;
9647 else
9648 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9649
9650 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9651 back_to_previous_visible_line_start (it);
9652 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9653 hit_pos_limit = true;
9654 reseat (it, it->current.pos, true);
9655
9656 /* Move further back if we end up in a string or an image. */
9657 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9658 {
9659 /* First try to move to start of display line. */
9660 dvpos += it->vpos;
9661 move_it_vertically_backward (it, 0);
9662 dvpos -= it->vpos;
9663 if (IT_POS_VALID_AFTER_MOVE_P (it))
9664 break;
9665 /* If start of line is still in string or image,
9666 move further back. */
9667 back_to_previous_visible_line_start (it);
9668 reseat (it, it->current.pos, true);
9669 dvpos--;
9670 }
9671
9672 it->current_x = it->hpos = 0;
9673
9674 /* Above call may have moved too far if continuation lines
9675 are involved. Scan forward and see if it did. */
9676 SAVE_IT (it2, *it, it2data);
9677 it2.vpos = it2.current_y = 0;
9678 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9679 it->vpos -= it2.vpos;
9680 it->current_y -= it2.current_y;
9681 it->current_x = it->hpos = 0;
9682
9683 /* If we moved too far back, move IT some lines forward. */
9684 if (it2.vpos > -dvpos)
9685 {
9686 int delta = it2.vpos + dvpos;
9687
9688 RESTORE_IT (&it2, &it2, it2data);
9689 SAVE_IT (it2, *it, it2data);
9690 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9691 /* Move back again if we got too far ahead. */
9692 if (IT_CHARPOS (*it) >= start_charpos)
9693 RESTORE_IT (it, &it2, it2data);
9694 else
9695 bidi_unshelve_cache (it2data, true);
9696 }
9697 else if (hit_pos_limit && pos_limit > BEGV
9698 && dvpos < 0 && it2.vpos < -dvpos)
9699 {
9700 /* If we hit the limit, but still didn't make it far enough
9701 back, that means there's a display string with a newline
9702 covering a large chunk of text, and that caused
9703 back_to_previous_visible_line_start try to go too far.
9704 Punish those who commit such atrocities by going back
9705 until we've reached DVPOS, after lifting the limit, which
9706 could make it slow for very long lines. "If it hurts,
9707 don't do that!" */
9708 dvpos += it2.vpos;
9709 RESTORE_IT (it, it, it2data);
9710 for (i = -dvpos; i > 0; --i)
9711 {
9712 back_to_previous_visible_line_start (it);
9713 it->vpos--;
9714 }
9715 reseat_1 (it, it->current.pos, true);
9716 }
9717 else
9718 RESTORE_IT (it, it, it2data);
9719 }
9720 }
9721
9722 /* Return true if IT points into the middle of a display vector. */
9723
9724 bool
9725 in_display_vector_p (struct it *it)
9726 {
9727 return (it->method == GET_FROM_DISPLAY_VECTOR
9728 && it->current.dpvec_index > 0
9729 && it->dpvec + it->current.dpvec_index != it->dpend);
9730 }
9731
9732 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9733 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9734 WINDOW must be a live window and defaults to the selected one. The
9735 return value is a cons of the maximum pixel-width of any text line and
9736 the maximum pixel-height of all text lines.
9737
9738 The optional argument FROM, if non-nil, specifies the first text
9739 position and defaults to the minimum accessible position of the buffer.
9740 If FROM is t, use the minimum accessible position that is not a newline
9741 character. TO, if non-nil, specifies the last text position and
9742 defaults to the maximum accessible position of the buffer. If TO is t,
9743 use the maximum accessible position that is not a newline character.
9744
9745 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9746 width that can be returned. X-LIMIT nil or omitted, means to use the
9747 pixel-width of WINDOW's body; use this if you do not intend to change
9748 the width of WINDOW. Use the maximum width WINDOW may assume if you
9749 intend to change WINDOW's width. In any case, text whose x-coordinate
9750 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9751 can take some time, it's always a good idea to make this argument as
9752 small as possible; in particular, if the buffer contains long lines that
9753 shall be truncated anyway.
9754
9755 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9756 height that can be returned. Text lines whose y-coordinate is beyond
9757 Y-LIMIT are ignored. Since calculating the text height of a large
9758 buffer can take some time, it makes sense to specify this argument if
9759 the size of the buffer is unknown.
9760
9761 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9762 include the height of the mode- or header-line of WINDOW in the return
9763 value. If it is either the symbol `mode-line' or `header-line', include
9764 only the height of that line, if present, in the return value. If t,
9765 include the height of both, if present, in the return value. */)
9766 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9767 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9768 {
9769 struct window *w = decode_live_window (window);
9770 Lisp_Object buffer = w->contents;
9771 struct buffer *b;
9772 struct it it;
9773 struct buffer *old_b = NULL;
9774 ptrdiff_t start, end, pos;
9775 struct text_pos startp;
9776 void *itdata = NULL;
9777 int c, max_y = -1, x = 0, y = 0;
9778
9779 CHECK_BUFFER (buffer);
9780 b = XBUFFER (buffer);
9781
9782 if (b != current_buffer)
9783 {
9784 old_b = current_buffer;
9785 set_buffer_internal (b);
9786 }
9787
9788 if (NILP (from))
9789 start = BEGV;
9790 else if (EQ (from, Qt))
9791 {
9792 start = pos = BEGV;
9793 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9794 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9795 start = pos;
9796 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9797 start = pos;
9798 }
9799 else
9800 {
9801 CHECK_NUMBER_COERCE_MARKER (from);
9802 start = min (max (XINT (from), BEGV), ZV);
9803 }
9804
9805 if (NILP (to))
9806 end = ZV;
9807 else if (EQ (to, Qt))
9808 {
9809 end = pos = ZV;
9810 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9811 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9812 end = pos;
9813 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9814 end = pos;
9815 }
9816 else
9817 {
9818 CHECK_NUMBER_COERCE_MARKER (to);
9819 end = max (start, min (XINT (to), ZV));
9820 }
9821
9822 if (!NILP (y_limit))
9823 {
9824 CHECK_NUMBER (y_limit);
9825 max_y = min (XINT (y_limit), INT_MAX);
9826 }
9827
9828 itdata = bidi_shelve_cache ();
9829 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9830 start_display (&it, w, startp);
9831
9832 if (NILP (x_limit))
9833 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9834 else
9835 {
9836 CHECK_NUMBER (x_limit);
9837 it.last_visible_x = min (XINT (x_limit), INFINITY);
9838 /* Actually, we never want move_it_to stop at to_x. But to make
9839 sure that move_it_in_display_line_to always moves far enough,
9840 we set it to INT_MAX and specify MOVE_TO_X. */
9841 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9842 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9843 }
9844
9845 y = it.current_y + it.max_ascent + it.max_descent;
9846
9847 if (!EQ (mode_and_header_line, Qheader_line)
9848 && !EQ (mode_and_header_line, Qt))
9849 /* Do not count the header-line which was counted automatically by
9850 start_display. */
9851 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9852
9853 if (EQ (mode_and_header_line, Qmode_line)
9854 || EQ (mode_and_header_line, Qt))
9855 /* Do count the mode-line which is not included automatically by
9856 start_display. */
9857 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9858
9859 bidi_unshelve_cache (itdata, false);
9860
9861 if (old_b)
9862 set_buffer_internal (old_b);
9863
9864 return Fcons (make_number (x), make_number (y));
9865 }
9866 \f
9867 /***********************************************************************
9868 Messages
9869 ***********************************************************************/
9870
9871 /* Return the number of arguments the format string FORMAT needs. */
9872
9873 static ptrdiff_t
9874 format_nargs (char const *format)
9875 {
9876 ptrdiff_t nargs = 0;
9877 for (char const *p = format; (p = strchr (p, '%')); p++)
9878 if (p[1] == '%')
9879 p++;
9880 else
9881 nargs++;
9882 return nargs;
9883 }
9884
9885 /* Add a message with format string FORMAT and formatted arguments
9886 to *Messages*. */
9887
9888 void
9889 add_to_log (const char *format, ...)
9890 {
9891 va_list ap;
9892 va_start (ap, format);
9893 vadd_to_log (format, ap);
9894 va_end (ap);
9895 }
9896
9897 void
9898 vadd_to_log (char const *format, va_list ap)
9899 {
9900 ptrdiff_t form_nargs = format_nargs (format);
9901 ptrdiff_t nargs = 1 + form_nargs;
9902 Lisp_Object args[10];
9903 eassert (nargs <= ARRAYELTS (args));
9904 AUTO_STRING (args0, format);
9905 args[0] = args0;
9906 for (ptrdiff_t i = 1; i <= nargs; i++)
9907 args[i] = va_arg (ap, Lisp_Object);
9908 Lisp_Object msg = Qnil;
9909 msg = Fformat_message (nargs, args);
9910
9911 ptrdiff_t len = SBYTES (msg) + 1;
9912 USE_SAFE_ALLOCA;
9913 char *buffer = SAFE_ALLOCA (len);
9914 memcpy (buffer, SDATA (msg), len);
9915
9916 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
9917 SAFE_FREE ();
9918 }
9919
9920
9921 /* Output a newline in the *Messages* buffer if "needs" one. */
9922
9923 void
9924 message_log_maybe_newline (void)
9925 {
9926 if (message_log_need_newline)
9927 message_dolog ("", 0, true, false);
9928 }
9929
9930
9931 /* Add a string M of length NBYTES to the message log, optionally
9932 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9933 true, means interpret the contents of M as multibyte. This
9934 function calls low-level routines in order to bypass text property
9935 hooks, etc. which might not be safe to run.
9936
9937 This may GC (insert may run before/after change hooks),
9938 so the buffer M must NOT point to a Lisp string. */
9939
9940 void
9941 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9942 {
9943 const unsigned char *msg = (const unsigned char *) m;
9944
9945 if (!NILP (Vmemory_full))
9946 return;
9947
9948 if (!NILP (Vmessage_log_max))
9949 {
9950 struct buffer *oldbuf;
9951 Lisp_Object oldpoint, oldbegv, oldzv;
9952 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9953 ptrdiff_t point_at_end = 0;
9954 ptrdiff_t zv_at_end = 0;
9955 Lisp_Object old_deactivate_mark;
9956
9957 old_deactivate_mark = Vdeactivate_mark;
9958 oldbuf = current_buffer;
9959
9960 /* Ensure the Messages buffer exists, and switch to it.
9961 If we created it, set the major-mode. */
9962 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
9963 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9964 if (newbuffer
9965 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9966 call0 (intern ("messages-buffer-mode"));
9967
9968 bset_undo_list (current_buffer, Qt);
9969 bset_cache_long_scans (current_buffer, Qnil);
9970
9971 oldpoint = message_dolog_marker1;
9972 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9973 oldbegv = message_dolog_marker2;
9974 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9975 oldzv = message_dolog_marker3;
9976 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9977
9978 if (PT == Z)
9979 point_at_end = 1;
9980 if (ZV == Z)
9981 zv_at_end = 1;
9982
9983 BEGV = BEG;
9984 BEGV_BYTE = BEG_BYTE;
9985 ZV = Z;
9986 ZV_BYTE = Z_BYTE;
9987 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9988
9989 /* Insert the string--maybe converting multibyte to single byte
9990 or vice versa, so that all the text fits the buffer. */
9991 if (multibyte
9992 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9993 {
9994 ptrdiff_t i;
9995 int c, char_bytes;
9996 char work[1];
9997
9998 /* Convert a multibyte string to single-byte
9999 for the *Message* buffer. */
10000 for (i = 0; i < nbytes; i += char_bytes)
10001 {
10002 c = string_char_and_length (msg + i, &char_bytes);
10003 work[0] = CHAR_TO_BYTE8 (c);
10004 insert_1_both (work, 1, 1, true, false, false);
10005 }
10006 }
10007 else if (! multibyte
10008 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10009 {
10010 ptrdiff_t i;
10011 int c, char_bytes;
10012 unsigned char str[MAX_MULTIBYTE_LENGTH];
10013 /* Convert a single-byte string to multibyte
10014 for the *Message* buffer. */
10015 for (i = 0; i < nbytes; i++)
10016 {
10017 c = msg[i];
10018 MAKE_CHAR_MULTIBYTE (c);
10019 char_bytes = CHAR_STRING (c, str);
10020 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10021 }
10022 }
10023 else if (nbytes)
10024 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10025 true, false, false);
10026
10027 if (nlflag)
10028 {
10029 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10030 printmax_t dups;
10031
10032 insert_1_both ("\n", 1, 1, true, false, false);
10033
10034 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10035 this_bol = PT;
10036 this_bol_byte = PT_BYTE;
10037
10038 /* See if this line duplicates the previous one.
10039 If so, combine duplicates. */
10040 if (this_bol > BEG)
10041 {
10042 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10043 prev_bol = PT;
10044 prev_bol_byte = PT_BYTE;
10045
10046 dups = message_log_check_duplicate (prev_bol_byte,
10047 this_bol_byte);
10048 if (dups)
10049 {
10050 del_range_both (prev_bol, prev_bol_byte,
10051 this_bol, this_bol_byte, false);
10052 if (dups > 1)
10053 {
10054 char dupstr[sizeof " [ times]"
10055 + INT_STRLEN_BOUND (printmax_t)];
10056
10057 /* If you change this format, don't forget to also
10058 change message_log_check_duplicate. */
10059 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10060 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10061 insert_1_both (dupstr, duplen, duplen,
10062 true, false, true);
10063 }
10064 }
10065 }
10066
10067 /* If we have more than the desired maximum number of lines
10068 in the *Messages* buffer now, delete the oldest ones.
10069 This is safe because we don't have undo in this buffer. */
10070
10071 if (NATNUMP (Vmessage_log_max))
10072 {
10073 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10074 -XFASTINT (Vmessage_log_max) - 1, false);
10075 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10076 }
10077 }
10078 BEGV = marker_position (oldbegv);
10079 BEGV_BYTE = marker_byte_position (oldbegv);
10080
10081 if (zv_at_end)
10082 {
10083 ZV = Z;
10084 ZV_BYTE = Z_BYTE;
10085 }
10086 else
10087 {
10088 ZV = marker_position (oldzv);
10089 ZV_BYTE = marker_byte_position (oldzv);
10090 }
10091
10092 if (point_at_end)
10093 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10094 else
10095 /* We can't do Fgoto_char (oldpoint) because it will run some
10096 Lisp code. */
10097 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10098 marker_byte_position (oldpoint));
10099
10100 unchain_marker (XMARKER (oldpoint));
10101 unchain_marker (XMARKER (oldbegv));
10102 unchain_marker (XMARKER (oldzv));
10103
10104 /* We called insert_1_both above with its 5th argument (PREPARE)
10105 false, which prevents insert_1_both from calling
10106 prepare_to_modify_buffer, which in turns prevents us from
10107 incrementing windows_or_buffers_changed even if *Messages* is
10108 shown in some window. So we must manually set
10109 windows_or_buffers_changed here to make up for that. */
10110 windows_or_buffers_changed = old_windows_or_buffers_changed;
10111 bset_redisplay (current_buffer);
10112
10113 set_buffer_internal (oldbuf);
10114
10115 message_log_need_newline = !nlflag;
10116 Vdeactivate_mark = old_deactivate_mark;
10117 }
10118 }
10119
10120
10121 /* We are at the end of the buffer after just having inserted a newline.
10122 (Note: We depend on the fact we won't be crossing the gap.)
10123 Check to see if the most recent message looks a lot like the previous one.
10124 Return 0 if different, 1 if the new one should just replace it, or a
10125 value N > 1 if we should also append " [N times]". */
10126
10127 static intmax_t
10128 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10129 {
10130 ptrdiff_t i;
10131 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10132 bool seen_dots = false;
10133 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10134 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10135
10136 for (i = 0; i < len; i++)
10137 {
10138 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10139 seen_dots = true;
10140 if (p1[i] != p2[i])
10141 return seen_dots;
10142 }
10143 p1 += len;
10144 if (*p1 == '\n')
10145 return 2;
10146 if (*p1++ == ' ' && *p1++ == '[')
10147 {
10148 char *pend;
10149 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10150 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10151 return n + 1;
10152 }
10153 return 0;
10154 }
10155 \f
10156
10157 /* Display an echo area message M with a specified length of NBYTES
10158 bytes. The string may include null characters. If M is not a
10159 string, clear out any existing message, and let the mini-buffer
10160 text show through.
10161
10162 This function cancels echoing. */
10163
10164 void
10165 message3 (Lisp_Object m)
10166 {
10167 clear_message (true, true);
10168 cancel_echoing ();
10169
10170 /* First flush out any partial line written with print. */
10171 message_log_maybe_newline ();
10172 if (STRINGP (m))
10173 {
10174 ptrdiff_t nbytes = SBYTES (m);
10175 bool multibyte = STRING_MULTIBYTE (m);
10176 char *buffer;
10177 USE_SAFE_ALLOCA;
10178 SAFE_ALLOCA_STRING (buffer, m);
10179 message_dolog (buffer, nbytes, true, multibyte);
10180 SAFE_FREE ();
10181 }
10182 if (! inhibit_message)
10183 message3_nolog (m);
10184 }
10185
10186 /* Log the message M to stderr. Log an empty line if M is not a string. */
10187
10188 static void
10189 message_to_stderr (Lisp_Object m)
10190 {
10191 if (noninteractive_need_newline)
10192 {
10193 noninteractive_need_newline = false;
10194 fputc ('\n', stderr);
10195 }
10196 if (STRINGP (m))
10197 {
10198 Lisp_Object s = ENCODE_SYSTEM (m);
10199 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10200 }
10201 if (!cursor_in_echo_area)
10202 fputc ('\n', stderr);
10203 fflush (stderr);
10204 }
10205
10206 /* The non-logging version of message3.
10207 This does not cancel echoing, because it is used for echoing.
10208 Perhaps we need to make a separate function for echoing
10209 and make this cancel echoing. */
10210
10211 void
10212 message3_nolog (Lisp_Object m)
10213 {
10214 struct frame *sf = SELECTED_FRAME ();
10215
10216 if (FRAME_INITIAL_P (sf))
10217 message_to_stderr (m);
10218 /* Error messages get reported properly by cmd_error, so this must be just an
10219 informative message; if the frame hasn't really been initialized yet, just
10220 toss it. */
10221 else if (INTERACTIVE && sf->glyphs_initialized_p)
10222 {
10223 /* Get the frame containing the mini-buffer
10224 that the selected frame is using. */
10225 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10226 Lisp_Object frame = XWINDOW (mini_window)->frame;
10227 struct frame *f = XFRAME (frame);
10228
10229 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10230 Fmake_frame_visible (frame);
10231
10232 if (STRINGP (m) && SCHARS (m) > 0)
10233 {
10234 set_message (m);
10235 if (minibuffer_auto_raise)
10236 Fraise_frame (frame);
10237 /* Assume we are not echoing.
10238 (If we are, echo_now will override this.) */
10239 echo_message_buffer = Qnil;
10240 }
10241 else
10242 clear_message (true, true);
10243
10244 do_pending_window_change (false);
10245 echo_area_display (true);
10246 do_pending_window_change (false);
10247 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10248 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10249 }
10250 }
10251
10252
10253 /* Display a null-terminated echo area message M. If M is 0, clear
10254 out any existing message, and let the mini-buffer text show through.
10255
10256 The buffer M must continue to exist until after the echo area gets
10257 cleared or some other message gets displayed there. Do not pass
10258 text that is stored in a Lisp string. Do not pass text in a buffer
10259 that was alloca'd. */
10260
10261 void
10262 message1 (const char *m)
10263 {
10264 message3 (m ? build_unibyte_string (m) : Qnil);
10265 }
10266
10267
10268 /* The non-logging counterpart of message1. */
10269
10270 void
10271 message1_nolog (const char *m)
10272 {
10273 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10274 }
10275
10276 /* Display a message M which contains a single %s
10277 which gets replaced with STRING. */
10278
10279 void
10280 message_with_string (const char *m, Lisp_Object string, bool log)
10281 {
10282 CHECK_STRING (string);
10283
10284 bool need_message;
10285 if (noninteractive)
10286 need_message = !!m;
10287 else if (!INTERACTIVE)
10288 need_message = false;
10289 else
10290 {
10291 /* The frame whose minibuffer we're going to display the message on.
10292 It may be larger than the selected frame, so we need
10293 to use its buffer, not the selected frame's buffer. */
10294 Lisp_Object mini_window;
10295 struct frame *f, *sf = SELECTED_FRAME ();
10296
10297 /* Get the frame containing the minibuffer
10298 that the selected frame is using. */
10299 mini_window = FRAME_MINIBUF_WINDOW (sf);
10300 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10301
10302 /* Error messages get reported properly by cmd_error, so this must be
10303 just an informative message; if the frame hasn't really been
10304 initialized yet, just toss it. */
10305 need_message = f->glyphs_initialized_p;
10306 }
10307
10308 if (need_message)
10309 {
10310 AUTO_STRING (fmt, m);
10311 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10312
10313 if (noninteractive)
10314 message_to_stderr (msg);
10315 else
10316 {
10317 if (log)
10318 message3 (msg);
10319 else
10320 message3_nolog (msg);
10321
10322 /* Print should start at the beginning of the message
10323 buffer next time. */
10324 message_buf_print = false;
10325 }
10326 }
10327 }
10328
10329
10330 /* Dump an informative message to the minibuf. If M is 0, clear out
10331 any existing message, and let the mini-buffer text show through.
10332
10333 The message must be safe ASCII and the format must not contain ` or
10334 '. If your message and format do not fit into this category,
10335 convert your arguments to Lisp objects and use Fmessage instead. */
10336
10337 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10338 vmessage (const char *m, va_list ap)
10339 {
10340 if (noninteractive)
10341 {
10342 if (m)
10343 {
10344 if (noninteractive_need_newline)
10345 putc ('\n', stderr);
10346 noninteractive_need_newline = false;
10347 vfprintf (stderr, m, ap);
10348 if (!cursor_in_echo_area)
10349 fprintf (stderr, "\n");
10350 fflush (stderr);
10351 }
10352 }
10353 else if (INTERACTIVE)
10354 {
10355 /* The frame whose mini-buffer we're going to display the message
10356 on. It may be larger than the selected frame, so we need to
10357 use its buffer, not the selected frame's buffer. */
10358 Lisp_Object mini_window;
10359 struct frame *f, *sf = SELECTED_FRAME ();
10360
10361 /* Get the frame containing the mini-buffer
10362 that the selected frame is using. */
10363 mini_window = FRAME_MINIBUF_WINDOW (sf);
10364 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10365
10366 /* Error messages get reported properly by cmd_error, so this must be
10367 just an informative message; if the frame hasn't really been
10368 initialized yet, just toss it. */
10369 if (f->glyphs_initialized_p)
10370 {
10371 if (m)
10372 {
10373 ptrdiff_t len;
10374 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10375 USE_SAFE_ALLOCA;
10376 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10377
10378 len = doprnt (message_buf, maxsize, m, 0, ap);
10379
10380 message3 (make_string (message_buf, len));
10381 SAFE_FREE ();
10382 }
10383 else
10384 message1 (0);
10385
10386 /* Print should start at the beginning of the message
10387 buffer next time. */
10388 message_buf_print = false;
10389 }
10390 }
10391 }
10392
10393 void
10394 message (const char *m, ...)
10395 {
10396 va_list ap;
10397 va_start (ap, m);
10398 vmessage (m, ap);
10399 va_end (ap);
10400 }
10401
10402
10403 /* Display the current message in the current mini-buffer. This is
10404 only called from error handlers in process.c, and is not time
10405 critical. */
10406
10407 void
10408 update_echo_area (void)
10409 {
10410 if (!NILP (echo_area_buffer[0]))
10411 {
10412 Lisp_Object string;
10413 string = Fcurrent_message ();
10414 message3 (string);
10415 }
10416 }
10417
10418
10419 /* Make sure echo area buffers in `echo_buffers' are live.
10420 If they aren't, make new ones. */
10421
10422 static void
10423 ensure_echo_area_buffers (void)
10424 {
10425 int i;
10426
10427 for (i = 0; i < 2; ++i)
10428 if (!BUFFERP (echo_buffer[i])
10429 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10430 {
10431 char name[30];
10432 Lisp_Object old_buffer;
10433 int j;
10434
10435 old_buffer = echo_buffer[i];
10436 echo_buffer[i] = Fget_buffer_create
10437 (make_formatted_string (name, " *Echo Area %d*", i));
10438 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10439 /* to force word wrap in echo area -
10440 it was decided to postpone this*/
10441 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10442
10443 for (j = 0; j < 2; ++j)
10444 if (EQ (old_buffer, echo_area_buffer[j]))
10445 echo_area_buffer[j] = echo_buffer[i];
10446 }
10447 }
10448
10449
10450 /* Call FN with args A1..A2 with either the current or last displayed
10451 echo_area_buffer as current buffer.
10452
10453 WHICH zero means use the current message buffer
10454 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10455 from echo_buffer[] and clear it.
10456
10457 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10458 suitable buffer from echo_buffer[] and clear it.
10459
10460 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10461 that the current message becomes the last displayed one, make
10462 choose a suitable buffer for echo_area_buffer[0], and clear it.
10463
10464 Value is what FN returns. */
10465
10466 static bool
10467 with_echo_area_buffer (struct window *w, int which,
10468 bool (*fn) (ptrdiff_t, Lisp_Object),
10469 ptrdiff_t a1, Lisp_Object a2)
10470 {
10471 Lisp_Object buffer;
10472 bool this_one, the_other, clear_buffer_p, rc;
10473 ptrdiff_t count = SPECPDL_INDEX ();
10474
10475 /* If buffers aren't live, make new ones. */
10476 ensure_echo_area_buffers ();
10477
10478 clear_buffer_p = false;
10479
10480 if (which == 0)
10481 this_one = false, the_other = true;
10482 else if (which > 0)
10483 this_one = true, the_other = false;
10484 else
10485 {
10486 this_one = false, the_other = true;
10487 clear_buffer_p = true;
10488
10489 /* We need a fresh one in case the current echo buffer equals
10490 the one containing the last displayed echo area message. */
10491 if (!NILP (echo_area_buffer[this_one])
10492 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10493 echo_area_buffer[this_one] = Qnil;
10494 }
10495
10496 /* Choose a suitable buffer from echo_buffer[] is we don't
10497 have one. */
10498 if (NILP (echo_area_buffer[this_one]))
10499 {
10500 echo_area_buffer[this_one]
10501 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10502 ? echo_buffer[the_other]
10503 : echo_buffer[this_one]);
10504 clear_buffer_p = true;
10505 }
10506
10507 buffer = echo_area_buffer[this_one];
10508
10509 /* Don't get confused by reusing the buffer used for echoing
10510 for a different purpose. */
10511 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10512 cancel_echoing ();
10513
10514 record_unwind_protect (unwind_with_echo_area_buffer,
10515 with_echo_area_buffer_unwind_data (w));
10516
10517 /* Make the echo area buffer current. Note that for display
10518 purposes, it is not necessary that the displayed window's buffer
10519 == current_buffer, except for text property lookup. So, let's
10520 only set that buffer temporarily here without doing a full
10521 Fset_window_buffer. We must also change w->pointm, though,
10522 because otherwise an assertions in unshow_buffer fails, and Emacs
10523 aborts. */
10524 set_buffer_internal_1 (XBUFFER (buffer));
10525 if (w)
10526 {
10527 wset_buffer (w, buffer);
10528 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10529 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10530 }
10531
10532 bset_undo_list (current_buffer, Qt);
10533 bset_read_only (current_buffer, Qnil);
10534 specbind (Qinhibit_read_only, Qt);
10535 specbind (Qinhibit_modification_hooks, Qt);
10536
10537 if (clear_buffer_p && Z > BEG)
10538 del_range (BEG, Z);
10539
10540 eassert (BEGV >= BEG);
10541 eassert (ZV <= Z && ZV >= BEGV);
10542
10543 rc = fn (a1, a2);
10544
10545 eassert (BEGV >= BEG);
10546 eassert (ZV <= Z && ZV >= BEGV);
10547
10548 unbind_to (count, Qnil);
10549 return rc;
10550 }
10551
10552
10553 /* Save state that should be preserved around the call to the function
10554 FN called in with_echo_area_buffer. */
10555
10556 static Lisp_Object
10557 with_echo_area_buffer_unwind_data (struct window *w)
10558 {
10559 int i = 0;
10560 Lisp_Object vector, tmp;
10561
10562 /* Reduce consing by keeping one vector in
10563 Vwith_echo_area_save_vector. */
10564 vector = Vwith_echo_area_save_vector;
10565 Vwith_echo_area_save_vector = Qnil;
10566
10567 if (NILP (vector))
10568 vector = Fmake_vector (make_number (11), Qnil);
10569
10570 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10571 ASET (vector, i, Vdeactivate_mark); ++i;
10572 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10573
10574 if (w)
10575 {
10576 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10577 ASET (vector, i, w->contents); ++i;
10578 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10579 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10580 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10581 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10582 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10583 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10584 }
10585 else
10586 {
10587 int end = i + 8;
10588 for (; i < end; ++i)
10589 ASET (vector, i, Qnil);
10590 }
10591
10592 eassert (i == ASIZE (vector));
10593 return vector;
10594 }
10595
10596
10597 /* Restore global state from VECTOR which was created by
10598 with_echo_area_buffer_unwind_data. */
10599
10600 static void
10601 unwind_with_echo_area_buffer (Lisp_Object vector)
10602 {
10603 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10604 Vdeactivate_mark = AREF (vector, 1);
10605 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10606
10607 if (WINDOWP (AREF (vector, 3)))
10608 {
10609 struct window *w;
10610 Lisp_Object buffer;
10611
10612 w = XWINDOW (AREF (vector, 3));
10613 buffer = AREF (vector, 4);
10614
10615 wset_buffer (w, buffer);
10616 set_marker_both (w->pointm, buffer,
10617 XFASTINT (AREF (vector, 5)),
10618 XFASTINT (AREF (vector, 6)));
10619 set_marker_both (w->old_pointm, buffer,
10620 XFASTINT (AREF (vector, 7)),
10621 XFASTINT (AREF (vector, 8)));
10622 set_marker_both (w->start, buffer,
10623 XFASTINT (AREF (vector, 9)),
10624 XFASTINT (AREF (vector, 10)));
10625 }
10626
10627 Vwith_echo_area_save_vector = vector;
10628 }
10629
10630
10631 /* Set up the echo area for use by print functions. MULTIBYTE_P
10632 means we will print multibyte. */
10633
10634 void
10635 setup_echo_area_for_printing (bool multibyte_p)
10636 {
10637 /* If we can't find an echo area any more, exit. */
10638 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10639 Fkill_emacs (Qnil);
10640
10641 ensure_echo_area_buffers ();
10642
10643 if (!message_buf_print)
10644 {
10645 /* A message has been output since the last time we printed.
10646 Choose a fresh echo area buffer. */
10647 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10648 echo_area_buffer[0] = echo_buffer[1];
10649 else
10650 echo_area_buffer[0] = echo_buffer[0];
10651
10652 /* Switch to that buffer and clear it. */
10653 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10654 bset_truncate_lines (current_buffer, Qnil);
10655
10656 if (Z > BEG)
10657 {
10658 ptrdiff_t count = SPECPDL_INDEX ();
10659 specbind (Qinhibit_read_only, Qt);
10660 /* Note that undo recording is always disabled. */
10661 del_range (BEG, Z);
10662 unbind_to (count, Qnil);
10663 }
10664 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10665
10666 /* Set up the buffer for the multibyteness we need. */
10667 if (multibyte_p
10668 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10669 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10670
10671 /* Raise the frame containing the echo area. */
10672 if (minibuffer_auto_raise)
10673 {
10674 struct frame *sf = SELECTED_FRAME ();
10675 Lisp_Object mini_window;
10676 mini_window = FRAME_MINIBUF_WINDOW (sf);
10677 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10678 }
10679
10680 message_log_maybe_newline ();
10681 message_buf_print = true;
10682 }
10683 else
10684 {
10685 if (NILP (echo_area_buffer[0]))
10686 {
10687 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10688 echo_area_buffer[0] = echo_buffer[1];
10689 else
10690 echo_area_buffer[0] = echo_buffer[0];
10691 }
10692
10693 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10694 {
10695 /* Someone switched buffers between print requests. */
10696 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10697 bset_truncate_lines (current_buffer, Qnil);
10698 }
10699 }
10700 }
10701
10702
10703 /* Display an echo area message in window W. Value is true if W's
10704 height is changed. If display_last_displayed_message_p,
10705 display the message that was last displayed, otherwise
10706 display the current message. */
10707
10708 static bool
10709 display_echo_area (struct window *w)
10710 {
10711 bool no_message_p, window_height_changed_p;
10712
10713 /* Temporarily disable garbage collections while displaying the echo
10714 area. This is done because a GC can print a message itself.
10715 That message would modify the echo area buffer's contents while a
10716 redisplay of the buffer is going on, and seriously confuse
10717 redisplay. */
10718 ptrdiff_t count = inhibit_garbage_collection ();
10719
10720 /* If there is no message, we must call display_echo_area_1
10721 nevertheless because it resizes the window. But we will have to
10722 reset the echo_area_buffer in question to nil at the end because
10723 with_echo_area_buffer will sets it to an empty buffer. */
10724 bool i = display_last_displayed_message_p;
10725 no_message_p = NILP (echo_area_buffer[i]);
10726
10727 window_height_changed_p
10728 = with_echo_area_buffer (w, display_last_displayed_message_p,
10729 display_echo_area_1,
10730 (intptr_t) w, Qnil);
10731
10732 if (no_message_p)
10733 echo_area_buffer[i] = Qnil;
10734
10735 unbind_to (count, Qnil);
10736 return window_height_changed_p;
10737 }
10738
10739
10740 /* Helper for display_echo_area. Display the current buffer which
10741 contains the current echo area message in window W, a mini-window,
10742 a pointer to which is passed in A1. A2..A4 are currently not used.
10743 Change the height of W so that all of the message is displayed.
10744 Value is true if height of W was changed. */
10745
10746 static bool
10747 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10748 {
10749 intptr_t i1 = a1;
10750 struct window *w = (struct window *) i1;
10751 Lisp_Object window;
10752 struct text_pos start;
10753
10754 /* We are about to enter redisplay without going through
10755 redisplay_internal, so we need to forget these faces by hand
10756 here. */
10757 forget_escape_and_glyphless_faces ();
10758
10759 /* Do this before displaying, so that we have a large enough glyph
10760 matrix for the display. If we can't get enough space for the
10761 whole text, display the last N lines. That works by setting w->start. */
10762 bool window_height_changed_p = resize_mini_window (w, false);
10763
10764 /* Use the starting position chosen by resize_mini_window. */
10765 SET_TEXT_POS_FROM_MARKER (start, w->start);
10766
10767 /* Display. */
10768 clear_glyph_matrix (w->desired_matrix);
10769 XSETWINDOW (window, w);
10770 try_window (window, start, 0);
10771
10772 return window_height_changed_p;
10773 }
10774
10775
10776 /* Resize the echo area window to exactly the size needed for the
10777 currently displayed message, if there is one. If a mini-buffer
10778 is active, don't shrink it. */
10779
10780 void
10781 resize_echo_area_exactly (void)
10782 {
10783 if (BUFFERP (echo_area_buffer[0])
10784 && WINDOWP (echo_area_window))
10785 {
10786 struct window *w = XWINDOW (echo_area_window);
10787 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10788 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10789 (intptr_t) w, resize_exactly);
10790 if (resized_p)
10791 {
10792 windows_or_buffers_changed = 42;
10793 update_mode_lines = 30;
10794 redisplay_internal ();
10795 }
10796 }
10797 }
10798
10799
10800 /* Callback function for with_echo_area_buffer, when used from
10801 resize_echo_area_exactly. A1 contains a pointer to the window to
10802 resize, EXACTLY non-nil means resize the mini-window exactly to the
10803 size of the text displayed. A3 and A4 are not used. Value is what
10804 resize_mini_window returns. */
10805
10806 static bool
10807 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10808 {
10809 intptr_t i1 = a1;
10810 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10811 }
10812
10813
10814 /* Resize mini-window W to fit the size of its contents. EXACT_P
10815 means size the window exactly to the size needed. Otherwise, it's
10816 only enlarged until W's buffer is empty.
10817
10818 Set W->start to the right place to begin display. If the whole
10819 contents fit, start at the beginning. Otherwise, start so as
10820 to make the end of the contents appear. This is particularly
10821 important for y-or-n-p, but seems desirable generally.
10822
10823 Value is true if the window height has been changed. */
10824
10825 bool
10826 resize_mini_window (struct window *w, bool exact_p)
10827 {
10828 struct frame *f = XFRAME (w->frame);
10829 bool window_height_changed_p = false;
10830
10831 eassert (MINI_WINDOW_P (w));
10832
10833 /* By default, start display at the beginning. */
10834 set_marker_both (w->start, w->contents,
10835 BUF_BEGV (XBUFFER (w->contents)),
10836 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10837
10838 /* Don't resize windows while redisplaying a window; it would
10839 confuse redisplay functions when the size of the window they are
10840 displaying changes from under them. Such a resizing can happen,
10841 for instance, when which-func prints a long message while
10842 we are running fontification-functions. We're running these
10843 functions with safe_call which binds inhibit-redisplay to t. */
10844 if (!NILP (Vinhibit_redisplay))
10845 return false;
10846
10847 /* Nil means don't try to resize. */
10848 if (NILP (Vresize_mini_windows)
10849 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10850 return false;
10851
10852 if (!FRAME_MINIBUF_ONLY_P (f))
10853 {
10854 struct it it;
10855 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10856 + WINDOW_PIXEL_HEIGHT (w));
10857 int unit = FRAME_LINE_HEIGHT (f);
10858 int height, max_height;
10859 struct text_pos start;
10860 struct buffer *old_current_buffer = NULL;
10861
10862 if (current_buffer != XBUFFER (w->contents))
10863 {
10864 old_current_buffer = current_buffer;
10865 set_buffer_internal (XBUFFER (w->contents));
10866 }
10867
10868 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10869
10870 /* Compute the max. number of lines specified by the user. */
10871 if (FLOATP (Vmax_mini_window_height))
10872 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10873 else if (INTEGERP (Vmax_mini_window_height))
10874 max_height = XINT (Vmax_mini_window_height) * unit;
10875 else
10876 max_height = total_height / 4;
10877
10878 /* Correct that max. height if it's bogus. */
10879 max_height = clip_to_bounds (unit, max_height, total_height);
10880
10881 /* Find out the height of the text in the window. */
10882 if (it.line_wrap == TRUNCATE)
10883 height = unit;
10884 else
10885 {
10886 last_height = 0;
10887 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10888 if (it.max_ascent == 0 && it.max_descent == 0)
10889 height = it.current_y + last_height;
10890 else
10891 height = it.current_y + it.max_ascent + it.max_descent;
10892 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10893 }
10894
10895 /* Compute a suitable window start. */
10896 if (height > max_height)
10897 {
10898 height = (max_height / unit) * unit;
10899 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10900 move_it_vertically_backward (&it, height - unit);
10901 start = it.current.pos;
10902 }
10903 else
10904 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10905 SET_MARKER_FROM_TEXT_POS (w->start, start);
10906
10907 if (EQ (Vresize_mini_windows, Qgrow_only))
10908 {
10909 /* Let it grow only, until we display an empty message, in which
10910 case the window shrinks again. */
10911 if (height > WINDOW_PIXEL_HEIGHT (w))
10912 {
10913 int old_height = WINDOW_PIXEL_HEIGHT (w);
10914
10915 FRAME_WINDOWS_FROZEN (f) = true;
10916 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10917 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10918 }
10919 else if (height < WINDOW_PIXEL_HEIGHT (w)
10920 && (exact_p || BEGV == ZV))
10921 {
10922 int old_height = WINDOW_PIXEL_HEIGHT (w);
10923
10924 FRAME_WINDOWS_FROZEN (f) = false;
10925 shrink_mini_window (w, true);
10926 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10927 }
10928 }
10929 else
10930 {
10931 /* Always resize to exact size needed. */
10932 if (height > WINDOW_PIXEL_HEIGHT (w))
10933 {
10934 int old_height = WINDOW_PIXEL_HEIGHT (w);
10935
10936 FRAME_WINDOWS_FROZEN (f) = true;
10937 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10938 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10939 }
10940 else if (height < WINDOW_PIXEL_HEIGHT (w))
10941 {
10942 int old_height = WINDOW_PIXEL_HEIGHT (w);
10943
10944 FRAME_WINDOWS_FROZEN (f) = false;
10945 shrink_mini_window (w, true);
10946
10947 if (height)
10948 {
10949 FRAME_WINDOWS_FROZEN (f) = true;
10950 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
10951 }
10952
10953 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10954 }
10955 }
10956
10957 if (old_current_buffer)
10958 set_buffer_internal (old_current_buffer);
10959 }
10960
10961 return window_height_changed_p;
10962 }
10963
10964
10965 /* Value is the current message, a string, or nil if there is no
10966 current message. */
10967
10968 Lisp_Object
10969 current_message (void)
10970 {
10971 Lisp_Object msg;
10972
10973 if (!BUFFERP (echo_area_buffer[0]))
10974 msg = Qnil;
10975 else
10976 {
10977 with_echo_area_buffer (0, 0, current_message_1,
10978 (intptr_t) &msg, Qnil);
10979 if (NILP (msg))
10980 echo_area_buffer[0] = Qnil;
10981 }
10982
10983 return msg;
10984 }
10985
10986
10987 static bool
10988 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10989 {
10990 intptr_t i1 = a1;
10991 Lisp_Object *msg = (Lisp_Object *) i1;
10992
10993 if (Z > BEG)
10994 *msg = make_buffer_string (BEG, Z, true);
10995 else
10996 *msg = Qnil;
10997 return false;
10998 }
10999
11000
11001 /* Push the current message on Vmessage_stack for later restoration
11002 by restore_message. Value is true if the current message isn't
11003 empty. This is a relatively infrequent operation, so it's not
11004 worth optimizing. */
11005
11006 bool
11007 push_message (void)
11008 {
11009 Lisp_Object msg = current_message ();
11010 Vmessage_stack = Fcons (msg, Vmessage_stack);
11011 return STRINGP (msg);
11012 }
11013
11014
11015 /* Restore message display from the top of Vmessage_stack. */
11016
11017 void
11018 restore_message (void)
11019 {
11020 eassert (CONSP (Vmessage_stack));
11021 message3_nolog (XCAR (Vmessage_stack));
11022 }
11023
11024
11025 /* Handler for unwind-protect calling pop_message. */
11026
11027 void
11028 pop_message_unwind (void)
11029 {
11030 /* Pop the top-most entry off Vmessage_stack. */
11031 eassert (CONSP (Vmessage_stack));
11032 Vmessage_stack = XCDR (Vmessage_stack);
11033 }
11034
11035
11036 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11037 exits. If the stack is not empty, we have a missing pop_message
11038 somewhere. */
11039
11040 void
11041 check_message_stack (void)
11042 {
11043 if (!NILP (Vmessage_stack))
11044 emacs_abort ();
11045 }
11046
11047
11048 /* Truncate to NCHARS what will be displayed in the echo area the next
11049 time we display it---but don't redisplay it now. */
11050
11051 void
11052 truncate_echo_area (ptrdiff_t nchars)
11053 {
11054 if (nchars == 0)
11055 echo_area_buffer[0] = Qnil;
11056 else if (!noninteractive
11057 && INTERACTIVE
11058 && !NILP (echo_area_buffer[0]))
11059 {
11060 struct frame *sf = SELECTED_FRAME ();
11061 /* Error messages get reported properly by cmd_error, so this must be
11062 just an informative message; if the frame hasn't really been
11063 initialized yet, just toss it. */
11064 if (sf->glyphs_initialized_p)
11065 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11066 }
11067 }
11068
11069
11070 /* Helper function for truncate_echo_area. Truncate the current
11071 message to at most NCHARS characters. */
11072
11073 static bool
11074 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11075 {
11076 if (BEG + nchars < Z)
11077 del_range (BEG + nchars, Z);
11078 if (Z == BEG)
11079 echo_area_buffer[0] = Qnil;
11080 return false;
11081 }
11082
11083 /* Set the current message to STRING. */
11084
11085 static void
11086 set_message (Lisp_Object string)
11087 {
11088 eassert (STRINGP (string));
11089
11090 message_enable_multibyte = STRING_MULTIBYTE (string);
11091
11092 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11093 message_buf_print = false;
11094 help_echo_showing_p = false;
11095
11096 if (STRINGP (Vdebug_on_message)
11097 && STRINGP (string)
11098 && fast_string_match (Vdebug_on_message, string) >= 0)
11099 call_debugger (list2 (Qerror, string));
11100 }
11101
11102
11103 /* Helper function for set_message. First argument is ignored and second
11104 argument has the same meaning as for set_message.
11105 This function is called with the echo area buffer being current. */
11106
11107 static bool
11108 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11109 {
11110 eassert (STRINGP (string));
11111
11112 /* Change multibyteness of the echo buffer appropriately. */
11113 if (message_enable_multibyte
11114 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11115 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11116
11117 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11118 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11119 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11120
11121 /* Insert new message at BEG. */
11122 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11123
11124 /* This function takes care of single/multibyte conversion.
11125 We just have to ensure that the echo area buffer has the right
11126 setting of enable_multibyte_characters. */
11127 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11128
11129 return false;
11130 }
11131
11132
11133 /* Clear messages. CURRENT_P means clear the current message.
11134 LAST_DISPLAYED_P means clear the message last displayed. */
11135
11136 void
11137 clear_message (bool current_p, bool last_displayed_p)
11138 {
11139 if (current_p)
11140 {
11141 echo_area_buffer[0] = Qnil;
11142 message_cleared_p = true;
11143 }
11144
11145 if (last_displayed_p)
11146 echo_area_buffer[1] = Qnil;
11147
11148 message_buf_print = false;
11149 }
11150
11151 /* Clear garbaged frames.
11152
11153 This function is used where the old redisplay called
11154 redraw_garbaged_frames which in turn called redraw_frame which in
11155 turn called clear_frame. The call to clear_frame was a source of
11156 flickering. I believe a clear_frame is not necessary. It should
11157 suffice in the new redisplay to invalidate all current matrices,
11158 and ensure a complete redisplay of all windows. */
11159
11160 static void
11161 clear_garbaged_frames (void)
11162 {
11163 if (frame_garbaged)
11164 {
11165 Lisp_Object tail, frame;
11166
11167 FOR_EACH_FRAME (tail, frame)
11168 {
11169 struct frame *f = XFRAME (frame);
11170
11171 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11172 {
11173 if (f->resized_p)
11174 redraw_frame (f);
11175 else
11176 clear_current_matrices (f);
11177 fset_redisplay (f);
11178 f->garbaged = false;
11179 f->resized_p = false;
11180 }
11181 }
11182
11183 frame_garbaged = false;
11184 }
11185 }
11186
11187
11188 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11189 selected_frame. */
11190
11191 static void
11192 echo_area_display (bool update_frame_p)
11193 {
11194 Lisp_Object mini_window;
11195 struct window *w;
11196 struct frame *f;
11197 bool window_height_changed_p = false;
11198 struct frame *sf = SELECTED_FRAME ();
11199
11200 mini_window = FRAME_MINIBUF_WINDOW (sf);
11201 w = XWINDOW (mini_window);
11202 f = XFRAME (WINDOW_FRAME (w));
11203
11204 /* Don't display if frame is invisible or not yet initialized. */
11205 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11206 return;
11207
11208 #ifdef HAVE_WINDOW_SYSTEM
11209 /* When Emacs starts, selected_frame may be the initial terminal
11210 frame. If we let this through, a message would be displayed on
11211 the terminal. */
11212 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11213 return;
11214 #endif /* HAVE_WINDOW_SYSTEM */
11215
11216 /* Redraw garbaged frames. */
11217 clear_garbaged_frames ();
11218
11219 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11220 {
11221 echo_area_window = mini_window;
11222 window_height_changed_p = display_echo_area (w);
11223 w->must_be_updated_p = true;
11224
11225 /* Update the display, unless called from redisplay_internal.
11226 Also don't update the screen during redisplay itself. The
11227 update will happen at the end of redisplay, and an update
11228 here could cause confusion. */
11229 if (update_frame_p && !redisplaying_p)
11230 {
11231 int n = 0;
11232
11233 /* If the display update has been interrupted by pending
11234 input, update mode lines in the frame. Due to the
11235 pending input, it might have been that redisplay hasn't
11236 been called, so that mode lines above the echo area are
11237 garbaged. This looks odd, so we prevent it here. */
11238 if (!display_completed)
11239 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11240
11241 if (window_height_changed_p
11242 /* Don't do this if Emacs is shutting down. Redisplay
11243 needs to run hooks. */
11244 && !NILP (Vrun_hooks))
11245 {
11246 /* Must update other windows. Likewise as in other
11247 cases, don't let this update be interrupted by
11248 pending input. */
11249 ptrdiff_t count = SPECPDL_INDEX ();
11250 specbind (Qredisplay_dont_pause, Qt);
11251 fset_redisplay (f);
11252 redisplay_internal ();
11253 unbind_to (count, Qnil);
11254 }
11255 else if (FRAME_WINDOW_P (f) && n == 0)
11256 {
11257 /* Window configuration is the same as before.
11258 Can do with a display update of the echo area,
11259 unless we displayed some mode lines. */
11260 update_single_window (w);
11261 flush_frame (f);
11262 }
11263 else
11264 update_frame (f, true, true);
11265
11266 /* If cursor is in the echo area, make sure that the next
11267 redisplay displays the minibuffer, so that the cursor will
11268 be replaced with what the minibuffer wants. */
11269 if (cursor_in_echo_area)
11270 wset_redisplay (XWINDOW (mini_window));
11271 }
11272 }
11273 else if (!EQ (mini_window, selected_window))
11274 wset_redisplay (XWINDOW (mini_window));
11275
11276 /* Last displayed message is now the current message. */
11277 echo_area_buffer[1] = echo_area_buffer[0];
11278 /* Inform read_char that we're not echoing. */
11279 echo_message_buffer = Qnil;
11280
11281 /* Prevent redisplay optimization in redisplay_internal by resetting
11282 this_line_start_pos. This is done because the mini-buffer now
11283 displays the message instead of its buffer text. */
11284 if (EQ (mini_window, selected_window))
11285 CHARPOS (this_line_start_pos) = 0;
11286
11287 if (window_height_changed_p)
11288 {
11289 fset_redisplay (f);
11290
11291 /* If window configuration was changed, frames may have been
11292 marked garbaged. Clear them or we will experience
11293 surprises wrt scrolling.
11294 FIXME: How/why/when? */
11295 clear_garbaged_frames ();
11296 }
11297 }
11298
11299 /* True if W's buffer was changed but not saved. */
11300
11301 static bool
11302 window_buffer_changed (struct window *w)
11303 {
11304 struct buffer *b = XBUFFER (w->contents);
11305
11306 eassert (BUFFER_LIVE_P (b));
11307
11308 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11309 }
11310
11311 /* True if W has %c in its mode line and mode line should be updated. */
11312
11313 static bool
11314 mode_line_update_needed (struct window *w)
11315 {
11316 return (w->column_number_displayed != -1
11317 && !(PT == w->last_point && !window_outdated (w))
11318 && (w->column_number_displayed != current_column ()));
11319 }
11320
11321 /* True if window start of W is frozen and may not be changed during
11322 redisplay. */
11323
11324 static bool
11325 window_frozen_p (struct window *w)
11326 {
11327 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11328 {
11329 Lisp_Object window;
11330
11331 XSETWINDOW (window, w);
11332 if (MINI_WINDOW_P (w))
11333 return false;
11334 else if (EQ (window, selected_window))
11335 return false;
11336 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11337 && EQ (window, Vminibuf_scroll_window))
11338 /* This special window can't be frozen too. */
11339 return false;
11340 else
11341 return true;
11342 }
11343 return false;
11344 }
11345
11346 /***********************************************************************
11347 Mode Lines and Frame Titles
11348 ***********************************************************************/
11349
11350 /* A buffer for constructing non-propertized mode-line strings and
11351 frame titles in it; allocated from the heap in init_xdisp and
11352 resized as needed in store_mode_line_noprop_char. */
11353
11354 static char *mode_line_noprop_buf;
11355
11356 /* The buffer's end, and a current output position in it. */
11357
11358 static char *mode_line_noprop_buf_end;
11359 static char *mode_line_noprop_ptr;
11360
11361 #define MODE_LINE_NOPROP_LEN(start) \
11362 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11363
11364 static enum {
11365 MODE_LINE_DISPLAY = 0,
11366 MODE_LINE_TITLE,
11367 MODE_LINE_NOPROP,
11368 MODE_LINE_STRING
11369 } mode_line_target;
11370
11371 /* Alist that caches the results of :propertize.
11372 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11373 static Lisp_Object mode_line_proptrans_alist;
11374
11375 /* List of strings making up the mode-line. */
11376 static Lisp_Object mode_line_string_list;
11377
11378 /* Base face property when building propertized mode line string. */
11379 static Lisp_Object mode_line_string_face;
11380 static Lisp_Object mode_line_string_face_prop;
11381
11382
11383 /* Unwind data for mode line strings */
11384
11385 static Lisp_Object Vmode_line_unwind_vector;
11386
11387 static Lisp_Object
11388 format_mode_line_unwind_data (struct frame *target_frame,
11389 struct buffer *obuf,
11390 Lisp_Object owin,
11391 bool save_proptrans)
11392 {
11393 Lisp_Object vector, tmp;
11394
11395 /* Reduce consing by keeping one vector in
11396 Vwith_echo_area_save_vector. */
11397 vector = Vmode_line_unwind_vector;
11398 Vmode_line_unwind_vector = Qnil;
11399
11400 if (NILP (vector))
11401 vector = Fmake_vector (make_number (10), Qnil);
11402
11403 ASET (vector, 0, make_number (mode_line_target));
11404 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11405 ASET (vector, 2, mode_line_string_list);
11406 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11407 ASET (vector, 4, mode_line_string_face);
11408 ASET (vector, 5, mode_line_string_face_prop);
11409
11410 if (obuf)
11411 XSETBUFFER (tmp, obuf);
11412 else
11413 tmp = Qnil;
11414 ASET (vector, 6, tmp);
11415 ASET (vector, 7, owin);
11416 if (target_frame)
11417 {
11418 /* Similarly to `with-selected-window', if the operation selects
11419 a window on another frame, we must restore that frame's
11420 selected window, and (for a tty) the top-frame. */
11421 ASET (vector, 8, target_frame->selected_window);
11422 if (FRAME_TERMCAP_P (target_frame))
11423 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11424 }
11425
11426 return vector;
11427 }
11428
11429 static void
11430 unwind_format_mode_line (Lisp_Object vector)
11431 {
11432 Lisp_Object old_window = AREF (vector, 7);
11433 Lisp_Object target_frame_window = AREF (vector, 8);
11434 Lisp_Object old_top_frame = AREF (vector, 9);
11435
11436 mode_line_target = XINT (AREF (vector, 0));
11437 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11438 mode_line_string_list = AREF (vector, 2);
11439 if (! EQ (AREF (vector, 3), Qt))
11440 mode_line_proptrans_alist = AREF (vector, 3);
11441 mode_line_string_face = AREF (vector, 4);
11442 mode_line_string_face_prop = AREF (vector, 5);
11443
11444 /* Select window before buffer, since it may change the buffer. */
11445 if (!NILP (old_window))
11446 {
11447 /* If the operation that we are unwinding had selected a window
11448 on a different frame, reset its frame-selected-window. For a
11449 text terminal, reset its top-frame if necessary. */
11450 if (!NILP (target_frame_window))
11451 {
11452 Lisp_Object frame
11453 = WINDOW_FRAME (XWINDOW (target_frame_window));
11454
11455 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11456 Fselect_window (target_frame_window, Qt);
11457
11458 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11459 Fselect_frame (old_top_frame, Qt);
11460 }
11461
11462 Fselect_window (old_window, Qt);
11463 }
11464
11465 if (!NILP (AREF (vector, 6)))
11466 {
11467 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11468 ASET (vector, 6, Qnil);
11469 }
11470
11471 Vmode_line_unwind_vector = vector;
11472 }
11473
11474
11475 /* Store a single character C for the frame title in mode_line_noprop_buf.
11476 Re-allocate mode_line_noprop_buf if necessary. */
11477
11478 static void
11479 store_mode_line_noprop_char (char c)
11480 {
11481 /* If output position has reached the end of the allocated buffer,
11482 increase the buffer's size. */
11483 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11484 {
11485 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11486 ptrdiff_t size = len;
11487 mode_line_noprop_buf =
11488 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11489 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11490 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11491 }
11492
11493 *mode_line_noprop_ptr++ = c;
11494 }
11495
11496
11497 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11498 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11499 characters that yield more columns than PRECISION; PRECISION <= 0
11500 means copy the whole string. Pad with spaces until FIELD_WIDTH
11501 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11502 pad. Called from display_mode_element when it is used to build a
11503 frame title. */
11504
11505 static int
11506 store_mode_line_noprop (const char *string, int field_width, int precision)
11507 {
11508 const unsigned char *str = (const unsigned char *) string;
11509 int n = 0;
11510 ptrdiff_t dummy, nbytes;
11511
11512 /* Copy at most PRECISION chars from STR. */
11513 nbytes = strlen (string);
11514 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11515 while (nbytes--)
11516 store_mode_line_noprop_char (*str++);
11517
11518 /* Fill up with spaces until FIELD_WIDTH reached. */
11519 while (field_width > 0
11520 && n < field_width)
11521 {
11522 store_mode_line_noprop_char (' ');
11523 ++n;
11524 }
11525
11526 return n;
11527 }
11528
11529 /***********************************************************************
11530 Frame Titles
11531 ***********************************************************************/
11532
11533 #ifdef HAVE_WINDOW_SYSTEM
11534
11535 /* Set the title of FRAME, if it has changed. The title format is
11536 Vicon_title_format if FRAME is iconified, otherwise it is
11537 frame_title_format. */
11538
11539 static void
11540 x_consider_frame_title (Lisp_Object frame)
11541 {
11542 struct frame *f = XFRAME (frame);
11543
11544 if (FRAME_WINDOW_P (f)
11545 || FRAME_MINIBUF_ONLY_P (f)
11546 || f->explicit_name)
11547 {
11548 /* Do we have more than one visible frame on this X display? */
11549 Lisp_Object tail, other_frame, fmt;
11550 ptrdiff_t title_start;
11551 char *title;
11552 ptrdiff_t len;
11553 struct it it;
11554 ptrdiff_t count = SPECPDL_INDEX ();
11555
11556 FOR_EACH_FRAME (tail, other_frame)
11557 {
11558 struct frame *tf = XFRAME (other_frame);
11559
11560 if (tf != f
11561 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11562 && !FRAME_MINIBUF_ONLY_P (tf)
11563 && !EQ (other_frame, tip_frame)
11564 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11565 break;
11566 }
11567
11568 /* Set global variable indicating that multiple frames exist. */
11569 multiple_frames = CONSP (tail);
11570
11571 /* Switch to the buffer of selected window of the frame. Set up
11572 mode_line_target so that display_mode_element will output into
11573 mode_line_noprop_buf; then display the title. */
11574 record_unwind_protect (unwind_format_mode_line,
11575 format_mode_line_unwind_data
11576 (f, current_buffer, selected_window, false));
11577
11578 Fselect_window (f->selected_window, Qt);
11579 set_buffer_internal_1
11580 (XBUFFER (XWINDOW (f->selected_window)->contents));
11581 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11582
11583 mode_line_target = MODE_LINE_TITLE;
11584 title_start = MODE_LINE_NOPROP_LEN (0);
11585 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11586 NULL, DEFAULT_FACE_ID);
11587 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11588 len = MODE_LINE_NOPROP_LEN (title_start);
11589 title = mode_line_noprop_buf + title_start;
11590 unbind_to (count, Qnil);
11591
11592 /* Set the title only if it's changed. This avoids consing in
11593 the common case where it hasn't. (If it turns out that we've
11594 already wasted too much time by walking through the list with
11595 display_mode_element, then we might need to optimize at a
11596 higher level than this.) */
11597 if (! STRINGP (f->name)
11598 || SBYTES (f->name) != len
11599 || memcmp (title, SDATA (f->name), len) != 0)
11600 x_implicitly_set_name (f, make_string (title, len), Qnil);
11601 }
11602 }
11603
11604 #endif /* not HAVE_WINDOW_SYSTEM */
11605
11606 \f
11607 /***********************************************************************
11608 Menu Bars
11609 ***********************************************************************/
11610
11611 /* True if we will not redisplay all visible windows. */
11612 #define REDISPLAY_SOME_P() \
11613 ((windows_or_buffers_changed == 0 \
11614 || windows_or_buffers_changed == REDISPLAY_SOME) \
11615 && (update_mode_lines == 0 \
11616 || update_mode_lines == REDISPLAY_SOME))
11617
11618 /* Prepare for redisplay by updating menu-bar item lists when
11619 appropriate. This can call eval. */
11620
11621 static void
11622 prepare_menu_bars (void)
11623 {
11624 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11625 bool some_windows = REDISPLAY_SOME_P ();
11626 Lisp_Object tooltip_frame;
11627
11628 #ifdef HAVE_WINDOW_SYSTEM
11629 tooltip_frame = tip_frame;
11630 #else
11631 tooltip_frame = Qnil;
11632 #endif
11633
11634 if (FUNCTIONP (Vpre_redisplay_function))
11635 {
11636 Lisp_Object windows = all_windows ? Qt : Qnil;
11637 if (all_windows && some_windows)
11638 {
11639 Lisp_Object ws = window_list ();
11640 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11641 {
11642 Lisp_Object this = XCAR (ws);
11643 struct window *w = XWINDOW (this);
11644 if (w->redisplay
11645 || XFRAME (w->frame)->redisplay
11646 || XBUFFER (w->contents)->text->redisplay)
11647 {
11648 windows = Fcons (this, windows);
11649 }
11650 }
11651 }
11652 safe__call1 (true, Vpre_redisplay_function, windows);
11653 }
11654
11655 /* Update all frame titles based on their buffer names, etc. We do
11656 this before the menu bars so that the buffer-menu will show the
11657 up-to-date frame titles. */
11658 #ifdef HAVE_WINDOW_SYSTEM
11659 if (all_windows)
11660 {
11661 Lisp_Object tail, frame;
11662
11663 FOR_EACH_FRAME (tail, frame)
11664 {
11665 struct frame *f = XFRAME (frame);
11666 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11667 if (some_windows
11668 && !f->redisplay
11669 && !w->redisplay
11670 && !XBUFFER (w->contents)->text->redisplay)
11671 continue;
11672
11673 if (!EQ (frame, tooltip_frame)
11674 && (FRAME_ICONIFIED_P (f)
11675 || FRAME_VISIBLE_P (f) == 1
11676 /* Exclude TTY frames that are obscured because they
11677 are not the top frame on their console. This is
11678 because x_consider_frame_title actually switches
11679 to the frame, which for TTY frames means it is
11680 marked as garbaged, and will be completely
11681 redrawn on the next redisplay cycle. This causes
11682 TTY frames to be completely redrawn, when there
11683 are more than one of them, even though nothing
11684 should be changed on display. */
11685 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11686 x_consider_frame_title (frame);
11687 }
11688 }
11689 #endif /* HAVE_WINDOW_SYSTEM */
11690
11691 /* Update the menu bar item lists, if appropriate. This has to be
11692 done before any actual redisplay or generation of display lines. */
11693
11694 if (all_windows)
11695 {
11696 Lisp_Object tail, frame;
11697 ptrdiff_t count = SPECPDL_INDEX ();
11698 /* True means that update_menu_bar has run its hooks
11699 so any further calls to update_menu_bar shouldn't do so again. */
11700 bool menu_bar_hooks_run = false;
11701
11702 record_unwind_save_match_data ();
11703
11704 FOR_EACH_FRAME (tail, frame)
11705 {
11706 struct frame *f = XFRAME (frame);
11707 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11708
11709 /* Ignore tooltip frame. */
11710 if (EQ (frame, tooltip_frame))
11711 continue;
11712
11713 if (some_windows
11714 && !f->redisplay
11715 && !w->redisplay
11716 && !XBUFFER (w->contents)->text->redisplay)
11717 continue;
11718
11719 /* If a window on this frame changed size, report that to
11720 the user and clear the size-change flag. */
11721 if (FRAME_WINDOW_SIZES_CHANGED (f))
11722 {
11723 Lisp_Object functions;
11724
11725 /* Clear flag first in case we get an error below. */
11726 FRAME_WINDOW_SIZES_CHANGED (f) = false;
11727 functions = Vwindow_size_change_functions;
11728
11729 while (CONSP (functions))
11730 {
11731 if (!EQ (XCAR (functions), Qt))
11732 call1 (XCAR (functions), frame);
11733 functions = XCDR (functions);
11734 }
11735 }
11736
11737 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11738 #ifdef HAVE_WINDOW_SYSTEM
11739 update_tool_bar (f, false);
11740 #endif
11741 }
11742
11743 unbind_to (count, Qnil);
11744 }
11745 else
11746 {
11747 struct frame *sf = SELECTED_FRAME ();
11748 update_menu_bar (sf, true, false);
11749 #ifdef HAVE_WINDOW_SYSTEM
11750 update_tool_bar (sf, true);
11751 #endif
11752 }
11753 }
11754
11755
11756 /* Update the menu bar item list for frame F. This has to be done
11757 before we start to fill in any display lines, because it can call
11758 eval.
11759
11760 If SAVE_MATCH_DATA, we must save and restore it here.
11761
11762 If HOOKS_RUN, a previous call to update_menu_bar
11763 already ran the menu bar hooks for this redisplay, so there
11764 is no need to run them again. The return value is the
11765 updated value of this flag, to pass to the next call. */
11766
11767 static bool
11768 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
11769 {
11770 Lisp_Object window;
11771 struct window *w;
11772
11773 /* If called recursively during a menu update, do nothing. This can
11774 happen when, for instance, an activate-menubar-hook causes a
11775 redisplay. */
11776 if (inhibit_menubar_update)
11777 return hooks_run;
11778
11779 window = FRAME_SELECTED_WINDOW (f);
11780 w = XWINDOW (window);
11781
11782 if (FRAME_WINDOW_P (f)
11783 ?
11784 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11785 || defined (HAVE_NS) || defined (USE_GTK)
11786 FRAME_EXTERNAL_MENU_BAR (f)
11787 #else
11788 FRAME_MENU_BAR_LINES (f) > 0
11789 #endif
11790 : FRAME_MENU_BAR_LINES (f) > 0)
11791 {
11792 /* If the user has switched buffers or windows, we need to
11793 recompute to reflect the new bindings. But we'll
11794 recompute when update_mode_lines is set too; that means
11795 that people can use force-mode-line-update to request
11796 that the menu bar be recomputed. The adverse effect on
11797 the rest of the redisplay algorithm is about the same as
11798 windows_or_buffers_changed anyway. */
11799 if (windows_or_buffers_changed
11800 /* This used to test w->update_mode_line, but we believe
11801 there is no need to recompute the menu in that case. */
11802 || update_mode_lines
11803 || window_buffer_changed (w))
11804 {
11805 struct buffer *prev = current_buffer;
11806 ptrdiff_t count = SPECPDL_INDEX ();
11807
11808 specbind (Qinhibit_menubar_update, Qt);
11809
11810 set_buffer_internal_1 (XBUFFER (w->contents));
11811 if (save_match_data)
11812 record_unwind_save_match_data ();
11813 if (NILP (Voverriding_local_map_menu_flag))
11814 {
11815 specbind (Qoverriding_terminal_local_map, Qnil);
11816 specbind (Qoverriding_local_map, Qnil);
11817 }
11818
11819 if (!hooks_run)
11820 {
11821 /* Run the Lucid hook. */
11822 safe_run_hooks (Qactivate_menubar_hook);
11823
11824 /* If it has changed current-menubar from previous value,
11825 really recompute the menu-bar from the value. */
11826 if (! NILP (Vlucid_menu_bar_dirty_flag))
11827 call0 (Qrecompute_lucid_menubar);
11828
11829 safe_run_hooks (Qmenu_bar_update_hook);
11830
11831 hooks_run = true;
11832 }
11833
11834 XSETFRAME (Vmenu_updating_frame, f);
11835 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11836
11837 /* Redisplay the menu bar in case we changed it. */
11838 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11839 || defined (HAVE_NS) || defined (USE_GTK)
11840 if (FRAME_WINDOW_P (f))
11841 {
11842 #if defined (HAVE_NS)
11843 /* All frames on Mac OS share the same menubar. So only
11844 the selected frame should be allowed to set it. */
11845 if (f == SELECTED_FRAME ())
11846 #endif
11847 set_frame_menubar (f, false, false);
11848 }
11849 else
11850 /* On a terminal screen, the menu bar is an ordinary screen
11851 line, and this makes it get updated. */
11852 w->update_mode_line = true;
11853 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11854 /* In the non-toolkit version, the menu bar is an ordinary screen
11855 line, and this makes it get updated. */
11856 w->update_mode_line = true;
11857 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11858
11859 unbind_to (count, Qnil);
11860 set_buffer_internal_1 (prev);
11861 }
11862 }
11863
11864 return hooks_run;
11865 }
11866
11867 /***********************************************************************
11868 Tool-bars
11869 ***********************************************************************/
11870
11871 #ifdef HAVE_WINDOW_SYSTEM
11872
11873 /* Select `frame' temporarily without running all the code in
11874 do_switch_frame.
11875 FIXME: Maybe do_switch_frame should be trimmed down similarly
11876 when `norecord' is set. */
11877 static void
11878 fast_set_selected_frame (Lisp_Object frame)
11879 {
11880 if (!EQ (selected_frame, frame))
11881 {
11882 selected_frame = frame;
11883 selected_window = XFRAME (frame)->selected_window;
11884 }
11885 }
11886
11887 /* Update the tool-bar item list for frame F. This has to be done
11888 before we start to fill in any display lines. Called from
11889 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
11890 and restore it here. */
11891
11892 static void
11893 update_tool_bar (struct frame *f, bool save_match_data)
11894 {
11895 #if defined (USE_GTK) || defined (HAVE_NS)
11896 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11897 #else
11898 bool do_update = (WINDOWP (f->tool_bar_window)
11899 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11900 #endif
11901
11902 if (do_update)
11903 {
11904 Lisp_Object window;
11905 struct window *w;
11906
11907 window = FRAME_SELECTED_WINDOW (f);
11908 w = XWINDOW (window);
11909
11910 /* If the user has switched buffers or windows, we need to
11911 recompute to reflect the new bindings. But we'll
11912 recompute when update_mode_lines is set too; that means
11913 that people can use force-mode-line-update to request
11914 that the menu bar be recomputed. The adverse effect on
11915 the rest of the redisplay algorithm is about the same as
11916 windows_or_buffers_changed anyway. */
11917 if (windows_or_buffers_changed
11918 || w->update_mode_line
11919 || update_mode_lines
11920 || window_buffer_changed (w))
11921 {
11922 struct buffer *prev = current_buffer;
11923 ptrdiff_t count = SPECPDL_INDEX ();
11924 Lisp_Object frame, new_tool_bar;
11925 int new_n_tool_bar;
11926
11927 /* Set current_buffer to the buffer of the selected
11928 window of the frame, so that we get the right local
11929 keymaps. */
11930 set_buffer_internal_1 (XBUFFER (w->contents));
11931
11932 /* Save match data, if we must. */
11933 if (save_match_data)
11934 record_unwind_save_match_data ();
11935
11936 /* Make sure that we don't accidentally use bogus keymaps. */
11937 if (NILP (Voverriding_local_map_menu_flag))
11938 {
11939 specbind (Qoverriding_terminal_local_map, Qnil);
11940 specbind (Qoverriding_local_map, Qnil);
11941 }
11942
11943 /* We must temporarily set the selected frame to this frame
11944 before calling tool_bar_items, because the calculation of
11945 the tool-bar keymap uses the selected frame (see
11946 `tool-bar-make-keymap' in tool-bar.el). */
11947 eassert (EQ (selected_window,
11948 /* Since we only explicitly preserve selected_frame,
11949 check that selected_window would be redundant. */
11950 XFRAME (selected_frame)->selected_window));
11951 record_unwind_protect (fast_set_selected_frame, selected_frame);
11952 XSETFRAME (frame, f);
11953 fast_set_selected_frame (frame);
11954
11955 /* Build desired tool-bar items from keymaps. */
11956 new_tool_bar
11957 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11958 &new_n_tool_bar);
11959
11960 /* Redisplay the tool-bar if we changed it. */
11961 if (new_n_tool_bar != f->n_tool_bar_items
11962 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11963 {
11964 /* Redisplay that happens asynchronously due to an expose event
11965 may access f->tool_bar_items. Make sure we update both
11966 variables within BLOCK_INPUT so no such event interrupts. */
11967 block_input ();
11968 fset_tool_bar_items (f, new_tool_bar);
11969 f->n_tool_bar_items = new_n_tool_bar;
11970 w->update_mode_line = true;
11971 unblock_input ();
11972 }
11973
11974 unbind_to (count, Qnil);
11975 set_buffer_internal_1 (prev);
11976 }
11977 }
11978 }
11979
11980 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11981
11982 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11983 F's desired tool-bar contents. F->tool_bar_items must have
11984 been set up previously by calling prepare_menu_bars. */
11985
11986 static void
11987 build_desired_tool_bar_string (struct frame *f)
11988 {
11989 int i, size, size_needed;
11990 Lisp_Object image, plist;
11991
11992 image = plist = Qnil;
11993
11994 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11995 Otherwise, make a new string. */
11996
11997 /* The size of the string we might be able to reuse. */
11998 size = (STRINGP (f->desired_tool_bar_string)
11999 ? SCHARS (f->desired_tool_bar_string)
12000 : 0);
12001
12002 /* We need one space in the string for each image. */
12003 size_needed = f->n_tool_bar_items;
12004
12005 /* Reuse f->desired_tool_bar_string, if possible. */
12006 if (size < size_needed || NILP (f->desired_tool_bar_string))
12007 fset_desired_tool_bar_string
12008 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12009 else
12010 {
12011 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12012 Fremove_text_properties (make_number (0), make_number (size),
12013 props, f->desired_tool_bar_string);
12014 }
12015
12016 /* Put a `display' property on the string for the images to display,
12017 put a `menu_item' property on tool-bar items with a value that
12018 is the index of the item in F's tool-bar item vector. */
12019 for (i = 0; i < f->n_tool_bar_items; ++i)
12020 {
12021 #define PROP(IDX) \
12022 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12023
12024 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12025 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12026 int hmargin, vmargin, relief, idx, end;
12027
12028 /* If image is a vector, choose the image according to the
12029 button state. */
12030 image = PROP (TOOL_BAR_ITEM_IMAGES);
12031 if (VECTORP (image))
12032 {
12033 if (enabled_p)
12034 idx = (selected_p
12035 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12036 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12037 else
12038 idx = (selected_p
12039 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12040 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12041
12042 eassert (ASIZE (image) >= idx);
12043 image = AREF (image, idx);
12044 }
12045 else
12046 idx = -1;
12047
12048 /* Ignore invalid image specifications. */
12049 if (!valid_image_p (image))
12050 continue;
12051
12052 /* Display the tool-bar button pressed, or depressed. */
12053 plist = Fcopy_sequence (XCDR (image));
12054
12055 /* Compute margin and relief to draw. */
12056 relief = (tool_bar_button_relief >= 0
12057 ? tool_bar_button_relief
12058 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12059 hmargin = vmargin = relief;
12060
12061 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12062 INT_MAX - max (hmargin, vmargin)))
12063 {
12064 hmargin += XFASTINT (Vtool_bar_button_margin);
12065 vmargin += XFASTINT (Vtool_bar_button_margin);
12066 }
12067 else if (CONSP (Vtool_bar_button_margin))
12068 {
12069 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12070 INT_MAX - hmargin))
12071 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12072
12073 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12074 INT_MAX - vmargin))
12075 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12076 }
12077
12078 if (auto_raise_tool_bar_buttons_p)
12079 {
12080 /* Add a `:relief' property to the image spec if the item is
12081 selected. */
12082 if (selected_p)
12083 {
12084 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12085 hmargin -= relief;
12086 vmargin -= relief;
12087 }
12088 }
12089 else
12090 {
12091 /* If image is selected, display it pressed, i.e. with a
12092 negative relief. If it's not selected, display it with a
12093 raised relief. */
12094 plist = Fplist_put (plist, QCrelief,
12095 (selected_p
12096 ? make_number (-relief)
12097 : make_number (relief)));
12098 hmargin -= relief;
12099 vmargin -= relief;
12100 }
12101
12102 /* Put a margin around the image. */
12103 if (hmargin || vmargin)
12104 {
12105 if (hmargin == vmargin)
12106 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12107 else
12108 plist = Fplist_put (plist, QCmargin,
12109 Fcons (make_number (hmargin),
12110 make_number (vmargin)));
12111 }
12112
12113 /* If button is not enabled, and we don't have special images
12114 for the disabled state, make the image appear disabled by
12115 applying an appropriate algorithm to it. */
12116 if (!enabled_p && idx < 0)
12117 plist = Fplist_put (plist, QCconversion, Qdisabled);
12118
12119 /* Put a `display' text property on the string for the image to
12120 display. Put a `menu-item' property on the string that gives
12121 the start of this item's properties in the tool-bar items
12122 vector. */
12123 image = Fcons (Qimage, plist);
12124 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12125 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12126
12127 /* Let the last image hide all remaining spaces in the tool bar
12128 string. The string can be longer than needed when we reuse a
12129 previous string. */
12130 if (i + 1 == f->n_tool_bar_items)
12131 end = SCHARS (f->desired_tool_bar_string);
12132 else
12133 end = i + 1;
12134 Fadd_text_properties (make_number (i), make_number (end),
12135 props, f->desired_tool_bar_string);
12136 #undef PROP
12137 }
12138 }
12139
12140
12141 /* Display one line of the tool-bar of frame IT->f.
12142
12143 HEIGHT specifies the desired height of the tool-bar line.
12144 If the actual height of the glyph row is less than HEIGHT, the
12145 row's height is increased to HEIGHT, and the icons are centered
12146 vertically in the new height.
12147
12148 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12149 count a final empty row in case the tool-bar width exactly matches
12150 the window width.
12151 */
12152
12153 static void
12154 display_tool_bar_line (struct it *it, int height)
12155 {
12156 struct glyph_row *row = it->glyph_row;
12157 int max_x = it->last_visible_x;
12158 struct glyph *last;
12159
12160 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12161 clear_glyph_row (row);
12162 row->enabled_p = true;
12163 row->y = it->current_y;
12164
12165 /* Note that this isn't made use of if the face hasn't a box,
12166 so there's no need to check the face here. */
12167 it->start_of_box_run_p = true;
12168
12169 while (it->current_x < max_x)
12170 {
12171 int x, n_glyphs_before, i, nglyphs;
12172 struct it it_before;
12173
12174 /* Get the next display element. */
12175 if (!get_next_display_element (it))
12176 {
12177 /* Don't count empty row if we are counting needed tool-bar lines. */
12178 if (height < 0 && !it->hpos)
12179 return;
12180 break;
12181 }
12182
12183 /* Produce glyphs. */
12184 n_glyphs_before = row->used[TEXT_AREA];
12185 it_before = *it;
12186
12187 PRODUCE_GLYPHS (it);
12188
12189 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12190 i = 0;
12191 x = it_before.current_x;
12192 while (i < nglyphs)
12193 {
12194 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12195
12196 if (x + glyph->pixel_width > max_x)
12197 {
12198 /* Glyph doesn't fit on line. Backtrack. */
12199 row->used[TEXT_AREA] = n_glyphs_before;
12200 *it = it_before;
12201 /* If this is the only glyph on this line, it will never fit on the
12202 tool-bar, so skip it. But ensure there is at least one glyph,
12203 so we don't accidentally disable the tool-bar. */
12204 if (n_glyphs_before == 0
12205 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12206 break;
12207 goto out;
12208 }
12209
12210 ++it->hpos;
12211 x += glyph->pixel_width;
12212 ++i;
12213 }
12214
12215 /* Stop at line end. */
12216 if (ITERATOR_AT_END_OF_LINE_P (it))
12217 break;
12218
12219 set_iterator_to_next (it, true);
12220 }
12221
12222 out:;
12223
12224 row->displays_text_p = row->used[TEXT_AREA] != 0;
12225
12226 /* Use default face for the border below the tool bar.
12227
12228 FIXME: When auto-resize-tool-bars is grow-only, there is
12229 no additional border below the possibly empty tool-bar lines.
12230 So to make the extra empty lines look "normal", we have to
12231 use the tool-bar face for the border too. */
12232 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12233 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12234 it->face_id = DEFAULT_FACE_ID;
12235
12236 extend_face_to_end_of_line (it);
12237 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12238 last->right_box_line_p = true;
12239 if (last == row->glyphs[TEXT_AREA])
12240 last->left_box_line_p = true;
12241
12242 /* Make line the desired height and center it vertically. */
12243 if ((height -= it->max_ascent + it->max_descent) > 0)
12244 {
12245 /* Don't add more than one line height. */
12246 height %= FRAME_LINE_HEIGHT (it->f);
12247 it->max_ascent += height / 2;
12248 it->max_descent += (height + 1) / 2;
12249 }
12250
12251 compute_line_metrics (it);
12252
12253 /* If line is empty, make it occupy the rest of the tool-bar. */
12254 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12255 {
12256 row->height = row->phys_height = it->last_visible_y - row->y;
12257 row->visible_height = row->height;
12258 row->ascent = row->phys_ascent = 0;
12259 row->extra_line_spacing = 0;
12260 }
12261
12262 row->full_width_p = true;
12263 row->continued_p = false;
12264 row->truncated_on_left_p = false;
12265 row->truncated_on_right_p = false;
12266
12267 it->current_x = it->hpos = 0;
12268 it->current_y += row->height;
12269 ++it->vpos;
12270 ++it->glyph_row;
12271 }
12272
12273
12274 /* Value is the number of pixels needed to make all tool-bar items of
12275 frame F visible. The actual number of glyph rows needed is
12276 returned in *N_ROWS if non-NULL. */
12277 static int
12278 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12279 {
12280 struct window *w = XWINDOW (f->tool_bar_window);
12281 struct it it;
12282 /* tool_bar_height is called from redisplay_tool_bar after building
12283 the desired matrix, so use (unused) mode-line row as temporary row to
12284 avoid destroying the first tool-bar row. */
12285 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12286
12287 /* Initialize an iterator for iteration over
12288 F->desired_tool_bar_string in the tool-bar window of frame F. */
12289 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12290 temp_row->reversed_p = false;
12291 it.first_visible_x = 0;
12292 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12293 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12294 it.paragraph_embedding = L2R;
12295
12296 while (!ITERATOR_AT_END_P (&it))
12297 {
12298 clear_glyph_row (temp_row);
12299 it.glyph_row = temp_row;
12300 display_tool_bar_line (&it, -1);
12301 }
12302 clear_glyph_row (temp_row);
12303
12304 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12305 if (n_rows)
12306 *n_rows = it.vpos > 0 ? it.vpos : -1;
12307
12308 if (pixelwise)
12309 return it.current_y;
12310 else
12311 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12312 }
12313
12314 #endif /* !USE_GTK && !HAVE_NS */
12315
12316 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12317 0, 2, 0,
12318 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12319 If FRAME is nil or omitted, use the selected frame. Optional argument
12320 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12321 (Lisp_Object frame, Lisp_Object pixelwise)
12322 {
12323 int height = 0;
12324
12325 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12326 struct frame *f = decode_any_frame (frame);
12327
12328 if (WINDOWP (f->tool_bar_window)
12329 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12330 {
12331 update_tool_bar (f, true);
12332 if (f->n_tool_bar_items)
12333 {
12334 build_desired_tool_bar_string (f);
12335 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12336 }
12337 }
12338 #endif
12339
12340 return make_number (height);
12341 }
12342
12343
12344 /* Display the tool-bar of frame F. Value is true if tool-bar's
12345 height should be changed. */
12346 static bool
12347 redisplay_tool_bar (struct frame *f)
12348 {
12349 f->tool_bar_redisplayed = true;
12350 #if defined (USE_GTK) || defined (HAVE_NS)
12351
12352 if (FRAME_EXTERNAL_TOOL_BAR (f))
12353 update_frame_tool_bar (f);
12354 return false;
12355
12356 #else /* !USE_GTK && !HAVE_NS */
12357
12358 struct window *w;
12359 struct it it;
12360 struct glyph_row *row;
12361
12362 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12363 do anything. This means you must start with tool-bar-lines
12364 non-zero to get the auto-sizing effect. Or in other words, you
12365 can turn off tool-bars by specifying tool-bar-lines zero. */
12366 if (!WINDOWP (f->tool_bar_window)
12367 || (w = XWINDOW (f->tool_bar_window),
12368 WINDOW_TOTAL_LINES (w) == 0))
12369 return false;
12370
12371 /* Set up an iterator for the tool-bar window. */
12372 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12373 it.first_visible_x = 0;
12374 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12375 row = it.glyph_row;
12376 row->reversed_p = false;
12377
12378 /* Build a string that represents the contents of the tool-bar. */
12379 build_desired_tool_bar_string (f);
12380 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12381 /* FIXME: This should be controlled by a user option. But it
12382 doesn't make sense to have an R2L tool bar if the menu bar cannot
12383 be drawn also R2L, and making the menu bar R2L is tricky due
12384 toolkit-specific code that implements it. If an R2L tool bar is
12385 ever supported, display_tool_bar_line should also be augmented to
12386 call unproduce_glyphs like display_line and display_string
12387 do. */
12388 it.paragraph_embedding = L2R;
12389
12390 if (f->n_tool_bar_rows == 0)
12391 {
12392 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12393
12394 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12395 {
12396 x_change_tool_bar_height (f, new_height);
12397 frame_default_tool_bar_height = new_height;
12398 /* Always do that now. */
12399 clear_glyph_matrix (w->desired_matrix);
12400 f->fonts_changed = true;
12401 return true;
12402 }
12403 }
12404
12405 /* Display as many lines as needed to display all tool-bar items. */
12406
12407 if (f->n_tool_bar_rows > 0)
12408 {
12409 int border, rows, height, extra;
12410
12411 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12412 border = XINT (Vtool_bar_border);
12413 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12414 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12415 else if (EQ (Vtool_bar_border, Qborder_width))
12416 border = f->border_width;
12417 else
12418 border = 0;
12419 if (border < 0)
12420 border = 0;
12421
12422 rows = f->n_tool_bar_rows;
12423 height = max (1, (it.last_visible_y - border) / rows);
12424 extra = it.last_visible_y - border - height * rows;
12425
12426 while (it.current_y < it.last_visible_y)
12427 {
12428 int h = 0;
12429 if (extra > 0 && rows-- > 0)
12430 {
12431 h = (extra + rows - 1) / rows;
12432 extra -= h;
12433 }
12434 display_tool_bar_line (&it, height + h);
12435 }
12436 }
12437 else
12438 {
12439 while (it.current_y < it.last_visible_y)
12440 display_tool_bar_line (&it, 0);
12441 }
12442
12443 /* It doesn't make much sense to try scrolling in the tool-bar
12444 window, so don't do it. */
12445 w->desired_matrix->no_scrolling_p = true;
12446 w->must_be_updated_p = true;
12447
12448 if (!NILP (Vauto_resize_tool_bars))
12449 {
12450 bool change_height_p = true;
12451
12452 /* If we couldn't display everything, change the tool-bar's
12453 height if there is room for more. */
12454 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12455 change_height_p = true;
12456
12457 /* We subtract 1 because display_tool_bar_line advances the
12458 glyph_row pointer before returning to its caller. We want to
12459 examine the last glyph row produced by
12460 display_tool_bar_line. */
12461 row = it.glyph_row - 1;
12462
12463 /* If there are blank lines at the end, except for a partially
12464 visible blank line at the end that is smaller than
12465 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12466 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12467 && row->height >= FRAME_LINE_HEIGHT (f))
12468 change_height_p = true;
12469
12470 /* If row displays tool-bar items, but is partially visible,
12471 change the tool-bar's height. */
12472 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12473 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12474 change_height_p = true;
12475
12476 /* Resize windows as needed by changing the `tool-bar-lines'
12477 frame parameter. */
12478 if (change_height_p)
12479 {
12480 int nrows;
12481 int new_height = tool_bar_height (f, &nrows, true);
12482
12483 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12484 && !f->minimize_tool_bar_window_p)
12485 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12486 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12487 f->minimize_tool_bar_window_p = false;
12488
12489 if (change_height_p)
12490 {
12491 x_change_tool_bar_height (f, new_height);
12492 frame_default_tool_bar_height = new_height;
12493 clear_glyph_matrix (w->desired_matrix);
12494 f->n_tool_bar_rows = nrows;
12495 f->fonts_changed = true;
12496
12497 return true;
12498 }
12499 }
12500 }
12501
12502 f->minimize_tool_bar_window_p = false;
12503 return false;
12504
12505 #endif /* USE_GTK || HAVE_NS */
12506 }
12507
12508 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12509
12510 /* Get information about the tool-bar item which is displayed in GLYPH
12511 on frame F. Return in *PROP_IDX the index where tool-bar item
12512 properties start in F->tool_bar_items. Value is false if
12513 GLYPH doesn't display a tool-bar item. */
12514
12515 static bool
12516 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12517 {
12518 Lisp_Object prop;
12519 int charpos;
12520
12521 /* This function can be called asynchronously, which means we must
12522 exclude any possibility that Fget_text_property signals an
12523 error. */
12524 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12525 charpos = max (0, charpos);
12526
12527 /* Get the text property `menu-item' at pos. The value of that
12528 property is the start index of this item's properties in
12529 F->tool_bar_items. */
12530 prop = Fget_text_property (make_number (charpos),
12531 Qmenu_item, f->current_tool_bar_string);
12532 if (! INTEGERP (prop))
12533 return false;
12534 *prop_idx = XINT (prop);
12535 return true;
12536 }
12537
12538 \f
12539 /* Get information about the tool-bar item at position X/Y on frame F.
12540 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12541 the current matrix of the tool-bar window of F, or NULL if not
12542 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12543 item in F->tool_bar_items. Value is
12544
12545 -1 if X/Y is not on a tool-bar item
12546 0 if X/Y is on the same item that was highlighted before.
12547 1 otherwise. */
12548
12549 static int
12550 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12551 int *hpos, int *vpos, int *prop_idx)
12552 {
12553 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12554 struct window *w = XWINDOW (f->tool_bar_window);
12555 int area;
12556
12557 /* Find the glyph under X/Y. */
12558 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12559 if (*glyph == NULL)
12560 return -1;
12561
12562 /* Get the start of this tool-bar item's properties in
12563 f->tool_bar_items. */
12564 if (!tool_bar_item_info (f, *glyph, prop_idx))
12565 return -1;
12566
12567 /* Is mouse on the highlighted item? */
12568 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12569 && *vpos >= hlinfo->mouse_face_beg_row
12570 && *vpos <= hlinfo->mouse_face_end_row
12571 && (*vpos > hlinfo->mouse_face_beg_row
12572 || *hpos >= hlinfo->mouse_face_beg_col)
12573 && (*vpos < hlinfo->mouse_face_end_row
12574 || *hpos < hlinfo->mouse_face_end_col
12575 || hlinfo->mouse_face_past_end))
12576 return 0;
12577
12578 return 1;
12579 }
12580
12581
12582 /* EXPORT:
12583 Handle mouse button event on the tool-bar of frame F, at
12584 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12585 false for button release. MODIFIERS is event modifiers for button
12586 release. */
12587
12588 void
12589 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12590 int modifiers)
12591 {
12592 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12593 struct window *w = XWINDOW (f->tool_bar_window);
12594 int hpos, vpos, prop_idx;
12595 struct glyph *glyph;
12596 Lisp_Object enabled_p;
12597 int ts;
12598
12599 /* If not on the highlighted tool-bar item, and mouse-highlight is
12600 non-nil, return. This is so we generate the tool-bar button
12601 click only when the mouse button is released on the same item as
12602 where it was pressed. However, when mouse-highlight is disabled,
12603 generate the click when the button is released regardless of the
12604 highlight, since tool-bar items are not highlighted in that
12605 case. */
12606 frame_to_window_pixel_xy (w, &x, &y);
12607 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12608 if (ts == -1
12609 || (ts != 0 && !NILP (Vmouse_highlight)))
12610 return;
12611
12612 /* When mouse-highlight is off, generate the click for the item
12613 where the button was pressed, disregarding where it was
12614 released. */
12615 if (NILP (Vmouse_highlight) && !down_p)
12616 prop_idx = f->last_tool_bar_item;
12617
12618 /* If item is disabled, do nothing. */
12619 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12620 if (NILP (enabled_p))
12621 return;
12622
12623 if (down_p)
12624 {
12625 /* Show item in pressed state. */
12626 if (!NILP (Vmouse_highlight))
12627 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12628 f->last_tool_bar_item = prop_idx;
12629 }
12630 else
12631 {
12632 Lisp_Object key, frame;
12633 struct input_event event;
12634 EVENT_INIT (event);
12635
12636 /* Show item in released state. */
12637 if (!NILP (Vmouse_highlight))
12638 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12639
12640 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12641
12642 XSETFRAME (frame, f);
12643 event.kind = TOOL_BAR_EVENT;
12644 event.frame_or_window = frame;
12645 event.arg = frame;
12646 kbd_buffer_store_event (&event);
12647
12648 event.kind = TOOL_BAR_EVENT;
12649 event.frame_or_window = frame;
12650 event.arg = key;
12651 event.modifiers = modifiers;
12652 kbd_buffer_store_event (&event);
12653 f->last_tool_bar_item = -1;
12654 }
12655 }
12656
12657
12658 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12659 tool-bar window-relative coordinates X/Y. Called from
12660 note_mouse_highlight. */
12661
12662 static void
12663 note_tool_bar_highlight (struct frame *f, int x, int y)
12664 {
12665 Lisp_Object window = f->tool_bar_window;
12666 struct window *w = XWINDOW (window);
12667 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12668 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12669 int hpos, vpos;
12670 struct glyph *glyph;
12671 struct glyph_row *row;
12672 int i;
12673 Lisp_Object enabled_p;
12674 int prop_idx;
12675 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12676 bool mouse_down_p;
12677 int rc;
12678
12679 /* Function note_mouse_highlight is called with negative X/Y
12680 values when mouse moves outside of the frame. */
12681 if (x <= 0 || y <= 0)
12682 {
12683 clear_mouse_face (hlinfo);
12684 return;
12685 }
12686
12687 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12688 if (rc < 0)
12689 {
12690 /* Not on tool-bar item. */
12691 clear_mouse_face (hlinfo);
12692 return;
12693 }
12694 else if (rc == 0)
12695 /* On same tool-bar item as before. */
12696 goto set_help_echo;
12697
12698 clear_mouse_face (hlinfo);
12699
12700 /* Mouse is down, but on different tool-bar item? */
12701 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12702 && f == dpyinfo->last_mouse_frame);
12703
12704 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12705 return;
12706
12707 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12708
12709 /* If tool-bar item is not enabled, don't highlight it. */
12710 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12711 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12712 {
12713 /* Compute the x-position of the glyph. In front and past the
12714 image is a space. We include this in the highlighted area. */
12715 row = MATRIX_ROW (w->current_matrix, vpos);
12716 for (i = x = 0; i < hpos; ++i)
12717 x += row->glyphs[TEXT_AREA][i].pixel_width;
12718
12719 /* Record this as the current active region. */
12720 hlinfo->mouse_face_beg_col = hpos;
12721 hlinfo->mouse_face_beg_row = vpos;
12722 hlinfo->mouse_face_beg_x = x;
12723 hlinfo->mouse_face_past_end = false;
12724
12725 hlinfo->mouse_face_end_col = hpos + 1;
12726 hlinfo->mouse_face_end_row = vpos;
12727 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12728 hlinfo->mouse_face_window = window;
12729 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12730
12731 /* Display it as active. */
12732 show_mouse_face (hlinfo, draw);
12733 }
12734
12735 set_help_echo:
12736
12737 /* Set help_echo_string to a help string to display for this tool-bar item.
12738 XTread_socket does the rest. */
12739 help_echo_object = help_echo_window = Qnil;
12740 help_echo_pos = -1;
12741 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12742 if (NILP (help_echo_string))
12743 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12744 }
12745
12746 #endif /* !USE_GTK && !HAVE_NS */
12747
12748 #endif /* HAVE_WINDOW_SYSTEM */
12749
12750
12751 \f
12752 /************************************************************************
12753 Horizontal scrolling
12754 ************************************************************************/
12755
12756 /* For all leaf windows in the window tree rooted at WINDOW, set their
12757 hscroll value so that PT is (i) visible in the window, and (ii) so
12758 that it is not within a certain margin at the window's left and
12759 right border. Value is true if any window's hscroll has been
12760 changed. */
12761
12762 static bool
12763 hscroll_window_tree (Lisp_Object window)
12764 {
12765 bool hscrolled_p = false;
12766 bool hscroll_relative_p = FLOATP (Vhscroll_step);
12767 int hscroll_step_abs = 0;
12768 double hscroll_step_rel = 0;
12769
12770 if (hscroll_relative_p)
12771 {
12772 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12773 if (hscroll_step_rel < 0)
12774 {
12775 hscroll_relative_p = false;
12776 hscroll_step_abs = 0;
12777 }
12778 }
12779 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12780 {
12781 hscroll_step_abs = XINT (Vhscroll_step);
12782 if (hscroll_step_abs < 0)
12783 hscroll_step_abs = 0;
12784 }
12785 else
12786 hscroll_step_abs = 0;
12787
12788 while (WINDOWP (window))
12789 {
12790 struct window *w = XWINDOW (window);
12791
12792 if (WINDOWP (w->contents))
12793 hscrolled_p |= hscroll_window_tree (w->contents);
12794 else if (w->cursor.vpos >= 0)
12795 {
12796 int h_margin;
12797 int text_area_width;
12798 struct glyph_row *cursor_row;
12799 struct glyph_row *bottom_row;
12800
12801 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12802 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12803 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12804 else
12805 cursor_row = bottom_row - 1;
12806
12807 if (!cursor_row->enabled_p)
12808 {
12809 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12810 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12811 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12812 else
12813 cursor_row = bottom_row - 1;
12814 }
12815 bool row_r2l_p = cursor_row->reversed_p;
12816
12817 text_area_width = window_box_width (w, TEXT_AREA);
12818
12819 /* Scroll when cursor is inside this scroll margin. */
12820 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12821
12822 /* If the position of this window's point has explicitly
12823 changed, no more suspend auto hscrolling. */
12824 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12825 w->suspend_auto_hscroll = false;
12826
12827 /* Remember window point. */
12828 Fset_marker (w->old_pointm,
12829 ((w == XWINDOW (selected_window))
12830 ? make_number (BUF_PT (XBUFFER (w->contents)))
12831 : Fmarker_position (w->pointm)),
12832 w->contents);
12833
12834 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12835 && !w->suspend_auto_hscroll
12836 /* In some pathological cases, like restoring a window
12837 configuration into a frame that is much smaller than
12838 the one from which the configuration was saved, we
12839 get glyph rows whose start and end have zero buffer
12840 positions, which we cannot handle below. Just skip
12841 such windows. */
12842 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12843 /* For left-to-right rows, hscroll when cursor is either
12844 (i) inside the right hscroll margin, or (ii) if it is
12845 inside the left margin and the window is already
12846 hscrolled. */
12847 && ((!row_r2l_p
12848 && ((w->hscroll && w->cursor.x <= h_margin)
12849 || (cursor_row->enabled_p
12850 && cursor_row->truncated_on_right_p
12851 && (w->cursor.x >= text_area_width - h_margin))))
12852 /* For right-to-left rows, the logic is similar,
12853 except that rules for scrolling to left and right
12854 are reversed. E.g., if cursor.x <= h_margin, we
12855 need to hscroll "to the right" unconditionally,
12856 and that will scroll the screen to the left so as
12857 to reveal the next portion of the row. */
12858 || (row_r2l_p
12859 && ((cursor_row->enabled_p
12860 /* FIXME: It is confusing to set the
12861 truncated_on_right_p flag when R2L rows
12862 are actually truncated on the left. */
12863 && cursor_row->truncated_on_right_p
12864 && w->cursor.x <= h_margin)
12865 || (w->hscroll
12866 && (w->cursor.x >= text_area_width - h_margin))))))
12867 {
12868 struct it it;
12869 ptrdiff_t hscroll;
12870 struct buffer *saved_current_buffer;
12871 ptrdiff_t pt;
12872 int wanted_x;
12873
12874 /* Find point in a display of infinite width. */
12875 saved_current_buffer = current_buffer;
12876 current_buffer = XBUFFER (w->contents);
12877
12878 if (w == XWINDOW (selected_window))
12879 pt = PT;
12880 else
12881 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12882
12883 /* Move iterator to pt starting at cursor_row->start in
12884 a line with infinite width. */
12885 init_to_row_start (&it, w, cursor_row);
12886 it.last_visible_x = INFINITY;
12887 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12888 current_buffer = saved_current_buffer;
12889
12890 /* Position cursor in window. */
12891 if (!hscroll_relative_p && hscroll_step_abs == 0)
12892 hscroll = max (0, (it.current_x
12893 - (ITERATOR_AT_END_OF_LINE_P (&it)
12894 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12895 : (text_area_width / 2))))
12896 / FRAME_COLUMN_WIDTH (it.f);
12897 else if ((!row_r2l_p
12898 && w->cursor.x >= text_area_width - h_margin)
12899 || (row_r2l_p && w->cursor.x <= h_margin))
12900 {
12901 if (hscroll_relative_p)
12902 wanted_x = text_area_width * (1 - hscroll_step_rel)
12903 - h_margin;
12904 else
12905 wanted_x = text_area_width
12906 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12907 - h_margin;
12908 hscroll
12909 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12910 }
12911 else
12912 {
12913 if (hscroll_relative_p)
12914 wanted_x = text_area_width * hscroll_step_rel
12915 + h_margin;
12916 else
12917 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12918 + h_margin;
12919 hscroll
12920 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12921 }
12922 hscroll = max (hscroll, w->min_hscroll);
12923
12924 /* Don't prevent redisplay optimizations if hscroll
12925 hasn't changed, as it will unnecessarily slow down
12926 redisplay. */
12927 if (w->hscroll != hscroll)
12928 {
12929 struct buffer *b = XBUFFER (w->contents);
12930 b->prevent_redisplay_optimizations_p = true;
12931 w->hscroll = hscroll;
12932 hscrolled_p = true;
12933 }
12934 }
12935 }
12936
12937 window = w->next;
12938 }
12939
12940 /* Value is true if hscroll of any leaf window has been changed. */
12941 return hscrolled_p;
12942 }
12943
12944
12945 /* Set hscroll so that cursor is visible and not inside horizontal
12946 scroll margins for all windows in the tree rooted at WINDOW. See
12947 also hscroll_window_tree above. Value is true if any window's
12948 hscroll has been changed. If it has, desired matrices on the frame
12949 of WINDOW are cleared. */
12950
12951 static bool
12952 hscroll_windows (Lisp_Object window)
12953 {
12954 bool hscrolled_p = hscroll_window_tree (window);
12955 if (hscrolled_p)
12956 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12957 return hscrolled_p;
12958 }
12959
12960
12961 \f
12962 /************************************************************************
12963 Redisplay
12964 ************************************************************************/
12965
12966 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
12967 This is sometimes handy to have in a debugger session. */
12968
12969 #ifdef GLYPH_DEBUG
12970
12971 /* First and last unchanged row for try_window_id. */
12972
12973 static int debug_first_unchanged_at_end_vpos;
12974 static int debug_last_unchanged_at_beg_vpos;
12975
12976 /* Delta vpos and y. */
12977
12978 static int debug_dvpos, debug_dy;
12979
12980 /* Delta in characters and bytes for try_window_id. */
12981
12982 static ptrdiff_t debug_delta, debug_delta_bytes;
12983
12984 /* Values of window_end_pos and window_end_vpos at the end of
12985 try_window_id. */
12986
12987 static ptrdiff_t debug_end_vpos;
12988
12989 /* Append a string to W->desired_matrix->method. FMT is a printf
12990 format string. If trace_redisplay_p is true also printf the
12991 resulting string to stderr. */
12992
12993 static void debug_method_add (struct window *, char const *, ...)
12994 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12995
12996 static void
12997 debug_method_add (struct window *w, char const *fmt, ...)
12998 {
12999 void *ptr = w;
13000 char *method = w->desired_matrix->method;
13001 int len = strlen (method);
13002 int size = sizeof w->desired_matrix->method;
13003 int remaining = size - len - 1;
13004 va_list ap;
13005
13006 if (len && remaining)
13007 {
13008 method[len] = '|';
13009 --remaining, ++len;
13010 }
13011
13012 va_start (ap, fmt);
13013 vsnprintf (method + len, remaining + 1, fmt, ap);
13014 va_end (ap);
13015
13016 if (trace_redisplay_p)
13017 fprintf (stderr, "%p (%s): %s\n",
13018 ptr,
13019 ((BUFFERP (w->contents)
13020 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13021 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13022 : "no buffer"),
13023 method + len);
13024 }
13025
13026 #endif /* GLYPH_DEBUG */
13027
13028
13029 /* Value is true if all changes in window W, which displays
13030 current_buffer, are in the text between START and END. START is a
13031 buffer position, END is given as a distance from Z. Used in
13032 redisplay_internal for display optimization. */
13033
13034 static bool
13035 text_outside_line_unchanged_p (struct window *w,
13036 ptrdiff_t start, ptrdiff_t end)
13037 {
13038 bool unchanged_p = true;
13039
13040 /* If text or overlays have changed, see where. */
13041 if (window_outdated (w))
13042 {
13043 /* Gap in the line? */
13044 if (GPT < start || Z - GPT < end)
13045 unchanged_p = false;
13046
13047 /* Changes start in front of the line, or end after it? */
13048 if (unchanged_p
13049 && (BEG_UNCHANGED < start - 1
13050 || END_UNCHANGED < end))
13051 unchanged_p = false;
13052
13053 /* If selective display, can't optimize if changes start at the
13054 beginning of the line. */
13055 if (unchanged_p
13056 && INTEGERP (BVAR (current_buffer, selective_display))
13057 && XINT (BVAR (current_buffer, selective_display)) > 0
13058 && (BEG_UNCHANGED < start || GPT <= start))
13059 unchanged_p = false;
13060
13061 /* If there are overlays at the start or end of the line, these
13062 may have overlay strings with newlines in them. A change at
13063 START, for instance, may actually concern the display of such
13064 overlay strings as well, and they are displayed on different
13065 lines. So, quickly rule out this case. (For the future, it
13066 might be desirable to implement something more telling than
13067 just BEG/END_UNCHANGED.) */
13068 if (unchanged_p)
13069 {
13070 if (BEG + BEG_UNCHANGED == start
13071 && overlay_touches_p (start))
13072 unchanged_p = false;
13073 if (END_UNCHANGED == end
13074 && overlay_touches_p (Z - end))
13075 unchanged_p = false;
13076 }
13077
13078 /* Under bidi reordering, adding or deleting a character in the
13079 beginning of a paragraph, before the first strong directional
13080 character, can change the base direction of the paragraph (unless
13081 the buffer specifies a fixed paragraph direction), which will
13082 require to redisplay the whole paragraph. It might be worthwhile
13083 to find the paragraph limits and widen the range of redisplayed
13084 lines to that, but for now just give up this optimization. */
13085 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13086 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13087 unchanged_p = false;
13088 }
13089
13090 return unchanged_p;
13091 }
13092
13093
13094 /* Do a frame update, taking possible shortcuts into account. This is
13095 the main external entry point for redisplay.
13096
13097 If the last redisplay displayed an echo area message and that message
13098 is no longer requested, we clear the echo area or bring back the
13099 mini-buffer if that is in use. */
13100
13101 void
13102 redisplay (void)
13103 {
13104 redisplay_internal ();
13105 }
13106
13107
13108 static Lisp_Object
13109 overlay_arrow_string_or_property (Lisp_Object var)
13110 {
13111 Lisp_Object val;
13112
13113 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13114 return val;
13115
13116 return Voverlay_arrow_string;
13117 }
13118
13119 /* Return true if there are any overlay-arrows in current_buffer. */
13120 static bool
13121 overlay_arrow_in_current_buffer_p (void)
13122 {
13123 Lisp_Object vlist;
13124
13125 for (vlist = Voverlay_arrow_variable_list;
13126 CONSP (vlist);
13127 vlist = XCDR (vlist))
13128 {
13129 Lisp_Object var = XCAR (vlist);
13130 Lisp_Object val;
13131
13132 if (!SYMBOLP (var))
13133 continue;
13134 val = find_symbol_value (var);
13135 if (MARKERP (val)
13136 && current_buffer == XMARKER (val)->buffer)
13137 return true;
13138 }
13139 return false;
13140 }
13141
13142
13143 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13144 has changed. */
13145
13146 static bool
13147 overlay_arrows_changed_p (void)
13148 {
13149 Lisp_Object vlist;
13150
13151 for (vlist = Voverlay_arrow_variable_list;
13152 CONSP (vlist);
13153 vlist = XCDR (vlist))
13154 {
13155 Lisp_Object var = XCAR (vlist);
13156 Lisp_Object val, pstr;
13157
13158 if (!SYMBOLP (var))
13159 continue;
13160 val = find_symbol_value (var);
13161 if (!MARKERP (val))
13162 continue;
13163 if (! EQ (COERCE_MARKER (val),
13164 Fget (var, Qlast_arrow_position))
13165 || ! (pstr = overlay_arrow_string_or_property (var),
13166 EQ (pstr, Fget (var, Qlast_arrow_string))))
13167 return true;
13168 }
13169 return false;
13170 }
13171
13172 /* Mark overlay arrows to be updated on next redisplay. */
13173
13174 static void
13175 update_overlay_arrows (int up_to_date)
13176 {
13177 Lisp_Object vlist;
13178
13179 for (vlist = Voverlay_arrow_variable_list;
13180 CONSP (vlist);
13181 vlist = XCDR (vlist))
13182 {
13183 Lisp_Object var = XCAR (vlist);
13184
13185 if (!SYMBOLP (var))
13186 continue;
13187
13188 if (up_to_date > 0)
13189 {
13190 Lisp_Object val = find_symbol_value (var);
13191 Fput (var, Qlast_arrow_position,
13192 COERCE_MARKER (val));
13193 Fput (var, Qlast_arrow_string,
13194 overlay_arrow_string_or_property (var));
13195 }
13196 else if (up_to_date < 0
13197 || !NILP (Fget (var, Qlast_arrow_position)))
13198 {
13199 Fput (var, Qlast_arrow_position, Qt);
13200 Fput (var, Qlast_arrow_string, Qt);
13201 }
13202 }
13203 }
13204
13205
13206 /* Return overlay arrow string to display at row.
13207 Return integer (bitmap number) for arrow bitmap in left fringe.
13208 Return nil if no overlay arrow. */
13209
13210 static Lisp_Object
13211 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13212 {
13213 Lisp_Object vlist;
13214
13215 for (vlist = Voverlay_arrow_variable_list;
13216 CONSP (vlist);
13217 vlist = XCDR (vlist))
13218 {
13219 Lisp_Object var = XCAR (vlist);
13220 Lisp_Object val;
13221
13222 if (!SYMBOLP (var))
13223 continue;
13224
13225 val = find_symbol_value (var);
13226
13227 if (MARKERP (val)
13228 && current_buffer == XMARKER (val)->buffer
13229 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13230 {
13231 if (FRAME_WINDOW_P (it->f)
13232 /* FIXME: if ROW->reversed_p is set, this should test
13233 the right fringe, not the left one. */
13234 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13235 {
13236 #ifdef HAVE_WINDOW_SYSTEM
13237 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13238 {
13239 int fringe_bitmap = lookup_fringe_bitmap (val);
13240 if (fringe_bitmap != 0)
13241 return make_number (fringe_bitmap);
13242 }
13243 #endif
13244 return make_number (-1); /* Use default arrow bitmap. */
13245 }
13246 return overlay_arrow_string_or_property (var);
13247 }
13248 }
13249
13250 return Qnil;
13251 }
13252
13253 /* Return true if point moved out of or into a composition. Otherwise
13254 return false. PREV_BUF and PREV_PT are the last point buffer and
13255 position. BUF and PT are the current point buffer and position. */
13256
13257 static bool
13258 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13259 struct buffer *buf, ptrdiff_t pt)
13260 {
13261 ptrdiff_t start, end;
13262 Lisp_Object prop;
13263 Lisp_Object buffer;
13264
13265 XSETBUFFER (buffer, buf);
13266 /* Check a composition at the last point if point moved within the
13267 same buffer. */
13268 if (prev_buf == buf)
13269 {
13270 if (prev_pt == pt)
13271 /* Point didn't move. */
13272 return false;
13273
13274 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13275 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13276 && composition_valid_p (start, end, prop)
13277 && start < prev_pt && end > prev_pt)
13278 /* The last point was within the composition. Return true iff
13279 point moved out of the composition. */
13280 return (pt <= start || pt >= end);
13281 }
13282
13283 /* Check a composition at the current point. */
13284 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13285 && find_composition (pt, -1, &start, &end, &prop, buffer)
13286 && composition_valid_p (start, end, prop)
13287 && start < pt && end > pt);
13288 }
13289
13290 /* Reconsider the clip changes of buffer which is displayed in W. */
13291
13292 static void
13293 reconsider_clip_changes (struct window *w)
13294 {
13295 struct buffer *b = XBUFFER (w->contents);
13296
13297 if (b->clip_changed
13298 && w->window_end_valid
13299 && w->current_matrix->buffer == b
13300 && w->current_matrix->zv == BUF_ZV (b)
13301 && w->current_matrix->begv == BUF_BEGV (b))
13302 b->clip_changed = false;
13303
13304 /* If display wasn't paused, and W is not a tool bar window, see if
13305 point has been moved into or out of a composition. In that case,
13306 set b->clip_changed to force updating the screen. If
13307 b->clip_changed has already been set, skip this check. */
13308 if (!b->clip_changed && w->window_end_valid)
13309 {
13310 ptrdiff_t pt = (w == XWINDOW (selected_window)
13311 ? PT : marker_position (w->pointm));
13312
13313 if ((w->current_matrix->buffer != b || pt != w->last_point)
13314 && check_point_in_composition (w->current_matrix->buffer,
13315 w->last_point, b, pt))
13316 b->clip_changed = true;
13317 }
13318 }
13319
13320 static void
13321 propagate_buffer_redisplay (void)
13322 { /* Resetting b->text->redisplay is problematic!
13323 We can't just reset it in the case that some window that displays
13324 it has not been redisplayed; and such a window can stay
13325 unredisplayed for a long time if it's currently invisible.
13326 But we do want to reset it at the end of redisplay otherwise
13327 its displayed windows will keep being redisplayed over and over
13328 again.
13329 So we copy all b->text->redisplay flags up to their windows here,
13330 such that mark_window_display_accurate can safely reset
13331 b->text->redisplay. */
13332 Lisp_Object ws = window_list ();
13333 for (; CONSP (ws); ws = XCDR (ws))
13334 {
13335 struct window *thisw = XWINDOW (XCAR (ws));
13336 struct buffer *thisb = XBUFFER (thisw->contents);
13337 if (thisb->text->redisplay)
13338 thisw->redisplay = true;
13339 }
13340 }
13341
13342 #define STOP_POLLING \
13343 do { if (! polling_stopped_here) stop_polling (); \
13344 polling_stopped_here = true; } while (false)
13345
13346 #define RESUME_POLLING \
13347 do { if (polling_stopped_here) start_polling (); \
13348 polling_stopped_here = false; } while (false)
13349
13350
13351 /* Perhaps in the future avoid recentering windows if it
13352 is not necessary; currently that causes some problems. */
13353
13354 static void
13355 redisplay_internal (void)
13356 {
13357 struct window *w = XWINDOW (selected_window);
13358 struct window *sw;
13359 struct frame *fr;
13360 bool pending;
13361 bool must_finish = false, match_p;
13362 struct text_pos tlbufpos, tlendpos;
13363 int number_of_visible_frames;
13364 ptrdiff_t count;
13365 struct frame *sf;
13366 bool polling_stopped_here = false;
13367 Lisp_Object tail, frame;
13368
13369 /* True means redisplay has to consider all windows on all
13370 frames. False, only selected_window is considered. */
13371 bool consider_all_windows_p;
13372
13373 /* True means redisplay has to redisplay the miniwindow. */
13374 bool update_miniwindow_p = false;
13375
13376 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13377
13378 /* No redisplay if running in batch mode or frame is not yet fully
13379 initialized, or redisplay is explicitly turned off by setting
13380 Vinhibit_redisplay. */
13381 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13382 || !NILP (Vinhibit_redisplay))
13383 return;
13384
13385 /* Don't examine these until after testing Vinhibit_redisplay.
13386 When Emacs is shutting down, perhaps because its connection to
13387 X has dropped, we should not look at them at all. */
13388 fr = XFRAME (w->frame);
13389 sf = SELECTED_FRAME ();
13390
13391 if (!fr->glyphs_initialized_p)
13392 return;
13393
13394 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13395 if (popup_activated ())
13396 return;
13397 #endif
13398
13399 /* I don't think this happens but let's be paranoid. */
13400 if (redisplaying_p)
13401 return;
13402
13403 /* Record a function that clears redisplaying_p
13404 when we leave this function. */
13405 count = SPECPDL_INDEX ();
13406 record_unwind_protect_void (unwind_redisplay);
13407 redisplaying_p = true;
13408 specbind (Qinhibit_free_realized_faces, Qnil);
13409
13410 /* Record this function, so it appears on the profiler's backtraces. */
13411 record_in_backtrace (Qredisplay_internal, 0, 0);
13412
13413 FOR_EACH_FRAME (tail, frame)
13414 XFRAME (frame)->already_hscrolled_p = false;
13415
13416 retry:
13417 /* Remember the currently selected window. */
13418 sw = w;
13419
13420 pending = false;
13421 forget_escape_and_glyphless_faces ();
13422
13423 inhibit_free_realized_faces = false;
13424
13425 /* If face_change, init_iterator will free all realized faces, which
13426 includes the faces referenced from current matrices. So, we
13427 can't reuse current matrices in this case. */
13428 if (face_change)
13429 windows_or_buffers_changed = 47;
13430
13431 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13432 && FRAME_TTY (sf)->previous_frame != sf)
13433 {
13434 /* Since frames on a single ASCII terminal share the same
13435 display area, displaying a different frame means redisplay
13436 the whole thing. */
13437 SET_FRAME_GARBAGED (sf);
13438 #ifndef DOS_NT
13439 set_tty_color_mode (FRAME_TTY (sf), sf);
13440 #endif
13441 FRAME_TTY (sf)->previous_frame = sf;
13442 }
13443
13444 /* Set the visible flags for all frames. Do this before checking for
13445 resized or garbaged frames; they want to know if their frames are
13446 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13447 number_of_visible_frames = 0;
13448
13449 FOR_EACH_FRAME (tail, frame)
13450 {
13451 struct frame *f = XFRAME (frame);
13452
13453 if (FRAME_VISIBLE_P (f))
13454 {
13455 ++number_of_visible_frames;
13456 /* Adjust matrices for visible frames only. */
13457 if (f->fonts_changed)
13458 {
13459 adjust_frame_glyphs (f);
13460 /* Disable all redisplay optimizations for this frame.
13461 This is because adjust_frame_glyphs resets the
13462 enabled_p flag for all glyph rows of all windows, so
13463 many optimizations will fail anyway, and some might
13464 fail to test that flag and do bogus things as
13465 result. */
13466 SET_FRAME_GARBAGED (f);
13467 f->fonts_changed = false;
13468 }
13469 /* If cursor type has been changed on the frame
13470 other than selected, consider all frames. */
13471 if (f != sf && f->cursor_type_changed)
13472 fset_redisplay (f);
13473 }
13474 clear_desired_matrices (f);
13475 }
13476
13477 /* Notice any pending interrupt request to change frame size. */
13478 do_pending_window_change (true);
13479
13480 /* do_pending_window_change could change the selected_window due to
13481 frame resizing which makes the selected window too small. */
13482 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13483 sw = w;
13484
13485 /* Clear frames marked as garbaged. */
13486 clear_garbaged_frames ();
13487
13488 /* Build menubar and tool-bar items. */
13489 if (NILP (Vmemory_full))
13490 prepare_menu_bars ();
13491
13492 reconsider_clip_changes (w);
13493
13494 /* In most cases selected window displays current buffer. */
13495 match_p = XBUFFER (w->contents) == current_buffer;
13496 if (match_p)
13497 {
13498 /* Detect case that we need to write or remove a star in the mode line. */
13499 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13500 w->update_mode_line = true;
13501
13502 if (mode_line_update_needed (w))
13503 w->update_mode_line = true;
13504
13505 /* If reconsider_clip_changes above decided that the narrowing
13506 in the current buffer changed, make sure all other windows
13507 showing that buffer will be redisplayed. */
13508 if (current_buffer->clip_changed)
13509 bset_update_mode_line (current_buffer);
13510 }
13511
13512 /* Normally the message* functions will have already displayed and
13513 updated the echo area, but the frame may have been trashed, or
13514 the update may have been preempted, so display the echo area
13515 again here. Checking message_cleared_p captures the case that
13516 the echo area should be cleared. */
13517 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13518 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13519 || (message_cleared_p
13520 && minibuf_level == 0
13521 /* If the mini-window is currently selected, this means the
13522 echo-area doesn't show through. */
13523 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13524 {
13525 echo_area_display (false);
13526
13527 if (message_cleared_p)
13528 update_miniwindow_p = true;
13529
13530 must_finish = true;
13531
13532 /* If we don't display the current message, don't clear the
13533 message_cleared_p flag, because, if we did, we wouldn't clear
13534 the echo area in the next redisplay which doesn't preserve
13535 the echo area. */
13536 if (!display_last_displayed_message_p)
13537 message_cleared_p = false;
13538 }
13539 else if (EQ (selected_window, minibuf_window)
13540 && (current_buffer->clip_changed || window_outdated (w))
13541 && resize_mini_window (w, false))
13542 {
13543 /* Resized active mini-window to fit the size of what it is
13544 showing if its contents might have changed. */
13545 must_finish = true;
13546
13547 /* If window configuration was changed, frames may have been
13548 marked garbaged. Clear them or we will experience
13549 surprises wrt scrolling. */
13550 clear_garbaged_frames ();
13551 }
13552
13553 if (windows_or_buffers_changed && !update_mode_lines)
13554 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13555 only the windows's contents needs to be refreshed, or whether the
13556 mode-lines also need a refresh. */
13557 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13558 ? REDISPLAY_SOME : 32);
13559
13560 /* If specs for an arrow have changed, do thorough redisplay
13561 to ensure we remove any arrow that should no longer exist. */
13562 if (overlay_arrows_changed_p ())
13563 /* Apparently, this is the only case where we update other windows,
13564 without updating other mode-lines. */
13565 windows_or_buffers_changed = 49;
13566
13567 consider_all_windows_p = (update_mode_lines
13568 || windows_or_buffers_changed);
13569
13570 #define AINC(a,i) \
13571 { \
13572 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13573 if (INTEGERP (entry)) \
13574 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13575 }
13576
13577 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13578 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13579
13580 /* Optimize the case that only the line containing the cursor in the
13581 selected window has changed. Variables starting with this_ are
13582 set in display_line and record information about the line
13583 containing the cursor. */
13584 tlbufpos = this_line_start_pos;
13585 tlendpos = this_line_end_pos;
13586 if (!consider_all_windows_p
13587 && CHARPOS (tlbufpos) > 0
13588 && !w->update_mode_line
13589 && !current_buffer->clip_changed
13590 && !current_buffer->prevent_redisplay_optimizations_p
13591 && FRAME_VISIBLE_P (XFRAME (w->frame))
13592 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13593 && !XFRAME (w->frame)->cursor_type_changed
13594 && !XFRAME (w->frame)->face_change
13595 /* Make sure recorded data applies to current buffer, etc. */
13596 && this_line_buffer == current_buffer
13597 && match_p
13598 && !w->force_start
13599 && !w->optional_new_start
13600 /* Point must be on the line that we have info recorded about. */
13601 && PT >= CHARPOS (tlbufpos)
13602 && PT <= Z - CHARPOS (tlendpos)
13603 /* All text outside that line, including its final newline,
13604 must be unchanged. */
13605 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13606 CHARPOS (tlendpos)))
13607 {
13608 if (CHARPOS (tlbufpos) > BEGV
13609 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13610 && (CHARPOS (tlbufpos) == ZV
13611 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13612 /* Former continuation line has disappeared by becoming empty. */
13613 goto cancel;
13614 else if (window_outdated (w) || MINI_WINDOW_P (w))
13615 {
13616 /* We have to handle the case of continuation around a
13617 wide-column character (see the comment in indent.c around
13618 line 1340).
13619
13620 For instance, in the following case:
13621
13622 -------- Insert --------
13623 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13624 J_I_ ==> J_I_ `^^' are cursors.
13625 ^^ ^^
13626 -------- --------
13627
13628 As we have to redraw the line above, we cannot use this
13629 optimization. */
13630
13631 struct it it;
13632 int line_height_before = this_line_pixel_height;
13633
13634 /* Note that start_display will handle the case that the
13635 line starting at tlbufpos is a continuation line. */
13636 start_display (&it, w, tlbufpos);
13637
13638 /* Implementation note: It this still necessary? */
13639 if (it.current_x != this_line_start_x)
13640 goto cancel;
13641
13642 TRACE ((stderr, "trying display optimization 1\n"));
13643 w->cursor.vpos = -1;
13644 overlay_arrow_seen = false;
13645 it.vpos = this_line_vpos;
13646 it.current_y = this_line_y;
13647 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13648 display_line (&it);
13649
13650 /* If line contains point, is not continued,
13651 and ends at same distance from eob as before, we win. */
13652 if (w->cursor.vpos >= 0
13653 /* Line is not continued, otherwise this_line_start_pos
13654 would have been set to 0 in display_line. */
13655 && CHARPOS (this_line_start_pos)
13656 /* Line ends as before. */
13657 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13658 /* Line has same height as before. Otherwise other lines
13659 would have to be shifted up or down. */
13660 && this_line_pixel_height == line_height_before)
13661 {
13662 /* If this is not the window's last line, we must adjust
13663 the charstarts of the lines below. */
13664 if (it.current_y < it.last_visible_y)
13665 {
13666 struct glyph_row *row
13667 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13668 ptrdiff_t delta, delta_bytes;
13669
13670 /* We used to distinguish between two cases here,
13671 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13672 when the line ends in a newline or the end of the
13673 buffer's accessible portion. But both cases did
13674 the same, so they were collapsed. */
13675 delta = (Z
13676 - CHARPOS (tlendpos)
13677 - MATRIX_ROW_START_CHARPOS (row));
13678 delta_bytes = (Z_BYTE
13679 - BYTEPOS (tlendpos)
13680 - MATRIX_ROW_START_BYTEPOS (row));
13681
13682 increment_matrix_positions (w->current_matrix,
13683 this_line_vpos + 1,
13684 w->current_matrix->nrows,
13685 delta, delta_bytes);
13686 }
13687
13688 /* If this row displays text now but previously didn't,
13689 or vice versa, w->window_end_vpos may have to be
13690 adjusted. */
13691 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13692 {
13693 if (w->window_end_vpos < this_line_vpos)
13694 w->window_end_vpos = this_line_vpos;
13695 }
13696 else if (w->window_end_vpos == this_line_vpos
13697 && this_line_vpos > 0)
13698 w->window_end_vpos = this_line_vpos - 1;
13699 w->window_end_valid = false;
13700
13701 /* Update hint: No need to try to scroll in update_window. */
13702 w->desired_matrix->no_scrolling_p = true;
13703
13704 #ifdef GLYPH_DEBUG
13705 *w->desired_matrix->method = 0;
13706 debug_method_add (w, "optimization 1");
13707 #endif
13708 #ifdef HAVE_WINDOW_SYSTEM
13709 update_window_fringes (w, false);
13710 #endif
13711 goto update;
13712 }
13713 else
13714 goto cancel;
13715 }
13716 else if (/* Cursor position hasn't changed. */
13717 PT == w->last_point
13718 /* Make sure the cursor was last displayed
13719 in this window. Otherwise we have to reposition it. */
13720
13721 /* PXW: Must be converted to pixels, probably. */
13722 && 0 <= w->cursor.vpos
13723 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13724 {
13725 if (!must_finish)
13726 {
13727 do_pending_window_change (true);
13728 /* If selected_window changed, redisplay again. */
13729 if (WINDOWP (selected_window)
13730 && (w = XWINDOW (selected_window)) != sw)
13731 goto retry;
13732
13733 /* We used to always goto end_of_redisplay here, but this
13734 isn't enough if we have a blinking cursor. */
13735 if (w->cursor_off_p == w->last_cursor_off_p)
13736 goto end_of_redisplay;
13737 }
13738 goto update;
13739 }
13740 /* If highlighting the region, or if the cursor is in the echo area,
13741 then we can't just move the cursor. */
13742 else if (NILP (Vshow_trailing_whitespace)
13743 && !cursor_in_echo_area)
13744 {
13745 struct it it;
13746 struct glyph_row *row;
13747
13748 /* Skip from tlbufpos to PT and see where it is. Note that
13749 PT may be in invisible text. If so, we will end at the
13750 next visible position. */
13751 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13752 NULL, DEFAULT_FACE_ID);
13753 it.current_x = this_line_start_x;
13754 it.current_y = this_line_y;
13755 it.vpos = this_line_vpos;
13756
13757 /* The call to move_it_to stops in front of PT, but
13758 moves over before-strings. */
13759 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13760
13761 if (it.vpos == this_line_vpos
13762 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13763 row->enabled_p))
13764 {
13765 eassert (this_line_vpos == it.vpos);
13766 eassert (this_line_y == it.current_y);
13767 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13768 #ifdef GLYPH_DEBUG
13769 *w->desired_matrix->method = 0;
13770 debug_method_add (w, "optimization 3");
13771 #endif
13772 goto update;
13773 }
13774 else
13775 goto cancel;
13776 }
13777
13778 cancel:
13779 /* Text changed drastically or point moved off of line. */
13780 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13781 }
13782
13783 CHARPOS (this_line_start_pos) = 0;
13784 ++clear_face_cache_count;
13785 #ifdef HAVE_WINDOW_SYSTEM
13786 ++clear_image_cache_count;
13787 #endif
13788
13789 /* Build desired matrices, and update the display. If
13790 consider_all_windows_p, do it for all windows on all frames that
13791 require redisplay, as specified by their 'redisplay' flag.
13792 Otherwise do it for selected_window, only. */
13793
13794 if (consider_all_windows_p)
13795 {
13796 FOR_EACH_FRAME (tail, frame)
13797 XFRAME (frame)->updated_p = false;
13798
13799 propagate_buffer_redisplay ();
13800
13801 FOR_EACH_FRAME (tail, frame)
13802 {
13803 struct frame *f = XFRAME (frame);
13804
13805 /* We don't have to do anything for unselected terminal
13806 frames. */
13807 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13808 && !EQ (FRAME_TTY (f)->top_frame, frame))
13809 continue;
13810
13811 retry_frame:
13812 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13813 {
13814 bool gcscrollbars
13815 /* Only GC scrollbars when we redisplay the whole frame. */
13816 = f->redisplay || !REDISPLAY_SOME_P ();
13817 bool f_redisplay_flag = f->redisplay;
13818 /* Mark all the scroll bars to be removed; we'll redeem
13819 the ones we want when we redisplay their windows. */
13820 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13821 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13822
13823 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13824 redisplay_windows (FRAME_ROOT_WINDOW (f));
13825 /* Remember that the invisible frames need to be redisplayed next
13826 time they're visible. */
13827 else if (!REDISPLAY_SOME_P ())
13828 f->redisplay = true;
13829
13830 /* The X error handler may have deleted that frame. */
13831 if (!FRAME_LIVE_P (f))
13832 continue;
13833
13834 /* Any scroll bars which redisplay_windows should have
13835 nuked should now go away. */
13836 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13837 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13838
13839 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13840 {
13841 /* If fonts changed on visible frame, display again. */
13842 if (f->fonts_changed)
13843 {
13844 adjust_frame_glyphs (f);
13845 /* Disable all redisplay optimizations for this
13846 frame. For the reasons, see the comment near
13847 the previous call to adjust_frame_glyphs above. */
13848 SET_FRAME_GARBAGED (f);
13849 f->fonts_changed = false;
13850 goto retry_frame;
13851 }
13852
13853 /* See if we have to hscroll. */
13854 if (!f->already_hscrolled_p)
13855 {
13856 f->already_hscrolled_p = true;
13857 if (hscroll_windows (f->root_window))
13858 goto retry_frame;
13859 }
13860
13861 /* If the frame's redisplay flag was not set before
13862 we went about redisplaying its windows, but it is
13863 set now, that means we employed some redisplay
13864 optimizations inside redisplay_windows, and
13865 bypassed producing some screen lines. But if
13866 f->redisplay is now set, it might mean the old
13867 faces are no longer valid (e.g., if redisplaying
13868 some window called some Lisp which defined a new
13869 face or redefined an existing face), so trying to
13870 use them in update_frame will segfault.
13871 Therefore, we must redisplay this frame. */
13872 if (!f_redisplay_flag && f->redisplay)
13873 goto retry_frame;
13874
13875 /* Prevent various kinds of signals during display
13876 update. stdio is not robust about handling
13877 signals, which can cause an apparent I/O error. */
13878 if (interrupt_input)
13879 unrequest_sigio ();
13880 STOP_POLLING;
13881
13882 pending |= update_frame (f, false, false);
13883 f->cursor_type_changed = false;
13884 f->updated_p = true;
13885 }
13886 }
13887 }
13888
13889 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13890
13891 if (!pending)
13892 {
13893 /* Do the mark_window_display_accurate after all windows have
13894 been redisplayed because this call resets flags in buffers
13895 which are needed for proper redisplay. */
13896 FOR_EACH_FRAME (tail, frame)
13897 {
13898 struct frame *f = XFRAME (frame);
13899 if (f->updated_p)
13900 {
13901 f->redisplay = false;
13902 mark_window_display_accurate (f->root_window, true);
13903 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13904 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13905 }
13906 }
13907 }
13908 }
13909 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13910 {
13911 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13912 struct frame *mini_frame;
13913
13914 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13915 /* Use list_of_error, not Qerror, so that
13916 we catch only errors and don't run the debugger. */
13917 internal_condition_case_1 (redisplay_window_1, selected_window,
13918 list_of_error,
13919 redisplay_window_error);
13920 if (update_miniwindow_p)
13921 internal_condition_case_1 (redisplay_window_1, mini_window,
13922 list_of_error,
13923 redisplay_window_error);
13924
13925 /* Compare desired and current matrices, perform output. */
13926
13927 update:
13928 /* If fonts changed, display again. Likewise if redisplay_window_1
13929 above caused some change (e.g., a change in faces) that requires
13930 considering the entire frame again. */
13931 if (sf->fonts_changed || sf->redisplay)
13932 {
13933 if (sf->redisplay)
13934 {
13935 /* Set this to force a more thorough redisplay.
13936 Otherwise, we might immediately loop back to the
13937 above "else-if" clause (since all the conditions that
13938 led here might still be true), and we will then
13939 infloop, because the selected-frame's redisplay flag
13940 is not (and cannot be) reset. */
13941 windows_or_buffers_changed = 50;
13942 }
13943 goto retry;
13944 }
13945
13946 /* Prevent freeing of realized faces, since desired matrices are
13947 pending that reference the faces we computed and cached. */
13948 inhibit_free_realized_faces = true;
13949
13950 /* Prevent various kinds of signals during display update.
13951 stdio is not robust about handling signals,
13952 which can cause an apparent I/O error. */
13953 if (interrupt_input)
13954 unrequest_sigio ();
13955 STOP_POLLING;
13956
13957 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13958 {
13959 if (hscroll_windows (selected_window))
13960 goto retry;
13961
13962 XWINDOW (selected_window)->must_be_updated_p = true;
13963 pending = update_frame (sf, false, false);
13964 sf->cursor_type_changed = false;
13965 }
13966
13967 /* We may have called echo_area_display at the top of this
13968 function. If the echo area is on another frame, that may
13969 have put text on a frame other than the selected one, so the
13970 above call to update_frame would not have caught it. Catch
13971 it here. */
13972 mini_window = FRAME_MINIBUF_WINDOW (sf);
13973 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13974
13975 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13976 {
13977 XWINDOW (mini_window)->must_be_updated_p = true;
13978 pending |= update_frame (mini_frame, false, false);
13979 mini_frame->cursor_type_changed = false;
13980 if (!pending && hscroll_windows (mini_window))
13981 goto retry;
13982 }
13983 }
13984
13985 /* If display was paused because of pending input, make sure we do a
13986 thorough update the next time. */
13987 if (pending)
13988 {
13989 /* Prevent the optimization at the beginning of
13990 redisplay_internal that tries a single-line update of the
13991 line containing the cursor in the selected window. */
13992 CHARPOS (this_line_start_pos) = 0;
13993
13994 /* Let the overlay arrow be updated the next time. */
13995 update_overlay_arrows (0);
13996
13997 /* If we pause after scrolling, some rows in the current
13998 matrices of some windows are not valid. */
13999 if (!WINDOW_FULL_WIDTH_P (w)
14000 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14001 update_mode_lines = 36;
14002 }
14003 else
14004 {
14005 if (!consider_all_windows_p)
14006 {
14007 /* This has already been done above if
14008 consider_all_windows_p is set. */
14009 if (XBUFFER (w->contents)->text->redisplay
14010 && buffer_window_count (XBUFFER (w->contents)) > 1)
14011 /* This can happen if b->text->redisplay was set during
14012 jit-lock. */
14013 propagate_buffer_redisplay ();
14014 mark_window_display_accurate_1 (w, true);
14015
14016 /* Say overlay arrows are up to date. */
14017 update_overlay_arrows (1);
14018
14019 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14020 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14021 }
14022
14023 update_mode_lines = 0;
14024 windows_or_buffers_changed = 0;
14025 }
14026
14027 /* Start SIGIO interrupts coming again. Having them off during the
14028 code above makes it less likely one will discard output, but not
14029 impossible, since there might be stuff in the system buffer here.
14030 But it is much hairier to try to do anything about that. */
14031 if (interrupt_input)
14032 request_sigio ();
14033 RESUME_POLLING;
14034
14035 /* If a frame has become visible which was not before, redisplay
14036 again, so that we display it. Expose events for such a frame
14037 (which it gets when becoming visible) don't call the parts of
14038 redisplay constructing glyphs, so simply exposing a frame won't
14039 display anything in this case. So, we have to display these
14040 frames here explicitly. */
14041 if (!pending)
14042 {
14043 int new_count = 0;
14044
14045 FOR_EACH_FRAME (tail, frame)
14046 {
14047 if (XFRAME (frame)->visible)
14048 new_count++;
14049 }
14050
14051 if (new_count != number_of_visible_frames)
14052 windows_or_buffers_changed = 52;
14053 }
14054
14055 /* Change frame size now if a change is pending. */
14056 do_pending_window_change (true);
14057
14058 /* If we just did a pending size change, or have additional
14059 visible frames, or selected_window changed, redisplay again. */
14060 if ((windows_or_buffers_changed && !pending)
14061 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14062 goto retry;
14063
14064 /* Clear the face and image caches.
14065
14066 We used to do this only if consider_all_windows_p. But the cache
14067 needs to be cleared if a timer creates images in the current
14068 buffer (e.g. the test case in Bug#6230). */
14069
14070 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14071 {
14072 clear_face_cache (false);
14073 clear_face_cache_count = 0;
14074 }
14075
14076 #ifdef HAVE_WINDOW_SYSTEM
14077 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14078 {
14079 clear_image_caches (Qnil);
14080 clear_image_cache_count = 0;
14081 }
14082 #endif /* HAVE_WINDOW_SYSTEM */
14083
14084 end_of_redisplay:
14085 #ifdef HAVE_NS
14086 ns_set_doc_edited ();
14087 #endif
14088 if (interrupt_input && interrupts_deferred)
14089 request_sigio ();
14090
14091 unbind_to (count, Qnil);
14092 RESUME_POLLING;
14093 }
14094
14095
14096 /* Redisplay, but leave alone any recent echo area message unless
14097 another message has been requested in its place.
14098
14099 This is useful in situations where you need to redisplay but no
14100 user action has occurred, making it inappropriate for the message
14101 area to be cleared. See tracking_off and
14102 wait_reading_process_output for examples of these situations.
14103
14104 FROM_WHERE is an integer saying from where this function was
14105 called. This is useful for debugging. */
14106
14107 void
14108 redisplay_preserve_echo_area (int from_where)
14109 {
14110 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14111
14112 if (!NILP (echo_area_buffer[1]))
14113 {
14114 /* We have a previously displayed message, but no current
14115 message. Redisplay the previous message. */
14116 display_last_displayed_message_p = true;
14117 redisplay_internal ();
14118 display_last_displayed_message_p = false;
14119 }
14120 else
14121 redisplay_internal ();
14122
14123 flush_frame (SELECTED_FRAME ());
14124 }
14125
14126
14127 /* Function registered with record_unwind_protect in redisplay_internal. */
14128
14129 static void
14130 unwind_redisplay (void)
14131 {
14132 redisplaying_p = false;
14133 }
14134
14135
14136 /* Mark the display of leaf window W as accurate or inaccurate.
14137 If ACCURATE_P, mark display of W as accurate.
14138 If !ACCURATE_P, arrange for W to be redisplayed the next
14139 time redisplay_internal is called. */
14140
14141 static void
14142 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14143 {
14144 struct buffer *b = XBUFFER (w->contents);
14145
14146 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14147 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14148 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14149
14150 if (accurate_p)
14151 {
14152 b->clip_changed = false;
14153 b->prevent_redisplay_optimizations_p = false;
14154 eassert (buffer_window_count (b) > 0);
14155 /* Resetting b->text->redisplay is problematic!
14156 In order to make it safer to do it here, redisplay_internal must
14157 have copied all b->text->redisplay to their respective windows. */
14158 b->text->redisplay = false;
14159
14160 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14161 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14162 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14163 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14164
14165 w->current_matrix->buffer = b;
14166 w->current_matrix->begv = BUF_BEGV (b);
14167 w->current_matrix->zv = BUF_ZV (b);
14168
14169 w->last_cursor_vpos = w->cursor.vpos;
14170 w->last_cursor_off_p = w->cursor_off_p;
14171
14172 if (w == XWINDOW (selected_window))
14173 w->last_point = BUF_PT (b);
14174 else
14175 w->last_point = marker_position (w->pointm);
14176
14177 w->window_end_valid = true;
14178 w->update_mode_line = false;
14179 }
14180
14181 w->redisplay = !accurate_p;
14182 }
14183
14184
14185 /* Mark the display of windows in the window tree rooted at WINDOW as
14186 accurate or inaccurate. If ACCURATE_P, mark display of
14187 windows as accurate. If !ACCURATE_P, arrange for windows to
14188 be redisplayed the next time redisplay_internal is called. */
14189
14190 void
14191 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14192 {
14193 struct window *w;
14194
14195 for (; !NILP (window); window = w->next)
14196 {
14197 w = XWINDOW (window);
14198 if (WINDOWP (w->contents))
14199 mark_window_display_accurate (w->contents, accurate_p);
14200 else
14201 mark_window_display_accurate_1 (w, accurate_p);
14202 }
14203
14204 if (accurate_p)
14205 update_overlay_arrows (1);
14206 else
14207 /* Force a thorough redisplay the next time by setting
14208 last_arrow_position and last_arrow_string to t, which is
14209 unequal to any useful value of Voverlay_arrow_... */
14210 update_overlay_arrows (-1);
14211 }
14212
14213
14214 /* Return value in display table DP (Lisp_Char_Table *) for character
14215 C. Since a display table doesn't have any parent, we don't have to
14216 follow parent. Do not call this function directly but use the
14217 macro DISP_CHAR_VECTOR. */
14218
14219 Lisp_Object
14220 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14221 {
14222 Lisp_Object val;
14223
14224 if (ASCII_CHAR_P (c))
14225 {
14226 val = dp->ascii;
14227 if (SUB_CHAR_TABLE_P (val))
14228 val = XSUB_CHAR_TABLE (val)->contents[c];
14229 }
14230 else
14231 {
14232 Lisp_Object table;
14233
14234 XSETCHAR_TABLE (table, dp);
14235 val = char_table_ref (table, c);
14236 }
14237 if (NILP (val))
14238 val = dp->defalt;
14239 return val;
14240 }
14241
14242
14243 \f
14244 /***********************************************************************
14245 Window Redisplay
14246 ***********************************************************************/
14247
14248 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14249
14250 static void
14251 redisplay_windows (Lisp_Object window)
14252 {
14253 while (!NILP (window))
14254 {
14255 struct window *w = XWINDOW (window);
14256
14257 if (WINDOWP (w->contents))
14258 redisplay_windows (w->contents);
14259 else if (BUFFERP (w->contents))
14260 {
14261 displayed_buffer = XBUFFER (w->contents);
14262 /* Use list_of_error, not Qerror, so that
14263 we catch only errors and don't run the debugger. */
14264 internal_condition_case_1 (redisplay_window_0, window,
14265 list_of_error,
14266 redisplay_window_error);
14267 }
14268
14269 window = w->next;
14270 }
14271 }
14272
14273 static Lisp_Object
14274 redisplay_window_error (Lisp_Object ignore)
14275 {
14276 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14277 return Qnil;
14278 }
14279
14280 static Lisp_Object
14281 redisplay_window_0 (Lisp_Object window)
14282 {
14283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14284 redisplay_window (window, false);
14285 return Qnil;
14286 }
14287
14288 static Lisp_Object
14289 redisplay_window_1 (Lisp_Object window)
14290 {
14291 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14292 redisplay_window (window, true);
14293 return Qnil;
14294 }
14295 \f
14296
14297 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14298 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14299 which positions recorded in ROW differ from current buffer
14300 positions.
14301
14302 Return true iff cursor is on this row. */
14303
14304 static bool
14305 set_cursor_from_row (struct window *w, struct glyph_row *row,
14306 struct glyph_matrix *matrix,
14307 ptrdiff_t delta, ptrdiff_t delta_bytes,
14308 int dy, int dvpos)
14309 {
14310 struct glyph *glyph = row->glyphs[TEXT_AREA];
14311 struct glyph *end = glyph + row->used[TEXT_AREA];
14312 struct glyph *cursor = NULL;
14313 /* The last known character position in row. */
14314 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14315 int x = row->x;
14316 ptrdiff_t pt_old = PT - delta;
14317 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14318 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14319 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14320 /* A glyph beyond the edge of TEXT_AREA which we should never
14321 touch. */
14322 struct glyph *glyphs_end = end;
14323 /* True means we've found a match for cursor position, but that
14324 glyph has the avoid_cursor_p flag set. */
14325 bool match_with_avoid_cursor = false;
14326 /* True means we've seen at least one glyph that came from a
14327 display string. */
14328 bool string_seen = false;
14329 /* Largest and smallest buffer positions seen so far during scan of
14330 glyph row. */
14331 ptrdiff_t bpos_max = pos_before;
14332 ptrdiff_t bpos_min = pos_after;
14333 /* Last buffer position covered by an overlay string with an integer
14334 `cursor' property. */
14335 ptrdiff_t bpos_covered = 0;
14336 /* True means the display string on which to display the cursor
14337 comes from a text property, not from an overlay. */
14338 bool string_from_text_prop = false;
14339
14340 /* Don't even try doing anything if called for a mode-line or
14341 header-line row, since the rest of the code isn't prepared to
14342 deal with such calamities. */
14343 eassert (!row->mode_line_p);
14344 if (row->mode_line_p)
14345 return false;
14346
14347 /* Skip over glyphs not having an object at the start and the end of
14348 the row. These are special glyphs like truncation marks on
14349 terminal frames. */
14350 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14351 {
14352 if (!row->reversed_p)
14353 {
14354 while (glyph < end
14355 && NILP (glyph->object)
14356 && glyph->charpos < 0)
14357 {
14358 x += glyph->pixel_width;
14359 ++glyph;
14360 }
14361 while (end > glyph
14362 && NILP ((end - 1)->object)
14363 /* CHARPOS is zero for blanks and stretch glyphs
14364 inserted by extend_face_to_end_of_line. */
14365 && (end - 1)->charpos <= 0)
14366 --end;
14367 glyph_before = glyph - 1;
14368 glyph_after = end;
14369 }
14370 else
14371 {
14372 struct glyph *g;
14373
14374 /* If the glyph row is reversed, we need to process it from back
14375 to front, so swap the edge pointers. */
14376 glyphs_end = end = glyph - 1;
14377 glyph += row->used[TEXT_AREA] - 1;
14378
14379 while (glyph > end + 1
14380 && NILP (glyph->object)
14381 && glyph->charpos < 0)
14382 {
14383 --glyph;
14384 x -= glyph->pixel_width;
14385 }
14386 if (NILP (glyph->object) && glyph->charpos < 0)
14387 --glyph;
14388 /* By default, in reversed rows we put the cursor on the
14389 rightmost (first in the reading order) glyph. */
14390 for (g = end + 1; g < glyph; g++)
14391 x += g->pixel_width;
14392 while (end < glyph
14393 && NILP ((end + 1)->object)
14394 && (end + 1)->charpos <= 0)
14395 ++end;
14396 glyph_before = glyph + 1;
14397 glyph_after = end;
14398 }
14399 }
14400 else if (row->reversed_p)
14401 {
14402 /* In R2L rows that don't display text, put the cursor on the
14403 rightmost glyph. Case in point: an empty last line that is
14404 part of an R2L paragraph. */
14405 cursor = end - 1;
14406 /* Avoid placing the cursor on the last glyph of the row, where
14407 on terminal frames we hold the vertical border between
14408 adjacent windows. */
14409 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14410 && !WINDOW_RIGHTMOST_P (w)
14411 && cursor == row->glyphs[LAST_AREA] - 1)
14412 cursor--;
14413 x = -1; /* will be computed below, at label compute_x */
14414 }
14415
14416 /* Step 1: Try to find the glyph whose character position
14417 corresponds to point. If that's not possible, find 2 glyphs
14418 whose character positions are the closest to point, one before
14419 point, the other after it. */
14420 if (!row->reversed_p)
14421 while (/* not marched to end of glyph row */
14422 glyph < end
14423 /* glyph was not inserted by redisplay for internal purposes */
14424 && !NILP (glyph->object))
14425 {
14426 if (BUFFERP (glyph->object))
14427 {
14428 ptrdiff_t dpos = glyph->charpos - pt_old;
14429
14430 if (glyph->charpos > bpos_max)
14431 bpos_max = glyph->charpos;
14432 if (glyph->charpos < bpos_min)
14433 bpos_min = glyph->charpos;
14434 if (!glyph->avoid_cursor_p)
14435 {
14436 /* If we hit point, we've found the glyph on which to
14437 display the cursor. */
14438 if (dpos == 0)
14439 {
14440 match_with_avoid_cursor = false;
14441 break;
14442 }
14443 /* See if we've found a better approximation to
14444 POS_BEFORE or to POS_AFTER. */
14445 if (0 > dpos && dpos > pos_before - pt_old)
14446 {
14447 pos_before = glyph->charpos;
14448 glyph_before = glyph;
14449 }
14450 else if (0 < dpos && dpos < pos_after - pt_old)
14451 {
14452 pos_after = glyph->charpos;
14453 glyph_after = glyph;
14454 }
14455 }
14456 else if (dpos == 0)
14457 match_with_avoid_cursor = true;
14458 }
14459 else if (STRINGP (glyph->object))
14460 {
14461 Lisp_Object chprop;
14462 ptrdiff_t glyph_pos = glyph->charpos;
14463
14464 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14465 glyph->object);
14466 if (!NILP (chprop))
14467 {
14468 /* If the string came from a `display' text property,
14469 look up the buffer position of that property and
14470 use that position to update bpos_max, as if we
14471 actually saw such a position in one of the row's
14472 glyphs. This helps with supporting integer values
14473 of `cursor' property on the display string in
14474 situations where most or all of the row's buffer
14475 text is completely covered by display properties,
14476 so that no glyph with valid buffer positions is
14477 ever seen in the row. */
14478 ptrdiff_t prop_pos =
14479 string_buffer_position_lim (glyph->object, pos_before,
14480 pos_after, false);
14481
14482 if (prop_pos >= pos_before)
14483 bpos_max = prop_pos;
14484 }
14485 if (INTEGERP (chprop))
14486 {
14487 bpos_covered = bpos_max + XINT (chprop);
14488 /* If the `cursor' property covers buffer positions up
14489 to and including point, we should display cursor on
14490 this glyph. Note that, if a `cursor' property on one
14491 of the string's characters has an integer value, we
14492 will break out of the loop below _before_ we get to
14493 the position match above. IOW, integer values of
14494 the `cursor' property override the "exact match for
14495 point" strategy of positioning the cursor. */
14496 /* Implementation note: bpos_max == pt_old when, e.g.,
14497 we are in an empty line, where bpos_max is set to
14498 MATRIX_ROW_START_CHARPOS, see above. */
14499 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14500 {
14501 cursor = glyph;
14502 break;
14503 }
14504 }
14505
14506 string_seen = true;
14507 }
14508 x += glyph->pixel_width;
14509 ++glyph;
14510 }
14511 else if (glyph > end) /* row is reversed */
14512 while (!NILP (glyph->object))
14513 {
14514 if (BUFFERP (glyph->object))
14515 {
14516 ptrdiff_t dpos = glyph->charpos - pt_old;
14517
14518 if (glyph->charpos > bpos_max)
14519 bpos_max = glyph->charpos;
14520 if (glyph->charpos < bpos_min)
14521 bpos_min = glyph->charpos;
14522 if (!glyph->avoid_cursor_p)
14523 {
14524 if (dpos == 0)
14525 {
14526 match_with_avoid_cursor = false;
14527 break;
14528 }
14529 if (0 > dpos && dpos > pos_before - pt_old)
14530 {
14531 pos_before = glyph->charpos;
14532 glyph_before = glyph;
14533 }
14534 else if (0 < dpos && dpos < pos_after - pt_old)
14535 {
14536 pos_after = glyph->charpos;
14537 glyph_after = glyph;
14538 }
14539 }
14540 else if (dpos == 0)
14541 match_with_avoid_cursor = true;
14542 }
14543 else if (STRINGP (glyph->object))
14544 {
14545 Lisp_Object chprop;
14546 ptrdiff_t glyph_pos = glyph->charpos;
14547
14548 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14549 glyph->object);
14550 if (!NILP (chprop))
14551 {
14552 ptrdiff_t prop_pos =
14553 string_buffer_position_lim (glyph->object, pos_before,
14554 pos_after, false);
14555
14556 if (prop_pos >= pos_before)
14557 bpos_max = prop_pos;
14558 }
14559 if (INTEGERP (chprop))
14560 {
14561 bpos_covered = bpos_max + XINT (chprop);
14562 /* If the `cursor' property covers buffer positions up
14563 to and including point, we should display cursor on
14564 this glyph. */
14565 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14566 {
14567 cursor = glyph;
14568 break;
14569 }
14570 }
14571 string_seen = true;
14572 }
14573 --glyph;
14574 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14575 {
14576 x--; /* can't use any pixel_width */
14577 break;
14578 }
14579 x -= glyph->pixel_width;
14580 }
14581
14582 /* Step 2: If we didn't find an exact match for point, we need to
14583 look for a proper place to put the cursor among glyphs between
14584 GLYPH_BEFORE and GLYPH_AFTER. */
14585 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14586 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14587 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14588 {
14589 /* An empty line has a single glyph whose OBJECT is nil and
14590 whose CHARPOS is the position of a newline on that line.
14591 Note that on a TTY, there are more glyphs after that, which
14592 were produced by extend_face_to_end_of_line, but their
14593 CHARPOS is zero or negative. */
14594 bool empty_line_p =
14595 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14596 && NILP (glyph->object) && glyph->charpos > 0
14597 /* On a TTY, continued and truncated rows also have a glyph at
14598 their end whose OBJECT is nil and whose CHARPOS is
14599 positive (the continuation and truncation glyphs), but such
14600 rows are obviously not "empty". */
14601 && !(row->continued_p || row->truncated_on_right_p));
14602
14603 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14604 {
14605 ptrdiff_t ellipsis_pos;
14606
14607 /* Scan back over the ellipsis glyphs. */
14608 if (!row->reversed_p)
14609 {
14610 ellipsis_pos = (glyph - 1)->charpos;
14611 while (glyph > row->glyphs[TEXT_AREA]
14612 && (glyph - 1)->charpos == ellipsis_pos)
14613 glyph--, x -= glyph->pixel_width;
14614 /* That loop always goes one position too far, including
14615 the glyph before the ellipsis. So scan forward over
14616 that one. */
14617 x += glyph->pixel_width;
14618 glyph++;
14619 }
14620 else /* row is reversed */
14621 {
14622 ellipsis_pos = (glyph + 1)->charpos;
14623 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14624 && (glyph + 1)->charpos == ellipsis_pos)
14625 glyph++, x += glyph->pixel_width;
14626 x -= glyph->pixel_width;
14627 glyph--;
14628 }
14629 }
14630 else if (match_with_avoid_cursor)
14631 {
14632 cursor = glyph_after;
14633 x = -1;
14634 }
14635 else if (string_seen)
14636 {
14637 int incr = row->reversed_p ? -1 : +1;
14638
14639 /* Need to find the glyph that came out of a string which is
14640 present at point. That glyph is somewhere between
14641 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14642 positioned between POS_BEFORE and POS_AFTER in the
14643 buffer. */
14644 struct glyph *start, *stop;
14645 ptrdiff_t pos = pos_before;
14646
14647 x = -1;
14648
14649 /* If the row ends in a newline from a display string,
14650 reordering could have moved the glyphs belonging to the
14651 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14652 in this case we extend the search to the last glyph in
14653 the row that was not inserted by redisplay. */
14654 if (row->ends_in_newline_from_string_p)
14655 {
14656 glyph_after = end;
14657 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14658 }
14659
14660 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14661 correspond to POS_BEFORE and POS_AFTER, respectively. We
14662 need START and STOP in the order that corresponds to the
14663 row's direction as given by its reversed_p flag. If the
14664 directionality of characters between POS_BEFORE and
14665 POS_AFTER is the opposite of the row's base direction,
14666 these characters will have been reordered for display,
14667 and we need to reverse START and STOP. */
14668 if (!row->reversed_p)
14669 {
14670 start = min (glyph_before, glyph_after);
14671 stop = max (glyph_before, glyph_after);
14672 }
14673 else
14674 {
14675 start = max (glyph_before, glyph_after);
14676 stop = min (glyph_before, glyph_after);
14677 }
14678 for (glyph = start + incr;
14679 row->reversed_p ? glyph > stop : glyph < stop; )
14680 {
14681
14682 /* Any glyphs that come from the buffer are here because
14683 of bidi reordering. Skip them, and only pay
14684 attention to glyphs that came from some string. */
14685 if (STRINGP (glyph->object))
14686 {
14687 Lisp_Object str;
14688 ptrdiff_t tem;
14689 /* If the display property covers the newline, we
14690 need to search for it one position farther. */
14691 ptrdiff_t lim = pos_after
14692 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14693
14694 string_from_text_prop = false;
14695 str = glyph->object;
14696 tem = string_buffer_position_lim (str, pos, lim, false);
14697 if (tem == 0 /* from overlay */
14698 || pos <= tem)
14699 {
14700 /* If the string from which this glyph came is
14701 found in the buffer at point, or at position
14702 that is closer to point than pos_after, then
14703 we've found the glyph we've been looking for.
14704 If it comes from an overlay (tem == 0), and
14705 it has the `cursor' property on one of its
14706 glyphs, record that glyph as a candidate for
14707 displaying the cursor. (As in the
14708 unidirectional version, we will display the
14709 cursor on the last candidate we find.) */
14710 if (tem == 0
14711 || tem == pt_old
14712 || (tem - pt_old > 0 && tem < pos_after))
14713 {
14714 /* The glyphs from this string could have
14715 been reordered. Find the one with the
14716 smallest string position. Or there could
14717 be a character in the string with the
14718 `cursor' property, which means display
14719 cursor on that character's glyph. */
14720 ptrdiff_t strpos = glyph->charpos;
14721
14722 if (tem)
14723 {
14724 cursor = glyph;
14725 string_from_text_prop = true;
14726 }
14727 for ( ;
14728 (row->reversed_p ? glyph > stop : glyph < stop)
14729 && EQ (glyph->object, str);
14730 glyph += incr)
14731 {
14732 Lisp_Object cprop;
14733 ptrdiff_t gpos = glyph->charpos;
14734
14735 cprop = Fget_char_property (make_number (gpos),
14736 Qcursor,
14737 glyph->object);
14738 if (!NILP (cprop))
14739 {
14740 cursor = glyph;
14741 break;
14742 }
14743 if (tem && glyph->charpos < strpos)
14744 {
14745 strpos = glyph->charpos;
14746 cursor = glyph;
14747 }
14748 }
14749
14750 if (tem == pt_old
14751 || (tem - pt_old > 0 && tem < pos_after))
14752 goto compute_x;
14753 }
14754 if (tem)
14755 pos = tem + 1; /* don't find previous instances */
14756 }
14757 /* This string is not what we want; skip all of the
14758 glyphs that came from it. */
14759 while ((row->reversed_p ? glyph > stop : glyph < stop)
14760 && EQ (glyph->object, str))
14761 glyph += incr;
14762 }
14763 else
14764 glyph += incr;
14765 }
14766
14767 /* If we reached the end of the line, and END was from a string,
14768 the cursor is not on this line. */
14769 if (cursor == NULL
14770 && (row->reversed_p ? glyph <= end : glyph >= end)
14771 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14772 && STRINGP (end->object)
14773 && row->continued_p)
14774 return false;
14775 }
14776 /* A truncated row may not include PT among its character positions.
14777 Setting the cursor inside the scroll margin will trigger
14778 recalculation of hscroll in hscroll_window_tree. But if a
14779 display string covers point, defer to the string-handling
14780 code below to figure this out. */
14781 else if (row->truncated_on_left_p && pt_old < bpos_min)
14782 {
14783 cursor = glyph_before;
14784 x = -1;
14785 }
14786 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14787 /* Zero-width characters produce no glyphs. */
14788 || (!empty_line_p
14789 && (row->reversed_p
14790 ? glyph_after > glyphs_end
14791 : glyph_after < glyphs_end)))
14792 {
14793 cursor = glyph_after;
14794 x = -1;
14795 }
14796 }
14797
14798 compute_x:
14799 if (cursor != NULL)
14800 glyph = cursor;
14801 else if (glyph == glyphs_end
14802 && pos_before == pos_after
14803 && STRINGP ((row->reversed_p
14804 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14805 : row->glyphs[TEXT_AREA])->object))
14806 {
14807 /* If all the glyphs of this row came from strings, put the
14808 cursor on the first glyph of the row. This avoids having the
14809 cursor outside of the text area in this very rare and hard
14810 use case. */
14811 glyph =
14812 row->reversed_p
14813 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14814 : row->glyphs[TEXT_AREA];
14815 }
14816 if (x < 0)
14817 {
14818 struct glyph *g;
14819
14820 /* Need to compute x that corresponds to GLYPH. */
14821 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14822 {
14823 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14824 emacs_abort ();
14825 x += g->pixel_width;
14826 }
14827 }
14828
14829 /* ROW could be part of a continued line, which, under bidi
14830 reordering, might have other rows whose start and end charpos
14831 occlude point. Only set w->cursor if we found a better
14832 approximation to the cursor position than we have from previously
14833 examined candidate rows belonging to the same continued line. */
14834 if (/* We already have a candidate row. */
14835 w->cursor.vpos >= 0
14836 /* That candidate is not the row we are processing. */
14837 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14838 /* Make sure cursor.vpos specifies a row whose start and end
14839 charpos occlude point, and it is valid candidate for being a
14840 cursor-row. This is because some callers of this function
14841 leave cursor.vpos at the row where the cursor was displayed
14842 during the last redisplay cycle. */
14843 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14844 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14845 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14846 {
14847 struct glyph *g1
14848 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14849
14850 /* Don't consider glyphs that are outside TEXT_AREA. */
14851 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14852 return false;
14853 /* Keep the candidate whose buffer position is the closest to
14854 point or has the `cursor' property. */
14855 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14856 w->cursor.hpos >= 0
14857 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14858 && ((BUFFERP (g1->object)
14859 && (g1->charpos == pt_old /* An exact match always wins. */
14860 || (BUFFERP (glyph->object)
14861 && eabs (g1->charpos - pt_old)
14862 < eabs (glyph->charpos - pt_old))))
14863 /* Previous candidate is a glyph from a string that has
14864 a non-nil `cursor' property. */
14865 || (STRINGP (g1->object)
14866 && (!NILP (Fget_char_property (make_number (g1->charpos),
14867 Qcursor, g1->object))
14868 /* Previous candidate is from the same display
14869 string as this one, and the display string
14870 came from a text property. */
14871 || (EQ (g1->object, glyph->object)
14872 && string_from_text_prop)
14873 /* this candidate is from newline and its
14874 position is not an exact match */
14875 || (NILP (glyph->object)
14876 && glyph->charpos != pt_old)))))
14877 return false;
14878 /* If this candidate gives an exact match, use that. */
14879 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14880 /* If this candidate is a glyph created for the
14881 terminating newline of a line, and point is on that
14882 newline, it wins because it's an exact match. */
14883 || (!row->continued_p
14884 && NILP (glyph->object)
14885 && glyph->charpos == 0
14886 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14887 /* Otherwise, keep the candidate that comes from a row
14888 spanning less buffer positions. This may win when one or
14889 both candidate positions are on glyphs that came from
14890 display strings, for which we cannot compare buffer
14891 positions. */
14892 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14893 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14894 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14895 return false;
14896 }
14897 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14898 w->cursor.x = x;
14899 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14900 w->cursor.y = row->y + dy;
14901
14902 if (w == XWINDOW (selected_window))
14903 {
14904 if (!row->continued_p
14905 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14906 && row->x == 0)
14907 {
14908 this_line_buffer = XBUFFER (w->contents);
14909
14910 CHARPOS (this_line_start_pos)
14911 = MATRIX_ROW_START_CHARPOS (row) + delta;
14912 BYTEPOS (this_line_start_pos)
14913 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14914
14915 CHARPOS (this_line_end_pos)
14916 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14917 BYTEPOS (this_line_end_pos)
14918 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14919
14920 this_line_y = w->cursor.y;
14921 this_line_pixel_height = row->height;
14922 this_line_vpos = w->cursor.vpos;
14923 this_line_start_x = row->x;
14924 }
14925 else
14926 CHARPOS (this_line_start_pos) = 0;
14927 }
14928
14929 return true;
14930 }
14931
14932
14933 /* Run window scroll functions, if any, for WINDOW with new window
14934 start STARTP. Sets the window start of WINDOW to that position.
14935
14936 We assume that the window's buffer is really current. */
14937
14938 static struct text_pos
14939 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14940 {
14941 struct window *w = XWINDOW (window);
14942 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14943
14944 eassert (current_buffer == XBUFFER (w->contents));
14945
14946 if (!NILP (Vwindow_scroll_functions))
14947 {
14948 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14949 make_number (CHARPOS (startp)));
14950 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14951 /* In case the hook functions switch buffers. */
14952 set_buffer_internal (XBUFFER (w->contents));
14953 }
14954
14955 return startp;
14956 }
14957
14958
14959 /* Make sure the line containing the cursor is fully visible.
14960 A value of true means there is nothing to be done.
14961 (Either the line is fully visible, or it cannot be made so,
14962 or we cannot tell.)
14963
14964 If FORCE_P, return false even if partial visible cursor row
14965 is higher than window.
14966
14967 If CURRENT_MATRIX_P, use the information from the
14968 window's current glyph matrix; otherwise use the desired glyph
14969 matrix.
14970
14971 A value of false means the caller should do scrolling
14972 as if point had gone off the screen. */
14973
14974 static bool
14975 cursor_row_fully_visible_p (struct window *w, bool force_p,
14976 bool current_matrix_p)
14977 {
14978 struct glyph_matrix *matrix;
14979 struct glyph_row *row;
14980 int window_height;
14981
14982 if (!make_cursor_line_fully_visible_p)
14983 return true;
14984
14985 /* It's not always possible to find the cursor, e.g, when a window
14986 is full of overlay strings. Don't do anything in that case. */
14987 if (w->cursor.vpos < 0)
14988 return true;
14989
14990 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14991 row = MATRIX_ROW (matrix, w->cursor.vpos);
14992
14993 /* If the cursor row is not partially visible, there's nothing to do. */
14994 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14995 return true;
14996
14997 /* If the row the cursor is in is taller than the window's height,
14998 it's not clear what to do, so do nothing. */
14999 window_height = window_box_height (w);
15000 if (row->height >= window_height)
15001 {
15002 if (!force_p || MINI_WINDOW_P (w)
15003 || w->vscroll || w->cursor.vpos == 0)
15004 return true;
15005 }
15006 return false;
15007 }
15008
15009
15010 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15011 means only WINDOW is redisplayed in redisplay_internal.
15012 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15013 in redisplay_window to bring a partially visible line into view in
15014 the case that only the cursor has moved.
15015
15016 LAST_LINE_MISFIT should be true if we're scrolling because the
15017 last screen line's vertical height extends past the end of the screen.
15018
15019 Value is
15020
15021 1 if scrolling succeeded
15022
15023 0 if scrolling didn't find point.
15024
15025 -1 if new fonts have been loaded so that we must interrupt
15026 redisplay, adjust glyph matrices, and try again. */
15027
15028 enum
15029 {
15030 SCROLLING_SUCCESS,
15031 SCROLLING_FAILED,
15032 SCROLLING_NEED_LARGER_MATRICES
15033 };
15034
15035 /* If scroll-conservatively is more than this, never recenter.
15036
15037 If you change this, don't forget to update the doc string of
15038 `scroll-conservatively' and the Emacs manual. */
15039 #define SCROLL_LIMIT 100
15040
15041 static int
15042 try_scrolling (Lisp_Object window, bool just_this_one_p,
15043 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15044 bool temp_scroll_step, bool last_line_misfit)
15045 {
15046 struct window *w = XWINDOW (window);
15047 struct frame *f = XFRAME (w->frame);
15048 struct text_pos pos, startp;
15049 struct it it;
15050 int this_scroll_margin, scroll_max, rc, height;
15051 int dy = 0, amount_to_scroll = 0;
15052 bool scroll_down_p = false;
15053 int extra_scroll_margin_lines = last_line_misfit;
15054 Lisp_Object aggressive;
15055 /* We will never try scrolling more than this number of lines. */
15056 int scroll_limit = SCROLL_LIMIT;
15057 int frame_line_height = default_line_pixel_height (w);
15058 int window_total_lines
15059 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15060
15061 #ifdef GLYPH_DEBUG
15062 debug_method_add (w, "try_scrolling");
15063 #endif
15064
15065 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15066
15067 /* Compute scroll margin height in pixels. We scroll when point is
15068 within this distance from the top or bottom of the window. */
15069 if (scroll_margin > 0)
15070 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15071 * frame_line_height;
15072 else
15073 this_scroll_margin = 0;
15074
15075 /* Force arg_scroll_conservatively to have a reasonable value, to
15076 avoid scrolling too far away with slow move_it_* functions. Note
15077 that the user can supply scroll-conservatively equal to
15078 `most-positive-fixnum', which can be larger than INT_MAX. */
15079 if (arg_scroll_conservatively > scroll_limit)
15080 {
15081 arg_scroll_conservatively = scroll_limit + 1;
15082 scroll_max = scroll_limit * frame_line_height;
15083 }
15084 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15085 /* Compute how much we should try to scroll maximally to bring
15086 point into view. */
15087 scroll_max = (max (scroll_step,
15088 max (arg_scroll_conservatively, temp_scroll_step))
15089 * frame_line_height);
15090 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15091 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15092 /* We're trying to scroll because of aggressive scrolling but no
15093 scroll_step is set. Choose an arbitrary one. */
15094 scroll_max = 10 * frame_line_height;
15095 else
15096 scroll_max = 0;
15097
15098 too_near_end:
15099
15100 /* Decide whether to scroll down. */
15101 if (PT > CHARPOS (startp))
15102 {
15103 int scroll_margin_y;
15104
15105 /* Compute the pixel ypos of the scroll margin, then move IT to
15106 either that ypos or PT, whichever comes first. */
15107 start_display (&it, w, startp);
15108 scroll_margin_y = it.last_visible_y - this_scroll_margin
15109 - frame_line_height * extra_scroll_margin_lines;
15110 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15111 (MOVE_TO_POS | MOVE_TO_Y));
15112
15113 if (PT > CHARPOS (it.current.pos))
15114 {
15115 int y0 = line_bottom_y (&it);
15116 /* Compute how many pixels below window bottom to stop searching
15117 for PT. This avoids costly search for PT that is far away if
15118 the user limited scrolling by a small number of lines, but
15119 always finds PT if scroll_conservatively is set to a large
15120 number, such as most-positive-fixnum. */
15121 int slack = max (scroll_max, 10 * frame_line_height);
15122 int y_to_move = it.last_visible_y + slack;
15123
15124 /* Compute the distance from the scroll margin to PT or to
15125 the scroll limit, whichever comes first. This should
15126 include the height of the cursor line, to make that line
15127 fully visible. */
15128 move_it_to (&it, PT, -1, y_to_move,
15129 -1, MOVE_TO_POS | MOVE_TO_Y);
15130 dy = line_bottom_y (&it) - y0;
15131
15132 if (dy > scroll_max)
15133 return SCROLLING_FAILED;
15134
15135 if (dy > 0)
15136 scroll_down_p = true;
15137 }
15138 }
15139
15140 if (scroll_down_p)
15141 {
15142 /* Point is in or below the bottom scroll margin, so move the
15143 window start down. If scrolling conservatively, move it just
15144 enough down to make point visible. If scroll_step is set,
15145 move it down by scroll_step. */
15146 if (arg_scroll_conservatively)
15147 amount_to_scroll
15148 = min (max (dy, frame_line_height),
15149 frame_line_height * arg_scroll_conservatively);
15150 else if (scroll_step || temp_scroll_step)
15151 amount_to_scroll = scroll_max;
15152 else
15153 {
15154 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15155 height = WINDOW_BOX_TEXT_HEIGHT (w);
15156 if (NUMBERP (aggressive))
15157 {
15158 double float_amount = XFLOATINT (aggressive) * height;
15159 int aggressive_scroll = float_amount;
15160 if (aggressive_scroll == 0 && float_amount > 0)
15161 aggressive_scroll = 1;
15162 /* Don't let point enter the scroll margin near top of
15163 the window. This could happen if the value of
15164 scroll_up_aggressively is too large and there are
15165 non-zero margins, because scroll_up_aggressively
15166 means put point that fraction of window height
15167 _from_the_bottom_margin_. */
15168 if (aggressive_scroll + 2 * this_scroll_margin > height)
15169 aggressive_scroll = height - 2 * this_scroll_margin;
15170 amount_to_scroll = dy + aggressive_scroll;
15171 }
15172 }
15173
15174 if (amount_to_scroll <= 0)
15175 return SCROLLING_FAILED;
15176
15177 start_display (&it, w, startp);
15178 if (arg_scroll_conservatively <= scroll_limit)
15179 move_it_vertically (&it, amount_to_scroll);
15180 else
15181 {
15182 /* Extra precision for users who set scroll-conservatively
15183 to a large number: make sure the amount we scroll
15184 the window start is never less than amount_to_scroll,
15185 which was computed as distance from window bottom to
15186 point. This matters when lines at window top and lines
15187 below window bottom have different height. */
15188 struct it it1;
15189 void *it1data = NULL;
15190 /* We use a temporary it1 because line_bottom_y can modify
15191 its argument, if it moves one line down; see there. */
15192 int start_y;
15193
15194 SAVE_IT (it1, it, it1data);
15195 start_y = line_bottom_y (&it1);
15196 do {
15197 RESTORE_IT (&it, &it, it1data);
15198 move_it_by_lines (&it, 1);
15199 SAVE_IT (it1, it, it1data);
15200 } while (IT_CHARPOS (it) < ZV
15201 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15202 bidi_unshelve_cache (it1data, true);
15203 }
15204
15205 /* If STARTP is unchanged, move it down another screen line. */
15206 if (IT_CHARPOS (it) == CHARPOS (startp))
15207 move_it_by_lines (&it, 1);
15208 startp = it.current.pos;
15209 }
15210 else
15211 {
15212 struct text_pos scroll_margin_pos = startp;
15213 int y_offset = 0;
15214
15215 /* See if point is inside the scroll margin at the top of the
15216 window. */
15217 if (this_scroll_margin)
15218 {
15219 int y_start;
15220
15221 start_display (&it, w, startp);
15222 y_start = it.current_y;
15223 move_it_vertically (&it, this_scroll_margin);
15224 scroll_margin_pos = it.current.pos;
15225 /* If we didn't move enough before hitting ZV, request
15226 additional amount of scroll, to move point out of the
15227 scroll margin. */
15228 if (IT_CHARPOS (it) == ZV
15229 && it.current_y - y_start < this_scroll_margin)
15230 y_offset = this_scroll_margin - (it.current_y - y_start);
15231 }
15232
15233 if (PT < CHARPOS (scroll_margin_pos))
15234 {
15235 /* Point is in the scroll margin at the top of the window or
15236 above what is displayed in the window. */
15237 int y0, y_to_move;
15238
15239 /* Compute the vertical distance from PT to the scroll
15240 margin position. Move as far as scroll_max allows, or
15241 one screenful, or 10 screen lines, whichever is largest.
15242 Give up if distance is greater than scroll_max or if we
15243 didn't reach the scroll margin position. */
15244 SET_TEXT_POS (pos, PT, PT_BYTE);
15245 start_display (&it, w, pos);
15246 y0 = it.current_y;
15247 y_to_move = max (it.last_visible_y,
15248 max (scroll_max, 10 * frame_line_height));
15249 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15250 y_to_move, -1,
15251 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15252 dy = it.current_y - y0;
15253 if (dy > scroll_max
15254 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15255 return SCROLLING_FAILED;
15256
15257 /* Additional scroll for when ZV was too close to point. */
15258 dy += y_offset;
15259
15260 /* Compute new window start. */
15261 start_display (&it, w, startp);
15262
15263 if (arg_scroll_conservatively)
15264 amount_to_scroll = max (dy, frame_line_height
15265 * max (scroll_step, temp_scroll_step));
15266 else if (scroll_step || temp_scroll_step)
15267 amount_to_scroll = scroll_max;
15268 else
15269 {
15270 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15271 height = WINDOW_BOX_TEXT_HEIGHT (w);
15272 if (NUMBERP (aggressive))
15273 {
15274 double float_amount = XFLOATINT (aggressive) * height;
15275 int aggressive_scroll = float_amount;
15276 if (aggressive_scroll == 0 && float_amount > 0)
15277 aggressive_scroll = 1;
15278 /* Don't let point enter the scroll margin near
15279 bottom of the window, if the value of
15280 scroll_down_aggressively happens to be too
15281 large. */
15282 if (aggressive_scroll + 2 * this_scroll_margin > height)
15283 aggressive_scroll = height - 2 * this_scroll_margin;
15284 amount_to_scroll = dy + aggressive_scroll;
15285 }
15286 }
15287
15288 if (amount_to_scroll <= 0)
15289 return SCROLLING_FAILED;
15290
15291 move_it_vertically_backward (&it, amount_to_scroll);
15292 startp = it.current.pos;
15293 }
15294 }
15295
15296 /* Run window scroll functions. */
15297 startp = run_window_scroll_functions (window, startp);
15298
15299 /* Display the window. Give up if new fonts are loaded, or if point
15300 doesn't appear. */
15301 if (!try_window (window, startp, 0))
15302 rc = SCROLLING_NEED_LARGER_MATRICES;
15303 else if (w->cursor.vpos < 0)
15304 {
15305 clear_glyph_matrix (w->desired_matrix);
15306 rc = SCROLLING_FAILED;
15307 }
15308 else
15309 {
15310 /* Maybe forget recorded base line for line number display. */
15311 if (!just_this_one_p
15312 || current_buffer->clip_changed
15313 || BEG_UNCHANGED < CHARPOS (startp))
15314 w->base_line_number = 0;
15315
15316 /* If cursor ends up on a partially visible line,
15317 treat that as being off the bottom of the screen. */
15318 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15319 false)
15320 /* It's possible that the cursor is on the first line of the
15321 buffer, which is partially obscured due to a vscroll
15322 (Bug#7537). In that case, avoid looping forever. */
15323 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15324 {
15325 clear_glyph_matrix (w->desired_matrix);
15326 ++extra_scroll_margin_lines;
15327 goto too_near_end;
15328 }
15329 rc = SCROLLING_SUCCESS;
15330 }
15331
15332 return rc;
15333 }
15334
15335
15336 /* Compute a suitable window start for window W if display of W starts
15337 on a continuation line. Value is true if a new window start
15338 was computed.
15339
15340 The new window start will be computed, based on W's width, starting
15341 from the start of the continued line. It is the start of the
15342 screen line with the minimum distance from the old start W->start. */
15343
15344 static bool
15345 compute_window_start_on_continuation_line (struct window *w)
15346 {
15347 struct text_pos pos, start_pos;
15348 bool window_start_changed_p = false;
15349
15350 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15351
15352 /* If window start is on a continuation line... Window start may be
15353 < BEGV in case there's invisible text at the start of the
15354 buffer (M-x rmail, for example). */
15355 if (CHARPOS (start_pos) > BEGV
15356 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15357 {
15358 struct it it;
15359 struct glyph_row *row;
15360
15361 /* Handle the case that the window start is out of range. */
15362 if (CHARPOS (start_pos) < BEGV)
15363 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15364 else if (CHARPOS (start_pos) > ZV)
15365 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15366
15367 /* Find the start of the continued line. This should be fast
15368 because find_newline is fast (newline cache). */
15369 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15370 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15371 row, DEFAULT_FACE_ID);
15372 reseat_at_previous_visible_line_start (&it);
15373
15374 /* If the line start is "too far" away from the window start,
15375 say it takes too much time to compute a new window start. */
15376 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15377 /* PXW: Do we need upper bounds here? */
15378 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15379 {
15380 int min_distance, distance;
15381
15382 /* Move forward by display lines to find the new window
15383 start. If window width was enlarged, the new start can
15384 be expected to be > the old start. If window width was
15385 decreased, the new window start will be < the old start.
15386 So, we're looking for the display line start with the
15387 minimum distance from the old window start. */
15388 pos = it.current.pos;
15389 min_distance = INFINITY;
15390 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15391 distance < min_distance)
15392 {
15393 min_distance = distance;
15394 pos = it.current.pos;
15395 if (it.line_wrap == WORD_WRAP)
15396 {
15397 /* Under WORD_WRAP, move_it_by_lines is likely to
15398 overshoot and stop not at the first, but the
15399 second character from the left margin. So in
15400 that case, we need a more tight control on the X
15401 coordinate of the iterator than move_it_by_lines
15402 promises in its contract. The method is to first
15403 go to the last (rightmost) visible character of a
15404 line, then move to the leftmost character on the
15405 next line in a separate call. */
15406 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15407 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15408 move_it_to (&it, ZV, 0,
15409 it.current_y + it.max_ascent + it.max_descent, -1,
15410 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15411 }
15412 else
15413 move_it_by_lines (&it, 1);
15414 }
15415
15416 /* Set the window start there. */
15417 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15418 window_start_changed_p = true;
15419 }
15420 }
15421
15422 return window_start_changed_p;
15423 }
15424
15425
15426 /* Try cursor movement in case text has not changed in window WINDOW,
15427 with window start STARTP. Value is
15428
15429 CURSOR_MOVEMENT_SUCCESS if successful
15430
15431 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15432
15433 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15434 display. *SCROLL_STEP is set to true, under certain circumstances, if
15435 we want to scroll as if scroll-step were set to 1. See the code.
15436
15437 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15438 which case we have to abort this redisplay, and adjust matrices
15439 first. */
15440
15441 enum
15442 {
15443 CURSOR_MOVEMENT_SUCCESS,
15444 CURSOR_MOVEMENT_CANNOT_BE_USED,
15445 CURSOR_MOVEMENT_MUST_SCROLL,
15446 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15447 };
15448
15449 static int
15450 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15451 bool *scroll_step)
15452 {
15453 struct window *w = XWINDOW (window);
15454 struct frame *f = XFRAME (w->frame);
15455 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15456
15457 #ifdef GLYPH_DEBUG
15458 if (inhibit_try_cursor_movement)
15459 return rc;
15460 #endif
15461
15462 /* Previously, there was a check for Lisp integer in the
15463 if-statement below. Now, this field is converted to
15464 ptrdiff_t, thus zero means invalid position in a buffer. */
15465 eassert (w->last_point > 0);
15466 /* Likewise there was a check whether window_end_vpos is nil or larger
15467 than the window. Now window_end_vpos is int and so never nil, but
15468 let's leave eassert to check whether it fits in the window. */
15469 eassert (!w->window_end_valid
15470 || w->window_end_vpos < w->current_matrix->nrows);
15471
15472 /* Handle case where text has not changed, only point, and it has
15473 not moved off the frame. */
15474 if (/* Point may be in this window. */
15475 PT >= CHARPOS (startp)
15476 /* Selective display hasn't changed. */
15477 && !current_buffer->clip_changed
15478 /* Function force-mode-line-update is used to force a thorough
15479 redisplay. It sets either windows_or_buffers_changed or
15480 update_mode_lines. So don't take a shortcut here for these
15481 cases. */
15482 && !update_mode_lines
15483 && !windows_or_buffers_changed
15484 && !f->cursor_type_changed
15485 && NILP (Vshow_trailing_whitespace)
15486 /* This code is not used for mini-buffer for the sake of the case
15487 of redisplaying to replace an echo area message; since in
15488 that case the mini-buffer contents per se are usually
15489 unchanged. This code is of no real use in the mini-buffer
15490 since the handling of this_line_start_pos, etc., in redisplay
15491 handles the same cases. */
15492 && !EQ (window, minibuf_window)
15493 && (FRAME_WINDOW_P (f)
15494 || !overlay_arrow_in_current_buffer_p ()))
15495 {
15496 int this_scroll_margin, top_scroll_margin;
15497 struct glyph_row *row = NULL;
15498 int frame_line_height = default_line_pixel_height (w);
15499 int window_total_lines
15500 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15501
15502 #ifdef GLYPH_DEBUG
15503 debug_method_add (w, "cursor movement");
15504 #endif
15505
15506 /* Scroll if point within this distance from the top or bottom
15507 of the window. This is a pixel value. */
15508 if (scroll_margin > 0)
15509 {
15510 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15511 this_scroll_margin *= frame_line_height;
15512 }
15513 else
15514 this_scroll_margin = 0;
15515
15516 top_scroll_margin = this_scroll_margin;
15517 if (WINDOW_WANTS_HEADER_LINE_P (w))
15518 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15519
15520 /* Start with the row the cursor was displayed during the last
15521 not paused redisplay. Give up if that row is not valid. */
15522 if (w->last_cursor_vpos < 0
15523 || w->last_cursor_vpos >= w->current_matrix->nrows)
15524 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15525 else
15526 {
15527 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15528 if (row->mode_line_p)
15529 ++row;
15530 if (!row->enabled_p)
15531 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15532 }
15533
15534 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15535 {
15536 bool scroll_p = false, must_scroll = false;
15537 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15538
15539 if (PT > w->last_point)
15540 {
15541 /* Point has moved forward. */
15542 while (MATRIX_ROW_END_CHARPOS (row) < PT
15543 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15544 {
15545 eassert (row->enabled_p);
15546 ++row;
15547 }
15548
15549 /* If the end position of a row equals the start
15550 position of the next row, and PT is at that position,
15551 we would rather display cursor in the next line. */
15552 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15553 && MATRIX_ROW_END_CHARPOS (row) == PT
15554 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15555 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15556 && !cursor_row_p (row))
15557 ++row;
15558
15559 /* If within the scroll margin, scroll. Note that
15560 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15561 the next line would be drawn, and that
15562 this_scroll_margin can be zero. */
15563 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15564 || PT > MATRIX_ROW_END_CHARPOS (row)
15565 /* Line is completely visible last line in window
15566 and PT is to be set in the next line. */
15567 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15568 && PT == MATRIX_ROW_END_CHARPOS (row)
15569 && !row->ends_at_zv_p
15570 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15571 scroll_p = true;
15572 }
15573 else if (PT < w->last_point)
15574 {
15575 /* Cursor has to be moved backward. Note that PT >=
15576 CHARPOS (startp) because of the outer if-statement. */
15577 while (!row->mode_line_p
15578 && (MATRIX_ROW_START_CHARPOS (row) > PT
15579 || (MATRIX_ROW_START_CHARPOS (row) == PT
15580 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15581 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15582 row > w->current_matrix->rows
15583 && (row-1)->ends_in_newline_from_string_p))))
15584 && (row->y > top_scroll_margin
15585 || CHARPOS (startp) == BEGV))
15586 {
15587 eassert (row->enabled_p);
15588 --row;
15589 }
15590
15591 /* Consider the following case: Window starts at BEGV,
15592 there is invisible, intangible text at BEGV, so that
15593 display starts at some point START > BEGV. It can
15594 happen that we are called with PT somewhere between
15595 BEGV and START. Try to handle that case. */
15596 if (row < w->current_matrix->rows
15597 || row->mode_line_p)
15598 {
15599 row = w->current_matrix->rows;
15600 if (row->mode_line_p)
15601 ++row;
15602 }
15603
15604 /* Due to newlines in overlay strings, we may have to
15605 skip forward over overlay strings. */
15606 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15607 && MATRIX_ROW_END_CHARPOS (row) == PT
15608 && !cursor_row_p (row))
15609 ++row;
15610
15611 /* If within the scroll margin, scroll. */
15612 if (row->y < top_scroll_margin
15613 && CHARPOS (startp) != BEGV)
15614 scroll_p = true;
15615 }
15616 else
15617 {
15618 /* Cursor did not move. So don't scroll even if cursor line
15619 is partially visible, as it was so before. */
15620 rc = CURSOR_MOVEMENT_SUCCESS;
15621 }
15622
15623 if (PT < MATRIX_ROW_START_CHARPOS (row)
15624 || PT > MATRIX_ROW_END_CHARPOS (row))
15625 {
15626 /* if PT is not in the glyph row, give up. */
15627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15628 must_scroll = true;
15629 }
15630 else if (rc != CURSOR_MOVEMENT_SUCCESS
15631 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15632 {
15633 struct glyph_row *row1;
15634
15635 /* If rows are bidi-reordered and point moved, back up
15636 until we find a row that does not belong to a
15637 continuation line. This is because we must consider
15638 all rows of a continued line as candidates for the
15639 new cursor positioning, since row start and end
15640 positions change non-linearly with vertical position
15641 in such rows. */
15642 /* FIXME: Revisit this when glyph ``spilling'' in
15643 continuation lines' rows is implemented for
15644 bidi-reordered rows. */
15645 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15646 MATRIX_ROW_CONTINUATION_LINE_P (row);
15647 --row)
15648 {
15649 /* If we hit the beginning of the displayed portion
15650 without finding the first row of a continued
15651 line, give up. */
15652 if (row <= row1)
15653 {
15654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15655 break;
15656 }
15657 eassert (row->enabled_p);
15658 }
15659 }
15660 if (must_scroll)
15661 ;
15662 else if (rc != CURSOR_MOVEMENT_SUCCESS
15663 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15664 /* Make sure this isn't a header line by any chance, since
15665 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15666 && !row->mode_line_p
15667 && make_cursor_line_fully_visible_p)
15668 {
15669 if (PT == MATRIX_ROW_END_CHARPOS (row)
15670 && !row->ends_at_zv_p
15671 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15672 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15673 else if (row->height > window_box_height (w))
15674 {
15675 /* If we end up in a partially visible line, let's
15676 make it fully visible, except when it's taller
15677 than the window, in which case we can't do much
15678 about it. */
15679 *scroll_step = true;
15680 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15681 }
15682 else
15683 {
15684 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15685 if (!cursor_row_fully_visible_p (w, false, true))
15686 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15687 else
15688 rc = CURSOR_MOVEMENT_SUCCESS;
15689 }
15690 }
15691 else if (scroll_p)
15692 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15693 else if (rc != CURSOR_MOVEMENT_SUCCESS
15694 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15695 {
15696 /* With bidi-reordered rows, there could be more than
15697 one candidate row whose start and end positions
15698 occlude point. We need to let set_cursor_from_row
15699 find the best candidate. */
15700 /* FIXME: Revisit this when glyph ``spilling'' in
15701 continuation lines' rows is implemented for
15702 bidi-reordered rows. */
15703 bool rv = false;
15704
15705 do
15706 {
15707 bool at_zv_p = false, exact_match_p = false;
15708
15709 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15710 && PT <= MATRIX_ROW_END_CHARPOS (row)
15711 && cursor_row_p (row))
15712 rv |= set_cursor_from_row (w, row, w->current_matrix,
15713 0, 0, 0, 0);
15714 /* As soon as we've found the exact match for point,
15715 or the first suitable row whose ends_at_zv_p flag
15716 is set, we are done. */
15717 if (rv)
15718 {
15719 at_zv_p = MATRIX_ROW (w->current_matrix,
15720 w->cursor.vpos)->ends_at_zv_p;
15721 if (!at_zv_p
15722 && w->cursor.hpos >= 0
15723 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15724 w->cursor.vpos))
15725 {
15726 struct glyph_row *candidate =
15727 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15728 struct glyph *g =
15729 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15730 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15731
15732 exact_match_p =
15733 (BUFFERP (g->object) && g->charpos == PT)
15734 || (NILP (g->object)
15735 && (g->charpos == PT
15736 || (g->charpos == 0 && endpos - 1 == PT)));
15737 }
15738 if (at_zv_p || exact_match_p)
15739 {
15740 rc = CURSOR_MOVEMENT_SUCCESS;
15741 break;
15742 }
15743 }
15744 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15745 break;
15746 ++row;
15747 }
15748 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15749 || row->continued_p)
15750 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15751 || (MATRIX_ROW_START_CHARPOS (row) == PT
15752 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15753 /* If we didn't find any candidate rows, or exited the
15754 loop before all the candidates were examined, signal
15755 to the caller that this method failed. */
15756 if (rc != CURSOR_MOVEMENT_SUCCESS
15757 && !(rv
15758 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15759 && !row->continued_p))
15760 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15761 else if (rv)
15762 rc = CURSOR_MOVEMENT_SUCCESS;
15763 }
15764 else
15765 {
15766 do
15767 {
15768 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15769 {
15770 rc = CURSOR_MOVEMENT_SUCCESS;
15771 break;
15772 }
15773 ++row;
15774 }
15775 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15776 && MATRIX_ROW_START_CHARPOS (row) == PT
15777 && cursor_row_p (row));
15778 }
15779 }
15780 }
15781
15782 return rc;
15783 }
15784
15785
15786 void
15787 set_vertical_scroll_bar (struct window *w)
15788 {
15789 ptrdiff_t start, end, whole;
15790
15791 /* Calculate the start and end positions for the current window.
15792 At some point, it would be nice to choose between scrollbars
15793 which reflect the whole buffer size, with special markers
15794 indicating narrowing, and scrollbars which reflect only the
15795 visible region.
15796
15797 Note that mini-buffers sometimes aren't displaying any text. */
15798 if (!MINI_WINDOW_P (w)
15799 || (w == XWINDOW (minibuf_window)
15800 && NILP (echo_area_buffer[0])))
15801 {
15802 struct buffer *buf = XBUFFER (w->contents);
15803 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15804 start = marker_position (w->start) - BUF_BEGV (buf);
15805 /* I don't think this is guaranteed to be right. For the
15806 moment, we'll pretend it is. */
15807 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15808
15809 if (end < start)
15810 end = start;
15811 if (whole < (end - start))
15812 whole = end - start;
15813 }
15814 else
15815 start = end = whole = 0;
15816
15817 /* Indicate what this scroll bar ought to be displaying now. */
15818 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15819 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15820 (w, end - start, whole, start);
15821 }
15822
15823
15824 void
15825 set_horizontal_scroll_bar (struct window *w)
15826 {
15827 int start, end, whole, portion;
15828
15829 if (!MINI_WINDOW_P (w)
15830 || (w == XWINDOW (minibuf_window)
15831 && NILP (echo_area_buffer[0])))
15832 {
15833 struct buffer *b = XBUFFER (w->contents);
15834 struct buffer *old_buffer = NULL;
15835 struct it it;
15836 struct text_pos startp;
15837
15838 if (b != current_buffer)
15839 {
15840 old_buffer = current_buffer;
15841 set_buffer_internal (b);
15842 }
15843
15844 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15845 start_display (&it, w, startp);
15846 it.last_visible_x = INT_MAX;
15847 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15848 MOVE_TO_X | MOVE_TO_Y);
15849 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15850 window_box_height (w), -1,
15851 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15852
15853 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15854 end = start + window_box_width (w, TEXT_AREA);
15855 portion = end - start;
15856 /* After enlarging a horizontally scrolled window such that it
15857 gets at least as wide as the text it contains, make sure that
15858 the thumb doesn't fill the entire scroll bar so we can still
15859 drag it back to see the entire text. */
15860 whole = max (whole, end);
15861
15862 if (it.bidi_p)
15863 {
15864 Lisp_Object pdir;
15865
15866 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15867 if (EQ (pdir, Qright_to_left))
15868 {
15869 start = whole - end;
15870 end = start + portion;
15871 }
15872 }
15873
15874 if (old_buffer)
15875 set_buffer_internal (old_buffer);
15876 }
15877 else
15878 start = end = whole = portion = 0;
15879
15880 w->hscroll_whole = whole;
15881
15882 /* Indicate what this scroll bar ought to be displaying now. */
15883 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15884 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15885 (w, portion, whole, start);
15886 }
15887
15888
15889 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15890 selected_window is redisplayed.
15891
15892 We can return without actually redisplaying the window if fonts has been
15893 changed on window's frame. In that case, redisplay_internal will retry.
15894
15895 As one of the important parts of redisplaying a window, we need to
15896 decide whether the previous window-start position (stored in the
15897 window's w->start marker position) is still valid, and if it isn't,
15898 recompute it. Some details about that:
15899
15900 . The previous window-start could be in a continuation line, in
15901 which case we need to recompute it when the window width
15902 changes. See compute_window_start_on_continuation_line and its
15903 call below.
15904
15905 . The text that changed since last redisplay could include the
15906 previous window-start position. In that case, we try to salvage
15907 what we can from the current glyph matrix by calling
15908 try_scrolling, which see.
15909
15910 . Some Emacs command could force us to use a specific window-start
15911 position by setting the window's force_start flag, or gently
15912 propose doing that by setting the window's optional_new_start
15913 flag. In these cases, we try using the specified start point if
15914 that succeeds (i.e. the window desired matrix is successfully
15915 recomputed, and point location is within the window). In case
15916 of optional_new_start, we first check if the specified start
15917 position is feasible, i.e. if it will allow point to be
15918 displayed in the window. If using the specified start point
15919 fails, e.g., if new fonts are needed to be loaded, we abort the
15920 redisplay cycle and leave it up to the next cycle to figure out
15921 things.
15922
15923 . Note that the window's force_start flag is sometimes set by
15924 redisplay itself, when it decides that the previous window start
15925 point is fine and should be kept. Search for "goto force_start"
15926 below to see the details. Like the values of window-start
15927 specified outside of redisplay, these internally-deduced values
15928 are tested for feasibility, and ignored if found to be
15929 unfeasible.
15930
15931 . Note that the function try_window, used to completely redisplay
15932 a window, accepts the window's start point as its argument.
15933 This is used several times in the redisplay code to control
15934 where the window start will be, according to user options such
15935 as scroll-conservatively, and also to ensure the screen line
15936 showing point will be fully (as opposed to partially) visible on
15937 display. */
15938
15939 static void
15940 redisplay_window (Lisp_Object window, bool just_this_one_p)
15941 {
15942 struct window *w = XWINDOW (window);
15943 struct frame *f = XFRAME (w->frame);
15944 struct buffer *buffer = XBUFFER (w->contents);
15945 struct buffer *old = current_buffer;
15946 struct text_pos lpoint, opoint, startp;
15947 bool update_mode_line;
15948 int tem;
15949 struct it it;
15950 /* Record it now because it's overwritten. */
15951 bool current_matrix_up_to_date_p = false;
15952 bool used_current_matrix_p = false;
15953 /* This is less strict than current_matrix_up_to_date_p.
15954 It indicates that the buffer contents and narrowing are unchanged. */
15955 bool buffer_unchanged_p = false;
15956 bool temp_scroll_step = false;
15957 ptrdiff_t count = SPECPDL_INDEX ();
15958 int rc;
15959 int centering_position = -1;
15960 bool last_line_misfit = false;
15961 ptrdiff_t beg_unchanged, end_unchanged;
15962 int frame_line_height;
15963
15964 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15965 opoint = lpoint;
15966
15967 #ifdef GLYPH_DEBUG
15968 *w->desired_matrix->method = 0;
15969 #endif
15970
15971 if (!just_this_one_p
15972 && REDISPLAY_SOME_P ()
15973 && !w->redisplay
15974 && !w->update_mode_line
15975 && !f->face_change
15976 && !f->redisplay
15977 && !buffer->text->redisplay
15978 && BUF_PT (buffer) == w->last_point)
15979 return;
15980
15981 /* Make sure that both W's markers are valid. */
15982 eassert (XMARKER (w->start)->buffer == buffer);
15983 eassert (XMARKER (w->pointm)->buffer == buffer);
15984
15985 /* We come here again if we need to run window-text-change-functions
15986 below. */
15987 restart:
15988 reconsider_clip_changes (w);
15989 frame_line_height = default_line_pixel_height (w);
15990
15991 /* Has the mode line to be updated? */
15992 update_mode_line = (w->update_mode_line
15993 || update_mode_lines
15994 || buffer->clip_changed
15995 || buffer->prevent_redisplay_optimizations_p);
15996
15997 if (!just_this_one_p)
15998 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15999 cleverly elsewhere. */
16000 w->must_be_updated_p = true;
16001
16002 if (MINI_WINDOW_P (w))
16003 {
16004 if (w == XWINDOW (echo_area_window)
16005 && !NILP (echo_area_buffer[0]))
16006 {
16007 if (update_mode_line)
16008 /* We may have to update a tty frame's menu bar or a
16009 tool-bar. Example `M-x C-h C-h C-g'. */
16010 goto finish_menu_bars;
16011 else
16012 /* We've already displayed the echo area glyphs in this window. */
16013 goto finish_scroll_bars;
16014 }
16015 else if ((w != XWINDOW (minibuf_window)
16016 || minibuf_level == 0)
16017 /* When buffer is nonempty, redisplay window normally. */
16018 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16019 /* Quail displays non-mini buffers in minibuffer window.
16020 In that case, redisplay the window normally. */
16021 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16022 {
16023 /* W is a mini-buffer window, but it's not active, so clear
16024 it. */
16025 int yb = window_text_bottom_y (w);
16026 struct glyph_row *row;
16027 int y;
16028
16029 for (y = 0, row = w->desired_matrix->rows;
16030 y < yb;
16031 y += row->height, ++row)
16032 blank_row (w, row, y);
16033 goto finish_scroll_bars;
16034 }
16035
16036 clear_glyph_matrix (w->desired_matrix);
16037 }
16038
16039 /* Otherwise set up data on this window; select its buffer and point
16040 value. */
16041 /* Really select the buffer, for the sake of buffer-local
16042 variables. */
16043 set_buffer_internal_1 (XBUFFER (w->contents));
16044
16045 current_matrix_up_to_date_p
16046 = (w->window_end_valid
16047 && !current_buffer->clip_changed
16048 && !current_buffer->prevent_redisplay_optimizations_p
16049 && !window_outdated (w));
16050
16051 /* Run the window-text-change-functions
16052 if it is possible that the text on the screen has changed
16053 (either due to modification of the text, or any other reason). */
16054 if (!current_matrix_up_to_date_p
16055 && !NILP (Vwindow_text_change_functions))
16056 {
16057 safe_run_hooks (Qwindow_text_change_functions);
16058 goto restart;
16059 }
16060
16061 beg_unchanged = BEG_UNCHANGED;
16062 end_unchanged = END_UNCHANGED;
16063
16064 SET_TEXT_POS (opoint, PT, PT_BYTE);
16065
16066 specbind (Qinhibit_point_motion_hooks, Qt);
16067
16068 buffer_unchanged_p
16069 = (w->window_end_valid
16070 && !current_buffer->clip_changed
16071 && !window_outdated (w));
16072
16073 /* When windows_or_buffers_changed is non-zero, we can't rely
16074 on the window end being valid, so set it to zero there. */
16075 if (windows_or_buffers_changed)
16076 {
16077 /* If window starts on a continuation line, maybe adjust the
16078 window start in case the window's width changed. */
16079 if (XMARKER (w->start)->buffer == current_buffer)
16080 compute_window_start_on_continuation_line (w);
16081
16082 w->window_end_valid = false;
16083 /* If so, we also can't rely on current matrix
16084 and should not fool try_cursor_movement below. */
16085 current_matrix_up_to_date_p = false;
16086 }
16087
16088 /* Some sanity checks. */
16089 CHECK_WINDOW_END (w);
16090 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16091 emacs_abort ();
16092 if (BYTEPOS (opoint) < CHARPOS (opoint))
16093 emacs_abort ();
16094
16095 if (mode_line_update_needed (w))
16096 update_mode_line = true;
16097
16098 /* Point refers normally to the selected window. For any other
16099 window, set up appropriate value. */
16100 if (!EQ (window, selected_window))
16101 {
16102 ptrdiff_t new_pt = marker_position (w->pointm);
16103 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16104
16105 if (new_pt < BEGV)
16106 {
16107 new_pt = BEGV;
16108 new_pt_byte = BEGV_BYTE;
16109 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16110 }
16111 else if (new_pt > (ZV - 1))
16112 {
16113 new_pt = ZV;
16114 new_pt_byte = ZV_BYTE;
16115 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16116 }
16117
16118 /* We don't use SET_PT so that the point-motion hooks don't run. */
16119 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16120 }
16121
16122 /* If any of the character widths specified in the display table
16123 have changed, invalidate the width run cache. It's true that
16124 this may be a bit late to catch such changes, but the rest of
16125 redisplay goes (non-fatally) haywire when the display table is
16126 changed, so why should we worry about doing any better? */
16127 if (current_buffer->width_run_cache
16128 || (current_buffer->base_buffer
16129 && current_buffer->base_buffer->width_run_cache))
16130 {
16131 struct Lisp_Char_Table *disptab = buffer_display_table ();
16132
16133 if (! disptab_matches_widthtab
16134 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16135 {
16136 struct buffer *buf = current_buffer;
16137
16138 if (buf->base_buffer)
16139 buf = buf->base_buffer;
16140 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16141 recompute_width_table (current_buffer, disptab);
16142 }
16143 }
16144
16145 /* If window-start is screwed up, choose a new one. */
16146 if (XMARKER (w->start)->buffer != current_buffer)
16147 goto recenter;
16148
16149 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16150
16151 /* If someone specified a new starting point but did not insist,
16152 check whether it can be used. */
16153 if ((w->optional_new_start || window_frozen_p (w))
16154 && CHARPOS (startp) >= BEGV
16155 && CHARPOS (startp) <= ZV)
16156 {
16157 ptrdiff_t it_charpos;
16158
16159 w->optional_new_start = false;
16160 start_display (&it, w, startp);
16161 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16162 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16163 /* Record IT's position now, since line_bottom_y might change
16164 that. */
16165 it_charpos = IT_CHARPOS (it);
16166 /* Make sure we set the force_start flag only if the cursor row
16167 will be fully visible. Otherwise, the code under force_start
16168 label below will try to move point back into view, which is
16169 not what the code which sets optional_new_start wants. */
16170 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16171 && !w->force_start)
16172 {
16173 if (it_charpos == PT)
16174 w->force_start = true;
16175 /* IT may overshoot PT if text at PT is invisible. */
16176 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16177 w->force_start = true;
16178 #ifdef GLYPH_DEBUG
16179 if (w->force_start)
16180 {
16181 if (window_frozen_p (w))
16182 debug_method_add (w, "set force_start from frozen window start");
16183 else
16184 debug_method_add (w, "set force_start from optional_new_start");
16185 }
16186 #endif
16187 }
16188 }
16189
16190 force_start:
16191
16192 /* Handle case where place to start displaying has been specified,
16193 unless the specified location is outside the accessible range. */
16194 if (w->force_start)
16195 {
16196 /* We set this later on if we have to adjust point. */
16197 int new_vpos = -1;
16198
16199 w->force_start = false;
16200 w->vscroll = 0;
16201 w->window_end_valid = false;
16202
16203 /* Forget any recorded base line for line number display. */
16204 if (!buffer_unchanged_p)
16205 w->base_line_number = 0;
16206
16207 /* Redisplay the mode line. Select the buffer properly for that.
16208 Also, run the hook window-scroll-functions
16209 because we have scrolled. */
16210 /* Note, we do this after clearing force_start because
16211 if there's an error, it is better to forget about force_start
16212 than to get into an infinite loop calling the hook functions
16213 and having them get more errors. */
16214 if (!update_mode_line
16215 || ! NILP (Vwindow_scroll_functions))
16216 {
16217 update_mode_line = true;
16218 w->update_mode_line = true;
16219 startp = run_window_scroll_functions (window, startp);
16220 }
16221
16222 if (CHARPOS (startp) < BEGV)
16223 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16224 else if (CHARPOS (startp) > ZV)
16225 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16226
16227 /* Redisplay, then check if cursor has been set during the
16228 redisplay. Give up if new fonts were loaded. */
16229 /* We used to issue a CHECK_MARGINS argument to try_window here,
16230 but this causes scrolling to fail when point begins inside
16231 the scroll margin (bug#148) -- cyd */
16232 if (!try_window (window, startp, 0))
16233 {
16234 w->force_start = true;
16235 clear_glyph_matrix (w->desired_matrix);
16236 goto need_larger_matrices;
16237 }
16238
16239 if (w->cursor.vpos < 0)
16240 {
16241 /* If point does not appear, try to move point so it does
16242 appear. The desired matrix has been built above, so we
16243 can use it here. */
16244 new_vpos = window_box_height (w) / 2;
16245 }
16246
16247 if (!cursor_row_fully_visible_p (w, false, false))
16248 {
16249 /* Point does appear, but on a line partly visible at end of window.
16250 Move it back to a fully-visible line. */
16251 new_vpos = window_box_height (w);
16252 /* But if window_box_height suggests a Y coordinate that is
16253 not less than we already have, that line will clearly not
16254 be fully visible, so give up and scroll the display.
16255 This can happen when the default face uses a font whose
16256 dimensions are different from the frame's default
16257 font. */
16258 if (new_vpos >= w->cursor.y)
16259 {
16260 w->cursor.vpos = -1;
16261 clear_glyph_matrix (w->desired_matrix);
16262 goto try_to_scroll;
16263 }
16264 }
16265 else if (w->cursor.vpos >= 0)
16266 {
16267 /* Some people insist on not letting point enter the scroll
16268 margin, even though this part handles windows that didn't
16269 scroll at all. */
16270 int window_total_lines
16271 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16272 int margin = min (scroll_margin, window_total_lines / 4);
16273 int pixel_margin = margin * frame_line_height;
16274 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16275
16276 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16277 below, which finds the row to move point to, advances by
16278 the Y coordinate of the _next_ row, see the definition of
16279 MATRIX_ROW_BOTTOM_Y. */
16280 if (w->cursor.vpos < margin + header_line)
16281 {
16282 w->cursor.vpos = -1;
16283 clear_glyph_matrix (w->desired_matrix);
16284 goto try_to_scroll;
16285 }
16286 else
16287 {
16288 int window_height = window_box_height (w);
16289
16290 if (header_line)
16291 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16292 if (w->cursor.y >= window_height - pixel_margin)
16293 {
16294 w->cursor.vpos = -1;
16295 clear_glyph_matrix (w->desired_matrix);
16296 goto try_to_scroll;
16297 }
16298 }
16299 }
16300
16301 /* If we need to move point for either of the above reasons,
16302 now actually do it. */
16303 if (new_vpos >= 0)
16304 {
16305 struct glyph_row *row;
16306
16307 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16308 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16309 ++row;
16310
16311 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16312 MATRIX_ROW_START_BYTEPOS (row));
16313
16314 if (w != XWINDOW (selected_window))
16315 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16316 else if (current_buffer == old)
16317 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16318
16319 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16320
16321 /* Re-run pre-redisplay-function so it can update the region
16322 according to the new position of point. */
16323 /* Other than the cursor, w's redisplay is done so we can set its
16324 redisplay to false. Also the buffer's redisplay can be set to
16325 false, since propagate_buffer_redisplay should have already
16326 propagated its info to `w' anyway. */
16327 w->redisplay = false;
16328 XBUFFER (w->contents)->text->redisplay = false;
16329 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16330
16331 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16332 {
16333 /* pre-redisplay-function made changes (e.g. move the region)
16334 that require another round of redisplay. */
16335 clear_glyph_matrix (w->desired_matrix);
16336 if (!try_window (window, startp, 0))
16337 goto need_larger_matrices;
16338 }
16339 }
16340 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16341 {
16342 clear_glyph_matrix (w->desired_matrix);
16343 goto try_to_scroll;
16344 }
16345
16346 #ifdef GLYPH_DEBUG
16347 debug_method_add (w, "forced window start");
16348 #endif
16349 goto done;
16350 }
16351
16352 /* Handle case where text has not changed, only point, and it has
16353 not moved off the frame, and we are not retrying after hscroll.
16354 (current_matrix_up_to_date_p is true when retrying.) */
16355 if (current_matrix_up_to_date_p
16356 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16357 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16358 {
16359 switch (rc)
16360 {
16361 case CURSOR_MOVEMENT_SUCCESS:
16362 used_current_matrix_p = true;
16363 goto done;
16364
16365 case CURSOR_MOVEMENT_MUST_SCROLL:
16366 goto try_to_scroll;
16367
16368 default:
16369 emacs_abort ();
16370 }
16371 }
16372 /* If current starting point was originally the beginning of a line
16373 but no longer is, find a new starting point. */
16374 else if (w->start_at_line_beg
16375 && !(CHARPOS (startp) <= BEGV
16376 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16377 {
16378 #ifdef GLYPH_DEBUG
16379 debug_method_add (w, "recenter 1");
16380 #endif
16381 goto recenter;
16382 }
16383
16384 /* Try scrolling with try_window_id. Value is > 0 if update has
16385 been done, it is -1 if we know that the same window start will
16386 not work. It is 0 if unsuccessful for some other reason. */
16387 else if ((tem = try_window_id (w)) != 0)
16388 {
16389 #ifdef GLYPH_DEBUG
16390 debug_method_add (w, "try_window_id %d", tem);
16391 #endif
16392
16393 if (f->fonts_changed)
16394 goto need_larger_matrices;
16395 if (tem > 0)
16396 goto done;
16397
16398 /* Otherwise try_window_id has returned -1 which means that we
16399 don't want the alternative below this comment to execute. */
16400 }
16401 else if (CHARPOS (startp) >= BEGV
16402 && CHARPOS (startp) <= ZV
16403 && PT >= CHARPOS (startp)
16404 && (CHARPOS (startp) < ZV
16405 /* Avoid starting at end of buffer. */
16406 || CHARPOS (startp) == BEGV
16407 || !window_outdated (w)))
16408 {
16409 int d1, d2, d5, d6;
16410 int rtop, rbot;
16411
16412 /* If first window line is a continuation line, and window start
16413 is inside the modified region, but the first change is before
16414 current window start, we must select a new window start.
16415
16416 However, if this is the result of a down-mouse event (e.g. by
16417 extending the mouse-drag-overlay), we don't want to select a
16418 new window start, since that would change the position under
16419 the mouse, resulting in an unwanted mouse-movement rather
16420 than a simple mouse-click. */
16421 if (!w->start_at_line_beg
16422 && NILP (do_mouse_tracking)
16423 && CHARPOS (startp) > BEGV
16424 && CHARPOS (startp) > BEG + beg_unchanged
16425 && CHARPOS (startp) <= Z - end_unchanged
16426 /* Even if w->start_at_line_beg is nil, a new window may
16427 start at a line_beg, since that's how set_buffer_window
16428 sets it. So, we need to check the return value of
16429 compute_window_start_on_continuation_line. (See also
16430 bug#197). */
16431 && XMARKER (w->start)->buffer == current_buffer
16432 && compute_window_start_on_continuation_line (w)
16433 /* It doesn't make sense to force the window start like we
16434 do at label force_start if it is already known that point
16435 will not be fully visible in the resulting window, because
16436 doing so will move point from its correct position
16437 instead of scrolling the window to bring point into view.
16438 See bug#9324. */
16439 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16440 /* A very tall row could need more than the window height,
16441 in which case we accept that it is partially visible. */
16442 && (rtop != 0) == (rbot != 0))
16443 {
16444 w->force_start = true;
16445 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16446 #ifdef GLYPH_DEBUG
16447 debug_method_add (w, "recomputed window start in continuation line");
16448 #endif
16449 goto force_start;
16450 }
16451
16452 #ifdef GLYPH_DEBUG
16453 debug_method_add (w, "same window start");
16454 #endif
16455
16456 /* Try to redisplay starting at same place as before.
16457 If point has not moved off frame, accept the results. */
16458 if (!current_matrix_up_to_date_p
16459 /* Don't use try_window_reusing_current_matrix in this case
16460 because a window scroll function can have changed the
16461 buffer. */
16462 || !NILP (Vwindow_scroll_functions)
16463 || MINI_WINDOW_P (w)
16464 || !(used_current_matrix_p
16465 = try_window_reusing_current_matrix (w)))
16466 {
16467 IF_DEBUG (debug_method_add (w, "1"));
16468 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16469 /* -1 means we need to scroll.
16470 0 means we need new matrices, but fonts_changed
16471 is set in that case, so we will detect it below. */
16472 goto try_to_scroll;
16473 }
16474
16475 if (f->fonts_changed)
16476 goto need_larger_matrices;
16477
16478 if (w->cursor.vpos >= 0)
16479 {
16480 if (!just_this_one_p
16481 || current_buffer->clip_changed
16482 || BEG_UNCHANGED < CHARPOS (startp))
16483 /* Forget any recorded base line for line number display. */
16484 w->base_line_number = 0;
16485
16486 if (!cursor_row_fully_visible_p (w, true, false))
16487 {
16488 clear_glyph_matrix (w->desired_matrix);
16489 last_line_misfit = true;
16490 }
16491 /* Drop through and scroll. */
16492 else
16493 goto done;
16494 }
16495 else
16496 clear_glyph_matrix (w->desired_matrix);
16497 }
16498
16499 try_to_scroll:
16500
16501 /* Redisplay the mode line. Select the buffer properly for that. */
16502 if (!update_mode_line)
16503 {
16504 update_mode_line = true;
16505 w->update_mode_line = true;
16506 }
16507
16508 /* Try to scroll by specified few lines. */
16509 if ((scroll_conservatively
16510 || emacs_scroll_step
16511 || temp_scroll_step
16512 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16513 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16514 && CHARPOS (startp) >= BEGV
16515 && CHARPOS (startp) <= ZV)
16516 {
16517 /* The function returns -1 if new fonts were loaded, 1 if
16518 successful, 0 if not successful. */
16519 int ss = try_scrolling (window, just_this_one_p,
16520 scroll_conservatively,
16521 emacs_scroll_step,
16522 temp_scroll_step, last_line_misfit);
16523 switch (ss)
16524 {
16525 case SCROLLING_SUCCESS:
16526 goto done;
16527
16528 case SCROLLING_NEED_LARGER_MATRICES:
16529 goto need_larger_matrices;
16530
16531 case SCROLLING_FAILED:
16532 break;
16533
16534 default:
16535 emacs_abort ();
16536 }
16537 }
16538
16539 /* Finally, just choose a place to start which positions point
16540 according to user preferences. */
16541
16542 recenter:
16543
16544 #ifdef GLYPH_DEBUG
16545 debug_method_add (w, "recenter");
16546 #endif
16547
16548 /* Forget any previously recorded base line for line number display. */
16549 if (!buffer_unchanged_p)
16550 w->base_line_number = 0;
16551
16552 /* Determine the window start relative to point. */
16553 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16554 it.current_y = it.last_visible_y;
16555 if (centering_position < 0)
16556 {
16557 int window_total_lines
16558 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16559 int margin
16560 = scroll_margin > 0
16561 ? min (scroll_margin, window_total_lines / 4)
16562 : 0;
16563 ptrdiff_t margin_pos = CHARPOS (startp);
16564 Lisp_Object aggressive;
16565 bool scrolling_up;
16566
16567 /* If there is a scroll margin at the top of the window, find
16568 its character position. */
16569 if (margin
16570 /* Cannot call start_display if startp is not in the
16571 accessible region of the buffer. This can happen when we
16572 have just switched to a different buffer and/or changed
16573 its restriction. In that case, startp is initialized to
16574 the character position 1 (BEGV) because we did not yet
16575 have chance to display the buffer even once. */
16576 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16577 {
16578 struct it it1;
16579 void *it1data = NULL;
16580
16581 SAVE_IT (it1, it, it1data);
16582 start_display (&it1, w, startp);
16583 move_it_vertically (&it1, margin * frame_line_height);
16584 margin_pos = IT_CHARPOS (it1);
16585 RESTORE_IT (&it, &it, it1data);
16586 }
16587 scrolling_up = PT > margin_pos;
16588 aggressive =
16589 scrolling_up
16590 ? BVAR (current_buffer, scroll_up_aggressively)
16591 : BVAR (current_buffer, scroll_down_aggressively);
16592
16593 if (!MINI_WINDOW_P (w)
16594 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16595 {
16596 int pt_offset = 0;
16597
16598 /* Setting scroll-conservatively overrides
16599 scroll-*-aggressively. */
16600 if (!scroll_conservatively && NUMBERP (aggressive))
16601 {
16602 double float_amount = XFLOATINT (aggressive);
16603
16604 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16605 if (pt_offset == 0 && float_amount > 0)
16606 pt_offset = 1;
16607 if (pt_offset && margin > 0)
16608 margin -= 1;
16609 }
16610 /* Compute how much to move the window start backward from
16611 point so that point will be displayed where the user
16612 wants it. */
16613 if (scrolling_up)
16614 {
16615 centering_position = it.last_visible_y;
16616 if (pt_offset)
16617 centering_position -= pt_offset;
16618 centering_position -=
16619 (frame_line_height * (1 + margin + last_line_misfit)
16620 + WINDOW_HEADER_LINE_HEIGHT (w));
16621 /* Don't let point enter the scroll margin near top of
16622 the window. */
16623 if (centering_position < margin * frame_line_height)
16624 centering_position = margin * frame_line_height;
16625 }
16626 else
16627 centering_position = margin * frame_line_height + pt_offset;
16628 }
16629 else
16630 /* Set the window start half the height of the window backward
16631 from point. */
16632 centering_position = window_box_height (w) / 2;
16633 }
16634 move_it_vertically_backward (&it, centering_position);
16635
16636 eassert (IT_CHARPOS (it) >= BEGV);
16637
16638 /* The function move_it_vertically_backward may move over more
16639 than the specified y-distance. If it->w is small, e.g. a
16640 mini-buffer window, we may end up in front of the window's
16641 display area. Start displaying at the start of the line
16642 containing PT in this case. */
16643 if (it.current_y <= 0)
16644 {
16645 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16646 move_it_vertically_backward (&it, 0);
16647 it.current_y = 0;
16648 }
16649
16650 it.current_x = it.hpos = 0;
16651
16652 /* Set the window start position here explicitly, to avoid an
16653 infinite loop in case the functions in window-scroll-functions
16654 get errors. */
16655 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16656
16657 /* Run scroll hooks. */
16658 startp = run_window_scroll_functions (window, it.current.pos);
16659
16660 /* Redisplay the window. */
16661 if (!current_matrix_up_to_date_p
16662 || windows_or_buffers_changed
16663 || f->cursor_type_changed
16664 /* Don't use try_window_reusing_current_matrix in this case
16665 because it can have changed the buffer. */
16666 || !NILP (Vwindow_scroll_functions)
16667 || !just_this_one_p
16668 || MINI_WINDOW_P (w)
16669 || !(used_current_matrix_p
16670 = try_window_reusing_current_matrix (w)))
16671 try_window (window, startp, 0);
16672
16673 /* If new fonts have been loaded (due to fontsets), give up. We
16674 have to start a new redisplay since we need to re-adjust glyph
16675 matrices. */
16676 if (f->fonts_changed)
16677 goto need_larger_matrices;
16678
16679 /* If cursor did not appear assume that the middle of the window is
16680 in the first line of the window. Do it again with the next line.
16681 (Imagine a window of height 100, displaying two lines of height
16682 60. Moving back 50 from it->last_visible_y will end in the first
16683 line.) */
16684 if (w->cursor.vpos < 0)
16685 {
16686 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16687 {
16688 clear_glyph_matrix (w->desired_matrix);
16689 move_it_by_lines (&it, 1);
16690 try_window (window, it.current.pos, 0);
16691 }
16692 else if (PT < IT_CHARPOS (it))
16693 {
16694 clear_glyph_matrix (w->desired_matrix);
16695 move_it_by_lines (&it, -1);
16696 try_window (window, it.current.pos, 0);
16697 }
16698 else
16699 {
16700 /* Not much we can do about it. */
16701 }
16702 }
16703
16704 /* Consider the following case: Window starts at BEGV, there is
16705 invisible, intangible text at BEGV, so that display starts at
16706 some point START > BEGV. It can happen that we are called with
16707 PT somewhere between BEGV and START. Try to handle that case,
16708 and similar ones. */
16709 if (w->cursor.vpos < 0)
16710 {
16711 /* First, try locating the proper glyph row for PT. */
16712 struct glyph_row *row =
16713 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16714
16715 /* Sometimes point is at the beginning of invisible text that is
16716 before the 1st character displayed in the row. In that case,
16717 row_containing_pos fails to find the row, because no glyphs
16718 with appropriate buffer positions are present in the row.
16719 Therefore, we next try to find the row which shows the 1st
16720 position after the invisible text. */
16721 if (!row)
16722 {
16723 Lisp_Object val =
16724 get_char_property_and_overlay (make_number (PT), Qinvisible,
16725 Qnil, NULL);
16726
16727 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16728 {
16729 ptrdiff_t alt_pos;
16730 Lisp_Object invis_end =
16731 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16732 Qnil, Qnil);
16733
16734 if (NATNUMP (invis_end))
16735 alt_pos = XFASTINT (invis_end);
16736 else
16737 alt_pos = ZV;
16738 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16739 NULL, 0);
16740 }
16741 }
16742 /* Finally, fall back on the first row of the window after the
16743 header line (if any). This is slightly better than not
16744 displaying the cursor at all. */
16745 if (!row)
16746 {
16747 row = w->current_matrix->rows;
16748 if (row->mode_line_p)
16749 ++row;
16750 }
16751 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16752 }
16753
16754 if (!cursor_row_fully_visible_p (w, false, false))
16755 {
16756 /* If vscroll is enabled, disable it and try again. */
16757 if (w->vscroll)
16758 {
16759 w->vscroll = 0;
16760 clear_glyph_matrix (w->desired_matrix);
16761 goto recenter;
16762 }
16763
16764 /* Users who set scroll-conservatively to a large number want
16765 point just above/below the scroll margin. If we ended up
16766 with point's row partially visible, move the window start to
16767 make that row fully visible and out of the margin. */
16768 if (scroll_conservatively > SCROLL_LIMIT)
16769 {
16770 int window_total_lines
16771 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16772 int margin =
16773 scroll_margin > 0
16774 ? min (scroll_margin, window_total_lines / 4)
16775 : 0;
16776 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16777
16778 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16779 clear_glyph_matrix (w->desired_matrix);
16780 if (1 == try_window (window, it.current.pos,
16781 TRY_WINDOW_CHECK_MARGINS))
16782 goto done;
16783 }
16784
16785 /* If centering point failed to make the whole line visible,
16786 put point at the top instead. That has to make the whole line
16787 visible, if it can be done. */
16788 if (centering_position == 0)
16789 goto done;
16790
16791 clear_glyph_matrix (w->desired_matrix);
16792 centering_position = 0;
16793 goto recenter;
16794 }
16795
16796 done:
16797
16798 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16799 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16800 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16801
16802 /* Display the mode line, if we must. */
16803 if ((update_mode_line
16804 /* If window not full width, must redo its mode line
16805 if (a) the window to its side is being redone and
16806 (b) we do a frame-based redisplay. This is a consequence
16807 of how inverted lines are drawn in frame-based redisplay. */
16808 || (!just_this_one_p
16809 && !FRAME_WINDOW_P (f)
16810 && !WINDOW_FULL_WIDTH_P (w))
16811 /* Line number to display. */
16812 || w->base_line_pos > 0
16813 /* Column number is displayed and different from the one displayed. */
16814 || (w->column_number_displayed != -1
16815 && (w->column_number_displayed != current_column ())))
16816 /* This means that the window has a mode line. */
16817 && (WINDOW_WANTS_MODELINE_P (w)
16818 || WINDOW_WANTS_HEADER_LINE_P (w)))
16819 {
16820
16821 display_mode_lines (w);
16822
16823 /* If mode line height has changed, arrange for a thorough
16824 immediate redisplay using the correct mode line height. */
16825 if (WINDOW_WANTS_MODELINE_P (w)
16826 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16827 {
16828 f->fonts_changed = true;
16829 w->mode_line_height = -1;
16830 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16831 = DESIRED_MODE_LINE_HEIGHT (w);
16832 }
16833
16834 /* If header line height has changed, arrange for a thorough
16835 immediate redisplay using the correct header line height. */
16836 if (WINDOW_WANTS_HEADER_LINE_P (w)
16837 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16838 {
16839 f->fonts_changed = true;
16840 w->header_line_height = -1;
16841 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16842 = DESIRED_HEADER_LINE_HEIGHT (w);
16843 }
16844
16845 if (f->fonts_changed)
16846 goto need_larger_matrices;
16847 }
16848
16849 if (!line_number_displayed && w->base_line_pos != -1)
16850 {
16851 w->base_line_pos = 0;
16852 w->base_line_number = 0;
16853 }
16854
16855 finish_menu_bars:
16856
16857 /* When we reach a frame's selected window, redo the frame's menu bar. */
16858 if (update_mode_line
16859 && EQ (FRAME_SELECTED_WINDOW (f), window))
16860 {
16861 bool redisplay_menu_p;
16862
16863 if (FRAME_WINDOW_P (f))
16864 {
16865 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16866 || defined (HAVE_NS) || defined (USE_GTK)
16867 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16868 #else
16869 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16870 #endif
16871 }
16872 else
16873 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16874
16875 if (redisplay_menu_p)
16876 display_menu_bar (w);
16877
16878 #ifdef HAVE_WINDOW_SYSTEM
16879 if (FRAME_WINDOW_P (f))
16880 {
16881 #if defined (USE_GTK) || defined (HAVE_NS)
16882 if (FRAME_EXTERNAL_TOOL_BAR (f))
16883 redisplay_tool_bar (f);
16884 #else
16885 if (WINDOWP (f->tool_bar_window)
16886 && (FRAME_TOOL_BAR_LINES (f) > 0
16887 || !NILP (Vauto_resize_tool_bars))
16888 && redisplay_tool_bar (f))
16889 ignore_mouse_drag_p = true;
16890 #endif
16891 }
16892 #endif
16893 }
16894
16895 #ifdef HAVE_WINDOW_SYSTEM
16896 if (FRAME_WINDOW_P (f)
16897 && update_window_fringes (w, (just_this_one_p
16898 || (!used_current_matrix_p && !overlay_arrow_seen)
16899 || w->pseudo_window_p)))
16900 {
16901 update_begin (f);
16902 block_input ();
16903 if (draw_window_fringes (w, true))
16904 {
16905 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16906 x_draw_right_divider (w);
16907 else
16908 x_draw_vertical_border (w);
16909 }
16910 unblock_input ();
16911 update_end (f);
16912 }
16913
16914 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16915 x_draw_bottom_divider (w);
16916 #endif /* HAVE_WINDOW_SYSTEM */
16917
16918 /* We go to this label, with fonts_changed set, if it is
16919 necessary to try again using larger glyph matrices.
16920 We have to redeem the scroll bar even in this case,
16921 because the loop in redisplay_internal expects that. */
16922 need_larger_matrices:
16923 ;
16924 finish_scroll_bars:
16925
16926 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16927 {
16928 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16929 /* Set the thumb's position and size. */
16930 set_vertical_scroll_bar (w);
16931
16932 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16933 /* Set the thumb's position and size. */
16934 set_horizontal_scroll_bar (w);
16935
16936 /* Note that we actually used the scroll bar attached to this
16937 window, so it shouldn't be deleted at the end of redisplay. */
16938 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16939 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16940 }
16941
16942 /* Restore current_buffer and value of point in it. The window
16943 update may have changed the buffer, so first make sure `opoint'
16944 is still valid (Bug#6177). */
16945 if (CHARPOS (opoint) < BEGV)
16946 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16947 else if (CHARPOS (opoint) > ZV)
16948 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16949 else
16950 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16951
16952 set_buffer_internal_1 (old);
16953 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16954 shorter. This can be caused by log truncation in *Messages*. */
16955 if (CHARPOS (lpoint) <= ZV)
16956 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16957
16958 unbind_to (count, Qnil);
16959 }
16960
16961
16962 /* Build the complete desired matrix of WINDOW with a window start
16963 buffer position POS.
16964
16965 Value is 1 if successful. It is zero if fonts were loaded during
16966 redisplay which makes re-adjusting glyph matrices necessary, and -1
16967 if point would appear in the scroll margins.
16968 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16969 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16970 set in FLAGS.) */
16971
16972 int
16973 try_window (Lisp_Object window, struct text_pos pos, int flags)
16974 {
16975 struct window *w = XWINDOW (window);
16976 struct it it;
16977 struct glyph_row *last_text_row = NULL;
16978 struct frame *f = XFRAME (w->frame);
16979 int frame_line_height = default_line_pixel_height (w);
16980
16981 /* Make POS the new window start. */
16982 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16983
16984 /* Mark cursor position as unknown. No overlay arrow seen. */
16985 w->cursor.vpos = -1;
16986 overlay_arrow_seen = false;
16987
16988 /* Initialize iterator and info to start at POS. */
16989 start_display (&it, w, pos);
16990 it.glyph_row->reversed_p = false;
16991
16992 /* Display all lines of W. */
16993 while (it.current_y < it.last_visible_y)
16994 {
16995 if (display_line (&it))
16996 last_text_row = it.glyph_row - 1;
16997 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16998 return 0;
16999 }
17000
17001 /* Don't let the cursor end in the scroll margins. */
17002 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17003 && !MINI_WINDOW_P (w))
17004 {
17005 int this_scroll_margin;
17006 int window_total_lines
17007 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17008
17009 if (scroll_margin > 0)
17010 {
17011 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17012 this_scroll_margin *= frame_line_height;
17013 }
17014 else
17015 this_scroll_margin = 0;
17016
17017 if ((w->cursor.y >= 0 /* not vscrolled */
17018 && w->cursor.y < this_scroll_margin
17019 && CHARPOS (pos) > BEGV
17020 && IT_CHARPOS (it) < ZV)
17021 /* rms: considering make_cursor_line_fully_visible_p here
17022 seems to give wrong results. We don't want to recenter
17023 when the last line is partly visible, we want to allow
17024 that case to be handled in the usual way. */
17025 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17026 {
17027 w->cursor.vpos = -1;
17028 clear_glyph_matrix (w->desired_matrix);
17029 return -1;
17030 }
17031 }
17032
17033 /* If bottom moved off end of frame, change mode line percentage. */
17034 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17035 w->update_mode_line = true;
17036
17037 /* Set window_end_pos to the offset of the last character displayed
17038 on the window from the end of current_buffer. Set
17039 window_end_vpos to its row number. */
17040 if (last_text_row)
17041 {
17042 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17043 adjust_window_ends (w, last_text_row, false);
17044 eassert
17045 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17046 w->window_end_vpos)));
17047 }
17048 else
17049 {
17050 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17051 w->window_end_pos = Z - ZV;
17052 w->window_end_vpos = 0;
17053 }
17054
17055 /* But that is not valid info until redisplay finishes. */
17056 w->window_end_valid = false;
17057 return 1;
17058 }
17059
17060
17061 \f
17062 /************************************************************************
17063 Window redisplay reusing current matrix when buffer has not changed
17064 ************************************************************************/
17065
17066 /* Try redisplay of window W showing an unchanged buffer with a
17067 different window start than the last time it was displayed by
17068 reusing its current matrix. Value is true if successful.
17069 W->start is the new window start. */
17070
17071 static bool
17072 try_window_reusing_current_matrix (struct window *w)
17073 {
17074 struct frame *f = XFRAME (w->frame);
17075 struct glyph_row *bottom_row;
17076 struct it it;
17077 struct run run;
17078 struct text_pos start, new_start;
17079 int nrows_scrolled, i;
17080 struct glyph_row *last_text_row;
17081 struct glyph_row *last_reused_text_row;
17082 struct glyph_row *start_row;
17083 int start_vpos, min_y, max_y;
17084
17085 #ifdef GLYPH_DEBUG
17086 if (inhibit_try_window_reusing)
17087 return false;
17088 #endif
17089
17090 if (/* This function doesn't handle terminal frames. */
17091 !FRAME_WINDOW_P (f)
17092 /* Don't try to reuse the display if windows have been split
17093 or such. */
17094 || windows_or_buffers_changed
17095 || f->cursor_type_changed)
17096 return false;
17097
17098 /* Can't do this if showing trailing whitespace. */
17099 if (!NILP (Vshow_trailing_whitespace))
17100 return false;
17101
17102 /* If top-line visibility has changed, give up. */
17103 if (WINDOW_WANTS_HEADER_LINE_P (w)
17104 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17105 return false;
17106
17107 /* Give up if old or new display is scrolled vertically. We could
17108 make this function handle this, but right now it doesn't. */
17109 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17110 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17111 return false;
17112
17113 /* The variable new_start now holds the new window start. The old
17114 start `start' can be determined from the current matrix. */
17115 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17116 start = start_row->minpos;
17117 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17118
17119 /* Clear the desired matrix for the display below. */
17120 clear_glyph_matrix (w->desired_matrix);
17121
17122 if (CHARPOS (new_start) <= CHARPOS (start))
17123 {
17124 /* Don't use this method if the display starts with an ellipsis
17125 displayed for invisible text. It's not easy to handle that case
17126 below, and it's certainly not worth the effort since this is
17127 not a frequent case. */
17128 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17129 return false;
17130
17131 IF_DEBUG (debug_method_add (w, "twu1"));
17132
17133 /* Display up to a row that can be reused. The variable
17134 last_text_row is set to the last row displayed that displays
17135 text. Note that it.vpos == 0 if or if not there is a
17136 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17137 start_display (&it, w, new_start);
17138 w->cursor.vpos = -1;
17139 last_text_row = last_reused_text_row = NULL;
17140
17141 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17142 {
17143 /* If we have reached into the characters in the START row,
17144 that means the line boundaries have changed. So we
17145 can't start copying with the row START. Maybe it will
17146 work to start copying with the following row. */
17147 while (IT_CHARPOS (it) > CHARPOS (start))
17148 {
17149 /* Advance to the next row as the "start". */
17150 start_row++;
17151 start = start_row->minpos;
17152 /* If there are no more rows to try, or just one, give up. */
17153 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17154 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17155 || CHARPOS (start) == ZV)
17156 {
17157 clear_glyph_matrix (w->desired_matrix);
17158 return false;
17159 }
17160
17161 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17162 }
17163 /* If we have reached alignment, we can copy the rest of the
17164 rows. */
17165 if (IT_CHARPOS (it) == CHARPOS (start)
17166 /* Don't accept "alignment" inside a display vector,
17167 since start_row could have started in the middle of
17168 that same display vector (thus their character
17169 positions match), and we have no way of telling if
17170 that is the case. */
17171 && it.current.dpvec_index < 0)
17172 break;
17173
17174 it.glyph_row->reversed_p = false;
17175 if (display_line (&it))
17176 last_text_row = it.glyph_row - 1;
17177
17178 }
17179
17180 /* A value of current_y < last_visible_y means that we stopped
17181 at the previous window start, which in turn means that we
17182 have at least one reusable row. */
17183 if (it.current_y < it.last_visible_y)
17184 {
17185 struct glyph_row *row;
17186
17187 /* IT.vpos always starts from 0; it counts text lines. */
17188 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17189
17190 /* Find PT if not already found in the lines displayed. */
17191 if (w->cursor.vpos < 0)
17192 {
17193 int dy = it.current_y - start_row->y;
17194
17195 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17196 row = row_containing_pos (w, PT, row, NULL, dy);
17197 if (row)
17198 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17199 dy, nrows_scrolled);
17200 else
17201 {
17202 clear_glyph_matrix (w->desired_matrix);
17203 return false;
17204 }
17205 }
17206
17207 /* Scroll the display. Do it before the current matrix is
17208 changed. The problem here is that update has not yet
17209 run, i.e. part of the current matrix is not up to date.
17210 scroll_run_hook will clear the cursor, and use the
17211 current matrix to get the height of the row the cursor is
17212 in. */
17213 run.current_y = start_row->y;
17214 run.desired_y = it.current_y;
17215 run.height = it.last_visible_y - it.current_y;
17216
17217 if (run.height > 0 && run.current_y != run.desired_y)
17218 {
17219 update_begin (f);
17220 FRAME_RIF (f)->update_window_begin_hook (w);
17221 FRAME_RIF (f)->clear_window_mouse_face (w);
17222 FRAME_RIF (f)->scroll_run_hook (w, &run);
17223 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17224 update_end (f);
17225 }
17226
17227 /* Shift current matrix down by nrows_scrolled lines. */
17228 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17229 rotate_matrix (w->current_matrix,
17230 start_vpos,
17231 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17232 nrows_scrolled);
17233
17234 /* Disable lines that must be updated. */
17235 for (i = 0; i < nrows_scrolled; ++i)
17236 (start_row + i)->enabled_p = false;
17237
17238 /* Re-compute Y positions. */
17239 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17240 max_y = it.last_visible_y;
17241 for (row = start_row + nrows_scrolled;
17242 row < bottom_row;
17243 ++row)
17244 {
17245 row->y = it.current_y;
17246 row->visible_height = row->height;
17247
17248 if (row->y < min_y)
17249 row->visible_height -= min_y - row->y;
17250 if (row->y + row->height > max_y)
17251 row->visible_height -= row->y + row->height - max_y;
17252 if (row->fringe_bitmap_periodic_p)
17253 row->redraw_fringe_bitmaps_p = true;
17254
17255 it.current_y += row->height;
17256
17257 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17258 last_reused_text_row = row;
17259 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17260 break;
17261 }
17262
17263 /* Disable lines in the current matrix which are now
17264 below the window. */
17265 for (++row; row < bottom_row; ++row)
17266 row->enabled_p = row->mode_line_p = false;
17267 }
17268
17269 /* Update window_end_pos etc.; last_reused_text_row is the last
17270 reused row from the current matrix containing text, if any.
17271 The value of last_text_row is the last displayed line
17272 containing text. */
17273 if (last_reused_text_row)
17274 adjust_window_ends (w, last_reused_text_row, true);
17275 else if (last_text_row)
17276 adjust_window_ends (w, last_text_row, false);
17277 else
17278 {
17279 /* This window must be completely empty. */
17280 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17281 w->window_end_pos = Z - ZV;
17282 w->window_end_vpos = 0;
17283 }
17284 w->window_end_valid = false;
17285
17286 /* Update hint: don't try scrolling again in update_window. */
17287 w->desired_matrix->no_scrolling_p = true;
17288
17289 #ifdef GLYPH_DEBUG
17290 debug_method_add (w, "try_window_reusing_current_matrix 1");
17291 #endif
17292 return true;
17293 }
17294 else if (CHARPOS (new_start) > CHARPOS (start))
17295 {
17296 struct glyph_row *pt_row, *row;
17297 struct glyph_row *first_reusable_row;
17298 struct glyph_row *first_row_to_display;
17299 int dy;
17300 int yb = window_text_bottom_y (w);
17301
17302 /* Find the row starting at new_start, if there is one. Don't
17303 reuse a partially visible line at the end. */
17304 first_reusable_row = start_row;
17305 while (first_reusable_row->enabled_p
17306 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17307 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17308 < CHARPOS (new_start)))
17309 ++first_reusable_row;
17310
17311 /* Give up if there is no row to reuse. */
17312 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17313 || !first_reusable_row->enabled_p
17314 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17315 != CHARPOS (new_start)))
17316 return false;
17317
17318 /* We can reuse fully visible rows beginning with
17319 first_reusable_row to the end of the window. Set
17320 first_row_to_display to the first row that cannot be reused.
17321 Set pt_row to the row containing point, if there is any. */
17322 pt_row = NULL;
17323 for (first_row_to_display = first_reusable_row;
17324 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17325 ++first_row_to_display)
17326 {
17327 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17328 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17329 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17330 && first_row_to_display->ends_at_zv_p
17331 && pt_row == NULL)))
17332 pt_row = first_row_to_display;
17333 }
17334
17335 /* Start displaying at the start of first_row_to_display. */
17336 eassert (first_row_to_display->y < yb);
17337 init_to_row_start (&it, w, first_row_to_display);
17338
17339 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17340 - start_vpos);
17341 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17342 - nrows_scrolled);
17343 it.current_y = (first_row_to_display->y - first_reusable_row->y
17344 + WINDOW_HEADER_LINE_HEIGHT (w));
17345
17346 /* Display lines beginning with first_row_to_display in the
17347 desired matrix. Set last_text_row to the last row displayed
17348 that displays text. */
17349 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17350 if (pt_row == NULL)
17351 w->cursor.vpos = -1;
17352 last_text_row = NULL;
17353 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17354 if (display_line (&it))
17355 last_text_row = it.glyph_row - 1;
17356
17357 /* If point is in a reused row, adjust y and vpos of the cursor
17358 position. */
17359 if (pt_row)
17360 {
17361 w->cursor.vpos -= nrows_scrolled;
17362 w->cursor.y -= first_reusable_row->y - start_row->y;
17363 }
17364
17365 /* Give up if point isn't in a row displayed or reused. (This
17366 also handles the case where w->cursor.vpos < nrows_scrolled
17367 after the calls to display_line, which can happen with scroll
17368 margins. See bug#1295.) */
17369 if (w->cursor.vpos < 0)
17370 {
17371 clear_glyph_matrix (w->desired_matrix);
17372 return false;
17373 }
17374
17375 /* Scroll the display. */
17376 run.current_y = first_reusable_row->y;
17377 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17378 run.height = it.last_visible_y - run.current_y;
17379 dy = run.current_y - run.desired_y;
17380
17381 if (run.height)
17382 {
17383 update_begin (f);
17384 FRAME_RIF (f)->update_window_begin_hook (w);
17385 FRAME_RIF (f)->clear_window_mouse_face (w);
17386 FRAME_RIF (f)->scroll_run_hook (w, &run);
17387 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17388 update_end (f);
17389 }
17390
17391 /* Adjust Y positions of reused rows. */
17392 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17393 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17394 max_y = it.last_visible_y;
17395 for (row = first_reusable_row; row < first_row_to_display; ++row)
17396 {
17397 row->y -= dy;
17398 row->visible_height = row->height;
17399 if (row->y < min_y)
17400 row->visible_height -= min_y - row->y;
17401 if (row->y + row->height > max_y)
17402 row->visible_height -= row->y + row->height - max_y;
17403 if (row->fringe_bitmap_periodic_p)
17404 row->redraw_fringe_bitmaps_p = true;
17405 }
17406
17407 /* Scroll the current matrix. */
17408 eassert (nrows_scrolled > 0);
17409 rotate_matrix (w->current_matrix,
17410 start_vpos,
17411 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17412 -nrows_scrolled);
17413
17414 /* Disable rows not reused. */
17415 for (row -= nrows_scrolled; row < bottom_row; ++row)
17416 row->enabled_p = false;
17417
17418 /* Point may have moved to a different line, so we cannot assume that
17419 the previous cursor position is valid; locate the correct row. */
17420 if (pt_row)
17421 {
17422 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17423 row < bottom_row
17424 && PT >= MATRIX_ROW_END_CHARPOS (row)
17425 && !row->ends_at_zv_p;
17426 row++)
17427 {
17428 w->cursor.vpos++;
17429 w->cursor.y = row->y;
17430 }
17431 if (row < bottom_row)
17432 {
17433 /* Can't simply scan the row for point with
17434 bidi-reordered glyph rows. Let set_cursor_from_row
17435 figure out where to put the cursor, and if it fails,
17436 give up. */
17437 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17438 {
17439 if (!set_cursor_from_row (w, row, w->current_matrix,
17440 0, 0, 0, 0))
17441 {
17442 clear_glyph_matrix (w->desired_matrix);
17443 return false;
17444 }
17445 }
17446 else
17447 {
17448 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17449 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17450
17451 for (; glyph < end
17452 && (!BUFFERP (glyph->object)
17453 || glyph->charpos < PT);
17454 glyph++)
17455 {
17456 w->cursor.hpos++;
17457 w->cursor.x += glyph->pixel_width;
17458 }
17459 }
17460 }
17461 }
17462
17463 /* Adjust window end. A null value of last_text_row means that
17464 the window end is in reused rows which in turn means that
17465 only its vpos can have changed. */
17466 if (last_text_row)
17467 adjust_window_ends (w, last_text_row, false);
17468 else
17469 w->window_end_vpos -= nrows_scrolled;
17470
17471 w->window_end_valid = false;
17472 w->desired_matrix->no_scrolling_p = true;
17473
17474 #ifdef GLYPH_DEBUG
17475 debug_method_add (w, "try_window_reusing_current_matrix 2");
17476 #endif
17477 return true;
17478 }
17479
17480 return false;
17481 }
17482
17483
17484 \f
17485 /************************************************************************
17486 Window redisplay reusing current matrix when buffer has changed
17487 ************************************************************************/
17488
17489 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17490 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17491 ptrdiff_t *, ptrdiff_t *);
17492 static struct glyph_row *
17493 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17494 struct glyph_row *);
17495
17496
17497 /* Return the last row in MATRIX displaying text. If row START is
17498 non-null, start searching with that row. IT gives the dimensions
17499 of the display. Value is null if matrix is empty; otherwise it is
17500 a pointer to the row found. */
17501
17502 static struct glyph_row *
17503 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17504 struct glyph_row *start)
17505 {
17506 struct glyph_row *row, *row_found;
17507
17508 /* Set row_found to the last row in IT->w's current matrix
17509 displaying text. The loop looks funny but think of partially
17510 visible lines. */
17511 row_found = NULL;
17512 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17513 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17514 {
17515 eassert (row->enabled_p);
17516 row_found = row;
17517 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17518 break;
17519 ++row;
17520 }
17521
17522 return row_found;
17523 }
17524
17525
17526 /* Return the last row in the current matrix of W that is not affected
17527 by changes at the start of current_buffer that occurred since W's
17528 current matrix was built. Value is null if no such row exists.
17529
17530 BEG_UNCHANGED us the number of characters unchanged at the start of
17531 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17532 first changed character in current_buffer. Characters at positions <
17533 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17534 when the current matrix was built. */
17535
17536 static struct glyph_row *
17537 find_last_unchanged_at_beg_row (struct window *w)
17538 {
17539 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17540 struct glyph_row *row;
17541 struct glyph_row *row_found = NULL;
17542 int yb = window_text_bottom_y (w);
17543
17544 /* Find the last row displaying unchanged text. */
17545 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17546 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17547 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17548 ++row)
17549 {
17550 if (/* If row ends before first_changed_pos, it is unchanged,
17551 except in some case. */
17552 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17553 /* When row ends in ZV and we write at ZV it is not
17554 unchanged. */
17555 && !row->ends_at_zv_p
17556 /* When first_changed_pos is the end of a continued line,
17557 row is not unchanged because it may be no longer
17558 continued. */
17559 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17560 && (row->continued_p
17561 || row->exact_window_width_line_p))
17562 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17563 needs to be recomputed, so don't consider this row as
17564 unchanged. This happens when the last line was
17565 bidi-reordered and was killed immediately before this
17566 redisplay cycle. In that case, ROW->end stores the
17567 buffer position of the first visual-order character of
17568 the killed text, which is now beyond ZV. */
17569 && CHARPOS (row->end.pos) <= ZV)
17570 row_found = row;
17571
17572 /* Stop if last visible row. */
17573 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17574 break;
17575 }
17576
17577 return row_found;
17578 }
17579
17580
17581 /* Find the first glyph row in the current matrix of W that is not
17582 affected by changes at the end of current_buffer since the
17583 time W's current matrix was built.
17584
17585 Return in *DELTA the number of chars by which buffer positions in
17586 unchanged text at the end of current_buffer must be adjusted.
17587
17588 Return in *DELTA_BYTES the corresponding number of bytes.
17589
17590 Value is null if no such row exists, i.e. all rows are affected by
17591 changes. */
17592
17593 static struct glyph_row *
17594 find_first_unchanged_at_end_row (struct window *w,
17595 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17596 {
17597 struct glyph_row *row;
17598 struct glyph_row *row_found = NULL;
17599
17600 *delta = *delta_bytes = 0;
17601
17602 /* Display must not have been paused, otherwise the current matrix
17603 is not up to date. */
17604 eassert (w->window_end_valid);
17605
17606 /* A value of window_end_pos >= END_UNCHANGED means that the window
17607 end is in the range of changed text. If so, there is no
17608 unchanged row at the end of W's current matrix. */
17609 if (w->window_end_pos >= END_UNCHANGED)
17610 return NULL;
17611
17612 /* Set row to the last row in W's current matrix displaying text. */
17613 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17614
17615 /* If matrix is entirely empty, no unchanged row exists. */
17616 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17617 {
17618 /* The value of row is the last glyph row in the matrix having a
17619 meaningful buffer position in it. The end position of row
17620 corresponds to window_end_pos. This allows us to translate
17621 buffer positions in the current matrix to current buffer
17622 positions for characters not in changed text. */
17623 ptrdiff_t Z_old =
17624 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17625 ptrdiff_t Z_BYTE_old =
17626 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17627 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17628 struct glyph_row *first_text_row
17629 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17630
17631 *delta = Z - Z_old;
17632 *delta_bytes = Z_BYTE - Z_BYTE_old;
17633
17634 /* Set last_unchanged_pos to the buffer position of the last
17635 character in the buffer that has not been changed. Z is the
17636 index + 1 of the last character in current_buffer, i.e. by
17637 subtracting END_UNCHANGED we get the index of the last
17638 unchanged character, and we have to add BEG to get its buffer
17639 position. */
17640 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17641 last_unchanged_pos_old = last_unchanged_pos - *delta;
17642
17643 /* Search backward from ROW for a row displaying a line that
17644 starts at a minimum position >= last_unchanged_pos_old. */
17645 for (; row > first_text_row; --row)
17646 {
17647 /* This used to abort, but it can happen.
17648 It is ok to just stop the search instead here. KFS. */
17649 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17650 break;
17651
17652 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17653 row_found = row;
17654 }
17655 }
17656
17657 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17658
17659 return row_found;
17660 }
17661
17662
17663 /* Make sure that glyph rows in the current matrix of window W
17664 reference the same glyph memory as corresponding rows in the
17665 frame's frame matrix. This function is called after scrolling W's
17666 current matrix on a terminal frame in try_window_id and
17667 try_window_reusing_current_matrix. */
17668
17669 static void
17670 sync_frame_with_window_matrix_rows (struct window *w)
17671 {
17672 struct frame *f = XFRAME (w->frame);
17673 struct glyph_row *window_row, *window_row_end, *frame_row;
17674
17675 /* Preconditions: W must be a leaf window and full-width. Its frame
17676 must have a frame matrix. */
17677 eassert (BUFFERP (w->contents));
17678 eassert (WINDOW_FULL_WIDTH_P (w));
17679 eassert (!FRAME_WINDOW_P (f));
17680
17681 /* If W is a full-width window, glyph pointers in W's current matrix
17682 have, by definition, to be the same as glyph pointers in the
17683 corresponding frame matrix. Note that frame matrices have no
17684 marginal areas (see build_frame_matrix). */
17685 window_row = w->current_matrix->rows;
17686 window_row_end = window_row + w->current_matrix->nrows;
17687 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17688 while (window_row < window_row_end)
17689 {
17690 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17691 struct glyph *end = window_row->glyphs[LAST_AREA];
17692
17693 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17694 frame_row->glyphs[TEXT_AREA] = start;
17695 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17696 frame_row->glyphs[LAST_AREA] = end;
17697
17698 /* Disable frame rows whose corresponding window rows have
17699 been disabled in try_window_id. */
17700 if (!window_row->enabled_p)
17701 frame_row->enabled_p = false;
17702
17703 ++window_row, ++frame_row;
17704 }
17705 }
17706
17707
17708 /* Find the glyph row in window W containing CHARPOS. Consider all
17709 rows between START and END (not inclusive). END null means search
17710 all rows to the end of the display area of W. Value is the row
17711 containing CHARPOS or null. */
17712
17713 struct glyph_row *
17714 row_containing_pos (struct window *w, ptrdiff_t charpos,
17715 struct glyph_row *start, struct glyph_row *end, int dy)
17716 {
17717 struct glyph_row *row = start;
17718 struct glyph_row *best_row = NULL;
17719 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17720 int last_y;
17721
17722 /* If we happen to start on a header-line, skip that. */
17723 if (row->mode_line_p)
17724 ++row;
17725
17726 if ((end && row >= end) || !row->enabled_p)
17727 return NULL;
17728
17729 last_y = window_text_bottom_y (w) - dy;
17730
17731 while (true)
17732 {
17733 /* Give up if we have gone too far. */
17734 if (end && row >= end)
17735 return NULL;
17736 /* This formerly returned if they were equal.
17737 I think that both quantities are of a "last plus one" type;
17738 if so, when they are equal, the row is within the screen. -- rms. */
17739 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17740 return NULL;
17741
17742 /* If it is in this row, return this row. */
17743 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17744 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17745 /* The end position of a row equals the start
17746 position of the next row. If CHARPOS is there, we
17747 would rather consider it displayed in the next
17748 line, except when this line ends in ZV. */
17749 && !row_for_charpos_p (row, charpos)))
17750 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17751 {
17752 struct glyph *g;
17753
17754 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17755 || (!best_row && !row->continued_p))
17756 return row;
17757 /* In bidi-reordered rows, there could be several rows whose
17758 edges surround CHARPOS, all of these rows belonging to
17759 the same continued line. We need to find the row which
17760 fits CHARPOS the best. */
17761 for (g = row->glyphs[TEXT_AREA];
17762 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17763 g++)
17764 {
17765 if (!STRINGP (g->object))
17766 {
17767 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17768 {
17769 mindif = eabs (g->charpos - charpos);
17770 best_row = row;
17771 /* Exact match always wins. */
17772 if (mindif == 0)
17773 return best_row;
17774 }
17775 }
17776 }
17777 }
17778 else if (best_row && !row->continued_p)
17779 return best_row;
17780 ++row;
17781 }
17782 }
17783
17784
17785 /* Try to redisplay window W by reusing its existing display. W's
17786 current matrix must be up to date when this function is called,
17787 i.e., window_end_valid must be true.
17788
17789 Value is
17790
17791 >= 1 if successful, i.e. display has been updated
17792 specifically:
17793 1 means the changes were in front of a newline that precedes
17794 the window start, and the whole current matrix was reused
17795 2 means the changes were after the last position displayed
17796 in the window, and the whole current matrix was reused
17797 3 means portions of the current matrix were reused, while
17798 some of the screen lines were redrawn
17799 -1 if redisplay with same window start is known not to succeed
17800 0 if otherwise unsuccessful
17801
17802 The following steps are performed:
17803
17804 1. Find the last row in the current matrix of W that is not
17805 affected by changes at the start of current_buffer. If no such row
17806 is found, give up.
17807
17808 2. Find the first row in W's current matrix that is not affected by
17809 changes at the end of current_buffer. Maybe there is no such row.
17810
17811 3. Display lines beginning with the row + 1 found in step 1 to the
17812 row found in step 2 or, if step 2 didn't find a row, to the end of
17813 the window.
17814
17815 4. If cursor is not known to appear on the window, give up.
17816
17817 5. If display stopped at the row found in step 2, scroll the
17818 display and current matrix as needed.
17819
17820 6. Maybe display some lines at the end of W, if we must. This can
17821 happen under various circumstances, like a partially visible line
17822 becoming fully visible, or because newly displayed lines are displayed
17823 in smaller font sizes.
17824
17825 7. Update W's window end information. */
17826
17827 static int
17828 try_window_id (struct window *w)
17829 {
17830 struct frame *f = XFRAME (w->frame);
17831 struct glyph_matrix *current_matrix = w->current_matrix;
17832 struct glyph_matrix *desired_matrix = w->desired_matrix;
17833 struct glyph_row *last_unchanged_at_beg_row;
17834 struct glyph_row *first_unchanged_at_end_row;
17835 struct glyph_row *row;
17836 struct glyph_row *bottom_row;
17837 int bottom_vpos;
17838 struct it it;
17839 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17840 int dvpos, dy;
17841 struct text_pos start_pos;
17842 struct run run;
17843 int first_unchanged_at_end_vpos = 0;
17844 struct glyph_row *last_text_row, *last_text_row_at_end;
17845 struct text_pos start;
17846 ptrdiff_t first_changed_charpos, last_changed_charpos;
17847
17848 #ifdef GLYPH_DEBUG
17849 if (inhibit_try_window_id)
17850 return 0;
17851 #endif
17852
17853 /* This is handy for debugging. */
17854 #if false
17855 #define GIVE_UP(X) \
17856 do { \
17857 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17858 return 0; \
17859 } while (false)
17860 #else
17861 #define GIVE_UP(X) return 0
17862 #endif
17863
17864 SET_TEXT_POS_FROM_MARKER (start, w->start);
17865
17866 /* Don't use this for mini-windows because these can show
17867 messages and mini-buffers, and we don't handle that here. */
17868 if (MINI_WINDOW_P (w))
17869 GIVE_UP (1);
17870
17871 /* This flag is used to prevent redisplay optimizations. */
17872 if (windows_or_buffers_changed || f->cursor_type_changed)
17873 GIVE_UP (2);
17874
17875 /* This function's optimizations cannot be used if overlays have
17876 changed in the buffer displayed by the window, so give up if they
17877 have. */
17878 if (w->last_overlay_modified != OVERLAY_MODIFF)
17879 GIVE_UP (200);
17880
17881 /* Verify that narrowing has not changed.
17882 Also verify that we were not told to prevent redisplay optimizations.
17883 It would be nice to further
17884 reduce the number of cases where this prevents try_window_id. */
17885 if (current_buffer->clip_changed
17886 || current_buffer->prevent_redisplay_optimizations_p)
17887 GIVE_UP (3);
17888
17889 /* Window must either use window-based redisplay or be full width. */
17890 if (!FRAME_WINDOW_P (f)
17891 && (!FRAME_LINE_INS_DEL_OK (f)
17892 || !WINDOW_FULL_WIDTH_P (w)))
17893 GIVE_UP (4);
17894
17895 /* Give up if point is known NOT to appear in W. */
17896 if (PT < CHARPOS (start))
17897 GIVE_UP (5);
17898
17899 /* Another way to prevent redisplay optimizations. */
17900 if (w->last_modified == 0)
17901 GIVE_UP (6);
17902
17903 /* Verify that window is not hscrolled. */
17904 if (w->hscroll != 0)
17905 GIVE_UP (7);
17906
17907 /* Verify that display wasn't paused. */
17908 if (!w->window_end_valid)
17909 GIVE_UP (8);
17910
17911 /* Likewise if highlighting trailing whitespace. */
17912 if (!NILP (Vshow_trailing_whitespace))
17913 GIVE_UP (11);
17914
17915 /* Can't use this if overlay arrow position and/or string have
17916 changed. */
17917 if (overlay_arrows_changed_p ())
17918 GIVE_UP (12);
17919
17920 /* When word-wrap is on, adding a space to the first word of a
17921 wrapped line can change the wrap position, altering the line
17922 above it. It might be worthwhile to handle this more
17923 intelligently, but for now just redisplay from scratch. */
17924 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17925 GIVE_UP (21);
17926
17927 /* Under bidi reordering, adding or deleting a character in the
17928 beginning of a paragraph, before the first strong directional
17929 character, can change the base direction of the paragraph (unless
17930 the buffer specifies a fixed paragraph direction), which will
17931 require to redisplay the whole paragraph. It might be worthwhile
17932 to find the paragraph limits and widen the range of redisplayed
17933 lines to that, but for now just give up this optimization and
17934 redisplay from scratch. */
17935 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17936 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17937 GIVE_UP (22);
17938
17939 /* Give up if the buffer has line-spacing set, as Lisp-level changes
17940 to that variable require thorough redisplay. */
17941 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
17942 GIVE_UP (23);
17943
17944 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17945 only if buffer has really changed. The reason is that the gap is
17946 initially at Z for freshly visited files. The code below would
17947 set end_unchanged to 0 in that case. */
17948 if (MODIFF > SAVE_MODIFF
17949 /* This seems to happen sometimes after saving a buffer. */
17950 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17951 {
17952 if (GPT - BEG < BEG_UNCHANGED)
17953 BEG_UNCHANGED = GPT - BEG;
17954 if (Z - GPT < END_UNCHANGED)
17955 END_UNCHANGED = Z - GPT;
17956 }
17957
17958 /* The position of the first and last character that has been changed. */
17959 first_changed_charpos = BEG + BEG_UNCHANGED;
17960 last_changed_charpos = Z - END_UNCHANGED;
17961
17962 /* If window starts after a line end, and the last change is in
17963 front of that newline, then changes don't affect the display.
17964 This case happens with stealth-fontification. Note that although
17965 the display is unchanged, glyph positions in the matrix have to
17966 be adjusted, of course. */
17967 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17968 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17969 && ((last_changed_charpos < CHARPOS (start)
17970 && CHARPOS (start) == BEGV)
17971 || (last_changed_charpos < CHARPOS (start) - 1
17972 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17973 {
17974 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17975 struct glyph_row *r0;
17976
17977 /* Compute how many chars/bytes have been added to or removed
17978 from the buffer. */
17979 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17980 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17981 Z_delta = Z - Z_old;
17982 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17983
17984 /* Give up if PT is not in the window. Note that it already has
17985 been checked at the start of try_window_id that PT is not in
17986 front of the window start. */
17987 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17988 GIVE_UP (13);
17989
17990 /* If window start is unchanged, we can reuse the whole matrix
17991 as is, after adjusting glyph positions. No need to compute
17992 the window end again, since its offset from Z hasn't changed. */
17993 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17994 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17995 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17996 /* PT must not be in a partially visible line. */
17997 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17998 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17999 {
18000 /* Adjust positions in the glyph matrix. */
18001 if (Z_delta || Z_delta_bytes)
18002 {
18003 struct glyph_row *r1
18004 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18005 increment_matrix_positions (w->current_matrix,
18006 MATRIX_ROW_VPOS (r0, current_matrix),
18007 MATRIX_ROW_VPOS (r1, current_matrix),
18008 Z_delta, Z_delta_bytes);
18009 }
18010
18011 /* Set the cursor. */
18012 row = row_containing_pos (w, PT, r0, NULL, 0);
18013 if (row)
18014 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18015 return 1;
18016 }
18017 }
18018
18019 /* Handle the case that changes are all below what is displayed in
18020 the window, and that PT is in the window. This shortcut cannot
18021 be taken if ZV is visible in the window, and text has been added
18022 there that is visible in the window. */
18023 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18024 /* ZV is not visible in the window, or there are no
18025 changes at ZV, actually. */
18026 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18027 || first_changed_charpos == last_changed_charpos))
18028 {
18029 struct glyph_row *r0;
18030
18031 /* Give up if PT is not in the window. Note that it already has
18032 been checked at the start of try_window_id that PT is not in
18033 front of the window start. */
18034 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18035 GIVE_UP (14);
18036
18037 /* If window start is unchanged, we can reuse the whole matrix
18038 as is, without changing glyph positions since no text has
18039 been added/removed in front of the window end. */
18040 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18041 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18042 /* PT must not be in a partially visible line. */
18043 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18044 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18045 {
18046 /* We have to compute the window end anew since text
18047 could have been added/removed after it. */
18048 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18049 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18050
18051 /* Set the cursor. */
18052 row = row_containing_pos (w, PT, r0, NULL, 0);
18053 if (row)
18054 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18055 return 2;
18056 }
18057 }
18058
18059 /* Give up if window start is in the changed area.
18060
18061 The condition used to read
18062
18063 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18064
18065 but why that was tested escapes me at the moment. */
18066 if (CHARPOS (start) >= first_changed_charpos
18067 && CHARPOS (start) <= last_changed_charpos)
18068 GIVE_UP (15);
18069
18070 /* Check that window start agrees with the start of the first glyph
18071 row in its current matrix. Check this after we know the window
18072 start is not in changed text, otherwise positions would not be
18073 comparable. */
18074 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18075 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18076 GIVE_UP (16);
18077
18078 /* Give up if the window ends in strings. Overlay strings
18079 at the end are difficult to handle, so don't try. */
18080 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18081 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18082 GIVE_UP (20);
18083
18084 /* Compute the position at which we have to start displaying new
18085 lines. Some of the lines at the top of the window might be
18086 reusable because they are not displaying changed text. Find the
18087 last row in W's current matrix not affected by changes at the
18088 start of current_buffer. Value is null if changes start in the
18089 first line of window. */
18090 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18091 if (last_unchanged_at_beg_row)
18092 {
18093 /* Avoid starting to display in the middle of a character, a TAB
18094 for instance. This is easier than to set up the iterator
18095 exactly, and it's not a frequent case, so the additional
18096 effort wouldn't really pay off. */
18097 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18098 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18099 && last_unchanged_at_beg_row > w->current_matrix->rows)
18100 --last_unchanged_at_beg_row;
18101
18102 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18103 GIVE_UP (17);
18104
18105 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18106 GIVE_UP (18);
18107 start_pos = it.current.pos;
18108
18109 /* Start displaying new lines in the desired matrix at the same
18110 vpos we would use in the current matrix, i.e. below
18111 last_unchanged_at_beg_row. */
18112 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18113 current_matrix);
18114 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18115 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18116
18117 eassert (it.hpos == 0 && it.current_x == 0);
18118 }
18119 else
18120 {
18121 /* There are no reusable lines at the start of the window.
18122 Start displaying in the first text line. */
18123 start_display (&it, w, start);
18124 it.vpos = it.first_vpos;
18125 start_pos = it.current.pos;
18126 }
18127
18128 /* Find the first row that is not affected by changes at the end of
18129 the buffer. Value will be null if there is no unchanged row, in
18130 which case we must redisplay to the end of the window. delta
18131 will be set to the value by which buffer positions beginning with
18132 first_unchanged_at_end_row have to be adjusted due to text
18133 changes. */
18134 first_unchanged_at_end_row
18135 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18136 IF_DEBUG (debug_delta = delta);
18137 IF_DEBUG (debug_delta_bytes = delta_bytes);
18138
18139 /* Set stop_pos to the buffer position up to which we will have to
18140 display new lines. If first_unchanged_at_end_row != NULL, this
18141 is the buffer position of the start of the line displayed in that
18142 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18143 that we don't stop at a buffer position. */
18144 stop_pos = 0;
18145 if (first_unchanged_at_end_row)
18146 {
18147 eassert (last_unchanged_at_beg_row == NULL
18148 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18149
18150 /* If this is a continuation line, move forward to the next one
18151 that isn't. Changes in lines above affect this line.
18152 Caution: this may move first_unchanged_at_end_row to a row
18153 not displaying text. */
18154 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18155 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18156 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18157 < it.last_visible_y))
18158 ++first_unchanged_at_end_row;
18159
18160 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18161 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18162 >= it.last_visible_y))
18163 first_unchanged_at_end_row = NULL;
18164 else
18165 {
18166 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18167 + delta);
18168 first_unchanged_at_end_vpos
18169 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18170 eassert (stop_pos >= Z - END_UNCHANGED);
18171 }
18172 }
18173 else if (last_unchanged_at_beg_row == NULL)
18174 GIVE_UP (19);
18175
18176
18177 #ifdef GLYPH_DEBUG
18178
18179 /* Either there is no unchanged row at the end, or the one we have
18180 now displays text. This is a necessary condition for the window
18181 end pos calculation at the end of this function. */
18182 eassert (first_unchanged_at_end_row == NULL
18183 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18184
18185 debug_last_unchanged_at_beg_vpos
18186 = (last_unchanged_at_beg_row
18187 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18188 : -1);
18189 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18190
18191 #endif /* GLYPH_DEBUG */
18192
18193
18194 /* Display new lines. Set last_text_row to the last new line
18195 displayed which has text on it, i.e. might end up as being the
18196 line where the window_end_vpos is. */
18197 w->cursor.vpos = -1;
18198 last_text_row = NULL;
18199 overlay_arrow_seen = false;
18200 if (it.current_y < it.last_visible_y
18201 && !f->fonts_changed
18202 && (first_unchanged_at_end_row == NULL
18203 || IT_CHARPOS (it) < stop_pos))
18204 it.glyph_row->reversed_p = false;
18205 while (it.current_y < it.last_visible_y
18206 && !f->fonts_changed
18207 && (first_unchanged_at_end_row == NULL
18208 || IT_CHARPOS (it) < stop_pos))
18209 {
18210 if (display_line (&it))
18211 last_text_row = it.glyph_row - 1;
18212 }
18213
18214 if (f->fonts_changed)
18215 return -1;
18216
18217 /* The redisplay iterations in display_line above could have
18218 triggered font-lock, which could have done something that
18219 invalidates IT->w window's end-point information, on which we
18220 rely below. E.g., one package, which will remain unnamed, used
18221 to install a font-lock-fontify-region-function that called
18222 bury-buffer, whose side effect is to switch the buffer displayed
18223 by IT->w, and that predictably resets IT->w's window_end_valid
18224 flag, which we already tested at the entry to this function.
18225 Amply punish such packages/modes by giving up on this
18226 optimization in those cases. */
18227 if (!w->window_end_valid)
18228 {
18229 clear_glyph_matrix (w->desired_matrix);
18230 return -1;
18231 }
18232
18233 /* Compute differences in buffer positions, y-positions etc. for
18234 lines reused at the bottom of the window. Compute what we can
18235 scroll. */
18236 if (first_unchanged_at_end_row
18237 /* No lines reused because we displayed everything up to the
18238 bottom of the window. */
18239 && it.current_y < it.last_visible_y)
18240 {
18241 dvpos = (it.vpos
18242 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18243 current_matrix));
18244 dy = it.current_y - first_unchanged_at_end_row->y;
18245 run.current_y = first_unchanged_at_end_row->y;
18246 run.desired_y = run.current_y + dy;
18247 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18248 }
18249 else
18250 {
18251 delta = delta_bytes = dvpos = dy
18252 = run.current_y = run.desired_y = run.height = 0;
18253 first_unchanged_at_end_row = NULL;
18254 }
18255 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18256
18257
18258 /* Find the cursor if not already found. We have to decide whether
18259 PT will appear on this window (it sometimes doesn't, but this is
18260 not a very frequent case.) This decision has to be made before
18261 the current matrix is altered. A value of cursor.vpos < 0 means
18262 that PT is either in one of the lines beginning at
18263 first_unchanged_at_end_row or below the window. Don't care for
18264 lines that might be displayed later at the window end; as
18265 mentioned, this is not a frequent case. */
18266 if (w->cursor.vpos < 0)
18267 {
18268 /* Cursor in unchanged rows at the top? */
18269 if (PT < CHARPOS (start_pos)
18270 && last_unchanged_at_beg_row)
18271 {
18272 row = row_containing_pos (w, PT,
18273 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18274 last_unchanged_at_beg_row + 1, 0);
18275 if (row)
18276 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18277 }
18278
18279 /* Start from first_unchanged_at_end_row looking for PT. */
18280 else if (first_unchanged_at_end_row)
18281 {
18282 row = row_containing_pos (w, PT - delta,
18283 first_unchanged_at_end_row, NULL, 0);
18284 if (row)
18285 set_cursor_from_row (w, row, w->current_matrix, delta,
18286 delta_bytes, dy, dvpos);
18287 }
18288
18289 /* Give up if cursor was not found. */
18290 if (w->cursor.vpos < 0)
18291 {
18292 clear_glyph_matrix (w->desired_matrix);
18293 return -1;
18294 }
18295 }
18296
18297 /* Don't let the cursor end in the scroll margins. */
18298 {
18299 int this_scroll_margin, cursor_height;
18300 int frame_line_height = default_line_pixel_height (w);
18301 int window_total_lines
18302 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18303
18304 this_scroll_margin =
18305 max (0, min (scroll_margin, window_total_lines / 4));
18306 this_scroll_margin *= frame_line_height;
18307 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18308
18309 if ((w->cursor.y < this_scroll_margin
18310 && CHARPOS (start) > BEGV)
18311 /* Old redisplay didn't take scroll margin into account at the bottom,
18312 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18313 || (w->cursor.y + (make_cursor_line_fully_visible_p
18314 ? cursor_height + this_scroll_margin
18315 : 1)) > it.last_visible_y)
18316 {
18317 w->cursor.vpos = -1;
18318 clear_glyph_matrix (w->desired_matrix);
18319 return -1;
18320 }
18321 }
18322
18323 /* Scroll the display. Do it before changing the current matrix so
18324 that xterm.c doesn't get confused about where the cursor glyph is
18325 found. */
18326 if (dy && run.height)
18327 {
18328 update_begin (f);
18329
18330 if (FRAME_WINDOW_P (f))
18331 {
18332 FRAME_RIF (f)->update_window_begin_hook (w);
18333 FRAME_RIF (f)->clear_window_mouse_face (w);
18334 FRAME_RIF (f)->scroll_run_hook (w, &run);
18335 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18336 }
18337 else
18338 {
18339 /* Terminal frame. In this case, dvpos gives the number of
18340 lines to scroll by; dvpos < 0 means scroll up. */
18341 int from_vpos
18342 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18343 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18344 int end = (WINDOW_TOP_EDGE_LINE (w)
18345 + WINDOW_WANTS_HEADER_LINE_P (w)
18346 + window_internal_height (w));
18347
18348 #if defined (HAVE_GPM) || defined (MSDOS)
18349 x_clear_window_mouse_face (w);
18350 #endif
18351 /* Perform the operation on the screen. */
18352 if (dvpos > 0)
18353 {
18354 /* Scroll last_unchanged_at_beg_row to the end of the
18355 window down dvpos lines. */
18356 set_terminal_window (f, end);
18357
18358 /* On dumb terminals delete dvpos lines at the end
18359 before inserting dvpos empty lines. */
18360 if (!FRAME_SCROLL_REGION_OK (f))
18361 ins_del_lines (f, end - dvpos, -dvpos);
18362
18363 /* Insert dvpos empty lines in front of
18364 last_unchanged_at_beg_row. */
18365 ins_del_lines (f, from, dvpos);
18366 }
18367 else if (dvpos < 0)
18368 {
18369 /* Scroll up last_unchanged_at_beg_vpos to the end of
18370 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18371 set_terminal_window (f, end);
18372
18373 /* Delete dvpos lines in front of
18374 last_unchanged_at_beg_vpos. ins_del_lines will set
18375 the cursor to the given vpos and emit |dvpos| delete
18376 line sequences. */
18377 ins_del_lines (f, from + dvpos, dvpos);
18378
18379 /* On a dumb terminal insert dvpos empty lines at the
18380 end. */
18381 if (!FRAME_SCROLL_REGION_OK (f))
18382 ins_del_lines (f, end + dvpos, -dvpos);
18383 }
18384
18385 set_terminal_window (f, 0);
18386 }
18387
18388 update_end (f);
18389 }
18390
18391 /* Shift reused rows of the current matrix to the right position.
18392 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18393 text. */
18394 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18395 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18396 if (dvpos < 0)
18397 {
18398 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18399 bottom_vpos, dvpos);
18400 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18401 bottom_vpos);
18402 }
18403 else if (dvpos > 0)
18404 {
18405 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18406 bottom_vpos, dvpos);
18407 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18408 first_unchanged_at_end_vpos + dvpos);
18409 }
18410
18411 /* For frame-based redisplay, make sure that current frame and window
18412 matrix are in sync with respect to glyph memory. */
18413 if (!FRAME_WINDOW_P (f))
18414 sync_frame_with_window_matrix_rows (w);
18415
18416 /* Adjust buffer positions in reused rows. */
18417 if (delta || delta_bytes)
18418 increment_matrix_positions (current_matrix,
18419 first_unchanged_at_end_vpos + dvpos,
18420 bottom_vpos, delta, delta_bytes);
18421
18422 /* Adjust Y positions. */
18423 if (dy)
18424 shift_glyph_matrix (w, current_matrix,
18425 first_unchanged_at_end_vpos + dvpos,
18426 bottom_vpos, dy);
18427
18428 if (first_unchanged_at_end_row)
18429 {
18430 first_unchanged_at_end_row += dvpos;
18431 if (first_unchanged_at_end_row->y >= it.last_visible_y
18432 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18433 first_unchanged_at_end_row = NULL;
18434 }
18435
18436 /* If scrolling up, there may be some lines to display at the end of
18437 the window. */
18438 last_text_row_at_end = NULL;
18439 if (dy < 0)
18440 {
18441 /* Scrolling up can leave for example a partially visible line
18442 at the end of the window to be redisplayed. */
18443 /* Set last_row to the glyph row in the current matrix where the
18444 window end line is found. It has been moved up or down in
18445 the matrix by dvpos. */
18446 int last_vpos = w->window_end_vpos + dvpos;
18447 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18448
18449 /* If last_row is the window end line, it should display text. */
18450 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18451
18452 /* If window end line was partially visible before, begin
18453 displaying at that line. Otherwise begin displaying with the
18454 line following it. */
18455 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18456 {
18457 init_to_row_start (&it, w, last_row);
18458 it.vpos = last_vpos;
18459 it.current_y = last_row->y;
18460 }
18461 else
18462 {
18463 init_to_row_end (&it, w, last_row);
18464 it.vpos = 1 + last_vpos;
18465 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18466 ++last_row;
18467 }
18468
18469 /* We may start in a continuation line. If so, we have to
18470 get the right continuation_lines_width and current_x. */
18471 it.continuation_lines_width = last_row->continuation_lines_width;
18472 it.hpos = it.current_x = 0;
18473
18474 /* Display the rest of the lines at the window end. */
18475 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18476 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18477 {
18478 /* Is it always sure that the display agrees with lines in
18479 the current matrix? I don't think so, so we mark rows
18480 displayed invalid in the current matrix by setting their
18481 enabled_p flag to false. */
18482 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18483 if (display_line (&it))
18484 last_text_row_at_end = it.glyph_row - 1;
18485 }
18486 }
18487
18488 /* Update window_end_pos and window_end_vpos. */
18489 if (first_unchanged_at_end_row && !last_text_row_at_end)
18490 {
18491 /* Window end line if one of the preserved rows from the current
18492 matrix. Set row to the last row displaying text in current
18493 matrix starting at first_unchanged_at_end_row, after
18494 scrolling. */
18495 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18496 row = find_last_row_displaying_text (w->current_matrix, &it,
18497 first_unchanged_at_end_row);
18498 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18499 adjust_window_ends (w, row, true);
18500 eassert (w->window_end_bytepos >= 0);
18501 IF_DEBUG (debug_method_add (w, "A"));
18502 }
18503 else if (last_text_row_at_end)
18504 {
18505 adjust_window_ends (w, last_text_row_at_end, false);
18506 eassert (w->window_end_bytepos >= 0);
18507 IF_DEBUG (debug_method_add (w, "B"));
18508 }
18509 else if (last_text_row)
18510 {
18511 /* We have displayed either to the end of the window or at the
18512 end of the window, i.e. the last row with text is to be found
18513 in the desired matrix. */
18514 adjust_window_ends (w, last_text_row, false);
18515 eassert (w->window_end_bytepos >= 0);
18516 }
18517 else if (first_unchanged_at_end_row == NULL
18518 && last_text_row == NULL
18519 && last_text_row_at_end == NULL)
18520 {
18521 /* Displayed to end of window, but no line containing text was
18522 displayed. Lines were deleted at the end of the window. */
18523 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18524 int vpos = w->window_end_vpos;
18525 struct glyph_row *current_row = current_matrix->rows + vpos;
18526 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18527
18528 for (row = NULL;
18529 row == NULL && vpos >= first_vpos;
18530 --vpos, --current_row, --desired_row)
18531 {
18532 if (desired_row->enabled_p)
18533 {
18534 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18535 row = desired_row;
18536 }
18537 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18538 row = current_row;
18539 }
18540
18541 eassert (row != NULL);
18542 w->window_end_vpos = vpos + 1;
18543 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18544 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18545 eassert (w->window_end_bytepos >= 0);
18546 IF_DEBUG (debug_method_add (w, "C"));
18547 }
18548 else
18549 emacs_abort ();
18550
18551 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18552 debug_end_vpos = w->window_end_vpos));
18553
18554 /* Record that display has not been completed. */
18555 w->window_end_valid = false;
18556 w->desired_matrix->no_scrolling_p = true;
18557 return 3;
18558
18559 #undef GIVE_UP
18560 }
18561
18562
18563 \f
18564 /***********************************************************************
18565 More debugging support
18566 ***********************************************************************/
18567
18568 #ifdef GLYPH_DEBUG
18569
18570 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18571 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18572 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18573
18574
18575 /* Dump the contents of glyph matrix MATRIX on stderr.
18576
18577 GLYPHS 0 means don't show glyph contents.
18578 GLYPHS 1 means show glyphs in short form
18579 GLYPHS > 1 means show glyphs in long form. */
18580
18581 void
18582 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18583 {
18584 int i;
18585 for (i = 0; i < matrix->nrows; ++i)
18586 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18587 }
18588
18589
18590 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18591 the glyph row and area where the glyph comes from. */
18592
18593 void
18594 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18595 {
18596 if (glyph->type == CHAR_GLYPH
18597 || glyph->type == GLYPHLESS_GLYPH)
18598 {
18599 fprintf (stderr,
18600 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18601 glyph - row->glyphs[TEXT_AREA],
18602 (glyph->type == CHAR_GLYPH
18603 ? 'C'
18604 : 'G'),
18605 glyph->charpos,
18606 (BUFFERP (glyph->object)
18607 ? 'B'
18608 : (STRINGP (glyph->object)
18609 ? 'S'
18610 : (NILP (glyph->object)
18611 ? '0'
18612 : '-'))),
18613 glyph->pixel_width,
18614 glyph->u.ch,
18615 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18616 ? glyph->u.ch
18617 : '.'),
18618 glyph->face_id,
18619 glyph->left_box_line_p,
18620 glyph->right_box_line_p);
18621 }
18622 else if (glyph->type == STRETCH_GLYPH)
18623 {
18624 fprintf (stderr,
18625 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18626 glyph - row->glyphs[TEXT_AREA],
18627 'S',
18628 glyph->charpos,
18629 (BUFFERP (glyph->object)
18630 ? 'B'
18631 : (STRINGP (glyph->object)
18632 ? 'S'
18633 : (NILP (glyph->object)
18634 ? '0'
18635 : '-'))),
18636 glyph->pixel_width,
18637 0,
18638 ' ',
18639 glyph->face_id,
18640 glyph->left_box_line_p,
18641 glyph->right_box_line_p);
18642 }
18643 else if (glyph->type == IMAGE_GLYPH)
18644 {
18645 fprintf (stderr,
18646 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18647 glyph - row->glyphs[TEXT_AREA],
18648 'I',
18649 glyph->charpos,
18650 (BUFFERP (glyph->object)
18651 ? 'B'
18652 : (STRINGP (glyph->object)
18653 ? 'S'
18654 : (NILP (glyph->object)
18655 ? '0'
18656 : '-'))),
18657 glyph->pixel_width,
18658 glyph->u.img_id,
18659 '.',
18660 glyph->face_id,
18661 glyph->left_box_line_p,
18662 glyph->right_box_line_p);
18663 }
18664 else if (glyph->type == COMPOSITE_GLYPH)
18665 {
18666 fprintf (stderr,
18667 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18668 glyph - row->glyphs[TEXT_AREA],
18669 '+',
18670 glyph->charpos,
18671 (BUFFERP (glyph->object)
18672 ? 'B'
18673 : (STRINGP (glyph->object)
18674 ? 'S'
18675 : (NILP (glyph->object)
18676 ? '0'
18677 : '-'))),
18678 glyph->pixel_width,
18679 glyph->u.cmp.id);
18680 if (glyph->u.cmp.automatic)
18681 fprintf (stderr,
18682 "[%d-%d]",
18683 glyph->slice.cmp.from, glyph->slice.cmp.to);
18684 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18685 glyph->face_id,
18686 glyph->left_box_line_p,
18687 glyph->right_box_line_p);
18688 }
18689 }
18690
18691
18692 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18693 GLYPHS 0 means don't show glyph contents.
18694 GLYPHS 1 means show glyphs in short form
18695 GLYPHS > 1 means show glyphs in long form. */
18696
18697 void
18698 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18699 {
18700 if (glyphs != 1)
18701 {
18702 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18703 fprintf (stderr, "==============================================================================\n");
18704
18705 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18706 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18707 vpos,
18708 MATRIX_ROW_START_CHARPOS (row),
18709 MATRIX_ROW_END_CHARPOS (row),
18710 row->used[TEXT_AREA],
18711 row->contains_overlapping_glyphs_p,
18712 row->enabled_p,
18713 row->truncated_on_left_p,
18714 row->truncated_on_right_p,
18715 row->continued_p,
18716 MATRIX_ROW_CONTINUATION_LINE_P (row),
18717 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18718 row->ends_at_zv_p,
18719 row->fill_line_p,
18720 row->ends_in_middle_of_char_p,
18721 row->starts_in_middle_of_char_p,
18722 row->mouse_face_p,
18723 row->x,
18724 row->y,
18725 row->pixel_width,
18726 row->height,
18727 row->visible_height,
18728 row->ascent,
18729 row->phys_ascent);
18730 /* The next 3 lines should align to "Start" in the header. */
18731 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18732 row->end.overlay_string_index,
18733 row->continuation_lines_width);
18734 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18735 CHARPOS (row->start.string_pos),
18736 CHARPOS (row->end.string_pos));
18737 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18738 row->end.dpvec_index);
18739 }
18740
18741 if (glyphs > 1)
18742 {
18743 int area;
18744
18745 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18746 {
18747 struct glyph *glyph = row->glyphs[area];
18748 struct glyph *glyph_end = glyph + row->used[area];
18749
18750 /* Glyph for a line end in text. */
18751 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18752 ++glyph_end;
18753
18754 if (glyph < glyph_end)
18755 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18756
18757 for (; glyph < glyph_end; ++glyph)
18758 dump_glyph (row, glyph, area);
18759 }
18760 }
18761 else if (glyphs == 1)
18762 {
18763 int area;
18764 char s[SHRT_MAX + 4];
18765
18766 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18767 {
18768 int i;
18769
18770 for (i = 0; i < row->used[area]; ++i)
18771 {
18772 struct glyph *glyph = row->glyphs[area] + i;
18773 if (i == row->used[area] - 1
18774 && area == TEXT_AREA
18775 && NILP (glyph->object)
18776 && glyph->type == CHAR_GLYPH
18777 && glyph->u.ch == ' ')
18778 {
18779 strcpy (&s[i], "[\\n]");
18780 i += 4;
18781 }
18782 else if (glyph->type == CHAR_GLYPH
18783 && glyph->u.ch < 0x80
18784 && glyph->u.ch >= ' ')
18785 s[i] = glyph->u.ch;
18786 else
18787 s[i] = '.';
18788 }
18789
18790 s[i] = '\0';
18791 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18792 }
18793 }
18794 }
18795
18796
18797 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18798 Sdump_glyph_matrix, 0, 1, "p",
18799 doc: /* Dump the current matrix of the selected window to stderr.
18800 Shows contents of glyph row structures. With non-nil
18801 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18802 glyphs in short form, otherwise show glyphs in long form.
18803
18804 Interactively, no argument means show glyphs in short form;
18805 with numeric argument, its value is passed as the GLYPHS flag. */)
18806 (Lisp_Object glyphs)
18807 {
18808 struct window *w = XWINDOW (selected_window);
18809 struct buffer *buffer = XBUFFER (w->contents);
18810
18811 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18812 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18813 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18814 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18815 fprintf (stderr, "=============================================\n");
18816 dump_glyph_matrix (w->current_matrix,
18817 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18818 return Qnil;
18819 }
18820
18821
18822 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18823 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18824 Only text-mode frames have frame glyph matrices. */)
18825 (void)
18826 {
18827 struct frame *f = XFRAME (selected_frame);
18828
18829 if (f->current_matrix)
18830 dump_glyph_matrix (f->current_matrix, 1);
18831 else
18832 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18833 return Qnil;
18834 }
18835
18836
18837 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18838 doc: /* Dump glyph row ROW to stderr.
18839 GLYPH 0 means don't dump glyphs.
18840 GLYPH 1 means dump glyphs in short form.
18841 GLYPH > 1 or omitted means dump glyphs in long form. */)
18842 (Lisp_Object row, Lisp_Object glyphs)
18843 {
18844 struct glyph_matrix *matrix;
18845 EMACS_INT vpos;
18846
18847 CHECK_NUMBER (row);
18848 matrix = XWINDOW (selected_window)->current_matrix;
18849 vpos = XINT (row);
18850 if (vpos >= 0 && vpos < matrix->nrows)
18851 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18852 vpos,
18853 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18854 return Qnil;
18855 }
18856
18857
18858 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18859 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18860 GLYPH 0 means don't dump glyphs.
18861 GLYPH 1 means dump glyphs in short form.
18862 GLYPH > 1 or omitted means dump glyphs in long form.
18863
18864 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18865 do nothing. */)
18866 (Lisp_Object row, Lisp_Object glyphs)
18867 {
18868 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18869 struct frame *sf = SELECTED_FRAME ();
18870 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18871 EMACS_INT vpos;
18872
18873 CHECK_NUMBER (row);
18874 vpos = XINT (row);
18875 if (vpos >= 0 && vpos < m->nrows)
18876 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18877 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18878 #endif
18879 return Qnil;
18880 }
18881
18882
18883 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18884 doc: /* Toggle tracing of redisplay.
18885 With ARG, turn tracing on if and only if ARG is positive. */)
18886 (Lisp_Object arg)
18887 {
18888 if (NILP (arg))
18889 trace_redisplay_p = !trace_redisplay_p;
18890 else
18891 {
18892 arg = Fprefix_numeric_value (arg);
18893 trace_redisplay_p = XINT (arg) > 0;
18894 }
18895
18896 return Qnil;
18897 }
18898
18899
18900 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18901 doc: /* Like `format', but print result to stderr.
18902 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18903 (ptrdiff_t nargs, Lisp_Object *args)
18904 {
18905 Lisp_Object s = Fformat (nargs, args);
18906 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18907 return Qnil;
18908 }
18909
18910 #endif /* GLYPH_DEBUG */
18911
18912
18913 \f
18914 /***********************************************************************
18915 Building Desired Matrix Rows
18916 ***********************************************************************/
18917
18918 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18919 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18920
18921 static struct glyph_row *
18922 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18923 {
18924 struct frame *f = XFRAME (WINDOW_FRAME (w));
18925 struct buffer *buffer = XBUFFER (w->contents);
18926 struct buffer *old = current_buffer;
18927 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18928 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18929 const unsigned char *arrow_end = arrow_string + arrow_len;
18930 const unsigned char *p;
18931 struct it it;
18932 bool multibyte_p;
18933 int n_glyphs_before;
18934
18935 set_buffer_temp (buffer);
18936 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18937 scratch_glyph_row.reversed_p = false;
18938 it.glyph_row->used[TEXT_AREA] = 0;
18939 SET_TEXT_POS (it.position, 0, 0);
18940
18941 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18942 p = arrow_string;
18943 while (p < arrow_end)
18944 {
18945 Lisp_Object face, ilisp;
18946
18947 /* Get the next character. */
18948 if (multibyte_p)
18949 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18950 else
18951 {
18952 it.c = it.char_to_display = *p, it.len = 1;
18953 if (! ASCII_CHAR_P (it.c))
18954 it.char_to_display = BYTE8_TO_CHAR (it.c);
18955 }
18956 p += it.len;
18957
18958 /* Get its face. */
18959 ilisp = make_number (p - arrow_string);
18960 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18961 it.face_id = compute_char_face (f, it.char_to_display, face);
18962
18963 /* Compute its width, get its glyphs. */
18964 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18965 SET_TEXT_POS (it.position, -1, -1);
18966 PRODUCE_GLYPHS (&it);
18967
18968 /* If this character doesn't fit any more in the line, we have
18969 to remove some glyphs. */
18970 if (it.current_x > it.last_visible_x)
18971 {
18972 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18973 break;
18974 }
18975 }
18976
18977 set_buffer_temp (old);
18978 return it.glyph_row;
18979 }
18980
18981
18982 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18983 glyphs to insert is determined by produce_special_glyphs. */
18984
18985 static void
18986 insert_left_trunc_glyphs (struct it *it)
18987 {
18988 struct it truncate_it;
18989 struct glyph *from, *end, *to, *toend;
18990
18991 eassert (!FRAME_WINDOW_P (it->f)
18992 || (!it->glyph_row->reversed_p
18993 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18994 || (it->glyph_row->reversed_p
18995 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18996
18997 /* Get the truncation glyphs. */
18998 truncate_it = *it;
18999 truncate_it.current_x = 0;
19000 truncate_it.face_id = DEFAULT_FACE_ID;
19001 truncate_it.glyph_row = &scratch_glyph_row;
19002 truncate_it.area = TEXT_AREA;
19003 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19004 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19005 truncate_it.object = Qnil;
19006 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19007
19008 /* Overwrite glyphs from IT with truncation glyphs. */
19009 if (!it->glyph_row->reversed_p)
19010 {
19011 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19012
19013 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19014 end = from + tused;
19015 to = it->glyph_row->glyphs[TEXT_AREA];
19016 toend = to + it->glyph_row->used[TEXT_AREA];
19017 if (FRAME_WINDOW_P (it->f))
19018 {
19019 /* On GUI frames, when variable-size fonts are displayed,
19020 the truncation glyphs may need more pixels than the row's
19021 glyphs they overwrite. We overwrite more glyphs to free
19022 enough screen real estate, and enlarge the stretch glyph
19023 on the right (see display_line), if there is one, to
19024 preserve the screen position of the truncation glyphs on
19025 the right. */
19026 int w = 0;
19027 struct glyph *g = to;
19028 short used;
19029
19030 /* The first glyph could be partially visible, in which case
19031 it->glyph_row->x will be negative. But we want the left
19032 truncation glyphs to be aligned at the left margin of the
19033 window, so we override the x coordinate at which the row
19034 will begin. */
19035 it->glyph_row->x = 0;
19036 while (g < toend && w < it->truncation_pixel_width)
19037 {
19038 w += g->pixel_width;
19039 ++g;
19040 }
19041 if (g - to - tused > 0)
19042 {
19043 memmove (to + tused, g, (toend - g) * sizeof(*g));
19044 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19045 }
19046 used = it->glyph_row->used[TEXT_AREA];
19047 if (it->glyph_row->truncated_on_right_p
19048 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19049 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19050 == STRETCH_GLYPH)
19051 {
19052 int extra = w - it->truncation_pixel_width;
19053
19054 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19055 }
19056 }
19057
19058 while (from < end)
19059 *to++ = *from++;
19060
19061 /* There may be padding glyphs left over. Overwrite them too. */
19062 if (!FRAME_WINDOW_P (it->f))
19063 {
19064 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19065 {
19066 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19067 while (from < end)
19068 *to++ = *from++;
19069 }
19070 }
19071
19072 if (to > toend)
19073 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19074 }
19075 else
19076 {
19077 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19078
19079 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19080 that back to front. */
19081 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19082 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19083 toend = it->glyph_row->glyphs[TEXT_AREA];
19084 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19085 if (FRAME_WINDOW_P (it->f))
19086 {
19087 int w = 0;
19088 struct glyph *g = to;
19089
19090 while (g >= toend && w < it->truncation_pixel_width)
19091 {
19092 w += g->pixel_width;
19093 --g;
19094 }
19095 if (to - g - tused > 0)
19096 to = g + tused;
19097 if (it->glyph_row->truncated_on_right_p
19098 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19099 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19100 {
19101 int extra = w - it->truncation_pixel_width;
19102
19103 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19104 }
19105 }
19106
19107 while (from >= end && to >= toend)
19108 *to-- = *from--;
19109 if (!FRAME_WINDOW_P (it->f))
19110 {
19111 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19112 {
19113 from =
19114 truncate_it.glyph_row->glyphs[TEXT_AREA]
19115 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19116 while (from >= end && to >= toend)
19117 *to-- = *from--;
19118 }
19119 }
19120 if (from >= end)
19121 {
19122 /* Need to free some room before prepending additional
19123 glyphs. */
19124 int move_by = from - end + 1;
19125 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19126 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19127
19128 for ( ; g >= g0; g--)
19129 g[move_by] = *g;
19130 while (from >= end)
19131 *to-- = *from--;
19132 it->glyph_row->used[TEXT_AREA] += move_by;
19133 }
19134 }
19135 }
19136
19137 /* Compute the hash code for ROW. */
19138 unsigned
19139 row_hash (struct glyph_row *row)
19140 {
19141 int area, k;
19142 unsigned hashval = 0;
19143
19144 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19145 for (k = 0; k < row->used[area]; ++k)
19146 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19147 + row->glyphs[area][k].u.val
19148 + row->glyphs[area][k].face_id
19149 + row->glyphs[area][k].padding_p
19150 + (row->glyphs[area][k].type << 2));
19151
19152 return hashval;
19153 }
19154
19155 /* Compute the pixel height and width of IT->glyph_row.
19156
19157 Most of the time, ascent and height of a display line will be equal
19158 to the max_ascent and max_height values of the display iterator
19159 structure. This is not the case if
19160
19161 1. We hit ZV without displaying anything. In this case, max_ascent
19162 and max_height will be zero.
19163
19164 2. We have some glyphs that don't contribute to the line height.
19165 (The glyph row flag contributes_to_line_height_p is for future
19166 pixmap extensions).
19167
19168 The first case is easily covered by using default values because in
19169 these cases, the line height does not really matter, except that it
19170 must not be zero. */
19171
19172 static void
19173 compute_line_metrics (struct it *it)
19174 {
19175 struct glyph_row *row = it->glyph_row;
19176
19177 if (FRAME_WINDOW_P (it->f))
19178 {
19179 int i, min_y, max_y;
19180
19181 /* The line may consist of one space only, that was added to
19182 place the cursor on it. If so, the row's height hasn't been
19183 computed yet. */
19184 if (row->height == 0)
19185 {
19186 if (it->max_ascent + it->max_descent == 0)
19187 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19188 row->ascent = it->max_ascent;
19189 row->height = it->max_ascent + it->max_descent;
19190 row->phys_ascent = it->max_phys_ascent;
19191 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19192 row->extra_line_spacing = it->max_extra_line_spacing;
19193 }
19194
19195 /* Compute the width of this line. */
19196 row->pixel_width = row->x;
19197 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19198 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19199
19200 eassert (row->pixel_width >= 0);
19201 eassert (row->ascent >= 0 && row->height > 0);
19202
19203 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19204 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19205
19206 /* If first line's physical ascent is larger than its logical
19207 ascent, use the physical ascent, and make the row taller.
19208 This makes accented characters fully visible. */
19209 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19210 && row->phys_ascent > row->ascent)
19211 {
19212 row->height += row->phys_ascent - row->ascent;
19213 row->ascent = row->phys_ascent;
19214 }
19215
19216 /* Compute how much of the line is visible. */
19217 row->visible_height = row->height;
19218
19219 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19220 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19221
19222 if (row->y < min_y)
19223 row->visible_height -= min_y - row->y;
19224 if (row->y + row->height > max_y)
19225 row->visible_height -= row->y + row->height - max_y;
19226 }
19227 else
19228 {
19229 row->pixel_width = row->used[TEXT_AREA];
19230 if (row->continued_p)
19231 row->pixel_width -= it->continuation_pixel_width;
19232 else if (row->truncated_on_right_p)
19233 row->pixel_width -= it->truncation_pixel_width;
19234 row->ascent = row->phys_ascent = 0;
19235 row->height = row->phys_height = row->visible_height = 1;
19236 row->extra_line_spacing = 0;
19237 }
19238
19239 /* Compute a hash code for this row. */
19240 row->hash = row_hash (row);
19241
19242 it->max_ascent = it->max_descent = 0;
19243 it->max_phys_ascent = it->max_phys_descent = 0;
19244 }
19245
19246
19247 /* Append one space to the glyph row of iterator IT if doing a
19248 window-based redisplay. The space has the same face as
19249 IT->face_id. Value is true if a space was added.
19250
19251 This function is called to make sure that there is always one glyph
19252 at the end of a glyph row that the cursor can be set on under
19253 window-systems. (If there weren't such a glyph we would not know
19254 how wide and tall a box cursor should be displayed).
19255
19256 At the same time this space let's a nicely handle clearing to the
19257 end of the line if the row ends in italic text. */
19258
19259 static bool
19260 append_space_for_newline (struct it *it, bool default_face_p)
19261 {
19262 if (FRAME_WINDOW_P (it->f))
19263 {
19264 int n = it->glyph_row->used[TEXT_AREA];
19265
19266 if (it->glyph_row->glyphs[TEXT_AREA] + n
19267 < it->glyph_row->glyphs[1 + TEXT_AREA])
19268 {
19269 /* Save some values that must not be changed.
19270 Must save IT->c and IT->len because otherwise
19271 ITERATOR_AT_END_P wouldn't work anymore after
19272 append_space_for_newline has been called. */
19273 enum display_element_type saved_what = it->what;
19274 int saved_c = it->c, saved_len = it->len;
19275 int saved_char_to_display = it->char_to_display;
19276 int saved_x = it->current_x;
19277 int saved_face_id = it->face_id;
19278 bool saved_box_end = it->end_of_box_run_p;
19279 struct text_pos saved_pos;
19280 Lisp_Object saved_object;
19281 struct face *face;
19282 struct glyph *g;
19283
19284 saved_object = it->object;
19285 saved_pos = it->position;
19286
19287 it->what = IT_CHARACTER;
19288 memset (&it->position, 0, sizeof it->position);
19289 it->object = Qnil;
19290 it->c = it->char_to_display = ' ';
19291 it->len = 1;
19292
19293 /* If the default face was remapped, be sure to use the
19294 remapped face for the appended newline. */
19295 if (default_face_p)
19296 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19297 else if (it->face_before_selective_p)
19298 it->face_id = it->saved_face_id;
19299 face = FACE_FROM_ID (it->f, it->face_id);
19300 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19301 /* In R2L rows, we will prepend a stretch glyph that will
19302 have the end_of_box_run_p flag set for it, so there's no
19303 need for the appended newline glyph to have that flag
19304 set. */
19305 if (it->glyph_row->reversed_p
19306 /* But if the appended newline glyph goes all the way to
19307 the end of the row, there will be no stretch glyph,
19308 so leave the box flag set. */
19309 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19310 it->end_of_box_run_p = false;
19311
19312 PRODUCE_GLYPHS (it);
19313
19314 #ifdef HAVE_WINDOW_SYSTEM
19315 /* Make sure this space glyph has the right ascent and
19316 descent values, or else cursor at end of line will look
19317 funny, and height of empty lines will be incorrect. */
19318 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19319 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19320 if (n == 0)
19321 {
19322 Lisp_Object height, total_height;
19323 int extra_line_spacing = it->extra_line_spacing;
19324 int boff = font->baseline_offset;
19325
19326 if (font->vertical_centering)
19327 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19328
19329 it->object = saved_object; /* get_it_property needs this */
19330 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19331 /* Must do a subset of line height processing from
19332 x_produce_glyph for newline characters. */
19333 height = get_it_property (it, Qline_height);
19334 if (CONSP (height)
19335 && CONSP (XCDR (height))
19336 && NILP (XCDR (XCDR (height))))
19337 {
19338 total_height = XCAR (XCDR (height));
19339 height = XCAR (height);
19340 }
19341 else
19342 total_height = Qnil;
19343 height = calc_line_height_property (it, height, font, boff, true);
19344
19345 if (it->override_ascent >= 0)
19346 {
19347 it->ascent = it->override_ascent;
19348 it->descent = it->override_descent;
19349 boff = it->override_boff;
19350 }
19351 if (EQ (height, Qt))
19352 extra_line_spacing = 0;
19353 else
19354 {
19355 Lisp_Object spacing;
19356
19357 it->phys_ascent = it->ascent;
19358 it->phys_descent = it->descent;
19359 if (!NILP (height)
19360 && XINT (height) > it->ascent + it->descent)
19361 it->ascent = XINT (height) - it->descent;
19362
19363 if (!NILP (total_height))
19364 spacing = calc_line_height_property (it, total_height, font,
19365 boff, false);
19366 else
19367 {
19368 spacing = get_it_property (it, Qline_spacing);
19369 spacing = calc_line_height_property (it, spacing, font,
19370 boff, false);
19371 }
19372 if (INTEGERP (spacing))
19373 {
19374 extra_line_spacing = XINT (spacing);
19375 if (!NILP (total_height))
19376 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19377 }
19378 }
19379 if (extra_line_spacing > 0)
19380 {
19381 it->descent += extra_line_spacing;
19382 if (extra_line_spacing > it->max_extra_line_spacing)
19383 it->max_extra_line_spacing = extra_line_spacing;
19384 }
19385 it->max_ascent = it->ascent;
19386 it->max_descent = it->descent;
19387 /* Make sure compute_line_metrics recomputes the row height. */
19388 it->glyph_row->height = 0;
19389 }
19390
19391 g->ascent = it->max_ascent;
19392 g->descent = it->max_descent;
19393 #endif
19394
19395 it->override_ascent = -1;
19396 it->constrain_row_ascent_descent_p = false;
19397 it->current_x = saved_x;
19398 it->object = saved_object;
19399 it->position = saved_pos;
19400 it->what = saved_what;
19401 it->face_id = saved_face_id;
19402 it->len = saved_len;
19403 it->c = saved_c;
19404 it->char_to_display = saved_char_to_display;
19405 it->end_of_box_run_p = saved_box_end;
19406 return true;
19407 }
19408 }
19409
19410 return false;
19411 }
19412
19413
19414 /* Extend the face of the last glyph in the text area of IT->glyph_row
19415 to the end of the display line. Called from display_line. If the
19416 glyph row is empty, add a space glyph to it so that we know the
19417 face to draw. Set the glyph row flag fill_line_p. If the glyph
19418 row is R2L, prepend a stretch glyph to cover the empty space to the
19419 left of the leftmost glyph. */
19420
19421 static void
19422 extend_face_to_end_of_line (struct it *it)
19423 {
19424 struct face *face, *default_face;
19425 struct frame *f = it->f;
19426
19427 /* If line is already filled, do nothing. Non window-system frames
19428 get a grace of one more ``pixel'' because their characters are
19429 1-``pixel'' wide, so they hit the equality too early. This grace
19430 is needed only for R2L rows that are not continued, to produce
19431 one extra blank where we could display the cursor. */
19432 if ((it->current_x >= it->last_visible_x
19433 + (!FRAME_WINDOW_P (f)
19434 && it->glyph_row->reversed_p
19435 && !it->glyph_row->continued_p))
19436 /* If the window has display margins, we will need to extend
19437 their face even if the text area is filled. */
19438 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19439 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19440 return;
19441
19442 /* The default face, possibly remapped. */
19443 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19444
19445 /* Face extension extends the background and box of IT->face_id
19446 to the end of the line. If the background equals the background
19447 of the frame, we don't have to do anything. */
19448 if (it->face_before_selective_p)
19449 face = FACE_FROM_ID (f, it->saved_face_id);
19450 else
19451 face = FACE_FROM_ID (f, it->face_id);
19452
19453 if (FRAME_WINDOW_P (f)
19454 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19455 && face->box == FACE_NO_BOX
19456 && face->background == FRAME_BACKGROUND_PIXEL (f)
19457 #ifdef HAVE_WINDOW_SYSTEM
19458 && !face->stipple
19459 #endif
19460 && !it->glyph_row->reversed_p)
19461 return;
19462
19463 /* Set the glyph row flag indicating that the face of the last glyph
19464 in the text area has to be drawn to the end of the text area. */
19465 it->glyph_row->fill_line_p = true;
19466
19467 /* If current character of IT is not ASCII, make sure we have the
19468 ASCII face. This will be automatically undone the next time
19469 get_next_display_element returns a multibyte character. Note
19470 that the character will always be single byte in unibyte
19471 text. */
19472 if (!ASCII_CHAR_P (it->c))
19473 {
19474 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19475 }
19476
19477 if (FRAME_WINDOW_P (f))
19478 {
19479 /* If the row is empty, add a space with the current face of IT,
19480 so that we know which face to draw. */
19481 if (it->glyph_row->used[TEXT_AREA] == 0)
19482 {
19483 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19484 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19485 it->glyph_row->used[TEXT_AREA] = 1;
19486 }
19487 /* Mode line and the header line don't have margins, and
19488 likewise the frame's tool-bar window, if there is any. */
19489 if (!(it->glyph_row->mode_line_p
19490 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19491 || (WINDOWP (f->tool_bar_window)
19492 && it->w == XWINDOW (f->tool_bar_window))
19493 #endif
19494 ))
19495 {
19496 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19497 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19498 {
19499 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19500 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19501 default_face->id;
19502 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19503 }
19504 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19505 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19506 {
19507 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19508 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19509 default_face->id;
19510 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19511 }
19512 }
19513 #ifdef HAVE_WINDOW_SYSTEM
19514 if (it->glyph_row->reversed_p)
19515 {
19516 /* Prepend a stretch glyph to the row, such that the
19517 rightmost glyph will be drawn flushed all the way to the
19518 right margin of the window. The stretch glyph that will
19519 occupy the empty space, if any, to the left of the
19520 glyphs. */
19521 struct font *font = face->font ? face->font : FRAME_FONT (f);
19522 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19523 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19524 struct glyph *g;
19525 int row_width, stretch_ascent, stretch_width;
19526 struct text_pos saved_pos;
19527 int saved_face_id;
19528 bool saved_avoid_cursor, saved_box_start;
19529
19530 for (row_width = 0, g = row_start; g < row_end; g++)
19531 row_width += g->pixel_width;
19532
19533 /* FIXME: There are various minor display glitches in R2L
19534 rows when only one of the fringes is missing. The
19535 strange condition below produces the least bad effect. */
19536 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19537 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19538 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19539 stretch_width = window_box_width (it->w, TEXT_AREA);
19540 else
19541 stretch_width = it->last_visible_x - it->first_visible_x;
19542 stretch_width -= row_width;
19543
19544 if (stretch_width > 0)
19545 {
19546 stretch_ascent =
19547 (((it->ascent + it->descent)
19548 * FONT_BASE (font)) / FONT_HEIGHT (font));
19549 saved_pos = it->position;
19550 memset (&it->position, 0, sizeof it->position);
19551 saved_avoid_cursor = it->avoid_cursor_p;
19552 it->avoid_cursor_p = true;
19553 saved_face_id = it->face_id;
19554 saved_box_start = it->start_of_box_run_p;
19555 /* The last row's stretch glyph should get the default
19556 face, to avoid painting the rest of the window with
19557 the region face, if the region ends at ZV. */
19558 if (it->glyph_row->ends_at_zv_p)
19559 it->face_id = default_face->id;
19560 else
19561 it->face_id = face->id;
19562 it->start_of_box_run_p = false;
19563 append_stretch_glyph (it, Qnil, stretch_width,
19564 it->ascent + it->descent, stretch_ascent);
19565 it->position = saved_pos;
19566 it->avoid_cursor_p = saved_avoid_cursor;
19567 it->face_id = saved_face_id;
19568 it->start_of_box_run_p = saved_box_start;
19569 }
19570 /* If stretch_width comes out negative, it means that the
19571 last glyph is only partially visible. In R2L rows, we
19572 want the leftmost glyph to be partially visible, so we
19573 need to give the row the corresponding left offset. */
19574 if (stretch_width < 0)
19575 it->glyph_row->x = stretch_width;
19576 }
19577 #endif /* HAVE_WINDOW_SYSTEM */
19578 }
19579 else
19580 {
19581 /* Save some values that must not be changed. */
19582 int saved_x = it->current_x;
19583 struct text_pos saved_pos;
19584 Lisp_Object saved_object;
19585 enum display_element_type saved_what = it->what;
19586 int saved_face_id = it->face_id;
19587
19588 saved_object = it->object;
19589 saved_pos = it->position;
19590
19591 it->what = IT_CHARACTER;
19592 memset (&it->position, 0, sizeof it->position);
19593 it->object = Qnil;
19594 it->c = it->char_to_display = ' ';
19595 it->len = 1;
19596
19597 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19598 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19599 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19600 && !it->glyph_row->mode_line_p
19601 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19602 {
19603 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19604 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19605
19606 for (it->current_x = 0; g < e; g++)
19607 it->current_x += g->pixel_width;
19608
19609 it->area = LEFT_MARGIN_AREA;
19610 it->face_id = default_face->id;
19611 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19612 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19613 {
19614 PRODUCE_GLYPHS (it);
19615 /* term.c:produce_glyphs advances it->current_x only for
19616 TEXT_AREA. */
19617 it->current_x += it->pixel_width;
19618 }
19619
19620 it->current_x = saved_x;
19621 it->area = TEXT_AREA;
19622 }
19623
19624 /* The last row's blank glyphs should get the default face, to
19625 avoid painting the rest of the window with the region face,
19626 if the region ends at ZV. */
19627 if (it->glyph_row->ends_at_zv_p)
19628 it->face_id = default_face->id;
19629 else
19630 it->face_id = face->id;
19631 PRODUCE_GLYPHS (it);
19632
19633 while (it->current_x <= it->last_visible_x)
19634 PRODUCE_GLYPHS (it);
19635
19636 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19637 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19638 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19639 && !it->glyph_row->mode_line_p
19640 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19641 {
19642 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19643 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19644
19645 for ( ; g < e; g++)
19646 it->current_x += g->pixel_width;
19647
19648 it->area = RIGHT_MARGIN_AREA;
19649 it->face_id = default_face->id;
19650 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19651 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19652 {
19653 PRODUCE_GLYPHS (it);
19654 it->current_x += it->pixel_width;
19655 }
19656
19657 it->area = TEXT_AREA;
19658 }
19659
19660 /* Don't count these blanks really. It would let us insert a left
19661 truncation glyph below and make us set the cursor on them, maybe. */
19662 it->current_x = saved_x;
19663 it->object = saved_object;
19664 it->position = saved_pos;
19665 it->what = saved_what;
19666 it->face_id = saved_face_id;
19667 }
19668 }
19669
19670
19671 /* Value is true if text starting at CHARPOS in current_buffer is
19672 trailing whitespace. */
19673
19674 static bool
19675 trailing_whitespace_p (ptrdiff_t charpos)
19676 {
19677 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19678 int c = 0;
19679
19680 while (bytepos < ZV_BYTE
19681 && (c = FETCH_CHAR (bytepos),
19682 c == ' ' || c == '\t'))
19683 ++bytepos;
19684
19685 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19686 {
19687 if (bytepos != PT_BYTE)
19688 return true;
19689 }
19690 return false;
19691 }
19692
19693
19694 /* Highlight trailing whitespace, if any, in ROW. */
19695
19696 static void
19697 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19698 {
19699 int used = row->used[TEXT_AREA];
19700
19701 if (used)
19702 {
19703 struct glyph *start = row->glyphs[TEXT_AREA];
19704 struct glyph *glyph = start + used - 1;
19705
19706 if (row->reversed_p)
19707 {
19708 /* Right-to-left rows need to be processed in the opposite
19709 direction, so swap the edge pointers. */
19710 glyph = start;
19711 start = row->glyphs[TEXT_AREA] + used - 1;
19712 }
19713
19714 /* Skip over glyphs inserted to display the cursor at the
19715 end of a line, for extending the face of the last glyph
19716 to the end of the line on terminals, and for truncation
19717 and continuation glyphs. */
19718 if (!row->reversed_p)
19719 {
19720 while (glyph >= start
19721 && glyph->type == CHAR_GLYPH
19722 && NILP (glyph->object))
19723 --glyph;
19724 }
19725 else
19726 {
19727 while (glyph <= start
19728 && glyph->type == CHAR_GLYPH
19729 && NILP (glyph->object))
19730 ++glyph;
19731 }
19732
19733 /* If last glyph is a space or stretch, and it's trailing
19734 whitespace, set the face of all trailing whitespace glyphs in
19735 IT->glyph_row to `trailing-whitespace'. */
19736 if ((row->reversed_p ? glyph <= start : glyph >= start)
19737 && BUFFERP (glyph->object)
19738 && (glyph->type == STRETCH_GLYPH
19739 || (glyph->type == CHAR_GLYPH
19740 && glyph->u.ch == ' '))
19741 && trailing_whitespace_p (glyph->charpos))
19742 {
19743 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19744 if (face_id < 0)
19745 return;
19746
19747 if (!row->reversed_p)
19748 {
19749 while (glyph >= start
19750 && BUFFERP (glyph->object)
19751 && (glyph->type == STRETCH_GLYPH
19752 || (glyph->type == CHAR_GLYPH
19753 && glyph->u.ch == ' ')))
19754 (glyph--)->face_id = face_id;
19755 }
19756 else
19757 {
19758 while (glyph <= start
19759 && BUFFERP (glyph->object)
19760 && (glyph->type == STRETCH_GLYPH
19761 || (glyph->type == CHAR_GLYPH
19762 && glyph->u.ch == ' ')))
19763 (glyph++)->face_id = face_id;
19764 }
19765 }
19766 }
19767 }
19768
19769
19770 /* Value is true if glyph row ROW should be
19771 considered to hold the buffer position CHARPOS. */
19772
19773 static bool
19774 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19775 {
19776 bool result = true;
19777
19778 if (charpos == CHARPOS (row->end.pos)
19779 || charpos == MATRIX_ROW_END_CHARPOS (row))
19780 {
19781 /* Suppose the row ends on a string.
19782 Unless the row is continued, that means it ends on a newline
19783 in the string. If it's anything other than a display string
19784 (e.g., a before-string from an overlay), we don't want the
19785 cursor there. (This heuristic seems to give the optimal
19786 behavior for the various types of multi-line strings.)
19787 One exception: if the string has `cursor' property on one of
19788 its characters, we _do_ want the cursor there. */
19789 if (CHARPOS (row->end.string_pos) >= 0)
19790 {
19791 if (row->continued_p)
19792 result = true;
19793 else
19794 {
19795 /* Check for `display' property. */
19796 struct glyph *beg = row->glyphs[TEXT_AREA];
19797 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19798 struct glyph *glyph;
19799
19800 result = false;
19801 for (glyph = end; glyph >= beg; --glyph)
19802 if (STRINGP (glyph->object))
19803 {
19804 Lisp_Object prop
19805 = Fget_char_property (make_number (charpos),
19806 Qdisplay, Qnil);
19807 result =
19808 (!NILP (prop)
19809 && display_prop_string_p (prop, glyph->object));
19810 /* If there's a `cursor' property on one of the
19811 string's characters, this row is a cursor row,
19812 even though this is not a display string. */
19813 if (!result)
19814 {
19815 Lisp_Object s = glyph->object;
19816
19817 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19818 {
19819 ptrdiff_t gpos = glyph->charpos;
19820
19821 if (!NILP (Fget_char_property (make_number (gpos),
19822 Qcursor, s)))
19823 {
19824 result = true;
19825 break;
19826 }
19827 }
19828 }
19829 break;
19830 }
19831 }
19832 }
19833 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19834 {
19835 /* If the row ends in middle of a real character,
19836 and the line is continued, we want the cursor here.
19837 That's because CHARPOS (ROW->end.pos) would equal
19838 PT if PT is before the character. */
19839 if (!row->ends_in_ellipsis_p)
19840 result = row->continued_p;
19841 else
19842 /* If the row ends in an ellipsis, then
19843 CHARPOS (ROW->end.pos) will equal point after the
19844 invisible text. We want that position to be displayed
19845 after the ellipsis. */
19846 result = false;
19847 }
19848 /* If the row ends at ZV, display the cursor at the end of that
19849 row instead of at the start of the row below. */
19850 else
19851 result = row->ends_at_zv_p;
19852 }
19853
19854 return result;
19855 }
19856
19857 /* Value is true if glyph row ROW should be
19858 used to hold the cursor. */
19859
19860 static bool
19861 cursor_row_p (struct glyph_row *row)
19862 {
19863 return row_for_charpos_p (row, PT);
19864 }
19865
19866 \f
19867
19868 /* Push the property PROP so that it will be rendered at the current
19869 position in IT. Return true if PROP was successfully pushed, false
19870 otherwise. Called from handle_line_prefix to handle the
19871 `line-prefix' and `wrap-prefix' properties. */
19872
19873 static bool
19874 push_prefix_prop (struct it *it, Lisp_Object prop)
19875 {
19876 struct text_pos pos =
19877 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19878
19879 eassert (it->method == GET_FROM_BUFFER
19880 || it->method == GET_FROM_DISPLAY_VECTOR
19881 || it->method == GET_FROM_STRING
19882 || it->method == GET_FROM_IMAGE);
19883
19884 /* We need to save the current buffer/string position, so it will be
19885 restored by pop_it, because iterate_out_of_display_property
19886 depends on that being set correctly, but some situations leave
19887 it->position not yet set when this function is called. */
19888 push_it (it, &pos);
19889
19890 if (STRINGP (prop))
19891 {
19892 if (SCHARS (prop) == 0)
19893 {
19894 pop_it (it);
19895 return false;
19896 }
19897
19898 it->string = prop;
19899 it->string_from_prefix_prop_p = true;
19900 it->multibyte_p = STRING_MULTIBYTE (it->string);
19901 it->current.overlay_string_index = -1;
19902 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19903 it->end_charpos = it->string_nchars = SCHARS (it->string);
19904 it->method = GET_FROM_STRING;
19905 it->stop_charpos = 0;
19906 it->prev_stop = 0;
19907 it->base_level_stop = 0;
19908
19909 /* Force paragraph direction to be that of the parent
19910 buffer/string. */
19911 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19912 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19913 else
19914 it->paragraph_embedding = L2R;
19915
19916 /* Set up the bidi iterator for this display string. */
19917 if (it->bidi_p)
19918 {
19919 it->bidi_it.string.lstring = it->string;
19920 it->bidi_it.string.s = NULL;
19921 it->bidi_it.string.schars = it->end_charpos;
19922 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19923 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19924 it->bidi_it.string.unibyte = !it->multibyte_p;
19925 it->bidi_it.w = it->w;
19926 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19927 }
19928 }
19929 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19930 {
19931 it->method = GET_FROM_STRETCH;
19932 it->object = prop;
19933 }
19934 #ifdef HAVE_WINDOW_SYSTEM
19935 else if (IMAGEP (prop))
19936 {
19937 it->what = IT_IMAGE;
19938 it->image_id = lookup_image (it->f, prop);
19939 it->method = GET_FROM_IMAGE;
19940 }
19941 #endif /* HAVE_WINDOW_SYSTEM */
19942 else
19943 {
19944 pop_it (it); /* bogus display property, give up */
19945 return false;
19946 }
19947
19948 return true;
19949 }
19950
19951 /* Return the character-property PROP at the current position in IT. */
19952
19953 static Lisp_Object
19954 get_it_property (struct it *it, Lisp_Object prop)
19955 {
19956 Lisp_Object position, object = it->object;
19957
19958 if (STRINGP (object))
19959 position = make_number (IT_STRING_CHARPOS (*it));
19960 else if (BUFFERP (object))
19961 {
19962 position = make_number (IT_CHARPOS (*it));
19963 object = it->window;
19964 }
19965 else
19966 return Qnil;
19967
19968 return Fget_char_property (position, prop, object);
19969 }
19970
19971 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19972
19973 static void
19974 handle_line_prefix (struct it *it)
19975 {
19976 Lisp_Object prefix;
19977
19978 if (it->continuation_lines_width > 0)
19979 {
19980 prefix = get_it_property (it, Qwrap_prefix);
19981 if (NILP (prefix))
19982 prefix = Vwrap_prefix;
19983 }
19984 else
19985 {
19986 prefix = get_it_property (it, Qline_prefix);
19987 if (NILP (prefix))
19988 prefix = Vline_prefix;
19989 }
19990 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19991 {
19992 /* If the prefix is wider than the window, and we try to wrap
19993 it, it would acquire its own wrap prefix, and so on till the
19994 iterator stack overflows. So, don't wrap the prefix. */
19995 it->line_wrap = TRUNCATE;
19996 it->avoid_cursor_p = true;
19997 }
19998 }
19999
20000 \f
20001
20002 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20003 only for R2L lines from display_line and display_string, when they
20004 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20005 the line/string needs to be continued on the next glyph row. */
20006 static void
20007 unproduce_glyphs (struct it *it, int n)
20008 {
20009 struct glyph *glyph, *end;
20010
20011 eassert (it->glyph_row);
20012 eassert (it->glyph_row->reversed_p);
20013 eassert (it->area == TEXT_AREA);
20014 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20015
20016 if (n > it->glyph_row->used[TEXT_AREA])
20017 n = it->glyph_row->used[TEXT_AREA];
20018 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20019 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20020 for ( ; glyph < end; glyph++)
20021 glyph[-n] = *glyph;
20022 }
20023
20024 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20025 and ROW->maxpos. */
20026 static void
20027 find_row_edges (struct it *it, struct glyph_row *row,
20028 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20029 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20030 {
20031 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20032 lines' rows is implemented for bidi-reordered rows. */
20033
20034 /* ROW->minpos is the value of min_pos, the minimal buffer position
20035 we have in ROW, or ROW->start.pos if that is smaller. */
20036 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20037 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20038 else
20039 /* We didn't find buffer positions smaller than ROW->start, or
20040 didn't find _any_ valid buffer positions in any of the glyphs,
20041 so we must trust the iterator's computed positions. */
20042 row->minpos = row->start.pos;
20043 if (max_pos <= 0)
20044 {
20045 max_pos = CHARPOS (it->current.pos);
20046 max_bpos = BYTEPOS (it->current.pos);
20047 }
20048
20049 /* Here are the various use-cases for ending the row, and the
20050 corresponding values for ROW->maxpos:
20051
20052 Line ends in a newline from buffer eol_pos + 1
20053 Line is continued from buffer max_pos + 1
20054 Line is truncated on right it->current.pos
20055 Line ends in a newline from string max_pos + 1(*)
20056 (*) + 1 only when line ends in a forward scan
20057 Line is continued from string max_pos
20058 Line is continued from display vector max_pos
20059 Line is entirely from a string min_pos == max_pos
20060 Line is entirely from a display vector min_pos == max_pos
20061 Line that ends at ZV ZV
20062
20063 If you discover other use-cases, please add them here as
20064 appropriate. */
20065 if (row->ends_at_zv_p)
20066 row->maxpos = it->current.pos;
20067 else if (row->used[TEXT_AREA])
20068 {
20069 bool seen_this_string = false;
20070 struct glyph_row *r1 = row - 1;
20071
20072 /* Did we see the same display string on the previous row? */
20073 if (STRINGP (it->object)
20074 /* this is not the first row */
20075 && row > it->w->desired_matrix->rows
20076 /* previous row is not the header line */
20077 && !r1->mode_line_p
20078 /* previous row also ends in a newline from a string */
20079 && r1->ends_in_newline_from_string_p)
20080 {
20081 struct glyph *start, *end;
20082
20083 /* Search for the last glyph of the previous row that came
20084 from buffer or string. Depending on whether the row is
20085 L2R or R2L, we need to process it front to back or the
20086 other way round. */
20087 if (!r1->reversed_p)
20088 {
20089 start = r1->glyphs[TEXT_AREA];
20090 end = start + r1->used[TEXT_AREA];
20091 /* Glyphs inserted by redisplay have nil as their object. */
20092 while (end > start
20093 && NILP ((end - 1)->object)
20094 && (end - 1)->charpos <= 0)
20095 --end;
20096 if (end > start)
20097 {
20098 if (EQ ((end - 1)->object, it->object))
20099 seen_this_string = true;
20100 }
20101 else
20102 /* If all the glyphs of the previous row were inserted
20103 by redisplay, it means the previous row was
20104 produced from a single newline, which is only
20105 possible if that newline came from the same string
20106 as the one which produced this ROW. */
20107 seen_this_string = true;
20108 }
20109 else
20110 {
20111 end = r1->glyphs[TEXT_AREA] - 1;
20112 start = end + r1->used[TEXT_AREA];
20113 while (end < start
20114 && NILP ((end + 1)->object)
20115 && (end + 1)->charpos <= 0)
20116 ++end;
20117 if (end < start)
20118 {
20119 if (EQ ((end + 1)->object, it->object))
20120 seen_this_string = true;
20121 }
20122 else
20123 seen_this_string = true;
20124 }
20125 }
20126 /* Take note of each display string that covers a newline only
20127 once, the first time we see it. This is for when a display
20128 string includes more than one newline in it. */
20129 if (row->ends_in_newline_from_string_p && !seen_this_string)
20130 {
20131 /* If we were scanning the buffer forward when we displayed
20132 the string, we want to account for at least one buffer
20133 position that belongs to this row (position covered by
20134 the display string), so that cursor positioning will
20135 consider this row as a candidate when point is at the end
20136 of the visual line represented by this row. This is not
20137 required when scanning back, because max_pos will already
20138 have a much larger value. */
20139 if (CHARPOS (row->end.pos) > max_pos)
20140 INC_BOTH (max_pos, max_bpos);
20141 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20142 }
20143 else if (CHARPOS (it->eol_pos) > 0)
20144 SET_TEXT_POS (row->maxpos,
20145 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20146 else if (row->continued_p)
20147 {
20148 /* If max_pos is different from IT's current position, it
20149 means IT->method does not belong to the display element
20150 at max_pos. However, it also means that the display
20151 element at max_pos was displayed in its entirety on this
20152 line, which is equivalent to saying that the next line
20153 starts at the next buffer position. */
20154 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20155 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20156 else
20157 {
20158 INC_BOTH (max_pos, max_bpos);
20159 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20160 }
20161 }
20162 else if (row->truncated_on_right_p)
20163 /* display_line already called reseat_at_next_visible_line_start,
20164 which puts the iterator at the beginning of the next line, in
20165 the logical order. */
20166 row->maxpos = it->current.pos;
20167 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20168 /* A line that is entirely from a string/image/stretch... */
20169 row->maxpos = row->minpos;
20170 else
20171 emacs_abort ();
20172 }
20173 else
20174 row->maxpos = it->current.pos;
20175 }
20176
20177 /* Construct the glyph row IT->glyph_row in the desired matrix of
20178 IT->w from text at the current position of IT. See dispextern.h
20179 for an overview of struct it. Value is true if
20180 IT->glyph_row displays text, as opposed to a line displaying ZV
20181 only. */
20182
20183 static bool
20184 display_line (struct it *it)
20185 {
20186 struct glyph_row *row = it->glyph_row;
20187 Lisp_Object overlay_arrow_string;
20188 struct it wrap_it;
20189 void *wrap_data = NULL;
20190 bool may_wrap = false;
20191 int wrap_x IF_LINT (= 0);
20192 int wrap_row_used = -1;
20193 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20194 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20195 int wrap_row_extra_line_spacing IF_LINT (= 0);
20196 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20197 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20198 int cvpos;
20199 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20200 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20201 bool pending_handle_line_prefix = false;
20202
20203 /* We always start displaying at hpos zero even if hscrolled. */
20204 eassert (it->hpos == 0 && it->current_x == 0);
20205
20206 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20207 >= it->w->desired_matrix->nrows)
20208 {
20209 it->w->nrows_scale_factor++;
20210 it->f->fonts_changed = true;
20211 return false;
20212 }
20213
20214 /* Clear the result glyph row and enable it. */
20215 prepare_desired_row (it->w, row, false);
20216
20217 row->y = it->current_y;
20218 row->start = it->start;
20219 row->continuation_lines_width = it->continuation_lines_width;
20220 row->displays_text_p = true;
20221 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20222 it->starts_in_middle_of_char_p = false;
20223
20224 /* Arrange the overlays nicely for our purposes. Usually, we call
20225 display_line on only one line at a time, in which case this
20226 can't really hurt too much, or we call it on lines which appear
20227 one after another in the buffer, in which case all calls to
20228 recenter_overlay_lists but the first will be pretty cheap. */
20229 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20230
20231 /* Move over display elements that are not visible because we are
20232 hscrolled. This may stop at an x-position < IT->first_visible_x
20233 if the first glyph is partially visible or if we hit a line end. */
20234 if (it->current_x < it->first_visible_x)
20235 {
20236 enum move_it_result move_result;
20237
20238 this_line_min_pos = row->start.pos;
20239 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20240 MOVE_TO_POS | MOVE_TO_X);
20241 /* If we are under a large hscroll, move_it_in_display_line_to
20242 could hit the end of the line without reaching
20243 it->first_visible_x. Pretend that we did reach it. This is
20244 especially important on a TTY, where we will call
20245 extend_face_to_end_of_line, which needs to know how many
20246 blank glyphs to produce. */
20247 if (it->current_x < it->first_visible_x
20248 && (move_result == MOVE_NEWLINE_OR_CR
20249 || move_result == MOVE_POS_MATCH_OR_ZV))
20250 it->current_x = it->first_visible_x;
20251
20252 /* Record the smallest positions seen while we moved over
20253 display elements that are not visible. This is needed by
20254 redisplay_internal for optimizing the case where the cursor
20255 stays inside the same line. The rest of this function only
20256 considers positions that are actually displayed, so
20257 RECORD_MAX_MIN_POS will not otherwise record positions that
20258 are hscrolled to the left of the left edge of the window. */
20259 min_pos = CHARPOS (this_line_min_pos);
20260 min_bpos = BYTEPOS (this_line_min_pos);
20261 }
20262 else if (it->area == TEXT_AREA)
20263 {
20264 /* We only do this when not calling move_it_in_display_line_to
20265 above, because that function calls itself handle_line_prefix. */
20266 handle_line_prefix (it);
20267 }
20268 else
20269 {
20270 /* Line-prefix and wrap-prefix are always displayed in the text
20271 area. But if this is the first call to display_line after
20272 init_iterator, the iterator might have been set up to write
20273 into a marginal area, e.g. if the line begins with some
20274 display property that writes to the margins. So we need to
20275 wait with the call to handle_line_prefix until whatever
20276 writes to the margin has done its job. */
20277 pending_handle_line_prefix = true;
20278 }
20279
20280 /* Get the initial row height. This is either the height of the
20281 text hscrolled, if there is any, or zero. */
20282 row->ascent = it->max_ascent;
20283 row->height = it->max_ascent + it->max_descent;
20284 row->phys_ascent = it->max_phys_ascent;
20285 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20286 row->extra_line_spacing = it->max_extra_line_spacing;
20287
20288 /* Utility macro to record max and min buffer positions seen until now. */
20289 #define RECORD_MAX_MIN_POS(IT) \
20290 do \
20291 { \
20292 bool composition_p \
20293 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20294 ptrdiff_t current_pos = \
20295 composition_p ? (IT)->cmp_it.charpos \
20296 : IT_CHARPOS (*(IT)); \
20297 ptrdiff_t current_bpos = \
20298 composition_p ? CHAR_TO_BYTE (current_pos) \
20299 : IT_BYTEPOS (*(IT)); \
20300 if (current_pos < min_pos) \
20301 { \
20302 min_pos = current_pos; \
20303 min_bpos = current_bpos; \
20304 } \
20305 if (IT_CHARPOS (*it) > max_pos) \
20306 { \
20307 max_pos = IT_CHARPOS (*it); \
20308 max_bpos = IT_BYTEPOS (*it); \
20309 } \
20310 } \
20311 while (false)
20312
20313 /* Loop generating characters. The loop is left with IT on the next
20314 character to display. */
20315 while (true)
20316 {
20317 int n_glyphs_before, hpos_before, x_before;
20318 int x, nglyphs;
20319 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20320
20321 /* Retrieve the next thing to display. Value is false if end of
20322 buffer reached. */
20323 if (!get_next_display_element (it))
20324 {
20325 /* Maybe add a space at the end of this line that is used to
20326 display the cursor there under X. Set the charpos of the
20327 first glyph of blank lines not corresponding to any text
20328 to -1. */
20329 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20330 row->exact_window_width_line_p = true;
20331 else if ((append_space_for_newline (it, true)
20332 && row->used[TEXT_AREA] == 1)
20333 || row->used[TEXT_AREA] == 0)
20334 {
20335 row->glyphs[TEXT_AREA]->charpos = -1;
20336 row->displays_text_p = false;
20337
20338 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20339 && (!MINI_WINDOW_P (it->w)
20340 || (minibuf_level && EQ (it->window, minibuf_window))))
20341 row->indicate_empty_line_p = true;
20342 }
20343
20344 it->continuation_lines_width = 0;
20345 row->ends_at_zv_p = true;
20346 /* A row that displays right-to-left text must always have
20347 its last face extended all the way to the end of line,
20348 even if this row ends in ZV, because we still write to
20349 the screen left to right. We also need to extend the
20350 last face if the default face is remapped to some
20351 different face, otherwise the functions that clear
20352 portions of the screen will clear with the default face's
20353 background color. */
20354 if (row->reversed_p
20355 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20356 extend_face_to_end_of_line (it);
20357 break;
20358 }
20359
20360 /* Now, get the metrics of what we want to display. This also
20361 generates glyphs in `row' (which is IT->glyph_row). */
20362 n_glyphs_before = row->used[TEXT_AREA];
20363 x = it->current_x;
20364
20365 /* Remember the line height so far in case the next element doesn't
20366 fit on the line. */
20367 if (it->line_wrap != TRUNCATE)
20368 {
20369 ascent = it->max_ascent;
20370 descent = it->max_descent;
20371 phys_ascent = it->max_phys_ascent;
20372 phys_descent = it->max_phys_descent;
20373
20374 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20375 {
20376 if (IT_DISPLAYING_WHITESPACE (it))
20377 may_wrap = true;
20378 else if (may_wrap)
20379 {
20380 SAVE_IT (wrap_it, *it, wrap_data);
20381 wrap_x = x;
20382 wrap_row_used = row->used[TEXT_AREA];
20383 wrap_row_ascent = row->ascent;
20384 wrap_row_height = row->height;
20385 wrap_row_phys_ascent = row->phys_ascent;
20386 wrap_row_phys_height = row->phys_height;
20387 wrap_row_extra_line_spacing = row->extra_line_spacing;
20388 wrap_row_min_pos = min_pos;
20389 wrap_row_min_bpos = min_bpos;
20390 wrap_row_max_pos = max_pos;
20391 wrap_row_max_bpos = max_bpos;
20392 may_wrap = false;
20393 }
20394 }
20395 }
20396
20397 PRODUCE_GLYPHS (it);
20398
20399 /* If this display element was in marginal areas, continue with
20400 the next one. */
20401 if (it->area != TEXT_AREA)
20402 {
20403 row->ascent = max (row->ascent, it->max_ascent);
20404 row->height = max (row->height, it->max_ascent + it->max_descent);
20405 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20406 row->phys_height = max (row->phys_height,
20407 it->max_phys_ascent + it->max_phys_descent);
20408 row->extra_line_spacing = max (row->extra_line_spacing,
20409 it->max_extra_line_spacing);
20410 set_iterator_to_next (it, true);
20411 /* If we didn't handle the line/wrap prefix above, and the
20412 call to set_iterator_to_next just switched to TEXT_AREA,
20413 process the prefix now. */
20414 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20415 {
20416 pending_handle_line_prefix = false;
20417 handle_line_prefix (it);
20418 }
20419 continue;
20420 }
20421
20422 /* Does the display element fit on the line? If we truncate
20423 lines, we should draw past the right edge of the window. If
20424 we don't truncate, we want to stop so that we can display the
20425 continuation glyph before the right margin. If lines are
20426 continued, there are two possible strategies for characters
20427 resulting in more than 1 glyph (e.g. tabs): Display as many
20428 glyphs as possible in this line and leave the rest for the
20429 continuation line, or display the whole element in the next
20430 line. Original redisplay did the former, so we do it also. */
20431 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20432 hpos_before = it->hpos;
20433 x_before = x;
20434
20435 if (/* Not a newline. */
20436 nglyphs > 0
20437 /* Glyphs produced fit entirely in the line. */
20438 && it->current_x < it->last_visible_x)
20439 {
20440 it->hpos += nglyphs;
20441 row->ascent = max (row->ascent, it->max_ascent);
20442 row->height = max (row->height, it->max_ascent + it->max_descent);
20443 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20444 row->phys_height = max (row->phys_height,
20445 it->max_phys_ascent + it->max_phys_descent);
20446 row->extra_line_spacing = max (row->extra_line_spacing,
20447 it->max_extra_line_spacing);
20448 if (it->current_x - it->pixel_width < it->first_visible_x
20449 /* In R2L rows, we arrange in extend_face_to_end_of_line
20450 to add a right offset to the line, by a suitable
20451 change to the stretch glyph that is the leftmost
20452 glyph of the line. */
20453 && !row->reversed_p)
20454 row->x = x - it->first_visible_x;
20455 /* Record the maximum and minimum buffer positions seen so
20456 far in glyphs that will be displayed by this row. */
20457 if (it->bidi_p)
20458 RECORD_MAX_MIN_POS (it);
20459 }
20460 else
20461 {
20462 int i, new_x;
20463 struct glyph *glyph;
20464
20465 for (i = 0; i < nglyphs; ++i, x = new_x)
20466 {
20467 /* Identify the glyphs added by the last call to
20468 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20469 the previous glyphs. */
20470 if (!row->reversed_p)
20471 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20472 else
20473 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20474 new_x = x + glyph->pixel_width;
20475
20476 if (/* Lines are continued. */
20477 it->line_wrap != TRUNCATE
20478 && (/* Glyph doesn't fit on the line. */
20479 new_x > it->last_visible_x
20480 /* Or it fits exactly on a window system frame. */
20481 || (new_x == it->last_visible_x
20482 && FRAME_WINDOW_P (it->f)
20483 && (row->reversed_p
20484 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20485 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20486 {
20487 /* End of a continued line. */
20488
20489 if (it->hpos == 0
20490 || (new_x == it->last_visible_x
20491 && FRAME_WINDOW_P (it->f)
20492 && (row->reversed_p
20493 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20494 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20495 {
20496 /* Current glyph is the only one on the line or
20497 fits exactly on the line. We must continue
20498 the line because we can't draw the cursor
20499 after the glyph. */
20500 row->continued_p = true;
20501 it->current_x = new_x;
20502 it->continuation_lines_width += new_x;
20503 ++it->hpos;
20504 if (i == nglyphs - 1)
20505 {
20506 /* If line-wrap is on, check if a previous
20507 wrap point was found. */
20508 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20509 && wrap_row_used > 0
20510 /* Even if there is a previous wrap
20511 point, continue the line here as
20512 usual, if (i) the previous character
20513 was a space or tab AND (ii) the
20514 current character is not. */
20515 && (!may_wrap
20516 || IT_DISPLAYING_WHITESPACE (it)))
20517 goto back_to_wrap;
20518
20519 /* Record the maximum and minimum buffer
20520 positions seen so far in glyphs that will be
20521 displayed by this row. */
20522 if (it->bidi_p)
20523 RECORD_MAX_MIN_POS (it);
20524 set_iterator_to_next (it, true);
20525 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20526 {
20527 if (!get_next_display_element (it))
20528 {
20529 row->exact_window_width_line_p = true;
20530 it->continuation_lines_width = 0;
20531 row->continued_p = false;
20532 row->ends_at_zv_p = true;
20533 }
20534 else if (ITERATOR_AT_END_OF_LINE_P (it))
20535 {
20536 row->continued_p = false;
20537 row->exact_window_width_line_p = true;
20538 }
20539 /* If line-wrap is on, check if a
20540 previous wrap point was found. */
20541 else if (wrap_row_used > 0
20542 /* Even if there is a previous wrap
20543 point, continue the line here as
20544 usual, if (i) the previous character
20545 was a space or tab AND (ii) the
20546 current character is not. */
20547 && (!may_wrap
20548 || IT_DISPLAYING_WHITESPACE (it)))
20549 goto back_to_wrap;
20550
20551 }
20552 }
20553 else if (it->bidi_p)
20554 RECORD_MAX_MIN_POS (it);
20555 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20556 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20557 extend_face_to_end_of_line (it);
20558 }
20559 else if (CHAR_GLYPH_PADDING_P (*glyph)
20560 && !FRAME_WINDOW_P (it->f))
20561 {
20562 /* A padding glyph that doesn't fit on this line.
20563 This means the whole character doesn't fit
20564 on the line. */
20565 if (row->reversed_p)
20566 unproduce_glyphs (it, row->used[TEXT_AREA]
20567 - n_glyphs_before);
20568 row->used[TEXT_AREA] = n_glyphs_before;
20569
20570 /* Fill the rest of the row with continuation
20571 glyphs like in 20.x. */
20572 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20573 < row->glyphs[1 + TEXT_AREA])
20574 produce_special_glyphs (it, IT_CONTINUATION);
20575
20576 row->continued_p = true;
20577 it->current_x = x_before;
20578 it->continuation_lines_width += x_before;
20579
20580 /* Restore the height to what it was before the
20581 element not fitting on the line. */
20582 it->max_ascent = ascent;
20583 it->max_descent = descent;
20584 it->max_phys_ascent = phys_ascent;
20585 it->max_phys_descent = phys_descent;
20586 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20587 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20588 extend_face_to_end_of_line (it);
20589 }
20590 else if (wrap_row_used > 0)
20591 {
20592 back_to_wrap:
20593 if (row->reversed_p)
20594 unproduce_glyphs (it,
20595 row->used[TEXT_AREA] - wrap_row_used);
20596 RESTORE_IT (it, &wrap_it, wrap_data);
20597 it->continuation_lines_width += wrap_x;
20598 row->used[TEXT_AREA] = wrap_row_used;
20599 row->ascent = wrap_row_ascent;
20600 row->height = wrap_row_height;
20601 row->phys_ascent = wrap_row_phys_ascent;
20602 row->phys_height = wrap_row_phys_height;
20603 row->extra_line_spacing = wrap_row_extra_line_spacing;
20604 min_pos = wrap_row_min_pos;
20605 min_bpos = wrap_row_min_bpos;
20606 max_pos = wrap_row_max_pos;
20607 max_bpos = wrap_row_max_bpos;
20608 row->continued_p = true;
20609 row->ends_at_zv_p = false;
20610 row->exact_window_width_line_p = false;
20611 it->continuation_lines_width += x;
20612
20613 /* Make sure that a non-default face is extended
20614 up to the right margin of the window. */
20615 extend_face_to_end_of_line (it);
20616 }
20617 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20618 {
20619 /* A TAB that extends past the right edge of the
20620 window. This produces a single glyph on
20621 window system frames. We leave the glyph in
20622 this row and let it fill the row, but don't
20623 consume the TAB. */
20624 if ((row->reversed_p
20625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20627 produce_special_glyphs (it, IT_CONTINUATION);
20628 it->continuation_lines_width += it->last_visible_x;
20629 row->ends_in_middle_of_char_p = true;
20630 row->continued_p = true;
20631 glyph->pixel_width = it->last_visible_x - x;
20632 it->starts_in_middle_of_char_p = true;
20633 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20634 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20635 extend_face_to_end_of_line (it);
20636 }
20637 else
20638 {
20639 /* Something other than a TAB that draws past
20640 the right edge of the window. Restore
20641 positions to values before the element. */
20642 if (row->reversed_p)
20643 unproduce_glyphs (it, row->used[TEXT_AREA]
20644 - (n_glyphs_before + i));
20645 row->used[TEXT_AREA] = n_glyphs_before + i;
20646
20647 /* Display continuation glyphs. */
20648 it->current_x = x_before;
20649 it->continuation_lines_width += x;
20650 if (!FRAME_WINDOW_P (it->f)
20651 || (row->reversed_p
20652 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20653 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20654 produce_special_glyphs (it, IT_CONTINUATION);
20655 row->continued_p = true;
20656
20657 extend_face_to_end_of_line (it);
20658
20659 if (nglyphs > 1 && i > 0)
20660 {
20661 row->ends_in_middle_of_char_p = true;
20662 it->starts_in_middle_of_char_p = true;
20663 }
20664
20665 /* Restore the height to what it was before the
20666 element not fitting on the line. */
20667 it->max_ascent = ascent;
20668 it->max_descent = descent;
20669 it->max_phys_ascent = phys_ascent;
20670 it->max_phys_descent = phys_descent;
20671 }
20672
20673 break;
20674 }
20675 else if (new_x > it->first_visible_x)
20676 {
20677 /* Increment number of glyphs actually displayed. */
20678 ++it->hpos;
20679
20680 /* Record the maximum and minimum buffer positions
20681 seen so far in glyphs that will be displayed by
20682 this row. */
20683 if (it->bidi_p)
20684 RECORD_MAX_MIN_POS (it);
20685
20686 if (x < it->first_visible_x && !row->reversed_p)
20687 /* Glyph is partially visible, i.e. row starts at
20688 negative X position. Don't do that in R2L
20689 rows, where we arrange to add a right offset to
20690 the line in extend_face_to_end_of_line, by a
20691 suitable change to the stretch glyph that is
20692 the leftmost glyph of the line. */
20693 row->x = x - it->first_visible_x;
20694 /* When the last glyph of an R2L row only fits
20695 partially on the line, we need to set row->x to a
20696 negative offset, so that the leftmost glyph is
20697 the one that is partially visible. But if we are
20698 going to produce the truncation glyph, this will
20699 be taken care of in produce_special_glyphs. */
20700 if (row->reversed_p
20701 && new_x > it->last_visible_x
20702 && !(it->line_wrap == TRUNCATE
20703 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20704 {
20705 eassert (FRAME_WINDOW_P (it->f));
20706 row->x = it->last_visible_x - new_x;
20707 }
20708 }
20709 else
20710 {
20711 /* Glyph is completely off the left margin of the
20712 window. This should not happen because of the
20713 move_it_in_display_line at the start of this
20714 function, unless the text display area of the
20715 window is empty. */
20716 eassert (it->first_visible_x <= it->last_visible_x);
20717 }
20718 }
20719 /* Even if this display element produced no glyphs at all,
20720 we want to record its position. */
20721 if (it->bidi_p && nglyphs == 0)
20722 RECORD_MAX_MIN_POS (it);
20723
20724 row->ascent = max (row->ascent, it->max_ascent);
20725 row->height = max (row->height, it->max_ascent + it->max_descent);
20726 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20727 row->phys_height = max (row->phys_height,
20728 it->max_phys_ascent + it->max_phys_descent);
20729 row->extra_line_spacing = max (row->extra_line_spacing,
20730 it->max_extra_line_spacing);
20731
20732 /* End of this display line if row is continued. */
20733 if (row->continued_p || row->ends_at_zv_p)
20734 break;
20735 }
20736
20737 at_end_of_line:
20738 /* Is this a line end? If yes, we're also done, after making
20739 sure that a non-default face is extended up to the right
20740 margin of the window. */
20741 if (ITERATOR_AT_END_OF_LINE_P (it))
20742 {
20743 int used_before = row->used[TEXT_AREA];
20744
20745 row->ends_in_newline_from_string_p = STRINGP (it->object);
20746
20747 /* Add a space at the end of the line that is used to
20748 display the cursor there. */
20749 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20750 append_space_for_newline (it, false);
20751
20752 /* Extend the face to the end of the line. */
20753 extend_face_to_end_of_line (it);
20754
20755 /* Make sure we have the position. */
20756 if (used_before == 0)
20757 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20758
20759 /* Record the position of the newline, for use in
20760 find_row_edges. */
20761 it->eol_pos = it->current.pos;
20762
20763 /* Consume the line end. This skips over invisible lines. */
20764 set_iterator_to_next (it, true);
20765 it->continuation_lines_width = 0;
20766 break;
20767 }
20768
20769 /* Proceed with next display element. Note that this skips
20770 over lines invisible because of selective display. */
20771 set_iterator_to_next (it, true);
20772
20773 /* If we truncate lines, we are done when the last displayed
20774 glyphs reach past the right margin of the window. */
20775 if (it->line_wrap == TRUNCATE
20776 && ((FRAME_WINDOW_P (it->f)
20777 /* Images are preprocessed in produce_image_glyph such
20778 that they are cropped at the right edge of the
20779 window, so an image glyph will always end exactly at
20780 last_visible_x, even if there's no right fringe. */
20781 && ((row->reversed_p
20782 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20783 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20784 || it->what == IT_IMAGE))
20785 ? (it->current_x >= it->last_visible_x)
20786 : (it->current_x > it->last_visible_x)))
20787 {
20788 /* Maybe add truncation glyphs. */
20789 if (!FRAME_WINDOW_P (it->f)
20790 || (row->reversed_p
20791 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20792 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20793 {
20794 int i, n;
20795
20796 if (!row->reversed_p)
20797 {
20798 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20799 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20800 break;
20801 }
20802 else
20803 {
20804 for (i = 0; i < row->used[TEXT_AREA]; i++)
20805 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20806 break;
20807 /* Remove any padding glyphs at the front of ROW, to
20808 make room for the truncation glyphs we will be
20809 adding below. The loop below always inserts at
20810 least one truncation glyph, so also remove the
20811 last glyph added to ROW. */
20812 unproduce_glyphs (it, i + 1);
20813 /* Adjust i for the loop below. */
20814 i = row->used[TEXT_AREA] - (i + 1);
20815 }
20816
20817 /* produce_special_glyphs overwrites the last glyph, so
20818 we don't want that if we want to keep that last
20819 glyph, which means it's an image. */
20820 if (it->current_x > it->last_visible_x)
20821 {
20822 it->current_x = x_before;
20823 if (!FRAME_WINDOW_P (it->f))
20824 {
20825 for (n = row->used[TEXT_AREA]; i < n; ++i)
20826 {
20827 row->used[TEXT_AREA] = i;
20828 produce_special_glyphs (it, IT_TRUNCATION);
20829 }
20830 }
20831 else
20832 {
20833 row->used[TEXT_AREA] = i;
20834 produce_special_glyphs (it, IT_TRUNCATION);
20835 }
20836 it->hpos = hpos_before;
20837 }
20838 }
20839 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20840 {
20841 /* Don't truncate if we can overflow newline into fringe. */
20842 if (!get_next_display_element (it))
20843 {
20844 it->continuation_lines_width = 0;
20845 row->ends_at_zv_p = true;
20846 row->exact_window_width_line_p = true;
20847 break;
20848 }
20849 if (ITERATOR_AT_END_OF_LINE_P (it))
20850 {
20851 row->exact_window_width_line_p = true;
20852 goto at_end_of_line;
20853 }
20854 it->current_x = x_before;
20855 it->hpos = hpos_before;
20856 }
20857
20858 row->truncated_on_right_p = true;
20859 it->continuation_lines_width = 0;
20860 reseat_at_next_visible_line_start (it, false);
20861 /* We insist below that IT's position be at ZV because in
20862 bidi-reordered lines the character at visible line start
20863 might not be the character that follows the newline in
20864 the logical order. */
20865 if (IT_BYTEPOS (*it) > BEG_BYTE)
20866 row->ends_at_zv_p =
20867 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20868 else
20869 row->ends_at_zv_p = false;
20870 break;
20871 }
20872 }
20873
20874 if (wrap_data)
20875 bidi_unshelve_cache (wrap_data, true);
20876
20877 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20878 at the left window margin. */
20879 if (it->first_visible_x
20880 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20881 {
20882 if (!FRAME_WINDOW_P (it->f)
20883 || (((row->reversed_p
20884 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20885 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20886 /* Don't let insert_left_trunc_glyphs overwrite the
20887 first glyph of the row if it is an image. */
20888 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20889 insert_left_trunc_glyphs (it);
20890 row->truncated_on_left_p = true;
20891 }
20892
20893 /* Remember the position at which this line ends.
20894
20895 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20896 cannot be before the call to find_row_edges below, since that is
20897 where these positions are determined. */
20898 row->end = it->current;
20899 if (!it->bidi_p)
20900 {
20901 row->minpos = row->start.pos;
20902 row->maxpos = row->end.pos;
20903 }
20904 else
20905 {
20906 /* ROW->minpos and ROW->maxpos must be the smallest and
20907 `1 + the largest' buffer positions in ROW. But if ROW was
20908 bidi-reordered, these two positions can be anywhere in the
20909 row, so we must determine them now. */
20910 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20911 }
20912
20913 /* If the start of this line is the overlay arrow-position, then
20914 mark this glyph row as the one containing the overlay arrow.
20915 This is clearly a mess with variable size fonts. It would be
20916 better to let it be displayed like cursors under X. */
20917 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20918 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20919 !NILP (overlay_arrow_string)))
20920 {
20921 /* Overlay arrow in window redisplay is a fringe bitmap. */
20922 if (STRINGP (overlay_arrow_string))
20923 {
20924 struct glyph_row *arrow_row
20925 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20926 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20927 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20928 struct glyph *p = row->glyphs[TEXT_AREA];
20929 struct glyph *p2, *end;
20930
20931 /* Copy the arrow glyphs. */
20932 while (glyph < arrow_end)
20933 *p++ = *glyph++;
20934
20935 /* Throw away padding glyphs. */
20936 p2 = p;
20937 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20938 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20939 ++p2;
20940 if (p2 > p)
20941 {
20942 while (p2 < end)
20943 *p++ = *p2++;
20944 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20945 }
20946 }
20947 else
20948 {
20949 eassert (INTEGERP (overlay_arrow_string));
20950 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20951 }
20952 overlay_arrow_seen = true;
20953 }
20954
20955 /* Highlight trailing whitespace. */
20956 if (!NILP (Vshow_trailing_whitespace))
20957 highlight_trailing_whitespace (it->f, it->glyph_row);
20958
20959 /* Compute pixel dimensions of this line. */
20960 compute_line_metrics (it);
20961
20962 /* Implementation note: No changes in the glyphs of ROW or in their
20963 faces can be done past this point, because compute_line_metrics
20964 computes ROW's hash value and stores it within the glyph_row
20965 structure. */
20966
20967 /* Record whether this row ends inside an ellipsis. */
20968 row->ends_in_ellipsis_p
20969 = (it->method == GET_FROM_DISPLAY_VECTOR
20970 && it->ellipsis_p);
20971
20972 /* Save fringe bitmaps in this row. */
20973 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20974 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20975 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20976 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20977
20978 it->left_user_fringe_bitmap = 0;
20979 it->left_user_fringe_face_id = 0;
20980 it->right_user_fringe_bitmap = 0;
20981 it->right_user_fringe_face_id = 0;
20982
20983 /* Maybe set the cursor. */
20984 cvpos = it->w->cursor.vpos;
20985 if ((cvpos < 0
20986 /* In bidi-reordered rows, keep checking for proper cursor
20987 position even if one has been found already, because buffer
20988 positions in such rows change non-linearly with ROW->VPOS,
20989 when a line is continued. One exception: when we are at ZV,
20990 display cursor on the first suitable glyph row, since all
20991 the empty rows after that also have their position set to ZV. */
20992 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20993 lines' rows is implemented for bidi-reordered rows. */
20994 || (it->bidi_p
20995 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20996 && PT >= MATRIX_ROW_START_CHARPOS (row)
20997 && PT <= MATRIX_ROW_END_CHARPOS (row)
20998 && cursor_row_p (row))
20999 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21000
21001 /* Prepare for the next line. This line starts horizontally at (X
21002 HPOS) = (0 0). Vertical positions are incremented. As a
21003 convenience for the caller, IT->glyph_row is set to the next
21004 row to be used. */
21005 it->current_x = it->hpos = 0;
21006 it->current_y += row->height;
21007 SET_TEXT_POS (it->eol_pos, 0, 0);
21008 ++it->vpos;
21009 ++it->glyph_row;
21010 /* The next row should by default use the same value of the
21011 reversed_p flag as this one. set_iterator_to_next decides when
21012 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21013 the flag accordingly. */
21014 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21015 it->glyph_row->reversed_p = row->reversed_p;
21016 it->start = row->end;
21017 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21018
21019 #undef RECORD_MAX_MIN_POS
21020 }
21021
21022 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21023 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21024 doc: /* Return paragraph direction at point in BUFFER.
21025 Value is either `left-to-right' or `right-to-left'.
21026 If BUFFER is omitted or nil, it defaults to the current buffer.
21027
21028 Paragraph direction determines how the text in the paragraph is displayed.
21029 In left-to-right paragraphs, text begins at the left margin of the window
21030 and the reading direction is generally left to right. In right-to-left
21031 paragraphs, text begins at the right margin and is read from right to left.
21032
21033 See also `bidi-paragraph-direction'. */)
21034 (Lisp_Object buffer)
21035 {
21036 struct buffer *buf = current_buffer;
21037 struct buffer *old = buf;
21038
21039 if (! NILP (buffer))
21040 {
21041 CHECK_BUFFER (buffer);
21042 buf = XBUFFER (buffer);
21043 }
21044
21045 if (NILP (BVAR (buf, bidi_display_reordering))
21046 || NILP (BVAR (buf, enable_multibyte_characters))
21047 /* When we are loading loadup.el, the character property tables
21048 needed for bidi iteration are not yet available. */
21049 || !NILP (Vpurify_flag))
21050 return Qleft_to_right;
21051 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21052 return BVAR (buf, bidi_paragraph_direction);
21053 else
21054 {
21055 /* Determine the direction from buffer text. We could try to
21056 use current_matrix if it is up to date, but this seems fast
21057 enough as it is. */
21058 struct bidi_it itb;
21059 ptrdiff_t pos = BUF_PT (buf);
21060 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21061 int c;
21062 void *itb_data = bidi_shelve_cache ();
21063
21064 set_buffer_temp (buf);
21065 /* bidi_paragraph_init finds the base direction of the paragraph
21066 by searching forward from paragraph start. We need the base
21067 direction of the current or _previous_ paragraph, so we need
21068 to make sure we are within that paragraph. To that end, find
21069 the previous non-empty line. */
21070 if (pos >= ZV && pos > BEGV)
21071 DEC_BOTH (pos, bytepos);
21072 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21073 if (fast_looking_at (trailing_white_space,
21074 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21075 {
21076 while ((c = FETCH_BYTE (bytepos)) == '\n'
21077 || c == ' ' || c == '\t' || c == '\f')
21078 {
21079 if (bytepos <= BEGV_BYTE)
21080 break;
21081 bytepos--;
21082 pos--;
21083 }
21084 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21085 bytepos--;
21086 }
21087 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21088 itb.paragraph_dir = NEUTRAL_DIR;
21089 itb.string.s = NULL;
21090 itb.string.lstring = Qnil;
21091 itb.string.bufpos = 0;
21092 itb.string.from_disp_str = false;
21093 itb.string.unibyte = false;
21094 /* We have no window to use here for ignoring window-specific
21095 overlays. Using NULL for window pointer will cause
21096 compute_display_string_pos to use the current buffer. */
21097 itb.w = NULL;
21098 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21099 bidi_unshelve_cache (itb_data, false);
21100 set_buffer_temp (old);
21101 switch (itb.paragraph_dir)
21102 {
21103 case L2R:
21104 return Qleft_to_right;
21105 break;
21106 case R2L:
21107 return Qright_to_left;
21108 break;
21109 default:
21110 emacs_abort ();
21111 }
21112 }
21113 }
21114
21115 DEFUN ("bidi-find-overridden-directionality",
21116 Fbidi_find_overridden_directionality,
21117 Sbidi_find_overridden_directionality, 2, 3, 0,
21118 doc: /* Return position between FROM and TO where directionality was overridden.
21119
21120 This function returns the first character position in the specified
21121 region of OBJECT where there is a character whose `bidi-class' property
21122 is `L', but which was forced to display as `R' by a directional
21123 override, and likewise with characters whose `bidi-class' is `R'
21124 or `AL' that were forced to display as `L'.
21125
21126 If no such character is found, the function returns nil.
21127
21128 OBJECT is a Lisp string or buffer to search for overridden
21129 directionality, and defaults to the current buffer if nil or omitted.
21130 OBJECT can also be a window, in which case the function will search
21131 the buffer displayed in that window. Passing the window instead of
21132 a buffer is preferable when the buffer is displayed in some window,
21133 because this function will then be able to correctly account for
21134 window-specific overlays, which can affect the results.
21135
21136 Strong directional characters `L', `R', and `AL' can have their
21137 intrinsic directionality overridden by directional override
21138 control characters RLO (u+202e) and LRO (u+202d). See the
21139 function `get-char-code-property' for a way to inquire about
21140 the `bidi-class' property of a character. */)
21141 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21142 {
21143 struct buffer *buf = current_buffer;
21144 struct buffer *old = buf;
21145 struct window *w = NULL;
21146 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21147 struct bidi_it itb;
21148 ptrdiff_t from_pos, to_pos, from_bpos;
21149 void *itb_data;
21150
21151 if (!NILP (object))
21152 {
21153 if (BUFFERP (object))
21154 buf = XBUFFER (object);
21155 else if (WINDOWP (object))
21156 {
21157 w = decode_live_window (object);
21158 buf = XBUFFER (w->contents);
21159 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21160 }
21161 else
21162 CHECK_STRING (object);
21163 }
21164
21165 if (STRINGP (object))
21166 {
21167 /* Characters in unibyte strings are always treated by bidi.c as
21168 strong LTR. */
21169 if (!STRING_MULTIBYTE (object)
21170 /* When we are loading loadup.el, the character property
21171 tables needed for bidi iteration are not yet
21172 available. */
21173 || !NILP (Vpurify_flag))
21174 return Qnil;
21175
21176 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21177 if (from_pos >= SCHARS (object))
21178 return Qnil;
21179
21180 /* Set up the bidi iterator. */
21181 itb_data = bidi_shelve_cache ();
21182 itb.paragraph_dir = NEUTRAL_DIR;
21183 itb.string.lstring = object;
21184 itb.string.s = NULL;
21185 itb.string.schars = SCHARS (object);
21186 itb.string.bufpos = 0;
21187 itb.string.from_disp_str = false;
21188 itb.string.unibyte = false;
21189 itb.w = w;
21190 bidi_init_it (0, 0, frame_window_p, &itb);
21191 }
21192 else
21193 {
21194 /* Nothing this fancy can happen in unibyte buffers, or in a
21195 buffer that disabled reordering, or if FROM is at EOB. */
21196 if (NILP (BVAR (buf, bidi_display_reordering))
21197 || NILP (BVAR (buf, enable_multibyte_characters))
21198 /* When we are loading loadup.el, the character property
21199 tables needed for bidi iteration are not yet
21200 available. */
21201 || !NILP (Vpurify_flag))
21202 return Qnil;
21203
21204 set_buffer_temp (buf);
21205 validate_region (&from, &to);
21206 from_pos = XINT (from);
21207 to_pos = XINT (to);
21208 if (from_pos >= ZV)
21209 return Qnil;
21210
21211 /* Set up the bidi iterator. */
21212 itb_data = bidi_shelve_cache ();
21213 from_bpos = CHAR_TO_BYTE (from_pos);
21214 if (from_pos == BEGV)
21215 {
21216 itb.charpos = BEGV;
21217 itb.bytepos = BEGV_BYTE;
21218 }
21219 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21220 {
21221 itb.charpos = from_pos;
21222 itb.bytepos = from_bpos;
21223 }
21224 else
21225 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21226 -1, &itb.bytepos);
21227 itb.paragraph_dir = NEUTRAL_DIR;
21228 itb.string.s = NULL;
21229 itb.string.lstring = Qnil;
21230 itb.string.bufpos = 0;
21231 itb.string.from_disp_str = false;
21232 itb.string.unibyte = false;
21233 itb.w = w;
21234 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21235 }
21236
21237 ptrdiff_t found;
21238 do {
21239 /* For the purposes of this function, the actual base direction of
21240 the paragraph doesn't matter, so just set it to L2R. */
21241 bidi_paragraph_init (L2R, &itb, false);
21242 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21243 ;
21244 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21245
21246 bidi_unshelve_cache (itb_data, false);
21247 set_buffer_temp (old);
21248
21249 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21250 }
21251
21252 DEFUN ("move-point-visually", Fmove_point_visually,
21253 Smove_point_visually, 1, 1, 0,
21254 doc: /* Move point in the visual order in the specified DIRECTION.
21255 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21256 left.
21257
21258 Value is the new character position of point. */)
21259 (Lisp_Object direction)
21260 {
21261 struct window *w = XWINDOW (selected_window);
21262 struct buffer *b = XBUFFER (w->contents);
21263 struct glyph_row *row;
21264 int dir;
21265 Lisp_Object paragraph_dir;
21266
21267 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21268 (!(ROW)->continued_p \
21269 && NILP ((GLYPH)->object) \
21270 && (GLYPH)->type == CHAR_GLYPH \
21271 && (GLYPH)->u.ch == ' ' \
21272 && (GLYPH)->charpos >= 0 \
21273 && !(GLYPH)->avoid_cursor_p)
21274
21275 CHECK_NUMBER (direction);
21276 dir = XINT (direction);
21277 if (dir > 0)
21278 dir = 1;
21279 else
21280 dir = -1;
21281
21282 /* If current matrix is up-to-date, we can use the information
21283 recorded in the glyphs, at least as long as the goal is on the
21284 screen. */
21285 if (w->window_end_valid
21286 && !windows_or_buffers_changed
21287 && b
21288 && !b->clip_changed
21289 && !b->prevent_redisplay_optimizations_p
21290 && !window_outdated (w)
21291 /* We rely below on the cursor coordinates to be up to date, but
21292 we cannot trust them if some command moved point since the
21293 last complete redisplay. */
21294 && w->last_point == BUF_PT (b)
21295 && w->cursor.vpos >= 0
21296 && w->cursor.vpos < w->current_matrix->nrows
21297 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21298 {
21299 struct glyph *g = row->glyphs[TEXT_AREA];
21300 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21301 struct glyph *gpt = g + w->cursor.hpos;
21302
21303 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21304 {
21305 if (BUFFERP (g->object) && g->charpos != PT)
21306 {
21307 SET_PT (g->charpos);
21308 w->cursor.vpos = -1;
21309 return make_number (PT);
21310 }
21311 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21312 {
21313 ptrdiff_t new_pos;
21314
21315 if (BUFFERP (gpt->object))
21316 {
21317 new_pos = PT;
21318 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21319 new_pos += (row->reversed_p ? -dir : dir);
21320 else
21321 new_pos -= (row->reversed_p ? -dir : dir);
21322 }
21323 else if (BUFFERP (g->object))
21324 new_pos = g->charpos;
21325 else
21326 break;
21327 SET_PT (new_pos);
21328 w->cursor.vpos = -1;
21329 return make_number (PT);
21330 }
21331 else if (ROW_GLYPH_NEWLINE_P (row, g))
21332 {
21333 /* Glyphs inserted at the end of a non-empty line for
21334 positioning the cursor have zero charpos, so we must
21335 deduce the value of point by other means. */
21336 if (g->charpos > 0)
21337 SET_PT (g->charpos);
21338 else if (row->ends_at_zv_p && PT != ZV)
21339 SET_PT (ZV);
21340 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21341 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21342 else
21343 break;
21344 w->cursor.vpos = -1;
21345 return make_number (PT);
21346 }
21347 }
21348 if (g == e || NILP (g->object))
21349 {
21350 if (row->truncated_on_left_p || row->truncated_on_right_p)
21351 goto simulate_display;
21352 if (!row->reversed_p)
21353 row += dir;
21354 else
21355 row -= dir;
21356 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21357 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21358 goto simulate_display;
21359
21360 if (dir > 0)
21361 {
21362 if (row->reversed_p && !row->continued_p)
21363 {
21364 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21365 w->cursor.vpos = -1;
21366 return make_number (PT);
21367 }
21368 g = row->glyphs[TEXT_AREA];
21369 e = g + row->used[TEXT_AREA];
21370 for ( ; g < e; g++)
21371 {
21372 if (BUFFERP (g->object)
21373 /* Empty lines have only one glyph, which stands
21374 for the newline, and whose charpos is the
21375 buffer position of the newline. */
21376 || ROW_GLYPH_NEWLINE_P (row, g)
21377 /* When the buffer ends in a newline, the line at
21378 EOB also has one glyph, but its charpos is -1. */
21379 || (row->ends_at_zv_p
21380 && !row->reversed_p
21381 && NILP (g->object)
21382 && g->type == CHAR_GLYPH
21383 && g->u.ch == ' '))
21384 {
21385 if (g->charpos > 0)
21386 SET_PT (g->charpos);
21387 else if (!row->reversed_p
21388 && row->ends_at_zv_p
21389 && PT != ZV)
21390 SET_PT (ZV);
21391 else
21392 continue;
21393 w->cursor.vpos = -1;
21394 return make_number (PT);
21395 }
21396 }
21397 }
21398 else
21399 {
21400 if (!row->reversed_p && !row->continued_p)
21401 {
21402 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21403 w->cursor.vpos = -1;
21404 return make_number (PT);
21405 }
21406 e = row->glyphs[TEXT_AREA];
21407 g = e + row->used[TEXT_AREA] - 1;
21408 for ( ; g >= e; g--)
21409 {
21410 if (BUFFERP (g->object)
21411 || (ROW_GLYPH_NEWLINE_P (row, g)
21412 && g->charpos > 0)
21413 /* Empty R2L lines on GUI frames have the buffer
21414 position of the newline stored in the stretch
21415 glyph. */
21416 || g->type == STRETCH_GLYPH
21417 || (row->ends_at_zv_p
21418 && row->reversed_p
21419 && NILP (g->object)
21420 && g->type == CHAR_GLYPH
21421 && g->u.ch == ' '))
21422 {
21423 if (g->charpos > 0)
21424 SET_PT (g->charpos);
21425 else if (row->reversed_p
21426 && row->ends_at_zv_p
21427 && PT != ZV)
21428 SET_PT (ZV);
21429 else
21430 continue;
21431 w->cursor.vpos = -1;
21432 return make_number (PT);
21433 }
21434 }
21435 }
21436 }
21437 }
21438
21439 simulate_display:
21440
21441 /* If we wind up here, we failed to move by using the glyphs, so we
21442 need to simulate display instead. */
21443
21444 if (b)
21445 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21446 else
21447 paragraph_dir = Qleft_to_right;
21448 if (EQ (paragraph_dir, Qright_to_left))
21449 dir = -dir;
21450 if (PT <= BEGV && dir < 0)
21451 xsignal0 (Qbeginning_of_buffer);
21452 else if (PT >= ZV && dir > 0)
21453 xsignal0 (Qend_of_buffer);
21454 else
21455 {
21456 struct text_pos pt;
21457 struct it it;
21458 int pt_x, target_x, pixel_width, pt_vpos;
21459 bool at_eol_p;
21460 bool overshoot_expected = false;
21461 bool target_is_eol_p = false;
21462
21463 /* Setup the arena. */
21464 SET_TEXT_POS (pt, PT, PT_BYTE);
21465 start_display (&it, w, pt);
21466 /* When lines are truncated, we could be called with point
21467 outside of the windows edges, in which case move_it_*
21468 functions either prematurely stop at window's edge or jump to
21469 the next screen line, whereas we rely below on our ability to
21470 reach point, in order to start from its X coordinate. So we
21471 need to disregard the window's horizontal extent in that case. */
21472 if (it.line_wrap == TRUNCATE)
21473 it.last_visible_x = INFINITY;
21474
21475 if (it.cmp_it.id < 0
21476 && it.method == GET_FROM_STRING
21477 && it.area == TEXT_AREA
21478 && it.string_from_display_prop_p
21479 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21480 overshoot_expected = true;
21481
21482 /* Find the X coordinate of point. We start from the beginning
21483 of this or previous line to make sure we are before point in
21484 the logical order (since the move_it_* functions can only
21485 move forward). */
21486 reseat:
21487 reseat_at_previous_visible_line_start (&it);
21488 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21489 if (IT_CHARPOS (it) != PT)
21490 {
21491 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21492 -1, -1, -1, MOVE_TO_POS);
21493 /* If we missed point because the character there is
21494 displayed out of a display vector that has more than one
21495 glyph, retry expecting overshoot. */
21496 if (it.method == GET_FROM_DISPLAY_VECTOR
21497 && it.current.dpvec_index > 0
21498 && !overshoot_expected)
21499 {
21500 overshoot_expected = true;
21501 goto reseat;
21502 }
21503 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21504 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21505 }
21506 pt_x = it.current_x;
21507 pt_vpos = it.vpos;
21508 if (dir > 0 || overshoot_expected)
21509 {
21510 struct glyph_row *row = it.glyph_row;
21511
21512 /* When point is at beginning of line, we don't have
21513 information about the glyph there loaded into struct
21514 it. Calling get_next_display_element fixes that. */
21515 if (pt_x == 0)
21516 get_next_display_element (&it);
21517 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21518 it.glyph_row = NULL;
21519 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21520 it.glyph_row = row;
21521 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21522 it, lest it will become out of sync with it's buffer
21523 position. */
21524 it.current_x = pt_x;
21525 }
21526 else
21527 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21528 pixel_width = it.pixel_width;
21529 if (overshoot_expected && at_eol_p)
21530 pixel_width = 0;
21531 else if (pixel_width <= 0)
21532 pixel_width = 1;
21533
21534 /* If there's a display string (or something similar) at point,
21535 we are actually at the glyph to the left of point, so we need
21536 to correct the X coordinate. */
21537 if (overshoot_expected)
21538 {
21539 if (it.bidi_p)
21540 pt_x += pixel_width * it.bidi_it.scan_dir;
21541 else
21542 pt_x += pixel_width;
21543 }
21544
21545 /* Compute target X coordinate, either to the left or to the
21546 right of point. On TTY frames, all characters have the same
21547 pixel width of 1, so we can use that. On GUI frames we don't
21548 have an easy way of getting at the pixel width of the
21549 character to the left of point, so we use a different method
21550 of getting to that place. */
21551 if (dir > 0)
21552 target_x = pt_x + pixel_width;
21553 else
21554 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21555
21556 /* Target X coordinate could be one line above or below the line
21557 of point, in which case we need to adjust the target X
21558 coordinate. Also, if moving to the left, we need to begin at
21559 the left edge of the point's screen line. */
21560 if (dir < 0)
21561 {
21562 if (pt_x > 0)
21563 {
21564 start_display (&it, w, pt);
21565 if (it.line_wrap == TRUNCATE)
21566 it.last_visible_x = INFINITY;
21567 reseat_at_previous_visible_line_start (&it);
21568 it.current_x = it.current_y = it.hpos = 0;
21569 if (pt_vpos != 0)
21570 move_it_by_lines (&it, pt_vpos);
21571 }
21572 else
21573 {
21574 move_it_by_lines (&it, -1);
21575 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21576 target_is_eol_p = true;
21577 /* Under word-wrap, we don't know the x coordinate of
21578 the last character displayed on the previous line,
21579 which immediately precedes the wrap point. To find
21580 out its x coordinate, we try moving to the right
21581 margin of the window, which will stop at the wrap
21582 point, and then reset target_x to point at the
21583 character that precedes the wrap point. This is not
21584 needed on GUI frames, because (see below) there we
21585 move from the left margin one grapheme cluster at a
21586 time, and stop when we hit the wrap point. */
21587 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21588 {
21589 void *it_data = NULL;
21590 struct it it2;
21591
21592 SAVE_IT (it2, it, it_data);
21593 move_it_in_display_line_to (&it, ZV, target_x,
21594 MOVE_TO_POS | MOVE_TO_X);
21595 /* If we arrived at target_x, that _is_ the last
21596 character on the previous line. */
21597 if (it.current_x != target_x)
21598 target_x = it.current_x - 1;
21599 RESTORE_IT (&it, &it2, it_data);
21600 }
21601 }
21602 }
21603 else
21604 {
21605 if (at_eol_p
21606 || (target_x >= it.last_visible_x
21607 && it.line_wrap != TRUNCATE))
21608 {
21609 if (pt_x > 0)
21610 move_it_by_lines (&it, 0);
21611 move_it_by_lines (&it, 1);
21612 target_x = 0;
21613 }
21614 }
21615
21616 /* Move to the target X coordinate. */
21617 #ifdef HAVE_WINDOW_SYSTEM
21618 /* On GUI frames, as we don't know the X coordinate of the
21619 character to the left of point, moving point to the left
21620 requires walking, one grapheme cluster at a time, until we
21621 find ourself at a place immediately to the left of the
21622 character at point. */
21623 if (FRAME_WINDOW_P (it.f) && dir < 0)
21624 {
21625 struct text_pos new_pos;
21626 enum move_it_result rc = MOVE_X_REACHED;
21627
21628 if (it.current_x == 0)
21629 get_next_display_element (&it);
21630 if (it.what == IT_COMPOSITION)
21631 {
21632 new_pos.charpos = it.cmp_it.charpos;
21633 new_pos.bytepos = -1;
21634 }
21635 else
21636 new_pos = it.current.pos;
21637
21638 while (it.current_x + it.pixel_width <= target_x
21639 && (rc == MOVE_X_REACHED
21640 /* Under word-wrap, move_it_in_display_line_to
21641 stops at correct coordinates, but sometimes
21642 returns MOVE_POS_MATCH_OR_ZV. */
21643 || (it.line_wrap == WORD_WRAP
21644 && rc == MOVE_POS_MATCH_OR_ZV)))
21645 {
21646 int new_x = it.current_x + it.pixel_width;
21647
21648 /* For composed characters, we want the position of the
21649 first character in the grapheme cluster (usually, the
21650 composition's base character), whereas it.current
21651 might give us the position of the _last_ one, e.g. if
21652 the composition is rendered in reverse due to bidi
21653 reordering. */
21654 if (it.what == IT_COMPOSITION)
21655 {
21656 new_pos.charpos = it.cmp_it.charpos;
21657 new_pos.bytepos = -1;
21658 }
21659 else
21660 new_pos = it.current.pos;
21661 if (new_x == it.current_x)
21662 new_x++;
21663 rc = move_it_in_display_line_to (&it, ZV, new_x,
21664 MOVE_TO_POS | MOVE_TO_X);
21665 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21666 break;
21667 }
21668 /* The previous position we saw in the loop is the one we
21669 want. */
21670 if (new_pos.bytepos == -1)
21671 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21672 it.current.pos = new_pos;
21673 }
21674 else
21675 #endif
21676 if (it.current_x != target_x)
21677 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21678
21679 /* If we ended up in a display string that covers point, move to
21680 buffer position to the right in the visual order. */
21681 if (dir > 0)
21682 {
21683 while (IT_CHARPOS (it) == PT)
21684 {
21685 set_iterator_to_next (&it, false);
21686 if (!get_next_display_element (&it))
21687 break;
21688 }
21689 }
21690
21691 /* Move point to that position. */
21692 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21693 }
21694
21695 return make_number (PT);
21696
21697 #undef ROW_GLYPH_NEWLINE_P
21698 }
21699
21700 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21701 Sbidi_resolved_levels, 0, 1, 0,
21702 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21703
21704 The resolved levels are produced by the Emacs bidi reordering engine
21705 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21706 read the Unicode Standard Annex 9 (UAX#9) for background information
21707 about these levels.
21708
21709 VPOS is the zero-based number of the current window's screen line
21710 for which to produce the resolved levels. If VPOS is nil or omitted,
21711 it defaults to the screen line of point. If the window displays a
21712 header line, VPOS of zero will report on the header line, and first
21713 line of text in the window will have VPOS of 1.
21714
21715 Value is an array of resolved levels, indexed by glyph number.
21716 Glyphs are numbered from zero starting from the beginning of the
21717 screen line, i.e. the left edge of the window for left-to-right lines
21718 and from the right edge for right-to-left lines. The resolved levels
21719 are produced only for the window's text area; text in display margins
21720 is not included.
21721
21722 If the selected window's display is not up-to-date, or if the specified
21723 screen line does not display text, this function returns nil. It is
21724 highly recommended to bind this function to some simple key, like F8,
21725 in order to avoid these problems.
21726
21727 This function exists mainly for testing the correctness of the
21728 Emacs UBA implementation, in particular with the test suite. */)
21729 (Lisp_Object vpos)
21730 {
21731 struct window *w = XWINDOW (selected_window);
21732 struct buffer *b = XBUFFER (w->contents);
21733 int nrow;
21734 struct glyph_row *row;
21735
21736 if (NILP (vpos))
21737 {
21738 int d1, d2, d3, d4, d5;
21739
21740 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21741 }
21742 else
21743 {
21744 CHECK_NUMBER_COERCE_MARKER (vpos);
21745 nrow = XINT (vpos);
21746 }
21747
21748 /* We require up-to-date glyph matrix for this window. */
21749 if (w->window_end_valid
21750 && !windows_or_buffers_changed
21751 && b
21752 && !b->clip_changed
21753 && !b->prevent_redisplay_optimizations_p
21754 && !window_outdated (w)
21755 && nrow >= 0
21756 && nrow < w->current_matrix->nrows
21757 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21758 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21759 {
21760 struct glyph *g, *e, *g1;
21761 int nglyphs, i;
21762 Lisp_Object levels;
21763
21764 if (!row->reversed_p) /* Left-to-right glyph row. */
21765 {
21766 g = g1 = row->glyphs[TEXT_AREA];
21767 e = g + row->used[TEXT_AREA];
21768
21769 /* Skip over glyphs at the start of the row that was
21770 generated by redisplay for its own needs. */
21771 while (g < e
21772 && NILP (g->object)
21773 && g->charpos < 0)
21774 g++;
21775 g1 = g;
21776
21777 /* Count the "interesting" glyphs in this row. */
21778 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21779 nglyphs++;
21780
21781 /* Create and fill the array. */
21782 levels = make_uninit_vector (nglyphs);
21783 for (i = 0; g1 < g; i++, g1++)
21784 ASET (levels, i, make_number (g1->resolved_level));
21785 }
21786 else /* Right-to-left glyph row. */
21787 {
21788 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21789 e = row->glyphs[TEXT_AREA] - 1;
21790 while (g > e
21791 && NILP (g->object)
21792 && g->charpos < 0)
21793 g--;
21794 g1 = g;
21795 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21796 nglyphs++;
21797 levels = make_uninit_vector (nglyphs);
21798 for (i = 0; g1 > g; i++, g1--)
21799 ASET (levels, i, make_number (g1->resolved_level));
21800 }
21801 return levels;
21802 }
21803 else
21804 return Qnil;
21805 }
21806
21807
21808 \f
21809 /***********************************************************************
21810 Menu Bar
21811 ***********************************************************************/
21812
21813 /* Redisplay the menu bar in the frame for window W.
21814
21815 The menu bar of X frames that don't have X toolkit support is
21816 displayed in a special window W->frame->menu_bar_window.
21817
21818 The menu bar of terminal frames is treated specially as far as
21819 glyph matrices are concerned. Menu bar lines are not part of
21820 windows, so the update is done directly on the frame matrix rows
21821 for the menu bar. */
21822
21823 static void
21824 display_menu_bar (struct window *w)
21825 {
21826 struct frame *f = XFRAME (WINDOW_FRAME (w));
21827 struct it it;
21828 Lisp_Object items;
21829 int i;
21830
21831 /* Don't do all this for graphical frames. */
21832 #ifdef HAVE_NTGUI
21833 if (FRAME_W32_P (f))
21834 return;
21835 #endif
21836 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21837 if (FRAME_X_P (f))
21838 return;
21839 #endif
21840
21841 #ifdef HAVE_NS
21842 if (FRAME_NS_P (f))
21843 return;
21844 #endif /* HAVE_NS */
21845
21846 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21847 eassert (!FRAME_WINDOW_P (f));
21848 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21849 it.first_visible_x = 0;
21850 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21851 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21852 if (FRAME_WINDOW_P (f))
21853 {
21854 /* Menu bar lines are displayed in the desired matrix of the
21855 dummy window menu_bar_window. */
21856 struct window *menu_w;
21857 menu_w = XWINDOW (f->menu_bar_window);
21858 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21859 MENU_FACE_ID);
21860 it.first_visible_x = 0;
21861 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21862 }
21863 else
21864 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21865 {
21866 /* This is a TTY frame, i.e. character hpos/vpos are used as
21867 pixel x/y. */
21868 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21869 MENU_FACE_ID);
21870 it.first_visible_x = 0;
21871 it.last_visible_x = FRAME_COLS (f);
21872 }
21873
21874 /* FIXME: This should be controlled by a user option. See the
21875 comments in redisplay_tool_bar and display_mode_line about
21876 this. */
21877 it.paragraph_embedding = L2R;
21878
21879 /* Clear all rows of the menu bar. */
21880 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21881 {
21882 struct glyph_row *row = it.glyph_row + i;
21883 clear_glyph_row (row);
21884 row->enabled_p = true;
21885 row->full_width_p = true;
21886 row->reversed_p = false;
21887 }
21888
21889 /* Display all items of the menu bar. */
21890 items = FRAME_MENU_BAR_ITEMS (it.f);
21891 for (i = 0; i < ASIZE (items); i += 4)
21892 {
21893 Lisp_Object string;
21894
21895 /* Stop at nil string. */
21896 string = AREF (items, i + 1);
21897 if (NILP (string))
21898 break;
21899
21900 /* Remember where item was displayed. */
21901 ASET (items, i + 3, make_number (it.hpos));
21902
21903 /* Display the item, pad with one space. */
21904 if (it.current_x < it.last_visible_x)
21905 display_string (NULL, string, Qnil, 0, 0, &it,
21906 SCHARS (string) + 1, 0, 0, -1);
21907 }
21908
21909 /* Fill out the line with spaces. */
21910 if (it.current_x < it.last_visible_x)
21911 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21912
21913 /* Compute the total height of the lines. */
21914 compute_line_metrics (&it);
21915 }
21916
21917 /* Deep copy of a glyph row, including the glyphs. */
21918 static void
21919 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21920 {
21921 struct glyph *pointers[1 + LAST_AREA];
21922 int to_used = to->used[TEXT_AREA];
21923
21924 /* Save glyph pointers of TO. */
21925 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21926
21927 /* Do a structure assignment. */
21928 *to = *from;
21929
21930 /* Restore original glyph pointers of TO. */
21931 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21932
21933 /* Copy the glyphs. */
21934 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21935 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21936
21937 /* If we filled only part of the TO row, fill the rest with
21938 space_glyph (which will display as empty space). */
21939 if (to_used > from->used[TEXT_AREA])
21940 fill_up_frame_row_with_spaces (to, to_used);
21941 }
21942
21943 /* Display one menu item on a TTY, by overwriting the glyphs in the
21944 frame F's desired glyph matrix with glyphs produced from the menu
21945 item text. Called from term.c to display TTY drop-down menus one
21946 item at a time.
21947
21948 ITEM_TEXT is the menu item text as a C string.
21949
21950 FACE_ID is the face ID to be used for this menu item. FACE_ID
21951 could specify one of 3 faces: a face for an enabled item, a face
21952 for a disabled item, or a face for a selected item.
21953
21954 X and Y are coordinates of the first glyph in the frame's desired
21955 matrix to be overwritten by the menu item. Since this is a TTY, Y
21956 is the zero-based number of the glyph row and X is the zero-based
21957 glyph number in the row, starting from left, where to start
21958 displaying the item.
21959
21960 SUBMENU means this menu item drops down a submenu, which
21961 should be indicated by displaying a proper visual cue after the
21962 item text. */
21963
21964 void
21965 display_tty_menu_item (const char *item_text, int width, int face_id,
21966 int x, int y, bool submenu)
21967 {
21968 struct it it;
21969 struct frame *f = SELECTED_FRAME ();
21970 struct window *w = XWINDOW (f->selected_window);
21971 struct glyph_row *row;
21972 size_t item_len = strlen (item_text);
21973
21974 eassert (FRAME_TERMCAP_P (f));
21975
21976 /* Don't write beyond the matrix's last row. This can happen for
21977 TTY screens that are not high enough to show the entire menu.
21978 (This is actually a bit of defensive programming, as
21979 tty_menu_display already limits the number of menu items to one
21980 less than the number of screen lines.) */
21981 if (y >= f->desired_matrix->nrows)
21982 return;
21983
21984 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21985 it.first_visible_x = 0;
21986 it.last_visible_x = FRAME_COLS (f) - 1;
21987 row = it.glyph_row;
21988 /* Start with the row contents from the current matrix. */
21989 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21990 bool saved_width = row->full_width_p;
21991 row->full_width_p = true;
21992 bool saved_reversed = row->reversed_p;
21993 row->reversed_p = false;
21994 row->enabled_p = true;
21995
21996 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21997 desired face. */
21998 eassert (x < f->desired_matrix->matrix_w);
21999 it.current_x = it.hpos = x;
22000 it.current_y = it.vpos = y;
22001 int saved_used = row->used[TEXT_AREA];
22002 bool saved_truncated = row->truncated_on_right_p;
22003 row->used[TEXT_AREA] = x;
22004 it.face_id = face_id;
22005 it.line_wrap = TRUNCATE;
22006
22007 /* FIXME: This should be controlled by a user option. See the
22008 comments in redisplay_tool_bar and display_mode_line about this.
22009 Also, if paragraph_embedding could ever be R2L, changes will be
22010 needed to avoid shifting to the right the row characters in
22011 term.c:append_glyph. */
22012 it.paragraph_embedding = L2R;
22013
22014 /* Pad with a space on the left. */
22015 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22016 width--;
22017 /* Display the menu item, pad with spaces to WIDTH. */
22018 if (submenu)
22019 {
22020 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22021 item_len, 0, FRAME_COLS (f) - 1, -1);
22022 width -= item_len;
22023 /* Indicate with " >" that there's a submenu. */
22024 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22025 FRAME_COLS (f) - 1, -1);
22026 }
22027 else
22028 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22029 width, 0, FRAME_COLS (f) - 1, -1);
22030
22031 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22032 row->truncated_on_right_p = saved_truncated;
22033 row->hash = row_hash (row);
22034 row->full_width_p = saved_width;
22035 row->reversed_p = saved_reversed;
22036 }
22037 \f
22038 /***********************************************************************
22039 Mode Line
22040 ***********************************************************************/
22041
22042 /* Redisplay mode lines in the window tree whose root is WINDOW.
22043 If FORCE, redisplay mode lines unconditionally.
22044 Otherwise, redisplay only mode lines that are garbaged. Value is
22045 the number of windows whose mode lines were redisplayed. */
22046
22047 static int
22048 redisplay_mode_lines (Lisp_Object window, bool force)
22049 {
22050 int nwindows = 0;
22051
22052 while (!NILP (window))
22053 {
22054 struct window *w = XWINDOW (window);
22055
22056 if (WINDOWP (w->contents))
22057 nwindows += redisplay_mode_lines (w->contents, force);
22058 else if (force
22059 || FRAME_GARBAGED_P (XFRAME (w->frame))
22060 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22061 {
22062 struct text_pos lpoint;
22063 struct buffer *old = current_buffer;
22064
22065 /* Set the window's buffer for the mode line display. */
22066 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22067 set_buffer_internal_1 (XBUFFER (w->contents));
22068
22069 /* Point refers normally to the selected window. For any
22070 other window, set up appropriate value. */
22071 if (!EQ (window, selected_window))
22072 {
22073 struct text_pos pt;
22074
22075 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22076 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22077 }
22078
22079 /* Display mode lines. */
22080 clear_glyph_matrix (w->desired_matrix);
22081 if (display_mode_lines (w))
22082 ++nwindows;
22083
22084 /* Restore old settings. */
22085 set_buffer_internal_1 (old);
22086 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22087 }
22088
22089 window = w->next;
22090 }
22091
22092 return nwindows;
22093 }
22094
22095
22096 /* Display the mode and/or header line of window W. Value is the
22097 sum number of mode lines and header lines displayed. */
22098
22099 static int
22100 display_mode_lines (struct window *w)
22101 {
22102 Lisp_Object old_selected_window = selected_window;
22103 Lisp_Object old_selected_frame = selected_frame;
22104 Lisp_Object new_frame = w->frame;
22105 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22106 int n = 0;
22107
22108 selected_frame = new_frame;
22109 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22110 or window's point, then we'd need select_window_1 here as well. */
22111 XSETWINDOW (selected_window, w);
22112 XFRAME (new_frame)->selected_window = selected_window;
22113
22114 /* These will be set while the mode line specs are processed. */
22115 line_number_displayed = false;
22116 w->column_number_displayed = -1;
22117
22118 if (WINDOW_WANTS_MODELINE_P (w))
22119 {
22120 struct window *sel_w = XWINDOW (old_selected_window);
22121
22122 /* Select mode line face based on the real selected window. */
22123 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22124 BVAR (current_buffer, mode_line_format));
22125 ++n;
22126 }
22127
22128 if (WINDOW_WANTS_HEADER_LINE_P (w))
22129 {
22130 display_mode_line (w, HEADER_LINE_FACE_ID,
22131 BVAR (current_buffer, header_line_format));
22132 ++n;
22133 }
22134
22135 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22136 selected_frame = old_selected_frame;
22137 selected_window = old_selected_window;
22138 if (n > 0)
22139 w->must_be_updated_p = true;
22140 return n;
22141 }
22142
22143
22144 /* Display mode or header line of window W. FACE_ID specifies which
22145 line to display; it is either MODE_LINE_FACE_ID or
22146 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22147 display. Value is the pixel height of the mode/header line
22148 displayed. */
22149
22150 static int
22151 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22152 {
22153 struct it it;
22154 struct face *face;
22155 ptrdiff_t count = SPECPDL_INDEX ();
22156
22157 init_iterator (&it, w, -1, -1, NULL, face_id);
22158 /* Don't extend on a previously drawn mode-line.
22159 This may happen if called from pos_visible_p. */
22160 it.glyph_row->enabled_p = false;
22161 prepare_desired_row (w, it.glyph_row, true);
22162
22163 it.glyph_row->mode_line_p = true;
22164
22165 /* FIXME: This should be controlled by a user option. But
22166 supporting such an option is not trivial, since the mode line is
22167 made up of many separate strings. */
22168 it.paragraph_embedding = L2R;
22169
22170 record_unwind_protect (unwind_format_mode_line,
22171 format_mode_line_unwind_data (NULL, NULL,
22172 Qnil, false));
22173
22174 mode_line_target = MODE_LINE_DISPLAY;
22175
22176 /* Temporarily make frame's keyboard the current kboard so that
22177 kboard-local variables in the mode_line_format will get the right
22178 values. */
22179 push_kboard (FRAME_KBOARD (it.f));
22180 record_unwind_save_match_data ();
22181 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22182 pop_kboard ();
22183
22184 unbind_to (count, Qnil);
22185
22186 /* Fill up with spaces. */
22187 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22188
22189 compute_line_metrics (&it);
22190 it.glyph_row->full_width_p = true;
22191 it.glyph_row->continued_p = false;
22192 it.glyph_row->truncated_on_left_p = false;
22193 it.glyph_row->truncated_on_right_p = false;
22194
22195 /* Make a 3D mode-line have a shadow at its right end. */
22196 face = FACE_FROM_ID (it.f, face_id);
22197 extend_face_to_end_of_line (&it);
22198 if (face->box != FACE_NO_BOX)
22199 {
22200 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22201 + it.glyph_row->used[TEXT_AREA] - 1);
22202 last->right_box_line_p = true;
22203 }
22204
22205 return it.glyph_row->height;
22206 }
22207
22208 /* Move element ELT in LIST to the front of LIST.
22209 Return the updated list. */
22210
22211 static Lisp_Object
22212 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22213 {
22214 register Lisp_Object tail, prev;
22215 register Lisp_Object tem;
22216
22217 tail = list;
22218 prev = Qnil;
22219 while (CONSP (tail))
22220 {
22221 tem = XCAR (tail);
22222
22223 if (EQ (elt, tem))
22224 {
22225 /* Splice out the link TAIL. */
22226 if (NILP (prev))
22227 list = XCDR (tail);
22228 else
22229 Fsetcdr (prev, XCDR (tail));
22230
22231 /* Now make it the first. */
22232 Fsetcdr (tail, list);
22233 return tail;
22234 }
22235 else
22236 prev = tail;
22237 tail = XCDR (tail);
22238 QUIT;
22239 }
22240
22241 /* Not found--return unchanged LIST. */
22242 return list;
22243 }
22244
22245 /* Contribute ELT to the mode line for window IT->w. How it
22246 translates into text depends on its data type.
22247
22248 IT describes the display environment in which we display, as usual.
22249
22250 DEPTH is the depth in recursion. It is used to prevent
22251 infinite recursion here.
22252
22253 FIELD_WIDTH is the number of characters the display of ELT should
22254 occupy in the mode line, and PRECISION is the maximum number of
22255 characters to display from ELT's representation. See
22256 display_string for details.
22257
22258 Returns the hpos of the end of the text generated by ELT.
22259
22260 PROPS is a property list to add to any string we encounter.
22261
22262 If RISKY, remove (disregard) any properties in any string
22263 we encounter, and ignore :eval and :propertize.
22264
22265 The global variable `mode_line_target' determines whether the
22266 output is passed to `store_mode_line_noprop',
22267 `store_mode_line_string', or `display_string'. */
22268
22269 static int
22270 display_mode_element (struct it *it, int depth, int field_width, int precision,
22271 Lisp_Object elt, Lisp_Object props, bool risky)
22272 {
22273 int n = 0, field, prec;
22274 bool literal = false;
22275
22276 tail_recurse:
22277 if (depth > 100)
22278 elt = build_string ("*too-deep*");
22279
22280 depth++;
22281
22282 switch (XTYPE (elt))
22283 {
22284 case Lisp_String:
22285 {
22286 /* A string: output it and check for %-constructs within it. */
22287 unsigned char c;
22288 ptrdiff_t offset = 0;
22289
22290 if (SCHARS (elt) > 0
22291 && (!NILP (props) || risky))
22292 {
22293 Lisp_Object oprops, aelt;
22294 oprops = Ftext_properties_at (make_number (0), elt);
22295
22296 /* If the starting string's properties are not what
22297 we want, translate the string. Also, if the string
22298 is risky, do that anyway. */
22299
22300 if (NILP (Fequal (props, oprops)) || risky)
22301 {
22302 /* If the starting string has properties,
22303 merge the specified ones onto the existing ones. */
22304 if (! NILP (oprops) && !risky)
22305 {
22306 Lisp_Object tem;
22307
22308 oprops = Fcopy_sequence (oprops);
22309 tem = props;
22310 while (CONSP (tem))
22311 {
22312 oprops = Fplist_put (oprops, XCAR (tem),
22313 XCAR (XCDR (tem)));
22314 tem = XCDR (XCDR (tem));
22315 }
22316 props = oprops;
22317 }
22318
22319 aelt = Fassoc (elt, mode_line_proptrans_alist);
22320 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22321 {
22322 /* AELT is what we want. Move it to the front
22323 without consing. */
22324 elt = XCAR (aelt);
22325 mode_line_proptrans_alist
22326 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22327 }
22328 else
22329 {
22330 Lisp_Object tem;
22331
22332 /* If AELT has the wrong props, it is useless.
22333 so get rid of it. */
22334 if (! NILP (aelt))
22335 mode_line_proptrans_alist
22336 = Fdelq (aelt, mode_line_proptrans_alist);
22337
22338 elt = Fcopy_sequence (elt);
22339 Fset_text_properties (make_number (0), Flength (elt),
22340 props, elt);
22341 /* Add this item to mode_line_proptrans_alist. */
22342 mode_line_proptrans_alist
22343 = Fcons (Fcons (elt, props),
22344 mode_line_proptrans_alist);
22345 /* Truncate mode_line_proptrans_alist
22346 to at most 50 elements. */
22347 tem = Fnthcdr (make_number (50),
22348 mode_line_proptrans_alist);
22349 if (! NILP (tem))
22350 XSETCDR (tem, Qnil);
22351 }
22352 }
22353 }
22354
22355 offset = 0;
22356
22357 if (literal)
22358 {
22359 prec = precision - n;
22360 switch (mode_line_target)
22361 {
22362 case MODE_LINE_NOPROP:
22363 case MODE_LINE_TITLE:
22364 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22365 break;
22366 case MODE_LINE_STRING:
22367 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22368 break;
22369 case MODE_LINE_DISPLAY:
22370 n += display_string (NULL, elt, Qnil, 0, 0, it,
22371 0, prec, 0, STRING_MULTIBYTE (elt));
22372 break;
22373 }
22374
22375 break;
22376 }
22377
22378 /* Handle the non-literal case. */
22379
22380 while ((precision <= 0 || n < precision)
22381 && SREF (elt, offset) != 0
22382 && (mode_line_target != MODE_LINE_DISPLAY
22383 || it->current_x < it->last_visible_x))
22384 {
22385 ptrdiff_t last_offset = offset;
22386
22387 /* Advance to end of string or next format specifier. */
22388 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22389 ;
22390
22391 if (offset - 1 != last_offset)
22392 {
22393 ptrdiff_t nchars, nbytes;
22394
22395 /* Output to end of string or up to '%'. Field width
22396 is length of string. Don't output more than
22397 PRECISION allows us. */
22398 offset--;
22399
22400 prec = c_string_width (SDATA (elt) + last_offset,
22401 offset - last_offset, precision - n,
22402 &nchars, &nbytes);
22403
22404 switch (mode_line_target)
22405 {
22406 case MODE_LINE_NOPROP:
22407 case MODE_LINE_TITLE:
22408 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22409 break;
22410 case MODE_LINE_STRING:
22411 {
22412 ptrdiff_t bytepos = last_offset;
22413 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22414 ptrdiff_t endpos = (precision <= 0
22415 ? string_byte_to_char (elt, offset)
22416 : charpos + nchars);
22417 Lisp_Object mode_string
22418 = Fsubstring (elt, make_number (charpos),
22419 make_number (endpos));
22420 n += store_mode_line_string (NULL, mode_string, false,
22421 0, 0, Qnil);
22422 }
22423 break;
22424 case MODE_LINE_DISPLAY:
22425 {
22426 ptrdiff_t bytepos = last_offset;
22427 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22428
22429 if (precision <= 0)
22430 nchars = string_byte_to_char (elt, offset) - charpos;
22431 n += display_string (NULL, elt, Qnil, 0, charpos,
22432 it, 0, nchars, 0,
22433 STRING_MULTIBYTE (elt));
22434 }
22435 break;
22436 }
22437 }
22438 else /* c == '%' */
22439 {
22440 ptrdiff_t percent_position = offset;
22441
22442 /* Get the specified minimum width. Zero means
22443 don't pad. */
22444 field = 0;
22445 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22446 field = field * 10 + c - '0';
22447
22448 /* Don't pad beyond the total padding allowed. */
22449 if (field_width - n > 0 && field > field_width - n)
22450 field = field_width - n;
22451
22452 /* Note that either PRECISION <= 0 or N < PRECISION. */
22453 prec = precision - n;
22454
22455 if (c == 'M')
22456 n += display_mode_element (it, depth, field, prec,
22457 Vglobal_mode_string, props,
22458 risky);
22459 else if (c != 0)
22460 {
22461 bool multibyte;
22462 ptrdiff_t bytepos, charpos;
22463 const char *spec;
22464 Lisp_Object string;
22465
22466 bytepos = percent_position;
22467 charpos = (STRING_MULTIBYTE (elt)
22468 ? string_byte_to_char (elt, bytepos)
22469 : bytepos);
22470 spec = decode_mode_spec (it->w, c, field, &string);
22471 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22472
22473 switch (mode_line_target)
22474 {
22475 case MODE_LINE_NOPROP:
22476 case MODE_LINE_TITLE:
22477 n += store_mode_line_noprop (spec, field, prec);
22478 break;
22479 case MODE_LINE_STRING:
22480 {
22481 Lisp_Object tem = build_string (spec);
22482 props = Ftext_properties_at (make_number (charpos), elt);
22483 /* Should only keep face property in props */
22484 n += store_mode_line_string (NULL, tem, false,
22485 field, prec, props);
22486 }
22487 break;
22488 case MODE_LINE_DISPLAY:
22489 {
22490 int nglyphs_before, nwritten;
22491
22492 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22493 nwritten = display_string (spec, string, elt,
22494 charpos, 0, it,
22495 field, prec, 0,
22496 multibyte);
22497
22498 /* Assign to the glyphs written above the
22499 string where the `%x' came from, position
22500 of the `%'. */
22501 if (nwritten > 0)
22502 {
22503 struct glyph *glyph
22504 = (it->glyph_row->glyphs[TEXT_AREA]
22505 + nglyphs_before);
22506 int i;
22507
22508 for (i = 0; i < nwritten; ++i)
22509 {
22510 glyph[i].object = elt;
22511 glyph[i].charpos = charpos;
22512 }
22513
22514 n += nwritten;
22515 }
22516 }
22517 break;
22518 }
22519 }
22520 else /* c == 0 */
22521 break;
22522 }
22523 }
22524 }
22525 break;
22526
22527 case Lisp_Symbol:
22528 /* A symbol: process the value of the symbol recursively
22529 as if it appeared here directly. Avoid error if symbol void.
22530 Special case: if value of symbol is a string, output the string
22531 literally. */
22532 {
22533 register Lisp_Object tem;
22534
22535 /* If the variable is not marked as risky to set
22536 then its contents are risky to use. */
22537 if (NILP (Fget (elt, Qrisky_local_variable)))
22538 risky = true;
22539
22540 tem = Fboundp (elt);
22541 if (!NILP (tem))
22542 {
22543 tem = Fsymbol_value (elt);
22544 /* If value is a string, output that string literally:
22545 don't check for % within it. */
22546 if (STRINGP (tem))
22547 literal = true;
22548
22549 if (!EQ (tem, elt))
22550 {
22551 /* Give up right away for nil or t. */
22552 elt = tem;
22553 goto tail_recurse;
22554 }
22555 }
22556 }
22557 break;
22558
22559 case Lisp_Cons:
22560 {
22561 register Lisp_Object car, tem;
22562
22563 /* A cons cell: five distinct cases.
22564 If first element is :eval or :propertize, do something special.
22565 If first element is a string or a cons, process all the elements
22566 and effectively concatenate them.
22567 If first element is a negative number, truncate displaying cdr to
22568 at most that many characters. If positive, pad (with spaces)
22569 to at least that many characters.
22570 If first element is a symbol, process the cadr or caddr recursively
22571 according to whether the symbol's value is non-nil or nil. */
22572 car = XCAR (elt);
22573 if (EQ (car, QCeval))
22574 {
22575 /* An element of the form (:eval FORM) means evaluate FORM
22576 and use the result as mode line elements. */
22577
22578 if (risky)
22579 break;
22580
22581 if (CONSP (XCDR (elt)))
22582 {
22583 Lisp_Object spec;
22584 spec = safe__eval (true, XCAR (XCDR (elt)));
22585 n += display_mode_element (it, depth, field_width - n,
22586 precision - n, spec, props,
22587 risky);
22588 }
22589 }
22590 else if (EQ (car, QCpropertize))
22591 {
22592 /* An element of the form (:propertize ELT PROPS...)
22593 means display ELT but applying properties PROPS. */
22594
22595 if (risky)
22596 break;
22597
22598 if (CONSP (XCDR (elt)))
22599 n += display_mode_element (it, depth, field_width - n,
22600 precision - n, XCAR (XCDR (elt)),
22601 XCDR (XCDR (elt)), risky);
22602 }
22603 else if (SYMBOLP (car))
22604 {
22605 tem = Fboundp (car);
22606 elt = XCDR (elt);
22607 if (!CONSP (elt))
22608 goto invalid;
22609 /* elt is now the cdr, and we know it is a cons cell.
22610 Use its car if CAR has a non-nil value. */
22611 if (!NILP (tem))
22612 {
22613 tem = Fsymbol_value (car);
22614 if (!NILP (tem))
22615 {
22616 elt = XCAR (elt);
22617 goto tail_recurse;
22618 }
22619 }
22620 /* Symbol's value is nil (or symbol is unbound)
22621 Get the cddr of the original list
22622 and if possible find the caddr and use that. */
22623 elt = XCDR (elt);
22624 if (NILP (elt))
22625 break;
22626 else if (!CONSP (elt))
22627 goto invalid;
22628 elt = XCAR (elt);
22629 goto tail_recurse;
22630 }
22631 else if (INTEGERP (car))
22632 {
22633 register int lim = XINT (car);
22634 elt = XCDR (elt);
22635 if (lim < 0)
22636 {
22637 /* Negative int means reduce maximum width. */
22638 if (precision <= 0)
22639 precision = -lim;
22640 else
22641 precision = min (precision, -lim);
22642 }
22643 else if (lim > 0)
22644 {
22645 /* Padding specified. Don't let it be more than
22646 current maximum. */
22647 if (precision > 0)
22648 lim = min (precision, lim);
22649
22650 /* If that's more padding than already wanted, queue it.
22651 But don't reduce padding already specified even if
22652 that is beyond the current truncation point. */
22653 field_width = max (lim, field_width);
22654 }
22655 goto tail_recurse;
22656 }
22657 else if (STRINGP (car) || CONSP (car))
22658 {
22659 Lisp_Object halftail = elt;
22660 int len = 0;
22661
22662 while (CONSP (elt)
22663 && (precision <= 0 || n < precision))
22664 {
22665 n += display_mode_element (it, depth,
22666 /* Do padding only after the last
22667 element in the list. */
22668 (! CONSP (XCDR (elt))
22669 ? field_width - n
22670 : 0),
22671 precision - n, XCAR (elt),
22672 props, risky);
22673 elt = XCDR (elt);
22674 len++;
22675 if ((len & 1) == 0)
22676 halftail = XCDR (halftail);
22677 /* Check for cycle. */
22678 if (EQ (halftail, elt))
22679 break;
22680 }
22681 }
22682 }
22683 break;
22684
22685 default:
22686 invalid:
22687 elt = build_string ("*invalid*");
22688 goto tail_recurse;
22689 }
22690
22691 /* Pad to FIELD_WIDTH. */
22692 if (field_width > 0 && n < field_width)
22693 {
22694 switch (mode_line_target)
22695 {
22696 case MODE_LINE_NOPROP:
22697 case MODE_LINE_TITLE:
22698 n += store_mode_line_noprop ("", field_width - n, 0);
22699 break;
22700 case MODE_LINE_STRING:
22701 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22702 Qnil);
22703 break;
22704 case MODE_LINE_DISPLAY:
22705 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22706 0, 0, 0);
22707 break;
22708 }
22709 }
22710
22711 return n;
22712 }
22713
22714 /* Store a mode-line string element in mode_line_string_list.
22715
22716 If STRING is non-null, display that C string. Otherwise, the Lisp
22717 string LISP_STRING is displayed.
22718
22719 FIELD_WIDTH is the minimum number of output glyphs to produce.
22720 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22721 with spaces. FIELD_WIDTH <= 0 means don't pad.
22722
22723 PRECISION is the maximum number of characters to output from
22724 STRING. PRECISION <= 0 means don't truncate the string.
22725
22726 If COPY_STRING, make a copy of LISP_STRING before adding
22727 properties to the string.
22728
22729 PROPS are the properties to add to the string.
22730 The mode_line_string_face face property is always added to the string.
22731 */
22732
22733 static int
22734 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22735 bool copy_string,
22736 int field_width, int precision, Lisp_Object props)
22737 {
22738 ptrdiff_t len;
22739 int n = 0;
22740
22741 if (string != NULL)
22742 {
22743 len = strlen (string);
22744 if (precision > 0 && len > precision)
22745 len = precision;
22746 lisp_string = make_string (string, len);
22747 if (NILP (props))
22748 props = mode_line_string_face_prop;
22749 else if (!NILP (mode_line_string_face))
22750 {
22751 Lisp_Object face = Fplist_get (props, Qface);
22752 props = Fcopy_sequence (props);
22753 if (NILP (face))
22754 face = mode_line_string_face;
22755 else
22756 face = list2 (face, mode_line_string_face);
22757 props = Fplist_put (props, Qface, face);
22758 }
22759 Fadd_text_properties (make_number (0), make_number (len),
22760 props, lisp_string);
22761 }
22762 else
22763 {
22764 len = XFASTINT (Flength (lisp_string));
22765 if (precision > 0 && len > precision)
22766 {
22767 len = precision;
22768 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22769 precision = -1;
22770 }
22771 if (!NILP (mode_line_string_face))
22772 {
22773 Lisp_Object face;
22774 if (NILP (props))
22775 props = Ftext_properties_at (make_number (0), lisp_string);
22776 face = Fplist_get (props, Qface);
22777 if (NILP (face))
22778 face = mode_line_string_face;
22779 else
22780 face = list2 (face, mode_line_string_face);
22781 props = list2 (Qface, face);
22782 if (copy_string)
22783 lisp_string = Fcopy_sequence (lisp_string);
22784 }
22785 if (!NILP (props))
22786 Fadd_text_properties (make_number (0), make_number (len),
22787 props, lisp_string);
22788 }
22789
22790 if (len > 0)
22791 {
22792 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22793 n += len;
22794 }
22795
22796 if (field_width > len)
22797 {
22798 field_width -= len;
22799 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22800 if (!NILP (props))
22801 Fadd_text_properties (make_number (0), make_number (field_width),
22802 props, lisp_string);
22803 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22804 n += field_width;
22805 }
22806
22807 return n;
22808 }
22809
22810
22811 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22812 1, 4, 0,
22813 doc: /* Format a string out of a mode line format specification.
22814 First arg FORMAT specifies the mode line format (see `mode-line-format'
22815 for details) to use.
22816
22817 By default, the format is evaluated for the currently selected window.
22818
22819 Optional second arg FACE specifies the face property to put on all
22820 characters for which no face is specified. The value nil means the
22821 default face. The value t means whatever face the window's mode line
22822 currently uses (either `mode-line' or `mode-line-inactive',
22823 depending on whether the window is the selected window or not).
22824 An integer value means the value string has no text
22825 properties.
22826
22827 Optional third and fourth args WINDOW and BUFFER specify the window
22828 and buffer to use as the context for the formatting (defaults
22829 are the selected window and the WINDOW's buffer). */)
22830 (Lisp_Object format, Lisp_Object face,
22831 Lisp_Object window, Lisp_Object buffer)
22832 {
22833 struct it it;
22834 int len;
22835 struct window *w;
22836 struct buffer *old_buffer = NULL;
22837 int face_id;
22838 bool no_props = INTEGERP (face);
22839 ptrdiff_t count = SPECPDL_INDEX ();
22840 Lisp_Object str;
22841 int string_start = 0;
22842
22843 w = decode_any_window (window);
22844 XSETWINDOW (window, w);
22845
22846 if (NILP (buffer))
22847 buffer = w->contents;
22848 CHECK_BUFFER (buffer);
22849
22850 /* Make formatting the modeline a non-op when noninteractive, otherwise
22851 there will be problems later caused by a partially initialized frame. */
22852 if (NILP (format) || noninteractive)
22853 return empty_unibyte_string;
22854
22855 if (no_props)
22856 face = Qnil;
22857
22858 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22859 : EQ (face, Qt) ? (EQ (window, selected_window)
22860 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22861 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22862 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22863 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22864 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22865 : DEFAULT_FACE_ID;
22866
22867 old_buffer = current_buffer;
22868
22869 /* Save things including mode_line_proptrans_alist,
22870 and set that to nil so that we don't alter the outer value. */
22871 record_unwind_protect (unwind_format_mode_line,
22872 format_mode_line_unwind_data
22873 (XFRAME (WINDOW_FRAME (w)),
22874 old_buffer, selected_window, true));
22875 mode_line_proptrans_alist = Qnil;
22876
22877 Fselect_window (window, Qt);
22878 set_buffer_internal_1 (XBUFFER (buffer));
22879
22880 init_iterator (&it, w, -1, -1, NULL, face_id);
22881
22882 if (no_props)
22883 {
22884 mode_line_target = MODE_LINE_NOPROP;
22885 mode_line_string_face_prop = Qnil;
22886 mode_line_string_list = Qnil;
22887 string_start = MODE_LINE_NOPROP_LEN (0);
22888 }
22889 else
22890 {
22891 mode_line_target = MODE_LINE_STRING;
22892 mode_line_string_list = Qnil;
22893 mode_line_string_face = face;
22894 mode_line_string_face_prop
22895 = NILP (face) ? Qnil : list2 (Qface, face);
22896 }
22897
22898 push_kboard (FRAME_KBOARD (it.f));
22899 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22900 pop_kboard ();
22901
22902 if (no_props)
22903 {
22904 len = MODE_LINE_NOPROP_LEN (string_start);
22905 str = make_string (mode_line_noprop_buf + string_start, len);
22906 }
22907 else
22908 {
22909 mode_line_string_list = Fnreverse (mode_line_string_list);
22910 str = Fmapconcat (Qidentity, mode_line_string_list,
22911 empty_unibyte_string);
22912 }
22913
22914 unbind_to (count, Qnil);
22915 return str;
22916 }
22917
22918 /* Write a null-terminated, right justified decimal representation of
22919 the positive integer D to BUF using a minimal field width WIDTH. */
22920
22921 static void
22922 pint2str (register char *buf, register int width, register ptrdiff_t d)
22923 {
22924 register char *p = buf;
22925
22926 if (d <= 0)
22927 *p++ = '0';
22928 else
22929 {
22930 while (d > 0)
22931 {
22932 *p++ = d % 10 + '0';
22933 d /= 10;
22934 }
22935 }
22936
22937 for (width -= (int) (p - buf); width > 0; --width)
22938 *p++ = ' ';
22939 *p-- = '\0';
22940 while (p > buf)
22941 {
22942 d = *buf;
22943 *buf++ = *p;
22944 *p-- = d;
22945 }
22946 }
22947
22948 /* Write a null-terminated, right justified decimal and "human
22949 readable" representation of the nonnegative integer D to BUF using
22950 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22951
22952 static const char power_letter[] =
22953 {
22954 0, /* no letter */
22955 'k', /* kilo */
22956 'M', /* mega */
22957 'G', /* giga */
22958 'T', /* tera */
22959 'P', /* peta */
22960 'E', /* exa */
22961 'Z', /* zetta */
22962 'Y' /* yotta */
22963 };
22964
22965 static void
22966 pint2hrstr (char *buf, int width, ptrdiff_t d)
22967 {
22968 /* We aim to represent the nonnegative integer D as
22969 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22970 ptrdiff_t quotient = d;
22971 int remainder = 0;
22972 /* -1 means: do not use TENTHS. */
22973 int tenths = -1;
22974 int exponent = 0;
22975
22976 /* Length of QUOTIENT.TENTHS as a string. */
22977 int length;
22978
22979 char * psuffix;
22980 char * p;
22981
22982 if (quotient >= 1000)
22983 {
22984 /* Scale to the appropriate EXPONENT. */
22985 do
22986 {
22987 remainder = quotient % 1000;
22988 quotient /= 1000;
22989 exponent++;
22990 }
22991 while (quotient >= 1000);
22992
22993 /* Round to nearest and decide whether to use TENTHS or not. */
22994 if (quotient <= 9)
22995 {
22996 tenths = remainder / 100;
22997 if (remainder % 100 >= 50)
22998 {
22999 if (tenths < 9)
23000 tenths++;
23001 else
23002 {
23003 quotient++;
23004 if (quotient == 10)
23005 tenths = -1;
23006 else
23007 tenths = 0;
23008 }
23009 }
23010 }
23011 else
23012 if (remainder >= 500)
23013 {
23014 if (quotient < 999)
23015 quotient++;
23016 else
23017 {
23018 quotient = 1;
23019 exponent++;
23020 tenths = 0;
23021 }
23022 }
23023 }
23024
23025 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23026 if (tenths == -1 && quotient <= 99)
23027 if (quotient <= 9)
23028 length = 1;
23029 else
23030 length = 2;
23031 else
23032 length = 3;
23033 p = psuffix = buf + max (width, length);
23034
23035 /* Print EXPONENT. */
23036 *psuffix++ = power_letter[exponent];
23037 *psuffix = '\0';
23038
23039 /* Print TENTHS. */
23040 if (tenths >= 0)
23041 {
23042 *--p = '0' + tenths;
23043 *--p = '.';
23044 }
23045
23046 /* Print QUOTIENT. */
23047 do
23048 {
23049 int digit = quotient % 10;
23050 *--p = '0' + digit;
23051 }
23052 while ((quotient /= 10) != 0);
23053
23054 /* Print leading spaces. */
23055 while (buf < p)
23056 *--p = ' ';
23057 }
23058
23059 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23060 If EOL_FLAG, set also a mnemonic character for end-of-line
23061 type of CODING_SYSTEM. Return updated pointer into BUF. */
23062
23063 static unsigned char invalid_eol_type[] = "(*invalid*)";
23064
23065 static char *
23066 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23067 {
23068 Lisp_Object val;
23069 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23070 const unsigned char *eol_str;
23071 int eol_str_len;
23072 /* The EOL conversion we are using. */
23073 Lisp_Object eoltype;
23074
23075 val = CODING_SYSTEM_SPEC (coding_system);
23076 eoltype = Qnil;
23077
23078 if (!VECTORP (val)) /* Not yet decided. */
23079 {
23080 *buf++ = multibyte ? '-' : ' ';
23081 if (eol_flag)
23082 eoltype = eol_mnemonic_undecided;
23083 /* Don't mention EOL conversion if it isn't decided. */
23084 }
23085 else
23086 {
23087 Lisp_Object attrs;
23088 Lisp_Object eolvalue;
23089
23090 attrs = AREF (val, 0);
23091 eolvalue = AREF (val, 2);
23092
23093 *buf++ = multibyte
23094 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23095 : ' ';
23096
23097 if (eol_flag)
23098 {
23099 /* The EOL conversion that is normal on this system. */
23100
23101 if (NILP (eolvalue)) /* Not yet decided. */
23102 eoltype = eol_mnemonic_undecided;
23103 else if (VECTORP (eolvalue)) /* Not yet decided. */
23104 eoltype = eol_mnemonic_undecided;
23105 else /* eolvalue is Qunix, Qdos, or Qmac. */
23106 eoltype = (EQ (eolvalue, Qunix)
23107 ? eol_mnemonic_unix
23108 : EQ (eolvalue, Qdos)
23109 ? eol_mnemonic_dos : eol_mnemonic_mac);
23110 }
23111 }
23112
23113 if (eol_flag)
23114 {
23115 /* Mention the EOL conversion if it is not the usual one. */
23116 if (STRINGP (eoltype))
23117 {
23118 eol_str = SDATA (eoltype);
23119 eol_str_len = SBYTES (eoltype);
23120 }
23121 else if (CHARACTERP (eoltype))
23122 {
23123 int c = XFASTINT (eoltype);
23124 return buf + CHAR_STRING (c, (unsigned char *) buf);
23125 }
23126 else
23127 {
23128 eol_str = invalid_eol_type;
23129 eol_str_len = sizeof (invalid_eol_type) - 1;
23130 }
23131 memcpy (buf, eol_str, eol_str_len);
23132 buf += eol_str_len;
23133 }
23134
23135 return buf;
23136 }
23137
23138 /* Return a string for the output of a mode line %-spec for window W,
23139 generated by character C. FIELD_WIDTH > 0 means pad the string
23140 returned with spaces to that value. Return a Lisp string in
23141 *STRING if the resulting string is taken from that Lisp string.
23142
23143 Note we operate on the current buffer for most purposes. */
23144
23145 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23146
23147 static const char *
23148 decode_mode_spec (struct window *w, register int c, int field_width,
23149 Lisp_Object *string)
23150 {
23151 Lisp_Object obj;
23152 struct frame *f = XFRAME (WINDOW_FRAME (w));
23153 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23154 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23155 produce strings from numerical values, so limit preposterously
23156 large values of FIELD_WIDTH to avoid overrunning the buffer's
23157 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23158 bytes plus the terminating null. */
23159 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23160 struct buffer *b = current_buffer;
23161
23162 obj = Qnil;
23163 *string = Qnil;
23164
23165 switch (c)
23166 {
23167 case '*':
23168 if (!NILP (BVAR (b, read_only)))
23169 return "%";
23170 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23171 return "*";
23172 return "-";
23173
23174 case '+':
23175 /* This differs from %* only for a modified read-only buffer. */
23176 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23177 return "*";
23178 if (!NILP (BVAR (b, read_only)))
23179 return "%";
23180 return "-";
23181
23182 case '&':
23183 /* This differs from %* in ignoring read-only-ness. */
23184 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23185 return "*";
23186 return "-";
23187
23188 case '%':
23189 return "%";
23190
23191 case '[':
23192 {
23193 int i;
23194 char *p;
23195
23196 if (command_loop_level > 5)
23197 return "[[[... ";
23198 p = decode_mode_spec_buf;
23199 for (i = 0; i < command_loop_level; i++)
23200 *p++ = '[';
23201 *p = 0;
23202 return decode_mode_spec_buf;
23203 }
23204
23205 case ']':
23206 {
23207 int i;
23208 char *p;
23209
23210 if (command_loop_level > 5)
23211 return " ...]]]";
23212 p = decode_mode_spec_buf;
23213 for (i = 0; i < command_loop_level; i++)
23214 *p++ = ']';
23215 *p = 0;
23216 return decode_mode_spec_buf;
23217 }
23218
23219 case '-':
23220 {
23221 register int i;
23222
23223 /* Let lots_of_dashes be a string of infinite length. */
23224 if (mode_line_target == MODE_LINE_NOPROP
23225 || mode_line_target == MODE_LINE_STRING)
23226 return "--";
23227 if (field_width <= 0
23228 || field_width > sizeof (lots_of_dashes))
23229 {
23230 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23231 decode_mode_spec_buf[i] = '-';
23232 decode_mode_spec_buf[i] = '\0';
23233 return decode_mode_spec_buf;
23234 }
23235 else
23236 return lots_of_dashes;
23237 }
23238
23239 case 'b':
23240 obj = BVAR (b, name);
23241 break;
23242
23243 case 'c':
23244 /* %c and %l are ignored in `frame-title-format'.
23245 (In redisplay_internal, the frame title is drawn _before_ the
23246 windows are updated, so the stuff which depends on actual
23247 window contents (such as %l) may fail to render properly, or
23248 even crash emacs.) */
23249 if (mode_line_target == MODE_LINE_TITLE)
23250 return "";
23251 else
23252 {
23253 ptrdiff_t col = current_column ();
23254 w->column_number_displayed = col;
23255 pint2str (decode_mode_spec_buf, width, col);
23256 return decode_mode_spec_buf;
23257 }
23258
23259 case 'e':
23260 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23261 {
23262 if (NILP (Vmemory_full))
23263 return "";
23264 else
23265 return "!MEM FULL! ";
23266 }
23267 #else
23268 return "";
23269 #endif
23270
23271 case 'F':
23272 /* %F displays the frame name. */
23273 if (!NILP (f->title))
23274 return SSDATA (f->title);
23275 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23276 return SSDATA (f->name);
23277 return "Emacs";
23278
23279 case 'f':
23280 obj = BVAR (b, filename);
23281 break;
23282
23283 case 'i':
23284 {
23285 ptrdiff_t size = ZV - BEGV;
23286 pint2str (decode_mode_spec_buf, width, size);
23287 return decode_mode_spec_buf;
23288 }
23289
23290 case 'I':
23291 {
23292 ptrdiff_t size = ZV - BEGV;
23293 pint2hrstr (decode_mode_spec_buf, width, size);
23294 return decode_mode_spec_buf;
23295 }
23296
23297 case 'l':
23298 {
23299 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23300 ptrdiff_t topline, nlines, height;
23301 ptrdiff_t junk;
23302
23303 /* %c and %l are ignored in `frame-title-format'. */
23304 if (mode_line_target == MODE_LINE_TITLE)
23305 return "";
23306
23307 startpos = marker_position (w->start);
23308 startpos_byte = marker_byte_position (w->start);
23309 height = WINDOW_TOTAL_LINES (w);
23310
23311 /* If we decided that this buffer isn't suitable for line numbers,
23312 don't forget that too fast. */
23313 if (w->base_line_pos == -1)
23314 goto no_value;
23315
23316 /* If the buffer is very big, don't waste time. */
23317 if (INTEGERP (Vline_number_display_limit)
23318 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23319 {
23320 w->base_line_pos = 0;
23321 w->base_line_number = 0;
23322 goto no_value;
23323 }
23324
23325 if (w->base_line_number > 0
23326 && w->base_line_pos > 0
23327 && w->base_line_pos <= startpos)
23328 {
23329 line = w->base_line_number;
23330 linepos = w->base_line_pos;
23331 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23332 }
23333 else
23334 {
23335 line = 1;
23336 linepos = BUF_BEGV (b);
23337 linepos_byte = BUF_BEGV_BYTE (b);
23338 }
23339
23340 /* Count lines from base line to window start position. */
23341 nlines = display_count_lines (linepos_byte,
23342 startpos_byte,
23343 startpos, &junk);
23344
23345 topline = nlines + line;
23346
23347 /* Determine a new base line, if the old one is too close
23348 or too far away, or if we did not have one.
23349 "Too close" means it's plausible a scroll-down would
23350 go back past it. */
23351 if (startpos == BUF_BEGV (b))
23352 {
23353 w->base_line_number = topline;
23354 w->base_line_pos = BUF_BEGV (b);
23355 }
23356 else if (nlines < height + 25 || nlines > height * 3 + 50
23357 || linepos == BUF_BEGV (b))
23358 {
23359 ptrdiff_t limit = BUF_BEGV (b);
23360 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23361 ptrdiff_t position;
23362 ptrdiff_t distance =
23363 (height * 2 + 30) * line_number_display_limit_width;
23364
23365 if (startpos - distance > limit)
23366 {
23367 limit = startpos - distance;
23368 limit_byte = CHAR_TO_BYTE (limit);
23369 }
23370
23371 nlines = display_count_lines (startpos_byte,
23372 limit_byte,
23373 - (height * 2 + 30),
23374 &position);
23375 /* If we couldn't find the lines we wanted within
23376 line_number_display_limit_width chars per line,
23377 give up on line numbers for this window. */
23378 if (position == limit_byte && limit == startpos - distance)
23379 {
23380 w->base_line_pos = -1;
23381 w->base_line_number = 0;
23382 goto no_value;
23383 }
23384
23385 w->base_line_number = topline - nlines;
23386 w->base_line_pos = BYTE_TO_CHAR (position);
23387 }
23388
23389 /* Now count lines from the start pos to point. */
23390 nlines = display_count_lines (startpos_byte,
23391 PT_BYTE, PT, &junk);
23392
23393 /* Record that we did display the line number. */
23394 line_number_displayed = true;
23395
23396 /* Make the string to show. */
23397 pint2str (decode_mode_spec_buf, width, topline + nlines);
23398 return decode_mode_spec_buf;
23399 no_value:
23400 {
23401 char *p = decode_mode_spec_buf;
23402 int pad = width - 2;
23403 while (pad-- > 0)
23404 *p++ = ' ';
23405 *p++ = '?';
23406 *p++ = '?';
23407 *p = '\0';
23408 return decode_mode_spec_buf;
23409 }
23410 }
23411 break;
23412
23413 case 'm':
23414 obj = BVAR (b, mode_name);
23415 break;
23416
23417 case 'n':
23418 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23419 return " Narrow";
23420 break;
23421
23422 case 'p':
23423 {
23424 ptrdiff_t pos = marker_position (w->start);
23425 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23426
23427 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23428 {
23429 if (pos <= BUF_BEGV (b))
23430 return "All";
23431 else
23432 return "Bottom";
23433 }
23434 else if (pos <= BUF_BEGV (b))
23435 return "Top";
23436 else
23437 {
23438 if (total > 1000000)
23439 /* Do it differently for a large value, to avoid overflow. */
23440 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23441 else
23442 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23443 /* We can't normally display a 3-digit number,
23444 so get us a 2-digit number that is close. */
23445 if (total == 100)
23446 total = 99;
23447 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23448 return decode_mode_spec_buf;
23449 }
23450 }
23451
23452 /* Display percentage of size above the bottom of the screen. */
23453 case 'P':
23454 {
23455 ptrdiff_t toppos = marker_position (w->start);
23456 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23457 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23458
23459 if (botpos >= BUF_ZV (b))
23460 {
23461 if (toppos <= BUF_BEGV (b))
23462 return "All";
23463 else
23464 return "Bottom";
23465 }
23466 else
23467 {
23468 if (total > 1000000)
23469 /* Do it differently for a large value, to avoid overflow. */
23470 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23471 else
23472 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23473 /* We can't normally display a 3-digit number,
23474 so get us a 2-digit number that is close. */
23475 if (total == 100)
23476 total = 99;
23477 if (toppos <= BUF_BEGV (b))
23478 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23479 else
23480 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23481 return decode_mode_spec_buf;
23482 }
23483 }
23484
23485 case 's':
23486 /* status of process */
23487 obj = Fget_buffer_process (Fcurrent_buffer ());
23488 if (NILP (obj))
23489 return "no process";
23490 #ifndef MSDOS
23491 obj = Fsymbol_name (Fprocess_status (obj));
23492 #endif
23493 break;
23494
23495 case '@':
23496 {
23497 ptrdiff_t count = inhibit_garbage_collection ();
23498 Lisp_Object curdir = BVAR (current_buffer, directory);
23499 Lisp_Object val = Qnil;
23500
23501 if (STRINGP (curdir))
23502 val = call1 (intern ("file-remote-p"), curdir);
23503
23504 unbind_to (count, Qnil);
23505
23506 if (NILP (val))
23507 return "-";
23508 else
23509 return "@";
23510 }
23511
23512 case 'z':
23513 /* coding-system (not including end-of-line format) */
23514 case 'Z':
23515 /* coding-system (including end-of-line type) */
23516 {
23517 bool eol_flag = (c == 'Z');
23518 char *p = decode_mode_spec_buf;
23519
23520 if (! FRAME_WINDOW_P (f))
23521 {
23522 /* No need to mention EOL here--the terminal never needs
23523 to do EOL conversion. */
23524 p = decode_mode_spec_coding (CODING_ID_NAME
23525 (FRAME_KEYBOARD_CODING (f)->id),
23526 p, false);
23527 p = decode_mode_spec_coding (CODING_ID_NAME
23528 (FRAME_TERMINAL_CODING (f)->id),
23529 p, false);
23530 }
23531 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23532 p, eol_flag);
23533
23534 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23535 #ifdef subprocesses
23536 obj = Fget_buffer_process (Fcurrent_buffer ());
23537 if (PROCESSP (obj))
23538 {
23539 p = decode_mode_spec_coding
23540 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23541 p = decode_mode_spec_coding
23542 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23543 }
23544 #endif /* subprocesses */
23545 #endif /* false */
23546 *p = 0;
23547 return decode_mode_spec_buf;
23548 }
23549 }
23550
23551 if (STRINGP (obj))
23552 {
23553 *string = obj;
23554 return SSDATA (obj);
23555 }
23556 else
23557 return "";
23558 }
23559
23560
23561 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23562 means count lines back from START_BYTE. But don't go beyond
23563 LIMIT_BYTE. Return the number of lines thus found (always
23564 nonnegative).
23565
23566 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23567 either the position COUNT lines after/before START_BYTE, if we
23568 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23569 COUNT lines. */
23570
23571 static ptrdiff_t
23572 display_count_lines (ptrdiff_t start_byte,
23573 ptrdiff_t limit_byte, ptrdiff_t count,
23574 ptrdiff_t *byte_pos_ptr)
23575 {
23576 register unsigned char *cursor;
23577 unsigned char *base;
23578
23579 register ptrdiff_t ceiling;
23580 register unsigned char *ceiling_addr;
23581 ptrdiff_t orig_count = count;
23582
23583 /* If we are not in selective display mode,
23584 check only for newlines. */
23585 bool selective_display
23586 = (!NILP (BVAR (current_buffer, selective_display))
23587 && !INTEGERP (BVAR (current_buffer, selective_display)));
23588
23589 if (count > 0)
23590 {
23591 while (start_byte < limit_byte)
23592 {
23593 ceiling = BUFFER_CEILING_OF (start_byte);
23594 ceiling = min (limit_byte - 1, ceiling);
23595 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23596 base = (cursor = BYTE_POS_ADDR (start_byte));
23597
23598 do
23599 {
23600 if (selective_display)
23601 {
23602 while (*cursor != '\n' && *cursor != 015
23603 && ++cursor != ceiling_addr)
23604 continue;
23605 if (cursor == ceiling_addr)
23606 break;
23607 }
23608 else
23609 {
23610 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23611 if (! cursor)
23612 break;
23613 }
23614
23615 cursor++;
23616
23617 if (--count == 0)
23618 {
23619 start_byte += cursor - base;
23620 *byte_pos_ptr = start_byte;
23621 return orig_count;
23622 }
23623 }
23624 while (cursor < ceiling_addr);
23625
23626 start_byte += ceiling_addr - base;
23627 }
23628 }
23629 else
23630 {
23631 while (start_byte > limit_byte)
23632 {
23633 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23634 ceiling = max (limit_byte, ceiling);
23635 ceiling_addr = BYTE_POS_ADDR (ceiling);
23636 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23637 while (true)
23638 {
23639 if (selective_display)
23640 {
23641 while (--cursor >= ceiling_addr
23642 && *cursor != '\n' && *cursor != 015)
23643 continue;
23644 if (cursor < ceiling_addr)
23645 break;
23646 }
23647 else
23648 {
23649 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23650 if (! cursor)
23651 break;
23652 }
23653
23654 if (++count == 0)
23655 {
23656 start_byte += cursor - base + 1;
23657 *byte_pos_ptr = start_byte;
23658 /* When scanning backwards, we should
23659 not count the newline posterior to which we stop. */
23660 return - orig_count - 1;
23661 }
23662 }
23663 start_byte += ceiling_addr - base;
23664 }
23665 }
23666
23667 *byte_pos_ptr = limit_byte;
23668
23669 if (count < 0)
23670 return - orig_count + count;
23671 return orig_count - count;
23672
23673 }
23674
23675
23676 \f
23677 /***********************************************************************
23678 Displaying strings
23679 ***********************************************************************/
23680
23681 /* Display a NUL-terminated string, starting with index START.
23682
23683 If STRING is non-null, display that C string. Otherwise, the Lisp
23684 string LISP_STRING is displayed. There's a case that STRING is
23685 non-null and LISP_STRING is not nil. It means STRING is a string
23686 data of LISP_STRING. In that case, we display LISP_STRING while
23687 ignoring its text properties.
23688
23689 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23690 FACE_STRING. Display STRING or LISP_STRING with the face at
23691 FACE_STRING_POS in FACE_STRING:
23692
23693 Display the string in the environment given by IT, but use the
23694 standard display table, temporarily.
23695
23696 FIELD_WIDTH is the minimum number of output glyphs to produce.
23697 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23698 with spaces. If STRING has more characters, more than FIELD_WIDTH
23699 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23700
23701 PRECISION is the maximum number of characters to output from
23702 STRING. PRECISION < 0 means don't truncate the string.
23703
23704 This is roughly equivalent to printf format specifiers:
23705
23706 FIELD_WIDTH PRECISION PRINTF
23707 ----------------------------------------
23708 -1 -1 %s
23709 -1 10 %.10s
23710 10 -1 %10s
23711 20 10 %20.10s
23712
23713 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23714 display them, and < 0 means obey the current buffer's value of
23715 enable_multibyte_characters.
23716
23717 Value is the number of columns displayed. */
23718
23719 static int
23720 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23721 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23722 int field_width, int precision, int max_x, int multibyte)
23723 {
23724 int hpos_at_start = it->hpos;
23725 int saved_face_id = it->face_id;
23726 struct glyph_row *row = it->glyph_row;
23727 ptrdiff_t it_charpos;
23728
23729 /* Initialize the iterator IT for iteration over STRING beginning
23730 with index START. */
23731 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23732 precision, field_width, multibyte);
23733 if (string && STRINGP (lisp_string))
23734 /* LISP_STRING is the one returned by decode_mode_spec. We should
23735 ignore its text properties. */
23736 it->stop_charpos = it->end_charpos;
23737
23738 /* If displaying STRING, set up the face of the iterator from
23739 FACE_STRING, if that's given. */
23740 if (STRINGP (face_string))
23741 {
23742 ptrdiff_t endptr;
23743 struct face *face;
23744
23745 it->face_id
23746 = face_at_string_position (it->w, face_string, face_string_pos,
23747 0, &endptr, it->base_face_id, false);
23748 face = FACE_FROM_ID (it->f, it->face_id);
23749 it->face_box_p = face->box != FACE_NO_BOX;
23750 }
23751
23752 /* Set max_x to the maximum allowed X position. Don't let it go
23753 beyond the right edge of the window. */
23754 if (max_x <= 0)
23755 max_x = it->last_visible_x;
23756 else
23757 max_x = min (max_x, it->last_visible_x);
23758
23759 /* Skip over display elements that are not visible. because IT->w is
23760 hscrolled. */
23761 if (it->current_x < it->first_visible_x)
23762 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23763 MOVE_TO_POS | MOVE_TO_X);
23764
23765 row->ascent = it->max_ascent;
23766 row->height = it->max_ascent + it->max_descent;
23767 row->phys_ascent = it->max_phys_ascent;
23768 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23769 row->extra_line_spacing = it->max_extra_line_spacing;
23770
23771 if (STRINGP (it->string))
23772 it_charpos = IT_STRING_CHARPOS (*it);
23773 else
23774 it_charpos = IT_CHARPOS (*it);
23775
23776 /* This condition is for the case that we are called with current_x
23777 past last_visible_x. */
23778 while (it->current_x < max_x)
23779 {
23780 int x_before, x, n_glyphs_before, i, nglyphs;
23781
23782 /* Get the next display element. */
23783 if (!get_next_display_element (it))
23784 break;
23785
23786 /* Produce glyphs. */
23787 x_before = it->current_x;
23788 n_glyphs_before = row->used[TEXT_AREA];
23789 PRODUCE_GLYPHS (it);
23790
23791 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23792 i = 0;
23793 x = x_before;
23794 while (i < nglyphs)
23795 {
23796 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23797
23798 if (it->line_wrap != TRUNCATE
23799 && x + glyph->pixel_width > max_x)
23800 {
23801 /* End of continued line or max_x reached. */
23802 if (CHAR_GLYPH_PADDING_P (*glyph))
23803 {
23804 /* A wide character is unbreakable. */
23805 if (row->reversed_p)
23806 unproduce_glyphs (it, row->used[TEXT_AREA]
23807 - n_glyphs_before);
23808 row->used[TEXT_AREA] = n_glyphs_before;
23809 it->current_x = x_before;
23810 }
23811 else
23812 {
23813 if (row->reversed_p)
23814 unproduce_glyphs (it, row->used[TEXT_AREA]
23815 - (n_glyphs_before + i));
23816 row->used[TEXT_AREA] = n_glyphs_before + i;
23817 it->current_x = x;
23818 }
23819 break;
23820 }
23821 else if (x + glyph->pixel_width >= it->first_visible_x)
23822 {
23823 /* Glyph is at least partially visible. */
23824 ++it->hpos;
23825 if (x < it->first_visible_x)
23826 row->x = x - it->first_visible_x;
23827 }
23828 else
23829 {
23830 /* Glyph is off the left margin of the display area.
23831 Should not happen. */
23832 emacs_abort ();
23833 }
23834
23835 row->ascent = max (row->ascent, it->max_ascent);
23836 row->height = max (row->height, it->max_ascent + it->max_descent);
23837 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23838 row->phys_height = max (row->phys_height,
23839 it->max_phys_ascent + it->max_phys_descent);
23840 row->extra_line_spacing = max (row->extra_line_spacing,
23841 it->max_extra_line_spacing);
23842 x += glyph->pixel_width;
23843 ++i;
23844 }
23845
23846 /* Stop if max_x reached. */
23847 if (i < nglyphs)
23848 break;
23849
23850 /* Stop at line ends. */
23851 if (ITERATOR_AT_END_OF_LINE_P (it))
23852 {
23853 it->continuation_lines_width = 0;
23854 break;
23855 }
23856
23857 set_iterator_to_next (it, true);
23858 if (STRINGP (it->string))
23859 it_charpos = IT_STRING_CHARPOS (*it);
23860 else
23861 it_charpos = IT_CHARPOS (*it);
23862
23863 /* Stop if truncating at the right edge. */
23864 if (it->line_wrap == TRUNCATE
23865 && it->current_x >= it->last_visible_x)
23866 {
23867 /* Add truncation mark, but don't do it if the line is
23868 truncated at a padding space. */
23869 if (it_charpos < it->string_nchars)
23870 {
23871 if (!FRAME_WINDOW_P (it->f))
23872 {
23873 int ii, n;
23874
23875 if (it->current_x > it->last_visible_x)
23876 {
23877 if (!row->reversed_p)
23878 {
23879 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23880 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23881 break;
23882 }
23883 else
23884 {
23885 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23886 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23887 break;
23888 unproduce_glyphs (it, ii + 1);
23889 ii = row->used[TEXT_AREA] - (ii + 1);
23890 }
23891 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23892 {
23893 row->used[TEXT_AREA] = ii;
23894 produce_special_glyphs (it, IT_TRUNCATION);
23895 }
23896 }
23897 produce_special_glyphs (it, IT_TRUNCATION);
23898 }
23899 row->truncated_on_right_p = true;
23900 }
23901 break;
23902 }
23903 }
23904
23905 /* Maybe insert a truncation at the left. */
23906 if (it->first_visible_x
23907 && it_charpos > 0)
23908 {
23909 if (!FRAME_WINDOW_P (it->f)
23910 || (row->reversed_p
23911 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23912 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23913 insert_left_trunc_glyphs (it);
23914 row->truncated_on_left_p = true;
23915 }
23916
23917 it->face_id = saved_face_id;
23918
23919 /* Value is number of columns displayed. */
23920 return it->hpos - hpos_at_start;
23921 }
23922
23923
23924 \f
23925 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23926 appears as an element of LIST or as the car of an element of LIST.
23927 If PROPVAL is a list, compare each element against LIST in that
23928 way, and return 1/2 if any element of PROPVAL is found in LIST.
23929 Otherwise return 0. This function cannot quit.
23930 The return value is 2 if the text is invisible but with an ellipsis
23931 and 1 if it's invisible and without an ellipsis. */
23932
23933 int
23934 invisible_prop (Lisp_Object propval, Lisp_Object list)
23935 {
23936 Lisp_Object tail, proptail;
23937
23938 for (tail = list; CONSP (tail); tail = XCDR (tail))
23939 {
23940 register Lisp_Object tem;
23941 tem = XCAR (tail);
23942 if (EQ (propval, tem))
23943 return 1;
23944 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23945 return NILP (XCDR (tem)) ? 1 : 2;
23946 }
23947
23948 if (CONSP (propval))
23949 {
23950 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23951 {
23952 Lisp_Object propelt;
23953 propelt = XCAR (proptail);
23954 for (tail = list; CONSP (tail); tail = XCDR (tail))
23955 {
23956 register Lisp_Object tem;
23957 tem = XCAR (tail);
23958 if (EQ (propelt, tem))
23959 return 1;
23960 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23961 return NILP (XCDR (tem)) ? 1 : 2;
23962 }
23963 }
23964 }
23965
23966 return 0;
23967 }
23968
23969 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23970 doc: /* Non-nil if the property makes the text invisible.
23971 POS-OR-PROP can be a marker or number, in which case it is taken to be
23972 a position in the current buffer and the value of the `invisible' property
23973 is checked; or it can be some other value, which is then presumed to be the
23974 value of the `invisible' property of the text of interest.
23975 The non-nil value returned can be t for truly invisible text or something
23976 else if the text is replaced by an ellipsis. */)
23977 (Lisp_Object pos_or_prop)
23978 {
23979 Lisp_Object prop
23980 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23981 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23982 : pos_or_prop);
23983 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23984 return (invis == 0 ? Qnil
23985 : invis == 1 ? Qt
23986 : make_number (invis));
23987 }
23988
23989 /* Calculate a width or height in pixels from a specification using
23990 the following elements:
23991
23992 SPEC ::=
23993 NUM - a (fractional) multiple of the default font width/height
23994 (NUM) - specifies exactly NUM pixels
23995 UNIT - a fixed number of pixels, see below.
23996 ELEMENT - size of a display element in pixels, see below.
23997 (NUM . SPEC) - equals NUM * SPEC
23998 (+ SPEC SPEC ...) - add pixel values
23999 (- SPEC SPEC ...) - subtract pixel values
24000 (- SPEC) - negate pixel value
24001
24002 NUM ::=
24003 INT or FLOAT - a number constant
24004 SYMBOL - use symbol's (buffer local) variable binding.
24005
24006 UNIT ::=
24007 in - pixels per inch *)
24008 mm - pixels per 1/1000 meter *)
24009 cm - pixels per 1/100 meter *)
24010 width - width of current font in pixels.
24011 height - height of current font in pixels.
24012
24013 *) using the ratio(s) defined in display-pixels-per-inch.
24014
24015 ELEMENT ::=
24016
24017 left-fringe - left fringe width in pixels
24018 right-fringe - right fringe width in pixels
24019
24020 left-margin - left margin width in pixels
24021 right-margin - right margin width in pixels
24022
24023 scroll-bar - scroll-bar area width in pixels
24024
24025 Examples:
24026
24027 Pixels corresponding to 5 inches:
24028 (5 . in)
24029
24030 Total width of non-text areas on left side of window (if scroll-bar is on left):
24031 '(space :width (+ left-fringe left-margin scroll-bar))
24032
24033 Align to first text column (in header line):
24034 '(space :align-to 0)
24035
24036 Align to middle of text area minus half the width of variable `my-image'
24037 containing a loaded image:
24038 '(space :align-to (0.5 . (- text my-image)))
24039
24040 Width of left margin minus width of 1 character in the default font:
24041 '(space :width (- left-margin 1))
24042
24043 Width of left margin minus width of 2 characters in the current font:
24044 '(space :width (- left-margin (2 . width)))
24045
24046 Center 1 character over left-margin (in header line):
24047 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24048
24049 Different ways to express width of left fringe plus left margin minus one pixel:
24050 '(space :width (- (+ left-fringe left-margin) (1)))
24051 '(space :width (+ left-fringe left-margin (- (1))))
24052 '(space :width (+ left-fringe left-margin (-1)))
24053
24054 */
24055
24056 static bool
24057 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24058 struct font *font, bool width_p, int *align_to)
24059 {
24060 double pixels;
24061
24062 # define OK_PIXELS(val) (*res = (val), true)
24063 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24064
24065 if (NILP (prop))
24066 return OK_PIXELS (0);
24067
24068 eassert (FRAME_LIVE_P (it->f));
24069
24070 if (SYMBOLP (prop))
24071 {
24072 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24073 {
24074 char *unit = SSDATA (SYMBOL_NAME (prop));
24075
24076 if (unit[0] == 'i' && unit[1] == 'n')
24077 pixels = 1.0;
24078 else if (unit[0] == 'm' && unit[1] == 'm')
24079 pixels = 25.4;
24080 else if (unit[0] == 'c' && unit[1] == 'm')
24081 pixels = 2.54;
24082 else
24083 pixels = 0;
24084 if (pixels > 0)
24085 {
24086 double ppi = (width_p ? FRAME_RES_X (it->f)
24087 : FRAME_RES_Y (it->f));
24088
24089 if (ppi > 0)
24090 return OK_PIXELS (ppi / pixels);
24091 return false;
24092 }
24093 }
24094
24095 #ifdef HAVE_WINDOW_SYSTEM
24096 if (EQ (prop, Qheight))
24097 return OK_PIXELS (font
24098 ? normal_char_height (font, -1)
24099 : FRAME_LINE_HEIGHT (it->f));
24100 if (EQ (prop, Qwidth))
24101 return OK_PIXELS (font
24102 ? FONT_WIDTH (font)
24103 : FRAME_COLUMN_WIDTH (it->f));
24104 #else
24105 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24106 return OK_PIXELS (1);
24107 #endif
24108
24109 if (EQ (prop, Qtext))
24110 return OK_PIXELS (width_p
24111 ? window_box_width (it->w, TEXT_AREA)
24112 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24113
24114 if (align_to && *align_to < 0)
24115 {
24116 *res = 0;
24117 if (EQ (prop, Qleft))
24118 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24119 if (EQ (prop, Qright))
24120 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24121 if (EQ (prop, Qcenter))
24122 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24123 + window_box_width (it->w, TEXT_AREA) / 2);
24124 if (EQ (prop, Qleft_fringe))
24125 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24126 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24127 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24128 if (EQ (prop, Qright_fringe))
24129 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24130 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24131 : window_box_right_offset (it->w, TEXT_AREA));
24132 if (EQ (prop, Qleft_margin))
24133 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24134 if (EQ (prop, Qright_margin))
24135 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24136 if (EQ (prop, Qscroll_bar))
24137 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24138 ? 0
24139 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24140 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24141 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24142 : 0)));
24143 }
24144 else
24145 {
24146 if (EQ (prop, Qleft_fringe))
24147 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24148 if (EQ (prop, Qright_fringe))
24149 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24150 if (EQ (prop, Qleft_margin))
24151 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24152 if (EQ (prop, Qright_margin))
24153 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24154 if (EQ (prop, Qscroll_bar))
24155 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24156 }
24157
24158 prop = buffer_local_value (prop, it->w->contents);
24159 if (EQ (prop, Qunbound))
24160 prop = Qnil;
24161 }
24162
24163 if (NUMBERP (prop))
24164 {
24165 int base_unit = (width_p
24166 ? FRAME_COLUMN_WIDTH (it->f)
24167 : FRAME_LINE_HEIGHT (it->f));
24168 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24169 }
24170
24171 if (CONSP (prop))
24172 {
24173 Lisp_Object car = XCAR (prop);
24174 Lisp_Object cdr = XCDR (prop);
24175
24176 if (SYMBOLP (car))
24177 {
24178 #ifdef HAVE_WINDOW_SYSTEM
24179 if (FRAME_WINDOW_P (it->f)
24180 && valid_image_p (prop))
24181 {
24182 ptrdiff_t id = lookup_image (it->f, prop);
24183 struct image *img = IMAGE_FROM_ID (it->f, id);
24184
24185 return OK_PIXELS (width_p ? img->width : img->height);
24186 }
24187 #endif
24188 if (EQ (car, Qplus) || EQ (car, Qminus))
24189 {
24190 bool first = true;
24191 double px;
24192
24193 pixels = 0;
24194 while (CONSP (cdr))
24195 {
24196 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24197 font, width_p, align_to))
24198 return false;
24199 if (first)
24200 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24201 else
24202 pixels += px;
24203 cdr = XCDR (cdr);
24204 }
24205 if (EQ (car, Qminus))
24206 pixels = -pixels;
24207 return OK_PIXELS (pixels);
24208 }
24209
24210 car = buffer_local_value (car, it->w->contents);
24211 if (EQ (car, Qunbound))
24212 car = Qnil;
24213 }
24214
24215 if (NUMBERP (car))
24216 {
24217 double fact;
24218 pixels = XFLOATINT (car);
24219 if (NILP (cdr))
24220 return OK_PIXELS (pixels);
24221 if (calc_pixel_width_or_height (&fact, it, cdr,
24222 font, width_p, align_to))
24223 return OK_PIXELS (pixels * fact);
24224 return false;
24225 }
24226
24227 return false;
24228 }
24229
24230 return false;
24231 }
24232
24233 void
24234 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24235 {
24236 #ifdef HAVE_WINDOW_SYSTEM
24237 normal_char_ascent_descent (font, -1, ascent, descent);
24238 #else
24239 *ascent = 1;
24240 *descent = 0;
24241 #endif
24242 }
24243
24244 \f
24245 /***********************************************************************
24246 Glyph Display
24247 ***********************************************************************/
24248
24249 #ifdef HAVE_WINDOW_SYSTEM
24250
24251 #ifdef GLYPH_DEBUG
24252
24253 void
24254 dump_glyph_string (struct glyph_string *s)
24255 {
24256 fprintf (stderr, "glyph string\n");
24257 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24258 s->x, s->y, s->width, s->height);
24259 fprintf (stderr, " ybase = %d\n", s->ybase);
24260 fprintf (stderr, " hl = %d\n", s->hl);
24261 fprintf (stderr, " left overhang = %d, right = %d\n",
24262 s->left_overhang, s->right_overhang);
24263 fprintf (stderr, " nchars = %d\n", s->nchars);
24264 fprintf (stderr, " extends to end of line = %d\n",
24265 s->extends_to_end_of_line_p);
24266 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24267 fprintf (stderr, " bg width = %d\n", s->background_width);
24268 }
24269
24270 #endif /* GLYPH_DEBUG */
24271
24272 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24273 of XChar2b structures for S; it can't be allocated in
24274 init_glyph_string because it must be allocated via `alloca'. W
24275 is the window on which S is drawn. ROW and AREA are the glyph row
24276 and area within the row from which S is constructed. START is the
24277 index of the first glyph structure covered by S. HL is a
24278 face-override for drawing S. */
24279
24280 #ifdef HAVE_NTGUI
24281 #define OPTIONAL_HDC(hdc) HDC hdc,
24282 #define DECLARE_HDC(hdc) HDC hdc;
24283 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24284 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24285 #endif
24286
24287 #ifndef OPTIONAL_HDC
24288 #define OPTIONAL_HDC(hdc)
24289 #define DECLARE_HDC(hdc)
24290 #define ALLOCATE_HDC(hdc, f)
24291 #define RELEASE_HDC(hdc, f)
24292 #endif
24293
24294 static void
24295 init_glyph_string (struct glyph_string *s,
24296 OPTIONAL_HDC (hdc)
24297 XChar2b *char2b, struct window *w, struct glyph_row *row,
24298 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24299 {
24300 memset (s, 0, sizeof *s);
24301 s->w = w;
24302 s->f = XFRAME (w->frame);
24303 #ifdef HAVE_NTGUI
24304 s->hdc = hdc;
24305 #endif
24306 s->display = FRAME_X_DISPLAY (s->f);
24307 s->window = FRAME_X_WINDOW (s->f);
24308 s->char2b = char2b;
24309 s->hl = hl;
24310 s->row = row;
24311 s->area = area;
24312 s->first_glyph = row->glyphs[area] + start;
24313 s->height = row->height;
24314 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24315 s->ybase = s->y + row->ascent;
24316 }
24317
24318
24319 /* Append the list of glyph strings with head H and tail T to the list
24320 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24321
24322 static void
24323 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24324 struct glyph_string *h, struct glyph_string *t)
24325 {
24326 if (h)
24327 {
24328 if (*head)
24329 (*tail)->next = h;
24330 else
24331 *head = h;
24332 h->prev = *tail;
24333 *tail = t;
24334 }
24335 }
24336
24337
24338 /* Prepend the list of glyph strings with head H and tail T to the
24339 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24340 result. */
24341
24342 static void
24343 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24344 struct glyph_string *h, struct glyph_string *t)
24345 {
24346 if (h)
24347 {
24348 if (*head)
24349 (*head)->prev = t;
24350 else
24351 *tail = t;
24352 t->next = *head;
24353 *head = h;
24354 }
24355 }
24356
24357
24358 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24359 Set *HEAD and *TAIL to the resulting list. */
24360
24361 static void
24362 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24363 struct glyph_string *s)
24364 {
24365 s->next = s->prev = NULL;
24366 append_glyph_string_lists (head, tail, s, s);
24367 }
24368
24369
24370 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24371 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24372 make sure that X resources for the face returned are allocated.
24373 Value is a pointer to a realized face that is ready for display if
24374 DISPLAY_P. */
24375
24376 static struct face *
24377 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24378 XChar2b *char2b, bool display_p)
24379 {
24380 struct face *face = FACE_FROM_ID (f, face_id);
24381 unsigned code = 0;
24382
24383 if (face->font)
24384 {
24385 code = face->font->driver->encode_char (face->font, c);
24386
24387 if (code == FONT_INVALID_CODE)
24388 code = 0;
24389 }
24390 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24391
24392 /* Make sure X resources of the face are allocated. */
24393 #ifdef HAVE_X_WINDOWS
24394 if (display_p)
24395 #endif
24396 {
24397 eassert (face != NULL);
24398 prepare_face_for_display (f, face);
24399 }
24400
24401 return face;
24402 }
24403
24404
24405 /* Get face and two-byte form of character glyph GLYPH on frame F.
24406 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24407 a pointer to a realized face that is ready for display. */
24408
24409 static struct face *
24410 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24411 XChar2b *char2b)
24412 {
24413 struct face *face;
24414 unsigned code = 0;
24415
24416 eassert (glyph->type == CHAR_GLYPH);
24417 face = FACE_FROM_ID (f, glyph->face_id);
24418
24419 /* Make sure X resources of the face are allocated. */
24420 eassert (face != NULL);
24421 prepare_face_for_display (f, face);
24422
24423 if (face->font)
24424 {
24425 if (CHAR_BYTE8_P (glyph->u.ch))
24426 code = CHAR_TO_BYTE8 (glyph->u.ch);
24427 else
24428 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24429
24430 if (code == FONT_INVALID_CODE)
24431 code = 0;
24432 }
24433
24434 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24435 return face;
24436 }
24437
24438
24439 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24440 Return true iff FONT has a glyph for C. */
24441
24442 static bool
24443 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24444 {
24445 unsigned code;
24446
24447 if (CHAR_BYTE8_P (c))
24448 code = CHAR_TO_BYTE8 (c);
24449 else
24450 code = font->driver->encode_char (font, c);
24451
24452 if (code == FONT_INVALID_CODE)
24453 return false;
24454 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24455 return true;
24456 }
24457
24458
24459 /* Fill glyph string S with composition components specified by S->cmp.
24460
24461 BASE_FACE is the base face of the composition.
24462 S->cmp_from is the index of the first component for S.
24463
24464 OVERLAPS non-zero means S should draw the foreground only, and use
24465 its physical height for clipping. See also draw_glyphs.
24466
24467 Value is the index of a component not in S. */
24468
24469 static int
24470 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24471 int overlaps)
24472 {
24473 int i;
24474 /* For all glyphs of this composition, starting at the offset
24475 S->cmp_from, until we reach the end of the definition or encounter a
24476 glyph that requires the different face, add it to S. */
24477 struct face *face;
24478
24479 eassert (s);
24480
24481 s->for_overlaps = overlaps;
24482 s->face = NULL;
24483 s->font = NULL;
24484 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24485 {
24486 int c = COMPOSITION_GLYPH (s->cmp, i);
24487
24488 /* TAB in a composition means display glyphs with padding space
24489 on the left or right. */
24490 if (c != '\t')
24491 {
24492 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24493 -1, Qnil);
24494
24495 face = get_char_face_and_encoding (s->f, c, face_id,
24496 s->char2b + i, true);
24497 if (face)
24498 {
24499 if (! s->face)
24500 {
24501 s->face = face;
24502 s->font = s->face->font;
24503 }
24504 else if (s->face != face)
24505 break;
24506 }
24507 }
24508 ++s->nchars;
24509 }
24510 s->cmp_to = i;
24511
24512 if (s->face == NULL)
24513 {
24514 s->face = base_face->ascii_face;
24515 s->font = s->face->font;
24516 }
24517
24518 /* All glyph strings for the same composition has the same width,
24519 i.e. the width set for the first component of the composition. */
24520 s->width = s->first_glyph->pixel_width;
24521
24522 /* If the specified font could not be loaded, use the frame's
24523 default font, but record the fact that we couldn't load it in
24524 the glyph string so that we can draw rectangles for the
24525 characters of the glyph string. */
24526 if (s->font == NULL)
24527 {
24528 s->font_not_found_p = true;
24529 s->font = FRAME_FONT (s->f);
24530 }
24531
24532 /* Adjust base line for subscript/superscript text. */
24533 s->ybase += s->first_glyph->voffset;
24534
24535 return s->cmp_to;
24536 }
24537
24538 static int
24539 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24540 int start, int end, int overlaps)
24541 {
24542 struct glyph *glyph, *last;
24543 Lisp_Object lgstring;
24544 int i;
24545
24546 s->for_overlaps = overlaps;
24547 glyph = s->row->glyphs[s->area] + start;
24548 last = s->row->glyphs[s->area] + end;
24549 s->cmp_id = glyph->u.cmp.id;
24550 s->cmp_from = glyph->slice.cmp.from;
24551 s->cmp_to = glyph->slice.cmp.to + 1;
24552 s->face = FACE_FROM_ID (s->f, face_id);
24553 lgstring = composition_gstring_from_id (s->cmp_id);
24554 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24555 glyph++;
24556 while (glyph < last
24557 && glyph->u.cmp.automatic
24558 && glyph->u.cmp.id == s->cmp_id
24559 && s->cmp_to == glyph->slice.cmp.from)
24560 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24561
24562 for (i = s->cmp_from; i < s->cmp_to; i++)
24563 {
24564 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24565 unsigned code = LGLYPH_CODE (lglyph);
24566
24567 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24568 }
24569 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24570 return glyph - s->row->glyphs[s->area];
24571 }
24572
24573
24574 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24575 See the comment of fill_glyph_string for arguments.
24576 Value is the index of the first glyph not in S. */
24577
24578
24579 static int
24580 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24581 int start, int end, int overlaps)
24582 {
24583 struct glyph *glyph, *last;
24584 int voffset;
24585
24586 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24587 s->for_overlaps = overlaps;
24588 glyph = s->row->glyphs[s->area] + start;
24589 last = s->row->glyphs[s->area] + end;
24590 voffset = glyph->voffset;
24591 s->face = FACE_FROM_ID (s->f, face_id);
24592 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24593 s->nchars = 1;
24594 s->width = glyph->pixel_width;
24595 glyph++;
24596 while (glyph < last
24597 && glyph->type == GLYPHLESS_GLYPH
24598 && glyph->voffset == voffset
24599 && glyph->face_id == face_id)
24600 {
24601 s->nchars++;
24602 s->width += glyph->pixel_width;
24603 glyph++;
24604 }
24605 s->ybase += voffset;
24606 return glyph - s->row->glyphs[s->area];
24607 }
24608
24609
24610 /* Fill glyph string S from a sequence of character glyphs.
24611
24612 FACE_ID is the face id of the string. START is the index of the
24613 first glyph to consider, END is the index of the last + 1.
24614 OVERLAPS non-zero means S should draw the foreground only, and use
24615 its physical height for clipping. See also draw_glyphs.
24616
24617 Value is the index of the first glyph not in S. */
24618
24619 static int
24620 fill_glyph_string (struct glyph_string *s, int face_id,
24621 int start, int end, int overlaps)
24622 {
24623 struct glyph *glyph, *last;
24624 int voffset;
24625 bool glyph_not_available_p;
24626
24627 eassert (s->f == XFRAME (s->w->frame));
24628 eassert (s->nchars == 0);
24629 eassert (start >= 0 && end > start);
24630
24631 s->for_overlaps = overlaps;
24632 glyph = s->row->glyphs[s->area] + start;
24633 last = s->row->glyphs[s->area] + end;
24634 voffset = glyph->voffset;
24635 s->padding_p = glyph->padding_p;
24636 glyph_not_available_p = glyph->glyph_not_available_p;
24637
24638 while (glyph < last
24639 && glyph->type == CHAR_GLYPH
24640 && glyph->voffset == voffset
24641 /* Same face id implies same font, nowadays. */
24642 && glyph->face_id == face_id
24643 && glyph->glyph_not_available_p == glyph_not_available_p)
24644 {
24645 s->face = get_glyph_face_and_encoding (s->f, glyph,
24646 s->char2b + s->nchars);
24647 ++s->nchars;
24648 eassert (s->nchars <= end - start);
24649 s->width += glyph->pixel_width;
24650 if (glyph++->padding_p != s->padding_p)
24651 break;
24652 }
24653
24654 s->font = s->face->font;
24655
24656 /* If the specified font could not be loaded, use the frame's font,
24657 but record the fact that we couldn't load it in
24658 S->font_not_found_p so that we can draw rectangles for the
24659 characters of the glyph string. */
24660 if (s->font == NULL || glyph_not_available_p)
24661 {
24662 s->font_not_found_p = true;
24663 s->font = FRAME_FONT (s->f);
24664 }
24665
24666 /* Adjust base line for subscript/superscript text. */
24667 s->ybase += voffset;
24668
24669 eassert (s->face && s->face->gc);
24670 return glyph - s->row->glyphs[s->area];
24671 }
24672
24673
24674 /* Fill glyph string S from image glyph S->first_glyph. */
24675
24676 static void
24677 fill_image_glyph_string (struct glyph_string *s)
24678 {
24679 eassert (s->first_glyph->type == IMAGE_GLYPH);
24680 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24681 eassert (s->img);
24682 s->slice = s->first_glyph->slice.img;
24683 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24684 s->font = s->face->font;
24685 s->width = s->first_glyph->pixel_width;
24686
24687 /* Adjust base line for subscript/superscript text. */
24688 s->ybase += s->first_glyph->voffset;
24689 }
24690
24691
24692 /* Fill glyph string S from a sequence of stretch glyphs.
24693
24694 START is the index of the first glyph to consider,
24695 END is the index of the last + 1.
24696
24697 Value is the index of the first glyph not in S. */
24698
24699 static int
24700 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24701 {
24702 struct glyph *glyph, *last;
24703 int voffset, face_id;
24704
24705 eassert (s->first_glyph->type == STRETCH_GLYPH);
24706
24707 glyph = s->row->glyphs[s->area] + start;
24708 last = s->row->glyphs[s->area] + end;
24709 face_id = glyph->face_id;
24710 s->face = FACE_FROM_ID (s->f, face_id);
24711 s->font = s->face->font;
24712 s->width = glyph->pixel_width;
24713 s->nchars = 1;
24714 voffset = glyph->voffset;
24715
24716 for (++glyph;
24717 (glyph < last
24718 && glyph->type == STRETCH_GLYPH
24719 && glyph->voffset == voffset
24720 && glyph->face_id == face_id);
24721 ++glyph)
24722 s->width += glyph->pixel_width;
24723
24724 /* Adjust base line for subscript/superscript text. */
24725 s->ybase += voffset;
24726
24727 /* The case that face->gc == 0 is handled when drawing the glyph
24728 string by calling prepare_face_for_display. */
24729 eassert (s->face);
24730 return glyph - s->row->glyphs[s->area];
24731 }
24732
24733 static struct font_metrics *
24734 get_per_char_metric (struct font *font, XChar2b *char2b)
24735 {
24736 static struct font_metrics metrics;
24737 unsigned code;
24738
24739 if (! font)
24740 return NULL;
24741 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24742 if (code == FONT_INVALID_CODE)
24743 return NULL;
24744 font->driver->text_extents (font, &code, 1, &metrics);
24745 return &metrics;
24746 }
24747
24748 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24749 for FONT. Values are taken from font-global ones, except for fonts
24750 that claim preposterously large values, but whose glyphs actually
24751 have reasonable dimensions. C is the character to use for metrics
24752 if the font-global values are too large; if C is negative, the
24753 function selects a default character. */
24754 static void
24755 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24756 {
24757 *ascent = FONT_BASE (font);
24758 *descent = FONT_DESCENT (font);
24759
24760 if (FONT_TOO_HIGH (font))
24761 {
24762 XChar2b char2b;
24763
24764 /* Get metrics of C, defaulting to a reasonably sized ASCII
24765 character. */
24766 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24767 {
24768 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24769
24770 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24771 {
24772 /* We add 1 pixel to character dimensions as heuristics
24773 that produces nicer display, e.g. when the face has
24774 the box attribute. */
24775 *ascent = pcm->ascent + 1;
24776 *descent = pcm->descent + 1;
24777 }
24778 }
24779 }
24780 }
24781
24782 /* A subroutine that computes a reasonable "normal character height"
24783 for fonts that claim preposterously large vertical dimensions, but
24784 whose glyphs are actually reasonably sized. C is the character
24785 whose metrics to use for those fonts, or -1 for default
24786 character. */
24787 static int
24788 normal_char_height (struct font *font, int c)
24789 {
24790 int ascent, descent;
24791
24792 normal_char_ascent_descent (font, c, &ascent, &descent);
24793
24794 return ascent + descent;
24795 }
24796
24797 /* EXPORT for RIF:
24798 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24799 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24800 assumed to be zero. */
24801
24802 void
24803 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24804 {
24805 *left = *right = 0;
24806
24807 if (glyph->type == CHAR_GLYPH)
24808 {
24809 XChar2b char2b;
24810 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24811 if (face->font)
24812 {
24813 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24814 if (pcm)
24815 {
24816 if (pcm->rbearing > pcm->width)
24817 *right = pcm->rbearing - pcm->width;
24818 if (pcm->lbearing < 0)
24819 *left = -pcm->lbearing;
24820 }
24821 }
24822 }
24823 else if (glyph->type == COMPOSITE_GLYPH)
24824 {
24825 if (! glyph->u.cmp.automatic)
24826 {
24827 struct composition *cmp = composition_table[glyph->u.cmp.id];
24828
24829 if (cmp->rbearing > cmp->pixel_width)
24830 *right = cmp->rbearing - cmp->pixel_width;
24831 if (cmp->lbearing < 0)
24832 *left = - cmp->lbearing;
24833 }
24834 else
24835 {
24836 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24837 struct font_metrics metrics;
24838
24839 composition_gstring_width (gstring, glyph->slice.cmp.from,
24840 glyph->slice.cmp.to + 1, &metrics);
24841 if (metrics.rbearing > metrics.width)
24842 *right = metrics.rbearing - metrics.width;
24843 if (metrics.lbearing < 0)
24844 *left = - metrics.lbearing;
24845 }
24846 }
24847 }
24848
24849
24850 /* Return the index of the first glyph preceding glyph string S that
24851 is overwritten by S because of S's left overhang. Value is -1
24852 if no glyphs are overwritten. */
24853
24854 static int
24855 left_overwritten (struct glyph_string *s)
24856 {
24857 int k;
24858
24859 if (s->left_overhang)
24860 {
24861 int x = 0, i;
24862 struct glyph *glyphs = s->row->glyphs[s->area];
24863 int first = s->first_glyph - glyphs;
24864
24865 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24866 x -= glyphs[i].pixel_width;
24867
24868 k = i + 1;
24869 }
24870 else
24871 k = -1;
24872
24873 return k;
24874 }
24875
24876
24877 /* Return the index of the first glyph preceding glyph string S that
24878 is overwriting S because of its right overhang. Value is -1 if no
24879 glyph in front of S overwrites S. */
24880
24881 static int
24882 left_overwriting (struct glyph_string *s)
24883 {
24884 int i, k, x;
24885 struct glyph *glyphs = s->row->glyphs[s->area];
24886 int first = s->first_glyph - glyphs;
24887
24888 k = -1;
24889 x = 0;
24890 for (i = first - 1; i >= 0; --i)
24891 {
24892 int left, right;
24893 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24894 if (x + right > 0)
24895 k = i;
24896 x -= glyphs[i].pixel_width;
24897 }
24898
24899 return k;
24900 }
24901
24902
24903 /* Return the index of the last glyph following glyph string S that is
24904 overwritten by S because of S's right overhang. Value is -1 if
24905 no such glyph is found. */
24906
24907 static int
24908 right_overwritten (struct glyph_string *s)
24909 {
24910 int k = -1;
24911
24912 if (s->right_overhang)
24913 {
24914 int x = 0, i;
24915 struct glyph *glyphs = s->row->glyphs[s->area];
24916 int first = (s->first_glyph - glyphs
24917 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24918 int end = s->row->used[s->area];
24919
24920 for (i = first; i < end && s->right_overhang > x; ++i)
24921 x += glyphs[i].pixel_width;
24922
24923 k = i;
24924 }
24925
24926 return k;
24927 }
24928
24929
24930 /* Return the index of the last glyph following glyph string S that
24931 overwrites S because of its left overhang. Value is negative
24932 if no such glyph is found. */
24933
24934 static int
24935 right_overwriting (struct glyph_string *s)
24936 {
24937 int i, k, x;
24938 int end = s->row->used[s->area];
24939 struct glyph *glyphs = s->row->glyphs[s->area];
24940 int first = (s->first_glyph - glyphs
24941 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24942
24943 k = -1;
24944 x = 0;
24945 for (i = first; i < end; ++i)
24946 {
24947 int left, right;
24948 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24949 if (x - left < 0)
24950 k = i;
24951 x += glyphs[i].pixel_width;
24952 }
24953
24954 return k;
24955 }
24956
24957
24958 /* Set background width of glyph string S. START is the index of the
24959 first glyph following S. LAST_X is the right-most x-position + 1
24960 in the drawing area. */
24961
24962 static void
24963 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24964 {
24965 /* If the face of this glyph string has to be drawn to the end of
24966 the drawing area, set S->extends_to_end_of_line_p. */
24967
24968 if (start == s->row->used[s->area]
24969 && ((s->row->fill_line_p
24970 && (s->hl == DRAW_NORMAL_TEXT
24971 || s->hl == DRAW_IMAGE_RAISED
24972 || s->hl == DRAW_IMAGE_SUNKEN))
24973 || s->hl == DRAW_MOUSE_FACE))
24974 s->extends_to_end_of_line_p = true;
24975
24976 /* If S extends its face to the end of the line, set its
24977 background_width to the distance to the right edge of the drawing
24978 area. */
24979 if (s->extends_to_end_of_line_p)
24980 s->background_width = last_x - s->x + 1;
24981 else
24982 s->background_width = s->width;
24983 }
24984
24985
24986 /* Compute overhangs and x-positions for glyph string S and its
24987 predecessors, or successors. X is the starting x-position for S.
24988 BACKWARD_P means process predecessors. */
24989
24990 static void
24991 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24992 {
24993 if (backward_p)
24994 {
24995 while (s)
24996 {
24997 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24998 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24999 x -= s->width;
25000 s->x = x;
25001 s = s->prev;
25002 }
25003 }
25004 else
25005 {
25006 while (s)
25007 {
25008 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25009 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25010 s->x = x;
25011 x += s->width;
25012 s = s->next;
25013 }
25014 }
25015 }
25016
25017
25018
25019 /* The following macros are only called from draw_glyphs below.
25020 They reference the following parameters of that function directly:
25021 `w', `row', `area', and `overlap_p'
25022 as well as the following local variables:
25023 `s', `f', and `hdc' (in W32) */
25024
25025 #ifdef HAVE_NTGUI
25026 /* On W32, silently add local `hdc' variable to argument list of
25027 init_glyph_string. */
25028 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25029 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25030 #else
25031 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25032 init_glyph_string (s, char2b, w, row, area, start, hl)
25033 #endif
25034
25035 /* Add a glyph string for a stretch glyph to the list of strings
25036 between HEAD and TAIL. START is the index of the stretch glyph in
25037 row area AREA of glyph row ROW. END is the index of the last glyph
25038 in that glyph row area. X is the current output position assigned
25039 to the new glyph string constructed. HL overrides that face of the
25040 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25041 is the right-most x-position of the drawing area. */
25042
25043 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25044 and below -- keep them on one line. */
25045 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25046 do \
25047 { \
25048 s = alloca (sizeof *s); \
25049 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25050 START = fill_stretch_glyph_string (s, START, END); \
25051 append_glyph_string (&HEAD, &TAIL, s); \
25052 s->x = (X); \
25053 } \
25054 while (false)
25055
25056
25057 /* Add a glyph string for an image glyph to the list of strings
25058 between HEAD and TAIL. START is the index of the image glyph in
25059 row area AREA of glyph row ROW. END is the index of the last glyph
25060 in that glyph row area. X is the current output position assigned
25061 to the new glyph string constructed. HL overrides that face of the
25062 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25063 is the right-most x-position of the drawing area. */
25064
25065 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25066 do \
25067 { \
25068 s = alloca (sizeof *s); \
25069 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25070 fill_image_glyph_string (s); \
25071 append_glyph_string (&HEAD, &TAIL, s); \
25072 ++START; \
25073 s->x = (X); \
25074 } \
25075 while (false)
25076
25077
25078 /* Add a glyph string for a sequence of character glyphs to the list
25079 of strings between HEAD and TAIL. START is the index of the first
25080 glyph in row area AREA of glyph row ROW that is part of the new
25081 glyph string. END is the index of the last glyph in that glyph row
25082 area. X is the current output position assigned to the new glyph
25083 string constructed. HL overrides that face of the glyph; e.g. it
25084 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25085 right-most x-position of the drawing area. */
25086
25087 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25088 do \
25089 { \
25090 int face_id; \
25091 XChar2b *char2b; \
25092 \
25093 face_id = (row)->glyphs[area][START].face_id; \
25094 \
25095 s = alloca (sizeof *s); \
25096 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25097 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25098 append_glyph_string (&HEAD, &TAIL, s); \
25099 s->x = (X); \
25100 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25101 } \
25102 while (false)
25103
25104
25105 /* Add a glyph string for a composite sequence to the list of strings
25106 between HEAD and TAIL. START is the index of the first glyph in
25107 row area AREA of glyph row ROW that is part of the new glyph
25108 string. END is the index of the last glyph in that glyph row area.
25109 X is the current output position assigned to the new glyph string
25110 constructed. HL overrides that face of the glyph; e.g. it is
25111 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25112 x-position of the drawing area. */
25113
25114 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25115 do { \
25116 int face_id = (row)->glyphs[area][START].face_id; \
25117 struct face *base_face = FACE_FROM_ID (f, face_id); \
25118 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25119 struct composition *cmp = composition_table[cmp_id]; \
25120 XChar2b *char2b; \
25121 struct glyph_string *first_s = NULL; \
25122 int n; \
25123 \
25124 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25125 \
25126 /* Make glyph_strings for each glyph sequence that is drawable by \
25127 the same face, and append them to HEAD/TAIL. */ \
25128 for (n = 0; n < cmp->glyph_len;) \
25129 { \
25130 s = alloca (sizeof *s); \
25131 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25132 append_glyph_string (&(HEAD), &(TAIL), s); \
25133 s->cmp = cmp; \
25134 s->cmp_from = n; \
25135 s->x = (X); \
25136 if (n == 0) \
25137 first_s = s; \
25138 n = fill_composite_glyph_string (s, base_face, overlaps); \
25139 } \
25140 \
25141 ++START; \
25142 s = first_s; \
25143 } while (false)
25144
25145
25146 /* Add a glyph string for a glyph-string sequence to the list of strings
25147 between HEAD and TAIL. */
25148
25149 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25150 do { \
25151 int face_id; \
25152 XChar2b *char2b; \
25153 Lisp_Object gstring; \
25154 \
25155 face_id = (row)->glyphs[area][START].face_id; \
25156 gstring = (composition_gstring_from_id \
25157 ((row)->glyphs[area][START].u.cmp.id)); \
25158 s = alloca (sizeof *s); \
25159 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25160 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25161 append_glyph_string (&(HEAD), &(TAIL), s); \
25162 s->x = (X); \
25163 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25164 } while (false)
25165
25166
25167 /* Add a glyph string for a sequence of glyphless character's glyphs
25168 to the list of strings between HEAD and TAIL. The meanings of
25169 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25170
25171 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25172 do \
25173 { \
25174 int face_id; \
25175 \
25176 face_id = (row)->glyphs[area][START].face_id; \
25177 \
25178 s = alloca (sizeof *s); \
25179 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25180 append_glyph_string (&HEAD, &TAIL, s); \
25181 s->x = (X); \
25182 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25183 overlaps); \
25184 } \
25185 while (false)
25186
25187
25188 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25189 of AREA of glyph row ROW on window W between indices START and END.
25190 HL overrides the face for drawing glyph strings, e.g. it is
25191 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25192 x-positions of the drawing area.
25193
25194 This is an ugly monster macro construct because we must use alloca
25195 to allocate glyph strings (because draw_glyphs can be called
25196 asynchronously). */
25197
25198 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25199 do \
25200 { \
25201 HEAD = TAIL = NULL; \
25202 while (START < END) \
25203 { \
25204 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25205 switch (first_glyph->type) \
25206 { \
25207 case CHAR_GLYPH: \
25208 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25209 HL, X, LAST_X); \
25210 break; \
25211 \
25212 case COMPOSITE_GLYPH: \
25213 if (first_glyph->u.cmp.automatic) \
25214 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25215 HL, X, LAST_X); \
25216 else \
25217 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25218 HL, X, LAST_X); \
25219 break; \
25220 \
25221 case STRETCH_GLYPH: \
25222 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25223 HL, X, LAST_X); \
25224 break; \
25225 \
25226 case IMAGE_GLYPH: \
25227 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25228 HL, X, LAST_X); \
25229 break; \
25230 \
25231 case GLYPHLESS_GLYPH: \
25232 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25233 HL, X, LAST_X); \
25234 break; \
25235 \
25236 default: \
25237 emacs_abort (); \
25238 } \
25239 \
25240 if (s) \
25241 { \
25242 set_glyph_string_background_width (s, START, LAST_X); \
25243 (X) += s->width; \
25244 } \
25245 } \
25246 } while (false)
25247
25248
25249 /* Draw glyphs between START and END in AREA of ROW on window W,
25250 starting at x-position X. X is relative to AREA in W. HL is a
25251 face-override with the following meaning:
25252
25253 DRAW_NORMAL_TEXT draw normally
25254 DRAW_CURSOR draw in cursor face
25255 DRAW_MOUSE_FACE draw in mouse face.
25256 DRAW_INVERSE_VIDEO draw in mode line face
25257 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25258 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25259
25260 If OVERLAPS is non-zero, draw only the foreground of characters and
25261 clip to the physical height of ROW. Non-zero value also defines
25262 the overlapping part to be drawn:
25263
25264 OVERLAPS_PRED overlap with preceding rows
25265 OVERLAPS_SUCC overlap with succeeding rows
25266 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25267 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25268
25269 Value is the x-position reached, relative to AREA of W. */
25270
25271 static int
25272 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25273 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25274 enum draw_glyphs_face hl, int overlaps)
25275 {
25276 struct glyph_string *head, *tail;
25277 struct glyph_string *s;
25278 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25279 int i, j, x_reached, last_x, area_left = 0;
25280 struct frame *f = XFRAME (WINDOW_FRAME (w));
25281 DECLARE_HDC (hdc);
25282
25283 ALLOCATE_HDC (hdc, f);
25284
25285 /* Let's rather be paranoid than getting a SEGV. */
25286 end = min (end, row->used[area]);
25287 start = clip_to_bounds (0, start, end);
25288
25289 /* Translate X to frame coordinates. Set last_x to the right
25290 end of the drawing area. */
25291 if (row->full_width_p)
25292 {
25293 /* X is relative to the left edge of W, without scroll bars
25294 or fringes. */
25295 area_left = WINDOW_LEFT_EDGE_X (w);
25296 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25297 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25298 }
25299 else
25300 {
25301 area_left = window_box_left (w, area);
25302 last_x = area_left + window_box_width (w, area);
25303 }
25304 x += area_left;
25305
25306 /* Build a doubly-linked list of glyph_string structures between
25307 head and tail from what we have to draw. Note that the macro
25308 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25309 the reason we use a separate variable `i'. */
25310 i = start;
25311 USE_SAFE_ALLOCA;
25312 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25313 if (tail)
25314 x_reached = tail->x + tail->background_width;
25315 else
25316 x_reached = x;
25317
25318 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25319 the row, redraw some glyphs in front or following the glyph
25320 strings built above. */
25321 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25322 {
25323 struct glyph_string *h, *t;
25324 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25325 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25326 bool check_mouse_face = false;
25327 int dummy_x = 0;
25328
25329 /* If mouse highlighting is on, we may need to draw adjacent
25330 glyphs using mouse-face highlighting. */
25331 if (area == TEXT_AREA && row->mouse_face_p
25332 && hlinfo->mouse_face_beg_row >= 0
25333 && hlinfo->mouse_face_end_row >= 0)
25334 {
25335 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25336
25337 if (row_vpos >= hlinfo->mouse_face_beg_row
25338 && row_vpos <= hlinfo->mouse_face_end_row)
25339 {
25340 check_mouse_face = true;
25341 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25342 ? hlinfo->mouse_face_beg_col : 0;
25343 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25344 ? hlinfo->mouse_face_end_col
25345 : row->used[TEXT_AREA];
25346 }
25347 }
25348
25349 /* Compute overhangs for all glyph strings. */
25350 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25351 for (s = head; s; s = s->next)
25352 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25353
25354 /* Prepend glyph strings for glyphs in front of the first glyph
25355 string that are overwritten because of the first glyph
25356 string's left overhang. The background of all strings
25357 prepended must be drawn because the first glyph string
25358 draws over it. */
25359 i = left_overwritten (head);
25360 if (i >= 0)
25361 {
25362 enum draw_glyphs_face overlap_hl;
25363
25364 /* If this row contains mouse highlighting, attempt to draw
25365 the overlapped glyphs with the correct highlight. This
25366 code fails if the overlap encompasses more than one glyph
25367 and mouse-highlight spans only some of these glyphs.
25368 However, making it work perfectly involves a lot more
25369 code, and I don't know if the pathological case occurs in
25370 practice, so we'll stick to this for now. --- cyd */
25371 if (check_mouse_face
25372 && mouse_beg_col < start && mouse_end_col > i)
25373 overlap_hl = DRAW_MOUSE_FACE;
25374 else
25375 overlap_hl = DRAW_NORMAL_TEXT;
25376
25377 if (hl != overlap_hl)
25378 clip_head = head;
25379 j = i;
25380 BUILD_GLYPH_STRINGS (j, start, h, t,
25381 overlap_hl, dummy_x, last_x);
25382 start = i;
25383 compute_overhangs_and_x (t, head->x, true);
25384 prepend_glyph_string_lists (&head, &tail, h, t);
25385 if (clip_head == NULL)
25386 clip_head = head;
25387 }
25388
25389 /* Prepend glyph strings for glyphs in front of the first glyph
25390 string that overwrite that glyph string because of their
25391 right overhang. For these strings, only the foreground must
25392 be drawn, because it draws over the glyph string at `head'.
25393 The background must not be drawn because this would overwrite
25394 right overhangs of preceding glyphs for which no glyph
25395 strings exist. */
25396 i = left_overwriting (head);
25397 if (i >= 0)
25398 {
25399 enum draw_glyphs_face overlap_hl;
25400
25401 if (check_mouse_face
25402 && mouse_beg_col < start && mouse_end_col > i)
25403 overlap_hl = DRAW_MOUSE_FACE;
25404 else
25405 overlap_hl = DRAW_NORMAL_TEXT;
25406
25407 if (hl == overlap_hl || clip_head == NULL)
25408 clip_head = head;
25409 BUILD_GLYPH_STRINGS (i, start, h, t,
25410 overlap_hl, dummy_x, last_x);
25411 for (s = h; s; s = s->next)
25412 s->background_filled_p = true;
25413 compute_overhangs_and_x (t, head->x, true);
25414 prepend_glyph_string_lists (&head, &tail, h, t);
25415 }
25416
25417 /* Append glyphs strings for glyphs following the last glyph
25418 string tail that are overwritten by tail. The background of
25419 these strings has to be drawn because tail's foreground draws
25420 over it. */
25421 i = right_overwritten (tail);
25422 if (i >= 0)
25423 {
25424 enum draw_glyphs_face overlap_hl;
25425
25426 if (check_mouse_face
25427 && mouse_beg_col < i && mouse_end_col > end)
25428 overlap_hl = DRAW_MOUSE_FACE;
25429 else
25430 overlap_hl = DRAW_NORMAL_TEXT;
25431
25432 if (hl != overlap_hl)
25433 clip_tail = tail;
25434 BUILD_GLYPH_STRINGS (end, i, h, t,
25435 overlap_hl, x, last_x);
25436 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25437 we don't have `end = i;' here. */
25438 compute_overhangs_and_x (h, tail->x + tail->width, false);
25439 append_glyph_string_lists (&head, &tail, h, t);
25440 if (clip_tail == NULL)
25441 clip_tail = tail;
25442 }
25443
25444 /* Append glyph strings for glyphs following the last glyph
25445 string tail that overwrite tail. The foreground of such
25446 glyphs has to be drawn because it writes into the background
25447 of tail. The background must not be drawn because it could
25448 paint over the foreground of following glyphs. */
25449 i = right_overwriting (tail);
25450 if (i >= 0)
25451 {
25452 enum draw_glyphs_face overlap_hl;
25453 if (check_mouse_face
25454 && mouse_beg_col < i && mouse_end_col > end)
25455 overlap_hl = DRAW_MOUSE_FACE;
25456 else
25457 overlap_hl = DRAW_NORMAL_TEXT;
25458
25459 if (hl == overlap_hl || clip_tail == NULL)
25460 clip_tail = tail;
25461 i++; /* We must include the Ith glyph. */
25462 BUILD_GLYPH_STRINGS (end, i, h, t,
25463 overlap_hl, x, last_x);
25464 for (s = h; s; s = s->next)
25465 s->background_filled_p = true;
25466 compute_overhangs_and_x (h, tail->x + tail->width, false);
25467 append_glyph_string_lists (&head, &tail, h, t);
25468 }
25469 if (clip_head || clip_tail)
25470 for (s = head; s; s = s->next)
25471 {
25472 s->clip_head = clip_head;
25473 s->clip_tail = clip_tail;
25474 }
25475 }
25476
25477 /* Draw all strings. */
25478 for (s = head; s; s = s->next)
25479 FRAME_RIF (f)->draw_glyph_string (s);
25480
25481 #ifndef HAVE_NS
25482 /* When focus a sole frame and move horizontally, this clears on_p
25483 causing a failure to erase prev cursor position. */
25484 if (area == TEXT_AREA
25485 && !row->full_width_p
25486 /* When drawing overlapping rows, only the glyph strings'
25487 foreground is drawn, which doesn't erase a cursor
25488 completely. */
25489 && !overlaps)
25490 {
25491 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25492 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25493 : (tail ? tail->x + tail->background_width : x));
25494 x0 -= area_left;
25495 x1 -= area_left;
25496
25497 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25498 row->y, MATRIX_ROW_BOTTOM_Y (row));
25499 }
25500 #endif
25501
25502 /* Value is the x-position up to which drawn, relative to AREA of W.
25503 This doesn't include parts drawn because of overhangs. */
25504 if (row->full_width_p)
25505 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25506 else
25507 x_reached -= area_left;
25508
25509 RELEASE_HDC (hdc, f);
25510
25511 SAFE_FREE ();
25512 return x_reached;
25513 }
25514
25515 /* Expand row matrix if too narrow. Don't expand if area
25516 is not present. */
25517
25518 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25519 { \
25520 if (!it->f->fonts_changed \
25521 && (it->glyph_row->glyphs[area] \
25522 < it->glyph_row->glyphs[area + 1])) \
25523 { \
25524 it->w->ncols_scale_factor++; \
25525 it->f->fonts_changed = true; \
25526 } \
25527 }
25528
25529 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25530 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25531
25532 static void
25533 append_glyph (struct it *it)
25534 {
25535 struct glyph *glyph;
25536 enum glyph_row_area area = it->area;
25537
25538 eassert (it->glyph_row);
25539 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25540
25541 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25542 if (glyph < it->glyph_row->glyphs[area + 1])
25543 {
25544 /* If the glyph row is reversed, we need to prepend the glyph
25545 rather than append it. */
25546 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25547 {
25548 struct glyph *g;
25549
25550 /* Make room for the additional glyph. */
25551 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25552 g[1] = *g;
25553 glyph = it->glyph_row->glyphs[area];
25554 }
25555 glyph->charpos = CHARPOS (it->position);
25556 glyph->object = it->object;
25557 if (it->pixel_width > 0)
25558 {
25559 glyph->pixel_width = it->pixel_width;
25560 glyph->padding_p = false;
25561 }
25562 else
25563 {
25564 /* Assure at least 1-pixel width. Otherwise, cursor can't
25565 be displayed correctly. */
25566 glyph->pixel_width = 1;
25567 glyph->padding_p = true;
25568 }
25569 glyph->ascent = it->ascent;
25570 glyph->descent = it->descent;
25571 glyph->voffset = it->voffset;
25572 glyph->type = CHAR_GLYPH;
25573 glyph->avoid_cursor_p = it->avoid_cursor_p;
25574 glyph->multibyte_p = it->multibyte_p;
25575 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25576 {
25577 /* In R2L rows, the left and the right box edges need to be
25578 drawn in reverse direction. */
25579 glyph->right_box_line_p = it->start_of_box_run_p;
25580 glyph->left_box_line_p = it->end_of_box_run_p;
25581 }
25582 else
25583 {
25584 glyph->left_box_line_p = it->start_of_box_run_p;
25585 glyph->right_box_line_p = it->end_of_box_run_p;
25586 }
25587 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25588 || it->phys_descent > it->descent);
25589 glyph->glyph_not_available_p = it->glyph_not_available_p;
25590 glyph->face_id = it->face_id;
25591 glyph->u.ch = it->char_to_display;
25592 glyph->slice.img = null_glyph_slice;
25593 glyph->font_type = FONT_TYPE_UNKNOWN;
25594 if (it->bidi_p)
25595 {
25596 glyph->resolved_level = it->bidi_it.resolved_level;
25597 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25598 glyph->bidi_type = it->bidi_it.type;
25599 }
25600 else
25601 {
25602 glyph->resolved_level = 0;
25603 glyph->bidi_type = UNKNOWN_BT;
25604 }
25605 ++it->glyph_row->used[area];
25606 }
25607 else
25608 IT_EXPAND_MATRIX_WIDTH (it, area);
25609 }
25610
25611 /* Store one glyph for the composition IT->cmp_it.id in
25612 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25613 non-null. */
25614
25615 static void
25616 append_composite_glyph (struct it *it)
25617 {
25618 struct glyph *glyph;
25619 enum glyph_row_area area = it->area;
25620
25621 eassert (it->glyph_row);
25622
25623 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25624 if (glyph < it->glyph_row->glyphs[area + 1])
25625 {
25626 /* If the glyph row is reversed, we need to prepend the glyph
25627 rather than append it. */
25628 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25629 {
25630 struct glyph *g;
25631
25632 /* Make room for the new glyph. */
25633 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25634 g[1] = *g;
25635 glyph = it->glyph_row->glyphs[it->area];
25636 }
25637 glyph->charpos = it->cmp_it.charpos;
25638 glyph->object = it->object;
25639 glyph->pixel_width = it->pixel_width;
25640 glyph->ascent = it->ascent;
25641 glyph->descent = it->descent;
25642 glyph->voffset = it->voffset;
25643 glyph->type = COMPOSITE_GLYPH;
25644 if (it->cmp_it.ch < 0)
25645 {
25646 glyph->u.cmp.automatic = false;
25647 glyph->u.cmp.id = it->cmp_it.id;
25648 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25649 }
25650 else
25651 {
25652 glyph->u.cmp.automatic = true;
25653 glyph->u.cmp.id = it->cmp_it.id;
25654 glyph->slice.cmp.from = it->cmp_it.from;
25655 glyph->slice.cmp.to = it->cmp_it.to - 1;
25656 }
25657 glyph->avoid_cursor_p = it->avoid_cursor_p;
25658 glyph->multibyte_p = it->multibyte_p;
25659 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25660 {
25661 /* In R2L rows, the left and the right box edges need to be
25662 drawn in reverse direction. */
25663 glyph->right_box_line_p = it->start_of_box_run_p;
25664 glyph->left_box_line_p = it->end_of_box_run_p;
25665 }
25666 else
25667 {
25668 glyph->left_box_line_p = it->start_of_box_run_p;
25669 glyph->right_box_line_p = it->end_of_box_run_p;
25670 }
25671 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25672 || it->phys_descent > it->descent);
25673 glyph->padding_p = false;
25674 glyph->glyph_not_available_p = false;
25675 glyph->face_id = it->face_id;
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 /* Change IT->ascent and IT->height according to the setting of
25691 IT->voffset. */
25692
25693 static void
25694 take_vertical_position_into_account (struct it *it)
25695 {
25696 if (it->voffset)
25697 {
25698 if (it->voffset < 0)
25699 /* Increase the ascent so that we can display the text higher
25700 in the line. */
25701 it->ascent -= it->voffset;
25702 else
25703 /* Increase the descent so that we can display the text lower
25704 in the line. */
25705 it->descent += it->voffset;
25706 }
25707 }
25708
25709
25710 /* Produce glyphs/get display metrics for the image IT is loaded with.
25711 See the description of struct display_iterator in dispextern.h for
25712 an overview of struct display_iterator. */
25713
25714 static void
25715 produce_image_glyph (struct it *it)
25716 {
25717 struct image *img;
25718 struct face *face;
25719 int glyph_ascent, crop;
25720 struct glyph_slice slice;
25721
25722 eassert (it->what == IT_IMAGE);
25723
25724 face = FACE_FROM_ID (it->f, it->face_id);
25725 eassert (face);
25726 /* Make sure X resources of the face is loaded. */
25727 prepare_face_for_display (it->f, face);
25728
25729 if (it->image_id < 0)
25730 {
25731 /* Fringe bitmap. */
25732 it->ascent = it->phys_ascent = 0;
25733 it->descent = it->phys_descent = 0;
25734 it->pixel_width = 0;
25735 it->nglyphs = 0;
25736 return;
25737 }
25738
25739 img = IMAGE_FROM_ID (it->f, it->image_id);
25740 eassert (img);
25741 /* Make sure X resources of the image is loaded. */
25742 prepare_image_for_display (it->f, img);
25743
25744 slice.x = slice.y = 0;
25745 slice.width = img->width;
25746 slice.height = img->height;
25747
25748 if (INTEGERP (it->slice.x))
25749 slice.x = XINT (it->slice.x);
25750 else if (FLOATP (it->slice.x))
25751 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25752
25753 if (INTEGERP (it->slice.y))
25754 slice.y = XINT (it->slice.y);
25755 else if (FLOATP (it->slice.y))
25756 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25757
25758 if (INTEGERP (it->slice.width))
25759 slice.width = XINT (it->slice.width);
25760 else if (FLOATP (it->slice.width))
25761 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25762
25763 if (INTEGERP (it->slice.height))
25764 slice.height = XINT (it->slice.height);
25765 else if (FLOATP (it->slice.height))
25766 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25767
25768 if (slice.x >= img->width)
25769 slice.x = img->width;
25770 if (slice.y >= img->height)
25771 slice.y = img->height;
25772 if (slice.x + slice.width >= img->width)
25773 slice.width = img->width - slice.x;
25774 if (slice.y + slice.height > img->height)
25775 slice.height = img->height - slice.y;
25776
25777 if (slice.width == 0 || slice.height == 0)
25778 return;
25779
25780 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25781
25782 it->descent = slice.height - glyph_ascent;
25783 if (slice.y == 0)
25784 it->descent += img->vmargin;
25785 if (slice.y + slice.height == img->height)
25786 it->descent += img->vmargin;
25787 it->phys_descent = it->descent;
25788
25789 it->pixel_width = slice.width;
25790 if (slice.x == 0)
25791 it->pixel_width += img->hmargin;
25792 if (slice.x + slice.width == img->width)
25793 it->pixel_width += img->hmargin;
25794
25795 /* It's quite possible for images to have an ascent greater than
25796 their height, so don't get confused in that case. */
25797 if (it->descent < 0)
25798 it->descent = 0;
25799
25800 it->nglyphs = 1;
25801
25802 if (face->box != FACE_NO_BOX)
25803 {
25804 if (face->box_line_width > 0)
25805 {
25806 if (slice.y == 0)
25807 it->ascent += face->box_line_width;
25808 if (slice.y + slice.height == img->height)
25809 it->descent += face->box_line_width;
25810 }
25811
25812 if (it->start_of_box_run_p && slice.x == 0)
25813 it->pixel_width += eabs (face->box_line_width);
25814 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25815 it->pixel_width += eabs (face->box_line_width);
25816 }
25817
25818 take_vertical_position_into_account (it);
25819
25820 /* Automatically crop wide image glyphs at right edge so we can
25821 draw the cursor on same display row. */
25822 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25823 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25824 {
25825 it->pixel_width -= crop;
25826 slice.width -= crop;
25827 }
25828
25829 if (it->glyph_row)
25830 {
25831 struct glyph *glyph;
25832 enum glyph_row_area area = it->area;
25833
25834 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25835 if (it->glyph_row->reversed_p)
25836 {
25837 struct glyph *g;
25838
25839 /* Make room for the new glyph. */
25840 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25841 g[1] = *g;
25842 glyph = it->glyph_row->glyphs[it->area];
25843 }
25844 if (glyph < it->glyph_row->glyphs[area + 1])
25845 {
25846 glyph->charpos = CHARPOS (it->position);
25847 glyph->object = it->object;
25848 glyph->pixel_width = it->pixel_width;
25849 glyph->ascent = glyph_ascent;
25850 glyph->descent = it->descent;
25851 glyph->voffset = it->voffset;
25852 glyph->type = IMAGE_GLYPH;
25853 glyph->avoid_cursor_p = it->avoid_cursor_p;
25854 glyph->multibyte_p = it->multibyte_p;
25855 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25856 {
25857 /* In R2L rows, the left and the right box edges need to be
25858 drawn in reverse direction. */
25859 glyph->right_box_line_p = it->start_of_box_run_p;
25860 glyph->left_box_line_p = it->end_of_box_run_p;
25861 }
25862 else
25863 {
25864 glyph->left_box_line_p = it->start_of_box_run_p;
25865 glyph->right_box_line_p = it->end_of_box_run_p;
25866 }
25867 glyph->overlaps_vertically_p = false;
25868 glyph->padding_p = false;
25869 glyph->glyph_not_available_p = false;
25870 glyph->face_id = it->face_id;
25871 glyph->u.img_id = img->id;
25872 glyph->slice.img = slice;
25873 glyph->font_type = FONT_TYPE_UNKNOWN;
25874 if (it->bidi_p)
25875 {
25876 glyph->resolved_level = it->bidi_it.resolved_level;
25877 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25878 glyph->bidi_type = it->bidi_it.type;
25879 }
25880 ++it->glyph_row->used[area];
25881 }
25882 else
25883 IT_EXPAND_MATRIX_WIDTH (it, area);
25884 }
25885 }
25886
25887
25888 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25889 of the glyph, WIDTH and HEIGHT are the width and height of the
25890 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25891
25892 static void
25893 append_stretch_glyph (struct it *it, Lisp_Object object,
25894 int width, int height, int ascent)
25895 {
25896 struct glyph *glyph;
25897 enum glyph_row_area area = it->area;
25898
25899 eassert (ascent >= 0 && ascent <= height);
25900
25901 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25902 if (glyph < it->glyph_row->glyphs[area + 1])
25903 {
25904 /* If the glyph row is reversed, we need to prepend the glyph
25905 rather than append it. */
25906 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25907 {
25908 struct glyph *g;
25909
25910 /* Make room for the additional glyph. */
25911 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25912 g[1] = *g;
25913 glyph = it->glyph_row->glyphs[area];
25914
25915 /* Decrease the width of the first glyph of the row that
25916 begins before first_visible_x (e.g., due to hscroll).
25917 This is so the overall width of the row becomes smaller
25918 by the scroll amount, and the stretch glyph appended by
25919 extend_face_to_end_of_line will be wider, to shift the
25920 row glyphs to the right. (In L2R rows, the corresponding
25921 left-shift effect is accomplished by setting row->x to a
25922 negative value, which won't work with R2L rows.)
25923
25924 This must leave us with a positive value of WIDTH, since
25925 otherwise the call to move_it_in_display_line_to at the
25926 beginning of display_line would have got past the entire
25927 first glyph, and then it->current_x would have been
25928 greater or equal to it->first_visible_x. */
25929 if (it->current_x < it->first_visible_x)
25930 width -= it->first_visible_x - it->current_x;
25931 eassert (width > 0);
25932 }
25933 glyph->charpos = CHARPOS (it->position);
25934 glyph->object = object;
25935 glyph->pixel_width = width;
25936 glyph->ascent = ascent;
25937 glyph->descent = height - ascent;
25938 glyph->voffset = it->voffset;
25939 glyph->type = STRETCH_GLYPH;
25940 glyph->avoid_cursor_p = it->avoid_cursor_p;
25941 glyph->multibyte_p = it->multibyte_p;
25942 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25943 {
25944 /* In R2L rows, the left and the right box edges need to be
25945 drawn in reverse direction. */
25946 glyph->right_box_line_p = it->start_of_box_run_p;
25947 glyph->left_box_line_p = it->end_of_box_run_p;
25948 }
25949 else
25950 {
25951 glyph->left_box_line_p = it->start_of_box_run_p;
25952 glyph->right_box_line_p = it->end_of_box_run_p;
25953 }
25954 glyph->overlaps_vertically_p = false;
25955 glyph->padding_p = false;
25956 glyph->glyph_not_available_p = false;
25957 glyph->face_id = it->face_id;
25958 glyph->u.stretch.ascent = ascent;
25959 glyph->u.stretch.height = height;
25960 glyph->slice.img = null_glyph_slice;
25961 glyph->font_type = FONT_TYPE_UNKNOWN;
25962 if (it->bidi_p)
25963 {
25964 glyph->resolved_level = it->bidi_it.resolved_level;
25965 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25966 glyph->bidi_type = it->bidi_it.type;
25967 }
25968 else
25969 {
25970 glyph->resolved_level = 0;
25971 glyph->bidi_type = UNKNOWN_BT;
25972 }
25973 ++it->glyph_row->used[area];
25974 }
25975 else
25976 IT_EXPAND_MATRIX_WIDTH (it, area);
25977 }
25978
25979 #endif /* HAVE_WINDOW_SYSTEM */
25980
25981 /* Produce a stretch glyph for iterator IT. IT->object is the value
25982 of the glyph property displayed. The value must be a list
25983 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25984 being recognized:
25985
25986 1. `:width WIDTH' specifies that the space should be WIDTH *
25987 canonical char width wide. WIDTH may be an integer or floating
25988 point number.
25989
25990 2. `:relative-width FACTOR' specifies that the width of the stretch
25991 should be computed from the width of the first character having the
25992 `glyph' property, and should be FACTOR times that width.
25993
25994 3. `:align-to HPOS' specifies that the space should be wide enough
25995 to reach HPOS, a value in canonical character units.
25996
25997 Exactly one of the above pairs must be present.
25998
25999 4. `:height HEIGHT' specifies that the height of the stretch produced
26000 should be HEIGHT, measured in canonical character units.
26001
26002 5. `:relative-height FACTOR' specifies that the height of the
26003 stretch should be FACTOR times the height of the characters having
26004 the glyph property.
26005
26006 Either none or exactly one of 4 or 5 must be present.
26007
26008 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26009 of the stretch should be used for the ascent of the stretch.
26010 ASCENT must be in the range 0 <= ASCENT <= 100. */
26011
26012 void
26013 produce_stretch_glyph (struct it *it)
26014 {
26015 /* (space :width WIDTH :height HEIGHT ...) */
26016 Lisp_Object prop, plist;
26017 int width = 0, height = 0, align_to = -1;
26018 bool zero_width_ok_p = false;
26019 double tem;
26020 struct font *font = NULL;
26021
26022 #ifdef HAVE_WINDOW_SYSTEM
26023 int ascent = 0;
26024 bool zero_height_ok_p = false;
26025
26026 if (FRAME_WINDOW_P (it->f))
26027 {
26028 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26029 font = face->font ? face->font : FRAME_FONT (it->f);
26030 prepare_face_for_display (it->f, face);
26031 }
26032 #endif
26033
26034 /* List should start with `space'. */
26035 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26036 plist = XCDR (it->object);
26037
26038 /* Compute the width of the stretch. */
26039 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26040 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26041 {
26042 /* Absolute width `:width WIDTH' specified and valid. */
26043 zero_width_ok_p = true;
26044 width = (int)tem;
26045 }
26046 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26047 {
26048 /* Relative width `:relative-width FACTOR' specified and valid.
26049 Compute the width of the characters having the `glyph'
26050 property. */
26051 struct it it2;
26052 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26053
26054 it2 = *it;
26055 if (it->multibyte_p)
26056 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26057 else
26058 {
26059 it2.c = it2.char_to_display = *p, it2.len = 1;
26060 if (! ASCII_CHAR_P (it2.c))
26061 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26062 }
26063
26064 it2.glyph_row = NULL;
26065 it2.what = IT_CHARACTER;
26066 PRODUCE_GLYPHS (&it2);
26067 width = NUMVAL (prop) * it2.pixel_width;
26068 }
26069 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26070 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26071 &align_to))
26072 {
26073 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26074 align_to = (align_to < 0
26075 ? 0
26076 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26077 else if (align_to < 0)
26078 align_to = window_box_left_offset (it->w, TEXT_AREA);
26079 width = max (0, (int)tem + align_to - it->current_x);
26080 zero_width_ok_p = true;
26081 }
26082 else
26083 /* Nothing specified -> width defaults to canonical char width. */
26084 width = FRAME_COLUMN_WIDTH (it->f);
26085
26086 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26087 width = 1;
26088
26089 #ifdef HAVE_WINDOW_SYSTEM
26090 /* Compute height. */
26091 if (FRAME_WINDOW_P (it->f))
26092 {
26093 int default_height = normal_char_height (font, ' ');
26094
26095 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26096 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26097 {
26098 height = (int)tem;
26099 zero_height_ok_p = true;
26100 }
26101 else if (prop = Fplist_get (plist, QCrelative_height),
26102 NUMVAL (prop) > 0)
26103 height = default_height * NUMVAL (prop);
26104 else
26105 height = default_height;
26106
26107 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26108 height = 1;
26109
26110 /* Compute percentage of height used for ascent. If
26111 `:ascent ASCENT' is present and valid, use that. Otherwise,
26112 derive the ascent from the font in use. */
26113 if (prop = Fplist_get (plist, QCascent),
26114 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26115 ascent = height * NUMVAL (prop) / 100.0;
26116 else if (!NILP (prop)
26117 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26118 ascent = min (max (0, (int)tem), height);
26119 else
26120 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26121 }
26122 else
26123 #endif /* HAVE_WINDOW_SYSTEM */
26124 height = 1;
26125
26126 if (width > 0 && it->line_wrap != TRUNCATE
26127 && it->current_x + width > it->last_visible_x)
26128 {
26129 width = it->last_visible_x - it->current_x;
26130 #ifdef HAVE_WINDOW_SYSTEM
26131 /* Subtract one more pixel from the stretch width, but only on
26132 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26133 width -= FRAME_WINDOW_P (it->f);
26134 #endif
26135 }
26136
26137 if (width > 0 && height > 0 && it->glyph_row)
26138 {
26139 Lisp_Object o_object = it->object;
26140 Lisp_Object object = it->stack[it->sp - 1].string;
26141 int n = width;
26142
26143 if (!STRINGP (object))
26144 object = it->w->contents;
26145 #ifdef HAVE_WINDOW_SYSTEM
26146 if (FRAME_WINDOW_P (it->f))
26147 append_stretch_glyph (it, object, width, height, ascent);
26148 else
26149 #endif
26150 {
26151 it->object = object;
26152 it->char_to_display = ' ';
26153 it->pixel_width = it->len = 1;
26154 while (n--)
26155 tty_append_glyph (it);
26156 it->object = o_object;
26157 }
26158 }
26159
26160 it->pixel_width = width;
26161 #ifdef HAVE_WINDOW_SYSTEM
26162 if (FRAME_WINDOW_P (it->f))
26163 {
26164 it->ascent = it->phys_ascent = ascent;
26165 it->descent = it->phys_descent = height - it->ascent;
26166 it->nglyphs = width > 0 && height > 0;
26167 take_vertical_position_into_account (it);
26168 }
26169 else
26170 #endif
26171 it->nglyphs = width;
26172 }
26173
26174 /* Get information about special display element WHAT in an
26175 environment described by IT. WHAT is one of IT_TRUNCATION or
26176 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26177 non-null glyph_row member. This function ensures that fields like
26178 face_id, c, len of IT are left untouched. */
26179
26180 static void
26181 produce_special_glyphs (struct it *it, enum display_element_type what)
26182 {
26183 struct it temp_it;
26184 Lisp_Object gc;
26185 GLYPH glyph;
26186
26187 temp_it = *it;
26188 temp_it.object = Qnil;
26189 memset (&temp_it.current, 0, sizeof temp_it.current);
26190
26191 if (what == IT_CONTINUATION)
26192 {
26193 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26194 if (it->bidi_it.paragraph_dir == R2L)
26195 SET_GLYPH_FROM_CHAR (glyph, '/');
26196 else
26197 SET_GLYPH_FROM_CHAR (glyph, '\\');
26198 if (it->dp
26199 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26200 {
26201 /* FIXME: Should we mirror GC for R2L lines? */
26202 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26203 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26204 }
26205 }
26206 else if (what == IT_TRUNCATION)
26207 {
26208 /* Truncation glyph. */
26209 SET_GLYPH_FROM_CHAR (glyph, '$');
26210 if (it->dp
26211 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26212 {
26213 /* FIXME: Should we mirror GC for R2L lines? */
26214 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26215 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26216 }
26217 }
26218 else
26219 emacs_abort ();
26220
26221 #ifdef HAVE_WINDOW_SYSTEM
26222 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26223 is turned off, we precede the truncation/continuation glyphs by a
26224 stretch glyph whose width is computed such that these special
26225 glyphs are aligned at the window margin, even when very different
26226 fonts are used in different glyph rows. */
26227 if (FRAME_WINDOW_P (temp_it.f)
26228 /* init_iterator calls this with it->glyph_row == NULL, and it
26229 wants only the pixel width of the truncation/continuation
26230 glyphs. */
26231 && temp_it.glyph_row
26232 /* insert_left_trunc_glyphs calls us at the beginning of the
26233 row, and it has its own calculation of the stretch glyph
26234 width. */
26235 && temp_it.glyph_row->used[TEXT_AREA] > 0
26236 && (temp_it.glyph_row->reversed_p
26237 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26238 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26239 {
26240 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26241
26242 if (stretch_width > 0)
26243 {
26244 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26245 struct font *font =
26246 face->font ? face->font : FRAME_FONT (temp_it.f);
26247 int stretch_ascent =
26248 (((temp_it.ascent + temp_it.descent)
26249 * FONT_BASE (font)) / FONT_HEIGHT (font));
26250
26251 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26252 temp_it.ascent + temp_it.descent,
26253 stretch_ascent);
26254 }
26255 }
26256 #endif
26257
26258 temp_it.dp = NULL;
26259 temp_it.what = IT_CHARACTER;
26260 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26261 temp_it.face_id = GLYPH_FACE (glyph);
26262 temp_it.len = CHAR_BYTES (temp_it.c);
26263
26264 PRODUCE_GLYPHS (&temp_it);
26265 it->pixel_width = temp_it.pixel_width;
26266 it->nglyphs = temp_it.nglyphs;
26267 }
26268
26269 #ifdef HAVE_WINDOW_SYSTEM
26270
26271 /* Calculate line-height and line-spacing properties.
26272 An integer value specifies explicit pixel value.
26273 A float value specifies relative value to current face height.
26274 A cons (float . face-name) specifies relative value to
26275 height of specified face font.
26276
26277 Returns height in pixels, or nil. */
26278
26279 static Lisp_Object
26280 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26281 int boff, bool override)
26282 {
26283 Lisp_Object face_name = Qnil;
26284 int ascent, descent, height;
26285
26286 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26287 return val;
26288
26289 if (CONSP (val))
26290 {
26291 face_name = XCAR (val);
26292 val = XCDR (val);
26293 if (!NUMBERP (val))
26294 val = make_number (1);
26295 if (NILP (face_name))
26296 {
26297 height = it->ascent + it->descent;
26298 goto scale;
26299 }
26300 }
26301
26302 if (NILP (face_name))
26303 {
26304 font = FRAME_FONT (it->f);
26305 boff = FRAME_BASELINE_OFFSET (it->f);
26306 }
26307 else if (EQ (face_name, Qt))
26308 {
26309 override = false;
26310 }
26311 else
26312 {
26313 int face_id;
26314 struct face *face;
26315
26316 face_id = lookup_named_face (it->f, face_name, false);
26317 if (face_id < 0)
26318 return make_number (-1);
26319
26320 face = FACE_FROM_ID (it->f, face_id);
26321 font = face->font;
26322 if (font == NULL)
26323 return make_number (-1);
26324 boff = font->baseline_offset;
26325 if (font->vertical_centering)
26326 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26327 }
26328
26329 normal_char_ascent_descent (font, -1, &ascent, &descent);
26330
26331 if (override)
26332 {
26333 it->override_ascent = ascent;
26334 it->override_descent = descent;
26335 it->override_boff = boff;
26336 }
26337
26338 height = ascent + descent;
26339
26340 scale:
26341 if (FLOATP (val))
26342 height = (int)(XFLOAT_DATA (val) * height);
26343 else if (INTEGERP (val))
26344 height *= XINT (val);
26345
26346 return make_number (height);
26347 }
26348
26349
26350 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26351 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26352 and only if this is for a character for which no font was found.
26353
26354 If the display method (it->glyphless_method) is
26355 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26356 length of the acronym or the hexadecimal string, UPPER_XOFF and
26357 UPPER_YOFF are pixel offsets for the upper part of the string,
26358 LOWER_XOFF and LOWER_YOFF are for the lower part.
26359
26360 For the other display methods, LEN through LOWER_YOFF are zero. */
26361
26362 static void
26363 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26364 short upper_xoff, short upper_yoff,
26365 short lower_xoff, short lower_yoff)
26366 {
26367 struct glyph *glyph;
26368 enum glyph_row_area area = it->area;
26369
26370 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26371 if (glyph < it->glyph_row->glyphs[area + 1])
26372 {
26373 /* If the glyph row is reversed, we need to prepend the glyph
26374 rather than append it. */
26375 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26376 {
26377 struct glyph *g;
26378
26379 /* Make room for the additional glyph. */
26380 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26381 g[1] = *g;
26382 glyph = it->glyph_row->glyphs[area];
26383 }
26384 glyph->charpos = CHARPOS (it->position);
26385 glyph->object = it->object;
26386 glyph->pixel_width = it->pixel_width;
26387 glyph->ascent = it->ascent;
26388 glyph->descent = it->descent;
26389 glyph->voffset = it->voffset;
26390 glyph->type = GLYPHLESS_GLYPH;
26391 glyph->u.glyphless.method = it->glyphless_method;
26392 glyph->u.glyphless.for_no_font = for_no_font;
26393 glyph->u.glyphless.len = len;
26394 glyph->u.glyphless.ch = it->c;
26395 glyph->slice.glyphless.upper_xoff = upper_xoff;
26396 glyph->slice.glyphless.upper_yoff = upper_yoff;
26397 glyph->slice.glyphless.lower_xoff = lower_xoff;
26398 glyph->slice.glyphless.lower_yoff = lower_yoff;
26399 glyph->avoid_cursor_p = it->avoid_cursor_p;
26400 glyph->multibyte_p = it->multibyte_p;
26401 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26402 {
26403 /* In R2L rows, the left and the right box edges need to be
26404 drawn in reverse direction. */
26405 glyph->right_box_line_p = it->start_of_box_run_p;
26406 glyph->left_box_line_p = it->end_of_box_run_p;
26407 }
26408 else
26409 {
26410 glyph->left_box_line_p = it->start_of_box_run_p;
26411 glyph->right_box_line_p = it->end_of_box_run_p;
26412 }
26413 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26414 || it->phys_descent > it->descent);
26415 glyph->padding_p = false;
26416 glyph->glyph_not_available_p = false;
26417 glyph->face_id = face_id;
26418 glyph->font_type = FONT_TYPE_UNKNOWN;
26419 if (it->bidi_p)
26420 {
26421 glyph->resolved_level = it->bidi_it.resolved_level;
26422 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26423 glyph->bidi_type = it->bidi_it.type;
26424 }
26425 ++it->glyph_row->used[area];
26426 }
26427 else
26428 IT_EXPAND_MATRIX_WIDTH (it, area);
26429 }
26430
26431
26432 /* Produce a glyph for a glyphless character for iterator IT.
26433 IT->glyphless_method specifies which method to use for displaying
26434 the character. See the description of enum
26435 glyphless_display_method in dispextern.h for the detail.
26436
26437 FOR_NO_FONT is true if and only if this is for a character for
26438 which no font was found. ACRONYM, if non-nil, is an acronym string
26439 for the character. */
26440
26441 static void
26442 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26443 {
26444 int face_id;
26445 struct face *face;
26446 struct font *font;
26447 int base_width, base_height, width, height;
26448 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26449 int len;
26450
26451 /* Get the metrics of the base font. We always refer to the current
26452 ASCII face. */
26453 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26454 font = face->font ? face->font : FRAME_FONT (it->f);
26455 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26456 it->ascent += font->baseline_offset;
26457 it->descent -= font->baseline_offset;
26458 base_height = it->ascent + it->descent;
26459 base_width = font->average_width;
26460
26461 face_id = merge_glyphless_glyph_face (it);
26462
26463 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26464 {
26465 it->pixel_width = THIN_SPACE_WIDTH;
26466 len = 0;
26467 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26468 }
26469 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26470 {
26471 width = CHAR_WIDTH (it->c);
26472 if (width == 0)
26473 width = 1;
26474 else if (width > 4)
26475 width = 4;
26476 it->pixel_width = base_width * width;
26477 len = 0;
26478 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26479 }
26480 else
26481 {
26482 char buf[7];
26483 const char *str;
26484 unsigned int code[6];
26485 int upper_len;
26486 int ascent, descent;
26487 struct font_metrics metrics_upper, metrics_lower;
26488
26489 face = FACE_FROM_ID (it->f, face_id);
26490 font = face->font ? face->font : FRAME_FONT (it->f);
26491 prepare_face_for_display (it->f, face);
26492
26493 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26494 {
26495 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26496 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26497 if (CONSP (acronym))
26498 acronym = XCAR (acronym);
26499 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26500 }
26501 else
26502 {
26503 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26504 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26505 str = buf;
26506 }
26507 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26508 code[len] = font->driver->encode_char (font, str[len]);
26509 upper_len = (len + 1) / 2;
26510 font->driver->text_extents (font, code, upper_len,
26511 &metrics_upper);
26512 font->driver->text_extents (font, code + upper_len, len - upper_len,
26513 &metrics_lower);
26514
26515
26516
26517 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26518 width = max (metrics_upper.width, metrics_lower.width) + 4;
26519 upper_xoff = upper_yoff = 2; /* the typical case */
26520 if (base_width >= width)
26521 {
26522 /* Align the upper to the left, the lower to the right. */
26523 it->pixel_width = base_width;
26524 lower_xoff = base_width - 2 - metrics_lower.width;
26525 }
26526 else
26527 {
26528 /* Center the shorter one. */
26529 it->pixel_width = width;
26530 if (metrics_upper.width >= metrics_lower.width)
26531 lower_xoff = (width - metrics_lower.width) / 2;
26532 else
26533 {
26534 /* FIXME: This code doesn't look right. It formerly was
26535 missing the "lower_xoff = 0;", which couldn't have
26536 been right since it left lower_xoff uninitialized. */
26537 lower_xoff = 0;
26538 upper_xoff = (width - metrics_upper.width) / 2;
26539 }
26540 }
26541
26542 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26543 top, bottom, and between upper and lower strings. */
26544 height = (metrics_upper.ascent + metrics_upper.descent
26545 + metrics_lower.ascent + metrics_lower.descent) + 5;
26546 /* Center vertically.
26547 H:base_height, D:base_descent
26548 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26549
26550 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26551 descent = D - H/2 + h/2;
26552 lower_yoff = descent - 2 - ld;
26553 upper_yoff = lower_yoff - la - 1 - ud; */
26554 ascent = - (it->descent - (base_height + height + 1) / 2);
26555 descent = it->descent - (base_height - height) / 2;
26556 lower_yoff = descent - 2 - metrics_lower.descent;
26557 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26558 - metrics_upper.descent);
26559 /* Don't make the height shorter than the base height. */
26560 if (height > base_height)
26561 {
26562 it->ascent = ascent;
26563 it->descent = descent;
26564 }
26565 }
26566
26567 it->phys_ascent = it->ascent;
26568 it->phys_descent = it->descent;
26569 if (it->glyph_row)
26570 append_glyphless_glyph (it, face_id, for_no_font, len,
26571 upper_xoff, upper_yoff,
26572 lower_xoff, lower_yoff);
26573 it->nglyphs = 1;
26574 take_vertical_position_into_account (it);
26575 }
26576
26577
26578 /* RIF:
26579 Produce glyphs/get display metrics for the display element IT is
26580 loaded with. See the description of struct it in dispextern.h
26581 for an overview of struct it. */
26582
26583 void
26584 x_produce_glyphs (struct it *it)
26585 {
26586 int extra_line_spacing = it->extra_line_spacing;
26587
26588 it->glyph_not_available_p = false;
26589
26590 if (it->what == IT_CHARACTER)
26591 {
26592 XChar2b char2b;
26593 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26594 struct font *font = face->font;
26595 struct font_metrics *pcm = NULL;
26596 int boff; /* Baseline offset. */
26597
26598 if (font == NULL)
26599 {
26600 /* When no suitable font is found, display this character by
26601 the method specified in the first extra slot of
26602 Vglyphless_char_display. */
26603 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26604
26605 eassert (it->what == IT_GLYPHLESS);
26606 produce_glyphless_glyph (it, true,
26607 STRINGP (acronym) ? acronym : Qnil);
26608 goto done;
26609 }
26610
26611 boff = font->baseline_offset;
26612 if (font->vertical_centering)
26613 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26614
26615 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26616 {
26617 it->nglyphs = 1;
26618
26619 if (it->override_ascent >= 0)
26620 {
26621 it->ascent = it->override_ascent;
26622 it->descent = it->override_descent;
26623 boff = it->override_boff;
26624 }
26625 else
26626 {
26627 it->ascent = FONT_BASE (font) + boff;
26628 it->descent = FONT_DESCENT (font) - boff;
26629 }
26630
26631 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26632 {
26633 pcm = get_per_char_metric (font, &char2b);
26634 if (pcm->width == 0
26635 && pcm->rbearing == 0 && pcm->lbearing == 0)
26636 pcm = NULL;
26637 }
26638
26639 if (pcm)
26640 {
26641 it->phys_ascent = pcm->ascent + boff;
26642 it->phys_descent = pcm->descent - boff;
26643 it->pixel_width = pcm->width;
26644 /* Don't use font-global values for ascent and descent
26645 if they result in an exceedingly large line height. */
26646 if (it->override_ascent < 0)
26647 {
26648 if (FONT_TOO_HIGH (font))
26649 {
26650 it->ascent = it->phys_ascent;
26651 it->descent = it->phys_descent;
26652 /* These limitations are enforced by an
26653 assertion near the end of this function. */
26654 if (it->ascent < 0)
26655 it->ascent = 0;
26656 if (it->descent < 0)
26657 it->descent = 0;
26658 }
26659 }
26660 }
26661 else
26662 {
26663 it->glyph_not_available_p = true;
26664 it->phys_ascent = it->ascent;
26665 it->phys_descent = it->descent;
26666 it->pixel_width = font->space_width;
26667 }
26668
26669 if (it->constrain_row_ascent_descent_p)
26670 {
26671 if (it->descent > it->max_descent)
26672 {
26673 it->ascent += it->descent - it->max_descent;
26674 it->descent = it->max_descent;
26675 }
26676 if (it->ascent > it->max_ascent)
26677 {
26678 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26679 it->ascent = it->max_ascent;
26680 }
26681 it->phys_ascent = min (it->phys_ascent, it->ascent);
26682 it->phys_descent = min (it->phys_descent, it->descent);
26683 extra_line_spacing = 0;
26684 }
26685
26686 /* If this is a space inside a region of text with
26687 `space-width' property, change its width. */
26688 bool stretched_p
26689 = it->char_to_display == ' ' && !NILP (it->space_width);
26690 if (stretched_p)
26691 it->pixel_width *= XFLOATINT (it->space_width);
26692
26693 /* If face has a box, add the box thickness to the character
26694 height. If character has a box line to the left and/or
26695 right, add the box line width to the character's width. */
26696 if (face->box != FACE_NO_BOX)
26697 {
26698 int thick = face->box_line_width;
26699
26700 if (thick > 0)
26701 {
26702 it->ascent += thick;
26703 it->descent += thick;
26704 }
26705 else
26706 thick = -thick;
26707
26708 if (it->start_of_box_run_p)
26709 it->pixel_width += thick;
26710 if (it->end_of_box_run_p)
26711 it->pixel_width += thick;
26712 }
26713
26714 /* If face has an overline, add the height of the overline
26715 (1 pixel) and a 1 pixel margin to the character height. */
26716 if (face->overline_p)
26717 it->ascent += overline_margin;
26718
26719 if (it->constrain_row_ascent_descent_p)
26720 {
26721 if (it->ascent > it->max_ascent)
26722 it->ascent = it->max_ascent;
26723 if (it->descent > it->max_descent)
26724 it->descent = it->max_descent;
26725 }
26726
26727 take_vertical_position_into_account (it);
26728
26729 /* If we have to actually produce glyphs, do it. */
26730 if (it->glyph_row)
26731 {
26732 if (stretched_p)
26733 {
26734 /* Translate a space with a `space-width' property
26735 into a stretch glyph. */
26736 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26737 / FONT_HEIGHT (font));
26738 append_stretch_glyph (it, it->object, it->pixel_width,
26739 it->ascent + it->descent, ascent);
26740 }
26741 else
26742 append_glyph (it);
26743
26744 /* If characters with lbearing or rbearing are displayed
26745 in this line, record that fact in a flag of the
26746 glyph row. This is used to optimize X output code. */
26747 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26748 it->glyph_row->contains_overlapping_glyphs_p = true;
26749 }
26750 if (! stretched_p && it->pixel_width == 0)
26751 /* We assure that all visible glyphs have at least 1-pixel
26752 width. */
26753 it->pixel_width = 1;
26754 }
26755 else if (it->char_to_display == '\n')
26756 {
26757 /* A newline has no width, but we need the height of the
26758 line. But if previous part of the line sets a height,
26759 don't increase that height. */
26760
26761 Lisp_Object height;
26762 Lisp_Object total_height = Qnil;
26763
26764 it->override_ascent = -1;
26765 it->pixel_width = 0;
26766 it->nglyphs = 0;
26767
26768 height = get_it_property (it, Qline_height);
26769 /* Split (line-height total-height) list. */
26770 if (CONSP (height)
26771 && CONSP (XCDR (height))
26772 && NILP (XCDR (XCDR (height))))
26773 {
26774 total_height = XCAR (XCDR (height));
26775 height = XCAR (height);
26776 }
26777 height = calc_line_height_property (it, height, font, boff, true);
26778
26779 if (it->override_ascent >= 0)
26780 {
26781 it->ascent = it->override_ascent;
26782 it->descent = it->override_descent;
26783 boff = it->override_boff;
26784 }
26785 else
26786 {
26787 if (FONT_TOO_HIGH (font))
26788 {
26789 it->ascent = font->pixel_size + boff - 1;
26790 it->descent = -boff + 1;
26791 if (it->descent < 0)
26792 it->descent = 0;
26793 }
26794 else
26795 {
26796 it->ascent = FONT_BASE (font) + boff;
26797 it->descent = FONT_DESCENT (font) - boff;
26798 }
26799 }
26800
26801 if (EQ (height, Qt))
26802 {
26803 if (it->descent > it->max_descent)
26804 {
26805 it->ascent += it->descent - it->max_descent;
26806 it->descent = it->max_descent;
26807 }
26808 if (it->ascent > it->max_ascent)
26809 {
26810 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26811 it->ascent = it->max_ascent;
26812 }
26813 it->phys_ascent = min (it->phys_ascent, it->ascent);
26814 it->phys_descent = min (it->phys_descent, it->descent);
26815 it->constrain_row_ascent_descent_p = true;
26816 extra_line_spacing = 0;
26817 }
26818 else
26819 {
26820 Lisp_Object spacing;
26821
26822 it->phys_ascent = it->ascent;
26823 it->phys_descent = it->descent;
26824
26825 if ((it->max_ascent > 0 || it->max_descent > 0)
26826 && face->box != FACE_NO_BOX
26827 && face->box_line_width > 0)
26828 {
26829 it->ascent += face->box_line_width;
26830 it->descent += face->box_line_width;
26831 }
26832 if (!NILP (height)
26833 && XINT (height) > it->ascent + it->descent)
26834 it->ascent = XINT (height) - it->descent;
26835
26836 if (!NILP (total_height))
26837 spacing = calc_line_height_property (it, total_height, font,
26838 boff, false);
26839 else
26840 {
26841 spacing = get_it_property (it, Qline_spacing);
26842 spacing = calc_line_height_property (it, spacing, font,
26843 boff, false);
26844 }
26845 if (INTEGERP (spacing))
26846 {
26847 extra_line_spacing = XINT (spacing);
26848 if (!NILP (total_height))
26849 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26850 }
26851 }
26852 }
26853 else /* i.e. (it->char_to_display == '\t') */
26854 {
26855 if (font->space_width > 0)
26856 {
26857 int tab_width = it->tab_width * font->space_width;
26858 int x = it->current_x + it->continuation_lines_width;
26859 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26860
26861 /* If the distance from the current position to the next tab
26862 stop is less than a space character width, use the
26863 tab stop after that. */
26864 if (next_tab_x - x < font->space_width)
26865 next_tab_x += tab_width;
26866
26867 it->pixel_width = next_tab_x - x;
26868 it->nglyphs = 1;
26869 if (FONT_TOO_HIGH (font))
26870 {
26871 if (get_char_glyph_code (' ', font, &char2b))
26872 {
26873 pcm = get_per_char_metric (font, &char2b);
26874 if (pcm->width == 0
26875 && pcm->rbearing == 0 && pcm->lbearing == 0)
26876 pcm = NULL;
26877 }
26878
26879 if (pcm)
26880 {
26881 it->ascent = pcm->ascent + boff;
26882 it->descent = pcm->descent - boff;
26883 }
26884 else
26885 {
26886 it->ascent = font->pixel_size + boff - 1;
26887 it->descent = -boff + 1;
26888 }
26889 if (it->ascent < 0)
26890 it->ascent = 0;
26891 if (it->descent < 0)
26892 it->descent = 0;
26893 }
26894 else
26895 {
26896 it->ascent = FONT_BASE (font) + boff;
26897 it->descent = FONT_DESCENT (font) - boff;
26898 }
26899 it->phys_ascent = it->ascent;
26900 it->phys_descent = it->descent;
26901
26902 if (it->glyph_row)
26903 {
26904 append_stretch_glyph (it, it->object, it->pixel_width,
26905 it->ascent + it->descent, it->ascent);
26906 }
26907 }
26908 else
26909 {
26910 it->pixel_width = 0;
26911 it->nglyphs = 1;
26912 }
26913 }
26914
26915 if (FONT_TOO_HIGH (font))
26916 {
26917 int font_ascent, font_descent;
26918
26919 /* For very large fonts, where we ignore the declared font
26920 dimensions, and go by per-character metrics instead,
26921 don't let the row ascent and descent values (and the row
26922 height computed from them) be smaller than the "normal"
26923 character metrics. This avoids unpleasant effects
26924 whereby lines on display would change their height
26925 depending on which characters are shown. */
26926 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26927 it->max_ascent = max (it->max_ascent, font_ascent);
26928 it->max_descent = max (it->max_descent, font_descent);
26929 }
26930 }
26931 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26932 {
26933 /* A static composition.
26934
26935 Note: A composition is represented as one glyph in the
26936 glyph matrix. There are no padding glyphs.
26937
26938 Important note: pixel_width, ascent, and descent are the
26939 values of what is drawn by draw_glyphs (i.e. the values of
26940 the overall glyphs composed). */
26941 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26942 int boff; /* baseline offset */
26943 struct composition *cmp = composition_table[it->cmp_it.id];
26944 int glyph_len = cmp->glyph_len;
26945 struct font *font = face->font;
26946
26947 it->nglyphs = 1;
26948
26949 /* If we have not yet calculated pixel size data of glyphs of
26950 the composition for the current face font, calculate them
26951 now. Theoretically, we have to check all fonts for the
26952 glyphs, but that requires much time and memory space. So,
26953 here we check only the font of the first glyph. This may
26954 lead to incorrect display, but it's very rare, and C-l
26955 (recenter-top-bottom) can correct the display anyway. */
26956 if (! cmp->font || cmp->font != font)
26957 {
26958 /* Ascent and descent of the font of the first character
26959 of this composition (adjusted by baseline offset).
26960 Ascent and descent of overall glyphs should not be less
26961 than these, respectively. */
26962 int font_ascent, font_descent, font_height;
26963 /* Bounding box of the overall glyphs. */
26964 int leftmost, rightmost, lowest, highest;
26965 int lbearing, rbearing;
26966 int i, width, ascent, descent;
26967 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26968 XChar2b char2b;
26969 struct font_metrics *pcm;
26970 ptrdiff_t pos;
26971
26972 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26973 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26974 break;
26975 bool right_padded = glyph_len < cmp->glyph_len;
26976 for (i = 0; i < glyph_len; i++)
26977 {
26978 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26979 break;
26980 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26981 }
26982 bool left_padded = i > 0;
26983
26984 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26985 : IT_CHARPOS (*it));
26986 /* If no suitable font is found, use the default font. */
26987 bool font_not_found_p = font == NULL;
26988 if (font_not_found_p)
26989 {
26990 face = face->ascii_face;
26991 font = face->font;
26992 }
26993 boff = font->baseline_offset;
26994 if (font->vertical_centering)
26995 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26996 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26997 font_ascent += boff;
26998 font_descent -= boff;
26999 font_height = font_ascent + font_descent;
27000
27001 cmp->font = font;
27002
27003 pcm = NULL;
27004 if (! font_not_found_p)
27005 {
27006 get_char_face_and_encoding (it->f, c, it->face_id,
27007 &char2b, false);
27008 pcm = get_per_char_metric (font, &char2b);
27009 }
27010
27011 /* Initialize the bounding box. */
27012 if (pcm)
27013 {
27014 width = cmp->glyph_len > 0 ? pcm->width : 0;
27015 ascent = pcm->ascent;
27016 descent = pcm->descent;
27017 lbearing = pcm->lbearing;
27018 rbearing = pcm->rbearing;
27019 }
27020 else
27021 {
27022 width = cmp->glyph_len > 0 ? font->space_width : 0;
27023 ascent = FONT_BASE (font);
27024 descent = FONT_DESCENT (font);
27025 lbearing = 0;
27026 rbearing = width;
27027 }
27028
27029 rightmost = width;
27030 leftmost = 0;
27031 lowest = - descent + boff;
27032 highest = ascent + boff;
27033
27034 if (! font_not_found_p
27035 && font->default_ascent
27036 && CHAR_TABLE_P (Vuse_default_ascent)
27037 && !NILP (Faref (Vuse_default_ascent,
27038 make_number (it->char_to_display))))
27039 highest = font->default_ascent + boff;
27040
27041 /* Draw the first glyph at the normal position. It may be
27042 shifted to right later if some other glyphs are drawn
27043 at the left. */
27044 cmp->offsets[i * 2] = 0;
27045 cmp->offsets[i * 2 + 1] = boff;
27046 cmp->lbearing = lbearing;
27047 cmp->rbearing = rbearing;
27048
27049 /* Set cmp->offsets for the remaining glyphs. */
27050 for (i++; i < glyph_len; i++)
27051 {
27052 int left, right, btm, top;
27053 int ch = COMPOSITION_GLYPH (cmp, i);
27054 int face_id;
27055 struct face *this_face;
27056
27057 if (ch == '\t')
27058 ch = ' ';
27059 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27060 this_face = FACE_FROM_ID (it->f, face_id);
27061 font = this_face->font;
27062
27063 if (font == NULL)
27064 pcm = NULL;
27065 else
27066 {
27067 get_char_face_and_encoding (it->f, ch, face_id,
27068 &char2b, false);
27069 pcm = get_per_char_metric (font, &char2b);
27070 }
27071 if (! pcm)
27072 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27073 else
27074 {
27075 width = pcm->width;
27076 ascent = pcm->ascent;
27077 descent = pcm->descent;
27078 lbearing = pcm->lbearing;
27079 rbearing = pcm->rbearing;
27080 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27081 {
27082 /* Relative composition with or without
27083 alternate chars. */
27084 left = (leftmost + rightmost - width) / 2;
27085 btm = - descent + boff;
27086 if (font->relative_compose
27087 && (! CHAR_TABLE_P (Vignore_relative_composition)
27088 || NILP (Faref (Vignore_relative_composition,
27089 make_number (ch)))))
27090 {
27091
27092 if (- descent >= font->relative_compose)
27093 /* One extra pixel between two glyphs. */
27094 btm = highest + 1;
27095 else if (ascent <= 0)
27096 /* One extra pixel between two glyphs. */
27097 btm = lowest - 1 - ascent - descent;
27098 }
27099 }
27100 else
27101 {
27102 /* A composition rule is specified by an integer
27103 value that encodes global and new reference
27104 points (GREF and NREF). GREF and NREF are
27105 specified by numbers as below:
27106
27107 0---1---2 -- ascent
27108 | |
27109 | |
27110 | |
27111 9--10--11 -- center
27112 | |
27113 ---3---4---5--- baseline
27114 | |
27115 6---7---8 -- descent
27116 */
27117 int rule = COMPOSITION_RULE (cmp, i);
27118 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27119
27120 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27121 grefx = gref % 3, nrefx = nref % 3;
27122 grefy = gref / 3, nrefy = nref / 3;
27123 if (xoff)
27124 xoff = font_height * (xoff - 128) / 256;
27125 if (yoff)
27126 yoff = font_height * (yoff - 128) / 256;
27127
27128 left = (leftmost
27129 + grefx * (rightmost - leftmost) / 2
27130 - nrefx * width / 2
27131 + xoff);
27132
27133 btm = ((grefy == 0 ? highest
27134 : grefy == 1 ? 0
27135 : grefy == 2 ? lowest
27136 : (highest + lowest) / 2)
27137 - (nrefy == 0 ? ascent + descent
27138 : nrefy == 1 ? descent - boff
27139 : nrefy == 2 ? 0
27140 : (ascent + descent) / 2)
27141 + yoff);
27142 }
27143
27144 cmp->offsets[i * 2] = left;
27145 cmp->offsets[i * 2 + 1] = btm + descent;
27146
27147 /* Update the bounding box of the overall glyphs. */
27148 if (width > 0)
27149 {
27150 right = left + width;
27151 if (left < leftmost)
27152 leftmost = left;
27153 if (right > rightmost)
27154 rightmost = right;
27155 }
27156 top = btm + descent + ascent;
27157 if (top > highest)
27158 highest = top;
27159 if (btm < lowest)
27160 lowest = btm;
27161
27162 if (cmp->lbearing > left + lbearing)
27163 cmp->lbearing = left + lbearing;
27164 if (cmp->rbearing < left + rbearing)
27165 cmp->rbearing = left + rbearing;
27166 }
27167 }
27168
27169 /* If there are glyphs whose x-offsets are negative,
27170 shift all glyphs to the right and make all x-offsets
27171 non-negative. */
27172 if (leftmost < 0)
27173 {
27174 for (i = 0; i < cmp->glyph_len; i++)
27175 cmp->offsets[i * 2] -= leftmost;
27176 rightmost -= leftmost;
27177 cmp->lbearing -= leftmost;
27178 cmp->rbearing -= leftmost;
27179 }
27180
27181 if (left_padded && cmp->lbearing < 0)
27182 {
27183 for (i = 0; i < cmp->glyph_len; i++)
27184 cmp->offsets[i * 2] -= cmp->lbearing;
27185 rightmost -= cmp->lbearing;
27186 cmp->rbearing -= cmp->lbearing;
27187 cmp->lbearing = 0;
27188 }
27189 if (right_padded && rightmost < cmp->rbearing)
27190 {
27191 rightmost = cmp->rbearing;
27192 }
27193
27194 cmp->pixel_width = rightmost;
27195 cmp->ascent = highest;
27196 cmp->descent = - lowest;
27197 if (cmp->ascent < font_ascent)
27198 cmp->ascent = font_ascent;
27199 if (cmp->descent < font_descent)
27200 cmp->descent = font_descent;
27201 }
27202
27203 if (it->glyph_row
27204 && (cmp->lbearing < 0
27205 || cmp->rbearing > cmp->pixel_width))
27206 it->glyph_row->contains_overlapping_glyphs_p = true;
27207
27208 it->pixel_width = cmp->pixel_width;
27209 it->ascent = it->phys_ascent = cmp->ascent;
27210 it->descent = it->phys_descent = cmp->descent;
27211 if (face->box != FACE_NO_BOX)
27212 {
27213 int thick = face->box_line_width;
27214
27215 if (thick > 0)
27216 {
27217 it->ascent += thick;
27218 it->descent += thick;
27219 }
27220 else
27221 thick = - thick;
27222
27223 if (it->start_of_box_run_p)
27224 it->pixel_width += thick;
27225 if (it->end_of_box_run_p)
27226 it->pixel_width += thick;
27227 }
27228
27229 /* If face has an overline, add the height of the overline
27230 (1 pixel) and a 1 pixel margin to the character height. */
27231 if (face->overline_p)
27232 it->ascent += overline_margin;
27233
27234 take_vertical_position_into_account (it);
27235 if (it->ascent < 0)
27236 it->ascent = 0;
27237 if (it->descent < 0)
27238 it->descent = 0;
27239
27240 if (it->glyph_row && cmp->glyph_len > 0)
27241 append_composite_glyph (it);
27242 }
27243 else if (it->what == IT_COMPOSITION)
27244 {
27245 /* A dynamic (automatic) composition. */
27246 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27247 Lisp_Object gstring;
27248 struct font_metrics metrics;
27249
27250 it->nglyphs = 1;
27251
27252 gstring = composition_gstring_from_id (it->cmp_it.id);
27253 it->pixel_width
27254 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27255 &metrics);
27256 if (it->glyph_row
27257 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27258 it->glyph_row->contains_overlapping_glyphs_p = true;
27259 it->ascent = it->phys_ascent = metrics.ascent;
27260 it->descent = it->phys_descent = metrics.descent;
27261 if (face->box != FACE_NO_BOX)
27262 {
27263 int thick = face->box_line_width;
27264
27265 if (thick > 0)
27266 {
27267 it->ascent += thick;
27268 it->descent += thick;
27269 }
27270 else
27271 thick = - thick;
27272
27273 if (it->start_of_box_run_p)
27274 it->pixel_width += thick;
27275 if (it->end_of_box_run_p)
27276 it->pixel_width += thick;
27277 }
27278 /* If face has an overline, add the height of the overline
27279 (1 pixel) and a 1 pixel margin to the character height. */
27280 if (face->overline_p)
27281 it->ascent += overline_margin;
27282 take_vertical_position_into_account (it);
27283 if (it->ascent < 0)
27284 it->ascent = 0;
27285 if (it->descent < 0)
27286 it->descent = 0;
27287
27288 if (it->glyph_row)
27289 append_composite_glyph (it);
27290 }
27291 else if (it->what == IT_GLYPHLESS)
27292 produce_glyphless_glyph (it, false, Qnil);
27293 else if (it->what == IT_IMAGE)
27294 produce_image_glyph (it);
27295 else if (it->what == IT_STRETCH)
27296 produce_stretch_glyph (it);
27297
27298 done:
27299 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27300 because this isn't true for images with `:ascent 100'. */
27301 eassert (it->ascent >= 0 && it->descent >= 0);
27302 if (it->area == TEXT_AREA)
27303 it->current_x += it->pixel_width;
27304
27305 if (extra_line_spacing > 0)
27306 {
27307 it->descent += extra_line_spacing;
27308 if (extra_line_spacing > it->max_extra_line_spacing)
27309 it->max_extra_line_spacing = extra_line_spacing;
27310 }
27311
27312 it->max_ascent = max (it->max_ascent, it->ascent);
27313 it->max_descent = max (it->max_descent, it->descent);
27314 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27315 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27316 }
27317
27318 /* EXPORT for RIF:
27319 Output LEN glyphs starting at START at the nominal cursor position.
27320 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27321 being updated, and UPDATED_AREA is the area of that row being updated. */
27322
27323 void
27324 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27325 struct glyph *start, enum glyph_row_area updated_area, int len)
27326 {
27327 int x, hpos, chpos = w->phys_cursor.hpos;
27328
27329 eassert (updated_row);
27330 /* When the window is hscrolled, cursor hpos can legitimately be out
27331 of bounds, but we draw the cursor at the corresponding window
27332 margin in that case. */
27333 if (!updated_row->reversed_p && chpos < 0)
27334 chpos = 0;
27335 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27336 chpos = updated_row->used[TEXT_AREA] - 1;
27337
27338 block_input ();
27339
27340 /* Write glyphs. */
27341
27342 hpos = start - updated_row->glyphs[updated_area];
27343 x = draw_glyphs (w, w->output_cursor.x,
27344 updated_row, updated_area,
27345 hpos, hpos + len,
27346 DRAW_NORMAL_TEXT, 0);
27347
27348 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27349 if (updated_area == TEXT_AREA
27350 && w->phys_cursor_on_p
27351 && w->phys_cursor.vpos == w->output_cursor.vpos
27352 && chpos >= hpos
27353 && chpos < hpos + len)
27354 w->phys_cursor_on_p = false;
27355
27356 unblock_input ();
27357
27358 /* Advance the output cursor. */
27359 w->output_cursor.hpos += len;
27360 w->output_cursor.x = x;
27361 }
27362
27363
27364 /* EXPORT for RIF:
27365 Insert LEN glyphs from START at the nominal cursor position. */
27366
27367 void
27368 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27369 struct glyph *start, enum glyph_row_area updated_area, int len)
27370 {
27371 struct frame *f;
27372 int line_height, shift_by_width, shifted_region_width;
27373 struct glyph_row *row;
27374 struct glyph *glyph;
27375 int frame_x, frame_y;
27376 ptrdiff_t hpos;
27377
27378 eassert (updated_row);
27379 block_input ();
27380 f = XFRAME (WINDOW_FRAME (w));
27381
27382 /* Get the height of the line we are in. */
27383 row = updated_row;
27384 line_height = row->height;
27385
27386 /* Get the width of the glyphs to insert. */
27387 shift_by_width = 0;
27388 for (glyph = start; glyph < start + len; ++glyph)
27389 shift_by_width += glyph->pixel_width;
27390
27391 /* Get the width of the region to shift right. */
27392 shifted_region_width = (window_box_width (w, updated_area)
27393 - w->output_cursor.x
27394 - shift_by_width);
27395
27396 /* Shift right. */
27397 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27398 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27399
27400 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27401 line_height, shift_by_width);
27402
27403 /* Write the glyphs. */
27404 hpos = start - row->glyphs[updated_area];
27405 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27406 hpos, hpos + len,
27407 DRAW_NORMAL_TEXT, 0);
27408
27409 /* Advance the output cursor. */
27410 w->output_cursor.hpos += len;
27411 w->output_cursor.x += shift_by_width;
27412 unblock_input ();
27413 }
27414
27415
27416 /* EXPORT for RIF:
27417 Erase the current text line from the nominal cursor position
27418 (inclusive) to pixel column TO_X (exclusive). The idea is that
27419 everything from TO_X onward is already erased.
27420
27421 TO_X is a pixel position relative to UPDATED_AREA of currently
27422 updated window W. TO_X == -1 means clear to the end of this area. */
27423
27424 void
27425 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27426 enum glyph_row_area updated_area, int to_x)
27427 {
27428 struct frame *f;
27429 int max_x, min_y, max_y;
27430 int from_x, from_y, to_y;
27431
27432 eassert (updated_row);
27433 f = XFRAME (w->frame);
27434
27435 if (updated_row->full_width_p)
27436 max_x = (WINDOW_PIXEL_WIDTH (w)
27437 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27438 else
27439 max_x = window_box_width (w, updated_area);
27440 max_y = window_text_bottom_y (w);
27441
27442 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27443 of window. For TO_X > 0, truncate to end of drawing area. */
27444 if (to_x == 0)
27445 return;
27446 else if (to_x < 0)
27447 to_x = max_x;
27448 else
27449 to_x = min (to_x, max_x);
27450
27451 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27452
27453 /* Notice if the cursor will be cleared by this operation. */
27454 if (!updated_row->full_width_p)
27455 notice_overwritten_cursor (w, updated_area,
27456 w->output_cursor.x, -1,
27457 updated_row->y,
27458 MATRIX_ROW_BOTTOM_Y (updated_row));
27459
27460 from_x = w->output_cursor.x;
27461
27462 /* Translate to frame coordinates. */
27463 if (updated_row->full_width_p)
27464 {
27465 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27466 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27467 }
27468 else
27469 {
27470 int area_left = window_box_left (w, updated_area);
27471 from_x += area_left;
27472 to_x += area_left;
27473 }
27474
27475 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27476 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27477 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27478
27479 /* Prevent inadvertently clearing to end of the X window. */
27480 if (to_x > from_x && to_y > from_y)
27481 {
27482 block_input ();
27483 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27484 to_x - from_x, to_y - from_y);
27485 unblock_input ();
27486 }
27487 }
27488
27489 #endif /* HAVE_WINDOW_SYSTEM */
27490
27491
27492 \f
27493 /***********************************************************************
27494 Cursor types
27495 ***********************************************************************/
27496
27497 /* Value is the internal representation of the specified cursor type
27498 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27499 of the bar cursor. */
27500
27501 static enum text_cursor_kinds
27502 get_specified_cursor_type (Lisp_Object arg, int *width)
27503 {
27504 enum text_cursor_kinds type;
27505
27506 if (NILP (arg))
27507 return NO_CURSOR;
27508
27509 if (EQ (arg, Qbox))
27510 return FILLED_BOX_CURSOR;
27511
27512 if (EQ (arg, Qhollow))
27513 return HOLLOW_BOX_CURSOR;
27514
27515 if (EQ (arg, Qbar))
27516 {
27517 *width = 2;
27518 return BAR_CURSOR;
27519 }
27520
27521 if (CONSP (arg)
27522 && EQ (XCAR (arg), Qbar)
27523 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27524 {
27525 *width = XINT (XCDR (arg));
27526 return BAR_CURSOR;
27527 }
27528
27529 if (EQ (arg, Qhbar))
27530 {
27531 *width = 2;
27532 return HBAR_CURSOR;
27533 }
27534
27535 if (CONSP (arg)
27536 && EQ (XCAR (arg), Qhbar)
27537 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27538 {
27539 *width = XINT (XCDR (arg));
27540 return HBAR_CURSOR;
27541 }
27542
27543 /* Treat anything unknown as "hollow box cursor".
27544 It was bad to signal an error; people have trouble fixing
27545 .Xdefaults with Emacs, when it has something bad in it. */
27546 type = HOLLOW_BOX_CURSOR;
27547
27548 return type;
27549 }
27550
27551 /* Set the default cursor types for specified frame. */
27552 void
27553 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27554 {
27555 int width = 1;
27556 Lisp_Object tem;
27557
27558 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27559 FRAME_CURSOR_WIDTH (f) = width;
27560
27561 /* By default, set up the blink-off state depending on the on-state. */
27562
27563 tem = Fassoc (arg, Vblink_cursor_alist);
27564 if (!NILP (tem))
27565 {
27566 FRAME_BLINK_OFF_CURSOR (f)
27567 = get_specified_cursor_type (XCDR (tem), &width);
27568 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27569 }
27570 else
27571 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27572
27573 /* Make sure the cursor gets redrawn. */
27574 f->cursor_type_changed = true;
27575 }
27576
27577
27578 #ifdef HAVE_WINDOW_SYSTEM
27579
27580 /* Return the cursor we want to be displayed in window W. Return
27581 width of bar/hbar cursor through WIDTH arg. Return with
27582 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27583 (i.e. if the `system caret' should track this cursor).
27584
27585 In a mini-buffer window, we want the cursor only to appear if we
27586 are reading input from this window. For the selected window, we
27587 want the cursor type given by the frame parameter or buffer local
27588 setting of cursor-type. If explicitly marked off, draw no cursor.
27589 In all other cases, we want a hollow box cursor. */
27590
27591 static enum text_cursor_kinds
27592 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27593 bool *active_cursor)
27594 {
27595 struct frame *f = XFRAME (w->frame);
27596 struct buffer *b = XBUFFER (w->contents);
27597 int cursor_type = DEFAULT_CURSOR;
27598 Lisp_Object alt_cursor;
27599 bool non_selected = false;
27600
27601 *active_cursor = true;
27602
27603 /* Echo area */
27604 if (cursor_in_echo_area
27605 && FRAME_HAS_MINIBUF_P (f)
27606 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27607 {
27608 if (w == XWINDOW (echo_area_window))
27609 {
27610 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27611 {
27612 *width = FRAME_CURSOR_WIDTH (f);
27613 return FRAME_DESIRED_CURSOR (f);
27614 }
27615 else
27616 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27617 }
27618
27619 *active_cursor = false;
27620 non_selected = true;
27621 }
27622
27623 /* Detect a nonselected window or nonselected frame. */
27624 else if (w != XWINDOW (f->selected_window)
27625 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27626 {
27627 *active_cursor = false;
27628
27629 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27630 return NO_CURSOR;
27631
27632 non_selected = true;
27633 }
27634
27635 /* Never display a cursor in a window in which cursor-type is nil. */
27636 if (NILP (BVAR (b, cursor_type)))
27637 return NO_CURSOR;
27638
27639 /* Get the normal cursor type for this window. */
27640 if (EQ (BVAR (b, cursor_type), Qt))
27641 {
27642 cursor_type = FRAME_DESIRED_CURSOR (f);
27643 *width = FRAME_CURSOR_WIDTH (f);
27644 }
27645 else
27646 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27647
27648 /* Use cursor-in-non-selected-windows instead
27649 for non-selected window or frame. */
27650 if (non_selected)
27651 {
27652 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27653 if (!EQ (Qt, alt_cursor))
27654 return get_specified_cursor_type (alt_cursor, width);
27655 /* t means modify the normal cursor type. */
27656 if (cursor_type == FILLED_BOX_CURSOR)
27657 cursor_type = HOLLOW_BOX_CURSOR;
27658 else if (cursor_type == BAR_CURSOR && *width > 1)
27659 --*width;
27660 return cursor_type;
27661 }
27662
27663 /* Use normal cursor if not blinked off. */
27664 if (!w->cursor_off_p)
27665 {
27666 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27667 {
27668 if (cursor_type == FILLED_BOX_CURSOR)
27669 {
27670 /* Using a block cursor on large images can be very annoying.
27671 So use a hollow cursor for "large" images.
27672 If image is not transparent (no mask), also use hollow cursor. */
27673 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27674 if (img != NULL && IMAGEP (img->spec))
27675 {
27676 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27677 where N = size of default frame font size.
27678 This should cover most of the "tiny" icons people may use. */
27679 if (!img->mask
27680 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27681 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27682 cursor_type = HOLLOW_BOX_CURSOR;
27683 }
27684 }
27685 else if (cursor_type != NO_CURSOR)
27686 {
27687 /* Display current only supports BOX and HOLLOW cursors for images.
27688 So for now, unconditionally use a HOLLOW cursor when cursor is
27689 not a solid box cursor. */
27690 cursor_type = HOLLOW_BOX_CURSOR;
27691 }
27692 }
27693 return cursor_type;
27694 }
27695
27696 /* Cursor is blinked off, so determine how to "toggle" it. */
27697
27698 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27699 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27700 return get_specified_cursor_type (XCDR (alt_cursor), width);
27701
27702 /* Then see if frame has specified a specific blink off cursor type. */
27703 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27704 {
27705 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27706 return FRAME_BLINK_OFF_CURSOR (f);
27707 }
27708
27709 #if false
27710 /* Some people liked having a permanently visible blinking cursor,
27711 while others had very strong opinions against it. So it was
27712 decided to remove it. KFS 2003-09-03 */
27713
27714 /* Finally perform built-in cursor blinking:
27715 filled box <-> hollow box
27716 wide [h]bar <-> narrow [h]bar
27717 narrow [h]bar <-> no cursor
27718 other type <-> no cursor */
27719
27720 if (cursor_type == FILLED_BOX_CURSOR)
27721 return HOLLOW_BOX_CURSOR;
27722
27723 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27724 {
27725 *width = 1;
27726 return cursor_type;
27727 }
27728 #endif
27729
27730 return NO_CURSOR;
27731 }
27732
27733
27734 /* Notice when the text cursor of window W has been completely
27735 overwritten by a drawing operation that outputs glyphs in AREA
27736 starting at X0 and ending at X1 in the line starting at Y0 and
27737 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27738 the rest of the line after X0 has been written. Y coordinates
27739 are window-relative. */
27740
27741 static void
27742 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27743 int x0, int x1, int y0, int y1)
27744 {
27745 int cx0, cx1, cy0, cy1;
27746 struct glyph_row *row;
27747
27748 if (!w->phys_cursor_on_p)
27749 return;
27750 if (area != TEXT_AREA)
27751 return;
27752
27753 if (w->phys_cursor.vpos < 0
27754 || w->phys_cursor.vpos >= w->current_matrix->nrows
27755 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27756 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27757 return;
27758
27759 if (row->cursor_in_fringe_p)
27760 {
27761 row->cursor_in_fringe_p = false;
27762 draw_fringe_bitmap (w, row, row->reversed_p);
27763 w->phys_cursor_on_p = false;
27764 return;
27765 }
27766
27767 cx0 = w->phys_cursor.x;
27768 cx1 = cx0 + w->phys_cursor_width;
27769 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27770 return;
27771
27772 /* The cursor image will be completely removed from the
27773 screen if the output area intersects the cursor area in
27774 y-direction. When we draw in [y0 y1[, and some part of
27775 the cursor is at y < y0, that part must have been drawn
27776 before. When scrolling, the cursor is erased before
27777 actually scrolling, so we don't come here. When not
27778 scrolling, the rows above the old cursor row must have
27779 changed, and in this case these rows must have written
27780 over the cursor image.
27781
27782 Likewise if part of the cursor is below y1, with the
27783 exception of the cursor being in the first blank row at
27784 the buffer and window end because update_text_area
27785 doesn't draw that row. (Except when it does, but
27786 that's handled in update_text_area.) */
27787
27788 cy0 = w->phys_cursor.y;
27789 cy1 = cy0 + w->phys_cursor_height;
27790 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27791 return;
27792
27793 w->phys_cursor_on_p = false;
27794 }
27795
27796 #endif /* HAVE_WINDOW_SYSTEM */
27797
27798 \f
27799 /************************************************************************
27800 Mouse Face
27801 ************************************************************************/
27802
27803 #ifdef HAVE_WINDOW_SYSTEM
27804
27805 /* EXPORT for RIF:
27806 Fix the display of area AREA of overlapping row ROW in window W
27807 with respect to the overlapping part OVERLAPS. */
27808
27809 void
27810 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27811 enum glyph_row_area area, int overlaps)
27812 {
27813 int i, x;
27814
27815 block_input ();
27816
27817 x = 0;
27818 for (i = 0; i < row->used[area];)
27819 {
27820 if (row->glyphs[area][i].overlaps_vertically_p)
27821 {
27822 int start = i, start_x = x;
27823
27824 do
27825 {
27826 x += row->glyphs[area][i].pixel_width;
27827 ++i;
27828 }
27829 while (i < row->used[area]
27830 && row->glyphs[area][i].overlaps_vertically_p);
27831
27832 draw_glyphs (w, start_x, row, area,
27833 start, i,
27834 DRAW_NORMAL_TEXT, overlaps);
27835 }
27836 else
27837 {
27838 x += row->glyphs[area][i].pixel_width;
27839 ++i;
27840 }
27841 }
27842
27843 unblock_input ();
27844 }
27845
27846
27847 /* EXPORT:
27848 Draw the cursor glyph of window W in glyph row ROW. See the
27849 comment of draw_glyphs for the meaning of HL. */
27850
27851 void
27852 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27853 enum draw_glyphs_face hl)
27854 {
27855 /* If cursor hpos is out of bounds, don't draw garbage. This can
27856 happen in mini-buffer windows when switching between echo area
27857 glyphs and mini-buffer. */
27858 if ((row->reversed_p
27859 ? (w->phys_cursor.hpos >= 0)
27860 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27861 {
27862 bool on_p = w->phys_cursor_on_p;
27863 int x1;
27864 int hpos = w->phys_cursor.hpos;
27865
27866 /* When the window is hscrolled, cursor hpos can legitimately be
27867 out of bounds, but we draw the cursor at the corresponding
27868 window margin in that case. */
27869 if (!row->reversed_p && hpos < 0)
27870 hpos = 0;
27871 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27872 hpos = row->used[TEXT_AREA] - 1;
27873
27874 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27875 hl, 0);
27876 w->phys_cursor_on_p = on_p;
27877
27878 if (hl == DRAW_CURSOR)
27879 w->phys_cursor_width = x1 - w->phys_cursor.x;
27880 /* When we erase the cursor, and ROW is overlapped by other
27881 rows, make sure that these overlapping parts of other rows
27882 are redrawn. */
27883 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27884 {
27885 w->phys_cursor_width = x1 - w->phys_cursor.x;
27886
27887 if (row > w->current_matrix->rows
27888 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27889 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27890 OVERLAPS_ERASED_CURSOR);
27891
27892 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27893 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27894 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27895 OVERLAPS_ERASED_CURSOR);
27896 }
27897 }
27898 }
27899
27900
27901 /* Erase the image of a cursor of window W from the screen. */
27902
27903 void
27904 erase_phys_cursor (struct window *w)
27905 {
27906 struct frame *f = XFRAME (w->frame);
27907 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27908 int hpos = w->phys_cursor.hpos;
27909 int vpos = w->phys_cursor.vpos;
27910 bool mouse_face_here_p = false;
27911 struct glyph_matrix *active_glyphs = w->current_matrix;
27912 struct glyph_row *cursor_row;
27913 struct glyph *cursor_glyph;
27914 enum draw_glyphs_face hl;
27915
27916 /* No cursor displayed or row invalidated => nothing to do on the
27917 screen. */
27918 if (w->phys_cursor_type == NO_CURSOR)
27919 goto mark_cursor_off;
27920
27921 /* VPOS >= active_glyphs->nrows means that window has been resized.
27922 Don't bother to erase the cursor. */
27923 if (vpos >= active_glyphs->nrows)
27924 goto mark_cursor_off;
27925
27926 /* If row containing cursor is marked invalid, there is nothing we
27927 can do. */
27928 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27929 if (!cursor_row->enabled_p)
27930 goto mark_cursor_off;
27931
27932 /* If line spacing is > 0, old cursor may only be partially visible in
27933 window after split-window. So adjust visible height. */
27934 cursor_row->visible_height = min (cursor_row->visible_height,
27935 window_text_bottom_y (w) - cursor_row->y);
27936
27937 /* If row is completely invisible, don't attempt to delete a cursor which
27938 isn't there. This can happen if cursor is at top of a window, and
27939 we switch to a buffer with a header line in that window. */
27940 if (cursor_row->visible_height <= 0)
27941 goto mark_cursor_off;
27942
27943 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27944 if (cursor_row->cursor_in_fringe_p)
27945 {
27946 cursor_row->cursor_in_fringe_p = false;
27947 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27948 goto mark_cursor_off;
27949 }
27950
27951 /* This can happen when the new row is shorter than the old one.
27952 In this case, either draw_glyphs or clear_end_of_line
27953 should have cleared the cursor. Note that we wouldn't be
27954 able to erase the cursor in this case because we don't have a
27955 cursor glyph at hand. */
27956 if ((cursor_row->reversed_p
27957 ? (w->phys_cursor.hpos < 0)
27958 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27959 goto mark_cursor_off;
27960
27961 /* When the window is hscrolled, cursor hpos can legitimately be out
27962 of bounds, but we draw the cursor at the corresponding window
27963 margin in that case. */
27964 if (!cursor_row->reversed_p && hpos < 0)
27965 hpos = 0;
27966 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27967 hpos = cursor_row->used[TEXT_AREA] - 1;
27968
27969 /* If the cursor is in the mouse face area, redisplay that when
27970 we clear the cursor. */
27971 if (! NILP (hlinfo->mouse_face_window)
27972 && coords_in_mouse_face_p (w, hpos, vpos)
27973 /* Don't redraw the cursor's spot in mouse face if it is at the
27974 end of a line (on a newline). The cursor appears there, but
27975 mouse highlighting does not. */
27976 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27977 mouse_face_here_p = true;
27978
27979 /* Maybe clear the display under the cursor. */
27980 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27981 {
27982 int x, y;
27983 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27984 int width;
27985
27986 cursor_glyph = get_phys_cursor_glyph (w);
27987 if (cursor_glyph == NULL)
27988 goto mark_cursor_off;
27989
27990 width = cursor_glyph->pixel_width;
27991 x = w->phys_cursor.x;
27992 if (x < 0)
27993 {
27994 width += x;
27995 x = 0;
27996 }
27997 width = min (width, window_box_width (w, TEXT_AREA) - x);
27998 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27999 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28000
28001 if (width > 0)
28002 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28003 }
28004
28005 /* Erase the cursor by redrawing the character underneath it. */
28006 if (mouse_face_here_p)
28007 hl = DRAW_MOUSE_FACE;
28008 else
28009 hl = DRAW_NORMAL_TEXT;
28010 draw_phys_cursor_glyph (w, cursor_row, hl);
28011
28012 mark_cursor_off:
28013 w->phys_cursor_on_p = false;
28014 w->phys_cursor_type = NO_CURSOR;
28015 }
28016
28017
28018 /* Display or clear cursor of window W. If !ON, clear the cursor.
28019 If ON, display the cursor; where to put the cursor is specified by
28020 HPOS, VPOS, X and Y. */
28021
28022 void
28023 display_and_set_cursor (struct window *w, bool on,
28024 int hpos, int vpos, int x, int y)
28025 {
28026 struct frame *f = XFRAME (w->frame);
28027 int new_cursor_type;
28028 int new_cursor_width;
28029 bool active_cursor;
28030 struct glyph_row *glyph_row;
28031 struct glyph *glyph;
28032
28033 /* This is pointless on invisible frames, and dangerous on garbaged
28034 windows and frames; in the latter case, the frame or window may
28035 be in the midst of changing its size, and x and y may be off the
28036 window. */
28037 if (! FRAME_VISIBLE_P (f)
28038 || FRAME_GARBAGED_P (f)
28039 || vpos >= w->current_matrix->nrows
28040 || hpos >= w->current_matrix->matrix_w)
28041 return;
28042
28043 /* If cursor is off and we want it off, return quickly. */
28044 if (!on && !w->phys_cursor_on_p)
28045 return;
28046
28047 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28048 /* If cursor row is not enabled, we don't really know where to
28049 display the cursor. */
28050 if (!glyph_row->enabled_p)
28051 {
28052 w->phys_cursor_on_p = false;
28053 return;
28054 }
28055
28056 glyph = NULL;
28057 if (!glyph_row->exact_window_width_line_p
28058 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28059 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28060
28061 eassert (input_blocked_p ());
28062
28063 /* Set new_cursor_type to the cursor we want to be displayed. */
28064 new_cursor_type = get_window_cursor_type (w, glyph,
28065 &new_cursor_width, &active_cursor);
28066
28067 /* If cursor is currently being shown and we don't want it to be or
28068 it is in the wrong place, or the cursor type is not what we want,
28069 erase it. */
28070 if (w->phys_cursor_on_p
28071 && (!on
28072 || w->phys_cursor.x != x
28073 || w->phys_cursor.y != y
28074 /* HPOS can be negative in R2L rows whose
28075 exact_window_width_line_p flag is set (i.e. their newline
28076 would "overflow into the fringe"). */
28077 || hpos < 0
28078 || new_cursor_type != w->phys_cursor_type
28079 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28080 && new_cursor_width != w->phys_cursor_width)))
28081 erase_phys_cursor (w);
28082
28083 /* Don't check phys_cursor_on_p here because that flag is only set
28084 to false in some cases where we know that the cursor has been
28085 completely erased, to avoid the extra work of erasing the cursor
28086 twice. In other words, phys_cursor_on_p can be true and the cursor
28087 still not be visible, or it has only been partly erased. */
28088 if (on)
28089 {
28090 w->phys_cursor_ascent = glyph_row->ascent;
28091 w->phys_cursor_height = glyph_row->height;
28092
28093 /* Set phys_cursor_.* before x_draw_.* is called because some
28094 of them may need the information. */
28095 w->phys_cursor.x = x;
28096 w->phys_cursor.y = glyph_row->y;
28097 w->phys_cursor.hpos = hpos;
28098 w->phys_cursor.vpos = vpos;
28099 }
28100
28101 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28102 new_cursor_type, new_cursor_width,
28103 on, active_cursor);
28104 }
28105
28106
28107 /* Switch the display of W's cursor on or off, according to the value
28108 of ON. */
28109
28110 static void
28111 update_window_cursor (struct window *w, bool on)
28112 {
28113 /* Don't update cursor in windows whose frame is in the process
28114 of being deleted. */
28115 if (w->current_matrix)
28116 {
28117 int hpos = w->phys_cursor.hpos;
28118 int vpos = w->phys_cursor.vpos;
28119 struct glyph_row *row;
28120
28121 if (vpos >= w->current_matrix->nrows
28122 || hpos >= w->current_matrix->matrix_w)
28123 return;
28124
28125 row = MATRIX_ROW (w->current_matrix, vpos);
28126
28127 /* When the window is hscrolled, cursor hpos can legitimately be
28128 out of bounds, but we draw the cursor at the corresponding
28129 window margin in that case. */
28130 if (!row->reversed_p && hpos < 0)
28131 hpos = 0;
28132 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28133 hpos = row->used[TEXT_AREA] - 1;
28134
28135 block_input ();
28136 display_and_set_cursor (w, on, hpos, vpos,
28137 w->phys_cursor.x, w->phys_cursor.y);
28138 unblock_input ();
28139 }
28140 }
28141
28142
28143 /* Call update_window_cursor with parameter ON_P on all leaf windows
28144 in the window tree rooted at W. */
28145
28146 static void
28147 update_cursor_in_window_tree (struct window *w, bool on_p)
28148 {
28149 while (w)
28150 {
28151 if (WINDOWP (w->contents))
28152 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28153 else
28154 update_window_cursor (w, on_p);
28155
28156 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28157 }
28158 }
28159
28160
28161 /* EXPORT:
28162 Display the cursor on window W, or clear it, according to ON_P.
28163 Don't change the cursor's position. */
28164
28165 void
28166 x_update_cursor (struct frame *f, bool on_p)
28167 {
28168 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28169 }
28170
28171
28172 /* EXPORT:
28173 Clear the cursor of window W to background color, and mark the
28174 cursor as not shown. This is used when the text where the cursor
28175 is about to be rewritten. */
28176
28177 void
28178 x_clear_cursor (struct window *w)
28179 {
28180 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28181 update_window_cursor (w, false);
28182 }
28183
28184 #endif /* HAVE_WINDOW_SYSTEM */
28185
28186 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28187 and MSDOS. */
28188 static void
28189 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28190 int start_hpos, int end_hpos,
28191 enum draw_glyphs_face draw)
28192 {
28193 #ifdef HAVE_WINDOW_SYSTEM
28194 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28195 {
28196 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28197 return;
28198 }
28199 #endif
28200 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28201 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28202 #endif
28203 }
28204
28205 /* Display the active region described by mouse_face_* according to DRAW. */
28206
28207 static void
28208 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28209 {
28210 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28211 struct frame *f = XFRAME (WINDOW_FRAME (w));
28212
28213 if (/* If window is in the process of being destroyed, don't bother
28214 to do anything. */
28215 w->current_matrix != NULL
28216 /* Don't update mouse highlight if hidden. */
28217 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28218 /* Recognize when we are called to operate on rows that don't exist
28219 anymore. This can happen when a window is split. */
28220 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28221 {
28222 bool phys_cursor_on_p = w->phys_cursor_on_p;
28223 struct glyph_row *row, *first, *last;
28224
28225 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28226 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28227
28228 for (row = first; row <= last && row->enabled_p; ++row)
28229 {
28230 int start_hpos, end_hpos, start_x;
28231
28232 /* For all but the first row, the highlight starts at column 0. */
28233 if (row == first)
28234 {
28235 /* R2L rows have BEG and END in reversed order, but the
28236 screen drawing geometry is always left to right. So
28237 we need to mirror the beginning and end of the
28238 highlighted area in R2L rows. */
28239 if (!row->reversed_p)
28240 {
28241 start_hpos = hlinfo->mouse_face_beg_col;
28242 start_x = hlinfo->mouse_face_beg_x;
28243 }
28244 else if (row == last)
28245 {
28246 start_hpos = hlinfo->mouse_face_end_col;
28247 start_x = hlinfo->mouse_face_end_x;
28248 }
28249 else
28250 {
28251 start_hpos = 0;
28252 start_x = 0;
28253 }
28254 }
28255 else if (row->reversed_p && row == last)
28256 {
28257 start_hpos = hlinfo->mouse_face_end_col;
28258 start_x = hlinfo->mouse_face_end_x;
28259 }
28260 else
28261 {
28262 start_hpos = 0;
28263 start_x = 0;
28264 }
28265
28266 if (row == last)
28267 {
28268 if (!row->reversed_p)
28269 end_hpos = hlinfo->mouse_face_end_col;
28270 else if (row == first)
28271 end_hpos = hlinfo->mouse_face_beg_col;
28272 else
28273 {
28274 end_hpos = row->used[TEXT_AREA];
28275 if (draw == DRAW_NORMAL_TEXT)
28276 row->fill_line_p = true; /* Clear to end of line. */
28277 }
28278 }
28279 else if (row->reversed_p && row == first)
28280 end_hpos = hlinfo->mouse_face_beg_col;
28281 else
28282 {
28283 end_hpos = row->used[TEXT_AREA];
28284 if (draw == DRAW_NORMAL_TEXT)
28285 row->fill_line_p = true; /* Clear to end of line. */
28286 }
28287
28288 if (end_hpos > start_hpos)
28289 {
28290 draw_row_with_mouse_face (w, start_x, row,
28291 start_hpos, end_hpos, draw);
28292
28293 row->mouse_face_p
28294 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28295 }
28296 }
28297
28298 #ifdef HAVE_WINDOW_SYSTEM
28299 /* When we've written over the cursor, arrange for it to
28300 be displayed again. */
28301 if (FRAME_WINDOW_P (f)
28302 && phys_cursor_on_p && !w->phys_cursor_on_p)
28303 {
28304 int hpos = w->phys_cursor.hpos;
28305
28306 /* When the window is hscrolled, cursor hpos can legitimately be
28307 out of bounds, but we draw the cursor at the corresponding
28308 window margin in that case. */
28309 if (!row->reversed_p && hpos < 0)
28310 hpos = 0;
28311 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28312 hpos = row->used[TEXT_AREA] - 1;
28313
28314 block_input ();
28315 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28316 w->phys_cursor.x, w->phys_cursor.y);
28317 unblock_input ();
28318 }
28319 #endif /* HAVE_WINDOW_SYSTEM */
28320 }
28321
28322 #ifdef HAVE_WINDOW_SYSTEM
28323 /* Change the mouse cursor. */
28324 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28325 {
28326 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28327 if (draw == DRAW_NORMAL_TEXT
28328 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28329 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28330 else
28331 #endif
28332 if (draw == DRAW_MOUSE_FACE)
28333 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28334 else
28335 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28336 }
28337 #endif /* HAVE_WINDOW_SYSTEM */
28338 }
28339
28340 /* EXPORT:
28341 Clear out the mouse-highlighted active region.
28342 Redraw it un-highlighted first. Value is true if mouse
28343 face was actually drawn unhighlighted. */
28344
28345 bool
28346 clear_mouse_face (Mouse_HLInfo *hlinfo)
28347 {
28348 bool cleared
28349 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28350 if (cleared)
28351 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28352 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28353 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28354 hlinfo->mouse_face_window = Qnil;
28355 hlinfo->mouse_face_overlay = Qnil;
28356 return cleared;
28357 }
28358
28359 /* Return true if the coordinates HPOS and VPOS on windows W are
28360 within the mouse face on that window. */
28361 static bool
28362 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28363 {
28364 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28365
28366 /* Quickly resolve the easy cases. */
28367 if (!(WINDOWP (hlinfo->mouse_face_window)
28368 && XWINDOW (hlinfo->mouse_face_window) == w))
28369 return false;
28370 if (vpos < hlinfo->mouse_face_beg_row
28371 || vpos > hlinfo->mouse_face_end_row)
28372 return false;
28373 if (vpos > hlinfo->mouse_face_beg_row
28374 && vpos < hlinfo->mouse_face_end_row)
28375 return true;
28376
28377 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28378 {
28379 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28380 {
28381 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28382 return true;
28383 }
28384 else if ((vpos == hlinfo->mouse_face_beg_row
28385 && hpos >= hlinfo->mouse_face_beg_col)
28386 || (vpos == hlinfo->mouse_face_end_row
28387 && hpos < hlinfo->mouse_face_end_col))
28388 return true;
28389 }
28390 else
28391 {
28392 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28393 {
28394 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28395 return true;
28396 }
28397 else if ((vpos == hlinfo->mouse_face_beg_row
28398 && hpos <= hlinfo->mouse_face_beg_col)
28399 || (vpos == hlinfo->mouse_face_end_row
28400 && hpos > hlinfo->mouse_face_end_col))
28401 return true;
28402 }
28403 return false;
28404 }
28405
28406
28407 /* EXPORT:
28408 True if physical cursor of window W is within mouse face. */
28409
28410 bool
28411 cursor_in_mouse_face_p (struct window *w)
28412 {
28413 int hpos = w->phys_cursor.hpos;
28414 int vpos = w->phys_cursor.vpos;
28415 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28416
28417 /* When the window is hscrolled, cursor hpos can legitimately be out
28418 of bounds, but we draw the cursor at the corresponding window
28419 margin in that case. */
28420 if (!row->reversed_p && hpos < 0)
28421 hpos = 0;
28422 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28423 hpos = row->used[TEXT_AREA] - 1;
28424
28425 return coords_in_mouse_face_p (w, hpos, vpos);
28426 }
28427
28428
28429 \f
28430 /* Find the glyph rows START_ROW and END_ROW of window W that display
28431 characters between buffer positions START_CHARPOS and END_CHARPOS
28432 (excluding END_CHARPOS). DISP_STRING is a display string that
28433 covers these buffer positions. This is similar to
28434 row_containing_pos, but is more accurate when bidi reordering makes
28435 buffer positions change non-linearly with glyph rows. */
28436 static void
28437 rows_from_pos_range (struct window *w,
28438 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28439 Lisp_Object disp_string,
28440 struct glyph_row **start, struct glyph_row **end)
28441 {
28442 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28443 int last_y = window_text_bottom_y (w);
28444 struct glyph_row *row;
28445
28446 *start = NULL;
28447 *end = NULL;
28448
28449 while (!first->enabled_p
28450 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28451 first++;
28452
28453 /* Find the START row. */
28454 for (row = first;
28455 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28456 row++)
28457 {
28458 /* A row can potentially be the START row if the range of the
28459 characters it displays intersects the range
28460 [START_CHARPOS..END_CHARPOS). */
28461 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28462 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28463 /* See the commentary in row_containing_pos, for the
28464 explanation of the complicated way to check whether
28465 some position is beyond the end of the characters
28466 displayed by a row. */
28467 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28468 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28469 && !row->ends_at_zv_p
28470 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28471 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28472 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28473 && !row->ends_at_zv_p
28474 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28475 {
28476 /* Found a candidate row. Now make sure at least one of the
28477 glyphs it displays has a charpos from the range
28478 [START_CHARPOS..END_CHARPOS).
28479
28480 This is not obvious because bidi reordering could make
28481 buffer positions of a row be 1,2,3,102,101,100, and if we
28482 want to highlight characters in [50..60), we don't want
28483 this row, even though [50..60) does intersect [1..103),
28484 the range of character positions given by the row's start
28485 and end positions. */
28486 struct glyph *g = row->glyphs[TEXT_AREA];
28487 struct glyph *e = g + row->used[TEXT_AREA];
28488
28489 while (g < e)
28490 {
28491 if (((BUFFERP (g->object) || NILP (g->object))
28492 && start_charpos <= g->charpos && g->charpos < end_charpos)
28493 /* A glyph that comes from DISP_STRING is by
28494 definition to be highlighted. */
28495 || EQ (g->object, disp_string))
28496 *start = row;
28497 g++;
28498 }
28499 if (*start)
28500 break;
28501 }
28502 }
28503
28504 /* Find the END row. */
28505 if (!*start
28506 /* If the last row is partially visible, start looking for END
28507 from that row, instead of starting from FIRST. */
28508 && !(row->enabled_p
28509 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28510 row = first;
28511 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28512 {
28513 struct glyph_row *next = row + 1;
28514 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28515
28516 if (!next->enabled_p
28517 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28518 /* The first row >= START whose range of displayed characters
28519 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28520 is the row END + 1. */
28521 || (start_charpos < next_start
28522 && end_charpos < next_start)
28523 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28524 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28525 && !next->ends_at_zv_p
28526 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28527 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28528 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28529 && !next->ends_at_zv_p
28530 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28531 {
28532 *end = row;
28533 break;
28534 }
28535 else
28536 {
28537 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28538 but none of the characters it displays are in the range, it is
28539 also END + 1. */
28540 struct glyph *g = next->glyphs[TEXT_AREA];
28541 struct glyph *s = g;
28542 struct glyph *e = g + next->used[TEXT_AREA];
28543
28544 while (g < e)
28545 {
28546 if (((BUFFERP (g->object) || NILP (g->object))
28547 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28548 /* If the buffer position of the first glyph in
28549 the row is equal to END_CHARPOS, it means
28550 the last character to be highlighted is the
28551 newline of ROW, and we must consider NEXT as
28552 END, not END+1. */
28553 || (((!next->reversed_p && g == s)
28554 || (next->reversed_p && g == e - 1))
28555 && (g->charpos == end_charpos
28556 /* Special case for when NEXT is an
28557 empty line at ZV. */
28558 || (g->charpos == -1
28559 && !row->ends_at_zv_p
28560 && next_start == end_charpos)))))
28561 /* A glyph that comes from DISP_STRING is by
28562 definition to be highlighted. */
28563 || EQ (g->object, disp_string))
28564 break;
28565 g++;
28566 }
28567 if (g == e)
28568 {
28569 *end = row;
28570 break;
28571 }
28572 /* The first row that ends at ZV must be the last to be
28573 highlighted. */
28574 else if (next->ends_at_zv_p)
28575 {
28576 *end = next;
28577 break;
28578 }
28579 }
28580 }
28581 }
28582
28583 /* This function sets the mouse_face_* elements of HLINFO, assuming
28584 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28585 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28586 for the overlay or run of text properties specifying the mouse
28587 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28588 before-string and after-string that must also be highlighted.
28589 DISP_STRING, if non-nil, is a display string that may cover some
28590 or all of the highlighted text. */
28591
28592 static void
28593 mouse_face_from_buffer_pos (Lisp_Object window,
28594 Mouse_HLInfo *hlinfo,
28595 ptrdiff_t mouse_charpos,
28596 ptrdiff_t start_charpos,
28597 ptrdiff_t end_charpos,
28598 Lisp_Object before_string,
28599 Lisp_Object after_string,
28600 Lisp_Object disp_string)
28601 {
28602 struct window *w = XWINDOW (window);
28603 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28604 struct glyph_row *r1, *r2;
28605 struct glyph *glyph, *end;
28606 ptrdiff_t ignore, pos;
28607 int x;
28608
28609 eassert (NILP (disp_string) || STRINGP (disp_string));
28610 eassert (NILP (before_string) || STRINGP (before_string));
28611 eassert (NILP (after_string) || STRINGP (after_string));
28612
28613 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28614 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28615 if (r1 == NULL)
28616 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28617 /* If the before-string or display-string contains newlines,
28618 rows_from_pos_range skips to its last row. Move back. */
28619 if (!NILP (before_string) || !NILP (disp_string))
28620 {
28621 struct glyph_row *prev;
28622 while ((prev = r1 - 1, prev >= first)
28623 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28624 && prev->used[TEXT_AREA] > 0)
28625 {
28626 struct glyph *beg = prev->glyphs[TEXT_AREA];
28627 glyph = beg + prev->used[TEXT_AREA];
28628 while (--glyph >= beg && NILP (glyph->object));
28629 if (glyph < beg
28630 || !(EQ (glyph->object, before_string)
28631 || EQ (glyph->object, disp_string)))
28632 break;
28633 r1 = prev;
28634 }
28635 }
28636 if (r2 == NULL)
28637 {
28638 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28639 hlinfo->mouse_face_past_end = true;
28640 }
28641 else if (!NILP (after_string))
28642 {
28643 /* If the after-string has newlines, advance to its last row. */
28644 struct glyph_row *next;
28645 struct glyph_row *last
28646 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28647
28648 for (next = r2 + 1;
28649 next <= last
28650 && next->used[TEXT_AREA] > 0
28651 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28652 ++next)
28653 r2 = next;
28654 }
28655 /* The rest of the display engine assumes that mouse_face_beg_row is
28656 either above mouse_face_end_row or identical to it. But with
28657 bidi-reordered continued lines, the row for START_CHARPOS could
28658 be below the row for END_CHARPOS. If so, swap the rows and store
28659 them in correct order. */
28660 if (r1->y > r2->y)
28661 {
28662 struct glyph_row *tem = r2;
28663
28664 r2 = r1;
28665 r1 = tem;
28666 }
28667
28668 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28669 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28670
28671 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28672 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28673 could be anywhere in the row and in any order. The strategy
28674 below is to find the leftmost and the rightmost glyph that
28675 belongs to either of these 3 strings, or whose position is
28676 between START_CHARPOS and END_CHARPOS, and highlight all the
28677 glyphs between those two. This may cover more than just the text
28678 between START_CHARPOS and END_CHARPOS if the range of characters
28679 strides the bidi level boundary, e.g. if the beginning is in R2L
28680 text while the end is in L2R text or vice versa. */
28681 if (!r1->reversed_p)
28682 {
28683 /* This row is in a left to right paragraph. Scan it left to
28684 right. */
28685 glyph = r1->glyphs[TEXT_AREA];
28686 end = glyph + r1->used[TEXT_AREA];
28687 x = r1->x;
28688
28689 /* Skip truncation glyphs at the start of the glyph row. */
28690 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28691 for (; glyph < end
28692 && NILP (glyph->object)
28693 && glyph->charpos < 0;
28694 ++glyph)
28695 x += glyph->pixel_width;
28696
28697 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28698 or DISP_STRING, and the first glyph from buffer whose
28699 position is between START_CHARPOS and END_CHARPOS. */
28700 for (; glyph < end
28701 && !NILP (glyph->object)
28702 && !EQ (glyph->object, disp_string)
28703 && !(BUFFERP (glyph->object)
28704 && (glyph->charpos >= start_charpos
28705 && glyph->charpos < end_charpos));
28706 ++glyph)
28707 {
28708 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28709 are present at buffer positions between START_CHARPOS and
28710 END_CHARPOS, or if they come from an overlay. */
28711 if (EQ (glyph->object, before_string))
28712 {
28713 pos = string_buffer_position (before_string,
28714 start_charpos);
28715 /* If pos == 0, it means before_string came from an
28716 overlay, not from a buffer position. */
28717 if (!pos || (pos >= start_charpos && pos < end_charpos))
28718 break;
28719 }
28720 else if (EQ (glyph->object, after_string))
28721 {
28722 pos = string_buffer_position (after_string, end_charpos);
28723 if (!pos || (pos >= start_charpos && pos < end_charpos))
28724 break;
28725 }
28726 x += glyph->pixel_width;
28727 }
28728 hlinfo->mouse_face_beg_x = x;
28729 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28730 }
28731 else
28732 {
28733 /* This row is in a right to left paragraph. Scan it right to
28734 left. */
28735 struct glyph *g;
28736
28737 end = r1->glyphs[TEXT_AREA] - 1;
28738 glyph = end + r1->used[TEXT_AREA];
28739
28740 /* Skip truncation glyphs at the start of the glyph row. */
28741 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28742 for (; glyph > end
28743 && NILP (glyph->object)
28744 && glyph->charpos < 0;
28745 --glyph)
28746 ;
28747
28748 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28749 or DISP_STRING, and the first glyph from buffer whose
28750 position is between START_CHARPOS and END_CHARPOS. */
28751 for (; glyph > end
28752 && !NILP (glyph->object)
28753 && !EQ (glyph->object, disp_string)
28754 && !(BUFFERP (glyph->object)
28755 && (glyph->charpos >= start_charpos
28756 && glyph->charpos < end_charpos));
28757 --glyph)
28758 {
28759 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28760 are present at buffer positions between START_CHARPOS and
28761 END_CHARPOS, or if they come from an overlay. */
28762 if (EQ (glyph->object, before_string))
28763 {
28764 pos = string_buffer_position (before_string, start_charpos);
28765 /* If pos == 0, it means before_string came from an
28766 overlay, not from a buffer position. */
28767 if (!pos || (pos >= start_charpos && pos < end_charpos))
28768 break;
28769 }
28770 else if (EQ (glyph->object, after_string))
28771 {
28772 pos = string_buffer_position (after_string, end_charpos);
28773 if (!pos || (pos >= start_charpos && pos < end_charpos))
28774 break;
28775 }
28776 }
28777
28778 glyph++; /* first glyph to the right of the highlighted area */
28779 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28780 x += g->pixel_width;
28781 hlinfo->mouse_face_beg_x = x;
28782 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28783 }
28784
28785 /* If the highlight ends in a different row, compute GLYPH and END
28786 for the end row. Otherwise, reuse the values computed above for
28787 the row where the highlight begins. */
28788 if (r2 != r1)
28789 {
28790 if (!r2->reversed_p)
28791 {
28792 glyph = r2->glyphs[TEXT_AREA];
28793 end = glyph + r2->used[TEXT_AREA];
28794 x = r2->x;
28795 }
28796 else
28797 {
28798 end = r2->glyphs[TEXT_AREA] - 1;
28799 glyph = end + r2->used[TEXT_AREA];
28800 }
28801 }
28802
28803 if (!r2->reversed_p)
28804 {
28805 /* Skip truncation and continuation glyphs near the end of the
28806 row, and also blanks and stretch glyphs inserted by
28807 extend_face_to_end_of_line. */
28808 while (end > glyph
28809 && NILP ((end - 1)->object))
28810 --end;
28811 /* Scan the rest of the glyph row from the end, looking for the
28812 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28813 DISP_STRING, or whose position is between START_CHARPOS
28814 and END_CHARPOS */
28815 for (--end;
28816 end > glyph
28817 && !NILP (end->object)
28818 && !EQ (end->object, disp_string)
28819 && !(BUFFERP (end->object)
28820 && (end->charpos >= start_charpos
28821 && end->charpos < end_charpos));
28822 --end)
28823 {
28824 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28825 are present at buffer positions between START_CHARPOS and
28826 END_CHARPOS, or if they come from an overlay. */
28827 if (EQ (end->object, before_string))
28828 {
28829 pos = string_buffer_position (before_string, start_charpos);
28830 if (!pos || (pos >= start_charpos && pos < end_charpos))
28831 break;
28832 }
28833 else if (EQ (end->object, after_string))
28834 {
28835 pos = string_buffer_position (after_string, end_charpos);
28836 if (!pos || (pos >= start_charpos && pos < end_charpos))
28837 break;
28838 }
28839 }
28840 /* Find the X coordinate of the last glyph to be highlighted. */
28841 for (; glyph <= end; ++glyph)
28842 x += glyph->pixel_width;
28843
28844 hlinfo->mouse_face_end_x = x;
28845 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28846 }
28847 else
28848 {
28849 /* Skip truncation and continuation glyphs near the end of the
28850 row, and also blanks and stretch glyphs inserted by
28851 extend_face_to_end_of_line. */
28852 x = r2->x;
28853 end++;
28854 while (end < glyph
28855 && NILP (end->object))
28856 {
28857 x += end->pixel_width;
28858 ++end;
28859 }
28860 /* Scan the rest of the glyph row from the end, looking for the
28861 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28862 DISP_STRING, or whose position is between START_CHARPOS
28863 and END_CHARPOS */
28864 for ( ;
28865 end < glyph
28866 && !NILP (end->object)
28867 && !EQ (end->object, disp_string)
28868 && !(BUFFERP (end->object)
28869 && (end->charpos >= start_charpos
28870 && end->charpos < end_charpos));
28871 ++end)
28872 {
28873 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28874 are present at buffer positions between START_CHARPOS and
28875 END_CHARPOS, or if they come from an overlay. */
28876 if (EQ (end->object, before_string))
28877 {
28878 pos = string_buffer_position (before_string, start_charpos);
28879 if (!pos || (pos >= start_charpos && pos < end_charpos))
28880 break;
28881 }
28882 else if (EQ (end->object, after_string))
28883 {
28884 pos = string_buffer_position (after_string, end_charpos);
28885 if (!pos || (pos >= start_charpos && pos < end_charpos))
28886 break;
28887 }
28888 x += end->pixel_width;
28889 }
28890 /* If we exited the above loop because we arrived at the last
28891 glyph of the row, and its buffer position is still not in
28892 range, it means the last character in range is the preceding
28893 newline. Bump the end column and x values to get past the
28894 last glyph. */
28895 if (end == glyph
28896 && BUFFERP (end->object)
28897 && (end->charpos < start_charpos
28898 || end->charpos >= end_charpos))
28899 {
28900 x += end->pixel_width;
28901 ++end;
28902 }
28903 hlinfo->mouse_face_end_x = x;
28904 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28905 }
28906
28907 hlinfo->mouse_face_window = window;
28908 hlinfo->mouse_face_face_id
28909 = face_at_buffer_position (w, mouse_charpos, &ignore,
28910 mouse_charpos + 1,
28911 !hlinfo->mouse_face_hidden, -1);
28912 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28913 }
28914
28915 /* The following function is not used anymore (replaced with
28916 mouse_face_from_string_pos), but I leave it here for the time
28917 being, in case someone would. */
28918
28919 #if false /* not used */
28920
28921 /* Find the position of the glyph for position POS in OBJECT in
28922 window W's current matrix, and return in *X, *Y the pixel
28923 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28924
28925 RIGHT_P means return the position of the right edge of the glyph.
28926 !RIGHT_P means return the left edge position.
28927
28928 If no glyph for POS exists in the matrix, return the position of
28929 the glyph with the next smaller position that is in the matrix, if
28930 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28931 exists in the matrix, return the position of the glyph with the
28932 next larger position in OBJECT.
28933
28934 Value is true if a glyph was found. */
28935
28936 static bool
28937 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28938 int *hpos, int *vpos, int *x, int *y, bool right_p)
28939 {
28940 int yb = window_text_bottom_y (w);
28941 struct glyph_row *r;
28942 struct glyph *best_glyph = NULL;
28943 struct glyph_row *best_row = NULL;
28944 int best_x = 0;
28945
28946 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28947 r->enabled_p && r->y < yb;
28948 ++r)
28949 {
28950 struct glyph *g = r->glyphs[TEXT_AREA];
28951 struct glyph *e = g + r->used[TEXT_AREA];
28952 int gx;
28953
28954 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28955 if (EQ (g->object, object))
28956 {
28957 if (g->charpos == pos)
28958 {
28959 best_glyph = g;
28960 best_x = gx;
28961 best_row = r;
28962 goto found;
28963 }
28964 else if (best_glyph == NULL
28965 || ((eabs (g->charpos - pos)
28966 < eabs (best_glyph->charpos - pos))
28967 && (right_p
28968 ? g->charpos < pos
28969 : g->charpos > pos)))
28970 {
28971 best_glyph = g;
28972 best_x = gx;
28973 best_row = r;
28974 }
28975 }
28976 }
28977
28978 found:
28979
28980 if (best_glyph)
28981 {
28982 *x = best_x;
28983 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28984
28985 if (right_p)
28986 {
28987 *x += best_glyph->pixel_width;
28988 ++*hpos;
28989 }
28990
28991 *y = best_row->y;
28992 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28993 }
28994
28995 return best_glyph != NULL;
28996 }
28997 #endif /* not used */
28998
28999 /* Find the positions of the first and the last glyphs in window W's
29000 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29001 (assumed to be a string), and return in HLINFO's mouse_face_*
29002 members the pixel and column/row coordinates of those glyphs. */
29003
29004 static void
29005 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29006 Lisp_Object object,
29007 ptrdiff_t startpos, ptrdiff_t endpos)
29008 {
29009 int yb = window_text_bottom_y (w);
29010 struct glyph_row *r;
29011 struct glyph *g, *e;
29012 int gx;
29013 bool found = false;
29014
29015 /* Find the glyph row with at least one position in the range
29016 [STARTPOS..ENDPOS), and the first glyph in that row whose
29017 position belongs to that range. */
29018 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29019 r->enabled_p && r->y < yb;
29020 ++r)
29021 {
29022 if (!r->reversed_p)
29023 {
29024 g = r->glyphs[TEXT_AREA];
29025 e = g + r->used[TEXT_AREA];
29026 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29027 if (EQ (g->object, object)
29028 && startpos <= g->charpos && g->charpos < endpos)
29029 {
29030 hlinfo->mouse_face_beg_row
29031 = MATRIX_ROW_VPOS (r, w->current_matrix);
29032 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29033 hlinfo->mouse_face_beg_x = gx;
29034 found = true;
29035 break;
29036 }
29037 }
29038 else
29039 {
29040 struct glyph *g1;
29041
29042 e = r->glyphs[TEXT_AREA];
29043 g = e + r->used[TEXT_AREA];
29044 for ( ; g > e; --g)
29045 if (EQ ((g-1)->object, object)
29046 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29047 {
29048 hlinfo->mouse_face_beg_row
29049 = MATRIX_ROW_VPOS (r, w->current_matrix);
29050 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29051 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29052 gx += g1->pixel_width;
29053 hlinfo->mouse_face_beg_x = gx;
29054 found = true;
29055 break;
29056 }
29057 }
29058 if (found)
29059 break;
29060 }
29061
29062 if (!found)
29063 return;
29064
29065 /* Starting with the next row, look for the first row which does NOT
29066 include any glyphs whose positions are in the range. */
29067 for (++r; r->enabled_p && r->y < yb; ++r)
29068 {
29069 g = r->glyphs[TEXT_AREA];
29070 e = g + r->used[TEXT_AREA];
29071 found = false;
29072 for ( ; g < e; ++g)
29073 if (EQ (g->object, object)
29074 && startpos <= g->charpos && g->charpos < endpos)
29075 {
29076 found = true;
29077 break;
29078 }
29079 if (!found)
29080 break;
29081 }
29082
29083 /* The highlighted region ends on the previous row. */
29084 r--;
29085
29086 /* Set the end row. */
29087 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29088
29089 /* Compute and set the end column and the end column's horizontal
29090 pixel coordinate. */
29091 if (!r->reversed_p)
29092 {
29093 g = r->glyphs[TEXT_AREA];
29094 e = g + r->used[TEXT_AREA];
29095 for ( ; e > g; --e)
29096 if (EQ ((e-1)->object, object)
29097 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29098 break;
29099 hlinfo->mouse_face_end_col = e - g;
29100
29101 for (gx = r->x; g < e; ++g)
29102 gx += g->pixel_width;
29103 hlinfo->mouse_face_end_x = gx;
29104 }
29105 else
29106 {
29107 e = r->glyphs[TEXT_AREA];
29108 g = e + r->used[TEXT_AREA];
29109 for (gx = r->x ; e < g; ++e)
29110 {
29111 if (EQ (e->object, object)
29112 && startpos <= e->charpos && e->charpos < endpos)
29113 break;
29114 gx += e->pixel_width;
29115 }
29116 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29117 hlinfo->mouse_face_end_x = gx;
29118 }
29119 }
29120
29121 #ifdef HAVE_WINDOW_SYSTEM
29122
29123 /* See if position X, Y is within a hot-spot of an image. */
29124
29125 static bool
29126 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29127 {
29128 if (!CONSP (hot_spot))
29129 return false;
29130
29131 if (EQ (XCAR (hot_spot), Qrect))
29132 {
29133 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29134 Lisp_Object rect = XCDR (hot_spot);
29135 Lisp_Object tem;
29136 if (!CONSP (rect))
29137 return false;
29138 if (!CONSP (XCAR (rect)))
29139 return false;
29140 if (!CONSP (XCDR (rect)))
29141 return false;
29142 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29143 return false;
29144 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29145 return false;
29146 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29147 return false;
29148 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29149 return false;
29150 return true;
29151 }
29152 else if (EQ (XCAR (hot_spot), Qcircle))
29153 {
29154 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29155 Lisp_Object circ = XCDR (hot_spot);
29156 Lisp_Object lr, lx0, ly0;
29157 if (CONSP (circ)
29158 && CONSP (XCAR (circ))
29159 && (lr = XCDR (circ), NUMBERP (lr))
29160 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29161 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29162 {
29163 double r = XFLOATINT (lr);
29164 double dx = XINT (lx0) - x;
29165 double dy = XINT (ly0) - y;
29166 return (dx * dx + dy * dy <= r * r);
29167 }
29168 }
29169 else if (EQ (XCAR (hot_spot), Qpoly))
29170 {
29171 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29172 if (VECTORP (XCDR (hot_spot)))
29173 {
29174 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29175 Lisp_Object *poly = v->contents;
29176 ptrdiff_t n = v->header.size;
29177 ptrdiff_t i;
29178 bool inside = false;
29179 Lisp_Object lx, ly;
29180 int x0, y0;
29181
29182 /* Need an even number of coordinates, and at least 3 edges. */
29183 if (n < 6 || n & 1)
29184 return false;
29185
29186 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29187 If count is odd, we are inside polygon. Pixels on edges
29188 may or may not be included depending on actual geometry of the
29189 polygon. */
29190 if ((lx = poly[n-2], !INTEGERP (lx))
29191 || (ly = poly[n-1], !INTEGERP (lx)))
29192 return false;
29193 x0 = XINT (lx), y0 = XINT (ly);
29194 for (i = 0; i < n; i += 2)
29195 {
29196 int x1 = x0, y1 = y0;
29197 if ((lx = poly[i], !INTEGERP (lx))
29198 || (ly = poly[i+1], !INTEGERP (ly)))
29199 return false;
29200 x0 = XINT (lx), y0 = XINT (ly);
29201
29202 /* Does this segment cross the X line? */
29203 if (x0 >= x)
29204 {
29205 if (x1 >= x)
29206 continue;
29207 }
29208 else if (x1 < x)
29209 continue;
29210 if (y > y0 && y > y1)
29211 continue;
29212 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29213 inside = !inside;
29214 }
29215 return inside;
29216 }
29217 }
29218 return false;
29219 }
29220
29221 Lisp_Object
29222 find_hot_spot (Lisp_Object map, int x, int y)
29223 {
29224 while (CONSP (map))
29225 {
29226 if (CONSP (XCAR (map))
29227 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29228 return XCAR (map);
29229 map = XCDR (map);
29230 }
29231
29232 return Qnil;
29233 }
29234
29235 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29236 3, 3, 0,
29237 doc: /* Lookup in image map MAP coordinates X and Y.
29238 An image map is an alist where each element has the format (AREA ID PLIST).
29239 An AREA is specified as either a rectangle, a circle, or a polygon:
29240 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29241 pixel coordinates of the upper left and bottom right corners.
29242 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29243 and the radius of the circle; r may be a float or integer.
29244 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29245 vector describes one corner in the polygon.
29246 Returns the alist element for the first matching AREA in MAP. */)
29247 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29248 {
29249 if (NILP (map))
29250 return Qnil;
29251
29252 CHECK_NUMBER (x);
29253 CHECK_NUMBER (y);
29254
29255 return find_hot_spot (map,
29256 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29257 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29258 }
29259
29260
29261 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29262 static void
29263 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29264 {
29265 /* Do not change cursor shape while dragging mouse. */
29266 if (EQ (do_mouse_tracking, Qdragging))
29267 return;
29268
29269 if (!NILP (pointer))
29270 {
29271 if (EQ (pointer, Qarrow))
29272 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29273 else if (EQ (pointer, Qhand))
29274 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29275 else if (EQ (pointer, Qtext))
29276 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29277 else if (EQ (pointer, intern ("hdrag")))
29278 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29279 else if (EQ (pointer, intern ("nhdrag")))
29280 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29281 #ifdef HAVE_X_WINDOWS
29282 else if (EQ (pointer, intern ("vdrag")))
29283 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29284 #endif
29285 else if (EQ (pointer, intern ("hourglass")))
29286 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29287 else if (EQ (pointer, Qmodeline))
29288 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29289 else
29290 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29291 }
29292
29293 if (cursor != No_Cursor)
29294 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29295 }
29296
29297 #endif /* HAVE_WINDOW_SYSTEM */
29298
29299 /* Take proper action when mouse has moved to the mode or header line
29300 or marginal area AREA of window W, x-position X and y-position Y.
29301 X is relative to the start of the text display area of W, so the
29302 width of bitmap areas and scroll bars must be subtracted to get a
29303 position relative to the start of the mode line. */
29304
29305 static void
29306 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29307 enum window_part area)
29308 {
29309 struct window *w = XWINDOW (window);
29310 struct frame *f = XFRAME (w->frame);
29311 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29312 #ifdef HAVE_WINDOW_SYSTEM
29313 Display_Info *dpyinfo;
29314 #endif
29315 Cursor cursor = No_Cursor;
29316 Lisp_Object pointer = Qnil;
29317 int dx, dy, width, height;
29318 ptrdiff_t charpos;
29319 Lisp_Object string, object = Qnil;
29320 Lisp_Object pos IF_LINT (= Qnil), help;
29321
29322 Lisp_Object mouse_face;
29323 int original_x_pixel = x;
29324 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29325 struct glyph_row *row IF_LINT (= 0);
29326
29327 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29328 {
29329 int x0;
29330 struct glyph *end;
29331
29332 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29333 returns them in row/column units! */
29334 string = mode_line_string (w, area, &x, &y, &charpos,
29335 &object, &dx, &dy, &width, &height);
29336
29337 row = (area == ON_MODE_LINE
29338 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29339 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29340
29341 /* Find the glyph under the mouse pointer. */
29342 if (row->mode_line_p && row->enabled_p)
29343 {
29344 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29345 end = glyph + row->used[TEXT_AREA];
29346
29347 for (x0 = original_x_pixel;
29348 glyph < end && x0 >= glyph->pixel_width;
29349 ++glyph)
29350 x0 -= glyph->pixel_width;
29351
29352 if (glyph >= end)
29353 glyph = NULL;
29354 }
29355 }
29356 else
29357 {
29358 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29359 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29360 returns them in row/column units! */
29361 string = marginal_area_string (w, area, &x, &y, &charpos,
29362 &object, &dx, &dy, &width, &height);
29363 }
29364
29365 help = Qnil;
29366
29367 #ifdef HAVE_WINDOW_SYSTEM
29368 if (IMAGEP (object))
29369 {
29370 Lisp_Object image_map, hotspot;
29371 if ((image_map = Fplist_get (XCDR (object), QCmap),
29372 !NILP (image_map))
29373 && (hotspot = find_hot_spot (image_map, dx, dy),
29374 CONSP (hotspot))
29375 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29376 {
29377 Lisp_Object plist;
29378
29379 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29380 If so, we could look for mouse-enter, mouse-leave
29381 properties in PLIST (and do something...). */
29382 hotspot = XCDR (hotspot);
29383 if (CONSP (hotspot)
29384 && (plist = XCAR (hotspot), CONSP (plist)))
29385 {
29386 pointer = Fplist_get (plist, Qpointer);
29387 if (NILP (pointer))
29388 pointer = Qhand;
29389 help = Fplist_get (plist, Qhelp_echo);
29390 if (!NILP (help))
29391 {
29392 help_echo_string = help;
29393 XSETWINDOW (help_echo_window, w);
29394 help_echo_object = w->contents;
29395 help_echo_pos = charpos;
29396 }
29397 }
29398 }
29399 if (NILP (pointer))
29400 pointer = Fplist_get (XCDR (object), QCpointer);
29401 }
29402 #endif /* HAVE_WINDOW_SYSTEM */
29403
29404 if (STRINGP (string))
29405 pos = make_number (charpos);
29406
29407 /* Set the help text and mouse pointer. If the mouse is on a part
29408 of the mode line without any text (e.g. past the right edge of
29409 the mode line text), use the default help text and pointer. */
29410 if (STRINGP (string) || area == ON_MODE_LINE)
29411 {
29412 /* Arrange to display the help by setting the global variables
29413 help_echo_string, help_echo_object, and help_echo_pos. */
29414 if (NILP (help))
29415 {
29416 if (STRINGP (string))
29417 help = Fget_text_property (pos, Qhelp_echo, string);
29418
29419 if (!NILP (help))
29420 {
29421 help_echo_string = help;
29422 XSETWINDOW (help_echo_window, w);
29423 help_echo_object = string;
29424 help_echo_pos = charpos;
29425 }
29426 else if (area == ON_MODE_LINE)
29427 {
29428 Lisp_Object default_help
29429 = buffer_local_value (Qmode_line_default_help_echo,
29430 w->contents);
29431
29432 if (STRINGP (default_help))
29433 {
29434 help_echo_string = default_help;
29435 XSETWINDOW (help_echo_window, w);
29436 help_echo_object = Qnil;
29437 help_echo_pos = -1;
29438 }
29439 }
29440 }
29441
29442 #ifdef HAVE_WINDOW_SYSTEM
29443 /* Change the mouse pointer according to what is under it. */
29444 if (FRAME_WINDOW_P (f))
29445 {
29446 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29447 || minibuf_level
29448 || NILP (Vresize_mini_windows));
29449
29450 dpyinfo = FRAME_DISPLAY_INFO (f);
29451 if (STRINGP (string))
29452 {
29453 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29454
29455 if (NILP (pointer))
29456 pointer = Fget_text_property (pos, Qpointer, string);
29457
29458 /* Change the mouse pointer according to what is under X/Y. */
29459 if (NILP (pointer)
29460 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29461 {
29462 Lisp_Object map;
29463 map = Fget_text_property (pos, Qlocal_map, string);
29464 if (!KEYMAPP (map))
29465 map = Fget_text_property (pos, Qkeymap, string);
29466 if (!KEYMAPP (map) && draggable)
29467 cursor = dpyinfo->vertical_scroll_bar_cursor;
29468 }
29469 }
29470 else if (draggable)
29471 /* Default mode-line pointer. */
29472 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29473 }
29474 #endif
29475 }
29476
29477 /* Change the mouse face according to what is under X/Y. */
29478 bool mouse_face_shown = false;
29479 if (STRINGP (string))
29480 {
29481 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29482 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29483 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29484 && glyph)
29485 {
29486 Lisp_Object b, e;
29487
29488 struct glyph * tmp_glyph;
29489
29490 int gpos;
29491 int gseq_length;
29492 int total_pixel_width;
29493 ptrdiff_t begpos, endpos, ignore;
29494
29495 int vpos, hpos;
29496
29497 b = Fprevious_single_property_change (make_number (charpos + 1),
29498 Qmouse_face, string, Qnil);
29499 if (NILP (b))
29500 begpos = 0;
29501 else
29502 begpos = XINT (b);
29503
29504 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29505 if (NILP (e))
29506 endpos = SCHARS (string);
29507 else
29508 endpos = XINT (e);
29509
29510 /* Calculate the glyph position GPOS of GLYPH in the
29511 displayed string, relative to the beginning of the
29512 highlighted part of the string.
29513
29514 Note: GPOS is different from CHARPOS. CHARPOS is the
29515 position of GLYPH in the internal string object. A mode
29516 line string format has structures which are converted to
29517 a flattened string by the Emacs Lisp interpreter. The
29518 internal string is an element of those structures. The
29519 displayed string is the flattened string. */
29520 tmp_glyph = row_start_glyph;
29521 while (tmp_glyph < glyph
29522 && (!(EQ (tmp_glyph->object, glyph->object)
29523 && begpos <= tmp_glyph->charpos
29524 && tmp_glyph->charpos < endpos)))
29525 tmp_glyph++;
29526 gpos = glyph - tmp_glyph;
29527
29528 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29529 the highlighted part of the displayed string to which
29530 GLYPH belongs. Note: GSEQ_LENGTH is different from
29531 SCHARS (STRING), because the latter returns the length of
29532 the internal string. */
29533 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29534 tmp_glyph > glyph
29535 && (!(EQ (tmp_glyph->object, glyph->object)
29536 && begpos <= tmp_glyph->charpos
29537 && tmp_glyph->charpos < endpos));
29538 tmp_glyph--)
29539 ;
29540 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29541
29542 /* Calculate the total pixel width of all the glyphs between
29543 the beginning of the highlighted area and GLYPH. */
29544 total_pixel_width = 0;
29545 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29546 total_pixel_width += tmp_glyph->pixel_width;
29547
29548 /* Pre calculation of re-rendering position. Note: X is in
29549 column units here, after the call to mode_line_string or
29550 marginal_area_string. */
29551 hpos = x - gpos;
29552 vpos = (area == ON_MODE_LINE
29553 ? (w->current_matrix)->nrows - 1
29554 : 0);
29555
29556 /* If GLYPH's position is included in the region that is
29557 already drawn in mouse face, we have nothing to do. */
29558 if ( EQ (window, hlinfo->mouse_face_window)
29559 && (!row->reversed_p
29560 ? (hlinfo->mouse_face_beg_col <= hpos
29561 && hpos < hlinfo->mouse_face_end_col)
29562 /* In R2L rows we swap BEG and END, see below. */
29563 : (hlinfo->mouse_face_end_col <= hpos
29564 && hpos < hlinfo->mouse_face_beg_col))
29565 && hlinfo->mouse_face_beg_row == vpos )
29566 return;
29567
29568 if (clear_mouse_face (hlinfo))
29569 cursor = No_Cursor;
29570
29571 if (!row->reversed_p)
29572 {
29573 hlinfo->mouse_face_beg_col = hpos;
29574 hlinfo->mouse_face_beg_x = original_x_pixel
29575 - (total_pixel_width + dx);
29576 hlinfo->mouse_face_end_col = hpos + gseq_length;
29577 hlinfo->mouse_face_end_x = 0;
29578 }
29579 else
29580 {
29581 /* In R2L rows, show_mouse_face expects BEG and END
29582 coordinates to be swapped. */
29583 hlinfo->mouse_face_end_col = hpos;
29584 hlinfo->mouse_face_end_x = original_x_pixel
29585 - (total_pixel_width + dx);
29586 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29587 hlinfo->mouse_face_beg_x = 0;
29588 }
29589
29590 hlinfo->mouse_face_beg_row = vpos;
29591 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29592 hlinfo->mouse_face_past_end = false;
29593 hlinfo->mouse_face_window = window;
29594
29595 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29596 charpos,
29597 0, &ignore,
29598 glyph->face_id,
29599 true);
29600 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29601 mouse_face_shown = true;
29602
29603 if (NILP (pointer))
29604 pointer = Qhand;
29605 }
29606 }
29607
29608 /* If mouse-face doesn't need to be shown, clear any existing
29609 mouse-face. */
29610 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29611 clear_mouse_face (hlinfo);
29612
29613 #ifdef HAVE_WINDOW_SYSTEM
29614 if (FRAME_WINDOW_P (f))
29615 define_frame_cursor1 (f, cursor, pointer);
29616 #endif
29617 }
29618
29619
29620 /* EXPORT:
29621 Take proper action when the mouse has moved to position X, Y on
29622 frame F with regards to highlighting portions of display that have
29623 mouse-face properties. Also de-highlight portions of display where
29624 the mouse was before, set the mouse pointer shape as appropriate
29625 for the mouse coordinates, and activate help echo (tooltips).
29626 X and Y can be negative or out of range. */
29627
29628 void
29629 note_mouse_highlight (struct frame *f, int x, int y)
29630 {
29631 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29632 enum window_part part = ON_NOTHING;
29633 Lisp_Object window;
29634 struct window *w;
29635 Cursor cursor = No_Cursor;
29636 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29637 struct buffer *b;
29638
29639 /* When a menu is active, don't highlight because this looks odd. */
29640 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29641 if (popup_activated ())
29642 return;
29643 #endif
29644
29645 if (!f->glyphs_initialized_p
29646 || f->pointer_invisible)
29647 return;
29648
29649 hlinfo->mouse_face_mouse_x = x;
29650 hlinfo->mouse_face_mouse_y = y;
29651 hlinfo->mouse_face_mouse_frame = f;
29652
29653 if (hlinfo->mouse_face_defer)
29654 return;
29655
29656 /* Which window is that in? */
29657 window = window_from_coordinates (f, x, y, &part, true);
29658
29659 /* If displaying active text in another window, clear that. */
29660 if (! EQ (window, hlinfo->mouse_face_window)
29661 /* Also clear if we move out of text area in same window. */
29662 || (!NILP (hlinfo->mouse_face_window)
29663 && !NILP (window)
29664 && part != ON_TEXT
29665 && part != ON_MODE_LINE
29666 && part != ON_HEADER_LINE))
29667 clear_mouse_face (hlinfo);
29668
29669 /* Not on a window -> return. */
29670 if (!WINDOWP (window))
29671 return;
29672
29673 /* Reset help_echo_string. It will get recomputed below. */
29674 help_echo_string = Qnil;
29675
29676 /* Convert to window-relative pixel coordinates. */
29677 w = XWINDOW (window);
29678 frame_to_window_pixel_xy (w, &x, &y);
29679
29680 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29681 /* Handle tool-bar window differently since it doesn't display a
29682 buffer. */
29683 if (EQ (window, f->tool_bar_window))
29684 {
29685 note_tool_bar_highlight (f, x, y);
29686 return;
29687 }
29688 #endif
29689
29690 /* Mouse is on the mode, header line or margin? */
29691 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29692 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29693 {
29694 note_mode_line_or_margin_highlight (window, x, y, part);
29695
29696 #ifdef HAVE_WINDOW_SYSTEM
29697 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29698 {
29699 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29700 /* Show non-text cursor (Bug#16647). */
29701 goto set_cursor;
29702 }
29703 else
29704 #endif
29705 return;
29706 }
29707
29708 #ifdef HAVE_WINDOW_SYSTEM
29709 if (part == ON_VERTICAL_BORDER)
29710 {
29711 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29712 help_echo_string = build_string ("drag-mouse-1: resize");
29713 }
29714 else if (part == ON_RIGHT_DIVIDER)
29715 {
29716 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29717 help_echo_string = build_string ("drag-mouse-1: resize");
29718 }
29719 else if (part == ON_BOTTOM_DIVIDER)
29720 if (! WINDOW_BOTTOMMOST_P (w)
29721 || minibuf_level
29722 || NILP (Vresize_mini_windows))
29723 {
29724 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29725 help_echo_string = build_string ("drag-mouse-1: resize");
29726 }
29727 else
29728 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29729 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29730 || part == ON_VERTICAL_SCROLL_BAR
29731 || part == ON_HORIZONTAL_SCROLL_BAR)
29732 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29733 else
29734 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29735 #endif
29736
29737 /* Are we in a window whose display is up to date?
29738 And verify the buffer's text has not changed. */
29739 b = XBUFFER (w->contents);
29740 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29741 {
29742 int hpos, vpos, dx, dy, area = LAST_AREA;
29743 ptrdiff_t pos;
29744 struct glyph *glyph;
29745 Lisp_Object object;
29746 Lisp_Object mouse_face = Qnil, position;
29747 Lisp_Object *overlay_vec = NULL;
29748 ptrdiff_t i, noverlays;
29749 struct buffer *obuf;
29750 ptrdiff_t obegv, ozv;
29751 bool same_region;
29752
29753 /* Find the glyph under X/Y. */
29754 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29755
29756 #ifdef HAVE_WINDOW_SYSTEM
29757 /* Look for :pointer property on image. */
29758 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29759 {
29760 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29761 if (img != NULL && IMAGEP (img->spec))
29762 {
29763 Lisp_Object image_map, hotspot;
29764 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29765 !NILP (image_map))
29766 && (hotspot = find_hot_spot (image_map,
29767 glyph->slice.img.x + dx,
29768 glyph->slice.img.y + dy),
29769 CONSP (hotspot))
29770 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29771 {
29772 Lisp_Object plist;
29773
29774 /* Could check XCAR (hotspot) to see if we enter/leave
29775 this hot-spot.
29776 If so, we could look for mouse-enter, mouse-leave
29777 properties in PLIST (and do something...). */
29778 hotspot = XCDR (hotspot);
29779 if (CONSP (hotspot)
29780 && (plist = XCAR (hotspot), CONSP (plist)))
29781 {
29782 pointer = Fplist_get (plist, Qpointer);
29783 if (NILP (pointer))
29784 pointer = Qhand;
29785 help_echo_string = Fplist_get (plist, Qhelp_echo);
29786 if (!NILP (help_echo_string))
29787 {
29788 help_echo_window = window;
29789 help_echo_object = glyph->object;
29790 help_echo_pos = glyph->charpos;
29791 }
29792 }
29793 }
29794 if (NILP (pointer))
29795 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29796 }
29797 }
29798 #endif /* HAVE_WINDOW_SYSTEM */
29799
29800 /* Clear mouse face if X/Y not over text. */
29801 if (glyph == NULL
29802 || area != TEXT_AREA
29803 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29804 /* Glyph's OBJECT is nil for glyphs inserted by the
29805 display engine for its internal purposes, like truncation
29806 and continuation glyphs and blanks beyond the end of
29807 line's text on text terminals. If we are over such a
29808 glyph, we are not over any text. */
29809 || NILP (glyph->object)
29810 /* R2L rows have a stretch glyph at their front, which
29811 stands for no text, whereas L2R rows have no glyphs at
29812 all beyond the end of text. Treat such stretch glyphs
29813 like we do with NULL glyphs in L2R rows. */
29814 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29815 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29816 && glyph->type == STRETCH_GLYPH
29817 && glyph->avoid_cursor_p))
29818 {
29819 if (clear_mouse_face (hlinfo))
29820 cursor = No_Cursor;
29821 #ifdef HAVE_WINDOW_SYSTEM
29822 if (FRAME_WINDOW_P (f) && NILP (pointer))
29823 {
29824 if (area != TEXT_AREA)
29825 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29826 else
29827 pointer = Vvoid_text_area_pointer;
29828 }
29829 #endif
29830 goto set_cursor;
29831 }
29832
29833 pos = glyph->charpos;
29834 object = glyph->object;
29835 if (!STRINGP (object) && !BUFFERP (object))
29836 goto set_cursor;
29837
29838 /* If we get an out-of-range value, return now; avoid an error. */
29839 if (BUFFERP (object) && pos > BUF_Z (b))
29840 goto set_cursor;
29841
29842 /* Make the window's buffer temporarily current for
29843 overlays_at and compute_char_face. */
29844 obuf = current_buffer;
29845 current_buffer = b;
29846 obegv = BEGV;
29847 ozv = ZV;
29848 BEGV = BEG;
29849 ZV = Z;
29850
29851 /* Is this char mouse-active or does it have help-echo? */
29852 position = make_number (pos);
29853
29854 USE_SAFE_ALLOCA;
29855
29856 if (BUFFERP (object))
29857 {
29858 /* Put all the overlays we want in a vector in overlay_vec. */
29859 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29860 /* Sort overlays into increasing priority order. */
29861 noverlays = sort_overlays (overlay_vec, noverlays, w);
29862 }
29863 else
29864 noverlays = 0;
29865
29866 if (NILP (Vmouse_highlight))
29867 {
29868 clear_mouse_face (hlinfo);
29869 goto check_help_echo;
29870 }
29871
29872 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29873
29874 if (same_region)
29875 cursor = No_Cursor;
29876
29877 /* Check mouse-face highlighting. */
29878 if (! same_region
29879 /* If there exists an overlay with mouse-face overlapping
29880 the one we are currently highlighting, we have to
29881 check if we enter the overlapping overlay, and then
29882 highlight only that. */
29883 || (OVERLAYP (hlinfo->mouse_face_overlay)
29884 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29885 {
29886 /* Find the highest priority overlay with a mouse-face. */
29887 Lisp_Object overlay = Qnil;
29888 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29889 {
29890 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29891 if (!NILP (mouse_face))
29892 overlay = overlay_vec[i];
29893 }
29894
29895 /* If we're highlighting the same overlay as before, there's
29896 no need to do that again. */
29897 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29898 goto check_help_echo;
29899 hlinfo->mouse_face_overlay = overlay;
29900
29901 /* Clear the display of the old active region, if any. */
29902 if (clear_mouse_face (hlinfo))
29903 cursor = No_Cursor;
29904
29905 /* If no overlay applies, get a text property. */
29906 if (NILP (overlay))
29907 mouse_face = Fget_text_property (position, Qmouse_face, object);
29908
29909 /* Next, compute the bounds of the mouse highlighting and
29910 display it. */
29911 if (!NILP (mouse_face) && STRINGP (object))
29912 {
29913 /* The mouse-highlighting comes from a display string
29914 with a mouse-face. */
29915 Lisp_Object s, e;
29916 ptrdiff_t ignore;
29917
29918 s = Fprevious_single_property_change
29919 (make_number (pos + 1), Qmouse_face, object, Qnil);
29920 e = Fnext_single_property_change
29921 (position, Qmouse_face, object, Qnil);
29922 if (NILP (s))
29923 s = make_number (0);
29924 if (NILP (e))
29925 e = make_number (SCHARS (object));
29926 mouse_face_from_string_pos (w, hlinfo, object,
29927 XINT (s), XINT (e));
29928 hlinfo->mouse_face_past_end = false;
29929 hlinfo->mouse_face_window = window;
29930 hlinfo->mouse_face_face_id
29931 = face_at_string_position (w, object, pos, 0, &ignore,
29932 glyph->face_id, true);
29933 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29934 cursor = No_Cursor;
29935 }
29936 else
29937 {
29938 /* The mouse-highlighting, if any, comes from an overlay
29939 or text property in the buffer. */
29940 Lisp_Object buffer IF_LINT (= Qnil);
29941 Lisp_Object disp_string IF_LINT (= Qnil);
29942
29943 if (STRINGP (object))
29944 {
29945 /* If we are on a display string with no mouse-face,
29946 check if the text under it has one. */
29947 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29948 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29949 pos = string_buffer_position (object, start);
29950 if (pos > 0)
29951 {
29952 mouse_face = get_char_property_and_overlay
29953 (make_number (pos), Qmouse_face, w->contents, &overlay);
29954 buffer = w->contents;
29955 disp_string = object;
29956 }
29957 }
29958 else
29959 {
29960 buffer = object;
29961 disp_string = Qnil;
29962 }
29963
29964 if (!NILP (mouse_face))
29965 {
29966 Lisp_Object before, after;
29967 Lisp_Object before_string, after_string;
29968 /* To correctly find the limits of mouse highlight
29969 in a bidi-reordered buffer, we must not use the
29970 optimization of limiting the search in
29971 previous-single-property-change and
29972 next-single-property-change, because
29973 rows_from_pos_range needs the real start and end
29974 positions to DTRT in this case. That's because
29975 the first row visible in a window does not
29976 necessarily display the character whose position
29977 is the smallest. */
29978 Lisp_Object lim1
29979 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29980 ? Fmarker_position (w->start)
29981 : Qnil;
29982 Lisp_Object lim2
29983 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29984 ? make_number (BUF_Z (XBUFFER (buffer))
29985 - w->window_end_pos)
29986 : Qnil;
29987
29988 if (NILP (overlay))
29989 {
29990 /* Handle the text property case. */
29991 before = Fprevious_single_property_change
29992 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29993 after = Fnext_single_property_change
29994 (make_number (pos), Qmouse_face, buffer, lim2);
29995 before_string = after_string = Qnil;
29996 }
29997 else
29998 {
29999 /* Handle the overlay case. */
30000 before = Foverlay_start (overlay);
30001 after = Foverlay_end (overlay);
30002 before_string = Foverlay_get (overlay, Qbefore_string);
30003 after_string = Foverlay_get (overlay, Qafter_string);
30004
30005 if (!STRINGP (before_string)) before_string = Qnil;
30006 if (!STRINGP (after_string)) after_string = Qnil;
30007 }
30008
30009 mouse_face_from_buffer_pos (window, hlinfo, pos,
30010 NILP (before)
30011 ? 1
30012 : XFASTINT (before),
30013 NILP (after)
30014 ? BUF_Z (XBUFFER (buffer))
30015 : XFASTINT (after),
30016 before_string, after_string,
30017 disp_string);
30018 cursor = No_Cursor;
30019 }
30020 }
30021 }
30022
30023 check_help_echo:
30024
30025 /* Look for a `help-echo' property. */
30026 if (NILP (help_echo_string)) {
30027 Lisp_Object help, overlay;
30028
30029 /* Check overlays first. */
30030 help = overlay = Qnil;
30031 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30032 {
30033 overlay = overlay_vec[i];
30034 help = Foverlay_get (overlay, Qhelp_echo);
30035 }
30036
30037 if (!NILP (help))
30038 {
30039 help_echo_string = help;
30040 help_echo_window = window;
30041 help_echo_object = overlay;
30042 help_echo_pos = pos;
30043 }
30044 else
30045 {
30046 Lisp_Object obj = glyph->object;
30047 ptrdiff_t charpos = glyph->charpos;
30048
30049 /* Try text properties. */
30050 if (STRINGP (obj)
30051 && charpos >= 0
30052 && charpos < SCHARS (obj))
30053 {
30054 help = Fget_text_property (make_number (charpos),
30055 Qhelp_echo, obj);
30056 if (NILP (help))
30057 {
30058 /* If the string itself doesn't specify a help-echo,
30059 see if the buffer text ``under'' it does. */
30060 struct glyph_row *r
30061 = MATRIX_ROW (w->current_matrix, vpos);
30062 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30063 ptrdiff_t p = string_buffer_position (obj, start);
30064 if (p > 0)
30065 {
30066 help = Fget_char_property (make_number (p),
30067 Qhelp_echo, w->contents);
30068 if (!NILP (help))
30069 {
30070 charpos = p;
30071 obj = w->contents;
30072 }
30073 }
30074 }
30075 }
30076 else if (BUFFERP (obj)
30077 && charpos >= BEGV
30078 && charpos < ZV)
30079 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30080 obj);
30081
30082 if (!NILP (help))
30083 {
30084 help_echo_string = help;
30085 help_echo_window = window;
30086 help_echo_object = obj;
30087 help_echo_pos = charpos;
30088 }
30089 }
30090 }
30091
30092 #ifdef HAVE_WINDOW_SYSTEM
30093 /* Look for a `pointer' property. */
30094 if (FRAME_WINDOW_P (f) && NILP (pointer))
30095 {
30096 /* Check overlays first. */
30097 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30098 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30099
30100 if (NILP (pointer))
30101 {
30102 Lisp_Object obj = glyph->object;
30103 ptrdiff_t charpos = glyph->charpos;
30104
30105 /* Try text properties. */
30106 if (STRINGP (obj)
30107 && charpos >= 0
30108 && charpos < SCHARS (obj))
30109 {
30110 pointer = Fget_text_property (make_number (charpos),
30111 Qpointer, obj);
30112 if (NILP (pointer))
30113 {
30114 /* If the string itself doesn't specify a pointer,
30115 see if the buffer text ``under'' it does. */
30116 struct glyph_row *r
30117 = MATRIX_ROW (w->current_matrix, vpos);
30118 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30119 ptrdiff_t p = string_buffer_position (obj, start);
30120 if (p > 0)
30121 pointer = Fget_char_property (make_number (p),
30122 Qpointer, w->contents);
30123 }
30124 }
30125 else if (BUFFERP (obj)
30126 && charpos >= BEGV
30127 && charpos < ZV)
30128 pointer = Fget_text_property (make_number (charpos),
30129 Qpointer, obj);
30130 }
30131 }
30132 #endif /* HAVE_WINDOW_SYSTEM */
30133
30134 BEGV = obegv;
30135 ZV = ozv;
30136 current_buffer = obuf;
30137 SAFE_FREE ();
30138 }
30139
30140 set_cursor:
30141
30142 #ifdef HAVE_WINDOW_SYSTEM
30143 if (FRAME_WINDOW_P (f))
30144 define_frame_cursor1 (f, cursor, pointer);
30145 #else
30146 /* This is here to prevent a compiler error, about "label at end of
30147 compound statement". */
30148 return;
30149 #endif
30150 }
30151
30152
30153 /* EXPORT for RIF:
30154 Clear any mouse-face on window W. This function is part of the
30155 redisplay interface, and is called from try_window_id and similar
30156 functions to ensure the mouse-highlight is off. */
30157
30158 void
30159 x_clear_window_mouse_face (struct window *w)
30160 {
30161 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30162 Lisp_Object window;
30163
30164 block_input ();
30165 XSETWINDOW (window, w);
30166 if (EQ (window, hlinfo->mouse_face_window))
30167 clear_mouse_face (hlinfo);
30168 unblock_input ();
30169 }
30170
30171
30172 /* EXPORT:
30173 Just discard the mouse face information for frame F, if any.
30174 This is used when the size of F is changed. */
30175
30176 void
30177 cancel_mouse_face (struct frame *f)
30178 {
30179 Lisp_Object window;
30180 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30181
30182 window = hlinfo->mouse_face_window;
30183 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30184 reset_mouse_highlight (hlinfo);
30185 }
30186
30187
30188 \f
30189 /***********************************************************************
30190 Exposure Events
30191 ***********************************************************************/
30192
30193 #ifdef HAVE_WINDOW_SYSTEM
30194
30195 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30196 which intersects rectangle R. R is in window-relative coordinates. */
30197
30198 static void
30199 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30200 enum glyph_row_area area)
30201 {
30202 struct glyph *first = row->glyphs[area];
30203 struct glyph *end = row->glyphs[area] + row->used[area];
30204 struct glyph *last;
30205 int first_x, start_x, x;
30206
30207 if (area == TEXT_AREA && row->fill_line_p)
30208 /* If row extends face to end of line write the whole line. */
30209 draw_glyphs (w, 0, row, area,
30210 0, row->used[area],
30211 DRAW_NORMAL_TEXT, 0);
30212 else
30213 {
30214 /* Set START_X to the window-relative start position for drawing glyphs of
30215 AREA. The first glyph of the text area can be partially visible.
30216 The first glyphs of other areas cannot. */
30217 start_x = window_box_left_offset (w, area);
30218 x = start_x;
30219 if (area == TEXT_AREA)
30220 x += row->x;
30221
30222 /* Find the first glyph that must be redrawn. */
30223 while (first < end
30224 && x + first->pixel_width < r->x)
30225 {
30226 x += first->pixel_width;
30227 ++first;
30228 }
30229
30230 /* Find the last one. */
30231 last = first;
30232 first_x = x;
30233 /* Use a signed int intermediate value to avoid catastrophic
30234 failures due to comparison between signed and unsigned, when
30235 x is negative (can happen for wide images that are hscrolled). */
30236 int r_end = r->x + r->width;
30237 while (last < end && x < r_end)
30238 {
30239 x += last->pixel_width;
30240 ++last;
30241 }
30242
30243 /* Repaint. */
30244 if (last > first)
30245 draw_glyphs (w, first_x - start_x, row, area,
30246 first - row->glyphs[area], last - row->glyphs[area],
30247 DRAW_NORMAL_TEXT, 0);
30248 }
30249 }
30250
30251
30252 /* Redraw the parts of the glyph row ROW on window W intersecting
30253 rectangle R. R is in window-relative coordinates. Value is
30254 true if mouse-face was overwritten. */
30255
30256 static bool
30257 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30258 {
30259 eassert (row->enabled_p);
30260
30261 if (row->mode_line_p || w->pseudo_window_p)
30262 draw_glyphs (w, 0, row, TEXT_AREA,
30263 0, row->used[TEXT_AREA],
30264 DRAW_NORMAL_TEXT, 0);
30265 else
30266 {
30267 if (row->used[LEFT_MARGIN_AREA])
30268 expose_area (w, row, r, LEFT_MARGIN_AREA);
30269 if (row->used[TEXT_AREA])
30270 expose_area (w, row, r, TEXT_AREA);
30271 if (row->used[RIGHT_MARGIN_AREA])
30272 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30273 draw_row_fringe_bitmaps (w, row);
30274 }
30275
30276 return row->mouse_face_p;
30277 }
30278
30279
30280 /* Redraw those parts of glyphs rows during expose event handling that
30281 overlap other rows. Redrawing of an exposed line writes over parts
30282 of lines overlapping that exposed line; this function fixes that.
30283
30284 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30285 row in W's current matrix that is exposed and overlaps other rows.
30286 LAST_OVERLAPPING_ROW is the last such row. */
30287
30288 static void
30289 expose_overlaps (struct window *w,
30290 struct glyph_row *first_overlapping_row,
30291 struct glyph_row *last_overlapping_row,
30292 XRectangle *r)
30293 {
30294 struct glyph_row *row;
30295
30296 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30297 if (row->overlapping_p)
30298 {
30299 eassert (row->enabled_p && !row->mode_line_p);
30300
30301 row->clip = r;
30302 if (row->used[LEFT_MARGIN_AREA])
30303 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30304
30305 if (row->used[TEXT_AREA])
30306 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30307
30308 if (row->used[RIGHT_MARGIN_AREA])
30309 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30310 row->clip = NULL;
30311 }
30312 }
30313
30314
30315 /* Return true if W's cursor intersects rectangle R. */
30316
30317 static bool
30318 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30319 {
30320 XRectangle cr, result;
30321 struct glyph *cursor_glyph;
30322 struct glyph_row *row;
30323
30324 if (w->phys_cursor.vpos >= 0
30325 && w->phys_cursor.vpos < w->current_matrix->nrows
30326 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30327 row->enabled_p)
30328 && row->cursor_in_fringe_p)
30329 {
30330 /* Cursor is in the fringe. */
30331 cr.x = window_box_right_offset (w,
30332 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30333 ? RIGHT_MARGIN_AREA
30334 : TEXT_AREA));
30335 cr.y = row->y;
30336 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30337 cr.height = row->height;
30338 return x_intersect_rectangles (&cr, r, &result);
30339 }
30340
30341 cursor_glyph = get_phys_cursor_glyph (w);
30342 if (cursor_glyph)
30343 {
30344 /* r is relative to W's box, but w->phys_cursor.x is relative
30345 to left edge of W's TEXT area. Adjust it. */
30346 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30347 cr.y = w->phys_cursor.y;
30348 cr.width = cursor_glyph->pixel_width;
30349 cr.height = w->phys_cursor_height;
30350 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30351 I assume the effect is the same -- and this is portable. */
30352 return x_intersect_rectangles (&cr, r, &result);
30353 }
30354 /* If we don't understand the format, pretend we're not in the hot-spot. */
30355 return false;
30356 }
30357
30358
30359 /* EXPORT:
30360 Draw a vertical window border to the right of window W if W doesn't
30361 have vertical scroll bars. */
30362
30363 void
30364 x_draw_vertical_border (struct window *w)
30365 {
30366 struct frame *f = XFRAME (WINDOW_FRAME (w));
30367
30368 /* We could do better, if we knew what type of scroll-bar the adjacent
30369 windows (on either side) have... But we don't :-(
30370 However, I think this works ok. ++KFS 2003-04-25 */
30371
30372 /* Redraw borders between horizontally adjacent windows. Don't
30373 do it for frames with vertical scroll bars because either the
30374 right scroll bar of a window, or the left scroll bar of its
30375 neighbor will suffice as a border. */
30376 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30377 return;
30378
30379 /* Note: It is necessary to redraw both the left and the right
30380 borders, for when only this single window W is being
30381 redisplayed. */
30382 if (!WINDOW_RIGHTMOST_P (w)
30383 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30384 {
30385 int x0, x1, y0, y1;
30386
30387 window_box_edges (w, &x0, &y0, &x1, &y1);
30388 y1 -= 1;
30389
30390 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30391 x1 -= 1;
30392
30393 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30394 }
30395
30396 if (!WINDOW_LEFTMOST_P (w)
30397 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30398 {
30399 int x0, x1, y0, y1;
30400
30401 window_box_edges (w, &x0, &y0, &x1, &y1);
30402 y1 -= 1;
30403
30404 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30405 x0 -= 1;
30406
30407 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30408 }
30409 }
30410
30411
30412 /* Draw window dividers for window W. */
30413
30414 void
30415 x_draw_right_divider (struct window *w)
30416 {
30417 struct frame *f = WINDOW_XFRAME (w);
30418
30419 if (w->mini || w->pseudo_window_p)
30420 return;
30421 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30422 {
30423 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30424 int x1 = WINDOW_RIGHT_EDGE_X (w);
30425 int y0 = WINDOW_TOP_EDGE_Y (w);
30426 /* The bottom divider prevails. */
30427 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30428
30429 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30430 }
30431 }
30432
30433 static void
30434 x_draw_bottom_divider (struct window *w)
30435 {
30436 struct frame *f = XFRAME (WINDOW_FRAME (w));
30437
30438 if (w->mini || w->pseudo_window_p)
30439 return;
30440 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30441 {
30442 int x0 = WINDOW_LEFT_EDGE_X (w);
30443 int x1 = WINDOW_RIGHT_EDGE_X (w);
30444 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30445 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30446
30447 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30448 }
30449 }
30450
30451 /* Redraw the part of window W intersection rectangle FR. Pixel
30452 coordinates in FR are frame-relative. Call this function with
30453 input blocked. Value is true if the exposure overwrites
30454 mouse-face. */
30455
30456 static bool
30457 expose_window (struct window *w, XRectangle *fr)
30458 {
30459 struct frame *f = XFRAME (w->frame);
30460 XRectangle wr, r;
30461 bool mouse_face_overwritten_p = false;
30462
30463 /* If window is not yet fully initialized, do nothing. This can
30464 happen when toolkit scroll bars are used and a window is split.
30465 Reconfiguring the scroll bar will generate an expose for a newly
30466 created window. */
30467 if (w->current_matrix == NULL)
30468 return false;
30469
30470 /* When we're currently updating the window, display and current
30471 matrix usually don't agree. Arrange for a thorough display
30472 later. */
30473 if (w->must_be_updated_p)
30474 {
30475 SET_FRAME_GARBAGED (f);
30476 return false;
30477 }
30478
30479 /* Frame-relative pixel rectangle of W. */
30480 wr.x = WINDOW_LEFT_EDGE_X (w);
30481 wr.y = WINDOW_TOP_EDGE_Y (w);
30482 wr.width = WINDOW_PIXEL_WIDTH (w);
30483 wr.height = WINDOW_PIXEL_HEIGHT (w);
30484
30485 if (x_intersect_rectangles (fr, &wr, &r))
30486 {
30487 int yb = window_text_bottom_y (w);
30488 struct glyph_row *row;
30489 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30490
30491 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30492 r.x, r.y, r.width, r.height));
30493
30494 /* Convert to window coordinates. */
30495 r.x -= WINDOW_LEFT_EDGE_X (w);
30496 r.y -= WINDOW_TOP_EDGE_Y (w);
30497
30498 /* Turn off the cursor. */
30499 bool cursor_cleared_p = (!w->pseudo_window_p
30500 && phys_cursor_in_rect_p (w, &r));
30501 if (cursor_cleared_p)
30502 x_clear_cursor (w);
30503
30504 /* If the row containing the cursor extends face to end of line,
30505 then expose_area might overwrite the cursor outside the
30506 rectangle and thus notice_overwritten_cursor might clear
30507 w->phys_cursor_on_p. We remember the original value and
30508 check later if it is changed. */
30509 bool phys_cursor_on_p = w->phys_cursor_on_p;
30510
30511 /* Use a signed int intermediate value to avoid catastrophic
30512 failures due to comparison between signed and unsigned, when
30513 y0 or y1 is negative (can happen for tall images). */
30514 int r_bottom = r.y + r.height;
30515
30516 /* Update lines intersecting rectangle R. */
30517 first_overlapping_row = last_overlapping_row = NULL;
30518 for (row = w->current_matrix->rows;
30519 row->enabled_p;
30520 ++row)
30521 {
30522 int y0 = row->y;
30523 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30524
30525 if ((y0 >= r.y && y0 < r_bottom)
30526 || (y1 > r.y && y1 < r_bottom)
30527 || (r.y >= y0 && r.y < y1)
30528 || (r_bottom > y0 && r_bottom < y1))
30529 {
30530 /* A header line may be overlapping, but there is no need
30531 to fix overlapping areas for them. KFS 2005-02-12 */
30532 if (row->overlapping_p && !row->mode_line_p)
30533 {
30534 if (first_overlapping_row == NULL)
30535 first_overlapping_row = row;
30536 last_overlapping_row = row;
30537 }
30538
30539 row->clip = fr;
30540 if (expose_line (w, row, &r))
30541 mouse_face_overwritten_p = true;
30542 row->clip = NULL;
30543 }
30544 else if (row->overlapping_p)
30545 {
30546 /* We must redraw a row overlapping the exposed area. */
30547 if (y0 < r.y
30548 ? y0 + row->phys_height > r.y
30549 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30550 {
30551 if (first_overlapping_row == NULL)
30552 first_overlapping_row = row;
30553 last_overlapping_row = row;
30554 }
30555 }
30556
30557 if (y1 >= yb)
30558 break;
30559 }
30560
30561 /* Display the mode line if there is one. */
30562 if (WINDOW_WANTS_MODELINE_P (w)
30563 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30564 row->enabled_p)
30565 && row->y < r_bottom)
30566 {
30567 if (expose_line (w, row, &r))
30568 mouse_face_overwritten_p = true;
30569 }
30570
30571 if (!w->pseudo_window_p)
30572 {
30573 /* Fix the display of overlapping rows. */
30574 if (first_overlapping_row)
30575 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30576 fr);
30577
30578 /* Draw border between windows. */
30579 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30580 x_draw_right_divider (w);
30581 else
30582 x_draw_vertical_border (w);
30583
30584 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30585 x_draw_bottom_divider (w);
30586
30587 /* Turn the cursor on again. */
30588 if (cursor_cleared_p
30589 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30590 update_window_cursor (w, true);
30591 }
30592 }
30593
30594 return mouse_face_overwritten_p;
30595 }
30596
30597
30598
30599 /* Redraw (parts) of all windows in the window tree rooted at W that
30600 intersect R. R contains frame pixel coordinates. Value is
30601 true if the exposure overwrites mouse-face. */
30602
30603 static bool
30604 expose_window_tree (struct window *w, XRectangle *r)
30605 {
30606 struct frame *f = XFRAME (w->frame);
30607 bool mouse_face_overwritten_p = false;
30608
30609 while (w && !FRAME_GARBAGED_P (f))
30610 {
30611 mouse_face_overwritten_p
30612 |= (WINDOWP (w->contents)
30613 ? expose_window_tree (XWINDOW (w->contents), r)
30614 : expose_window (w, r));
30615
30616 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30617 }
30618
30619 return mouse_face_overwritten_p;
30620 }
30621
30622
30623 /* EXPORT:
30624 Redisplay an exposed area of frame F. X and Y are the upper-left
30625 corner of the exposed rectangle. W and H are width and height of
30626 the exposed area. All are pixel values. W or H zero means redraw
30627 the entire frame. */
30628
30629 void
30630 expose_frame (struct frame *f, int x, int y, int w, int h)
30631 {
30632 XRectangle r;
30633 bool mouse_face_overwritten_p = false;
30634
30635 TRACE ((stderr, "expose_frame "));
30636
30637 /* No need to redraw if frame will be redrawn soon. */
30638 if (FRAME_GARBAGED_P (f))
30639 {
30640 TRACE ((stderr, " garbaged\n"));
30641 return;
30642 }
30643
30644 /* If basic faces haven't been realized yet, there is no point in
30645 trying to redraw anything. This can happen when we get an expose
30646 event while Emacs is starting, e.g. by moving another window. */
30647 if (FRAME_FACE_CACHE (f) == NULL
30648 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30649 {
30650 TRACE ((stderr, " no faces\n"));
30651 return;
30652 }
30653
30654 if (w == 0 || h == 0)
30655 {
30656 r.x = r.y = 0;
30657 r.width = FRAME_TEXT_WIDTH (f);
30658 r.height = FRAME_TEXT_HEIGHT (f);
30659 }
30660 else
30661 {
30662 r.x = x;
30663 r.y = y;
30664 r.width = w;
30665 r.height = h;
30666 }
30667
30668 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30669 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30670
30671 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30672 if (WINDOWP (f->tool_bar_window))
30673 mouse_face_overwritten_p
30674 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30675 #endif
30676
30677 #ifdef HAVE_X_WINDOWS
30678 #ifndef MSDOS
30679 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30680 if (WINDOWP (f->menu_bar_window))
30681 mouse_face_overwritten_p
30682 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30683 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30684 #endif
30685 #endif
30686
30687 /* Some window managers support a focus-follows-mouse style with
30688 delayed raising of frames. Imagine a partially obscured frame,
30689 and moving the mouse into partially obscured mouse-face on that
30690 frame. The visible part of the mouse-face will be highlighted,
30691 then the WM raises the obscured frame. With at least one WM, KDE
30692 2.1, Emacs is not getting any event for the raising of the frame
30693 (even tried with SubstructureRedirectMask), only Expose events.
30694 These expose events will draw text normally, i.e. not
30695 highlighted. Which means we must redo the highlight here.
30696 Subsume it under ``we love X''. --gerd 2001-08-15 */
30697 /* Included in Windows version because Windows most likely does not
30698 do the right thing if any third party tool offers
30699 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30700 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30701 {
30702 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30703 if (f == hlinfo->mouse_face_mouse_frame)
30704 {
30705 int mouse_x = hlinfo->mouse_face_mouse_x;
30706 int mouse_y = hlinfo->mouse_face_mouse_y;
30707 clear_mouse_face (hlinfo);
30708 note_mouse_highlight (f, mouse_x, mouse_y);
30709 }
30710 }
30711 }
30712
30713
30714 /* EXPORT:
30715 Determine the intersection of two rectangles R1 and R2. Return
30716 the intersection in *RESULT. Value is true if RESULT is not
30717 empty. */
30718
30719 bool
30720 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30721 {
30722 XRectangle *left, *right;
30723 XRectangle *upper, *lower;
30724 bool intersection_p = false;
30725
30726 /* Rearrange so that R1 is the left-most rectangle. */
30727 if (r1->x < r2->x)
30728 left = r1, right = r2;
30729 else
30730 left = r2, right = r1;
30731
30732 /* X0 of the intersection is right.x0, if this is inside R1,
30733 otherwise there is no intersection. */
30734 if (right->x <= left->x + left->width)
30735 {
30736 result->x = right->x;
30737
30738 /* The right end of the intersection is the minimum of
30739 the right ends of left and right. */
30740 result->width = (min (left->x + left->width, right->x + right->width)
30741 - result->x);
30742
30743 /* Same game for Y. */
30744 if (r1->y < r2->y)
30745 upper = r1, lower = r2;
30746 else
30747 upper = r2, lower = r1;
30748
30749 /* The upper end of the intersection is lower.y0, if this is inside
30750 of upper. Otherwise, there is no intersection. */
30751 if (lower->y <= upper->y + upper->height)
30752 {
30753 result->y = lower->y;
30754
30755 /* The lower end of the intersection is the minimum of the lower
30756 ends of upper and lower. */
30757 result->height = (min (lower->y + lower->height,
30758 upper->y + upper->height)
30759 - result->y);
30760 intersection_p = true;
30761 }
30762 }
30763
30764 return intersection_p;
30765 }
30766
30767 #endif /* HAVE_WINDOW_SYSTEM */
30768
30769 \f
30770 /***********************************************************************
30771 Initialization
30772 ***********************************************************************/
30773
30774 void
30775 syms_of_xdisp (void)
30776 {
30777 Vwith_echo_area_save_vector = Qnil;
30778 staticpro (&Vwith_echo_area_save_vector);
30779
30780 Vmessage_stack = Qnil;
30781 staticpro (&Vmessage_stack);
30782
30783 /* Non-nil means don't actually do any redisplay. */
30784 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30785
30786 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30787
30788 DEFVAR_BOOL("inhibit-message", inhibit_message,
30789 doc: /* Non-nil means calls to `message' are not displayed.
30790 They are still logged to the *Messages* buffer. */);
30791 inhibit_message = 0;
30792
30793 message_dolog_marker1 = Fmake_marker ();
30794 staticpro (&message_dolog_marker1);
30795 message_dolog_marker2 = Fmake_marker ();
30796 staticpro (&message_dolog_marker2);
30797 message_dolog_marker3 = Fmake_marker ();
30798 staticpro (&message_dolog_marker3);
30799
30800 #ifdef GLYPH_DEBUG
30801 defsubr (&Sdump_frame_glyph_matrix);
30802 defsubr (&Sdump_glyph_matrix);
30803 defsubr (&Sdump_glyph_row);
30804 defsubr (&Sdump_tool_bar_row);
30805 defsubr (&Strace_redisplay);
30806 defsubr (&Strace_to_stderr);
30807 #endif
30808 #ifdef HAVE_WINDOW_SYSTEM
30809 defsubr (&Stool_bar_height);
30810 defsubr (&Slookup_image_map);
30811 #endif
30812 defsubr (&Sline_pixel_height);
30813 defsubr (&Sformat_mode_line);
30814 defsubr (&Sinvisible_p);
30815 defsubr (&Scurrent_bidi_paragraph_direction);
30816 defsubr (&Swindow_text_pixel_size);
30817 defsubr (&Smove_point_visually);
30818 defsubr (&Sbidi_find_overridden_directionality);
30819
30820 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30821 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30822 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30823 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30824 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30825 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30826 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30827 DEFSYM (Qeval, "eval");
30828 DEFSYM (QCdata, ":data");
30829
30830 /* Names of text properties relevant for redisplay. */
30831 DEFSYM (Qdisplay, "display");
30832 DEFSYM (Qspace_width, "space-width");
30833 DEFSYM (Qraise, "raise");
30834 DEFSYM (Qslice, "slice");
30835 DEFSYM (Qspace, "space");
30836 DEFSYM (Qmargin, "margin");
30837 DEFSYM (Qpointer, "pointer");
30838 DEFSYM (Qleft_margin, "left-margin");
30839 DEFSYM (Qright_margin, "right-margin");
30840 DEFSYM (Qcenter, "center");
30841 DEFSYM (Qline_height, "line-height");
30842 DEFSYM (QCalign_to, ":align-to");
30843 DEFSYM (QCrelative_width, ":relative-width");
30844 DEFSYM (QCrelative_height, ":relative-height");
30845 DEFSYM (QCeval, ":eval");
30846 DEFSYM (QCpropertize, ":propertize");
30847 DEFSYM (QCfile, ":file");
30848 DEFSYM (Qfontified, "fontified");
30849 DEFSYM (Qfontification_functions, "fontification-functions");
30850
30851 /* Name of the face used to highlight trailing whitespace. */
30852 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30853
30854 /* Name and number of the face used to highlight escape glyphs. */
30855 DEFSYM (Qescape_glyph, "escape-glyph");
30856
30857 /* Name and number of the face used to highlight non-breaking spaces. */
30858 DEFSYM (Qnobreak_space, "nobreak-space");
30859
30860 /* The symbol 'image' which is the car of the lists used to represent
30861 images in Lisp. Also a tool bar style. */
30862 DEFSYM (Qimage, "image");
30863
30864 /* Tool bar styles. */
30865 DEFSYM (Qtext, "text");
30866 DEFSYM (Qboth, "both");
30867 DEFSYM (Qboth_horiz, "both-horiz");
30868 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30869
30870 /* The image map types. */
30871 DEFSYM (QCmap, ":map");
30872 DEFSYM (QCpointer, ":pointer");
30873 DEFSYM (Qrect, "rect");
30874 DEFSYM (Qcircle, "circle");
30875 DEFSYM (Qpoly, "poly");
30876
30877 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30878
30879 DEFSYM (Qgrow_only, "grow-only");
30880 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30881 DEFSYM (Qposition, "position");
30882 DEFSYM (Qbuffer_position, "buffer-position");
30883 DEFSYM (Qobject, "object");
30884
30885 /* Cursor shapes. */
30886 DEFSYM (Qbar, "bar");
30887 DEFSYM (Qhbar, "hbar");
30888 DEFSYM (Qbox, "box");
30889 DEFSYM (Qhollow, "hollow");
30890
30891 /* Pointer shapes. */
30892 DEFSYM (Qhand, "hand");
30893 DEFSYM (Qarrow, "arrow");
30894 /* also Qtext */
30895
30896 DEFSYM (Qdragging, "dragging");
30897
30898 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30899
30900 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30901 staticpro (&list_of_error);
30902
30903 /* Values of those variables at last redisplay are stored as
30904 properties on 'overlay-arrow-position' symbol. However, if
30905 Voverlay_arrow_position is a marker, last-arrow-position is its
30906 numerical position. */
30907 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30908 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30909
30910 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30911 properties on a symbol in overlay-arrow-variable-list. */
30912 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30913 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30914
30915 echo_buffer[0] = echo_buffer[1] = Qnil;
30916 staticpro (&echo_buffer[0]);
30917 staticpro (&echo_buffer[1]);
30918
30919 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30920 staticpro (&echo_area_buffer[0]);
30921 staticpro (&echo_area_buffer[1]);
30922
30923 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30924 staticpro (&Vmessages_buffer_name);
30925
30926 mode_line_proptrans_alist = Qnil;
30927 staticpro (&mode_line_proptrans_alist);
30928 mode_line_string_list = Qnil;
30929 staticpro (&mode_line_string_list);
30930 mode_line_string_face = Qnil;
30931 staticpro (&mode_line_string_face);
30932 mode_line_string_face_prop = Qnil;
30933 staticpro (&mode_line_string_face_prop);
30934 Vmode_line_unwind_vector = Qnil;
30935 staticpro (&Vmode_line_unwind_vector);
30936
30937 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30938
30939 help_echo_string = Qnil;
30940 staticpro (&help_echo_string);
30941 help_echo_object = Qnil;
30942 staticpro (&help_echo_object);
30943 help_echo_window = Qnil;
30944 staticpro (&help_echo_window);
30945 previous_help_echo_string = Qnil;
30946 staticpro (&previous_help_echo_string);
30947 help_echo_pos = -1;
30948
30949 DEFSYM (Qright_to_left, "right-to-left");
30950 DEFSYM (Qleft_to_right, "left-to-right");
30951 defsubr (&Sbidi_resolved_levels);
30952
30953 #ifdef HAVE_WINDOW_SYSTEM
30954 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30955 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30956 For example, if a block cursor is over a tab, it will be drawn as
30957 wide as that tab on the display. */);
30958 x_stretch_cursor_p = 0;
30959 #endif
30960
30961 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30962 doc: /* Non-nil means highlight trailing whitespace.
30963 The face used for trailing whitespace is `trailing-whitespace'. */);
30964 Vshow_trailing_whitespace = Qnil;
30965
30966 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30967 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30968 If the value is t, Emacs highlights non-ASCII chars which have the
30969 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30970 or `escape-glyph' face respectively.
30971
30972 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30973 U+2011 (non-breaking hyphen) are affected.
30974
30975 Any other non-nil value means to display these characters as a escape
30976 glyph followed by an ordinary space or hyphen.
30977
30978 A value of nil means no special handling of these characters. */);
30979 Vnobreak_char_display = Qt;
30980
30981 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30982 doc: /* The pointer shape to show in void text areas.
30983 A value of nil means to show the text pointer. Other options are
30984 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30985 `hourglass'. */);
30986 Vvoid_text_area_pointer = Qarrow;
30987
30988 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30989 doc: /* Non-nil means don't actually do any redisplay.
30990 This is used for internal purposes. */);
30991 Vinhibit_redisplay = Qnil;
30992
30993 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30994 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30995 Vglobal_mode_string = Qnil;
30996
30997 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30998 doc: /* Marker for where to display an arrow on top of the buffer text.
30999 This must be the beginning of a line in order to work.
31000 See also `overlay-arrow-string'. */);
31001 Voverlay_arrow_position = Qnil;
31002
31003 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31004 doc: /* String to display as an arrow in non-window frames.
31005 See also `overlay-arrow-position'. */);
31006 Voverlay_arrow_string = build_pure_c_string ("=>");
31007
31008 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31009 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31010 The symbols on this list are examined during redisplay to determine
31011 where to display overlay arrows. */);
31012 Voverlay_arrow_variable_list
31013 = list1 (intern_c_string ("overlay-arrow-position"));
31014
31015 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31016 doc: /* The number of lines to try scrolling a window by when point moves out.
31017 If that fails to bring point back on frame, point is centered instead.
31018 If this is zero, point is always centered after it moves off frame.
31019 If you want scrolling to always be a line at a time, you should set
31020 `scroll-conservatively' to a large value rather than set this to 1. */);
31021
31022 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31023 doc: /* Scroll up to this many lines, to bring point back on screen.
31024 If point moves off-screen, redisplay will scroll by up to
31025 `scroll-conservatively' lines in order to bring point just barely
31026 onto the screen again. If that cannot be done, then redisplay
31027 recenters point as usual.
31028
31029 If the value is greater than 100, redisplay will never recenter point,
31030 but will always scroll just enough text to bring point into view, even
31031 if you move far away.
31032
31033 A value of zero means always recenter point if it moves off screen. */);
31034 scroll_conservatively = 0;
31035
31036 DEFVAR_INT ("scroll-margin", scroll_margin,
31037 doc: /* Number of lines of margin at the top and bottom of a window.
31038 Recenter the window whenever point gets within this many lines
31039 of the top or bottom of the window. */);
31040 scroll_margin = 0;
31041
31042 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31043 doc: /* Pixels per inch value for non-window system displays.
31044 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31045 Vdisplay_pixels_per_inch = make_float (72.0);
31046
31047 #ifdef GLYPH_DEBUG
31048 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31049 #endif
31050
31051 DEFVAR_LISP ("truncate-partial-width-windows",
31052 Vtruncate_partial_width_windows,
31053 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31054 For an integer value, truncate lines in each window narrower than the
31055 full frame width, provided the window width is less than that integer;
31056 otherwise, respect the value of `truncate-lines'.
31057
31058 For any other non-nil value, truncate lines in all windows that do
31059 not span the full frame width.
31060
31061 A value of nil means to respect the value of `truncate-lines'.
31062
31063 If `word-wrap' is enabled, you might want to reduce this. */);
31064 Vtruncate_partial_width_windows = make_number (50);
31065
31066 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31067 doc: /* Maximum buffer size for which line number should be displayed.
31068 If the buffer is bigger than this, the line number does not appear
31069 in the mode line. A value of nil means no limit. */);
31070 Vline_number_display_limit = Qnil;
31071
31072 DEFVAR_INT ("line-number-display-limit-width",
31073 line_number_display_limit_width,
31074 doc: /* Maximum line width (in characters) for line number display.
31075 If the average length of the lines near point is bigger than this, then the
31076 line number may be omitted from the mode line. */);
31077 line_number_display_limit_width = 200;
31078
31079 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31080 doc: /* Non-nil means highlight region even in nonselected windows. */);
31081 highlight_nonselected_windows = false;
31082
31083 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31084 doc: /* Non-nil if more than one frame is visible on this display.
31085 Minibuffer-only frames don't count, but iconified frames do.
31086 This variable is not guaranteed to be accurate except while processing
31087 `frame-title-format' and `icon-title-format'. */);
31088
31089 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31090 doc: /* Template for displaying the title bar of visible frames.
31091 (Assuming the window manager supports this feature.)
31092
31093 This variable has the same structure as `mode-line-format', except that
31094 the %c and %l constructs are ignored. It is used only on frames for
31095 which no explicit name has been set (see `modify-frame-parameters'). */);
31096
31097 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31098 doc: /* Template for displaying the title bar of an iconified frame.
31099 (Assuming the window manager supports this feature.)
31100 This variable has the same structure as `mode-line-format' (which see),
31101 and is used only on frames for which no explicit name has been set
31102 (see `modify-frame-parameters'). */);
31103 Vicon_title_format
31104 = Vframe_title_format
31105 = listn (CONSTYPE_PURE, 3,
31106 intern_c_string ("multiple-frames"),
31107 build_pure_c_string ("%b"),
31108 listn (CONSTYPE_PURE, 4,
31109 empty_unibyte_string,
31110 intern_c_string ("invocation-name"),
31111 build_pure_c_string ("@"),
31112 intern_c_string ("system-name")));
31113
31114 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31115 doc: /* Maximum number of lines to keep in the message log buffer.
31116 If nil, disable message logging. If t, log messages but don't truncate
31117 the buffer when it becomes large. */);
31118 Vmessage_log_max = make_number (1000);
31119
31120 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31121 doc: /* Functions called before redisplay, if window sizes have changed.
31122 The value should be a list of functions that take one argument.
31123 Just before redisplay, for each frame, if any of its windows have changed
31124 size since the last redisplay, or have been split or deleted,
31125 all the functions in the list are called, with the frame as argument. */);
31126 Vwindow_size_change_functions = Qnil;
31127
31128 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31129 doc: /* List of functions to call before redisplaying a window with scrolling.
31130 Each function is called with two arguments, the window and its new
31131 display-start position.
31132 These functions are called whenever the `window-start' marker is modified,
31133 either to point into another buffer (e.g. via `set-window-buffer') or another
31134 place in the same buffer.
31135 Note that the value of `window-end' is not valid when these functions are
31136 called.
31137
31138 Warning: Do not use this feature to alter the way the window
31139 is scrolled. It is not designed for that, and such use probably won't
31140 work. */);
31141 Vwindow_scroll_functions = Qnil;
31142
31143 DEFVAR_LISP ("window-text-change-functions",
31144 Vwindow_text_change_functions,
31145 doc: /* Functions to call in redisplay when text in the window might change. */);
31146 Vwindow_text_change_functions = Qnil;
31147
31148 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31149 doc: /* Functions called when redisplay of a window reaches the end trigger.
31150 Each function is called with two arguments, the window and the end trigger value.
31151 See `set-window-redisplay-end-trigger'. */);
31152 Vredisplay_end_trigger_functions = Qnil;
31153
31154 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31155 doc: /* Non-nil means autoselect window with mouse pointer.
31156 If nil, do not autoselect windows.
31157 A positive number means delay autoselection by that many seconds: a
31158 window is autoselected only after the mouse has remained in that
31159 window for the duration of the delay.
31160 A negative number has a similar effect, but causes windows to be
31161 autoselected only after the mouse has stopped moving. (Because of
31162 the way Emacs compares mouse events, you will occasionally wait twice
31163 that time before the window gets selected.)
31164 Any other value means to autoselect window instantaneously when the
31165 mouse pointer enters it.
31166
31167 Autoselection selects the minibuffer only if it is active, and never
31168 unselects the minibuffer if it is active.
31169
31170 When customizing this variable make sure that the actual value of
31171 `focus-follows-mouse' matches the behavior of your window manager. */);
31172 Vmouse_autoselect_window = Qnil;
31173
31174 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31175 doc: /* Non-nil means automatically resize tool-bars.
31176 This dynamically changes the tool-bar's height to the minimum height
31177 that is needed to make all tool-bar items visible.
31178 If value is `grow-only', the tool-bar's height is only increased
31179 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31180 Vauto_resize_tool_bars = Qt;
31181
31182 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31183 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31184 auto_raise_tool_bar_buttons_p = true;
31185
31186 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31187 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31188 make_cursor_line_fully_visible_p = true;
31189
31190 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31191 doc: /* Border below tool-bar in pixels.
31192 If an integer, use it as the height of the border.
31193 If it is one of `internal-border-width' or `border-width', use the
31194 value of the corresponding frame parameter.
31195 Otherwise, no border is added below the tool-bar. */);
31196 Vtool_bar_border = Qinternal_border_width;
31197
31198 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31199 doc: /* Margin around tool-bar buttons in pixels.
31200 If an integer, use that for both horizontal and vertical margins.
31201 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31202 HORZ specifying the horizontal margin, and VERT specifying the
31203 vertical margin. */);
31204 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31205
31206 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31207 doc: /* Relief thickness of tool-bar buttons. */);
31208 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31209
31210 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31211 doc: /* Tool bar style to use.
31212 It can be one of
31213 image - show images only
31214 text - show text only
31215 both - show both, text below image
31216 both-horiz - show text to the right of the image
31217 text-image-horiz - show text to the left of the image
31218 any other - use system default or image if no system default.
31219
31220 This variable only affects the GTK+ toolkit version of Emacs. */);
31221 Vtool_bar_style = Qnil;
31222
31223 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31224 doc: /* Maximum number of characters a label can have to be shown.
31225 The tool bar style must also show labels for this to have any effect, see
31226 `tool-bar-style'. */);
31227 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31228
31229 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31230 doc: /* List of functions to call to fontify regions of text.
31231 Each function is called with one argument POS. Functions must
31232 fontify a region starting at POS in the current buffer, and give
31233 fontified regions the property `fontified'. */);
31234 Vfontification_functions = Qnil;
31235 Fmake_variable_buffer_local (Qfontification_functions);
31236
31237 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31238 unibyte_display_via_language_environment,
31239 doc: /* Non-nil means display unibyte text according to language environment.
31240 Specifically, this means that raw bytes in the range 160-255 decimal
31241 are displayed by converting them to the equivalent multibyte characters
31242 according to the current language environment. As a result, they are
31243 displayed according to the current fontset.
31244
31245 Note that this variable affects only how these bytes are displayed,
31246 but does not change the fact they are interpreted as raw bytes. */);
31247 unibyte_display_via_language_environment = false;
31248
31249 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31250 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31251 If a float, it specifies a fraction of the mini-window frame's height.
31252 If an integer, it specifies a number of lines. */);
31253 Vmax_mini_window_height = make_float (0.25);
31254
31255 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31256 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31257 A value of nil means don't automatically resize mini-windows.
31258 A value of t means resize them to fit the text displayed in them.
31259 A value of `grow-only', the default, means let mini-windows grow only;
31260 they return to their normal size when the minibuffer is closed, or the
31261 echo area becomes empty. */);
31262 Vresize_mini_windows = Qgrow_only;
31263
31264 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31265 doc: /* Alist specifying how to blink the cursor off.
31266 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31267 `cursor-type' frame-parameter or variable equals ON-STATE,
31268 comparing using `equal', Emacs uses OFF-STATE to specify
31269 how to blink it off. ON-STATE and OFF-STATE are values for
31270 the `cursor-type' frame parameter.
31271
31272 If a frame's ON-STATE has no entry in this list,
31273 the frame's other specifications determine how to blink the cursor off. */);
31274 Vblink_cursor_alist = Qnil;
31275
31276 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31277 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31278 If non-nil, windows are automatically scrolled horizontally to make
31279 point visible. */);
31280 automatic_hscrolling_p = true;
31281 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31282
31283 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31284 doc: /* How many columns away from the window edge point is allowed to get
31285 before automatic hscrolling will horizontally scroll the window. */);
31286 hscroll_margin = 5;
31287
31288 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31289 doc: /* How many columns to scroll the window when point gets too close to the edge.
31290 When point is less than `hscroll-margin' columns from the window
31291 edge, automatic hscrolling will scroll the window by the amount of columns
31292 determined by this variable. If its value is a positive integer, scroll that
31293 many columns. If it's a positive floating-point number, it specifies the
31294 fraction of the window's width to scroll. If it's nil or zero, point will be
31295 centered horizontally after the scroll. Any other value, including negative
31296 numbers, are treated as if the value were zero.
31297
31298 Automatic hscrolling always moves point outside the scroll margin, so if
31299 point was more than scroll step columns inside the margin, the window will
31300 scroll more than the value given by the scroll step.
31301
31302 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31303 and `scroll-right' overrides this variable's effect. */);
31304 Vhscroll_step = make_number (0);
31305
31306 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31307 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31308 Bind this around calls to `message' to let it take effect. */);
31309 message_truncate_lines = false;
31310
31311 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31312 doc: /* Normal hook run to update the menu bar definitions.
31313 Redisplay runs this hook before it redisplays the menu bar.
31314 This is used to update menus such as Buffers, whose contents depend on
31315 various data. */);
31316 Vmenu_bar_update_hook = Qnil;
31317
31318 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31319 doc: /* Frame for which we are updating a menu.
31320 The enable predicate for a menu binding should check this variable. */);
31321 Vmenu_updating_frame = Qnil;
31322
31323 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31324 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31325 inhibit_menubar_update = false;
31326
31327 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31328 doc: /* Prefix prepended to all continuation lines at display time.
31329 The value may be a string, an image, or a stretch-glyph; it is
31330 interpreted in the same way as the value of a `display' text property.
31331
31332 This variable is overridden by any `wrap-prefix' text or overlay
31333 property.
31334
31335 To add a prefix to non-continuation lines, use `line-prefix'. */);
31336 Vwrap_prefix = Qnil;
31337 DEFSYM (Qwrap_prefix, "wrap-prefix");
31338 Fmake_variable_buffer_local (Qwrap_prefix);
31339
31340 DEFVAR_LISP ("line-prefix", Vline_prefix,
31341 doc: /* Prefix prepended to all non-continuation lines at display time.
31342 The value may be a string, an image, or a stretch-glyph; it is
31343 interpreted in the same way as the value of a `display' text property.
31344
31345 This variable is overridden by any `line-prefix' text or overlay
31346 property.
31347
31348 To add a prefix to continuation lines, use `wrap-prefix'. */);
31349 Vline_prefix = Qnil;
31350 DEFSYM (Qline_prefix, "line-prefix");
31351 Fmake_variable_buffer_local (Qline_prefix);
31352
31353 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31354 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31355 inhibit_eval_during_redisplay = false;
31356
31357 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31358 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31359 inhibit_free_realized_faces = false;
31360
31361 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31362 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31363 Intended for use during debugging and for testing bidi display;
31364 see biditest.el in the test suite. */);
31365 inhibit_bidi_mirroring = false;
31366
31367 #ifdef GLYPH_DEBUG
31368 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31369 doc: /* Inhibit try_window_id display optimization. */);
31370 inhibit_try_window_id = false;
31371
31372 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31373 doc: /* Inhibit try_window_reusing display optimization. */);
31374 inhibit_try_window_reusing = false;
31375
31376 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31377 doc: /* Inhibit try_cursor_movement display optimization. */);
31378 inhibit_try_cursor_movement = false;
31379 #endif /* GLYPH_DEBUG */
31380
31381 DEFVAR_INT ("overline-margin", overline_margin,
31382 doc: /* Space between overline and text, in pixels.
31383 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31384 margin to the character height. */);
31385 overline_margin = 2;
31386
31387 DEFVAR_INT ("underline-minimum-offset",
31388 underline_minimum_offset,
31389 doc: /* Minimum distance between baseline and underline.
31390 This can improve legibility of underlined text at small font sizes,
31391 particularly when using variable `x-use-underline-position-properties'
31392 with fonts that specify an UNDERLINE_POSITION relatively close to the
31393 baseline. The default value is 1. */);
31394 underline_minimum_offset = 1;
31395
31396 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31397 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31398 This feature only works when on a window system that can change
31399 cursor shapes. */);
31400 display_hourglass_p = true;
31401
31402 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31403 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31404 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31405
31406 #ifdef HAVE_WINDOW_SYSTEM
31407 hourglass_atimer = NULL;
31408 hourglass_shown_p = false;
31409 #endif /* HAVE_WINDOW_SYSTEM */
31410
31411 /* Name of the face used to display glyphless characters. */
31412 DEFSYM (Qglyphless_char, "glyphless-char");
31413
31414 /* Method symbols for Vglyphless_char_display. */
31415 DEFSYM (Qhex_code, "hex-code");
31416 DEFSYM (Qempty_box, "empty-box");
31417 DEFSYM (Qthin_space, "thin-space");
31418 DEFSYM (Qzero_width, "zero-width");
31419
31420 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31421 doc: /* Function run just before redisplay.
31422 It is called with one argument, which is the set of windows that are to
31423 be redisplayed. This set can be nil (meaning, only the selected window),
31424 or t (meaning all windows). */);
31425 Vpre_redisplay_function = intern ("ignore");
31426
31427 /* Symbol for the purpose of Vglyphless_char_display. */
31428 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31429 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31430
31431 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31432 doc: /* Char-table defining glyphless characters.
31433 Each element, if non-nil, should be one of the following:
31434 an ASCII acronym string: display this string in a box
31435 `hex-code': display the hexadecimal code of a character in a box
31436 `empty-box': display as an empty box
31437 `thin-space': display as 1-pixel width space
31438 `zero-width': don't display
31439 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31440 display method for graphical terminals and text terminals respectively.
31441 GRAPHICAL and TEXT should each have one of the values listed above.
31442
31443 The char-table has one extra slot to control the display of a character for
31444 which no font is found. This slot only takes effect on graphical terminals.
31445 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31446 `thin-space'. The default is `empty-box'.
31447
31448 If a character has a non-nil entry in an active display table, the
31449 display table takes effect; in this case, Emacs does not consult
31450 `glyphless-char-display' at all. */);
31451 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31452 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31453 Qempty_box);
31454
31455 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31456 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31457 Vdebug_on_message = Qnil;
31458
31459 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31460 doc: /* */);
31461 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31462
31463 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31464 doc: /* */);
31465 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31466 }
31467
31468
31469 /* Initialize this module when Emacs starts. */
31470
31471 void
31472 init_xdisp (void)
31473 {
31474 CHARPOS (this_line_start_pos) = 0;
31475
31476 if (!noninteractive)
31477 {
31478 struct window *m = XWINDOW (minibuf_window);
31479 Lisp_Object frame = m->frame;
31480 struct frame *f = XFRAME (frame);
31481 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31482 struct window *r = XWINDOW (root);
31483 int i;
31484
31485 echo_area_window = minibuf_window;
31486
31487 r->top_line = FRAME_TOP_MARGIN (f);
31488 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31489 r->total_cols = FRAME_COLS (f);
31490 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31491 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31492 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31493
31494 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31495 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31496 m->total_cols = FRAME_COLS (f);
31497 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31498 m->total_lines = 1;
31499 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31500
31501 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31502 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31503 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31504
31505 /* The default ellipsis glyphs `...'. */
31506 for (i = 0; i < 3; ++i)
31507 default_invis_vector[i] = make_number ('.');
31508 }
31509
31510 {
31511 /* Allocate the buffer for frame titles.
31512 Also used for `format-mode-line'. */
31513 int size = 100;
31514 mode_line_noprop_buf = xmalloc (size);
31515 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31516 mode_line_noprop_ptr = mode_line_noprop_buf;
31517 mode_line_target = MODE_LINE_DISPLAY;
31518 }
31519
31520 help_echo_showing_p = false;
31521 }
31522
31523 #ifdef HAVE_WINDOW_SYSTEM
31524
31525 /* Platform-independent portion of hourglass implementation. */
31526
31527 /* Timer function of hourglass_atimer. */
31528
31529 static void
31530 show_hourglass (struct atimer *timer)
31531 {
31532 /* The timer implementation will cancel this timer automatically
31533 after this function has run. Set hourglass_atimer to null
31534 so that we know the timer doesn't have to be canceled. */
31535 hourglass_atimer = NULL;
31536
31537 if (!hourglass_shown_p)
31538 {
31539 Lisp_Object tail, frame;
31540
31541 block_input ();
31542
31543 FOR_EACH_FRAME (tail, frame)
31544 {
31545 struct frame *f = XFRAME (frame);
31546
31547 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31548 && FRAME_RIF (f)->show_hourglass)
31549 FRAME_RIF (f)->show_hourglass (f);
31550 }
31551
31552 hourglass_shown_p = true;
31553 unblock_input ();
31554 }
31555 }
31556
31557 /* Cancel a currently active hourglass timer, and start a new one. */
31558
31559 void
31560 start_hourglass (void)
31561 {
31562 struct timespec delay;
31563
31564 cancel_hourglass ();
31565
31566 if (INTEGERP (Vhourglass_delay)
31567 && XINT (Vhourglass_delay) > 0)
31568 delay = make_timespec (min (XINT (Vhourglass_delay),
31569 TYPE_MAXIMUM (time_t)),
31570 0);
31571 else if (FLOATP (Vhourglass_delay)
31572 && XFLOAT_DATA (Vhourglass_delay) > 0)
31573 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31574 else
31575 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31576
31577 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31578 show_hourglass, NULL);
31579 }
31580
31581 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31582 shown. */
31583
31584 void
31585 cancel_hourglass (void)
31586 {
31587 if (hourglass_atimer)
31588 {
31589 cancel_atimer (hourglass_atimer);
31590 hourglass_atimer = NULL;
31591 }
31592
31593 if (hourglass_shown_p)
31594 {
31595 Lisp_Object tail, frame;
31596
31597 block_input ();
31598
31599 FOR_EACH_FRAME (tail, frame)
31600 {
31601 struct frame *f = XFRAME (frame);
31602
31603 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31604 && FRAME_RIF (f)->hide_hourglass)
31605 FRAME_RIF (f)->hide_hourglass (f);
31606 #ifdef HAVE_NTGUI
31607 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31608 else if (!FRAME_W32_P (f))
31609 w32_arrow_cursor ();
31610 #endif
31611 }
31612
31613 hourglass_shown_p = false;
31614 unblock_input ();
31615 }
31616 }
31617
31618 #endif /* HAVE_WINDOW_SYSTEM */