]> code.delx.au - gnu-emacs/blob - src/xdisp.c
; * src/xdisp.c: Improve commentary for 'update_mode_lines'.
[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. Not 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 windows_or_buffers_changed = 50;
13935 goto retry;
13936 }
13937
13938 /* Prevent freeing of realized faces, since desired matrices are
13939 pending that reference the faces we computed and cached. */
13940 inhibit_free_realized_faces = true;
13941
13942 /* Prevent various kinds of signals during display update.
13943 stdio is not robust about handling signals,
13944 which can cause an apparent I/O error. */
13945 if (interrupt_input)
13946 unrequest_sigio ();
13947 STOP_POLLING;
13948
13949 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13950 {
13951 if (hscroll_windows (selected_window))
13952 goto retry;
13953
13954 XWINDOW (selected_window)->must_be_updated_p = true;
13955 pending = update_frame (sf, false, false);
13956 sf->cursor_type_changed = false;
13957 }
13958
13959 /* We may have called echo_area_display at the top of this
13960 function. If the echo area is on another frame, that may
13961 have put text on a frame other than the selected one, so the
13962 above call to update_frame would not have caught it. Catch
13963 it here. */
13964 mini_window = FRAME_MINIBUF_WINDOW (sf);
13965 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13966
13967 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13968 {
13969 XWINDOW (mini_window)->must_be_updated_p = true;
13970 pending |= update_frame (mini_frame, false, false);
13971 mini_frame->cursor_type_changed = false;
13972 if (!pending && hscroll_windows (mini_window))
13973 goto retry;
13974 }
13975 }
13976
13977 /* If display was paused because of pending input, make sure we do a
13978 thorough update the next time. */
13979 if (pending)
13980 {
13981 /* Prevent the optimization at the beginning of
13982 redisplay_internal that tries a single-line update of the
13983 line containing the cursor in the selected window. */
13984 CHARPOS (this_line_start_pos) = 0;
13985
13986 /* Let the overlay arrow be updated the next time. */
13987 update_overlay_arrows (0);
13988
13989 /* If we pause after scrolling, some rows in the current
13990 matrices of some windows are not valid. */
13991 if (!WINDOW_FULL_WIDTH_P (w)
13992 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13993 update_mode_lines = 36;
13994 }
13995 else
13996 {
13997 if (!consider_all_windows_p)
13998 {
13999 /* This has already been done above if
14000 consider_all_windows_p is set. */
14001 if (XBUFFER (w->contents)->text->redisplay
14002 && buffer_window_count (XBUFFER (w->contents)) > 1)
14003 /* This can happen if b->text->redisplay was set during
14004 jit-lock. */
14005 propagate_buffer_redisplay ();
14006 mark_window_display_accurate_1 (w, true);
14007
14008 /* Say overlay arrows are up to date. */
14009 update_overlay_arrows (1);
14010
14011 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14012 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14013 }
14014
14015 update_mode_lines = 0;
14016 windows_or_buffers_changed = 0;
14017 }
14018
14019 /* Start SIGIO interrupts coming again. Having them off during the
14020 code above makes it less likely one will discard output, but not
14021 impossible, since there might be stuff in the system buffer here.
14022 But it is much hairier to try to do anything about that. */
14023 if (interrupt_input)
14024 request_sigio ();
14025 RESUME_POLLING;
14026
14027 /* If a frame has become visible which was not before, redisplay
14028 again, so that we display it. Expose events for such a frame
14029 (which it gets when becoming visible) don't call the parts of
14030 redisplay constructing glyphs, so simply exposing a frame won't
14031 display anything in this case. So, we have to display these
14032 frames here explicitly. */
14033 if (!pending)
14034 {
14035 int new_count = 0;
14036
14037 FOR_EACH_FRAME (tail, frame)
14038 {
14039 if (XFRAME (frame)->visible)
14040 new_count++;
14041 }
14042
14043 if (new_count != number_of_visible_frames)
14044 windows_or_buffers_changed = 52;
14045 }
14046
14047 /* Change frame size now if a change is pending. */
14048 do_pending_window_change (true);
14049
14050 /* If we just did a pending size change, or have additional
14051 visible frames, or selected_window changed, redisplay again. */
14052 if ((windows_or_buffers_changed && !pending)
14053 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14054 goto retry;
14055
14056 /* Clear the face and image caches.
14057
14058 We used to do this only if consider_all_windows_p. But the cache
14059 needs to be cleared if a timer creates images in the current
14060 buffer (e.g. the test case in Bug#6230). */
14061
14062 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14063 {
14064 clear_face_cache (false);
14065 clear_face_cache_count = 0;
14066 }
14067
14068 #ifdef HAVE_WINDOW_SYSTEM
14069 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14070 {
14071 clear_image_caches (Qnil);
14072 clear_image_cache_count = 0;
14073 }
14074 #endif /* HAVE_WINDOW_SYSTEM */
14075
14076 end_of_redisplay:
14077 #ifdef HAVE_NS
14078 ns_set_doc_edited ();
14079 #endif
14080 if (interrupt_input && interrupts_deferred)
14081 request_sigio ();
14082
14083 unbind_to (count, Qnil);
14084 RESUME_POLLING;
14085 }
14086
14087
14088 /* Redisplay, but leave alone any recent echo area message unless
14089 another message has been requested in its place.
14090
14091 This is useful in situations where you need to redisplay but no
14092 user action has occurred, making it inappropriate for the message
14093 area to be cleared. See tracking_off and
14094 wait_reading_process_output for examples of these situations.
14095
14096 FROM_WHERE is an integer saying from where this function was
14097 called. This is useful for debugging. */
14098
14099 void
14100 redisplay_preserve_echo_area (int from_where)
14101 {
14102 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14103
14104 if (!NILP (echo_area_buffer[1]))
14105 {
14106 /* We have a previously displayed message, but no current
14107 message. Redisplay the previous message. */
14108 display_last_displayed_message_p = true;
14109 redisplay_internal ();
14110 display_last_displayed_message_p = false;
14111 }
14112 else
14113 redisplay_internal ();
14114
14115 flush_frame (SELECTED_FRAME ());
14116 }
14117
14118
14119 /* Function registered with record_unwind_protect in redisplay_internal. */
14120
14121 static void
14122 unwind_redisplay (void)
14123 {
14124 redisplaying_p = false;
14125 }
14126
14127
14128 /* Mark the display of leaf window W as accurate or inaccurate.
14129 If ACCURATE_P, mark display of W as accurate.
14130 If !ACCURATE_P, arrange for W to be redisplayed the next
14131 time redisplay_internal is called. */
14132
14133 static void
14134 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14135 {
14136 struct buffer *b = XBUFFER (w->contents);
14137
14138 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14139 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14140 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14141
14142 if (accurate_p)
14143 {
14144 b->clip_changed = false;
14145 b->prevent_redisplay_optimizations_p = false;
14146 eassert (buffer_window_count (b) > 0);
14147 /* Resetting b->text->redisplay is problematic!
14148 In order to make it safer to do it here, redisplay_internal must
14149 have copied all b->text->redisplay to their respective windows. */
14150 b->text->redisplay = false;
14151
14152 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14153 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14154 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14155 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14156
14157 w->current_matrix->buffer = b;
14158 w->current_matrix->begv = BUF_BEGV (b);
14159 w->current_matrix->zv = BUF_ZV (b);
14160
14161 w->last_cursor_vpos = w->cursor.vpos;
14162 w->last_cursor_off_p = w->cursor_off_p;
14163
14164 if (w == XWINDOW (selected_window))
14165 w->last_point = BUF_PT (b);
14166 else
14167 w->last_point = marker_position (w->pointm);
14168
14169 w->window_end_valid = true;
14170 w->update_mode_line = false;
14171 }
14172
14173 w->redisplay = !accurate_p;
14174 }
14175
14176
14177 /* Mark the display of windows in the window tree rooted at WINDOW as
14178 accurate or inaccurate. If ACCURATE_P, mark display of
14179 windows as accurate. If !ACCURATE_P, arrange for windows to
14180 be redisplayed the next time redisplay_internal is called. */
14181
14182 void
14183 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14184 {
14185 struct window *w;
14186
14187 for (; !NILP (window); window = w->next)
14188 {
14189 w = XWINDOW (window);
14190 if (WINDOWP (w->contents))
14191 mark_window_display_accurate (w->contents, accurate_p);
14192 else
14193 mark_window_display_accurate_1 (w, accurate_p);
14194 }
14195
14196 if (accurate_p)
14197 update_overlay_arrows (1);
14198 else
14199 /* Force a thorough redisplay the next time by setting
14200 last_arrow_position and last_arrow_string to t, which is
14201 unequal to any useful value of Voverlay_arrow_... */
14202 update_overlay_arrows (-1);
14203 }
14204
14205
14206 /* Return value in display table DP (Lisp_Char_Table *) for character
14207 C. Since a display table doesn't have any parent, we don't have to
14208 follow parent. Do not call this function directly but use the
14209 macro DISP_CHAR_VECTOR. */
14210
14211 Lisp_Object
14212 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14213 {
14214 Lisp_Object val;
14215
14216 if (ASCII_CHAR_P (c))
14217 {
14218 val = dp->ascii;
14219 if (SUB_CHAR_TABLE_P (val))
14220 val = XSUB_CHAR_TABLE (val)->contents[c];
14221 }
14222 else
14223 {
14224 Lisp_Object table;
14225
14226 XSETCHAR_TABLE (table, dp);
14227 val = char_table_ref (table, c);
14228 }
14229 if (NILP (val))
14230 val = dp->defalt;
14231 return val;
14232 }
14233
14234
14235 \f
14236 /***********************************************************************
14237 Window Redisplay
14238 ***********************************************************************/
14239
14240 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14241
14242 static void
14243 redisplay_windows (Lisp_Object window)
14244 {
14245 while (!NILP (window))
14246 {
14247 struct window *w = XWINDOW (window);
14248
14249 if (WINDOWP (w->contents))
14250 redisplay_windows (w->contents);
14251 else if (BUFFERP (w->contents))
14252 {
14253 displayed_buffer = XBUFFER (w->contents);
14254 /* Use list_of_error, not Qerror, so that
14255 we catch only errors and don't run the debugger. */
14256 internal_condition_case_1 (redisplay_window_0, window,
14257 list_of_error,
14258 redisplay_window_error);
14259 }
14260
14261 window = w->next;
14262 }
14263 }
14264
14265 static Lisp_Object
14266 redisplay_window_error (Lisp_Object ignore)
14267 {
14268 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14269 return Qnil;
14270 }
14271
14272 static Lisp_Object
14273 redisplay_window_0 (Lisp_Object window)
14274 {
14275 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14276 redisplay_window (window, false);
14277 return Qnil;
14278 }
14279
14280 static Lisp_Object
14281 redisplay_window_1 (Lisp_Object window)
14282 {
14283 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14284 redisplay_window (window, true);
14285 return Qnil;
14286 }
14287 \f
14288
14289 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14290 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14291 which positions recorded in ROW differ from current buffer
14292 positions.
14293
14294 Return true iff cursor is on this row. */
14295
14296 static bool
14297 set_cursor_from_row (struct window *w, struct glyph_row *row,
14298 struct glyph_matrix *matrix,
14299 ptrdiff_t delta, ptrdiff_t delta_bytes,
14300 int dy, int dvpos)
14301 {
14302 struct glyph *glyph = row->glyphs[TEXT_AREA];
14303 struct glyph *end = glyph + row->used[TEXT_AREA];
14304 struct glyph *cursor = NULL;
14305 /* The last known character position in row. */
14306 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14307 int x = row->x;
14308 ptrdiff_t pt_old = PT - delta;
14309 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14310 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14311 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14312 /* A glyph beyond the edge of TEXT_AREA which we should never
14313 touch. */
14314 struct glyph *glyphs_end = end;
14315 /* True means we've found a match for cursor position, but that
14316 glyph has the avoid_cursor_p flag set. */
14317 bool match_with_avoid_cursor = false;
14318 /* True means we've seen at least one glyph that came from a
14319 display string. */
14320 bool string_seen = false;
14321 /* Largest and smallest buffer positions seen so far during scan of
14322 glyph row. */
14323 ptrdiff_t bpos_max = pos_before;
14324 ptrdiff_t bpos_min = pos_after;
14325 /* Last buffer position covered by an overlay string with an integer
14326 `cursor' property. */
14327 ptrdiff_t bpos_covered = 0;
14328 /* True means the display string on which to display the cursor
14329 comes from a text property, not from an overlay. */
14330 bool string_from_text_prop = false;
14331
14332 /* Don't even try doing anything if called for a mode-line or
14333 header-line row, since the rest of the code isn't prepared to
14334 deal with such calamities. */
14335 eassert (!row->mode_line_p);
14336 if (row->mode_line_p)
14337 return false;
14338
14339 /* Skip over glyphs not having an object at the start and the end of
14340 the row. These are special glyphs like truncation marks on
14341 terminal frames. */
14342 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14343 {
14344 if (!row->reversed_p)
14345 {
14346 while (glyph < end
14347 && NILP (glyph->object)
14348 && glyph->charpos < 0)
14349 {
14350 x += glyph->pixel_width;
14351 ++glyph;
14352 }
14353 while (end > glyph
14354 && NILP ((end - 1)->object)
14355 /* CHARPOS is zero for blanks and stretch glyphs
14356 inserted by extend_face_to_end_of_line. */
14357 && (end - 1)->charpos <= 0)
14358 --end;
14359 glyph_before = glyph - 1;
14360 glyph_after = end;
14361 }
14362 else
14363 {
14364 struct glyph *g;
14365
14366 /* If the glyph row is reversed, we need to process it from back
14367 to front, so swap the edge pointers. */
14368 glyphs_end = end = glyph - 1;
14369 glyph += row->used[TEXT_AREA] - 1;
14370
14371 while (glyph > end + 1
14372 && NILP (glyph->object)
14373 && glyph->charpos < 0)
14374 {
14375 --glyph;
14376 x -= glyph->pixel_width;
14377 }
14378 if (NILP (glyph->object) && glyph->charpos < 0)
14379 --glyph;
14380 /* By default, in reversed rows we put the cursor on the
14381 rightmost (first in the reading order) glyph. */
14382 for (g = end + 1; g < glyph; g++)
14383 x += g->pixel_width;
14384 while (end < glyph
14385 && NILP ((end + 1)->object)
14386 && (end + 1)->charpos <= 0)
14387 ++end;
14388 glyph_before = glyph + 1;
14389 glyph_after = end;
14390 }
14391 }
14392 else if (row->reversed_p)
14393 {
14394 /* In R2L rows that don't display text, put the cursor on the
14395 rightmost glyph. Case in point: an empty last line that is
14396 part of an R2L paragraph. */
14397 cursor = end - 1;
14398 /* Avoid placing the cursor on the last glyph of the row, where
14399 on terminal frames we hold the vertical border between
14400 adjacent windows. */
14401 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14402 && !WINDOW_RIGHTMOST_P (w)
14403 && cursor == row->glyphs[LAST_AREA] - 1)
14404 cursor--;
14405 x = -1; /* will be computed below, at label compute_x */
14406 }
14407
14408 /* Step 1: Try to find the glyph whose character position
14409 corresponds to point. If that's not possible, find 2 glyphs
14410 whose character positions are the closest to point, one before
14411 point, the other after it. */
14412 if (!row->reversed_p)
14413 while (/* not marched to end of glyph row */
14414 glyph < end
14415 /* glyph was not inserted by redisplay for internal purposes */
14416 && !NILP (glyph->object))
14417 {
14418 if (BUFFERP (glyph->object))
14419 {
14420 ptrdiff_t dpos = glyph->charpos - pt_old;
14421
14422 if (glyph->charpos > bpos_max)
14423 bpos_max = glyph->charpos;
14424 if (glyph->charpos < bpos_min)
14425 bpos_min = glyph->charpos;
14426 if (!glyph->avoid_cursor_p)
14427 {
14428 /* If we hit point, we've found the glyph on which to
14429 display the cursor. */
14430 if (dpos == 0)
14431 {
14432 match_with_avoid_cursor = false;
14433 break;
14434 }
14435 /* See if we've found a better approximation to
14436 POS_BEFORE or to POS_AFTER. */
14437 if (0 > dpos && dpos > pos_before - pt_old)
14438 {
14439 pos_before = glyph->charpos;
14440 glyph_before = glyph;
14441 }
14442 else if (0 < dpos && dpos < pos_after - pt_old)
14443 {
14444 pos_after = glyph->charpos;
14445 glyph_after = glyph;
14446 }
14447 }
14448 else if (dpos == 0)
14449 match_with_avoid_cursor = true;
14450 }
14451 else if (STRINGP (glyph->object))
14452 {
14453 Lisp_Object chprop;
14454 ptrdiff_t glyph_pos = glyph->charpos;
14455
14456 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14457 glyph->object);
14458 if (!NILP (chprop))
14459 {
14460 /* If the string came from a `display' text property,
14461 look up the buffer position of that property and
14462 use that position to update bpos_max, as if we
14463 actually saw such a position in one of the row's
14464 glyphs. This helps with supporting integer values
14465 of `cursor' property on the display string in
14466 situations where most or all of the row's buffer
14467 text is completely covered by display properties,
14468 so that no glyph with valid buffer positions is
14469 ever seen in the row. */
14470 ptrdiff_t prop_pos =
14471 string_buffer_position_lim (glyph->object, pos_before,
14472 pos_after, false);
14473
14474 if (prop_pos >= pos_before)
14475 bpos_max = prop_pos;
14476 }
14477 if (INTEGERP (chprop))
14478 {
14479 bpos_covered = bpos_max + XINT (chprop);
14480 /* If the `cursor' property covers buffer positions up
14481 to and including point, we should display cursor on
14482 this glyph. Note that, if a `cursor' property on one
14483 of the string's characters has an integer value, we
14484 will break out of the loop below _before_ we get to
14485 the position match above. IOW, integer values of
14486 the `cursor' property override the "exact match for
14487 point" strategy of positioning the cursor. */
14488 /* Implementation note: bpos_max == pt_old when, e.g.,
14489 we are in an empty line, where bpos_max is set to
14490 MATRIX_ROW_START_CHARPOS, see above. */
14491 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14492 {
14493 cursor = glyph;
14494 break;
14495 }
14496 }
14497
14498 string_seen = true;
14499 }
14500 x += glyph->pixel_width;
14501 ++glyph;
14502 }
14503 else if (glyph > end) /* row is reversed */
14504 while (!NILP (glyph->object))
14505 {
14506 if (BUFFERP (glyph->object))
14507 {
14508 ptrdiff_t dpos = glyph->charpos - pt_old;
14509
14510 if (glyph->charpos > bpos_max)
14511 bpos_max = glyph->charpos;
14512 if (glyph->charpos < bpos_min)
14513 bpos_min = glyph->charpos;
14514 if (!glyph->avoid_cursor_p)
14515 {
14516 if (dpos == 0)
14517 {
14518 match_with_avoid_cursor = false;
14519 break;
14520 }
14521 if (0 > dpos && dpos > pos_before - pt_old)
14522 {
14523 pos_before = glyph->charpos;
14524 glyph_before = glyph;
14525 }
14526 else if (0 < dpos && dpos < pos_after - pt_old)
14527 {
14528 pos_after = glyph->charpos;
14529 glyph_after = glyph;
14530 }
14531 }
14532 else if (dpos == 0)
14533 match_with_avoid_cursor = true;
14534 }
14535 else if (STRINGP (glyph->object))
14536 {
14537 Lisp_Object chprop;
14538 ptrdiff_t glyph_pos = glyph->charpos;
14539
14540 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14541 glyph->object);
14542 if (!NILP (chprop))
14543 {
14544 ptrdiff_t prop_pos =
14545 string_buffer_position_lim (glyph->object, pos_before,
14546 pos_after, false);
14547
14548 if (prop_pos >= pos_before)
14549 bpos_max = prop_pos;
14550 }
14551 if (INTEGERP (chprop))
14552 {
14553 bpos_covered = bpos_max + XINT (chprop);
14554 /* If the `cursor' property covers buffer positions up
14555 to and including point, we should display cursor on
14556 this glyph. */
14557 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14558 {
14559 cursor = glyph;
14560 break;
14561 }
14562 }
14563 string_seen = true;
14564 }
14565 --glyph;
14566 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14567 {
14568 x--; /* can't use any pixel_width */
14569 break;
14570 }
14571 x -= glyph->pixel_width;
14572 }
14573
14574 /* Step 2: If we didn't find an exact match for point, we need to
14575 look for a proper place to put the cursor among glyphs between
14576 GLYPH_BEFORE and GLYPH_AFTER. */
14577 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14578 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14579 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14580 {
14581 /* An empty line has a single glyph whose OBJECT is nil and
14582 whose CHARPOS is the position of a newline on that line.
14583 Note that on a TTY, there are more glyphs after that, which
14584 were produced by extend_face_to_end_of_line, but their
14585 CHARPOS is zero or negative. */
14586 bool empty_line_p =
14587 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14588 && NILP (glyph->object) && glyph->charpos > 0
14589 /* On a TTY, continued and truncated rows also have a glyph at
14590 their end whose OBJECT is nil and whose CHARPOS is
14591 positive (the continuation and truncation glyphs), but such
14592 rows are obviously not "empty". */
14593 && !(row->continued_p || row->truncated_on_right_p));
14594
14595 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14596 {
14597 ptrdiff_t ellipsis_pos;
14598
14599 /* Scan back over the ellipsis glyphs. */
14600 if (!row->reversed_p)
14601 {
14602 ellipsis_pos = (glyph - 1)->charpos;
14603 while (glyph > row->glyphs[TEXT_AREA]
14604 && (glyph - 1)->charpos == ellipsis_pos)
14605 glyph--, x -= glyph->pixel_width;
14606 /* That loop always goes one position too far, including
14607 the glyph before the ellipsis. So scan forward over
14608 that one. */
14609 x += glyph->pixel_width;
14610 glyph++;
14611 }
14612 else /* row is reversed */
14613 {
14614 ellipsis_pos = (glyph + 1)->charpos;
14615 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14616 && (glyph + 1)->charpos == ellipsis_pos)
14617 glyph++, x += glyph->pixel_width;
14618 x -= glyph->pixel_width;
14619 glyph--;
14620 }
14621 }
14622 else if (match_with_avoid_cursor)
14623 {
14624 cursor = glyph_after;
14625 x = -1;
14626 }
14627 else if (string_seen)
14628 {
14629 int incr = row->reversed_p ? -1 : +1;
14630
14631 /* Need to find the glyph that came out of a string which is
14632 present at point. That glyph is somewhere between
14633 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14634 positioned between POS_BEFORE and POS_AFTER in the
14635 buffer. */
14636 struct glyph *start, *stop;
14637 ptrdiff_t pos = pos_before;
14638
14639 x = -1;
14640
14641 /* If the row ends in a newline from a display string,
14642 reordering could have moved the glyphs belonging to the
14643 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14644 in this case we extend the search to the last glyph in
14645 the row that was not inserted by redisplay. */
14646 if (row->ends_in_newline_from_string_p)
14647 {
14648 glyph_after = end;
14649 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14650 }
14651
14652 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14653 correspond to POS_BEFORE and POS_AFTER, respectively. We
14654 need START and STOP in the order that corresponds to the
14655 row's direction as given by its reversed_p flag. If the
14656 directionality of characters between POS_BEFORE and
14657 POS_AFTER is the opposite of the row's base direction,
14658 these characters will have been reordered for display,
14659 and we need to reverse START and STOP. */
14660 if (!row->reversed_p)
14661 {
14662 start = min (glyph_before, glyph_after);
14663 stop = max (glyph_before, glyph_after);
14664 }
14665 else
14666 {
14667 start = max (glyph_before, glyph_after);
14668 stop = min (glyph_before, glyph_after);
14669 }
14670 for (glyph = start + incr;
14671 row->reversed_p ? glyph > stop : glyph < stop; )
14672 {
14673
14674 /* Any glyphs that come from the buffer are here because
14675 of bidi reordering. Skip them, and only pay
14676 attention to glyphs that came from some string. */
14677 if (STRINGP (glyph->object))
14678 {
14679 Lisp_Object str;
14680 ptrdiff_t tem;
14681 /* If the display property covers the newline, we
14682 need to search for it one position farther. */
14683 ptrdiff_t lim = pos_after
14684 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14685
14686 string_from_text_prop = false;
14687 str = glyph->object;
14688 tem = string_buffer_position_lim (str, pos, lim, false);
14689 if (tem == 0 /* from overlay */
14690 || pos <= tem)
14691 {
14692 /* If the string from which this glyph came is
14693 found in the buffer at point, or at position
14694 that is closer to point than pos_after, then
14695 we've found the glyph we've been looking for.
14696 If it comes from an overlay (tem == 0), and
14697 it has the `cursor' property on one of its
14698 glyphs, record that glyph as a candidate for
14699 displaying the cursor. (As in the
14700 unidirectional version, we will display the
14701 cursor on the last candidate we find.) */
14702 if (tem == 0
14703 || tem == pt_old
14704 || (tem - pt_old > 0 && tem < pos_after))
14705 {
14706 /* The glyphs from this string could have
14707 been reordered. Find the one with the
14708 smallest string position. Or there could
14709 be a character in the string with the
14710 `cursor' property, which means display
14711 cursor on that character's glyph. */
14712 ptrdiff_t strpos = glyph->charpos;
14713
14714 if (tem)
14715 {
14716 cursor = glyph;
14717 string_from_text_prop = true;
14718 }
14719 for ( ;
14720 (row->reversed_p ? glyph > stop : glyph < stop)
14721 && EQ (glyph->object, str);
14722 glyph += incr)
14723 {
14724 Lisp_Object cprop;
14725 ptrdiff_t gpos = glyph->charpos;
14726
14727 cprop = Fget_char_property (make_number (gpos),
14728 Qcursor,
14729 glyph->object);
14730 if (!NILP (cprop))
14731 {
14732 cursor = glyph;
14733 break;
14734 }
14735 if (tem && glyph->charpos < strpos)
14736 {
14737 strpos = glyph->charpos;
14738 cursor = glyph;
14739 }
14740 }
14741
14742 if (tem == pt_old
14743 || (tem - pt_old > 0 && tem < pos_after))
14744 goto compute_x;
14745 }
14746 if (tem)
14747 pos = tem + 1; /* don't find previous instances */
14748 }
14749 /* This string is not what we want; skip all of the
14750 glyphs that came from it. */
14751 while ((row->reversed_p ? glyph > stop : glyph < stop)
14752 && EQ (glyph->object, str))
14753 glyph += incr;
14754 }
14755 else
14756 glyph += incr;
14757 }
14758
14759 /* If we reached the end of the line, and END was from a string,
14760 the cursor is not on this line. */
14761 if (cursor == NULL
14762 && (row->reversed_p ? glyph <= end : glyph >= end)
14763 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14764 && STRINGP (end->object)
14765 && row->continued_p)
14766 return false;
14767 }
14768 /* A truncated row may not include PT among its character positions.
14769 Setting the cursor inside the scroll margin will trigger
14770 recalculation of hscroll in hscroll_window_tree. But if a
14771 display string covers point, defer to the string-handling
14772 code below to figure this out. */
14773 else if (row->truncated_on_left_p && pt_old < bpos_min)
14774 {
14775 cursor = glyph_before;
14776 x = -1;
14777 }
14778 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14779 /* Zero-width characters produce no glyphs. */
14780 || (!empty_line_p
14781 && (row->reversed_p
14782 ? glyph_after > glyphs_end
14783 : glyph_after < glyphs_end)))
14784 {
14785 cursor = glyph_after;
14786 x = -1;
14787 }
14788 }
14789
14790 compute_x:
14791 if (cursor != NULL)
14792 glyph = cursor;
14793 else if (glyph == glyphs_end
14794 && pos_before == pos_after
14795 && STRINGP ((row->reversed_p
14796 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14797 : row->glyphs[TEXT_AREA])->object))
14798 {
14799 /* If all the glyphs of this row came from strings, put the
14800 cursor on the first glyph of the row. This avoids having the
14801 cursor outside of the text area in this very rare and hard
14802 use case. */
14803 glyph =
14804 row->reversed_p
14805 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14806 : row->glyphs[TEXT_AREA];
14807 }
14808 if (x < 0)
14809 {
14810 struct glyph *g;
14811
14812 /* Need to compute x that corresponds to GLYPH. */
14813 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14814 {
14815 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14816 emacs_abort ();
14817 x += g->pixel_width;
14818 }
14819 }
14820
14821 /* ROW could be part of a continued line, which, under bidi
14822 reordering, might have other rows whose start and end charpos
14823 occlude point. Only set w->cursor if we found a better
14824 approximation to the cursor position than we have from previously
14825 examined candidate rows belonging to the same continued line. */
14826 if (/* We already have a candidate row. */
14827 w->cursor.vpos >= 0
14828 /* That candidate is not the row we are processing. */
14829 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14830 /* Make sure cursor.vpos specifies a row whose start and end
14831 charpos occlude point, and it is valid candidate for being a
14832 cursor-row. This is because some callers of this function
14833 leave cursor.vpos at the row where the cursor was displayed
14834 during the last redisplay cycle. */
14835 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14836 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14837 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14838 {
14839 struct glyph *g1
14840 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14841
14842 /* Don't consider glyphs that are outside TEXT_AREA. */
14843 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14844 return false;
14845 /* Keep the candidate whose buffer position is the closest to
14846 point or has the `cursor' property. */
14847 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14848 w->cursor.hpos >= 0
14849 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14850 && ((BUFFERP (g1->object)
14851 && (g1->charpos == pt_old /* An exact match always wins. */
14852 || (BUFFERP (glyph->object)
14853 && eabs (g1->charpos - pt_old)
14854 < eabs (glyph->charpos - pt_old))))
14855 /* Previous candidate is a glyph from a string that has
14856 a non-nil `cursor' property. */
14857 || (STRINGP (g1->object)
14858 && (!NILP (Fget_char_property (make_number (g1->charpos),
14859 Qcursor, g1->object))
14860 /* Previous candidate is from the same display
14861 string as this one, and the display string
14862 came from a text property. */
14863 || (EQ (g1->object, glyph->object)
14864 && string_from_text_prop)
14865 /* this candidate is from newline and its
14866 position is not an exact match */
14867 || (NILP (glyph->object)
14868 && glyph->charpos != pt_old)))))
14869 return false;
14870 /* If this candidate gives an exact match, use that. */
14871 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14872 /* If this candidate is a glyph created for the
14873 terminating newline of a line, and point is on that
14874 newline, it wins because it's an exact match. */
14875 || (!row->continued_p
14876 && NILP (glyph->object)
14877 && glyph->charpos == 0
14878 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14879 /* Otherwise, keep the candidate that comes from a row
14880 spanning less buffer positions. This may win when one or
14881 both candidate positions are on glyphs that came from
14882 display strings, for which we cannot compare buffer
14883 positions. */
14884 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14885 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14886 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14887 return false;
14888 }
14889 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14890 w->cursor.x = x;
14891 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14892 w->cursor.y = row->y + dy;
14893
14894 if (w == XWINDOW (selected_window))
14895 {
14896 if (!row->continued_p
14897 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14898 && row->x == 0)
14899 {
14900 this_line_buffer = XBUFFER (w->contents);
14901
14902 CHARPOS (this_line_start_pos)
14903 = MATRIX_ROW_START_CHARPOS (row) + delta;
14904 BYTEPOS (this_line_start_pos)
14905 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14906
14907 CHARPOS (this_line_end_pos)
14908 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14909 BYTEPOS (this_line_end_pos)
14910 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14911
14912 this_line_y = w->cursor.y;
14913 this_line_pixel_height = row->height;
14914 this_line_vpos = w->cursor.vpos;
14915 this_line_start_x = row->x;
14916 }
14917 else
14918 CHARPOS (this_line_start_pos) = 0;
14919 }
14920
14921 return true;
14922 }
14923
14924
14925 /* Run window scroll functions, if any, for WINDOW with new window
14926 start STARTP. Sets the window start of WINDOW to that position.
14927
14928 We assume that the window's buffer is really current. */
14929
14930 static struct text_pos
14931 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14932 {
14933 struct window *w = XWINDOW (window);
14934 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14935
14936 eassert (current_buffer == XBUFFER (w->contents));
14937
14938 if (!NILP (Vwindow_scroll_functions))
14939 {
14940 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14941 make_number (CHARPOS (startp)));
14942 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14943 /* In case the hook functions switch buffers. */
14944 set_buffer_internal (XBUFFER (w->contents));
14945 }
14946
14947 return startp;
14948 }
14949
14950
14951 /* Make sure the line containing the cursor is fully visible.
14952 A value of true means there is nothing to be done.
14953 (Either the line is fully visible, or it cannot be made so,
14954 or we cannot tell.)
14955
14956 If FORCE_P, return false even if partial visible cursor row
14957 is higher than window.
14958
14959 If CURRENT_MATRIX_P, use the information from the
14960 window's current glyph matrix; otherwise use the desired glyph
14961 matrix.
14962
14963 A value of false means the caller should do scrolling
14964 as if point had gone off the screen. */
14965
14966 static bool
14967 cursor_row_fully_visible_p (struct window *w, bool force_p,
14968 bool current_matrix_p)
14969 {
14970 struct glyph_matrix *matrix;
14971 struct glyph_row *row;
14972 int window_height;
14973
14974 if (!make_cursor_line_fully_visible_p)
14975 return true;
14976
14977 /* It's not always possible to find the cursor, e.g, when a window
14978 is full of overlay strings. Don't do anything in that case. */
14979 if (w->cursor.vpos < 0)
14980 return true;
14981
14982 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14983 row = MATRIX_ROW (matrix, w->cursor.vpos);
14984
14985 /* If the cursor row is not partially visible, there's nothing to do. */
14986 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14987 return true;
14988
14989 /* If the row the cursor is in is taller than the window's height,
14990 it's not clear what to do, so do nothing. */
14991 window_height = window_box_height (w);
14992 if (row->height >= window_height)
14993 {
14994 if (!force_p || MINI_WINDOW_P (w)
14995 || w->vscroll || w->cursor.vpos == 0)
14996 return true;
14997 }
14998 return false;
14999 }
15000
15001
15002 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15003 means only WINDOW is redisplayed in redisplay_internal.
15004 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15005 in redisplay_window to bring a partially visible line into view in
15006 the case that only the cursor has moved.
15007
15008 LAST_LINE_MISFIT should be true if we're scrolling because the
15009 last screen line's vertical height extends past the end of the screen.
15010
15011 Value is
15012
15013 1 if scrolling succeeded
15014
15015 0 if scrolling didn't find point.
15016
15017 -1 if new fonts have been loaded so that we must interrupt
15018 redisplay, adjust glyph matrices, and try again. */
15019
15020 enum
15021 {
15022 SCROLLING_SUCCESS,
15023 SCROLLING_FAILED,
15024 SCROLLING_NEED_LARGER_MATRICES
15025 };
15026
15027 /* If scroll-conservatively is more than this, never recenter.
15028
15029 If you change this, don't forget to update the doc string of
15030 `scroll-conservatively' and the Emacs manual. */
15031 #define SCROLL_LIMIT 100
15032
15033 static int
15034 try_scrolling (Lisp_Object window, bool just_this_one_p,
15035 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15036 bool temp_scroll_step, bool last_line_misfit)
15037 {
15038 struct window *w = XWINDOW (window);
15039 struct frame *f = XFRAME (w->frame);
15040 struct text_pos pos, startp;
15041 struct it it;
15042 int this_scroll_margin, scroll_max, rc, height;
15043 int dy = 0, amount_to_scroll = 0;
15044 bool scroll_down_p = false;
15045 int extra_scroll_margin_lines = last_line_misfit;
15046 Lisp_Object aggressive;
15047 /* We will never try scrolling more than this number of lines. */
15048 int scroll_limit = SCROLL_LIMIT;
15049 int frame_line_height = default_line_pixel_height (w);
15050 int window_total_lines
15051 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15052
15053 #ifdef GLYPH_DEBUG
15054 debug_method_add (w, "try_scrolling");
15055 #endif
15056
15057 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15058
15059 /* Compute scroll margin height in pixels. We scroll when point is
15060 within this distance from the top or bottom of the window. */
15061 if (scroll_margin > 0)
15062 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15063 * frame_line_height;
15064 else
15065 this_scroll_margin = 0;
15066
15067 /* Force arg_scroll_conservatively to have a reasonable value, to
15068 avoid scrolling too far away with slow move_it_* functions. Note
15069 that the user can supply scroll-conservatively equal to
15070 `most-positive-fixnum', which can be larger than INT_MAX. */
15071 if (arg_scroll_conservatively > scroll_limit)
15072 {
15073 arg_scroll_conservatively = scroll_limit + 1;
15074 scroll_max = scroll_limit * frame_line_height;
15075 }
15076 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15077 /* Compute how much we should try to scroll maximally to bring
15078 point into view. */
15079 scroll_max = (max (scroll_step,
15080 max (arg_scroll_conservatively, temp_scroll_step))
15081 * frame_line_height);
15082 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15083 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15084 /* We're trying to scroll because of aggressive scrolling but no
15085 scroll_step is set. Choose an arbitrary one. */
15086 scroll_max = 10 * frame_line_height;
15087 else
15088 scroll_max = 0;
15089
15090 too_near_end:
15091
15092 /* Decide whether to scroll down. */
15093 if (PT > CHARPOS (startp))
15094 {
15095 int scroll_margin_y;
15096
15097 /* Compute the pixel ypos of the scroll margin, then move IT to
15098 either that ypos or PT, whichever comes first. */
15099 start_display (&it, w, startp);
15100 scroll_margin_y = it.last_visible_y - this_scroll_margin
15101 - frame_line_height * extra_scroll_margin_lines;
15102 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15103 (MOVE_TO_POS | MOVE_TO_Y));
15104
15105 if (PT > CHARPOS (it.current.pos))
15106 {
15107 int y0 = line_bottom_y (&it);
15108 /* Compute how many pixels below window bottom to stop searching
15109 for PT. This avoids costly search for PT that is far away if
15110 the user limited scrolling by a small number of lines, but
15111 always finds PT if scroll_conservatively is set to a large
15112 number, such as most-positive-fixnum. */
15113 int slack = max (scroll_max, 10 * frame_line_height);
15114 int y_to_move = it.last_visible_y + slack;
15115
15116 /* Compute the distance from the scroll margin to PT or to
15117 the scroll limit, whichever comes first. This should
15118 include the height of the cursor line, to make that line
15119 fully visible. */
15120 move_it_to (&it, PT, -1, y_to_move,
15121 -1, MOVE_TO_POS | MOVE_TO_Y);
15122 dy = line_bottom_y (&it) - y0;
15123
15124 if (dy > scroll_max)
15125 return SCROLLING_FAILED;
15126
15127 if (dy > 0)
15128 scroll_down_p = true;
15129 }
15130 }
15131
15132 if (scroll_down_p)
15133 {
15134 /* Point is in or below the bottom scroll margin, so move the
15135 window start down. If scrolling conservatively, move it just
15136 enough down to make point visible. If scroll_step is set,
15137 move it down by scroll_step. */
15138 if (arg_scroll_conservatively)
15139 amount_to_scroll
15140 = min (max (dy, frame_line_height),
15141 frame_line_height * arg_scroll_conservatively);
15142 else if (scroll_step || temp_scroll_step)
15143 amount_to_scroll = scroll_max;
15144 else
15145 {
15146 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15147 height = WINDOW_BOX_TEXT_HEIGHT (w);
15148 if (NUMBERP (aggressive))
15149 {
15150 double float_amount = XFLOATINT (aggressive) * height;
15151 int aggressive_scroll = float_amount;
15152 if (aggressive_scroll == 0 && float_amount > 0)
15153 aggressive_scroll = 1;
15154 /* Don't let point enter the scroll margin near top of
15155 the window. This could happen if the value of
15156 scroll_up_aggressively is too large and there are
15157 non-zero margins, because scroll_up_aggressively
15158 means put point that fraction of window height
15159 _from_the_bottom_margin_. */
15160 if (aggressive_scroll + 2 * this_scroll_margin > height)
15161 aggressive_scroll = height - 2 * this_scroll_margin;
15162 amount_to_scroll = dy + aggressive_scroll;
15163 }
15164 }
15165
15166 if (amount_to_scroll <= 0)
15167 return SCROLLING_FAILED;
15168
15169 start_display (&it, w, startp);
15170 if (arg_scroll_conservatively <= scroll_limit)
15171 move_it_vertically (&it, amount_to_scroll);
15172 else
15173 {
15174 /* Extra precision for users who set scroll-conservatively
15175 to a large number: make sure the amount we scroll
15176 the window start is never less than amount_to_scroll,
15177 which was computed as distance from window bottom to
15178 point. This matters when lines at window top and lines
15179 below window bottom have different height. */
15180 struct it it1;
15181 void *it1data = NULL;
15182 /* We use a temporary it1 because line_bottom_y can modify
15183 its argument, if it moves one line down; see there. */
15184 int start_y;
15185
15186 SAVE_IT (it1, it, it1data);
15187 start_y = line_bottom_y (&it1);
15188 do {
15189 RESTORE_IT (&it, &it, it1data);
15190 move_it_by_lines (&it, 1);
15191 SAVE_IT (it1, it, it1data);
15192 } while (IT_CHARPOS (it) < ZV
15193 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15194 bidi_unshelve_cache (it1data, true);
15195 }
15196
15197 /* If STARTP is unchanged, move it down another screen line. */
15198 if (IT_CHARPOS (it) == CHARPOS (startp))
15199 move_it_by_lines (&it, 1);
15200 startp = it.current.pos;
15201 }
15202 else
15203 {
15204 struct text_pos scroll_margin_pos = startp;
15205 int y_offset = 0;
15206
15207 /* See if point is inside the scroll margin at the top of the
15208 window. */
15209 if (this_scroll_margin)
15210 {
15211 int y_start;
15212
15213 start_display (&it, w, startp);
15214 y_start = it.current_y;
15215 move_it_vertically (&it, this_scroll_margin);
15216 scroll_margin_pos = it.current.pos;
15217 /* If we didn't move enough before hitting ZV, request
15218 additional amount of scroll, to move point out of the
15219 scroll margin. */
15220 if (IT_CHARPOS (it) == ZV
15221 && it.current_y - y_start < this_scroll_margin)
15222 y_offset = this_scroll_margin - (it.current_y - y_start);
15223 }
15224
15225 if (PT < CHARPOS (scroll_margin_pos))
15226 {
15227 /* Point is in the scroll margin at the top of the window or
15228 above what is displayed in the window. */
15229 int y0, y_to_move;
15230
15231 /* Compute the vertical distance from PT to the scroll
15232 margin position. Move as far as scroll_max allows, or
15233 one screenful, or 10 screen lines, whichever is largest.
15234 Give up if distance is greater than scroll_max or if we
15235 didn't reach the scroll margin position. */
15236 SET_TEXT_POS (pos, PT, PT_BYTE);
15237 start_display (&it, w, pos);
15238 y0 = it.current_y;
15239 y_to_move = max (it.last_visible_y,
15240 max (scroll_max, 10 * frame_line_height));
15241 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15242 y_to_move, -1,
15243 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15244 dy = it.current_y - y0;
15245 if (dy > scroll_max
15246 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15247 return SCROLLING_FAILED;
15248
15249 /* Additional scroll for when ZV was too close to point. */
15250 dy += y_offset;
15251
15252 /* Compute new window start. */
15253 start_display (&it, w, startp);
15254
15255 if (arg_scroll_conservatively)
15256 amount_to_scroll = max (dy, frame_line_height
15257 * max (scroll_step, temp_scroll_step));
15258 else if (scroll_step || temp_scroll_step)
15259 amount_to_scroll = scroll_max;
15260 else
15261 {
15262 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15263 height = WINDOW_BOX_TEXT_HEIGHT (w);
15264 if (NUMBERP (aggressive))
15265 {
15266 double float_amount = XFLOATINT (aggressive) * height;
15267 int aggressive_scroll = float_amount;
15268 if (aggressive_scroll == 0 && float_amount > 0)
15269 aggressive_scroll = 1;
15270 /* Don't let point enter the scroll margin near
15271 bottom of the window, if the value of
15272 scroll_down_aggressively happens to be too
15273 large. */
15274 if (aggressive_scroll + 2 * this_scroll_margin > height)
15275 aggressive_scroll = height - 2 * this_scroll_margin;
15276 amount_to_scroll = dy + aggressive_scroll;
15277 }
15278 }
15279
15280 if (amount_to_scroll <= 0)
15281 return SCROLLING_FAILED;
15282
15283 move_it_vertically_backward (&it, amount_to_scroll);
15284 startp = it.current.pos;
15285 }
15286 }
15287
15288 /* Run window scroll functions. */
15289 startp = run_window_scroll_functions (window, startp);
15290
15291 /* Display the window. Give up if new fonts are loaded, or if point
15292 doesn't appear. */
15293 if (!try_window (window, startp, 0))
15294 rc = SCROLLING_NEED_LARGER_MATRICES;
15295 else if (w->cursor.vpos < 0)
15296 {
15297 clear_glyph_matrix (w->desired_matrix);
15298 rc = SCROLLING_FAILED;
15299 }
15300 else
15301 {
15302 /* Maybe forget recorded base line for line number display. */
15303 if (!just_this_one_p
15304 || current_buffer->clip_changed
15305 || BEG_UNCHANGED < CHARPOS (startp))
15306 w->base_line_number = 0;
15307
15308 /* If cursor ends up on a partially visible line,
15309 treat that as being off the bottom of the screen. */
15310 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15311 false)
15312 /* It's possible that the cursor is on the first line of the
15313 buffer, which is partially obscured due to a vscroll
15314 (Bug#7537). In that case, avoid looping forever. */
15315 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15316 {
15317 clear_glyph_matrix (w->desired_matrix);
15318 ++extra_scroll_margin_lines;
15319 goto too_near_end;
15320 }
15321 rc = SCROLLING_SUCCESS;
15322 }
15323
15324 return rc;
15325 }
15326
15327
15328 /* Compute a suitable window start for window W if display of W starts
15329 on a continuation line. Value is true if a new window start
15330 was computed.
15331
15332 The new window start will be computed, based on W's width, starting
15333 from the start of the continued line. It is the start of the
15334 screen line with the minimum distance from the old start W->start. */
15335
15336 static bool
15337 compute_window_start_on_continuation_line (struct window *w)
15338 {
15339 struct text_pos pos, start_pos;
15340 bool window_start_changed_p = false;
15341
15342 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15343
15344 /* If window start is on a continuation line... Window start may be
15345 < BEGV in case there's invisible text at the start of the
15346 buffer (M-x rmail, for example). */
15347 if (CHARPOS (start_pos) > BEGV
15348 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15349 {
15350 struct it it;
15351 struct glyph_row *row;
15352
15353 /* Handle the case that the window start is out of range. */
15354 if (CHARPOS (start_pos) < BEGV)
15355 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15356 else if (CHARPOS (start_pos) > ZV)
15357 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15358
15359 /* Find the start of the continued line. This should be fast
15360 because find_newline is fast (newline cache). */
15361 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15362 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15363 row, DEFAULT_FACE_ID);
15364 reseat_at_previous_visible_line_start (&it);
15365
15366 /* If the line start is "too far" away from the window start,
15367 say it takes too much time to compute a new window start. */
15368 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15369 /* PXW: Do we need upper bounds here? */
15370 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15371 {
15372 int min_distance, distance;
15373
15374 /* Move forward by display lines to find the new window
15375 start. If window width was enlarged, the new start can
15376 be expected to be > the old start. If window width was
15377 decreased, the new window start will be < the old start.
15378 So, we're looking for the display line start with the
15379 minimum distance from the old window start. */
15380 pos = it.current.pos;
15381 min_distance = INFINITY;
15382 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15383 distance < min_distance)
15384 {
15385 min_distance = distance;
15386 pos = it.current.pos;
15387 if (it.line_wrap == WORD_WRAP)
15388 {
15389 /* Under WORD_WRAP, move_it_by_lines is likely to
15390 overshoot and stop not at the first, but the
15391 second character from the left margin. So in
15392 that case, we need a more tight control on the X
15393 coordinate of the iterator than move_it_by_lines
15394 promises in its contract. The method is to first
15395 go to the last (rightmost) visible character of a
15396 line, then move to the leftmost character on the
15397 next line in a separate call. */
15398 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15399 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15400 move_it_to (&it, ZV, 0,
15401 it.current_y + it.max_ascent + it.max_descent, -1,
15402 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15403 }
15404 else
15405 move_it_by_lines (&it, 1);
15406 }
15407
15408 /* Set the window start there. */
15409 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15410 window_start_changed_p = true;
15411 }
15412 }
15413
15414 return window_start_changed_p;
15415 }
15416
15417
15418 /* Try cursor movement in case text has not changed in window WINDOW,
15419 with window start STARTP. Value is
15420
15421 CURSOR_MOVEMENT_SUCCESS if successful
15422
15423 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15424
15425 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15426 display. *SCROLL_STEP is set to true, under certain circumstances, if
15427 we want to scroll as if scroll-step were set to 1. See the code.
15428
15429 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15430 which case we have to abort this redisplay, and adjust matrices
15431 first. */
15432
15433 enum
15434 {
15435 CURSOR_MOVEMENT_SUCCESS,
15436 CURSOR_MOVEMENT_CANNOT_BE_USED,
15437 CURSOR_MOVEMENT_MUST_SCROLL,
15438 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15439 };
15440
15441 static int
15442 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15443 bool *scroll_step)
15444 {
15445 struct window *w = XWINDOW (window);
15446 struct frame *f = XFRAME (w->frame);
15447 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15448
15449 #ifdef GLYPH_DEBUG
15450 if (inhibit_try_cursor_movement)
15451 return rc;
15452 #endif
15453
15454 /* Previously, there was a check for Lisp integer in the
15455 if-statement below. Now, this field is converted to
15456 ptrdiff_t, thus zero means invalid position in a buffer. */
15457 eassert (w->last_point > 0);
15458 /* Likewise there was a check whether window_end_vpos is nil or larger
15459 than the window. Now window_end_vpos is int and so never nil, but
15460 let's leave eassert to check whether it fits in the window. */
15461 eassert (!w->window_end_valid
15462 || w->window_end_vpos < w->current_matrix->nrows);
15463
15464 /* Handle case where text has not changed, only point, and it has
15465 not moved off the frame. */
15466 if (/* Point may be in this window. */
15467 PT >= CHARPOS (startp)
15468 /* Selective display hasn't changed. */
15469 && !current_buffer->clip_changed
15470 /* Function force-mode-line-update is used to force a thorough
15471 redisplay. It sets either windows_or_buffers_changed or
15472 update_mode_lines. So don't take a shortcut here for these
15473 cases. */
15474 && !update_mode_lines
15475 && !windows_or_buffers_changed
15476 && !f->cursor_type_changed
15477 && NILP (Vshow_trailing_whitespace)
15478 /* This code is not used for mini-buffer for the sake of the case
15479 of redisplaying to replace an echo area message; since in
15480 that case the mini-buffer contents per se are usually
15481 unchanged. This code is of no real use in the mini-buffer
15482 since the handling of this_line_start_pos, etc., in redisplay
15483 handles the same cases. */
15484 && !EQ (window, minibuf_window)
15485 && (FRAME_WINDOW_P (f)
15486 || !overlay_arrow_in_current_buffer_p ()))
15487 {
15488 int this_scroll_margin, top_scroll_margin;
15489 struct glyph_row *row = NULL;
15490 int frame_line_height = default_line_pixel_height (w);
15491 int window_total_lines
15492 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15493
15494 #ifdef GLYPH_DEBUG
15495 debug_method_add (w, "cursor movement");
15496 #endif
15497
15498 /* Scroll if point within this distance from the top or bottom
15499 of the window. This is a pixel value. */
15500 if (scroll_margin > 0)
15501 {
15502 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15503 this_scroll_margin *= frame_line_height;
15504 }
15505 else
15506 this_scroll_margin = 0;
15507
15508 top_scroll_margin = this_scroll_margin;
15509 if (WINDOW_WANTS_HEADER_LINE_P (w))
15510 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15511
15512 /* Start with the row the cursor was displayed during the last
15513 not paused redisplay. Give up if that row is not valid. */
15514 if (w->last_cursor_vpos < 0
15515 || w->last_cursor_vpos >= w->current_matrix->nrows)
15516 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15517 else
15518 {
15519 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15520 if (row->mode_line_p)
15521 ++row;
15522 if (!row->enabled_p)
15523 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15524 }
15525
15526 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15527 {
15528 bool scroll_p = false, must_scroll = false;
15529 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15530
15531 if (PT > w->last_point)
15532 {
15533 /* Point has moved forward. */
15534 while (MATRIX_ROW_END_CHARPOS (row) < PT
15535 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15536 {
15537 eassert (row->enabled_p);
15538 ++row;
15539 }
15540
15541 /* If the end position of a row equals the start
15542 position of the next row, and PT is at that position,
15543 we would rather display cursor in the next line. */
15544 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15545 && MATRIX_ROW_END_CHARPOS (row) == PT
15546 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15547 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15548 && !cursor_row_p (row))
15549 ++row;
15550
15551 /* If within the scroll margin, scroll. Note that
15552 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15553 the next line would be drawn, and that
15554 this_scroll_margin can be zero. */
15555 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15556 || PT > MATRIX_ROW_END_CHARPOS (row)
15557 /* Line is completely visible last line in window
15558 and PT is to be set in the next line. */
15559 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15560 && PT == MATRIX_ROW_END_CHARPOS (row)
15561 && !row->ends_at_zv_p
15562 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15563 scroll_p = true;
15564 }
15565 else if (PT < w->last_point)
15566 {
15567 /* Cursor has to be moved backward. Note that PT >=
15568 CHARPOS (startp) because of the outer if-statement. */
15569 while (!row->mode_line_p
15570 && (MATRIX_ROW_START_CHARPOS (row) > PT
15571 || (MATRIX_ROW_START_CHARPOS (row) == PT
15572 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15573 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15574 row > w->current_matrix->rows
15575 && (row-1)->ends_in_newline_from_string_p))))
15576 && (row->y > top_scroll_margin
15577 || CHARPOS (startp) == BEGV))
15578 {
15579 eassert (row->enabled_p);
15580 --row;
15581 }
15582
15583 /* Consider the following case: Window starts at BEGV,
15584 there is invisible, intangible text at BEGV, so that
15585 display starts at some point START > BEGV. It can
15586 happen that we are called with PT somewhere between
15587 BEGV and START. Try to handle that case. */
15588 if (row < w->current_matrix->rows
15589 || row->mode_line_p)
15590 {
15591 row = w->current_matrix->rows;
15592 if (row->mode_line_p)
15593 ++row;
15594 }
15595
15596 /* Due to newlines in overlay strings, we may have to
15597 skip forward over overlay strings. */
15598 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15599 && MATRIX_ROW_END_CHARPOS (row) == PT
15600 && !cursor_row_p (row))
15601 ++row;
15602
15603 /* If within the scroll margin, scroll. */
15604 if (row->y < top_scroll_margin
15605 && CHARPOS (startp) != BEGV)
15606 scroll_p = true;
15607 }
15608 else
15609 {
15610 /* Cursor did not move. So don't scroll even if cursor line
15611 is partially visible, as it was so before. */
15612 rc = CURSOR_MOVEMENT_SUCCESS;
15613 }
15614
15615 if (PT < MATRIX_ROW_START_CHARPOS (row)
15616 || PT > MATRIX_ROW_END_CHARPOS (row))
15617 {
15618 /* if PT is not in the glyph row, give up. */
15619 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15620 must_scroll = true;
15621 }
15622 else if (rc != CURSOR_MOVEMENT_SUCCESS
15623 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15624 {
15625 struct glyph_row *row1;
15626
15627 /* If rows are bidi-reordered and point moved, back up
15628 until we find a row that does not belong to a
15629 continuation line. This is because we must consider
15630 all rows of a continued line as candidates for the
15631 new cursor positioning, since row start and end
15632 positions change non-linearly with vertical position
15633 in such rows. */
15634 /* FIXME: Revisit this when glyph ``spilling'' in
15635 continuation lines' rows is implemented for
15636 bidi-reordered rows. */
15637 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15638 MATRIX_ROW_CONTINUATION_LINE_P (row);
15639 --row)
15640 {
15641 /* If we hit the beginning of the displayed portion
15642 without finding the first row of a continued
15643 line, give up. */
15644 if (row <= row1)
15645 {
15646 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15647 break;
15648 }
15649 eassert (row->enabled_p);
15650 }
15651 }
15652 if (must_scroll)
15653 ;
15654 else if (rc != CURSOR_MOVEMENT_SUCCESS
15655 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15656 /* Make sure this isn't a header line by any chance, since
15657 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
15658 && !row->mode_line_p
15659 && make_cursor_line_fully_visible_p)
15660 {
15661 if (PT == MATRIX_ROW_END_CHARPOS (row)
15662 && !row->ends_at_zv_p
15663 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15664 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15665 else if (row->height > window_box_height (w))
15666 {
15667 /* If we end up in a partially visible line, let's
15668 make it fully visible, except when it's taller
15669 than the window, in which case we can't do much
15670 about it. */
15671 *scroll_step = true;
15672 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15673 }
15674 else
15675 {
15676 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15677 if (!cursor_row_fully_visible_p (w, false, true))
15678 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15679 else
15680 rc = CURSOR_MOVEMENT_SUCCESS;
15681 }
15682 }
15683 else if (scroll_p)
15684 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15685 else if (rc != CURSOR_MOVEMENT_SUCCESS
15686 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15687 {
15688 /* With bidi-reordered rows, there could be more than
15689 one candidate row whose start and end positions
15690 occlude point. We need to let set_cursor_from_row
15691 find the best candidate. */
15692 /* FIXME: Revisit this when glyph ``spilling'' in
15693 continuation lines' rows is implemented for
15694 bidi-reordered rows. */
15695 bool rv = false;
15696
15697 do
15698 {
15699 bool at_zv_p = false, exact_match_p = false;
15700
15701 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15702 && PT <= MATRIX_ROW_END_CHARPOS (row)
15703 && cursor_row_p (row))
15704 rv |= set_cursor_from_row (w, row, w->current_matrix,
15705 0, 0, 0, 0);
15706 /* As soon as we've found the exact match for point,
15707 or the first suitable row whose ends_at_zv_p flag
15708 is set, we are done. */
15709 if (rv)
15710 {
15711 at_zv_p = MATRIX_ROW (w->current_matrix,
15712 w->cursor.vpos)->ends_at_zv_p;
15713 if (!at_zv_p
15714 && w->cursor.hpos >= 0
15715 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15716 w->cursor.vpos))
15717 {
15718 struct glyph_row *candidate =
15719 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15720 struct glyph *g =
15721 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15722 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15723
15724 exact_match_p =
15725 (BUFFERP (g->object) && g->charpos == PT)
15726 || (NILP (g->object)
15727 && (g->charpos == PT
15728 || (g->charpos == 0 && endpos - 1 == PT)));
15729 }
15730 if (at_zv_p || exact_match_p)
15731 {
15732 rc = CURSOR_MOVEMENT_SUCCESS;
15733 break;
15734 }
15735 }
15736 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15737 break;
15738 ++row;
15739 }
15740 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15741 || row->continued_p)
15742 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15743 || (MATRIX_ROW_START_CHARPOS (row) == PT
15744 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15745 /* If we didn't find any candidate rows, or exited the
15746 loop before all the candidates were examined, signal
15747 to the caller that this method failed. */
15748 if (rc != CURSOR_MOVEMENT_SUCCESS
15749 && !(rv
15750 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15751 && !row->continued_p))
15752 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15753 else if (rv)
15754 rc = CURSOR_MOVEMENT_SUCCESS;
15755 }
15756 else
15757 {
15758 do
15759 {
15760 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15761 {
15762 rc = CURSOR_MOVEMENT_SUCCESS;
15763 break;
15764 }
15765 ++row;
15766 }
15767 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15768 && MATRIX_ROW_START_CHARPOS (row) == PT
15769 && cursor_row_p (row));
15770 }
15771 }
15772 }
15773
15774 return rc;
15775 }
15776
15777
15778 void
15779 set_vertical_scroll_bar (struct window *w)
15780 {
15781 ptrdiff_t start, end, whole;
15782
15783 /* Calculate the start and end positions for the current window.
15784 At some point, it would be nice to choose between scrollbars
15785 which reflect the whole buffer size, with special markers
15786 indicating narrowing, and scrollbars which reflect only the
15787 visible region.
15788
15789 Note that mini-buffers sometimes aren't displaying any text. */
15790 if (!MINI_WINDOW_P (w)
15791 || (w == XWINDOW (minibuf_window)
15792 && NILP (echo_area_buffer[0])))
15793 {
15794 struct buffer *buf = XBUFFER (w->contents);
15795 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15796 start = marker_position (w->start) - BUF_BEGV (buf);
15797 /* I don't think this is guaranteed to be right. For the
15798 moment, we'll pretend it is. */
15799 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15800
15801 if (end < start)
15802 end = start;
15803 if (whole < (end - start))
15804 whole = end - start;
15805 }
15806 else
15807 start = end = whole = 0;
15808
15809 /* Indicate what this scroll bar ought to be displaying now. */
15810 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15811 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15812 (w, end - start, whole, start);
15813 }
15814
15815
15816 void
15817 set_horizontal_scroll_bar (struct window *w)
15818 {
15819 int start, end, whole, portion;
15820
15821 if (!MINI_WINDOW_P (w)
15822 || (w == XWINDOW (minibuf_window)
15823 && NILP (echo_area_buffer[0])))
15824 {
15825 struct buffer *b = XBUFFER (w->contents);
15826 struct buffer *old_buffer = NULL;
15827 struct it it;
15828 struct text_pos startp;
15829
15830 if (b != current_buffer)
15831 {
15832 old_buffer = current_buffer;
15833 set_buffer_internal (b);
15834 }
15835
15836 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15837 start_display (&it, w, startp);
15838 it.last_visible_x = INT_MAX;
15839 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15840 MOVE_TO_X | MOVE_TO_Y);
15841 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15842 window_box_height (w), -1,
15843 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15844
15845 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15846 end = start + window_box_width (w, TEXT_AREA);
15847 portion = end - start;
15848 /* After enlarging a horizontally scrolled window such that it
15849 gets at least as wide as the text it contains, make sure that
15850 the thumb doesn't fill the entire scroll bar so we can still
15851 drag it back to see the entire text. */
15852 whole = max (whole, end);
15853
15854 if (it.bidi_p)
15855 {
15856 Lisp_Object pdir;
15857
15858 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15859 if (EQ (pdir, Qright_to_left))
15860 {
15861 start = whole - end;
15862 end = start + portion;
15863 }
15864 }
15865
15866 if (old_buffer)
15867 set_buffer_internal (old_buffer);
15868 }
15869 else
15870 start = end = whole = portion = 0;
15871
15872 w->hscroll_whole = whole;
15873
15874 /* Indicate what this scroll bar ought to be displaying now. */
15875 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15876 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15877 (w, portion, whole, start);
15878 }
15879
15880
15881 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
15882 selected_window is redisplayed.
15883
15884 We can return without actually redisplaying the window if fonts has been
15885 changed on window's frame. In that case, redisplay_internal will retry.
15886
15887 As one of the important parts of redisplaying a window, we need to
15888 decide whether the previous window-start position (stored in the
15889 window's w->start marker position) is still valid, and if it isn't,
15890 recompute it. Some details about that:
15891
15892 . The previous window-start could be in a continuation line, in
15893 which case we need to recompute it when the window width
15894 changes. See compute_window_start_on_continuation_line and its
15895 call below.
15896
15897 . The text that changed since last redisplay could include the
15898 previous window-start position. In that case, we try to salvage
15899 what we can from the current glyph matrix by calling
15900 try_scrolling, which see.
15901
15902 . Some Emacs command could force us to use a specific window-start
15903 position by setting the window's force_start flag, or gently
15904 propose doing that by setting the window's optional_new_start
15905 flag. In these cases, we try using the specified start point if
15906 that succeeds (i.e. the window desired matrix is successfully
15907 recomputed, and point location is within the window). In case
15908 of optional_new_start, we first check if the specified start
15909 position is feasible, i.e. if it will allow point to be
15910 displayed in the window. If using the specified start point
15911 fails, e.g., if new fonts are needed to be loaded, we abort the
15912 redisplay cycle and leave it up to the next cycle to figure out
15913 things.
15914
15915 . Note that the window's force_start flag is sometimes set by
15916 redisplay itself, when it decides that the previous window start
15917 point is fine and should be kept. Search for "goto force_start"
15918 below to see the details. Like the values of window-start
15919 specified outside of redisplay, these internally-deduced values
15920 are tested for feasibility, and ignored if found to be
15921 unfeasible.
15922
15923 . Note that the function try_window, used to completely redisplay
15924 a window, accepts the window's start point as its argument.
15925 This is used several times in the redisplay code to control
15926 where the window start will be, according to user options such
15927 as scroll-conservatively, and also to ensure the screen line
15928 showing point will be fully (as opposed to partially) visible on
15929 display. */
15930
15931 static void
15932 redisplay_window (Lisp_Object window, bool just_this_one_p)
15933 {
15934 struct window *w = XWINDOW (window);
15935 struct frame *f = XFRAME (w->frame);
15936 struct buffer *buffer = XBUFFER (w->contents);
15937 struct buffer *old = current_buffer;
15938 struct text_pos lpoint, opoint, startp;
15939 bool update_mode_line;
15940 int tem;
15941 struct it it;
15942 /* Record it now because it's overwritten. */
15943 bool current_matrix_up_to_date_p = false;
15944 bool used_current_matrix_p = false;
15945 /* This is less strict than current_matrix_up_to_date_p.
15946 It indicates that the buffer contents and narrowing are unchanged. */
15947 bool buffer_unchanged_p = false;
15948 bool temp_scroll_step = false;
15949 ptrdiff_t count = SPECPDL_INDEX ();
15950 int rc;
15951 int centering_position = -1;
15952 bool last_line_misfit = false;
15953 ptrdiff_t beg_unchanged, end_unchanged;
15954 int frame_line_height;
15955
15956 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15957 opoint = lpoint;
15958
15959 #ifdef GLYPH_DEBUG
15960 *w->desired_matrix->method = 0;
15961 #endif
15962
15963 if (!just_this_one_p
15964 && REDISPLAY_SOME_P ()
15965 && !w->redisplay
15966 && !w->update_mode_line
15967 && !f->face_change
15968 && !f->redisplay
15969 && !buffer->text->redisplay
15970 && BUF_PT (buffer) == w->last_point)
15971 return;
15972
15973 /* Make sure that both W's markers are valid. */
15974 eassert (XMARKER (w->start)->buffer == buffer);
15975 eassert (XMARKER (w->pointm)->buffer == buffer);
15976
15977 /* We come here again if we need to run window-text-change-functions
15978 below. */
15979 restart:
15980 reconsider_clip_changes (w);
15981 frame_line_height = default_line_pixel_height (w);
15982
15983 /* Has the mode line to be updated? */
15984 update_mode_line = (w->update_mode_line
15985 || update_mode_lines
15986 || buffer->clip_changed
15987 || buffer->prevent_redisplay_optimizations_p);
15988
15989 if (!just_this_one_p)
15990 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15991 cleverly elsewhere. */
15992 w->must_be_updated_p = true;
15993
15994 if (MINI_WINDOW_P (w))
15995 {
15996 if (w == XWINDOW (echo_area_window)
15997 && !NILP (echo_area_buffer[0]))
15998 {
15999 if (update_mode_line)
16000 /* We may have to update a tty frame's menu bar or a
16001 tool-bar. Example `M-x C-h C-h C-g'. */
16002 goto finish_menu_bars;
16003 else
16004 /* We've already displayed the echo area glyphs in this window. */
16005 goto finish_scroll_bars;
16006 }
16007 else if ((w != XWINDOW (minibuf_window)
16008 || minibuf_level == 0)
16009 /* When buffer is nonempty, redisplay window normally. */
16010 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16011 /* Quail displays non-mini buffers in minibuffer window.
16012 In that case, redisplay the window normally. */
16013 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16014 {
16015 /* W is a mini-buffer window, but it's not active, so clear
16016 it. */
16017 int yb = window_text_bottom_y (w);
16018 struct glyph_row *row;
16019 int y;
16020
16021 for (y = 0, row = w->desired_matrix->rows;
16022 y < yb;
16023 y += row->height, ++row)
16024 blank_row (w, row, y);
16025 goto finish_scroll_bars;
16026 }
16027
16028 clear_glyph_matrix (w->desired_matrix);
16029 }
16030
16031 /* Otherwise set up data on this window; select its buffer and point
16032 value. */
16033 /* Really select the buffer, for the sake of buffer-local
16034 variables. */
16035 set_buffer_internal_1 (XBUFFER (w->contents));
16036
16037 current_matrix_up_to_date_p
16038 = (w->window_end_valid
16039 && !current_buffer->clip_changed
16040 && !current_buffer->prevent_redisplay_optimizations_p
16041 && !window_outdated (w));
16042
16043 /* Run the window-text-change-functions
16044 if it is possible that the text on the screen has changed
16045 (either due to modification of the text, or any other reason). */
16046 if (!current_matrix_up_to_date_p
16047 && !NILP (Vwindow_text_change_functions))
16048 {
16049 safe_run_hooks (Qwindow_text_change_functions);
16050 goto restart;
16051 }
16052
16053 beg_unchanged = BEG_UNCHANGED;
16054 end_unchanged = END_UNCHANGED;
16055
16056 SET_TEXT_POS (opoint, PT, PT_BYTE);
16057
16058 specbind (Qinhibit_point_motion_hooks, Qt);
16059
16060 buffer_unchanged_p
16061 = (w->window_end_valid
16062 && !current_buffer->clip_changed
16063 && !window_outdated (w));
16064
16065 /* When windows_or_buffers_changed is non-zero, we can't rely
16066 on the window end being valid, so set it to zero there. */
16067 if (windows_or_buffers_changed)
16068 {
16069 /* If window starts on a continuation line, maybe adjust the
16070 window start in case the window's width changed. */
16071 if (XMARKER (w->start)->buffer == current_buffer)
16072 compute_window_start_on_continuation_line (w);
16073
16074 w->window_end_valid = false;
16075 /* If so, we also can't rely on current matrix
16076 and should not fool try_cursor_movement below. */
16077 current_matrix_up_to_date_p = false;
16078 }
16079
16080 /* Some sanity checks. */
16081 CHECK_WINDOW_END (w);
16082 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16083 emacs_abort ();
16084 if (BYTEPOS (opoint) < CHARPOS (opoint))
16085 emacs_abort ();
16086
16087 if (mode_line_update_needed (w))
16088 update_mode_line = true;
16089
16090 /* Point refers normally to the selected window. For any other
16091 window, set up appropriate value. */
16092 if (!EQ (window, selected_window))
16093 {
16094 ptrdiff_t new_pt = marker_position (w->pointm);
16095 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16096
16097 if (new_pt < BEGV)
16098 {
16099 new_pt = BEGV;
16100 new_pt_byte = BEGV_BYTE;
16101 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16102 }
16103 else if (new_pt > (ZV - 1))
16104 {
16105 new_pt = ZV;
16106 new_pt_byte = ZV_BYTE;
16107 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16108 }
16109
16110 /* We don't use SET_PT so that the point-motion hooks don't run. */
16111 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16112 }
16113
16114 /* If any of the character widths specified in the display table
16115 have changed, invalidate the width run cache. It's true that
16116 this may be a bit late to catch such changes, but the rest of
16117 redisplay goes (non-fatally) haywire when the display table is
16118 changed, so why should we worry about doing any better? */
16119 if (current_buffer->width_run_cache
16120 || (current_buffer->base_buffer
16121 && current_buffer->base_buffer->width_run_cache))
16122 {
16123 struct Lisp_Char_Table *disptab = buffer_display_table ();
16124
16125 if (! disptab_matches_widthtab
16126 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16127 {
16128 struct buffer *buf = current_buffer;
16129
16130 if (buf->base_buffer)
16131 buf = buf->base_buffer;
16132 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16133 recompute_width_table (current_buffer, disptab);
16134 }
16135 }
16136
16137 /* If window-start is screwed up, choose a new one. */
16138 if (XMARKER (w->start)->buffer != current_buffer)
16139 goto recenter;
16140
16141 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16142
16143 /* If someone specified a new starting point but did not insist,
16144 check whether it can be used. */
16145 if ((w->optional_new_start || window_frozen_p (w))
16146 && CHARPOS (startp) >= BEGV
16147 && CHARPOS (startp) <= ZV)
16148 {
16149 ptrdiff_t it_charpos;
16150
16151 w->optional_new_start = false;
16152 start_display (&it, w, startp);
16153 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16154 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16155 /* Record IT's position now, since line_bottom_y might change
16156 that. */
16157 it_charpos = IT_CHARPOS (it);
16158 /* Make sure we set the force_start flag only if the cursor row
16159 will be fully visible. Otherwise, the code under force_start
16160 label below will try to move point back into view, which is
16161 not what the code which sets optional_new_start wants. */
16162 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16163 && !w->force_start)
16164 {
16165 if (it_charpos == PT)
16166 w->force_start = true;
16167 /* IT may overshoot PT if text at PT is invisible. */
16168 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16169 w->force_start = true;
16170 #ifdef GLYPH_DEBUG
16171 if (w->force_start)
16172 {
16173 if (window_frozen_p (w))
16174 debug_method_add (w, "set force_start from frozen window start");
16175 else
16176 debug_method_add (w, "set force_start from optional_new_start");
16177 }
16178 #endif
16179 }
16180 }
16181
16182 force_start:
16183
16184 /* Handle case where place to start displaying has been specified,
16185 unless the specified location is outside the accessible range. */
16186 if (w->force_start)
16187 {
16188 /* We set this later on if we have to adjust point. */
16189 int new_vpos = -1;
16190
16191 w->force_start = false;
16192 w->vscroll = 0;
16193 w->window_end_valid = false;
16194
16195 /* Forget any recorded base line for line number display. */
16196 if (!buffer_unchanged_p)
16197 w->base_line_number = 0;
16198
16199 /* Redisplay the mode line. Select the buffer properly for that.
16200 Also, run the hook window-scroll-functions
16201 because we have scrolled. */
16202 /* Note, we do this after clearing force_start because
16203 if there's an error, it is better to forget about force_start
16204 than to get into an infinite loop calling the hook functions
16205 and having them get more errors. */
16206 if (!update_mode_line
16207 || ! NILP (Vwindow_scroll_functions))
16208 {
16209 update_mode_line = true;
16210 w->update_mode_line = true;
16211 startp = run_window_scroll_functions (window, startp);
16212 }
16213
16214 if (CHARPOS (startp) < BEGV)
16215 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16216 else if (CHARPOS (startp) > ZV)
16217 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16218
16219 /* Redisplay, then check if cursor has been set during the
16220 redisplay. Give up if new fonts were loaded. */
16221 /* We used to issue a CHECK_MARGINS argument to try_window here,
16222 but this causes scrolling to fail when point begins inside
16223 the scroll margin (bug#148) -- cyd */
16224 if (!try_window (window, startp, 0))
16225 {
16226 w->force_start = true;
16227 clear_glyph_matrix (w->desired_matrix);
16228 goto need_larger_matrices;
16229 }
16230
16231 if (w->cursor.vpos < 0)
16232 {
16233 /* If point does not appear, try to move point so it does
16234 appear. The desired matrix has been built above, so we
16235 can use it here. */
16236 new_vpos = window_box_height (w) / 2;
16237 }
16238
16239 if (!cursor_row_fully_visible_p (w, false, false))
16240 {
16241 /* Point does appear, but on a line partly visible at end of window.
16242 Move it back to a fully-visible line. */
16243 new_vpos = window_box_height (w);
16244 /* But if window_box_height suggests a Y coordinate that is
16245 not less than we already have, that line will clearly not
16246 be fully visible, so give up and scroll the display.
16247 This can happen when the default face uses a font whose
16248 dimensions are different from the frame's default
16249 font. */
16250 if (new_vpos >= w->cursor.y)
16251 {
16252 w->cursor.vpos = -1;
16253 clear_glyph_matrix (w->desired_matrix);
16254 goto try_to_scroll;
16255 }
16256 }
16257 else if (w->cursor.vpos >= 0)
16258 {
16259 /* Some people insist on not letting point enter the scroll
16260 margin, even though this part handles windows that didn't
16261 scroll at all. */
16262 int window_total_lines
16263 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16264 int margin = min (scroll_margin, window_total_lines / 4);
16265 int pixel_margin = margin * frame_line_height;
16266 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16267
16268 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16269 below, which finds the row to move point to, advances by
16270 the Y coordinate of the _next_ row, see the definition of
16271 MATRIX_ROW_BOTTOM_Y. */
16272 if (w->cursor.vpos < margin + header_line)
16273 {
16274 w->cursor.vpos = -1;
16275 clear_glyph_matrix (w->desired_matrix);
16276 goto try_to_scroll;
16277 }
16278 else
16279 {
16280 int window_height = window_box_height (w);
16281
16282 if (header_line)
16283 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16284 if (w->cursor.y >= window_height - pixel_margin)
16285 {
16286 w->cursor.vpos = -1;
16287 clear_glyph_matrix (w->desired_matrix);
16288 goto try_to_scroll;
16289 }
16290 }
16291 }
16292
16293 /* If we need to move point for either of the above reasons,
16294 now actually do it. */
16295 if (new_vpos >= 0)
16296 {
16297 struct glyph_row *row;
16298
16299 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16300 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16301 ++row;
16302
16303 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16304 MATRIX_ROW_START_BYTEPOS (row));
16305
16306 if (w != XWINDOW (selected_window))
16307 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16308 else if (current_buffer == old)
16309 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16310
16311 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16312
16313 /* Re-run pre-redisplay-function so it can update the region
16314 according to the new position of point. */
16315 /* Other than the cursor, w's redisplay is done so we can set its
16316 redisplay to false. Also the buffer's redisplay can be set to
16317 false, since propagate_buffer_redisplay should have already
16318 propagated its info to `w' anyway. */
16319 w->redisplay = false;
16320 XBUFFER (w->contents)->text->redisplay = false;
16321 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16322
16323 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16324 {
16325 /* pre-redisplay-function made changes (e.g. move the region)
16326 that require another round of redisplay. */
16327 clear_glyph_matrix (w->desired_matrix);
16328 if (!try_window (window, startp, 0))
16329 goto need_larger_matrices;
16330 }
16331 }
16332 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16333 {
16334 clear_glyph_matrix (w->desired_matrix);
16335 goto try_to_scroll;
16336 }
16337
16338 #ifdef GLYPH_DEBUG
16339 debug_method_add (w, "forced window start");
16340 #endif
16341 goto done;
16342 }
16343
16344 /* Handle case where text has not changed, only point, and it has
16345 not moved off the frame, and we are not retrying after hscroll.
16346 (current_matrix_up_to_date_p is true when retrying.) */
16347 if (current_matrix_up_to_date_p
16348 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16349 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16350 {
16351 switch (rc)
16352 {
16353 case CURSOR_MOVEMENT_SUCCESS:
16354 used_current_matrix_p = true;
16355 goto done;
16356
16357 case CURSOR_MOVEMENT_MUST_SCROLL:
16358 goto try_to_scroll;
16359
16360 default:
16361 emacs_abort ();
16362 }
16363 }
16364 /* If current starting point was originally the beginning of a line
16365 but no longer is, find a new starting point. */
16366 else if (w->start_at_line_beg
16367 && !(CHARPOS (startp) <= BEGV
16368 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16369 {
16370 #ifdef GLYPH_DEBUG
16371 debug_method_add (w, "recenter 1");
16372 #endif
16373 goto recenter;
16374 }
16375
16376 /* Try scrolling with try_window_id. Value is > 0 if update has
16377 been done, it is -1 if we know that the same window start will
16378 not work. It is 0 if unsuccessful for some other reason. */
16379 else if ((tem = try_window_id (w)) != 0)
16380 {
16381 #ifdef GLYPH_DEBUG
16382 debug_method_add (w, "try_window_id %d", tem);
16383 #endif
16384
16385 if (f->fonts_changed)
16386 goto need_larger_matrices;
16387 if (tem > 0)
16388 goto done;
16389
16390 /* Otherwise try_window_id has returned -1 which means that we
16391 don't want the alternative below this comment to execute. */
16392 }
16393 else if (CHARPOS (startp) >= BEGV
16394 && CHARPOS (startp) <= ZV
16395 && PT >= CHARPOS (startp)
16396 && (CHARPOS (startp) < ZV
16397 /* Avoid starting at end of buffer. */
16398 || CHARPOS (startp) == BEGV
16399 || !window_outdated (w)))
16400 {
16401 int d1, d2, d5, d6;
16402 int rtop, rbot;
16403
16404 /* If first window line is a continuation line, and window start
16405 is inside the modified region, but the first change is before
16406 current window start, we must select a new window start.
16407
16408 However, if this is the result of a down-mouse event (e.g. by
16409 extending the mouse-drag-overlay), we don't want to select a
16410 new window start, since that would change the position under
16411 the mouse, resulting in an unwanted mouse-movement rather
16412 than a simple mouse-click. */
16413 if (!w->start_at_line_beg
16414 && NILP (do_mouse_tracking)
16415 && CHARPOS (startp) > BEGV
16416 && CHARPOS (startp) > BEG + beg_unchanged
16417 && CHARPOS (startp) <= Z - end_unchanged
16418 /* Even if w->start_at_line_beg is nil, a new window may
16419 start at a line_beg, since that's how set_buffer_window
16420 sets it. So, we need to check the return value of
16421 compute_window_start_on_continuation_line. (See also
16422 bug#197). */
16423 && XMARKER (w->start)->buffer == current_buffer
16424 && compute_window_start_on_continuation_line (w)
16425 /* It doesn't make sense to force the window start like we
16426 do at label force_start if it is already known that point
16427 will not be fully visible in the resulting window, because
16428 doing so will move point from its correct position
16429 instead of scrolling the window to bring point into view.
16430 See bug#9324. */
16431 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16432 /* A very tall row could need more than the window height,
16433 in which case we accept that it is partially visible. */
16434 && (rtop != 0) == (rbot != 0))
16435 {
16436 w->force_start = true;
16437 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16438 #ifdef GLYPH_DEBUG
16439 debug_method_add (w, "recomputed window start in continuation line");
16440 #endif
16441 goto force_start;
16442 }
16443
16444 #ifdef GLYPH_DEBUG
16445 debug_method_add (w, "same window start");
16446 #endif
16447
16448 /* Try to redisplay starting at same place as before.
16449 If point has not moved off frame, accept the results. */
16450 if (!current_matrix_up_to_date_p
16451 /* Don't use try_window_reusing_current_matrix in this case
16452 because a window scroll function can have changed the
16453 buffer. */
16454 || !NILP (Vwindow_scroll_functions)
16455 || MINI_WINDOW_P (w)
16456 || !(used_current_matrix_p
16457 = try_window_reusing_current_matrix (w)))
16458 {
16459 IF_DEBUG (debug_method_add (w, "1"));
16460 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16461 /* -1 means we need to scroll.
16462 0 means we need new matrices, but fonts_changed
16463 is set in that case, so we will detect it below. */
16464 goto try_to_scroll;
16465 }
16466
16467 if (f->fonts_changed)
16468 goto need_larger_matrices;
16469
16470 if (w->cursor.vpos >= 0)
16471 {
16472 if (!just_this_one_p
16473 || current_buffer->clip_changed
16474 || BEG_UNCHANGED < CHARPOS (startp))
16475 /* Forget any recorded base line for line number display. */
16476 w->base_line_number = 0;
16477
16478 if (!cursor_row_fully_visible_p (w, true, false))
16479 {
16480 clear_glyph_matrix (w->desired_matrix);
16481 last_line_misfit = true;
16482 }
16483 /* Drop through and scroll. */
16484 else
16485 goto done;
16486 }
16487 else
16488 clear_glyph_matrix (w->desired_matrix);
16489 }
16490
16491 try_to_scroll:
16492
16493 /* Redisplay the mode line. Select the buffer properly for that. */
16494 if (!update_mode_line)
16495 {
16496 update_mode_line = true;
16497 w->update_mode_line = true;
16498 }
16499
16500 /* Try to scroll by specified few lines. */
16501 if ((scroll_conservatively
16502 || emacs_scroll_step
16503 || temp_scroll_step
16504 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16505 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16506 && CHARPOS (startp) >= BEGV
16507 && CHARPOS (startp) <= ZV)
16508 {
16509 /* The function returns -1 if new fonts were loaded, 1 if
16510 successful, 0 if not successful. */
16511 int ss = try_scrolling (window, just_this_one_p,
16512 scroll_conservatively,
16513 emacs_scroll_step,
16514 temp_scroll_step, last_line_misfit);
16515 switch (ss)
16516 {
16517 case SCROLLING_SUCCESS:
16518 goto done;
16519
16520 case SCROLLING_NEED_LARGER_MATRICES:
16521 goto need_larger_matrices;
16522
16523 case SCROLLING_FAILED:
16524 break;
16525
16526 default:
16527 emacs_abort ();
16528 }
16529 }
16530
16531 /* Finally, just choose a place to start which positions point
16532 according to user preferences. */
16533
16534 recenter:
16535
16536 #ifdef GLYPH_DEBUG
16537 debug_method_add (w, "recenter");
16538 #endif
16539
16540 /* Forget any previously recorded base line for line number display. */
16541 if (!buffer_unchanged_p)
16542 w->base_line_number = 0;
16543
16544 /* Determine the window start relative to point. */
16545 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16546 it.current_y = it.last_visible_y;
16547 if (centering_position < 0)
16548 {
16549 int window_total_lines
16550 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16551 int margin
16552 = scroll_margin > 0
16553 ? min (scroll_margin, window_total_lines / 4)
16554 : 0;
16555 ptrdiff_t margin_pos = CHARPOS (startp);
16556 Lisp_Object aggressive;
16557 bool scrolling_up;
16558
16559 /* If there is a scroll margin at the top of the window, find
16560 its character position. */
16561 if (margin
16562 /* Cannot call start_display if startp is not in the
16563 accessible region of the buffer. This can happen when we
16564 have just switched to a different buffer and/or changed
16565 its restriction. In that case, startp is initialized to
16566 the character position 1 (BEGV) because we did not yet
16567 have chance to display the buffer even once. */
16568 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16569 {
16570 struct it it1;
16571 void *it1data = NULL;
16572
16573 SAVE_IT (it1, it, it1data);
16574 start_display (&it1, w, startp);
16575 move_it_vertically (&it1, margin * frame_line_height);
16576 margin_pos = IT_CHARPOS (it1);
16577 RESTORE_IT (&it, &it, it1data);
16578 }
16579 scrolling_up = PT > margin_pos;
16580 aggressive =
16581 scrolling_up
16582 ? BVAR (current_buffer, scroll_up_aggressively)
16583 : BVAR (current_buffer, scroll_down_aggressively);
16584
16585 if (!MINI_WINDOW_P (w)
16586 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16587 {
16588 int pt_offset = 0;
16589
16590 /* Setting scroll-conservatively overrides
16591 scroll-*-aggressively. */
16592 if (!scroll_conservatively && NUMBERP (aggressive))
16593 {
16594 double float_amount = XFLOATINT (aggressive);
16595
16596 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16597 if (pt_offset == 0 && float_amount > 0)
16598 pt_offset = 1;
16599 if (pt_offset && margin > 0)
16600 margin -= 1;
16601 }
16602 /* Compute how much to move the window start backward from
16603 point so that point will be displayed where the user
16604 wants it. */
16605 if (scrolling_up)
16606 {
16607 centering_position = it.last_visible_y;
16608 if (pt_offset)
16609 centering_position -= pt_offset;
16610 centering_position -=
16611 (frame_line_height * (1 + margin + last_line_misfit)
16612 + WINDOW_HEADER_LINE_HEIGHT (w));
16613 /* Don't let point enter the scroll margin near top of
16614 the window. */
16615 if (centering_position < margin * frame_line_height)
16616 centering_position = margin * frame_line_height;
16617 }
16618 else
16619 centering_position = margin * frame_line_height + pt_offset;
16620 }
16621 else
16622 /* Set the window start half the height of the window backward
16623 from point. */
16624 centering_position = window_box_height (w) / 2;
16625 }
16626 move_it_vertically_backward (&it, centering_position);
16627
16628 eassert (IT_CHARPOS (it) >= BEGV);
16629
16630 /* The function move_it_vertically_backward may move over more
16631 than the specified y-distance. If it->w is small, e.g. a
16632 mini-buffer window, we may end up in front of the window's
16633 display area. Start displaying at the start of the line
16634 containing PT in this case. */
16635 if (it.current_y <= 0)
16636 {
16637 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16638 move_it_vertically_backward (&it, 0);
16639 it.current_y = 0;
16640 }
16641
16642 it.current_x = it.hpos = 0;
16643
16644 /* Set the window start position here explicitly, to avoid an
16645 infinite loop in case the functions in window-scroll-functions
16646 get errors. */
16647 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16648
16649 /* Run scroll hooks. */
16650 startp = run_window_scroll_functions (window, it.current.pos);
16651
16652 /* Redisplay the window. */
16653 if (!current_matrix_up_to_date_p
16654 || windows_or_buffers_changed
16655 || f->cursor_type_changed
16656 /* Don't use try_window_reusing_current_matrix in this case
16657 because it can have changed the buffer. */
16658 || !NILP (Vwindow_scroll_functions)
16659 || !just_this_one_p
16660 || MINI_WINDOW_P (w)
16661 || !(used_current_matrix_p
16662 = try_window_reusing_current_matrix (w)))
16663 try_window (window, startp, 0);
16664
16665 /* If new fonts have been loaded (due to fontsets), give up. We
16666 have to start a new redisplay since we need to re-adjust glyph
16667 matrices. */
16668 if (f->fonts_changed)
16669 goto need_larger_matrices;
16670
16671 /* If cursor did not appear assume that the middle of the window is
16672 in the first line of the window. Do it again with the next line.
16673 (Imagine a window of height 100, displaying two lines of height
16674 60. Moving back 50 from it->last_visible_y will end in the first
16675 line.) */
16676 if (w->cursor.vpos < 0)
16677 {
16678 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16679 {
16680 clear_glyph_matrix (w->desired_matrix);
16681 move_it_by_lines (&it, 1);
16682 try_window (window, it.current.pos, 0);
16683 }
16684 else if (PT < IT_CHARPOS (it))
16685 {
16686 clear_glyph_matrix (w->desired_matrix);
16687 move_it_by_lines (&it, -1);
16688 try_window (window, it.current.pos, 0);
16689 }
16690 else
16691 {
16692 /* Not much we can do about it. */
16693 }
16694 }
16695
16696 /* Consider the following case: Window starts at BEGV, there is
16697 invisible, intangible text at BEGV, so that display starts at
16698 some point START > BEGV. It can happen that we are called with
16699 PT somewhere between BEGV and START. Try to handle that case,
16700 and similar ones. */
16701 if (w->cursor.vpos < 0)
16702 {
16703 /* First, try locating the proper glyph row for PT. */
16704 struct glyph_row *row =
16705 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16706
16707 /* Sometimes point is at the beginning of invisible text that is
16708 before the 1st character displayed in the row. In that case,
16709 row_containing_pos fails to find the row, because no glyphs
16710 with appropriate buffer positions are present in the row.
16711 Therefore, we next try to find the row which shows the 1st
16712 position after the invisible text. */
16713 if (!row)
16714 {
16715 Lisp_Object val =
16716 get_char_property_and_overlay (make_number (PT), Qinvisible,
16717 Qnil, NULL);
16718
16719 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
16720 {
16721 ptrdiff_t alt_pos;
16722 Lisp_Object invis_end =
16723 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16724 Qnil, Qnil);
16725
16726 if (NATNUMP (invis_end))
16727 alt_pos = XFASTINT (invis_end);
16728 else
16729 alt_pos = ZV;
16730 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16731 NULL, 0);
16732 }
16733 }
16734 /* Finally, fall back on the first row of the window after the
16735 header line (if any). This is slightly better than not
16736 displaying the cursor at all. */
16737 if (!row)
16738 {
16739 row = w->current_matrix->rows;
16740 if (row->mode_line_p)
16741 ++row;
16742 }
16743 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16744 }
16745
16746 if (!cursor_row_fully_visible_p (w, false, false))
16747 {
16748 /* If vscroll is enabled, disable it and try again. */
16749 if (w->vscroll)
16750 {
16751 w->vscroll = 0;
16752 clear_glyph_matrix (w->desired_matrix);
16753 goto recenter;
16754 }
16755
16756 /* Users who set scroll-conservatively to a large number want
16757 point just above/below the scroll margin. If we ended up
16758 with point's row partially visible, move the window start to
16759 make that row fully visible and out of the margin. */
16760 if (scroll_conservatively > SCROLL_LIMIT)
16761 {
16762 int window_total_lines
16763 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16764 int margin =
16765 scroll_margin > 0
16766 ? min (scroll_margin, window_total_lines / 4)
16767 : 0;
16768 bool move_down = w->cursor.vpos >= window_total_lines / 2;
16769
16770 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16771 clear_glyph_matrix (w->desired_matrix);
16772 if (1 == try_window (window, it.current.pos,
16773 TRY_WINDOW_CHECK_MARGINS))
16774 goto done;
16775 }
16776
16777 /* If centering point failed to make the whole line visible,
16778 put point at the top instead. That has to make the whole line
16779 visible, if it can be done. */
16780 if (centering_position == 0)
16781 goto done;
16782
16783 clear_glyph_matrix (w->desired_matrix);
16784 centering_position = 0;
16785 goto recenter;
16786 }
16787
16788 done:
16789
16790 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16791 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16793
16794 /* Display the mode line, if we must. */
16795 if ((update_mode_line
16796 /* If window not full width, must redo its mode line
16797 if (a) the window to its side is being redone and
16798 (b) we do a frame-based redisplay. This is a consequence
16799 of how inverted lines are drawn in frame-based redisplay. */
16800 || (!just_this_one_p
16801 && !FRAME_WINDOW_P (f)
16802 && !WINDOW_FULL_WIDTH_P (w))
16803 /* Line number to display. */
16804 || w->base_line_pos > 0
16805 /* Column number is displayed and different from the one displayed. */
16806 || (w->column_number_displayed != -1
16807 && (w->column_number_displayed != current_column ())))
16808 /* This means that the window has a mode line. */
16809 && (WINDOW_WANTS_MODELINE_P (w)
16810 || WINDOW_WANTS_HEADER_LINE_P (w)))
16811 {
16812
16813 display_mode_lines (w);
16814
16815 /* If mode line height has changed, arrange for a thorough
16816 immediate redisplay using the correct mode line height. */
16817 if (WINDOW_WANTS_MODELINE_P (w)
16818 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16819 {
16820 f->fonts_changed = true;
16821 w->mode_line_height = -1;
16822 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16823 = DESIRED_MODE_LINE_HEIGHT (w);
16824 }
16825
16826 /* If header line height has changed, arrange for a thorough
16827 immediate redisplay using the correct header line height. */
16828 if (WINDOW_WANTS_HEADER_LINE_P (w)
16829 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16830 {
16831 f->fonts_changed = true;
16832 w->header_line_height = -1;
16833 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16834 = DESIRED_HEADER_LINE_HEIGHT (w);
16835 }
16836
16837 if (f->fonts_changed)
16838 goto need_larger_matrices;
16839 }
16840
16841 if (!line_number_displayed && w->base_line_pos != -1)
16842 {
16843 w->base_line_pos = 0;
16844 w->base_line_number = 0;
16845 }
16846
16847 finish_menu_bars:
16848
16849 /* When we reach a frame's selected window, redo the frame's menu bar. */
16850 if (update_mode_line
16851 && EQ (FRAME_SELECTED_WINDOW (f), window))
16852 {
16853 bool redisplay_menu_p;
16854
16855 if (FRAME_WINDOW_P (f))
16856 {
16857 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16858 || defined (HAVE_NS) || defined (USE_GTK)
16859 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16860 #else
16861 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16862 #endif
16863 }
16864 else
16865 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16866
16867 if (redisplay_menu_p)
16868 display_menu_bar (w);
16869
16870 #ifdef HAVE_WINDOW_SYSTEM
16871 if (FRAME_WINDOW_P (f))
16872 {
16873 #if defined (USE_GTK) || defined (HAVE_NS)
16874 if (FRAME_EXTERNAL_TOOL_BAR (f))
16875 redisplay_tool_bar (f);
16876 #else
16877 if (WINDOWP (f->tool_bar_window)
16878 && (FRAME_TOOL_BAR_LINES (f) > 0
16879 || !NILP (Vauto_resize_tool_bars))
16880 && redisplay_tool_bar (f))
16881 ignore_mouse_drag_p = true;
16882 #endif
16883 }
16884 #endif
16885 }
16886
16887 #ifdef HAVE_WINDOW_SYSTEM
16888 if (FRAME_WINDOW_P (f)
16889 && update_window_fringes (w, (just_this_one_p
16890 || (!used_current_matrix_p && !overlay_arrow_seen)
16891 || w->pseudo_window_p)))
16892 {
16893 update_begin (f);
16894 block_input ();
16895 if (draw_window_fringes (w, true))
16896 {
16897 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16898 x_draw_right_divider (w);
16899 else
16900 x_draw_vertical_border (w);
16901 }
16902 unblock_input ();
16903 update_end (f);
16904 }
16905
16906 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16907 x_draw_bottom_divider (w);
16908 #endif /* HAVE_WINDOW_SYSTEM */
16909
16910 /* We go to this label, with fonts_changed set, if it is
16911 necessary to try again using larger glyph matrices.
16912 We have to redeem the scroll bar even in this case,
16913 because the loop in redisplay_internal expects that. */
16914 need_larger_matrices:
16915 ;
16916 finish_scroll_bars:
16917
16918 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16919 {
16920 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16921 /* Set the thumb's position and size. */
16922 set_vertical_scroll_bar (w);
16923
16924 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16925 /* Set the thumb's position and size. */
16926 set_horizontal_scroll_bar (w);
16927
16928 /* Note that we actually used the scroll bar attached to this
16929 window, so it shouldn't be deleted at the end of redisplay. */
16930 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16931 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16932 }
16933
16934 /* Restore current_buffer and value of point in it. The window
16935 update may have changed the buffer, so first make sure `opoint'
16936 is still valid (Bug#6177). */
16937 if (CHARPOS (opoint) < BEGV)
16938 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16939 else if (CHARPOS (opoint) > ZV)
16940 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16941 else
16942 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16943
16944 set_buffer_internal_1 (old);
16945 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16946 shorter. This can be caused by log truncation in *Messages*. */
16947 if (CHARPOS (lpoint) <= ZV)
16948 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16949
16950 unbind_to (count, Qnil);
16951 }
16952
16953
16954 /* Build the complete desired matrix of WINDOW with a window start
16955 buffer position POS.
16956
16957 Value is 1 if successful. It is zero if fonts were loaded during
16958 redisplay which makes re-adjusting glyph matrices necessary, and -1
16959 if point would appear in the scroll margins.
16960 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16961 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16962 set in FLAGS.) */
16963
16964 int
16965 try_window (Lisp_Object window, struct text_pos pos, int flags)
16966 {
16967 struct window *w = XWINDOW (window);
16968 struct it it;
16969 struct glyph_row *last_text_row = NULL;
16970 struct frame *f = XFRAME (w->frame);
16971 int frame_line_height = default_line_pixel_height (w);
16972
16973 /* Make POS the new window start. */
16974 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16975
16976 /* Mark cursor position as unknown. No overlay arrow seen. */
16977 w->cursor.vpos = -1;
16978 overlay_arrow_seen = false;
16979
16980 /* Initialize iterator and info to start at POS. */
16981 start_display (&it, w, pos);
16982 it.glyph_row->reversed_p = false;
16983
16984 /* Display all lines of W. */
16985 while (it.current_y < it.last_visible_y)
16986 {
16987 if (display_line (&it))
16988 last_text_row = it.glyph_row - 1;
16989 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16990 return 0;
16991 }
16992
16993 /* Don't let the cursor end in the scroll margins. */
16994 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16995 && !MINI_WINDOW_P (w))
16996 {
16997 int this_scroll_margin;
16998 int window_total_lines
16999 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17000
17001 if (scroll_margin > 0)
17002 {
17003 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17004 this_scroll_margin *= frame_line_height;
17005 }
17006 else
17007 this_scroll_margin = 0;
17008
17009 if ((w->cursor.y >= 0 /* not vscrolled */
17010 && w->cursor.y < this_scroll_margin
17011 && CHARPOS (pos) > BEGV
17012 && IT_CHARPOS (it) < ZV)
17013 /* rms: considering make_cursor_line_fully_visible_p here
17014 seems to give wrong results. We don't want to recenter
17015 when the last line is partly visible, we want to allow
17016 that case to be handled in the usual way. */
17017 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17018 {
17019 w->cursor.vpos = -1;
17020 clear_glyph_matrix (w->desired_matrix);
17021 return -1;
17022 }
17023 }
17024
17025 /* If bottom moved off end of frame, change mode line percentage. */
17026 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17027 w->update_mode_line = true;
17028
17029 /* Set window_end_pos to the offset of the last character displayed
17030 on the window from the end of current_buffer. Set
17031 window_end_vpos to its row number. */
17032 if (last_text_row)
17033 {
17034 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17035 adjust_window_ends (w, last_text_row, false);
17036 eassert
17037 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17038 w->window_end_vpos)));
17039 }
17040 else
17041 {
17042 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17043 w->window_end_pos = Z - ZV;
17044 w->window_end_vpos = 0;
17045 }
17046
17047 /* But that is not valid info until redisplay finishes. */
17048 w->window_end_valid = false;
17049 return 1;
17050 }
17051
17052
17053 \f
17054 /************************************************************************
17055 Window redisplay reusing current matrix when buffer has not changed
17056 ************************************************************************/
17057
17058 /* Try redisplay of window W showing an unchanged buffer with a
17059 different window start than the last time it was displayed by
17060 reusing its current matrix. Value is true if successful.
17061 W->start is the new window start. */
17062
17063 static bool
17064 try_window_reusing_current_matrix (struct window *w)
17065 {
17066 struct frame *f = XFRAME (w->frame);
17067 struct glyph_row *bottom_row;
17068 struct it it;
17069 struct run run;
17070 struct text_pos start, new_start;
17071 int nrows_scrolled, i;
17072 struct glyph_row *last_text_row;
17073 struct glyph_row *last_reused_text_row;
17074 struct glyph_row *start_row;
17075 int start_vpos, min_y, max_y;
17076
17077 #ifdef GLYPH_DEBUG
17078 if (inhibit_try_window_reusing)
17079 return false;
17080 #endif
17081
17082 if (/* This function doesn't handle terminal frames. */
17083 !FRAME_WINDOW_P (f)
17084 /* Don't try to reuse the display if windows have been split
17085 or such. */
17086 || windows_or_buffers_changed
17087 || f->cursor_type_changed)
17088 return false;
17089
17090 /* Can't do this if showing trailing whitespace. */
17091 if (!NILP (Vshow_trailing_whitespace))
17092 return false;
17093
17094 /* If top-line visibility has changed, give up. */
17095 if (WINDOW_WANTS_HEADER_LINE_P (w)
17096 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17097 return false;
17098
17099 /* Give up if old or new display is scrolled vertically. We could
17100 make this function handle this, but right now it doesn't. */
17101 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17102 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17103 return false;
17104
17105 /* The variable new_start now holds the new window start. The old
17106 start `start' can be determined from the current matrix. */
17107 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17108 start = start_row->minpos;
17109 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17110
17111 /* Clear the desired matrix for the display below. */
17112 clear_glyph_matrix (w->desired_matrix);
17113
17114 if (CHARPOS (new_start) <= CHARPOS (start))
17115 {
17116 /* Don't use this method if the display starts with an ellipsis
17117 displayed for invisible text. It's not easy to handle that case
17118 below, and it's certainly not worth the effort since this is
17119 not a frequent case. */
17120 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17121 return false;
17122
17123 IF_DEBUG (debug_method_add (w, "twu1"));
17124
17125 /* Display up to a row that can be reused. The variable
17126 last_text_row is set to the last row displayed that displays
17127 text. Note that it.vpos == 0 if or if not there is a
17128 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17129 start_display (&it, w, new_start);
17130 w->cursor.vpos = -1;
17131 last_text_row = last_reused_text_row = NULL;
17132
17133 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17134 {
17135 /* If we have reached into the characters in the START row,
17136 that means the line boundaries have changed. So we
17137 can't start copying with the row START. Maybe it will
17138 work to start copying with the following row. */
17139 while (IT_CHARPOS (it) > CHARPOS (start))
17140 {
17141 /* Advance to the next row as the "start". */
17142 start_row++;
17143 start = start_row->minpos;
17144 /* If there are no more rows to try, or just one, give up. */
17145 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17146 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17147 || CHARPOS (start) == ZV)
17148 {
17149 clear_glyph_matrix (w->desired_matrix);
17150 return false;
17151 }
17152
17153 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17154 }
17155 /* If we have reached alignment, we can copy the rest of the
17156 rows. */
17157 if (IT_CHARPOS (it) == CHARPOS (start)
17158 /* Don't accept "alignment" inside a display vector,
17159 since start_row could have started in the middle of
17160 that same display vector (thus their character
17161 positions match), and we have no way of telling if
17162 that is the case. */
17163 && it.current.dpvec_index < 0)
17164 break;
17165
17166 it.glyph_row->reversed_p = false;
17167 if (display_line (&it))
17168 last_text_row = it.glyph_row - 1;
17169
17170 }
17171
17172 /* A value of current_y < last_visible_y means that we stopped
17173 at the previous window start, which in turn means that we
17174 have at least one reusable row. */
17175 if (it.current_y < it.last_visible_y)
17176 {
17177 struct glyph_row *row;
17178
17179 /* IT.vpos always starts from 0; it counts text lines. */
17180 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17181
17182 /* Find PT if not already found in the lines displayed. */
17183 if (w->cursor.vpos < 0)
17184 {
17185 int dy = it.current_y - start_row->y;
17186
17187 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17188 row = row_containing_pos (w, PT, row, NULL, dy);
17189 if (row)
17190 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17191 dy, nrows_scrolled);
17192 else
17193 {
17194 clear_glyph_matrix (w->desired_matrix);
17195 return false;
17196 }
17197 }
17198
17199 /* Scroll the display. Do it before the current matrix is
17200 changed. The problem here is that update has not yet
17201 run, i.e. part of the current matrix is not up to date.
17202 scroll_run_hook will clear the cursor, and use the
17203 current matrix to get the height of the row the cursor is
17204 in. */
17205 run.current_y = start_row->y;
17206 run.desired_y = it.current_y;
17207 run.height = it.last_visible_y - it.current_y;
17208
17209 if (run.height > 0 && run.current_y != run.desired_y)
17210 {
17211 update_begin (f);
17212 FRAME_RIF (f)->update_window_begin_hook (w);
17213 FRAME_RIF (f)->clear_window_mouse_face (w);
17214 FRAME_RIF (f)->scroll_run_hook (w, &run);
17215 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17216 update_end (f);
17217 }
17218
17219 /* Shift current matrix down by nrows_scrolled lines. */
17220 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17221 rotate_matrix (w->current_matrix,
17222 start_vpos,
17223 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17224 nrows_scrolled);
17225
17226 /* Disable lines that must be updated. */
17227 for (i = 0; i < nrows_scrolled; ++i)
17228 (start_row + i)->enabled_p = false;
17229
17230 /* Re-compute Y positions. */
17231 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17232 max_y = it.last_visible_y;
17233 for (row = start_row + nrows_scrolled;
17234 row < bottom_row;
17235 ++row)
17236 {
17237 row->y = it.current_y;
17238 row->visible_height = row->height;
17239
17240 if (row->y < min_y)
17241 row->visible_height -= min_y - row->y;
17242 if (row->y + row->height > max_y)
17243 row->visible_height -= row->y + row->height - max_y;
17244 if (row->fringe_bitmap_periodic_p)
17245 row->redraw_fringe_bitmaps_p = true;
17246
17247 it.current_y += row->height;
17248
17249 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17250 last_reused_text_row = row;
17251 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17252 break;
17253 }
17254
17255 /* Disable lines in the current matrix which are now
17256 below the window. */
17257 for (++row; row < bottom_row; ++row)
17258 row->enabled_p = row->mode_line_p = false;
17259 }
17260
17261 /* Update window_end_pos etc.; last_reused_text_row is the last
17262 reused row from the current matrix containing text, if any.
17263 The value of last_text_row is the last displayed line
17264 containing text. */
17265 if (last_reused_text_row)
17266 adjust_window_ends (w, last_reused_text_row, true);
17267 else if (last_text_row)
17268 adjust_window_ends (w, last_text_row, false);
17269 else
17270 {
17271 /* This window must be completely empty. */
17272 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17273 w->window_end_pos = Z - ZV;
17274 w->window_end_vpos = 0;
17275 }
17276 w->window_end_valid = false;
17277
17278 /* Update hint: don't try scrolling again in update_window. */
17279 w->desired_matrix->no_scrolling_p = true;
17280
17281 #ifdef GLYPH_DEBUG
17282 debug_method_add (w, "try_window_reusing_current_matrix 1");
17283 #endif
17284 return true;
17285 }
17286 else if (CHARPOS (new_start) > CHARPOS (start))
17287 {
17288 struct glyph_row *pt_row, *row;
17289 struct glyph_row *first_reusable_row;
17290 struct glyph_row *first_row_to_display;
17291 int dy;
17292 int yb = window_text_bottom_y (w);
17293
17294 /* Find the row starting at new_start, if there is one. Don't
17295 reuse a partially visible line at the end. */
17296 first_reusable_row = start_row;
17297 while (first_reusable_row->enabled_p
17298 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17299 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17300 < CHARPOS (new_start)))
17301 ++first_reusable_row;
17302
17303 /* Give up if there is no row to reuse. */
17304 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17305 || !first_reusable_row->enabled_p
17306 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17307 != CHARPOS (new_start)))
17308 return false;
17309
17310 /* We can reuse fully visible rows beginning with
17311 first_reusable_row to the end of the window. Set
17312 first_row_to_display to the first row that cannot be reused.
17313 Set pt_row to the row containing point, if there is any. */
17314 pt_row = NULL;
17315 for (first_row_to_display = first_reusable_row;
17316 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17317 ++first_row_to_display)
17318 {
17319 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17320 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17321 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17322 && first_row_to_display->ends_at_zv_p
17323 && pt_row == NULL)))
17324 pt_row = first_row_to_display;
17325 }
17326
17327 /* Start displaying at the start of first_row_to_display. */
17328 eassert (first_row_to_display->y < yb);
17329 init_to_row_start (&it, w, first_row_to_display);
17330
17331 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17332 - start_vpos);
17333 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17334 - nrows_scrolled);
17335 it.current_y = (first_row_to_display->y - first_reusable_row->y
17336 + WINDOW_HEADER_LINE_HEIGHT (w));
17337
17338 /* Display lines beginning with first_row_to_display in the
17339 desired matrix. Set last_text_row to the last row displayed
17340 that displays text. */
17341 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17342 if (pt_row == NULL)
17343 w->cursor.vpos = -1;
17344 last_text_row = NULL;
17345 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17346 if (display_line (&it))
17347 last_text_row = it.glyph_row - 1;
17348
17349 /* If point is in a reused row, adjust y and vpos of the cursor
17350 position. */
17351 if (pt_row)
17352 {
17353 w->cursor.vpos -= nrows_scrolled;
17354 w->cursor.y -= first_reusable_row->y - start_row->y;
17355 }
17356
17357 /* Give up if point isn't in a row displayed or reused. (This
17358 also handles the case where w->cursor.vpos < nrows_scrolled
17359 after the calls to display_line, which can happen with scroll
17360 margins. See bug#1295.) */
17361 if (w->cursor.vpos < 0)
17362 {
17363 clear_glyph_matrix (w->desired_matrix);
17364 return false;
17365 }
17366
17367 /* Scroll the display. */
17368 run.current_y = first_reusable_row->y;
17369 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17370 run.height = it.last_visible_y - run.current_y;
17371 dy = run.current_y - run.desired_y;
17372
17373 if (run.height)
17374 {
17375 update_begin (f);
17376 FRAME_RIF (f)->update_window_begin_hook (w);
17377 FRAME_RIF (f)->clear_window_mouse_face (w);
17378 FRAME_RIF (f)->scroll_run_hook (w, &run);
17379 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17380 update_end (f);
17381 }
17382
17383 /* Adjust Y positions of reused rows. */
17384 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17385 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17386 max_y = it.last_visible_y;
17387 for (row = first_reusable_row; row < first_row_to_display; ++row)
17388 {
17389 row->y -= dy;
17390 row->visible_height = row->height;
17391 if (row->y < min_y)
17392 row->visible_height -= min_y - row->y;
17393 if (row->y + row->height > max_y)
17394 row->visible_height -= row->y + row->height - max_y;
17395 if (row->fringe_bitmap_periodic_p)
17396 row->redraw_fringe_bitmaps_p = true;
17397 }
17398
17399 /* Scroll the current matrix. */
17400 eassert (nrows_scrolled > 0);
17401 rotate_matrix (w->current_matrix,
17402 start_vpos,
17403 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17404 -nrows_scrolled);
17405
17406 /* Disable rows not reused. */
17407 for (row -= nrows_scrolled; row < bottom_row; ++row)
17408 row->enabled_p = false;
17409
17410 /* Point may have moved to a different line, so we cannot assume that
17411 the previous cursor position is valid; locate the correct row. */
17412 if (pt_row)
17413 {
17414 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17415 row < bottom_row
17416 && PT >= MATRIX_ROW_END_CHARPOS (row)
17417 && !row->ends_at_zv_p;
17418 row++)
17419 {
17420 w->cursor.vpos++;
17421 w->cursor.y = row->y;
17422 }
17423 if (row < bottom_row)
17424 {
17425 /* Can't simply scan the row for point with
17426 bidi-reordered glyph rows. Let set_cursor_from_row
17427 figure out where to put the cursor, and if it fails,
17428 give up. */
17429 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17430 {
17431 if (!set_cursor_from_row (w, row, w->current_matrix,
17432 0, 0, 0, 0))
17433 {
17434 clear_glyph_matrix (w->desired_matrix);
17435 return false;
17436 }
17437 }
17438 else
17439 {
17440 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17441 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17442
17443 for (; glyph < end
17444 && (!BUFFERP (glyph->object)
17445 || glyph->charpos < PT);
17446 glyph++)
17447 {
17448 w->cursor.hpos++;
17449 w->cursor.x += glyph->pixel_width;
17450 }
17451 }
17452 }
17453 }
17454
17455 /* Adjust window end. A null value of last_text_row means that
17456 the window end is in reused rows which in turn means that
17457 only its vpos can have changed. */
17458 if (last_text_row)
17459 adjust_window_ends (w, last_text_row, false);
17460 else
17461 w->window_end_vpos -= nrows_scrolled;
17462
17463 w->window_end_valid = false;
17464 w->desired_matrix->no_scrolling_p = true;
17465
17466 #ifdef GLYPH_DEBUG
17467 debug_method_add (w, "try_window_reusing_current_matrix 2");
17468 #endif
17469 return true;
17470 }
17471
17472 return false;
17473 }
17474
17475
17476 \f
17477 /************************************************************************
17478 Window redisplay reusing current matrix when buffer has changed
17479 ************************************************************************/
17480
17481 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17482 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17483 ptrdiff_t *, ptrdiff_t *);
17484 static struct glyph_row *
17485 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17486 struct glyph_row *);
17487
17488
17489 /* Return the last row in MATRIX displaying text. If row START is
17490 non-null, start searching with that row. IT gives the dimensions
17491 of the display. Value is null if matrix is empty; otherwise it is
17492 a pointer to the row found. */
17493
17494 static struct glyph_row *
17495 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17496 struct glyph_row *start)
17497 {
17498 struct glyph_row *row, *row_found;
17499
17500 /* Set row_found to the last row in IT->w's current matrix
17501 displaying text. The loop looks funny but think of partially
17502 visible lines. */
17503 row_found = NULL;
17504 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17505 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17506 {
17507 eassert (row->enabled_p);
17508 row_found = row;
17509 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17510 break;
17511 ++row;
17512 }
17513
17514 return row_found;
17515 }
17516
17517
17518 /* Return the last row in the current matrix of W that is not affected
17519 by changes at the start of current_buffer that occurred since W's
17520 current matrix was built. Value is null if no such row exists.
17521
17522 BEG_UNCHANGED us the number of characters unchanged at the start of
17523 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17524 first changed character in current_buffer. Characters at positions <
17525 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17526 when the current matrix was built. */
17527
17528 static struct glyph_row *
17529 find_last_unchanged_at_beg_row (struct window *w)
17530 {
17531 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17532 struct glyph_row *row;
17533 struct glyph_row *row_found = NULL;
17534 int yb = window_text_bottom_y (w);
17535
17536 /* Find the last row displaying unchanged text. */
17537 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17538 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17539 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17540 ++row)
17541 {
17542 if (/* If row ends before first_changed_pos, it is unchanged,
17543 except in some case. */
17544 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17545 /* When row ends in ZV and we write at ZV it is not
17546 unchanged. */
17547 && !row->ends_at_zv_p
17548 /* When first_changed_pos is the end of a continued line,
17549 row is not unchanged because it may be no longer
17550 continued. */
17551 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17552 && (row->continued_p
17553 || row->exact_window_width_line_p))
17554 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17555 needs to be recomputed, so don't consider this row as
17556 unchanged. This happens when the last line was
17557 bidi-reordered and was killed immediately before this
17558 redisplay cycle. In that case, ROW->end stores the
17559 buffer position of the first visual-order character of
17560 the killed text, which is now beyond ZV. */
17561 && CHARPOS (row->end.pos) <= ZV)
17562 row_found = row;
17563
17564 /* Stop if last visible row. */
17565 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17566 break;
17567 }
17568
17569 return row_found;
17570 }
17571
17572
17573 /* Find the first glyph row in the current matrix of W that is not
17574 affected by changes at the end of current_buffer since the
17575 time W's current matrix was built.
17576
17577 Return in *DELTA the number of chars by which buffer positions in
17578 unchanged text at the end of current_buffer must be adjusted.
17579
17580 Return in *DELTA_BYTES the corresponding number of bytes.
17581
17582 Value is null if no such row exists, i.e. all rows are affected by
17583 changes. */
17584
17585 static struct glyph_row *
17586 find_first_unchanged_at_end_row (struct window *w,
17587 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17588 {
17589 struct glyph_row *row;
17590 struct glyph_row *row_found = NULL;
17591
17592 *delta = *delta_bytes = 0;
17593
17594 /* Display must not have been paused, otherwise the current matrix
17595 is not up to date. */
17596 eassert (w->window_end_valid);
17597
17598 /* A value of window_end_pos >= END_UNCHANGED means that the window
17599 end is in the range of changed text. If so, there is no
17600 unchanged row at the end of W's current matrix. */
17601 if (w->window_end_pos >= END_UNCHANGED)
17602 return NULL;
17603
17604 /* Set row to the last row in W's current matrix displaying text. */
17605 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17606
17607 /* If matrix is entirely empty, no unchanged row exists. */
17608 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17609 {
17610 /* The value of row is the last glyph row in the matrix having a
17611 meaningful buffer position in it. The end position of row
17612 corresponds to window_end_pos. This allows us to translate
17613 buffer positions in the current matrix to current buffer
17614 positions for characters not in changed text. */
17615 ptrdiff_t Z_old =
17616 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17617 ptrdiff_t Z_BYTE_old =
17618 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17619 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17620 struct glyph_row *first_text_row
17621 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17622
17623 *delta = Z - Z_old;
17624 *delta_bytes = Z_BYTE - Z_BYTE_old;
17625
17626 /* Set last_unchanged_pos to the buffer position of the last
17627 character in the buffer that has not been changed. Z is the
17628 index + 1 of the last character in current_buffer, i.e. by
17629 subtracting END_UNCHANGED we get the index of the last
17630 unchanged character, and we have to add BEG to get its buffer
17631 position. */
17632 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17633 last_unchanged_pos_old = last_unchanged_pos - *delta;
17634
17635 /* Search backward from ROW for a row displaying a line that
17636 starts at a minimum position >= last_unchanged_pos_old. */
17637 for (; row > first_text_row; --row)
17638 {
17639 /* This used to abort, but it can happen.
17640 It is ok to just stop the search instead here. KFS. */
17641 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17642 break;
17643
17644 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17645 row_found = row;
17646 }
17647 }
17648
17649 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17650
17651 return row_found;
17652 }
17653
17654
17655 /* Make sure that glyph rows in the current matrix of window W
17656 reference the same glyph memory as corresponding rows in the
17657 frame's frame matrix. This function is called after scrolling W's
17658 current matrix on a terminal frame in try_window_id and
17659 try_window_reusing_current_matrix. */
17660
17661 static void
17662 sync_frame_with_window_matrix_rows (struct window *w)
17663 {
17664 struct frame *f = XFRAME (w->frame);
17665 struct glyph_row *window_row, *window_row_end, *frame_row;
17666
17667 /* Preconditions: W must be a leaf window and full-width. Its frame
17668 must have a frame matrix. */
17669 eassert (BUFFERP (w->contents));
17670 eassert (WINDOW_FULL_WIDTH_P (w));
17671 eassert (!FRAME_WINDOW_P (f));
17672
17673 /* If W is a full-width window, glyph pointers in W's current matrix
17674 have, by definition, to be the same as glyph pointers in the
17675 corresponding frame matrix. Note that frame matrices have no
17676 marginal areas (see build_frame_matrix). */
17677 window_row = w->current_matrix->rows;
17678 window_row_end = window_row + w->current_matrix->nrows;
17679 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17680 while (window_row < window_row_end)
17681 {
17682 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17683 struct glyph *end = window_row->glyphs[LAST_AREA];
17684
17685 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17686 frame_row->glyphs[TEXT_AREA] = start;
17687 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17688 frame_row->glyphs[LAST_AREA] = end;
17689
17690 /* Disable frame rows whose corresponding window rows have
17691 been disabled in try_window_id. */
17692 if (!window_row->enabled_p)
17693 frame_row->enabled_p = false;
17694
17695 ++window_row, ++frame_row;
17696 }
17697 }
17698
17699
17700 /* Find the glyph row in window W containing CHARPOS. Consider all
17701 rows between START and END (not inclusive). END null means search
17702 all rows to the end of the display area of W. Value is the row
17703 containing CHARPOS or null. */
17704
17705 struct glyph_row *
17706 row_containing_pos (struct window *w, ptrdiff_t charpos,
17707 struct glyph_row *start, struct glyph_row *end, int dy)
17708 {
17709 struct glyph_row *row = start;
17710 struct glyph_row *best_row = NULL;
17711 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17712 int last_y;
17713
17714 /* If we happen to start on a header-line, skip that. */
17715 if (row->mode_line_p)
17716 ++row;
17717
17718 if ((end && row >= end) || !row->enabled_p)
17719 return NULL;
17720
17721 last_y = window_text_bottom_y (w) - dy;
17722
17723 while (true)
17724 {
17725 /* Give up if we have gone too far. */
17726 if (end && row >= end)
17727 return NULL;
17728 /* This formerly returned if they were equal.
17729 I think that both quantities are of a "last plus one" type;
17730 if so, when they are equal, the row is within the screen. -- rms. */
17731 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17732 return NULL;
17733
17734 /* If it is in this row, return this row. */
17735 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17736 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17737 /* The end position of a row equals the start
17738 position of the next row. If CHARPOS is there, we
17739 would rather consider it displayed in the next
17740 line, except when this line ends in ZV. */
17741 && !row_for_charpos_p (row, charpos)))
17742 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17743 {
17744 struct glyph *g;
17745
17746 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17747 || (!best_row && !row->continued_p))
17748 return row;
17749 /* In bidi-reordered rows, there could be several rows whose
17750 edges surround CHARPOS, all of these rows belonging to
17751 the same continued line. We need to find the row which
17752 fits CHARPOS the best. */
17753 for (g = row->glyphs[TEXT_AREA];
17754 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17755 g++)
17756 {
17757 if (!STRINGP (g->object))
17758 {
17759 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17760 {
17761 mindif = eabs (g->charpos - charpos);
17762 best_row = row;
17763 /* Exact match always wins. */
17764 if (mindif == 0)
17765 return best_row;
17766 }
17767 }
17768 }
17769 }
17770 else if (best_row && !row->continued_p)
17771 return best_row;
17772 ++row;
17773 }
17774 }
17775
17776
17777 /* Try to redisplay window W by reusing its existing display. W's
17778 current matrix must be up to date when this function is called,
17779 i.e., window_end_valid must be true.
17780
17781 Value is
17782
17783 >= 1 if successful, i.e. display has been updated
17784 specifically:
17785 1 means the changes were in front of a newline that precedes
17786 the window start, and the whole current matrix was reused
17787 2 means the changes were after the last position displayed
17788 in the window, and the whole current matrix was reused
17789 3 means portions of the current matrix were reused, while
17790 some of the screen lines were redrawn
17791 -1 if redisplay with same window start is known not to succeed
17792 0 if otherwise unsuccessful
17793
17794 The following steps are performed:
17795
17796 1. Find the last row in the current matrix of W that is not
17797 affected by changes at the start of current_buffer. If no such row
17798 is found, give up.
17799
17800 2. Find the first row in W's current matrix that is not affected by
17801 changes at the end of current_buffer. Maybe there is no such row.
17802
17803 3. Display lines beginning with the row + 1 found in step 1 to the
17804 row found in step 2 or, if step 2 didn't find a row, to the end of
17805 the window.
17806
17807 4. If cursor is not known to appear on the window, give up.
17808
17809 5. If display stopped at the row found in step 2, scroll the
17810 display and current matrix as needed.
17811
17812 6. Maybe display some lines at the end of W, if we must. This can
17813 happen under various circumstances, like a partially visible line
17814 becoming fully visible, or because newly displayed lines are displayed
17815 in smaller font sizes.
17816
17817 7. Update W's window end information. */
17818
17819 static int
17820 try_window_id (struct window *w)
17821 {
17822 struct frame *f = XFRAME (w->frame);
17823 struct glyph_matrix *current_matrix = w->current_matrix;
17824 struct glyph_matrix *desired_matrix = w->desired_matrix;
17825 struct glyph_row *last_unchanged_at_beg_row;
17826 struct glyph_row *first_unchanged_at_end_row;
17827 struct glyph_row *row;
17828 struct glyph_row *bottom_row;
17829 int bottom_vpos;
17830 struct it it;
17831 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17832 int dvpos, dy;
17833 struct text_pos start_pos;
17834 struct run run;
17835 int first_unchanged_at_end_vpos = 0;
17836 struct glyph_row *last_text_row, *last_text_row_at_end;
17837 struct text_pos start;
17838 ptrdiff_t first_changed_charpos, last_changed_charpos;
17839
17840 #ifdef GLYPH_DEBUG
17841 if (inhibit_try_window_id)
17842 return 0;
17843 #endif
17844
17845 /* This is handy for debugging. */
17846 #if false
17847 #define GIVE_UP(X) \
17848 do { \
17849 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
17850 return 0; \
17851 } while (false)
17852 #else
17853 #define GIVE_UP(X) return 0
17854 #endif
17855
17856 SET_TEXT_POS_FROM_MARKER (start, w->start);
17857
17858 /* Don't use this for mini-windows because these can show
17859 messages and mini-buffers, and we don't handle that here. */
17860 if (MINI_WINDOW_P (w))
17861 GIVE_UP (1);
17862
17863 /* This flag is used to prevent redisplay optimizations. */
17864 if (windows_or_buffers_changed || f->cursor_type_changed)
17865 GIVE_UP (2);
17866
17867 /* This function's optimizations cannot be used if overlays have
17868 changed in the buffer displayed by the window, so give up if they
17869 have. */
17870 if (w->last_overlay_modified != OVERLAY_MODIFF)
17871 GIVE_UP (200);
17872
17873 /* Verify that narrowing has not changed.
17874 Also verify that we were not told to prevent redisplay optimizations.
17875 It would be nice to further
17876 reduce the number of cases where this prevents try_window_id. */
17877 if (current_buffer->clip_changed
17878 || current_buffer->prevent_redisplay_optimizations_p)
17879 GIVE_UP (3);
17880
17881 /* Window must either use window-based redisplay or be full width. */
17882 if (!FRAME_WINDOW_P (f)
17883 && (!FRAME_LINE_INS_DEL_OK (f)
17884 || !WINDOW_FULL_WIDTH_P (w)))
17885 GIVE_UP (4);
17886
17887 /* Give up if point is known NOT to appear in W. */
17888 if (PT < CHARPOS (start))
17889 GIVE_UP (5);
17890
17891 /* Another way to prevent redisplay optimizations. */
17892 if (w->last_modified == 0)
17893 GIVE_UP (6);
17894
17895 /* Verify that window is not hscrolled. */
17896 if (w->hscroll != 0)
17897 GIVE_UP (7);
17898
17899 /* Verify that display wasn't paused. */
17900 if (!w->window_end_valid)
17901 GIVE_UP (8);
17902
17903 /* Likewise if highlighting trailing whitespace. */
17904 if (!NILP (Vshow_trailing_whitespace))
17905 GIVE_UP (11);
17906
17907 /* Can't use this if overlay arrow position and/or string have
17908 changed. */
17909 if (overlay_arrows_changed_p ())
17910 GIVE_UP (12);
17911
17912 /* When word-wrap is on, adding a space to the first word of a
17913 wrapped line can change the wrap position, altering the line
17914 above it. It might be worthwhile to handle this more
17915 intelligently, but for now just redisplay from scratch. */
17916 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17917 GIVE_UP (21);
17918
17919 /* Under bidi reordering, adding or deleting a character in the
17920 beginning of a paragraph, before the first strong directional
17921 character, can change the base direction of the paragraph (unless
17922 the buffer specifies a fixed paragraph direction), which will
17923 require to redisplay the whole paragraph. It might be worthwhile
17924 to find the paragraph limits and widen the range of redisplayed
17925 lines to that, but for now just give up this optimization and
17926 redisplay from scratch. */
17927 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17928 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17929 GIVE_UP (22);
17930
17931 /* Give up if the buffer has line-spacing set, as Lisp-level changes
17932 to that variable require thorough redisplay. */
17933 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
17934 GIVE_UP (23);
17935
17936 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17937 only if buffer has really changed. The reason is that the gap is
17938 initially at Z for freshly visited files. The code below would
17939 set end_unchanged to 0 in that case. */
17940 if (MODIFF > SAVE_MODIFF
17941 /* This seems to happen sometimes after saving a buffer. */
17942 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17943 {
17944 if (GPT - BEG < BEG_UNCHANGED)
17945 BEG_UNCHANGED = GPT - BEG;
17946 if (Z - GPT < END_UNCHANGED)
17947 END_UNCHANGED = Z - GPT;
17948 }
17949
17950 /* The position of the first and last character that has been changed. */
17951 first_changed_charpos = BEG + BEG_UNCHANGED;
17952 last_changed_charpos = Z - END_UNCHANGED;
17953
17954 /* If window starts after a line end, and the last change is in
17955 front of that newline, then changes don't affect the display.
17956 This case happens with stealth-fontification. Note that although
17957 the display is unchanged, glyph positions in the matrix have to
17958 be adjusted, of course. */
17959 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17960 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17961 && ((last_changed_charpos < CHARPOS (start)
17962 && CHARPOS (start) == BEGV)
17963 || (last_changed_charpos < CHARPOS (start) - 1
17964 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17965 {
17966 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17967 struct glyph_row *r0;
17968
17969 /* Compute how many chars/bytes have been added to or removed
17970 from the buffer. */
17971 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17972 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17973 Z_delta = Z - Z_old;
17974 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17975
17976 /* Give up if PT is not in the window. Note that it already has
17977 been checked at the start of try_window_id that PT is not in
17978 front of the window start. */
17979 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17980 GIVE_UP (13);
17981
17982 /* If window start is unchanged, we can reuse the whole matrix
17983 as is, after adjusting glyph positions. No need to compute
17984 the window end again, since its offset from Z hasn't changed. */
17985 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17986 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17987 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17988 /* PT must not be in a partially visible line. */
17989 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17990 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17991 {
17992 /* Adjust positions in the glyph matrix. */
17993 if (Z_delta || Z_delta_bytes)
17994 {
17995 struct glyph_row *r1
17996 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17997 increment_matrix_positions (w->current_matrix,
17998 MATRIX_ROW_VPOS (r0, current_matrix),
17999 MATRIX_ROW_VPOS (r1, current_matrix),
18000 Z_delta, Z_delta_bytes);
18001 }
18002
18003 /* Set the cursor. */
18004 row = row_containing_pos (w, PT, r0, NULL, 0);
18005 if (row)
18006 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18007 return 1;
18008 }
18009 }
18010
18011 /* Handle the case that changes are all below what is displayed in
18012 the window, and that PT is in the window. This shortcut cannot
18013 be taken if ZV is visible in the window, and text has been added
18014 there that is visible in the window. */
18015 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18016 /* ZV is not visible in the window, or there are no
18017 changes at ZV, actually. */
18018 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18019 || first_changed_charpos == last_changed_charpos))
18020 {
18021 struct glyph_row *r0;
18022
18023 /* Give up if PT is not in the window. Note that it already has
18024 been checked at the start of try_window_id that PT is not in
18025 front of the window start. */
18026 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18027 GIVE_UP (14);
18028
18029 /* If window start is unchanged, we can reuse the whole matrix
18030 as is, without changing glyph positions since no text has
18031 been added/removed in front of the window end. */
18032 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18033 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18034 /* PT must not be in a partially visible line. */
18035 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18036 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18037 {
18038 /* We have to compute the window end anew since text
18039 could have been added/removed after it. */
18040 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18041 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18042
18043 /* Set the cursor. */
18044 row = row_containing_pos (w, PT, r0, NULL, 0);
18045 if (row)
18046 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18047 return 2;
18048 }
18049 }
18050
18051 /* Give up if window start is in the changed area.
18052
18053 The condition used to read
18054
18055 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18056
18057 but why that was tested escapes me at the moment. */
18058 if (CHARPOS (start) >= first_changed_charpos
18059 && CHARPOS (start) <= last_changed_charpos)
18060 GIVE_UP (15);
18061
18062 /* Check that window start agrees with the start of the first glyph
18063 row in its current matrix. Check this after we know the window
18064 start is not in changed text, otherwise positions would not be
18065 comparable. */
18066 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18067 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18068 GIVE_UP (16);
18069
18070 /* Give up if the window ends in strings. Overlay strings
18071 at the end are difficult to handle, so don't try. */
18072 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18073 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18074 GIVE_UP (20);
18075
18076 /* Compute the position at which we have to start displaying new
18077 lines. Some of the lines at the top of the window might be
18078 reusable because they are not displaying changed text. Find the
18079 last row in W's current matrix not affected by changes at the
18080 start of current_buffer. Value is null if changes start in the
18081 first line of window. */
18082 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18083 if (last_unchanged_at_beg_row)
18084 {
18085 /* Avoid starting to display in the middle of a character, a TAB
18086 for instance. This is easier than to set up the iterator
18087 exactly, and it's not a frequent case, so the additional
18088 effort wouldn't really pay off. */
18089 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18090 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18091 && last_unchanged_at_beg_row > w->current_matrix->rows)
18092 --last_unchanged_at_beg_row;
18093
18094 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18095 GIVE_UP (17);
18096
18097 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18098 GIVE_UP (18);
18099 start_pos = it.current.pos;
18100
18101 /* Start displaying new lines in the desired matrix at the same
18102 vpos we would use in the current matrix, i.e. below
18103 last_unchanged_at_beg_row. */
18104 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18105 current_matrix);
18106 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18107 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18108
18109 eassert (it.hpos == 0 && it.current_x == 0);
18110 }
18111 else
18112 {
18113 /* There are no reusable lines at the start of the window.
18114 Start displaying in the first text line. */
18115 start_display (&it, w, start);
18116 it.vpos = it.first_vpos;
18117 start_pos = it.current.pos;
18118 }
18119
18120 /* Find the first row that is not affected by changes at the end of
18121 the buffer. Value will be null if there is no unchanged row, in
18122 which case we must redisplay to the end of the window. delta
18123 will be set to the value by which buffer positions beginning with
18124 first_unchanged_at_end_row have to be adjusted due to text
18125 changes. */
18126 first_unchanged_at_end_row
18127 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18128 IF_DEBUG (debug_delta = delta);
18129 IF_DEBUG (debug_delta_bytes = delta_bytes);
18130
18131 /* Set stop_pos to the buffer position up to which we will have to
18132 display new lines. If first_unchanged_at_end_row != NULL, this
18133 is the buffer position of the start of the line displayed in that
18134 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18135 that we don't stop at a buffer position. */
18136 stop_pos = 0;
18137 if (first_unchanged_at_end_row)
18138 {
18139 eassert (last_unchanged_at_beg_row == NULL
18140 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18141
18142 /* If this is a continuation line, move forward to the next one
18143 that isn't. Changes in lines above affect this line.
18144 Caution: this may move first_unchanged_at_end_row to a row
18145 not displaying text. */
18146 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18147 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18148 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18149 < it.last_visible_y))
18150 ++first_unchanged_at_end_row;
18151
18152 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18153 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18154 >= it.last_visible_y))
18155 first_unchanged_at_end_row = NULL;
18156 else
18157 {
18158 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18159 + delta);
18160 first_unchanged_at_end_vpos
18161 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18162 eassert (stop_pos >= Z - END_UNCHANGED);
18163 }
18164 }
18165 else if (last_unchanged_at_beg_row == NULL)
18166 GIVE_UP (19);
18167
18168
18169 #ifdef GLYPH_DEBUG
18170
18171 /* Either there is no unchanged row at the end, or the one we have
18172 now displays text. This is a necessary condition for the window
18173 end pos calculation at the end of this function. */
18174 eassert (first_unchanged_at_end_row == NULL
18175 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18176
18177 debug_last_unchanged_at_beg_vpos
18178 = (last_unchanged_at_beg_row
18179 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18180 : -1);
18181 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18182
18183 #endif /* GLYPH_DEBUG */
18184
18185
18186 /* Display new lines. Set last_text_row to the last new line
18187 displayed which has text on it, i.e. might end up as being the
18188 line where the window_end_vpos is. */
18189 w->cursor.vpos = -1;
18190 last_text_row = NULL;
18191 overlay_arrow_seen = false;
18192 if (it.current_y < it.last_visible_y
18193 && !f->fonts_changed
18194 && (first_unchanged_at_end_row == NULL
18195 || IT_CHARPOS (it) < stop_pos))
18196 it.glyph_row->reversed_p = false;
18197 while (it.current_y < it.last_visible_y
18198 && !f->fonts_changed
18199 && (first_unchanged_at_end_row == NULL
18200 || IT_CHARPOS (it) < stop_pos))
18201 {
18202 if (display_line (&it))
18203 last_text_row = it.glyph_row - 1;
18204 }
18205
18206 if (f->fonts_changed)
18207 return -1;
18208
18209 /* The redisplay iterations in display_line above could have
18210 triggered font-lock, which could have done something that
18211 invalidates IT->w window's end-point information, on which we
18212 rely below. E.g., one package, which will remain unnamed, used
18213 to install a font-lock-fontify-region-function that called
18214 bury-buffer, whose side effect is to switch the buffer displayed
18215 by IT->w, and that predictably resets IT->w's window_end_valid
18216 flag, which we already tested at the entry to this function.
18217 Amply punish such packages/modes by giving up on this
18218 optimization in those cases. */
18219 if (!w->window_end_valid)
18220 {
18221 clear_glyph_matrix (w->desired_matrix);
18222 return -1;
18223 }
18224
18225 /* Compute differences in buffer positions, y-positions etc. for
18226 lines reused at the bottom of the window. Compute what we can
18227 scroll. */
18228 if (first_unchanged_at_end_row
18229 /* No lines reused because we displayed everything up to the
18230 bottom of the window. */
18231 && it.current_y < it.last_visible_y)
18232 {
18233 dvpos = (it.vpos
18234 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18235 current_matrix));
18236 dy = it.current_y - first_unchanged_at_end_row->y;
18237 run.current_y = first_unchanged_at_end_row->y;
18238 run.desired_y = run.current_y + dy;
18239 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18240 }
18241 else
18242 {
18243 delta = delta_bytes = dvpos = dy
18244 = run.current_y = run.desired_y = run.height = 0;
18245 first_unchanged_at_end_row = NULL;
18246 }
18247 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18248
18249
18250 /* Find the cursor if not already found. We have to decide whether
18251 PT will appear on this window (it sometimes doesn't, but this is
18252 not a very frequent case.) This decision has to be made before
18253 the current matrix is altered. A value of cursor.vpos < 0 means
18254 that PT is either in one of the lines beginning at
18255 first_unchanged_at_end_row or below the window. Don't care for
18256 lines that might be displayed later at the window end; as
18257 mentioned, this is not a frequent case. */
18258 if (w->cursor.vpos < 0)
18259 {
18260 /* Cursor in unchanged rows at the top? */
18261 if (PT < CHARPOS (start_pos)
18262 && last_unchanged_at_beg_row)
18263 {
18264 row = row_containing_pos (w, PT,
18265 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18266 last_unchanged_at_beg_row + 1, 0);
18267 if (row)
18268 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18269 }
18270
18271 /* Start from first_unchanged_at_end_row looking for PT. */
18272 else if (first_unchanged_at_end_row)
18273 {
18274 row = row_containing_pos (w, PT - delta,
18275 first_unchanged_at_end_row, NULL, 0);
18276 if (row)
18277 set_cursor_from_row (w, row, w->current_matrix, delta,
18278 delta_bytes, dy, dvpos);
18279 }
18280
18281 /* Give up if cursor was not found. */
18282 if (w->cursor.vpos < 0)
18283 {
18284 clear_glyph_matrix (w->desired_matrix);
18285 return -1;
18286 }
18287 }
18288
18289 /* Don't let the cursor end in the scroll margins. */
18290 {
18291 int this_scroll_margin, cursor_height;
18292 int frame_line_height = default_line_pixel_height (w);
18293 int window_total_lines
18294 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18295
18296 this_scroll_margin =
18297 max (0, min (scroll_margin, window_total_lines / 4));
18298 this_scroll_margin *= frame_line_height;
18299 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18300
18301 if ((w->cursor.y < this_scroll_margin
18302 && CHARPOS (start) > BEGV)
18303 /* Old redisplay didn't take scroll margin into account at the bottom,
18304 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18305 || (w->cursor.y + (make_cursor_line_fully_visible_p
18306 ? cursor_height + this_scroll_margin
18307 : 1)) > it.last_visible_y)
18308 {
18309 w->cursor.vpos = -1;
18310 clear_glyph_matrix (w->desired_matrix);
18311 return -1;
18312 }
18313 }
18314
18315 /* Scroll the display. Do it before changing the current matrix so
18316 that xterm.c doesn't get confused about where the cursor glyph is
18317 found. */
18318 if (dy && run.height)
18319 {
18320 update_begin (f);
18321
18322 if (FRAME_WINDOW_P (f))
18323 {
18324 FRAME_RIF (f)->update_window_begin_hook (w);
18325 FRAME_RIF (f)->clear_window_mouse_face (w);
18326 FRAME_RIF (f)->scroll_run_hook (w, &run);
18327 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18328 }
18329 else
18330 {
18331 /* Terminal frame. In this case, dvpos gives the number of
18332 lines to scroll by; dvpos < 0 means scroll up. */
18333 int from_vpos
18334 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18335 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18336 int end = (WINDOW_TOP_EDGE_LINE (w)
18337 + WINDOW_WANTS_HEADER_LINE_P (w)
18338 + window_internal_height (w));
18339
18340 #if defined (HAVE_GPM) || defined (MSDOS)
18341 x_clear_window_mouse_face (w);
18342 #endif
18343 /* Perform the operation on the screen. */
18344 if (dvpos > 0)
18345 {
18346 /* Scroll last_unchanged_at_beg_row to the end of the
18347 window down dvpos lines. */
18348 set_terminal_window (f, end);
18349
18350 /* On dumb terminals delete dvpos lines at the end
18351 before inserting dvpos empty lines. */
18352 if (!FRAME_SCROLL_REGION_OK (f))
18353 ins_del_lines (f, end - dvpos, -dvpos);
18354
18355 /* Insert dvpos empty lines in front of
18356 last_unchanged_at_beg_row. */
18357 ins_del_lines (f, from, dvpos);
18358 }
18359 else if (dvpos < 0)
18360 {
18361 /* Scroll up last_unchanged_at_beg_vpos to the end of
18362 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18363 set_terminal_window (f, end);
18364
18365 /* Delete dvpos lines in front of
18366 last_unchanged_at_beg_vpos. ins_del_lines will set
18367 the cursor to the given vpos and emit |dvpos| delete
18368 line sequences. */
18369 ins_del_lines (f, from + dvpos, dvpos);
18370
18371 /* On a dumb terminal insert dvpos empty lines at the
18372 end. */
18373 if (!FRAME_SCROLL_REGION_OK (f))
18374 ins_del_lines (f, end + dvpos, -dvpos);
18375 }
18376
18377 set_terminal_window (f, 0);
18378 }
18379
18380 update_end (f);
18381 }
18382
18383 /* Shift reused rows of the current matrix to the right position.
18384 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18385 text. */
18386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18387 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18388 if (dvpos < 0)
18389 {
18390 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18391 bottom_vpos, dvpos);
18392 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18393 bottom_vpos);
18394 }
18395 else if (dvpos > 0)
18396 {
18397 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18398 bottom_vpos, dvpos);
18399 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18400 first_unchanged_at_end_vpos + dvpos);
18401 }
18402
18403 /* For frame-based redisplay, make sure that current frame and window
18404 matrix are in sync with respect to glyph memory. */
18405 if (!FRAME_WINDOW_P (f))
18406 sync_frame_with_window_matrix_rows (w);
18407
18408 /* Adjust buffer positions in reused rows. */
18409 if (delta || delta_bytes)
18410 increment_matrix_positions (current_matrix,
18411 first_unchanged_at_end_vpos + dvpos,
18412 bottom_vpos, delta, delta_bytes);
18413
18414 /* Adjust Y positions. */
18415 if (dy)
18416 shift_glyph_matrix (w, current_matrix,
18417 first_unchanged_at_end_vpos + dvpos,
18418 bottom_vpos, dy);
18419
18420 if (first_unchanged_at_end_row)
18421 {
18422 first_unchanged_at_end_row += dvpos;
18423 if (first_unchanged_at_end_row->y >= it.last_visible_y
18424 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18425 first_unchanged_at_end_row = NULL;
18426 }
18427
18428 /* If scrolling up, there may be some lines to display at the end of
18429 the window. */
18430 last_text_row_at_end = NULL;
18431 if (dy < 0)
18432 {
18433 /* Scrolling up can leave for example a partially visible line
18434 at the end of the window to be redisplayed. */
18435 /* Set last_row to the glyph row in the current matrix where the
18436 window end line is found. It has been moved up or down in
18437 the matrix by dvpos. */
18438 int last_vpos = w->window_end_vpos + dvpos;
18439 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18440
18441 /* If last_row is the window end line, it should display text. */
18442 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18443
18444 /* If window end line was partially visible before, begin
18445 displaying at that line. Otherwise begin displaying with the
18446 line following it. */
18447 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18448 {
18449 init_to_row_start (&it, w, last_row);
18450 it.vpos = last_vpos;
18451 it.current_y = last_row->y;
18452 }
18453 else
18454 {
18455 init_to_row_end (&it, w, last_row);
18456 it.vpos = 1 + last_vpos;
18457 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18458 ++last_row;
18459 }
18460
18461 /* We may start in a continuation line. If so, we have to
18462 get the right continuation_lines_width and current_x. */
18463 it.continuation_lines_width = last_row->continuation_lines_width;
18464 it.hpos = it.current_x = 0;
18465
18466 /* Display the rest of the lines at the window end. */
18467 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18468 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18469 {
18470 /* Is it always sure that the display agrees with lines in
18471 the current matrix? I don't think so, so we mark rows
18472 displayed invalid in the current matrix by setting their
18473 enabled_p flag to false. */
18474 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18475 if (display_line (&it))
18476 last_text_row_at_end = it.glyph_row - 1;
18477 }
18478 }
18479
18480 /* Update window_end_pos and window_end_vpos. */
18481 if (first_unchanged_at_end_row && !last_text_row_at_end)
18482 {
18483 /* Window end line if one of the preserved rows from the current
18484 matrix. Set row to the last row displaying text in current
18485 matrix starting at first_unchanged_at_end_row, after
18486 scrolling. */
18487 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18488 row = find_last_row_displaying_text (w->current_matrix, &it,
18489 first_unchanged_at_end_row);
18490 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18491 adjust_window_ends (w, row, true);
18492 eassert (w->window_end_bytepos >= 0);
18493 IF_DEBUG (debug_method_add (w, "A"));
18494 }
18495 else if (last_text_row_at_end)
18496 {
18497 adjust_window_ends (w, last_text_row_at_end, false);
18498 eassert (w->window_end_bytepos >= 0);
18499 IF_DEBUG (debug_method_add (w, "B"));
18500 }
18501 else if (last_text_row)
18502 {
18503 /* We have displayed either to the end of the window or at the
18504 end of the window, i.e. the last row with text is to be found
18505 in the desired matrix. */
18506 adjust_window_ends (w, last_text_row, false);
18507 eassert (w->window_end_bytepos >= 0);
18508 }
18509 else if (first_unchanged_at_end_row == NULL
18510 && last_text_row == NULL
18511 && last_text_row_at_end == NULL)
18512 {
18513 /* Displayed to end of window, but no line containing text was
18514 displayed. Lines were deleted at the end of the window. */
18515 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18516 int vpos = w->window_end_vpos;
18517 struct glyph_row *current_row = current_matrix->rows + vpos;
18518 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18519
18520 for (row = NULL;
18521 row == NULL && vpos >= first_vpos;
18522 --vpos, --current_row, --desired_row)
18523 {
18524 if (desired_row->enabled_p)
18525 {
18526 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18527 row = desired_row;
18528 }
18529 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18530 row = current_row;
18531 }
18532
18533 eassert (row != NULL);
18534 w->window_end_vpos = vpos + 1;
18535 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18536 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18537 eassert (w->window_end_bytepos >= 0);
18538 IF_DEBUG (debug_method_add (w, "C"));
18539 }
18540 else
18541 emacs_abort ();
18542
18543 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18544 debug_end_vpos = w->window_end_vpos));
18545
18546 /* Record that display has not been completed. */
18547 w->window_end_valid = false;
18548 w->desired_matrix->no_scrolling_p = true;
18549 return 3;
18550
18551 #undef GIVE_UP
18552 }
18553
18554
18555 \f
18556 /***********************************************************************
18557 More debugging support
18558 ***********************************************************************/
18559
18560 #ifdef GLYPH_DEBUG
18561
18562 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18563 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18564 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18565
18566
18567 /* Dump the contents of glyph matrix MATRIX on stderr.
18568
18569 GLYPHS 0 means don't show glyph contents.
18570 GLYPHS 1 means show glyphs in short form
18571 GLYPHS > 1 means show glyphs in long form. */
18572
18573 void
18574 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18575 {
18576 int i;
18577 for (i = 0; i < matrix->nrows; ++i)
18578 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18579 }
18580
18581
18582 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18583 the glyph row and area where the glyph comes from. */
18584
18585 void
18586 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18587 {
18588 if (glyph->type == CHAR_GLYPH
18589 || glyph->type == GLYPHLESS_GLYPH)
18590 {
18591 fprintf (stderr,
18592 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18593 glyph - row->glyphs[TEXT_AREA],
18594 (glyph->type == CHAR_GLYPH
18595 ? 'C'
18596 : 'G'),
18597 glyph->charpos,
18598 (BUFFERP (glyph->object)
18599 ? 'B'
18600 : (STRINGP (glyph->object)
18601 ? 'S'
18602 : (NILP (glyph->object)
18603 ? '0'
18604 : '-'))),
18605 glyph->pixel_width,
18606 glyph->u.ch,
18607 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18608 ? glyph->u.ch
18609 : '.'),
18610 glyph->face_id,
18611 glyph->left_box_line_p,
18612 glyph->right_box_line_p);
18613 }
18614 else if (glyph->type == STRETCH_GLYPH)
18615 {
18616 fprintf (stderr,
18617 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18618 glyph - row->glyphs[TEXT_AREA],
18619 'S',
18620 glyph->charpos,
18621 (BUFFERP (glyph->object)
18622 ? 'B'
18623 : (STRINGP (glyph->object)
18624 ? 'S'
18625 : (NILP (glyph->object)
18626 ? '0'
18627 : '-'))),
18628 glyph->pixel_width,
18629 0,
18630 ' ',
18631 glyph->face_id,
18632 glyph->left_box_line_p,
18633 glyph->right_box_line_p);
18634 }
18635 else if (glyph->type == IMAGE_GLYPH)
18636 {
18637 fprintf (stderr,
18638 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18639 glyph - row->glyphs[TEXT_AREA],
18640 'I',
18641 glyph->charpos,
18642 (BUFFERP (glyph->object)
18643 ? 'B'
18644 : (STRINGP (glyph->object)
18645 ? 'S'
18646 : (NILP (glyph->object)
18647 ? '0'
18648 : '-'))),
18649 glyph->pixel_width,
18650 glyph->u.img_id,
18651 '.',
18652 glyph->face_id,
18653 glyph->left_box_line_p,
18654 glyph->right_box_line_p);
18655 }
18656 else if (glyph->type == COMPOSITE_GLYPH)
18657 {
18658 fprintf (stderr,
18659 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18660 glyph - row->glyphs[TEXT_AREA],
18661 '+',
18662 glyph->charpos,
18663 (BUFFERP (glyph->object)
18664 ? 'B'
18665 : (STRINGP (glyph->object)
18666 ? 'S'
18667 : (NILP (glyph->object)
18668 ? '0'
18669 : '-'))),
18670 glyph->pixel_width,
18671 glyph->u.cmp.id);
18672 if (glyph->u.cmp.automatic)
18673 fprintf (stderr,
18674 "[%d-%d]",
18675 glyph->slice.cmp.from, glyph->slice.cmp.to);
18676 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18677 glyph->face_id,
18678 glyph->left_box_line_p,
18679 glyph->right_box_line_p);
18680 }
18681 }
18682
18683
18684 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18685 GLYPHS 0 means don't show glyph contents.
18686 GLYPHS 1 means show glyphs in short form
18687 GLYPHS > 1 means show glyphs in long form. */
18688
18689 void
18690 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18691 {
18692 if (glyphs != 1)
18693 {
18694 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18695 fprintf (stderr, "==============================================================================\n");
18696
18697 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18698 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18699 vpos,
18700 MATRIX_ROW_START_CHARPOS (row),
18701 MATRIX_ROW_END_CHARPOS (row),
18702 row->used[TEXT_AREA],
18703 row->contains_overlapping_glyphs_p,
18704 row->enabled_p,
18705 row->truncated_on_left_p,
18706 row->truncated_on_right_p,
18707 row->continued_p,
18708 MATRIX_ROW_CONTINUATION_LINE_P (row),
18709 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18710 row->ends_at_zv_p,
18711 row->fill_line_p,
18712 row->ends_in_middle_of_char_p,
18713 row->starts_in_middle_of_char_p,
18714 row->mouse_face_p,
18715 row->x,
18716 row->y,
18717 row->pixel_width,
18718 row->height,
18719 row->visible_height,
18720 row->ascent,
18721 row->phys_ascent);
18722 /* The next 3 lines should align to "Start" in the header. */
18723 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18724 row->end.overlay_string_index,
18725 row->continuation_lines_width);
18726 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18727 CHARPOS (row->start.string_pos),
18728 CHARPOS (row->end.string_pos));
18729 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18730 row->end.dpvec_index);
18731 }
18732
18733 if (glyphs > 1)
18734 {
18735 int area;
18736
18737 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18738 {
18739 struct glyph *glyph = row->glyphs[area];
18740 struct glyph *glyph_end = glyph + row->used[area];
18741
18742 /* Glyph for a line end in text. */
18743 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18744 ++glyph_end;
18745
18746 if (glyph < glyph_end)
18747 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18748
18749 for (; glyph < glyph_end; ++glyph)
18750 dump_glyph (row, glyph, area);
18751 }
18752 }
18753 else if (glyphs == 1)
18754 {
18755 int area;
18756 char s[SHRT_MAX + 4];
18757
18758 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18759 {
18760 int i;
18761
18762 for (i = 0; i < row->used[area]; ++i)
18763 {
18764 struct glyph *glyph = row->glyphs[area] + i;
18765 if (i == row->used[area] - 1
18766 && area == TEXT_AREA
18767 && NILP (glyph->object)
18768 && glyph->type == CHAR_GLYPH
18769 && glyph->u.ch == ' ')
18770 {
18771 strcpy (&s[i], "[\\n]");
18772 i += 4;
18773 }
18774 else if (glyph->type == CHAR_GLYPH
18775 && glyph->u.ch < 0x80
18776 && glyph->u.ch >= ' ')
18777 s[i] = glyph->u.ch;
18778 else
18779 s[i] = '.';
18780 }
18781
18782 s[i] = '\0';
18783 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18784 }
18785 }
18786 }
18787
18788
18789 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18790 Sdump_glyph_matrix, 0, 1, "p",
18791 doc: /* Dump the current matrix of the selected window to stderr.
18792 Shows contents of glyph row structures. With non-nil
18793 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18794 glyphs in short form, otherwise show glyphs in long form.
18795
18796 Interactively, no argument means show glyphs in short form;
18797 with numeric argument, its value is passed as the GLYPHS flag. */)
18798 (Lisp_Object glyphs)
18799 {
18800 struct window *w = XWINDOW (selected_window);
18801 struct buffer *buffer = XBUFFER (w->contents);
18802
18803 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18804 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18805 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18806 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18807 fprintf (stderr, "=============================================\n");
18808 dump_glyph_matrix (w->current_matrix,
18809 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18810 return Qnil;
18811 }
18812
18813
18814 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18815 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18816 Only text-mode frames have frame glyph matrices. */)
18817 (void)
18818 {
18819 struct frame *f = XFRAME (selected_frame);
18820
18821 if (f->current_matrix)
18822 dump_glyph_matrix (f->current_matrix, 1);
18823 else
18824 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18825 return Qnil;
18826 }
18827
18828
18829 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18830 doc: /* Dump glyph row ROW to stderr.
18831 GLYPH 0 means don't dump glyphs.
18832 GLYPH 1 means dump glyphs in short form.
18833 GLYPH > 1 or omitted means dump glyphs in long form. */)
18834 (Lisp_Object row, Lisp_Object glyphs)
18835 {
18836 struct glyph_matrix *matrix;
18837 EMACS_INT vpos;
18838
18839 CHECK_NUMBER (row);
18840 matrix = XWINDOW (selected_window)->current_matrix;
18841 vpos = XINT (row);
18842 if (vpos >= 0 && vpos < matrix->nrows)
18843 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18844 vpos,
18845 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18846 return Qnil;
18847 }
18848
18849
18850 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18851 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18852 GLYPH 0 means don't dump glyphs.
18853 GLYPH 1 means dump glyphs in short form.
18854 GLYPH > 1 or omitted means dump glyphs in long form.
18855
18856 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18857 do nothing. */)
18858 (Lisp_Object row, Lisp_Object glyphs)
18859 {
18860 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18861 struct frame *sf = SELECTED_FRAME ();
18862 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18863 EMACS_INT vpos;
18864
18865 CHECK_NUMBER (row);
18866 vpos = XINT (row);
18867 if (vpos >= 0 && vpos < m->nrows)
18868 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18869 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18870 #endif
18871 return Qnil;
18872 }
18873
18874
18875 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18876 doc: /* Toggle tracing of redisplay.
18877 With ARG, turn tracing on if and only if ARG is positive. */)
18878 (Lisp_Object arg)
18879 {
18880 if (NILP (arg))
18881 trace_redisplay_p = !trace_redisplay_p;
18882 else
18883 {
18884 arg = Fprefix_numeric_value (arg);
18885 trace_redisplay_p = XINT (arg) > 0;
18886 }
18887
18888 return Qnil;
18889 }
18890
18891
18892 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18893 doc: /* Like `format', but print result to stderr.
18894 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18895 (ptrdiff_t nargs, Lisp_Object *args)
18896 {
18897 Lisp_Object s = Fformat (nargs, args);
18898 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18899 return Qnil;
18900 }
18901
18902 #endif /* GLYPH_DEBUG */
18903
18904
18905 \f
18906 /***********************************************************************
18907 Building Desired Matrix Rows
18908 ***********************************************************************/
18909
18910 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18911 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18912
18913 static struct glyph_row *
18914 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18915 {
18916 struct frame *f = XFRAME (WINDOW_FRAME (w));
18917 struct buffer *buffer = XBUFFER (w->contents);
18918 struct buffer *old = current_buffer;
18919 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18920 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18921 const unsigned char *arrow_end = arrow_string + arrow_len;
18922 const unsigned char *p;
18923 struct it it;
18924 bool multibyte_p;
18925 int n_glyphs_before;
18926
18927 set_buffer_temp (buffer);
18928 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18929 scratch_glyph_row.reversed_p = false;
18930 it.glyph_row->used[TEXT_AREA] = 0;
18931 SET_TEXT_POS (it.position, 0, 0);
18932
18933 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18934 p = arrow_string;
18935 while (p < arrow_end)
18936 {
18937 Lisp_Object face, ilisp;
18938
18939 /* Get the next character. */
18940 if (multibyte_p)
18941 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18942 else
18943 {
18944 it.c = it.char_to_display = *p, it.len = 1;
18945 if (! ASCII_CHAR_P (it.c))
18946 it.char_to_display = BYTE8_TO_CHAR (it.c);
18947 }
18948 p += it.len;
18949
18950 /* Get its face. */
18951 ilisp = make_number (p - arrow_string);
18952 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18953 it.face_id = compute_char_face (f, it.char_to_display, face);
18954
18955 /* Compute its width, get its glyphs. */
18956 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18957 SET_TEXT_POS (it.position, -1, -1);
18958 PRODUCE_GLYPHS (&it);
18959
18960 /* If this character doesn't fit any more in the line, we have
18961 to remove some glyphs. */
18962 if (it.current_x > it.last_visible_x)
18963 {
18964 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18965 break;
18966 }
18967 }
18968
18969 set_buffer_temp (old);
18970 return it.glyph_row;
18971 }
18972
18973
18974 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18975 glyphs to insert is determined by produce_special_glyphs. */
18976
18977 static void
18978 insert_left_trunc_glyphs (struct it *it)
18979 {
18980 struct it truncate_it;
18981 struct glyph *from, *end, *to, *toend;
18982
18983 eassert (!FRAME_WINDOW_P (it->f)
18984 || (!it->glyph_row->reversed_p
18985 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18986 || (it->glyph_row->reversed_p
18987 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18988
18989 /* Get the truncation glyphs. */
18990 truncate_it = *it;
18991 truncate_it.current_x = 0;
18992 truncate_it.face_id = DEFAULT_FACE_ID;
18993 truncate_it.glyph_row = &scratch_glyph_row;
18994 truncate_it.area = TEXT_AREA;
18995 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18996 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18997 truncate_it.object = Qnil;
18998 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18999
19000 /* Overwrite glyphs from IT with truncation glyphs. */
19001 if (!it->glyph_row->reversed_p)
19002 {
19003 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19004
19005 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19006 end = from + tused;
19007 to = it->glyph_row->glyphs[TEXT_AREA];
19008 toend = to + it->glyph_row->used[TEXT_AREA];
19009 if (FRAME_WINDOW_P (it->f))
19010 {
19011 /* On GUI frames, when variable-size fonts are displayed,
19012 the truncation glyphs may need more pixels than the row's
19013 glyphs they overwrite. We overwrite more glyphs to free
19014 enough screen real estate, and enlarge the stretch glyph
19015 on the right (see display_line), if there is one, to
19016 preserve the screen position of the truncation glyphs on
19017 the right. */
19018 int w = 0;
19019 struct glyph *g = to;
19020 short used;
19021
19022 /* The first glyph could be partially visible, in which case
19023 it->glyph_row->x will be negative. But we want the left
19024 truncation glyphs to be aligned at the left margin of the
19025 window, so we override the x coordinate at which the row
19026 will begin. */
19027 it->glyph_row->x = 0;
19028 while (g < toend && w < it->truncation_pixel_width)
19029 {
19030 w += g->pixel_width;
19031 ++g;
19032 }
19033 if (g - to - tused > 0)
19034 {
19035 memmove (to + tused, g, (toend - g) * sizeof(*g));
19036 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19037 }
19038 used = it->glyph_row->used[TEXT_AREA];
19039 if (it->glyph_row->truncated_on_right_p
19040 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19041 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19042 == STRETCH_GLYPH)
19043 {
19044 int extra = w - it->truncation_pixel_width;
19045
19046 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19047 }
19048 }
19049
19050 while (from < end)
19051 *to++ = *from++;
19052
19053 /* There may be padding glyphs left over. Overwrite them too. */
19054 if (!FRAME_WINDOW_P (it->f))
19055 {
19056 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19057 {
19058 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19059 while (from < end)
19060 *to++ = *from++;
19061 }
19062 }
19063
19064 if (to > toend)
19065 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19066 }
19067 else
19068 {
19069 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19070
19071 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19072 that back to front. */
19073 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19074 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19075 toend = it->glyph_row->glyphs[TEXT_AREA];
19076 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19077 if (FRAME_WINDOW_P (it->f))
19078 {
19079 int w = 0;
19080 struct glyph *g = to;
19081
19082 while (g >= toend && w < it->truncation_pixel_width)
19083 {
19084 w += g->pixel_width;
19085 --g;
19086 }
19087 if (to - g - tused > 0)
19088 to = g + tused;
19089 if (it->glyph_row->truncated_on_right_p
19090 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19091 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19092 {
19093 int extra = w - it->truncation_pixel_width;
19094
19095 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19096 }
19097 }
19098
19099 while (from >= end && to >= toend)
19100 *to-- = *from--;
19101 if (!FRAME_WINDOW_P (it->f))
19102 {
19103 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19104 {
19105 from =
19106 truncate_it.glyph_row->glyphs[TEXT_AREA]
19107 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19108 while (from >= end && to >= toend)
19109 *to-- = *from--;
19110 }
19111 }
19112 if (from >= end)
19113 {
19114 /* Need to free some room before prepending additional
19115 glyphs. */
19116 int move_by = from - end + 1;
19117 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19118 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19119
19120 for ( ; g >= g0; g--)
19121 g[move_by] = *g;
19122 while (from >= end)
19123 *to-- = *from--;
19124 it->glyph_row->used[TEXT_AREA] += move_by;
19125 }
19126 }
19127 }
19128
19129 /* Compute the hash code for ROW. */
19130 unsigned
19131 row_hash (struct glyph_row *row)
19132 {
19133 int area, k;
19134 unsigned hashval = 0;
19135
19136 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19137 for (k = 0; k < row->used[area]; ++k)
19138 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19139 + row->glyphs[area][k].u.val
19140 + row->glyphs[area][k].face_id
19141 + row->glyphs[area][k].padding_p
19142 + (row->glyphs[area][k].type << 2));
19143
19144 return hashval;
19145 }
19146
19147 /* Compute the pixel height and width of IT->glyph_row.
19148
19149 Most of the time, ascent and height of a display line will be equal
19150 to the max_ascent and max_height values of the display iterator
19151 structure. This is not the case if
19152
19153 1. We hit ZV without displaying anything. In this case, max_ascent
19154 and max_height will be zero.
19155
19156 2. We have some glyphs that don't contribute to the line height.
19157 (The glyph row flag contributes_to_line_height_p is for future
19158 pixmap extensions).
19159
19160 The first case is easily covered by using default values because in
19161 these cases, the line height does not really matter, except that it
19162 must not be zero. */
19163
19164 static void
19165 compute_line_metrics (struct it *it)
19166 {
19167 struct glyph_row *row = it->glyph_row;
19168
19169 if (FRAME_WINDOW_P (it->f))
19170 {
19171 int i, min_y, max_y;
19172
19173 /* The line may consist of one space only, that was added to
19174 place the cursor on it. If so, the row's height hasn't been
19175 computed yet. */
19176 if (row->height == 0)
19177 {
19178 if (it->max_ascent + it->max_descent == 0)
19179 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19180 row->ascent = it->max_ascent;
19181 row->height = it->max_ascent + it->max_descent;
19182 row->phys_ascent = it->max_phys_ascent;
19183 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19184 row->extra_line_spacing = it->max_extra_line_spacing;
19185 }
19186
19187 /* Compute the width of this line. */
19188 row->pixel_width = row->x;
19189 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19190 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19191
19192 eassert (row->pixel_width >= 0);
19193 eassert (row->ascent >= 0 && row->height > 0);
19194
19195 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19196 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19197
19198 /* If first line's physical ascent is larger than its logical
19199 ascent, use the physical ascent, and make the row taller.
19200 This makes accented characters fully visible. */
19201 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19202 && row->phys_ascent > row->ascent)
19203 {
19204 row->height += row->phys_ascent - row->ascent;
19205 row->ascent = row->phys_ascent;
19206 }
19207
19208 /* Compute how much of the line is visible. */
19209 row->visible_height = row->height;
19210
19211 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19212 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19213
19214 if (row->y < min_y)
19215 row->visible_height -= min_y - row->y;
19216 if (row->y + row->height > max_y)
19217 row->visible_height -= row->y + row->height - max_y;
19218 }
19219 else
19220 {
19221 row->pixel_width = row->used[TEXT_AREA];
19222 if (row->continued_p)
19223 row->pixel_width -= it->continuation_pixel_width;
19224 else if (row->truncated_on_right_p)
19225 row->pixel_width -= it->truncation_pixel_width;
19226 row->ascent = row->phys_ascent = 0;
19227 row->height = row->phys_height = row->visible_height = 1;
19228 row->extra_line_spacing = 0;
19229 }
19230
19231 /* Compute a hash code for this row. */
19232 row->hash = row_hash (row);
19233
19234 it->max_ascent = it->max_descent = 0;
19235 it->max_phys_ascent = it->max_phys_descent = 0;
19236 }
19237
19238
19239 /* Append one space to the glyph row of iterator IT if doing a
19240 window-based redisplay. The space has the same face as
19241 IT->face_id. Value is true if a space was added.
19242
19243 This function is called to make sure that there is always one glyph
19244 at the end of a glyph row that the cursor can be set on under
19245 window-systems. (If there weren't such a glyph we would not know
19246 how wide and tall a box cursor should be displayed).
19247
19248 At the same time this space let's a nicely handle clearing to the
19249 end of the line if the row ends in italic text. */
19250
19251 static bool
19252 append_space_for_newline (struct it *it, bool default_face_p)
19253 {
19254 if (FRAME_WINDOW_P (it->f))
19255 {
19256 int n = it->glyph_row->used[TEXT_AREA];
19257
19258 if (it->glyph_row->glyphs[TEXT_AREA] + n
19259 < it->glyph_row->glyphs[1 + TEXT_AREA])
19260 {
19261 /* Save some values that must not be changed.
19262 Must save IT->c and IT->len because otherwise
19263 ITERATOR_AT_END_P wouldn't work anymore after
19264 append_space_for_newline has been called. */
19265 enum display_element_type saved_what = it->what;
19266 int saved_c = it->c, saved_len = it->len;
19267 int saved_char_to_display = it->char_to_display;
19268 int saved_x = it->current_x;
19269 int saved_face_id = it->face_id;
19270 bool saved_box_end = it->end_of_box_run_p;
19271 struct text_pos saved_pos;
19272 Lisp_Object saved_object;
19273 struct face *face;
19274 struct glyph *g;
19275
19276 saved_object = it->object;
19277 saved_pos = it->position;
19278
19279 it->what = IT_CHARACTER;
19280 memset (&it->position, 0, sizeof it->position);
19281 it->object = Qnil;
19282 it->c = it->char_to_display = ' ';
19283 it->len = 1;
19284
19285 /* If the default face was remapped, be sure to use the
19286 remapped face for the appended newline. */
19287 if (default_face_p)
19288 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19289 else if (it->face_before_selective_p)
19290 it->face_id = it->saved_face_id;
19291 face = FACE_FROM_ID (it->f, it->face_id);
19292 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19293 /* In R2L rows, we will prepend a stretch glyph that will
19294 have the end_of_box_run_p flag set for it, so there's no
19295 need for the appended newline glyph to have that flag
19296 set. */
19297 if (it->glyph_row->reversed_p
19298 /* But if the appended newline glyph goes all the way to
19299 the end of the row, there will be no stretch glyph,
19300 so leave the box flag set. */
19301 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19302 it->end_of_box_run_p = false;
19303
19304 PRODUCE_GLYPHS (it);
19305
19306 #ifdef HAVE_WINDOW_SYSTEM
19307 /* Make sure this space glyph has the right ascent and
19308 descent values, or else cursor at end of line will look
19309 funny, and height of empty lines will be incorrect. */
19310 g = it->glyph_row->glyphs[TEXT_AREA] + n;
19311 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19312 if (n == 0)
19313 {
19314 Lisp_Object height, total_height;
19315 int extra_line_spacing = it->extra_line_spacing;
19316 int boff = font->baseline_offset;
19317
19318 if (font->vertical_centering)
19319 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19320
19321 it->object = saved_object; /* get_it_property needs this */
19322 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19323 /* Must do a subset of line height processing from
19324 x_produce_glyph for newline characters. */
19325 height = get_it_property (it, Qline_height);
19326 if (CONSP (height)
19327 && CONSP (XCDR (height))
19328 && NILP (XCDR (XCDR (height))))
19329 {
19330 total_height = XCAR (XCDR (height));
19331 height = XCAR (height);
19332 }
19333 else
19334 total_height = Qnil;
19335 height = calc_line_height_property (it, height, font, boff, true);
19336
19337 if (it->override_ascent >= 0)
19338 {
19339 it->ascent = it->override_ascent;
19340 it->descent = it->override_descent;
19341 boff = it->override_boff;
19342 }
19343 if (EQ (height, Qt))
19344 extra_line_spacing = 0;
19345 else
19346 {
19347 Lisp_Object spacing;
19348
19349 it->phys_ascent = it->ascent;
19350 it->phys_descent = it->descent;
19351 if (!NILP (height)
19352 && XINT (height) > it->ascent + it->descent)
19353 it->ascent = XINT (height) - it->descent;
19354
19355 if (!NILP (total_height))
19356 spacing = calc_line_height_property (it, total_height, font,
19357 boff, false);
19358 else
19359 {
19360 spacing = get_it_property (it, Qline_spacing);
19361 spacing = calc_line_height_property (it, spacing, font,
19362 boff, false);
19363 }
19364 if (INTEGERP (spacing))
19365 {
19366 extra_line_spacing = XINT (spacing);
19367 if (!NILP (total_height))
19368 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19369 }
19370 }
19371 if (extra_line_spacing > 0)
19372 {
19373 it->descent += extra_line_spacing;
19374 if (extra_line_spacing > it->max_extra_line_spacing)
19375 it->max_extra_line_spacing = extra_line_spacing;
19376 }
19377 it->max_ascent = it->ascent;
19378 it->max_descent = it->descent;
19379 /* Make sure compute_line_metrics recomputes the row height. */
19380 it->glyph_row->height = 0;
19381 }
19382
19383 g->ascent = it->max_ascent;
19384 g->descent = it->max_descent;
19385 #endif
19386
19387 it->override_ascent = -1;
19388 it->constrain_row_ascent_descent_p = false;
19389 it->current_x = saved_x;
19390 it->object = saved_object;
19391 it->position = saved_pos;
19392 it->what = saved_what;
19393 it->face_id = saved_face_id;
19394 it->len = saved_len;
19395 it->c = saved_c;
19396 it->char_to_display = saved_char_to_display;
19397 it->end_of_box_run_p = saved_box_end;
19398 return true;
19399 }
19400 }
19401
19402 return false;
19403 }
19404
19405
19406 /* Extend the face of the last glyph in the text area of IT->glyph_row
19407 to the end of the display line. Called from display_line. If the
19408 glyph row is empty, add a space glyph to it so that we know the
19409 face to draw. Set the glyph row flag fill_line_p. If the glyph
19410 row is R2L, prepend a stretch glyph to cover the empty space to the
19411 left of the leftmost glyph. */
19412
19413 static void
19414 extend_face_to_end_of_line (struct it *it)
19415 {
19416 struct face *face, *default_face;
19417 struct frame *f = it->f;
19418
19419 /* If line is already filled, do nothing. Non window-system frames
19420 get a grace of one more ``pixel'' because their characters are
19421 1-``pixel'' wide, so they hit the equality too early. This grace
19422 is needed only for R2L rows that are not continued, to produce
19423 one extra blank where we could display the cursor. */
19424 if ((it->current_x >= it->last_visible_x
19425 + (!FRAME_WINDOW_P (f)
19426 && it->glyph_row->reversed_p
19427 && !it->glyph_row->continued_p))
19428 /* If the window has display margins, we will need to extend
19429 their face even if the text area is filled. */
19430 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19431 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19432 return;
19433
19434 /* The default face, possibly remapped. */
19435 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19436
19437 /* Face extension extends the background and box of IT->face_id
19438 to the end of the line. If the background equals the background
19439 of the frame, we don't have to do anything. */
19440 if (it->face_before_selective_p)
19441 face = FACE_FROM_ID (f, it->saved_face_id);
19442 else
19443 face = FACE_FROM_ID (f, it->face_id);
19444
19445 if (FRAME_WINDOW_P (f)
19446 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19447 && face->box == FACE_NO_BOX
19448 && face->background == FRAME_BACKGROUND_PIXEL (f)
19449 #ifdef HAVE_WINDOW_SYSTEM
19450 && !face->stipple
19451 #endif
19452 && !it->glyph_row->reversed_p)
19453 return;
19454
19455 /* Set the glyph row flag indicating that the face of the last glyph
19456 in the text area has to be drawn to the end of the text area. */
19457 it->glyph_row->fill_line_p = true;
19458
19459 /* If current character of IT is not ASCII, make sure we have the
19460 ASCII face. This will be automatically undone the next time
19461 get_next_display_element returns a multibyte character. Note
19462 that the character will always be single byte in unibyte
19463 text. */
19464 if (!ASCII_CHAR_P (it->c))
19465 {
19466 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19467 }
19468
19469 if (FRAME_WINDOW_P (f))
19470 {
19471 /* If the row is empty, add a space with the current face of IT,
19472 so that we know which face to draw. */
19473 if (it->glyph_row->used[TEXT_AREA] == 0)
19474 {
19475 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19476 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19477 it->glyph_row->used[TEXT_AREA] = 1;
19478 }
19479 /* Mode line and the header line don't have margins, and
19480 likewise the frame's tool-bar window, if there is any. */
19481 if (!(it->glyph_row->mode_line_p
19482 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19483 || (WINDOWP (f->tool_bar_window)
19484 && it->w == XWINDOW (f->tool_bar_window))
19485 #endif
19486 ))
19487 {
19488 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19489 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19490 {
19491 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19492 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19493 default_face->id;
19494 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19495 }
19496 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19497 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19498 {
19499 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19500 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19501 default_face->id;
19502 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19503 }
19504 }
19505 #ifdef HAVE_WINDOW_SYSTEM
19506 if (it->glyph_row->reversed_p)
19507 {
19508 /* Prepend a stretch glyph to the row, such that the
19509 rightmost glyph will be drawn flushed all the way to the
19510 right margin of the window. The stretch glyph that will
19511 occupy the empty space, if any, to the left of the
19512 glyphs. */
19513 struct font *font = face->font ? face->font : FRAME_FONT (f);
19514 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19515 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19516 struct glyph *g;
19517 int row_width, stretch_ascent, stretch_width;
19518 struct text_pos saved_pos;
19519 int saved_face_id;
19520 bool saved_avoid_cursor, saved_box_start;
19521
19522 for (row_width = 0, g = row_start; g < row_end; g++)
19523 row_width += g->pixel_width;
19524
19525 /* FIXME: There are various minor display glitches in R2L
19526 rows when only one of the fringes is missing. The
19527 strange condition below produces the least bad effect. */
19528 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19529 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19530 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19531 stretch_width = window_box_width (it->w, TEXT_AREA);
19532 else
19533 stretch_width = it->last_visible_x - it->first_visible_x;
19534 stretch_width -= row_width;
19535
19536 if (stretch_width > 0)
19537 {
19538 stretch_ascent =
19539 (((it->ascent + it->descent)
19540 * FONT_BASE (font)) / FONT_HEIGHT (font));
19541 saved_pos = it->position;
19542 memset (&it->position, 0, sizeof it->position);
19543 saved_avoid_cursor = it->avoid_cursor_p;
19544 it->avoid_cursor_p = true;
19545 saved_face_id = it->face_id;
19546 saved_box_start = it->start_of_box_run_p;
19547 /* The last row's stretch glyph should get the default
19548 face, to avoid painting the rest of the window with
19549 the region face, if the region ends at ZV. */
19550 if (it->glyph_row->ends_at_zv_p)
19551 it->face_id = default_face->id;
19552 else
19553 it->face_id = face->id;
19554 it->start_of_box_run_p = false;
19555 append_stretch_glyph (it, Qnil, stretch_width,
19556 it->ascent + it->descent, stretch_ascent);
19557 it->position = saved_pos;
19558 it->avoid_cursor_p = saved_avoid_cursor;
19559 it->face_id = saved_face_id;
19560 it->start_of_box_run_p = saved_box_start;
19561 }
19562 /* If stretch_width comes out negative, it means that the
19563 last glyph is only partially visible. In R2L rows, we
19564 want the leftmost glyph to be partially visible, so we
19565 need to give the row the corresponding left offset. */
19566 if (stretch_width < 0)
19567 it->glyph_row->x = stretch_width;
19568 }
19569 #endif /* HAVE_WINDOW_SYSTEM */
19570 }
19571 else
19572 {
19573 /* Save some values that must not be changed. */
19574 int saved_x = it->current_x;
19575 struct text_pos saved_pos;
19576 Lisp_Object saved_object;
19577 enum display_element_type saved_what = it->what;
19578 int saved_face_id = it->face_id;
19579
19580 saved_object = it->object;
19581 saved_pos = it->position;
19582
19583 it->what = IT_CHARACTER;
19584 memset (&it->position, 0, sizeof it->position);
19585 it->object = Qnil;
19586 it->c = it->char_to_display = ' ';
19587 it->len = 1;
19588
19589 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19590 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19591 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19592 && !it->glyph_row->mode_line_p
19593 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19594 {
19595 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19596 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19597
19598 for (it->current_x = 0; g < e; g++)
19599 it->current_x += g->pixel_width;
19600
19601 it->area = LEFT_MARGIN_AREA;
19602 it->face_id = default_face->id;
19603 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19604 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19605 {
19606 PRODUCE_GLYPHS (it);
19607 /* term.c:produce_glyphs advances it->current_x only for
19608 TEXT_AREA. */
19609 it->current_x += it->pixel_width;
19610 }
19611
19612 it->current_x = saved_x;
19613 it->area = TEXT_AREA;
19614 }
19615
19616 /* The last row's blank glyphs should get the default face, to
19617 avoid painting the rest of the window with the region face,
19618 if the region ends at ZV. */
19619 if (it->glyph_row->ends_at_zv_p)
19620 it->face_id = default_face->id;
19621 else
19622 it->face_id = face->id;
19623 PRODUCE_GLYPHS (it);
19624
19625 while (it->current_x <= it->last_visible_x)
19626 PRODUCE_GLYPHS (it);
19627
19628 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19629 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19630 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19631 && !it->glyph_row->mode_line_p
19632 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19633 {
19634 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19635 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19636
19637 for ( ; g < e; g++)
19638 it->current_x += g->pixel_width;
19639
19640 it->area = RIGHT_MARGIN_AREA;
19641 it->face_id = default_face->id;
19642 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19643 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19644 {
19645 PRODUCE_GLYPHS (it);
19646 it->current_x += it->pixel_width;
19647 }
19648
19649 it->area = TEXT_AREA;
19650 }
19651
19652 /* Don't count these blanks really. It would let us insert a left
19653 truncation glyph below and make us set the cursor on them, maybe. */
19654 it->current_x = saved_x;
19655 it->object = saved_object;
19656 it->position = saved_pos;
19657 it->what = saved_what;
19658 it->face_id = saved_face_id;
19659 }
19660 }
19661
19662
19663 /* Value is true if text starting at CHARPOS in current_buffer is
19664 trailing whitespace. */
19665
19666 static bool
19667 trailing_whitespace_p (ptrdiff_t charpos)
19668 {
19669 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19670 int c = 0;
19671
19672 while (bytepos < ZV_BYTE
19673 && (c = FETCH_CHAR (bytepos),
19674 c == ' ' || c == '\t'))
19675 ++bytepos;
19676
19677 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19678 {
19679 if (bytepos != PT_BYTE)
19680 return true;
19681 }
19682 return false;
19683 }
19684
19685
19686 /* Highlight trailing whitespace, if any, in ROW. */
19687
19688 static void
19689 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19690 {
19691 int used = row->used[TEXT_AREA];
19692
19693 if (used)
19694 {
19695 struct glyph *start = row->glyphs[TEXT_AREA];
19696 struct glyph *glyph = start + used - 1;
19697
19698 if (row->reversed_p)
19699 {
19700 /* Right-to-left rows need to be processed in the opposite
19701 direction, so swap the edge pointers. */
19702 glyph = start;
19703 start = row->glyphs[TEXT_AREA] + used - 1;
19704 }
19705
19706 /* Skip over glyphs inserted to display the cursor at the
19707 end of a line, for extending the face of the last glyph
19708 to the end of the line on terminals, and for truncation
19709 and continuation glyphs. */
19710 if (!row->reversed_p)
19711 {
19712 while (glyph >= start
19713 && glyph->type == CHAR_GLYPH
19714 && NILP (glyph->object))
19715 --glyph;
19716 }
19717 else
19718 {
19719 while (glyph <= start
19720 && glyph->type == CHAR_GLYPH
19721 && NILP (glyph->object))
19722 ++glyph;
19723 }
19724
19725 /* If last glyph is a space or stretch, and it's trailing
19726 whitespace, set the face of all trailing whitespace glyphs in
19727 IT->glyph_row to `trailing-whitespace'. */
19728 if ((row->reversed_p ? glyph <= start : glyph >= start)
19729 && BUFFERP (glyph->object)
19730 && (glyph->type == STRETCH_GLYPH
19731 || (glyph->type == CHAR_GLYPH
19732 && glyph->u.ch == ' '))
19733 && trailing_whitespace_p (glyph->charpos))
19734 {
19735 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19736 if (face_id < 0)
19737 return;
19738
19739 if (!row->reversed_p)
19740 {
19741 while (glyph >= start
19742 && BUFFERP (glyph->object)
19743 && (glyph->type == STRETCH_GLYPH
19744 || (glyph->type == CHAR_GLYPH
19745 && glyph->u.ch == ' ')))
19746 (glyph--)->face_id = face_id;
19747 }
19748 else
19749 {
19750 while (glyph <= start
19751 && BUFFERP (glyph->object)
19752 && (glyph->type == STRETCH_GLYPH
19753 || (glyph->type == CHAR_GLYPH
19754 && glyph->u.ch == ' ')))
19755 (glyph++)->face_id = face_id;
19756 }
19757 }
19758 }
19759 }
19760
19761
19762 /* Value is true if glyph row ROW should be
19763 considered to hold the buffer position CHARPOS. */
19764
19765 static bool
19766 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19767 {
19768 bool result = true;
19769
19770 if (charpos == CHARPOS (row->end.pos)
19771 || charpos == MATRIX_ROW_END_CHARPOS (row))
19772 {
19773 /* Suppose the row ends on a string.
19774 Unless the row is continued, that means it ends on a newline
19775 in the string. If it's anything other than a display string
19776 (e.g., a before-string from an overlay), we don't want the
19777 cursor there. (This heuristic seems to give the optimal
19778 behavior for the various types of multi-line strings.)
19779 One exception: if the string has `cursor' property on one of
19780 its characters, we _do_ want the cursor there. */
19781 if (CHARPOS (row->end.string_pos) >= 0)
19782 {
19783 if (row->continued_p)
19784 result = true;
19785 else
19786 {
19787 /* Check for `display' property. */
19788 struct glyph *beg = row->glyphs[TEXT_AREA];
19789 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19790 struct glyph *glyph;
19791
19792 result = false;
19793 for (glyph = end; glyph >= beg; --glyph)
19794 if (STRINGP (glyph->object))
19795 {
19796 Lisp_Object prop
19797 = Fget_char_property (make_number (charpos),
19798 Qdisplay, Qnil);
19799 result =
19800 (!NILP (prop)
19801 && display_prop_string_p (prop, glyph->object));
19802 /* If there's a `cursor' property on one of the
19803 string's characters, this row is a cursor row,
19804 even though this is not a display string. */
19805 if (!result)
19806 {
19807 Lisp_Object s = glyph->object;
19808
19809 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19810 {
19811 ptrdiff_t gpos = glyph->charpos;
19812
19813 if (!NILP (Fget_char_property (make_number (gpos),
19814 Qcursor, s)))
19815 {
19816 result = true;
19817 break;
19818 }
19819 }
19820 }
19821 break;
19822 }
19823 }
19824 }
19825 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19826 {
19827 /* If the row ends in middle of a real character,
19828 and the line is continued, we want the cursor here.
19829 That's because CHARPOS (ROW->end.pos) would equal
19830 PT if PT is before the character. */
19831 if (!row->ends_in_ellipsis_p)
19832 result = row->continued_p;
19833 else
19834 /* If the row ends in an ellipsis, then
19835 CHARPOS (ROW->end.pos) will equal point after the
19836 invisible text. We want that position to be displayed
19837 after the ellipsis. */
19838 result = false;
19839 }
19840 /* If the row ends at ZV, display the cursor at the end of that
19841 row instead of at the start of the row below. */
19842 else
19843 result = row->ends_at_zv_p;
19844 }
19845
19846 return result;
19847 }
19848
19849 /* Value is true if glyph row ROW should be
19850 used to hold the cursor. */
19851
19852 static bool
19853 cursor_row_p (struct glyph_row *row)
19854 {
19855 return row_for_charpos_p (row, PT);
19856 }
19857
19858 \f
19859
19860 /* Push the property PROP so that it will be rendered at the current
19861 position in IT. Return true if PROP was successfully pushed, false
19862 otherwise. Called from handle_line_prefix to handle the
19863 `line-prefix' and `wrap-prefix' properties. */
19864
19865 static bool
19866 push_prefix_prop (struct it *it, Lisp_Object prop)
19867 {
19868 struct text_pos pos =
19869 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19870
19871 eassert (it->method == GET_FROM_BUFFER
19872 || it->method == GET_FROM_DISPLAY_VECTOR
19873 || it->method == GET_FROM_STRING
19874 || it->method == GET_FROM_IMAGE);
19875
19876 /* We need to save the current buffer/string position, so it will be
19877 restored by pop_it, because iterate_out_of_display_property
19878 depends on that being set correctly, but some situations leave
19879 it->position not yet set when this function is called. */
19880 push_it (it, &pos);
19881
19882 if (STRINGP (prop))
19883 {
19884 if (SCHARS (prop) == 0)
19885 {
19886 pop_it (it);
19887 return false;
19888 }
19889
19890 it->string = prop;
19891 it->string_from_prefix_prop_p = true;
19892 it->multibyte_p = STRING_MULTIBYTE (it->string);
19893 it->current.overlay_string_index = -1;
19894 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19895 it->end_charpos = it->string_nchars = SCHARS (it->string);
19896 it->method = GET_FROM_STRING;
19897 it->stop_charpos = 0;
19898 it->prev_stop = 0;
19899 it->base_level_stop = 0;
19900
19901 /* Force paragraph direction to be that of the parent
19902 buffer/string. */
19903 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19904 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19905 else
19906 it->paragraph_embedding = L2R;
19907
19908 /* Set up the bidi iterator for this display string. */
19909 if (it->bidi_p)
19910 {
19911 it->bidi_it.string.lstring = it->string;
19912 it->bidi_it.string.s = NULL;
19913 it->bidi_it.string.schars = it->end_charpos;
19914 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19915 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19916 it->bidi_it.string.unibyte = !it->multibyte_p;
19917 it->bidi_it.w = it->w;
19918 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19919 }
19920 }
19921 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19922 {
19923 it->method = GET_FROM_STRETCH;
19924 it->object = prop;
19925 }
19926 #ifdef HAVE_WINDOW_SYSTEM
19927 else if (IMAGEP (prop))
19928 {
19929 it->what = IT_IMAGE;
19930 it->image_id = lookup_image (it->f, prop);
19931 it->method = GET_FROM_IMAGE;
19932 }
19933 #endif /* HAVE_WINDOW_SYSTEM */
19934 else
19935 {
19936 pop_it (it); /* bogus display property, give up */
19937 return false;
19938 }
19939
19940 return true;
19941 }
19942
19943 /* Return the character-property PROP at the current position in IT. */
19944
19945 static Lisp_Object
19946 get_it_property (struct it *it, Lisp_Object prop)
19947 {
19948 Lisp_Object position, object = it->object;
19949
19950 if (STRINGP (object))
19951 position = make_number (IT_STRING_CHARPOS (*it));
19952 else if (BUFFERP (object))
19953 {
19954 position = make_number (IT_CHARPOS (*it));
19955 object = it->window;
19956 }
19957 else
19958 return Qnil;
19959
19960 return Fget_char_property (position, prop, object);
19961 }
19962
19963 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19964
19965 static void
19966 handle_line_prefix (struct it *it)
19967 {
19968 Lisp_Object prefix;
19969
19970 if (it->continuation_lines_width > 0)
19971 {
19972 prefix = get_it_property (it, Qwrap_prefix);
19973 if (NILP (prefix))
19974 prefix = Vwrap_prefix;
19975 }
19976 else
19977 {
19978 prefix = get_it_property (it, Qline_prefix);
19979 if (NILP (prefix))
19980 prefix = Vline_prefix;
19981 }
19982 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19983 {
19984 /* If the prefix is wider than the window, and we try to wrap
19985 it, it would acquire its own wrap prefix, and so on till the
19986 iterator stack overflows. So, don't wrap the prefix. */
19987 it->line_wrap = TRUNCATE;
19988 it->avoid_cursor_p = true;
19989 }
19990 }
19991
19992 \f
19993
19994 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19995 only for R2L lines from display_line and display_string, when they
19996 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19997 the line/string needs to be continued on the next glyph row. */
19998 static void
19999 unproduce_glyphs (struct it *it, int n)
20000 {
20001 struct glyph *glyph, *end;
20002
20003 eassert (it->glyph_row);
20004 eassert (it->glyph_row->reversed_p);
20005 eassert (it->area == TEXT_AREA);
20006 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20007
20008 if (n > it->glyph_row->used[TEXT_AREA])
20009 n = it->glyph_row->used[TEXT_AREA];
20010 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20011 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20012 for ( ; glyph < end; glyph++)
20013 glyph[-n] = *glyph;
20014 }
20015
20016 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20017 and ROW->maxpos. */
20018 static void
20019 find_row_edges (struct it *it, struct glyph_row *row,
20020 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20021 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20022 {
20023 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20024 lines' rows is implemented for bidi-reordered rows. */
20025
20026 /* ROW->minpos is the value of min_pos, the minimal buffer position
20027 we have in ROW, or ROW->start.pos if that is smaller. */
20028 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20029 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20030 else
20031 /* We didn't find buffer positions smaller than ROW->start, or
20032 didn't find _any_ valid buffer positions in any of the glyphs,
20033 so we must trust the iterator's computed positions. */
20034 row->minpos = row->start.pos;
20035 if (max_pos <= 0)
20036 {
20037 max_pos = CHARPOS (it->current.pos);
20038 max_bpos = BYTEPOS (it->current.pos);
20039 }
20040
20041 /* Here are the various use-cases for ending the row, and the
20042 corresponding values for ROW->maxpos:
20043
20044 Line ends in a newline from buffer eol_pos + 1
20045 Line is continued from buffer max_pos + 1
20046 Line is truncated on right it->current.pos
20047 Line ends in a newline from string max_pos + 1(*)
20048 (*) + 1 only when line ends in a forward scan
20049 Line is continued from string max_pos
20050 Line is continued from display vector max_pos
20051 Line is entirely from a string min_pos == max_pos
20052 Line is entirely from a display vector min_pos == max_pos
20053 Line that ends at ZV ZV
20054
20055 If you discover other use-cases, please add them here as
20056 appropriate. */
20057 if (row->ends_at_zv_p)
20058 row->maxpos = it->current.pos;
20059 else if (row->used[TEXT_AREA])
20060 {
20061 bool seen_this_string = false;
20062 struct glyph_row *r1 = row - 1;
20063
20064 /* Did we see the same display string on the previous row? */
20065 if (STRINGP (it->object)
20066 /* this is not the first row */
20067 && row > it->w->desired_matrix->rows
20068 /* previous row is not the header line */
20069 && !r1->mode_line_p
20070 /* previous row also ends in a newline from a string */
20071 && r1->ends_in_newline_from_string_p)
20072 {
20073 struct glyph *start, *end;
20074
20075 /* Search for the last glyph of the previous row that came
20076 from buffer or string. Depending on whether the row is
20077 L2R or R2L, we need to process it front to back or the
20078 other way round. */
20079 if (!r1->reversed_p)
20080 {
20081 start = r1->glyphs[TEXT_AREA];
20082 end = start + r1->used[TEXT_AREA];
20083 /* Glyphs inserted by redisplay have nil as their object. */
20084 while (end > start
20085 && NILP ((end - 1)->object)
20086 && (end - 1)->charpos <= 0)
20087 --end;
20088 if (end > start)
20089 {
20090 if (EQ ((end - 1)->object, it->object))
20091 seen_this_string = true;
20092 }
20093 else
20094 /* If all the glyphs of the previous row were inserted
20095 by redisplay, it means the previous row was
20096 produced from a single newline, which is only
20097 possible if that newline came from the same string
20098 as the one which produced this ROW. */
20099 seen_this_string = true;
20100 }
20101 else
20102 {
20103 end = r1->glyphs[TEXT_AREA] - 1;
20104 start = end + r1->used[TEXT_AREA];
20105 while (end < start
20106 && NILP ((end + 1)->object)
20107 && (end + 1)->charpos <= 0)
20108 ++end;
20109 if (end < start)
20110 {
20111 if (EQ ((end + 1)->object, it->object))
20112 seen_this_string = true;
20113 }
20114 else
20115 seen_this_string = true;
20116 }
20117 }
20118 /* Take note of each display string that covers a newline only
20119 once, the first time we see it. This is for when a display
20120 string includes more than one newline in it. */
20121 if (row->ends_in_newline_from_string_p && !seen_this_string)
20122 {
20123 /* If we were scanning the buffer forward when we displayed
20124 the string, we want to account for at least one buffer
20125 position that belongs to this row (position covered by
20126 the display string), so that cursor positioning will
20127 consider this row as a candidate when point is at the end
20128 of the visual line represented by this row. This is not
20129 required when scanning back, because max_pos will already
20130 have a much larger value. */
20131 if (CHARPOS (row->end.pos) > max_pos)
20132 INC_BOTH (max_pos, max_bpos);
20133 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20134 }
20135 else if (CHARPOS (it->eol_pos) > 0)
20136 SET_TEXT_POS (row->maxpos,
20137 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20138 else if (row->continued_p)
20139 {
20140 /* If max_pos is different from IT's current position, it
20141 means IT->method does not belong to the display element
20142 at max_pos. However, it also means that the display
20143 element at max_pos was displayed in its entirety on this
20144 line, which is equivalent to saying that the next line
20145 starts at the next buffer position. */
20146 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20147 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20148 else
20149 {
20150 INC_BOTH (max_pos, max_bpos);
20151 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20152 }
20153 }
20154 else if (row->truncated_on_right_p)
20155 /* display_line already called reseat_at_next_visible_line_start,
20156 which puts the iterator at the beginning of the next line, in
20157 the logical order. */
20158 row->maxpos = it->current.pos;
20159 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20160 /* A line that is entirely from a string/image/stretch... */
20161 row->maxpos = row->minpos;
20162 else
20163 emacs_abort ();
20164 }
20165 else
20166 row->maxpos = it->current.pos;
20167 }
20168
20169 /* Construct the glyph row IT->glyph_row in the desired matrix of
20170 IT->w from text at the current position of IT. See dispextern.h
20171 for an overview of struct it. Value is true if
20172 IT->glyph_row displays text, as opposed to a line displaying ZV
20173 only. */
20174
20175 static bool
20176 display_line (struct it *it)
20177 {
20178 struct glyph_row *row = it->glyph_row;
20179 Lisp_Object overlay_arrow_string;
20180 struct it wrap_it;
20181 void *wrap_data = NULL;
20182 bool may_wrap = false;
20183 int wrap_x IF_LINT (= 0);
20184 int wrap_row_used = -1;
20185 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20186 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20187 int wrap_row_extra_line_spacing IF_LINT (= 0);
20188 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20189 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20190 int cvpos;
20191 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20192 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20193 bool pending_handle_line_prefix = false;
20194
20195 /* We always start displaying at hpos zero even if hscrolled. */
20196 eassert (it->hpos == 0 && it->current_x == 0);
20197
20198 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20199 >= it->w->desired_matrix->nrows)
20200 {
20201 it->w->nrows_scale_factor++;
20202 it->f->fonts_changed = true;
20203 return false;
20204 }
20205
20206 /* Clear the result glyph row and enable it. */
20207 prepare_desired_row (it->w, row, false);
20208
20209 row->y = it->current_y;
20210 row->start = it->start;
20211 row->continuation_lines_width = it->continuation_lines_width;
20212 row->displays_text_p = true;
20213 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20214 it->starts_in_middle_of_char_p = false;
20215
20216 /* Arrange the overlays nicely for our purposes. Usually, we call
20217 display_line on only one line at a time, in which case this
20218 can't really hurt too much, or we call it on lines which appear
20219 one after another in the buffer, in which case all calls to
20220 recenter_overlay_lists but the first will be pretty cheap. */
20221 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20222
20223 /* Move over display elements that are not visible because we are
20224 hscrolled. This may stop at an x-position < IT->first_visible_x
20225 if the first glyph is partially visible or if we hit a line end. */
20226 if (it->current_x < it->first_visible_x)
20227 {
20228 enum move_it_result move_result;
20229
20230 this_line_min_pos = row->start.pos;
20231 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20232 MOVE_TO_POS | MOVE_TO_X);
20233 /* If we are under a large hscroll, move_it_in_display_line_to
20234 could hit the end of the line without reaching
20235 it->first_visible_x. Pretend that we did reach it. This is
20236 especially important on a TTY, where we will call
20237 extend_face_to_end_of_line, which needs to know how many
20238 blank glyphs to produce. */
20239 if (it->current_x < it->first_visible_x
20240 && (move_result == MOVE_NEWLINE_OR_CR
20241 || move_result == MOVE_POS_MATCH_OR_ZV))
20242 it->current_x = it->first_visible_x;
20243
20244 /* Record the smallest positions seen while we moved over
20245 display elements that are not visible. This is needed by
20246 redisplay_internal for optimizing the case where the cursor
20247 stays inside the same line. The rest of this function only
20248 considers positions that are actually displayed, so
20249 RECORD_MAX_MIN_POS will not otherwise record positions that
20250 are hscrolled to the left of the left edge of the window. */
20251 min_pos = CHARPOS (this_line_min_pos);
20252 min_bpos = BYTEPOS (this_line_min_pos);
20253 }
20254 else if (it->area == TEXT_AREA)
20255 {
20256 /* We only do this when not calling move_it_in_display_line_to
20257 above, because that function calls itself handle_line_prefix. */
20258 handle_line_prefix (it);
20259 }
20260 else
20261 {
20262 /* Line-prefix and wrap-prefix are always displayed in the text
20263 area. But if this is the first call to display_line after
20264 init_iterator, the iterator might have been set up to write
20265 into a marginal area, e.g. if the line begins with some
20266 display property that writes to the margins. So we need to
20267 wait with the call to handle_line_prefix until whatever
20268 writes to the margin has done its job. */
20269 pending_handle_line_prefix = true;
20270 }
20271
20272 /* Get the initial row height. This is either the height of the
20273 text hscrolled, if there is any, or zero. */
20274 row->ascent = it->max_ascent;
20275 row->height = it->max_ascent + it->max_descent;
20276 row->phys_ascent = it->max_phys_ascent;
20277 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20278 row->extra_line_spacing = it->max_extra_line_spacing;
20279
20280 /* Utility macro to record max and min buffer positions seen until now. */
20281 #define RECORD_MAX_MIN_POS(IT) \
20282 do \
20283 { \
20284 bool composition_p \
20285 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20286 ptrdiff_t current_pos = \
20287 composition_p ? (IT)->cmp_it.charpos \
20288 : IT_CHARPOS (*(IT)); \
20289 ptrdiff_t current_bpos = \
20290 composition_p ? CHAR_TO_BYTE (current_pos) \
20291 : IT_BYTEPOS (*(IT)); \
20292 if (current_pos < min_pos) \
20293 { \
20294 min_pos = current_pos; \
20295 min_bpos = current_bpos; \
20296 } \
20297 if (IT_CHARPOS (*it) > max_pos) \
20298 { \
20299 max_pos = IT_CHARPOS (*it); \
20300 max_bpos = IT_BYTEPOS (*it); \
20301 } \
20302 } \
20303 while (false)
20304
20305 /* Loop generating characters. The loop is left with IT on the next
20306 character to display. */
20307 while (true)
20308 {
20309 int n_glyphs_before, hpos_before, x_before;
20310 int x, nglyphs;
20311 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20312
20313 /* Retrieve the next thing to display. Value is false if end of
20314 buffer reached. */
20315 if (!get_next_display_element (it))
20316 {
20317 /* Maybe add a space at the end of this line that is used to
20318 display the cursor there under X. Set the charpos of the
20319 first glyph of blank lines not corresponding to any text
20320 to -1. */
20321 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20322 row->exact_window_width_line_p = true;
20323 else if ((append_space_for_newline (it, true)
20324 && row->used[TEXT_AREA] == 1)
20325 || row->used[TEXT_AREA] == 0)
20326 {
20327 row->glyphs[TEXT_AREA]->charpos = -1;
20328 row->displays_text_p = false;
20329
20330 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20331 && (!MINI_WINDOW_P (it->w)
20332 || (minibuf_level && EQ (it->window, minibuf_window))))
20333 row->indicate_empty_line_p = true;
20334 }
20335
20336 it->continuation_lines_width = 0;
20337 row->ends_at_zv_p = true;
20338 /* A row that displays right-to-left text must always have
20339 its last face extended all the way to the end of line,
20340 even if this row ends in ZV, because we still write to
20341 the screen left to right. We also need to extend the
20342 last face if the default face is remapped to some
20343 different face, otherwise the functions that clear
20344 portions of the screen will clear with the default face's
20345 background color. */
20346 if (row->reversed_p
20347 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20348 extend_face_to_end_of_line (it);
20349 break;
20350 }
20351
20352 /* Now, get the metrics of what we want to display. This also
20353 generates glyphs in `row' (which is IT->glyph_row). */
20354 n_glyphs_before = row->used[TEXT_AREA];
20355 x = it->current_x;
20356
20357 /* Remember the line height so far in case the next element doesn't
20358 fit on the line. */
20359 if (it->line_wrap != TRUNCATE)
20360 {
20361 ascent = it->max_ascent;
20362 descent = it->max_descent;
20363 phys_ascent = it->max_phys_ascent;
20364 phys_descent = it->max_phys_descent;
20365
20366 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20367 {
20368 if (IT_DISPLAYING_WHITESPACE (it))
20369 may_wrap = true;
20370 else if (may_wrap)
20371 {
20372 SAVE_IT (wrap_it, *it, wrap_data);
20373 wrap_x = x;
20374 wrap_row_used = row->used[TEXT_AREA];
20375 wrap_row_ascent = row->ascent;
20376 wrap_row_height = row->height;
20377 wrap_row_phys_ascent = row->phys_ascent;
20378 wrap_row_phys_height = row->phys_height;
20379 wrap_row_extra_line_spacing = row->extra_line_spacing;
20380 wrap_row_min_pos = min_pos;
20381 wrap_row_min_bpos = min_bpos;
20382 wrap_row_max_pos = max_pos;
20383 wrap_row_max_bpos = max_bpos;
20384 may_wrap = false;
20385 }
20386 }
20387 }
20388
20389 PRODUCE_GLYPHS (it);
20390
20391 /* If this display element was in marginal areas, continue with
20392 the next one. */
20393 if (it->area != TEXT_AREA)
20394 {
20395 row->ascent = max (row->ascent, it->max_ascent);
20396 row->height = max (row->height, it->max_ascent + it->max_descent);
20397 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20398 row->phys_height = max (row->phys_height,
20399 it->max_phys_ascent + it->max_phys_descent);
20400 row->extra_line_spacing = max (row->extra_line_spacing,
20401 it->max_extra_line_spacing);
20402 set_iterator_to_next (it, true);
20403 /* If we didn't handle the line/wrap prefix above, and the
20404 call to set_iterator_to_next just switched to TEXT_AREA,
20405 process the prefix now. */
20406 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20407 {
20408 pending_handle_line_prefix = false;
20409 handle_line_prefix (it);
20410 }
20411 continue;
20412 }
20413
20414 /* Does the display element fit on the line? If we truncate
20415 lines, we should draw past the right edge of the window. If
20416 we don't truncate, we want to stop so that we can display the
20417 continuation glyph before the right margin. If lines are
20418 continued, there are two possible strategies for characters
20419 resulting in more than 1 glyph (e.g. tabs): Display as many
20420 glyphs as possible in this line and leave the rest for the
20421 continuation line, or display the whole element in the next
20422 line. Original redisplay did the former, so we do it also. */
20423 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20424 hpos_before = it->hpos;
20425 x_before = x;
20426
20427 if (/* Not a newline. */
20428 nglyphs > 0
20429 /* Glyphs produced fit entirely in the line. */
20430 && it->current_x < it->last_visible_x)
20431 {
20432 it->hpos += nglyphs;
20433 row->ascent = max (row->ascent, it->max_ascent);
20434 row->height = max (row->height, it->max_ascent + it->max_descent);
20435 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20436 row->phys_height = max (row->phys_height,
20437 it->max_phys_ascent + it->max_phys_descent);
20438 row->extra_line_spacing = max (row->extra_line_spacing,
20439 it->max_extra_line_spacing);
20440 if (it->current_x - it->pixel_width < it->first_visible_x
20441 /* In R2L rows, we arrange in extend_face_to_end_of_line
20442 to add a right offset to the line, by a suitable
20443 change to the stretch glyph that is the leftmost
20444 glyph of the line. */
20445 && !row->reversed_p)
20446 row->x = x - it->first_visible_x;
20447 /* Record the maximum and minimum buffer positions seen so
20448 far in glyphs that will be displayed by this row. */
20449 if (it->bidi_p)
20450 RECORD_MAX_MIN_POS (it);
20451 }
20452 else
20453 {
20454 int i, new_x;
20455 struct glyph *glyph;
20456
20457 for (i = 0; i < nglyphs; ++i, x = new_x)
20458 {
20459 /* Identify the glyphs added by the last call to
20460 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20461 the previous glyphs. */
20462 if (!row->reversed_p)
20463 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20464 else
20465 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20466 new_x = x + glyph->pixel_width;
20467
20468 if (/* Lines are continued. */
20469 it->line_wrap != TRUNCATE
20470 && (/* Glyph doesn't fit on the line. */
20471 new_x > it->last_visible_x
20472 /* Or it fits exactly on a window system frame. */
20473 || (new_x == it->last_visible_x
20474 && FRAME_WINDOW_P (it->f)
20475 && (row->reversed_p
20476 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20477 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20478 {
20479 /* End of a continued line. */
20480
20481 if (it->hpos == 0
20482 || (new_x == it->last_visible_x
20483 && FRAME_WINDOW_P (it->f)
20484 && (row->reversed_p
20485 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20486 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20487 {
20488 /* Current glyph is the only one on the line or
20489 fits exactly on the line. We must continue
20490 the line because we can't draw the cursor
20491 after the glyph. */
20492 row->continued_p = true;
20493 it->current_x = new_x;
20494 it->continuation_lines_width += new_x;
20495 ++it->hpos;
20496 if (i == nglyphs - 1)
20497 {
20498 /* If line-wrap is on, check if a previous
20499 wrap point was found. */
20500 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20501 && wrap_row_used > 0
20502 /* Even if there is a previous wrap
20503 point, continue the line here as
20504 usual, if (i) the previous character
20505 was a space or tab AND (ii) the
20506 current character is not. */
20507 && (!may_wrap
20508 || IT_DISPLAYING_WHITESPACE (it)))
20509 goto back_to_wrap;
20510
20511 /* Record the maximum and minimum buffer
20512 positions seen so far in glyphs that will be
20513 displayed by this row. */
20514 if (it->bidi_p)
20515 RECORD_MAX_MIN_POS (it);
20516 set_iterator_to_next (it, true);
20517 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20518 {
20519 if (!get_next_display_element (it))
20520 {
20521 row->exact_window_width_line_p = true;
20522 it->continuation_lines_width = 0;
20523 row->continued_p = false;
20524 row->ends_at_zv_p = true;
20525 }
20526 else if (ITERATOR_AT_END_OF_LINE_P (it))
20527 {
20528 row->continued_p = false;
20529 row->exact_window_width_line_p = true;
20530 }
20531 /* If line-wrap is on, check if a
20532 previous wrap point was found. */
20533 else if (wrap_row_used > 0
20534 /* Even if there is a previous wrap
20535 point, continue the line here as
20536 usual, if (i) the previous character
20537 was a space or tab AND (ii) the
20538 current character is not. */
20539 && (!may_wrap
20540 || IT_DISPLAYING_WHITESPACE (it)))
20541 goto back_to_wrap;
20542
20543 }
20544 }
20545 else if (it->bidi_p)
20546 RECORD_MAX_MIN_POS (it);
20547 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20548 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20549 extend_face_to_end_of_line (it);
20550 }
20551 else if (CHAR_GLYPH_PADDING_P (*glyph)
20552 && !FRAME_WINDOW_P (it->f))
20553 {
20554 /* A padding glyph that doesn't fit on this line.
20555 This means the whole character doesn't fit
20556 on the line. */
20557 if (row->reversed_p)
20558 unproduce_glyphs (it, row->used[TEXT_AREA]
20559 - n_glyphs_before);
20560 row->used[TEXT_AREA] = n_glyphs_before;
20561
20562 /* Fill the rest of the row with continuation
20563 glyphs like in 20.x. */
20564 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20565 < row->glyphs[1 + TEXT_AREA])
20566 produce_special_glyphs (it, IT_CONTINUATION);
20567
20568 row->continued_p = true;
20569 it->current_x = x_before;
20570 it->continuation_lines_width += x_before;
20571
20572 /* Restore the height to what it was before the
20573 element not fitting on the line. */
20574 it->max_ascent = ascent;
20575 it->max_descent = descent;
20576 it->max_phys_ascent = phys_ascent;
20577 it->max_phys_descent = phys_descent;
20578 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20579 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20580 extend_face_to_end_of_line (it);
20581 }
20582 else if (wrap_row_used > 0)
20583 {
20584 back_to_wrap:
20585 if (row->reversed_p)
20586 unproduce_glyphs (it,
20587 row->used[TEXT_AREA] - wrap_row_used);
20588 RESTORE_IT (it, &wrap_it, wrap_data);
20589 it->continuation_lines_width += wrap_x;
20590 row->used[TEXT_AREA] = wrap_row_used;
20591 row->ascent = wrap_row_ascent;
20592 row->height = wrap_row_height;
20593 row->phys_ascent = wrap_row_phys_ascent;
20594 row->phys_height = wrap_row_phys_height;
20595 row->extra_line_spacing = wrap_row_extra_line_spacing;
20596 min_pos = wrap_row_min_pos;
20597 min_bpos = wrap_row_min_bpos;
20598 max_pos = wrap_row_max_pos;
20599 max_bpos = wrap_row_max_bpos;
20600 row->continued_p = true;
20601 row->ends_at_zv_p = false;
20602 row->exact_window_width_line_p = false;
20603 it->continuation_lines_width += x;
20604
20605 /* Make sure that a non-default face is extended
20606 up to the right margin of the window. */
20607 extend_face_to_end_of_line (it);
20608 }
20609 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20610 {
20611 /* A TAB that extends past the right edge of the
20612 window. This produces a single glyph on
20613 window system frames. We leave the glyph in
20614 this row and let it fill the row, but don't
20615 consume the TAB. */
20616 if ((row->reversed_p
20617 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20618 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20619 produce_special_glyphs (it, IT_CONTINUATION);
20620 it->continuation_lines_width += it->last_visible_x;
20621 row->ends_in_middle_of_char_p = true;
20622 row->continued_p = true;
20623 glyph->pixel_width = it->last_visible_x - x;
20624 it->starts_in_middle_of_char_p = true;
20625 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20626 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20627 extend_face_to_end_of_line (it);
20628 }
20629 else
20630 {
20631 /* Something other than a TAB that draws past
20632 the right edge of the window. Restore
20633 positions to values before the element. */
20634 if (row->reversed_p)
20635 unproduce_glyphs (it, row->used[TEXT_AREA]
20636 - (n_glyphs_before + i));
20637 row->used[TEXT_AREA] = n_glyphs_before + i;
20638
20639 /* Display continuation glyphs. */
20640 it->current_x = x_before;
20641 it->continuation_lines_width += x;
20642 if (!FRAME_WINDOW_P (it->f)
20643 || (row->reversed_p
20644 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20645 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20646 produce_special_glyphs (it, IT_CONTINUATION);
20647 row->continued_p = true;
20648
20649 extend_face_to_end_of_line (it);
20650
20651 if (nglyphs > 1 && i > 0)
20652 {
20653 row->ends_in_middle_of_char_p = true;
20654 it->starts_in_middle_of_char_p = true;
20655 }
20656
20657 /* Restore the height to what it was before the
20658 element not fitting on the line. */
20659 it->max_ascent = ascent;
20660 it->max_descent = descent;
20661 it->max_phys_ascent = phys_ascent;
20662 it->max_phys_descent = phys_descent;
20663 }
20664
20665 break;
20666 }
20667 else if (new_x > it->first_visible_x)
20668 {
20669 /* Increment number of glyphs actually displayed. */
20670 ++it->hpos;
20671
20672 /* Record the maximum and minimum buffer positions
20673 seen so far in glyphs that will be displayed by
20674 this row. */
20675 if (it->bidi_p)
20676 RECORD_MAX_MIN_POS (it);
20677
20678 if (x < it->first_visible_x && !row->reversed_p)
20679 /* Glyph is partially visible, i.e. row starts at
20680 negative X position. Don't do that in R2L
20681 rows, where we arrange to add a right offset to
20682 the line in extend_face_to_end_of_line, by a
20683 suitable change to the stretch glyph that is
20684 the leftmost glyph of the line. */
20685 row->x = x - it->first_visible_x;
20686 /* When the last glyph of an R2L row only fits
20687 partially on the line, we need to set row->x to a
20688 negative offset, so that the leftmost glyph is
20689 the one that is partially visible. But if we are
20690 going to produce the truncation glyph, this will
20691 be taken care of in produce_special_glyphs. */
20692 if (row->reversed_p
20693 && new_x > it->last_visible_x
20694 && !(it->line_wrap == TRUNCATE
20695 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20696 {
20697 eassert (FRAME_WINDOW_P (it->f));
20698 row->x = it->last_visible_x - new_x;
20699 }
20700 }
20701 else
20702 {
20703 /* Glyph is completely off the left margin of the
20704 window. This should not happen because of the
20705 move_it_in_display_line at the start of this
20706 function, unless the text display area of the
20707 window is empty. */
20708 eassert (it->first_visible_x <= it->last_visible_x);
20709 }
20710 }
20711 /* Even if this display element produced no glyphs at all,
20712 we want to record its position. */
20713 if (it->bidi_p && nglyphs == 0)
20714 RECORD_MAX_MIN_POS (it);
20715
20716 row->ascent = max (row->ascent, it->max_ascent);
20717 row->height = max (row->height, it->max_ascent + it->max_descent);
20718 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20719 row->phys_height = max (row->phys_height,
20720 it->max_phys_ascent + it->max_phys_descent);
20721 row->extra_line_spacing = max (row->extra_line_spacing,
20722 it->max_extra_line_spacing);
20723
20724 /* End of this display line if row is continued. */
20725 if (row->continued_p || row->ends_at_zv_p)
20726 break;
20727 }
20728
20729 at_end_of_line:
20730 /* Is this a line end? If yes, we're also done, after making
20731 sure that a non-default face is extended up to the right
20732 margin of the window. */
20733 if (ITERATOR_AT_END_OF_LINE_P (it))
20734 {
20735 int used_before = row->used[TEXT_AREA];
20736
20737 row->ends_in_newline_from_string_p = STRINGP (it->object);
20738
20739 /* Add a space at the end of the line that is used to
20740 display the cursor there. */
20741 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20742 append_space_for_newline (it, false);
20743
20744 /* Extend the face to the end of the line. */
20745 extend_face_to_end_of_line (it);
20746
20747 /* Make sure we have the position. */
20748 if (used_before == 0)
20749 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20750
20751 /* Record the position of the newline, for use in
20752 find_row_edges. */
20753 it->eol_pos = it->current.pos;
20754
20755 /* Consume the line end. This skips over invisible lines. */
20756 set_iterator_to_next (it, true);
20757 it->continuation_lines_width = 0;
20758 break;
20759 }
20760
20761 /* Proceed with next display element. Note that this skips
20762 over lines invisible because of selective display. */
20763 set_iterator_to_next (it, true);
20764
20765 /* If we truncate lines, we are done when the last displayed
20766 glyphs reach past the right margin of the window. */
20767 if (it->line_wrap == TRUNCATE
20768 && ((FRAME_WINDOW_P (it->f)
20769 /* Images are preprocessed in produce_image_glyph such
20770 that they are cropped at the right edge of the
20771 window, so an image glyph will always end exactly at
20772 last_visible_x, even if there's no right fringe. */
20773 && ((row->reversed_p
20774 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20775 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20776 || it->what == IT_IMAGE))
20777 ? (it->current_x >= it->last_visible_x)
20778 : (it->current_x > it->last_visible_x)))
20779 {
20780 /* Maybe add truncation glyphs. */
20781 if (!FRAME_WINDOW_P (it->f)
20782 || (row->reversed_p
20783 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20784 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20785 {
20786 int i, n;
20787
20788 if (!row->reversed_p)
20789 {
20790 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20791 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20792 break;
20793 }
20794 else
20795 {
20796 for (i = 0; i < row->used[TEXT_AREA]; i++)
20797 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20798 break;
20799 /* Remove any padding glyphs at the front of ROW, to
20800 make room for the truncation glyphs we will be
20801 adding below. The loop below always inserts at
20802 least one truncation glyph, so also remove the
20803 last glyph added to ROW. */
20804 unproduce_glyphs (it, i + 1);
20805 /* Adjust i for the loop below. */
20806 i = row->used[TEXT_AREA] - (i + 1);
20807 }
20808
20809 /* produce_special_glyphs overwrites the last glyph, so
20810 we don't want that if we want to keep that last
20811 glyph, which means it's an image. */
20812 if (it->current_x > it->last_visible_x)
20813 {
20814 it->current_x = x_before;
20815 if (!FRAME_WINDOW_P (it->f))
20816 {
20817 for (n = row->used[TEXT_AREA]; i < n; ++i)
20818 {
20819 row->used[TEXT_AREA] = i;
20820 produce_special_glyphs (it, IT_TRUNCATION);
20821 }
20822 }
20823 else
20824 {
20825 row->used[TEXT_AREA] = i;
20826 produce_special_glyphs (it, IT_TRUNCATION);
20827 }
20828 it->hpos = hpos_before;
20829 }
20830 }
20831 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20832 {
20833 /* Don't truncate if we can overflow newline into fringe. */
20834 if (!get_next_display_element (it))
20835 {
20836 it->continuation_lines_width = 0;
20837 row->ends_at_zv_p = true;
20838 row->exact_window_width_line_p = true;
20839 break;
20840 }
20841 if (ITERATOR_AT_END_OF_LINE_P (it))
20842 {
20843 row->exact_window_width_line_p = true;
20844 goto at_end_of_line;
20845 }
20846 it->current_x = x_before;
20847 it->hpos = hpos_before;
20848 }
20849
20850 row->truncated_on_right_p = true;
20851 it->continuation_lines_width = 0;
20852 reseat_at_next_visible_line_start (it, false);
20853 /* We insist below that IT's position be at ZV because in
20854 bidi-reordered lines the character at visible line start
20855 might not be the character that follows the newline in
20856 the logical order. */
20857 if (IT_BYTEPOS (*it) > BEG_BYTE)
20858 row->ends_at_zv_p =
20859 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20860 else
20861 row->ends_at_zv_p = false;
20862 break;
20863 }
20864 }
20865
20866 if (wrap_data)
20867 bidi_unshelve_cache (wrap_data, true);
20868
20869 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20870 at the left window margin. */
20871 if (it->first_visible_x
20872 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20873 {
20874 if (!FRAME_WINDOW_P (it->f)
20875 || (((row->reversed_p
20876 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20877 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20878 /* Don't let insert_left_trunc_glyphs overwrite the
20879 first glyph of the row if it is an image. */
20880 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20881 insert_left_trunc_glyphs (it);
20882 row->truncated_on_left_p = true;
20883 }
20884
20885 /* Remember the position at which this line ends.
20886
20887 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20888 cannot be before the call to find_row_edges below, since that is
20889 where these positions are determined. */
20890 row->end = it->current;
20891 if (!it->bidi_p)
20892 {
20893 row->minpos = row->start.pos;
20894 row->maxpos = row->end.pos;
20895 }
20896 else
20897 {
20898 /* ROW->minpos and ROW->maxpos must be the smallest and
20899 `1 + the largest' buffer positions in ROW. But if ROW was
20900 bidi-reordered, these two positions can be anywhere in the
20901 row, so we must determine them now. */
20902 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20903 }
20904
20905 /* If the start of this line is the overlay arrow-position, then
20906 mark this glyph row as the one containing the overlay arrow.
20907 This is clearly a mess with variable size fonts. It would be
20908 better to let it be displayed like cursors under X. */
20909 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20910 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20911 !NILP (overlay_arrow_string)))
20912 {
20913 /* Overlay arrow in window redisplay is a fringe bitmap. */
20914 if (STRINGP (overlay_arrow_string))
20915 {
20916 struct glyph_row *arrow_row
20917 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20918 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20919 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20920 struct glyph *p = row->glyphs[TEXT_AREA];
20921 struct glyph *p2, *end;
20922
20923 /* Copy the arrow glyphs. */
20924 while (glyph < arrow_end)
20925 *p++ = *glyph++;
20926
20927 /* Throw away padding glyphs. */
20928 p2 = p;
20929 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20930 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20931 ++p2;
20932 if (p2 > p)
20933 {
20934 while (p2 < end)
20935 *p++ = *p2++;
20936 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20937 }
20938 }
20939 else
20940 {
20941 eassert (INTEGERP (overlay_arrow_string));
20942 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20943 }
20944 overlay_arrow_seen = true;
20945 }
20946
20947 /* Highlight trailing whitespace. */
20948 if (!NILP (Vshow_trailing_whitespace))
20949 highlight_trailing_whitespace (it->f, it->glyph_row);
20950
20951 /* Compute pixel dimensions of this line. */
20952 compute_line_metrics (it);
20953
20954 /* Implementation note: No changes in the glyphs of ROW or in their
20955 faces can be done past this point, because compute_line_metrics
20956 computes ROW's hash value and stores it within the glyph_row
20957 structure. */
20958
20959 /* Record whether this row ends inside an ellipsis. */
20960 row->ends_in_ellipsis_p
20961 = (it->method == GET_FROM_DISPLAY_VECTOR
20962 && it->ellipsis_p);
20963
20964 /* Save fringe bitmaps in this row. */
20965 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20966 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20967 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20968 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20969
20970 it->left_user_fringe_bitmap = 0;
20971 it->left_user_fringe_face_id = 0;
20972 it->right_user_fringe_bitmap = 0;
20973 it->right_user_fringe_face_id = 0;
20974
20975 /* Maybe set the cursor. */
20976 cvpos = it->w->cursor.vpos;
20977 if ((cvpos < 0
20978 /* In bidi-reordered rows, keep checking for proper cursor
20979 position even if one has been found already, because buffer
20980 positions in such rows change non-linearly with ROW->VPOS,
20981 when a line is continued. One exception: when we are at ZV,
20982 display cursor on the first suitable glyph row, since all
20983 the empty rows after that also have their position set to ZV. */
20984 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20985 lines' rows is implemented for bidi-reordered rows. */
20986 || (it->bidi_p
20987 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20988 && PT >= MATRIX_ROW_START_CHARPOS (row)
20989 && PT <= MATRIX_ROW_END_CHARPOS (row)
20990 && cursor_row_p (row))
20991 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20992
20993 /* Prepare for the next line. This line starts horizontally at (X
20994 HPOS) = (0 0). Vertical positions are incremented. As a
20995 convenience for the caller, IT->glyph_row is set to the next
20996 row to be used. */
20997 it->current_x = it->hpos = 0;
20998 it->current_y += row->height;
20999 SET_TEXT_POS (it->eol_pos, 0, 0);
21000 ++it->vpos;
21001 ++it->glyph_row;
21002 /* The next row should by default use the same value of the
21003 reversed_p flag as this one. set_iterator_to_next decides when
21004 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21005 the flag accordingly. */
21006 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21007 it->glyph_row->reversed_p = row->reversed_p;
21008 it->start = row->end;
21009 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21010
21011 #undef RECORD_MAX_MIN_POS
21012 }
21013
21014 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21015 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21016 doc: /* Return paragraph direction at point in BUFFER.
21017 Value is either `left-to-right' or `right-to-left'.
21018 If BUFFER is omitted or nil, it defaults to the current buffer.
21019
21020 Paragraph direction determines how the text in the paragraph is displayed.
21021 In left-to-right paragraphs, text begins at the left margin of the window
21022 and the reading direction is generally left to right. In right-to-left
21023 paragraphs, text begins at the right margin and is read from right to left.
21024
21025 See also `bidi-paragraph-direction'. */)
21026 (Lisp_Object buffer)
21027 {
21028 struct buffer *buf = current_buffer;
21029 struct buffer *old = buf;
21030
21031 if (! NILP (buffer))
21032 {
21033 CHECK_BUFFER (buffer);
21034 buf = XBUFFER (buffer);
21035 }
21036
21037 if (NILP (BVAR (buf, bidi_display_reordering))
21038 || NILP (BVAR (buf, enable_multibyte_characters))
21039 /* When we are loading loadup.el, the character property tables
21040 needed for bidi iteration are not yet available. */
21041 || !NILP (Vpurify_flag))
21042 return Qleft_to_right;
21043 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21044 return BVAR (buf, bidi_paragraph_direction);
21045 else
21046 {
21047 /* Determine the direction from buffer text. We could try to
21048 use current_matrix if it is up to date, but this seems fast
21049 enough as it is. */
21050 struct bidi_it itb;
21051 ptrdiff_t pos = BUF_PT (buf);
21052 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21053 int c;
21054 void *itb_data = bidi_shelve_cache ();
21055
21056 set_buffer_temp (buf);
21057 /* bidi_paragraph_init finds the base direction of the paragraph
21058 by searching forward from paragraph start. We need the base
21059 direction of the current or _previous_ paragraph, so we need
21060 to make sure we are within that paragraph. To that end, find
21061 the previous non-empty line. */
21062 if (pos >= ZV && pos > BEGV)
21063 DEC_BOTH (pos, bytepos);
21064 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21065 if (fast_looking_at (trailing_white_space,
21066 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21067 {
21068 while ((c = FETCH_BYTE (bytepos)) == '\n'
21069 || c == ' ' || c == '\t' || c == '\f')
21070 {
21071 if (bytepos <= BEGV_BYTE)
21072 break;
21073 bytepos--;
21074 pos--;
21075 }
21076 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21077 bytepos--;
21078 }
21079 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21080 itb.paragraph_dir = NEUTRAL_DIR;
21081 itb.string.s = NULL;
21082 itb.string.lstring = Qnil;
21083 itb.string.bufpos = 0;
21084 itb.string.from_disp_str = false;
21085 itb.string.unibyte = false;
21086 /* We have no window to use here for ignoring window-specific
21087 overlays. Using NULL for window pointer will cause
21088 compute_display_string_pos to use the current buffer. */
21089 itb.w = NULL;
21090 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21091 bidi_unshelve_cache (itb_data, false);
21092 set_buffer_temp (old);
21093 switch (itb.paragraph_dir)
21094 {
21095 case L2R:
21096 return Qleft_to_right;
21097 break;
21098 case R2L:
21099 return Qright_to_left;
21100 break;
21101 default:
21102 emacs_abort ();
21103 }
21104 }
21105 }
21106
21107 DEFUN ("bidi-find-overridden-directionality",
21108 Fbidi_find_overridden_directionality,
21109 Sbidi_find_overridden_directionality, 2, 3, 0,
21110 doc: /* Return position between FROM and TO where directionality was overridden.
21111
21112 This function returns the first character position in the specified
21113 region of OBJECT where there is a character whose `bidi-class' property
21114 is `L', but which was forced to display as `R' by a directional
21115 override, and likewise with characters whose `bidi-class' is `R'
21116 or `AL' that were forced to display as `L'.
21117
21118 If no such character is found, the function returns nil.
21119
21120 OBJECT is a Lisp string or buffer to search for overridden
21121 directionality, and defaults to the current buffer if nil or omitted.
21122 OBJECT can also be a window, in which case the function will search
21123 the buffer displayed in that window. Passing the window instead of
21124 a buffer is preferable when the buffer is displayed in some window,
21125 because this function will then be able to correctly account for
21126 window-specific overlays, which can affect the results.
21127
21128 Strong directional characters `L', `R', and `AL' can have their
21129 intrinsic directionality overridden by directional override
21130 control characters RLO (u+202e) and LRO (u+202d). See the
21131 function `get-char-code-property' for a way to inquire about
21132 the `bidi-class' property of a character. */)
21133 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21134 {
21135 struct buffer *buf = current_buffer;
21136 struct buffer *old = buf;
21137 struct window *w = NULL;
21138 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21139 struct bidi_it itb;
21140 ptrdiff_t from_pos, to_pos, from_bpos;
21141 void *itb_data;
21142
21143 if (!NILP (object))
21144 {
21145 if (BUFFERP (object))
21146 buf = XBUFFER (object);
21147 else if (WINDOWP (object))
21148 {
21149 w = decode_live_window (object);
21150 buf = XBUFFER (w->contents);
21151 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21152 }
21153 else
21154 CHECK_STRING (object);
21155 }
21156
21157 if (STRINGP (object))
21158 {
21159 /* Characters in unibyte strings are always treated by bidi.c as
21160 strong LTR. */
21161 if (!STRING_MULTIBYTE (object)
21162 /* When we are loading loadup.el, the character property
21163 tables needed for bidi iteration are not yet
21164 available. */
21165 || !NILP (Vpurify_flag))
21166 return Qnil;
21167
21168 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21169 if (from_pos >= SCHARS (object))
21170 return Qnil;
21171
21172 /* Set up the bidi iterator. */
21173 itb_data = bidi_shelve_cache ();
21174 itb.paragraph_dir = NEUTRAL_DIR;
21175 itb.string.lstring = object;
21176 itb.string.s = NULL;
21177 itb.string.schars = SCHARS (object);
21178 itb.string.bufpos = 0;
21179 itb.string.from_disp_str = false;
21180 itb.string.unibyte = false;
21181 itb.w = w;
21182 bidi_init_it (0, 0, frame_window_p, &itb);
21183 }
21184 else
21185 {
21186 /* Nothing this fancy can happen in unibyte buffers, or in a
21187 buffer that disabled reordering, or if FROM is at EOB. */
21188 if (NILP (BVAR (buf, bidi_display_reordering))
21189 || NILP (BVAR (buf, enable_multibyte_characters))
21190 /* When we are loading loadup.el, the character property
21191 tables needed for bidi iteration are not yet
21192 available. */
21193 || !NILP (Vpurify_flag))
21194 return Qnil;
21195
21196 set_buffer_temp (buf);
21197 validate_region (&from, &to);
21198 from_pos = XINT (from);
21199 to_pos = XINT (to);
21200 if (from_pos >= ZV)
21201 return Qnil;
21202
21203 /* Set up the bidi iterator. */
21204 itb_data = bidi_shelve_cache ();
21205 from_bpos = CHAR_TO_BYTE (from_pos);
21206 if (from_pos == BEGV)
21207 {
21208 itb.charpos = BEGV;
21209 itb.bytepos = BEGV_BYTE;
21210 }
21211 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21212 {
21213 itb.charpos = from_pos;
21214 itb.bytepos = from_bpos;
21215 }
21216 else
21217 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21218 -1, &itb.bytepos);
21219 itb.paragraph_dir = NEUTRAL_DIR;
21220 itb.string.s = NULL;
21221 itb.string.lstring = Qnil;
21222 itb.string.bufpos = 0;
21223 itb.string.from_disp_str = false;
21224 itb.string.unibyte = false;
21225 itb.w = w;
21226 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21227 }
21228
21229 ptrdiff_t found;
21230 do {
21231 /* For the purposes of this function, the actual base direction of
21232 the paragraph doesn't matter, so just set it to L2R. */
21233 bidi_paragraph_init (L2R, &itb, false);
21234 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21235 ;
21236 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21237
21238 bidi_unshelve_cache (itb_data, false);
21239 set_buffer_temp (old);
21240
21241 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21242 }
21243
21244 DEFUN ("move-point-visually", Fmove_point_visually,
21245 Smove_point_visually, 1, 1, 0,
21246 doc: /* Move point in the visual order in the specified DIRECTION.
21247 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21248 left.
21249
21250 Value is the new character position of point. */)
21251 (Lisp_Object direction)
21252 {
21253 struct window *w = XWINDOW (selected_window);
21254 struct buffer *b = XBUFFER (w->contents);
21255 struct glyph_row *row;
21256 int dir;
21257 Lisp_Object paragraph_dir;
21258
21259 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21260 (!(ROW)->continued_p \
21261 && NILP ((GLYPH)->object) \
21262 && (GLYPH)->type == CHAR_GLYPH \
21263 && (GLYPH)->u.ch == ' ' \
21264 && (GLYPH)->charpos >= 0 \
21265 && !(GLYPH)->avoid_cursor_p)
21266
21267 CHECK_NUMBER (direction);
21268 dir = XINT (direction);
21269 if (dir > 0)
21270 dir = 1;
21271 else
21272 dir = -1;
21273
21274 /* If current matrix is up-to-date, we can use the information
21275 recorded in the glyphs, at least as long as the goal is on the
21276 screen. */
21277 if (w->window_end_valid
21278 && !windows_or_buffers_changed
21279 && b
21280 && !b->clip_changed
21281 && !b->prevent_redisplay_optimizations_p
21282 && !window_outdated (w)
21283 /* We rely below on the cursor coordinates to be up to date, but
21284 we cannot trust them if some command moved point since the
21285 last complete redisplay. */
21286 && w->last_point == BUF_PT (b)
21287 && w->cursor.vpos >= 0
21288 && w->cursor.vpos < w->current_matrix->nrows
21289 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21290 {
21291 struct glyph *g = row->glyphs[TEXT_AREA];
21292 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21293 struct glyph *gpt = g + w->cursor.hpos;
21294
21295 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21296 {
21297 if (BUFFERP (g->object) && g->charpos != PT)
21298 {
21299 SET_PT (g->charpos);
21300 w->cursor.vpos = -1;
21301 return make_number (PT);
21302 }
21303 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21304 {
21305 ptrdiff_t new_pos;
21306
21307 if (BUFFERP (gpt->object))
21308 {
21309 new_pos = PT;
21310 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21311 new_pos += (row->reversed_p ? -dir : dir);
21312 else
21313 new_pos -= (row->reversed_p ? -dir : dir);
21314 }
21315 else if (BUFFERP (g->object))
21316 new_pos = g->charpos;
21317 else
21318 break;
21319 SET_PT (new_pos);
21320 w->cursor.vpos = -1;
21321 return make_number (PT);
21322 }
21323 else if (ROW_GLYPH_NEWLINE_P (row, g))
21324 {
21325 /* Glyphs inserted at the end of a non-empty line for
21326 positioning the cursor have zero charpos, so we must
21327 deduce the value of point by other means. */
21328 if (g->charpos > 0)
21329 SET_PT (g->charpos);
21330 else if (row->ends_at_zv_p && PT != ZV)
21331 SET_PT (ZV);
21332 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21333 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21334 else
21335 break;
21336 w->cursor.vpos = -1;
21337 return make_number (PT);
21338 }
21339 }
21340 if (g == e || NILP (g->object))
21341 {
21342 if (row->truncated_on_left_p || row->truncated_on_right_p)
21343 goto simulate_display;
21344 if (!row->reversed_p)
21345 row += dir;
21346 else
21347 row -= dir;
21348 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21349 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21350 goto simulate_display;
21351
21352 if (dir > 0)
21353 {
21354 if (row->reversed_p && !row->continued_p)
21355 {
21356 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21357 w->cursor.vpos = -1;
21358 return make_number (PT);
21359 }
21360 g = row->glyphs[TEXT_AREA];
21361 e = g + row->used[TEXT_AREA];
21362 for ( ; g < e; g++)
21363 {
21364 if (BUFFERP (g->object)
21365 /* Empty lines have only one glyph, which stands
21366 for the newline, and whose charpos is the
21367 buffer position of the newline. */
21368 || ROW_GLYPH_NEWLINE_P (row, g)
21369 /* When the buffer ends in a newline, the line at
21370 EOB also has one glyph, but its charpos is -1. */
21371 || (row->ends_at_zv_p
21372 && !row->reversed_p
21373 && NILP (g->object)
21374 && g->type == CHAR_GLYPH
21375 && g->u.ch == ' '))
21376 {
21377 if (g->charpos > 0)
21378 SET_PT (g->charpos);
21379 else if (!row->reversed_p
21380 && row->ends_at_zv_p
21381 && PT != ZV)
21382 SET_PT (ZV);
21383 else
21384 continue;
21385 w->cursor.vpos = -1;
21386 return make_number (PT);
21387 }
21388 }
21389 }
21390 else
21391 {
21392 if (!row->reversed_p && !row->continued_p)
21393 {
21394 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21395 w->cursor.vpos = -1;
21396 return make_number (PT);
21397 }
21398 e = row->glyphs[TEXT_AREA];
21399 g = e + row->used[TEXT_AREA] - 1;
21400 for ( ; g >= e; g--)
21401 {
21402 if (BUFFERP (g->object)
21403 || (ROW_GLYPH_NEWLINE_P (row, g)
21404 && g->charpos > 0)
21405 /* Empty R2L lines on GUI frames have the buffer
21406 position of the newline stored in the stretch
21407 glyph. */
21408 || g->type == STRETCH_GLYPH
21409 || (row->ends_at_zv_p
21410 && row->reversed_p
21411 && NILP (g->object)
21412 && g->type == CHAR_GLYPH
21413 && g->u.ch == ' '))
21414 {
21415 if (g->charpos > 0)
21416 SET_PT (g->charpos);
21417 else if (row->reversed_p
21418 && row->ends_at_zv_p
21419 && PT != ZV)
21420 SET_PT (ZV);
21421 else
21422 continue;
21423 w->cursor.vpos = -1;
21424 return make_number (PT);
21425 }
21426 }
21427 }
21428 }
21429 }
21430
21431 simulate_display:
21432
21433 /* If we wind up here, we failed to move by using the glyphs, so we
21434 need to simulate display instead. */
21435
21436 if (b)
21437 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21438 else
21439 paragraph_dir = Qleft_to_right;
21440 if (EQ (paragraph_dir, Qright_to_left))
21441 dir = -dir;
21442 if (PT <= BEGV && dir < 0)
21443 xsignal0 (Qbeginning_of_buffer);
21444 else if (PT >= ZV && dir > 0)
21445 xsignal0 (Qend_of_buffer);
21446 else
21447 {
21448 struct text_pos pt;
21449 struct it it;
21450 int pt_x, target_x, pixel_width, pt_vpos;
21451 bool at_eol_p;
21452 bool overshoot_expected = false;
21453 bool target_is_eol_p = false;
21454
21455 /* Setup the arena. */
21456 SET_TEXT_POS (pt, PT, PT_BYTE);
21457 start_display (&it, w, pt);
21458 /* When lines are truncated, we could be called with point
21459 outside of the windows edges, in which case move_it_*
21460 functions either prematurely stop at window's edge or jump to
21461 the next screen line, whereas we rely below on our ability to
21462 reach point, in order to start from its X coordinate. So we
21463 need to disregard the window's horizontal extent in that case. */
21464 if (it.line_wrap == TRUNCATE)
21465 it.last_visible_x = INFINITY;
21466
21467 if (it.cmp_it.id < 0
21468 && it.method == GET_FROM_STRING
21469 && it.area == TEXT_AREA
21470 && it.string_from_display_prop_p
21471 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21472 overshoot_expected = true;
21473
21474 /* Find the X coordinate of point. We start from the beginning
21475 of this or previous line to make sure we are before point in
21476 the logical order (since the move_it_* functions can only
21477 move forward). */
21478 reseat:
21479 reseat_at_previous_visible_line_start (&it);
21480 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21481 if (IT_CHARPOS (it) != PT)
21482 {
21483 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21484 -1, -1, -1, MOVE_TO_POS);
21485 /* If we missed point because the character there is
21486 displayed out of a display vector that has more than one
21487 glyph, retry expecting overshoot. */
21488 if (it.method == GET_FROM_DISPLAY_VECTOR
21489 && it.current.dpvec_index > 0
21490 && !overshoot_expected)
21491 {
21492 overshoot_expected = true;
21493 goto reseat;
21494 }
21495 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21496 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21497 }
21498 pt_x = it.current_x;
21499 pt_vpos = it.vpos;
21500 if (dir > 0 || overshoot_expected)
21501 {
21502 struct glyph_row *row = it.glyph_row;
21503
21504 /* When point is at beginning of line, we don't have
21505 information about the glyph there loaded into struct
21506 it. Calling get_next_display_element fixes that. */
21507 if (pt_x == 0)
21508 get_next_display_element (&it);
21509 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21510 it.glyph_row = NULL;
21511 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21512 it.glyph_row = row;
21513 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21514 it, lest it will become out of sync with it's buffer
21515 position. */
21516 it.current_x = pt_x;
21517 }
21518 else
21519 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21520 pixel_width = it.pixel_width;
21521 if (overshoot_expected && at_eol_p)
21522 pixel_width = 0;
21523 else if (pixel_width <= 0)
21524 pixel_width = 1;
21525
21526 /* If there's a display string (or something similar) at point,
21527 we are actually at the glyph to the left of point, so we need
21528 to correct the X coordinate. */
21529 if (overshoot_expected)
21530 {
21531 if (it.bidi_p)
21532 pt_x += pixel_width * it.bidi_it.scan_dir;
21533 else
21534 pt_x += pixel_width;
21535 }
21536
21537 /* Compute target X coordinate, either to the left or to the
21538 right of point. On TTY frames, all characters have the same
21539 pixel width of 1, so we can use that. On GUI frames we don't
21540 have an easy way of getting at the pixel width of the
21541 character to the left of point, so we use a different method
21542 of getting to that place. */
21543 if (dir > 0)
21544 target_x = pt_x + pixel_width;
21545 else
21546 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21547
21548 /* Target X coordinate could be one line above or below the line
21549 of point, in which case we need to adjust the target X
21550 coordinate. Also, if moving to the left, we need to begin at
21551 the left edge of the point's screen line. */
21552 if (dir < 0)
21553 {
21554 if (pt_x > 0)
21555 {
21556 start_display (&it, w, pt);
21557 if (it.line_wrap == TRUNCATE)
21558 it.last_visible_x = INFINITY;
21559 reseat_at_previous_visible_line_start (&it);
21560 it.current_x = it.current_y = it.hpos = 0;
21561 if (pt_vpos != 0)
21562 move_it_by_lines (&it, pt_vpos);
21563 }
21564 else
21565 {
21566 move_it_by_lines (&it, -1);
21567 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21568 target_is_eol_p = true;
21569 /* Under word-wrap, we don't know the x coordinate of
21570 the last character displayed on the previous line,
21571 which immediately precedes the wrap point. To find
21572 out its x coordinate, we try moving to the right
21573 margin of the window, which will stop at the wrap
21574 point, and then reset target_x to point at the
21575 character that precedes the wrap point. This is not
21576 needed on GUI frames, because (see below) there we
21577 move from the left margin one grapheme cluster at a
21578 time, and stop when we hit the wrap point. */
21579 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21580 {
21581 void *it_data = NULL;
21582 struct it it2;
21583
21584 SAVE_IT (it2, it, it_data);
21585 move_it_in_display_line_to (&it, ZV, target_x,
21586 MOVE_TO_POS | MOVE_TO_X);
21587 /* If we arrived at target_x, that _is_ the last
21588 character on the previous line. */
21589 if (it.current_x != target_x)
21590 target_x = it.current_x - 1;
21591 RESTORE_IT (&it, &it2, it_data);
21592 }
21593 }
21594 }
21595 else
21596 {
21597 if (at_eol_p
21598 || (target_x >= it.last_visible_x
21599 && it.line_wrap != TRUNCATE))
21600 {
21601 if (pt_x > 0)
21602 move_it_by_lines (&it, 0);
21603 move_it_by_lines (&it, 1);
21604 target_x = 0;
21605 }
21606 }
21607
21608 /* Move to the target X coordinate. */
21609 #ifdef HAVE_WINDOW_SYSTEM
21610 /* On GUI frames, as we don't know the X coordinate of the
21611 character to the left of point, moving point to the left
21612 requires walking, one grapheme cluster at a time, until we
21613 find ourself at a place immediately to the left of the
21614 character at point. */
21615 if (FRAME_WINDOW_P (it.f) && dir < 0)
21616 {
21617 struct text_pos new_pos;
21618 enum move_it_result rc = MOVE_X_REACHED;
21619
21620 if (it.current_x == 0)
21621 get_next_display_element (&it);
21622 if (it.what == IT_COMPOSITION)
21623 {
21624 new_pos.charpos = it.cmp_it.charpos;
21625 new_pos.bytepos = -1;
21626 }
21627 else
21628 new_pos = it.current.pos;
21629
21630 while (it.current_x + it.pixel_width <= target_x
21631 && (rc == MOVE_X_REACHED
21632 /* Under word-wrap, move_it_in_display_line_to
21633 stops at correct coordinates, but sometimes
21634 returns MOVE_POS_MATCH_OR_ZV. */
21635 || (it.line_wrap == WORD_WRAP
21636 && rc == MOVE_POS_MATCH_OR_ZV)))
21637 {
21638 int new_x = it.current_x + it.pixel_width;
21639
21640 /* For composed characters, we want the position of the
21641 first character in the grapheme cluster (usually, the
21642 composition's base character), whereas it.current
21643 might give us the position of the _last_ one, e.g. if
21644 the composition is rendered in reverse due to bidi
21645 reordering. */
21646 if (it.what == IT_COMPOSITION)
21647 {
21648 new_pos.charpos = it.cmp_it.charpos;
21649 new_pos.bytepos = -1;
21650 }
21651 else
21652 new_pos = it.current.pos;
21653 if (new_x == it.current_x)
21654 new_x++;
21655 rc = move_it_in_display_line_to (&it, ZV, new_x,
21656 MOVE_TO_POS | MOVE_TO_X);
21657 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21658 break;
21659 }
21660 /* The previous position we saw in the loop is the one we
21661 want. */
21662 if (new_pos.bytepos == -1)
21663 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21664 it.current.pos = new_pos;
21665 }
21666 else
21667 #endif
21668 if (it.current_x != target_x)
21669 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21670
21671 /* If we ended up in a display string that covers point, move to
21672 buffer position to the right in the visual order. */
21673 if (dir > 0)
21674 {
21675 while (IT_CHARPOS (it) == PT)
21676 {
21677 set_iterator_to_next (&it, false);
21678 if (!get_next_display_element (&it))
21679 break;
21680 }
21681 }
21682
21683 /* Move point to that position. */
21684 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21685 }
21686
21687 return make_number (PT);
21688
21689 #undef ROW_GLYPH_NEWLINE_P
21690 }
21691
21692 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21693 Sbidi_resolved_levels, 0, 1, 0,
21694 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21695
21696 The resolved levels are produced by the Emacs bidi reordering engine
21697 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21698 read the Unicode Standard Annex 9 (UAX#9) for background information
21699 about these levels.
21700
21701 VPOS is the zero-based number of the current window's screen line
21702 for which to produce the resolved levels. If VPOS is nil or omitted,
21703 it defaults to the screen line of point. If the window displays a
21704 header line, VPOS of zero will report on the header line, and first
21705 line of text in the window will have VPOS of 1.
21706
21707 Value is an array of resolved levels, indexed by glyph number.
21708 Glyphs are numbered from zero starting from the beginning of the
21709 screen line, i.e. the left edge of the window for left-to-right lines
21710 and from the right edge for right-to-left lines. The resolved levels
21711 are produced only for the window's text area; text in display margins
21712 is not included.
21713
21714 If the selected window's display is not up-to-date, or if the specified
21715 screen line does not display text, this function returns nil. It is
21716 highly recommended to bind this function to some simple key, like F8,
21717 in order to avoid these problems.
21718
21719 This function exists mainly for testing the correctness of the
21720 Emacs UBA implementation, in particular with the test suite. */)
21721 (Lisp_Object vpos)
21722 {
21723 struct window *w = XWINDOW (selected_window);
21724 struct buffer *b = XBUFFER (w->contents);
21725 int nrow;
21726 struct glyph_row *row;
21727
21728 if (NILP (vpos))
21729 {
21730 int d1, d2, d3, d4, d5;
21731
21732 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21733 }
21734 else
21735 {
21736 CHECK_NUMBER_COERCE_MARKER (vpos);
21737 nrow = XINT (vpos);
21738 }
21739
21740 /* We require up-to-date glyph matrix for this window. */
21741 if (w->window_end_valid
21742 && !windows_or_buffers_changed
21743 && b
21744 && !b->clip_changed
21745 && !b->prevent_redisplay_optimizations_p
21746 && !window_outdated (w)
21747 && nrow >= 0
21748 && nrow < w->current_matrix->nrows
21749 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21750 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21751 {
21752 struct glyph *g, *e, *g1;
21753 int nglyphs, i;
21754 Lisp_Object levels;
21755
21756 if (!row->reversed_p) /* Left-to-right glyph row. */
21757 {
21758 g = g1 = row->glyphs[TEXT_AREA];
21759 e = g + row->used[TEXT_AREA];
21760
21761 /* Skip over glyphs at the start of the row that was
21762 generated by redisplay for its own needs. */
21763 while (g < e
21764 && NILP (g->object)
21765 && g->charpos < 0)
21766 g++;
21767 g1 = g;
21768
21769 /* Count the "interesting" glyphs in this row. */
21770 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21771 nglyphs++;
21772
21773 /* Create and fill the array. */
21774 levels = make_uninit_vector (nglyphs);
21775 for (i = 0; g1 < g; i++, g1++)
21776 ASET (levels, i, make_number (g1->resolved_level));
21777 }
21778 else /* Right-to-left glyph row. */
21779 {
21780 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21781 e = row->glyphs[TEXT_AREA] - 1;
21782 while (g > e
21783 && NILP (g->object)
21784 && g->charpos < 0)
21785 g--;
21786 g1 = g;
21787 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21788 nglyphs++;
21789 levels = make_uninit_vector (nglyphs);
21790 for (i = 0; g1 > g; i++, g1--)
21791 ASET (levels, i, make_number (g1->resolved_level));
21792 }
21793 return levels;
21794 }
21795 else
21796 return Qnil;
21797 }
21798
21799
21800 \f
21801 /***********************************************************************
21802 Menu Bar
21803 ***********************************************************************/
21804
21805 /* Redisplay the menu bar in the frame for window W.
21806
21807 The menu bar of X frames that don't have X toolkit support is
21808 displayed in a special window W->frame->menu_bar_window.
21809
21810 The menu bar of terminal frames is treated specially as far as
21811 glyph matrices are concerned. Menu bar lines are not part of
21812 windows, so the update is done directly on the frame matrix rows
21813 for the menu bar. */
21814
21815 static void
21816 display_menu_bar (struct window *w)
21817 {
21818 struct frame *f = XFRAME (WINDOW_FRAME (w));
21819 struct it it;
21820 Lisp_Object items;
21821 int i;
21822
21823 /* Don't do all this for graphical frames. */
21824 #ifdef HAVE_NTGUI
21825 if (FRAME_W32_P (f))
21826 return;
21827 #endif
21828 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21829 if (FRAME_X_P (f))
21830 return;
21831 #endif
21832
21833 #ifdef HAVE_NS
21834 if (FRAME_NS_P (f))
21835 return;
21836 #endif /* HAVE_NS */
21837
21838 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21839 eassert (!FRAME_WINDOW_P (f));
21840 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21841 it.first_visible_x = 0;
21842 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21843 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21844 if (FRAME_WINDOW_P (f))
21845 {
21846 /* Menu bar lines are displayed in the desired matrix of the
21847 dummy window menu_bar_window. */
21848 struct window *menu_w;
21849 menu_w = XWINDOW (f->menu_bar_window);
21850 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21851 MENU_FACE_ID);
21852 it.first_visible_x = 0;
21853 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21854 }
21855 else
21856 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21857 {
21858 /* This is a TTY frame, i.e. character hpos/vpos are used as
21859 pixel x/y. */
21860 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21861 MENU_FACE_ID);
21862 it.first_visible_x = 0;
21863 it.last_visible_x = FRAME_COLS (f);
21864 }
21865
21866 /* FIXME: This should be controlled by a user option. See the
21867 comments in redisplay_tool_bar and display_mode_line about
21868 this. */
21869 it.paragraph_embedding = L2R;
21870
21871 /* Clear all rows of the menu bar. */
21872 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21873 {
21874 struct glyph_row *row = it.glyph_row + i;
21875 clear_glyph_row (row);
21876 row->enabled_p = true;
21877 row->full_width_p = true;
21878 row->reversed_p = false;
21879 }
21880
21881 /* Display all items of the menu bar. */
21882 items = FRAME_MENU_BAR_ITEMS (it.f);
21883 for (i = 0; i < ASIZE (items); i += 4)
21884 {
21885 Lisp_Object string;
21886
21887 /* Stop at nil string. */
21888 string = AREF (items, i + 1);
21889 if (NILP (string))
21890 break;
21891
21892 /* Remember where item was displayed. */
21893 ASET (items, i + 3, make_number (it.hpos));
21894
21895 /* Display the item, pad with one space. */
21896 if (it.current_x < it.last_visible_x)
21897 display_string (NULL, string, Qnil, 0, 0, &it,
21898 SCHARS (string) + 1, 0, 0, -1);
21899 }
21900
21901 /* Fill out the line with spaces. */
21902 if (it.current_x < it.last_visible_x)
21903 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21904
21905 /* Compute the total height of the lines. */
21906 compute_line_metrics (&it);
21907 }
21908
21909 /* Deep copy of a glyph row, including the glyphs. */
21910 static void
21911 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21912 {
21913 struct glyph *pointers[1 + LAST_AREA];
21914 int to_used = to->used[TEXT_AREA];
21915
21916 /* Save glyph pointers of TO. */
21917 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21918
21919 /* Do a structure assignment. */
21920 *to = *from;
21921
21922 /* Restore original glyph pointers of TO. */
21923 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21924
21925 /* Copy the glyphs. */
21926 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21927 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21928
21929 /* If we filled only part of the TO row, fill the rest with
21930 space_glyph (which will display as empty space). */
21931 if (to_used > from->used[TEXT_AREA])
21932 fill_up_frame_row_with_spaces (to, to_used);
21933 }
21934
21935 /* Display one menu item on a TTY, by overwriting the glyphs in the
21936 frame F's desired glyph matrix with glyphs produced from the menu
21937 item text. Called from term.c to display TTY drop-down menus one
21938 item at a time.
21939
21940 ITEM_TEXT is the menu item text as a C string.
21941
21942 FACE_ID is the face ID to be used for this menu item. FACE_ID
21943 could specify one of 3 faces: a face for an enabled item, a face
21944 for a disabled item, or a face for a selected item.
21945
21946 X and Y are coordinates of the first glyph in the frame's desired
21947 matrix to be overwritten by the menu item. Since this is a TTY, Y
21948 is the zero-based number of the glyph row and X is the zero-based
21949 glyph number in the row, starting from left, where to start
21950 displaying the item.
21951
21952 SUBMENU means this menu item drops down a submenu, which
21953 should be indicated by displaying a proper visual cue after the
21954 item text. */
21955
21956 void
21957 display_tty_menu_item (const char *item_text, int width, int face_id,
21958 int x, int y, bool submenu)
21959 {
21960 struct it it;
21961 struct frame *f = SELECTED_FRAME ();
21962 struct window *w = XWINDOW (f->selected_window);
21963 struct glyph_row *row;
21964 size_t item_len = strlen (item_text);
21965
21966 eassert (FRAME_TERMCAP_P (f));
21967
21968 /* Don't write beyond the matrix's last row. This can happen for
21969 TTY screens that are not high enough to show the entire menu.
21970 (This is actually a bit of defensive programming, as
21971 tty_menu_display already limits the number of menu items to one
21972 less than the number of screen lines.) */
21973 if (y >= f->desired_matrix->nrows)
21974 return;
21975
21976 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21977 it.first_visible_x = 0;
21978 it.last_visible_x = FRAME_COLS (f) - 1;
21979 row = it.glyph_row;
21980 /* Start with the row contents from the current matrix. */
21981 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21982 bool saved_width = row->full_width_p;
21983 row->full_width_p = true;
21984 bool saved_reversed = row->reversed_p;
21985 row->reversed_p = false;
21986 row->enabled_p = true;
21987
21988 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21989 desired face. */
21990 eassert (x < f->desired_matrix->matrix_w);
21991 it.current_x = it.hpos = x;
21992 it.current_y = it.vpos = y;
21993 int saved_used = row->used[TEXT_AREA];
21994 bool saved_truncated = row->truncated_on_right_p;
21995 row->used[TEXT_AREA] = x;
21996 it.face_id = face_id;
21997 it.line_wrap = TRUNCATE;
21998
21999 /* FIXME: This should be controlled by a user option. See the
22000 comments in redisplay_tool_bar and display_mode_line about this.
22001 Also, if paragraph_embedding could ever be R2L, changes will be
22002 needed to avoid shifting to the right the row characters in
22003 term.c:append_glyph. */
22004 it.paragraph_embedding = L2R;
22005
22006 /* Pad with a space on the left. */
22007 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22008 width--;
22009 /* Display the menu item, pad with spaces to WIDTH. */
22010 if (submenu)
22011 {
22012 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22013 item_len, 0, FRAME_COLS (f) - 1, -1);
22014 width -= item_len;
22015 /* Indicate with " >" that there's a submenu. */
22016 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22017 FRAME_COLS (f) - 1, -1);
22018 }
22019 else
22020 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22021 width, 0, FRAME_COLS (f) - 1, -1);
22022
22023 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22024 row->truncated_on_right_p = saved_truncated;
22025 row->hash = row_hash (row);
22026 row->full_width_p = saved_width;
22027 row->reversed_p = saved_reversed;
22028 }
22029 \f
22030 /***********************************************************************
22031 Mode Line
22032 ***********************************************************************/
22033
22034 /* Redisplay mode lines in the window tree whose root is WINDOW.
22035 If FORCE, redisplay mode lines unconditionally.
22036 Otherwise, redisplay only mode lines that are garbaged. Value is
22037 the number of windows whose mode lines were redisplayed. */
22038
22039 static int
22040 redisplay_mode_lines (Lisp_Object window, bool force)
22041 {
22042 int nwindows = 0;
22043
22044 while (!NILP (window))
22045 {
22046 struct window *w = XWINDOW (window);
22047
22048 if (WINDOWP (w->contents))
22049 nwindows += redisplay_mode_lines (w->contents, force);
22050 else if (force
22051 || FRAME_GARBAGED_P (XFRAME (w->frame))
22052 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22053 {
22054 struct text_pos lpoint;
22055 struct buffer *old = current_buffer;
22056
22057 /* Set the window's buffer for the mode line display. */
22058 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22059 set_buffer_internal_1 (XBUFFER (w->contents));
22060
22061 /* Point refers normally to the selected window. For any
22062 other window, set up appropriate value. */
22063 if (!EQ (window, selected_window))
22064 {
22065 struct text_pos pt;
22066
22067 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22068 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22069 }
22070
22071 /* Display mode lines. */
22072 clear_glyph_matrix (w->desired_matrix);
22073 if (display_mode_lines (w))
22074 ++nwindows;
22075
22076 /* Restore old settings. */
22077 set_buffer_internal_1 (old);
22078 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22079 }
22080
22081 window = w->next;
22082 }
22083
22084 return nwindows;
22085 }
22086
22087
22088 /* Display the mode and/or header line of window W. Value is the
22089 sum number of mode lines and header lines displayed. */
22090
22091 static int
22092 display_mode_lines (struct window *w)
22093 {
22094 Lisp_Object old_selected_window = selected_window;
22095 Lisp_Object old_selected_frame = selected_frame;
22096 Lisp_Object new_frame = w->frame;
22097 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22098 int n = 0;
22099
22100 selected_frame = new_frame;
22101 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22102 or window's point, then we'd need select_window_1 here as well. */
22103 XSETWINDOW (selected_window, w);
22104 XFRAME (new_frame)->selected_window = selected_window;
22105
22106 /* These will be set while the mode line specs are processed. */
22107 line_number_displayed = false;
22108 w->column_number_displayed = -1;
22109
22110 if (WINDOW_WANTS_MODELINE_P (w))
22111 {
22112 struct window *sel_w = XWINDOW (old_selected_window);
22113
22114 /* Select mode line face based on the real selected window. */
22115 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22116 BVAR (current_buffer, mode_line_format));
22117 ++n;
22118 }
22119
22120 if (WINDOW_WANTS_HEADER_LINE_P (w))
22121 {
22122 display_mode_line (w, HEADER_LINE_FACE_ID,
22123 BVAR (current_buffer, header_line_format));
22124 ++n;
22125 }
22126
22127 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22128 selected_frame = old_selected_frame;
22129 selected_window = old_selected_window;
22130 if (n > 0)
22131 w->must_be_updated_p = true;
22132 return n;
22133 }
22134
22135
22136 /* Display mode or header line of window W. FACE_ID specifies which
22137 line to display; it is either MODE_LINE_FACE_ID or
22138 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22139 display. Value is the pixel height of the mode/header line
22140 displayed. */
22141
22142 static int
22143 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22144 {
22145 struct it it;
22146 struct face *face;
22147 ptrdiff_t count = SPECPDL_INDEX ();
22148
22149 init_iterator (&it, w, -1, -1, NULL, face_id);
22150 /* Don't extend on a previously drawn mode-line.
22151 This may happen if called from pos_visible_p. */
22152 it.glyph_row->enabled_p = false;
22153 prepare_desired_row (w, it.glyph_row, true);
22154
22155 it.glyph_row->mode_line_p = true;
22156
22157 /* FIXME: This should be controlled by a user option. But
22158 supporting such an option is not trivial, since the mode line is
22159 made up of many separate strings. */
22160 it.paragraph_embedding = L2R;
22161
22162 record_unwind_protect (unwind_format_mode_line,
22163 format_mode_line_unwind_data (NULL, NULL,
22164 Qnil, false));
22165
22166 mode_line_target = MODE_LINE_DISPLAY;
22167
22168 /* Temporarily make frame's keyboard the current kboard so that
22169 kboard-local variables in the mode_line_format will get the right
22170 values. */
22171 push_kboard (FRAME_KBOARD (it.f));
22172 record_unwind_save_match_data ();
22173 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22174 pop_kboard ();
22175
22176 unbind_to (count, Qnil);
22177
22178 /* Fill up with spaces. */
22179 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22180
22181 compute_line_metrics (&it);
22182 it.glyph_row->full_width_p = true;
22183 it.glyph_row->continued_p = false;
22184 it.glyph_row->truncated_on_left_p = false;
22185 it.glyph_row->truncated_on_right_p = false;
22186
22187 /* Make a 3D mode-line have a shadow at its right end. */
22188 face = FACE_FROM_ID (it.f, face_id);
22189 extend_face_to_end_of_line (&it);
22190 if (face->box != FACE_NO_BOX)
22191 {
22192 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22193 + it.glyph_row->used[TEXT_AREA] - 1);
22194 last->right_box_line_p = true;
22195 }
22196
22197 return it.glyph_row->height;
22198 }
22199
22200 /* Move element ELT in LIST to the front of LIST.
22201 Return the updated list. */
22202
22203 static Lisp_Object
22204 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22205 {
22206 register Lisp_Object tail, prev;
22207 register Lisp_Object tem;
22208
22209 tail = list;
22210 prev = Qnil;
22211 while (CONSP (tail))
22212 {
22213 tem = XCAR (tail);
22214
22215 if (EQ (elt, tem))
22216 {
22217 /* Splice out the link TAIL. */
22218 if (NILP (prev))
22219 list = XCDR (tail);
22220 else
22221 Fsetcdr (prev, XCDR (tail));
22222
22223 /* Now make it the first. */
22224 Fsetcdr (tail, list);
22225 return tail;
22226 }
22227 else
22228 prev = tail;
22229 tail = XCDR (tail);
22230 QUIT;
22231 }
22232
22233 /* Not found--return unchanged LIST. */
22234 return list;
22235 }
22236
22237 /* Contribute ELT to the mode line for window IT->w. How it
22238 translates into text depends on its data type.
22239
22240 IT describes the display environment in which we display, as usual.
22241
22242 DEPTH is the depth in recursion. It is used to prevent
22243 infinite recursion here.
22244
22245 FIELD_WIDTH is the number of characters the display of ELT should
22246 occupy in the mode line, and PRECISION is the maximum number of
22247 characters to display from ELT's representation. See
22248 display_string for details.
22249
22250 Returns the hpos of the end of the text generated by ELT.
22251
22252 PROPS is a property list to add to any string we encounter.
22253
22254 If RISKY, remove (disregard) any properties in any string
22255 we encounter, and ignore :eval and :propertize.
22256
22257 The global variable `mode_line_target' determines whether the
22258 output is passed to `store_mode_line_noprop',
22259 `store_mode_line_string', or `display_string'. */
22260
22261 static int
22262 display_mode_element (struct it *it, int depth, int field_width, int precision,
22263 Lisp_Object elt, Lisp_Object props, bool risky)
22264 {
22265 int n = 0, field, prec;
22266 bool literal = false;
22267
22268 tail_recurse:
22269 if (depth > 100)
22270 elt = build_string ("*too-deep*");
22271
22272 depth++;
22273
22274 switch (XTYPE (elt))
22275 {
22276 case Lisp_String:
22277 {
22278 /* A string: output it and check for %-constructs within it. */
22279 unsigned char c;
22280 ptrdiff_t offset = 0;
22281
22282 if (SCHARS (elt) > 0
22283 && (!NILP (props) || risky))
22284 {
22285 Lisp_Object oprops, aelt;
22286 oprops = Ftext_properties_at (make_number (0), elt);
22287
22288 /* If the starting string's properties are not what
22289 we want, translate the string. Also, if the string
22290 is risky, do that anyway. */
22291
22292 if (NILP (Fequal (props, oprops)) || risky)
22293 {
22294 /* If the starting string has properties,
22295 merge the specified ones onto the existing ones. */
22296 if (! NILP (oprops) && !risky)
22297 {
22298 Lisp_Object tem;
22299
22300 oprops = Fcopy_sequence (oprops);
22301 tem = props;
22302 while (CONSP (tem))
22303 {
22304 oprops = Fplist_put (oprops, XCAR (tem),
22305 XCAR (XCDR (tem)));
22306 tem = XCDR (XCDR (tem));
22307 }
22308 props = oprops;
22309 }
22310
22311 aelt = Fassoc (elt, mode_line_proptrans_alist);
22312 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22313 {
22314 /* AELT is what we want. Move it to the front
22315 without consing. */
22316 elt = XCAR (aelt);
22317 mode_line_proptrans_alist
22318 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22319 }
22320 else
22321 {
22322 Lisp_Object tem;
22323
22324 /* If AELT has the wrong props, it is useless.
22325 so get rid of it. */
22326 if (! NILP (aelt))
22327 mode_line_proptrans_alist
22328 = Fdelq (aelt, mode_line_proptrans_alist);
22329
22330 elt = Fcopy_sequence (elt);
22331 Fset_text_properties (make_number (0), Flength (elt),
22332 props, elt);
22333 /* Add this item to mode_line_proptrans_alist. */
22334 mode_line_proptrans_alist
22335 = Fcons (Fcons (elt, props),
22336 mode_line_proptrans_alist);
22337 /* Truncate mode_line_proptrans_alist
22338 to at most 50 elements. */
22339 tem = Fnthcdr (make_number (50),
22340 mode_line_proptrans_alist);
22341 if (! NILP (tem))
22342 XSETCDR (tem, Qnil);
22343 }
22344 }
22345 }
22346
22347 offset = 0;
22348
22349 if (literal)
22350 {
22351 prec = precision - n;
22352 switch (mode_line_target)
22353 {
22354 case MODE_LINE_NOPROP:
22355 case MODE_LINE_TITLE:
22356 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22357 break;
22358 case MODE_LINE_STRING:
22359 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22360 break;
22361 case MODE_LINE_DISPLAY:
22362 n += display_string (NULL, elt, Qnil, 0, 0, it,
22363 0, prec, 0, STRING_MULTIBYTE (elt));
22364 break;
22365 }
22366
22367 break;
22368 }
22369
22370 /* Handle the non-literal case. */
22371
22372 while ((precision <= 0 || n < precision)
22373 && SREF (elt, offset) != 0
22374 && (mode_line_target != MODE_LINE_DISPLAY
22375 || it->current_x < it->last_visible_x))
22376 {
22377 ptrdiff_t last_offset = offset;
22378
22379 /* Advance to end of string or next format specifier. */
22380 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22381 ;
22382
22383 if (offset - 1 != last_offset)
22384 {
22385 ptrdiff_t nchars, nbytes;
22386
22387 /* Output to end of string or up to '%'. Field width
22388 is length of string. Don't output more than
22389 PRECISION allows us. */
22390 offset--;
22391
22392 prec = c_string_width (SDATA (elt) + last_offset,
22393 offset - last_offset, precision - n,
22394 &nchars, &nbytes);
22395
22396 switch (mode_line_target)
22397 {
22398 case MODE_LINE_NOPROP:
22399 case MODE_LINE_TITLE:
22400 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22401 break;
22402 case MODE_LINE_STRING:
22403 {
22404 ptrdiff_t bytepos = last_offset;
22405 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22406 ptrdiff_t endpos = (precision <= 0
22407 ? string_byte_to_char (elt, offset)
22408 : charpos + nchars);
22409 Lisp_Object mode_string
22410 = Fsubstring (elt, make_number (charpos),
22411 make_number (endpos));
22412 n += store_mode_line_string (NULL, mode_string, false,
22413 0, 0, Qnil);
22414 }
22415 break;
22416 case MODE_LINE_DISPLAY:
22417 {
22418 ptrdiff_t bytepos = last_offset;
22419 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22420
22421 if (precision <= 0)
22422 nchars = string_byte_to_char (elt, offset) - charpos;
22423 n += display_string (NULL, elt, Qnil, 0, charpos,
22424 it, 0, nchars, 0,
22425 STRING_MULTIBYTE (elt));
22426 }
22427 break;
22428 }
22429 }
22430 else /* c == '%' */
22431 {
22432 ptrdiff_t percent_position = offset;
22433
22434 /* Get the specified minimum width. Zero means
22435 don't pad. */
22436 field = 0;
22437 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22438 field = field * 10 + c - '0';
22439
22440 /* Don't pad beyond the total padding allowed. */
22441 if (field_width - n > 0 && field > field_width - n)
22442 field = field_width - n;
22443
22444 /* Note that either PRECISION <= 0 or N < PRECISION. */
22445 prec = precision - n;
22446
22447 if (c == 'M')
22448 n += display_mode_element (it, depth, field, prec,
22449 Vglobal_mode_string, props,
22450 risky);
22451 else if (c != 0)
22452 {
22453 bool multibyte;
22454 ptrdiff_t bytepos, charpos;
22455 const char *spec;
22456 Lisp_Object string;
22457
22458 bytepos = percent_position;
22459 charpos = (STRING_MULTIBYTE (elt)
22460 ? string_byte_to_char (elt, bytepos)
22461 : bytepos);
22462 spec = decode_mode_spec (it->w, c, field, &string);
22463 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22464
22465 switch (mode_line_target)
22466 {
22467 case MODE_LINE_NOPROP:
22468 case MODE_LINE_TITLE:
22469 n += store_mode_line_noprop (spec, field, prec);
22470 break;
22471 case MODE_LINE_STRING:
22472 {
22473 Lisp_Object tem = build_string (spec);
22474 props = Ftext_properties_at (make_number (charpos), elt);
22475 /* Should only keep face property in props */
22476 n += store_mode_line_string (NULL, tem, false,
22477 field, prec, props);
22478 }
22479 break;
22480 case MODE_LINE_DISPLAY:
22481 {
22482 int nglyphs_before, nwritten;
22483
22484 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22485 nwritten = display_string (spec, string, elt,
22486 charpos, 0, it,
22487 field, prec, 0,
22488 multibyte);
22489
22490 /* Assign to the glyphs written above the
22491 string where the `%x' came from, position
22492 of the `%'. */
22493 if (nwritten > 0)
22494 {
22495 struct glyph *glyph
22496 = (it->glyph_row->glyphs[TEXT_AREA]
22497 + nglyphs_before);
22498 int i;
22499
22500 for (i = 0; i < nwritten; ++i)
22501 {
22502 glyph[i].object = elt;
22503 glyph[i].charpos = charpos;
22504 }
22505
22506 n += nwritten;
22507 }
22508 }
22509 break;
22510 }
22511 }
22512 else /* c == 0 */
22513 break;
22514 }
22515 }
22516 }
22517 break;
22518
22519 case Lisp_Symbol:
22520 /* A symbol: process the value of the symbol recursively
22521 as if it appeared here directly. Avoid error if symbol void.
22522 Special case: if value of symbol is a string, output the string
22523 literally. */
22524 {
22525 register Lisp_Object tem;
22526
22527 /* If the variable is not marked as risky to set
22528 then its contents are risky to use. */
22529 if (NILP (Fget (elt, Qrisky_local_variable)))
22530 risky = true;
22531
22532 tem = Fboundp (elt);
22533 if (!NILP (tem))
22534 {
22535 tem = Fsymbol_value (elt);
22536 /* If value is a string, output that string literally:
22537 don't check for % within it. */
22538 if (STRINGP (tem))
22539 literal = true;
22540
22541 if (!EQ (tem, elt))
22542 {
22543 /* Give up right away for nil or t. */
22544 elt = tem;
22545 goto tail_recurse;
22546 }
22547 }
22548 }
22549 break;
22550
22551 case Lisp_Cons:
22552 {
22553 register Lisp_Object car, tem;
22554
22555 /* A cons cell: five distinct cases.
22556 If first element is :eval or :propertize, do something special.
22557 If first element is a string or a cons, process all the elements
22558 and effectively concatenate them.
22559 If first element is a negative number, truncate displaying cdr to
22560 at most that many characters. If positive, pad (with spaces)
22561 to at least that many characters.
22562 If first element is a symbol, process the cadr or caddr recursively
22563 according to whether the symbol's value is non-nil or nil. */
22564 car = XCAR (elt);
22565 if (EQ (car, QCeval))
22566 {
22567 /* An element of the form (:eval FORM) means evaluate FORM
22568 and use the result as mode line elements. */
22569
22570 if (risky)
22571 break;
22572
22573 if (CONSP (XCDR (elt)))
22574 {
22575 Lisp_Object spec;
22576 spec = safe__eval (true, XCAR (XCDR (elt)));
22577 n += display_mode_element (it, depth, field_width - n,
22578 precision - n, spec, props,
22579 risky);
22580 }
22581 }
22582 else if (EQ (car, QCpropertize))
22583 {
22584 /* An element of the form (:propertize ELT PROPS...)
22585 means display ELT but applying properties PROPS. */
22586
22587 if (risky)
22588 break;
22589
22590 if (CONSP (XCDR (elt)))
22591 n += display_mode_element (it, depth, field_width - n,
22592 precision - n, XCAR (XCDR (elt)),
22593 XCDR (XCDR (elt)), risky);
22594 }
22595 else if (SYMBOLP (car))
22596 {
22597 tem = Fboundp (car);
22598 elt = XCDR (elt);
22599 if (!CONSP (elt))
22600 goto invalid;
22601 /* elt is now the cdr, and we know it is a cons cell.
22602 Use its car if CAR has a non-nil value. */
22603 if (!NILP (tem))
22604 {
22605 tem = Fsymbol_value (car);
22606 if (!NILP (tem))
22607 {
22608 elt = XCAR (elt);
22609 goto tail_recurse;
22610 }
22611 }
22612 /* Symbol's value is nil (or symbol is unbound)
22613 Get the cddr of the original list
22614 and if possible find the caddr and use that. */
22615 elt = XCDR (elt);
22616 if (NILP (elt))
22617 break;
22618 else if (!CONSP (elt))
22619 goto invalid;
22620 elt = XCAR (elt);
22621 goto tail_recurse;
22622 }
22623 else if (INTEGERP (car))
22624 {
22625 register int lim = XINT (car);
22626 elt = XCDR (elt);
22627 if (lim < 0)
22628 {
22629 /* Negative int means reduce maximum width. */
22630 if (precision <= 0)
22631 precision = -lim;
22632 else
22633 precision = min (precision, -lim);
22634 }
22635 else if (lim > 0)
22636 {
22637 /* Padding specified. Don't let it be more than
22638 current maximum. */
22639 if (precision > 0)
22640 lim = min (precision, lim);
22641
22642 /* If that's more padding than already wanted, queue it.
22643 But don't reduce padding already specified even if
22644 that is beyond the current truncation point. */
22645 field_width = max (lim, field_width);
22646 }
22647 goto tail_recurse;
22648 }
22649 else if (STRINGP (car) || CONSP (car))
22650 {
22651 Lisp_Object halftail = elt;
22652 int len = 0;
22653
22654 while (CONSP (elt)
22655 && (precision <= 0 || n < precision))
22656 {
22657 n += display_mode_element (it, depth,
22658 /* Do padding only after the last
22659 element in the list. */
22660 (! CONSP (XCDR (elt))
22661 ? field_width - n
22662 : 0),
22663 precision - n, XCAR (elt),
22664 props, risky);
22665 elt = XCDR (elt);
22666 len++;
22667 if ((len & 1) == 0)
22668 halftail = XCDR (halftail);
22669 /* Check for cycle. */
22670 if (EQ (halftail, elt))
22671 break;
22672 }
22673 }
22674 }
22675 break;
22676
22677 default:
22678 invalid:
22679 elt = build_string ("*invalid*");
22680 goto tail_recurse;
22681 }
22682
22683 /* Pad to FIELD_WIDTH. */
22684 if (field_width > 0 && n < field_width)
22685 {
22686 switch (mode_line_target)
22687 {
22688 case MODE_LINE_NOPROP:
22689 case MODE_LINE_TITLE:
22690 n += store_mode_line_noprop ("", field_width - n, 0);
22691 break;
22692 case MODE_LINE_STRING:
22693 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
22694 Qnil);
22695 break;
22696 case MODE_LINE_DISPLAY:
22697 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22698 0, 0, 0);
22699 break;
22700 }
22701 }
22702
22703 return n;
22704 }
22705
22706 /* Store a mode-line string element in mode_line_string_list.
22707
22708 If STRING is non-null, display that C string. Otherwise, the Lisp
22709 string LISP_STRING is displayed.
22710
22711 FIELD_WIDTH is the minimum number of output glyphs to produce.
22712 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22713 with spaces. FIELD_WIDTH <= 0 means don't pad.
22714
22715 PRECISION is the maximum number of characters to output from
22716 STRING. PRECISION <= 0 means don't truncate the string.
22717
22718 If COPY_STRING, make a copy of LISP_STRING before adding
22719 properties to the string.
22720
22721 PROPS are the properties to add to the string.
22722 The mode_line_string_face face property is always added to the string.
22723 */
22724
22725 static int
22726 store_mode_line_string (const char *string, Lisp_Object lisp_string,
22727 bool copy_string,
22728 int field_width, int precision, Lisp_Object props)
22729 {
22730 ptrdiff_t len;
22731 int n = 0;
22732
22733 if (string != NULL)
22734 {
22735 len = strlen (string);
22736 if (precision > 0 && len > precision)
22737 len = precision;
22738 lisp_string = make_string (string, len);
22739 if (NILP (props))
22740 props = mode_line_string_face_prop;
22741 else if (!NILP (mode_line_string_face))
22742 {
22743 Lisp_Object face = Fplist_get (props, Qface);
22744 props = Fcopy_sequence (props);
22745 if (NILP (face))
22746 face = mode_line_string_face;
22747 else
22748 face = list2 (face, mode_line_string_face);
22749 props = Fplist_put (props, Qface, face);
22750 }
22751 Fadd_text_properties (make_number (0), make_number (len),
22752 props, lisp_string);
22753 }
22754 else
22755 {
22756 len = XFASTINT (Flength (lisp_string));
22757 if (precision > 0 && len > precision)
22758 {
22759 len = precision;
22760 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22761 precision = -1;
22762 }
22763 if (!NILP (mode_line_string_face))
22764 {
22765 Lisp_Object face;
22766 if (NILP (props))
22767 props = Ftext_properties_at (make_number (0), lisp_string);
22768 face = Fplist_get (props, Qface);
22769 if (NILP (face))
22770 face = mode_line_string_face;
22771 else
22772 face = list2 (face, mode_line_string_face);
22773 props = list2 (Qface, face);
22774 if (copy_string)
22775 lisp_string = Fcopy_sequence (lisp_string);
22776 }
22777 if (!NILP (props))
22778 Fadd_text_properties (make_number (0), make_number (len),
22779 props, lisp_string);
22780 }
22781
22782 if (len > 0)
22783 {
22784 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22785 n += len;
22786 }
22787
22788 if (field_width > len)
22789 {
22790 field_width -= len;
22791 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22792 if (!NILP (props))
22793 Fadd_text_properties (make_number (0), make_number (field_width),
22794 props, lisp_string);
22795 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22796 n += field_width;
22797 }
22798
22799 return n;
22800 }
22801
22802
22803 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22804 1, 4, 0,
22805 doc: /* Format a string out of a mode line format specification.
22806 First arg FORMAT specifies the mode line format (see `mode-line-format'
22807 for details) to use.
22808
22809 By default, the format is evaluated for the currently selected window.
22810
22811 Optional second arg FACE specifies the face property to put on all
22812 characters for which no face is specified. The value nil means the
22813 default face. The value t means whatever face the window's mode line
22814 currently uses (either `mode-line' or `mode-line-inactive',
22815 depending on whether the window is the selected window or not).
22816 An integer value means the value string has no text
22817 properties.
22818
22819 Optional third and fourth args WINDOW and BUFFER specify the window
22820 and buffer to use as the context for the formatting (defaults
22821 are the selected window and the WINDOW's buffer). */)
22822 (Lisp_Object format, Lisp_Object face,
22823 Lisp_Object window, Lisp_Object buffer)
22824 {
22825 struct it it;
22826 int len;
22827 struct window *w;
22828 struct buffer *old_buffer = NULL;
22829 int face_id;
22830 bool no_props = INTEGERP (face);
22831 ptrdiff_t count = SPECPDL_INDEX ();
22832 Lisp_Object str;
22833 int string_start = 0;
22834
22835 w = decode_any_window (window);
22836 XSETWINDOW (window, w);
22837
22838 if (NILP (buffer))
22839 buffer = w->contents;
22840 CHECK_BUFFER (buffer);
22841
22842 /* Make formatting the modeline a non-op when noninteractive, otherwise
22843 there will be problems later caused by a partially initialized frame. */
22844 if (NILP (format) || noninteractive)
22845 return empty_unibyte_string;
22846
22847 if (no_props)
22848 face = Qnil;
22849
22850 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22851 : EQ (face, Qt) ? (EQ (window, selected_window)
22852 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22853 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22854 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22855 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22856 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22857 : DEFAULT_FACE_ID;
22858
22859 old_buffer = current_buffer;
22860
22861 /* Save things including mode_line_proptrans_alist,
22862 and set that to nil so that we don't alter the outer value. */
22863 record_unwind_protect (unwind_format_mode_line,
22864 format_mode_line_unwind_data
22865 (XFRAME (WINDOW_FRAME (w)),
22866 old_buffer, selected_window, true));
22867 mode_line_proptrans_alist = Qnil;
22868
22869 Fselect_window (window, Qt);
22870 set_buffer_internal_1 (XBUFFER (buffer));
22871
22872 init_iterator (&it, w, -1, -1, NULL, face_id);
22873
22874 if (no_props)
22875 {
22876 mode_line_target = MODE_LINE_NOPROP;
22877 mode_line_string_face_prop = Qnil;
22878 mode_line_string_list = Qnil;
22879 string_start = MODE_LINE_NOPROP_LEN (0);
22880 }
22881 else
22882 {
22883 mode_line_target = MODE_LINE_STRING;
22884 mode_line_string_list = Qnil;
22885 mode_line_string_face = face;
22886 mode_line_string_face_prop
22887 = NILP (face) ? Qnil : list2 (Qface, face);
22888 }
22889
22890 push_kboard (FRAME_KBOARD (it.f));
22891 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22892 pop_kboard ();
22893
22894 if (no_props)
22895 {
22896 len = MODE_LINE_NOPROP_LEN (string_start);
22897 str = make_string (mode_line_noprop_buf + string_start, len);
22898 }
22899 else
22900 {
22901 mode_line_string_list = Fnreverse (mode_line_string_list);
22902 str = Fmapconcat (Qidentity, mode_line_string_list,
22903 empty_unibyte_string);
22904 }
22905
22906 unbind_to (count, Qnil);
22907 return str;
22908 }
22909
22910 /* Write a null-terminated, right justified decimal representation of
22911 the positive integer D to BUF using a minimal field width WIDTH. */
22912
22913 static void
22914 pint2str (register char *buf, register int width, register ptrdiff_t d)
22915 {
22916 register char *p = buf;
22917
22918 if (d <= 0)
22919 *p++ = '0';
22920 else
22921 {
22922 while (d > 0)
22923 {
22924 *p++ = d % 10 + '0';
22925 d /= 10;
22926 }
22927 }
22928
22929 for (width -= (int) (p - buf); width > 0; --width)
22930 *p++ = ' ';
22931 *p-- = '\0';
22932 while (p > buf)
22933 {
22934 d = *buf;
22935 *buf++ = *p;
22936 *p-- = d;
22937 }
22938 }
22939
22940 /* Write a null-terminated, right justified decimal and "human
22941 readable" representation of the nonnegative integer D to BUF using
22942 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22943
22944 static const char power_letter[] =
22945 {
22946 0, /* no letter */
22947 'k', /* kilo */
22948 'M', /* mega */
22949 'G', /* giga */
22950 'T', /* tera */
22951 'P', /* peta */
22952 'E', /* exa */
22953 'Z', /* zetta */
22954 'Y' /* yotta */
22955 };
22956
22957 static void
22958 pint2hrstr (char *buf, int width, ptrdiff_t d)
22959 {
22960 /* We aim to represent the nonnegative integer D as
22961 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22962 ptrdiff_t quotient = d;
22963 int remainder = 0;
22964 /* -1 means: do not use TENTHS. */
22965 int tenths = -1;
22966 int exponent = 0;
22967
22968 /* Length of QUOTIENT.TENTHS as a string. */
22969 int length;
22970
22971 char * psuffix;
22972 char * p;
22973
22974 if (quotient >= 1000)
22975 {
22976 /* Scale to the appropriate EXPONENT. */
22977 do
22978 {
22979 remainder = quotient % 1000;
22980 quotient /= 1000;
22981 exponent++;
22982 }
22983 while (quotient >= 1000);
22984
22985 /* Round to nearest and decide whether to use TENTHS or not. */
22986 if (quotient <= 9)
22987 {
22988 tenths = remainder / 100;
22989 if (remainder % 100 >= 50)
22990 {
22991 if (tenths < 9)
22992 tenths++;
22993 else
22994 {
22995 quotient++;
22996 if (quotient == 10)
22997 tenths = -1;
22998 else
22999 tenths = 0;
23000 }
23001 }
23002 }
23003 else
23004 if (remainder >= 500)
23005 {
23006 if (quotient < 999)
23007 quotient++;
23008 else
23009 {
23010 quotient = 1;
23011 exponent++;
23012 tenths = 0;
23013 }
23014 }
23015 }
23016
23017 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23018 if (tenths == -1 && quotient <= 99)
23019 if (quotient <= 9)
23020 length = 1;
23021 else
23022 length = 2;
23023 else
23024 length = 3;
23025 p = psuffix = buf + max (width, length);
23026
23027 /* Print EXPONENT. */
23028 *psuffix++ = power_letter[exponent];
23029 *psuffix = '\0';
23030
23031 /* Print TENTHS. */
23032 if (tenths >= 0)
23033 {
23034 *--p = '0' + tenths;
23035 *--p = '.';
23036 }
23037
23038 /* Print QUOTIENT. */
23039 do
23040 {
23041 int digit = quotient % 10;
23042 *--p = '0' + digit;
23043 }
23044 while ((quotient /= 10) != 0);
23045
23046 /* Print leading spaces. */
23047 while (buf < p)
23048 *--p = ' ';
23049 }
23050
23051 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23052 If EOL_FLAG, set also a mnemonic character for end-of-line
23053 type of CODING_SYSTEM. Return updated pointer into BUF. */
23054
23055 static unsigned char invalid_eol_type[] = "(*invalid*)";
23056
23057 static char *
23058 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23059 {
23060 Lisp_Object val;
23061 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23062 const unsigned char *eol_str;
23063 int eol_str_len;
23064 /* The EOL conversion we are using. */
23065 Lisp_Object eoltype;
23066
23067 val = CODING_SYSTEM_SPEC (coding_system);
23068 eoltype = Qnil;
23069
23070 if (!VECTORP (val)) /* Not yet decided. */
23071 {
23072 *buf++ = multibyte ? '-' : ' ';
23073 if (eol_flag)
23074 eoltype = eol_mnemonic_undecided;
23075 /* Don't mention EOL conversion if it isn't decided. */
23076 }
23077 else
23078 {
23079 Lisp_Object attrs;
23080 Lisp_Object eolvalue;
23081
23082 attrs = AREF (val, 0);
23083 eolvalue = AREF (val, 2);
23084
23085 *buf++ = multibyte
23086 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23087 : ' ';
23088
23089 if (eol_flag)
23090 {
23091 /* The EOL conversion that is normal on this system. */
23092
23093 if (NILP (eolvalue)) /* Not yet decided. */
23094 eoltype = eol_mnemonic_undecided;
23095 else if (VECTORP (eolvalue)) /* Not yet decided. */
23096 eoltype = eol_mnemonic_undecided;
23097 else /* eolvalue is Qunix, Qdos, or Qmac. */
23098 eoltype = (EQ (eolvalue, Qunix)
23099 ? eol_mnemonic_unix
23100 : EQ (eolvalue, Qdos)
23101 ? eol_mnemonic_dos : eol_mnemonic_mac);
23102 }
23103 }
23104
23105 if (eol_flag)
23106 {
23107 /* Mention the EOL conversion if it is not the usual one. */
23108 if (STRINGP (eoltype))
23109 {
23110 eol_str = SDATA (eoltype);
23111 eol_str_len = SBYTES (eoltype);
23112 }
23113 else if (CHARACTERP (eoltype))
23114 {
23115 int c = XFASTINT (eoltype);
23116 return buf + CHAR_STRING (c, (unsigned char *) buf);
23117 }
23118 else
23119 {
23120 eol_str = invalid_eol_type;
23121 eol_str_len = sizeof (invalid_eol_type) - 1;
23122 }
23123 memcpy (buf, eol_str, eol_str_len);
23124 buf += eol_str_len;
23125 }
23126
23127 return buf;
23128 }
23129
23130 /* Return a string for the output of a mode line %-spec for window W,
23131 generated by character C. FIELD_WIDTH > 0 means pad the string
23132 returned with spaces to that value. Return a Lisp string in
23133 *STRING if the resulting string is taken from that Lisp string.
23134
23135 Note we operate on the current buffer for most purposes. */
23136
23137 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23138
23139 static const char *
23140 decode_mode_spec (struct window *w, register int c, int field_width,
23141 Lisp_Object *string)
23142 {
23143 Lisp_Object obj;
23144 struct frame *f = XFRAME (WINDOW_FRAME (w));
23145 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23146 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23147 produce strings from numerical values, so limit preposterously
23148 large values of FIELD_WIDTH to avoid overrunning the buffer's
23149 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23150 bytes plus the terminating null. */
23151 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23152 struct buffer *b = current_buffer;
23153
23154 obj = Qnil;
23155 *string = Qnil;
23156
23157 switch (c)
23158 {
23159 case '*':
23160 if (!NILP (BVAR (b, read_only)))
23161 return "%";
23162 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23163 return "*";
23164 return "-";
23165
23166 case '+':
23167 /* This differs from %* only for a modified read-only buffer. */
23168 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23169 return "*";
23170 if (!NILP (BVAR (b, read_only)))
23171 return "%";
23172 return "-";
23173
23174 case '&':
23175 /* This differs from %* in ignoring read-only-ness. */
23176 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23177 return "*";
23178 return "-";
23179
23180 case '%':
23181 return "%";
23182
23183 case '[':
23184 {
23185 int i;
23186 char *p;
23187
23188 if (command_loop_level > 5)
23189 return "[[[... ";
23190 p = decode_mode_spec_buf;
23191 for (i = 0; i < command_loop_level; i++)
23192 *p++ = '[';
23193 *p = 0;
23194 return decode_mode_spec_buf;
23195 }
23196
23197 case ']':
23198 {
23199 int i;
23200 char *p;
23201
23202 if (command_loop_level > 5)
23203 return " ...]]]";
23204 p = decode_mode_spec_buf;
23205 for (i = 0; i < command_loop_level; i++)
23206 *p++ = ']';
23207 *p = 0;
23208 return decode_mode_spec_buf;
23209 }
23210
23211 case '-':
23212 {
23213 register int i;
23214
23215 /* Let lots_of_dashes be a string of infinite length. */
23216 if (mode_line_target == MODE_LINE_NOPROP
23217 || mode_line_target == MODE_LINE_STRING)
23218 return "--";
23219 if (field_width <= 0
23220 || field_width > sizeof (lots_of_dashes))
23221 {
23222 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23223 decode_mode_spec_buf[i] = '-';
23224 decode_mode_spec_buf[i] = '\0';
23225 return decode_mode_spec_buf;
23226 }
23227 else
23228 return lots_of_dashes;
23229 }
23230
23231 case 'b':
23232 obj = BVAR (b, name);
23233 break;
23234
23235 case 'c':
23236 /* %c and %l are ignored in `frame-title-format'.
23237 (In redisplay_internal, the frame title is drawn _before_ the
23238 windows are updated, so the stuff which depends on actual
23239 window contents (such as %l) may fail to render properly, or
23240 even crash emacs.) */
23241 if (mode_line_target == MODE_LINE_TITLE)
23242 return "";
23243 else
23244 {
23245 ptrdiff_t col = current_column ();
23246 w->column_number_displayed = col;
23247 pint2str (decode_mode_spec_buf, width, col);
23248 return decode_mode_spec_buf;
23249 }
23250
23251 case 'e':
23252 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23253 {
23254 if (NILP (Vmemory_full))
23255 return "";
23256 else
23257 return "!MEM FULL! ";
23258 }
23259 #else
23260 return "";
23261 #endif
23262
23263 case 'F':
23264 /* %F displays the frame name. */
23265 if (!NILP (f->title))
23266 return SSDATA (f->title);
23267 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23268 return SSDATA (f->name);
23269 return "Emacs";
23270
23271 case 'f':
23272 obj = BVAR (b, filename);
23273 break;
23274
23275 case 'i':
23276 {
23277 ptrdiff_t size = ZV - BEGV;
23278 pint2str (decode_mode_spec_buf, width, size);
23279 return decode_mode_spec_buf;
23280 }
23281
23282 case 'I':
23283 {
23284 ptrdiff_t size = ZV - BEGV;
23285 pint2hrstr (decode_mode_spec_buf, width, size);
23286 return decode_mode_spec_buf;
23287 }
23288
23289 case 'l':
23290 {
23291 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23292 ptrdiff_t topline, nlines, height;
23293 ptrdiff_t junk;
23294
23295 /* %c and %l are ignored in `frame-title-format'. */
23296 if (mode_line_target == MODE_LINE_TITLE)
23297 return "";
23298
23299 startpos = marker_position (w->start);
23300 startpos_byte = marker_byte_position (w->start);
23301 height = WINDOW_TOTAL_LINES (w);
23302
23303 /* If we decided that this buffer isn't suitable for line numbers,
23304 don't forget that too fast. */
23305 if (w->base_line_pos == -1)
23306 goto no_value;
23307
23308 /* If the buffer is very big, don't waste time. */
23309 if (INTEGERP (Vline_number_display_limit)
23310 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23311 {
23312 w->base_line_pos = 0;
23313 w->base_line_number = 0;
23314 goto no_value;
23315 }
23316
23317 if (w->base_line_number > 0
23318 && w->base_line_pos > 0
23319 && w->base_line_pos <= startpos)
23320 {
23321 line = w->base_line_number;
23322 linepos = w->base_line_pos;
23323 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23324 }
23325 else
23326 {
23327 line = 1;
23328 linepos = BUF_BEGV (b);
23329 linepos_byte = BUF_BEGV_BYTE (b);
23330 }
23331
23332 /* Count lines from base line to window start position. */
23333 nlines = display_count_lines (linepos_byte,
23334 startpos_byte,
23335 startpos, &junk);
23336
23337 topline = nlines + line;
23338
23339 /* Determine a new base line, if the old one is too close
23340 or too far away, or if we did not have one.
23341 "Too close" means it's plausible a scroll-down would
23342 go back past it. */
23343 if (startpos == BUF_BEGV (b))
23344 {
23345 w->base_line_number = topline;
23346 w->base_line_pos = BUF_BEGV (b);
23347 }
23348 else if (nlines < height + 25 || nlines > height * 3 + 50
23349 || linepos == BUF_BEGV (b))
23350 {
23351 ptrdiff_t limit = BUF_BEGV (b);
23352 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23353 ptrdiff_t position;
23354 ptrdiff_t distance =
23355 (height * 2 + 30) * line_number_display_limit_width;
23356
23357 if (startpos - distance > limit)
23358 {
23359 limit = startpos - distance;
23360 limit_byte = CHAR_TO_BYTE (limit);
23361 }
23362
23363 nlines = display_count_lines (startpos_byte,
23364 limit_byte,
23365 - (height * 2 + 30),
23366 &position);
23367 /* If we couldn't find the lines we wanted within
23368 line_number_display_limit_width chars per line,
23369 give up on line numbers for this window. */
23370 if (position == limit_byte && limit == startpos - distance)
23371 {
23372 w->base_line_pos = -1;
23373 w->base_line_number = 0;
23374 goto no_value;
23375 }
23376
23377 w->base_line_number = topline - nlines;
23378 w->base_line_pos = BYTE_TO_CHAR (position);
23379 }
23380
23381 /* Now count lines from the start pos to point. */
23382 nlines = display_count_lines (startpos_byte,
23383 PT_BYTE, PT, &junk);
23384
23385 /* Record that we did display the line number. */
23386 line_number_displayed = true;
23387
23388 /* Make the string to show. */
23389 pint2str (decode_mode_spec_buf, width, topline + nlines);
23390 return decode_mode_spec_buf;
23391 no_value:
23392 {
23393 char *p = decode_mode_spec_buf;
23394 int pad = width - 2;
23395 while (pad-- > 0)
23396 *p++ = ' ';
23397 *p++ = '?';
23398 *p++ = '?';
23399 *p = '\0';
23400 return decode_mode_spec_buf;
23401 }
23402 }
23403 break;
23404
23405 case 'm':
23406 obj = BVAR (b, mode_name);
23407 break;
23408
23409 case 'n':
23410 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23411 return " Narrow";
23412 break;
23413
23414 case 'p':
23415 {
23416 ptrdiff_t pos = marker_position (w->start);
23417 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23418
23419 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23420 {
23421 if (pos <= BUF_BEGV (b))
23422 return "All";
23423 else
23424 return "Bottom";
23425 }
23426 else if (pos <= BUF_BEGV (b))
23427 return "Top";
23428 else
23429 {
23430 if (total > 1000000)
23431 /* Do it differently for a large value, to avoid overflow. */
23432 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23433 else
23434 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23435 /* We can't normally display a 3-digit number,
23436 so get us a 2-digit number that is close. */
23437 if (total == 100)
23438 total = 99;
23439 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23440 return decode_mode_spec_buf;
23441 }
23442 }
23443
23444 /* Display percentage of size above the bottom of the screen. */
23445 case 'P':
23446 {
23447 ptrdiff_t toppos = marker_position (w->start);
23448 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23449 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23450
23451 if (botpos >= BUF_ZV (b))
23452 {
23453 if (toppos <= BUF_BEGV (b))
23454 return "All";
23455 else
23456 return "Bottom";
23457 }
23458 else
23459 {
23460 if (total > 1000000)
23461 /* Do it differently for a large value, to avoid overflow. */
23462 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23463 else
23464 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23465 /* We can't normally display a 3-digit number,
23466 so get us a 2-digit number that is close. */
23467 if (total == 100)
23468 total = 99;
23469 if (toppos <= BUF_BEGV (b))
23470 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23471 else
23472 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23473 return decode_mode_spec_buf;
23474 }
23475 }
23476
23477 case 's':
23478 /* status of process */
23479 obj = Fget_buffer_process (Fcurrent_buffer ());
23480 if (NILP (obj))
23481 return "no process";
23482 #ifndef MSDOS
23483 obj = Fsymbol_name (Fprocess_status (obj));
23484 #endif
23485 break;
23486
23487 case '@':
23488 {
23489 ptrdiff_t count = inhibit_garbage_collection ();
23490 Lisp_Object curdir = BVAR (current_buffer, directory);
23491 Lisp_Object val = Qnil;
23492
23493 if (STRINGP (curdir))
23494 val = call1 (intern ("file-remote-p"), curdir);
23495
23496 unbind_to (count, Qnil);
23497
23498 if (NILP (val))
23499 return "-";
23500 else
23501 return "@";
23502 }
23503
23504 case 'z':
23505 /* coding-system (not including end-of-line format) */
23506 case 'Z':
23507 /* coding-system (including end-of-line type) */
23508 {
23509 bool eol_flag = (c == 'Z');
23510 char *p = decode_mode_spec_buf;
23511
23512 if (! FRAME_WINDOW_P (f))
23513 {
23514 /* No need to mention EOL here--the terminal never needs
23515 to do EOL conversion. */
23516 p = decode_mode_spec_coding (CODING_ID_NAME
23517 (FRAME_KEYBOARD_CODING (f)->id),
23518 p, false);
23519 p = decode_mode_spec_coding (CODING_ID_NAME
23520 (FRAME_TERMINAL_CODING (f)->id),
23521 p, false);
23522 }
23523 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23524 p, eol_flag);
23525
23526 #if false /* This proves to be annoying; I think we can do without. -- rms. */
23527 #ifdef subprocesses
23528 obj = Fget_buffer_process (Fcurrent_buffer ());
23529 if (PROCESSP (obj))
23530 {
23531 p = decode_mode_spec_coding
23532 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23533 p = decode_mode_spec_coding
23534 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23535 }
23536 #endif /* subprocesses */
23537 #endif /* false */
23538 *p = 0;
23539 return decode_mode_spec_buf;
23540 }
23541 }
23542
23543 if (STRINGP (obj))
23544 {
23545 *string = obj;
23546 return SSDATA (obj);
23547 }
23548 else
23549 return "";
23550 }
23551
23552
23553 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23554 means count lines back from START_BYTE. But don't go beyond
23555 LIMIT_BYTE. Return the number of lines thus found (always
23556 nonnegative).
23557
23558 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23559 either the position COUNT lines after/before START_BYTE, if we
23560 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23561 COUNT lines. */
23562
23563 static ptrdiff_t
23564 display_count_lines (ptrdiff_t start_byte,
23565 ptrdiff_t limit_byte, ptrdiff_t count,
23566 ptrdiff_t *byte_pos_ptr)
23567 {
23568 register unsigned char *cursor;
23569 unsigned char *base;
23570
23571 register ptrdiff_t ceiling;
23572 register unsigned char *ceiling_addr;
23573 ptrdiff_t orig_count = count;
23574
23575 /* If we are not in selective display mode,
23576 check only for newlines. */
23577 bool selective_display
23578 = (!NILP (BVAR (current_buffer, selective_display))
23579 && !INTEGERP (BVAR (current_buffer, selective_display)));
23580
23581 if (count > 0)
23582 {
23583 while (start_byte < limit_byte)
23584 {
23585 ceiling = BUFFER_CEILING_OF (start_byte);
23586 ceiling = min (limit_byte - 1, ceiling);
23587 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23588 base = (cursor = BYTE_POS_ADDR (start_byte));
23589
23590 do
23591 {
23592 if (selective_display)
23593 {
23594 while (*cursor != '\n' && *cursor != 015
23595 && ++cursor != ceiling_addr)
23596 continue;
23597 if (cursor == ceiling_addr)
23598 break;
23599 }
23600 else
23601 {
23602 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23603 if (! cursor)
23604 break;
23605 }
23606
23607 cursor++;
23608
23609 if (--count == 0)
23610 {
23611 start_byte += cursor - base;
23612 *byte_pos_ptr = start_byte;
23613 return orig_count;
23614 }
23615 }
23616 while (cursor < ceiling_addr);
23617
23618 start_byte += ceiling_addr - base;
23619 }
23620 }
23621 else
23622 {
23623 while (start_byte > limit_byte)
23624 {
23625 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23626 ceiling = max (limit_byte, ceiling);
23627 ceiling_addr = BYTE_POS_ADDR (ceiling);
23628 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23629 while (true)
23630 {
23631 if (selective_display)
23632 {
23633 while (--cursor >= ceiling_addr
23634 && *cursor != '\n' && *cursor != 015)
23635 continue;
23636 if (cursor < ceiling_addr)
23637 break;
23638 }
23639 else
23640 {
23641 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23642 if (! cursor)
23643 break;
23644 }
23645
23646 if (++count == 0)
23647 {
23648 start_byte += cursor - base + 1;
23649 *byte_pos_ptr = start_byte;
23650 /* When scanning backwards, we should
23651 not count the newline posterior to which we stop. */
23652 return - orig_count - 1;
23653 }
23654 }
23655 start_byte += ceiling_addr - base;
23656 }
23657 }
23658
23659 *byte_pos_ptr = limit_byte;
23660
23661 if (count < 0)
23662 return - orig_count + count;
23663 return orig_count - count;
23664
23665 }
23666
23667
23668 \f
23669 /***********************************************************************
23670 Displaying strings
23671 ***********************************************************************/
23672
23673 /* Display a NUL-terminated string, starting with index START.
23674
23675 If STRING is non-null, display that C string. Otherwise, the Lisp
23676 string LISP_STRING is displayed. There's a case that STRING is
23677 non-null and LISP_STRING is not nil. It means STRING is a string
23678 data of LISP_STRING. In that case, we display LISP_STRING while
23679 ignoring its text properties.
23680
23681 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23682 FACE_STRING. Display STRING or LISP_STRING with the face at
23683 FACE_STRING_POS in FACE_STRING:
23684
23685 Display the string in the environment given by IT, but use the
23686 standard display table, temporarily.
23687
23688 FIELD_WIDTH is the minimum number of output glyphs to produce.
23689 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23690 with spaces. If STRING has more characters, more than FIELD_WIDTH
23691 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23692
23693 PRECISION is the maximum number of characters to output from
23694 STRING. PRECISION < 0 means don't truncate the string.
23695
23696 This is roughly equivalent to printf format specifiers:
23697
23698 FIELD_WIDTH PRECISION PRINTF
23699 ----------------------------------------
23700 -1 -1 %s
23701 -1 10 %.10s
23702 10 -1 %10s
23703 20 10 %20.10s
23704
23705 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23706 display them, and < 0 means obey the current buffer's value of
23707 enable_multibyte_characters.
23708
23709 Value is the number of columns displayed. */
23710
23711 static int
23712 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23713 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23714 int field_width, int precision, int max_x, int multibyte)
23715 {
23716 int hpos_at_start = it->hpos;
23717 int saved_face_id = it->face_id;
23718 struct glyph_row *row = it->glyph_row;
23719 ptrdiff_t it_charpos;
23720
23721 /* Initialize the iterator IT for iteration over STRING beginning
23722 with index START. */
23723 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23724 precision, field_width, multibyte);
23725 if (string && STRINGP (lisp_string))
23726 /* LISP_STRING is the one returned by decode_mode_spec. We should
23727 ignore its text properties. */
23728 it->stop_charpos = it->end_charpos;
23729
23730 /* If displaying STRING, set up the face of the iterator from
23731 FACE_STRING, if that's given. */
23732 if (STRINGP (face_string))
23733 {
23734 ptrdiff_t endptr;
23735 struct face *face;
23736
23737 it->face_id
23738 = face_at_string_position (it->w, face_string, face_string_pos,
23739 0, &endptr, it->base_face_id, false);
23740 face = FACE_FROM_ID (it->f, it->face_id);
23741 it->face_box_p = face->box != FACE_NO_BOX;
23742 }
23743
23744 /* Set max_x to the maximum allowed X position. Don't let it go
23745 beyond the right edge of the window. */
23746 if (max_x <= 0)
23747 max_x = it->last_visible_x;
23748 else
23749 max_x = min (max_x, it->last_visible_x);
23750
23751 /* Skip over display elements that are not visible. because IT->w is
23752 hscrolled. */
23753 if (it->current_x < it->first_visible_x)
23754 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23755 MOVE_TO_POS | MOVE_TO_X);
23756
23757 row->ascent = it->max_ascent;
23758 row->height = it->max_ascent + it->max_descent;
23759 row->phys_ascent = it->max_phys_ascent;
23760 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23761 row->extra_line_spacing = it->max_extra_line_spacing;
23762
23763 if (STRINGP (it->string))
23764 it_charpos = IT_STRING_CHARPOS (*it);
23765 else
23766 it_charpos = IT_CHARPOS (*it);
23767
23768 /* This condition is for the case that we are called with current_x
23769 past last_visible_x. */
23770 while (it->current_x < max_x)
23771 {
23772 int x_before, x, n_glyphs_before, i, nglyphs;
23773
23774 /* Get the next display element. */
23775 if (!get_next_display_element (it))
23776 break;
23777
23778 /* Produce glyphs. */
23779 x_before = it->current_x;
23780 n_glyphs_before = row->used[TEXT_AREA];
23781 PRODUCE_GLYPHS (it);
23782
23783 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23784 i = 0;
23785 x = x_before;
23786 while (i < nglyphs)
23787 {
23788 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23789
23790 if (it->line_wrap != TRUNCATE
23791 && x + glyph->pixel_width > max_x)
23792 {
23793 /* End of continued line or max_x reached. */
23794 if (CHAR_GLYPH_PADDING_P (*glyph))
23795 {
23796 /* A wide character is unbreakable. */
23797 if (row->reversed_p)
23798 unproduce_glyphs (it, row->used[TEXT_AREA]
23799 - n_glyphs_before);
23800 row->used[TEXT_AREA] = n_glyphs_before;
23801 it->current_x = x_before;
23802 }
23803 else
23804 {
23805 if (row->reversed_p)
23806 unproduce_glyphs (it, row->used[TEXT_AREA]
23807 - (n_glyphs_before + i));
23808 row->used[TEXT_AREA] = n_glyphs_before + i;
23809 it->current_x = x;
23810 }
23811 break;
23812 }
23813 else if (x + glyph->pixel_width >= it->first_visible_x)
23814 {
23815 /* Glyph is at least partially visible. */
23816 ++it->hpos;
23817 if (x < it->first_visible_x)
23818 row->x = x - it->first_visible_x;
23819 }
23820 else
23821 {
23822 /* Glyph is off the left margin of the display area.
23823 Should not happen. */
23824 emacs_abort ();
23825 }
23826
23827 row->ascent = max (row->ascent, it->max_ascent);
23828 row->height = max (row->height, it->max_ascent + it->max_descent);
23829 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23830 row->phys_height = max (row->phys_height,
23831 it->max_phys_ascent + it->max_phys_descent);
23832 row->extra_line_spacing = max (row->extra_line_spacing,
23833 it->max_extra_line_spacing);
23834 x += glyph->pixel_width;
23835 ++i;
23836 }
23837
23838 /* Stop if max_x reached. */
23839 if (i < nglyphs)
23840 break;
23841
23842 /* Stop at line ends. */
23843 if (ITERATOR_AT_END_OF_LINE_P (it))
23844 {
23845 it->continuation_lines_width = 0;
23846 break;
23847 }
23848
23849 set_iterator_to_next (it, true);
23850 if (STRINGP (it->string))
23851 it_charpos = IT_STRING_CHARPOS (*it);
23852 else
23853 it_charpos = IT_CHARPOS (*it);
23854
23855 /* Stop if truncating at the right edge. */
23856 if (it->line_wrap == TRUNCATE
23857 && it->current_x >= it->last_visible_x)
23858 {
23859 /* Add truncation mark, but don't do it if the line is
23860 truncated at a padding space. */
23861 if (it_charpos < it->string_nchars)
23862 {
23863 if (!FRAME_WINDOW_P (it->f))
23864 {
23865 int ii, n;
23866
23867 if (it->current_x > it->last_visible_x)
23868 {
23869 if (!row->reversed_p)
23870 {
23871 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23872 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23873 break;
23874 }
23875 else
23876 {
23877 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23878 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23879 break;
23880 unproduce_glyphs (it, ii + 1);
23881 ii = row->used[TEXT_AREA] - (ii + 1);
23882 }
23883 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23884 {
23885 row->used[TEXT_AREA] = ii;
23886 produce_special_glyphs (it, IT_TRUNCATION);
23887 }
23888 }
23889 produce_special_glyphs (it, IT_TRUNCATION);
23890 }
23891 row->truncated_on_right_p = true;
23892 }
23893 break;
23894 }
23895 }
23896
23897 /* Maybe insert a truncation at the left. */
23898 if (it->first_visible_x
23899 && it_charpos > 0)
23900 {
23901 if (!FRAME_WINDOW_P (it->f)
23902 || (row->reversed_p
23903 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23904 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23905 insert_left_trunc_glyphs (it);
23906 row->truncated_on_left_p = true;
23907 }
23908
23909 it->face_id = saved_face_id;
23910
23911 /* Value is number of columns displayed. */
23912 return it->hpos - hpos_at_start;
23913 }
23914
23915
23916 \f
23917 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23918 appears as an element of LIST or as the car of an element of LIST.
23919 If PROPVAL is a list, compare each element against LIST in that
23920 way, and return 1/2 if any element of PROPVAL is found in LIST.
23921 Otherwise return 0. This function cannot quit.
23922 The return value is 2 if the text is invisible but with an ellipsis
23923 and 1 if it's invisible and without an ellipsis. */
23924
23925 int
23926 invisible_prop (Lisp_Object propval, Lisp_Object list)
23927 {
23928 Lisp_Object tail, proptail;
23929
23930 for (tail = list; CONSP (tail); tail = XCDR (tail))
23931 {
23932 register Lisp_Object tem;
23933 tem = XCAR (tail);
23934 if (EQ (propval, tem))
23935 return 1;
23936 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23937 return NILP (XCDR (tem)) ? 1 : 2;
23938 }
23939
23940 if (CONSP (propval))
23941 {
23942 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23943 {
23944 Lisp_Object propelt;
23945 propelt = XCAR (proptail);
23946 for (tail = list; CONSP (tail); tail = XCDR (tail))
23947 {
23948 register Lisp_Object tem;
23949 tem = XCAR (tail);
23950 if (EQ (propelt, tem))
23951 return 1;
23952 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23953 return NILP (XCDR (tem)) ? 1 : 2;
23954 }
23955 }
23956 }
23957
23958 return 0;
23959 }
23960
23961 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23962 doc: /* Non-nil if the property makes the text invisible.
23963 POS-OR-PROP can be a marker or number, in which case it is taken to be
23964 a position in the current buffer and the value of the `invisible' property
23965 is checked; or it can be some other value, which is then presumed to be the
23966 value of the `invisible' property of the text of interest.
23967 The non-nil value returned can be t for truly invisible text or something
23968 else if the text is replaced by an ellipsis. */)
23969 (Lisp_Object pos_or_prop)
23970 {
23971 Lisp_Object prop
23972 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23973 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23974 : pos_or_prop);
23975 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23976 return (invis == 0 ? Qnil
23977 : invis == 1 ? Qt
23978 : make_number (invis));
23979 }
23980
23981 /* Calculate a width or height in pixels from a specification using
23982 the following elements:
23983
23984 SPEC ::=
23985 NUM - a (fractional) multiple of the default font width/height
23986 (NUM) - specifies exactly NUM pixels
23987 UNIT - a fixed number of pixels, see below.
23988 ELEMENT - size of a display element in pixels, see below.
23989 (NUM . SPEC) - equals NUM * SPEC
23990 (+ SPEC SPEC ...) - add pixel values
23991 (- SPEC SPEC ...) - subtract pixel values
23992 (- SPEC) - negate pixel value
23993
23994 NUM ::=
23995 INT or FLOAT - a number constant
23996 SYMBOL - use symbol's (buffer local) variable binding.
23997
23998 UNIT ::=
23999 in - pixels per inch *)
24000 mm - pixels per 1/1000 meter *)
24001 cm - pixels per 1/100 meter *)
24002 width - width of current font in pixels.
24003 height - height of current font in pixels.
24004
24005 *) using the ratio(s) defined in display-pixels-per-inch.
24006
24007 ELEMENT ::=
24008
24009 left-fringe - left fringe width in pixels
24010 right-fringe - right fringe width in pixels
24011
24012 left-margin - left margin width in pixels
24013 right-margin - right margin width in pixels
24014
24015 scroll-bar - scroll-bar area width in pixels
24016
24017 Examples:
24018
24019 Pixels corresponding to 5 inches:
24020 (5 . in)
24021
24022 Total width of non-text areas on left side of window (if scroll-bar is on left):
24023 '(space :width (+ left-fringe left-margin scroll-bar))
24024
24025 Align to first text column (in header line):
24026 '(space :align-to 0)
24027
24028 Align to middle of text area minus half the width of variable `my-image'
24029 containing a loaded image:
24030 '(space :align-to (0.5 . (- text my-image)))
24031
24032 Width of left margin minus width of 1 character in the default font:
24033 '(space :width (- left-margin 1))
24034
24035 Width of left margin minus width of 2 characters in the current font:
24036 '(space :width (- left-margin (2 . width)))
24037
24038 Center 1 character over left-margin (in header line):
24039 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24040
24041 Different ways to express width of left fringe plus left margin minus one pixel:
24042 '(space :width (- (+ left-fringe left-margin) (1)))
24043 '(space :width (+ left-fringe left-margin (- (1))))
24044 '(space :width (+ left-fringe left-margin (-1)))
24045
24046 */
24047
24048 static bool
24049 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24050 struct font *font, bool width_p, int *align_to)
24051 {
24052 double pixels;
24053
24054 # define OK_PIXELS(val) (*res = (val), true)
24055 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24056
24057 if (NILP (prop))
24058 return OK_PIXELS (0);
24059
24060 eassert (FRAME_LIVE_P (it->f));
24061
24062 if (SYMBOLP (prop))
24063 {
24064 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24065 {
24066 char *unit = SSDATA (SYMBOL_NAME (prop));
24067
24068 if (unit[0] == 'i' && unit[1] == 'n')
24069 pixels = 1.0;
24070 else if (unit[0] == 'm' && unit[1] == 'm')
24071 pixels = 25.4;
24072 else if (unit[0] == 'c' && unit[1] == 'm')
24073 pixels = 2.54;
24074 else
24075 pixels = 0;
24076 if (pixels > 0)
24077 {
24078 double ppi = (width_p ? FRAME_RES_X (it->f)
24079 : FRAME_RES_Y (it->f));
24080
24081 if (ppi > 0)
24082 return OK_PIXELS (ppi / pixels);
24083 return false;
24084 }
24085 }
24086
24087 #ifdef HAVE_WINDOW_SYSTEM
24088 if (EQ (prop, Qheight))
24089 return OK_PIXELS (font
24090 ? normal_char_height (font, -1)
24091 : FRAME_LINE_HEIGHT (it->f));
24092 if (EQ (prop, Qwidth))
24093 return OK_PIXELS (font
24094 ? FONT_WIDTH (font)
24095 : FRAME_COLUMN_WIDTH (it->f));
24096 #else
24097 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24098 return OK_PIXELS (1);
24099 #endif
24100
24101 if (EQ (prop, Qtext))
24102 return OK_PIXELS (width_p
24103 ? window_box_width (it->w, TEXT_AREA)
24104 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24105
24106 if (align_to && *align_to < 0)
24107 {
24108 *res = 0;
24109 if (EQ (prop, Qleft))
24110 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24111 if (EQ (prop, Qright))
24112 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24113 if (EQ (prop, Qcenter))
24114 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24115 + window_box_width (it->w, TEXT_AREA) / 2);
24116 if (EQ (prop, Qleft_fringe))
24117 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24118 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24119 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24120 if (EQ (prop, Qright_fringe))
24121 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24122 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24123 : window_box_right_offset (it->w, TEXT_AREA));
24124 if (EQ (prop, Qleft_margin))
24125 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24126 if (EQ (prop, Qright_margin))
24127 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24128 if (EQ (prop, Qscroll_bar))
24129 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24130 ? 0
24131 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24132 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24133 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24134 : 0)));
24135 }
24136 else
24137 {
24138 if (EQ (prop, Qleft_fringe))
24139 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24140 if (EQ (prop, Qright_fringe))
24141 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24142 if (EQ (prop, Qleft_margin))
24143 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24144 if (EQ (prop, Qright_margin))
24145 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24146 if (EQ (prop, Qscroll_bar))
24147 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24148 }
24149
24150 prop = buffer_local_value (prop, it->w->contents);
24151 if (EQ (prop, Qunbound))
24152 prop = Qnil;
24153 }
24154
24155 if (NUMBERP (prop))
24156 {
24157 int base_unit = (width_p
24158 ? FRAME_COLUMN_WIDTH (it->f)
24159 : FRAME_LINE_HEIGHT (it->f));
24160 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24161 }
24162
24163 if (CONSP (prop))
24164 {
24165 Lisp_Object car = XCAR (prop);
24166 Lisp_Object cdr = XCDR (prop);
24167
24168 if (SYMBOLP (car))
24169 {
24170 #ifdef HAVE_WINDOW_SYSTEM
24171 if (FRAME_WINDOW_P (it->f)
24172 && valid_image_p (prop))
24173 {
24174 ptrdiff_t id = lookup_image (it->f, prop);
24175 struct image *img = IMAGE_FROM_ID (it->f, id);
24176
24177 return OK_PIXELS (width_p ? img->width : img->height);
24178 }
24179 #endif
24180 if (EQ (car, Qplus) || EQ (car, Qminus))
24181 {
24182 bool first = true;
24183 double px;
24184
24185 pixels = 0;
24186 while (CONSP (cdr))
24187 {
24188 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24189 font, width_p, align_to))
24190 return false;
24191 if (first)
24192 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24193 else
24194 pixels += px;
24195 cdr = XCDR (cdr);
24196 }
24197 if (EQ (car, Qminus))
24198 pixels = -pixels;
24199 return OK_PIXELS (pixels);
24200 }
24201
24202 car = buffer_local_value (car, it->w->contents);
24203 if (EQ (car, Qunbound))
24204 car = Qnil;
24205 }
24206
24207 if (NUMBERP (car))
24208 {
24209 double fact;
24210 pixels = XFLOATINT (car);
24211 if (NILP (cdr))
24212 return OK_PIXELS (pixels);
24213 if (calc_pixel_width_or_height (&fact, it, cdr,
24214 font, width_p, align_to))
24215 return OK_PIXELS (pixels * fact);
24216 return false;
24217 }
24218
24219 return false;
24220 }
24221
24222 return false;
24223 }
24224
24225 void
24226 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24227 {
24228 #ifdef HAVE_WINDOW_SYSTEM
24229 normal_char_ascent_descent (font, -1, ascent, descent);
24230 #else
24231 *ascent = 1;
24232 *descent = 0;
24233 #endif
24234 }
24235
24236 \f
24237 /***********************************************************************
24238 Glyph Display
24239 ***********************************************************************/
24240
24241 #ifdef HAVE_WINDOW_SYSTEM
24242
24243 #ifdef GLYPH_DEBUG
24244
24245 void
24246 dump_glyph_string (struct glyph_string *s)
24247 {
24248 fprintf (stderr, "glyph string\n");
24249 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24250 s->x, s->y, s->width, s->height);
24251 fprintf (stderr, " ybase = %d\n", s->ybase);
24252 fprintf (stderr, " hl = %d\n", s->hl);
24253 fprintf (stderr, " left overhang = %d, right = %d\n",
24254 s->left_overhang, s->right_overhang);
24255 fprintf (stderr, " nchars = %d\n", s->nchars);
24256 fprintf (stderr, " extends to end of line = %d\n",
24257 s->extends_to_end_of_line_p);
24258 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24259 fprintf (stderr, " bg width = %d\n", s->background_width);
24260 }
24261
24262 #endif /* GLYPH_DEBUG */
24263
24264 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24265 of XChar2b structures for S; it can't be allocated in
24266 init_glyph_string because it must be allocated via `alloca'. W
24267 is the window on which S is drawn. ROW and AREA are the glyph row
24268 and area within the row from which S is constructed. START is the
24269 index of the first glyph structure covered by S. HL is a
24270 face-override for drawing S. */
24271
24272 #ifdef HAVE_NTGUI
24273 #define OPTIONAL_HDC(hdc) HDC hdc,
24274 #define DECLARE_HDC(hdc) HDC hdc;
24275 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24276 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24277 #endif
24278
24279 #ifndef OPTIONAL_HDC
24280 #define OPTIONAL_HDC(hdc)
24281 #define DECLARE_HDC(hdc)
24282 #define ALLOCATE_HDC(hdc, f)
24283 #define RELEASE_HDC(hdc, f)
24284 #endif
24285
24286 static void
24287 init_glyph_string (struct glyph_string *s,
24288 OPTIONAL_HDC (hdc)
24289 XChar2b *char2b, struct window *w, struct glyph_row *row,
24290 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24291 {
24292 memset (s, 0, sizeof *s);
24293 s->w = w;
24294 s->f = XFRAME (w->frame);
24295 #ifdef HAVE_NTGUI
24296 s->hdc = hdc;
24297 #endif
24298 s->display = FRAME_X_DISPLAY (s->f);
24299 s->window = FRAME_X_WINDOW (s->f);
24300 s->char2b = char2b;
24301 s->hl = hl;
24302 s->row = row;
24303 s->area = area;
24304 s->first_glyph = row->glyphs[area] + start;
24305 s->height = row->height;
24306 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24307 s->ybase = s->y + row->ascent;
24308 }
24309
24310
24311 /* Append the list of glyph strings with head H and tail T to the list
24312 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24313
24314 static void
24315 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24316 struct glyph_string *h, struct glyph_string *t)
24317 {
24318 if (h)
24319 {
24320 if (*head)
24321 (*tail)->next = h;
24322 else
24323 *head = h;
24324 h->prev = *tail;
24325 *tail = t;
24326 }
24327 }
24328
24329
24330 /* Prepend the list of glyph strings with head H and tail T to the
24331 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24332 result. */
24333
24334 static void
24335 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24336 struct glyph_string *h, struct glyph_string *t)
24337 {
24338 if (h)
24339 {
24340 if (*head)
24341 (*head)->prev = t;
24342 else
24343 *tail = t;
24344 t->next = *head;
24345 *head = h;
24346 }
24347 }
24348
24349
24350 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24351 Set *HEAD and *TAIL to the resulting list. */
24352
24353 static void
24354 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24355 struct glyph_string *s)
24356 {
24357 s->next = s->prev = NULL;
24358 append_glyph_string_lists (head, tail, s, s);
24359 }
24360
24361
24362 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24363 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24364 make sure that X resources for the face returned are allocated.
24365 Value is a pointer to a realized face that is ready for display if
24366 DISPLAY_P. */
24367
24368 static struct face *
24369 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24370 XChar2b *char2b, bool display_p)
24371 {
24372 struct face *face = FACE_FROM_ID (f, face_id);
24373 unsigned code = 0;
24374
24375 if (face->font)
24376 {
24377 code = face->font->driver->encode_char (face->font, c);
24378
24379 if (code == FONT_INVALID_CODE)
24380 code = 0;
24381 }
24382 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24383
24384 /* Make sure X resources of the face are allocated. */
24385 #ifdef HAVE_X_WINDOWS
24386 if (display_p)
24387 #endif
24388 {
24389 eassert (face != NULL);
24390 prepare_face_for_display (f, face);
24391 }
24392
24393 return face;
24394 }
24395
24396
24397 /* Get face and two-byte form of character glyph GLYPH on frame F.
24398 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24399 a pointer to a realized face that is ready for display. */
24400
24401 static struct face *
24402 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24403 XChar2b *char2b)
24404 {
24405 struct face *face;
24406 unsigned code = 0;
24407
24408 eassert (glyph->type == CHAR_GLYPH);
24409 face = FACE_FROM_ID (f, glyph->face_id);
24410
24411 /* Make sure X resources of the face are allocated. */
24412 eassert (face != NULL);
24413 prepare_face_for_display (f, face);
24414
24415 if (face->font)
24416 {
24417 if (CHAR_BYTE8_P (glyph->u.ch))
24418 code = CHAR_TO_BYTE8 (glyph->u.ch);
24419 else
24420 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24421
24422 if (code == FONT_INVALID_CODE)
24423 code = 0;
24424 }
24425
24426 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24427 return face;
24428 }
24429
24430
24431 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24432 Return true iff FONT has a glyph for C. */
24433
24434 static bool
24435 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24436 {
24437 unsigned code;
24438
24439 if (CHAR_BYTE8_P (c))
24440 code = CHAR_TO_BYTE8 (c);
24441 else
24442 code = font->driver->encode_char (font, c);
24443
24444 if (code == FONT_INVALID_CODE)
24445 return false;
24446 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24447 return true;
24448 }
24449
24450
24451 /* Fill glyph string S with composition components specified by S->cmp.
24452
24453 BASE_FACE is the base face of the composition.
24454 S->cmp_from is the index of the first component for S.
24455
24456 OVERLAPS non-zero means S should draw the foreground only, and use
24457 its physical height for clipping. See also draw_glyphs.
24458
24459 Value is the index of a component not in S. */
24460
24461 static int
24462 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24463 int overlaps)
24464 {
24465 int i;
24466 /* For all glyphs of this composition, starting at the offset
24467 S->cmp_from, until we reach the end of the definition or encounter a
24468 glyph that requires the different face, add it to S. */
24469 struct face *face;
24470
24471 eassert (s);
24472
24473 s->for_overlaps = overlaps;
24474 s->face = NULL;
24475 s->font = NULL;
24476 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24477 {
24478 int c = COMPOSITION_GLYPH (s->cmp, i);
24479
24480 /* TAB in a composition means display glyphs with padding space
24481 on the left or right. */
24482 if (c != '\t')
24483 {
24484 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24485 -1, Qnil);
24486
24487 face = get_char_face_and_encoding (s->f, c, face_id,
24488 s->char2b + i, true);
24489 if (face)
24490 {
24491 if (! s->face)
24492 {
24493 s->face = face;
24494 s->font = s->face->font;
24495 }
24496 else if (s->face != face)
24497 break;
24498 }
24499 }
24500 ++s->nchars;
24501 }
24502 s->cmp_to = i;
24503
24504 if (s->face == NULL)
24505 {
24506 s->face = base_face->ascii_face;
24507 s->font = s->face->font;
24508 }
24509
24510 /* All glyph strings for the same composition has the same width,
24511 i.e. the width set for the first component of the composition. */
24512 s->width = s->first_glyph->pixel_width;
24513
24514 /* If the specified font could not be loaded, use the frame's
24515 default font, but record the fact that we couldn't load it in
24516 the glyph string so that we can draw rectangles for the
24517 characters of the glyph string. */
24518 if (s->font == NULL)
24519 {
24520 s->font_not_found_p = true;
24521 s->font = FRAME_FONT (s->f);
24522 }
24523
24524 /* Adjust base line for subscript/superscript text. */
24525 s->ybase += s->first_glyph->voffset;
24526
24527 return s->cmp_to;
24528 }
24529
24530 static int
24531 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24532 int start, int end, int overlaps)
24533 {
24534 struct glyph *glyph, *last;
24535 Lisp_Object lgstring;
24536 int i;
24537
24538 s->for_overlaps = overlaps;
24539 glyph = s->row->glyphs[s->area] + start;
24540 last = s->row->glyphs[s->area] + end;
24541 s->cmp_id = glyph->u.cmp.id;
24542 s->cmp_from = glyph->slice.cmp.from;
24543 s->cmp_to = glyph->slice.cmp.to + 1;
24544 s->face = FACE_FROM_ID (s->f, face_id);
24545 lgstring = composition_gstring_from_id (s->cmp_id);
24546 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24547 glyph++;
24548 while (glyph < last
24549 && glyph->u.cmp.automatic
24550 && glyph->u.cmp.id == s->cmp_id
24551 && s->cmp_to == glyph->slice.cmp.from)
24552 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24553
24554 for (i = s->cmp_from; i < s->cmp_to; i++)
24555 {
24556 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24557 unsigned code = LGLYPH_CODE (lglyph);
24558
24559 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24560 }
24561 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24562 return glyph - s->row->glyphs[s->area];
24563 }
24564
24565
24566 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24567 See the comment of fill_glyph_string for arguments.
24568 Value is the index of the first glyph not in S. */
24569
24570
24571 static int
24572 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24573 int start, int end, int overlaps)
24574 {
24575 struct glyph *glyph, *last;
24576 int voffset;
24577
24578 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24579 s->for_overlaps = overlaps;
24580 glyph = s->row->glyphs[s->area] + start;
24581 last = s->row->glyphs[s->area] + end;
24582 voffset = glyph->voffset;
24583 s->face = FACE_FROM_ID (s->f, face_id);
24584 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24585 s->nchars = 1;
24586 s->width = glyph->pixel_width;
24587 glyph++;
24588 while (glyph < last
24589 && glyph->type == GLYPHLESS_GLYPH
24590 && glyph->voffset == voffset
24591 && glyph->face_id == face_id)
24592 {
24593 s->nchars++;
24594 s->width += glyph->pixel_width;
24595 glyph++;
24596 }
24597 s->ybase += voffset;
24598 return glyph - s->row->glyphs[s->area];
24599 }
24600
24601
24602 /* Fill glyph string S from a sequence of character glyphs.
24603
24604 FACE_ID is the face id of the string. START is the index of the
24605 first glyph to consider, END is the index of the last + 1.
24606 OVERLAPS non-zero means S should draw the foreground only, and use
24607 its physical height for clipping. See also draw_glyphs.
24608
24609 Value is the index of the first glyph not in S. */
24610
24611 static int
24612 fill_glyph_string (struct glyph_string *s, int face_id,
24613 int start, int end, int overlaps)
24614 {
24615 struct glyph *glyph, *last;
24616 int voffset;
24617 bool glyph_not_available_p;
24618
24619 eassert (s->f == XFRAME (s->w->frame));
24620 eassert (s->nchars == 0);
24621 eassert (start >= 0 && end > start);
24622
24623 s->for_overlaps = overlaps;
24624 glyph = s->row->glyphs[s->area] + start;
24625 last = s->row->glyphs[s->area] + end;
24626 voffset = glyph->voffset;
24627 s->padding_p = glyph->padding_p;
24628 glyph_not_available_p = glyph->glyph_not_available_p;
24629
24630 while (glyph < last
24631 && glyph->type == CHAR_GLYPH
24632 && glyph->voffset == voffset
24633 /* Same face id implies same font, nowadays. */
24634 && glyph->face_id == face_id
24635 && glyph->glyph_not_available_p == glyph_not_available_p)
24636 {
24637 s->face = get_glyph_face_and_encoding (s->f, glyph,
24638 s->char2b + s->nchars);
24639 ++s->nchars;
24640 eassert (s->nchars <= end - start);
24641 s->width += glyph->pixel_width;
24642 if (glyph++->padding_p != s->padding_p)
24643 break;
24644 }
24645
24646 s->font = s->face->font;
24647
24648 /* If the specified font could not be loaded, use the frame's font,
24649 but record the fact that we couldn't load it in
24650 S->font_not_found_p so that we can draw rectangles for the
24651 characters of the glyph string. */
24652 if (s->font == NULL || glyph_not_available_p)
24653 {
24654 s->font_not_found_p = true;
24655 s->font = FRAME_FONT (s->f);
24656 }
24657
24658 /* Adjust base line for subscript/superscript text. */
24659 s->ybase += voffset;
24660
24661 eassert (s->face && s->face->gc);
24662 return glyph - s->row->glyphs[s->area];
24663 }
24664
24665
24666 /* Fill glyph string S from image glyph S->first_glyph. */
24667
24668 static void
24669 fill_image_glyph_string (struct glyph_string *s)
24670 {
24671 eassert (s->first_glyph->type == IMAGE_GLYPH);
24672 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24673 eassert (s->img);
24674 s->slice = s->first_glyph->slice.img;
24675 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24676 s->font = s->face->font;
24677 s->width = s->first_glyph->pixel_width;
24678
24679 /* Adjust base line for subscript/superscript text. */
24680 s->ybase += s->first_glyph->voffset;
24681 }
24682
24683
24684 /* Fill glyph string S from a sequence of stretch glyphs.
24685
24686 START is the index of the first glyph to consider,
24687 END is the index of the last + 1.
24688
24689 Value is the index of the first glyph not in S. */
24690
24691 static int
24692 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24693 {
24694 struct glyph *glyph, *last;
24695 int voffset, face_id;
24696
24697 eassert (s->first_glyph->type == STRETCH_GLYPH);
24698
24699 glyph = s->row->glyphs[s->area] + start;
24700 last = s->row->glyphs[s->area] + end;
24701 face_id = glyph->face_id;
24702 s->face = FACE_FROM_ID (s->f, face_id);
24703 s->font = s->face->font;
24704 s->width = glyph->pixel_width;
24705 s->nchars = 1;
24706 voffset = glyph->voffset;
24707
24708 for (++glyph;
24709 (glyph < last
24710 && glyph->type == STRETCH_GLYPH
24711 && glyph->voffset == voffset
24712 && glyph->face_id == face_id);
24713 ++glyph)
24714 s->width += glyph->pixel_width;
24715
24716 /* Adjust base line for subscript/superscript text. */
24717 s->ybase += voffset;
24718
24719 /* The case that face->gc == 0 is handled when drawing the glyph
24720 string by calling prepare_face_for_display. */
24721 eassert (s->face);
24722 return glyph - s->row->glyphs[s->area];
24723 }
24724
24725 static struct font_metrics *
24726 get_per_char_metric (struct font *font, XChar2b *char2b)
24727 {
24728 static struct font_metrics metrics;
24729 unsigned code;
24730
24731 if (! font)
24732 return NULL;
24733 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24734 if (code == FONT_INVALID_CODE)
24735 return NULL;
24736 font->driver->text_extents (font, &code, 1, &metrics);
24737 return &metrics;
24738 }
24739
24740 /* A subroutine that computes "normal" values of ASCENT and DESCENT
24741 for FONT. Values are taken from font-global ones, except for fonts
24742 that claim preposterously large values, but whose glyphs actually
24743 have reasonable dimensions. C is the character to use for metrics
24744 if the font-global values are too large; if C is negative, the
24745 function selects a default character. */
24746 static void
24747 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
24748 {
24749 *ascent = FONT_BASE (font);
24750 *descent = FONT_DESCENT (font);
24751
24752 if (FONT_TOO_HIGH (font))
24753 {
24754 XChar2b char2b;
24755
24756 /* Get metrics of C, defaulting to a reasonably sized ASCII
24757 character. */
24758 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
24759 {
24760 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
24761
24762 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
24763 {
24764 /* We add 1 pixel to character dimensions as heuristics
24765 that produces nicer display, e.g. when the face has
24766 the box attribute. */
24767 *ascent = pcm->ascent + 1;
24768 *descent = pcm->descent + 1;
24769 }
24770 }
24771 }
24772 }
24773
24774 /* A subroutine that computes a reasonable "normal character height"
24775 for fonts that claim preposterously large vertical dimensions, but
24776 whose glyphs are actually reasonably sized. C is the character
24777 whose metrics to use for those fonts, or -1 for default
24778 character. */
24779 static int
24780 normal_char_height (struct font *font, int c)
24781 {
24782 int ascent, descent;
24783
24784 normal_char_ascent_descent (font, c, &ascent, &descent);
24785
24786 return ascent + descent;
24787 }
24788
24789 /* EXPORT for RIF:
24790 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24791 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24792 assumed to be zero. */
24793
24794 void
24795 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24796 {
24797 *left = *right = 0;
24798
24799 if (glyph->type == CHAR_GLYPH)
24800 {
24801 XChar2b char2b;
24802 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24803 if (face->font)
24804 {
24805 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24806 if (pcm)
24807 {
24808 if (pcm->rbearing > pcm->width)
24809 *right = pcm->rbearing - pcm->width;
24810 if (pcm->lbearing < 0)
24811 *left = -pcm->lbearing;
24812 }
24813 }
24814 }
24815 else if (glyph->type == COMPOSITE_GLYPH)
24816 {
24817 if (! glyph->u.cmp.automatic)
24818 {
24819 struct composition *cmp = composition_table[glyph->u.cmp.id];
24820
24821 if (cmp->rbearing > cmp->pixel_width)
24822 *right = cmp->rbearing - cmp->pixel_width;
24823 if (cmp->lbearing < 0)
24824 *left = - cmp->lbearing;
24825 }
24826 else
24827 {
24828 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24829 struct font_metrics metrics;
24830
24831 composition_gstring_width (gstring, glyph->slice.cmp.from,
24832 glyph->slice.cmp.to + 1, &metrics);
24833 if (metrics.rbearing > metrics.width)
24834 *right = metrics.rbearing - metrics.width;
24835 if (metrics.lbearing < 0)
24836 *left = - metrics.lbearing;
24837 }
24838 }
24839 }
24840
24841
24842 /* Return the index of the first glyph preceding glyph string S that
24843 is overwritten by S because of S's left overhang. Value is -1
24844 if no glyphs are overwritten. */
24845
24846 static int
24847 left_overwritten (struct glyph_string *s)
24848 {
24849 int k;
24850
24851 if (s->left_overhang)
24852 {
24853 int x = 0, i;
24854 struct glyph *glyphs = s->row->glyphs[s->area];
24855 int first = s->first_glyph - glyphs;
24856
24857 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24858 x -= glyphs[i].pixel_width;
24859
24860 k = i + 1;
24861 }
24862 else
24863 k = -1;
24864
24865 return k;
24866 }
24867
24868
24869 /* Return the index of the first glyph preceding glyph string S that
24870 is overwriting S because of its right overhang. Value is -1 if no
24871 glyph in front of S overwrites S. */
24872
24873 static int
24874 left_overwriting (struct glyph_string *s)
24875 {
24876 int i, k, x;
24877 struct glyph *glyphs = s->row->glyphs[s->area];
24878 int first = s->first_glyph - glyphs;
24879
24880 k = -1;
24881 x = 0;
24882 for (i = first - 1; i >= 0; --i)
24883 {
24884 int left, right;
24885 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24886 if (x + right > 0)
24887 k = i;
24888 x -= glyphs[i].pixel_width;
24889 }
24890
24891 return k;
24892 }
24893
24894
24895 /* Return the index of the last glyph following glyph string S that is
24896 overwritten by S because of S's right overhang. Value is -1 if
24897 no such glyph is found. */
24898
24899 static int
24900 right_overwritten (struct glyph_string *s)
24901 {
24902 int k = -1;
24903
24904 if (s->right_overhang)
24905 {
24906 int x = 0, i;
24907 struct glyph *glyphs = s->row->glyphs[s->area];
24908 int first = (s->first_glyph - glyphs
24909 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24910 int end = s->row->used[s->area];
24911
24912 for (i = first; i < end && s->right_overhang > x; ++i)
24913 x += glyphs[i].pixel_width;
24914
24915 k = i;
24916 }
24917
24918 return k;
24919 }
24920
24921
24922 /* Return the index of the last glyph following glyph string S that
24923 overwrites S because of its left overhang. Value is negative
24924 if no such glyph is found. */
24925
24926 static int
24927 right_overwriting (struct glyph_string *s)
24928 {
24929 int i, k, x;
24930 int end = s->row->used[s->area];
24931 struct glyph *glyphs = s->row->glyphs[s->area];
24932 int first = (s->first_glyph - glyphs
24933 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24934
24935 k = -1;
24936 x = 0;
24937 for (i = first; i < end; ++i)
24938 {
24939 int left, right;
24940 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24941 if (x - left < 0)
24942 k = i;
24943 x += glyphs[i].pixel_width;
24944 }
24945
24946 return k;
24947 }
24948
24949
24950 /* Set background width of glyph string S. START is the index of the
24951 first glyph following S. LAST_X is the right-most x-position + 1
24952 in the drawing area. */
24953
24954 static void
24955 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24956 {
24957 /* If the face of this glyph string has to be drawn to the end of
24958 the drawing area, set S->extends_to_end_of_line_p. */
24959
24960 if (start == s->row->used[s->area]
24961 && ((s->row->fill_line_p
24962 && (s->hl == DRAW_NORMAL_TEXT
24963 || s->hl == DRAW_IMAGE_RAISED
24964 || s->hl == DRAW_IMAGE_SUNKEN))
24965 || s->hl == DRAW_MOUSE_FACE))
24966 s->extends_to_end_of_line_p = true;
24967
24968 /* If S extends its face to the end of the line, set its
24969 background_width to the distance to the right edge of the drawing
24970 area. */
24971 if (s->extends_to_end_of_line_p)
24972 s->background_width = last_x - s->x + 1;
24973 else
24974 s->background_width = s->width;
24975 }
24976
24977
24978 /* Compute overhangs and x-positions for glyph string S and its
24979 predecessors, or successors. X is the starting x-position for S.
24980 BACKWARD_P means process predecessors. */
24981
24982 static void
24983 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
24984 {
24985 if (backward_p)
24986 {
24987 while (s)
24988 {
24989 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24990 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24991 x -= s->width;
24992 s->x = x;
24993 s = s->prev;
24994 }
24995 }
24996 else
24997 {
24998 while (s)
24999 {
25000 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25001 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25002 s->x = x;
25003 x += s->width;
25004 s = s->next;
25005 }
25006 }
25007 }
25008
25009
25010
25011 /* The following macros are only called from draw_glyphs below.
25012 They reference the following parameters of that function directly:
25013 `w', `row', `area', and `overlap_p'
25014 as well as the following local variables:
25015 `s', `f', and `hdc' (in W32) */
25016
25017 #ifdef HAVE_NTGUI
25018 /* On W32, silently add local `hdc' variable to argument list of
25019 init_glyph_string. */
25020 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25021 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25022 #else
25023 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25024 init_glyph_string (s, char2b, w, row, area, start, hl)
25025 #endif
25026
25027 /* Add a glyph string for a stretch glyph to the list of strings
25028 between HEAD and TAIL. START is the index of the stretch glyph in
25029 row area AREA of glyph row ROW. END is the index of the last glyph
25030 in that glyph row area. X is the current output position assigned
25031 to the new glyph string constructed. HL overrides that face of the
25032 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25033 is the right-most x-position of the drawing area. */
25034
25035 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25036 and below -- keep them on one line. */
25037 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25038 do \
25039 { \
25040 s = alloca (sizeof *s); \
25041 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25042 START = fill_stretch_glyph_string (s, START, END); \
25043 append_glyph_string (&HEAD, &TAIL, s); \
25044 s->x = (X); \
25045 } \
25046 while (false)
25047
25048
25049 /* Add a glyph string for an image glyph to the list of strings
25050 between HEAD and TAIL. START is the index of the image glyph in
25051 row area AREA of glyph row ROW. END is the index of the last glyph
25052 in that glyph row area. X is the current output position assigned
25053 to the new glyph string constructed. HL overrides that face of the
25054 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25055 is the right-most x-position of the drawing area. */
25056
25057 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25058 do \
25059 { \
25060 s = alloca (sizeof *s); \
25061 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25062 fill_image_glyph_string (s); \
25063 append_glyph_string (&HEAD, &TAIL, s); \
25064 ++START; \
25065 s->x = (X); \
25066 } \
25067 while (false)
25068
25069
25070 /* Add a glyph string for a sequence of character glyphs to the list
25071 of strings between HEAD and TAIL. START is the index of the first
25072 glyph in row area AREA of glyph row ROW that is part of the new
25073 glyph string. END is the index of the last glyph in that glyph row
25074 area. X is the current output position assigned to the new glyph
25075 string constructed. HL overrides that face of the glyph; e.g. it
25076 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25077 right-most x-position of the drawing area. */
25078
25079 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25080 do \
25081 { \
25082 int face_id; \
25083 XChar2b *char2b; \
25084 \
25085 face_id = (row)->glyphs[area][START].face_id; \
25086 \
25087 s = alloca (sizeof *s); \
25088 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25089 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25090 append_glyph_string (&HEAD, &TAIL, s); \
25091 s->x = (X); \
25092 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25093 } \
25094 while (false)
25095
25096
25097 /* Add a glyph string for a composite sequence to the list of strings
25098 between HEAD and TAIL. START is the index of the first glyph in
25099 row area AREA of glyph row ROW that is part of the new glyph
25100 string. END is the index of the last glyph in that glyph row area.
25101 X is the current output position assigned to the new glyph string
25102 constructed. HL overrides that face of the glyph; e.g. it is
25103 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25104 x-position of the drawing area. */
25105
25106 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25107 do { \
25108 int face_id = (row)->glyphs[area][START].face_id; \
25109 struct face *base_face = FACE_FROM_ID (f, face_id); \
25110 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25111 struct composition *cmp = composition_table[cmp_id]; \
25112 XChar2b *char2b; \
25113 struct glyph_string *first_s = NULL; \
25114 int n; \
25115 \
25116 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25117 \
25118 /* Make glyph_strings for each glyph sequence that is drawable by \
25119 the same face, and append them to HEAD/TAIL. */ \
25120 for (n = 0; n < cmp->glyph_len;) \
25121 { \
25122 s = alloca (sizeof *s); \
25123 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25124 append_glyph_string (&(HEAD), &(TAIL), s); \
25125 s->cmp = cmp; \
25126 s->cmp_from = n; \
25127 s->x = (X); \
25128 if (n == 0) \
25129 first_s = s; \
25130 n = fill_composite_glyph_string (s, base_face, overlaps); \
25131 } \
25132 \
25133 ++START; \
25134 s = first_s; \
25135 } while (false)
25136
25137
25138 /* Add a glyph string for a glyph-string sequence to the list of strings
25139 between HEAD and TAIL. */
25140
25141 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25142 do { \
25143 int face_id; \
25144 XChar2b *char2b; \
25145 Lisp_Object gstring; \
25146 \
25147 face_id = (row)->glyphs[area][START].face_id; \
25148 gstring = (composition_gstring_from_id \
25149 ((row)->glyphs[area][START].u.cmp.id)); \
25150 s = alloca (sizeof *s); \
25151 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25152 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25153 append_glyph_string (&(HEAD), &(TAIL), s); \
25154 s->x = (X); \
25155 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25156 } while (false)
25157
25158
25159 /* Add a glyph string for a sequence of glyphless character's glyphs
25160 to the list of strings between HEAD and TAIL. The meanings of
25161 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25162
25163 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25164 do \
25165 { \
25166 int face_id; \
25167 \
25168 face_id = (row)->glyphs[area][START].face_id; \
25169 \
25170 s = alloca (sizeof *s); \
25171 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25172 append_glyph_string (&HEAD, &TAIL, s); \
25173 s->x = (X); \
25174 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25175 overlaps); \
25176 } \
25177 while (false)
25178
25179
25180 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25181 of AREA of glyph row ROW on window W between indices START and END.
25182 HL overrides the face for drawing glyph strings, e.g. it is
25183 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25184 x-positions of the drawing area.
25185
25186 This is an ugly monster macro construct because we must use alloca
25187 to allocate glyph strings (because draw_glyphs can be called
25188 asynchronously). */
25189
25190 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25191 do \
25192 { \
25193 HEAD = TAIL = NULL; \
25194 while (START < END) \
25195 { \
25196 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25197 switch (first_glyph->type) \
25198 { \
25199 case CHAR_GLYPH: \
25200 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25201 HL, X, LAST_X); \
25202 break; \
25203 \
25204 case COMPOSITE_GLYPH: \
25205 if (first_glyph->u.cmp.automatic) \
25206 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25207 HL, X, LAST_X); \
25208 else \
25209 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25210 HL, X, LAST_X); \
25211 break; \
25212 \
25213 case STRETCH_GLYPH: \
25214 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25215 HL, X, LAST_X); \
25216 break; \
25217 \
25218 case IMAGE_GLYPH: \
25219 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25220 HL, X, LAST_X); \
25221 break; \
25222 \
25223 case GLYPHLESS_GLYPH: \
25224 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25225 HL, X, LAST_X); \
25226 break; \
25227 \
25228 default: \
25229 emacs_abort (); \
25230 } \
25231 \
25232 if (s) \
25233 { \
25234 set_glyph_string_background_width (s, START, LAST_X); \
25235 (X) += s->width; \
25236 } \
25237 } \
25238 } while (false)
25239
25240
25241 /* Draw glyphs between START and END in AREA of ROW on window W,
25242 starting at x-position X. X is relative to AREA in W. HL is a
25243 face-override with the following meaning:
25244
25245 DRAW_NORMAL_TEXT draw normally
25246 DRAW_CURSOR draw in cursor face
25247 DRAW_MOUSE_FACE draw in mouse face.
25248 DRAW_INVERSE_VIDEO draw in mode line face
25249 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25250 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25251
25252 If OVERLAPS is non-zero, draw only the foreground of characters and
25253 clip to the physical height of ROW. Non-zero value also defines
25254 the overlapping part to be drawn:
25255
25256 OVERLAPS_PRED overlap with preceding rows
25257 OVERLAPS_SUCC overlap with succeeding rows
25258 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25259 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25260
25261 Value is the x-position reached, relative to AREA of W. */
25262
25263 static int
25264 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25265 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25266 enum draw_glyphs_face hl, int overlaps)
25267 {
25268 struct glyph_string *head, *tail;
25269 struct glyph_string *s;
25270 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25271 int i, j, x_reached, last_x, area_left = 0;
25272 struct frame *f = XFRAME (WINDOW_FRAME (w));
25273 DECLARE_HDC (hdc);
25274
25275 ALLOCATE_HDC (hdc, f);
25276
25277 /* Let's rather be paranoid than getting a SEGV. */
25278 end = min (end, row->used[area]);
25279 start = clip_to_bounds (0, start, end);
25280
25281 /* Translate X to frame coordinates. Set last_x to the right
25282 end of the drawing area. */
25283 if (row->full_width_p)
25284 {
25285 /* X is relative to the left edge of W, without scroll bars
25286 or fringes. */
25287 area_left = WINDOW_LEFT_EDGE_X (w);
25288 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25289 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25290 }
25291 else
25292 {
25293 area_left = window_box_left (w, area);
25294 last_x = area_left + window_box_width (w, area);
25295 }
25296 x += area_left;
25297
25298 /* Build a doubly-linked list of glyph_string structures between
25299 head and tail from what we have to draw. Note that the macro
25300 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25301 the reason we use a separate variable `i'. */
25302 i = start;
25303 USE_SAFE_ALLOCA;
25304 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25305 if (tail)
25306 x_reached = tail->x + tail->background_width;
25307 else
25308 x_reached = x;
25309
25310 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25311 the row, redraw some glyphs in front or following the glyph
25312 strings built above. */
25313 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25314 {
25315 struct glyph_string *h, *t;
25316 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25317 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25318 bool check_mouse_face = false;
25319 int dummy_x = 0;
25320
25321 /* If mouse highlighting is on, we may need to draw adjacent
25322 glyphs using mouse-face highlighting. */
25323 if (area == TEXT_AREA && row->mouse_face_p
25324 && hlinfo->mouse_face_beg_row >= 0
25325 && hlinfo->mouse_face_end_row >= 0)
25326 {
25327 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25328
25329 if (row_vpos >= hlinfo->mouse_face_beg_row
25330 && row_vpos <= hlinfo->mouse_face_end_row)
25331 {
25332 check_mouse_face = true;
25333 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25334 ? hlinfo->mouse_face_beg_col : 0;
25335 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25336 ? hlinfo->mouse_face_end_col
25337 : row->used[TEXT_AREA];
25338 }
25339 }
25340
25341 /* Compute overhangs for all glyph strings. */
25342 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25343 for (s = head; s; s = s->next)
25344 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25345
25346 /* Prepend glyph strings for glyphs in front of the first glyph
25347 string that are overwritten because of the first glyph
25348 string's left overhang. The background of all strings
25349 prepended must be drawn because the first glyph string
25350 draws over it. */
25351 i = left_overwritten (head);
25352 if (i >= 0)
25353 {
25354 enum draw_glyphs_face overlap_hl;
25355
25356 /* If this row contains mouse highlighting, attempt to draw
25357 the overlapped glyphs with the correct highlight. This
25358 code fails if the overlap encompasses more than one glyph
25359 and mouse-highlight spans only some of these glyphs.
25360 However, making it work perfectly involves a lot more
25361 code, and I don't know if the pathological case occurs in
25362 practice, so we'll stick to this for now. --- cyd */
25363 if (check_mouse_face
25364 && mouse_beg_col < start && mouse_end_col > i)
25365 overlap_hl = DRAW_MOUSE_FACE;
25366 else
25367 overlap_hl = DRAW_NORMAL_TEXT;
25368
25369 if (hl != overlap_hl)
25370 clip_head = head;
25371 j = i;
25372 BUILD_GLYPH_STRINGS (j, start, h, t,
25373 overlap_hl, dummy_x, last_x);
25374 start = i;
25375 compute_overhangs_and_x (t, head->x, true);
25376 prepend_glyph_string_lists (&head, &tail, h, t);
25377 if (clip_head == NULL)
25378 clip_head = head;
25379 }
25380
25381 /* Prepend glyph strings for glyphs in front of the first glyph
25382 string that overwrite that glyph string because of their
25383 right overhang. For these strings, only the foreground must
25384 be drawn, because it draws over the glyph string at `head'.
25385 The background must not be drawn because this would overwrite
25386 right overhangs of preceding glyphs for which no glyph
25387 strings exist. */
25388 i = left_overwriting (head);
25389 if (i >= 0)
25390 {
25391 enum draw_glyphs_face overlap_hl;
25392
25393 if (check_mouse_face
25394 && mouse_beg_col < start && mouse_end_col > i)
25395 overlap_hl = DRAW_MOUSE_FACE;
25396 else
25397 overlap_hl = DRAW_NORMAL_TEXT;
25398
25399 if (hl == overlap_hl || clip_head == NULL)
25400 clip_head = head;
25401 BUILD_GLYPH_STRINGS (i, start, h, t,
25402 overlap_hl, dummy_x, last_x);
25403 for (s = h; s; s = s->next)
25404 s->background_filled_p = true;
25405 compute_overhangs_and_x (t, head->x, true);
25406 prepend_glyph_string_lists (&head, &tail, h, t);
25407 }
25408
25409 /* Append glyphs strings for glyphs following the last glyph
25410 string tail that are overwritten by tail. The background of
25411 these strings has to be drawn because tail's foreground draws
25412 over it. */
25413 i = right_overwritten (tail);
25414 if (i >= 0)
25415 {
25416 enum draw_glyphs_face overlap_hl;
25417
25418 if (check_mouse_face
25419 && mouse_beg_col < i && mouse_end_col > end)
25420 overlap_hl = DRAW_MOUSE_FACE;
25421 else
25422 overlap_hl = DRAW_NORMAL_TEXT;
25423
25424 if (hl != overlap_hl)
25425 clip_tail = tail;
25426 BUILD_GLYPH_STRINGS (end, i, h, t,
25427 overlap_hl, x, last_x);
25428 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25429 we don't have `end = i;' here. */
25430 compute_overhangs_and_x (h, tail->x + tail->width, false);
25431 append_glyph_string_lists (&head, &tail, h, t);
25432 if (clip_tail == NULL)
25433 clip_tail = tail;
25434 }
25435
25436 /* Append glyph strings for glyphs following the last glyph
25437 string tail that overwrite tail. The foreground of such
25438 glyphs has to be drawn because it writes into the background
25439 of tail. The background must not be drawn because it could
25440 paint over the foreground of following glyphs. */
25441 i = right_overwriting (tail);
25442 if (i >= 0)
25443 {
25444 enum draw_glyphs_face overlap_hl;
25445 if (check_mouse_face
25446 && mouse_beg_col < i && mouse_end_col > end)
25447 overlap_hl = DRAW_MOUSE_FACE;
25448 else
25449 overlap_hl = DRAW_NORMAL_TEXT;
25450
25451 if (hl == overlap_hl || clip_tail == NULL)
25452 clip_tail = tail;
25453 i++; /* We must include the Ith glyph. */
25454 BUILD_GLYPH_STRINGS (end, i, h, t,
25455 overlap_hl, x, last_x);
25456 for (s = h; s; s = s->next)
25457 s->background_filled_p = true;
25458 compute_overhangs_and_x (h, tail->x + tail->width, false);
25459 append_glyph_string_lists (&head, &tail, h, t);
25460 }
25461 if (clip_head || clip_tail)
25462 for (s = head; s; s = s->next)
25463 {
25464 s->clip_head = clip_head;
25465 s->clip_tail = clip_tail;
25466 }
25467 }
25468
25469 /* Draw all strings. */
25470 for (s = head; s; s = s->next)
25471 FRAME_RIF (f)->draw_glyph_string (s);
25472
25473 #ifndef HAVE_NS
25474 /* When focus a sole frame and move horizontally, this clears on_p
25475 causing a failure to erase prev cursor position. */
25476 if (area == TEXT_AREA
25477 && !row->full_width_p
25478 /* When drawing overlapping rows, only the glyph strings'
25479 foreground is drawn, which doesn't erase a cursor
25480 completely. */
25481 && !overlaps)
25482 {
25483 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25484 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25485 : (tail ? tail->x + tail->background_width : x));
25486 x0 -= area_left;
25487 x1 -= area_left;
25488
25489 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25490 row->y, MATRIX_ROW_BOTTOM_Y (row));
25491 }
25492 #endif
25493
25494 /* Value is the x-position up to which drawn, relative to AREA of W.
25495 This doesn't include parts drawn because of overhangs. */
25496 if (row->full_width_p)
25497 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25498 else
25499 x_reached -= area_left;
25500
25501 RELEASE_HDC (hdc, f);
25502
25503 SAFE_FREE ();
25504 return x_reached;
25505 }
25506
25507 /* Expand row matrix if too narrow. Don't expand if area
25508 is not present. */
25509
25510 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25511 { \
25512 if (!it->f->fonts_changed \
25513 && (it->glyph_row->glyphs[area] \
25514 < it->glyph_row->glyphs[area + 1])) \
25515 { \
25516 it->w->ncols_scale_factor++; \
25517 it->f->fonts_changed = true; \
25518 } \
25519 }
25520
25521 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25522 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25523
25524 static void
25525 append_glyph (struct it *it)
25526 {
25527 struct glyph *glyph;
25528 enum glyph_row_area area = it->area;
25529
25530 eassert (it->glyph_row);
25531 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25532
25533 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25534 if (glyph < it->glyph_row->glyphs[area + 1])
25535 {
25536 /* If the glyph row is reversed, we need to prepend the glyph
25537 rather than append it. */
25538 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25539 {
25540 struct glyph *g;
25541
25542 /* Make room for the additional glyph. */
25543 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25544 g[1] = *g;
25545 glyph = it->glyph_row->glyphs[area];
25546 }
25547 glyph->charpos = CHARPOS (it->position);
25548 glyph->object = it->object;
25549 if (it->pixel_width > 0)
25550 {
25551 glyph->pixel_width = it->pixel_width;
25552 glyph->padding_p = false;
25553 }
25554 else
25555 {
25556 /* Assure at least 1-pixel width. Otherwise, cursor can't
25557 be displayed correctly. */
25558 glyph->pixel_width = 1;
25559 glyph->padding_p = true;
25560 }
25561 glyph->ascent = it->ascent;
25562 glyph->descent = it->descent;
25563 glyph->voffset = it->voffset;
25564 glyph->type = CHAR_GLYPH;
25565 glyph->avoid_cursor_p = it->avoid_cursor_p;
25566 glyph->multibyte_p = it->multibyte_p;
25567 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25568 {
25569 /* In R2L rows, the left and the right box edges need to be
25570 drawn in reverse direction. */
25571 glyph->right_box_line_p = it->start_of_box_run_p;
25572 glyph->left_box_line_p = it->end_of_box_run_p;
25573 }
25574 else
25575 {
25576 glyph->left_box_line_p = it->start_of_box_run_p;
25577 glyph->right_box_line_p = it->end_of_box_run_p;
25578 }
25579 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25580 || it->phys_descent > it->descent);
25581 glyph->glyph_not_available_p = it->glyph_not_available_p;
25582 glyph->face_id = it->face_id;
25583 glyph->u.ch = it->char_to_display;
25584 glyph->slice.img = null_glyph_slice;
25585 glyph->font_type = FONT_TYPE_UNKNOWN;
25586 if (it->bidi_p)
25587 {
25588 glyph->resolved_level = it->bidi_it.resolved_level;
25589 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25590 glyph->bidi_type = it->bidi_it.type;
25591 }
25592 else
25593 {
25594 glyph->resolved_level = 0;
25595 glyph->bidi_type = UNKNOWN_BT;
25596 }
25597 ++it->glyph_row->used[area];
25598 }
25599 else
25600 IT_EXPAND_MATRIX_WIDTH (it, area);
25601 }
25602
25603 /* Store one glyph for the composition IT->cmp_it.id in
25604 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25605 non-null. */
25606
25607 static void
25608 append_composite_glyph (struct it *it)
25609 {
25610 struct glyph *glyph;
25611 enum glyph_row_area area = it->area;
25612
25613 eassert (it->glyph_row);
25614
25615 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25616 if (glyph < it->glyph_row->glyphs[area + 1])
25617 {
25618 /* If the glyph row is reversed, we need to prepend the glyph
25619 rather than append it. */
25620 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25621 {
25622 struct glyph *g;
25623
25624 /* Make room for the new glyph. */
25625 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25626 g[1] = *g;
25627 glyph = it->glyph_row->glyphs[it->area];
25628 }
25629 glyph->charpos = it->cmp_it.charpos;
25630 glyph->object = it->object;
25631 glyph->pixel_width = it->pixel_width;
25632 glyph->ascent = it->ascent;
25633 glyph->descent = it->descent;
25634 glyph->voffset = it->voffset;
25635 glyph->type = COMPOSITE_GLYPH;
25636 if (it->cmp_it.ch < 0)
25637 {
25638 glyph->u.cmp.automatic = false;
25639 glyph->u.cmp.id = it->cmp_it.id;
25640 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25641 }
25642 else
25643 {
25644 glyph->u.cmp.automatic = true;
25645 glyph->u.cmp.id = it->cmp_it.id;
25646 glyph->slice.cmp.from = it->cmp_it.from;
25647 glyph->slice.cmp.to = it->cmp_it.to - 1;
25648 }
25649 glyph->avoid_cursor_p = it->avoid_cursor_p;
25650 glyph->multibyte_p = it->multibyte_p;
25651 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25652 {
25653 /* In R2L rows, the left and the right box edges need to be
25654 drawn in reverse direction. */
25655 glyph->right_box_line_p = it->start_of_box_run_p;
25656 glyph->left_box_line_p = it->end_of_box_run_p;
25657 }
25658 else
25659 {
25660 glyph->left_box_line_p = it->start_of_box_run_p;
25661 glyph->right_box_line_p = it->end_of_box_run_p;
25662 }
25663 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25664 || it->phys_descent > it->descent);
25665 glyph->padding_p = false;
25666 glyph->glyph_not_available_p = false;
25667 glyph->face_id = it->face_id;
25668 glyph->font_type = FONT_TYPE_UNKNOWN;
25669 if (it->bidi_p)
25670 {
25671 glyph->resolved_level = it->bidi_it.resolved_level;
25672 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25673 glyph->bidi_type = it->bidi_it.type;
25674 }
25675 ++it->glyph_row->used[area];
25676 }
25677 else
25678 IT_EXPAND_MATRIX_WIDTH (it, area);
25679 }
25680
25681
25682 /* Change IT->ascent and IT->height according to the setting of
25683 IT->voffset. */
25684
25685 static void
25686 take_vertical_position_into_account (struct it *it)
25687 {
25688 if (it->voffset)
25689 {
25690 if (it->voffset < 0)
25691 /* Increase the ascent so that we can display the text higher
25692 in the line. */
25693 it->ascent -= it->voffset;
25694 else
25695 /* Increase the descent so that we can display the text lower
25696 in the line. */
25697 it->descent += it->voffset;
25698 }
25699 }
25700
25701
25702 /* Produce glyphs/get display metrics for the image IT is loaded with.
25703 See the description of struct display_iterator in dispextern.h for
25704 an overview of struct display_iterator. */
25705
25706 static void
25707 produce_image_glyph (struct it *it)
25708 {
25709 struct image *img;
25710 struct face *face;
25711 int glyph_ascent, crop;
25712 struct glyph_slice slice;
25713
25714 eassert (it->what == IT_IMAGE);
25715
25716 face = FACE_FROM_ID (it->f, it->face_id);
25717 eassert (face);
25718 /* Make sure X resources of the face is loaded. */
25719 prepare_face_for_display (it->f, face);
25720
25721 if (it->image_id < 0)
25722 {
25723 /* Fringe bitmap. */
25724 it->ascent = it->phys_ascent = 0;
25725 it->descent = it->phys_descent = 0;
25726 it->pixel_width = 0;
25727 it->nglyphs = 0;
25728 return;
25729 }
25730
25731 img = IMAGE_FROM_ID (it->f, it->image_id);
25732 eassert (img);
25733 /* Make sure X resources of the image is loaded. */
25734 prepare_image_for_display (it->f, img);
25735
25736 slice.x = slice.y = 0;
25737 slice.width = img->width;
25738 slice.height = img->height;
25739
25740 if (INTEGERP (it->slice.x))
25741 slice.x = XINT (it->slice.x);
25742 else if (FLOATP (it->slice.x))
25743 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25744
25745 if (INTEGERP (it->slice.y))
25746 slice.y = XINT (it->slice.y);
25747 else if (FLOATP (it->slice.y))
25748 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25749
25750 if (INTEGERP (it->slice.width))
25751 slice.width = XINT (it->slice.width);
25752 else if (FLOATP (it->slice.width))
25753 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25754
25755 if (INTEGERP (it->slice.height))
25756 slice.height = XINT (it->slice.height);
25757 else if (FLOATP (it->slice.height))
25758 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25759
25760 if (slice.x >= img->width)
25761 slice.x = img->width;
25762 if (slice.y >= img->height)
25763 slice.y = img->height;
25764 if (slice.x + slice.width >= img->width)
25765 slice.width = img->width - slice.x;
25766 if (slice.y + slice.height > img->height)
25767 slice.height = img->height - slice.y;
25768
25769 if (slice.width == 0 || slice.height == 0)
25770 return;
25771
25772 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25773
25774 it->descent = slice.height - glyph_ascent;
25775 if (slice.y == 0)
25776 it->descent += img->vmargin;
25777 if (slice.y + slice.height == img->height)
25778 it->descent += img->vmargin;
25779 it->phys_descent = it->descent;
25780
25781 it->pixel_width = slice.width;
25782 if (slice.x == 0)
25783 it->pixel_width += img->hmargin;
25784 if (slice.x + slice.width == img->width)
25785 it->pixel_width += img->hmargin;
25786
25787 /* It's quite possible for images to have an ascent greater than
25788 their height, so don't get confused in that case. */
25789 if (it->descent < 0)
25790 it->descent = 0;
25791
25792 it->nglyphs = 1;
25793
25794 if (face->box != FACE_NO_BOX)
25795 {
25796 if (face->box_line_width > 0)
25797 {
25798 if (slice.y == 0)
25799 it->ascent += face->box_line_width;
25800 if (slice.y + slice.height == img->height)
25801 it->descent += face->box_line_width;
25802 }
25803
25804 if (it->start_of_box_run_p && slice.x == 0)
25805 it->pixel_width += eabs (face->box_line_width);
25806 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25807 it->pixel_width += eabs (face->box_line_width);
25808 }
25809
25810 take_vertical_position_into_account (it);
25811
25812 /* Automatically crop wide image glyphs at right edge so we can
25813 draw the cursor on same display row. */
25814 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25815 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25816 {
25817 it->pixel_width -= crop;
25818 slice.width -= crop;
25819 }
25820
25821 if (it->glyph_row)
25822 {
25823 struct glyph *glyph;
25824 enum glyph_row_area area = it->area;
25825
25826 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25827 if (it->glyph_row->reversed_p)
25828 {
25829 struct glyph *g;
25830
25831 /* Make room for the new glyph. */
25832 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25833 g[1] = *g;
25834 glyph = it->glyph_row->glyphs[it->area];
25835 }
25836 if (glyph < it->glyph_row->glyphs[area + 1])
25837 {
25838 glyph->charpos = CHARPOS (it->position);
25839 glyph->object = it->object;
25840 glyph->pixel_width = it->pixel_width;
25841 glyph->ascent = glyph_ascent;
25842 glyph->descent = it->descent;
25843 glyph->voffset = it->voffset;
25844 glyph->type = IMAGE_GLYPH;
25845 glyph->avoid_cursor_p = it->avoid_cursor_p;
25846 glyph->multibyte_p = it->multibyte_p;
25847 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25848 {
25849 /* In R2L rows, the left and the right box edges need to be
25850 drawn in reverse direction. */
25851 glyph->right_box_line_p = it->start_of_box_run_p;
25852 glyph->left_box_line_p = it->end_of_box_run_p;
25853 }
25854 else
25855 {
25856 glyph->left_box_line_p = it->start_of_box_run_p;
25857 glyph->right_box_line_p = it->end_of_box_run_p;
25858 }
25859 glyph->overlaps_vertically_p = false;
25860 glyph->padding_p = false;
25861 glyph->glyph_not_available_p = false;
25862 glyph->face_id = it->face_id;
25863 glyph->u.img_id = img->id;
25864 glyph->slice.img = slice;
25865 glyph->font_type = FONT_TYPE_UNKNOWN;
25866 if (it->bidi_p)
25867 {
25868 glyph->resolved_level = it->bidi_it.resolved_level;
25869 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25870 glyph->bidi_type = it->bidi_it.type;
25871 }
25872 ++it->glyph_row->used[area];
25873 }
25874 else
25875 IT_EXPAND_MATRIX_WIDTH (it, area);
25876 }
25877 }
25878
25879
25880 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25881 of the glyph, WIDTH and HEIGHT are the width and height of the
25882 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25883
25884 static void
25885 append_stretch_glyph (struct it *it, Lisp_Object object,
25886 int width, int height, int ascent)
25887 {
25888 struct glyph *glyph;
25889 enum glyph_row_area area = it->area;
25890
25891 eassert (ascent >= 0 && ascent <= height);
25892
25893 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25894 if (glyph < it->glyph_row->glyphs[area + 1])
25895 {
25896 /* If the glyph row is reversed, we need to prepend the glyph
25897 rather than append it. */
25898 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25899 {
25900 struct glyph *g;
25901
25902 /* Make room for the additional glyph. */
25903 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25904 g[1] = *g;
25905 glyph = it->glyph_row->glyphs[area];
25906
25907 /* Decrease the width of the first glyph of the row that
25908 begins before first_visible_x (e.g., due to hscroll).
25909 This is so the overall width of the row becomes smaller
25910 by the scroll amount, and the stretch glyph appended by
25911 extend_face_to_end_of_line will be wider, to shift the
25912 row glyphs to the right. (In L2R rows, the corresponding
25913 left-shift effect is accomplished by setting row->x to a
25914 negative value, which won't work with R2L rows.)
25915
25916 This must leave us with a positive value of WIDTH, since
25917 otherwise the call to move_it_in_display_line_to at the
25918 beginning of display_line would have got past the entire
25919 first glyph, and then it->current_x would have been
25920 greater or equal to it->first_visible_x. */
25921 if (it->current_x < it->first_visible_x)
25922 width -= it->first_visible_x - it->current_x;
25923 eassert (width > 0);
25924 }
25925 glyph->charpos = CHARPOS (it->position);
25926 glyph->object = object;
25927 glyph->pixel_width = width;
25928 glyph->ascent = ascent;
25929 glyph->descent = height - ascent;
25930 glyph->voffset = it->voffset;
25931 glyph->type = STRETCH_GLYPH;
25932 glyph->avoid_cursor_p = it->avoid_cursor_p;
25933 glyph->multibyte_p = it->multibyte_p;
25934 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25935 {
25936 /* In R2L rows, the left and the right box edges need to be
25937 drawn in reverse direction. */
25938 glyph->right_box_line_p = it->start_of_box_run_p;
25939 glyph->left_box_line_p = it->end_of_box_run_p;
25940 }
25941 else
25942 {
25943 glyph->left_box_line_p = it->start_of_box_run_p;
25944 glyph->right_box_line_p = it->end_of_box_run_p;
25945 }
25946 glyph->overlaps_vertically_p = false;
25947 glyph->padding_p = false;
25948 glyph->glyph_not_available_p = false;
25949 glyph->face_id = it->face_id;
25950 glyph->u.stretch.ascent = ascent;
25951 glyph->u.stretch.height = height;
25952 glyph->slice.img = null_glyph_slice;
25953 glyph->font_type = FONT_TYPE_UNKNOWN;
25954 if (it->bidi_p)
25955 {
25956 glyph->resolved_level = it->bidi_it.resolved_level;
25957 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25958 glyph->bidi_type = it->bidi_it.type;
25959 }
25960 else
25961 {
25962 glyph->resolved_level = 0;
25963 glyph->bidi_type = UNKNOWN_BT;
25964 }
25965 ++it->glyph_row->used[area];
25966 }
25967 else
25968 IT_EXPAND_MATRIX_WIDTH (it, area);
25969 }
25970
25971 #endif /* HAVE_WINDOW_SYSTEM */
25972
25973 /* Produce a stretch glyph for iterator IT. IT->object is the value
25974 of the glyph property displayed. The value must be a list
25975 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25976 being recognized:
25977
25978 1. `:width WIDTH' specifies that the space should be WIDTH *
25979 canonical char width wide. WIDTH may be an integer or floating
25980 point number.
25981
25982 2. `:relative-width FACTOR' specifies that the width of the stretch
25983 should be computed from the width of the first character having the
25984 `glyph' property, and should be FACTOR times that width.
25985
25986 3. `:align-to HPOS' specifies that the space should be wide enough
25987 to reach HPOS, a value in canonical character units.
25988
25989 Exactly one of the above pairs must be present.
25990
25991 4. `:height HEIGHT' specifies that the height of the stretch produced
25992 should be HEIGHT, measured in canonical character units.
25993
25994 5. `:relative-height FACTOR' specifies that the height of the
25995 stretch should be FACTOR times the height of the characters having
25996 the glyph property.
25997
25998 Either none or exactly one of 4 or 5 must be present.
25999
26000 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26001 of the stretch should be used for the ascent of the stretch.
26002 ASCENT must be in the range 0 <= ASCENT <= 100. */
26003
26004 void
26005 produce_stretch_glyph (struct it *it)
26006 {
26007 /* (space :width WIDTH :height HEIGHT ...) */
26008 Lisp_Object prop, plist;
26009 int width = 0, height = 0, align_to = -1;
26010 bool zero_width_ok_p = false;
26011 double tem;
26012 struct font *font = NULL;
26013
26014 #ifdef HAVE_WINDOW_SYSTEM
26015 int ascent = 0;
26016 bool zero_height_ok_p = false;
26017
26018 if (FRAME_WINDOW_P (it->f))
26019 {
26020 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26021 font = face->font ? face->font : FRAME_FONT (it->f);
26022 prepare_face_for_display (it->f, face);
26023 }
26024 #endif
26025
26026 /* List should start with `space'. */
26027 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26028 plist = XCDR (it->object);
26029
26030 /* Compute the width of the stretch. */
26031 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26032 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26033 {
26034 /* Absolute width `:width WIDTH' specified and valid. */
26035 zero_width_ok_p = true;
26036 width = (int)tem;
26037 }
26038 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26039 {
26040 /* Relative width `:relative-width FACTOR' specified and valid.
26041 Compute the width of the characters having the `glyph'
26042 property. */
26043 struct it it2;
26044 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26045
26046 it2 = *it;
26047 if (it->multibyte_p)
26048 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26049 else
26050 {
26051 it2.c = it2.char_to_display = *p, it2.len = 1;
26052 if (! ASCII_CHAR_P (it2.c))
26053 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26054 }
26055
26056 it2.glyph_row = NULL;
26057 it2.what = IT_CHARACTER;
26058 PRODUCE_GLYPHS (&it2);
26059 width = NUMVAL (prop) * it2.pixel_width;
26060 }
26061 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26062 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26063 &align_to))
26064 {
26065 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26066 align_to = (align_to < 0
26067 ? 0
26068 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26069 else if (align_to < 0)
26070 align_to = window_box_left_offset (it->w, TEXT_AREA);
26071 width = max (0, (int)tem + align_to - it->current_x);
26072 zero_width_ok_p = true;
26073 }
26074 else
26075 /* Nothing specified -> width defaults to canonical char width. */
26076 width = FRAME_COLUMN_WIDTH (it->f);
26077
26078 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26079 width = 1;
26080
26081 #ifdef HAVE_WINDOW_SYSTEM
26082 /* Compute height. */
26083 if (FRAME_WINDOW_P (it->f))
26084 {
26085 int default_height = normal_char_height (font, ' ');
26086
26087 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26088 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26089 {
26090 height = (int)tem;
26091 zero_height_ok_p = true;
26092 }
26093 else if (prop = Fplist_get (plist, QCrelative_height),
26094 NUMVAL (prop) > 0)
26095 height = default_height * NUMVAL (prop);
26096 else
26097 height = default_height;
26098
26099 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26100 height = 1;
26101
26102 /* Compute percentage of height used for ascent. If
26103 `:ascent ASCENT' is present and valid, use that. Otherwise,
26104 derive the ascent from the font in use. */
26105 if (prop = Fplist_get (plist, QCascent),
26106 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26107 ascent = height * NUMVAL (prop) / 100.0;
26108 else if (!NILP (prop)
26109 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26110 ascent = min (max (0, (int)tem), height);
26111 else
26112 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26113 }
26114 else
26115 #endif /* HAVE_WINDOW_SYSTEM */
26116 height = 1;
26117
26118 if (width > 0 && it->line_wrap != TRUNCATE
26119 && it->current_x + width > it->last_visible_x)
26120 {
26121 width = it->last_visible_x - it->current_x;
26122 #ifdef HAVE_WINDOW_SYSTEM
26123 /* Subtract one more pixel from the stretch width, but only on
26124 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26125 width -= FRAME_WINDOW_P (it->f);
26126 #endif
26127 }
26128
26129 if (width > 0 && height > 0 && it->glyph_row)
26130 {
26131 Lisp_Object o_object = it->object;
26132 Lisp_Object object = it->stack[it->sp - 1].string;
26133 int n = width;
26134
26135 if (!STRINGP (object))
26136 object = it->w->contents;
26137 #ifdef HAVE_WINDOW_SYSTEM
26138 if (FRAME_WINDOW_P (it->f))
26139 append_stretch_glyph (it, object, width, height, ascent);
26140 else
26141 #endif
26142 {
26143 it->object = object;
26144 it->char_to_display = ' ';
26145 it->pixel_width = it->len = 1;
26146 while (n--)
26147 tty_append_glyph (it);
26148 it->object = o_object;
26149 }
26150 }
26151
26152 it->pixel_width = width;
26153 #ifdef HAVE_WINDOW_SYSTEM
26154 if (FRAME_WINDOW_P (it->f))
26155 {
26156 it->ascent = it->phys_ascent = ascent;
26157 it->descent = it->phys_descent = height - it->ascent;
26158 it->nglyphs = width > 0 && height > 0;
26159 take_vertical_position_into_account (it);
26160 }
26161 else
26162 #endif
26163 it->nglyphs = width;
26164 }
26165
26166 /* Get information about special display element WHAT in an
26167 environment described by IT. WHAT is one of IT_TRUNCATION or
26168 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26169 non-null glyph_row member. This function ensures that fields like
26170 face_id, c, len of IT are left untouched. */
26171
26172 static void
26173 produce_special_glyphs (struct it *it, enum display_element_type what)
26174 {
26175 struct it temp_it;
26176 Lisp_Object gc;
26177 GLYPH glyph;
26178
26179 temp_it = *it;
26180 temp_it.object = Qnil;
26181 memset (&temp_it.current, 0, sizeof temp_it.current);
26182
26183 if (what == IT_CONTINUATION)
26184 {
26185 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26186 if (it->bidi_it.paragraph_dir == R2L)
26187 SET_GLYPH_FROM_CHAR (glyph, '/');
26188 else
26189 SET_GLYPH_FROM_CHAR (glyph, '\\');
26190 if (it->dp
26191 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26192 {
26193 /* FIXME: Should we mirror GC for R2L lines? */
26194 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26195 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26196 }
26197 }
26198 else if (what == IT_TRUNCATION)
26199 {
26200 /* Truncation glyph. */
26201 SET_GLYPH_FROM_CHAR (glyph, '$');
26202 if (it->dp
26203 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26204 {
26205 /* FIXME: Should we mirror GC for R2L lines? */
26206 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26207 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26208 }
26209 }
26210 else
26211 emacs_abort ();
26212
26213 #ifdef HAVE_WINDOW_SYSTEM
26214 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26215 is turned off, we precede the truncation/continuation glyphs by a
26216 stretch glyph whose width is computed such that these special
26217 glyphs are aligned at the window margin, even when very different
26218 fonts are used in different glyph rows. */
26219 if (FRAME_WINDOW_P (temp_it.f)
26220 /* init_iterator calls this with it->glyph_row == NULL, and it
26221 wants only the pixel width of the truncation/continuation
26222 glyphs. */
26223 && temp_it.glyph_row
26224 /* insert_left_trunc_glyphs calls us at the beginning of the
26225 row, and it has its own calculation of the stretch glyph
26226 width. */
26227 && temp_it.glyph_row->used[TEXT_AREA] > 0
26228 && (temp_it.glyph_row->reversed_p
26229 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26230 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26231 {
26232 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26233
26234 if (stretch_width > 0)
26235 {
26236 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26237 struct font *font =
26238 face->font ? face->font : FRAME_FONT (temp_it.f);
26239 int stretch_ascent =
26240 (((temp_it.ascent + temp_it.descent)
26241 * FONT_BASE (font)) / FONT_HEIGHT (font));
26242
26243 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26244 temp_it.ascent + temp_it.descent,
26245 stretch_ascent);
26246 }
26247 }
26248 #endif
26249
26250 temp_it.dp = NULL;
26251 temp_it.what = IT_CHARACTER;
26252 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26253 temp_it.face_id = GLYPH_FACE (glyph);
26254 temp_it.len = CHAR_BYTES (temp_it.c);
26255
26256 PRODUCE_GLYPHS (&temp_it);
26257 it->pixel_width = temp_it.pixel_width;
26258 it->nglyphs = temp_it.nglyphs;
26259 }
26260
26261 #ifdef HAVE_WINDOW_SYSTEM
26262
26263 /* Calculate line-height and line-spacing properties.
26264 An integer value specifies explicit pixel value.
26265 A float value specifies relative value to current face height.
26266 A cons (float . face-name) specifies relative value to
26267 height of specified face font.
26268
26269 Returns height in pixels, or nil. */
26270
26271 static Lisp_Object
26272 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26273 int boff, bool override)
26274 {
26275 Lisp_Object face_name = Qnil;
26276 int ascent, descent, height;
26277
26278 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26279 return val;
26280
26281 if (CONSP (val))
26282 {
26283 face_name = XCAR (val);
26284 val = XCDR (val);
26285 if (!NUMBERP (val))
26286 val = make_number (1);
26287 if (NILP (face_name))
26288 {
26289 height = it->ascent + it->descent;
26290 goto scale;
26291 }
26292 }
26293
26294 if (NILP (face_name))
26295 {
26296 font = FRAME_FONT (it->f);
26297 boff = FRAME_BASELINE_OFFSET (it->f);
26298 }
26299 else if (EQ (face_name, Qt))
26300 {
26301 override = false;
26302 }
26303 else
26304 {
26305 int face_id;
26306 struct face *face;
26307
26308 face_id = lookup_named_face (it->f, face_name, false);
26309 if (face_id < 0)
26310 return make_number (-1);
26311
26312 face = FACE_FROM_ID (it->f, face_id);
26313 font = face->font;
26314 if (font == NULL)
26315 return make_number (-1);
26316 boff = font->baseline_offset;
26317 if (font->vertical_centering)
26318 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26319 }
26320
26321 normal_char_ascent_descent (font, -1, &ascent, &descent);
26322
26323 if (override)
26324 {
26325 it->override_ascent = ascent;
26326 it->override_descent = descent;
26327 it->override_boff = boff;
26328 }
26329
26330 height = ascent + descent;
26331
26332 scale:
26333 if (FLOATP (val))
26334 height = (int)(XFLOAT_DATA (val) * height);
26335 else if (INTEGERP (val))
26336 height *= XINT (val);
26337
26338 return make_number (height);
26339 }
26340
26341
26342 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26343 is a face ID to be used for the glyph. FOR_NO_FONT is true if
26344 and only if this is for a character for which no font was found.
26345
26346 If the display method (it->glyphless_method) is
26347 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26348 length of the acronym or the hexadecimal string, UPPER_XOFF and
26349 UPPER_YOFF are pixel offsets for the upper part of the string,
26350 LOWER_XOFF and LOWER_YOFF are for the lower part.
26351
26352 For the other display methods, LEN through LOWER_YOFF are zero. */
26353
26354 static void
26355 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
26356 short upper_xoff, short upper_yoff,
26357 short lower_xoff, short lower_yoff)
26358 {
26359 struct glyph *glyph;
26360 enum glyph_row_area area = it->area;
26361
26362 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26363 if (glyph < it->glyph_row->glyphs[area + 1])
26364 {
26365 /* If the glyph row is reversed, we need to prepend the glyph
26366 rather than append it. */
26367 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26368 {
26369 struct glyph *g;
26370
26371 /* Make room for the additional glyph. */
26372 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26373 g[1] = *g;
26374 glyph = it->glyph_row->glyphs[area];
26375 }
26376 glyph->charpos = CHARPOS (it->position);
26377 glyph->object = it->object;
26378 glyph->pixel_width = it->pixel_width;
26379 glyph->ascent = it->ascent;
26380 glyph->descent = it->descent;
26381 glyph->voffset = it->voffset;
26382 glyph->type = GLYPHLESS_GLYPH;
26383 glyph->u.glyphless.method = it->glyphless_method;
26384 glyph->u.glyphless.for_no_font = for_no_font;
26385 glyph->u.glyphless.len = len;
26386 glyph->u.glyphless.ch = it->c;
26387 glyph->slice.glyphless.upper_xoff = upper_xoff;
26388 glyph->slice.glyphless.upper_yoff = upper_yoff;
26389 glyph->slice.glyphless.lower_xoff = lower_xoff;
26390 glyph->slice.glyphless.lower_yoff = lower_yoff;
26391 glyph->avoid_cursor_p = it->avoid_cursor_p;
26392 glyph->multibyte_p = it->multibyte_p;
26393 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26394 {
26395 /* In R2L rows, the left and the right box edges need to be
26396 drawn in reverse direction. */
26397 glyph->right_box_line_p = it->start_of_box_run_p;
26398 glyph->left_box_line_p = it->end_of_box_run_p;
26399 }
26400 else
26401 {
26402 glyph->left_box_line_p = it->start_of_box_run_p;
26403 glyph->right_box_line_p = it->end_of_box_run_p;
26404 }
26405 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26406 || it->phys_descent > it->descent);
26407 glyph->padding_p = false;
26408 glyph->glyph_not_available_p = false;
26409 glyph->face_id = face_id;
26410 glyph->font_type = FONT_TYPE_UNKNOWN;
26411 if (it->bidi_p)
26412 {
26413 glyph->resolved_level = it->bidi_it.resolved_level;
26414 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26415 glyph->bidi_type = it->bidi_it.type;
26416 }
26417 ++it->glyph_row->used[area];
26418 }
26419 else
26420 IT_EXPAND_MATRIX_WIDTH (it, area);
26421 }
26422
26423
26424 /* Produce a glyph for a glyphless character for iterator IT.
26425 IT->glyphless_method specifies which method to use for displaying
26426 the character. See the description of enum
26427 glyphless_display_method in dispextern.h for the detail.
26428
26429 FOR_NO_FONT is true if and only if this is for a character for
26430 which no font was found. ACRONYM, if non-nil, is an acronym string
26431 for the character. */
26432
26433 static void
26434 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
26435 {
26436 int face_id;
26437 struct face *face;
26438 struct font *font;
26439 int base_width, base_height, width, height;
26440 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26441 int len;
26442
26443 /* Get the metrics of the base font. We always refer to the current
26444 ASCII face. */
26445 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26446 font = face->font ? face->font : FRAME_FONT (it->f);
26447 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
26448 it->ascent += font->baseline_offset;
26449 it->descent -= font->baseline_offset;
26450 base_height = it->ascent + it->descent;
26451 base_width = font->average_width;
26452
26453 face_id = merge_glyphless_glyph_face (it);
26454
26455 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26456 {
26457 it->pixel_width = THIN_SPACE_WIDTH;
26458 len = 0;
26459 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26460 }
26461 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26462 {
26463 width = CHAR_WIDTH (it->c);
26464 if (width == 0)
26465 width = 1;
26466 else if (width > 4)
26467 width = 4;
26468 it->pixel_width = base_width * width;
26469 len = 0;
26470 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26471 }
26472 else
26473 {
26474 char buf[7];
26475 const char *str;
26476 unsigned int code[6];
26477 int upper_len;
26478 int ascent, descent;
26479 struct font_metrics metrics_upper, metrics_lower;
26480
26481 face = FACE_FROM_ID (it->f, face_id);
26482 font = face->font ? face->font : FRAME_FONT (it->f);
26483 prepare_face_for_display (it->f, face);
26484
26485 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26486 {
26487 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26488 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26489 if (CONSP (acronym))
26490 acronym = XCAR (acronym);
26491 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26492 }
26493 else
26494 {
26495 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26496 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
26497 str = buf;
26498 }
26499 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26500 code[len] = font->driver->encode_char (font, str[len]);
26501 upper_len = (len + 1) / 2;
26502 font->driver->text_extents (font, code, upper_len,
26503 &metrics_upper);
26504 font->driver->text_extents (font, code + upper_len, len - upper_len,
26505 &metrics_lower);
26506
26507
26508
26509 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26510 width = max (metrics_upper.width, metrics_lower.width) + 4;
26511 upper_xoff = upper_yoff = 2; /* the typical case */
26512 if (base_width >= width)
26513 {
26514 /* Align the upper to the left, the lower to the right. */
26515 it->pixel_width = base_width;
26516 lower_xoff = base_width - 2 - metrics_lower.width;
26517 }
26518 else
26519 {
26520 /* Center the shorter one. */
26521 it->pixel_width = width;
26522 if (metrics_upper.width >= metrics_lower.width)
26523 lower_xoff = (width - metrics_lower.width) / 2;
26524 else
26525 {
26526 /* FIXME: This code doesn't look right. It formerly was
26527 missing the "lower_xoff = 0;", which couldn't have
26528 been right since it left lower_xoff uninitialized. */
26529 lower_xoff = 0;
26530 upper_xoff = (width - metrics_upper.width) / 2;
26531 }
26532 }
26533
26534 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26535 top, bottom, and between upper and lower strings. */
26536 height = (metrics_upper.ascent + metrics_upper.descent
26537 + metrics_lower.ascent + metrics_lower.descent) + 5;
26538 /* Center vertically.
26539 H:base_height, D:base_descent
26540 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26541
26542 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26543 descent = D - H/2 + h/2;
26544 lower_yoff = descent - 2 - ld;
26545 upper_yoff = lower_yoff - la - 1 - ud; */
26546 ascent = - (it->descent - (base_height + height + 1) / 2);
26547 descent = it->descent - (base_height - height) / 2;
26548 lower_yoff = descent - 2 - metrics_lower.descent;
26549 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26550 - metrics_upper.descent);
26551 /* Don't make the height shorter than the base height. */
26552 if (height > base_height)
26553 {
26554 it->ascent = ascent;
26555 it->descent = descent;
26556 }
26557 }
26558
26559 it->phys_ascent = it->ascent;
26560 it->phys_descent = it->descent;
26561 if (it->glyph_row)
26562 append_glyphless_glyph (it, face_id, for_no_font, len,
26563 upper_xoff, upper_yoff,
26564 lower_xoff, lower_yoff);
26565 it->nglyphs = 1;
26566 take_vertical_position_into_account (it);
26567 }
26568
26569
26570 /* RIF:
26571 Produce glyphs/get display metrics for the display element IT is
26572 loaded with. See the description of struct it in dispextern.h
26573 for an overview of struct it. */
26574
26575 void
26576 x_produce_glyphs (struct it *it)
26577 {
26578 int extra_line_spacing = it->extra_line_spacing;
26579
26580 it->glyph_not_available_p = false;
26581
26582 if (it->what == IT_CHARACTER)
26583 {
26584 XChar2b char2b;
26585 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26586 struct font *font = face->font;
26587 struct font_metrics *pcm = NULL;
26588 int boff; /* Baseline offset. */
26589
26590 if (font == NULL)
26591 {
26592 /* When no suitable font is found, display this character by
26593 the method specified in the first extra slot of
26594 Vglyphless_char_display. */
26595 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26596
26597 eassert (it->what == IT_GLYPHLESS);
26598 produce_glyphless_glyph (it, true,
26599 STRINGP (acronym) ? acronym : Qnil);
26600 goto done;
26601 }
26602
26603 boff = font->baseline_offset;
26604 if (font->vertical_centering)
26605 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26606
26607 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26608 {
26609 it->nglyphs = 1;
26610
26611 if (it->override_ascent >= 0)
26612 {
26613 it->ascent = it->override_ascent;
26614 it->descent = it->override_descent;
26615 boff = it->override_boff;
26616 }
26617 else
26618 {
26619 it->ascent = FONT_BASE (font) + boff;
26620 it->descent = FONT_DESCENT (font) - boff;
26621 }
26622
26623 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26624 {
26625 pcm = get_per_char_metric (font, &char2b);
26626 if (pcm->width == 0
26627 && pcm->rbearing == 0 && pcm->lbearing == 0)
26628 pcm = NULL;
26629 }
26630
26631 if (pcm)
26632 {
26633 it->phys_ascent = pcm->ascent + boff;
26634 it->phys_descent = pcm->descent - boff;
26635 it->pixel_width = pcm->width;
26636 /* Don't use font-global values for ascent and descent
26637 if they result in an exceedingly large line height. */
26638 if (it->override_ascent < 0)
26639 {
26640 if (FONT_TOO_HIGH (font))
26641 {
26642 it->ascent = it->phys_ascent;
26643 it->descent = it->phys_descent;
26644 /* These limitations are enforced by an
26645 assertion near the end of this function. */
26646 if (it->ascent < 0)
26647 it->ascent = 0;
26648 if (it->descent < 0)
26649 it->descent = 0;
26650 }
26651 }
26652 }
26653 else
26654 {
26655 it->glyph_not_available_p = true;
26656 it->phys_ascent = it->ascent;
26657 it->phys_descent = it->descent;
26658 it->pixel_width = font->space_width;
26659 }
26660
26661 if (it->constrain_row_ascent_descent_p)
26662 {
26663 if (it->descent > it->max_descent)
26664 {
26665 it->ascent += it->descent - it->max_descent;
26666 it->descent = it->max_descent;
26667 }
26668 if (it->ascent > it->max_ascent)
26669 {
26670 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26671 it->ascent = it->max_ascent;
26672 }
26673 it->phys_ascent = min (it->phys_ascent, it->ascent);
26674 it->phys_descent = min (it->phys_descent, it->descent);
26675 extra_line_spacing = 0;
26676 }
26677
26678 /* If this is a space inside a region of text with
26679 `space-width' property, change its width. */
26680 bool stretched_p
26681 = it->char_to_display == ' ' && !NILP (it->space_width);
26682 if (stretched_p)
26683 it->pixel_width *= XFLOATINT (it->space_width);
26684
26685 /* If face has a box, add the box thickness to the character
26686 height. If character has a box line to the left and/or
26687 right, add the box line width to the character's width. */
26688 if (face->box != FACE_NO_BOX)
26689 {
26690 int thick = face->box_line_width;
26691
26692 if (thick > 0)
26693 {
26694 it->ascent += thick;
26695 it->descent += thick;
26696 }
26697 else
26698 thick = -thick;
26699
26700 if (it->start_of_box_run_p)
26701 it->pixel_width += thick;
26702 if (it->end_of_box_run_p)
26703 it->pixel_width += thick;
26704 }
26705
26706 /* If face has an overline, add the height of the overline
26707 (1 pixel) and a 1 pixel margin to the character height. */
26708 if (face->overline_p)
26709 it->ascent += overline_margin;
26710
26711 if (it->constrain_row_ascent_descent_p)
26712 {
26713 if (it->ascent > it->max_ascent)
26714 it->ascent = it->max_ascent;
26715 if (it->descent > it->max_descent)
26716 it->descent = it->max_descent;
26717 }
26718
26719 take_vertical_position_into_account (it);
26720
26721 /* If we have to actually produce glyphs, do it. */
26722 if (it->glyph_row)
26723 {
26724 if (stretched_p)
26725 {
26726 /* Translate a space with a `space-width' property
26727 into a stretch glyph. */
26728 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26729 / FONT_HEIGHT (font));
26730 append_stretch_glyph (it, it->object, it->pixel_width,
26731 it->ascent + it->descent, ascent);
26732 }
26733 else
26734 append_glyph (it);
26735
26736 /* If characters with lbearing or rbearing are displayed
26737 in this line, record that fact in a flag of the
26738 glyph row. This is used to optimize X output code. */
26739 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26740 it->glyph_row->contains_overlapping_glyphs_p = true;
26741 }
26742 if (! stretched_p && it->pixel_width == 0)
26743 /* We assure that all visible glyphs have at least 1-pixel
26744 width. */
26745 it->pixel_width = 1;
26746 }
26747 else if (it->char_to_display == '\n')
26748 {
26749 /* A newline has no width, but we need the height of the
26750 line. But if previous part of the line sets a height,
26751 don't increase that height. */
26752
26753 Lisp_Object height;
26754 Lisp_Object total_height = Qnil;
26755
26756 it->override_ascent = -1;
26757 it->pixel_width = 0;
26758 it->nglyphs = 0;
26759
26760 height = get_it_property (it, Qline_height);
26761 /* Split (line-height total-height) list. */
26762 if (CONSP (height)
26763 && CONSP (XCDR (height))
26764 && NILP (XCDR (XCDR (height))))
26765 {
26766 total_height = XCAR (XCDR (height));
26767 height = XCAR (height);
26768 }
26769 height = calc_line_height_property (it, height, font, boff, true);
26770
26771 if (it->override_ascent >= 0)
26772 {
26773 it->ascent = it->override_ascent;
26774 it->descent = it->override_descent;
26775 boff = it->override_boff;
26776 }
26777 else
26778 {
26779 if (FONT_TOO_HIGH (font))
26780 {
26781 it->ascent = font->pixel_size + boff - 1;
26782 it->descent = -boff + 1;
26783 if (it->descent < 0)
26784 it->descent = 0;
26785 }
26786 else
26787 {
26788 it->ascent = FONT_BASE (font) + boff;
26789 it->descent = FONT_DESCENT (font) - boff;
26790 }
26791 }
26792
26793 if (EQ (height, Qt))
26794 {
26795 if (it->descent > it->max_descent)
26796 {
26797 it->ascent += it->descent - it->max_descent;
26798 it->descent = it->max_descent;
26799 }
26800 if (it->ascent > it->max_ascent)
26801 {
26802 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26803 it->ascent = it->max_ascent;
26804 }
26805 it->phys_ascent = min (it->phys_ascent, it->ascent);
26806 it->phys_descent = min (it->phys_descent, it->descent);
26807 it->constrain_row_ascent_descent_p = true;
26808 extra_line_spacing = 0;
26809 }
26810 else
26811 {
26812 Lisp_Object spacing;
26813
26814 it->phys_ascent = it->ascent;
26815 it->phys_descent = it->descent;
26816
26817 if ((it->max_ascent > 0 || it->max_descent > 0)
26818 && face->box != FACE_NO_BOX
26819 && face->box_line_width > 0)
26820 {
26821 it->ascent += face->box_line_width;
26822 it->descent += face->box_line_width;
26823 }
26824 if (!NILP (height)
26825 && XINT (height) > it->ascent + it->descent)
26826 it->ascent = XINT (height) - it->descent;
26827
26828 if (!NILP (total_height))
26829 spacing = calc_line_height_property (it, total_height, font,
26830 boff, false);
26831 else
26832 {
26833 spacing = get_it_property (it, Qline_spacing);
26834 spacing = calc_line_height_property (it, spacing, font,
26835 boff, false);
26836 }
26837 if (INTEGERP (spacing))
26838 {
26839 extra_line_spacing = XINT (spacing);
26840 if (!NILP (total_height))
26841 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26842 }
26843 }
26844 }
26845 else /* i.e. (it->char_to_display == '\t') */
26846 {
26847 if (font->space_width > 0)
26848 {
26849 int tab_width = it->tab_width * font->space_width;
26850 int x = it->current_x + it->continuation_lines_width;
26851 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26852
26853 /* If the distance from the current position to the next tab
26854 stop is less than a space character width, use the
26855 tab stop after that. */
26856 if (next_tab_x - x < font->space_width)
26857 next_tab_x += tab_width;
26858
26859 it->pixel_width = next_tab_x - x;
26860 it->nglyphs = 1;
26861 if (FONT_TOO_HIGH (font))
26862 {
26863 if (get_char_glyph_code (' ', font, &char2b))
26864 {
26865 pcm = get_per_char_metric (font, &char2b);
26866 if (pcm->width == 0
26867 && pcm->rbearing == 0 && pcm->lbearing == 0)
26868 pcm = NULL;
26869 }
26870
26871 if (pcm)
26872 {
26873 it->ascent = pcm->ascent + boff;
26874 it->descent = pcm->descent - boff;
26875 }
26876 else
26877 {
26878 it->ascent = font->pixel_size + boff - 1;
26879 it->descent = -boff + 1;
26880 }
26881 if (it->ascent < 0)
26882 it->ascent = 0;
26883 if (it->descent < 0)
26884 it->descent = 0;
26885 }
26886 else
26887 {
26888 it->ascent = FONT_BASE (font) + boff;
26889 it->descent = FONT_DESCENT (font) - boff;
26890 }
26891 it->phys_ascent = it->ascent;
26892 it->phys_descent = it->descent;
26893
26894 if (it->glyph_row)
26895 {
26896 append_stretch_glyph (it, it->object, it->pixel_width,
26897 it->ascent + it->descent, it->ascent);
26898 }
26899 }
26900 else
26901 {
26902 it->pixel_width = 0;
26903 it->nglyphs = 1;
26904 }
26905 }
26906
26907 if (FONT_TOO_HIGH (font))
26908 {
26909 int font_ascent, font_descent;
26910
26911 /* For very large fonts, where we ignore the declared font
26912 dimensions, and go by per-character metrics instead,
26913 don't let the row ascent and descent values (and the row
26914 height computed from them) be smaller than the "normal"
26915 character metrics. This avoids unpleasant effects
26916 whereby lines on display would change their height
26917 depending on which characters are shown. */
26918 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26919 it->max_ascent = max (it->max_ascent, font_ascent);
26920 it->max_descent = max (it->max_descent, font_descent);
26921 }
26922 }
26923 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26924 {
26925 /* A static composition.
26926
26927 Note: A composition is represented as one glyph in the
26928 glyph matrix. There are no padding glyphs.
26929
26930 Important note: pixel_width, ascent, and descent are the
26931 values of what is drawn by draw_glyphs (i.e. the values of
26932 the overall glyphs composed). */
26933 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26934 int boff; /* baseline offset */
26935 struct composition *cmp = composition_table[it->cmp_it.id];
26936 int glyph_len = cmp->glyph_len;
26937 struct font *font = face->font;
26938
26939 it->nglyphs = 1;
26940
26941 /* If we have not yet calculated pixel size data of glyphs of
26942 the composition for the current face font, calculate them
26943 now. Theoretically, we have to check all fonts for the
26944 glyphs, but that requires much time and memory space. So,
26945 here we check only the font of the first glyph. This may
26946 lead to incorrect display, but it's very rare, and C-l
26947 (recenter-top-bottom) can correct the display anyway. */
26948 if (! cmp->font || cmp->font != font)
26949 {
26950 /* Ascent and descent of the font of the first character
26951 of this composition (adjusted by baseline offset).
26952 Ascent and descent of overall glyphs should not be less
26953 than these, respectively. */
26954 int font_ascent, font_descent, font_height;
26955 /* Bounding box of the overall glyphs. */
26956 int leftmost, rightmost, lowest, highest;
26957 int lbearing, rbearing;
26958 int i, width, ascent, descent;
26959 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26960 XChar2b char2b;
26961 struct font_metrics *pcm;
26962 ptrdiff_t pos;
26963
26964 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26965 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26966 break;
26967 bool right_padded = glyph_len < cmp->glyph_len;
26968 for (i = 0; i < glyph_len; i++)
26969 {
26970 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26971 break;
26972 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26973 }
26974 bool left_padded = i > 0;
26975
26976 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26977 : IT_CHARPOS (*it));
26978 /* If no suitable font is found, use the default font. */
26979 bool font_not_found_p = font == NULL;
26980 if (font_not_found_p)
26981 {
26982 face = face->ascii_face;
26983 font = face->font;
26984 }
26985 boff = font->baseline_offset;
26986 if (font->vertical_centering)
26987 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26988 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
26989 font_ascent += boff;
26990 font_descent -= boff;
26991 font_height = font_ascent + font_descent;
26992
26993 cmp->font = font;
26994
26995 pcm = NULL;
26996 if (! font_not_found_p)
26997 {
26998 get_char_face_and_encoding (it->f, c, it->face_id,
26999 &char2b, false);
27000 pcm = get_per_char_metric (font, &char2b);
27001 }
27002
27003 /* Initialize the bounding box. */
27004 if (pcm)
27005 {
27006 width = cmp->glyph_len > 0 ? pcm->width : 0;
27007 ascent = pcm->ascent;
27008 descent = pcm->descent;
27009 lbearing = pcm->lbearing;
27010 rbearing = pcm->rbearing;
27011 }
27012 else
27013 {
27014 width = cmp->glyph_len > 0 ? font->space_width : 0;
27015 ascent = FONT_BASE (font);
27016 descent = FONT_DESCENT (font);
27017 lbearing = 0;
27018 rbearing = width;
27019 }
27020
27021 rightmost = width;
27022 leftmost = 0;
27023 lowest = - descent + boff;
27024 highest = ascent + boff;
27025
27026 if (! font_not_found_p
27027 && font->default_ascent
27028 && CHAR_TABLE_P (Vuse_default_ascent)
27029 && !NILP (Faref (Vuse_default_ascent,
27030 make_number (it->char_to_display))))
27031 highest = font->default_ascent + boff;
27032
27033 /* Draw the first glyph at the normal position. It may be
27034 shifted to right later if some other glyphs are drawn
27035 at the left. */
27036 cmp->offsets[i * 2] = 0;
27037 cmp->offsets[i * 2 + 1] = boff;
27038 cmp->lbearing = lbearing;
27039 cmp->rbearing = rbearing;
27040
27041 /* Set cmp->offsets for the remaining glyphs. */
27042 for (i++; i < glyph_len; i++)
27043 {
27044 int left, right, btm, top;
27045 int ch = COMPOSITION_GLYPH (cmp, i);
27046 int face_id;
27047 struct face *this_face;
27048
27049 if (ch == '\t')
27050 ch = ' ';
27051 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27052 this_face = FACE_FROM_ID (it->f, face_id);
27053 font = this_face->font;
27054
27055 if (font == NULL)
27056 pcm = NULL;
27057 else
27058 {
27059 get_char_face_and_encoding (it->f, ch, face_id,
27060 &char2b, false);
27061 pcm = get_per_char_metric (font, &char2b);
27062 }
27063 if (! pcm)
27064 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27065 else
27066 {
27067 width = pcm->width;
27068 ascent = pcm->ascent;
27069 descent = pcm->descent;
27070 lbearing = pcm->lbearing;
27071 rbearing = pcm->rbearing;
27072 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27073 {
27074 /* Relative composition with or without
27075 alternate chars. */
27076 left = (leftmost + rightmost - width) / 2;
27077 btm = - descent + boff;
27078 if (font->relative_compose
27079 && (! CHAR_TABLE_P (Vignore_relative_composition)
27080 || NILP (Faref (Vignore_relative_composition,
27081 make_number (ch)))))
27082 {
27083
27084 if (- descent >= font->relative_compose)
27085 /* One extra pixel between two glyphs. */
27086 btm = highest + 1;
27087 else if (ascent <= 0)
27088 /* One extra pixel between two glyphs. */
27089 btm = lowest - 1 - ascent - descent;
27090 }
27091 }
27092 else
27093 {
27094 /* A composition rule is specified by an integer
27095 value that encodes global and new reference
27096 points (GREF and NREF). GREF and NREF are
27097 specified by numbers as below:
27098
27099 0---1---2 -- ascent
27100 | |
27101 | |
27102 | |
27103 9--10--11 -- center
27104 | |
27105 ---3---4---5--- baseline
27106 | |
27107 6---7---8 -- descent
27108 */
27109 int rule = COMPOSITION_RULE (cmp, i);
27110 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27111
27112 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27113 grefx = gref % 3, nrefx = nref % 3;
27114 grefy = gref / 3, nrefy = nref / 3;
27115 if (xoff)
27116 xoff = font_height * (xoff - 128) / 256;
27117 if (yoff)
27118 yoff = font_height * (yoff - 128) / 256;
27119
27120 left = (leftmost
27121 + grefx * (rightmost - leftmost) / 2
27122 - nrefx * width / 2
27123 + xoff);
27124
27125 btm = ((grefy == 0 ? highest
27126 : grefy == 1 ? 0
27127 : grefy == 2 ? lowest
27128 : (highest + lowest) / 2)
27129 - (nrefy == 0 ? ascent + descent
27130 : nrefy == 1 ? descent - boff
27131 : nrefy == 2 ? 0
27132 : (ascent + descent) / 2)
27133 + yoff);
27134 }
27135
27136 cmp->offsets[i * 2] = left;
27137 cmp->offsets[i * 2 + 1] = btm + descent;
27138
27139 /* Update the bounding box of the overall glyphs. */
27140 if (width > 0)
27141 {
27142 right = left + width;
27143 if (left < leftmost)
27144 leftmost = left;
27145 if (right > rightmost)
27146 rightmost = right;
27147 }
27148 top = btm + descent + ascent;
27149 if (top > highest)
27150 highest = top;
27151 if (btm < lowest)
27152 lowest = btm;
27153
27154 if (cmp->lbearing > left + lbearing)
27155 cmp->lbearing = left + lbearing;
27156 if (cmp->rbearing < left + rbearing)
27157 cmp->rbearing = left + rbearing;
27158 }
27159 }
27160
27161 /* If there are glyphs whose x-offsets are negative,
27162 shift all glyphs to the right and make all x-offsets
27163 non-negative. */
27164 if (leftmost < 0)
27165 {
27166 for (i = 0; i < cmp->glyph_len; i++)
27167 cmp->offsets[i * 2] -= leftmost;
27168 rightmost -= leftmost;
27169 cmp->lbearing -= leftmost;
27170 cmp->rbearing -= leftmost;
27171 }
27172
27173 if (left_padded && cmp->lbearing < 0)
27174 {
27175 for (i = 0; i < cmp->glyph_len; i++)
27176 cmp->offsets[i * 2] -= cmp->lbearing;
27177 rightmost -= cmp->lbearing;
27178 cmp->rbearing -= cmp->lbearing;
27179 cmp->lbearing = 0;
27180 }
27181 if (right_padded && rightmost < cmp->rbearing)
27182 {
27183 rightmost = cmp->rbearing;
27184 }
27185
27186 cmp->pixel_width = rightmost;
27187 cmp->ascent = highest;
27188 cmp->descent = - lowest;
27189 if (cmp->ascent < font_ascent)
27190 cmp->ascent = font_ascent;
27191 if (cmp->descent < font_descent)
27192 cmp->descent = font_descent;
27193 }
27194
27195 if (it->glyph_row
27196 && (cmp->lbearing < 0
27197 || cmp->rbearing > cmp->pixel_width))
27198 it->glyph_row->contains_overlapping_glyphs_p = true;
27199
27200 it->pixel_width = cmp->pixel_width;
27201 it->ascent = it->phys_ascent = cmp->ascent;
27202 it->descent = it->phys_descent = cmp->descent;
27203 if (face->box != FACE_NO_BOX)
27204 {
27205 int thick = face->box_line_width;
27206
27207 if (thick > 0)
27208 {
27209 it->ascent += thick;
27210 it->descent += thick;
27211 }
27212 else
27213 thick = - thick;
27214
27215 if (it->start_of_box_run_p)
27216 it->pixel_width += thick;
27217 if (it->end_of_box_run_p)
27218 it->pixel_width += thick;
27219 }
27220
27221 /* If face has an overline, add the height of the overline
27222 (1 pixel) and a 1 pixel margin to the character height. */
27223 if (face->overline_p)
27224 it->ascent += overline_margin;
27225
27226 take_vertical_position_into_account (it);
27227 if (it->ascent < 0)
27228 it->ascent = 0;
27229 if (it->descent < 0)
27230 it->descent = 0;
27231
27232 if (it->glyph_row && cmp->glyph_len > 0)
27233 append_composite_glyph (it);
27234 }
27235 else if (it->what == IT_COMPOSITION)
27236 {
27237 /* A dynamic (automatic) composition. */
27238 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27239 Lisp_Object gstring;
27240 struct font_metrics metrics;
27241
27242 it->nglyphs = 1;
27243
27244 gstring = composition_gstring_from_id (it->cmp_it.id);
27245 it->pixel_width
27246 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27247 &metrics);
27248 if (it->glyph_row
27249 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27250 it->glyph_row->contains_overlapping_glyphs_p = true;
27251 it->ascent = it->phys_ascent = metrics.ascent;
27252 it->descent = it->phys_descent = metrics.descent;
27253 if (face->box != FACE_NO_BOX)
27254 {
27255 int thick = face->box_line_width;
27256
27257 if (thick > 0)
27258 {
27259 it->ascent += thick;
27260 it->descent += thick;
27261 }
27262 else
27263 thick = - thick;
27264
27265 if (it->start_of_box_run_p)
27266 it->pixel_width += thick;
27267 if (it->end_of_box_run_p)
27268 it->pixel_width += thick;
27269 }
27270 /* If face has an overline, add the height of the overline
27271 (1 pixel) and a 1 pixel margin to the character height. */
27272 if (face->overline_p)
27273 it->ascent += overline_margin;
27274 take_vertical_position_into_account (it);
27275 if (it->ascent < 0)
27276 it->ascent = 0;
27277 if (it->descent < 0)
27278 it->descent = 0;
27279
27280 if (it->glyph_row)
27281 append_composite_glyph (it);
27282 }
27283 else if (it->what == IT_GLYPHLESS)
27284 produce_glyphless_glyph (it, false, Qnil);
27285 else if (it->what == IT_IMAGE)
27286 produce_image_glyph (it);
27287 else if (it->what == IT_STRETCH)
27288 produce_stretch_glyph (it);
27289
27290 done:
27291 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27292 because this isn't true for images with `:ascent 100'. */
27293 eassert (it->ascent >= 0 && it->descent >= 0);
27294 if (it->area == TEXT_AREA)
27295 it->current_x += it->pixel_width;
27296
27297 if (extra_line_spacing > 0)
27298 {
27299 it->descent += extra_line_spacing;
27300 if (extra_line_spacing > it->max_extra_line_spacing)
27301 it->max_extra_line_spacing = extra_line_spacing;
27302 }
27303
27304 it->max_ascent = max (it->max_ascent, it->ascent);
27305 it->max_descent = max (it->max_descent, it->descent);
27306 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27307 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27308 }
27309
27310 /* EXPORT for RIF:
27311 Output LEN glyphs starting at START at the nominal cursor position.
27312 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27313 being updated, and UPDATED_AREA is the area of that row being updated. */
27314
27315 void
27316 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27317 struct glyph *start, enum glyph_row_area updated_area, int len)
27318 {
27319 int x, hpos, chpos = w->phys_cursor.hpos;
27320
27321 eassert (updated_row);
27322 /* When the window is hscrolled, cursor hpos can legitimately be out
27323 of bounds, but we draw the cursor at the corresponding window
27324 margin in that case. */
27325 if (!updated_row->reversed_p && chpos < 0)
27326 chpos = 0;
27327 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27328 chpos = updated_row->used[TEXT_AREA] - 1;
27329
27330 block_input ();
27331
27332 /* Write glyphs. */
27333
27334 hpos = start - updated_row->glyphs[updated_area];
27335 x = draw_glyphs (w, w->output_cursor.x,
27336 updated_row, updated_area,
27337 hpos, hpos + len,
27338 DRAW_NORMAL_TEXT, 0);
27339
27340 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27341 if (updated_area == TEXT_AREA
27342 && w->phys_cursor_on_p
27343 && w->phys_cursor.vpos == w->output_cursor.vpos
27344 && chpos >= hpos
27345 && chpos < hpos + len)
27346 w->phys_cursor_on_p = false;
27347
27348 unblock_input ();
27349
27350 /* Advance the output cursor. */
27351 w->output_cursor.hpos += len;
27352 w->output_cursor.x = x;
27353 }
27354
27355
27356 /* EXPORT for RIF:
27357 Insert LEN glyphs from START at the nominal cursor position. */
27358
27359 void
27360 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27361 struct glyph *start, enum glyph_row_area updated_area, int len)
27362 {
27363 struct frame *f;
27364 int line_height, shift_by_width, shifted_region_width;
27365 struct glyph_row *row;
27366 struct glyph *glyph;
27367 int frame_x, frame_y;
27368 ptrdiff_t hpos;
27369
27370 eassert (updated_row);
27371 block_input ();
27372 f = XFRAME (WINDOW_FRAME (w));
27373
27374 /* Get the height of the line we are in. */
27375 row = updated_row;
27376 line_height = row->height;
27377
27378 /* Get the width of the glyphs to insert. */
27379 shift_by_width = 0;
27380 for (glyph = start; glyph < start + len; ++glyph)
27381 shift_by_width += glyph->pixel_width;
27382
27383 /* Get the width of the region to shift right. */
27384 shifted_region_width = (window_box_width (w, updated_area)
27385 - w->output_cursor.x
27386 - shift_by_width);
27387
27388 /* Shift right. */
27389 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27390 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27391
27392 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27393 line_height, shift_by_width);
27394
27395 /* Write the glyphs. */
27396 hpos = start - row->glyphs[updated_area];
27397 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27398 hpos, hpos + len,
27399 DRAW_NORMAL_TEXT, 0);
27400
27401 /* Advance the output cursor. */
27402 w->output_cursor.hpos += len;
27403 w->output_cursor.x += shift_by_width;
27404 unblock_input ();
27405 }
27406
27407
27408 /* EXPORT for RIF:
27409 Erase the current text line from the nominal cursor position
27410 (inclusive) to pixel column TO_X (exclusive). The idea is that
27411 everything from TO_X onward is already erased.
27412
27413 TO_X is a pixel position relative to UPDATED_AREA of currently
27414 updated window W. TO_X == -1 means clear to the end of this area. */
27415
27416 void
27417 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27418 enum glyph_row_area updated_area, int to_x)
27419 {
27420 struct frame *f;
27421 int max_x, min_y, max_y;
27422 int from_x, from_y, to_y;
27423
27424 eassert (updated_row);
27425 f = XFRAME (w->frame);
27426
27427 if (updated_row->full_width_p)
27428 max_x = (WINDOW_PIXEL_WIDTH (w)
27429 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27430 else
27431 max_x = window_box_width (w, updated_area);
27432 max_y = window_text_bottom_y (w);
27433
27434 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27435 of window. For TO_X > 0, truncate to end of drawing area. */
27436 if (to_x == 0)
27437 return;
27438 else if (to_x < 0)
27439 to_x = max_x;
27440 else
27441 to_x = min (to_x, max_x);
27442
27443 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27444
27445 /* Notice if the cursor will be cleared by this operation. */
27446 if (!updated_row->full_width_p)
27447 notice_overwritten_cursor (w, updated_area,
27448 w->output_cursor.x, -1,
27449 updated_row->y,
27450 MATRIX_ROW_BOTTOM_Y (updated_row));
27451
27452 from_x = w->output_cursor.x;
27453
27454 /* Translate to frame coordinates. */
27455 if (updated_row->full_width_p)
27456 {
27457 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27458 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27459 }
27460 else
27461 {
27462 int area_left = window_box_left (w, updated_area);
27463 from_x += area_left;
27464 to_x += area_left;
27465 }
27466
27467 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27468 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27469 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27470
27471 /* Prevent inadvertently clearing to end of the X window. */
27472 if (to_x > from_x && to_y > from_y)
27473 {
27474 block_input ();
27475 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27476 to_x - from_x, to_y - from_y);
27477 unblock_input ();
27478 }
27479 }
27480
27481 #endif /* HAVE_WINDOW_SYSTEM */
27482
27483
27484 \f
27485 /***********************************************************************
27486 Cursor types
27487 ***********************************************************************/
27488
27489 /* Value is the internal representation of the specified cursor type
27490 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27491 of the bar cursor. */
27492
27493 static enum text_cursor_kinds
27494 get_specified_cursor_type (Lisp_Object arg, int *width)
27495 {
27496 enum text_cursor_kinds type;
27497
27498 if (NILP (arg))
27499 return NO_CURSOR;
27500
27501 if (EQ (arg, Qbox))
27502 return FILLED_BOX_CURSOR;
27503
27504 if (EQ (arg, Qhollow))
27505 return HOLLOW_BOX_CURSOR;
27506
27507 if (EQ (arg, Qbar))
27508 {
27509 *width = 2;
27510 return BAR_CURSOR;
27511 }
27512
27513 if (CONSP (arg)
27514 && EQ (XCAR (arg), Qbar)
27515 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27516 {
27517 *width = XINT (XCDR (arg));
27518 return BAR_CURSOR;
27519 }
27520
27521 if (EQ (arg, Qhbar))
27522 {
27523 *width = 2;
27524 return HBAR_CURSOR;
27525 }
27526
27527 if (CONSP (arg)
27528 && EQ (XCAR (arg), Qhbar)
27529 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27530 {
27531 *width = XINT (XCDR (arg));
27532 return HBAR_CURSOR;
27533 }
27534
27535 /* Treat anything unknown as "hollow box cursor".
27536 It was bad to signal an error; people have trouble fixing
27537 .Xdefaults with Emacs, when it has something bad in it. */
27538 type = HOLLOW_BOX_CURSOR;
27539
27540 return type;
27541 }
27542
27543 /* Set the default cursor types for specified frame. */
27544 void
27545 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27546 {
27547 int width = 1;
27548 Lisp_Object tem;
27549
27550 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27551 FRAME_CURSOR_WIDTH (f) = width;
27552
27553 /* By default, set up the blink-off state depending on the on-state. */
27554
27555 tem = Fassoc (arg, Vblink_cursor_alist);
27556 if (!NILP (tem))
27557 {
27558 FRAME_BLINK_OFF_CURSOR (f)
27559 = get_specified_cursor_type (XCDR (tem), &width);
27560 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27561 }
27562 else
27563 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27564
27565 /* Make sure the cursor gets redrawn. */
27566 f->cursor_type_changed = true;
27567 }
27568
27569
27570 #ifdef HAVE_WINDOW_SYSTEM
27571
27572 /* Return the cursor we want to be displayed in window W. Return
27573 width of bar/hbar cursor through WIDTH arg. Return with
27574 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
27575 (i.e. if the `system caret' should track this cursor).
27576
27577 In a mini-buffer window, we want the cursor only to appear if we
27578 are reading input from this window. For the selected window, we
27579 want the cursor type given by the frame parameter or buffer local
27580 setting of cursor-type. If explicitly marked off, draw no cursor.
27581 In all other cases, we want a hollow box cursor. */
27582
27583 static enum text_cursor_kinds
27584 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27585 bool *active_cursor)
27586 {
27587 struct frame *f = XFRAME (w->frame);
27588 struct buffer *b = XBUFFER (w->contents);
27589 int cursor_type = DEFAULT_CURSOR;
27590 Lisp_Object alt_cursor;
27591 bool non_selected = false;
27592
27593 *active_cursor = true;
27594
27595 /* Echo area */
27596 if (cursor_in_echo_area
27597 && FRAME_HAS_MINIBUF_P (f)
27598 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27599 {
27600 if (w == XWINDOW (echo_area_window))
27601 {
27602 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27603 {
27604 *width = FRAME_CURSOR_WIDTH (f);
27605 return FRAME_DESIRED_CURSOR (f);
27606 }
27607 else
27608 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27609 }
27610
27611 *active_cursor = false;
27612 non_selected = true;
27613 }
27614
27615 /* Detect a nonselected window or nonselected frame. */
27616 else if (w != XWINDOW (f->selected_window)
27617 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27618 {
27619 *active_cursor = false;
27620
27621 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27622 return NO_CURSOR;
27623
27624 non_selected = true;
27625 }
27626
27627 /* Never display a cursor in a window in which cursor-type is nil. */
27628 if (NILP (BVAR (b, cursor_type)))
27629 return NO_CURSOR;
27630
27631 /* Get the normal cursor type for this window. */
27632 if (EQ (BVAR (b, cursor_type), Qt))
27633 {
27634 cursor_type = FRAME_DESIRED_CURSOR (f);
27635 *width = FRAME_CURSOR_WIDTH (f);
27636 }
27637 else
27638 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27639
27640 /* Use cursor-in-non-selected-windows instead
27641 for non-selected window or frame. */
27642 if (non_selected)
27643 {
27644 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27645 if (!EQ (Qt, alt_cursor))
27646 return get_specified_cursor_type (alt_cursor, width);
27647 /* t means modify the normal cursor type. */
27648 if (cursor_type == FILLED_BOX_CURSOR)
27649 cursor_type = HOLLOW_BOX_CURSOR;
27650 else if (cursor_type == BAR_CURSOR && *width > 1)
27651 --*width;
27652 return cursor_type;
27653 }
27654
27655 /* Use normal cursor if not blinked off. */
27656 if (!w->cursor_off_p)
27657 {
27658 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27659 {
27660 if (cursor_type == FILLED_BOX_CURSOR)
27661 {
27662 /* Using a block cursor on large images can be very annoying.
27663 So use a hollow cursor for "large" images.
27664 If image is not transparent (no mask), also use hollow cursor. */
27665 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27666 if (img != NULL && IMAGEP (img->spec))
27667 {
27668 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27669 where N = size of default frame font size.
27670 This should cover most of the "tiny" icons people may use. */
27671 if (!img->mask
27672 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27673 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27674 cursor_type = HOLLOW_BOX_CURSOR;
27675 }
27676 }
27677 else if (cursor_type != NO_CURSOR)
27678 {
27679 /* Display current only supports BOX and HOLLOW cursors for images.
27680 So for now, unconditionally use a HOLLOW cursor when cursor is
27681 not a solid box cursor. */
27682 cursor_type = HOLLOW_BOX_CURSOR;
27683 }
27684 }
27685 return cursor_type;
27686 }
27687
27688 /* Cursor is blinked off, so determine how to "toggle" it. */
27689
27690 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27691 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27692 return get_specified_cursor_type (XCDR (alt_cursor), width);
27693
27694 /* Then see if frame has specified a specific blink off cursor type. */
27695 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27696 {
27697 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27698 return FRAME_BLINK_OFF_CURSOR (f);
27699 }
27700
27701 #if false
27702 /* Some people liked having a permanently visible blinking cursor,
27703 while others had very strong opinions against it. So it was
27704 decided to remove it. KFS 2003-09-03 */
27705
27706 /* Finally perform built-in cursor blinking:
27707 filled box <-> hollow box
27708 wide [h]bar <-> narrow [h]bar
27709 narrow [h]bar <-> no cursor
27710 other type <-> no cursor */
27711
27712 if (cursor_type == FILLED_BOX_CURSOR)
27713 return HOLLOW_BOX_CURSOR;
27714
27715 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27716 {
27717 *width = 1;
27718 return cursor_type;
27719 }
27720 #endif
27721
27722 return NO_CURSOR;
27723 }
27724
27725
27726 /* Notice when the text cursor of window W has been completely
27727 overwritten by a drawing operation that outputs glyphs in AREA
27728 starting at X0 and ending at X1 in the line starting at Y0 and
27729 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27730 the rest of the line after X0 has been written. Y coordinates
27731 are window-relative. */
27732
27733 static void
27734 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27735 int x0, int x1, int y0, int y1)
27736 {
27737 int cx0, cx1, cy0, cy1;
27738 struct glyph_row *row;
27739
27740 if (!w->phys_cursor_on_p)
27741 return;
27742 if (area != TEXT_AREA)
27743 return;
27744
27745 if (w->phys_cursor.vpos < 0
27746 || w->phys_cursor.vpos >= w->current_matrix->nrows
27747 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27748 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27749 return;
27750
27751 if (row->cursor_in_fringe_p)
27752 {
27753 row->cursor_in_fringe_p = false;
27754 draw_fringe_bitmap (w, row, row->reversed_p);
27755 w->phys_cursor_on_p = false;
27756 return;
27757 }
27758
27759 cx0 = w->phys_cursor.x;
27760 cx1 = cx0 + w->phys_cursor_width;
27761 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27762 return;
27763
27764 /* The cursor image will be completely removed from the
27765 screen if the output area intersects the cursor area in
27766 y-direction. When we draw in [y0 y1[, and some part of
27767 the cursor is at y < y0, that part must have been drawn
27768 before. When scrolling, the cursor is erased before
27769 actually scrolling, so we don't come here. When not
27770 scrolling, the rows above the old cursor row must have
27771 changed, and in this case these rows must have written
27772 over the cursor image.
27773
27774 Likewise if part of the cursor is below y1, with the
27775 exception of the cursor being in the first blank row at
27776 the buffer and window end because update_text_area
27777 doesn't draw that row. (Except when it does, but
27778 that's handled in update_text_area.) */
27779
27780 cy0 = w->phys_cursor.y;
27781 cy1 = cy0 + w->phys_cursor_height;
27782 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27783 return;
27784
27785 w->phys_cursor_on_p = false;
27786 }
27787
27788 #endif /* HAVE_WINDOW_SYSTEM */
27789
27790 \f
27791 /************************************************************************
27792 Mouse Face
27793 ************************************************************************/
27794
27795 #ifdef HAVE_WINDOW_SYSTEM
27796
27797 /* EXPORT for RIF:
27798 Fix the display of area AREA of overlapping row ROW in window W
27799 with respect to the overlapping part OVERLAPS. */
27800
27801 void
27802 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27803 enum glyph_row_area area, int overlaps)
27804 {
27805 int i, x;
27806
27807 block_input ();
27808
27809 x = 0;
27810 for (i = 0; i < row->used[area];)
27811 {
27812 if (row->glyphs[area][i].overlaps_vertically_p)
27813 {
27814 int start = i, start_x = x;
27815
27816 do
27817 {
27818 x += row->glyphs[area][i].pixel_width;
27819 ++i;
27820 }
27821 while (i < row->used[area]
27822 && row->glyphs[area][i].overlaps_vertically_p);
27823
27824 draw_glyphs (w, start_x, row, area,
27825 start, i,
27826 DRAW_NORMAL_TEXT, overlaps);
27827 }
27828 else
27829 {
27830 x += row->glyphs[area][i].pixel_width;
27831 ++i;
27832 }
27833 }
27834
27835 unblock_input ();
27836 }
27837
27838
27839 /* EXPORT:
27840 Draw the cursor glyph of window W in glyph row ROW. See the
27841 comment of draw_glyphs for the meaning of HL. */
27842
27843 void
27844 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27845 enum draw_glyphs_face hl)
27846 {
27847 /* If cursor hpos is out of bounds, don't draw garbage. This can
27848 happen in mini-buffer windows when switching between echo area
27849 glyphs and mini-buffer. */
27850 if ((row->reversed_p
27851 ? (w->phys_cursor.hpos >= 0)
27852 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27853 {
27854 bool on_p = w->phys_cursor_on_p;
27855 int x1;
27856 int hpos = w->phys_cursor.hpos;
27857
27858 /* When the window is hscrolled, cursor hpos can legitimately be
27859 out of bounds, but we draw the cursor at the corresponding
27860 window margin in that case. */
27861 if (!row->reversed_p && hpos < 0)
27862 hpos = 0;
27863 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27864 hpos = row->used[TEXT_AREA] - 1;
27865
27866 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27867 hl, 0);
27868 w->phys_cursor_on_p = on_p;
27869
27870 if (hl == DRAW_CURSOR)
27871 w->phys_cursor_width = x1 - w->phys_cursor.x;
27872 /* When we erase the cursor, and ROW is overlapped by other
27873 rows, make sure that these overlapping parts of other rows
27874 are redrawn. */
27875 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27876 {
27877 w->phys_cursor_width = x1 - w->phys_cursor.x;
27878
27879 if (row > w->current_matrix->rows
27880 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27881 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27882 OVERLAPS_ERASED_CURSOR);
27883
27884 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27885 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27886 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27887 OVERLAPS_ERASED_CURSOR);
27888 }
27889 }
27890 }
27891
27892
27893 /* Erase the image of a cursor of window W from the screen. */
27894
27895 void
27896 erase_phys_cursor (struct window *w)
27897 {
27898 struct frame *f = XFRAME (w->frame);
27899 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27900 int hpos = w->phys_cursor.hpos;
27901 int vpos = w->phys_cursor.vpos;
27902 bool mouse_face_here_p = false;
27903 struct glyph_matrix *active_glyphs = w->current_matrix;
27904 struct glyph_row *cursor_row;
27905 struct glyph *cursor_glyph;
27906 enum draw_glyphs_face hl;
27907
27908 /* No cursor displayed or row invalidated => nothing to do on the
27909 screen. */
27910 if (w->phys_cursor_type == NO_CURSOR)
27911 goto mark_cursor_off;
27912
27913 /* VPOS >= active_glyphs->nrows means that window has been resized.
27914 Don't bother to erase the cursor. */
27915 if (vpos >= active_glyphs->nrows)
27916 goto mark_cursor_off;
27917
27918 /* If row containing cursor is marked invalid, there is nothing we
27919 can do. */
27920 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27921 if (!cursor_row->enabled_p)
27922 goto mark_cursor_off;
27923
27924 /* If line spacing is > 0, old cursor may only be partially visible in
27925 window after split-window. So adjust visible height. */
27926 cursor_row->visible_height = min (cursor_row->visible_height,
27927 window_text_bottom_y (w) - cursor_row->y);
27928
27929 /* If row is completely invisible, don't attempt to delete a cursor which
27930 isn't there. This can happen if cursor is at top of a window, and
27931 we switch to a buffer with a header line in that window. */
27932 if (cursor_row->visible_height <= 0)
27933 goto mark_cursor_off;
27934
27935 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27936 if (cursor_row->cursor_in_fringe_p)
27937 {
27938 cursor_row->cursor_in_fringe_p = false;
27939 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27940 goto mark_cursor_off;
27941 }
27942
27943 /* This can happen when the new row is shorter than the old one.
27944 In this case, either draw_glyphs or clear_end_of_line
27945 should have cleared the cursor. Note that we wouldn't be
27946 able to erase the cursor in this case because we don't have a
27947 cursor glyph at hand. */
27948 if ((cursor_row->reversed_p
27949 ? (w->phys_cursor.hpos < 0)
27950 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27951 goto mark_cursor_off;
27952
27953 /* When the window is hscrolled, cursor hpos can legitimately be out
27954 of bounds, but we draw the cursor at the corresponding window
27955 margin in that case. */
27956 if (!cursor_row->reversed_p && hpos < 0)
27957 hpos = 0;
27958 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27959 hpos = cursor_row->used[TEXT_AREA] - 1;
27960
27961 /* If the cursor is in the mouse face area, redisplay that when
27962 we clear the cursor. */
27963 if (! NILP (hlinfo->mouse_face_window)
27964 && coords_in_mouse_face_p (w, hpos, vpos)
27965 /* Don't redraw the cursor's spot in mouse face if it is at the
27966 end of a line (on a newline). The cursor appears there, but
27967 mouse highlighting does not. */
27968 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27969 mouse_face_here_p = true;
27970
27971 /* Maybe clear the display under the cursor. */
27972 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27973 {
27974 int x, y;
27975 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27976 int width;
27977
27978 cursor_glyph = get_phys_cursor_glyph (w);
27979 if (cursor_glyph == NULL)
27980 goto mark_cursor_off;
27981
27982 width = cursor_glyph->pixel_width;
27983 x = w->phys_cursor.x;
27984 if (x < 0)
27985 {
27986 width += x;
27987 x = 0;
27988 }
27989 width = min (width, window_box_width (w, TEXT_AREA) - x);
27990 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27991 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27992
27993 if (width > 0)
27994 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27995 }
27996
27997 /* Erase the cursor by redrawing the character underneath it. */
27998 if (mouse_face_here_p)
27999 hl = DRAW_MOUSE_FACE;
28000 else
28001 hl = DRAW_NORMAL_TEXT;
28002 draw_phys_cursor_glyph (w, cursor_row, hl);
28003
28004 mark_cursor_off:
28005 w->phys_cursor_on_p = false;
28006 w->phys_cursor_type = NO_CURSOR;
28007 }
28008
28009
28010 /* Display or clear cursor of window W. If !ON, clear the cursor.
28011 If ON, display the cursor; where to put the cursor is specified by
28012 HPOS, VPOS, X and Y. */
28013
28014 void
28015 display_and_set_cursor (struct window *w, bool on,
28016 int hpos, int vpos, int x, int y)
28017 {
28018 struct frame *f = XFRAME (w->frame);
28019 int new_cursor_type;
28020 int new_cursor_width;
28021 bool active_cursor;
28022 struct glyph_row *glyph_row;
28023 struct glyph *glyph;
28024
28025 /* This is pointless on invisible frames, and dangerous on garbaged
28026 windows and frames; in the latter case, the frame or window may
28027 be in the midst of changing its size, and x and y may be off the
28028 window. */
28029 if (! FRAME_VISIBLE_P (f)
28030 || FRAME_GARBAGED_P (f)
28031 || vpos >= w->current_matrix->nrows
28032 || hpos >= w->current_matrix->matrix_w)
28033 return;
28034
28035 /* If cursor is off and we want it off, return quickly. */
28036 if (!on && !w->phys_cursor_on_p)
28037 return;
28038
28039 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28040 /* If cursor row is not enabled, we don't really know where to
28041 display the cursor. */
28042 if (!glyph_row->enabled_p)
28043 {
28044 w->phys_cursor_on_p = false;
28045 return;
28046 }
28047
28048 glyph = NULL;
28049 if (!glyph_row->exact_window_width_line_p
28050 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28051 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28052
28053 eassert (input_blocked_p ());
28054
28055 /* Set new_cursor_type to the cursor we want to be displayed. */
28056 new_cursor_type = get_window_cursor_type (w, glyph,
28057 &new_cursor_width, &active_cursor);
28058
28059 /* If cursor is currently being shown and we don't want it to be or
28060 it is in the wrong place, or the cursor type is not what we want,
28061 erase it. */
28062 if (w->phys_cursor_on_p
28063 && (!on
28064 || w->phys_cursor.x != x
28065 || w->phys_cursor.y != y
28066 /* HPOS can be negative in R2L rows whose
28067 exact_window_width_line_p flag is set (i.e. their newline
28068 would "overflow into the fringe"). */
28069 || hpos < 0
28070 || new_cursor_type != w->phys_cursor_type
28071 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28072 && new_cursor_width != w->phys_cursor_width)))
28073 erase_phys_cursor (w);
28074
28075 /* Don't check phys_cursor_on_p here because that flag is only set
28076 to false in some cases where we know that the cursor has been
28077 completely erased, to avoid the extra work of erasing the cursor
28078 twice. In other words, phys_cursor_on_p can be true and the cursor
28079 still not be visible, or it has only been partly erased. */
28080 if (on)
28081 {
28082 w->phys_cursor_ascent = glyph_row->ascent;
28083 w->phys_cursor_height = glyph_row->height;
28084
28085 /* Set phys_cursor_.* before x_draw_.* is called because some
28086 of them may need the information. */
28087 w->phys_cursor.x = x;
28088 w->phys_cursor.y = glyph_row->y;
28089 w->phys_cursor.hpos = hpos;
28090 w->phys_cursor.vpos = vpos;
28091 }
28092
28093 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28094 new_cursor_type, new_cursor_width,
28095 on, active_cursor);
28096 }
28097
28098
28099 /* Switch the display of W's cursor on or off, according to the value
28100 of ON. */
28101
28102 static void
28103 update_window_cursor (struct window *w, bool on)
28104 {
28105 /* Don't update cursor in windows whose frame is in the process
28106 of being deleted. */
28107 if (w->current_matrix)
28108 {
28109 int hpos = w->phys_cursor.hpos;
28110 int vpos = w->phys_cursor.vpos;
28111 struct glyph_row *row;
28112
28113 if (vpos >= w->current_matrix->nrows
28114 || hpos >= w->current_matrix->matrix_w)
28115 return;
28116
28117 row = MATRIX_ROW (w->current_matrix, vpos);
28118
28119 /* When the window is hscrolled, cursor hpos can legitimately be
28120 out of bounds, but we draw the cursor at the corresponding
28121 window margin in that case. */
28122 if (!row->reversed_p && hpos < 0)
28123 hpos = 0;
28124 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28125 hpos = row->used[TEXT_AREA] - 1;
28126
28127 block_input ();
28128 display_and_set_cursor (w, on, hpos, vpos,
28129 w->phys_cursor.x, w->phys_cursor.y);
28130 unblock_input ();
28131 }
28132 }
28133
28134
28135 /* Call update_window_cursor with parameter ON_P on all leaf windows
28136 in the window tree rooted at W. */
28137
28138 static void
28139 update_cursor_in_window_tree (struct window *w, bool on_p)
28140 {
28141 while (w)
28142 {
28143 if (WINDOWP (w->contents))
28144 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28145 else
28146 update_window_cursor (w, on_p);
28147
28148 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28149 }
28150 }
28151
28152
28153 /* EXPORT:
28154 Display the cursor on window W, or clear it, according to ON_P.
28155 Don't change the cursor's position. */
28156
28157 void
28158 x_update_cursor (struct frame *f, bool on_p)
28159 {
28160 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28161 }
28162
28163
28164 /* EXPORT:
28165 Clear the cursor of window W to background color, and mark the
28166 cursor as not shown. This is used when the text where the cursor
28167 is about to be rewritten. */
28168
28169 void
28170 x_clear_cursor (struct window *w)
28171 {
28172 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28173 update_window_cursor (w, false);
28174 }
28175
28176 #endif /* HAVE_WINDOW_SYSTEM */
28177
28178 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28179 and MSDOS. */
28180 static void
28181 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28182 int start_hpos, int end_hpos,
28183 enum draw_glyphs_face draw)
28184 {
28185 #ifdef HAVE_WINDOW_SYSTEM
28186 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28187 {
28188 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28189 return;
28190 }
28191 #endif
28192 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28193 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28194 #endif
28195 }
28196
28197 /* Display the active region described by mouse_face_* according to DRAW. */
28198
28199 static void
28200 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28201 {
28202 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28203 struct frame *f = XFRAME (WINDOW_FRAME (w));
28204
28205 if (/* If window is in the process of being destroyed, don't bother
28206 to do anything. */
28207 w->current_matrix != NULL
28208 /* Don't update mouse highlight if hidden. */
28209 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28210 /* Recognize when we are called to operate on rows that don't exist
28211 anymore. This can happen when a window is split. */
28212 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28213 {
28214 bool phys_cursor_on_p = w->phys_cursor_on_p;
28215 struct glyph_row *row, *first, *last;
28216
28217 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28218 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28219
28220 for (row = first; row <= last && row->enabled_p; ++row)
28221 {
28222 int start_hpos, end_hpos, start_x;
28223
28224 /* For all but the first row, the highlight starts at column 0. */
28225 if (row == first)
28226 {
28227 /* R2L rows have BEG and END in reversed order, but the
28228 screen drawing geometry is always left to right. So
28229 we need to mirror the beginning and end of the
28230 highlighted area in R2L rows. */
28231 if (!row->reversed_p)
28232 {
28233 start_hpos = hlinfo->mouse_face_beg_col;
28234 start_x = hlinfo->mouse_face_beg_x;
28235 }
28236 else if (row == last)
28237 {
28238 start_hpos = hlinfo->mouse_face_end_col;
28239 start_x = hlinfo->mouse_face_end_x;
28240 }
28241 else
28242 {
28243 start_hpos = 0;
28244 start_x = 0;
28245 }
28246 }
28247 else if (row->reversed_p && row == last)
28248 {
28249 start_hpos = hlinfo->mouse_face_end_col;
28250 start_x = hlinfo->mouse_face_end_x;
28251 }
28252 else
28253 {
28254 start_hpos = 0;
28255 start_x = 0;
28256 }
28257
28258 if (row == last)
28259 {
28260 if (!row->reversed_p)
28261 end_hpos = hlinfo->mouse_face_end_col;
28262 else if (row == first)
28263 end_hpos = hlinfo->mouse_face_beg_col;
28264 else
28265 {
28266 end_hpos = row->used[TEXT_AREA];
28267 if (draw == DRAW_NORMAL_TEXT)
28268 row->fill_line_p = true; /* Clear to end of line. */
28269 }
28270 }
28271 else if (row->reversed_p && row == first)
28272 end_hpos = hlinfo->mouse_face_beg_col;
28273 else
28274 {
28275 end_hpos = row->used[TEXT_AREA];
28276 if (draw == DRAW_NORMAL_TEXT)
28277 row->fill_line_p = true; /* Clear to end of line. */
28278 }
28279
28280 if (end_hpos > start_hpos)
28281 {
28282 draw_row_with_mouse_face (w, start_x, row,
28283 start_hpos, end_hpos, draw);
28284
28285 row->mouse_face_p
28286 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28287 }
28288 }
28289
28290 #ifdef HAVE_WINDOW_SYSTEM
28291 /* When we've written over the cursor, arrange for it to
28292 be displayed again. */
28293 if (FRAME_WINDOW_P (f)
28294 && phys_cursor_on_p && !w->phys_cursor_on_p)
28295 {
28296 int hpos = w->phys_cursor.hpos;
28297
28298 /* When the window is hscrolled, cursor hpos can legitimately be
28299 out of bounds, but we draw the cursor at the corresponding
28300 window margin in that case. */
28301 if (!row->reversed_p && hpos < 0)
28302 hpos = 0;
28303 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28304 hpos = row->used[TEXT_AREA] - 1;
28305
28306 block_input ();
28307 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
28308 w->phys_cursor.x, w->phys_cursor.y);
28309 unblock_input ();
28310 }
28311 #endif /* HAVE_WINDOW_SYSTEM */
28312 }
28313
28314 #ifdef HAVE_WINDOW_SYSTEM
28315 /* Change the mouse cursor. */
28316 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28317 {
28318 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28319 if (draw == DRAW_NORMAL_TEXT
28320 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28321 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28322 else
28323 #endif
28324 if (draw == DRAW_MOUSE_FACE)
28325 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28326 else
28327 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28328 }
28329 #endif /* HAVE_WINDOW_SYSTEM */
28330 }
28331
28332 /* EXPORT:
28333 Clear out the mouse-highlighted active region.
28334 Redraw it un-highlighted first. Value is true if mouse
28335 face was actually drawn unhighlighted. */
28336
28337 bool
28338 clear_mouse_face (Mouse_HLInfo *hlinfo)
28339 {
28340 bool cleared
28341 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
28342 if (cleared)
28343 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28344 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28345 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28346 hlinfo->mouse_face_window = Qnil;
28347 hlinfo->mouse_face_overlay = Qnil;
28348 return cleared;
28349 }
28350
28351 /* Return true if the coordinates HPOS and VPOS on windows W are
28352 within the mouse face on that window. */
28353 static bool
28354 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28355 {
28356 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28357
28358 /* Quickly resolve the easy cases. */
28359 if (!(WINDOWP (hlinfo->mouse_face_window)
28360 && XWINDOW (hlinfo->mouse_face_window) == w))
28361 return false;
28362 if (vpos < hlinfo->mouse_face_beg_row
28363 || vpos > hlinfo->mouse_face_end_row)
28364 return false;
28365 if (vpos > hlinfo->mouse_face_beg_row
28366 && vpos < hlinfo->mouse_face_end_row)
28367 return true;
28368
28369 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28370 {
28371 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28372 {
28373 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28374 return true;
28375 }
28376 else if ((vpos == hlinfo->mouse_face_beg_row
28377 && hpos >= hlinfo->mouse_face_beg_col)
28378 || (vpos == hlinfo->mouse_face_end_row
28379 && hpos < hlinfo->mouse_face_end_col))
28380 return true;
28381 }
28382 else
28383 {
28384 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28385 {
28386 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28387 return true;
28388 }
28389 else if ((vpos == hlinfo->mouse_face_beg_row
28390 && hpos <= hlinfo->mouse_face_beg_col)
28391 || (vpos == hlinfo->mouse_face_end_row
28392 && hpos > hlinfo->mouse_face_end_col))
28393 return true;
28394 }
28395 return false;
28396 }
28397
28398
28399 /* EXPORT:
28400 True if physical cursor of window W is within mouse face. */
28401
28402 bool
28403 cursor_in_mouse_face_p (struct window *w)
28404 {
28405 int hpos = w->phys_cursor.hpos;
28406 int vpos = w->phys_cursor.vpos;
28407 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28408
28409 /* When the window is hscrolled, cursor hpos can legitimately be out
28410 of bounds, but we draw the cursor at the corresponding window
28411 margin in that case. */
28412 if (!row->reversed_p && hpos < 0)
28413 hpos = 0;
28414 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28415 hpos = row->used[TEXT_AREA] - 1;
28416
28417 return coords_in_mouse_face_p (w, hpos, vpos);
28418 }
28419
28420
28421 \f
28422 /* Find the glyph rows START_ROW and END_ROW of window W that display
28423 characters between buffer positions START_CHARPOS and END_CHARPOS
28424 (excluding END_CHARPOS). DISP_STRING is a display string that
28425 covers these buffer positions. This is similar to
28426 row_containing_pos, but is more accurate when bidi reordering makes
28427 buffer positions change non-linearly with glyph rows. */
28428 static void
28429 rows_from_pos_range (struct window *w,
28430 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28431 Lisp_Object disp_string,
28432 struct glyph_row **start, struct glyph_row **end)
28433 {
28434 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28435 int last_y = window_text_bottom_y (w);
28436 struct glyph_row *row;
28437
28438 *start = NULL;
28439 *end = NULL;
28440
28441 while (!first->enabled_p
28442 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28443 first++;
28444
28445 /* Find the START row. */
28446 for (row = first;
28447 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28448 row++)
28449 {
28450 /* A row can potentially be the START row if the range of the
28451 characters it displays intersects the range
28452 [START_CHARPOS..END_CHARPOS). */
28453 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28454 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28455 /* See the commentary in row_containing_pos, for the
28456 explanation of the complicated way to check whether
28457 some position is beyond the end of the characters
28458 displayed by a row. */
28459 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28460 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28461 && !row->ends_at_zv_p
28462 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28463 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28464 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28465 && !row->ends_at_zv_p
28466 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28467 {
28468 /* Found a candidate row. Now make sure at least one of the
28469 glyphs it displays has a charpos from the range
28470 [START_CHARPOS..END_CHARPOS).
28471
28472 This is not obvious because bidi reordering could make
28473 buffer positions of a row be 1,2,3,102,101,100, and if we
28474 want to highlight characters in [50..60), we don't want
28475 this row, even though [50..60) does intersect [1..103),
28476 the range of character positions given by the row's start
28477 and end positions. */
28478 struct glyph *g = row->glyphs[TEXT_AREA];
28479 struct glyph *e = g + row->used[TEXT_AREA];
28480
28481 while (g < e)
28482 {
28483 if (((BUFFERP (g->object) || NILP (g->object))
28484 && start_charpos <= g->charpos && g->charpos < end_charpos)
28485 /* A glyph that comes from DISP_STRING is by
28486 definition to be highlighted. */
28487 || EQ (g->object, disp_string))
28488 *start = row;
28489 g++;
28490 }
28491 if (*start)
28492 break;
28493 }
28494 }
28495
28496 /* Find the END row. */
28497 if (!*start
28498 /* If the last row is partially visible, start looking for END
28499 from that row, instead of starting from FIRST. */
28500 && !(row->enabled_p
28501 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28502 row = first;
28503 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28504 {
28505 struct glyph_row *next = row + 1;
28506 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28507
28508 if (!next->enabled_p
28509 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28510 /* The first row >= START whose range of displayed characters
28511 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28512 is the row END + 1. */
28513 || (start_charpos < next_start
28514 && end_charpos < next_start)
28515 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28516 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28517 && !next->ends_at_zv_p
28518 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28519 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28520 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28521 && !next->ends_at_zv_p
28522 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28523 {
28524 *end = row;
28525 break;
28526 }
28527 else
28528 {
28529 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28530 but none of the characters it displays are in the range, it is
28531 also END + 1. */
28532 struct glyph *g = next->glyphs[TEXT_AREA];
28533 struct glyph *s = g;
28534 struct glyph *e = g + next->used[TEXT_AREA];
28535
28536 while (g < e)
28537 {
28538 if (((BUFFERP (g->object) || NILP (g->object))
28539 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28540 /* If the buffer position of the first glyph in
28541 the row is equal to END_CHARPOS, it means
28542 the last character to be highlighted is the
28543 newline of ROW, and we must consider NEXT as
28544 END, not END+1. */
28545 || (((!next->reversed_p && g == s)
28546 || (next->reversed_p && g == e - 1))
28547 && (g->charpos == end_charpos
28548 /* Special case for when NEXT is an
28549 empty line at ZV. */
28550 || (g->charpos == -1
28551 && !row->ends_at_zv_p
28552 && next_start == end_charpos)))))
28553 /* A glyph that comes from DISP_STRING is by
28554 definition to be highlighted. */
28555 || EQ (g->object, disp_string))
28556 break;
28557 g++;
28558 }
28559 if (g == e)
28560 {
28561 *end = row;
28562 break;
28563 }
28564 /* The first row that ends at ZV must be the last to be
28565 highlighted. */
28566 else if (next->ends_at_zv_p)
28567 {
28568 *end = next;
28569 break;
28570 }
28571 }
28572 }
28573 }
28574
28575 /* This function sets the mouse_face_* elements of HLINFO, assuming
28576 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28577 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28578 for the overlay or run of text properties specifying the mouse
28579 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28580 before-string and after-string that must also be highlighted.
28581 DISP_STRING, if non-nil, is a display string that may cover some
28582 or all of the highlighted text. */
28583
28584 static void
28585 mouse_face_from_buffer_pos (Lisp_Object window,
28586 Mouse_HLInfo *hlinfo,
28587 ptrdiff_t mouse_charpos,
28588 ptrdiff_t start_charpos,
28589 ptrdiff_t end_charpos,
28590 Lisp_Object before_string,
28591 Lisp_Object after_string,
28592 Lisp_Object disp_string)
28593 {
28594 struct window *w = XWINDOW (window);
28595 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28596 struct glyph_row *r1, *r2;
28597 struct glyph *glyph, *end;
28598 ptrdiff_t ignore, pos;
28599 int x;
28600
28601 eassert (NILP (disp_string) || STRINGP (disp_string));
28602 eassert (NILP (before_string) || STRINGP (before_string));
28603 eassert (NILP (after_string) || STRINGP (after_string));
28604
28605 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28606 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28607 if (r1 == NULL)
28608 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28609 /* If the before-string or display-string contains newlines,
28610 rows_from_pos_range skips to its last row. Move back. */
28611 if (!NILP (before_string) || !NILP (disp_string))
28612 {
28613 struct glyph_row *prev;
28614 while ((prev = r1 - 1, prev >= first)
28615 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28616 && prev->used[TEXT_AREA] > 0)
28617 {
28618 struct glyph *beg = prev->glyphs[TEXT_AREA];
28619 glyph = beg + prev->used[TEXT_AREA];
28620 while (--glyph >= beg && NILP (glyph->object));
28621 if (glyph < beg
28622 || !(EQ (glyph->object, before_string)
28623 || EQ (glyph->object, disp_string)))
28624 break;
28625 r1 = prev;
28626 }
28627 }
28628 if (r2 == NULL)
28629 {
28630 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28631 hlinfo->mouse_face_past_end = true;
28632 }
28633 else if (!NILP (after_string))
28634 {
28635 /* If the after-string has newlines, advance to its last row. */
28636 struct glyph_row *next;
28637 struct glyph_row *last
28638 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28639
28640 for (next = r2 + 1;
28641 next <= last
28642 && next->used[TEXT_AREA] > 0
28643 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28644 ++next)
28645 r2 = next;
28646 }
28647 /* The rest of the display engine assumes that mouse_face_beg_row is
28648 either above mouse_face_end_row or identical to it. But with
28649 bidi-reordered continued lines, the row for START_CHARPOS could
28650 be below the row for END_CHARPOS. If so, swap the rows and store
28651 them in correct order. */
28652 if (r1->y > r2->y)
28653 {
28654 struct glyph_row *tem = r2;
28655
28656 r2 = r1;
28657 r1 = tem;
28658 }
28659
28660 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28661 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28662
28663 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28664 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28665 could be anywhere in the row and in any order. The strategy
28666 below is to find the leftmost and the rightmost glyph that
28667 belongs to either of these 3 strings, or whose position is
28668 between START_CHARPOS and END_CHARPOS, and highlight all the
28669 glyphs between those two. This may cover more than just the text
28670 between START_CHARPOS and END_CHARPOS if the range of characters
28671 strides the bidi level boundary, e.g. if the beginning is in R2L
28672 text while the end is in L2R text or vice versa. */
28673 if (!r1->reversed_p)
28674 {
28675 /* This row is in a left to right paragraph. Scan it left to
28676 right. */
28677 glyph = r1->glyphs[TEXT_AREA];
28678 end = glyph + r1->used[TEXT_AREA];
28679 x = r1->x;
28680
28681 /* Skip truncation glyphs at the start of the glyph row. */
28682 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28683 for (; glyph < end
28684 && NILP (glyph->object)
28685 && glyph->charpos < 0;
28686 ++glyph)
28687 x += glyph->pixel_width;
28688
28689 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28690 or DISP_STRING, and the first glyph from buffer whose
28691 position is between START_CHARPOS and END_CHARPOS. */
28692 for (; glyph < end
28693 && !NILP (glyph->object)
28694 && !EQ (glyph->object, disp_string)
28695 && !(BUFFERP (glyph->object)
28696 && (glyph->charpos >= start_charpos
28697 && glyph->charpos < end_charpos));
28698 ++glyph)
28699 {
28700 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28701 are present at buffer positions between START_CHARPOS and
28702 END_CHARPOS, or if they come from an overlay. */
28703 if (EQ (glyph->object, before_string))
28704 {
28705 pos = string_buffer_position (before_string,
28706 start_charpos);
28707 /* If pos == 0, it means before_string came from an
28708 overlay, not from a buffer position. */
28709 if (!pos || (pos >= start_charpos && pos < end_charpos))
28710 break;
28711 }
28712 else if (EQ (glyph->object, after_string))
28713 {
28714 pos = string_buffer_position (after_string, end_charpos);
28715 if (!pos || (pos >= start_charpos && pos < end_charpos))
28716 break;
28717 }
28718 x += glyph->pixel_width;
28719 }
28720 hlinfo->mouse_face_beg_x = x;
28721 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28722 }
28723 else
28724 {
28725 /* This row is in a right to left paragraph. Scan it right to
28726 left. */
28727 struct glyph *g;
28728
28729 end = r1->glyphs[TEXT_AREA] - 1;
28730 glyph = end + r1->used[TEXT_AREA];
28731
28732 /* Skip truncation glyphs at the start of the glyph row. */
28733 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28734 for (; glyph > end
28735 && NILP (glyph->object)
28736 && glyph->charpos < 0;
28737 --glyph)
28738 ;
28739
28740 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28741 or DISP_STRING, and the first glyph from buffer whose
28742 position is between START_CHARPOS and END_CHARPOS. */
28743 for (; glyph > end
28744 && !NILP (glyph->object)
28745 && !EQ (glyph->object, disp_string)
28746 && !(BUFFERP (glyph->object)
28747 && (glyph->charpos >= start_charpos
28748 && glyph->charpos < end_charpos));
28749 --glyph)
28750 {
28751 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28752 are present at buffer positions between START_CHARPOS and
28753 END_CHARPOS, or if they come from an overlay. */
28754 if (EQ (glyph->object, before_string))
28755 {
28756 pos = string_buffer_position (before_string, start_charpos);
28757 /* If pos == 0, it means before_string came from an
28758 overlay, not from a buffer position. */
28759 if (!pos || (pos >= start_charpos && pos < end_charpos))
28760 break;
28761 }
28762 else if (EQ (glyph->object, after_string))
28763 {
28764 pos = string_buffer_position (after_string, end_charpos);
28765 if (!pos || (pos >= start_charpos && pos < end_charpos))
28766 break;
28767 }
28768 }
28769
28770 glyph++; /* first glyph to the right of the highlighted area */
28771 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28772 x += g->pixel_width;
28773 hlinfo->mouse_face_beg_x = x;
28774 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28775 }
28776
28777 /* If the highlight ends in a different row, compute GLYPH and END
28778 for the end row. Otherwise, reuse the values computed above for
28779 the row where the highlight begins. */
28780 if (r2 != r1)
28781 {
28782 if (!r2->reversed_p)
28783 {
28784 glyph = r2->glyphs[TEXT_AREA];
28785 end = glyph + r2->used[TEXT_AREA];
28786 x = r2->x;
28787 }
28788 else
28789 {
28790 end = r2->glyphs[TEXT_AREA] - 1;
28791 glyph = end + r2->used[TEXT_AREA];
28792 }
28793 }
28794
28795 if (!r2->reversed_p)
28796 {
28797 /* Skip truncation and continuation glyphs near the end of the
28798 row, and also blanks and stretch glyphs inserted by
28799 extend_face_to_end_of_line. */
28800 while (end > glyph
28801 && NILP ((end - 1)->object))
28802 --end;
28803 /* Scan the rest of the glyph row from the end, looking for the
28804 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28805 DISP_STRING, or whose position is between START_CHARPOS
28806 and END_CHARPOS */
28807 for (--end;
28808 end > glyph
28809 && !NILP (end->object)
28810 && !EQ (end->object, disp_string)
28811 && !(BUFFERP (end->object)
28812 && (end->charpos >= start_charpos
28813 && end->charpos < end_charpos));
28814 --end)
28815 {
28816 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28817 are present at buffer positions between START_CHARPOS and
28818 END_CHARPOS, or if they come from an overlay. */
28819 if (EQ (end->object, before_string))
28820 {
28821 pos = string_buffer_position (before_string, start_charpos);
28822 if (!pos || (pos >= start_charpos && pos < end_charpos))
28823 break;
28824 }
28825 else if (EQ (end->object, after_string))
28826 {
28827 pos = string_buffer_position (after_string, end_charpos);
28828 if (!pos || (pos >= start_charpos && pos < end_charpos))
28829 break;
28830 }
28831 }
28832 /* Find the X coordinate of the last glyph to be highlighted. */
28833 for (; glyph <= end; ++glyph)
28834 x += glyph->pixel_width;
28835
28836 hlinfo->mouse_face_end_x = x;
28837 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28838 }
28839 else
28840 {
28841 /* Skip truncation and continuation glyphs near the end of the
28842 row, and also blanks and stretch glyphs inserted by
28843 extend_face_to_end_of_line. */
28844 x = r2->x;
28845 end++;
28846 while (end < glyph
28847 && NILP (end->object))
28848 {
28849 x += end->pixel_width;
28850 ++end;
28851 }
28852 /* Scan the rest of the glyph row from the end, looking for the
28853 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28854 DISP_STRING, or whose position is between START_CHARPOS
28855 and END_CHARPOS */
28856 for ( ;
28857 end < glyph
28858 && !NILP (end->object)
28859 && !EQ (end->object, disp_string)
28860 && !(BUFFERP (end->object)
28861 && (end->charpos >= start_charpos
28862 && end->charpos < end_charpos));
28863 ++end)
28864 {
28865 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28866 are present at buffer positions between START_CHARPOS and
28867 END_CHARPOS, or if they come from an overlay. */
28868 if (EQ (end->object, before_string))
28869 {
28870 pos = string_buffer_position (before_string, start_charpos);
28871 if (!pos || (pos >= start_charpos && pos < end_charpos))
28872 break;
28873 }
28874 else if (EQ (end->object, after_string))
28875 {
28876 pos = string_buffer_position (after_string, end_charpos);
28877 if (!pos || (pos >= start_charpos && pos < end_charpos))
28878 break;
28879 }
28880 x += end->pixel_width;
28881 }
28882 /* If we exited the above loop because we arrived at the last
28883 glyph of the row, and its buffer position is still not in
28884 range, it means the last character in range is the preceding
28885 newline. Bump the end column and x values to get past the
28886 last glyph. */
28887 if (end == glyph
28888 && BUFFERP (end->object)
28889 && (end->charpos < start_charpos
28890 || end->charpos >= end_charpos))
28891 {
28892 x += end->pixel_width;
28893 ++end;
28894 }
28895 hlinfo->mouse_face_end_x = x;
28896 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28897 }
28898
28899 hlinfo->mouse_face_window = window;
28900 hlinfo->mouse_face_face_id
28901 = face_at_buffer_position (w, mouse_charpos, &ignore,
28902 mouse_charpos + 1,
28903 !hlinfo->mouse_face_hidden, -1);
28904 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28905 }
28906
28907 /* The following function is not used anymore (replaced with
28908 mouse_face_from_string_pos), but I leave it here for the time
28909 being, in case someone would. */
28910
28911 #if false /* not used */
28912
28913 /* Find the position of the glyph for position POS in OBJECT in
28914 window W's current matrix, and return in *X, *Y the pixel
28915 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28916
28917 RIGHT_P means return the position of the right edge of the glyph.
28918 !RIGHT_P means return the left edge position.
28919
28920 If no glyph for POS exists in the matrix, return the position of
28921 the glyph with the next smaller position that is in the matrix, if
28922 RIGHT_P is false. If RIGHT_P, and no glyph for POS
28923 exists in the matrix, return the position of the glyph with the
28924 next larger position in OBJECT.
28925
28926 Value is true if a glyph was found. */
28927
28928 static bool
28929 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28930 int *hpos, int *vpos, int *x, int *y, bool right_p)
28931 {
28932 int yb = window_text_bottom_y (w);
28933 struct glyph_row *r;
28934 struct glyph *best_glyph = NULL;
28935 struct glyph_row *best_row = NULL;
28936 int best_x = 0;
28937
28938 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28939 r->enabled_p && r->y < yb;
28940 ++r)
28941 {
28942 struct glyph *g = r->glyphs[TEXT_AREA];
28943 struct glyph *e = g + r->used[TEXT_AREA];
28944 int gx;
28945
28946 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28947 if (EQ (g->object, object))
28948 {
28949 if (g->charpos == pos)
28950 {
28951 best_glyph = g;
28952 best_x = gx;
28953 best_row = r;
28954 goto found;
28955 }
28956 else if (best_glyph == NULL
28957 || ((eabs (g->charpos - pos)
28958 < eabs (best_glyph->charpos - pos))
28959 && (right_p
28960 ? g->charpos < pos
28961 : g->charpos > pos)))
28962 {
28963 best_glyph = g;
28964 best_x = gx;
28965 best_row = r;
28966 }
28967 }
28968 }
28969
28970 found:
28971
28972 if (best_glyph)
28973 {
28974 *x = best_x;
28975 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28976
28977 if (right_p)
28978 {
28979 *x += best_glyph->pixel_width;
28980 ++*hpos;
28981 }
28982
28983 *y = best_row->y;
28984 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28985 }
28986
28987 return best_glyph != NULL;
28988 }
28989 #endif /* not used */
28990
28991 /* Find the positions of the first and the last glyphs in window W's
28992 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28993 (assumed to be a string), and return in HLINFO's mouse_face_*
28994 members the pixel and column/row coordinates of those glyphs. */
28995
28996 static void
28997 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28998 Lisp_Object object,
28999 ptrdiff_t startpos, ptrdiff_t endpos)
29000 {
29001 int yb = window_text_bottom_y (w);
29002 struct glyph_row *r;
29003 struct glyph *g, *e;
29004 int gx;
29005 bool found = false;
29006
29007 /* Find the glyph row with at least one position in the range
29008 [STARTPOS..ENDPOS), and the first glyph in that row whose
29009 position belongs to that range. */
29010 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29011 r->enabled_p && r->y < yb;
29012 ++r)
29013 {
29014 if (!r->reversed_p)
29015 {
29016 g = r->glyphs[TEXT_AREA];
29017 e = g + r->used[TEXT_AREA];
29018 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29019 if (EQ (g->object, object)
29020 && startpos <= g->charpos && g->charpos < endpos)
29021 {
29022 hlinfo->mouse_face_beg_row
29023 = MATRIX_ROW_VPOS (r, w->current_matrix);
29024 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29025 hlinfo->mouse_face_beg_x = gx;
29026 found = true;
29027 break;
29028 }
29029 }
29030 else
29031 {
29032 struct glyph *g1;
29033
29034 e = r->glyphs[TEXT_AREA];
29035 g = e + r->used[TEXT_AREA];
29036 for ( ; g > e; --g)
29037 if (EQ ((g-1)->object, object)
29038 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29039 {
29040 hlinfo->mouse_face_beg_row
29041 = MATRIX_ROW_VPOS (r, w->current_matrix);
29042 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29043 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29044 gx += g1->pixel_width;
29045 hlinfo->mouse_face_beg_x = gx;
29046 found = true;
29047 break;
29048 }
29049 }
29050 if (found)
29051 break;
29052 }
29053
29054 if (!found)
29055 return;
29056
29057 /* Starting with the next row, look for the first row which does NOT
29058 include any glyphs whose positions are in the range. */
29059 for (++r; r->enabled_p && r->y < yb; ++r)
29060 {
29061 g = r->glyphs[TEXT_AREA];
29062 e = g + r->used[TEXT_AREA];
29063 found = false;
29064 for ( ; g < e; ++g)
29065 if (EQ (g->object, object)
29066 && startpos <= g->charpos && g->charpos < endpos)
29067 {
29068 found = true;
29069 break;
29070 }
29071 if (!found)
29072 break;
29073 }
29074
29075 /* The highlighted region ends on the previous row. */
29076 r--;
29077
29078 /* Set the end row. */
29079 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29080
29081 /* Compute and set the end column and the end column's horizontal
29082 pixel coordinate. */
29083 if (!r->reversed_p)
29084 {
29085 g = r->glyphs[TEXT_AREA];
29086 e = g + r->used[TEXT_AREA];
29087 for ( ; e > g; --e)
29088 if (EQ ((e-1)->object, object)
29089 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29090 break;
29091 hlinfo->mouse_face_end_col = e - g;
29092
29093 for (gx = r->x; g < e; ++g)
29094 gx += g->pixel_width;
29095 hlinfo->mouse_face_end_x = gx;
29096 }
29097 else
29098 {
29099 e = r->glyphs[TEXT_AREA];
29100 g = e + r->used[TEXT_AREA];
29101 for (gx = r->x ; e < g; ++e)
29102 {
29103 if (EQ (e->object, object)
29104 && startpos <= e->charpos && e->charpos < endpos)
29105 break;
29106 gx += e->pixel_width;
29107 }
29108 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29109 hlinfo->mouse_face_end_x = gx;
29110 }
29111 }
29112
29113 #ifdef HAVE_WINDOW_SYSTEM
29114
29115 /* See if position X, Y is within a hot-spot of an image. */
29116
29117 static bool
29118 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29119 {
29120 if (!CONSP (hot_spot))
29121 return false;
29122
29123 if (EQ (XCAR (hot_spot), Qrect))
29124 {
29125 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29126 Lisp_Object rect = XCDR (hot_spot);
29127 Lisp_Object tem;
29128 if (!CONSP (rect))
29129 return false;
29130 if (!CONSP (XCAR (rect)))
29131 return false;
29132 if (!CONSP (XCDR (rect)))
29133 return false;
29134 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29135 return false;
29136 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29137 return false;
29138 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29139 return false;
29140 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29141 return false;
29142 return true;
29143 }
29144 else if (EQ (XCAR (hot_spot), Qcircle))
29145 {
29146 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29147 Lisp_Object circ = XCDR (hot_spot);
29148 Lisp_Object lr, lx0, ly0;
29149 if (CONSP (circ)
29150 && CONSP (XCAR (circ))
29151 && (lr = XCDR (circ), NUMBERP (lr))
29152 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29153 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29154 {
29155 double r = XFLOATINT (lr);
29156 double dx = XINT (lx0) - x;
29157 double dy = XINT (ly0) - y;
29158 return (dx * dx + dy * dy <= r * r);
29159 }
29160 }
29161 else if (EQ (XCAR (hot_spot), Qpoly))
29162 {
29163 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29164 if (VECTORP (XCDR (hot_spot)))
29165 {
29166 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29167 Lisp_Object *poly = v->contents;
29168 ptrdiff_t n = v->header.size;
29169 ptrdiff_t i;
29170 bool inside = false;
29171 Lisp_Object lx, ly;
29172 int x0, y0;
29173
29174 /* Need an even number of coordinates, and at least 3 edges. */
29175 if (n < 6 || n & 1)
29176 return false;
29177
29178 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29179 If count is odd, we are inside polygon. Pixels on edges
29180 may or may not be included depending on actual geometry of the
29181 polygon. */
29182 if ((lx = poly[n-2], !INTEGERP (lx))
29183 || (ly = poly[n-1], !INTEGERP (lx)))
29184 return false;
29185 x0 = XINT (lx), y0 = XINT (ly);
29186 for (i = 0; i < n; i += 2)
29187 {
29188 int x1 = x0, y1 = y0;
29189 if ((lx = poly[i], !INTEGERP (lx))
29190 || (ly = poly[i+1], !INTEGERP (ly)))
29191 return false;
29192 x0 = XINT (lx), y0 = XINT (ly);
29193
29194 /* Does this segment cross the X line? */
29195 if (x0 >= x)
29196 {
29197 if (x1 >= x)
29198 continue;
29199 }
29200 else if (x1 < x)
29201 continue;
29202 if (y > y0 && y > y1)
29203 continue;
29204 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29205 inside = !inside;
29206 }
29207 return inside;
29208 }
29209 }
29210 return false;
29211 }
29212
29213 Lisp_Object
29214 find_hot_spot (Lisp_Object map, int x, int y)
29215 {
29216 while (CONSP (map))
29217 {
29218 if (CONSP (XCAR (map))
29219 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29220 return XCAR (map);
29221 map = XCDR (map);
29222 }
29223
29224 return Qnil;
29225 }
29226
29227 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29228 3, 3, 0,
29229 doc: /* Lookup in image map MAP coordinates X and Y.
29230 An image map is an alist where each element has the format (AREA ID PLIST).
29231 An AREA is specified as either a rectangle, a circle, or a polygon:
29232 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29233 pixel coordinates of the upper left and bottom right corners.
29234 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29235 and the radius of the circle; r may be a float or integer.
29236 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29237 vector describes one corner in the polygon.
29238 Returns the alist element for the first matching AREA in MAP. */)
29239 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29240 {
29241 if (NILP (map))
29242 return Qnil;
29243
29244 CHECK_NUMBER (x);
29245 CHECK_NUMBER (y);
29246
29247 return find_hot_spot (map,
29248 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29249 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29250 }
29251
29252
29253 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29254 static void
29255 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29256 {
29257 /* Do not change cursor shape while dragging mouse. */
29258 if (EQ (do_mouse_tracking, Qdragging))
29259 return;
29260
29261 if (!NILP (pointer))
29262 {
29263 if (EQ (pointer, Qarrow))
29264 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29265 else if (EQ (pointer, Qhand))
29266 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29267 else if (EQ (pointer, Qtext))
29268 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29269 else if (EQ (pointer, intern ("hdrag")))
29270 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29271 else if (EQ (pointer, intern ("nhdrag")))
29272 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29273 #ifdef HAVE_X_WINDOWS
29274 else if (EQ (pointer, intern ("vdrag")))
29275 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29276 #endif
29277 else if (EQ (pointer, intern ("hourglass")))
29278 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29279 else if (EQ (pointer, Qmodeline))
29280 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29281 else
29282 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29283 }
29284
29285 if (cursor != No_Cursor)
29286 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29287 }
29288
29289 #endif /* HAVE_WINDOW_SYSTEM */
29290
29291 /* Take proper action when mouse has moved to the mode or header line
29292 or marginal area AREA of window W, x-position X and y-position Y.
29293 X is relative to the start of the text display area of W, so the
29294 width of bitmap areas and scroll bars must be subtracted to get a
29295 position relative to the start of the mode line. */
29296
29297 static void
29298 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29299 enum window_part area)
29300 {
29301 struct window *w = XWINDOW (window);
29302 struct frame *f = XFRAME (w->frame);
29303 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29304 #ifdef HAVE_WINDOW_SYSTEM
29305 Display_Info *dpyinfo;
29306 #endif
29307 Cursor cursor = No_Cursor;
29308 Lisp_Object pointer = Qnil;
29309 int dx, dy, width, height;
29310 ptrdiff_t charpos;
29311 Lisp_Object string, object = Qnil;
29312 Lisp_Object pos IF_LINT (= Qnil), help;
29313
29314 Lisp_Object mouse_face;
29315 int original_x_pixel = x;
29316 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29317 struct glyph_row *row IF_LINT (= 0);
29318
29319 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29320 {
29321 int x0;
29322 struct glyph *end;
29323
29324 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29325 returns them in row/column units! */
29326 string = mode_line_string (w, area, &x, &y, &charpos,
29327 &object, &dx, &dy, &width, &height);
29328
29329 row = (area == ON_MODE_LINE
29330 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29331 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29332
29333 /* Find the glyph under the mouse pointer. */
29334 if (row->mode_line_p && row->enabled_p)
29335 {
29336 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29337 end = glyph + row->used[TEXT_AREA];
29338
29339 for (x0 = original_x_pixel;
29340 glyph < end && x0 >= glyph->pixel_width;
29341 ++glyph)
29342 x0 -= glyph->pixel_width;
29343
29344 if (glyph >= end)
29345 glyph = NULL;
29346 }
29347 }
29348 else
29349 {
29350 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29351 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29352 returns them in row/column units! */
29353 string = marginal_area_string (w, area, &x, &y, &charpos,
29354 &object, &dx, &dy, &width, &height);
29355 }
29356
29357 help = Qnil;
29358
29359 #ifdef HAVE_WINDOW_SYSTEM
29360 if (IMAGEP (object))
29361 {
29362 Lisp_Object image_map, hotspot;
29363 if ((image_map = Fplist_get (XCDR (object), QCmap),
29364 !NILP (image_map))
29365 && (hotspot = find_hot_spot (image_map, dx, dy),
29366 CONSP (hotspot))
29367 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29368 {
29369 Lisp_Object plist;
29370
29371 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29372 If so, we could look for mouse-enter, mouse-leave
29373 properties in PLIST (and do something...). */
29374 hotspot = XCDR (hotspot);
29375 if (CONSP (hotspot)
29376 && (plist = XCAR (hotspot), CONSP (plist)))
29377 {
29378 pointer = Fplist_get (plist, Qpointer);
29379 if (NILP (pointer))
29380 pointer = Qhand;
29381 help = Fplist_get (plist, Qhelp_echo);
29382 if (!NILP (help))
29383 {
29384 help_echo_string = help;
29385 XSETWINDOW (help_echo_window, w);
29386 help_echo_object = w->contents;
29387 help_echo_pos = charpos;
29388 }
29389 }
29390 }
29391 if (NILP (pointer))
29392 pointer = Fplist_get (XCDR (object), QCpointer);
29393 }
29394 #endif /* HAVE_WINDOW_SYSTEM */
29395
29396 if (STRINGP (string))
29397 pos = make_number (charpos);
29398
29399 /* Set the help text and mouse pointer. If the mouse is on a part
29400 of the mode line without any text (e.g. past the right edge of
29401 the mode line text), use the default help text and pointer. */
29402 if (STRINGP (string) || area == ON_MODE_LINE)
29403 {
29404 /* Arrange to display the help by setting the global variables
29405 help_echo_string, help_echo_object, and help_echo_pos. */
29406 if (NILP (help))
29407 {
29408 if (STRINGP (string))
29409 help = Fget_text_property (pos, Qhelp_echo, string);
29410
29411 if (!NILP (help))
29412 {
29413 help_echo_string = help;
29414 XSETWINDOW (help_echo_window, w);
29415 help_echo_object = string;
29416 help_echo_pos = charpos;
29417 }
29418 else if (area == ON_MODE_LINE)
29419 {
29420 Lisp_Object default_help
29421 = buffer_local_value (Qmode_line_default_help_echo,
29422 w->contents);
29423
29424 if (STRINGP (default_help))
29425 {
29426 help_echo_string = default_help;
29427 XSETWINDOW (help_echo_window, w);
29428 help_echo_object = Qnil;
29429 help_echo_pos = -1;
29430 }
29431 }
29432 }
29433
29434 #ifdef HAVE_WINDOW_SYSTEM
29435 /* Change the mouse pointer according to what is under it. */
29436 if (FRAME_WINDOW_P (f))
29437 {
29438 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29439 || minibuf_level
29440 || NILP (Vresize_mini_windows));
29441
29442 dpyinfo = FRAME_DISPLAY_INFO (f);
29443 if (STRINGP (string))
29444 {
29445 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29446
29447 if (NILP (pointer))
29448 pointer = Fget_text_property (pos, Qpointer, string);
29449
29450 /* Change the mouse pointer according to what is under X/Y. */
29451 if (NILP (pointer)
29452 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29453 {
29454 Lisp_Object map;
29455 map = Fget_text_property (pos, Qlocal_map, string);
29456 if (!KEYMAPP (map))
29457 map = Fget_text_property (pos, Qkeymap, string);
29458 if (!KEYMAPP (map) && draggable)
29459 cursor = dpyinfo->vertical_scroll_bar_cursor;
29460 }
29461 }
29462 else if (draggable)
29463 /* Default mode-line pointer. */
29464 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29465 }
29466 #endif
29467 }
29468
29469 /* Change the mouse face according to what is under X/Y. */
29470 bool mouse_face_shown = false;
29471 if (STRINGP (string))
29472 {
29473 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29474 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29475 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29476 && glyph)
29477 {
29478 Lisp_Object b, e;
29479
29480 struct glyph * tmp_glyph;
29481
29482 int gpos;
29483 int gseq_length;
29484 int total_pixel_width;
29485 ptrdiff_t begpos, endpos, ignore;
29486
29487 int vpos, hpos;
29488
29489 b = Fprevious_single_property_change (make_number (charpos + 1),
29490 Qmouse_face, string, Qnil);
29491 if (NILP (b))
29492 begpos = 0;
29493 else
29494 begpos = XINT (b);
29495
29496 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29497 if (NILP (e))
29498 endpos = SCHARS (string);
29499 else
29500 endpos = XINT (e);
29501
29502 /* Calculate the glyph position GPOS of GLYPH in the
29503 displayed string, relative to the beginning of the
29504 highlighted part of the string.
29505
29506 Note: GPOS is different from CHARPOS. CHARPOS is the
29507 position of GLYPH in the internal string object. A mode
29508 line string format has structures which are converted to
29509 a flattened string by the Emacs Lisp interpreter. The
29510 internal string is an element of those structures. The
29511 displayed string is the flattened string. */
29512 tmp_glyph = row_start_glyph;
29513 while (tmp_glyph < glyph
29514 && (!(EQ (tmp_glyph->object, glyph->object)
29515 && begpos <= tmp_glyph->charpos
29516 && tmp_glyph->charpos < endpos)))
29517 tmp_glyph++;
29518 gpos = glyph - tmp_glyph;
29519
29520 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29521 the highlighted part of the displayed string to which
29522 GLYPH belongs. Note: GSEQ_LENGTH is different from
29523 SCHARS (STRING), because the latter returns the length of
29524 the internal string. */
29525 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29526 tmp_glyph > glyph
29527 && (!(EQ (tmp_glyph->object, glyph->object)
29528 && begpos <= tmp_glyph->charpos
29529 && tmp_glyph->charpos < endpos));
29530 tmp_glyph--)
29531 ;
29532 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29533
29534 /* Calculate the total pixel width of all the glyphs between
29535 the beginning of the highlighted area and GLYPH. */
29536 total_pixel_width = 0;
29537 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29538 total_pixel_width += tmp_glyph->pixel_width;
29539
29540 /* Pre calculation of re-rendering position. Note: X is in
29541 column units here, after the call to mode_line_string or
29542 marginal_area_string. */
29543 hpos = x - gpos;
29544 vpos = (area == ON_MODE_LINE
29545 ? (w->current_matrix)->nrows - 1
29546 : 0);
29547
29548 /* If GLYPH's position is included in the region that is
29549 already drawn in mouse face, we have nothing to do. */
29550 if ( EQ (window, hlinfo->mouse_face_window)
29551 && (!row->reversed_p
29552 ? (hlinfo->mouse_face_beg_col <= hpos
29553 && hpos < hlinfo->mouse_face_end_col)
29554 /* In R2L rows we swap BEG and END, see below. */
29555 : (hlinfo->mouse_face_end_col <= hpos
29556 && hpos < hlinfo->mouse_face_beg_col))
29557 && hlinfo->mouse_face_beg_row == vpos )
29558 return;
29559
29560 if (clear_mouse_face (hlinfo))
29561 cursor = No_Cursor;
29562
29563 if (!row->reversed_p)
29564 {
29565 hlinfo->mouse_face_beg_col = hpos;
29566 hlinfo->mouse_face_beg_x = original_x_pixel
29567 - (total_pixel_width + dx);
29568 hlinfo->mouse_face_end_col = hpos + gseq_length;
29569 hlinfo->mouse_face_end_x = 0;
29570 }
29571 else
29572 {
29573 /* In R2L rows, show_mouse_face expects BEG and END
29574 coordinates to be swapped. */
29575 hlinfo->mouse_face_end_col = hpos;
29576 hlinfo->mouse_face_end_x = original_x_pixel
29577 - (total_pixel_width + dx);
29578 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29579 hlinfo->mouse_face_beg_x = 0;
29580 }
29581
29582 hlinfo->mouse_face_beg_row = vpos;
29583 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29584 hlinfo->mouse_face_past_end = false;
29585 hlinfo->mouse_face_window = window;
29586
29587 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29588 charpos,
29589 0, &ignore,
29590 glyph->face_id,
29591 true);
29592 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29593 mouse_face_shown = true;
29594
29595 if (NILP (pointer))
29596 pointer = Qhand;
29597 }
29598 }
29599
29600 /* If mouse-face doesn't need to be shown, clear any existing
29601 mouse-face. */
29602 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
29603 clear_mouse_face (hlinfo);
29604
29605 #ifdef HAVE_WINDOW_SYSTEM
29606 if (FRAME_WINDOW_P (f))
29607 define_frame_cursor1 (f, cursor, pointer);
29608 #endif
29609 }
29610
29611
29612 /* EXPORT:
29613 Take proper action when the mouse has moved to position X, Y on
29614 frame F with regards to highlighting portions of display that have
29615 mouse-face properties. Also de-highlight portions of display where
29616 the mouse was before, set the mouse pointer shape as appropriate
29617 for the mouse coordinates, and activate help echo (tooltips).
29618 X and Y can be negative or out of range. */
29619
29620 void
29621 note_mouse_highlight (struct frame *f, int x, int y)
29622 {
29623 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29624 enum window_part part = ON_NOTHING;
29625 Lisp_Object window;
29626 struct window *w;
29627 Cursor cursor = No_Cursor;
29628 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29629 struct buffer *b;
29630
29631 /* When a menu is active, don't highlight because this looks odd. */
29632 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29633 if (popup_activated ())
29634 return;
29635 #endif
29636
29637 if (!f->glyphs_initialized_p
29638 || f->pointer_invisible)
29639 return;
29640
29641 hlinfo->mouse_face_mouse_x = x;
29642 hlinfo->mouse_face_mouse_y = y;
29643 hlinfo->mouse_face_mouse_frame = f;
29644
29645 if (hlinfo->mouse_face_defer)
29646 return;
29647
29648 /* Which window is that in? */
29649 window = window_from_coordinates (f, x, y, &part, true);
29650
29651 /* If displaying active text in another window, clear that. */
29652 if (! EQ (window, hlinfo->mouse_face_window)
29653 /* Also clear if we move out of text area in same window. */
29654 || (!NILP (hlinfo->mouse_face_window)
29655 && !NILP (window)
29656 && part != ON_TEXT
29657 && part != ON_MODE_LINE
29658 && part != ON_HEADER_LINE))
29659 clear_mouse_face (hlinfo);
29660
29661 /* Not on a window -> return. */
29662 if (!WINDOWP (window))
29663 return;
29664
29665 /* Reset help_echo_string. It will get recomputed below. */
29666 help_echo_string = Qnil;
29667
29668 /* Convert to window-relative pixel coordinates. */
29669 w = XWINDOW (window);
29670 frame_to_window_pixel_xy (w, &x, &y);
29671
29672 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29673 /* Handle tool-bar window differently since it doesn't display a
29674 buffer. */
29675 if (EQ (window, f->tool_bar_window))
29676 {
29677 note_tool_bar_highlight (f, x, y);
29678 return;
29679 }
29680 #endif
29681
29682 /* Mouse is on the mode, header line or margin? */
29683 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29684 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29685 {
29686 note_mode_line_or_margin_highlight (window, x, y, part);
29687
29688 #ifdef HAVE_WINDOW_SYSTEM
29689 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29690 {
29691 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29692 /* Show non-text cursor (Bug#16647). */
29693 goto set_cursor;
29694 }
29695 else
29696 #endif
29697 return;
29698 }
29699
29700 #ifdef HAVE_WINDOW_SYSTEM
29701 if (part == ON_VERTICAL_BORDER)
29702 {
29703 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29704 help_echo_string = build_string ("drag-mouse-1: resize");
29705 }
29706 else if (part == ON_RIGHT_DIVIDER)
29707 {
29708 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29709 help_echo_string = build_string ("drag-mouse-1: resize");
29710 }
29711 else if (part == ON_BOTTOM_DIVIDER)
29712 if (! WINDOW_BOTTOMMOST_P (w)
29713 || minibuf_level
29714 || NILP (Vresize_mini_windows))
29715 {
29716 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29717 help_echo_string = build_string ("drag-mouse-1: resize");
29718 }
29719 else
29720 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29721 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29722 || part == ON_VERTICAL_SCROLL_BAR
29723 || part == ON_HORIZONTAL_SCROLL_BAR)
29724 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29725 else
29726 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29727 #endif
29728
29729 /* Are we in a window whose display is up to date?
29730 And verify the buffer's text has not changed. */
29731 b = XBUFFER (w->contents);
29732 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29733 {
29734 int hpos, vpos, dx, dy, area = LAST_AREA;
29735 ptrdiff_t pos;
29736 struct glyph *glyph;
29737 Lisp_Object object;
29738 Lisp_Object mouse_face = Qnil, position;
29739 Lisp_Object *overlay_vec = NULL;
29740 ptrdiff_t i, noverlays;
29741 struct buffer *obuf;
29742 ptrdiff_t obegv, ozv;
29743 bool same_region;
29744
29745 /* Find the glyph under X/Y. */
29746 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29747
29748 #ifdef HAVE_WINDOW_SYSTEM
29749 /* Look for :pointer property on image. */
29750 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29751 {
29752 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29753 if (img != NULL && IMAGEP (img->spec))
29754 {
29755 Lisp_Object image_map, hotspot;
29756 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29757 !NILP (image_map))
29758 && (hotspot = find_hot_spot (image_map,
29759 glyph->slice.img.x + dx,
29760 glyph->slice.img.y + dy),
29761 CONSP (hotspot))
29762 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29763 {
29764 Lisp_Object plist;
29765
29766 /* Could check XCAR (hotspot) to see if we enter/leave
29767 this hot-spot.
29768 If so, we could look for mouse-enter, mouse-leave
29769 properties in PLIST (and do something...). */
29770 hotspot = XCDR (hotspot);
29771 if (CONSP (hotspot)
29772 && (plist = XCAR (hotspot), CONSP (plist)))
29773 {
29774 pointer = Fplist_get (plist, Qpointer);
29775 if (NILP (pointer))
29776 pointer = Qhand;
29777 help_echo_string = Fplist_get (plist, Qhelp_echo);
29778 if (!NILP (help_echo_string))
29779 {
29780 help_echo_window = window;
29781 help_echo_object = glyph->object;
29782 help_echo_pos = glyph->charpos;
29783 }
29784 }
29785 }
29786 if (NILP (pointer))
29787 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29788 }
29789 }
29790 #endif /* HAVE_WINDOW_SYSTEM */
29791
29792 /* Clear mouse face if X/Y not over text. */
29793 if (glyph == NULL
29794 || area != TEXT_AREA
29795 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29796 /* Glyph's OBJECT is nil for glyphs inserted by the
29797 display engine for its internal purposes, like truncation
29798 and continuation glyphs and blanks beyond the end of
29799 line's text on text terminals. If we are over such a
29800 glyph, we are not over any text. */
29801 || NILP (glyph->object)
29802 /* R2L rows have a stretch glyph at their front, which
29803 stands for no text, whereas L2R rows have no glyphs at
29804 all beyond the end of text. Treat such stretch glyphs
29805 like we do with NULL glyphs in L2R rows. */
29806 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29807 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29808 && glyph->type == STRETCH_GLYPH
29809 && glyph->avoid_cursor_p))
29810 {
29811 if (clear_mouse_face (hlinfo))
29812 cursor = No_Cursor;
29813 #ifdef HAVE_WINDOW_SYSTEM
29814 if (FRAME_WINDOW_P (f) && NILP (pointer))
29815 {
29816 if (area != TEXT_AREA)
29817 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29818 else
29819 pointer = Vvoid_text_area_pointer;
29820 }
29821 #endif
29822 goto set_cursor;
29823 }
29824
29825 pos = glyph->charpos;
29826 object = glyph->object;
29827 if (!STRINGP (object) && !BUFFERP (object))
29828 goto set_cursor;
29829
29830 /* If we get an out-of-range value, return now; avoid an error. */
29831 if (BUFFERP (object) && pos > BUF_Z (b))
29832 goto set_cursor;
29833
29834 /* Make the window's buffer temporarily current for
29835 overlays_at and compute_char_face. */
29836 obuf = current_buffer;
29837 current_buffer = b;
29838 obegv = BEGV;
29839 ozv = ZV;
29840 BEGV = BEG;
29841 ZV = Z;
29842
29843 /* Is this char mouse-active or does it have help-echo? */
29844 position = make_number (pos);
29845
29846 USE_SAFE_ALLOCA;
29847
29848 if (BUFFERP (object))
29849 {
29850 /* Put all the overlays we want in a vector in overlay_vec. */
29851 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
29852 /* Sort overlays into increasing priority order. */
29853 noverlays = sort_overlays (overlay_vec, noverlays, w);
29854 }
29855 else
29856 noverlays = 0;
29857
29858 if (NILP (Vmouse_highlight))
29859 {
29860 clear_mouse_face (hlinfo);
29861 goto check_help_echo;
29862 }
29863
29864 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29865
29866 if (same_region)
29867 cursor = No_Cursor;
29868
29869 /* Check mouse-face highlighting. */
29870 if (! same_region
29871 /* If there exists an overlay with mouse-face overlapping
29872 the one we are currently highlighting, we have to
29873 check if we enter the overlapping overlay, and then
29874 highlight only that. */
29875 || (OVERLAYP (hlinfo->mouse_face_overlay)
29876 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29877 {
29878 /* Find the highest priority overlay with a mouse-face. */
29879 Lisp_Object overlay = Qnil;
29880 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29881 {
29882 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29883 if (!NILP (mouse_face))
29884 overlay = overlay_vec[i];
29885 }
29886
29887 /* If we're highlighting the same overlay as before, there's
29888 no need to do that again. */
29889 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29890 goto check_help_echo;
29891 hlinfo->mouse_face_overlay = overlay;
29892
29893 /* Clear the display of the old active region, if any. */
29894 if (clear_mouse_face (hlinfo))
29895 cursor = No_Cursor;
29896
29897 /* If no overlay applies, get a text property. */
29898 if (NILP (overlay))
29899 mouse_face = Fget_text_property (position, Qmouse_face, object);
29900
29901 /* Next, compute the bounds of the mouse highlighting and
29902 display it. */
29903 if (!NILP (mouse_face) && STRINGP (object))
29904 {
29905 /* The mouse-highlighting comes from a display string
29906 with a mouse-face. */
29907 Lisp_Object s, e;
29908 ptrdiff_t ignore;
29909
29910 s = Fprevious_single_property_change
29911 (make_number (pos + 1), Qmouse_face, object, Qnil);
29912 e = Fnext_single_property_change
29913 (position, Qmouse_face, object, Qnil);
29914 if (NILP (s))
29915 s = make_number (0);
29916 if (NILP (e))
29917 e = make_number (SCHARS (object));
29918 mouse_face_from_string_pos (w, hlinfo, object,
29919 XINT (s), XINT (e));
29920 hlinfo->mouse_face_past_end = false;
29921 hlinfo->mouse_face_window = window;
29922 hlinfo->mouse_face_face_id
29923 = face_at_string_position (w, object, pos, 0, &ignore,
29924 glyph->face_id, true);
29925 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29926 cursor = No_Cursor;
29927 }
29928 else
29929 {
29930 /* The mouse-highlighting, if any, comes from an overlay
29931 or text property in the buffer. */
29932 Lisp_Object buffer IF_LINT (= Qnil);
29933 Lisp_Object disp_string IF_LINT (= Qnil);
29934
29935 if (STRINGP (object))
29936 {
29937 /* If we are on a display string with no mouse-face,
29938 check if the text under it has one. */
29939 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29940 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29941 pos = string_buffer_position (object, start);
29942 if (pos > 0)
29943 {
29944 mouse_face = get_char_property_and_overlay
29945 (make_number (pos), Qmouse_face, w->contents, &overlay);
29946 buffer = w->contents;
29947 disp_string = object;
29948 }
29949 }
29950 else
29951 {
29952 buffer = object;
29953 disp_string = Qnil;
29954 }
29955
29956 if (!NILP (mouse_face))
29957 {
29958 Lisp_Object before, after;
29959 Lisp_Object before_string, after_string;
29960 /* To correctly find the limits of mouse highlight
29961 in a bidi-reordered buffer, we must not use the
29962 optimization of limiting the search in
29963 previous-single-property-change and
29964 next-single-property-change, because
29965 rows_from_pos_range needs the real start and end
29966 positions to DTRT in this case. That's because
29967 the first row visible in a window does not
29968 necessarily display the character whose position
29969 is the smallest. */
29970 Lisp_Object lim1
29971 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29972 ? Fmarker_position (w->start)
29973 : Qnil;
29974 Lisp_Object lim2
29975 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29976 ? make_number (BUF_Z (XBUFFER (buffer))
29977 - w->window_end_pos)
29978 : Qnil;
29979
29980 if (NILP (overlay))
29981 {
29982 /* Handle the text property case. */
29983 before = Fprevious_single_property_change
29984 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29985 after = Fnext_single_property_change
29986 (make_number (pos), Qmouse_face, buffer, lim2);
29987 before_string = after_string = Qnil;
29988 }
29989 else
29990 {
29991 /* Handle the overlay case. */
29992 before = Foverlay_start (overlay);
29993 after = Foverlay_end (overlay);
29994 before_string = Foverlay_get (overlay, Qbefore_string);
29995 after_string = Foverlay_get (overlay, Qafter_string);
29996
29997 if (!STRINGP (before_string)) before_string = Qnil;
29998 if (!STRINGP (after_string)) after_string = Qnil;
29999 }
30000
30001 mouse_face_from_buffer_pos (window, hlinfo, pos,
30002 NILP (before)
30003 ? 1
30004 : XFASTINT (before),
30005 NILP (after)
30006 ? BUF_Z (XBUFFER (buffer))
30007 : XFASTINT (after),
30008 before_string, after_string,
30009 disp_string);
30010 cursor = No_Cursor;
30011 }
30012 }
30013 }
30014
30015 check_help_echo:
30016
30017 /* Look for a `help-echo' property. */
30018 if (NILP (help_echo_string)) {
30019 Lisp_Object help, overlay;
30020
30021 /* Check overlays first. */
30022 help = overlay = Qnil;
30023 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30024 {
30025 overlay = overlay_vec[i];
30026 help = Foverlay_get (overlay, Qhelp_echo);
30027 }
30028
30029 if (!NILP (help))
30030 {
30031 help_echo_string = help;
30032 help_echo_window = window;
30033 help_echo_object = overlay;
30034 help_echo_pos = pos;
30035 }
30036 else
30037 {
30038 Lisp_Object obj = glyph->object;
30039 ptrdiff_t charpos = glyph->charpos;
30040
30041 /* Try text properties. */
30042 if (STRINGP (obj)
30043 && charpos >= 0
30044 && charpos < SCHARS (obj))
30045 {
30046 help = Fget_text_property (make_number (charpos),
30047 Qhelp_echo, obj);
30048 if (NILP (help))
30049 {
30050 /* If the string itself doesn't specify a help-echo,
30051 see if the buffer text ``under'' it does. */
30052 struct glyph_row *r
30053 = MATRIX_ROW (w->current_matrix, vpos);
30054 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30055 ptrdiff_t p = string_buffer_position (obj, start);
30056 if (p > 0)
30057 {
30058 help = Fget_char_property (make_number (p),
30059 Qhelp_echo, w->contents);
30060 if (!NILP (help))
30061 {
30062 charpos = p;
30063 obj = w->contents;
30064 }
30065 }
30066 }
30067 }
30068 else if (BUFFERP (obj)
30069 && charpos >= BEGV
30070 && charpos < ZV)
30071 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30072 obj);
30073
30074 if (!NILP (help))
30075 {
30076 help_echo_string = help;
30077 help_echo_window = window;
30078 help_echo_object = obj;
30079 help_echo_pos = charpos;
30080 }
30081 }
30082 }
30083
30084 #ifdef HAVE_WINDOW_SYSTEM
30085 /* Look for a `pointer' property. */
30086 if (FRAME_WINDOW_P (f) && NILP (pointer))
30087 {
30088 /* Check overlays first. */
30089 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30090 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30091
30092 if (NILP (pointer))
30093 {
30094 Lisp_Object obj = glyph->object;
30095 ptrdiff_t charpos = glyph->charpos;
30096
30097 /* Try text properties. */
30098 if (STRINGP (obj)
30099 && charpos >= 0
30100 && charpos < SCHARS (obj))
30101 {
30102 pointer = Fget_text_property (make_number (charpos),
30103 Qpointer, obj);
30104 if (NILP (pointer))
30105 {
30106 /* If the string itself doesn't specify a pointer,
30107 see if the buffer text ``under'' it does. */
30108 struct glyph_row *r
30109 = MATRIX_ROW (w->current_matrix, vpos);
30110 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30111 ptrdiff_t p = string_buffer_position (obj, start);
30112 if (p > 0)
30113 pointer = Fget_char_property (make_number (p),
30114 Qpointer, w->contents);
30115 }
30116 }
30117 else if (BUFFERP (obj)
30118 && charpos >= BEGV
30119 && charpos < ZV)
30120 pointer = Fget_text_property (make_number (charpos),
30121 Qpointer, obj);
30122 }
30123 }
30124 #endif /* HAVE_WINDOW_SYSTEM */
30125
30126 BEGV = obegv;
30127 ZV = ozv;
30128 current_buffer = obuf;
30129 SAFE_FREE ();
30130 }
30131
30132 set_cursor:
30133
30134 #ifdef HAVE_WINDOW_SYSTEM
30135 if (FRAME_WINDOW_P (f))
30136 define_frame_cursor1 (f, cursor, pointer);
30137 #else
30138 /* This is here to prevent a compiler error, about "label at end of
30139 compound statement". */
30140 return;
30141 #endif
30142 }
30143
30144
30145 /* EXPORT for RIF:
30146 Clear any mouse-face on window W. This function is part of the
30147 redisplay interface, and is called from try_window_id and similar
30148 functions to ensure the mouse-highlight is off. */
30149
30150 void
30151 x_clear_window_mouse_face (struct window *w)
30152 {
30153 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30154 Lisp_Object window;
30155
30156 block_input ();
30157 XSETWINDOW (window, w);
30158 if (EQ (window, hlinfo->mouse_face_window))
30159 clear_mouse_face (hlinfo);
30160 unblock_input ();
30161 }
30162
30163
30164 /* EXPORT:
30165 Just discard the mouse face information for frame F, if any.
30166 This is used when the size of F is changed. */
30167
30168 void
30169 cancel_mouse_face (struct frame *f)
30170 {
30171 Lisp_Object window;
30172 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30173
30174 window = hlinfo->mouse_face_window;
30175 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30176 reset_mouse_highlight (hlinfo);
30177 }
30178
30179
30180 \f
30181 /***********************************************************************
30182 Exposure Events
30183 ***********************************************************************/
30184
30185 #ifdef HAVE_WINDOW_SYSTEM
30186
30187 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30188 which intersects rectangle R. R is in window-relative coordinates. */
30189
30190 static void
30191 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30192 enum glyph_row_area area)
30193 {
30194 struct glyph *first = row->glyphs[area];
30195 struct glyph *end = row->glyphs[area] + row->used[area];
30196 struct glyph *last;
30197 int first_x, start_x, x;
30198
30199 if (area == TEXT_AREA && row->fill_line_p)
30200 /* If row extends face to end of line write the whole line. */
30201 draw_glyphs (w, 0, row, area,
30202 0, row->used[area],
30203 DRAW_NORMAL_TEXT, 0);
30204 else
30205 {
30206 /* Set START_X to the window-relative start position for drawing glyphs of
30207 AREA. The first glyph of the text area can be partially visible.
30208 The first glyphs of other areas cannot. */
30209 start_x = window_box_left_offset (w, area);
30210 x = start_x;
30211 if (area == TEXT_AREA)
30212 x += row->x;
30213
30214 /* Find the first glyph that must be redrawn. */
30215 while (first < end
30216 && x + first->pixel_width < r->x)
30217 {
30218 x += first->pixel_width;
30219 ++first;
30220 }
30221
30222 /* Find the last one. */
30223 last = first;
30224 first_x = x;
30225 /* Use a signed int intermediate value to avoid catastrophic
30226 failures due to comparison between signed and unsigned, when
30227 x is negative (can happen for wide images that are hscrolled). */
30228 int r_end = r->x + r->width;
30229 while (last < end && x < r_end)
30230 {
30231 x += last->pixel_width;
30232 ++last;
30233 }
30234
30235 /* Repaint. */
30236 if (last > first)
30237 draw_glyphs (w, first_x - start_x, row, area,
30238 first - row->glyphs[area], last - row->glyphs[area],
30239 DRAW_NORMAL_TEXT, 0);
30240 }
30241 }
30242
30243
30244 /* Redraw the parts of the glyph row ROW on window W intersecting
30245 rectangle R. R is in window-relative coordinates. Value is
30246 true if mouse-face was overwritten. */
30247
30248 static bool
30249 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30250 {
30251 eassert (row->enabled_p);
30252
30253 if (row->mode_line_p || w->pseudo_window_p)
30254 draw_glyphs (w, 0, row, TEXT_AREA,
30255 0, row->used[TEXT_AREA],
30256 DRAW_NORMAL_TEXT, 0);
30257 else
30258 {
30259 if (row->used[LEFT_MARGIN_AREA])
30260 expose_area (w, row, r, LEFT_MARGIN_AREA);
30261 if (row->used[TEXT_AREA])
30262 expose_area (w, row, r, TEXT_AREA);
30263 if (row->used[RIGHT_MARGIN_AREA])
30264 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30265 draw_row_fringe_bitmaps (w, row);
30266 }
30267
30268 return row->mouse_face_p;
30269 }
30270
30271
30272 /* Redraw those parts of glyphs rows during expose event handling that
30273 overlap other rows. Redrawing of an exposed line writes over parts
30274 of lines overlapping that exposed line; this function fixes that.
30275
30276 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30277 row in W's current matrix that is exposed and overlaps other rows.
30278 LAST_OVERLAPPING_ROW is the last such row. */
30279
30280 static void
30281 expose_overlaps (struct window *w,
30282 struct glyph_row *first_overlapping_row,
30283 struct glyph_row *last_overlapping_row,
30284 XRectangle *r)
30285 {
30286 struct glyph_row *row;
30287
30288 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30289 if (row->overlapping_p)
30290 {
30291 eassert (row->enabled_p && !row->mode_line_p);
30292
30293 row->clip = r;
30294 if (row->used[LEFT_MARGIN_AREA])
30295 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30296
30297 if (row->used[TEXT_AREA])
30298 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30299
30300 if (row->used[RIGHT_MARGIN_AREA])
30301 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30302 row->clip = NULL;
30303 }
30304 }
30305
30306
30307 /* Return true if W's cursor intersects rectangle R. */
30308
30309 static bool
30310 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30311 {
30312 XRectangle cr, result;
30313 struct glyph *cursor_glyph;
30314 struct glyph_row *row;
30315
30316 if (w->phys_cursor.vpos >= 0
30317 && w->phys_cursor.vpos < w->current_matrix->nrows
30318 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30319 row->enabled_p)
30320 && row->cursor_in_fringe_p)
30321 {
30322 /* Cursor is in the fringe. */
30323 cr.x = window_box_right_offset (w,
30324 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30325 ? RIGHT_MARGIN_AREA
30326 : TEXT_AREA));
30327 cr.y = row->y;
30328 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30329 cr.height = row->height;
30330 return x_intersect_rectangles (&cr, r, &result);
30331 }
30332
30333 cursor_glyph = get_phys_cursor_glyph (w);
30334 if (cursor_glyph)
30335 {
30336 /* r is relative to W's box, but w->phys_cursor.x is relative
30337 to left edge of W's TEXT area. Adjust it. */
30338 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30339 cr.y = w->phys_cursor.y;
30340 cr.width = cursor_glyph->pixel_width;
30341 cr.height = w->phys_cursor_height;
30342 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30343 I assume the effect is the same -- and this is portable. */
30344 return x_intersect_rectangles (&cr, r, &result);
30345 }
30346 /* If we don't understand the format, pretend we're not in the hot-spot. */
30347 return false;
30348 }
30349
30350
30351 /* EXPORT:
30352 Draw a vertical window border to the right of window W if W doesn't
30353 have vertical scroll bars. */
30354
30355 void
30356 x_draw_vertical_border (struct window *w)
30357 {
30358 struct frame *f = XFRAME (WINDOW_FRAME (w));
30359
30360 /* We could do better, if we knew what type of scroll-bar the adjacent
30361 windows (on either side) have... But we don't :-(
30362 However, I think this works ok. ++KFS 2003-04-25 */
30363
30364 /* Redraw borders between horizontally adjacent windows. Don't
30365 do it for frames with vertical scroll bars because either the
30366 right scroll bar of a window, or the left scroll bar of its
30367 neighbor will suffice as a border. */
30368 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30369 return;
30370
30371 /* Note: It is necessary to redraw both the left and the right
30372 borders, for when only this single window W is being
30373 redisplayed. */
30374 if (!WINDOW_RIGHTMOST_P (w)
30375 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30376 {
30377 int x0, x1, y0, y1;
30378
30379 window_box_edges (w, &x0, &y0, &x1, &y1);
30380 y1 -= 1;
30381
30382 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30383 x1 -= 1;
30384
30385 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30386 }
30387
30388 if (!WINDOW_LEFTMOST_P (w)
30389 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30390 {
30391 int x0, x1, y0, y1;
30392
30393 window_box_edges (w, &x0, &y0, &x1, &y1);
30394 y1 -= 1;
30395
30396 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30397 x0 -= 1;
30398
30399 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30400 }
30401 }
30402
30403
30404 /* Draw window dividers for window W. */
30405
30406 void
30407 x_draw_right_divider (struct window *w)
30408 {
30409 struct frame *f = WINDOW_XFRAME (w);
30410
30411 if (w->mini || w->pseudo_window_p)
30412 return;
30413 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30414 {
30415 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30416 int x1 = WINDOW_RIGHT_EDGE_X (w);
30417 int y0 = WINDOW_TOP_EDGE_Y (w);
30418 /* The bottom divider prevails. */
30419 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30420
30421 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30422 }
30423 }
30424
30425 static void
30426 x_draw_bottom_divider (struct window *w)
30427 {
30428 struct frame *f = XFRAME (WINDOW_FRAME (w));
30429
30430 if (w->mini || w->pseudo_window_p)
30431 return;
30432 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30433 {
30434 int x0 = WINDOW_LEFT_EDGE_X (w);
30435 int x1 = WINDOW_RIGHT_EDGE_X (w);
30436 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30437 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30438
30439 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30440 }
30441 }
30442
30443 /* Redraw the part of window W intersection rectangle FR. Pixel
30444 coordinates in FR are frame-relative. Call this function with
30445 input blocked. Value is true if the exposure overwrites
30446 mouse-face. */
30447
30448 static bool
30449 expose_window (struct window *w, XRectangle *fr)
30450 {
30451 struct frame *f = XFRAME (w->frame);
30452 XRectangle wr, r;
30453 bool mouse_face_overwritten_p = false;
30454
30455 /* If window is not yet fully initialized, do nothing. This can
30456 happen when toolkit scroll bars are used and a window is split.
30457 Reconfiguring the scroll bar will generate an expose for a newly
30458 created window. */
30459 if (w->current_matrix == NULL)
30460 return false;
30461
30462 /* When we're currently updating the window, display and current
30463 matrix usually don't agree. Arrange for a thorough display
30464 later. */
30465 if (w->must_be_updated_p)
30466 {
30467 SET_FRAME_GARBAGED (f);
30468 return false;
30469 }
30470
30471 /* Frame-relative pixel rectangle of W. */
30472 wr.x = WINDOW_LEFT_EDGE_X (w);
30473 wr.y = WINDOW_TOP_EDGE_Y (w);
30474 wr.width = WINDOW_PIXEL_WIDTH (w);
30475 wr.height = WINDOW_PIXEL_HEIGHT (w);
30476
30477 if (x_intersect_rectangles (fr, &wr, &r))
30478 {
30479 int yb = window_text_bottom_y (w);
30480 struct glyph_row *row;
30481 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30482
30483 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30484 r.x, r.y, r.width, r.height));
30485
30486 /* Convert to window coordinates. */
30487 r.x -= WINDOW_LEFT_EDGE_X (w);
30488 r.y -= WINDOW_TOP_EDGE_Y (w);
30489
30490 /* Turn off the cursor. */
30491 bool cursor_cleared_p = (!w->pseudo_window_p
30492 && phys_cursor_in_rect_p (w, &r));
30493 if (cursor_cleared_p)
30494 x_clear_cursor (w);
30495
30496 /* If the row containing the cursor extends face to end of line,
30497 then expose_area might overwrite the cursor outside the
30498 rectangle and thus notice_overwritten_cursor might clear
30499 w->phys_cursor_on_p. We remember the original value and
30500 check later if it is changed. */
30501 bool phys_cursor_on_p = w->phys_cursor_on_p;
30502
30503 /* Use a signed int intermediate value to avoid catastrophic
30504 failures due to comparison between signed and unsigned, when
30505 y0 or y1 is negative (can happen for tall images). */
30506 int r_bottom = r.y + r.height;
30507
30508 /* Update lines intersecting rectangle R. */
30509 first_overlapping_row = last_overlapping_row = NULL;
30510 for (row = w->current_matrix->rows;
30511 row->enabled_p;
30512 ++row)
30513 {
30514 int y0 = row->y;
30515 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30516
30517 if ((y0 >= r.y && y0 < r_bottom)
30518 || (y1 > r.y && y1 < r_bottom)
30519 || (r.y >= y0 && r.y < y1)
30520 || (r_bottom > y0 && r_bottom < y1))
30521 {
30522 /* A header line may be overlapping, but there is no need
30523 to fix overlapping areas for them. KFS 2005-02-12 */
30524 if (row->overlapping_p && !row->mode_line_p)
30525 {
30526 if (first_overlapping_row == NULL)
30527 first_overlapping_row = row;
30528 last_overlapping_row = row;
30529 }
30530
30531 row->clip = fr;
30532 if (expose_line (w, row, &r))
30533 mouse_face_overwritten_p = true;
30534 row->clip = NULL;
30535 }
30536 else if (row->overlapping_p)
30537 {
30538 /* We must redraw a row overlapping the exposed area. */
30539 if (y0 < r.y
30540 ? y0 + row->phys_height > r.y
30541 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30542 {
30543 if (first_overlapping_row == NULL)
30544 first_overlapping_row = row;
30545 last_overlapping_row = row;
30546 }
30547 }
30548
30549 if (y1 >= yb)
30550 break;
30551 }
30552
30553 /* Display the mode line if there is one. */
30554 if (WINDOW_WANTS_MODELINE_P (w)
30555 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30556 row->enabled_p)
30557 && row->y < r_bottom)
30558 {
30559 if (expose_line (w, row, &r))
30560 mouse_face_overwritten_p = true;
30561 }
30562
30563 if (!w->pseudo_window_p)
30564 {
30565 /* Fix the display of overlapping rows. */
30566 if (first_overlapping_row)
30567 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30568 fr);
30569
30570 /* Draw border between windows. */
30571 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30572 x_draw_right_divider (w);
30573 else
30574 x_draw_vertical_border (w);
30575
30576 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30577 x_draw_bottom_divider (w);
30578
30579 /* Turn the cursor on again. */
30580 if (cursor_cleared_p
30581 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30582 update_window_cursor (w, true);
30583 }
30584 }
30585
30586 return mouse_face_overwritten_p;
30587 }
30588
30589
30590
30591 /* Redraw (parts) of all windows in the window tree rooted at W that
30592 intersect R. R contains frame pixel coordinates. Value is
30593 true if the exposure overwrites mouse-face. */
30594
30595 static bool
30596 expose_window_tree (struct window *w, XRectangle *r)
30597 {
30598 struct frame *f = XFRAME (w->frame);
30599 bool mouse_face_overwritten_p = false;
30600
30601 while (w && !FRAME_GARBAGED_P (f))
30602 {
30603 mouse_face_overwritten_p
30604 |= (WINDOWP (w->contents)
30605 ? expose_window_tree (XWINDOW (w->contents), r)
30606 : expose_window (w, r));
30607
30608 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30609 }
30610
30611 return mouse_face_overwritten_p;
30612 }
30613
30614
30615 /* EXPORT:
30616 Redisplay an exposed area of frame F. X and Y are the upper-left
30617 corner of the exposed rectangle. W and H are width and height of
30618 the exposed area. All are pixel values. W or H zero means redraw
30619 the entire frame. */
30620
30621 void
30622 expose_frame (struct frame *f, int x, int y, int w, int h)
30623 {
30624 XRectangle r;
30625 bool mouse_face_overwritten_p = false;
30626
30627 TRACE ((stderr, "expose_frame "));
30628
30629 /* No need to redraw if frame will be redrawn soon. */
30630 if (FRAME_GARBAGED_P (f))
30631 {
30632 TRACE ((stderr, " garbaged\n"));
30633 return;
30634 }
30635
30636 /* If basic faces haven't been realized yet, there is no point in
30637 trying to redraw anything. This can happen when we get an expose
30638 event while Emacs is starting, e.g. by moving another window. */
30639 if (FRAME_FACE_CACHE (f) == NULL
30640 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30641 {
30642 TRACE ((stderr, " no faces\n"));
30643 return;
30644 }
30645
30646 if (w == 0 || h == 0)
30647 {
30648 r.x = r.y = 0;
30649 r.width = FRAME_TEXT_WIDTH (f);
30650 r.height = FRAME_TEXT_HEIGHT (f);
30651 }
30652 else
30653 {
30654 r.x = x;
30655 r.y = y;
30656 r.width = w;
30657 r.height = h;
30658 }
30659
30660 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30661 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30662
30663 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30664 if (WINDOWP (f->tool_bar_window))
30665 mouse_face_overwritten_p
30666 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30667 #endif
30668
30669 #ifdef HAVE_X_WINDOWS
30670 #ifndef MSDOS
30671 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30672 if (WINDOWP (f->menu_bar_window))
30673 mouse_face_overwritten_p
30674 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30675 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30676 #endif
30677 #endif
30678
30679 /* Some window managers support a focus-follows-mouse style with
30680 delayed raising of frames. Imagine a partially obscured frame,
30681 and moving the mouse into partially obscured mouse-face on that
30682 frame. The visible part of the mouse-face will be highlighted,
30683 then the WM raises the obscured frame. With at least one WM, KDE
30684 2.1, Emacs is not getting any event for the raising of the frame
30685 (even tried with SubstructureRedirectMask), only Expose events.
30686 These expose events will draw text normally, i.e. not
30687 highlighted. Which means we must redo the highlight here.
30688 Subsume it under ``we love X''. --gerd 2001-08-15 */
30689 /* Included in Windows version because Windows most likely does not
30690 do the right thing if any third party tool offers
30691 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30692 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30693 {
30694 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30695 if (f == hlinfo->mouse_face_mouse_frame)
30696 {
30697 int mouse_x = hlinfo->mouse_face_mouse_x;
30698 int mouse_y = hlinfo->mouse_face_mouse_y;
30699 clear_mouse_face (hlinfo);
30700 note_mouse_highlight (f, mouse_x, mouse_y);
30701 }
30702 }
30703 }
30704
30705
30706 /* EXPORT:
30707 Determine the intersection of two rectangles R1 and R2. Return
30708 the intersection in *RESULT. Value is true if RESULT is not
30709 empty. */
30710
30711 bool
30712 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30713 {
30714 XRectangle *left, *right;
30715 XRectangle *upper, *lower;
30716 bool intersection_p = false;
30717
30718 /* Rearrange so that R1 is the left-most rectangle. */
30719 if (r1->x < r2->x)
30720 left = r1, right = r2;
30721 else
30722 left = r2, right = r1;
30723
30724 /* X0 of the intersection is right.x0, if this is inside R1,
30725 otherwise there is no intersection. */
30726 if (right->x <= left->x + left->width)
30727 {
30728 result->x = right->x;
30729
30730 /* The right end of the intersection is the minimum of
30731 the right ends of left and right. */
30732 result->width = (min (left->x + left->width, right->x + right->width)
30733 - result->x);
30734
30735 /* Same game for Y. */
30736 if (r1->y < r2->y)
30737 upper = r1, lower = r2;
30738 else
30739 upper = r2, lower = r1;
30740
30741 /* The upper end of the intersection is lower.y0, if this is inside
30742 of upper. Otherwise, there is no intersection. */
30743 if (lower->y <= upper->y + upper->height)
30744 {
30745 result->y = lower->y;
30746
30747 /* The lower end of the intersection is the minimum of the lower
30748 ends of upper and lower. */
30749 result->height = (min (lower->y + lower->height,
30750 upper->y + upper->height)
30751 - result->y);
30752 intersection_p = true;
30753 }
30754 }
30755
30756 return intersection_p;
30757 }
30758
30759 #endif /* HAVE_WINDOW_SYSTEM */
30760
30761 \f
30762 /***********************************************************************
30763 Initialization
30764 ***********************************************************************/
30765
30766 void
30767 syms_of_xdisp (void)
30768 {
30769 Vwith_echo_area_save_vector = Qnil;
30770 staticpro (&Vwith_echo_area_save_vector);
30771
30772 Vmessage_stack = Qnil;
30773 staticpro (&Vmessage_stack);
30774
30775 /* Non-nil means don't actually do any redisplay. */
30776 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30777
30778 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30779
30780 DEFVAR_BOOL("inhibit-message", inhibit_message,
30781 doc: /* Non-nil means calls to `message' are not displayed.
30782 They are still logged to the *Messages* buffer. */);
30783 inhibit_message = 0;
30784
30785 message_dolog_marker1 = Fmake_marker ();
30786 staticpro (&message_dolog_marker1);
30787 message_dolog_marker2 = Fmake_marker ();
30788 staticpro (&message_dolog_marker2);
30789 message_dolog_marker3 = Fmake_marker ();
30790 staticpro (&message_dolog_marker3);
30791
30792 #ifdef GLYPH_DEBUG
30793 defsubr (&Sdump_frame_glyph_matrix);
30794 defsubr (&Sdump_glyph_matrix);
30795 defsubr (&Sdump_glyph_row);
30796 defsubr (&Sdump_tool_bar_row);
30797 defsubr (&Strace_redisplay);
30798 defsubr (&Strace_to_stderr);
30799 #endif
30800 #ifdef HAVE_WINDOW_SYSTEM
30801 defsubr (&Stool_bar_height);
30802 defsubr (&Slookup_image_map);
30803 #endif
30804 defsubr (&Sline_pixel_height);
30805 defsubr (&Sformat_mode_line);
30806 defsubr (&Sinvisible_p);
30807 defsubr (&Scurrent_bidi_paragraph_direction);
30808 defsubr (&Swindow_text_pixel_size);
30809 defsubr (&Smove_point_visually);
30810 defsubr (&Sbidi_find_overridden_directionality);
30811
30812 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30813 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30814 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30815 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30816 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30817 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30818 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30819 DEFSYM (Qeval, "eval");
30820 DEFSYM (QCdata, ":data");
30821
30822 /* Names of text properties relevant for redisplay. */
30823 DEFSYM (Qdisplay, "display");
30824 DEFSYM (Qspace_width, "space-width");
30825 DEFSYM (Qraise, "raise");
30826 DEFSYM (Qslice, "slice");
30827 DEFSYM (Qspace, "space");
30828 DEFSYM (Qmargin, "margin");
30829 DEFSYM (Qpointer, "pointer");
30830 DEFSYM (Qleft_margin, "left-margin");
30831 DEFSYM (Qright_margin, "right-margin");
30832 DEFSYM (Qcenter, "center");
30833 DEFSYM (Qline_height, "line-height");
30834 DEFSYM (QCalign_to, ":align-to");
30835 DEFSYM (QCrelative_width, ":relative-width");
30836 DEFSYM (QCrelative_height, ":relative-height");
30837 DEFSYM (QCeval, ":eval");
30838 DEFSYM (QCpropertize, ":propertize");
30839 DEFSYM (QCfile, ":file");
30840 DEFSYM (Qfontified, "fontified");
30841 DEFSYM (Qfontification_functions, "fontification-functions");
30842
30843 /* Name of the face used to highlight trailing whitespace. */
30844 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30845
30846 /* Name and number of the face used to highlight escape glyphs. */
30847 DEFSYM (Qescape_glyph, "escape-glyph");
30848
30849 /* Name and number of the face used to highlight non-breaking spaces. */
30850 DEFSYM (Qnobreak_space, "nobreak-space");
30851
30852 /* The symbol 'image' which is the car of the lists used to represent
30853 images in Lisp. Also a tool bar style. */
30854 DEFSYM (Qimage, "image");
30855
30856 /* Tool bar styles. */
30857 DEFSYM (Qtext, "text");
30858 DEFSYM (Qboth, "both");
30859 DEFSYM (Qboth_horiz, "both-horiz");
30860 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30861
30862 /* The image map types. */
30863 DEFSYM (QCmap, ":map");
30864 DEFSYM (QCpointer, ":pointer");
30865 DEFSYM (Qrect, "rect");
30866 DEFSYM (Qcircle, "circle");
30867 DEFSYM (Qpoly, "poly");
30868
30869 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30870
30871 DEFSYM (Qgrow_only, "grow-only");
30872 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30873 DEFSYM (Qposition, "position");
30874 DEFSYM (Qbuffer_position, "buffer-position");
30875 DEFSYM (Qobject, "object");
30876
30877 /* Cursor shapes. */
30878 DEFSYM (Qbar, "bar");
30879 DEFSYM (Qhbar, "hbar");
30880 DEFSYM (Qbox, "box");
30881 DEFSYM (Qhollow, "hollow");
30882
30883 /* Pointer shapes. */
30884 DEFSYM (Qhand, "hand");
30885 DEFSYM (Qarrow, "arrow");
30886 /* also Qtext */
30887
30888 DEFSYM (Qdragging, "dragging");
30889
30890 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30891
30892 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
30893 staticpro (&list_of_error);
30894
30895 /* Values of those variables at last redisplay are stored as
30896 properties on 'overlay-arrow-position' symbol. However, if
30897 Voverlay_arrow_position is a marker, last-arrow-position is its
30898 numerical position. */
30899 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30900 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30901
30902 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30903 properties on a symbol in overlay-arrow-variable-list. */
30904 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30905 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30906
30907 echo_buffer[0] = echo_buffer[1] = Qnil;
30908 staticpro (&echo_buffer[0]);
30909 staticpro (&echo_buffer[1]);
30910
30911 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30912 staticpro (&echo_area_buffer[0]);
30913 staticpro (&echo_area_buffer[1]);
30914
30915 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30916 staticpro (&Vmessages_buffer_name);
30917
30918 mode_line_proptrans_alist = Qnil;
30919 staticpro (&mode_line_proptrans_alist);
30920 mode_line_string_list = Qnil;
30921 staticpro (&mode_line_string_list);
30922 mode_line_string_face = Qnil;
30923 staticpro (&mode_line_string_face);
30924 mode_line_string_face_prop = Qnil;
30925 staticpro (&mode_line_string_face_prop);
30926 Vmode_line_unwind_vector = Qnil;
30927 staticpro (&Vmode_line_unwind_vector);
30928
30929 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30930
30931 help_echo_string = Qnil;
30932 staticpro (&help_echo_string);
30933 help_echo_object = Qnil;
30934 staticpro (&help_echo_object);
30935 help_echo_window = Qnil;
30936 staticpro (&help_echo_window);
30937 previous_help_echo_string = Qnil;
30938 staticpro (&previous_help_echo_string);
30939 help_echo_pos = -1;
30940
30941 DEFSYM (Qright_to_left, "right-to-left");
30942 DEFSYM (Qleft_to_right, "left-to-right");
30943 defsubr (&Sbidi_resolved_levels);
30944
30945 #ifdef HAVE_WINDOW_SYSTEM
30946 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30947 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30948 For example, if a block cursor is over a tab, it will be drawn as
30949 wide as that tab on the display. */);
30950 x_stretch_cursor_p = 0;
30951 #endif
30952
30953 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30954 doc: /* Non-nil means highlight trailing whitespace.
30955 The face used for trailing whitespace is `trailing-whitespace'. */);
30956 Vshow_trailing_whitespace = Qnil;
30957
30958 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30959 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30960 If the value is t, Emacs highlights non-ASCII chars which have the
30961 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30962 or `escape-glyph' face respectively.
30963
30964 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30965 U+2011 (non-breaking hyphen) are affected.
30966
30967 Any other non-nil value means to display these characters as a escape
30968 glyph followed by an ordinary space or hyphen.
30969
30970 A value of nil means no special handling of these characters. */);
30971 Vnobreak_char_display = Qt;
30972
30973 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30974 doc: /* The pointer shape to show in void text areas.
30975 A value of nil means to show the text pointer. Other options are
30976 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30977 `hourglass'. */);
30978 Vvoid_text_area_pointer = Qarrow;
30979
30980 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30981 doc: /* Non-nil means don't actually do any redisplay.
30982 This is used for internal purposes. */);
30983 Vinhibit_redisplay = Qnil;
30984
30985 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30986 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30987 Vglobal_mode_string = Qnil;
30988
30989 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30990 doc: /* Marker for where to display an arrow on top of the buffer text.
30991 This must be the beginning of a line in order to work.
30992 See also `overlay-arrow-string'. */);
30993 Voverlay_arrow_position = Qnil;
30994
30995 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30996 doc: /* String to display as an arrow in non-window frames.
30997 See also `overlay-arrow-position'. */);
30998 Voverlay_arrow_string = build_pure_c_string ("=>");
30999
31000 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31001 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31002 The symbols on this list are examined during redisplay to determine
31003 where to display overlay arrows. */);
31004 Voverlay_arrow_variable_list
31005 = list1 (intern_c_string ("overlay-arrow-position"));
31006
31007 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31008 doc: /* The number of lines to try scrolling a window by when point moves out.
31009 If that fails to bring point back on frame, point is centered instead.
31010 If this is zero, point is always centered after it moves off frame.
31011 If you want scrolling to always be a line at a time, you should set
31012 `scroll-conservatively' to a large value rather than set this to 1. */);
31013
31014 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31015 doc: /* Scroll up to this many lines, to bring point back on screen.
31016 If point moves off-screen, redisplay will scroll by up to
31017 `scroll-conservatively' lines in order to bring point just barely
31018 onto the screen again. If that cannot be done, then redisplay
31019 recenters point as usual.
31020
31021 If the value is greater than 100, redisplay will never recenter point,
31022 but will always scroll just enough text to bring point into view, even
31023 if you move far away.
31024
31025 A value of zero means always recenter point if it moves off screen. */);
31026 scroll_conservatively = 0;
31027
31028 DEFVAR_INT ("scroll-margin", scroll_margin,
31029 doc: /* Number of lines of margin at the top and bottom of a window.
31030 Recenter the window whenever point gets within this many lines
31031 of the top or bottom of the window. */);
31032 scroll_margin = 0;
31033
31034 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31035 doc: /* Pixels per inch value for non-window system displays.
31036 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31037 Vdisplay_pixels_per_inch = make_float (72.0);
31038
31039 #ifdef GLYPH_DEBUG
31040 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31041 #endif
31042
31043 DEFVAR_LISP ("truncate-partial-width-windows",
31044 Vtruncate_partial_width_windows,
31045 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31046 For an integer value, truncate lines in each window narrower than the
31047 full frame width, provided the window width is less than that integer;
31048 otherwise, respect the value of `truncate-lines'.
31049
31050 For any other non-nil value, truncate lines in all windows that do
31051 not span the full frame width.
31052
31053 A value of nil means to respect the value of `truncate-lines'.
31054
31055 If `word-wrap' is enabled, you might want to reduce this. */);
31056 Vtruncate_partial_width_windows = make_number (50);
31057
31058 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31059 doc: /* Maximum buffer size for which line number should be displayed.
31060 If the buffer is bigger than this, the line number does not appear
31061 in the mode line. A value of nil means no limit. */);
31062 Vline_number_display_limit = Qnil;
31063
31064 DEFVAR_INT ("line-number-display-limit-width",
31065 line_number_display_limit_width,
31066 doc: /* Maximum line width (in characters) for line number display.
31067 If the average length of the lines near point is bigger than this, then the
31068 line number may be omitted from the mode line. */);
31069 line_number_display_limit_width = 200;
31070
31071 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31072 doc: /* Non-nil means highlight region even in nonselected windows. */);
31073 highlight_nonselected_windows = false;
31074
31075 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31076 doc: /* Non-nil if more than one frame is visible on this display.
31077 Minibuffer-only frames don't count, but iconified frames do.
31078 This variable is not guaranteed to be accurate except while processing
31079 `frame-title-format' and `icon-title-format'. */);
31080
31081 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31082 doc: /* Template for displaying the title bar of visible frames.
31083 (Assuming the window manager supports this feature.)
31084
31085 This variable has the same structure as `mode-line-format', except that
31086 the %c and %l constructs are ignored. It is used only on frames for
31087 which no explicit name has been set (see `modify-frame-parameters'). */);
31088
31089 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31090 doc: /* Template for displaying the title bar of an iconified frame.
31091 (Assuming the window manager supports this feature.)
31092 This variable has the same structure as `mode-line-format' (which see),
31093 and is used only on frames for which no explicit name has been set
31094 (see `modify-frame-parameters'). */);
31095 Vicon_title_format
31096 = Vframe_title_format
31097 = listn (CONSTYPE_PURE, 3,
31098 intern_c_string ("multiple-frames"),
31099 build_pure_c_string ("%b"),
31100 listn (CONSTYPE_PURE, 4,
31101 empty_unibyte_string,
31102 intern_c_string ("invocation-name"),
31103 build_pure_c_string ("@"),
31104 intern_c_string ("system-name")));
31105
31106 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31107 doc: /* Maximum number of lines to keep in the message log buffer.
31108 If nil, disable message logging. If t, log messages but don't truncate
31109 the buffer when it becomes large. */);
31110 Vmessage_log_max = make_number (1000);
31111
31112 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31113 doc: /* Functions called before redisplay, if window sizes have changed.
31114 The value should be a list of functions that take one argument.
31115 Just before redisplay, for each frame, if any of its windows have changed
31116 size since the last redisplay, or have been split or deleted,
31117 all the functions in the list are called, with the frame as argument. */);
31118 Vwindow_size_change_functions = Qnil;
31119
31120 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31121 doc: /* List of functions to call before redisplaying a window with scrolling.
31122 Each function is called with two arguments, the window and its new
31123 display-start position.
31124 These functions are called whenever the `window-start' marker is modified,
31125 either to point into another buffer (e.g. via `set-window-buffer') or another
31126 place in the same buffer.
31127 Note that the value of `window-end' is not valid when these functions are
31128 called.
31129
31130 Warning: Do not use this feature to alter the way the window
31131 is scrolled. It is not designed for that, and such use probably won't
31132 work. */);
31133 Vwindow_scroll_functions = Qnil;
31134
31135 DEFVAR_LISP ("window-text-change-functions",
31136 Vwindow_text_change_functions,
31137 doc: /* Functions to call in redisplay when text in the window might change. */);
31138 Vwindow_text_change_functions = Qnil;
31139
31140 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31141 doc: /* Functions called when redisplay of a window reaches the end trigger.
31142 Each function is called with two arguments, the window and the end trigger value.
31143 See `set-window-redisplay-end-trigger'. */);
31144 Vredisplay_end_trigger_functions = Qnil;
31145
31146 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31147 doc: /* Non-nil means autoselect window with mouse pointer.
31148 If nil, do not autoselect windows.
31149 A positive number means delay autoselection by that many seconds: a
31150 window is autoselected only after the mouse has remained in that
31151 window for the duration of the delay.
31152 A negative number has a similar effect, but causes windows to be
31153 autoselected only after the mouse has stopped moving. (Because of
31154 the way Emacs compares mouse events, you will occasionally wait twice
31155 that time before the window gets selected.)
31156 Any other value means to autoselect window instantaneously when the
31157 mouse pointer enters it.
31158
31159 Autoselection selects the minibuffer only if it is active, and never
31160 unselects the minibuffer if it is active.
31161
31162 When customizing this variable make sure that the actual value of
31163 `focus-follows-mouse' matches the behavior of your window manager. */);
31164 Vmouse_autoselect_window = Qnil;
31165
31166 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31167 doc: /* Non-nil means automatically resize tool-bars.
31168 This dynamically changes the tool-bar's height to the minimum height
31169 that is needed to make all tool-bar items visible.
31170 If value is `grow-only', the tool-bar's height is only increased
31171 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31172 Vauto_resize_tool_bars = Qt;
31173
31174 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31175 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31176 auto_raise_tool_bar_buttons_p = true;
31177
31178 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31179 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31180 make_cursor_line_fully_visible_p = true;
31181
31182 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31183 doc: /* Border below tool-bar in pixels.
31184 If an integer, use it as the height of the border.
31185 If it is one of `internal-border-width' or `border-width', use the
31186 value of the corresponding frame parameter.
31187 Otherwise, no border is added below the tool-bar. */);
31188 Vtool_bar_border = Qinternal_border_width;
31189
31190 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31191 doc: /* Margin around tool-bar buttons in pixels.
31192 If an integer, use that for both horizontal and vertical margins.
31193 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31194 HORZ specifying the horizontal margin, and VERT specifying the
31195 vertical margin. */);
31196 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31197
31198 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31199 doc: /* Relief thickness of tool-bar buttons. */);
31200 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31201
31202 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31203 doc: /* Tool bar style to use.
31204 It can be one of
31205 image - show images only
31206 text - show text only
31207 both - show both, text below image
31208 both-horiz - show text to the right of the image
31209 text-image-horiz - show text to the left of the image
31210 any other - use system default or image if no system default.
31211
31212 This variable only affects the GTK+ toolkit version of Emacs. */);
31213 Vtool_bar_style = Qnil;
31214
31215 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31216 doc: /* Maximum number of characters a label can have to be shown.
31217 The tool bar style must also show labels for this to have any effect, see
31218 `tool-bar-style'. */);
31219 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31220
31221 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31222 doc: /* List of functions to call to fontify regions of text.
31223 Each function is called with one argument POS. Functions must
31224 fontify a region starting at POS in the current buffer, and give
31225 fontified regions the property `fontified'. */);
31226 Vfontification_functions = Qnil;
31227 Fmake_variable_buffer_local (Qfontification_functions);
31228
31229 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31230 unibyte_display_via_language_environment,
31231 doc: /* Non-nil means display unibyte text according to language environment.
31232 Specifically, this means that raw bytes in the range 160-255 decimal
31233 are displayed by converting them to the equivalent multibyte characters
31234 according to the current language environment. As a result, they are
31235 displayed according to the current fontset.
31236
31237 Note that this variable affects only how these bytes are displayed,
31238 but does not change the fact they are interpreted as raw bytes. */);
31239 unibyte_display_via_language_environment = false;
31240
31241 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31242 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31243 If a float, it specifies a fraction of the mini-window frame's height.
31244 If an integer, it specifies a number of lines. */);
31245 Vmax_mini_window_height = make_float (0.25);
31246
31247 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31248 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31249 A value of nil means don't automatically resize mini-windows.
31250 A value of t means resize them to fit the text displayed in them.
31251 A value of `grow-only', the default, means let mini-windows grow only;
31252 they return to their normal size when the minibuffer is closed, or the
31253 echo area becomes empty. */);
31254 Vresize_mini_windows = Qgrow_only;
31255
31256 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31257 doc: /* Alist specifying how to blink the cursor off.
31258 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31259 `cursor-type' frame-parameter or variable equals ON-STATE,
31260 comparing using `equal', Emacs uses OFF-STATE to specify
31261 how to blink it off. ON-STATE and OFF-STATE are values for
31262 the `cursor-type' frame parameter.
31263
31264 If a frame's ON-STATE has no entry in this list,
31265 the frame's other specifications determine how to blink the cursor off. */);
31266 Vblink_cursor_alist = Qnil;
31267
31268 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31269 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31270 If non-nil, windows are automatically scrolled horizontally to make
31271 point visible. */);
31272 automatic_hscrolling_p = true;
31273 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31274
31275 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31276 doc: /* How many columns away from the window edge point is allowed to get
31277 before automatic hscrolling will horizontally scroll the window. */);
31278 hscroll_margin = 5;
31279
31280 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31281 doc: /* How many columns to scroll the window when point gets too close to the edge.
31282 When point is less than `hscroll-margin' columns from the window
31283 edge, automatic hscrolling will scroll the window by the amount of columns
31284 determined by this variable. If its value is a positive integer, scroll that
31285 many columns. If it's a positive floating-point number, it specifies the
31286 fraction of the window's width to scroll. If it's nil or zero, point will be
31287 centered horizontally after the scroll. Any other value, including negative
31288 numbers, are treated as if the value were zero.
31289
31290 Automatic hscrolling always moves point outside the scroll margin, so if
31291 point was more than scroll step columns inside the margin, the window will
31292 scroll more than the value given by the scroll step.
31293
31294 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31295 and `scroll-right' overrides this variable's effect. */);
31296 Vhscroll_step = make_number (0);
31297
31298 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31299 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31300 Bind this around calls to `message' to let it take effect. */);
31301 message_truncate_lines = false;
31302
31303 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31304 doc: /* Normal hook run to update the menu bar definitions.
31305 Redisplay runs this hook before it redisplays the menu bar.
31306 This is used to update menus such as Buffers, whose contents depend on
31307 various data. */);
31308 Vmenu_bar_update_hook = Qnil;
31309
31310 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31311 doc: /* Frame for which we are updating a menu.
31312 The enable predicate for a menu binding should check this variable. */);
31313 Vmenu_updating_frame = Qnil;
31314
31315 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31316 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31317 inhibit_menubar_update = false;
31318
31319 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31320 doc: /* Prefix prepended to all continuation lines at display time.
31321 The value may be a string, an image, or a stretch-glyph; it is
31322 interpreted in the same way as the value of a `display' text property.
31323
31324 This variable is overridden by any `wrap-prefix' text or overlay
31325 property.
31326
31327 To add a prefix to non-continuation lines, use `line-prefix'. */);
31328 Vwrap_prefix = Qnil;
31329 DEFSYM (Qwrap_prefix, "wrap-prefix");
31330 Fmake_variable_buffer_local (Qwrap_prefix);
31331
31332 DEFVAR_LISP ("line-prefix", Vline_prefix,
31333 doc: /* Prefix prepended to all non-continuation lines at display time.
31334 The value may be a string, an image, or a stretch-glyph; it is
31335 interpreted in the same way as the value of a `display' text property.
31336
31337 This variable is overridden by any `line-prefix' text or overlay
31338 property.
31339
31340 To add a prefix to continuation lines, use `wrap-prefix'. */);
31341 Vline_prefix = Qnil;
31342 DEFSYM (Qline_prefix, "line-prefix");
31343 Fmake_variable_buffer_local (Qline_prefix);
31344
31345 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31346 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31347 inhibit_eval_during_redisplay = false;
31348
31349 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31350 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31351 inhibit_free_realized_faces = false;
31352
31353 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31354 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31355 Intended for use during debugging and for testing bidi display;
31356 see biditest.el in the test suite. */);
31357 inhibit_bidi_mirroring = false;
31358
31359 #ifdef GLYPH_DEBUG
31360 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31361 doc: /* Inhibit try_window_id display optimization. */);
31362 inhibit_try_window_id = false;
31363
31364 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31365 doc: /* Inhibit try_window_reusing display optimization. */);
31366 inhibit_try_window_reusing = false;
31367
31368 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31369 doc: /* Inhibit try_cursor_movement display optimization. */);
31370 inhibit_try_cursor_movement = false;
31371 #endif /* GLYPH_DEBUG */
31372
31373 DEFVAR_INT ("overline-margin", overline_margin,
31374 doc: /* Space between overline and text, in pixels.
31375 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31376 margin to the character height. */);
31377 overline_margin = 2;
31378
31379 DEFVAR_INT ("underline-minimum-offset",
31380 underline_minimum_offset,
31381 doc: /* Minimum distance between baseline and underline.
31382 This can improve legibility of underlined text at small font sizes,
31383 particularly when using variable `x-use-underline-position-properties'
31384 with fonts that specify an UNDERLINE_POSITION relatively close to the
31385 baseline. The default value is 1. */);
31386 underline_minimum_offset = 1;
31387
31388 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31389 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31390 This feature only works when on a window system that can change
31391 cursor shapes. */);
31392 display_hourglass_p = true;
31393
31394 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31395 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31396 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31397
31398 #ifdef HAVE_WINDOW_SYSTEM
31399 hourglass_atimer = NULL;
31400 hourglass_shown_p = false;
31401 #endif /* HAVE_WINDOW_SYSTEM */
31402
31403 /* Name of the face used to display glyphless characters. */
31404 DEFSYM (Qglyphless_char, "glyphless-char");
31405
31406 /* Method symbols for Vglyphless_char_display. */
31407 DEFSYM (Qhex_code, "hex-code");
31408 DEFSYM (Qempty_box, "empty-box");
31409 DEFSYM (Qthin_space, "thin-space");
31410 DEFSYM (Qzero_width, "zero-width");
31411
31412 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31413 doc: /* Function run just before redisplay.
31414 It is called with one argument, which is the set of windows that are to
31415 be redisplayed. This set can be nil (meaning, only the selected window),
31416 or t (meaning all windows). */);
31417 Vpre_redisplay_function = intern ("ignore");
31418
31419 /* Symbol for the purpose of Vglyphless_char_display. */
31420 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31421 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31422
31423 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31424 doc: /* Char-table defining glyphless characters.
31425 Each element, if non-nil, should be one of the following:
31426 an ASCII acronym string: display this string in a box
31427 `hex-code': display the hexadecimal code of a character in a box
31428 `empty-box': display as an empty box
31429 `thin-space': display as 1-pixel width space
31430 `zero-width': don't display
31431 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31432 display method for graphical terminals and text terminals respectively.
31433 GRAPHICAL and TEXT should each have one of the values listed above.
31434
31435 The char-table has one extra slot to control the display of a character for
31436 which no font is found. This slot only takes effect on graphical terminals.
31437 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31438 `thin-space'. The default is `empty-box'.
31439
31440 If a character has a non-nil entry in an active display table, the
31441 display table takes effect; in this case, Emacs does not consult
31442 `glyphless-char-display' at all. */);
31443 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31444 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31445 Qempty_box);
31446
31447 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31448 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31449 Vdebug_on_message = Qnil;
31450
31451 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31452 doc: /* */);
31453 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
31454
31455 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31456 doc: /* */);
31457 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
31458 }
31459
31460
31461 /* Initialize this module when Emacs starts. */
31462
31463 void
31464 init_xdisp (void)
31465 {
31466 CHARPOS (this_line_start_pos) = 0;
31467
31468 if (!noninteractive)
31469 {
31470 struct window *m = XWINDOW (minibuf_window);
31471 Lisp_Object frame = m->frame;
31472 struct frame *f = XFRAME (frame);
31473 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31474 struct window *r = XWINDOW (root);
31475 int i;
31476
31477 echo_area_window = minibuf_window;
31478
31479 r->top_line = FRAME_TOP_MARGIN (f);
31480 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31481 r->total_cols = FRAME_COLS (f);
31482 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31483 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31484 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31485
31486 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31487 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31488 m->total_cols = FRAME_COLS (f);
31489 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31490 m->total_lines = 1;
31491 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31492
31493 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31494 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31495 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31496
31497 /* The default ellipsis glyphs `...'. */
31498 for (i = 0; i < 3; ++i)
31499 default_invis_vector[i] = make_number ('.');
31500 }
31501
31502 {
31503 /* Allocate the buffer for frame titles.
31504 Also used for `format-mode-line'. */
31505 int size = 100;
31506 mode_line_noprop_buf = xmalloc (size);
31507 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31508 mode_line_noprop_ptr = mode_line_noprop_buf;
31509 mode_line_target = MODE_LINE_DISPLAY;
31510 }
31511
31512 help_echo_showing_p = false;
31513 }
31514
31515 #ifdef HAVE_WINDOW_SYSTEM
31516
31517 /* Platform-independent portion of hourglass implementation. */
31518
31519 /* Timer function of hourglass_atimer. */
31520
31521 static void
31522 show_hourglass (struct atimer *timer)
31523 {
31524 /* The timer implementation will cancel this timer automatically
31525 after this function has run. Set hourglass_atimer to null
31526 so that we know the timer doesn't have to be canceled. */
31527 hourglass_atimer = NULL;
31528
31529 if (!hourglass_shown_p)
31530 {
31531 Lisp_Object tail, frame;
31532
31533 block_input ();
31534
31535 FOR_EACH_FRAME (tail, frame)
31536 {
31537 struct frame *f = XFRAME (frame);
31538
31539 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31540 && FRAME_RIF (f)->show_hourglass)
31541 FRAME_RIF (f)->show_hourglass (f);
31542 }
31543
31544 hourglass_shown_p = true;
31545 unblock_input ();
31546 }
31547 }
31548
31549 /* Cancel a currently active hourglass timer, and start a new one. */
31550
31551 void
31552 start_hourglass (void)
31553 {
31554 struct timespec delay;
31555
31556 cancel_hourglass ();
31557
31558 if (INTEGERP (Vhourglass_delay)
31559 && XINT (Vhourglass_delay) > 0)
31560 delay = make_timespec (min (XINT (Vhourglass_delay),
31561 TYPE_MAXIMUM (time_t)),
31562 0);
31563 else if (FLOATP (Vhourglass_delay)
31564 && XFLOAT_DATA (Vhourglass_delay) > 0)
31565 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31566 else
31567 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31568
31569 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31570 show_hourglass, NULL);
31571 }
31572
31573 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31574 shown. */
31575
31576 void
31577 cancel_hourglass (void)
31578 {
31579 if (hourglass_atimer)
31580 {
31581 cancel_atimer (hourglass_atimer);
31582 hourglass_atimer = NULL;
31583 }
31584
31585 if (hourglass_shown_p)
31586 {
31587 Lisp_Object tail, frame;
31588
31589 block_input ();
31590
31591 FOR_EACH_FRAME (tail, frame)
31592 {
31593 struct frame *f = XFRAME (frame);
31594
31595 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31596 && FRAME_RIF (f)->hide_hourglass)
31597 FRAME_RIF (f)->hide_hourglass (f);
31598 #ifdef HAVE_NTGUI
31599 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31600 else if (!FRAME_W32_P (f))
31601 w32_arrow_cursor ();
31602 #endif
31603 }
31604
31605 hourglass_shown_p = false;
31606 unblock_input ();
31607 }
31608 }
31609
31610 #endif /* HAVE_WINDOW_SYSTEM */