]> code.delx.au - gnu-emacs/blob - src/xdisp.c
58a4f43be8181fd208bd4ce87445817382d055cf
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
104
105 . try_window
106
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
112
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
117
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
130
131 Desired matrices.
132
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
139
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
146
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
156
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
163
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
175
176 Frame matrices.
177
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
184
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
196
197 Bidirectional display.
198
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
211
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
220
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
239
240 Bidirectional display and character compositions
241
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
246
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
266
267 Moving an iterator in bidirectional text
268 without producing glyphs
269
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
288
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
292
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
320
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
324
325 #define INFINITY 10000000
326
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
329
330 #ifdef HAVE_WINDOW_SYSTEM
331
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
334
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
342
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
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 /* Non-zero means print newline to stdout before next mini-buffer
364 message. */
365
366 bool noninteractive_need_newline;
367
368 /* Non-zero means print newline to message log before next message. */
369
370 static bool message_log_need_newline;
371
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
378 \f
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
383
384 static struct text_pos this_line_start_pos;
385
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
388
389 static struct text_pos this_line_end_pos;
390
391 /* The vertical positions and the height of this line. */
392
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
396
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
399
400 static int this_line_start_x;
401
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
405
406 static struct text_pos this_line_min_pos;
407
408 /* Buffer that this_line_.* variables are referring to. */
409
410 static struct buffer *this_line_buffer;
411
412 /* Nonzero if an overlay arrow has been displayed in this window. */
413
414 static bool overlay_arrow_seen;
415
416 /* Vector containing glyphs for an ellipsis `...'. */
417
418 static Lisp_Object default_invis_vector[3];
419
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
423
424 Lisp_Object echo_area_window;
425
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
430
431 static Lisp_Object Vmessage_stack;
432
433 /* Nonzero means multibyte characters were enabled when the echo area
434 message was specified. */
435
436 static bool message_enable_multibyte;
437
438 /* Nonzero if we should redraw the mode lines on the next redisplay.
439 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
440 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
441 (the number used is then only used to track down the cause for this
442 full-redisplay). */
443
444 int update_mode_lines;
445
446 /* Nonzero if window sizes or contents other than selected-window have changed
447 since last redisplay that finished.
448 If it has value REDISPLAY_SOME, then only redisplay the windows where
449 the `redisplay' bit has been set. Otherwise, redisplay all windows
450 (the number used is then only used to track down the cause for this
451 full-redisplay). */
452
453 int windows_or_buffers_changed;
454
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
457
458 static bool line_number_displayed;
459
460 /* The name of the *Messages* buffer, a string. */
461
462 static Lisp_Object Vmessages_buffer_name;
463
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
466
467 Lisp_Object echo_area_buffer[2];
468
469 /* The buffers referenced from echo_area_buffer. */
470
471 static Lisp_Object echo_buffer[2];
472
473 /* A vector saved used in with_area_buffer to reduce consing. */
474
475 static Lisp_Object Vwith_echo_area_save_vector;
476
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
479
480 static bool display_last_displayed_message_p;
481
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
484
485 static bool message_buf_print;
486
487 /* Set to 1 in clear_message to make redisplay_internal aware
488 of an emptied echo area. */
489
490 static bool message_cleared_p;
491
492 /* A scratch glyph row with contents used for generating truncation
493 glyphs. Also used in direct_output_for_insert. */
494
495 #define MAX_SCRATCH_GLYPHS 100
496 static struct glyph_row scratch_glyph_row;
497 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
498
499 /* Ascent and height of the last line processed by move_it_to. */
500
501 static int last_height;
502
503 /* Non-zero if there's a help-echo in the echo area. */
504
505 bool help_echo_showing_p;
506
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
512
513 #define TEXT_PROP_DISTANCE_LIMIT 100
514
515 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
516 iterator state and later restore it. This is needed because the
517 bidi iterator on bidi.c keeps a stacked cache of its states, which
518 is really a singleton. When we use scratch iterator objects to
519 move around the buffer, we can cause the bidi cache to be pushed or
520 popped, and therefore we need to restore the cache state when we
521 return to the original iterator. */
522 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
523 do { \
524 if (CACHE) \
525 bidi_unshelve_cache (CACHE, 1); \
526 ITCOPY = ITORIG; \
527 CACHE = bidi_shelve_cache (); \
528 } while (0)
529
530 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
531 do { \
532 if (pITORIG != pITCOPY) \
533 *(pITORIG) = *(pITCOPY); \
534 bidi_unshelve_cache (CACHE, 0); \
535 CACHE = NULL; \
536 } while (0)
537
538 /* Functions to mark elements as needing redisplay. */
539 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
540
541 void
542 redisplay_other_windows (void)
543 {
544 if (!windows_or_buffers_changed)
545 windows_or_buffers_changed = REDISPLAY_SOME;
546 }
547
548 void
549 wset_redisplay (struct window *w)
550 {
551 /* Beware: selected_window can be nil during early stages. */
552 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
553 redisplay_other_windows ();
554 w->redisplay = true;
555 }
556
557 void
558 fset_redisplay (struct frame *f)
559 {
560 redisplay_other_windows ();
561 f->redisplay = true;
562 }
563
564 void
565 bset_redisplay (struct buffer *b)
566 {
567 int count = buffer_window_count (b);
568 if (count > 0)
569 {
570 /* ... it's visible in other window than selected, */
571 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
572 redisplay_other_windows ();
573 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
574 so that if we later set windows_or_buffers_changed, this buffer will
575 not be omitted. */
576 b->text->redisplay = true;
577 }
578 }
579
580 void
581 bset_update_mode_line (struct buffer *b)
582 {
583 if (!update_mode_lines)
584 update_mode_lines = REDISPLAY_SOME;
585 b->text->redisplay = true;
586 }
587
588 #ifdef GLYPH_DEBUG
589
590 /* Non-zero means print traces of redisplay if compiled with
591 GLYPH_DEBUG defined. */
592
593 bool trace_redisplay_p;
594
595 #endif /* GLYPH_DEBUG */
596
597 #ifdef DEBUG_TRACE_MOVE
598 /* Non-zero means trace with TRACE_MOVE to stderr. */
599 int trace_move;
600
601 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
602 #else
603 #define TRACE_MOVE(x) (void) 0
604 #endif
605
606 /* Buffer being redisplayed -- for redisplay_window_error. */
607
608 static struct buffer *displayed_buffer;
609
610 /* Value returned from text property handlers (see below). */
611
612 enum prop_handled
613 {
614 HANDLED_NORMALLY,
615 HANDLED_RECOMPUTE_PROPS,
616 HANDLED_OVERLAY_STRING_CONSUMED,
617 HANDLED_RETURN
618 };
619
620 /* A description of text properties that redisplay is interested
621 in. */
622
623 struct props
624 {
625 /* The name of the property. */
626 struct Lisp_Symbol *name;
627
628 /* A unique index for the property. */
629 enum prop_idx idx;
630
631 /* A handler function called to set up iterator IT from the property
632 at IT's current position. Value is used to steer handle_stop. */
633 enum prop_handled (*handler) (struct it *it);
634 };
635
636 static enum prop_handled handle_face_prop (struct it *);
637 static enum prop_handled handle_invisible_prop (struct it *);
638 static enum prop_handled handle_display_prop (struct it *);
639 static enum prop_handled handle_composition_prop (struct it *);
640 static enum prop_handled handle_overlay_change (struct it *);
641 static enum prop_handled handle_fontified_prop (struct it *);
642
643 /* Properties handled by iterators. */
644
645 static struct props it_props[] =
646 {
647 {XSYMBOL_INIT (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {XSYMBOL_INIT (Qface), FACE_PROP_IDX, handle_face_prop},
651 {XSYMBOL_INIT (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
652 {XSYMBOL_INIT (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {XSYMBOL_INIT (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
654 {NULL, 0, NULL}
655 };
656
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
659
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
661
662 /* Enumeration returned by some move_it_.* functions internally. */
663
664 enum move_it_result
665 {
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
668
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
671
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
674
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
678
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
682
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
685 };
686
687 /* This counter is used to clear the face cache every once in a while
688 in redisplay_internal. It is incremented for each redisplay.
689 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
690 cleared. */
691
692 #define CLEAR_FACE_CACHE_COUNT 500
693 static int clear_face_cache_count;
694
695 /* Similarly for the image cache. */
696
697 #ifdef HAVE_WINDOW_SYSTEM
698 #define CLEAR_IMAGE_CACHE_COUNT 101
699 static int clear_image_cache_count;
700
701 /* Null glyph slice */
702 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
703 #endif
704
705 /* True while redisplay_internal is in progress. */
706
707 bool redisplaying_p;
708
709 /* If a string, XTread_socket generates an event to display that string.
710 (The display is done in read_char.) */
711
712 Lisp_Object help_echo_string;
713 Lisp_Object help_echo_window;
714 Lisp_Object help_echo_object;
715 ptrdiff_t help_echo_pos;
716
717 /* Temporary variable for XTread_socket. */
718
719 Lisp_Object previous_help_echo_string;
720
721 /* Platform-independent portion of hourglass implementation. */
722
723 #ifdef HAVE_WINDOW_SYSTEM
724
725 /* Non-zero means an hourglass cursor is currently shown. */
726 static bool hourglass_shown_p;
727
728 /* If non-null, an asynchronous timer that, when it expires, displays
729 an hourglass cursor on all frames. */
730 static struct atimer *hourglass_atimer;
731
732 #endif /* HAVE_WINDOW_SYSTEM */
733
734 /* Default number of seconds to wait before displaying an hourglass
735 cursor. */
736 #define DEFAULT_HOURGLASS_DELAY 1
737
738 #ifdef HAVE_WINDOW_SYSTEM
739
740 /* Default pixel width of `thin-space' display method. */
741 #define THIN_SPACE_WIDTH 1
742
743 #endif /* HAVE_WINDOW_SYSTEM */
744
745 /* Function prototypes. */
746
747 static void setup_for_ellipsis (struct it *, int);
748 static void set_iterator_to_next (struct it *, int);
749 static void mark_window_display_accurate_1 (struct window *, int);
750 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
751 static int display_prop_string_p (Lisp_Object, Lisp_Object);
752 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
753 static int cursor_row_p (struct glyph_row *);
754 static int redisplay_mode_lines (Lisp_Object, bool);
755 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
756
757 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
758
759 static void handle_line_prefix (struct it *);
760
761 static void pint2str (char *, int, ptrdiff_t);
762 static void pint2hrstr (char *, int, ptrdiff_t);
763 static struct text_pos run_window_scroll_functions (Lisp_Object,
764 struct text_pos);
765 static int text_outside_line_unchanged_p (struct window *,
766 ptrdiff_t, ptrdiff_t);
767 static void store_mode_line_noprop_char (char);
768 static int store_mode_line_noprop (const char *, int, int);
769 static void handle_stop (struct it *);
770 static void handle_stop_backwards (struct it *, ptrdiff_t);
771 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
772 static void ensure_echo_area_buffers (void);
773 static void unwind_with_echo_area_buffer (Lisp_Object);
774 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
775 static int with_echo_area_buffer (struct window *, int,
776 int (*) (ptrdiff_t, Lisp_Object),
777 ptrdiff_t, Lisp_Object);
778 static void clear_garbaged_frames (void);
779 static int current_message_1 (ptrdiff_t, Lisp_Object);
780 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
781 static void set_message (Lisp_Object);
782 static int set_message_1 (ptrdiff_t, Lisp_Object);
783 static int display_echo_area (struct window *);
784 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
785 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
786 static void unwind_redisplay (void);
787 static int string_char_and_length (const unsigned char *, int *);
788 static struct text_pos display_prop_end (struct it *, Lisp_Object,
789 struct text_pos);
790 static int compute_window_start_on_continuation_line (struct window *);
791 static void insert_left_trunc_glyphs (struct it *);
792 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
793 Lisp_Object);
794 static void extend_face_to_end_of_line (struct it *);
795 static int append_space_for_newline (struct it *, int);
796 static int cursor_row_fully_visible_p (struct window *, int, int);
797 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
798 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
799 static int trailing_whitespace_p (ptrdiff_t);
800 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
801 static void push_it (struct it *, struct text_pos *);
802 static void iterate_out_of_display_property (struct it *);
803 static void pop_it (struct it *);
804 static void sync_frame_with_window_matrix_rows (struct window *);
805 static void redisplay_internal (void);
806 static bool echo_area_display (bool);
807 static void redisplay_windows (Lisp_Object);
808 static void redisplay_window (Lisp_Object, bool);
809 static Lisp_Object redisplay_window_error (Lisp_Object);
810 static Lisp_Object redisplay_window_0 (Lisp_Object);
811 static Lisp_Object redisplay_window_1 (Lisp_Object);
812 static int set_cursor_from_row (struct window *, struct glyph_row *,
813 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
814 int, int);
815 static int update_menu_bar (struct frame *, int, int);
816 static int try_window_reusing_current_matrix (struct window *);
817 static int try_window_id (struct window *);
818 static int display_line (struct it *);
819 static int display_mode_lines (struct window *);
820 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
821 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
822 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
823 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
824 static void display_menu_bar (struct window *);
825 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
826 ptrdiff_t *);
827 static int display_string (const char *, Lisp_Object, Lisp_Object,
828 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
829 static void compute_line_metrics (struct it *);
830 static void run_redisplay_end_trigger_hook (struct it *);
831 static int get_overlay_strings (struct it *, ptrdiff_t);
832 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
833 static void next_overlay_string (struct it *);
834 static void reseat (struct it *, struct text_pos, int);
835 static void reseat_1 (struct it *, struct text_pos, int);
836 static void back_to_previous_visible_line_start (struct it *);
837 static void reseat_at_next_visible_line_start (struct it *, int);
838 static int next_element_from_ellipsis (struct it *);
839 static int next_element_from_display_vector (struct it *);
840 static int next_element_from_string (struct it *);
841 static int next_element_from_c_string (struct it *);
842 static int next_element_from_buffer (struct it *);
843 static int next_element_from_composition (struct it *);
844 static int next_element_from_image (struct it *);
845 static int next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static int init_from_display_pos (struct it *, struct window *,
848 struct display_pos *);
849 static void reseat_to_string (struct it *, const char *,
850 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
851 static int get_next_display_element (struct it *);
852 static enum move_it_result
853 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
854 enum move_operation_enum);
855 static void get_visually_first_element (struct it *);
856 static void init_to_row_start (struct it *, struct window *,
857 struct glyph_row *);
858 static int init_to_row_end (struct it *, struct window *,
859 struct glyph_row *);
860 static void back_to_previous_line_start (struct it *);
861 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
862 static struct text_pos string_pos_nchars_ahead (struct text_pos,
863 Lisp_Object, ptrdiff_t);
864 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
865 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
866 static ptrdiff_t number_of_chars (const char *, bool);
867 static void compute_stop_pos (struct it *);
868 static void compute_string_pos (struct text_pos *, struct text_pos,
869 Lisp_Object);
870 static int face_before_or_after_it_pos (struct it *, int);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, int);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
880
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
883
884 #ifdef HAVE_WINDOW_SYSTEM
885
886 static void x_consider_frame_title (Lisp_Object);
887 static void update_tool_bar (struct frame *, int);
888 static int redisplay_tool_bar (struct frame *);
889 static void x_draw_bottom_divider (struct window *w);
890 static void notice_overwritten_cursor (struct window *,
891 enum glyph_row_area,
892 int, int, int, int);
893 static void append_stretch_glyph (struct it *, Lisp_Object,
894 int, int, int);
895
896
897 #endif /* HAVE_WINDOW_SYSTEM */
898
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
902
903
904 \f
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
908
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
912
913 This is the height of W minus the height of a mode line, if any. */
914
915 int
916 window_text_bottom_y (struct window *w)
917 {
918 int height = WINDOW_PIXEL_HEIGHT (w);
919
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
921
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
924
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
926
927 return height;
928 }
929
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
933
934 int
935 window_box_width (struct window *w, enum glyph_row_area area)
936 {
937 int width = w->pixel_width;
938
939 if (!w->pseudo_window_p)
940 {
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
943
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
951 }
952
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
956 }
957
958
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
961
962 int
963 window_box_height (struct window *w)
964 {
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
967
968 eassert (height >= 0);
969
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
972
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
978
979 if (WINDOW_WANTS_MODELINE_P (w))
980 {
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
989 }
990
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
992 {
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1001 }
1002
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1006 }
1007
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1011
1012 int
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1014 {
1015 int x;
1016
1017 if (w->pseudo_window_p)
1018 return 0;
1019
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1021
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1030 ? 0
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1035
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1038 }
1039
1040
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1044
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1047 {
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1051 }
1052
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left (struct window *w, enum glyph_row_area area)
1059 {
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1062
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1065
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1068
1069 return x;
1070 }
1071
1072
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1076
1077 int
1078 window_box_right (struct window *w, enum glyph_row_area area)
1079 {
1080 return window_box_left (w, area) + window_box_width (w, area);
1081 }
1082
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1089
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1093 {
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1101 {
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1105 }
1106 }
1107
1108 #ifdef HAVE_WINDOW_SYSTEM
1109
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1116
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1120 {
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1125 }
1126
1127 #endif /* HAVE_WINDOW_SYSTEM */
1128
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1132
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1135
1136 int
1137 line_bottom_y (struct it *it)
1138 {
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1141
1142 if (line_height == 0)
1143 {
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1147 {
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1152 }
1153 else
1154 {
1155 struct glyph_row *row = it->glyph_row;
1156
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1165 }
1166 }
1167
1168 return line_top_y + line_height;
1169 }
1170
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1174
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1177 {
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1183
1184 if (XBUFFER (w->contents) != current_buffer)
1185 {
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1188 }
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1196
1197 return result;
1198 }
1199
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1204
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1214 int
1215 default_line_pixel_height (struct window *w)
1216 {
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1219
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1221 {
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1224
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1228 {
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1232 {
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1234
1235 if (addon >= 0)
1236 height += addon;
1237 }
1238 }
1239 else
1240 height += f->extra_line_spacing;
1241 }
1242
1243 return height;
1244 }
1245
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1250 {
1251 if (CONSP (spec))
1252 {
1253 while (CONSP (spec))
1254 {
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1258 }
1259 }
1260 else if (VECTORP (spec))
1261 {
1262 ptrdiff_t i;
1263
1264 for (i = 0; i < ASIZE (spec); i++)
1265 {
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1268 }
1269 return Qnil;
1270 }
1271
1272 return spec;
1273 }
1274
1275
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1281 {
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1285
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1288
1289 return window_hscroll;
1290 }
1291
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1297
1298 int
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1301 {
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1308
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1311
1312 if (XBUFFER (w->contents) != current_buffer)
1313 {
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1316 }
1317
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. */
1322 if (CHARPOS (top) > ZV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1324
1325 /* Compute exact mode line heights. */
1326 if (WINDOW_WANTS_MODELINE_P (w))
1327 w->mode_line_height
1328 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1329 BVAR (current_buffer, mode_line_format));
1330
1331 if (WINDOW_WANTS_HEADER_LINE_P (w))
1332 w->header_line_height
1333 = display_mode_line (w, HEADER_LINE_FACE_ID,
1334 BVAR (current_buffer, header_line_format));
1335
1336 start_display (&it, w, top);
1337 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1338 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1339
1340 if (charpos >= 0
1341 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1342 && IT_CHARPOS (it) >= charpos)
1343 /* When scanning backwards under bidi iteration, move_it_to
1344 stops at or _before_ CHARPOS, because it stops at or to
1345 the _right_ of the character at CHARPOS. */
1346 || (it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) <= charpos)))
1348 {
1349 /* We have reached CHARPOS, or passed it. How the call to
1350 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1351 or covered by a display property, move_it_to stops at the end
1352 of the invisible text, to the right of CHARPOS. (ii) If
1353 CHARPOS is in a display vector, move_it_to stops on its last
1354 glyph. */
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1358 int bottom_y;
1359 struct it save_it;
1360 void *save_it_data = NULL;
1361
1362 /* Calling line_bottom_y may change it.method, it.position, etc. */
1363 SAVE_IT (save_it, it, save_it_data);
1364 last_height = 0;
1365 bottom_y = line_bottom_y (&it);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (bottom_y >= it.last_visible_y
1371 && it.bidi_p && it.bidi_it.scan_dir == -1
1372 && IT_CHARPOS (it) < charpos)
1373 {
1374 /* When the last line of the window is scanned backwards
1375 under bidi iteration, we could be duped into thinking
1376 that we have passed CHARPOS, when in fact move_it_to
1377 simply stopped short of CHARPOS because it reached
1378 last_visible_y. To see if that's what happened, we call
1379 move_it_to again with a slightly larger vertical limit,
1380 and see if it actually moved vertically; if it did, we
1381 didn't really reach CHARPOS, which is beyond window end. */
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines = 10 * default_line_pixel_height (w);
1385
1386 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1387 MOVE_TO_POS | MOVE_TO_Y);
1388 if (it.current_y > top_y)
1389 visible_p = 0;
1390
1391 }
1392 RESTORE_IT (&it, &save_it, save_it_data);
1393 if (visible_p)
1394 {
1395 if (it.method == GET_FROM_DISPLAY_VECTOR)
1396 {
1397 /* We stopped on the last glyph of a display vector.
1398 Try and recompute. Hack alert! */
1399 if (charpos < 2 || top.charpos >= charpos)
1400 top_x = it.glyph_row->x;
1401 else
1402 {
1403 struct it it2, it2_prev;
1404 /* The idea is to get to the previous buffer
1405 position, consume the character there, and use
1406 the pixel coordinates we get after that. But if
1407 the previous buffer position is also displayed
1408 from a display vector, we need to consume all of
1409 the glyphs from that display vector. */
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 /* If we didn't get to CHARPOS - 1, there's some
1413 replacing display property at that position, and
1414 we stopped after it. That is exactly the place
1415 whose coordinates we want. */
1416 if (IT_CHARPOS (it2) != charpos - 1)
1417 it2_prev = it2;
1418 else
1419 {
1420 /* Iterate until we get out of the display
1421 vector that displays the character at
1422 CHARPOS - 1. */
1423 do {
1424 get_next_display_element (&it2);
1425 PRODUCE_GLYPHS (&it2);
1426 it2_prev = it2;
1427 set_iterator_to_next (&it2, 1);
1428 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1429 && IT_CHARPOS (it2) < charpos);
1430 }
1431 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1432 || it2_prev.current_x > it2_prev.last_visible_x)
1433 top_x = it.glyph_row->x;
1434 else
1435 {
1436 top_x = it2_prev.current_x;
1437 top_y = it2_prev.current_y;
1438 }
1439 }
1440 }
1441 else if (IT_CHARPOS (it) != charpos)
1442 {
1443 Lisp_Object cpos = make_number (charpos);
1444 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1445 Lisp_Object string = string_from_display_spec (spec);
1446 struct text_pos tpos;
1447 int replacing_spec_p;
1448 bool newline_in_string
1449 = (STRINGP (string)
1450 && memchr (SDATA (string), '\n', SBYTES (string)));
1451
1452 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1453 replacing_spec_p
1454 = (!NILP (spec)
1455 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1456 charpos, FRAME_WINDOW_P (it.f)));
1457 /* The tricky code below is needed because there's a
1458 discrepancy between move_it_to and how we set cursor
1459 when PT is at the beginning of a portion of text
1460 covered by a display property or an overlay with a
1461 display property, or the display line ends in a
1462 newline from a display string. move_it_to will stop
1463 _after_ such display strings, whereas
1464 set_cursor_from_row conspires with cursor_row_p to
1465 place the cursor on the first glyph produced from the
1466 display string. */
1467
1468 /* We have overshoot PT because it is covered by a
1469 display property that replaces the text it covers.
1470 If the string includes embedded newlines, we are also
1471 in the wrong display line. Backtrack to the correct
1472 line, where the display property begins. */
1473 if (replacing_spec_p)
1474 {
1475 Lisp_Object startpos, endpos;
1476 EMACS_INT start, end;
1477 struct it it3;
1478 int it3_moved;
1479
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1508 {
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1515 }
1516
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1521 {
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1531 }
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays zero after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 it3_moved = 0;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1552 {
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = 1;
1558 set_iterator_to_next (&it3, 0);
1559 }
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1571 {
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1574
1575 while (EQ ((g - 1)->object, string))
1576 {
1577 --g;
1578 top_x -= g->pixel_width;
1579 }
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1582 }
1583 }
1584 }
1585
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1595 }
1596 }
1597 else
1598 {
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1604
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1610 {
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1626 }
1627 else
1628 bidi_unshelve_cache (it2data, 1);
1629 }
1630 bidi_unshelve_cache (itdata, 0);
1631
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1634
1635 if (visible_p)
1636 {
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1648 }
1649
1650 #if 0
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1658
1659 return visible_p;
1660 }
1661
1662
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1667
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1670 {
1671 int c;
1672
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1679
1680 return c;
1681 }
1682
1683
1684
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1687
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1690 {
1691 eassert (STRINGP (string) && nchars >= 0);
1692
1693 if (STRING_MULTIBYTE (string))
1694 {
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1697
1698 while (nchars--)
1699 {
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1704 }
1705 }
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1708
1709 return pos;
1710 }
1711
1712
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1715
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1718 {
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1724 }
1725
1726
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1729 means recognize multibyte characters. */
1730
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1733 {
1734 struct text_pos pos;
1735
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1738
1739 if (multibyte_p)
1740 {
1741 int len;
1742
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1745 {
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1750 }
1751 }
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1754
1755 return pos;
1756 }
1757
1758
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 non-zero means recognize multibyte characters. */
1761
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1764 {
1765 ptrdiff_t nchars;
1766
1767 if (multibyte_p)
1768 {
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1772
1773 for (nchars = 0; rest > 0; ++nchars)
1774 {
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1777 }
1778 }
1779 else
1780 nchars = strlen (s);
1781
1782 return nchars;
1783 }
1784
1785
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1789
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1792 {
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1795
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1801 }
1802
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1806
1807 int
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1809 {
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1812 {
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1814
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1818 {
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1821 {
1822 if (face->font)
1823 height = FONT_HEIGHT (face->font);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1826 }
1827 }
1828
1829 return height;
1830 }
1831 #endif
1832
1833 return 1;
1834 }
1835
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1839 not force the value into range. */
1840
1841 void
1842 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1843 int *x, int *y, NativeRectangle *bounds, int noclip)
1844 {
1845
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1848 {
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1855
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1858
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1865
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1868 {
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1873
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1878 }
1879 }
1880 #endif
1881
1882 *x = pix_x;
1883 *y = pix_y;
1884 }
1885
1886
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1894
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1898 {
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1902
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1905 {
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1911 }
1912
1913 *vpos = i;
1914 *hpos = 0;
1915
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1919
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1922 {
1923 *area = TEXT_AREA;
1924 x0 = 0;
1925 }
1926 else
1927 {
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1929 {
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1932 }
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1934 {
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1937 }
1938 else
1939 {
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1942 }
1943 }
1944
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1950 {
1951 x -= glyph->pixel_width;
1952 ++glyph;
1953 }
1954
1955 if (glyph == end)
1956 return NULL;
1957
1958 if (dx)
1959 {
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1962 }
1963
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1966 }
1967
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1970
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1973 {
1974 if (w->pseudo_window_p)
1975 {
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1981 }
1982 else
1983 {
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 }
1987 }
1988
1989 #ifdef HAVE_WINDOW_SYSTEM
1990
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1994
1995 int
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1997 {
1998 XRectangle r;
1999
2000 if (n <= 0)
2001 return 0;
2002
2003 if (s->row->full_width_p)
2004 {
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2011
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2018 }
2019 else
2020 {
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2025 }
2026
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2029 {
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2035 }
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2038 {
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2043 }
2044
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2049 {
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2052
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2059 {
2060 XRectangle rc, r_save = r;
2061
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2066
2067 x_intersect_rectangles (&r_save, &rc, &r);
2068 }
2069 }
2070 else
2071 {
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2080 }
2081
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2083
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2087 {
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2090
2091 if (s->x > r.x)
2092 {
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2098 }
2099 r.width = min (r.width, glyph->pixel_width);
2100
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2107 {
2108 r.y = max_y;
2109 r.height = height;
2110 }
2111 else
2112 {
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2116 {
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2120 }
2121 }
2122 }
2123
2124 if (s->row->clip)
2125 {
2126 XRectangle r_save = r;
2127
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2130 }
2131
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2134 {
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2141 }
2142 else
2143 {
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2154
2155 if (s->for_overlaps & OVERLAPS_PRED)
2156 {
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2159 {
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2164 }
2165 i++;
2166 }
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2168 {
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2171 {
2172 if (r.y + r.height > row_y + s->row->visible_height)
2173 {
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2176 }
2177 else
2178 rs[i].height = 0;
2179 }
2180 i++;
2181 }
2182
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2189 }
2190 }
2191
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2194
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2197 {
2198 get_glyph_string_clip_rects (s, nr, 1);
2199 }
2200
2201
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2205 */
2206
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2210 {
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0;
2213
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2219
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2222 {
2223 wd += x;
2224 x = 0;
2225 }
2226
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2231
2232 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2233
2234 /* If y is below window bottom, ensure that we still see a cursor. */
2235 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2236
2237 h = max (h0, glyph->ascent + glyph->descent);
2238 h0 = min (h0, glyph->ascent + glyph->descent);
2239
2240 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2241 if (y < y0)
2242 {
2243 h = max (h - (y0 - y) + 1, h0);
2244 y = y0 - 1;
2245 }
2246 else
2247 {
2248 y0 = window_text_bottom_y (w) - h0;
2249 if (y > y0)
2250 {
2251 h += y - y0;
2252 y = y0;
2253 }
2254 }
2255
2256 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2257 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2258 *heightp = h;
2259 }
2260
2261 /*
2262 * Remember which glyph the mouse is over.
2263 */
2264
2265 void
2266 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2267 {
2268 Lisp_Object window;
2269 struct window *w;
2270 struct glyph_row *r, *gr, *end_row;
2271 enum window_part part;
2272 enum glyph_row_area area;
2273 int x, y, width, height;
2274
2275 /* Try to determine frame pixel position and size of the glyph under
2276 frame pixel coordinates X/Y on frame F. */
2277
2278 if (window_resize_pixelwise)
2279 {
2280 width = height = 1;
2281 goto virtual_glyph;
2282 }
2283 else if (!f->glyphs_initialized_p
2284 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2285 NILP (window)))
2286 {
2287 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2288 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2289 goto virtual_glyph;
2290 }
2291
2292 w = XWINDOW (window);
2293 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2294 height = WINDOW_FRAME_LINE_HEIGHT (w);
2295
2296 x = window_relative_x_coord (w, part, gx);
2297 y = gy - WINDOW_TOP_EDGE_Y (w);
2298
2299 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2300 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2301
2302 if (w->pseudo_window_p)
2303 {
2304 area = TEXT_AREA;
2305 part = ON_MODE_LINE; /* Don't adjust margin. */
2306 goto text_glyph;
2307 }
2308
2309 switch (part)
2310 {
2311 case ON_LEFT_MARGIN:
2312 area = LEFT_MARGIN_AREA;
2313 goto text_glyph;
2314
2315 case ON_RIGHT_MARGIN:
2316 area = RIGHT_MARGIN_AREA;
2317 goto text_glyph;
2318
2319 case ON_HEADER_LINE:
2320 case ON_MODE_LINE:
2321 gr = (part == ON_HEADER_LINE
2322 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2323 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2324 gy = gr->y;
2325 area = TEXT_AREA;
2326 goto text_glyph_row_found;
2327
2328 case ON_TEXT:
2329 area = TEXT_AREA;
2330
2331 text_glyph:
2332 gr = 0; gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2335 {
2336 gr = r; gy = r->y;
2337 break;
2338 }
2339
2340 text_glyph_row_found:
2341 if (gr && gy <= y)
2342 {
2343 struct glyph *g = gr->glyphs[area];
2344 struct glyph *end = g + gr->used[area];
2345
2346 height = gr->height;
2347 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2348 if (gx + g->pixel_width > x)
2349 break;
2350
2351 if (g < end)
2352 {
2353 if (g->type == IMAGE_GLYPH)
2354 {
2355 /* Don't remember when mouse is over image, as
2356 image may have hot-spots. */
2357 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2358 return;
2359 }
2360 width = g->pixel_width;
2361 }
2362 else
2363 {
2364 /* Use nominal char spacing at end of line. */
2365 x -= gx;
2366 gx += (x / width) * width;
2367 }
2368
2369 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2370 {
2371 gx += window_box_left_offset (w, area);
2372 /* Don't expand over the modeline to make sure the vertical
2373 drag cursor is shown early enough. */
2374 height = min (height,
2375 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2376 }
2377 }
2378 else
2379 {
2380 /* Use nominal line height at end of window. */
2381 gx = (x / width) * width;
2382 y -= gy;
2383 gy += (y / height) * height;
2384 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 /* See comment above. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2388 }
2389 break;
2390
2391 case ON_LEFT_FRINGE:
2392 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2394 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2395 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2396 goto row_glyph;
2397
2398 case ON_RIGHT_FRINGE:
2399 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2400 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 : window_box_right_offset (w, TEXT_AREA));
2402 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2403 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2404 && !WINDOW_RIGHTMOST_P (w))
2405 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2406 /* Make sure the vertical border can get her own glyph to the
2407 right of the one we build here. */
2408 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2409 else
2410 width = WINDOW_PIXEL_WIDTH (w) - gx;
2411 else
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2413
2414 goto row_glyph;
2415
2416 case ON_VERTICAL_BORDER:
2417 gx = WINDOW_PIXEL_WIDTH (w) - width;
2418 goto row_glyph;
2419
2420 case ON_VERTICAL_SCROLL_BAR:
2421 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2422 ? 0
2423 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2425 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2426 : 0)));
2427 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2428
2429 row_glyph:
2430 gr = 0, gy = 0;
2431 for (; r <= end_row && r->enabled_p; ++r)
2432 if (r->y + r->height > y)
2433 {
2434 gr = r; gy = r->y;
2435 break;
2436 }
2437
2438 if (gr && gy <= y)
2439 height = gr->height;
2440 else
2441 {
2442 /* Use nominal line height at end of window. */
2443 y -= gy;
2444 gy += (y / height) * height;
2445 }
2446 break;
2447
2448 case ON_RIGHT_DIVIDER:
2449 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2450 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2451 gy = 0;
2452 /* The bottom divider prevails. */
2453 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2454 goto add_edge;
2455
2456 case ON_BOTTOM_DIVIDER:
2457 gx = 0;
2458 width = WINDOW_PIXEL_WIDTH (w);
2459 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2460 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2461 goto add_edge;
2462
2463 default:
2464 ;
2465 virtual_glyph:
2466 /* If there is no glyph under the mouse, then we divide the screen
2467 into a grid of the smallest glyph in the frame, and use that
2468 as our "glyph". */
2469
2470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2471 round down even for negative values. */
2472 if (gx < 0)
2473 gx -= width - 1;
2474 if (gy < 0)
2475 gy -= height - 1;
2476
2477 gx = (gx / width) * width;
2478 gy = (gy / height) * height;
2479
2480 goto store_rect;
2481 }
2482
2483 add_edge:
2484 gx += WINDOW_LEFT_EDGE_X (w);
2485 gy += WINDOW_TOP_EDGE_Y (w);
2486
2487 store_rect:
2488 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2489
2490 /* Visible feedback for debugging. */
2491 #if 0
2492 #if HAVE_X_WINDOWS
2493 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2494 f->output_data.x->normal_gc,
2495 gx, gy, width, height);
2496 #endif
2497 #endif
2498 }
2499
2500
2501 #endif /* HAVE_WINDOW_SYSTEM */
2502
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2505 {
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2511 }
2512
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2516
2517 /* Error handler for safe_eval and safe_call. */
2518
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2521 {
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2525 }
2526
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2530
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2533 {
2534 Lisp_Object val;
2535
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2539 {
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2545
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2549
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2559 }
2560
2561 return val;
2562 }
2563
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2566 {
2567 Lisp_Object retval;
2568 va_list ap;
2569
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2574 }
2575
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2578
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2581 {
2582 return safe_call (2, fn, arg);
2583 }
2584
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2587 {
2588 Lisp_Object retval;
2589 va_list ap;
2590
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2595 }
2596
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2599 {
2600 return safe__call1 (false, Qeval, sexpr);
2601 }
2602
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2605 {
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2607 }
2608
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2611
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2614 {
2615 return safe_call (3, fn, arg1, arg2);
2616 }
2617
2618
2619 \f
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2623
2624 #if 0
2625
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2628
2629 static void
2630 check_it (struct it *it)
2631 {
2632 if (it->method == GET_FROM_STRING)
2633 {
2634 eassert (STRINGP (it->string));
2635 eassert (IT_STRING_CHARPOS (*it) >= 0);
2636 }
2637 else
2638 {
2639 eassert (IT_STRING_CHARPOS (*it) < 0);
2640 if (it->method == GET_FROM_BUFFER)
2641 {
2642 /* Check that character and byte positions agree. */
2643 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2644 }
2645 }
2646
2647 if (it->dpvec)
2648 eassert (it->current.dpvec_index >= 0);
2649 else
2650 eassert (it->current.dpvec_index < 0);
2651 }
2652
2653 #define CHECK_IT(IT) check_it ((IT))
2654
2655 #else /* not 0 */
2656
2657 #define CHECK_IT(IT) (void) 0
2658
2659 #endif /* not 0 */
2660
2661
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2663
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2666
2667 static void
2668 check_window_end (struct window *w)
2669 {
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2671 {
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2677 }
2678 }
2679
2680 #define CHECK_WINDOW_END(W) check_window_end ((W))
2681
2682 #else
2683
2684 #define CHECK_WINDOW_END(W) (void) 0
2685
2686 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2687
2688 /***********************************************************************
2689 Iterator initialization
2690 ***********************************************************************/
2691
2692 /* Initialize IT for displaying current_buffer in window W, starting
2693 at character position CHARPOS. CHARPOS < 0 means that no buffer
2694 position is specified which is useful when the iterator is assigned
2695 a position later. BYTEPOS is the byte position corresponding to
2696 CHARPOS.
2697
2698 If ROW is not null, calls to produce_glyphs with IT as parameter
2699 will produce glyphs in that row.
2700
2701 BASE_FACE_ID is the id of a base face to use. It must be one of
2702 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2704 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2705
2706 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2707 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2708 will be initialized to use the corresponding mode line glyph row of
2709 the desired matrix of W. */
2710
2711 void
2712 init_iterator (struct it *it, struct window *w,
2713 ptrdiff_t charpos, ptrdiff_t bytepos,
2714 struct glyph_row *row, enum face_id base_face_id)
2715 {
2716 enum face_id remapped_base_face_id = base_face_id;
2717
2718 /* Some precondition checks. */
2719 eassert (w != NULL && it != NULL);
2720 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2721 && charpos <= ZV));
2722
2723 /* If face attributes have been changed since the last redisplay,
2724 free realized faces now because they depend on face definitions
2725 that might have changed. Don't free faces while there might be
2726 desired matrices pending which reference these faces. */
2727 if (face_change_count && !inhibit_free_realized_faces)
2728 {
2729 face_change_count = 0;
2730 free_all_realized_faces (Qnil);
2731 }
2732
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2737
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2741 {
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2747 }
2748
2749 /* Clear IT. */
2750 memset (it, 0, sizeof *it);
2751 it->current.overlay_string_index = -1;
2752 it->current.dpvec_index = -1;
2753 it->base_face_id = remapped_base_face_id;
2754 it->string = Qnil;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.string.lstring = Qnil;
2758 it->bidi_it.string.s = NULL;
2759 it->bidi_it.string.bufpos = 0;
2760 it->bidi_it.w = w;
2761
2762 /* The window in which we iterate over current_buffer: */
2763 XSETWINDOW (it->window, w);
2764 it->w = w;
2765 it->f = XFRAME (w->frame);
2766
2767 it->cmp_it.id = -1;
2768
2769 /* Extra space between lines (on window systems only). */
2770 if (base_face_id == DEFAULT_FACE_ID
2771 && FRAME_WINDOW_P (it->f))
2772 {
2773 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2774 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2775 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2776 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2777 * FRAME_LINE_HEIGHT (it->f));
2778 else if (it->f->extra_line_spacing > 0)
2779 it->extra_line_spacing = it->f->extra_line_spacing;
2780 it->max_extra_line_spacing = 0;
2781 }
2782
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2791
2792 /* Current value of the `slice', `space-width', and 'height' properties. */
2793 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2794 it->space_width = Qnil;
2795 it->font_height = Qnil;
2796 it->override_ascent = -1;
2797
2798 /* Are control characters displayed as `^C'? */
2799 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2800
2801 /* -1 means everything between a CR and the following line end
2802 is invisible. >0 means lines indented more than this value are
2803 invisible. */
2804 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2805 ? (clip_to_bounds
2806 (-1, XINT (BVAR (current_buffer, selective_display)),
2807 PTRDIFF_MAX))
2808 : (!NILP (BVAR (current_buffer, selective_display))
2809 ? -1 : 0));
2810 it->selective_display_ellipsis_p
2811 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2812
2813 /* Display table to use. */
2814 it->dp = window_display_table (w);
2815
2816 /* Are multibyte characters enabled in current_buffer? */
2817 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2818
2819 /* Get the position at which the redisplay_end_trigger hook should
2820 be run, if it is to be run at all. */
2821 if (MARKERP (w->redisplay_end_trigger)
2822 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2823 it->redisplay_end_trigger_charpos
2824 = marker_position (w->redisplay_end_trigger);
2825 else if (INTEGERP (w->redisplay_end_trigger))
2826 it->redisplay_end_trigger_charpos
2827 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2828 PTRDIFF_MAX);
2829
2830 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2831
2832 /* Are lines in the display truncated? */
2833 if (base_face_id != DEFAULT_FACE_ID
2834 || it->w->hscroll
2835 || (! WINDOW_FULL_WIDTH_P (it->w)
2836 && ((!NILP (Vtruncate_partial_width_windows)
2837 && !INTEGERP (Vtruncate_partial_width_windows))
2838 || (INTEGERP (Vtruncate_partial_width_windows)
2839 /* PXW: Shall we do something about this? */
2840 && (WINDOW_TOTAL_COLS (it->w)
2841 < XINT (Vtruncate_partial_width_windows))))))
2842 it->line_wrap = TRUNCATE;
2843 else if (NILP (BVAR (current_buffer, truncate_lines)))
2844 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2845 ? WINDOW_WRAP : WORD_WRAP;
2846 else
2847 it->line_wrap = TRUNCATE;
2848
2849 /* Get dimensions of truncation and continuation glyphs. These are
2850 displayed as fringe bitmaps under X, but we need them for such
2851 frames when the fringes are turned off. But leave the dimensions
2852 zero for tooltip frames, as these glyphs look ugly there and also
2853 sabotage calculations of tooltip dimensions in x-show-tip. */
2854 #ifdef HAVE_WINDOW_SYSTEM
2855 if (!(FRAME_WINDOW_P (it->f)
2856 && FRAMEP (tip_frame)
2857 && it->f == XFRAME (tip_frame)))
2858 #endif
2859 {
2860 if (it->line_wrap == TRUNCATE)
2861 {
2862 /* We will need the truncation glyph. */
2863 eassert (it->glyph_row == NULL);
2864 produce_special_glyphs (it, IT_TRUNCATION);
2865 it->truncation_pixel_width = it->pixel_width;
2866 }
2867 else
2868 {
2869 /* We will need the continuation glyph. */
2870 eassert (it->glyph_row == NULL);
2871 produce_special_glyphs (it, IT_CONTINUATION);
2872 it->continuation_pixel_width = it->pixel_width;
2873 }
2874 }
2875
2876 /* Reset these values to zero because the produce_special_glyphs
2877 above has changed them. */
2878 it->pixel_width = it->ascent = it->descent = 0;
2879 it->phys_ascent = it->phys_descent = 0;
2880
2881 /* Set this after getting the dimensions of truncation and
2882 continuation glyphs, so that we don't produce glyphs when calling
2883 produce_special_glyphs, above. */
2884 it->glyph_row = row;
2885 it->area = TEXT_AREA;
2886
2887 /* Get the dimensions of the display area. The display area
2888 consists of the visible window area plus a horizontally scrolled
2889 part to the left of the window. All x-values are relative to the
2890 start of this total display area. */
2891 if (base_face_id != DEFAULT_FACE_ID)
2892 {
2893 /* Mode lines, menu bar in terminal frames. */
2894 it->first_visible_x = 0;
2895 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2896 }
2897 else
2898 {
2899 it->first_visible_x
2900 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2901 it->last_visible_x = (it->first_visible_x
2902 + window_box_width (w, TEXT_AREA));
2903
2904 /* If we truncate lines, leave room for the truncation glyph(s) at
2905 the right margin. Otherwise, leave room for the continuation
2906 glyph(s). Done only if the window has no right fringe. */
2907 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2908 {
2909 if (it->line_wrap == TRUNCATE)
2910 it->last_visible_x -= it->truncation_pixel_width;
2911 else
2912 it->last_visible_x -= it->continuation_pixel_width;
2913 }
2914
2915 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2916 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2917 }
2918
2919 /* Leave room for a border glyph. */
2920 if (!FRAME_WINDOW_P (it->f)
2921 && !WINDOW_RIGHTMOST_P (it->w))
2922 it->last_visible_x -= 1;
2923
2924 it->last_visible_y = window_text_bottom_y (w);
2925
2926 /* For mode lines and alike, arrange for the first glyph having a
2927 left box line if the face specifies a box. */
2928 if (base_face_id != DEFAULT_FACE_ID)
2929 {
2930 struct face *face;
2931
2932 it->face_id = remapped_base_face_id;
2933
2934 /* If we have a boxed mode line, make the first character appear
2935 with a left box line. */
2936 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2937 if (face && face->box != FACE_NO_BOX)
2938 it->start_of_box_run_p = true;
2939 }
2940
2941 /* If a buffer position was specified, set the iterator there,
2942 getting overlays and face properties from that position. */
2943 if (charpos >= BUF_BEG (current_buffer))
2944 {
2945 it->stop_charpos = charpos;
2946 it->end_charpos = ZV;
2947 eassert (charpos == BYTE_TO_CHAR (bytepos));
2948 IT_CHARPOS (*it) = charpos;
2949 IT_BYTEPOS (*it) = bytepos;
2950
2951 /* We will rely on `reseat' to set this up properly, via
2952 handle_face_prop. */
2953 it->face_id = it->base_face_id;
2954
2955 it->start = it->current;
2956 /* Do we need to reorder bidirectional text? Not if this is a
2957 unibyte buffer: by definition, none of the single-byte
2958 characters are strong R2L, so no reordering is needed. And
2959 bidi.c doesn't support unibyte buffers anyway. Also, don't
2960 reorder while we are loading loadup.el, since the tables of
2961 character properties needed for reordering are not yet
2962 available. */
2963 it->bidi_p =
2964 NILP (Vpurify_flag)
2965 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2966 && it->multibyte_p;
2967
2968 /* If we are to reorder bidirectional text, init the bidi
2969 iterator. */
2970 if (it->bidi_p)
2971 {
2972 /* Since we don't know at this point whether there will be
2973 any R2L lines in the window, we reserve space for
2974 truncation/continuation glyphs even if only the left
2975 fringe is absent. */
2976 if (base_face_id == DEFAULT_FACE_ID
2977 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2978 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2979 {
2980 if (it->line_wrap == TRUNCATE)
2981 it->last_visible_x -= it->truncation_pixel_width;
2982 else
2983 it->last_visible_x -= it->continuation_pixel_width;
2984 }
2985 /* Note the paragraph direction that this buffer wants to
2986 use. */
2987 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2988 Qleft_to_right))
2989 it->paragraph_embedding = L2R;
2990 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2991 Qright_to_left))
2992 it->paragraph_embedding = R2L;
2993 else
2994 it->paragraph_embedding = NEUTRAL_DIR;
2995 bidi_unshelve_cache (NULL, 0);
2996 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2997 &it->bidi_it);
2998 }
2999
3000 /* Compute faces etc. */
3001 reseat (it, it->current.pos, 1);
3002 }
3003
3004 CHECK_IT (it);
3005 }
3006
3007
3008 /* Initialize IT for the display of window W with window start POS. */
3009
3010 void
3011 start_display (struct it *it, struct window *w, struct text_pos pos)
3012 {
3013 struct glyph_row *row;
3014 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3015
3016 row = w->desired_matrix->rows + first_vpos;
3017 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3018 it->first_vpos = first_vpos;
3019
3020 /* Don't reseat to previous visible line start if current start
3021 position is in a string or image. */
3022 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3023 {
3024 int start_at_line_beg_p;
3025 int first_y = it->current_y;
3026
3027 /* If window start is not at a line start, skip forward to POS to
3028 get the correct continuation lines width. */
3029 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3030 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3031 if (!start_at_line_beg_p)
3032 {
3033 int new_x;
3034
3035 reseat_at_previous_visible_line_start (it);
3036 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3037
3038 new_x = it->current_x + it->pixel_width;
3039
3040 /* If lines are continued, this line may end in the middle
3041 of a multi-glyph character (e.g. a control character
3042 displayed as \003, or in the middle of an overlay
3043 string). In this case move_it_to above will not have
3044 taken us to the start of the continuation line but to the
3045 end of the continued line. */
3046 if (it->current_x > 0
3047 && it->line_wrap != TRUNCATE /* Lines are continued. */
3048 && (/* And glyph doesn't fit on the line. */
3049 new_x > it->last_visible_x
3050 /* Or it fits exactly and we're on a window
3051 system frame. */
3052 || (new_x == it->last_visible_x
3053 && FRAME_WINDOW_P (it->f)
3054 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3055 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3056 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3057 {
3058 if ((it->current.dpvec_index >= 0
3059 || it->current.overlay_string_index >= 0)
3060 /* If we are on a newline from a display vector or
3061 overlay string, then we are already at the end of
3062 a screen line; no need to go to the next line in
3063 that case, as this line is not really continued.
3064 (If we do go to the next line, C-e will not DTRT.) */
3065 && it->c != '\n')
3066 {
3067 set_iterator_to_next (it, 1);
3068 move_it_in_display_line_to (it, -1, -1, 0);
3069 }
3070
3071 it->continuation_lines_width += it->current_x;
3072 }
3073 /* If the character at POS is displayed via a display
3074 vector, move_it_to above stops at the final glyph of
3075 IT->dpvec. To make the caller redisplay that character
3076 again (a.k.a. start at POS), we need to reset the
3077 dpvec_index to the beginning of IT->dpvec. */
3078 else if (it->current.dpvec_index >= 0)
3079 it->current.dpvec_index = 0;
3080
3081 /* We're starting a new display line, not affected by the
3082 height of the continued line, so clear the appropriate
3083 fields in the iterator structure. */
3084 it->max_ascent = it->max_descent = 0;
3085 it->max_phys_ascent = it->max_phys_descent = 0;
3086
3087 it->current_y = first_y;
3088 it->vpos = 0;
3089 it->current_x = it->hpos = 0;
3090 }
3091 }
3092 }
3093
3094
3095 /* Return 1 if POS is a position in ellipses displayed for invisible
3096 text. W is the window we display, for text property lookup. */
3097
3098 static int
3099 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3100 {
3101 Lisp_Object prop, window;
3102 int ellipses_p = 0;
3103 ptrdiff_t charpos = CHARPOS (pos->pos);
3104
3105 /* If POS specifies a position in a display vector, this might
3106 be for an ellipsis displayed for invisible text. We won't
3107 get the iterator set up for delivering that ellipsis unless
3108 we make sure that it gets aware of the invisible text. */
3109 if (pos->dpvec_index >= 0
3110 && pos->overlay_string_index < 0
3111 && CHARPOS (pos->string_pos) < 0
3112 && charpos > BEGV
3113 && (XSETWINDOW (window, w),
3114 prop = Fget_char_property (make_number (charpos),
3115 Qinvisible, window),
3116 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3117 {
3118 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3119 window);
3120 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3121 }
3122
3123 return ellipses_p;
3124 }
3125
3126
3127 /* Initialize IT for stepping through current_buffer in window W,
3128 starting at position POS that includes overlay string and display
3129 vector/ control character translation position information. Value
3130 is zero if there are overlay strings with newlines at POS. */
3131
3132 static int
3133 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3134 {
3135 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3136 int i, overlay_strings_with_newlines = 0;
3137
3138 /* If POS specifies a position in a display vector, this might
3139 be for an ellipsis displayed for invisible text. We won't
3140 get the iterator set up for delivering that ellipsis unless
3141 we make sure that it gets aware of the invisible text. */
3142 if (in_ellipses_for_invisible_text_p (pos, w))
3143 {
3144 --charpos;
3145 bytepos = 0;
3146 }
3147
3148 /* Keep in mind: the call to reseat in init_iterator skips invisible
3149 text, so we might end up at a position different from POS. This
3150 is only a problem when POS is a row start after a newline and an
3151 overlay starts there with an after-string, and the overlay has an
3152 invisible property. Since we don't skip invisible text in
3153 display_line and elsewhere immediately after consuming the
3154 newline before the row start, such a POS will not be in a string,
3155 but the call to init_iterator below will move us to the
3156 after-string. */
3157 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3158
3159 /* This only scans the current chunk -- it should scan all chunks.
3160 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3161 to 16 in 22.1 to make this a lesser problem. */
3162 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3163 {
3164 const char *s = SSDATA (it->overlay_strings[i]);
3165 const char *e = s + SBYTES (it->overlay_strings[i]);
3166
3167 while (s < e && *s != '\n')
3168 ++s;
3169
3170 if (s < e)
3171 {
3172 overlay_strings_with_newlines = 1;
3173 break;
3174 }
3175 }
3176
3177 /* If position is within an overlay string, set up IT to the right
3178 overlay string. */
3179 if (pos->overlay_string_index >= 0)
3180 {
3181 int relative_index;
3182
3183 /* If the first overlay string happens to have a `display'
3184 property for an image, the iterator will be set up for that
3185 image, and we have to undo that setup first before we can
3186 correct the overlay string index. */
3187 if (it->method == GET_FROM_IMAGE)
3188 pop_it (it);
3189
3190 /* We already have the first chunk of overlay strings in
3191 IT->overlay_strings. Load more until the one for
3192 pos->overlay_string_index is in IT->overlay_strings. */
3193 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3194 {
3195 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = 0;
3197 while (n--)
3198 {
3199 load_overlay_strings (it, 0);
3200 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3201 }
3202 }
3203
3204 it->current.overlay_string_index = pos->overlay_string_index;
3205 relative_index = (it->current.overlay_string_index
3206 % OVERLAY_STRING_CHUNK_SIZE);
3207 it->string = it->overlay_strings[relative_index];
3208 eassert (STRINGP (it->string));
3209 it->current.string_pos = pos->string_pos;
3210 it->method = GET_FROM_STRING;
3211 it->end_charpos = SCHARS (it->string);
3212 /* Set up the bidi iterator for this overlay string. */
3213 if (it->bidi_p)
3214 {
3215 it->bidi_it.string.lstring = it->string;
3216 it->bidi_it.string.s = NULL;
3217 it->bidi_it.string.schars = SCHARS (it->string);
3218 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3219 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3220 it->bidi_it.string.unibyte = !it->multibyte_p;
3221 it->bidi_it.w = it->w;
3222 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3223 FRAME_WINDOW_P (it->f), &it->bidi_it);
3224
3225 /* Synchronize the state of the bidi iterator with
3226 pos->string_pos. For any string position other than
3227 zero, this will be done automagically when we resume
3228 iteration over the string and get_visually_first_element
3229 is called. But if string_pos is zero, and the string is
3230 to be reordered for display, we need to resync manually,
3231 since it could be that the iteration state recorded in
3232 pos ended at string_pos of 0 moving backwards in string. */
3233 if (CHARPOS (pos->string_pos) == 0)
3234 {
3235 get_visually_first_element (it);
3236 if (IT_STRING_CHARPOS (*it) != 0)
3237 do {
3238 /* Paranoia. */
3239 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3240 bidi_move_to_visually_next (&it->bidi_it);
3241 } while (it->bidi_it.charpos != 0);
3242 }
3243 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3244 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3245 }
3246 }
3247
3248 if (CHARPOS (pos->string_pos) >= 0)
3249 {
3250 /* Recorded position is not in an overlay string, but in another
3251 string. This can only be a string from a `display' property.
3252 IT should already be filled with that string. */
3253 it->current.string_pos = pos->string_pos;
3254 eassert (STRINGP (it->string));
3255 if (it->bidi_p)
3256 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3257 FRAME_WINDOW_P (it->f), &it->bidi_it);
3258 }
3259
3260 /* Restore position in display vector translations, control
3261 character translations or ellipses. */
3262 if (pos->dpvec_index >= 0)
3263 {
3264 if (it->dpvec == NULL)
3265 get_next_display_element (it);
3266 eassert (it->dpvec && it->current.dpvec_index == 0);
3267 it->current.dpvec_index = pos->dpvec_index;
3268 }
3269
3270 CHECK_IT (it);
3271 return !overlay_strings_with_newlines;
3272 }
3273
3274
3275 /* Initialize IT for stepping through current_buffer in window W
3276 starting at ROW->start. */
3277
3278 static void
3279 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3280 {
3281 init_from_display_pos (it, w, &row->start);
3282 it->start = row->start;
3283 it->continuation_lines_width = row->continuation_lines_width;
3284 CHECK_IT (it);
3285 }
3286
3287
3288 /* Initialize IT for stepping through current_buffer in window W
3289 starting in the line following ROW, i.e. starting at ROW->end.
3290 Value is zero if there are overlay strings with newlines at ROW's
3291 end position. */
3292
3293 static int
3294 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3295 {
3296 int success = 0;
3297
3298 if (init_from_display_pos (it, w, &row->end))
3299 {
3300 if (row->continued_p)
3301 it->continuation_lines_width
3302 = row->continuation_lines_width + row->pixel_width;
3303 CHECK_IT (it);
3304 success = 1;
3305 }
3306
3307 return success;
3308 }
3309
3310
3311
3312 \f
3313 /***********************************************************************
3314 Text properties
3315 ***********************************************************************/
3316
3317 /* Called when IT reaches IT->stop_charpos. Handle text property and
3318 overlay changes. Set IT->stop_charpos to the next position where
3319 to stop. */
3320
3321 static void
3322 handle_stop (struct it *it)
3323 {
3324 enum prop_handled handled;
3325 int handle_overlay_change_p;
3326 struct props *p;
3327
3328 it->dpvec = NULL;
3329 it->current.dpvec_index = -1;
3330 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3331 it->ignore_overlay_strings_at_pos_p = 0;
3332 it->ellipsis_p = 0;
3333
3334 /* Use face of preceding text for ellipsis (if invisible) */
3335 if (it->selective_display_ellipsis_p)
3336 it->saved_face_id = it->face_id;
3337
3338 /* Here's the description of the semantics of, and the logic behind,
3339 the various HANDLED_* statuses:
3340
3341 HANDLED_NORMALLY means the handler did its job, and the loop
3342 should proceed to calling the next handler in order.
3343
3344 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3345 change in the properties and overlays at current position, so the
3346 loop should be restarted, to re-invoke the handlers that were
3347 already called. This happens when fontification-functions were
3348 called by handle_fontified_prop, and actually fontified
3349 something. Another case where HANDLED_RECOMPUTE_PROPS is
3350 returned is when we discover overlay strings that need to be
3351 displayed right away. The loop below will continue for as long
3352 as the status is HANDLED_RECOMPUTE_PROPS.
3353
3354 HANDLED_RETURN means return immediately to the caller, to
3355 continue iteration without calling any further handlers. This is
3356 used when we need to act on some property right away, for example
3357 when we need to display the ellipsis or a replacing display
3358 property, such as display string or image.
3359
3360 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3361 consumed, and the handler switched to the next overlay string.
3362 This signals the loop below to refrain from looking for more
3363 overlays before all the overlay strings of the current overlay
3364 are processed.
3365
3366 Some of the handlers called by the loop push the iterator state
3367 onto the stack (see 'push_it'), and arrange for the iteration to
3368 continue with another object, such as an image, a display string,
3369 or an overlay string. In most such cases, it->stop_charpos is
3370 set to the first character of the string, so that when the
3371 iteration resumes, this function will immediately be called
3372 again, to examine the properties at the beginning of the string.
3373
3374 When a display or overlay string is exhausted, the iterator state
3375 is popped (see 'pop_it'), and iteration continues with the
3376 previous object. Again, in many such cases this function is
3377 called again to find the next position where properties might
3378 change. */
3379
3380 do
3381 {
3382 handled = HANDLED_NORMALLY;
3383
3384 /* Call text property handlers. */
3385 for (p = it_props; p->handler; ++p)
3386 {
3387 handled = p->handler (it);
3388
3389 if (handled == HANDLED_RECOMPUTE_PROPS)
3390 break;
3391 else if (handled == HANDLED_RETURN)
3392 {
3393 /* We still want to show before and after strings from
3394 overlays even if the actual buffer text is replaced. */
3395 if (!handle_overlay_change_p
3396 || it->sp > 1
3397 /* Don't call get_overlay_strings_1 if we already
3398 have overlay strings loaded, because doing so
3399 will load them again and push the iterator state
3400 onto the stack one more time, which is not
3401 expected by the rest of the code that processes
3402 overlay strings. */
3403 || (it->current.overlay_string_index < 0
3404 ? !get_overlay_strings_1 (it, 0, 0)
3405 : 0))
3406 {
3407 if (it->ellipsis_p)
3408 setup_for_ellipsis (it, 0);
3409 /* When handling a display spec, we might load an
3410 empty string. In that case, discard it here. We
3411 used to discard it in handle_single_display_spec,
3412 but that causes get_overlay_strings_1, above, to
3413 ignore overlay strings that we must check. */
3414 if (STRINGP (it->string) && !SCHARS (it->string))
3415 pop_it (it);
3416 return;
3417 }
3418 else if (STRINGP (it->string) && !SCHARS (it->string))
3419 pop_it (it);
3420 else
3421 {
3422 it->ignore_overlay_strings_at_pos_p = true;
3423 it->string_from_display_prop_p = 0;
3424 it->from_disp_prop_p = 0;
3425 handle_overlay_change_p = 0;
3426 }
3427 handled = HANDLED_RECOMPUTE_PROPS;
3428 break;
3429 }
3430 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3431 handle_overlay_change_p = 0;
3432 }
3433
3434 if (handled != HANDLED_RECOMPUTE_PROPS)
3435 {
3436 /* Don't check for overlay strings below when set to deliver
3437 characters from a display vector. */
3438 if (it->method == GET_FROM_DISPLAY_VECTOR)
3439 handle_overlay_change_p = 0;
3440
3441 /* Handle overlay changes.
3442 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3443 if it finds overlays. */
3444 if (handle_overlay_change_p)
3445 handled = handle_overlay_change (it);
3446 }
3447
3448 if (it->ellipsis_p)
3449 {
3450 setup_for_ellipsis (it, 0);
3451 break;
3452 }
3453 }
3454 while (handled == HANDLED_RECOMPUTE_PROPS);
3455
3456 /* Determine where to stop next. */
3457 if (handled == HANDLED_NORMALLY)
3458 compute_stop_pos (it);
3459 }
3460
3461
3462 /* Compute IT->stop_charpos from text property and overlay change
3463 information for IT's current position. */
3464
3465 static void
3466 compute_stop_pos (struct it *it)
3467 {
3468 register INTERVAL iv, next_iv;
3469 Lisp_Object object, limit, position;
3470 ptrdiff_t charpos, bytepos;
3471
3472 if (STRINGP (it->string))
3473 {
3474 /* Strings are usually short, so don't limit the search for
3475 properties. */
3476 it->stop_charpos = it->end_charpos;
3477 object = it->string;
3478 limit = Qnil;
3479 charpos = IT_STRING_CHARPOS (*it);
3480 bytepos = IT_STRING_BYTEPOS (*it);
3481 }
3482 else
3483 {
3484 ptrdiff_t pos;
3485
3486 /* If end_charpos is out of range for some reason, such as a
3487 misbehaving display function, rationalize it (Bug#5984). */
3488 if (it->end_charpos > ZV)
3489 it->end_charpos = ZV;
3490 it->stop_charpos = it->end_charpos;
3491
3492 /* If next overlay change is in front of the current stop pos
3493 (which is IT->end_charpos), stop there. Note: value of
3494 next_overlay_change is point-max if no overlay change
3495 follows. */
3496 charpos = IT_CHARPOS (*it);
3497 bytepos = IT_BYTEPOS (*it);
3498 pos = next_overlay_change (charpos);
3499 if (pos < it->stop_charpos)
3500 it->stop_charpos = pos;
3501
3502 /* Set up variables for computing the stop position from text
3503 property changes. */
3504 XSETBUFFER (object, current_buffer);
3505 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3506 }
3507
3508 /* Get the interval containing IT's position. Value is a null
3509 interval if there isn't such an interval. */
3510 position = make_number (charpos);
3511 iv = validate_interval_range (object, &position, &position, 0);
3512 if (iv)
3513 {
3514 Lisp_Object values_here[LAST_PROP_IDX];
3515 struct props *p;
3516
3517 /* Get properties here. */
3518 for (p = it_props; p->handler; ++p)
3519 values_here[p->idx] = textget (iv->plist, make_lisp_symbol (p->name));
3520
3521 /* Look for an interval following iv that has different
3522 properties. */
3523 for (next_iv = next_interval (iv);
3524 (next_iv
3525 && (NILP (limit)
3526 || XFASTINT (limit) > next_iv->position));
3527 next_iv = next_interval (next_iv))
3528 {
3529 for (p = it_props; p->handler; ++p)
3530 {
3531 Lisp_Object new_value = textget (next_iv->plist,
3532 make_lisp_symbol (p->name));
3533 if (!EQ (values_here[p->idx], new_value))
3534 break;
3535 }
3536
3537 if (p->handler)
3538 break;
3539 }
3540
3541 if (next_iv)
3542 {
3543 if (INTEGERP (limit)
3544 && next_iv->position >= XFASTINT (limit))
3545 /* No text property change up to limit. */
3546 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3547 else
3548 /* Text properties change in next_iv. */
3549 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3550 }
3551 }
3552
3553 if (it->cmp_it.id < 0)
3554 {
3555 ptrdiff_t stoppos = it->end_charpos;
3556
3557 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3558 stoppos = -1;
3559 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3560 stoppos, it->string);
3561 }
3562
3563 eassert (STRINGP (it->string)
3564 || (it->stop_charpos >= BEGV
3565 && it->stop_charpos >= IT_CHARPOS (*it)));
3566 }
3567
3568
3569 /* Return the position of the next overlay change after POS in
3570 current_buffer. Value is point-max if no overlay change
3571 follows. This is like `next-overlay-change' but doesn't use
3572 xmalloc. */
3573
3574 static ptrdiff_t
3575 next_overlay_change (ptrdiff_t pos)
3576 {
3577 ptrdiff_t i, noverlays;
3578 ptrdiff_t endpos;
3579 Lisp_Object *overlays;
3580 USE_SAFE_ALLOCA;
3581
3582 /* Get all overlays at the given position. */
3583 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3584
3585 /* If any of these overlays ends before endpos,
3586 use its ending point instead. */
3587 for (i = 0; i < noverlays; ++i)
3588 {
3589 Lisp_Object oend;
3590 ptrdiff_t oendpos;
3591
3592 oend = OVERLAY_END (overlays[i]);
3593 oendpos = OVERLAY_POSITION (oend);
3594 endpos = min (endpos, oendpos);
3595 }
3596
3597 SAFE_FREE ();
3598 return endpos;
3599 }
3600
3601 /* How many characters forward to search for a display property or
3602 display string. Searching too far forward makes the bidi display
3603 sluggish, especially in small windows. */
3604 #define MAX_DISP_SCAN 250
3605
3606 /* Return the character position of a display string at or after
3607 position specified by POSITION. If no display string exists at or
3608 after POSITION, return ZV. A display string is either an overlay
3609 with `display' property whose value is a string, or a `display'
3610 text property whose value is a string. STRING is data about the
3611 string to iterate; if STRING->lstring is nil, we are iterating a
3612 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3613 on a GUI frame. DISP_PROP is set to zero if we searched
3614 MAX_DISP_SCAN characters forward without finding any display
3615 strings, non-zero otherwise. It is set to 2 if the display string
3616 uses any kind of `(space ...)' spec that will produce a stretch of
3617 white space in the text area. */
3618 ptrdiff_t
3619 compute_display_string_pos (struct text_pos *position,
3620 struct bidi_string_data *string,
3621 struct window *w,
3622 int frame_window_p, int *disp_prop)
3623 {
3624 /* OBJECT = nil means current buffer. */
3625 Lisp_Object object, object1;
3626 Lisp_Object pos, spec, limpos;
3627 int string_p = (string && (STRINGP (string->lstring) || string->s));
3628 ptrdiff_t eob = string_p ? string->schars : ZV;
3629 ptrdiff_t begb = string_p ? 0 : BEGV;
3630 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3631 ptrdiff_t lim =
3632 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3633 struct text_pos tpos;
3634 int rv = 0;
3635
3636 if (string && STRINGP (string->lstring))
3637 object1 = object = string->lstring;
3638 else if (w && !string_p)
3639 {
3640 XSETWINDOW (object, w);
3641 object1 = Qnil;
3642 }
3643 else
3644 object1 = object = Qnil;
3645
3646 *disp_prop = 1;
3647
3648 if (charpos >= eob
3649 /* We don't support display properties whose values are strings
3650 that have display string properties. */
3651 || string->from_disp_str
3652 /* C strings cannot have display properties. */
3653 || (string->s && !STRINGP (object)))
3654 {
3655 *disp_prop = 0;
3656 return eob;
3657 }
3658
3659 /* If the character at CHARPOS is where the display string begins,
3660 return CHARPOS. */
3661 pos = make_number (charpos);
3662 if (STRINGP (object))
3663 bufpos = string->bufpos;
3664 else
3665 bufpos = charpos;
3666 tpos = *position;
3667 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3668 && (charpos <= begb
3669 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3670 object),
3671 spec))
3672 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3673 frame_window_p)))
3674 {
3675 if (rv == 2)
3676 *disp_prop = 2;
3677 return charpos;
3678 }
3679
3680 /* Look forward for the first character with a `display' property
3681 that will replace the underlying text when displayed. */
3682 limpos = make_number (lim);
3683 do {
3684 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3685 CHARPOS (tpos) = XFASTINT (pos);
3686 if (CHARPOS (tpos) >= lim)
3687 {
3688 *disp_prop = 0;
3689 break;
3690 }
3691 if (STRINGP (object))
3692 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3693 else
3694 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3695 spec = Fget_char_property (pos, Qdisplay, object);
3696 if (!STRINGP (object))
3697 bufpos = CHARPOS (tpos);
3698 } while (NILP (spec)
3699 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3700 bufpos, frame_window_p)));
3701 if (rv == 2)
3702 *disp_prop = 2;
3703
3704 return CHARPOS (tpos);
3705 }
3706
3707 /* Return the character position of the end of the display string that
3708 started at CHARPOS. If there's no display string at CHARPOS,
3709 return -1. A display string is either an overlay with `display'
3710 property whose value is a string or a `display' text property whose
3711 value is a string. */
3712 ptrdiff_t
3713 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3714 {
3715 /* OBJECT = nil means current buffer. */
3716 Lisp_Object object =
3717 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3718 Lisp_Object pos = make_number (charpos);
3719 ptrdiff_t eob =
3720 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3721
3722 if (charpos >= eob || (string->s && !STRINGP (object)))
3723 return eob;
3724
3725 /* It could happen that the display property or overlay was removed
3726 since we found it in compute_display_string_pos above. One way
3727 this can happen is if JIT font-lock was called (through
3728 handle_fontified_prop), and jit-lock-functions remove text
3729 properties or overlays from the portion of buffer that includes
3730 CHARPOS. Muse mode is known to do that, for example. In this
3731 case, we return -1 to the caller, to signal that no display
3732 string is actually present at CHARPOS. See bidi_fetch_char for
3733 how this is handled.
3734
3735 An alternative would be to never look for display properties past
3736 it->stop_charpos. But neither compute_display_string_pos nor
3737 bidi_fetch_char that calls it know or care where the next
3738 stop_charpos is. */
3739 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3740 return -1;
3741
3742 /* Look forward for the first character where the `display' property
3743 changes. */
3744 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3745
3746 return XFASTINT (pos);
3747 }
3748
3749
3750 \f
3751 /***********************************************************************
3752 Fontification
3753 ***********************************************************************/
3754
3755 /* Handle changes in the `fontified' property of the current buffer by
3756 calling hook functions from Qfontification_functions to fontify
3757 regions of text. */
3758
3759 static enum prop_handled
3760 handle_fontified_prop (struct it *it)
3761 {
3762 Lisp_Object prop, pos;
3763 enum prop_handled handled = HANDLED_NORMALLY;
3764
3765 if (!NILP (Vmemory_full))
3766 return handled;
3767
3768 /* Get the value of the `fontified' property at IT's current buffer
3769 position. (The `fontified' property doesn't have a special
3770 meaning in strings.) If the value is nil, call functions from
3771 Qfontification_functions. */
3772 if (!STRINGP (it->string)
3773 && it->s == NULL
3774 && !NILP (Vfontification_functions)
3775 && !NILP (Vrun_hooks)
3776 && (pos = make_number (IT_CHARPOS (*it)),
3777 prop = Fget_char_property (pos, Qfontified, Qnil),
3778 /* Ignore the special cased nil value always present at EOB since
3779 no amount of fontifying will be able to change it. */
3780 NILP (prop) && IT_CHARPOS (*it) < Z))
3781 {
3782 ptrdiff_t count = SPECPDL_INDEX ();
3783 Lisp_Object val;
3784 struct buffer *obuf = current_buffer;
3785 ptrdiff_t begv = BEGV, zv = ZV;
3786 bool old_clip_changed = current_buffer->clip_changed;
3787
3788 val = Vfontification_functions;
3789 specbind (Qfontification_functions, Qnil);
3790
3791 eassert (it->end_charpos == ZV);
3792
3793 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3794 safe_call1 (val, pos);
3795 else
3796 {
3797 Lisp_Object fns, fn;
3798 struct gcpro gcpro1, gcpro2;
3799
3800 fns = Qnil;
3801 GCPRO2 (val, fns);
3802
3803 for (; CONSP (val); val = XCDR (val))
3804 {
3805 fn = XCAR (val);
3806
3807 if (EQ (fn, Qt))
3808 {
3809 /* A value of t indicates this hook has a local
3810 binding; it means to run the global binding too.
3811 In a global value, t should not occur. If it
3812 does, we must ignore it to avoid an endless
3813 loop. */
3814 for (fns = Fdefault_value (Qfontification_functions);
3815 CONSP (fns);
3816 fns = XCDR (fns))
3817 {
3818 fn = XCAR (fns);
3819 if (!EQ (fn, Qt))
3820 safe_call1 (fn, pos);
3821 }
3822 }
3823 else
3824 safe_call1 (fn, pos);
3825 }
3826
3827 UNGCPRO;
3828 }
3829
3830 unbind_to (count, Qnil);
3831
3832 /* Fontification functions routinely call `save-restriction'.
3833 Normally, this tags clip_changed, which can confuse redisplay
3834 (see discussion in Bug#6671). Since we don't perform any
3835 special handling of fontification changes in the case where
3836 `save-restriction' isn't called, there's no point doing so in
3837 this case either. So, if the buffer's restrictions are
3838 actually left unchanged, reset clip_changed. */
3839 if (obuf == current_buffer)
3840 {
3841 if (begv == BEGV && zv == ZV)
3842 current_buffer->clip_changed = old_clip_changed;
3843 }
3844 /* There isn't much we can reasonably do to protect against
3845 misbehaving fontification, but here's a fig leaf. */
3846 else if (BUFFER_LIVE_P (obuf))
3847 set_buffer_internal_1 (obuf);
3848
3849 /* The fontification code may have added/removed text.
3850 It could do even a lot worse, but let's at least protect against
3851 the most obvious case where only the text past `pos' gets changed',
3852 as is/was done in grep.el where some escapes sequences are turned
3853 into face properties (bug#7876). */
3854 it->end_charpos = ZV;
3855
3856 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3857 something. This avoids an endless loop if they failed to
3858 fontify the text for which reason ever. */
3859 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3860 handled = HANDLED_RECOMPUTE_PROPS;
3861 }
3862
3863 return handled;
3864 }
3865
3866
3867 \f
3868 /***********************************************************************
3869 Faces
3870 ***********************************************************************/
3871
3872 /* Set up iterator IT from face properties at its current position.
3873 Called from handle_stop. */
3874
3875 static enum prop_handled
3876 handle_face_prop (struct it *it)
3877 {
3878 int new_face_id;
3879 ptrdiff_t next_stop;
3880
3881 if (!STRINGP (it->string))
3882 {
3883 new_face_id
3884 = face_at_buffer_position (it->w,
3885 IT_CHARPOS (*it),
3886 &next_stop,
3887 (IT_CHARPOS (*it)
3888 + TEXT_PROP_DISTANCE_LIMIT),
3889 0, it->base_face_id);
3890
3891 /* Is this a start of a run of characters with box face?
3892 Caveat: this can be called for a freshly initialized
3893 iterator; face_id is -1 in this case. We know that the new
3894 face will not change until limit, i.e. if the new face has a
3895 box, all characters up to limit will have one. But, as
3896 usual, we don't know whether limit is really the end. */
3897 if (new_face_id != it->face_id)
3898 {
3899 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3900 /* If it->face_id is -1, old_face below will be NULL, see
3901 the definition of FACE_FROM_ID. This will happen if this
3902 is the initial call that gets the face. */
3903 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3904
3905 /* If the value of face_id of the iterator is -1, we have to
3906 look in front of IT's position and see whether there is a
3907 face there that's different from new_face_id. */
3908 if (!old_face && IT_CHARPOS (*it) > BEG)
3909 {
3910 int prev_face_id = face_before_it_pos (it);
3911
3912 old_face = FACE_FROM_ID (it->f, prev_face_id);
3913 }
3914
3915 /* If the new face has a box, but the old face does not,
3916 this is the start of a run of characters with box face,
3917 i.e. this character has a shadow on the left side. */
3918 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3919 && (old_face == NULL || !old_face->box));
3920 it->face_box_p = new_face->box != FACE_NO_BOX;
3921 }
3922 }
3923 else
3924 {
3925 int base_face_id;
3926 ptrdiff_t bufpos;
3927 int i;
3928 Lisp_Object from_overlay
3929 = (it->current.overlay_string_index >= 0
3930 ? it->string_overlays[it->current.overlay_string_index
3931 % OVERLAY_STRING_CHUNK_SIZE]
3932 : Qnil);
3933
3934 /* See if we got to this string directly or indirectly from
3935 an overlay property. That includes the before-string or
3936 after-string of an overlay, strings in display properties
3937 provided by an overlay, their text properties, etc.
3938
3939 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3940 if (! NILP (from_overlay))
3941 for (i = it->sp - 1; i >= 0; i--)
3942 {
3943 if (it->stack[i].current.overlay_string_index >= 0)
3944 from_overlay
3945 = it->string_overlays[it->stack[i].current.overlay_string_index
3946 % OVERLAY_STRING_CHUNK_SIZE];
3947 else if (! NILP (it->stack[i].from_overlay))
3948 from_overlay = it->stack[i].from_overlay;
3949
3950 if (!NILP (from_overlay))
3951 break;
3952 }
3953
3954 if (! NILP (from_overlay))
3955 {
3956 bufpos = IT_CHARPOS (*it);
3957 /* For a string from an overlay, the base face depends
3958 only on text properties and ignores overlays. */
3959 base_face_id
3960 = face_for_overlay_string (it->w,
3961 IT_CHARPOS (*it),
3962 &next_stop,
3963 (IT_CHARPOS (*it)
3964 + TEXT_PROP_DISTANCE_LIMIT),
3965 0,
3966 from_overlay);
3967 }
3968 else
3969 {
3970 bufpos = 0;
3971
3972 /* For strings from a `display' property, use the face at
3973 IT's current buffer position as the base face to merge
3974 with, so that overlay strings appear in the same face as
3975 surrounding text, unless they specify their own faces.
3976 For strings from wrap-prefix and line-prefix properties,
3977 use the default face, possibly remapped via
3978 Vface_remapping_alist. */
3979 /* Note that the fact that we use the face at _buffer_
3980 position means that a 'display' property on an overlay
3981 string will not inherit the face of that overlay string,
3982 but will instead revert to the face of buffer text
3983 covered by the overlay. This is visible, e.g., when the
3984 overlay specifies a box face, but neither the buffer nor
3985 the display string do. This sounds like a design bug,
3986 but Emacs always did that since v21.1, so changing that
3987 might be a big deal. */
3988 base_face_id = it->string_from_prefix_prop_p
3989 ? (!NILP (Vface_remapping_alist)
3990 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3991 : DEFAULT_FACE_ID)
3992 : underlying_face_id (it);
3993 }
3994
3995 new_face_id = face_at_string_position (it->w,
3996 it->string,
3997 IT_STRING_CHARPOS (*it),
3998 bufpos,
3999 &next_stop,
4000 base_face_id, 0);
4001
4002 /* Is this a start of a run of characters with box? Caveat:
4003 this can be called for a freshly allocated iterator; face_id
4004 is -1 is this case. We know that the new face will not
4005 change until the next check pos, i.e. if the new face has a
4006 box, all characters up to that position will have a
4007 box. But, as usual, we don't know whether that position
4008 is really the end. */
4009 if (new_face_id != it->face_id)
4010 {
4011 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4012 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4013
4014 /* If new face has a box but old face hasn't, this is the
4015 start of a run of characters with box, i.e. it has a
4016 shadow on the left side. */
4017 it->start_of_box_run_p
4018 = new_face->box && (old_face == NULL || !old_face->box);
4019 it->face_box_p = new_face->box != FACE_NO_BOX;
4020 }
4021 }
4022
4023 it->face_id = new_face_id;
4024 return HANDLED_NORMALLY;
4025 }
4026
4027
4028 /* Return the ID of the face ``underlying'' IT's current position,
4029 which is in a string. If the iterator is associated with a
4030 buffer, return the face at IT's current buffer position.
4031 Otherwise, use the iterator's base_face_id. */
4032
4033 static int
4034 underlying_face_id (struct it *it)
4035 {
4036 int face_id = it->base_face_id, i;
4037
4038 eassert (STRINGP (it->string));
4039
4040 for (i = it->sp - 1; i >= 0; --i)
4041 if (NILP (it->stack[i].string))
4042 face_id = it->stack[i].face_id;
4043
4044 return face_id;
4045 }
4046
4047
4048 /* Compute the face one character before or after the current position
4049 of IT, in the visual order. BEFORE_P non-zero means get the face
4050 in front (to the left in L2R paragraphs, to the right in R2L
4051 paragraphs) of IT's screen position. Value is the ID of the face. */
4052
4053 static int
4054 face_before_or_after_it_pos (struct it *it, int before_p)
4055 {
4056 int face_id, limit;
4057 ptrdiff_t next_check_charpos;
4058 struct it it_copy;
4059 void *it_copy_data = NULL;
4060
4061 eassert (it->s == NULL);
4062
4063 if (STRINGP (it->string))
4064 {
4065 ptrdiff_t bufpos, charpos;
4066 int base_face_id;
4067
4068 /* No face change past the end of the string (for the case
4069 we are padding with spaces). No face change before the
4070 string start. */
4071 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4072 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4073 return it->face_id;
4074
4075 if (!it->bidi_p)
4076 {
4077 /* Set charpos to the position before or after IT's current
4078 position, in the logical order, which in the non-bidi
4079 case is the same as the visual order. */
4080 if (before_p)
4081 charpos = IT_STRING_CHARPOS (*it) - 1;
4082 else if (it->what == IT_COMPOSITION)
4083 /* For composition, we must check the character after the
4084 composition. */
4085 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4086 else
4087 charpos = IT_STRING_CHARPOS (*it) + 1;
4088 }
4089 else
4090 {
4091 if (before_p)
4092 {
4093 /* With bidi iteration, the character before the current
4094 in the visual order cannot be found by simple
4095 iteration, because "reverse" reordering is not
4096 supported. Instead, we need to use the move_it_*
4097 family of functions. */
4098 /* Ignore face changes before the first visible
4099 character on this display line. */
4100 if (it->current_x <= it->first_visible_x)
4101 return it->face_id;
4102 SAVE_IT (it_copy, *it, it_copy_data);
4103 /* Implementation note: Since move_it_in_display_line
4104 works in the iterator geometry, and thinks the first
4105 character is always the leftmost, even in R2L lines,
4106 we don't need to distinguish between the R2L and L2R
4107 cases here. */
4108 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4109 it_copy.current_x - 1, MOVE_TO_X);
4110 charpos = IT_STRING_CHARPOS (it_copy);
4111 RESTORE_IT (it, it, it_copy_data);
4112 }
4113 else
4114 {
4115 /* Set charpos to the string position of the character
4116 that comes after IT's current position in the visual
4117 order. */
4118 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4119
4120 it_copy = *it;
4121 while (n--)
4122 bidi_move_to_visually_next (&it_copy.bidi_it);
4123
4124 charpos = it_copy.bidi_it.charpos;
4125 }
4126 }
4127 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4128
4129 if (it->current.overlay_string_index >= 0)
4130 bufpos = IT_CHARPOS (*it);
4131 else
4132 bufpos = 0;
4133
4134 base_face_id = underlying_face_id (it);
4135
4136 /* Get the face for ASCII, or unibyte. */
4137 face_id = face_at_string_position (it->w,
4138 it->string,
4139 charpos,
4140 bufpos,
4141 &next_check_charpos,
4142 base_face_id, 0);
4143
4144 /* Correct the face for charsets different from ASCII. Do it
4145 for the multibyte case only. The face returned above is
4146 suitable for unibyte text if IT->string is unibyte. */
4147 if (STRING_MULTIBYTE (it->string))
4148 {
4149 struct text_pos pos1 = string_pos (charpos, it->string);
4150 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4151 int c, len;
4152 struct face *face = FACE_FROM_ID (it->f, face_id);
4153
4154 c = string_char_and_length (p, &len);
4155 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4156 }
4157 }
4158 else
4159 {
4160 struct text_pos pos;
4161
4162 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4163 || (IT_CHARPOS (*it) <= BEGV && before_p))
4164 return it->face_id;
4165
4166 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4167 pos = it->current.pos;
4168
4169 if (!it->bidi_p)
4170 {
4171 if (before_p)
4172 DEC_TEXT_POS (pos, it->multibyte_p);
4173 else
4174 {
4175 if (it->what == IT_COMPOSITION)
4176 {
4177 /* For composition, we must check the position after
4178 the composition. */
4179 pos.charpos += it->cmp_it.nchars;
4180 pos.bytepos += it->len;
4181 }
4182 else
4183 INC_TEXT_POS (pos, it->multibyte_p);
4184 }
4185 }
4186 else
4187 {
4188 if (before_p)
4189 {
4190 /* With bidi iteration, the character before the current
4191 in the visual order cannot be found by simple
4192 iteration, because "reverse" reordering is not
4193 supported. Instead, we need to use the move_it_*
4194 family of functions. */
4195 /* Ignore face changes before the first visible
4196 character on this display line. */
4197 if (it->current_x <= it->first_visible_x)
4198 return it->face_id;
4199 SAVE_IT (it_copy, *it, it_copy_data);
4200 /* Implementation note: Since move_it_in_display_line
4201 works in the iterator geometry, and thinks the first
4202 character is always the leftmost, even in R2L lines,
4203 we don't need to distinguish between the R2L and L2R
4204 cases here. */
4205 move_it_in_display_line (&it_copy, ZV,
4206 it_copy.current_x - 1, MOVE_TO_X);
4207 pos = it_copy.current.pos;
4208 RESTORE_IT (it, it, it_copy_data);
4209 }
4210 else
4211 {
4212 /* Set charpos to the buffer position of the character
4213 that comes after IT's current position in the visual
4214 order. */
4215 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4216
4217 it_copy = *it;
4218 while (n--)
4219 bidi_move_to_visually_next (&it_copy.bidi_it);
4220
4221 SET_TEXT_POS (pos,
4222 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4223 }
4224 }
4225 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4226
4227 /* Determine face for CHARSET_ASCII, or unibyte. */
4228 face_id = face_at_buffer_position (it->w,
4229 CHARPOS (pos),
4230 &next_check_charpos,
4231 limit, 0, -1);
4232
4233 /* Correct the face for charsets different from ASCII. Do it
4234 for the multibyte case only. The face returned above is
4235 suitable for unibyte text if current_buffer is unibyte. */
4236 if (it->multibyte_p)
4237 {
4238 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4239 struct face *face = FACE_FROM_ID (it->f, face_id);
4240 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4241 }
4242 }
4243
4244 return face_id;
4245 }
4246
4247
4248 \f
4249 /***********************************************************************
4250 Invisible text
4251 ***********************************************************************/
4252
4253 /* Set up iterator IT from invisible properties at its current
4254 position. Called from handle_stop. */
4255
4256 static enum prop_handled
4257 handle_invisible_prop (struct it *it)
4258 {
4259 enum prop_handled handled = HANDLED_NORMALLY;
4260 int invis_p;
4261 Lisp_Object prop;
4262
4263 if (STRINGP (it->string))
4264 {
4265 Lisp_Object end_charpos, limit, charpos;
4266
4267 /* Get the value of the invisible text property at the
4268 current position. Value will be nil if there is no such
4269 property. */
4270 charpos = make_number (IT_STRING_CHARPOS (*it));
4271 prop = Fget_text_property (charpos, Qinvisible, it->string);
4272 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4273
4274 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4275 {
4276 /* Record whether we have to display an ellipsis for the
4277 invisible text. */
4278 int display_ellipsis_p = (invis_p == 2);
4279 ptrdiff_t len, endpos;
4280
4281 handled = HANDLED_RECOMPUTE_PROPS;
4282
4283 /* Get the position at which the next visible text can be
4284 found in IT->string, if any. */
4285 endpos = len = SCHARS (it->string);
4286 XSETINT (limit, len);
4287 do
4288 {
4289 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4290 it->string, limit);
4291 if (INTEGERP (end_charpos))
4292 {
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis_p == 2)
4297 display_ellipsis_p = true;
4298 }
4299 }
4300 while (invis_p && endpos < len);
4301
4302 if (display_ellipsis_p)
4303 it->ellipsis_p = true;
4304
4305 if (endpos < len)
4306 {
4307 /* Text at END_CHARPOS is visible. Move IT there. */
4308 struct text_pos old;
4309 ptrdiff_t oldpos;
4310
4311 old = it->current.string_pos;
4312 oldpos = CHARPOS (old);
4313 if (it->bidi_p)
4314 {
4315 if (it->bidi_it.first_elt
4316 && it->bidi_it.charpos < SCHARS (it->string))
4317 bidi_paragraph_init (it->paragraph_embedding,
4318 &it->bidi_it, 1);
4319 /* Bidi-iterate out of the invisible text. */
4320 do
4321 {
4322 bidi_move_to_visually_next (&it->bidi_it);
4323 }
4324 while (oldpos <= it->bidi_it.charpos
4325 && it->bidi_it.charpos < endpos);
4326
4327 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4329 if (IT_CHARPOS (*it) >= endpos)
4330 it->prev_stop = endpos;
4331 }
4332 else
4333 {
4334 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4335 compute_string_pos (&it->current.string_pos, old, it->string);
4336 }
4337 }
4338 else
4339 {
4340 /* The rest of the string is invisible. If this is an
4341 overlay string, proceed with the next overlay string
4342 or whatever comes and return a character from there. */
4343 if (it->current.overlay_string_index >= 0
4344 && !display_ellipsis_p)
4345 {
4346 next_overlay_string (it);
4347 /* Don't check for overlay strings when we just
4348 finished processing them. */
4349 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4350 }
4351 else
4352 {
4353 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4354 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4355 }
4356 }
4357 }
4358 }
4359 else
4360 {
4361 ptrdiff_t newpos, next_stop, start_charpos, tem;
4362 Lisp_Object pos, overlay;
4363
4364 /* First of all, is there invisible text at this position? */
4365 tem = start_charpos = IT_CHARPOS (*it);
4366 pos = make_number (tem);
4367 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4368 &overlay);
4369 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4370
4371 /* If we are on invisible text, skip over it. */
4372 if (invis_p && start_charpos < it->end_charpos)
4373 {
4374 /* Record whether we have to display an ellipsis for the
4375 invisible text. */
4376 int display_ellipsis_p = invis_p == 2;
4377
4378 handled = HANDLED_RECOMPUTE_PROPS;
4379
4380 /* Loop skipping over invisible text. The loop is left at
4381 ZV or with IT on the first char being visible again. */
4382 do
4383 {
4384 /* Try to skip some invisible text. Return value is the
4385 position reached which can be equal to where we start
4386 if there is nothing invisible there. This skips both
4387 over invisible text properties and overlays with
4388 invisible property. */
4389 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4390
4391 /* If we skipped nothing at all we weren't at invisible
4392 text in the first place. If everything to the end of
4393 the buffer was skipped, end the loop. */
4394 if (newpos == tem || newpos >= ZV)
4395 invis_p = 0;
4396 else
4397 {
4398 /* We skipped some characters but not necessarily
4399 all there are. Check if we ended up on visible
4400 text. Fget_char_property returns the property of
4401 the char before the given position, i.e. if we
4402 get invis_p = 0, this means that the char at
4403 newpos is visible. */
4404 pos = make_number (newpos);
4405 prop = Fget_char_property (pos, Qinvisible, it->window);
4406 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4407 }
4408
4409 /* If we ended up on invisible text, proceed to
4410 skip starting with next_stop. */
4411 if (invis_p)
4412 tem = next_stop;
4413
4414 /* If there are adjacent invisible texts, don't lose the
4415 second one's ellipsis. */
4416 if (invis_p == 2)
4417 display_ellipsis_p = true;
4418 }
4419 while (invis_p);
4420
4421 /* The position newpos is now either ZV or on visible text. */
4422 if (it->bidi_p)
4423 {
4424 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4425 int on_newline
4426 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4427 int after_newline
4428 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4429
4430 /* If the invisible text ends on a newline or on a
4431 character after a newline, we can avoid the costly,
4432 character by character, bidi iteration to NEWPOS, and
4433 instead simply reseat the iterator there. That's
4434 because all bidi reordering information is tossed at
4435 the newline. This is a big win for modes that hide
4436 complete lines, like Outline, Org, etc. */
4437 if (on_newline || after_newline)
4438 {
4439 struct text_pos tpos;
4440 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4441
4442 SET_TEXT_POS (tpos, newpos, bpos);
4443 reseat_1 (it, tpos, 0);
4444 /* If we reseat on a newline/ZV, we need to prep the
4445 bidi iterator for advancing to the next character
4446 after the newline/EOB, keeping the current paragraph
4447 direction (so that PRODUCE_GLYPHS does TRT wrt
4448 prepending/appending glyphs to a glyph row). */
4449 if (on_newline)
4450 {
4451 it->bidi_it.first_elt = 0;
4452 it->bidi_it.paragraph_dir = pdir;
4453 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4454 it->bidi_it.nchars = 1;
4455 it->bidi_it.ch_len = 1;
4456 }
4457 }
4458 else /* Must use the slow method. */
4459 {
4460 /* With bidi iteration, the region of invisible text
4461 could start and/or end in the middle of a
4462 non-base embedding level. Therefore, we need to
4463 skip invisible text using the bidi iterator,
4464 starting at IT's current position, until we find
4465 ourselves outside of the invisible text.
4466 Skipping invisible text _after_ bidi iteration
4467 avoids affecting the visual order of the
4468 displayed text when invisible properties are
4469 added or removed. */
4470 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4471 {
4472 /* If we were `reseat'ed to a new paragraph,
4473 determine the paragraph base direction. We
4474 need to do it now because
4475 next_element_from_buffer may not have a
4476 chance to do it, if we are going to skip any
4477 text at the beginning, which resets the
4478 FIRST_ELT flag. */
4479 bidi_paragraph_init (it->paragraph_embedding,
4480 &it->bidi_it, 1);
4481 }
4482 do
4483 {
4484 bidi_move_to_visually_next (&it->bidi_it);
4485 }
4486 while (it->stop_charpos <= it->bidi_it.charpos
4487 && it->bidi_it.charpos < newpos);
4488 IT_CHARPOS (*it) = it->bidi_it.charpos;
4489 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4490 /* If we overstepped NEWPOS, record its position in
4491 the iterator, so that we skip invisible text if
4492 later the bidi iteration lands us in the
4493 invisible region again. */
4494 if (IT_CHARPOS (*it) >= newpos)
4495 it->prev_stop = newpos;
4496 }
4497 }
4498 else
4499 {
4500 IT_CHARPOS (*it) = newpos;
4501 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4502 }
4503
4504 /* If there are before-strings at the start of invisible
4505 text, and the text is invisible because of a text
4506 property, arrange to show before-strings because 20.x did
4507 it that way. (If the text is invisible because of an
4508 overlay property instead of a text property, this is
4509 already handled in the overlay code.) */
4510 if (NILP (overlay)
4511 && get_overlay_strings (it, it->stop_charpos))
4512 {
4513 handled = HANDLED_RECOMPUTE_PROPS;
4514 if (it->sp > 0)
4515 {
4516 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4517 /* The call to get_overlay_strings above recomputes
4518 it->stop_charpos, but it only considers changes
4519 in properties and overlays beyond iterator's
4520 current position. This causes us to miss changes
4521 that happen exactly where the invisible property
4522 ended. So we play it safe here and force the
4523 iterator to check for potential stop positions
4524 immediately after the invisible text. Note that
4525 if get_overlay_strings returns non-zero, it
4526 normally also pushed the iterator stack, so we
4527 need to update the stop position in the slot
4528 below the current one. */
4529 it->stack[it->sp - 1].stop_charpos
4530 = CHARPOS (it->stack[it->sp - 1].current.pos);
4531 }
4532 }
4533 else if (display_ellipsis_p)
4534 {
4535 /* Make sure that the glyphs of the ellipsis will get
4536 correct `charpos' values. If we would not update
4537 it->position here, the glyphs would belong to the
4538 last visible character _before_ the invisible
4539 text, which confuses `set_cursor_from_row'.
4540
4541 We use the last invisible position instead of the
4542 first because this way the cursor is always drawn on
4543 the first "." of the ellipsis, whenever PT is inside
4544 the invisible text. Otherwise the cursor would be
4545 placed _after_ the ellipsis when the point is after the
4546 first invisible character. */
4547 if (!STRINGP (it->object))
4548 {
4549 it->position.charpos = newpos - 1;
4550 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4551 }
4552 it->ellipsis_p = true;
4553 /* Let the ellipsis display before
4554 considering any properties of the following char.
4555 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4556 handled = HANDLED_RETURN;
4557 }
4558 }
4559 }
4560
4561 return handled;
4562 }
4563
4564
4565 /* Make iterator IT return `...' next.
4566 Replaces LEN characters from buffer. */
4567
4568 static void
4569 setup_for_ellipsis (struct it *it, int len)
4570 {
4571 /* Use the display table definition for `...'. Invalid glyphs
4572 will be handled by the method returning elements from dpvec. */
4573 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4574 {
4575 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4576 it->dpvec = v->contents;
4577 it->dpend = v->contents + v->header.size;
4578 }
4579 else
4580 {
4581 /* Default `...'. */
4582 it->dpvec = default_invis_vector;
4583 it->dpend = default_invis_vector + 3;
4584 }
4585
4586 it->dpvec_char_len = len;
4587 it->current.dpvec_index = 0;
4588 it->dpvec_face_id = -1;
4589
4590 /* Remember the current face id in case glyphs specify faces.
4591 IT's face is restored in set_iterator_to_next.
4592 saved_face_id was set to preceding char's face in handle_stop. */
4593 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4594 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4595
4596 it->method = GET_FROM_DISPLAY_VECTOR;
4597 it->ellipsis_p = true;
4598 }
4599
4600
4601 \f
4602 /***********************************************************************
4603 'display' property
4604 ***********************************************************************/
4605
4606 /* Set up iterator IT from `display' property at its current position.
4607 Called from handle_stop.
4608 We return HANDLED_RETURN if some part of the display property
4609 overrides the display of the buffer text itself.
4610 Otherwise we return HANDLED_NORMALLY. */
4611
4612 static enum prop_handled
4613 handle_display_prop (struct it *it)
4614 {
4615 Lisp_Object propval, object, overlay;
4616 struct text_pos *position;
4617 ptrdiff_t bufpos;
4618 /* Nonzero if some property replaces the display of the text itself. */
4619 int display_replaced_p = 0;
4620
4621 if (STRINGP (it->string))
4622 {
4623 object = it->string;
4624 position = &it->current.string_pos;
4625 bufpos = CHARPOS (it->current.pos);
4626 }
4627 else
4628 {
4629 XSETWINDOW (object, it->w);
4630 position = &it->current.pos;
4631 bufpos = CHARPOS (*position);
4632 }
4633
4634 /* Reset those iterator values set from display property values. */
4635 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4636 it->space_width = Qnil;
4637 it->font_height = Qnil;
4638 it->voffset = 0;
4639
4640 /* We don't support recursive `display' properties, i.e. string
4641 values that have a string `display' property, that have a string
4642 `display' property etc. */
4643 if (!it->string_from_display_prop_p)
4644 it->area = TEXT_AREA;
4645
4646 propval = get_char_property_and_overlay (make_number (position->charpos),
4647 Qdisplay, object, &overlay);
4648 if (NILP (propval))
4649 return HANDLED_NORMALLY;
4650 /* Now OVERLAY is the overlay that gave us this property, or nil
4651 if it was a text property. */
4652
4653 if (!STRINGP (it->string))
4654 object = it->w->contents;
4655
4656 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4657 position, bufpos,
4658 FRAME_WINDOW_P (it->f));
4659
4660 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4661 }
4662
4663 /* Subroutine of handle_display_prop. Returns non-zero if the display
4664 specification in SPEC is a replacing specification, i.e. it would
4665 replace the text covered by `display' property with something else,
4666 such as an image or a display string. If SPEC includes any kind or
4667 `(space ...) specification, the value is 2; this is used by
4668 compute_display_string_pos, which see.
4669
4670 See handle_single_display_spec for documentation of arguments.
4671 frame_window_p is non-zero if the window being redisplayed is on a
4672 GUI frame; this argument is used only if IT is NULL, see below.
4673
4674 IT can be NULL, if this is called by the bidi reordering code
4675 through compute_display_string_pos, which see. In that case, this
4676 function only examines SPEC, but does not otherwise "handle" it, in
4677 the sense that it doesn't set up members of IT from the display
4678 spec. */
4679 static int
4680 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4681 Lisp_Object overlay, struct text_pos *position,
4682 ptrdiff_t bufpos, int frame_window_p)
4683 {
4684 int replacing_p = 0;
4685 int rv;
4686
4687 if (CONSP (spec)
4688 /* Simple specifications. */
4689 && !EQ (XCAR (spec), Qimage)
4690 && !EQ (XCAR (spec), Qspace)
4691 && !EQ (XCAR (spec), Qwhen)
4692 && !EQ (XCAR (spec), Qslice)
4693 && !EQ (XCAR (spec), Qspace_width)
4694 && !EQ (XCAR (spec), Qheight)
4695 && !EQ (XCAR (spec), Qraise)
4696 /* Marginal area specifications. */
4697 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4698 && !EQ (XCAR (spec), Qleft_fringe)
4699 && !EQ (XCAR (spec), Qright_fringe)
4700 && !NILP (XCAR (spec)))
4701 {
4702 for (; CONSP (spec); spec = XCDR (spec))
4703 {
4704 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4705 overlay, position, bufpos,
4706 replacing_p, frame_window_p)))
4707 {
4708 replacing_p = rv;
4709 /* If some text in a string is replaced, `position' no
4710 longer points to the position of `object'. */
4711 if (!it || STRINGP (object))
4712 break;
4713 }
4714 }
4715 }
4716 else if (VECTORP (spec))
4717 {
4718 ptrdiff_t i;
4719 for (i = 0; i < ASIZE (spec); ++i)
4720 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4721 overlay, position, bufpos,
4722 replacing_p, frame_window_p)))
4723 {
4724 replacing_p = rv;
4725 /* If some text in a string is replaced, `position' no
4726 longer points to the position of `object'. */
4727 if (!it || STRINGP (object))
4728 break;
4729 }
4730 }
4731 else
4732 {
4733 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4734 position, bufpos, 0,
4735 frame_window_p)))
4736 replacing_p = rv;
4737 }
4738
4739 return replacing_p;
4740 }
4741
4742 /* Value is the position of the end of the `display' property starting
4743 at START_POS in OBJECT. */
4744
4745 static struct text_pos
4746 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4747 {
4748 Lisp_Object end;
4749 struct text_pos end_pos;
4750
4751 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4752 Qdisplay, object, Qnil);
4753 CHARPOS (end_pos) = XFASTINT (end);
4754 if (STRINGP (object))
4755 compute_string_pos (&end_pos, start_pos, it->string);
4756 else
4757 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4758
4759 return end_pos;
4760 }
4761
4762
4763 /* Set up IT from a single `display' property specification SPEC. OBJECT
4764 is the object in which the `display' property was found. *POSITION
4765 is the position in OBJECT at which the `display' property was found.
4766 BUFPOS is the buffer position of OBJECT (different from POSITION if
4767 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4768 previously saw a display specification which already replaced text
4769 display with something else, for example an image; we ignore such
4770 properties after the first one has been processed.
4771
4772 OVERLAY is the overlay this `display' property came from,
4773 or nil if it was a text property.
4774
4775 If SPEC is a `space' or `image' specification, and in some other
4776 cases too, set *POSITION to the position where the `display'
4777 property ends.
4778
4779 If IT is NULL, only examine the property specification in SPEC, but
4780 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4781 is intended to be displayed in a window on a GUI frame.
4782
4783 Value is non-zero if something was found which replaces the display
4784 of buffer or string text. */
4785
4786 static int
4787 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4788 Lisp_Object overlay, struct text_pos *position,
4789 ptrdiff_t bufpos, int display_replaced_p,
4790 int frame_window_p)
4791 {
4792 Lisp_Object form;
4793 Lisp_Object location, value;
4794 struct text_pos start_pos = *position;
4795 int valid_p;
4796
4797 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4798 If the result is non-nil, use VALUE instead of SPEC. */
4799 form = Qt;
4800 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4801 {
4802 spec = XCDR (spec);
4803 if (!CONSP (spec))
4804 return 0;
4805 form = XCAR (spec);
4806 spec = XCDR (spec);
4807 }
4808
4809 if (!NILP (form) && !EQ (form, Qt))
4810 {
4811 ptrdiff_t count = SPECPDL_INDEX ();
4812 struct gcpro gcpro1;
4813
4814 /* Bind `object' to the object having the `display' property, a
4815 buffer or string. Bind `position' to the position in the
4816 object where the property was found, and `buffer-position'
4817 to the current position in the buffer. */
4818
4819 if (NILP (object))
4820 XSETBUFFER (object, current_buffer);
4821 specbind (Qobject, object);
4822 specbind (Qposition, make_number (CHARPOS (*position)));
4823 specbind (Qbuffer_position, make_number (bufpos));
4824 GCPRO1 (form);
4825 form = safe_eval (form);
4826 UNGCPRO;
4827 unbind_to (count, Qnil);
4828 }
4829
4830 if (NILP (form))
4831 return 0;
4832
4833 /* Handle `(height HEIGHT)' specifications. */
4834 if (CONSP (spec)
4835 && EQ (XCAR (spec), Qheight)
4836 && CONSP (XCDR (spec)))
4837 {
4838 if (it)
4839 {
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4842
4843 it->font_height = XCAR (XCDR (spec));
4844 if (!NILP (it->font_height))
4845 {
4846 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4847 int new_height = -1;
4848
4849 if (CONSP (it->font_height)
4850 && (EQ (XCAR (it->font_height), Qplus)
4851 || EQ (XCAR (it->font_height), Qminus))
4852 && CONSP (XCDR (it->font_height))
4853 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4854 {
4855 /* `(+ N)' or `(- N)' where N is an integer. */
4856 int steps = XINT (XCAR (XCDR (it->font_height)));
4857 if (EQ (XCAR (it->font_height), Qplus))
4858 steps = - steps;
4859 it->face_id = smaller_face (it->f, it->face_id, steps);
4860 }
4861 else if (FUNCTIONP (it->font_height))
4862 {
4863 /* Call function with current height as argument.
4864 Value is the new height. */
4865 Lisp_Object height;
4866 height = safe_call1 (it->font_height,
4867 face->lface[LFACE_HEIGHT_INDEX]);
4868 if (NUMBERP (height))
4869 new_height = XFLOATINT (height);
4870 }
4871 else if (NUMBERP (it->font_height))
4872 {
4873 /* Value is a multiple of the canonical char height. */
4874 struct face *f;
4875
4876 f = FACE_FROM_ID (it->f,
4877 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4878 new_height = (XFLOATINT (it->font_height)
4879 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4880 }
4881 else
4882 {
4883 /* Evaluate IT->font_height with `height' bound to the
4884 current specified height to get the new height. */
4885 ptrdiff_t count = SPECPDL_INDEX ();
4886
4887 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4888 value = safe_eval (it->font_height);
4889 unbind_to (count, Qnil);
4890
4891 if (NUMBERP (value))
4892 new_height = XFLOATINT (value);
4893 }
4894
4895 if (new_height > 0)
4896 it->face_id = face_with_height (it->f, it->face_id, new_height);
4897 }
4898 }
4899
4900 return 0;
4901 }
4902
4903 /* Handle `(space-width WIDTH)'. */
4904 if (CONSP (spec)
4905 && EQ (XCAR (spec), Qspace_width)
4906 && CONSP (XCDR (spec)))
4907 {
4908 if (it)
4909 {
4910 if (!FRAME_WINDOW_P (it->f))
4911 return 0;
4912
4913 value = XCAR (XCDR (spec));
4914 if (NUMBERP (value) && XFLOATINT (value) > 0)
4915 it->space_width = value;
4916 }
4917
4918 return 0;
4919 }
4920
4921 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4922 if (CONSP (spec)
4923 && EQ (XCAR (spec), Qslice))
4924 {
4925 Lisp_Object tem;
4926
4927 if (it)
4928 {
4929 if (!FRAME_WINDOW_P (it->f))
4930 return 0;
4931
4932 if (tem = XCDR (spec), CONSP (tem))
4933 {
4934 it->slice.x = XCAR (tem);
4935 if (tem = XCDR (tem), CONSP (tem))
4936 {
4937 it->slice.y = XCAR (tem);
4938 if (tem = XCDR (tem), CONSP (tem))
4939 {
4940 it->slice.width = XCAR (tem);
4941 if (tem = XCDR (tem), CONSP (tem))
4942 it->slice.height = XCAR (tem);
4943 }
4944 }
4945 }
4946 }
4947
4948 return 0;
4949 }
4950
4951 /* Handle `(raise FACTOR)'. */
4952 if (CONSP (spec)
4953 && EQ (XCAR (spec), Qraise)
4954 && CONSP (XCDR (spec)))
4955 {
4956 if (it)
4957 {
4958 if (!FRAME_WINDOW_P (it->f))
4959 return 0;
4960
4961 #ifdef HAVE_WINDOW_SYSTEM
4962 value = XCAR (XCDR (spec));
4963 if (NUMBERP (value))
4964 {
4965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4966 it->voffset = - (XFLOATINT (value)
4967 * (FONT_HEIGHT (face->font)));
4968 }
4969 #endif /* HAVE_WINDOW_SYSTEM */
4970 }
4971
4972 return 0;
4973 }
4974
4975 /* Don't handle the other kinds of display specifications
4976 inside a string that we got from a `display' property. */
4977 if (it && it->string_from_display_prop_p)
4978 return 0;
4979
4980 /* Characters having this form of property are not displayed, so
4981 we have to find the end of the property. */
4982 if (it)
4983 {
4984 start_pos = *position;
4985 *position = display_prop_end (it, object, start_pos);
4986 }
4987 value = Qnil;
4988
4989 /* Stop the scan at that end position--we assume that all
4990 text properties change there. */
4991 if (it)
4992 it->stop_charpos = position->charpos;
4993
4994 /* Handle `(left-fringe BITMAP [FACE])'
4995 and `(right-fringe BITMAP [FACE])'. */
4996 if (CONSP (spec)
4997 && (EQ (XCAR (spec), Qleft_fringe)
4998 || EQ (XCAR (spec), Qright_fringe))
4999 && CONSP (XCDR (spec)))
5000 {
5001 int fringe_bitmap;
5002
5003 if (it)
5004 {
5005 if (!FRAME_WINDOW_P (it->f))
5006 /* If we return here, POSITION has been advanced
5007 across the text with this property. */
5008 {
5009 /* Synchronize the bidi iterator with POSITION. This is
5010 needed because we are not going to push the iterator
5011 on behalf of this display property, so there will be
5012 no pop_it call to do this synchronization for us. */
5013 if (it->bidi_p)
5014 {
5015 it->position = *position;
5016 iterate_out_of_display_property (it);
5017 *position = it->position;
5018 }
5019 /* If we were to display this fringe bitmap,
5020 next_element_from_image would have reset this flag.
5021 Do the same, to avoid affecting overlays that
5022 follow. */
5023 it->ignore_overlay_strings_at_pos_p = 0;
5024 return 1;
5025 }
5026 }
5027 else if (!frame_window_p)
5028 return 1;
5029
5030 #ifdef HAVE_WINDOW_SYSTEM
5031 value = XCAR (XCDR (spec));
5032 if (!SYMBOLP (value)
5033 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5034 /* If we return here, POSITION has been advanced
5035 across the text with this property. */
5036 {
5037 if (it && it->bidi_p)
5038 {
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5042 }
5043 if (it)
5044 /* Reset this flag like next_element_from_image would. */
5045 it->ignore_overlay_strings_at_pos_p = 0;
5046 return 1;
5047 }
5048
5049 if (it)
5050 {
5051 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5052
5053 if (CONSP (XCDR (XCDR (spec))))
5054 {
5055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5056 int face_id2 = lookup_derived_face (it->f, face_name,
5057 FRINGE_FACE_ID, 0);
5058 if (face_id2 >= 0)
5059 face_id = face_id2;
5060 }
5061
5062 /* Save current settings of IT so that we can restore them
5063 when we are finished with the glyph property value. */
5064 push_it (it, position);
5065
5066 it->area = TEXT_AREA;
5067 it->what = IT_IMAGE;
5068 it->image_id = -1; /* no image */
5069 it->position = start_pos;
5070 it->object = NILP (object) ? it->w->contents : object;
5071 it->method = GET_FROM_IMAGE;
5072 it->from_overlay = Qnil;
5073 it->face_id = face_id;
5074 it->from_disp_prop_p = true;
5075
5076 /* Say that we haven't consumed the characters with
5077 `display' property yet. The call to pop_it in
5078 set_iterator_to_next will clean this up. */
5079 *position = start_pos;
5080
5081 if (EQ (XCAR (spec), Qleft_fringe))
5082 {
5083 it->left_user_fringe_bitmap = fringe_bitmap;
5084 it->left_user_fringe_face_id = face_id;
5085 }
5086 else
5087 {
5088 it->right_user_fringe_bitmap = fringe_bitmap;
5089 it->right_user_fringe_face_id = face_id;
5090 }
5091 }
5092 #endif /* HAVE_WINDOW_SYSTEM */
5093 return 1;
5094 }
5095
5096 /* Prepare to handle `((margin left-margin) ...)',
5097 `((margin right-margin) ...)' and `((margin nil) ...)'
5098 prefixes for display specifications. */
5099 location = Qunbound;
5100 if (CONSP (spec) && CONSP (XCAR (spec)))
5101 {
5102 Lisp_Object tem;
5103
5104 value = XCDR (spec);
5105 if (CONSP (value))
5106 value = XCAR (value);
5107
5108 tem = XCAR (spec);
5109 if (EQ (XCAR (tem), Qmargin)
5110 && (tem = XCDR (tem),
5111 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5112 (NILP (tem)
5113 || EQ (tem, Qleft_margin)
5114 || EQ (tem, Qright_margin))))
5115 location = tem;
5116 }
5117
5118 if (EQ (location, Qunbound))
5119 {
5120 location = Qnil;
5121 value = spec;
5122 }
5123
5124 /* After this point, VALUE is the property after any
5125 margin prefix has been stripped. It must be a string,
5126 an image specification, or `(space ...)'.
5127
5128 LOCATION specifies where to display: `left-margin',
5129 `right-margin' or nil. */
5130
5131 valid_p = (STRINGP (value)
5132 #ifdef HAVE_WINDOW_SYSTEM
5133 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5134 && valid_image_p (value))
5135 #endif /* not HAVE_WINDOW_SYSTEM */
5136 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5137
5138 if (valid_p && !display_replaced_p)
5139 {
5140 int retval = 1;
5141
5142 if (!it)
5143 {
5144 /* Callers need to know whether the display spec is any kind
5145 of `(space ...)' spec that is about to affect text-area
5146 display. */
5147 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5148 retval = 2;
5149 return retval;
5150 }
5151
5152 /* Save current settings of IT so that we can restore them
5153 when we are finished with the glyph property value. */
5154 push_it (it, position);
5155 it->from_overlay = overlay;
5156 it->from_disp_prop_p = true;
5157
5158 if (NILP (location))
5159 it->area = TEXT_AREA;
5160 else if (EQ (location, Qleft_margin))
5161 it->area = LEFT_MARGIN_AREA;
5162 else
5163 it->area = RIGHT_MARGIN_AREA;
5164
5165 if (STRINGP (value))
5166 {
5167 it->string = value;
5168 it->multibyte_p = STRING_MULTIBYTE (it->string);
5169 it->current.overlay_string_index = -1;
5170 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5171 it->end_charpos = it->string_nchars = SCHARS (it->string);
5172 it->method = GET_FROM_STRING;
5173 it->stop_charpos = 0;
5174 it->prev_stop = 0;
5175 it->base_level_stop = 0;
5176 it->string_from_display_prop_p = true;
5177 /* Say that we haven't consumed the characters with
5178 `display' property yet. The call to pop_it in
5179 set_iterator_to_next will clean this up. */
5180 if (BUFFERP (object))
5181 *position = start_pos;
5182
5183 /* Force paragraph direction to be that of the parent
5184 object. If the parent object's paragraph direction is
5185 not yet determined, default to L2R. */
5186 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5187 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5188 else
5189 it->paragraph_embedding = L2R;
5190
5191 /* Set up the bidi iterator for this display string. */
5192 if (it->bidi_p)
5193 {
5194 it->bidi_it.string.lstring = it->string;
5195 it->bidi_it.string.s = NULL;
5196 it->bidi_it.string.schars = it->end_charpos;
5197 it->bidi_it.string.bufpos = bufpos;
5198 it->bidi_it.string.from_disp_str = 1;
5199 it->bidi_it.string.unibyte = !it->multibyte_p;
5200 it->bidi_it.w = it->w;
5201 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5202 }
5203 }
5204 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5205 {
5206 it->method = GET_FROM_STRETCH;
5207 it->object = value;
5208 *position = it->position = start_pos;
5209 retval = 1 + (it->area == TEXT_AREA);
5210 }
5211 #ifdef HAVE_WINDOW_SYSTEM
5212 else
5213 {
5214 it->what = IT_IMAGE;
5215 it->image_id = lookup_image (it->f, value);
5216 it->position = start_pos;
5217 it->object = NILP (object) ? it->w->contents : object;
5218 it->method = GET_FROM_IMAGE;
5219
5220 /* Say that we haven't consumed the characters with
5221 `display' property yet. The call to pop_it in
5222 set_iterator_to_next will clean this up. */
5223 *position = start_pos;
5224 }
5225 #endif /* HAVE_WINDOW_SYSTEM */
5226
5227 return retval;
5228 }
5229
5230 /* Invalid property or property not supported. Restore
5231 POSITION to what it was before. */
5232 *position = start_pos;
5233 return 0;
5234 }
5235
5236 /* Check if PROP is a display property value whose text should be
5237 treated as intangible. OVERLAY is the overlay from which PROP
5238 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5239 specify the buffer position covered by PROP. */
5240
5241 int
5242 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5243 ptrdiff_t charpos, ptrdiff_t bytepos)
5244 {
5245 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5246 struct text_pos position;
5247
5248 SET_TEXT_POS (position, charpos, bytepos);
5249 return handle_display_spec (NULL, prop, Qnil, overlay,
5250 &position, charpos, frame_window_p);
5251 }
5252
5253
5254 /* Return 1 if PROP is a display sub-property value containing STRING.
5255
5256 Implementation note: this and the following function are really
5257 special cases of handle_display_spec and
5258 handle_single_display_spec, and should ideally use the same code.
5259 Until they do, these two pairs must be consistent and must be
5260 modified in sync. */
5261
5262 static int
5263 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5264 {
5265 if (EQ (string, prop))
5266 return 1;
5267
5268 /* Skip over `when FORM'. */
5269 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5270 {
5271 prop = XCDR (prop);
5272 if (!CONSP (prop))
5273 return 0;
5274 /* Actually, the condition following `when' should be eval'ed,
5275 like handle_single_display_spec does, and we should return
5276 zero if it evaluates to nil. However, this function is
5277 called only when the buffer was already displayed and some
5278 glyph in the glyph matrix was found to come from a display
5279 string. Therefore, the condition was already evaluated, and
5280 the result was non-nil, otherwise the display string wouldn't
5281 have been displayed and we would have never been called for
5282 this property. Thus, we can skip the evaluation and assume
5283 its result is non-nil. */
5284 prop = XCDR (prop);
5285 }
5286
5287 if (CONSP (prop))
5288 /* Skip over `margin LOCATION'. */
5289 if (EQ (XCAR (prop), Qmargin))
5290 {
5291 prop = XCDR (prop);
5292 if (!CONSP (prop))
5293 return 0;
5294
5295 prop = XCDR (prop);
5296 if (!CONSP (prop))
5297 return 0;
5298 }
5299
5300 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5301 }
5302
5303
5304 /* Return 1 if STRING appears in the `display' property PROP. */
5305
5306 static int
5307 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5308 {
5309 if (CONSP (prop)
5310 && !EQ (XCAR (prop), Qwhen)
5311 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5312 {
5313 /* A list of sub-properties. */
5314 while (CONSP (prop))
5315 {
5316 if (single_display_spec_string_p (XCAR (prop), string))
5317 return 1;
5318 prop = XCDR (prop);
5319 }
5320 }
5321 else if (VECTORP (prop))
5322 {
5323 /* A vector of sub-properties. */
5324 ptrdiff_t i;
5325 for (i = 0; i < ASIZE (prop); ++i)
5326 if (single_display_spec_string_p (AREF (prop, i), string))
5327 return 1;
5328 }
5329 else
5330 return single_display_spec_string_p (prop, string);
5331
5332 return 0;
5333 }
5334
5335 /* Look for STRING in overlays and text properties in the current
5336 buffer, between character positions FROM and TO (excluding TO).
5337 BACK_P non-zero means look back (in this case, TO is supposed to be
5338 less than FROM).
5339 Value is the first character position where STRING was found, or
5340 zero if it wasn't found before hitting TO.
5341
5342 This function may only use code that doesn't eval because it is
5343 called asynchronously from note_mouse_highlight. */
5344
5345 static ptrdiff_t
5346 string_buffer_position_lim (Lisp_Object string,
5347 ptrdiff_t from, ptrdiff_t to, int back_p)
5348 {
5349 Lisp_Object limit, prop, pos;
5350 int found = 0;
5351
5352 pos = make_number (max (from, BEGV));
5353
5354 if (!back_p) /* looking forward */
5355 {
5356 limit = make_number (min (to, ZV));
5357 while (!found && !EQ (pos, limit))
5358 {
5359 prop = Fget_char_property (pos, Qdisplay, Qnil);
5360 if (!NILP (prop) && display_prop_string_p (prop, string))
5361 found = 1;
5362 else
5363 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5364 limit);
5365 }
5366 }
5367 else /* looking back */
5368 {
5369 limit = make_number (max (to, BEGV));
5370 while (!found && !EQ (pos, limit))
5371 {
5372 prop = Fget_char_property (pos, Qdisplay, Qnil);
5373 if (!NILP (prop) && display_prop_string_p (prop, string))
5374 found = 1;
5375 else
5376 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5377 limit);
5378 }
5379 }
5380
5381 return found ? XINT (pos) : 0;
5382 }
5383
5384 /* Determine which buffer position in current buffer STRING comes from.
5385 AROUND_CHARPOS is an approximate position where it could come from.
5386 Value is the buffer position or 0 if it couldn't be determined.
5387
5388 This function is necessary because we don't record buffer positions
5389 in glyphs generated from strings (to keep struct glyph small).
5390 This function may only use code that doesn't eval because it is
5391 called asynchronously from note_mouse_highlight. */
5392
5393 static ptrdiff_t
5394 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5395 {
5396 const int MAX_DISTANCE = 1000;
5397 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5398 around_charpos + MAX_DISTANCE,
5399 0);
5400
5401 if (!found)
5402 found = string_buffer_position_lim (string, around_charpos,
5403 around_charpos - MAX_DISTANCE, 1);
5404 return found;
5405 }
5406
5407
5408 \f
5409 /***********************************************************************
5410 `composition' property
5411 ***********************************************************************/
5412
5413 /* Set up iterator IT from `composition' property at its current
5414 position. Called from handle_stop. */
5415
5416 static enum prop_handled
5417 handle_composition_prop (struct it *it)
5418 {
5419 Lisp_Object prop, string;
5420 ptrdiff_t pos, pos_byte, start, end;
5421
5422 if (STRINGP (it->string))
5423 {
5424 unsigned char *s;
5425
5426 pos = IT_STRING_CHARPOS (*it);
5427 pos_byte = IT_STRING_BYTEPOS (*it);
5428 string = it->string;
5429 s = SDATA (string) + pos_byte;
5430 it->c = STRING_CHAR (s);
5431 }
5432 else
5433 {
5434 pos = IT_CHARPOS (*it);
5435 pos_byte = IT_BYTEPOS (*it);
5436 string = Qnil;
5437 it->c = FETCH_CHAR (pos_byte);
5438 }
5439
5440 /* If there's a valid composition and point is not inside of the
5441 composition (in the case that the composition is from the current
5442 buffer), draw a glyph composed from the composition components. */
5443 if (find_composition (pos, -1, &start, &end, &prop, string)
5444 && composition_valid_p (start, end, prop)
5445 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5446 {
5447 if (start < pos)
5448 /* As we can't handle this situation (perhaps font-lock added
5449 a new composition), we just return here hoping that next
5450 redisplay will detect this composition much earlier. */
5451 return HANDLED_NORMALLY;
5452 if (start != pos)
5453 {
5454 if (STRINGP (it->string))
5455 pos_byte = string_char_to_byte (it->string, start);
5456 else
5457 pos_byte = CHAR_TO_BYTE (start);
5458 }
5459 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5460 prop, string);
5461
5462 if (it->cmp_it.id >= 0)
5463 {
5464 it->cmp_it.ch = -1;
5465 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5466 it->cmp_it.nglyphs = -1;
5467 }
5468 }
5469
5470 return HANDLED_NORMALLY;
5471 }
5472
5473
5474 \f
5475 /***********************************************************************
5476 Overlay strings
5477 ***********************************************************************/
5478
5479 /* The following structure is used to record overlay strings for
5480 later sorting in load_overlay_strings. */
5481
5482 struct overlay_entry
5483 {
5484 Lisp_Object overlay;
5485 Lisp_Object string;
5486 EMACS_INT priority;
5487 int after_string_p;
5488 };
5489
5490
5491 /* Set up iterator IT from overlay strings at its current position.
5492 Called from handle_stop. */
5493
5494 static enum prop_handled
5495 handle_overlay_change (struct it *it)
5496 {
5497 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5498 return HANDLED_RECOMPUTE_PROPS;
5499 else
5500 return HANDLED_NORMALLY;
5501 }
5502
5503
5504 /* Set up the next overlay string for delivery by IT, if there is an
5505 overlay string to deliver. Called by set_iterator_to_next when the
5506 end of the current overlay string is reached. If there are more
5507 overlay strings to display, IT->string and
5508 IT->current.overlay_string_index are set appropriately here.
5509 Otherwise IT->string is set to nil. */
5510
5511 static void
5512 next_overlay_string (struct it *it)
5513 {
5514 ++it->current.overlay_string_index;
5515 if (it->current.overlay_string_index == it->n_overlay_strings)
5516 {
5517 /* No more overlay strings. Restore IT's settings to what
5518 they were before overlay strings were processed, and
5519 continue to deliver from current_buffer. */
5520
5521 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5522 pop_it (it);
5523 eassert (it->sp > 0
5524 || (NILP (it->string)
5525 && it->method == GET_FROM_BUFFER
5526 && it->stop_charpos >= BEGV
5527 && it->stop_charpos <= it->end_charpos));
5528 it->current.overlay_string_index = -1;
5529 it->n_overlay_strings = 0;
5530 it->overlay_strings_charpos = -1;
5531 /* If there's an empty display string on the stack, pop the
5532 stack, to resync the bidi iterator with IT's position. Such
5533 empty strings are pushed onto the stack in
5534 get_overlay_strings_1. */
5535 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5536 pop_it (it);
5537
5538 /* If we're at the end of the buffer, record that we have
5539 processed the overlay strings there already, so that
5540 next_element_from_buffer doesn't try it again. */
5541 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5542 it->overlay_strings_at_end_processed_p = true;
5543 }
5544 else
5545 {
5546 /* There are more overlay strings to process. If
5547 IT->current.overlay_string_index has advanced to a position
5548 where we must load IT->overlay_strings with more strings, do
5549 it. We must load at the IT->overlay_strings_charpos where
5550 IT->n_overlay_strings was originally computed; when invisible
5551 text is present, this might not be IT_CHARPOS (Bug#7016). */
5552 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5553
5554 if (it->current.overlay_string_index && i == 0)
5555 load_overlay_strings (it, it->overlay_strings_charpos);
5556
5557 /* Initialize IT to deliver display elements from the overlay
5558 string. */
5559 it->string = it->overlay_strings[i];
5560 it->multibyte_p = STRING_MULTIBYTE (it->string);
5561 SET_TEXT_POS (it->current.string_pos, 0, 0);
5562 it->method = GET_FROM_STRING;
5563 it->stop_charpos = 0;
5564 it->end_charpos = SCHARS (it->string);
5565 if (it->cmp_it.stop_pos >= 0)
5566 it->cmp_it.stop_pos = 0;
5567 it->prev_stop = 0;
5568 it->base_level_stop = 0;
5569
5570 /* Set up the bidi iterator for this overlay string. */
5571 if (it->bidi_p)
5572 {
5573 it->bidi_it.string.lstring = it->string;
5574 it->bidi_it.string.s = NULL;
5575 it->bidi_it.string.schars = SCHARS (it->string);
5576 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5577 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5578 it->bidi_it.string.unibyte = !it->multibyte_p;
5579 it->bidi_it.w = it->w;
5580 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5581 }
5582 }
5583
5584 CHECK_IT (it);
5585 }
5586
5587
5588 /* Compare two overlay_entry structures E1 and E2. Used as a
5589 comparison function for qsort in load_overlay_strings. Overlay
5590 strings for the same position are sorted so that
5591
5592 1. All after-strings come in front of before-strings, except
5593 when they come from the same overlay.
5594
5595 2. Within after-strings, strings are sorted so that overlay strings
5596 from overlays with higher priorities come first.
5597
5598 2. Within before-strings, strings are sorted so that overlay
5599 strings from overlays with higher priorities come last.
5600
5601 Value is analogous to strcmp. */
5602
5603
5604 static int
5605 compare_overlay_entries (const void *e1, const void *e2)
5606 {
5607 struct overlay_entry const *entry1 = e1;
5608 struct overlay_entry const *entry2 = e2;
5609 int result;
5610
5611 if (entry1->after_string_p != entry2->after_string_p)
5612 {
5613 /* Let after-strings appear in front of before-strings if
5614 they come from different overlays. */
5615 if (EQ (entry1->overlay, entry2->overlay))
5616 result = entry1->after_string_p ? 1 : -1;
5617 else
5618 result = entry1->after_string_p ? -1 : 1;
5619 }
5620 else if (entry1->priority != entry2->priority)
5621 {
5622 if (entry1->after_string_p)
5623 /* After-strings sorted in order of decreasing priority. */
5624 result = entry2->priority < entry1->priority ? -1 : 1;
5625 else
5626 /* Before-strings sorted in order of increasing priority. */
5627 result = entry1->priority < entry2->priority ? -1 : 1;
5628 }
5629 else
5630 result = 0;
5631
5632 return result;
5633 }
5634
5635
5636 /* Load the vector IT->overlay_strings with overlay strings from IT's
5637 current buffer position, or from CHARPOS if that is > 0. Set
5638 IT->n_overlays to the total number of overlay strings found.
5639
5640 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5641 a time. On entry into load_overlay_strings,
5642 IT->current.overlay_string_index gives the number of overlay
5643 strings that have already been loaded by previous calls to this
5644 function.
5645
5646 IT->add_overlay_start contains an additional overlay start
5647 position to consider for taking overlay strings from, if non-zero.
5648 This position comes into play when the overlay has an `invisible'
5649 property, and both before and after-strings. When we've skipped to
5650 the end of the overlay, because of its `invisible' property, we
5651 nevertheless want its before-string to appear.
5652 IT->add_overlay_start will contain the overlay start position
5653 in this case.
5654
5655 Overlay strings are sorted so that after-string strings come in
5656 front of before-string strings. Within before and after-strings,
5657 strings are sorted by overlay priority. See also function
5658 compare_overlay_entries. */
5659
5660 static void
5661 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5662 {
5663 Lisp_Object overlay, window, str, invisible;
5664 struct Lisp_Overlay *ov;
5665 ptrdiff_t start, end;
5666 ptrdiff_t n = 0, i, j;
5667 int invis_p;
5668 struct overlay_entry entriesbuf[20];
5669 ptrdiff_t size = ARRAYELTS (entriesbuf);
5670 struct overlay_entry *entries = entriesbuf;
5671 USE_SAFE_ALLOCA;
5672
5673 if (charpos <= 0)
5674 charpos = IT_CHARPOS (*it);
5675
5676 /* Append the overlay string STRING of overlay OVERLAY to vector
5677 `entries' which has size `size' and currently contains `n'
5678 elements. AFTER_P non-zero means STRING is an after-string of
5679 OVERLAY. */
5680 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5681 do \
5682 { \
5683 Lisp_Object priority; \
5684 \
5685 if (n == size) \
5686 { \
5687 struct overlay_entry *old = entries; \
5688 SAFE_NALLOCA (entries, 2, size); \
5689 memcpy (entries, old, size * sizeof *entries); \
5690 size *= 2; \
5691 } \
5692 \
5693 entries[n].string = (STRING); \
5694 entries[n].overlay = (OVERLAY); \
5695 priority = Foverlay_get ((OVERLAY), Qpriority); \
5696 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5697 entries[n].after_string_p = (AFTER_P); \
5698 ++n; \
5699 } \
5700 while (0)
5701
5702 /* Process overlay before the overlay center. */
5703 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5704 {
5705 XSETMISC (overlay, ov);
5706 eassert (OVERLAYP (overlay));
5707 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5708 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5709
5710 if (end < charpos)
5711 break;
5712
5713 /* Skip this overlay if it doesn't start or end at IT's current
5714 position. */
5715 if (end != charpos && start != charpos)
5716 continue;
5717
5718 /* Skip this overlay if it doesn't apply to IT->w. */
5719 window = Foverlay_get (overlay, Qwindow);
5720 if (WINDOWP (window) && XWINDOW (window) != it->w)
5721 continue;
5722
5723 /* If the text ``under'' the overlay is invisible, both before-
5724 and after-strings from this overlay are visible; start and
5725 end position are indistinguishable. */
5726 invisible = Foverlay_get (overlay, Qinvisible);
5727 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5728
5729 /* If overlay has a non-empty before-string, record it. */
5730 if ((start == charpos || (end == charpos && invis_p))
5731 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5732 && SCHARS (str))
5733 RECORD_OVERLAY_STRING (overlay, str, 0);
5734
5735 /* If overlay has a non-empty after-string, record it. */
5736 if ((end == charpos || (start == charpos && invis_p))
5737 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5738 && SCHARS (str))
5739 RECORD_OVERLAY_STRING (overlay, str, 1);
5740 }
5741
5742 /* Process overlays after the overlay center. */
5743 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5744 {
5745 XSETMISC (overlay, ov);
5746 eassert (OVERLAYP (overlay));
5747 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5748 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5749
5750 if (start > charpos)
5751 break;
5752
5753 /* Skip this overlay if it doesn't start or end at IT's current
5754 position. */
5755 if (end != charpos && start != charpos)
5756 continue;
5757
5758 /* Skip this overlay if it doesn't apply to IT->w. */
5759 window = Foverlay_get (overlay, Qwindow);
5760 if (WINDOWP (window) && XWINDOW (window) != it->w)
5761 continue;
5762
5763 /* If the text ``under'' the overlay is invisible, it has a zero
5764 dimension, and both before- and after-strings apply. */
5765 invisible = Foverlay_get (overlay, Qinvisible);
5766 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5767
5768 /* If overlay has a non-empty before-string, record it. */
5769 if ((start == charpos || (end == charpos && invis_p))
5770 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5771 && SCHARS (str))
5772 RECORD_OVERLAY_STRING (overlay, str, 0);
5773
5774 /* If overlay has a non-empty after-string, record it. */
5775 if ((end == charpos || (start == charpos && invis_p))
5776 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5777 && SCHARS (str))
5778 RECORD_OVERLAY_STRING (overlay, str, 1);
5779 }
5780
5781 #undef RECORD_OVERLAY_STRING
5782
5783 /* Sort entries. */
5784 if (n > 1)
5785 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5786
5787 /* Record number of overlay strings, and where we computed it. */
5788 it->n_overlay_strings = n;
5789 it->overlay_strings_charpos = charpos;
5790
5791 /* IT->current.overlay_string_index is the number of overlay strings
5792 that have already been consumed by IT. Copy some of the
5793 remaining overlay strings to IT->overlay_strings. */
5794 i = 0;
5795 j = it->current.overlay_string_index;
5796 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5797 {
5798 it->overlay_strings[i] = entries[j].string;
5799 it->string_overlays[i++] = entries[j++].overlay;
5800 }
5801
5802 CHECK_IT (it);
5803 SAFE_FREE ();
5804 }
5805
5806
5807 /* Get the first chunk of overlay strings at IT's current buffer
5808 position, or at CHARPOS if that is > 0. Value is non-zero if at
5809 least one overlay string was found. */
5810
5811 static int
5812 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5813 {
5814 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5815 process. This fills IT->overlay_strings with strings, and sets
5816 IT->n_overlay_strings to the total number of strings to process.
5817 IT->pos.overlay_string_index has to be set temporarily to zero
5818 because load_overlay_strings needs this; it must be set to -1
5819 when no overlay strings are found because a zero value would
5820 indicate a position in the first overlay string. */
5821 it->current.overlay_string_index = 0;
5822 load_overlay_strings (it, charpos);
5823
5824 /* If we found overlay strings, set up IT to deliver display
5825 elements from the first one. Otherwise set up IT to deliver
5826 from current_buffer. */
5827 if (it->n_overlay_strings)
5828 {
5829 /* Make sure we know settings in current_buffer, so that we can
5830 restore meaningful values when we're done with the overlay
5831 strings. */
5832 if (compute_stop_p)
5833 compute_stop_pos (it);
5834 eassert (it->face_id >= 0);
5835
5836 /* Save IT's settings. They are restored after all overlay
5837 strings have been processed. */
5838 eassert (!compute_stop_p || it->sp == 0);
5839
5840 /* When called from handle_stop, there might be an empty display
5841 string loaded. In that case, don't bother saving it. But
5842 don't use this optimization with the bidi iterator, since we
5843 need the corresponding pop_it call to resync the bidi
5844 iterator's position with IT's position, after we are done
5845 with the overlay strings. (The corresponding call to pop_it
5846 in case of an empty display string is in
5847 next_overlay_string.) */
5848 if (!(!it->bidi_p
5849 && STRINGP (it->string) && !SCHARS (it->string)))
5850 push_it (it, NULL);
5851
5852 /* Set up IT to deliver display elements from the first overlay
5853 string. */
5854 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5855 it->string = it->overlay_strings[0];
5856 it->from_overlay = Qnil;
5857 it->stop_charpos = 0;
5858 eassert (STRINGP (it->string));
5859 it->end_charpos = SCHARS (it->string);
5860 it->prev_stop = 0;
5861 it->base_level_stop = 0;
5862 it->multibyte_p = STRING_MULTIBYTE (it->string);
5863 it->method = GET_FROM_STRING;
5864 it->from_disp_prop_p = 0;
5865
5866 /* Force paragraph direction to be that of the parent
5867 buffer. */
5868 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5869 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5870 else
5871 it->paragraph_embedding = L2R;
5872
5873 /* Set up the bidi iterator for this overlay string. */
5874 if (it->bidi_p)
5875 {
5876 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5877
5878 it->bidi_it.string.lstring = it->string;
5879 it->bidi_it.string.s = NULL;
5880 it->bidi_it.string.schars = SCHARS (it->string);
5881 it->bidi_it.string.bufpos = pos;
5882 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5883 it->bidi_it.string.unibyte = !it->multibyte_p;
5884 it->bidi_it.w = it->w;
5885 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5886 }
5887 return 1;
5888 }
5889
5890 it->current.overlay_string_index = -1;
5891 return 0;
5892 }
5893
5894 static int
5895 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5896 {
5897 it->string = Qnil;
5898 it->method = GET_FROM_BUFFER;
5899
5900 (void) get_overlay_strings_1 (it, charpos, 1);
5901
5902 CHECK_IT (it);
5903
5904 /* Value is non-zero if we found at least one overlay string. */
5905 return STRINGP (it->string);
5906 }
5907
5908
5909 \f
5910 /***********************************************************************
5911 Saving and restoring state
5912 ***********************************************************************/
5913
5914 /* Save current settings of IT on IT->stack. Called, for example,
5915 before setting up IT for an overlay string, to be able to restore
5916 IT's settings to what they were after the overlay string has been
5917 processed. If POSITION is non-NULL, it is the position to save on
5918 the stack instead of IT->position. */
5919
5920 static void
5921 push_it (struct it *it, struct text_pos *position)
5922 {
5923 struct iterator_stack_entry *p;
5924
5925 eassert (it->sp < IT_STACK_SIZE);
5926 p = it->stack + it->sp;
5927
5928 p->stop_charpos = it->stop_charpos;
5929 p->prev_stop = it->prev_stop;
5930 p->base_level_stop = it->base_level_stop;
5931 p->cmp_it = it->cmp_it;
5932 eassert (it->face_id >= 0);
5933 p->face_id = it->face_id;
5934 p->string = it->string;
5935 p->method = it->method;
5936 p->from_overlay = it->from_overlay;
5937 switch (p->method)
5938 {
5939 case GET_FROM_IMAGE:
5940 p->u.image.object = it->object;
5941 p->u.image.image_id = it->image_id;
5942 p->u.image.slice = it->slice;
5943 break;
5944 case GET_FROM_STRETCH:
5945 p->u.stretch.object = it->object;
5946 break;
5947 }
5948 p->position = position ? *position : it->position;
5949 p->current = it->current;
5950 p->end_charpos = it->end_charpos;
5951 p->string_nchars = it->string_nchars;
5952 p->area = it->area;
5953 p->multibyte_p = it->multibyte_p;
5954 p->avoid_cursor_p = it->avoid_cursor_p;
5955 p->space_width = it->space_width;
5956 p->font_height = it->font_height;
5957 p->voffset = it->voffset;
5958 p->string_from_display_prop_p = it->string_from_display_prop_p;
5959 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5960 p->display_ellipsis_p = 0;
5961 p->line_wrap = it->line_wrap;
5962 p->bidi_p = it->bidi_p;
5963 p->paragraph_embedding = it->paragraph_embedding;
5964 p->from_disp_prop_p = it->from_disp_prop_p;
5965 ++it->sp;
5966
5967 /* Save the state of the bidi iterator as well. */
5968 if (it->bidi_p)
5969 bidi_push_it (&it->bidi_it);
5970 }
5971
5972 static void
5973 iterate_out_of_display_property (struct it *it)
5974 {
5975 int buffer_p = !STRINGP (it->string);
5976 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5977 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5978
5979 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5980
5981 /* Maybe initialize paragraph direction. If we are at the beginning
5982 of a new paragraph, next_element_from_buffer may not have a
5983 chance to do that. */
5984 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5985 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5986 /* prev_stop can be zero, so check against BEGV as well. */
5987 while (it->bidi_it.charpos >= bob
5988 && it->prev_stop <= it->bidi_it.charpos
5989 && it->bidi_it.charpos < CHARPOS (it->position)
5990 && it->bidi_it.charpos < eob)
5991 bidi_move_to_visually_next (&it->bidi_it);
5992 /* Record the stop_pos we just crossed, for when we cross it
5993 back, maybe. */
5994 if (it->bidi_it.charpos > CHARPOS (it->position))
5995 it->prev_stop = CHARPOS (it->position);
5996 /* If we ended up not where pop_it put us, resync IT's
5997 positional members with the bidi iterator. */
5998 if (it->bidi_it.charpos != CHARPOS (it->position))
5999 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6000 if (buffer_p)
6001 it->current.pos = it->position;
6002 else
6003 it->current.string_pos = it->position;
6004 }
6005
6006 /* Restore IT's settings from IT->stack. Called, for example, when no
6007 more overlay strings must be processed, and we return to delivering
6008 display elements from a buffer, or when the end of a string from a
6009 `display' property is reached and we return to delivering display
6010 elements from an overlay string, or from a buffer. */
6011
6012 static void
6013 pop_it (struct it *it)
6014 {
6015 struct iterator_stack_entry *p;
6016 int from_display_prop = it->from_disp_prop_p;
6017
6018 eassert (it->sp > 0);
6019 --it->sp;
6020 p = it->stack + it->sp;
6021 it->stop_charpos = p->stop_charpos;
6022 it->prev_stop = p->prev_stop;
6023 it->base_level_stop = p->base_level_stop;
6024 it->cmp_it = p->cmp_it;
6025 it->face_id = p->face_id;
6026 it->current = p->current;
6027 it->position = p->position;
6028 it->string = p->string;
6029 it->from_overlay = p->from_overlay;
6030 if (NILP (it->string))
6031 SET_TEXT_POS (it->current.string_pos, -1, -1);
6032 it->method = p->method;
6033 switch (it->method)
6034 {
6035 case GET_FROM_IMAGE:
6036 it->image_id = p->u.image.image_id;
6037 it->object = p->u.image.object;
6038 it->slice = p->u.image.slice;
6039 break;
6040 case GET_FROM_STRETCH:
6041 it->object = p->u.stretch.object;
6042 break;
6043 case GET_FROM_BUFFER:
6044 it->object = it->w->contents;
6045 break;
6046 case GET_FROM_STRING:
6047 {
6048 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6049
6050 /* Restore the face_box_p flag, since it could have been
6051 overwritten by the face of the object that we just finished
6052 displaying. */
6053 if (face)
6054 it->face_box_p = face->box != FACE_NO_BOX;
6055 it->object = it->string;
6056 }
6057 break;
6058 case GET_FROM_DISPLAY_VECTOR:
6059 if (it->s)
6060 it->method = GET_FROM_C_STRING;
6061 else if (STRINGP (it->string))
6062 it->method = GET_FROM_STRING;
6063 else
6064 {
6065 it->method = GET_FROM_BUFFER;
6066 it->object = it->w->contents;
6067 }
6068 }
6069 it->end_charpos = p->end_charpos;
6070 it->string_nchars = p->string_nchars;
6071 it->area = p->area;
6072 it->multibyte_p = p->multibyte_p;
6073 it->avoid_cursor_p = p->avoid_cursor_p;
6074 it->space_width = p->space_width;
6075 it->font_height = p->font_height;
6076 it->voffset = p->voffset;
6077 it->string_from_display_prop_p = p->string_from_display_prop_p;
6078 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6079 it->line_wrap = p->line_wrap;
6080 it->bidi_p = p->bidi_p;
6081 it->paragraph_embedding = p->paragraph_embedding;
6082 it->from_disp_prop_p = p->from_disp_prop_p;
6083 if (it->bidi_p)
6084 {
6085 bidi_pop_it (&it->bidi_it);
6086 /* Bidi-iterate until we get out of the portion of text, if any,
6087 covered by a `display' text property or by an overlay with
6088 `display' property. (We cannot just jump there, because the
6089 internal coherency of the bidi iterator state can not be
6090 preserved across such jumps.) We also must determine the
6091 paragraph base direction if the overlay we just processed is
6092 at the beginning of a new paragraph. */
6093 if (from_display_prop
6094 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6095 iterate_out_of_display_property (it);
6096
6097 eassert ((BUFFERP (it->object)
6098 && IT_CHARPOS (*it) == it->bidi_it.charpos
6099 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6100 || (STRINGP (it->object)
6101 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6102 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6103 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6104 }
6105 }
6106
6107
6108 \f
6109 /***********************************************************************
6110 Moving over lines
6111 ***********************************************************************/
6112
6113 /* Set IT's current position to the previous line start. */
6114
6115 static void
6116 back_to_previous_line_start (struct it *it)
6117 {
6118 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6119
6120 DEC_BOTH (cp, bp);
6121 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6122 }
6123
6124
6125 /* Move IT to the next line start.
6126
6127 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6128 we skipped over part of the text (as opposed to moving the iterator
6129 continuously over the text). Otherwise, don't change the value
6130 of *SKIPPED_P.
6131
6132 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6133 iterator on the newline, if it was found.
6134
6135 Newlines may come from buffer text, overlay strings, or strings
6136 displayed via the `display' property. That's the reason we can't
6137 simply use find_newline_no_quit.
6138
6139 Note that this function may not skip over invisible text that is so
6140 because of text properties and immediately follows a newline. If
6141 it would, function reseat_at_next_visible_line_start, when called
6142 from set_iterator_to_next, would effectively make invisible
6143 characters following a newline part of the wrong glyph row, which
6144 leads to wrong cursor motion. */
6145
6146 static int
6147 forward_to_next_line_start (struct it *it, int *skipped_p,
6148 struct bidi_it *bidi_it_prev)
6149 {
6150 ptrdiff_t old_selective;
6151 int newline_found_p, n;
6152 const int MAX_NEWLINE_DISTANCE = 500;
6153
6154 /* If already on a newline, just consume it to avoid unintended
6155 skipping over invisible text below. */
6156 if (it->what == IT_CHARACTER
6157 && it->c == '\n'
6158 && CHARPOS (it->position) == IT_CHARPOS (*it))
6159 {
6160 if (it->bidi_p && bidi_it_prev)
6161 *bidi_it_prev = it->bidi_it;
6162 set_iterator_to_next (it, 0);
6163 it->c = 0;
6164 return 1;
6165 }
6166
6167 /* Don't handle selective display in the following. It's (a)
6168 unnecessary because it's done by the caller, and (b) leads to an
6169 infinite recursion because next_element_from_ellipsis indirectly
6170 calls this function. */
6171 old_selective = it->selective;
6172 it->selective = 0;
6173
6174 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6175 from buffer text. */
6176 for (n = newline_found_p = 0;
6177 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6178 n += STRINGP (it->string) ? 0 : 1)
6179 {
6180 if (!get_next_display_element (it))
6181 return 0;
6182 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6183 if (newline_found_p && it->bidi_p && bidi_it_prev)
6184 *bidi_it_prev = it->bidi_it;
6185 set_iterator_to_next (it, 0);
6186 }
6187
6188 /* If we didn't find a newline near enough, see if we can use a
6189 short-cut. */
6190 if (!newline_found_p)
6191 {
6192 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6193 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6194 1, &bytepos);
6195 Lisp_Object pos;
6196
6197 eassert (!STRINGP (it->string));
6198
6199 /* If there isn't any `display' property in sight, and no
6200 overlays, we can just use the position of the newline in
6201 buffer text. */
6202 if (it->stop_charpos >= limit
6203 || ((pos = Fnext_single_property_change (make_number (start),
6204 Qdisplay, Qnil,
6205 make_number (limit)),
6206 NILP (pos))
6207 && next_overlay_change (start) == ZV))
6208 {
6209 if (!it->bidi_p)
6210 {
6211 IT_CHARPOS (*it) = limit;
6212 IT_BYTEPOS (*it) = bytepos;
6213 }
6214 else
6215 {
6216 struct bidi_it bprev;
6217
6218 /* Help bidi.c avoid expensive searches for display
6219 properties and overlays, by telling it that there are
6220 none up to `limit'. */
6221 if (it->bidi_it.disp_pos < limit)
6222 {
6223 it->bidi_it.disp_pos = limit;
6224 it->bidi_it.disp_prop = 0;
6225 }
6226 do {
6227 bprev = it->bidi_it;
6228 bidi_move_to_visually_next (&it->bidi_it);
6229 } while (it->bidi_it.charpos != limit);
6230 IT_CHARPOS (*it) = limit;
6231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6232 if (bidi_it_prev)
6233 *bidi_it_prev = bprev;
6234 }
6235 *skipped_p = newline_found_p = true;
6236 }
6237 else
6238 {
6239 while (get_next_display_element (it)
6240 && !newline_found_p)
6241 {
6242 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6243 if (newline_found_p && it->bidi_p && bidi_it_prev)
6244 *bidi_it_prev = it->bidi_it;
6245 set_iterator_to_next (it, 0);
6246 }
6247 }
6248 }
6249
6250 it->selective = old_selective;
6251 return newline_found_p;
6252 }
6253
6254
6255 /* Set IT's current position to the previous visible line start. Skip
6256 invisible text that is so either due to text properties or due to
6257 selective display. Caution: this does not change IT->current_x and
6258 IT->hpos. */
6259
6260 static void
6261 back_to_previous_visible_line_start (struct it *it)
6262 {
6263 while (IT_CHARPOS (*it) > BEGV)
6264 {
6265 back_to_previous_line_start (it);
6266
6267 if (IT_CHARPOS (*it) <= BEGV)
6268 break;
6269
6270 /* If selective > 0, then lines indented more than its value are
6271 invisible. */
6272 if (it->selective > 0
6273 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6274 it->selective))
6275 continue;
6276
6277 /* Check the newline before point for invisibility. */
6278 {
6279 Lisp_Object prop;
6280 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6281 Qinvisible, it->window);
6282 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6283 continue;
6284 }
6285
6286 if (IT_CHARPOS (*it) <= BEGV)
6287 break;
6288
6289 {
6290 struct it it2;
6291 void *it2data = NULL;
6292 ptrdiff_t pos;
6293 ptrdiff_t beg, end;
6294 Lisp_Object val, overlay;
6295
6296 SAVE_IT (it2, *it, it2data);
6297
6298 /* If newline is part of a composition, continue from start of composition */
6299 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6300 && beg < IT_CHARPOS (*it))
6301 goto replaced;
6302
6303 /* If newline is replaced by a display property, find start of overlay
6304 or interval and continue search from that point. */
6305 pos = --IT_CHARPOS (it2);
6306 --IT_BYTEPOS (it2);
6307 it2.sp = 0;
6308 bidi_unshelve_cache (NULL, 0);
6309 it2.string_from_display_prop_p = 0;
6310 it2.from_disp_prop_p = 0;
6311 if (handle_display_prop (&it2) == HANDLED_RETURN
6312 && !NILP (val = get_char_property_and_overlay
6313 (make_number (pos), Qdisplay, Qnil, &overlay))
6314 && (OVERLAYP (overlay)
6315 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6316 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6317 {
6318 RESTORE_IT (it, it, it2data);
6319 goto replaced;
6320 }
6321
6322 /* Newline is not replaced by anything -- so we are done. */
6323 RESTORE_IT (it, it, it2data);
6324 break;
6325
6326 replaced:
6327 if (beg < BEGV)
6328 beg = BEGV;
6329 IT_CHARPOS (*it) = beg;
6330 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6331 }
6332 }
6333
6334 it->continuation_lines_width = 0;
6335
6336 eassert (IT_CHARPOS (*it) >= BEGV);
6337 eassert (IT_CHARPOS (*it) == BEGV
6338 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6339 CHECK_IT (it);
6340 }
6341
6342
6343 /* Reseat iterator IT at the previous visible line start. Skip
6344 invisible text that is so either due to text properties or due to
6345 selective display. At the end, update IT's overlay information,
6346 face information etc. */
6347
6348 void
6349 reseat_at_previous_visible_line_start (struct it *it)
6350 {
6351 back_to_previous_visible_line_start (it);
6352 reseat (it, it->current.pos, 1);
6353 CHECK_IT (it);
6354 }
6355
6356
6357 /* Reseat iterator IT on the next visible line start in the current
6358 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6359 preceding the line start. Skip over invisible text that is so
6360 because of selective display. Compute faces, overlays etc at the
6361 new position. Note that this function does not skip over text that
6362 is invisible because of text properties. */
6363
6364 static void
6365 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6366 {
6367 int newline_found_p, skipped_p = 0;
6368 struct bidi_it bidi_it_prev;
6369
6370 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6371
6372 /* Skip over lines that are invisible because they are indented
6373 more than the value of IT->selective. */
6374 if (it->selective > 0)
6375 while (IT_CHARPOS (*it) < ZV
6376 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6377 it->selective))
6378 {
6379 eassert (IT_BYTEPOS (*it) == BEGV
6380 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6381 newline_found_p =
6382 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6383 }
6384
6385 /* Position on the newline if that's what's requested. */
6386 if (on_newline_p && newline_found_p)
6387 {
6388 if (STRINGP (it->string))
6389 {
6390 if (IT_STRING_CHARPOS (*it) > 0)
6391 {
6392 if (!it->bidi_p)
6393 {
6394 --IT_STRING_CHARPOS (*it);
6395 --IT_STRING_BYTEPOS (*it);
6396 }
6397 else
6398 {
6399 /* We need to restore the bidi iterator to the state
6400 it had on the newline, and resync the IT's
6401 position with that. */
6402 it->bidi_it = bidi_it_prev;
6403 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6404 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6405 }
6406 }
6407 }
6408 else if (IT_CHARPOS (*it) > BEGV)
6409 {
6410 if (!it->bidi_p)
6411 {
6412 --IT_CHARPOS (*it);
6413 --IT_BYTEPOS (*it);
6414 }
6415 else
6416 {
6417 /* We need to restore the bidi iterator to the state it
6418 had on the newline and resync IT with that. */
6419 it->bidi_it = bidi_it_prev;
6420 IT_CHARPOS (*it) = it->bidi_it.charpos;
6421 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6422 }
6423 reseat (it, it->current.pos, 0);
6424 }
6425 }
6426 else if (skipped_p)
6427 reseat (it, it->current.pos, 0);
6428
6429 CHECK_IT (it);
6430 }
6431
6432
6433 \f
6434 /***********************************************************************
6435 Changing an iterator's position
6436 ***********************************************************************/
6437
6438 /* Change IT's current position to POS in current_buffer. If FORCE_P
6439 is non-zero, always check for text properties at the new position.
6440 Otherwise, text properties are only looked up if POS >=
6441 IT->check_charpos of a property. */
6442
6443 static void
6444 reseat (struct it *it, struct text_pos pos, int force_p)
6445 {
6446 ptrdiff_t original_pos = IT_CHARPOS (*it);
6447
6448 reseat_1 (it, pos, 0);
6449
6450 /* Determine where to check text properties. Avoid doing it
6451 where possible because text property lookup is very expensive. */
6452 if (force_p
6453 || CHARPOS (pos) > it->stop_charpos
6454 || CHARPOS (pos) < original_pos)
6455 {
6456 if (it->bidi_p)
6457 {
6458 /* For bidi iteration, we need to prime prev_stop and
6459 base_level_stop with our best estimations. */
6460 /* Implementation note: Of course, POS is not necessarily a
6461 stop position, so assigning prev_pos to it is a lie; we
6462 should have called compute_stop_backwards. However, if
6463 the current buffer does not include any R2L characters,
6464 that call would be a waste of cycles, because the
6465 iterator will never move back, and thus never cross this
6466 "fake" stop position. So we delay that backward search
6467 until the time we really need it, in next_element_from_buffer. */
6468 if (CHARPOS (pos) != it->prev_stop)
6469 it->prev_stop = CHARPOS (pos);
6470 if (CHARPOS (pos) < it->base_level_stop)
6471 it->base_level_stop = 0; /* meaning it's unknown */
6472 handle_stop (it);
6473 }
6474 else
6475 {
6476 handle_stop (it);
6477 it->prev_stop = it->base_level_stop = 0;
6478 }
6479
6480 }
6481
6482 CHECK_IT (it);
6483 }
6484
6485
6486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6487 IT->stop_pos to POS, also. */
6488
6489 static void
6490 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6491 {
6492 /* Don't call this function when scanning a C string. */
6493 eassert (it->s == NULL);
6494
6495 /* POS must be a reasonable value. */
6496 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6497
6498 it->current.pos = it->position = pos;
6499 it->end_charpos = ZV;
6500 it->dpvec = NULL;
6501 it->current.dpvec_index = -1;
6502 it->current.overlay_string_index = -1;
6503 IT_STRING_CHARPOS (*it) = -1;
6504 IT_STRING_BYTEPOS (*it) = -1;
6505 it->string = Qnil;
6506 it->method = GET_FROM_BUFFER;
6507 it->object = it->w->contents;
6508 it->area = TEXT_AREA;
6509 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6510 it->sp = 0;
6511 it->string_from_display_prop_p = 0;
6512 it->string_from_prefix_prop_p = 0;
6513
6514 it->from_disp_prop_p = 0;
6515 it->face_before_selective_p = 0;
6516 if (it->bidi_p)
6517 {
6518 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6519 &it->bidi_it);
6520 bidi_unshelve_cache (NULL, 0);
6521 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6522 it->bidi_it.string.s = NULL;
6523 it->bidi_it.string.lstring = Qnil;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = 0;
6527 it->bidi_it.w = it->w;
6528 }
6529
6530 if (set_stop_p)
6531 {
6532 it->stop_charpos = CHARPOS (pos);
6533 it->base_level_stop = CHARPOS (pos);
6534 }
6535 /* This make the information stored in it->cmp_it invalidate. */
6536 it->cmp_it.id = -1;
6537 }
6538
6539
6540 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6541 If S is non-null, it is a C string to iterate over. Otherwise,
6542 STRING gives a Lisp string to iterate over.
6543
6544 If PRECISION > 0, don't return more then PRECISION number of
6545 characters from the string.
6546
6547 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6548 characters have been returned. FIELD_WIDTH < 0 means an infinite
6549 field width.
6550
6551 MULTIBYTE = 0 means disable processing of multibyte characters,
6552 MULTIBYTE > 0 means enable it,
6553 MULTIBYTE < 0 means use IT->multibyte_p.
6554
6555 IT must be initialized via a prior call to init_iterator before
6556 calling this function. */
6557
6558 static void
6559 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6560 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6561 int multibyte)
6562 {
6563 /* No text property checks performed by default, but see below. */
6564 it->stop_charpos = -1;
6565
6566 /* Set iterator position and end position. */
6567 memset (&it->current, 0, sizeof it->current);
6568 it->current.overlay_string_index = -1;
6569 it->current.dpvec_index = -1;
6570 eassert (charpos >= 0);
6571
6572 /* If STRING is specified, use its multibyteness, otherwise use the
6573 setting of MULTIBYTE, if specified. */
6574 if (multibyte >= 0)
6575 it->multibyte_p = multibyte > 0;
6576
6577 /* Bidirectional reordering of strings is controlled by the default
6578 value of bidi-display-reordering. Don't try to reorder while
6579 loading loadup.el, as the necessary character property tables are
6580 not yet available. */
6581 it->bidi_p =
6582 NILP (Vpurify_flag)
6583 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6584
6585 if (s == NULL)
6586 {
6587 eassert (STRINGP (string));
6588 it->string = string;
6589 it->s = NULL;
6590 it->end_charpos = it->string_nchars = SCHARS (string);
6591 it->method = GET_FROM_STRING;
6592 it->current.string_pos = string_pos (charpos, string);
6593
6594 if (it->bidi_p)
6595 {
6596 it->bidi_it.string.lstring = string;
6597 it->bidi_it.string.s = NULL;
6598 it->bidi_it.string.schars = it->end_charpos;
6599 it->bidi_it.string.bufpos = 0;
6600 it->bidi_it.string.from_disp_str = 0;
6601 it->bidi_it.string.unibyte = !it->multibyte_p;
6602 it->bidi_it.w = it->w;
6603 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6604 FRAME_WINDOW_P (it->f), &it->bidi_it);
6605 }
6606 }
6607 else
6608 {
6609 it->s = (const unsigned char *) s;
6610 it->string = Qnil;
6611
6612 /* Note that we use IT->current.pos, not it->current.string_pos,
6613 for displaying C strings. */
6614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6615 if (it->multibyte_p)
6616 {
6617 it->current.pos = c_string_pos (charpos, s, 1);
6618 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6619 }
6620 else
6621 {
6622 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6623 it->end_charpos = it->string_nchars = strlen (s);
6624 }
6625
6626 if (it->bidi_p)
6627 {
6628 it->bidi_it.string.lstring = Qnil;
6629 it->bidi_it.string.s = (const unsigned char *) s;
6630 it->bidi_it.string.schars = it->end_charpos;
6631 it->bidi_it.string.bufpos = 0;
6632 it->bidi_it.string.from_disp_str = 0;
6633 it->bidi_it.string.unibyte = !it->multibyte_p;
6634 it->bidi_it.w = it->w;
6635 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6636 &it->bidi_it);
6637 }
6638 it->method = GET_FROM_C_STRING;
6639 }
6640
6641 /* PRECISION > 0 means don't return more than PRECISION characters
6642 from the string. */
6643 if (precision > 0 && it->end_charpos - charpos > precision)
6644 {
6645 it->end_charpos = it->string_nchars = charpos + precision;
6646 if (it->bidi_p)
6647 it->bidi_it.string.schars = it->end_charpos;
6648 }
6649
6650 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6651 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6652 FIELD_WIDTH < 0 means infinite field width. This is useful for
6653 padding with `-' at the end of a mode line. */
6654 if (field_width < 0)
6655 field_width = INFINITY;
6656 /* Implementation note: We deliberately don't enlarge
6657 it->bidi_it.string.schars here to fit it->end_charpos, because
6658 the bidi iterator cannot produce characters out of thin air. */
6659 if (field_width > it->end_charpos - charpos)
6660 it->end_charpos = charpos + field_width;
6661
6662 /* Use the standard display table for displaying strings. */
6663 if (DISP_TABLE_P (Vstandard_display_table))
6664 it->dp = XCHAR_TABLE (Vstandard_display_table);
6665
6666 it->stop_charpos = charpos;
6667 it->prev_stop = charpos;
6668 it->base_level_stop = 0;
6669 if (it->bidi_p)
6670 {
6671 it->bidi_it.first_elt = 1;
6672 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6673 it->bidi_it.disp_pos = -1;
6674 }
6675 if (s == NULL && it->multibyte_p)
6676 {
6677 ptrdiff_t endpos = SCHARS (it->string);
6678 if (endpos > it->end_charpos)
6679 endpos = it->end_charpos;
6680 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6681 it->string);
6682 }
6683 CHECK_IT (it);
6684 }
6685
6686
6687 \f
6688 /***********************************************************************
6689 Iteration
6690 ***********************************************************************/
6691
6692 /* Map enum it_method value to corresponding next_element_from_* function. */
6693
6694 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6695 {
6696 next_element_from_buffer,
6697 next_element_from_display_vector,
6698 next_element_from_string,
6699 next_element_from_c_string,
6700 next_element_from_image,
6701 next_element_from_stretch
6702 };
6703
6704 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6705
6706
6707 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6708 (possibly with the following characters). */
6709
6710 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6711 ((IT)->cmp_it.id >= 0 \
6712 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6713 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6714 END_CHARPOS, (IT)->w, \
6715 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6716 (IT)->string)))
6717
6718
6719 /* Lookup the char-table Vglyphless_char_display for character C (-1
6720 if we want information for no-font case), and return the display
6721 method symbol. By side-effect, update it->what and
6722 it->glyphless_method. This function is called from
6723 get_next_display_element for each character element, and from
6724 x_produce_glyphs when no suitable font was found. */
6725
6726 Lisp_Object
6727 lookup_glyphless_char_display (int c, struct it *it)
6728 {
6729 Lisp_Object glyphless_method = Qnil;
6730
6731 if (CHAR_TABLE_P (Vglyphless_char_display)
6732 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6733 {
6734 if (c >= 0)
6735 {
6736 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6737 if (CONSP (glyphless_method))
6738 glyphless_method = FRAME_WINDOW_P (it->f)
6739 ? XCAR (glyphless_method)
6740 : XCDR (glyphless_method);
6741 }
6742 else
6743 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6744 }
6745
6746 retry:
6747 if (NILP (glyphless_method))
6748 {
6749 if (c >= 0)
6750 /* The default is to display the character by a proper font. */
6751 return Qnil;
6752 /* The default for the no-font case is to display an empty box. */
6753 glyphless_method = Qempty_box;
6754 }
6755 if (EQ (glyphless_method, Qzero_width))
6756 {
6757 if (c >= 0)
6758 return glyphless_method;
6759 /* This method can't be used for the no-font case. */
6760 glyphless_method = Qempty_box;
6761 }
6762 if (EQ (glyphless_method, Qthin_space))
6763 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6764 else if (EQ (glyphless_method, Qempty_box))
6765 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6766 else if (EQ (glyphless_method, Qhex_code))
6767 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6768 else if (STRINGP (glyphless_method))
6769 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6770 else
6771 {
6772 /* Invalid value. We use the default method. */
6773 glyphless_method = Qnil;
6774 goto retry;
6775 }
6776 it->what = IT_GLYPHLESS;
6777 return glyphless_method;
6778 }
6779
6780 /* Merge escape glyph face and cache the result. */
6781
6782 static struct frame *last_escape_glyph_frame = NULL;
6783 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6784 static int last_escape_glyph_merged_face_id = 0;
6785
6786 static int
6787 merge_escape_glyph_face (struct it *it)
6788 {
6789 int face_id;
6790
6791 if (it->f == last_escape_glyph_frame
6792 && it->face_id == last_escape_glyph_face_id)
6793 face_id = last_escape_glyph_merged_face_id;
6794 else
6795 {
6796 /* Merge the `escape-glyph' face into the current face. */
6797 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6798 last_escape_glyph_frame = it->f;
6799 last_escape_glyph_face_id = it->face_id;
6800 last_escape_glyph_merged_face_id = face_id;
6801 }
6802 return face_id;
6803 }
6804
6805 /* Likewise for glyphless glyph face. */
6806
6807 static struct frame *last_glyphless_glyph_frame = NULL;
6808 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6809 static int last_glyphless_glyph_merged_face_id = 0;
6810
6811 int
6812 merge_glyphless_glyph_face (struct it *it)
6813 {
6814 int face_id;
6815
6816 if (it->f == last_glyphless_glyph_frame
6817 && it->face_id == last_glyphless_glyph_face_id)
6818 face_id = last_glyphless_glyph_merged_face_id;
6819 else
6820 {
6821 /* Merge the `glyphless-char' face into the current face. */
6822 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6823 last_glyphless_glyph_frame = it->f;
6824 last_glyphless_glyph_face_id = it->face_id;
6825 last_glyphless_glyph_merged_face_id = face_id;
6826 }
6827 return face_id;
6828 }
6829
6830 /* Load IT's display element fields with information about the next
6831 display element from the current position of IT. Value is zero if
6832 end of buffer (or C string) is reached. */
6833
6834 static int
6835 get_next_display_element (struct it *it)
6836 {
6837 /* Non-zero means that we found a display element. Zero means that
6838 we hit the end of what we iterate over. Performance note: the
6839 function pointer `method' used here turns out to be faster than
6840 using a sequence of if-statements. */
6841 int success_p;
6842
6843 get_next:
6844 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6845
6846 if (it->what == IT_CHARACTER)
6847 {
6848 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6849 and only if (a) the resolved directionality of that character
6850 is R..." */
6851 /* FIXME: Do we need an exception for characters from display
6852 tables? */
6853 if (it->bidi_p && it->bidi_it.type == STRONG_R
6854 && !inhibit_bidi_mirroring)
6855 it->c = bidi_mirror_char (it->c);
6856 /* Map via display table or translate control characters.
6857 IT->c, IT->len etc. have been set to the next character by
6858 the function call above. If we have a display table, and it
6859 contains an entry for IT->c, translate it. Don't do this if
6860 IT->c itself comes from a display table, otherwise we could
6861 end up in an infinite recursion. (An alternative could be to
6862 count the recursion depth of this function and signal an
6863 error when a certain maximum depth is reached.) Is it worth
6864 it? */
6865 if (success_p && it->dpvec == NULL)
6866 {
6867 Lisp_Object dv;
6868 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6869 int nonascii_space_p = 0;
6870 int nonascii_hyphen_p = 0;
6871 int c = it->c; /* This is the character to display. */
6872
6873 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6874 {
6875 eassert (SINGLE_BYTE_CHAR_P (c));
6876 if (unibyte_display_via_language_environment)
6877 {
6878 c = DECODE_CHAR (unibyte, c);
6879 if (c < 0)
6880 c = BYTE8_TO_CHAR (it->c);
6881 }
6882 else
6883 c = BYTE8_TO_CHAR (it->c);
6884 }
6885
6886 if (it->dp
6887 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6888 VECTORP (dv)))
6889 {
6890 struct Lisp_Vector *v = XVECTOR (dv);
6891
6892 /* Return the first character from the display table
6893 entry, if not empty. If empty, don't display the
6894 current character. */
6895 if (v->header.size)
6896 {
6897 it->dpvec_char_len = it->len;
6898 it->dpvec = v->contents;
6899 it->dpend = v->contents + v->header.size;
6900 it->current.dpvec_index = 0;
6901 it->dpvec_face_id = -1;
6902 it->saved_face_id = it->face_id;
6903 it->method = GET_FROM_DISPLAY_VECTOR;
6904 it->ellipsis_p = 0;
6905 }
6906 else
6907 {
6908 set_iterator_to_next (it, 0);
6909 }
6910 goto get_next;
6911 }
6912
6913 if (! NILP (lookup_glyphless_char_display (c, it)))
6914 {
6915 if (it->what == IT_GLYPHLESS)
6916 goto done;
6917 /* Don't display this character. */
6918 set_iterator_to_next (it, 0);
6919 goto get_next;
6920 }
6921
6922 /* If `nobreak-char-display' is non-nil, we display
6923 non-ASCII spaces and hyphens specially. */
6924 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6925 {
6926 if (c == 0xA0)
6927 nonascii_space_p = true;
6928 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6929 nonascii_hyphen_p = true;
6930 }
6931
6932 /* Translate control characters into `\003' or `^C' form.
6933 Control characters coming from a display table entry are
6934 currently not translated because we use IT->dpvec to hold
6935 the translation. This could easily be changed but I
6936 don't believe that it is worth doing.
6937
6938 The characters handled by `nobreak-char-display' must be
6939 translated too.
6940
6941 Non-printable characters and raw-byte characters are also
6942 translated to octal form. */
6943 if (((c < ' ' || c == 127) /* ASCII control chars. */
6944 ? (it->area != TEXT_AREA
6945 /* In mode line, treat \n, \t like other crl chars. */
6946 || (c != '\t'
6947 && it->glyph_row
6948 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6949 || (c != '\n' && c != '\t'))
6950 : (nonascii_space_p
6951 || nonascii_hyphen_p
6952 || CHAR_BYTE8_P (c)
6953 || ! CHAR_PRINTABLE_P (c))))
6954 {
6955 /* C is a control character, non-ASCII space/hyphen,
6956 raw-byte, or a non-printable character which must be
6957 displayed either as '\003' or as `^C' where the '\\'
6958 and '^' can be defined in the display table. Fill
6959 IT->ctl_chars with glyphs for what we have to
6960 display. Then, set IT->dpvec to these glyphs. */
6961 Lisp_Object gc;
6962 int ctl_len;
6963 int face_id;
6964 int lface_id = 0;
6965 int escape_glyph;
6966
6967 /* Handle control characters with ^. */
6968
6969 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6970 {
6971 int g;
6972
6973 g = '^'; /* default glyph for Control */
6974 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6975 if (it->dp
6976 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6977 {
6978 g = GLYPH_CODE_CHAR (gc);
6979 lface_id = GLYPH_CODE_FACE (gc);
6980 }
6981
6982 face_id = (lface_id
6983 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6984 : merge_escape_glyph_face (it));
6985
6986 XSETINT (it->ctl_chars[0], g);
6987 XSETINT (it->ctl_chars[1], c ^ 0100);
6988 ctl_len = 2;
6989 goto display_control;
6990 }
6991
6992 /* Handle non-ascii space in the mode where it only gets
6993 highlighting. */
6994
6995 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6996 {
6997 /* Merge `nobreak-space' into the current face. */
6998 face_id = merge_faces (it->f, Qnobreak_space, 0,
6999 it->face_id);
7000 XSETINT (it->ctl_chars[0], ' ');
7001 ctl_len = 1;
7002 goto display_control;
7003 }
7004
7005 /* Handle sequences that start with the "escape glyph". */
7006
7007 /* the default escape glyph is \. */
7008 escape_glyph = '\\';
7009
7010 if (it->dp
7011 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7012 {
7013 escape_glyph = GLYPH_CODE_CHAR (gc);
7014 lface_id = GLYPH_CODE_FACE (gc);
7015 }
7016
7017 face_id = (lface_id
7018 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7019 : merge_escape_glyph_face (it));
7020
7021 /* Draw non-ASCII hyphen with just highlighting: */
7022
7023 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7024 {
7025 XSETINT (it->ctl_chars[0], '-');
7026 ctl_len = 1;
7027 goto display_control;
7028 }
7029
7030 /* Draw non-ASCII space/hyphen with escape glyph: */
7031
7032 if (nonascii_space_p || nonascii_hyphen_p)
7033 {
7034 XSETINT (it->ctl_chars[0], escape_glyph);
7035 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7036 ctl_len = 2;
7037 goto display_control;
7038 }
7039
7040 {
7041 char str[10];
7042 int len, i;
7043
7044 if (CHAR_BYTE8_P (c))
7045 /* Display \200 instead of \17777600. */
7046 c = CHAR_TO_BYTE8 (c);
7047 len = sprintf (str, "%03o", c);
7048
7049 XSETINT (it->ctl_chars[0], escape_glyph);
7050 for (i = 0; i < len; i++)
7051 XSETINT (it->ctl_chars[i + 1], str[i]);
7052 ctl_len = len + 1;
7053 }
7054
7055 display_control:
7056 /* Set up IT->dpvec and return first character from it. */
7057 it->dpvec_char_len = it->len;
7058 it->dpvec = it->ctl_chars;
7059 it->dpend = it->dpvec + ctl_len;
7060 it->current.dpvec_index = 0;
7061 it->dpvec_face_id = face_id;
7062 it->saved_face_id = it->face_id;
7063 it->method = GET_FROM_DISPLAY_VECTOR;
7064 it->ellipsis_p = 0;
7065 goto get_next;
7066 }
7067 it->char_to_display = c;
7068 }
7069 else if (success_p)
7070 {
7071 it->char_to_display = it->c;
7072 }
7073 }
7074
7075 #ifdef HAVE_WINDOW_SYSTEM
7076 /* Adjust face id for a multibyte character. There are no multibyte
7077 character in unibyte text. */
7078 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7079 && it->multibyte_p
7080 && success_p
7081 && FRAME_WINDOW_P (it->f))
7082 {
7083 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7084
7085 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7086 {
7087 /* Automatic composition with glyph-string. */
7088 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7089
7090 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7091 }
7092 else
7093 {
7094 ptrdiff_t pos = (it->s ? -1
7095 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7096 : IT_CHARPOS (*it));
7097 int c;
7098
7099 if (it->what == IT_CHARACTER)
7100 c = it->char_to_display;
7101 else
7102 {
7103 struct composition *cmp = composition_table[it->cmp_it.id];
7104 int i;
7105
7106 c = ' ';
7107 for (i = 0; i < cmp->glyph_len; i++)
7108 /* TAB in a composition means display glyphs with
7109 padding space on the left or right. */
7110 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7111 break;
7112 }
7113 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7114 }
7115 }
7116 #endif /* HAVE_WINDOW_SYSTEM */
7117
7118 done:
7119 /* Is this character the last one of a run of characters with
7120 box? If yes, set IT->end_of_box_run_p to 1. */
7121 if (it->face_box_p
7122 && it->s == NULL)
7123 {
7124 if (it->method == GET_FROM_STRING && it->sp)
7125 {
7126 int face_id = underlying_face_id (it);
7127 struct face *face = FACE_FROM_ID (it->f, face_id);
7128
7129 if (face)
7130 {
7131 if (face->box == FACE_NO_BOX)
7132 {
7133 /* If the box comes from face properties in a
7134 display string, check faces in that string. */
7135 int string_face_id = face_after_it_pos (it);
7136 it->end_of_box_run_p
7137 = (FACE_FROM_ID (it->f, string_face_id)->box
7138 == FACE_NO_BOX);
7139 }
7140 /* Otherwise, the box comes from the underlying face.
7141 If this is the last string character displayed, check
7142 the next buffer location. */
7143 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7144 /* n_overlay_strings is unreliable unless
7145 overlay_string_index is non-negative. */
7146 && ((it->current.overlay_string_index >= 0
7147 && (it->current.overlay_string_index
7148 == it->n_overlay_strings - 1))
7149 /* A string from display property. */
7150 || it->from_disp_prop_p))
7151 {
7152 ptrdiff_t ignore;
7153 int next_face_id;
7154 struct text_pos pos = it->current.pos;
7155
7156 /* For a string from a display property, the next
7157 buffer position is stored in the 'position'
7158 member of the iteration stack slot below the
7159 current one, see handle_single_display_spec. By
7160 contrast, it->current.pos was is not yet updated
7161 to point to that buffer position; that will
7162 happen in pop_it, after we finish displaying the
7163 current string. Note that we already checked
7164 above that it->sp is positive, so subtracting one
7165 from it is safe. */
7166 if (it->from_disp_prop_p)
7167 pos = (it->stack + it->sp - 1)->position;
7168 else
7169 INC_TEXT_POS (pos, it->multibyte_p);
7170
7171 if (CHARPOS (pos) >= ZV)
7172 it->end_of_box_run_p = true;
7173 else
7174 {
7175 next_face_id = face_at_buffer_position
7176 (it->w, CHARPOS (pos), &ignore,
7177 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7178 it->end_of_box_run_p
7179 = (FACE_FROM_ID (it->f, next_face_id)->box
7180 == FACE_NO_BOX);
7181 }
7182 }
7183 }
7184 }
7185 /* next_element_from_display_vector sets this flag according to
7186 faces of the display vector glyphs, see there. */
7187 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7188 {
7189 int face_id = face_after_it_pos (it);
7190 it->end_of_box_run_p
7191 = (face_id != it->face_id
7192 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7193 }
7194 }
7195 /* If we reached the end of the object we've been iterating (e.g., a
7196 display string or an overlay string), and there's something on
7197 IT->stack, proceed with what's on the stack. It doesn't make
7198 sense to return zero if there's unprocessed stuff on the stack,
7199 because otherwise that stuff will never be displayed. */
7200 if (!success_p && it->sp > 0)
7201 {
7202 set_iterator_to_next (it, 0);
7203 success_p = get_next_display_element (it);
7204 }
7205
7206 /* Value is 0 if end of buffer or string reached. */
7207 return success_p;
7208 }
7209
7210
7211 /* Move IT to the next display element.
7212
7213 RESEAT_P non-zero means if called on a newline in buffer text,
7214 skip to the next visible line start.
7215
7216 Functions get_next_display_element and set_iterator_to_next are
7217 separate because I find this arrangement easier to handle than a
7218 get_next_display_element function that also increments IT's
7219 position. The way it is we can first look at an iterator's current
7220 display element, decide whether it fits on a line, and if it does,
7221 increment the iterator position. The other way around we probably
7222 would either need a flag indicating whether the iterator has to be
7223 incremented the next time, or we would have to implement a
7224 decrement position function which would not be easy to write. */
7225
7226 void
7227 set_iterator_to_next (struct it *it, int reseat_p)
7228 {
7229 /* Reset flags indicating start and end of a sequence of characters
7230 with box. Reset them at the start of this function because
7231 moving the iterator to a new position might set them. */
7232 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7233
7234 switch (it->method)
7235 {
7236 case GET_FROM_BUFFER:
7237 /* The current display element of IT is a character from
7238 current_buffer. Advance in the buffer, and maybe skip over
7239 invisible lines that are so because of selective display. */
7240 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7241 reseat_at_next_visible_line_start (it, 0);
7242 else if (it->cmp_it.id >= 0)
7243 {
7244 /* We are currently getting glyphs from a composition. */
7245 if (! it->bidi_p)
7246 {
7247 IT_CHARPOS (*it) += it->cmp_it.nchars;
7248 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7249 }
7250 else
7251 {
7252 int i;
7253
7254 /* Update IT's char/byte positions to point to the first
7255 character of the next grapheme cluster, or to the
7256 character visually after the current composition. */
7257 for (i = 0; i < it->cmp_it.nchars; i++)
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_CHARPOS (*it) = it->bidi_it.charpos;
7261 }
7262
7263 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7264 && it->cmp_it.to < it->cmp_it.nglyphs)
7265 {
7266 /* Composition created while scanning forward. Proceed
7267 to the next grapheme cluster. */
7268 it->cmp_it.from = it->cmp_it.to;
7269 }
7270 else if ((it->bidi_p && it->cmp_it.reversed_p)
7271 && it->cmp_it.from > 0)
7272 {
7273 /* Composition created while scanning backward. Proceed
7274 to the previous grapheme cluster. */
7275 it->cmp_it.to = it->cmp_it.from;
7276 }
7277 else
7278 {
7279 /* No more grapheme clusters in this composition.
7280 Find the next stop position. */
7281 ptrdiff_t stop = it->end_charpos;
7282
7283 if (it->bidi_it.scan_dir < 0)
7284 /* Now we are scanning backward and don't know
7285 where to stop. */
7286 stop = -1;
7287 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7288 IT_BYTEPOS (*it), stop, Qnil);
7289 }
7290 }
7291 else
7292 {
7293 eassert (it->len != 0);
7294
7295 if (!it->bidi_p)
7296 {
7297 IT_BYTEPOS (*it) += it->len;
7298 IT_CHARPOS (*it) += 1;
7299 }
7300 else
7301 {
7302 int prev_scan_dir = it->bidi_it.scan_dir;
7303 /* If this is a new paragraph, determine its base
7304 direction (a.k.a. its base embedding level). */
7305 if (it->bidi_it.new_paragraph)
7306 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7307 bidi_move_to_visually_next (&it->bidi_it);
7308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7309 IT_CHARPOS (*it) = it->bidi_it.charpos;
7310 if (prev_scan_dir != it->bidi_it.scan_dir)
7311 {
7312 /* As the scan direction was changed, we must
7313 re-compute the stop position for composition. */
7314 ptrdiff_t stop = it->end_charpos;
7315 if (it->bidi_it.scan_dir < 0)
7316 stop = -1;
7317 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7318 IT_BYTEPOS (*it), stop, Qnil);
7319 }
7320 }
7321 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7322 }
7323 break;
7324
7325 case GET_FROM_C_STRING:
7326 /* Current display element of IT is from a C string. */
7327 if (!it->bidi_p
7328 /* If the string position is beyond string's end, it means
7329 next_element_from_c_string is padding the string with
7330 blanks, in which case we bypass the bidi iterator,
7331 because it cannot deal with such virtual characters. */
7332 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7333 {
7334 IT_BYTEPOS (*it) += it->len;
7335 IT_CHARPOS (*it) += 1;
7336 }
7337 else
7338 {
7339 bidi_move_to_visually_next (&it->bidi_it);
7340 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7341 IT_CHARPOS (*it) = it->bidi_it.charpos;
7342 }
7343 break;
7344
7345 case GET_FROM_DISPLAY_VECTOR:
7346 /* Current display element of IT is from a display table entry.
7347 Advance in the display table definition. Reset it to null if
7348 end reached, and continue with characters from buffers/
7349 strings. */
7350 ++it->current.dpvec_index;
7351
7352 /* Restore face of the iterator to what they were before the
7353 display vector entry (these entries may contain faces). */
7354 it->face_id = it->saved_face_id;
7355
7356 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7357 {
7358 int recheck_faces = it->ellipsis_p;
7359
7360 if (it->s)
7361 it->method = GET_FROM_C_STRING;
7362 else if (STRINGP (it->string))
7363 it->method = GET_FROM_STRING;
7364 else
7365 {
7366 it->method = GET_FROM_BUFFER;
7367 it->object = it->w->contents;
7368 }
7369
7370 it->dpvec = NULL;
7371 it->current.dpvec_index = -1;
7372
7373 /* Skip over characters which were displayed via IT->dpvec. */
7374 if (it->dpvec_char_len < 0)
7375 reseat_at_next_visible_line_start (it, 1);
7376 else if (it->dpvec_char_len > 0)
7377 {
7378 if (it->method == GET_FROM_STRING
7379 && it->current.overlay_string_index >= 0
7380 && it->n_overlay_strings > 0)
7381 it->ignore_overlay_strings_at_pos_p = true;
7382 it->len = it->dpvec_char_len;
7383 set_iterator_to_next (it, reseat_p);
7384 }
7385
7386 /* Maybe recheck faces after display vector. */
7387 if (recheck_faces)
7388 it->stop_charpos = IT_CHARPOS (*it);
7389 }
7390 break;
7391
7392 case GET_FROM_STRING:
7393 /* Current display element is a character from a Lisp string. */
7394 eassert (it->s == NULL && STRINGP (it->string));
7395 /* Don't advance past string end. These conditions are true
7396 when set_iterator_to_next is called at the end of
7397 get_next_display_element, in which case the Lisp string is
7398 already exhausted, and all we want is pop the iterator
7399 stack. */
7400 if (it->current.overlay_string_index >= 0)
7401 {
7402 /* This is an overlay string, so there's no padding with
7403 spaces, and the number of characters in the string is
7404 where the string ends. */
7405 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 goto consider_string_end;
7407 }
7408 else
7409 {
7410 /* Not an overlay string. There could be padding, so test
7411 against it->end_charpos. */
7412 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7413 goto consider_string_end;
7414 }
7415 if (it->cmp_it.id >= 0)
7416 {
7417 /* We are delivering display elements from a composition.
7418 Update the string position past the grapheme cluster
7419 we've just processed. */
7420 if (! it->bidi_p)
7421 {
7422 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7423 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7424 }
7425 else
7426 {
7427 int i;
7428
7429 for (i = 0; i < it->cmp_it.nchars; i++)
7430 bidi_move_to_visually_next (&it->bidi_it);
7431 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7432 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7433 }
7434
7435 /* Did we exhaust all the grapheme clusters of this
7436 composition? */
7437 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7438 && (it->cmp_it.to < it->cmp_it.nglyphs))
7439 {
7440 /* Not all the grapheme clusters were processed yet;
7441 advance to the next cluster. */
7442 it->cmp_it.from = it->cmp_it.to;
7443 }
7444 else if ((it->bidi_p && it->cmp_it.reversed_p)
7445 && it->cmp_it.from > 0)
7446 {
7447 /* Likewise: advance to the next cluster, but going in
7448 the reverse direction. */
7449 it->cmp_it.to = it->cmp_it.from;
7450 }
7451 else
7452 {
7453 /* This composition was fully processed; find the next
7454 candidate place for checking for composed
7455 characters. */
7456 /* Always limit string searches to the string length;
7457 any padding spaces are not part of the string, and
7458 there cannot be any compositions in that padding. */
7459 ptrdiff_t stop = SCHARS (it->string);
7460
7461 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7462 stop = -1;
7463 else if (it->end_charpos < stop)
7464 {
7465 /* Cf. PRECISION in reseat_to_string: we might be
7466 limited in how many of the string characters we
7467 need to deliver. */
7468 stop = it->end_charpos;
7469 }
7470 composition_compute_stop_pos (&it->cmp_it,
7471 IT_STRING_CHARPOS (*it),
7472 IT_STRING_BYTEPOS (*it), stop,
7473 it->string);
7474 }
7475 }
7476 else
7477 {
7478 if (!it->bidi_p
7479 /* If the string position is beyond string's end, it
7480 means next_element_from_string is padding the string
7481 with blanks, in which case we bypass the bidi
7482 iterator, because it cannot deal with such virtual
7483 characters. */
7484 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7485 {
7486 IT_STRING_BYTEPOS (*it) += it->len;
7487 IT_STRING_CHARPOS (*it) += 1;
7488 }
7489 else
7490 {
7491 int prev_scan_dir = it->bidi_it.scan_dir;
7492
7493 bidi_move_to_visually_next (&it->bidi_it);
7494 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7495 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7496 /* If the scan direction changes, we may need to update
7497 the place where to check for composed characters. */
7498 if (prev_scan_dir != it->bidi_it.scan_dir)
7499 {
7500 ptrdiff_t stop = SCHARS (it->string);
7501
7502 if (it->bidi_it.scan_dir < 0)
7503 stop = -1;
7504 else if (it->end_charpos < stop)
7505 stop = it->end_charpos;
7506
7507 composition_compute_stop_pos (&it->cmp_it,
7508 IT_STRING_CHARPOS (*it),
7509 IT_STRING_BYTEPOS (*it), stop,
7510 it->string);
7511 }
7512 }
7513 }
7514
7515 consider_string_end:
7516
7517 if (it->current.overlay_string_index >= 0)
7518 {
7519 /* IT->string is an overlay string. Advance to the
7520 next, if there is one. */
7521 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7522 {
7523 it->ellipsis_p = 0;
7524 next_overlay_string (it);
7525 if (it->ellipsis_p)
7526 setup_for_ellipsis (it, 0);
7527 }
7528 }
7529 else
7530 {
7531 /* IT->string is not an overlay string. If we reached
7532 its end, and there is something on IT->stack, proceed
7533 with what is on the stack. This can be either another
7534 string, this time an overlay string, or a buffer. */
7535 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7536 && it->sp > 0)
7537 {
7538 pop_it (it);
7539 if (it->method == GET_FROM_STRING)
7540 goto consider_string_end;
7541 }
7542 }
7543 break;
7544
7545 case GET_FROM_IMAGE:
7546 case GET_FROM_STRETCH:
7547 /* The position etc with which we have to proceed are on
7548 the stack. The position may be at the end of a string,
7549 if the `display' property takes up the whole string. */
7550 eassert (it->sp > 0);
7551 pop_it (it);
7552 if (it->method == GET_FROM_STRING)
7553 goto consider_string_end;
7554 break;
7555
7556 default:
7557 /* There are no other methods defined, so this should be a bug. */
7558 emacs_abort ();
7559 }
7560
7561 eassert (it->method != GET_FROM_STRING
7562 || (STRINGP (it->string)
7563 && IT_STRING_CHARPOS (*it) >= 0));
7564 }
7565
7566 /* Load IT's display element fields with information about the next
7567 display element which comes from a display table entry or from the
7568 result of translating a control character to one of the forms `^C'
7569 or `\003'.
7570
7571 IT->dpvec holds the glyphs to return as characters.
7572 IT->saved_face_id holds the face id before the display vector--it
7573 is restored into IT->face_id in set_iterator_to_next. */
7574
7575 static int
7576 next_element_from_display_vector (struct it *it)
7577 {
7578 Lisp_Object gc;
7579 int prev_face_id = it->face_id;
7580 int next_face_id;
7581
7582 /* Precondition. */
7583 eassert (it->dpvec && it->current.dpvec_index >= 0);
7584
7585 it->face_id = it->saved_face_id;
7586
7587 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7588 That seemed totally bogus - so I changed it... */
7589 gc = it->dpvec[it->current.dpvec_index];
7590
7591 if (GLYPH_CODE_P (gc))
7592 {
7593 struct face *this_face, *prev_face, *next_face;
7594
7595 it->c = GLYPH_CODE_CHAR (gc);
7596 it->len = CHAR_BYTES (it->c);
7597
7598 /* The entry may contain a face id to use. Such a face id is
7599 the id of a Lisp face, not a realized face. A face id of
7600 zero means no face is specified. */
7601 if (it->dpvec_face_id >= 0)
7602 it->face_id = it->dpvec_face_id;
7603 else
7604 {
7605 int lface_id = GLYPH_CODE_FACE (gc);
7606 if (lface_id > 0)
7607 it->face_id = merge_faces (it->f, Qt, lface_id,
7608 it->saved_face_id);
7609 }
7610
7611 /* Glyphs in the display vector could have the box face, so we
7612 need to set the related flags in the iterator, as
7613 appropriate. */
7614 this_face = FACE_FROM_ID (it->f, it->face_id);
7615 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7616
7617 /* Is this character the first character of a box-face run? */
7618 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7619 && (!prev_face
7620 || prev_face->box == FACE_NO_BOX));
7621
7622 /* For the last character of the box-face run, we need to look
7623 either at the next glyph from the display vector, or at the
7624 face we saw before the display vector. */
7625 next_face_id = it->saved_face_id;
7626 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7627 {
7628 if (it->dpvec_face_id >= 0)
7629 next_face_id = it->dpvec_face_id;
7630 else
7631 {
7632 int lface_id =
7633 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7634
7635 if (lface_id > 0)
7636 next_face_id = merge_faces (it->f, Qt, lface_id,
7637 it->saved_face_id);
7638 }
7639 }
7640 next_face = FACE_FROM_ID (it->f, next_face_id);
7641 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7642 && (!next_face
7643 || next_face->box == FACE_NO_BOX));
7644 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7645 }
7646 else
7647 /* Display table entry is invalid. Return a space. */
7648 it->c = ' ', it->len = 1;
7649
7650 /* Don't change position and object of the iterator here. They are
7651 still the values of the character that had this display table
7652 entry or was translated, and that's what we want. */
7653 it->what = IT_CHARACTER;
7654 return 1;
7655 }
7656
7657 /* Get the first element of string/buffer in the visual order, after
7658 being reseated to a new position in a string or a buffer. */
7659 static void
7660 get_visually_first_element (struct it *it)
7661 {
7662 int string_p = STRINGP (it->string) || it->s;
7663 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7664 ptrdiff_t bob = (string_p ? 0 : BEGV);
7665
7666 if (STRINGP (it->string))
7667 {
7668 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7669 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7670 }
7671 else
7672 {
7673 it->bidi_it.charpos = IT_CHARPOS (*it);
7674 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7675 }
7676
7677 if (it->bidi_it.charpos == eob)
7678 {
7679 /* Nothing to do, but reset the FIRST_ELT flag, like
7680 bidi_paragraph_init does, because we are not going to
7681 call it. */
7682 it->bidi_it.first_elt = 0;
7683 }
7684 else if (it->bidi_it.charpos == bob
7685 || (!string_p
7686 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7687 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7688 {
7689 /* If we are at the beginning of a line/string, we can produce
7690 the next element right away. */
7691 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7692 bidi_move_to_visually_next (&it->bidi_it);
7693 }
7694 else
7695 {
7696 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7697
7698 /* We need to prime the bidi iterator starting at the line's or
7699 string's beginning, before we will be able to produce the
7700 next element. */
7701 if (string_p)
7702 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7703 else
7704 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7705 IT_BYTEPOS (*it), -1,
7706 &it->bidi_it.bytepos);
7707 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7708 do
7709 {
7710 /* Now return to buffer/string position where we were asked
7711 to get the next display element, and produce that. */
7712 bidi_move_to_visually_next (&it->bidi_it);
7713 }
7714 while (it->bidi_it.bytepos != orig_bytepos
7715 && it->bidi_it.charpos < eob);
7716 }
7717
7718 /* Adjust IT's position information to where we ended up. */
7719 if (STRINGP (it->string))
7720 {
7721 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7722 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7723 }
7724 else
7725 {
7726 IT_CHARPOS (*it) = it->bidi_it.charpos;
7727 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7728 }
7729
7730 if (STRINGP (it->string) || !it->s)
7731 {
7732 ptrdiff_t stop, charpos, bytepos;
7733
7734 if (STRINGP (it->string))
7735 {
7736 eassert (!it->s);
7737 stop = SCHARS (it->string);
7738 if (stop > it->end_charpos)
7739 stop = it->end_charpos;
7740 charpos = IT_STRING_CHARPOS (*it);
7741 bytepos = IT_STRING_BYTEPOS (*it);
7742 }
7743 else
7744 {
7745 stop = it->end_charpos;
7746 charpos = IT_CHARPOS (*it);
7747 bytepos = IT_BYTEPOS (*it);
7748 }
7749 if (it->bidi_it.scan_dir < 0)
7750 stop = -1;
7751 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7752 it->string);
7753 }
7754 }
7755
7756 /* Load IT with the next display element from Lisp string IT->string.
7757 IT->current.string_pos is the current position within the string.
7758 If IT->current.overlay_string_index >= 0, the Lisp string is an
7759 overlay string. */
7760
7761 static int
7762 next_element_from_string (struct it *it)
7763 {
7764 struct text_pos position;
7765
7766 eassert (STRINGP (it->string));
7767 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7768 eassert (IT_STRING_CHARPOS (*it) >= 0);
7769 position = it->current.string_pos;
7770
7771 /* With bidi reordering, the character to display might not be the
7772 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7773 that we were reseat()ed to a new string, whose paragraph
7774 direction is not known. */
7775 if (it->bidi_p && it->bidi_it.first_elt)
7776 {
7777 get_visually_first_element (it);
7778 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7779 }
7780
7781 /* Time to check for invisible text? */
7782 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7783 {
7784 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7785 {
7786 if (!(!it->bidi_p
7787 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7788 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7789 {
7790 /* With bidi non-linear iteration, we could find
7791 ourselves far beyond the last computed stop_charpos,
7792 with several other stop positions in between that we
7793 missed. Scan them all now, in buffer's logical
7794 order, until we find and handle the last stop_charpos
7795 that precedes our current position. */
7796 handle_stop_backwards (it, it->stop_charpos);
7797 return GET_NEXT_DISPLAY_ELEMENT (it);
7798 }
7799 else
7800 {
7801 if (it->bidi_p)
7802 {
7803 /* Take note of the stop position we just moved
7804 across, for when we will move back across it. */
7805 it->prev_stop = it->stop_charpos;
7806 /* If we are at base paragraph embedding level, take
7807 note of the last stop position seen at this
7808 level. */
7809 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7810 it->base_level_stop = it->stop_charpos;
7811 }
7812 handle_stop (it);
7813
7814 /* Since a handler may have changed IT->method, we must
7815 recurse here. */
7816 return GET_NEXT_DISPLAY_ELEMENT (it);
7817 }
7818 }
7819 else if (it->bidi_p
7820 /* If we are before prev_stop, we may have overstepped
7821 on our way backwards a stop_pos, and if so, we need
7822 to handle that stop_pos. */
7823 && IT_STRING_CHARPOS (*it) < it->prev_stop
7824 /* We can sometimes back up for reasons that have nothing
7825 to do with bidi reordering. E.g., compositions. The
7826 code below is only needed when we are above the base
7827 embedding level, so test for that explicitly. */
7828 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7829 {
7830 /* If we lost track of base_level_stop, we have no better
7831 place for handle_stop_backwards to start from than string
7832 beginning. This happens, e.g., when we were reseated to
7833 the previous screenful of text by vertical-motion. */
7834 if (it->base_level_stop <= 0
7835 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7836 it->base_level_stop = 0;
7837 handle_stop_backwards (it, it->base_level_stop);
7838 return GET_NEXT_DISPLAY_ELEMENT (it);
7839 }
7840 }
7841
7842 if (it->current.overlay_string_index >= 0)
7843 {
7844 /* Get the next character from an overlay string. In overlay
7845 strings, there is no field width or padding with spaces to
7846 do. */
7847 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7848 {
7849 it->what = IT_EOB;
7850 return 0;
7851 }
7852 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7853 IT_STRING_BYTEPOS (*it),
7854 it->bidi_it.scan_dir < 0
7855 ? -1
7856 : SCHARS (it->string))
7857 && next_element_from_composition (it))
7858 {
7859 return 1;
7860 }
7861 else if (STRING_MULTIBYTE (it->string))
7862 {
7863 const unsigned char *s = (SDATA (it->string)
7864 + IT_STRING_BYTEPOS (*it));
7865 it->c = string_char_and_length (s, &it->len);
7866 }
7867 else
7868 {
7869 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7870 it->len = 1;
7871 }
7872 }
7873 else
7874 {
7875 /* Get the next character from a Lisp string that is not an
7876 overlay string. Such strings come from the mode line, for
7877 example. We may have to pad with spaces, or truncate the
7878 string. See also next_element_from_c_string. */
7879 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7880 {
7881 it->what = IT_EOB;
7882 return 0;
7883 }
7884 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7885 {
7886 /* Pad with spaces. */
7887 it->c = ' ', it->len = 1;
7888 CHARPOS (position) = BYTEPOS (position) = -1;
7889 }
7890 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7891 IT_STRING_BYTEPOS (*it),
7892 it->bidi_it.scan_dir < 0
7893 ? -1
7894 : it->string_nchars)
7895 && next_element_from_composition (it))
7896 {
7897 return 1;
7898 }
7899 else if (STRING_MULTIBYTE (it->string))
7900 {
7901 const unsigned char *s = (SDATA (it->string)
7902 + IT_STRING_BYTEPOS (*it));
7903 it->c = string_char_and_length (s, &it->len);
7904 }
7905 else
7906 {
7907 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7908 it->len = 1;
7909 }
7910 }
7911
7912 /* Record what we have and where it came from. */
7913 it->what = IT_CHARACTER;
7914 it->object = it->string;
7915 it->position = position;
7916 return 1;
7917 }
7918
7919
7920 /* Load IT with next display element from C string IT->s.
7921 IT->string_nchars is the maximum number of characters to return
7922 from the string. IT->end_charpos may be greater than
7923 IT->string_nchars when this function is called, in which case we
7924 may have to return padding spaces. Value is zero if end of string
7925 reached, including padding spaces. */
7926
7927 static int
7928 next_element_from_c_string (struct it *it)
7929 {
7930 bool success_p = true;
7931
7932 eassert (it->s);
7933 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7934 it->what = IT_CHARACTER;
7935 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7936 it->object = Qnil;
7937
7938 /* With bidi reordering, the character to display might not be the
7939 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7940 we were reseated to a new string, whose paragraph direction is
7941 not known. */
7942 if (it->bidi_p && it->bidi_it.first_elt)
7943 get_visually_first_element (it);
7944
7945 /* IT's position can be greater than IT->string_nchars in case a
7946 field width or precision has been specified when the iterator was
7947 initialized. */
7948 if (IT_CHARPOS (*it) >= it->end_charpos)
7949 {
7950 /* End of the game. */
7951 it->what = IT_EOB;
7952 success_p = 0;
7953 }
7954 else if (IT_CHARPOS (*it) >= it->string_nchars)
7955 {
7956 /* Pad with spaces. */
7957 it->c = ' ', it->len = 1;
7958 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7959 }
7960 else if (it->multibyte_p)
7961 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7962 else
7963 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7964
7965 return success_p;
7966 }
7967
7968
7969 /* Set up IT to return characters from an ellipsis, if appropriate.
7970 The definition of the ellipsis glyphs may come from a display table
7971 entry. This function fills IT with the first glyph from the
7972 ellipsis if an ellipsis is to be displayed. */
7973
7974 static int
7975 next_element_from_ellipsis (struct it *it)
7976 {
7977 if (it->selective_display_ellipsis_p)
7978 setup_for_ellipsis (it, it->len);
7979 else
7980 {
7981 /* The face at the current position may be different from the
7982 face we find after the invisible text. Remember what it
7983 was in IT->saved_face_id, and signal that it's there by
7984 setting face_before_selective_p. */
7985 it->saved_face_id = it->face_id;
7986 it->method = GET_FROM_BUFFER;
7987 it->object = it->w->contents;
7988 reseat_at_next_visible_line_start (it, 1);
7989 it->face_before_selective_p = true;
7990 }
7991
7992 return GET_NEXT_DISPLAY_ELEMENT (it);
7993 }
7994
7995
7996 /* Deliver an image display element. The iterator IT is already
7997 filled with image information (done in handle_display_prop). Value
7998 is always 1. */
7999
8000
8001 static int
8002 next_element_from_image (struct it *it)
8003 {
8004 it->what = IT_IMAGE;
8005 it->ignore_overlay_strings_at_pos_p = 0;
8006 return 1;
8007 }
8008
8009
8010 /* Fill iterator IT with next display element from a stretch glyph
8011 property. IT->object is the value of the text property. Value is
8012 always 1. */
8013
8014 static int
8015 next_element_from_stretch (struct it *it)
8016 {
8017 it->what = IT_STRETCH;
8018 return 1;
8019 }
8020
8021 /* Scan backwards from IT's current position until we find a stop
8022 position, or until BEGV. This is called when we find ourself
8023 before both the last known prev_stop and base_level_stop while
8024 reordering bidirectional text. */
8025
8026 static void
8027 compute_stop_pos_backwards (struct it *it)
8028 {
8029 const int SCAN_BACK_LIMIT = 1000;
8030 struct text_pos pos;
8031 struct display_pos save_current = it->current;
8032 struct text_pos save_position = it->position;
8033 ptrdiff_t charpos = IT_CHARPOS (*it);
8034 ptrdiff_t where_we_are = charpos;
8035 ptrdiff_t save_stop_pos = it->stop_charpos;
8036 ptrdiff_t save_end_pos = it->end_charpos;
8037
8038 eassert (NILP (it->string) && !it->s);
8039 eassert (it->bidi_p);
8040 it->bidi_p = 0;
8041 do
8042 {
8043 it->end_charpos = min (charpos + 1, ZV);
8044 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8045 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8046 reseat_1 (it, pos, 0);
8047 compute_stop_pos (it);
8048 /* We must advance forward, right? */
8049 if (it->stop_charpos <= charpos)
8050 emacs_abort ();
8051 }
8052 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8053
8054 if (it->stop_charpos <= where_we_are)
8055 it->prev_stop = it->stop_charpos;
8056 else
8057 it->prev_stop = BEGV;
8058 it->bidi_p = true;
8059 it->current = save_current;
8060 it->position = save_position;
8061 it->stop_charpos = save_stop_pos;
8062 it->end_charpos = save_end_pos;
8063 }
8064
8065 /* Scan forward from CHARPOS in the current buffer/string, until we
8066 find a stop position > current IT's position. Then handle the stop
8067 position before that. This is called when we bump into a stop
8068 position while reordering bidirectional text. CHARPOS should be
8069 the last previously processed stop_pos (or BEGV/0, if none were
8070 processed yet) whose position is less that IT's current
8071 position. */
8072
8073 static void
8074 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8075 {
8076 int bufp = !STRINGP (it->string);
8077 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8078 struct display_pos save_current = it->current;
8079 struct text_pos save_position = it->position;
8080 struct text_pos pos1;
8081 ptrdiff_t next_stop;
8082
8083 /* Scan in strict logical order. */
8084 eassert (it->bidi_p);
8085 it->bidi_p = 0;
8086 do
8087 {
8088 it->prev_stop = charpos;
8089 if (bufp)
8090 {
8091 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8092 reseat_1 (it, pos1, 0);
8093 }
8094 else
8095 it->current.string_pos = string_pos (charpos, it->string);
8096 compute_stop_pos (it);
8097 /* We must advance forward, right? */
8098 if (it->stop_charpos <= it->prev_stop)
8099 emacs_abort ();
8100 charpos = it->stop_charpos;
8101 }
8102 while (charpos <= where_we_are);
8103
8104 it->bidi_p = true;
8105 it->current = save_current;
8106 it->position = save_position;
8107 next_stop = it->stop_charpos;
8108 it->stop_charpos = it->prev_stop;
8109 handle_stop (it);
8110 it->stop_charpos = next_stop;
8111 }
8112
8113 /* Load IT with the next display element from current_buffer. Value
8114 is zero if end of buffer reached. IT->stop_charpos is the next
8115 position at which to stop and check for text properties or buffer
8116 end. */
8117
8118 static int
8119 next_element_from_buffer (struct it *it)
8120 {
8121 bool success_p = true;
8122
8123 eassert (IT_CHARPOS (*it) >= BEGV);
8124 eassert (NILP (it->string) && !it->s);
8125 eassert (!it->bidi_p
8126 || (EQ (it->bidi_it.string.lstring, Qnil)
8127 && it->bidi_it.string.s == NULL));
8128
8129 /* With bidi reordering, the character to display might not be the
8130 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8131 we were reseat()ed to a new buffer position, which is potentially
8132 a different paragraph. */
8133 if (it->bidi_p && it->bidi_it.first_elt)
8134 {
8135 get_visually_first_element (it);
8136 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8137 }
8138
8139 if (IT_CHARPOS (*it) >= it->stop_charpos)
8140 {
8141 if (IT_CHARPOS (*it) >= it->end_charpos)
8142 {
8143 int overlay_strings_follow_p;
8144
8145 /* End of the game, except when overlay strings follow that
8146 haven't been returned yet. */
8147 if (it->overlay_strings_at_end_processed_p)
8148 overlay_strings_follow_p = 0;
8149 else
8150 {
8151 it->overlay_strings_at_end_processed_p = true;
8152 overlay_strings_follow_p = get_overlay_strings (it, 0);
8153 }
8154
8155 if (overlay_strings_follow_p)
8156 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8157 else
8158 {
8159 it->what = IT_EOB;
8160 it->position = it->current.pos;
8161 success_p = 0;
8162 }
8163 }
8164 else if (!(!it->bidi_p
8165 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8166 || IT_CHARPOS (*it) == it->stop_charpos))
8167 {
8168 /* With bidi non-linear iteration, we could find ourselves
8169 far beyond the last computed stop_charpos, with several
8170 other stop positions in between that we missed. Scan
8171 them all now, in buffer's logical order, until we find
8172 and handle the last stop_charpos that precedes our
8173 current position. */
8174 handle_stop_backwards (it, it->stop_charpos);
8175 return GET_NEXT_DISPLAY_ELEMENT (it);
8176 }
8177 else
8178 {
8179 if (it->bidi_p)
8180 {
8181 /* Take note of the stop position we just moved across,
8182 for when we will move back across it. */
8183 it->prev_stop = it->stop_charpos;
8184 /* If we are at base paragraph embedding level, take
8185 note of the last stop position seen at this
8186 level. */
8187 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8188 it->base_level_stop = it->stop_charpos;
8189 }
8190 handle_stop (it);
8191 return GET_NEXT_DISPLAY_ELEMENT (it);
8192 }
8193 }
8194 else if (it->bidi_p
8195 /* If we are before prev_stop, we may have overstepped on
8196 our way backwards a stop_pos, and if so, we need to
8197 handle that stop_pos. */
8198 && IT_CHARPOS (*it) < it->prev_stop
8199 /* We can sometimes back up for reasons that have nothing
8200 to do with bidi reordering. E.g., compositions. The
8201 code below is only needed when we are above the base
8202 embedding level, so test for that explicitly. */
8203 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8204 {
8205 if (it->base_level_stop <= 0
8206 || IT_CHARPOS (*it) < it->base_level_stop)
8207 {
8208 /* If we lost track of base_level_stop, we need to find
8209 prev_stop by looking backwards. This happens, e.g., when
8210 we were reseated to the previous screenful of text by
8211 vertical-motion. */
8212 it->base_level_stop = BEGV;
8213 compute_stop_pos_backwards (it);
8214 handle_stop_backwards (it, it->prev_stop);
8215 }
8216 else
8217 handle_stop_backwards (it, it->base_level_stop);
8218 return GET_NEXT_DISPLAY_ELEMENT (it);
8219 }
8220 else
8221 {
8222 /* No face changes, overlays etc. in sight, so just return a
8223 character from current_buffer. */
8224 unsigned char *p;
8225 ptrdiff_t stop;
8226
8227 /* We moved to the next buffer position, so any info about
8228 previously seen overlays is no longer valid. */
8229 it->ignore_overlay_strings_at_pos_p = 0;
8230
8231 /* Maybe run the redisplay end trigger hook. Performance note:
8232 This doesn't seem to cost measurable time. */
8233 if (it->redisplay_end_trigger_charpos
8234 && it->glyph_row
8235 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8236 run_redisplay_end_trigger_hook (it);
8237
8238 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8239 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8240 stop)
8241 && next_element_from_composition (it))
8242 {
8243 return 1;
8244 }
8245
8246 /* Get the next character, maybe multibyte. */
8247 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8248 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8249 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8250 else
8251 it->c = *p, it->len = 1;
8252
8253 /* Record what we have and where it came from. */
8254 it->what = IT_CHARACTER;
8255 it->object = it->w->contents;
8256 it->position = it->current.pos;
8257
8258 /* Normally we return the character found above, except when we
8259 really want to return an ellipsis for selective display. */
8260 if (it->selective)
8261 {
8262 if (it->c == '\n')
8263 {
8264 /* A value of selective > 0 means hide lines indented more
8265 than that number of columns. */
8266 if (it->selective > 0
8267 && IT_CHARPOS (*it) + 1 < ZV
8268 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8269 IT_BYTEPOS (*it) + 1,
8270 it->selective))
8271 {
8272 success_p = next_element_from_ellipsis (it);
8273 it->dpvec_char_len = -1;
8274 }
8275 }
8276 else if (it->c == '\r' && it->selective == -1)
8277 {
8278 /* A value of selective == -1 means that everything from the
8279 CR to the end of the line is invisible, with maybe an
8280 ellipsis displayed for it. */
8281 success_p = next_element_from_ellipsis (it);
8282 it->dpvec_char_len = -1;
8283 }
8284 }
8285 }
8286
8287 /* Value is zero if end of buffer reached. */
8288 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8289 return success_p;
8290 }
8291
8292
8293 /* Run the redisplay end trigger hook for IT. */
8294
8295 static void
8296 run_redisplay_end_trigger_hook (struct it *it)
8297 {
8298 Lisp_Object args[3];
8299
8300 /* IT->glyph_row should be non-null, i.e. we should be actually
8301 displaying something, or otherwise we should not run the hook. */
8302 eassert (it->glyph_row);
8303
8304 /* Set up hook arguments. */
8305 args[0] = Qredisplay_end_trigger_functions;
8306 args[1] = it->window;
8307 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8308 it->redisplay_end_trigger_charpos = 0;
8309
8310 /* Since we are *trying* to run these functions, don't try to run
8311 them again, even if they get an error. */
8312 wset_redisplay_end_trigger (it->w, Qnil);
8313 Frun_hook_with_args (3, args);
8314
8315 /* Notice if it changed the face of the character we are on. */
8316 handle_face_prop (it);
8317 }
8318
8319
8320 /* Deliver a composition display element. Unlike the other
8321 next_element_from_XXX, this function is not registered in the array
8322 get_next_element[]. It is called from next_element_from_buffer and
8323 next_element_from_string when necessary. */
8324
8325 static int
8326 next_element_from_composition (struct it *it)
8327 {
8328 it->what = IT_COMPOSITION;
8329 it->len = it->cmp_it.nbytes;
8330 if (STRINGP (it->string))
8331 {
8332 if (it->c < 0)
8333 {
8334 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8335 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8336 return 0;
8337 }
8338 it->position = it->current.string_pos;
8339 it->object = it->string;
8340 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8341 IT_STRING_BYTEPOS (*it), it->string);
8342 }
8343 else
8344 {
8345 if (it->c < 0)
8346 {
8347 IT_CHARPOS (*it) += it->cmp_it.nchars;
8348 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8349 if (it->bidi_p)
8350 {
8351 if (it->bidi_it.new_paragraph)
8352 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8353 /* Resync the bidi iterator with IT's new position.
8354 FIXME: this doesn't support bidirectional text. */
8355 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8356 bidi_move_to_visually_next (&it->bidi_it);
8357 }
8358 return 0;
8359 }
8360 it->position = it->current.pos;
8361 it->object = it->w->contents;
8362 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8363 IT_BYTEPOS (*it), Qnil);
8364 }
8365 return 1;
8366 }
8367
8368
8369 \f
8370 /***********************************************************************
8371 Moving an iterator without producing glyphs
8372 ***********************************************************************/
8373
8374 /* Check if iterator is at a position corresponding to a valid buffer
8375 position after some move_it_ call. */
8376
8377 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8378 ((it)->method == GET_FROM_STRING \
8379 ? IT_STRING_CHARPOS (*it) == 0 \
8380 : 1)
8381
8382
8383 /* Move iterator IT to a specified buffer or X position within one
8384 line on the display without producing glyphs.
8385
8386 OP should be a bit mask including some or all of these bits:
8387 MOVE_TO_X: Stop upon reaching x-position TO_X.
8388 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8389 Regardless of OP's value, stop upon reaching the end of the display line.
8390
8391 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8392 This means, in particular, that TO_X includes window's horizontal
8393 scroll amount.
8394
8395 The return value has several possible values that
8396 say what condition caused the scan to stop:
8397
8398 MOVE_POS_MATCH_OR_ZV
8399 - when TO_POS or ZV was reached.
8400
8401 MOVE_X_REACHED
8402 -when TO_X was reached before TO_POS or ZV were reached.
8403
8404 MOVE_LINE_CONTINUED
8405 - when we reached the end of the display area and the line must
8406 be continued.
8407
8408 MOVE_LINE_TRUNCATED
8409 - when we reached the end of the display area and the line is
8410 truncated.
8411
8412 MOVE_NEWLINE_OR_CR
8413 - when we stopped at a line end, i.e. a newline or a CR and selective
8414 display is on. */
8415
8416 static enum move_it_result
8417 move_it_in_display_line_to (struct it *it,
8418 ptrdiff_t to_charpos, int to_x,
8419 enum move_operation_enum op)
8420 {
8421 enum move_it_result result = MOVE_UNDEFINED;
8422 struct glyph_row *saved_glyph_row;
8423 struct it wrap_it, atpos_it, atx_it, ppos_it;
8424 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8425 void *ppos_data = NULL;
8426 int may_wrap = 0;
8427 enum it_method prev_method = it->method;
8428 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8429 int saw_smaller_pos = prev_pos < to_charpos;
8430
8431 /* Don't produce glyphs in produce_glyphs. */
8432 saved_glyph_row = it->glyph_row;
8433 it->glyph_row = NULL;
8434
8435 /* Use wrap_it to save a copy of IT wherever a word wrap could
8436 occur. Use atpos_it to save a copy of IT at the desired buffer
8437 position, if found, so that we can scan ahead and check if the
8438 word later overshoots the window edge. Use atx_it similarly, for
8439 pixel positions. */
8440 wrap_it.sp = -1;
8441 atpos_it.sp = -1;
8442 atx_it.sp = -1;
8443
8444 /* Use ppos_it under bidi reordering to save a copy of IT for the
8445 initial position. We restore that position in IT when we have
8446 scanned the entire display line without finding a match for
8447 TO_CHARPOS and all the character positions are greater than
8448 TO_CHARPOS. We then restart the scan from the initial position,
8449 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8450 the closest to TO_CHARPOS. */
8451 if (it->bidi_p)
8452 {
8453 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8454 {
8455 SAVE_IT (ppos_it, *it, ppos_data);
8456 closest_pos = IT_CHARPOS (*it);
8457 }
8458 else
8459 closest_pos = ZV;
8460 }
8461
8462 #define BUFFER_POS_REACHED_P() \
8463 ((op & MOVE_TO_POS) != 0 \
8464 && BUFFERP (it->object) \
8465 && (IT_CHARPOS (*it) == to_charpos \
8466 || ((!it->bidi_p \
8467 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8468 && IT_CHARPOS (*it) > to_charpos) \
8469 || (it->what == IT_COMPOSITION \
8470 && ((IT_CHARPOS (*it) > to_charpos \
8471 && to_charpos >= it->cmp_it.charpos) \
8472 || (IT_CHARPOS (*it) < to_charpos \
8473 && to_charpos <= it->cmp_it.charpos)))) \
8474 && (it->method == GET_FROM_BUFFER \
8475 || (it->method == GET_FROM_DISPLAY_VECTOR \
8476 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8477
8478 /* If there's a line-/wrap-prefix, handle it. */
8479 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8480 && it->current_y < it->last_visible_y)
8481 handle_line_prefix (it);
8482
8483 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8484 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8485
8486 while (1)
8487 {
8488 int x, i, ascent = 0, descent = 0;
8489
8490 /* Utility macro to reset an iterator with x, ascent, and descent. */
8491 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8492 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8493 (IT)->max_descent = descent)
8494
8495 /* Stop if we move beyond TO_CHARPOS (after an image or a
8496 display string or stretch glyph). */
8497 if ((op & MOVE_TO_POS) != 0
8498 && BUFFERP (it->object)
8499 && it->method == GET_FROM_BUFFER
8500 && (((!it->bidi_p
8501 /* When the iterator is at base embedding level, we
8502 are guaranteed that characters are delivered for
8503 display in strictly increasing order of their
8504 buffer positions. */
8505 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8506 && IT_CHARPOS (*it) > to_charpos)
8507 || (it->bidi_p
8508 && (prev_method == GET_FROM_IMAGE
8509 || prev_method == GET_FROM_STRETCH
8510 || prev_method == GET_FROM_STRING)
8511 /* Passed TO_CHARPOS from left to right. */
8512 && ((prev_pos < to_charpos
8513 && IT_CHARPOS (*it) > to_charpos)
8514 /* Passed TO_CHARPOS from right to left. */
8515 || (prev_pos > to_charpos
8516 && IT_CHARPOS (*it) < to_charpos)))))
8517 {
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8519 {
8520 result = MOVE_POS_MATCH_OR_ZV;
8521 break;
8522 }
8523 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8524 /* If wrap_it is valid, the current position might be in a
8525 word that is wrapped. So, save the iterator in
8526 atpos_it and continue to see if wrapping happens. */
8527 SAVE_IT (atpos_it, *it, atpos_data);
8528 }
8529
8530 /* Stop when ZV reached.
8531 We used to stop here when TO_CHARPOS reached as well, but that is
8532 too soon if this glyph does not fit on this line. So we handle it
8533 explicitly below. */
8534 if (!get_next_display_element (it))
8535 {
8536 result = MOVE_POS_MATCH_OR_ZV;
8537 break;
8538 }
8539
8540 if (it->line_wrap == TRUNCATE)
8541 {
8542 if (BUFFER_POS_REACHED_P ())
8543 {
8544 result = MOVE_POS_MATCH_OR_ZV;
8545 break;
8546 }
8547 }
8548 else
8549 {
8550 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8551 {
8552 if (IT_DISPLAYING_WHITESPACE (it))
8553 may_wrap = 1;
8554 else if (may_wrap)
8555 {
8556 /* We have reached a glyph that follows one or more
8557 whitespace characters. If the position is
8558 already found, we are done. */
8559 if (atpos_it.sp >= 0)
8560 {
8561 RESTORE_IT (it, &atpos_it, atpos_data);
8562 result = MOVE_POS_MATCH_OR_ZV;
8563 goto done;
8564 }
8565 if (atx_it.sp >= 0)
8566 {
8567 RESTORE_IT (it, &atx_it, atx_data);
8568 result = MOVE_X_REACHED;
8569 goto done;
8570 }
8571 /* Otherwise, we can wrap here. */
8572 SAVE_IT (wrap_it, *it, wrap_data);
8573 may_wrap = 0;
8574 }
8575 }
8576 }
8577
8578 /* Remember the line height for the current line, in case
8579 the next element doesn't fit on the line. */
8580 ascent = it->max_ascent;
8581 descent = it->max_descent;
8582
8583 /* The call to produce_glyphs will get the metrics of the
8584 display element IT is loaded with. Record the x-position
8585 before this display element, in case it doesn't fit on the
8586 line. */
8587 x = it->current_x;
8588
8589 PRODUCE_GLYPHS (it);
8590
8591 if (it->area != TEXT_AREA)
8592 {
8593 prev_method = it->method;
8594 if (it->method == GET_FROM_BUFFER)
8595 prev_pos = IT_CHARPOS (*it);
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos,
8599 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 if (it->bidi_p
8601 && (op & MOVE_TO_POS)
8602 && IT_CHARPOS (*it) > to_charpos
8603 && IT_CHARPOS (*it) < closest_pos)
8604 closest_pos = IT_CHARPOS (*it);
8605 continue;
8606 }
8607
8608 /* The number of glyphs we get back in IT->nglyphs will normally
8609 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8610 character on a terminal frame, or (iii) a line end. For the
8611 second case, IT->nglyphs - 1 padding glyphs will be present.
8612 (On X frames, there is only one glyph produced for a
8613 composite character.)
8614
8615 The behavior implemented below means, for continuation lines,
8616 that as many spaces of a TAB as fit on the current line are
8617 displayed there. For terminal frames, as many glyphs of a
8618 multi-glyph character are displayed in the current line, too.
8619 This is what the old redisplay code did, and we keep it that
8620 way. Under X, the whole shape of a complex character must
8621 fit on the line or it will be completely displayed in the
8622 next line.
8623
8624 Note that both for tabs and padding glyphs, all glyphs have
8625 the same width. */
8626 if (it->nglyphs)
8627 {
8628 /* More than one glyph or glyph doesn't fit on line. All
8629 glyphs have the same width. */
8630 int single_glyph_width = it->pixel_width / it->nglyphs;
8631 int new_x;
8632 int x_before_this_char = x;
8633 int hpos_before_this_char = it->hpos;
8634
8635 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8636 {
8637 new_x = x + single_glyph_width;
8638
8639 /* We want to leave anything reaching TO_X to the caller. */
8640 if ((op & MOVE_TO_X) && new_x > to_x)
8641 {
8642 if (BUFFER_POS_REACHED_P ())
8643 {
8644 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8645 goto buffer_pos_reached;
8646 if (atpos_it.sp < 0)
8647 {
8648 SAVE_IT (atpos_it, *it, atpos_data);
8649 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8650 }
8651 }
8652 else
8653 {
8654 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8655 {
8656 it->current_x = x;
8657 result = MOVE_X_REACHED;
8658 break;
8659 }
8660 if (atx_it.sp < 0)
8661 {
8662 SAVE_IT (atx_it, *it, atx_data);
8663 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8664 }
8665 }
8666 }
8667
8668 if (/* Lines are continued. */
8669 it->line_wrap != TRUNCATE
8670 && (/* And glyph doesn't fit on the line. */
8671 new_x > it->last_visible_x
8672 /* Or it fits exactly and we're on a window
8673 system frame. */
8674 || (new_x == it->last_visible_x
8675 && FRAME_WINDOW_P (it->f)
8676 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8677 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8678 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8679 {
8680 if (/* IT->hpos == 0 means the very first glyph
8681 doesn't fit on the line, e.g. a wide image. */
8682 it->hpos == 0
8683 || (new_x == it->last_visible_x
8684 && FRAME_WINDOW_P (it->f)))
8685 {
8686 ++it->hpos;
8687 it->current_x = new_x;
8688
8689 /* The character's last glyph just barely fits
8690 in this row. */
8691 if (i == it->nglyphs - 1)
8692 {
8693 /* If this is the destination position,
8694 return a position *before* it in this row,
8695 now that we know it fits in this row. */
8696 if (BUFFER_POS_REACHED_P ())
8697 {
8698 if (it->line_wrap != WORD_WRAP
8699 || wrap_it.sp < 0)
8700 {
8701 it->hpos = hpos_before_this_char;
8702 it->current_x = x_before_this_char;
8703 result = MOVE_POS_MATCH_OR_ZV;
8704 break;
8705 }
8706 if (it->line_wrap == WORD_WRAP
8707 && atpos_it.sp < 0)
8708 {
8709 SAVE_IT (atpos_it, *it, atpos_data);
8710 atpos_it.current_x = x_before_this_char;
8711 atpos_it.hpos = hpos_before_this_char;
8712 }
8713 }
8714
8715 prev_method = it->method;
8716 if (it->method == GET_FROM_BUFFER)
8717 prev_pos = IT_CHARPOS (*it);
8718 set_iterator_to_next (it, 1);
8719 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8720 SET_TEXT_POS (this_line_min_pos,
8721 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8722 /* On graphical terminals, newlines may
8723 "overflow" into the fringe if
8724 overflow-newline-into-fringe is non-nil.
8725 On text terminals, and on graphical
8726 terminals with no right margin, newlines
8727 may overflow into the last glyph on the
8728 display line.*/
8729 if (!FRAME_WINDOW_P (it->f)
8730 || ((it->bidi_p
8731 && it->bidi_it.paragraph_dir == R2L)
8732 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8733 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8734 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8735 {
8736 if (!get_next_display_element (it))
8737 {
8738 result = MOVE_POS_MATCH_OR_ZV;
8739 break;
8740 }
8741 if (BUFFER_POS_REACHED_P ())
8742 {
8743 if (ITERATOR_AT_END_OF_LINE_P (it))
8744 result = MOVE_POS_MATCH_OR_ZV;
8745 else
8746 result = MOVE_LINE_CONTINUED;
8747 break;
8748 }
8749 if (ITERATOR_AT_END_OF_LINE_P (it)
8750 && (it->line_wrap != WORD_WRAP
8751 || wrap_it.sp < 0
8752 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8753 {
8754 result = MOVE_NEWLINE_OR_CR;
8755 break;
8756 }
8757 }
8758 }
8759 }
8760 else
8761 IT_RESET_X_ASCENT_DESCENT (it);
8762
8763 if (wrap_it.sp >= 0)
8764 {
8765 RESTORE_IT (it, &wrap_it, wrap_data);
8766 atpos_it.sp = -1;
8767 atx_it.sp = -1;
8768 }
8769
8770 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8771 IT_CHARPOS (*it)));
8772 result = MOVE_LINE_CONTINUED;
8773 break;
8774 }
8775
8776 if (BUFFER_POS_REACHED_P ())
8777 {
8778 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8779 goto buffer_pos_reached;
8780 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8781 {
8782 SAVE_IT (atpos_it, *it, atpos_data);
8783 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8784 }
8785 }
8786
8787 if (new_x > it->first_visible_x)
8788 {
8789 /* Glyph is visible. Increment number of glyphs that
8790 would be displayed. */
8791 ++it->hpos;
8792 }
8793 }
8794
8795 if (result != MOVE_UNDEFINED)
8796 break;
8797 }
8798 else if (BUFFER_POS_REACHED_P ())
8799 {
8800 buffer_pos_reached:
8801 IT_RESET_X_ASCENT_DESCENT (it);
8802 result = MOVE_POS_MATCH_OR_ZV;
8803 break;
8804 }
8805 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8806 {
8807 /* Stop when TO_X specified and reached. This check is
8808 necessary here because of lines consisting of a line end,
8809 only. The line end will not produce any glyphs and we
8810 would never get MOVE_X_REACHED. */
8811 eassert (it->nglyphs == 0);
8812 result = MOVE_X_REACHED;
8813 break;
8814 }
8815
8816 /* Is this a line end? If yes, we're done. */
8817 if (ITERATOR_AT_END_OF_LINE_P (it))
8818 {
8819 /* If we are past TO_CHARPOS, but never saw any character
8820 positions smaller than TO_CHARPOS, return
8821 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8822 did. */
8823 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8824 {
8825 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8826 {
8827 if (closest_pos < ZV)
8828 {
8829 RESTORE_IT (it, &ppos_it, ppos_data);
8830 /* Don't recurse if closest_pos is equal to
8831 to_charpos, since we have just tried that. */
8832 if (closest_pos != to_charpos)
8833 move_it_in_display_line_to (it, closest_pos, -1,
8834 MOVE_TO_POS);
8835 result = MOVE_POS_MATCH_OR_ZV;
8836 }
8837 else
8838 goto buffer_pos_reached;
8839 }
8840 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8841 && IT_CHARPOS (*it) > to_charpos)
8842 goto buffer_pos_reached;
8843 else
8844 result = MOVE_NEWLINE_OR_CR;
8845 }
8846 else
8847 result = MOVE_NEWLINE_OR_CR;
8848 break;
8849 }
8850
8851 prev_method = it->method;
8852 if (it->method == GET_FROM_BUFFER)
8853 prev_pos = IT_CHARPOS (*it);
8854 /* The current display element has been consumed. Advance
8855 to the next. */
8856 set_iterator_to_next (it, 1);
8857 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8858 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8859 if (IT_CHARPOS (*it) < to_charpos)
8860 saw_smaller_pos = 1;
8861 if (it->bidi_p
8862 && (op & MOVE_TO_POS)
8863 && IT_CHARPOS (*it) >= to_charpos
8864 && IT_CHARPOS (*it) < closest_pos)
8865 closest_pos = IT_CHARPOS (*it);
8866
8867 /* Stop if lines are truncated and IT's current x-position is
8868 past the right edge of the window now. */
8869 if (it->line_wrap == TRUNCATE
8870 && it->current_x >= it->last_visible_x)
8871 {
8872 if (!FRAME_WINDOW_P (it->f)
8873 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8874 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8875 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8876 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8877 {
8878 int at_eob_p = 0;
8879
8880 if ((at_eob_p = !get_next_display_element (it))
8881 || BUFFER_POS_REACHED_P ()
8882 /* If we are past TO_CHARPOS, but never saw any
8883 character positions smaller than TO_CHARPOS,
8884 return MOVE_POS_MATCH_OR_ZV, like the
8885 unidirectional display did. */
8886 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8887 && !saw_smaller_pos
8888 && IT_CHARPOS (*it) > to_charpos))
8889 {
8890 if (it->bidi_p
8891 && !BUFFER_POS_REACHED_P ()
8892 && !at_eob_p && closest_pos < ZV)
8893 {
8894 RESTORE_IT (it, &ppos_it, ppos_data);
8895 if (closest_pos != to_charpos)
8896 move_it_in_display_line_to (it, closest_pos, -1,
8897 MOVE_TO_POS);
8898 }
8899 result = MOVE_POS_MATCH_OR_ZV;
8900 break;
8901 }
8902 if (ITERATOR_AT_END_OF_LINE_P (it))
8903 {
8904 result = MOVE_NEWLINE_OR_CR;
8905 break;
8906 }
8907 }
8908 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8909 && !saw_smaller_pos
8910 && IT_CHARPOS (*it) > to_charpos)
8911 {
8912 if (closest_pos < ZV)
8913 {
8914 RESTORE_IT (it, &ppos_it, ppos_data);
8915 if (closest_pos != to_charpos)
8916 move_it_in_display_line_to (it, closest_pos, -1,
8917 MOVE_TO_POS);
8918 }
8919 result = MOVE_POS_MATCH_OR_ZV;
8920 break;
8921 }
8922 result = MOVE_LINE_TRUNCATED;
8923 break;
8924 }
8925 #undef IT_RESET_X_ASCENT_DESCENT
8926 }
8927
8928 #undef BUFFER_POS_REACHED_P
8929
8930 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8931 restore the saved iterator. */
8932 if (atpos_it.sp >= 0)
8933 RESTORE_IT (it, &atpos_it, atpos_data);
8934 else if (atx_it.sp >= 0)
8935 RESTORE_IT (it, &atx_it, atx_data);
8936
8937 done:
8938
8939 if (atpos_data)
8940 bidi_unshelve_cache (atpos_data, 1);
8941 if (atx_data)
8942 bidi_unshelve_cache (atx_data, 1);
8943 if (wrap_data)
8944 bidi_unshelve_cache (wrap_data, 1);
8945 if (ppos_data)
8946 bidi_unshelve_cache (ppos_data, 1);
8947
8948 /* Restore the iterator settings altered at the beginning of this
8949 function. */
8950 it->glyph_row = saved_glyph_row;
8951 return result;
8952 }
8953
8954 /* For external use. */
8955 void
8956 move_it_in_display_line (struct it *it,
8957 ptrdiff_t to_charpos, int to_x,
8958 enum move_operation_enum op)
8959 {
8960 if (it->line_wrap == WORD_WRAP
8961 && (op & MOVE_TO_X))
8962 {
8963 struct it save_it;
8964 void *save_data = NULL;
8965 int skip;
8966
8967 SAVE_IT (save_it, *it, save_data);
8968 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8969 /* When word-wrap is on, TO_X may lie past the end
8970 of a wrapped line. Then it->current is the
8971 character on the next line, so backtrack to the
8972 space before the wrap point. */
8973 if (skip == MOVE_LINE_CONTINUED)
8974 {
8975 int prev_x = max (it->current_x - 1, 0);
8976 RESTORE_IT (it, &save_it, save_data);
8977 move_it_in_display_line_to
8978 (it, -1, prev_x, MOVE_TO_X);
8979 }
8980 else
8981 bidi_unshelve_cache (save_data, 1);
8982 }
8983 else
8984 move_it_in_display_line_to (it, to_charpos, to_x, op);
8985 }
8986
8987
8988 /* Move IT forward until it satisfies one or more of the criteria in
8989 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8990
8991 OP is a bit-mask that specifies where to stop, and in particular,
8992 which of those four position arguments makes a difference. See the
8993 description of enum move_operation_enum.
8994
8995 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8996 screen line, this function will set IT to the next position that is
8997 displayed to the right of TO_CHARPOS on the screen.
8998
8999 Return the maximum pixel length of any line scanned but never more
9000 than it.last_visible_x. */
9001
9002 int
9003 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9004 {
9005 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9006 int line_height, line_start_x = 0, reached = 0;
9007 int max_current_x = 0;
9008 void *backup_data = NULL;
9009
9010 for (;;)
9011 {
9012 if (op & MOVE_TO_VPOS)
9013 {
9014 /* If no TO_CHARPOS and no TO_X specified, stop at the
9015 start of the line TO_VPOS. */
9016 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9017 {
9018 if (it->vpos == to_vpos)
9019 {
9020 reached = 1;
9021 break;
9022 }
9023 else
9024 skip = move_it_in_display_line_to (it, -1, -1, 0);
9025 }
9026 else
9027 {
9028 /* TO_VPOS >= 0 means stop at TO_X in the line at
9029 TO_VPOS, or at TO_POS, whichever comes first. */
9030 if (it->vpos == to_vpos)
9031 {
9032 reached = 2;
9033 break;
9034 }
9035
9036 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9037
9038 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9039 {
9040 reached = 3;
9041 break;
9042 }
9043 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9044 {
9045 /* We have reached TO_X but not in the line we want. */
9046 skip = move_it_in_display_line_to (it, to_charpos,
9047 -1, MOVE_TO_POS);
9048 if (skip == MOVE_POS_MATCH_OR_ZV)
9049 {
9050 reached = 4;
9051 break;
9052 }
9053 }
9054 }
9055 }
9056 else if (op & MOVE_TO_Y)
9057 {
9058 struct it it_backup;
9059
9060 if (it->line_wrap == WORD_WRAP)
9061 SAVE_IT (it_backup, *it, backup_data);
9062
9063 /* TO_Y specified means stop at TO_X in the line containing
9064 TO_Y---or at TO_CHARPOS if this is reached first. The
9065 problem is that we can't really tell whether the line
9066 contains TO_Y before we have completely scanned it, and
9067 this may skip past TO_X. What we do is to first scan to
9068 TO_X.
9069
9070 If TO_X is not specified, use a TO_X of zero. The reason
9071 is to make the outcome of this function more predictable.
9072 If we didn't use TO_X == 0, we would stop at the end of
9073 the line which is probably not what a caller would expect
9074 to happen. */
9075 skip = move_it_in_display_line_to
9076 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9077 (MOVE_TO_X | (op & MOVE_TO_POS)));
9078
9079 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9080 if (skip == MOVE_POS_MATCH_OR_ZV)
9081 reached = 5;
9082 else if (skip == MOVE_X_REACHED)
9083 {
9084 /* If TO_X was reached, we want to know whether TO_Y is
9085 in the line. We know this is the case if the already
9086 scanned glyphs make the line tall enough. Otherwise,
9087 we must check by scanning the rest of the line. */
9088 line_height = it->max_ascent + it->max_descent;
9089 if (to_y >= it->current_y
9090 && to_y < it->current_y + line_height)
9091 {
9092 reached = 6;
9093 break;
9094 }
9095 SAVE_IT (it_backup, *it, backup_data);
9096 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9097 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9098 op & MOVE_TO_POS);
9099 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9100 line_height = it->max_ascent + it->max_descent;
9101 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9102
9103 if (to_y >= it->current_y
9104 && to_y < it->current_y + line_height)
9105 {
9106 /* If TO_Y is in this line and TO_X was reached
9107 above, we scanned too far. We have to restore
9108 IT's settings to the ones before skipping. But
9109 keep the more accurate values of max_ascent and
9110 max_descent we've found while skipping the rest
9111 of the line, for the sake of callers, such as
9112 pos_visible_p, that need to know the line
9113 height. */
9114 int max_ascent = it->max_ascent;
9115 int max_descent = it->max_descent;
9116
9117 RESTORE_IT (it, &it_backup, backup_data);
9118 it->max_ascent = max_ascent;
9119 it->max_descent = max_descent;
9120 reached = 6;
9121 }
9122 else
9123 {
9124 skip = skip2;
9125 if (skip == MOVE_POS_MATCH_OR_ZV)
9126 reached = 7;
9127 }
9128 }
9129 else
9130 {
9131 /* Check whether TO_Y is in this line. */
9132 line_height = it->max_ascent + it->max_descent;
9133 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9134
9135 if (to_y >= it->current_y
9136 && to_y < it->current_y + line_height)
9137 {
9138 if (to_y > it->current_y)
9139 max_current_x = max (it->current_x, max_current_x);
9140
9141 /* When word-wrap is on, TO_X may lie past the end
9142 of a wrapped line. Then it->current is the
9143 character on the next line, so backtrack to the
9144 space before the wrap point. */
9145 if (skip == MOVE_LINE_CONTINUED
9146 && it->line_wrap == WORD_WRAP)
9147 {
9148 int prev_x = max (it->current_x - 1, 0);
9149 RESTORE_IT (it, &it_backup, backup_data);
9150 skip = move_it_in_display_line_to
9151 (it, -1, prev_x, MOVE_TO_X);
9152 }
9153
9154 reached = 6;
9155 }
9156 }
9157
9158 if (reached)
9159 {
9160 max_current_x = max (it->current_x, max_current_x);
9161 break;
9162 }
9163 }
9164 else if (BUFFERP (it->object)
9165 && (it->method == GET_FROM_BUFFER
9166 || it->method == GET_FROM_STRETCH)
9167 && IT_CHARPOS (*it) >= to_charpos
9168 /* Under bidi iteration, a call to set_iterator_to_next
9169 can scan far beyond to_charpos if the initial
9170 portion of the next line needs to be reordered. In
9171 that case, give move_it_in_display_line_to another
9172 chance below. */
9173 && !(it->bidi_p
9174 && it->bidi_it.scan_dir == -1))
9175 skip = MOVE_POS_MATCH_OR_ZV;
9176 else
9177 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9178
9179 switch (skip)
9180 {
9181 case MOVE_POS_MATCH_OR_ZV:
9182 max_current_x = max (it->current_x, max_current_x);
9183 reached = 8;
9184 goto out;
9185
9186 case MOVE_NEWLINE_OR_CR:
9187 max_current_x = max (it->current_x, max_current_x);
9188 set_iterator_to_next (it, 1);
9189 it->continuation_lines_width = 0;
9190 break;
9191
9192 case MOVE_LINE_TRUNCATED:
9193 max_current_x = it->last_visible_x;
9194 it->continuation_lines_width = 0;
9195 reseat_at_next_visible_line_start (it, 0);
9196 if ((op & MOVE_TO_POS) != 0
9197 && IT_CHARPOS (*it) > to_charpos)
9198 {
9199 reached = 9;
9200 goto out;
9201 }
9202 break;
9203
9204 case MOVE_LINE_CONTINUED:
9205 max_current_x = it->last_visible_x;
9206 /* For continued lines ending in a tab, some of the glyphs
9207 associated with the tab are displayed on the current
9208 line. Since it->current_x does not include these glyphs,
9209 we use it->last_visible_x instead. */
9210 if (it->c == '\t')
9211 {
9212 it->continuation_lines_width += it->last_visible_x;
9213 /* When moving by vpos, ensure that the iterator really
9214 advances to the next line (bug#847, bug#969). Fixme:
9215 do we need to do this in other circumstances? */
9216 if (it->current_x != it->last_visible_x
9217 && (op & MOVE_TO_VPOS)
9218 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9219 {
9220 line_start_x = it->current_x + it->pixel_width
9221 - it->last_visible_x;
9222 if (FRAME_WINDOW_P (it->f))
9223 {
9224 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9225 struct font *face_font = face->font;
9226
9227 /* When display_line produces a continued line
9228 that ends in a TAB, it skips a tab stop that
9229 is closer than the font's space character
9230 width (see x_produce_glyphs where it produces
9231 the stretch glyph which represents a TAB).
9232 We need to reproduce the same logic here. */
9233 eassert (face_font);
9234 if (face_font)
9235 {
9236 if (line_start_x < face_font->space_width)
9237 line_start_x
9238 += it->tab_width * face_font->space_width;
9239 }
9240 }
9241 set_iterator_to_next (it, 0);
9242 }
9243 }
9244 else
9245 it->continuation_lines_width += it->current_x;
9246 break;
9247
9248 default:
9249 emacs_abort ();
9250 }
9251
9252 /* Reset/increment for the next run. */
9253 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9254 it->current_x = line_start_x;
9255 line_start_x = 0;
9256 it->hpos = 0;
9257 it->current_y += it->max_ascent + it->max_descent;
9258 ++it->vpos;
9259 last_height = it->max_ascent + it->max_descent;
9260 it->max_ascent = it->max_descent = 0;
9261 }
9262
9263 out:
9264
9265 /* On text terminals, we may stop at the end of a line in the middle
9266 of a multi-character glyph. If the glyph itself is continued,
9267 i.e. it is actually displayed on the next line, don't treat this
9268 stopping point as valid; move to the next line instead (unless
9269 that brings us offscreen). */
9270 if (!FRAME_WINDOW_P (it->f)
9271 && op & MOVE_TO_POS
9272 && IT_CHARPOS (*it) == to_charpos
9273 && it->what == IT_CHARACTER
9274 && it->nglyphs > 1
9275 && it->line_wrap == WINDOW_WRAP
9276 && it->current_x == it->last_visible_x - 1
9277 && it->c != '\n'
9278 && it->c != '\t'
9279 && it->vpos < it->w->window_end_vpos)
9280 {
9281 it->continuation_lines_width += it->current_x;
9282 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9283 it->current_y += it->max_ascent + it->max_descent;
9284 ++it->vpos;
9285 last_height = it->max_ascent + it->max_descent;
9286 }
9287
9288 if (backup_data)
9289 bidi_unshelve_cache (backup_data, 1);
9290
9291 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9292
9293 return max_current_x;
9294 }
9295
9296
9297 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9298
9299 If DY > 0, move IT backward at least that many pixels. DY = 0
9300 means move IT backward to the preceding line start or BEGV. This
9301 function may move over more than DY pixels if IT->current_y - DY
9302 ends up in the middle of a line; in this case IT->current_y will be
9303 set to the top of the line moved to. */
9304
9305 void
9306 move_it_vertically_backward (struct it *it, int dy)
9307 {
9308 int nlines, h;
9309 struct it it2, it3;
9310 void *it2data = NULL, *it3data = NULL;
9311 ptrdiff_t start_pos;
9312 int nchars_per_row
9313 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9314 ptrdiff_t pos_limit;
9315
9316 move_further_back:
9317 eassert (dy >= 0);
9318
9319 start_pos = IT_CHARPOS (*it);
9320
9321 /* Estimate how many newlines we must move back. */
9322 nlines = max (1, dy / default_line_pixel_height (it->w));
9323 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9324 pos_limit = BEGV;
9325 else
9326 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9327
9328 /* Set the iterator's position that many lines back. But don't go
9329 back more than NLINES full screen lines -- this wins a day with
9330 buffers which have very long lines. */
9331 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9332 back_to_previous_visible_line_start (it);
9333
9334 /* Reseat the iterator here. When moving backward, we don't want
9335 reseat to skip forward over invisible text, set up the iterator
9336 to deliver from overlay strings at the new position etc. So,
9337 use reseat_1 here. */
9338 reseat_1 (it, it->current.pos, 1);
9339
9340 /* We are now surely at a line start. */
9341 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9342 reordering is in effect. */
9343 it->continuation_lines_width = 0;
9344
9345 /* Move forward and see what y-distance we moved. First move to the
9346 start of the next line so that we get its height. We need this
9347 height to be able to tell whether we reached the specified
9348 y-distance. */
9349 SAVE_IT (it2, *it, it2data);
9350 it2.max_ascent = it2.max_descent = 0;
9351 do
9352 {
9353 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9354 MOVE_TO_POS | MOVE_TO_VPOS);
9355 }
9356 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9357 /* If we are in a display string which starts at START_POS,
9358 and that display string includes a newline, and we are
9359 right after that newline (i.e. at the beginning of a
9360 display line), exit the loop, because otherwise we will
9361 infloop, since move_it_to will see that it is already at
9362 START_POS and will not move. */
9363 || (it2.method == GET_FROM_STRING
9364 && IT_CHARPOS (it2) == start_pos
9365 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9366 eassert (IT_CHARPOS (*it) >= BEGV);
9367 SAVE_IT (it3, it2, it3data);
9368
9369 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9370 eassert (IT_CHARPOS (*it) >= BEGV);
9371 /* H is the actual vertical distance from the position in *IT
9372 and the starting position. */
9373 h = it2.current_y - it->current_y;
9374 /* NLINES is the distance in number of lines. */
9375 nlines = it2.vpos - it->vpos;
9376
9377 /* Correct IT's y and vpos position
9378 so that they are relative to the starting point. */
9379 it->vpos -= nlines;
9380 it->current_y -= h;
9381
9382 if (dy == 0)
9383 {
9384 /* DY == 0 means move to the start of the screen line. The
9385 value of nlines is > 0 if continuation lines were involved,
9386 or if the original IT position was at start of a line. */
9387 RESTORE_IT (it, it, it2data);
9388 if (nlines > 0)
9389 move_it_by_lines (it, nlines);
9390 /* The above code moves us to some position NLINES down,
9391 usually to its first glyph (leftmost in an L2R line), but
9392 that's not necessarily the start of the line, under bidi
9393 reordering. We want to get to the character position
9394 that is immediately after the newline of the previous
9395 line. */
9396 if (it->bidi_p
9397 && !it->continuation_lines_width
9398 && !STRINGP (it->string)
9399 && IT_CHARPOS (*it) > BEGV
9400 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9401 {
9402 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9403
9404 DEC_BOTH (cp, bp);
9405 cp = find_newline_no_quit (cp, bp, -1, NULL);
9406 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9407 }
9408 bidi_unshelve_cache (it3data, 1);
9409 }
9410 else
9411 {
9412 /* The y-position we try to reach, relative to *IT.
9413 Note that H has been subtracted in front of the if-statement. */
9414 int target_y = it->current_y + h - dy;
9415 int y0 = it3.current_y;
9416 int y1;
9417 int line_height;
9418
9419 RESTORE_IT (&it3, &it3, it3data);
9420 y1 = line_bottom_y (&it3);
9421 line_height = y1 - y0;
9422 RESTORE_IT (it, it, it2data);
9423 /* If we did not reach target_y, try to move further backward if
9424 we can. If we moved too far backward, try to move forward. */
9425 if (target_y < it->current_y
9426 /* This is heuristic. In a window that's 3 lines high, with
9427 a line height of 13 pixels each, recentering with point
9428 on the bottom line will try to move -39/2 = 19 pixels
9429 backward. Try to avoid moving into the first line. */
9430 && (it->current_y - target_y
9431 > min (window_box_height (it->w), line_height * 2 / 3))
9432 && IT_CHARPOS (*it) > BEGV)
9433 {
9434 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9435 target_y - it->current_y));
9436 dy = it->current_y - target_y;
9437 goto move_further_back;
9438 }
9439 else if (target_y >= it->current_y + line_height
9440 && IT_CHARPOS (*it) < ZV)
9441 {
9442 /* Should move forward by at least one line, maybe more.
9443
9444 Note: Calling move_it_by_lines can be expensive on
9445 terminal frames, where compute_motion is used (via
9446 vmotion) to do the job, when there are very long lines
9447 and truncate-lines is nil. That's the reason for
9448 treating terminal frames specially here. */
9449
9450 if (!FRAME_WINDOW_P (it->f))
9451 move_it_vertically (it, target_y - (it->current_y + line_height));
9452 else
9453 {
9454 do
9455 {
9456 move_it_by_lines (it, 1);
9457 }
9458 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9459 }
9460 }
9461 }
9462 }
9463
9464
9465 /* Move IT by a specified amount of pixel lines DY. DY negative means
9466 move backwards. DY = 0 means move to start of screen line. At the
9467 end, IT will be on the start of a screen line. */
9468
9469 void
9470 move_it_vertically (struct it *it, int dy)
9471 {
9472 if (dy <= 0)
9473 move_it_vertically_backward (it, -dy);
9474 else
9475 {
9476 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9477 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9478 MOVE_TO_POS | MOVE_TO_Y);
9479 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9480
9481 /* If buffer ends in ZV without a newline, move to the start of
9482 the line to satisfy the post-condition. */
9483 if (IT_CHARPOS (*it) == ZV
9484 && ZV > BEGV
9485 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9486 move_it_by_lines (it, 0);
9487 }
9488 }
9489
9490
9491 /* Move iterator IT past the end of the text line it is in. */
9492
9493 void
9494 move_it_past_eol (struct it *it)
9495 {
9496 enum move_it_result rc;
9497
9498 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9499 if (rc == MOVE_NEWLINE_OR_CR)
9500 set_iterator_to_next (it, 0);
9501 }
9502
9503
9504 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9505 negative means move up. DVPOS == 0 means move to the start of the
9506 screen line.
9507
9508 Optimization idea: If we would know that IT->f doesn't use
9509 a face with proportional font, we could be faster for
9510 truncate-lines nil. */
9511
9512 void
9513 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9514 {
9515
9516 /* The commented-out optimization uses vmotion on terminals. This
9517 gives bad results, because elements like it->what, on which
9518 callers such as pos_visible_p rely, aren't updated. */
9519 /* struct position pos;
9520 if (!FRAME_WINDOW_P (it->f))
9521 {
9522 struct text_pos textpos;
9523
9524 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9525 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9526 reseat (it, textpos, 1);
9527 it->vpos += pos.vpos;
9528 it->current_y += pos.vpos;
9529 }
9530 else */
9531
9532 if (dvpos == 0)
9533 {
9534 /* DVPOS == 0 means move to the start of the screen line. */
9535 move_it_vertically_backward (it, 0);
9536 /* Let next call to line_bottom_y calculate real line height. */
9537 last_height = 0;
9538 }
9539 else if (dvpos > 0)
9540 {
9541 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9542 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9543 {
9544 /* Only move to the next buffer position if we ended up in a
9545 string from display property, not in an overlay string
9546 (before-string or after-string). That is because the
9547 latter don't conceal the underlying buffer position, so
9548 we can ask to move the iterator to the exact position we
9549 are interested in. Note that, even if we are already at
9550 IT_CHARPOS (*it), the call below is not a no-op, as it
9551 will detect that we are at the end of the string, pop the
9552 iterator, and compute it->current_x and it->hpos
9553 correctly. */
9554 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9555 -1, -1, -1, MOVE_TO_POS);
9556 }
9557 }
9558 else
9559 {
9560 struct it it2;
9561 void *it2data = NULL;
9562 ptrdiff_t start_charpos, i;
9563 int nchars_per_row
9564 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9565 bool hit_pos_limit = false;
9566 ptrdiff_t pos_limit;
9567
9568 /* Start at the beginning of the screen line containing IT's
9569 position. This may actually move vertically backwards,
9570 in case of overlays, so adjust dvpos accordingly. */
9571 dvpos += it->vpos;
9572 move_it_vertically_backward (it, 0);
9573 dvpos -= it->vpos;
9574
9575 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9576 screen lines, and reseat the iterator there. */
9577 start_charpos = IT_CHARPOS (*it);
9578 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9579 pos_limit = BEGV;
9580 else
9581 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9582
9583 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9584 back_to_previous_visible_line_start (it);
9585 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9586 hit_pos_limit = true;
9587 reseat (it, it->current.pos, 1);
9588
9589 /* Move further back if we end up in a string or an image. */
9590 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9591 {
9592 /* First try to move to start of display line. */
9593 dvpos += it->vpos;
9594 move_it_vertically_backward (it, 0);
9595 dvpos -= it->vpos;
9596 if (IT_POS_VALID_AFTER_MOVE_P (it))
9597 break;
9598 /* If start of line is still in string or image,
9599 move further back. */
9600 back_to_previous_visible_line_start (it);
9601 reseat (it, it->current.pos, 1);
9602 dvpos--;
9603 }
9604
9605 it->current_x = it->hpos = 0;
9606
9607 /* Above call may have moved too far if continuation lines
9608 are involved. Scan forward and see if it did. */
9609 SAVE_IT (it2, *it, it2data);
9610 it2.vpos = it2.current_y = 0;
9611 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9612 it->vpos -= it2.vpos;
9613 it->current_y -= it2.current_y;
9614 it->current_x = it->hpos = 0;
9615
9616 /* If we moved too far back, move IT some lines forward. */
9617 if (it2.vpos > -dvpos)
9618 {
9619 int delta = it2.vpos + dvpos;
9620
9621 RESTORE_IT (&it2, &it2, it2data);
9622 SAVE_IT (it2, *it, it2data);
9623 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9624 /* Move back again if we got too far ahead. */
9625 if (IT_CHARPOS (*it) >= start_charpos)
9626 RESTORE_IT (it, &it2, it2data);
9627 else
9628 bidi_unshelve_cache (it2data, 1);
9629 }
9630 else if (hit_pos_limit && pos_limit > BEGV
9631 && dvpos < 0 && it2.vpos < -dvpos)
9632 {
9633 /* If we hit the limit, but still didn't make it far enough
9634 back, that means there's a display string with a newline
9635 covering a large chunk of text, and that caused
9636 back_to_previous_visible_line_start try to go too far.
9637 Punish those who commit such atrocities by going back
9638 until we've reached DVPOS, after lifting the limit, which
9639 could make it slow for very long lines. "If it hurts,
9640 don't do that!" */
9641 dvpos += it2.vpos;
9642 RESTORE_IT (it, it, it2data);
9643 for (i = -dvpos; i > 0; --i)
9644 {
9645 back_to_previous_visible_line_start (it);
9646 it->vpos--;
9647 }
9648 reseat_1 (it, it->current.pos, 1);
9649 }
9650 else
9651 RESTORE_IT (it, it, it2data);
9652 }
9653 }
9654
9655 /* Return true if IT points into the middle of a display vector. */
9656
9657 bool
9658 in_display_vector_p (struct it *it)
9659 {
9660 return (it->method == GET_FROM_DISPLAY_VECTOR
9661 && it->current.dpvec_index > 0
9662 && it->dpvec + it->current.dpvec_index != it->dpend);
9663 }
9664
9665 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9666 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9667 WINDOW must be a live window and defaults to the selected one. The
9668 return value is a cons of the maximum pixel-width of any text line and
9669 the maximum pixel-height of all text lines.
9670
9671 The optional argument FROM, if non-nil, specifies the first text
9672 position and defaults to the minimum accessible position of the buffer.
9673 If FROM is t, use the minimum accessible position that is not a newline
9674 character. TO, if non-nil, specifies the last text position and
9675 defaults to the maximum accessible position of the buffer. If TO is t,
9676 use the maximum accessible position that is not a newline character.
9677
9678 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9679 width that can be returned. X-LIMIT nil or omitted, means to use the
9680 pixel-width of WINDOW's body; use this if you do not intend to change
9681 the width of WINDOW. Use the maximum width WINDOW may assume if you
9682 intend to change WINDOW's width. In any case, text whose x-coordinate
9683 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9684 can take some time, it's always a good idea to make this argument as
9685 small as possible; in particular, if the buffer contains long lines that
9686 shall be truncated anyway.
9687
9688 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9689 height that can be returned. Text lines whose y-coordinate is beyond
9690 Y-LIMIT are ignored. Since calculating the text height of a large
9691 buffer can take some time, it makes sense to specify this argument if
9692 the size of the buffer is unknown.
9693
9694 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9695 include the height of the mode- or header-line of WINDOW in the return
9696 value. If it is either the symbol `mode-line' or `header-line', include
9697 only the height of that line, if present, in the return value. If t,
9698 include the height of both, if present, in the return value. */)
9699 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9700 Lisp_Object mode_and_header_line)
9701 {
9702 struct window *w = decode_live_window (window);
9703 Lisp_Object buf;
9704 struct buffer *b;
9705 struct it it;
9706 struct buffer *old_buffer = NULL;
9707 ptrdiff_t start, end, pos;
9708 struct text_pos startp;
9709 void *itdata = NULL;
9710 int c, max_y = -1, x = 0, y = 0;
9711
9712 buf = w->contents;
9713 CHECK_BUFFER (buf);
9714 b = XBUFFER (buf);
9715
9716 if (b != current_buffer)
9717 {
9718 old_buffer = current_buffer;
9719 set_buffer_internal (b);
9720 }
9721
9722 if (NILP (from))
9723 start = BEGV;
9724 else if (EQ (from, Qt))
9725 {
9726 start = pos = BEGV;
9727 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9728 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9729 start = pos;
9730 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9731 start = pos;
9732 }
9733 else
9734 {
9735 CHECK_NUMBER_COERCE_MARKER (from);
9736 start = min (max (XINT (from), BEGV), ZV);
9737 }
9738
9739 if (NILP (to))
9740 end = ZV;
9741 else if (EQ (to, Qt))
9742 {
9743 end = pos = ZV;
9744 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9745 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9746 end = pos;
9747 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9748 end = pos;
9749 }
9750 else
9751 {
9752 CHECK_NUMBER_COERCE_MARKER (to);
9753 end = max (start, min (XINT (to), ZV));
9754 }
9755
9756 if (!NILP (y_limit))
9757 {
9758 CHECK_NUMBER (y_limit);
9759 max_y = min (XINT (y_limit), INT_MAX);
9760 }
9761
9762 itdata = bidi_shelve_cache ();
9763 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9764 start_display (&it, w, startp);
9765
9766 if (NILP (x_limit))
9767 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9768 else
9769 {
9770 CHECK_NUMBER (x_limit);
9771 it.last_visible_x = min (XINT (x_limit), INFINITY);
9772 /* Actually, we never want move_it_to stop at to_x. But to make
9773 sure that move_it_in_display_line_to always moves far enough,
9774 we set it to INT_MAX and specify MOVE_TO_X. */
9775 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9776 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9777 }
9778
9779 y = it.current_y + it.max_ascent + it.max_descent;
9780
9781 if (!EQ (mode_and_header_line, Qheader_line)
9782 && !EQ (mode_and_header_line, Qt))
9783 /* Do not count the header-line which was counted automatically by
9784 start_display. */
9785 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9786
9787 if (EQ (mode_and_header_line, Qmode_line)
9788 || EQ (mode_and_header_line, Qt))
9789 /* Do count the mode-line which is not included automatically by
9790 start_display. */
9791 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9792
9793 bidi_unshelve_cache (itdata, 0);
9794
9795 if (old_buffer)
9796 set_buffer_internal (old_buffer);
9797
9798 return Fcons (make_number (x), make_number (y));
9799 }
9800 \f
9801 /***********************************************************************
9802 Messages
9803 ***********************************************************************/
9804
9805
9806 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9807 to *Messages*. */
9808
9809 void
9810 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9811 {
9812 Lisp_Object args[3];
9813 Lisp_Object msg, fmt;
9814 char *buffer;
9815 ptrdiff_t len;
9816 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9817 USE_SAFE_ALLOCA;
9818
9819 fmt = msg = Qnil;
9820 GCPRO4 (fmt, msg, arg1, arg2);
9821
9822 args[0] = fmt = build_string (format);
9823 args[1] = arg1;
9824 args[2] = arg2;
9825 msg = Fformat (3, args);
9826
9827 len = SBYTES (msg) + 1;
9828 buffer = SAFE_ALLOCA (len);
9829 memcpy (buffer, SDATA (msg), len);
9830
9831 message_dolog (buffer, len - 1, 1, 0);
9832 SAFE_FREE ();
9833
9834 UNGCPRO;
9835 }
9836
9837
9838 /* Output a newline in the *Messages* buffer if "needs" one. */
9839
9840 void
9841 message_log_maybe_newline (void)
9842 {
9843 if (message_log_need_newline)
9844 message_dolog ("", 0, 1, 0);
9845 }
9846
9847
9848 /* Add a string M of length NBYTES to the message log, optionally
9849 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9850 true, means interpret the contents of M as multibyte. This
9851 function calls low-level routines in order to bypass text property
9852 hooks, etc. which might not be safe to run.
9853
9854 This may GC (insert may run before/after change hooks),
9855 so the buffer M must NOT point to a Lisp string. */
9856
9857 void
9858 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9859 {
9860 const unsigned char *msg = (const unsigned char *) m;
9861
9862 if (!NILP (Vmemory_full))
9863 return;
9864
9865 if (!NILP (Vmessage_log_max))
9866 {
9867 struct buffer *oldbuf;
9868 Lisp_Object oldpoint, oldbegv, oldzv;
9869 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9870 ptrdiff_t point_at_end = 0;
9871 ptrdiff_t zv_at_end = 0;
9872 Lisp_Object old_deactivate_mark;
9873 struct gcpro gcpro1;
9874
9875 old_deactivate_mark = Vdeactivate_mark;
9876 oldbuf = current_buffer;
9877
9878 /* Ensure the Messages buffer exists, and switch to it.
9879 If we created it, set the major-mode. */
9880 {
9881 int newbuffer = 0;
9882 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9883
9884 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9885
9886 if (newbuffer
9887 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9888 call0 (intern ("messages-buffer-mode"));
9889 }
9890
9891 bset_undo_list (current_buffer, Qt);
9892 bset_cache_long_scans (current_buffer, Qnil);
9893
9894 oldpoint = message_dolog_marker1;
9895 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9896 oldbegv = message_dolog_marker2;
9897 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9898 oldzv = message_dolog_marker3;
9899 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9900 GCPRO1 (old_deactivate_mark);
9901
9902 if (PT == Z)
9903 point_at_end = 1;
9904 if (ZV == Z)
9905 zv_at_end = 1;
9906
9907 BEGV = BEG;
9908 BEGV_BYTE = BEG_BYTE;
9909 ZV = Z;
9910 ZV_BYTE = Z_BYTE;
9911 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9912
9913 /* Insert the string--maybe converting multibyte to single byte
9914 or vice versa, so that all the text fits the buffer. */
9915 if (multibyte
9916 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9917 {
9918 ptrdiff_t i;
9919 int c, char_bytes;
9920 char work[1];
9921
9922 /* Convert a multibyte string to single-byte
9923 for the *Message* buffer. */
9924 for (i = 0; i < nbytes; i += char_bytes)
9925 {
9926 c = string_char_and_length (msg + i, &char_bytes);
9927 work[0] = CHAR_TO_BYTE8 (c);
9928 insert_1_both (work, 1, 1, 1, 0, 0);
9929 }
9930 }
9931 else if (! multibyte
9932 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9933 {
9934 ptrdiff_t i;
9935 int c, char_bytes;
9936 unsigned char str[MAX_MULTIBYTE_LENGTH];
9937 /* Convert a single-byte string to multibyte
9938 for the *Message* buffer. */
9939 for (i = 0; i < nbytes; i++)
9940 {
9941 c = msg[i];
9942 MAKE_CHAR_MULTIBYTE (c);
9943 char_bytes = CHAR_STRING (c, str);
9944 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9945 }
9946 }
9947 else if (nbytes)
9948 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9949
9950 if (nlflag)
9951 {
9952 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9953 printmax_t dups;
9954
9955 insert_1_both ("\n", 1, 1, 1, 0, 0);
9956
9957 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9958 this_bol = PT;
9959 this_bol_byte = PT_BYTE;
9960
9961 /* See if this line duplicates the previous one.
9962 If so, combine duplicates. */
9963 if (this_bol > BEG)
9964 {
9965 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9966 prev_bol = PT;
9967 prev_bol_byte = PT_BYTE;
9968
9969 dups = message_log_check_duplicate (prev_bol_byte,
9970 this_bol_byte);
9971 if (dups)
9972 {
9973 del_range_both (prev_bol, prev_bol_byte,
9974 this_bol, this_bol_byte, 0);
9975 if (dups > 1)
9976 {
9977 char dupstr[sizeof " [ times]"
9978 + INT_STRLEN_BOUND (printmax_t)];
9979
9980 /* If you change this format, don't forget to also
9981 change message_log_check_duplicate. */
9982 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9983 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9984 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9985 }
9986 }
9987 }
9988
9989 /* If we have more than the desired maximum number of lines
9990 in the *Messages* buffer now, delete the oldest ones.
9991 This is safe because we don't have undo in this buffer. */
9992
9993 if (NATNUMP (Vmessage_log_max))
9994 {
9995 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9996 -XFASTINT (Vmessage_log_max) - 1, 0);
9997 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9998 }
9999 }
10000 BEGV = marker_position (oldbegv);
10001 BEGV_BYTE = marker_byte_position (oldbegv);
10002
10003 if (zv_at_end)
10004 {
10005 ZV = Z;
10006 ZV_BYTE = Z_BYTE;
10007 }
10008 else
10009 {
10010 ZV = marker_position (oldzv);
10011 ZV_BYTE = marker_byte_position (oldzv);
10012 }
10013
10014 if (point_at_end)
10015 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10016 else
10017 /* We can't do Fgoto_char (oldpoint) because it will run some
10018 Lisp code. */
10019 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10020 marker_byte_position (oldpoint));
10021
10022 UNGCPRO;
10023 unchain_marker (XMARKER (oldpoint));
10024 unchain_marker (XMARKER (oldbegv));
10025 unchain_marker (XMARKER (oldzv));
10026
10027 /* We called insert_1_both above with its 5th argument (PREPARE)
10028 zero, which prevents insert_1_both from calling
10029 prepare_to_modify_buffer, which in turns prevents us from
10030 incrementing windows_or_buffers_changed even if *Messages* is
10031 shown in some window. So we must manually set
10032 windows_or_buffers_changed here to make up for that. */
10033 windows_or_buffers_changed = old_windows_or_buffers_changed;
10034 bset_redisplay (current_buffer);
10035
10036 set_buffer_internal (oldbuf);
10037
10038 message_log_need_newline = !nlflag;
10039 Vdeactivate_mark = old_deactivate_mark;
10040 }
10041 }
10042
10043
10044 /* We are at the end of the buffer after just having inserted a newline.
10045 (Note: We depend on the fact we won't be crossing the gap.)
10046 Check to see if the most recent message looks a lot like the previous one.
10047 Return 0 if different, 1 if the new one should just replace it, or a
10048 value N > 1 if we should also append " [N times]". */
10049
10050 static intmax_t
10051 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10052 {
10053 ptrdiff_t i;
10054 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10055 int seen_dots = 0;
10056 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10057 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10058
10059 for (i = 0; i < len; i++)
10060 {
10061 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10062 seen_dots = 1;
10063 if (p1[i] != p2[i])
10064 return seen_dots;
10065 }
10066 p1 += len;
10067 if (*p1 == '\n')
10068 return 2;
10069 if (*p1++ == ' ' && *p1++ == '[')
10070 {
10071 char *pend;
10072 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10073 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10074 return n + 1;
10075 }
10076 return 0;
10077 }
10078 \f
10079
10080 /* Display an echo area message M with a specified length of NBYTES
10081 bytes. The string may include null characters. If M is not a
10082 string, clear out any existing message, and let the mini-buffer
10083 text show through.
10084
10085 This function cancels echoing. */
10086
10087 void
10088 message3 (Lisp_Object m)
10089 {
10090 struct gcpro gcpro1;
10091
10092 GCPRO1 (m);
10093 clear_message (true, true);
10094 cancel_echoing ();
10095
10096 /* First flush out any partial line written with print. */
10097 message_log_maybe_newline ();
10098 if (STRINGP (m))
10099 {
10100 ptrdiff_t nbytes = SBYTES (m);
10101 bool multibyte = STRING_MULTIBYTE (m);
10102 char *buffer;
10103 USE_SAFE_ALLOCA;
10104 SAFE_ALLOCA_STRING (buffer, m);
10105 message_dolog (buffer, nbytes, 1, multibyte);
10106 SAFE_FREE ();
10107 }
10108 message3_nolog (m);
10109
10110 UNGCPRO;
10111 }
10112
10113
10114 /* The non-logging version of message3.
10115 This does not cancel echoing, because it is used for echoing.
10116 Perhaps we need to make a separate function for echoing
10117 and make this cancel echoing. */
10118
10119 void
10120 message3_nolog (Lisp_Object m)
10121 {
10122 struct frame *sf = SELECTED_FRAME ();
10123
10124 if (FRAME_INITIAL_P (sf))
10125 {
10126 if (noninteractive_need_newline)
10127 putc ('\n', stderr);
10128 noninteractive_need_newline = 0;
10129 if (STRINGP (m))
10130 {
10131 Lisp_Object s = ENCODE_SYSTEM (m);
10132
10133 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10134 }
10135 if (cursor_in_echo_area == 0)
10136 fprintf (stderr, "\n");
10137 fflush (stderr);
10138 }
10139 /* Error messages get reported properly by cmd_error, so this must be just an
10140 informative message; if the frame hasn't really been initialized yet, just
10141 toss it. */
10142 else if (INTERACTIVE && sf->glyphs_initialized_p)
10143 {
10144 /* Get the frame containing the mini-buffer
10145 that the selected frame is using. */
10146 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10147 Lisp_Object frame = XWINDOW (mini_window)->frame;
10148 struct frame *f = XFRAME (frame);
10149
10150 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10151 Fmake_frame_visible (frame);
10152
10153 if (STRINGP (m) && SCHARS (m) > 0)
10154 {
10155 set_message (m);
10156 if (minibuffer_auto_raise)
10157 Fraise_frame (frame);
10158 /* Assume we are not echoing.
10159 (If we are, echo_now will override this.) */
10160 echo_message_buffer = Qnil;
10161 }
10162 else
10163 clear_message (true, true);
10164
10165 do_pending_window_change (false);
10166 echo_area_display (true);
10167 do_pending_window_change (false);
10168 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10169 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10170 }
10171 }
10172
10173
10174 /* Display a null-terminated echo area message M. If M is 0, clear
10175 out any existing message, and let the mini-buffer text show through.
10176
10177 The buffer M must continue to exist until after the echo area gets
10178 cleared or some other message gets displayed there. Do not pass
10179 text that is stored in a Lisp string. Do not pass text in a buffer
10180 that was alloca'd. */
10181
10182 void
10183 message1 (const char *m)
10184 {
10185 message3 (m ? build_unibyte_string (m) : Qnil);
10186 }
10187
10188
10189 /* The non-logging counterpart of message1. */
10190
10191 void
10192 message1_nolog (const char *m)
10193 {
10194 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10195 }
10196
10197 /* Display a message M which contains a single %s
10198 which gets replaced with STRING. */
10199
10200 void
10201 message_with_string (const char *m, Lisp_Object string, int log)
10202 {
10203 CHECK_STRING (string);
10204
10205 if (noninteractive)
10206 {
10207 if (m)
10208 {
10209 /* ENCODE_SYSTEM below can GC and/or relocate the
10210 Lisp data, so make sure we don't use it here. */
10211 eassert (relocatable_string_data_p (m) != 1);
10212
10213 if (noninteractive_need_newline)
10214 putc ('\n', stderr);
10215 noninteractive_need_newline = 0;
10216 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10217 if (!cursor_in_echo_area)
10218 fprintf (stderr, "\n");
10219 fflush (stderr);
10220 }
10221 }
10222 else if (INTERACTIVE)
10223 {
10224 /* The frame whose minibuffer we're going to display the message on.
10225 It may be larger than the selected frame, so we need
10226 to use its buffer, not the selected frame's buffer. */
10227 Lisp_Object mini_window;
10228 struct frame *f, *sf = SELECTED_FRAME ();
10229
10230 /* Get the frame containing the minibuffer
10231 that the selected frame is using. */
10232 mini_window = FRAME_MINIBUF_WINDOW (sf);
10233 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10234
10235 /* Error messages get reported properly by cmd_error, so this must be
10236 just an informative message; if the frame hasn't really been
10237 initialized yet, just toss it. */
10238 if (f->glyphs_initialized_p)
10239 {
10240 Lisp_Object args[2], msg;
10241 struct gcpro gcpro1, gcpro2;
10242
10243 args[0] = build_string (m);
10244 args[1] = msg = string;
10245 GCPRO2 (args[0], msg);
10246 gcpro1.nvars = 2;
10247
10248 msg = Fformat (2, args);
10249
10250 if (log)
10251 message3 (msg);
10252 else
10253 message3_nolog (msg);
10254
10255 UNGCPRO;
10256
10257 /* Print should start at the beginning of the message
10258 buffer next time. */
10259 message_buf_print = 0;
10260 }
10261 }
10262 }
10263
10264
10265 /* Dump an informative message to the minibuf. If M is 0, clear out
10266 any existing message, and let the mini-buffer text show through. */
10267
10268 static void
10269 vmessage (const char *m, va_list ap)
10270 {
10271 if (noninteractive)
10272 {
10273 if (m)
10274 {
10275 if (noninteractive_need_newline)
10276 putc ('\n', stderr);
10277 noninteractive_need_newline = 0;
10278 vfprintf (stderr, m, ap);
10279 if (cursor_in_echo_area == 0)
10280 fprintf (stderr, "\n");
10281 fflush (stderr);
10282 }
10283 }
10284 else if (INTERACTIVE)
10285 {
10286 /* The frame whose mini-buffer we're going to display the message
10287 on. It may be larger than the selected frame, so we need to
10288 use its buffer, not the selected frame's buffer. */
10289 Lisp_Object mini_window;
10290 struct frame *f, *sf = SELECTED_FRAME ();
10291
10292 /* Get the frame containing the mini-buffer
10293 that the selected frame is using. */
10294 mini_window = FRAME_MINIBUF_WINDOW (sf);
10295 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10296
10297 /* Error messages get reported properly by cmd_error, so this must be
10298 just an informative message; if the frame hasn't really been
10299 initialized yet, just toss it. */
10300 if (f->glyphs_initialized_p)
10301 {
10302 if (m)
10303 {
10304 ptrdiff_t len;
10305 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10306 USE_SAFE_ALLOCA;
10307 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10308
10309 len = doprnt (message_buf, maxsize, m, 0, ap);
10310
10311 message3 (make_string (message_buf, len));
10312 SAFE_FREE ();
10313 }
10314 else
10315 message1 (0);
10316
10317 /* Print should start at the beginning of the message
10318 buffer next time. */
10319 message_buf_print = 0;
10320 }
10321 }
10322 }
10323
10324 void
10325 message (const char *m, ...)
10326 {
10327 va_list ap;
10328 va_start (ap, m);
10329 vmessage (m, ap);
10330 va_end (ap);
10331 }
10332
10333
10334 #if 0
10335 /* The non-logging version of message. */
10336
10337 void
10338 message_nolog (const char *m, ...)
10339 {
10340 Lisp_Object old_log_max;
10341 va_list ap;
10342 va_start (ap, m);
10343 old_log_max = Vmessage_log_max;
10344 Vmessage_log_max = Qnil;
10345 vmessage (m, ap);
10346 Vmessage_log_max = old_log_max;
10347 va_end (ap);
10348 }
10349 #endif
10350
10351
10352 /* Display the current message in the current mini-buffer. This is
10353 only called from error handlers in process.c, and is not time
10354 critical. */
10355
10356 void
10357 update_echo_area (void)
10358 {
10359 if (!NILP (echo_area_buffer[0]))
10360 {
10361 Lisp_Object string;
10362 string = Fcurrent_message ();
10363 message3 (string);
10364 }
10365 }
10366
10367
10368 /* Make sure echo area buffers in `echo_buffers' are live.
10369 If they aren't, make new ones. */
10370
10371 static void
10372 ensure_echo_area_buffers (void)
10373 {
10374 int i;
10375
10376 for (i = 0; i < 2; ++i)
10377 if (!BUFFERP (echo_buffer[i])
10378 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10379 {
10380 char name[30];
10381 Lisp_Object old_buffer;
10382 int j;
10383
10384 old_buffer = echo_buffer[i];
10385 echo_buffer[i] = Fget_buffer_create
10386 (make_formatted_string (name, " *Echo Area %d*", i));
10387 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10388 /* to force word wrap in echo area -
10389 it was decided to postpone this*/
10390 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10391
10392 for (j = 0; j < 2; ++j)
10393 if (EQ (old_buffer, echo_area_buffer[j]))
10394 echo_area_buffer[j] = echo_buffer[i];
10395 }
10396 }
10397
10398
10399 /* Call FN with args A1..A2 with either the current or last displayed
10400 echo_area_buffer as current buffer.
10401
10402 WHICH zero means use the current message buffer
10403 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10404 from echo_buffer[] and clear it.
10405
10406 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10407 suitable buffer from echo_buffer[] and clear it.
10408
10409 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10410 that the current message becomes the last displayed one, make
10411 choose a suitable buffer for echo_area_buffer[0], and clear it.
10412
10413 Value is what FN returns. */
10414
10415 static int
10416 with_echo_area_buffer (struct window *w, int which,
10417 int (*fn) (ptrdiff_t, Lisp_Object),
10418 ptrdiff_t a1, Lisp_Object a2)
10419 {
10420 Lisp_Object buffer;
10421 int this_one, the_other, clear_buffer_p, rc;
10422 ptrdiff_t count = SPECPDL_INDEX ();
10423
10424 /* If buffers aren't live, make new ones. */
10425 ensure_echo_area_buffers ();
10426
10427 clear_buffer_p = 0;
10428
10429 if (which == 0)
10430 this_one = 0, the_other = 1;
10431 else if (which > 0)
10432 this_one = 1, the_other = 0;
10433 else
10434 {
10435 this_one = 0, the_other = 1;
10436 clear_buffer_p = true;
10437
10438 /* We need a fresh one in case the current echo buffer equals
10439 the one containing the last displayed echo area message. */
10440 if (!NILP (echo_area_buffer[this_one])
10441 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10442 echo_area_buffer[this_one] = Qnil;
10443 }
10444
10445 /* Choose a suitable buffer from echo_buffer[] is we don't
10446 have one. */
10447 if (NILP (echo_area_buffer[this_one]))
10448 {
10449 echo_area_buffer[this_one]
10450 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10451 ? echo_buffer[the_other]
10452 : echo_buffer[this_one]);
10453 clear_buffer_p = true;
10454 }
10455
10456 buffer = echo_area_buffer[this_one];
10457
10458 /* Don't get confused by reusing the buffer used for echoing
10459 for a different purpose. */
10460 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10461 cancel_echoing ();
10462
10463 record_unwind_protect (unwind_with_echo_area_buffer,
10464 with_echo_area_buffer_unwind_data (w));
10465
10466 /* Make the echo area buffer current. Note that for display
10467 purposes, it is not necessary that the displayed window's buffer
10468 == current_buffer, except for text property lookup. So, let's
10469 only set that buffer temporarily here without doing a full
10470 Fset_window_buffer. We must also change w->pointm, though,
10471 because otherwise an assertions in unshow_buffer fails, and Emacs
10472 aborts. */
10473 set_buffer_internal_1 (XBUFFER (buffer));
10474 if (w)
10475 {
10476 wset_buffer (w, buffer);
10477 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10478 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10479 }
10480
10481 bset_undo_list (current_buffer, Qt);
10482 bset_read_only (current_buffer, Qnil);
10483 specbind (Qinhibit_read_only, Qt);
10484 specbind (Qinhibit_modification_hooks, Qt);
10485
10486 if (clear_buffer_p && Z > BEG)
10487 del_range (BEG, Z);
10488
10489 eassert (BEGV >= BEG);
10490 eassert (ZV <= Z && ZV >= BEGV);
10491
10492 rc = fn (a1, a2);
10493
10494 eassert (BEGV >= BEG);
10495 eassert (ZV <= Z && ZV >= BEGV);
10496
10497 unbind_to (count, Qnil);
10498 return rc;
10499 }
10500
10501
10502 /* Save state that should be preserved around the call to the function
10503 FN called in with_echo_area_buffer. */
10504
10505 static Lisp_Object
10506 with_echo_area_buffer_unwind_data (struct window *w)
10507 {
10508 int i = 0;
10509 Lisp_Object vector, tmp;
10510
10511 /* Reduce consing by keeping one vector in
10512 Vwith_echo_area_save_vector. */
10513 vector = Vwith_echo_area_save_vector;
10514 Vwith_echo_area_save_vector = Qnil;
10515
10516 if (NILP (vector))
10517 vector = Fmake_vector (make_number (11), Qnil);
10518
10519 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10520 ASET (vector, i, Vdeactivate_mark); ++i;
10521 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10522
10523 if (w)
10524 {
10525 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10526 ASET (vector, i, w->contents); ++i;
10527 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10528 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10529 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10530 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10531 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10532 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10533 }
10534 else
10535 {
10536 int end = i + 8;
10537 for (; i < end; ++i)
10538 ASET (vector, i, Qnil);
10539 }
10540
10541 eassert (i == ASIZE (vector));
10542 return vector;
10543 }
10544
10545
10546 /* Restore global state from VECTOR which was created by
10547 with_echo_area_buffer_unwind_data. */
10548
10549 static void
10550 unwind_with_echo_area_buffer (Lisp_Object vector)
10551 {
10552 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10553 Vdeactivate_mark = AREF (vector, 1);
10554 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10555
10556 if (WINDOWP (AREF (vector, 3)))
10557 {
10558 struct window *w;
10559 Lisp_Object buffer;
10560
10561 w = XWINDOW (AREF (vector, 3));
10562 buffer = AREF (vector, 4);
10563
10564 wset_buffer (w, buffer);
10565 set_marker_both (w->pointm, buffer,
10566 XFASTINT (AREF (vector, 5)),
10567 XFASTINT (AREF (vector, 6)));
10568 set_marker_both (w->old_pointm, buffer,
10569 XFASTINT (AREF (vector, 7)),
10570 XFASTINT (AREF (vector, 8)));
10571 set_marker_both (w->start, buffer,
10572 XFASTINT (AREF (vector, 9)),
10573 XFASTINT (AREF (vector, 10)));
10574 }
10575
10576 Vwith_echo_area_save_vector = vector;
10577 }
10578
10579
10580 /* Set up the echo area for use by print functions. MULTIBYTE_P
10581 non-zero means we will print multibyte. */
10582
10583 void
10584 setup_echo_area_for_printing (int multibyte_p)
10585 {
10586 /* If we can't find an echo area any more, exit. */
10587 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10588 Fkill_emacs (Qnil);
10589
10590 ensure_echo_area_buffers ();
10591
10592 if (!message_buf_print)
10593 {
10594 /* A message has been output since the last time we printed.
10595 Choose a fresh echo area buffer. */
10596 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10597 echo_area_buffer[0] = echo_buffer[1];
10598 else
10599 echo_area_buffer[0] = echo_buffer[0];
10600
10601 /* Switch to that buffer and clear it. */
10602 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10603 bset_truncate_lines (current_buffer, Qnil);
10604
10605 if (Z > BEG)
10606 {
10607 ptrdiff_t count = SPECPDL_INDEX ();
10608 specbind (Qinhibit_read_only, Qt);
10609 /* Note that undo recording is always disabled. */
10610 del_range (BEG, Z);
10611 unbind_to (count, Qnil);
10612 }
10613 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10614
10615 /* Set up the buffer for the multibyteness we need. */
10616 if (multibyte_p
10617 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10618 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10619
10620 /* Raise the frame containing the echo area. */
10621 if (minibuffer_auto_raise)
10622 {
10623 struct frame *sf = SELECTED_FRAME ();
10624 Lisp_Object mini_window;
10625 mini_window = FRAME_MINIBUF_WINDOW (sf);
10626 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10627 }
10628
10629 message_log_maybe_newline ();
10630 message_buf_print = 1;
10631 }
10632 else
10633 {
10634 if (NILP (echo_area_buffer[0]))
10635 {
10636 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10637 echo_area_buffer[0] = echo_buffer[1];
10638 else
10639 echo_area_buffer[0] = echo_buffer[0];
10640 }
10641
10642 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10643 {
10644 /* Someone switched buffers between print requests. */
10645 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10646 bset_truncate_lines (current_buffer, Qnil);
10647 }
10648 }
10649 }
10650
10651
10652 /* Display an echo area message in window W. Value is non-zero if W's
10653 height is changed. If display_last_displayed_message_p is
10654 non-zero, display the message that was last displayed, otherwise
10655 display the current message. */
10656
10657 static int
10658 display_echo_area (struct window *w)
10659 {
10660 int i, no_message_p, window_height_changed_p;
10661
10662 /* Temporarily disable garbage collections while displaying the echo
10663 area. This is done because a GC can print a message itself.
10664 That message would modify the echo area buffer's contents while a
10665 redisplay of the buffer is going on, and seriously confuse
10666 redisplay. */
10667 ptrdiff_t count = inhibit_garbage_collection ();
10668
10669 /* If there is no message, we must call display_echo_area_1
10670 nevertheless because it resizes the window. But we will have to
10671 reset the echo_area_buffer in question to nil at the end because
10672 with_echo_area_buffer will sets it to an empty buffer. */
10673 i = display_last_displayed_message_p ? 1 : 0;
10674 no_message_p = NILP (echo_area_buffer[i]);
10675
10676 window_height_changed_p
10677 = with_echo_area_buffer (w, display_last_displayed_message_p,
10678 display_echo_area_1,
10679 (intptr_t) w, Qnil);
10680
10681 if (no_message_p)
10682 echo_area_buffer[i] = Qnil;
10683
10684 unbind_to (count, Qnil);
10685 return window_height_changed_p;
10686 }
10687
10688
10689 /* Helper for display_echo_area. Display the current buffer which
10690 contains the current echo area message in window W, a mini-window,
10691 a pointer to which is passed in A1. A2..A4 are currently not used.
10692 Change the height of W so that all of the message is displayed.
10693 Value is non-zero if height of W was changed. */
10694
10695 static int
10696 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10697 {
10698 intptr_t i1 = a1;
10699 struct window *w = (struct window *) i1;
10700 Lisp_Object window;
10701 struct text_pos start;
10702 int window_height_changed_p = 0;
10703
10704 /* Do this before displaying, so that we have a large enough glyph
10705 matrix for the display. If we can't get enough space for the
10706 whole text, display the last N lines. That works by setting w->start. */
10707 window_height_changed_p = resize_mini_window (w, 0);
10708
10709 /* Use the starting position chosen by resize_mini_window. */
10710 SET_TEXT_POS_FROM_MARKER (start, w->start);
10711
10712 /* Display. */
10713 clear_glyph_matrix (w->desired_matrix);
10714 XSETWINDOW (window, w);
10715 try_window (window, start, 0);
10716
10717 return window_height_changed_p;
10718 }
10719
10720
10721 /* Resize the echo area window to exactly the size needed for the
10722 currently displayed message, if there is one. If a mini-buffer
10723 is active, don't shrink it. */
10724
10725 void
10726 resize_echo_area_exactly (void)
10727 {
10728 if (BUFFERP (echo_area_buffer[0])
10729 && WINDOWP (echo_area_window))
10730 {
10731 struct window *w = XWINDOW (echo_area_window);
10732 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10733 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10734 (intptr_t) w, resize_exactly);
10735 if (resized_p)
10736 {
10737 windows_or_buffers_changed = 42;
10738 update_mode_lines = 30;
10739 redisplay_internal ();
10740 }
10741 }
10742 }
10743
10744
10745 /* Callback function for with_echo_area_buffer, when used from
10746 resize_echo_area_exactly. A1 contains a pointer to the window to
10747 resize, EXACTLY non-nil means resize the mini-window exactly to the
10748 size of the text displayed. A3 and A4 are not used. Value is what
10749 resize_mini_window returns. */
10750
10751 static int
10752 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10753 {
10754 intptr_t i1 = a1;
10755 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10756 }
10757
10758
10759 /* Resize mini-window W to fit the size of its contents. EXACT_P
10760 means size the window exactly to the size needed. Otherwise, it's
10761 only enlarged until W's buffer is empty.
10762
10763 Set W->start to the right place to begin display. If the whole
10764 contents fit, start at the beginning. Otherwise, start so as
10765 to make the end of the contents appear. This is particularly
10766 important for y-or-n-p, but seems desirable generally.
10767
10768 Value is non-zero if the window height has been changed. */
10769
10770 int
10771 resize_mini_window (struct window *w, int exact_p)
10772 {
10773 struct frame *f = XFRAME (w->frame);
10774 int window_height_changed_p = 0;
10775
10776 eassert (MINI_WINDOW_P (w));
10777
10778 /* By default, start display at the beginning. */
10779 set_marker_both (w->start, w->contents,
10780 BUF_BEGV (XBUFFER (w->contents)),
10781 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10782
10783 /* Don't resize windows while redisplaying a window; it would
10784 confuse redisplay functions when the size of the window they are
10785 displaying changes from under them. Such a resizing can happen,
10786 for instance, when which-func prints a long message while
10787 we are running fontification-functions. We're running these
10788 functions with safe_call which binds inhibit-redisplay to t. */
10789 if (!NILP (Vinhibit_redisplay))
10790 return 0;
10791
10792 /* Nil means don't try to resize. */
10793 if (NILP (Vresize_mini_windows)
10794 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10795 return 0;
10796
10797 if (!FRAME_MINIBUF_ONLY_P (f))
10798 {
10799 struct it it;
10800 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10801 + WINDOW_PIXEL_HEIGHT (w));
10802 int unit = FRAME_LINE_HEIGHT (f);
10803 int height, max_height;
10804 struct text_pos start;
10805 struct buffer *old_current_buffer = NULL;
10806
10807 if (current_buffer != XBUFFER (w->contents))
10808 {
10809 old_current_buffer = current_buffer;
10810 set_buffer_internal (XBUFFER (w->contents));
10811 }
10812
10813 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10814
10815 /* Compute the max. number of lines specified by the user. */
10816 if (FLOATP (Vmax_mini_window_height))
10817 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10818 else if (INTEGERP (Vmax_mini_window_height))
10819 max_height = XINT (Vmax_mini_window_height) * unit;
10820 else
10821 max_height = total_height / 4;
10822
10823 /* Correct that max. height if it's bogus. */
10824 max_height = clip_to_bounds (unit, max_height, total_height);
10825
10826 /* Find out the height of the text in the window. */
10827 if (it.line_wrap == TRUNCATE)
10828 height = unit;
10829 else
10830 {
10831 last_height = 0;
10832 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10833 if (it.max_ascent == 0 && it.max_descent == 0)
10834 height = it.current_y + last_height;
10835 else
10836 height = it.current_y + it.max_ascent + it.max_descent;
10837 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10838 }
10839
10840 /* Compute a suitable window start. */
10841 if (height > max_height)
10842 {
10843 height = (max_height / unit) * unit;
10844 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10845 move_it_vertically_backward (&it, height - unit);
10846 start = it.current.pos;
10847 }
10848 else
10849 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10850 SET_MARKER_FROM_TEXT_POS (w->start, start);
10851
10852 if (EQ (Vresize_mini_windows, Qgrow_only))
10853 {
10854 /* Let it grow only, until we display an empty message, in which
10855 case the window shrinks again. */
10856 if (height > WINDOW_PIXEL_HEIGHT (w))
10857 {
10858 int old_height = WINDOW_PIXEL_HEIGHT (w);
10859
10860 FRAME_WINDOWS_FROZEN (f) = 1;
10861 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10862 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10863 }
10864 else if (height < WINDOW_PIXEL_HEIGHT (w)
10865 && (exact_p || BEGV == ZV))
10866 {
10867 int old_height = WINDOW_PIXEL_HEIGHT (w);
10868
10869 FRAME_WINDOWS_FROZEN (f) = 0;
10870 shrink_mini_window (w, 1);
10871 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10872 }
10873 }
10874 else
10875 {
10876 /* Always resize to exact size needed. */
10877 if (height > WINDOW_PIXEL_HEIGHT (w))
10878 {
10879 int old_height = WINDOW_PIXEL_HEIGHT (w);
10880
10881 FRAME_WINDOWS_FROZEN (f) = 1;
10882 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10883 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10884 }
10885 else if (height < WINDOW_PIXEL_HEIGHT (w))
10886 {
10887 int old_height = WINDOW_PIXEL_HEIGHT (w);
10888
10889 FRAME_WINDOWS_FROZEN (f) = 0;
10890 shrink_mini_window (w, 1);
10891
10892 if (height)
10893 {
10894 FRAME_WINDOWS_FROZEN (f) = 1;
10895 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10896 }
10897
10898 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10899 }
10900 }
10901
10902 if (old_current_buffer)
10903 set_buffer_internal (old_current_buffer);
10904 }
10905
10906 return window_height_changed_p;
10907 }
10908
10909
10910 /* Value is the current message, a string, or nil if there is no
10911 current message. */
10912
10913 Lisp_Object
10914 current_message (void)
10915 {
10916 Lisp_Object msg;
10917
10918 if (!BUFFERP (echo_area_buffer[0]))
10919 msg = Qnil;
10920 else
10921 {
10922 with_echo_area_buffer (0, 0, current_message_1,
10923 (intptr_t) &msg, Qnil);
10924 if (NILP (msg))
10925 echo_area_buffer[0] = Qnil;
10926 }
10927
10928 return msg;
10929 }
10930
10931
10932 static int
10933 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10934 {
10935 intptr_t i1 = a1;
10936 Lisp_Object *msg = (Lisp_Object *) i1;
10937
10938 if (Z > BEG)
10939 *msg = make_buffer_string (BEG, Z, 1);
10940 else
10941 *msg = Qnil;
10942 return 0;
10943 }
10944
10945
10946 /* Push the current message on Vmessage_stack for later restoration
10947 by restore_message. Value is non-zero if the current message isn't
10948 empty. This is a relatively infrequent operation, so it's not
10949 worth optimizing. */
10950
10951 bool
10952 push_message (void)
10953 {
10954 Lisp_Object msg = current_message ();
10955 Vmessage_stack = Fcons (msg, Vmessage_stack);
10956 return STRINGP (msg);
10957 }
10958
10959
10960 /* Restore message display from the top of Vmessage_stack. */
10961
10962 void
10963 restore_message (void)
10964 {
10965 eassert (CONSP (Vmessage_stack));
10966 message3_nolog (XCAR (Vmessage_stack));
10967 }
10968
10969
10970 /* Handler for unwind-protect calling pop_message. */
10971
10972 void
10973 pop_message_unwind (void)
10974 {
10975 /* Pop the top-most entry off Vmessage_stack. */
10976 eassert (CONSP (Vmessage_stack));
10977 Vmessage_stack = XCDR (Vmessage_stack);
10978 }
10979
10980
10981 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10982 exits. If the stack is not empty, we have a missing pop_message
10983 somewhere. */
10984
10985 void
10986 check_message_stack (void)
10987 {
10988 if (!NILP (Vmessage_stack))
10989 emacs_abort ();
10990 }
10991
10992
10993 /* Truncate to NCHARS what will be displayed in the echo area the next
10994 time we display it---but don't redisplay it now. */
10995
10996 void
10997 truncate_echo_area (ptrdiff_t nchars)
10998 {
10999 if (nchars == 0)
11000 echo_area_buffer[0] = Qnil;
11001 else if (!noninteractive
11002 && INTERACTIVE
11003 && !NILP (echo_area_buffer[0]))
11004 {
11005 struct frame *sf = SELECTED_FRAME ();
11006 /* Error messages get reported properly by cmd_error, so this must be
11007 just an informative message; if the frame hasn't really been
11008 initialized yet, just toss it. */
11009 if (sf->glyphs_initialized_p)
11010 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11011 }
11012 }
11013
11014
11015 /* Helper function for truncate_echo_area. Truncate the current
11016 message to at most NCHARS characters. */
11017
11018 static int
11019 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11020 {
11021 if (BEG + nchars < Z)
11022 del_range (BEG + nchars, Z);
11023 if (Z == BEG)
11024 echo_area_buffer[0] = Qnil;
11025 return 0;
11026 }
11027
11028 /* Set the current message to STRING. */
11029
11030 static void
11031 set_message (Lisp_Object string)
11032 {
11033 eassert (STRINGP (string));
11034
11035 message_enable_multibyte = STRING_MULTIBYTE (string);
11036
11037 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11038 message_buf_print = 0;
11039 help_echo_showing_p = 0;
11040
11041 if (STRINGP (Vdebug_on_message)
11042 && STRINGP (string)
11043 && fast_string_match (Vdebug_on_message, string) >= 0)
11044 call_debugger (list2 (Qerror, string));
11045 }
11046
11047
11048 /* Helper function for set_message. First argument is ignored and second
11049 argument has the same meaning as for set_message.
11050 This function is called with the echo area buffer being current. */
11051
11052 static int
11053 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11054 {
11055 eassert (STRINGP (string));
11056
11057 /* Change multibyteness of the echo buffer appropriately. */
11058 if (message_enable_multibyte
11059 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11060 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11061
11062 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11063 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11064 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11065
11066 /* Insert new message at BEG. */
11067 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11068
11069 /* This function takes care of single/multibyte conversion.
11070 We just have to ensure that the echo area buffer has the right
11071 setting of enable_multibyte_characters. */
11072 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11073
11074 return 0;
11075 }
11076
11077
11078 /* Clear messages. CURRENT_P non-zero means clear the current
11079 message. LAST_DISPLAYED_P non-zero means clear the message
11080 last displayed. */
11081
11082 void
11083 clear_message (bool current_p, bool last_displayed_p)
11084 {
11085 if (current_p)
11086 {
11087 echo_area_buffer[0] = Qnil;
11088 message_cleared_p = true;
11089 }
11090
11091 if (last_displayed_p)
11092 echo_area_buffer[1] = Qnil;
11093
11094 message_buf_print = 0;
11095 }
11096
11097 /* Clear garbaged frames.
11098
11099 This function is used where the old redisplay called
11100 redraw_garbaged_frames which in turn called redraw_frame which in
11101 turn called clear_frame. The call to clear_frame was a source of
11102 flickering. I believe a clear_frame is not necessary. It should
11103 suffice in the new redisplay to invalidate all current matrices,
11104 and ensure a complete redisplay of all windows. */
11105
11106 static void
11107 clear_garbaged_frames (void)
11108 {
11109 if (frame_garbaged)
11110 {
11111 Lisp_Object tail, frame;
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 struct frame *f = XFRAME (frame);
11116
11117 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11118 {
11119 if (f->resized_p)
11120 redraw_frame (f);
11121 else
11122 clear_current_matrices (f);
11123 fset_redisplay (f);
11124 f->garbaged = false;
11125 f->resized_p = false;
11126 }
11127 }
11128
11129 frame_garbaged = false;
11130 }
11131 }
11132
11133
11134 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11135 is non-zero update selected_frame. Value is non-zero if the
11136 mini-windows height has been changed. */
11137
11138 static bool
11139 echo_area_display (bool update_frame_p)
11140 {
11141 Lisp_Object mini_window;
11142 struct window *w;
11143 struct frame *f;
11144 bool window_height_changed_p = false;
11145 struct frame *sf = SELECTED_FRAME ();
11146
11147 mini_window = FRAME_MINIBUF_WINDOW (sf);
11148 w = XWINDOW (mini_window);
11149 f = XFRAME (WINDOW_FRAME (w));
11150
11151 /* Don't display if frame is invisible or not yet initialized. */
11152 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11153 return 0;
11154
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 /* When Emacs starts, selected_frame may be the initial terminal
11157 frame. If we let this through, a message would be displayed on
11158 the terminal. */
11159 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11160 return 0;
11161 #endif /* HAVE_WINDOW_SYSTEM */
11162
11163 /* Redraw garbaged frames. */
11164 clear_garbaged_frames ();
11165
11166 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11167 {
11168 echo_area_window = mini_window;
11169 window_height_changed_p = display_echo_area (w);
11170 w->must_be_updated_p = true;
11171
11172 /* Update the display, unless called from redisplay_internal.
11173 Also don't update the screen during redisplay itself. The
11174 update will happen at the end of redisplay, and an update
11175 here could cause confusion. */
11176 if (update_frame_p && !redisplaying_p)
11177 {
11178 int n = 0;
11179
11180 /* If the display update has been interrupted by pending
11181 input, update mode lines in the frame. Due to the
11182 pending input, it might have been that redisplay hasn't
11183 been called, so that mode lines above the echo area are
11184 garbaged. This looks odd, so we prevent it here. */
11185 if (!display_completed)
11186 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11187
11188 if (window_height_changed_p
11189 /* Don't do this if Emacs is shutting down. Redisplay
11190 needs to run hooks. */
11191 && !NILP (Vrun_hooks))
11192 {
11193 /* Must update other windows. Likewise as in other
11194 cases, don't let this update be interrupted by
11195 pending input. */
11196 ptrdiff_t count = SPECPDL_INDEX ();
11197 specbind (Qredisplay_dont_pause, Qt);
11198 windows_or_buffers_changed = 44;
11199 redisplay_internal ();
11200 unbind_to (count, Qnil);
11201 }
11202 else if (FRAME_WINDOW_P (f) && n == 0)
11203 {
11204 /* Window configuration is the same as before.
11205 Can do with a display update of the echo area,
11206 unless we displayed some mode lines. */
11207 update_single_window (w);
11208 flush_frame (f);
11209 }
11210 else
11211 update_frame (f, true, true);
11212
11213 /* If cursor is in the echo area, make sure that the next
11214 redisplay displays the minibuffer, so that the cursor will
11215 be replaced with what the minibuffer wants. */
11216 if (cursor_in_echo_area)
11217 wset_redisplay (XWINDOW (mini_window));
11218 }
11219 }
11220 else if (!EQ (mini_window, selected_window))
11221 wset_redisplay (XWINDOW (mini_window));
11222
11223 /* Last displayed message is now the current message. */
11224 echo_area_buffer[1] = echo_area_buffer[0];
11225 /* Inform read_char that we're not echoing. */
11226 echo_message_buffer = Qnil;
11227
11228 /* Prevent redisplay optimization in redisplay_internal by resetting
11229 this_line_start_pos. This is done because the mini-buffer now
11230 displays the message instead of its buffer text. */
11231 if (EQ (mini_window, selected_window))
11232 CHARPOS (this_line_start_pos) = 0;
11233
11234 return window_height_changed_p;
11235 }
11236
11237 /* Nonzero if W's buffer was changed but not saved. */
11238
11239 static int
11240 window_buffer_changed (struct window *w)
11241 {
11242 struct buffer *b = XBUFFER (w->contents);
11243
11244 eassert (BUFFER_LIVE_P (b));
11245
11246 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11247 }
11248
11249 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11250
11251 static int
11252 mode_line_update_needed (struct window *w)
11253 {
11254 return (w->column_number_displayed != -1
11255 && !(PT == w->last_point && !window_outdated (w))
11256 && (w->column_number_displayed != current_column ()));
11257 }
11258
11259 /* Nonzero if window start of W is frozen and may not be changed during
11260 redisplay. */
11261
11262 static bool
11263 window_frozen_p (struct window *w)
11264 {
11265 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11266 {
11267 Lisp_Object window;
11268
11269 XSETWINDOW (window, w);
11270 if (MINI_WINDOW_P (w))
11271 return 0;
11272 else if (EQ (window, selected_window))
11273 return 0;
11274 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11275 && EQ (window, Vminibuf_scroll_window))
11276 /* This special window can't be frozen too. */
11277 return 0;
11278 else
11279 return 1;
11280 }
11281 return 0;
11282 }
11283
11284 /***********************************************************************
11285 Mode Lines and Frame Titles
11286 ***********************************************************************/
11287
11288 /* A buffer for constructing non-propertized mode-line strings and
11289 frame titles in it; allocated from the heap in init_xdisp and
11290 resized as needed in store_mode_line_noprop_char. */
11291
11292 static char *mode_line_noprop_buf;
11293
11294 /* The buffer's end, and a current output position in it. */
11295
11296 static char *mode_line_noprop_buf_end;
11297 static char *mode_line_noprop_ptr;
11298
11299 #define MODE_LINE_NOPROP_LEN(start) \
11300 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11301
11302 static enum {
11303 MODE_LINE_DISPLAY = 0,
11304 MODE_LINE_TITLE,
11305 MODE_LINE_NOPROP,
11306 MODE_LINE_STRING
11307 } mode_line_target;
11308
11309 /* Alist that caches the results of :propertize.
11310 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11311 static Lisp_Object mode_line_proptrans_alist;
11312
11313 /* List of strings making up the mode-line. */
11314 static Lisp_Object mode_line_string_list;
11315
11316 /* Base face property when building propertized mode line string. */
11317 static Lisp_Object mode_line_string_face;
11318 static Lisp_Object mode_line_string_face_prop;
11319
11320
11321 /* Unwind data for mode line strings */
11322
11323 static Lisp_Object Vmode_line_unwind_vector;
11324
11325 static Lisp_Object
11326 format_mode_line_unwind_data (struct frame *target_frame,
11327 struct buffer *obuf,
11328 Lisp_Object owin,
11329 int save_proptrans)
11330 {
11331 Lisp_Object vector, tmp;
11332
11333 /* Reduce consing by keeping one vector in
11334 Vwith_echo_area_save_vector. */
11335 vector = Vmode_line_unwind_vector;
11336 Vmode_line_unwind_vector = Qnil;
11337
11338 if (NILP (vector))
11339 vector = Fmake_vector (make_number (10), Qnil);
11340
11341 ASET (vector, 0, make_number (mode_line_target));
11342 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11343 ASET (vector, 2, mode_line_string_list);
11344 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11345 ASET (vector, 4, mode_line_string_face);
11346 ASET (vector, 5, mode_line_string_face_prop);
11347
11348 if (obuf)
11349 XSETBUFFER (tmp, obuf);
11350 else
11351 tmp = Qnil;
11352 ASET (vector, 6, tmp);
11353 ASET (vector, 7, owin);
11354 if (target_frame)
11355 {
11356 /* Similarly to `with-selected-window', if the operation selects
11357 a window on another frame, we must restore that frame's
11358 selected window, and (for a tty) the top-frame. */
11359 ASET (vector, 8, target_frame->selected_window);
11360 if (FRAME_TERMCAP_P (target_frame))
11361 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11362 }
11363
11364 return vector;
11365 }
11366
11367 static void
11368 unwind_format_mode_line (Lisp_Object vector)
11369 {
11370 Lisp_Object old_window = AREF (vector, 7);
11371 Lisp_Object target_frame_window = AREF (vector, 8);
11372 Lisp_Object old_top_frame = AREF (vector, 9);
11373
11374 mode_line_target = XINT (AREF (vector, 0));
11375 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11376 mode_line_string_list = AREF (vector, 2);
11377 if (! EQ (AREF (vector, 3), Qt))
11378 mode_line_proptrans_alist = AREF (vector, 3);
11379 mode_line_string_face = AREF (vector, 4);
11380 mode_line_string_face_prop = AREF (vector, 5);
11381
11382 /* Select window before buffer, since it may change the buffer. */
11383 if (!NILP (old_window))
11384 {
11385 /* If the operation that we are unwinding had selected a window
11386 on a different frame, reset its frame-selected-window. For a
11387 text terminal, reset its top-frame if necessary. */
11388 if (!NILP (target_frame_window))
11389 {
11390 Lisp_Object frame
11391 = WINDOW_FRAME (XWINDOW (target_frame_window));
11392
11393 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11394 Fselect_window (target_frame_window, Qt);
11395
11396 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11397 Fselect_frame (old_top_frame, Qt);
11398 }
11399
11400 Fselect_window (old_window, Qt);
11401 }
11402
11403 if (!NILP (AREF (vector, 6)))
11404 {
11405 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11406 ASET (vector, 6, Qnil);
11407 }
11408
11409 Vmode_line_unwind_vector = vector;
11410 }
11411
11412
11413 /* Store a single character C for the frame title in mode_line_noprop_buf.
11414 Re-allocate mode_line_noprop_buf if necessary. */
11415
11416 static void
11417 store_mode_line_noprop_char (char c)
11418 {
11419 /* If output position has reached the end of the allocated buffer,
11420 increase the buffer's size. */
11421 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11422 {
11423 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11424 ptrdiff_t size = len;
11425 mode_line_noprop_buf =
11426 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11427 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11428 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11429 }
11430
11431 *mode_line_noprop_ptr++ = c;
11432 }
11433
11434
11435 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11436 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11437 characters that yield more columns than PRECISION; PRECISION <= 0
11438 means copy the whole string. Pad with spaces until FIELD_WIDTH
11439 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11440 pad. Called from display_mode_element when it is used to build a
11441 frame title. */
11442
11443 static int
11444 store_mode_line_noprop (const char *string, int field_width, int precision)
11445 {
11446 const unsigned char *str = (const unsigned char *) string;
11447 int n = 0;
11448 ptrdiff_t dummy, nbytes;
11449
11450 /* Copy at most PRECISION chars from STR. */
11451 nbytes = strlen (string);
11452 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11453 while (nbytes--)
11454 store_mode_line_noprop_char (*str++);
11455
11456 /* Fill up with spaces until FIELD_WIDTH reached. */
11457 while (field_width > 0
11458 && n < field_width)
11459 {
11460 store_mode_line_noprop_char (' ');
11461 ++n;
11462 }
11463
11464 return n;
11465 }
11466
11467 /***********************************************************************
11468 Frame Titles
11469 ***********************************************************************/
11470
11471 #ifdef HAVE_WINDOW_SYSTEM
11472
11473 /* Set the title of FRAME, if it has changed. The title format is
11474 Vicon_title_format if FRAME is iconified, otherwise it is
11475 frame_title_format. */
11476
11477 static void
11478 x_consider_frame_title (Lisp_Object frame)
11479 {
11480 struct frame *f = XFRAME (frame);
11481
11482 if (FRAME_WINDOW_P (f)
11483 || FRAME_MINIBUF_ONLY_P (f)
11484 || f->explicit_name)
11485 {
11486 /* Do we have more than one visible frame on this X display? */
11487 Lisp_Object tail, other_frame, fmt;
11488 ptrdiff_t title_start;
11489 char *title;
11490 ptrdiff_t len;
11491 struct it it;
11492 ptrdiff_t count = SPECPDL_INDEX ();
11493
11494 FOR_EACH_FRAME (tail, other_frame)
11495 {
11496 struct frame *tf = XFRAME (other_frame);
11497
11498 if (tf != f
11499 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11500 && !FRAME_MINIBUF_ONLY_P (tf)
11501 && !EQ (other_frame, tip_frame)
11502 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11503 break;
11504 }
11505
11506 /* Set global variable indicating that multiple frames exist. */
11507 multiple_frames = CONSP (tail);
11508
11509 /* Switch to the buffer of selected window of the frame. Set up
11510 mode_line_target so that display_mode_element will output into
11511 mode_line_noprop_buf; then display the title. */
11512 record_unwind_protect (unwind_format_mode_line,
11513 format_mode_line_unwind_data
11514 (f, current_buffer, selected_window, 0));
11515
11516 Fselect_window (f->selected_window, Qt);
11517 set_buffer_internal_1
11518 (XBUFFER (XWINDOW (f->selected_window)->contents));
11519 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11520
11521 mode_line_target = MODE_LINE_TITLE;
11522 title_start = MODE_LINE_NOPROP_LEN (0);
11523 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11524 NULL, DEFAULT_FACE_ID);
11525 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11526 len = MODE_LINE_NOPROP_LEN (title_start);
11527 title = mode_line_noprop_buf + title_start;
11528 unbind_to (count, Qnil);
11529
11530 /* Set the title only if it's changed. This avoids consing in
11531 the common case where it hasn't. (If it turns out that we've
11532 already wasted too much time by walking through the list with
11533 display_mode_element, then we might need to optimize at a
11534 higher level than this.) */
11535 if (! STRINGP (f->name)
11536 || SBYTES (f->name) != len
11537 || memcmp (title, SDATA (f->name), len) != 0)
11538 x_implicitly_set_name (f, make_string (title, len), Qnil);
11539 }
11540 }
11541
11542 #endif /* not HAVE_WINDOW_SYSTEM */
11543
11544 \f
11545 /***********************************************************************
11546 Menu Bars
11547 ***********************************************************************/
11548
11549 /* Non-zero if we will not redisplay all visible windows. */
11550 #define REDISPLAY_SOME_P() \
11551 ((windows_or_buffers_changed == 0 \
11552 || windows_or_buffers_changed == REDISPLAY_SOME) \
11553 && (update_mode_lines == 0 \
11554 || update_mode_lines == REDISPLAY_SOME))
11555
11556 /* Prepare for redisplay by updating menu-bar item lists when
11557 appropriate. This can call eval. */
11558
11559 static void
11560 prepare_menu_bars (void)
11561 {
11562 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11563 bool some_windows = REDISPLAY_SOME_P ();
11564 struct gcpro gcpro1, gcpro2;
11565 Lisp_Object tooltip_frame;
11566
11567 #ifdef HAVE_WINDOW_SYSTEM
11568 tooltip_frame = tip_frame;
11569 #else
11570 tooltip_frame = Qnil;
11571 #endif
11572
11573 if (FUNCTIONP (Vpre_redisplay_function))
11574 {
11575 Lisp_Object windows = all_windows ? Qt : Qnil;
11576 if (all_windows && some_windows)
11577 {
11578 Lisp_Object ws = window_list ();
11579 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11580 {
11581 Lisp_Object this = XCAR (ws);
11582 struct window *w = XWINDOW (this);
11583 if (w->redisplay
11584 || XFRAME (w->frame)->redisplay
11585 || XBUFFER (w->contents)->text->redisplay)
11586 {
11587 windows = Fcons (this, windows);
11588 }
11589 }
11590 }
11591 safe__call1 (true, Vpre_redisplay_function, windows);
11592 }
11593
11594 /* Update all frame titles based on their buffer names, etc. We do
11595 this before the menu bars so that the buffer-menu will show the
11596 up-to-date frame titles. */
11597 #ifdef HAVE_WINDOW_SYSTEM
11598 if (all_windows)
11599 {
11600 Lisp_Object tail, frame;
11601
11602 FOR_EACH_FRAME (tail, frame)
11603 {
11604 struct frame *f = XFRAME (frame);
11605 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11606 if (some_windows
11607 && !f->redisplay
11608 && !w->redisplay
11609 && !XBUFFER (w->contents)->text->redisplay)
11610 continue;
11611
11612 if (!EQ (frame, tooltip_frame)
11613 && (FRAME_ICONIFIED_P (f)
11614 || FRAME_VISIBLE_P (f) == 1
11615 /* Exclude TTY frames that are obscured because they
11616 are not the top frame on their console. This is
11617 because x_consider_frame_title actually switches
11618 to the frame, which for TTY frames means it is
11619 marked as garbaged, and will be completely
11620 redrawn on the next redisplay cycle. This causes
11621 TTY frames to be completely redrawn, when there
11622 are more than one of them, even though nothing
11623 should be changed on display. */
11624 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11625 x_consider_frame_title (frame);
11626 }
11627 }
11628 #endif /* HAVE_WINDOW_SYSTEM */
11629
11630 /* Update the menu bar item lists, if appropriate. This has to be
11631 done before any actual redisplay or generation of display lines. */
11632
11633 if (all_windows)
11634 {
11635 Lisp_Object tail, frame;
11636 ptrdiff_t count = SPECPDL_INDEX ();
11637 /* 1 means that update_menu_bar has run its hooks
11638 so any further calls to update_menu_bar shouldn't do so again. */
11639 int menu_bar_hooks_run = 0;
11640
11641 record_unwind_save_match_data ();
11642
11643 FOR_EACH_FRAME (tail, frame)
11644 {
11645 struct frame *f = XFRAME (frame);
11646 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11647
11648 /* Ignore tooltip frame. */
11649 if (EQ (frame, tooltip_frame))
11650 continue;
11651
11652 if (some_windows
11653 && !f->redisplay
11654 && !w->redisplay
11655 && !XBUFFER (w->contents)->text->redisplay)
11656 continue;
11657
11658 /* If a window on this frame changed size, report that to
11659 the user and clear the size-change flag. */
11660 if (FRAME_WINDOW_SIZES_CHANGED (f))
11661 {
11662 Lisp_Object functions;
11663
11664 /* Clear flag first in case we get an error below. */
11665 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11666 functions = Vwindow_size_change_functions;
11667 GCPRO2 (tail, functions);
11668
11669 while (CONSP (functions))
11670 {
11671 if (!EQ (XCAR (functions), Qt))
11672 call1 (XCAR (functions), frame);
11673 functions = XCDR (functions);
11674 }
11675 UNGCPRO;
11676 }
11677
11678 GCPRO1 (tail);
11679 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11680 #ifdef HAVE_WINDOW_SYSTEM
11681 update_tool_bar (f, 0);
11682 #endif
11683 UNGCPRO;
11684 }
11685
11686 unbind_to (count, Qnil);
11687 }
11688 else
11689 {
11690 struct frame *sf = SELECTED_FRAME ();
11691 update_menu_bar (sf, 1, 0);
11692 #ifdef HAVE_WINDOW_SYSTEM
11693 update_tool_bar (sf, 1);
11694 #endif
11695 }
11696 }
11697
11698
11699 /* Update the menu bar item list for frame F. This has to be done
11700 before we start to fill in any display lines, because it can call
11701 eval.
11702
11703 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11704
11705 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11706 already ran the menu bar hooks for this redisplay, so there
11707 is no need to run them again. The return value is the
11708 updated value of this flag, to pass to the next call. */
11709
11710 static int
11711 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11712 {
11713 Lisp_Object window;
11714 register struct window *w;
11715
11716 /* If called recursively during a menu update, do nothing. This can
11717 happen when, for instance, an activate-menubar-hook causes a
11718 redisplay. */
11719 if (inhibit_menubar_update)
11720 return hooks_run;
11721
11722 window = FRAME_SELECTED_WINDOW (f);
11723 w = XWINDOW (window);
11724
11725 if (FRAME_WINDOW_P (f)
11726 ?
11727 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11728 || defined (HAVE_NS) || defined (USE_GTK)
11729 FRAME_EXTERNAL_MENU_BAR (f)
11730 #else
11731 FRAME_MENU_BAR_LINES (f) > 0
11732 #endif
11733 : FRAME_MENU_BAR_LINES (f) > 0)
11734 {
11735 /* If the user has switched buffers or windows, we need to
11736 recompute to reflect the new bindings. But we'll
11737 recompute when update_mode_lines is set too; that means
11738 that people can use force-mode-line-update to request
11739 that the menu bar be recomputed. The adverse effect on
11740 the rest of the redisplay algorithm is about the same as
11741 windows_or_buffers_changed anyway. */
11742 if (windows_or_buffers_changed
11743 /* This used to test w->update_mode_line, but we believe
11744 there is no need to recompute the menu in that case. */
11745 || update_mode_lines
11746 || window_buffer_changed (w))
11747 {
11748 struct buffer *prev = current_buffer;
11749 ptrdiff_t count = SPECPDL_INDEX ();
11750
11751 specbind (Qinhibit_menubar_update, Qt);
11752
11753 set_buffer_internal_1 (XBUFFER (w->contents));
11754 if (save_match_data)
11755 record_unwind_save_match_data ();
11756 if (NILP (Voverriding_local_map_menu_flag))
11757 {
11758 specbind (Qoverriding_terminal_local_map, Qnil);
11759 specbind (Qoverriding_local_map, Qnil);
11760 }
11761
11762 if (!hooks_run)
11763 {
11764 /* Run the Lucid hook. */
11765 safe_run_hooks (Qactivate_menubar_hook);
11766
11767 /* If it has changed current-menubar from previous value,
11768 really recompute the menu-bar from the value. */
11769 if (! NILP (Vlucid_menu_bar_dirty_flag))
11770 call0 (Qrecompute_lucid_menubar);
11771
11772 safe_run_hooks (Qmenu_bar_update_hook);
11773
11774 hooks_run = 1;
11775 }
11776
11777 XSETFRAME (Vmenu_updating_frame, f);
11778 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11779
11780 /* Redisplay the menu bar in case we changed it. */
11781 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11782 || defined (HAVE_NS) || defined (USE_GTK)
11783 if (FRAME_WINDOW_P (f))
11784 {
11785 #if defined (HAVE_NS)
11786 /* All frames on Mac OS share the same menubar. So only
11787 the selected frame should be allowed to set it. */
11788 if (f == SELECTED_FRAME ())
11789 #endif
11790 set_frame_menubar (f, 0, 0);
11791 }
11792 else
11793 /* On a terminal screen, the menu bar is an ordinary screen
11794 line, and this makes it get updated. */
11795 w->update_mode_line = 1;
11796 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11797 /* In the non-toolkit version, the menu bar is an ordinary screen
11798 line, and this makes it get updated. */
11799 w->update_mode_line = 1;
11800 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11801
11802 unbind_to (count, Qnil);
11803 set_buffer_internal_1 (prev);
11804 }
11805 }
11806
11807 return hooks_run;
11808 }
11809
11810 /***********************************************************************
11811 Tool-bars
11812 ***********************************************************************/
11813
11814 #ifdef HAVE_WINDOW_SYSTEM
11815
11816 /* Select `frame' temporarily without running all the code in
11817 do_switch_frame.
11818 FIXME: Maybe do_switch_frame should be trimmed down similarly
11819 when `norecord' is set. */
11820 static void
11821 fast_set_selected_frame (Lisp_Object frame)
11822 {
11823 if (!EQ (selected_frame, frame))
11824 {
11825 selected_frame = frame;
11826 selected_window = XFRAME (frame)->selected_window;
11827 }
11828 }
11829
11830 /* Update the tool-bar item list for frame F. This has to be done
11831 before we start to fill in any display lines. Called from
11832 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11833 and restore it here. */
11834
11835 static void
11836 update_tool_bar (struct frame *f, int save_match_data)
11837 {
11838 #if defined (USE_GTK) || defined (HAVE_NS)
11839 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11840 #else
11841 int do_update = (WINDOWP (f->tool_bar_window)
11842 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11843 #endif
11844
11845 if (do_update)
11846 {
11847 Lisp_Object window;
11848 struct window *w;
11849
11850 window = FRAME_SELECTED_WINDOW (f);
11851 w = XWINDOW (window);
11852
11853 /* If the user has switched buffers or windows, we need to
11854 recompute to reflect the new bindings. But we'll
11855 recompute when update_mode_lines is set too; that means
11856 that people can use force-mode-line-update to request
11857 that the menu bar be recomputed. The adverse effect on
11858 the rest of the redisplay algorithm is about the same as
11859 windows_or_buffers_changed anyway. */
11860 if (windows_or_buffers_changed
11861 || w->update_mode_line
11862 || update_mode_lines
11863 || window_buffer_changed (w))
11864 {
11865 struct buffer *prev = current_buffer;
11866 ptrdiff_t count = SPECPDL_INDEX ();
11867 Lisp_Object frame, new_tool_bar;
11868 int new_n_tool_bar;
11869 struct gcpro gcpro1;
11870
11871 /* Set current_buffer to the buffer of the selected
11872 window of the frame, so that we get the right local
11873 keymaps. */
11874 set_buffer_internal_1 (XBUFFER (w->contents));
11875
11876 /* Save match data, if we must. */
11877 if (save_match_data)
11878 record_unwind_save_match_data ();
11879
11880 /* Make sure that we don't accidentally use bogus keymaps. */
11881 if (NILP (Voverriding_local_map_menu_flag))
11882 {
11883 specbind (Qoverriding_terminal_local_map, Qnil);
11884 specbind (Qoverriding_local_map, Qnil);
11885 }
11886
11887 GCPRO1 (new_tool_bar);
11888
11889 /* We must temporarily set the selected frame to this frame
11890 before calling tool_bar_items, because the calculation of
11891 the tool-bar keymap uses the selected frame (see
11892 `tool-bar-make-keymap' in tool-bar.el). */
11893 eassert (EQ (selected_window,
11894 /* Since we only explicitly preserve selected_frame,
11895 check that selected_window would be redundant. */
11896 XFRAME (selected_frame)->selected_window));
11897 record_unwind_protect (fast_set_selected_frame, selected_frame);
11898 XSETFRAME (frame, f);
11899 fast_set_selected_frame (frame);
11900
11901 /* Build desired tool-bar items from keymaps. */
11902 new_tool_bar
11903 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11904 &new_n_tool_bar);
11905
11906 /* Redisplay the tool-bar if we changed it. */
11907 if (new_n_tool_bar != f->n_tool_bar_items
11908 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11909 {
11910 /* Redisplay that happens asynchronously due to an expose event
11911 may access f->tool_bar_items. Make sure we update both
11912 variables within BLOCK_INPUT so no such event interrupts. */
11913 block_input ();
11914 fset_tool_bar_items (f, new_tool_bar);
11915 f->n_tool_bar_items = new_n_tool_bar;
11916 w->update_mode_line = 1;
11917 unblock_input ();
11918 }
11919
11920 UNGCPRO;
11921
11922 unbind_to (count, Qnil);
11923 set_buffer_internal_1 (prev);
11924 }
11925 }
11926 }
11927
11928 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11929
11930 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11931 F's desired tool-bar contents. F->tool_bar_items must have
11932 been set up previously by calling prepare_menu_bars. */
11933
11934 static void
11935 build_desired_tool_bar_string (struct frame *f)
11936 {
11937 int i, size, size_needed;
11938 struct gcpro gcpro1, gcpro2;
11939 Lisp_Object image, plist;
11940
11941 image = plist = Qnil;
11942 GCPRO2 (image, plist);
11943
11944 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11945 Otherwise, make a new string. */
11946
11947 /* The size of the string we might be able to reuse. */
11948 size = (STRINGP (f->desired_tool_bar_string)
11949 ? SCHARS (f->desired_tool_bar_string)
11950 : 0);
11951
11952 /* We need one space in the string for each image. */
11953 size_needed = f->n_tool_bar_items;
11954
11955 /* Reuse f->desired_tool_bar_string, if possible. */
11956 if (size < size_needed || NILP (f->desired_tool_bar_string))
11957 fset_desired_tool_bar_string
11958 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11959 else
11960 {
11961 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11962 struct gcpro gcpro1;
11963 GCPRO1 (props);
11964 Fremove_text_properties (make_number (0), make_number (size),
11965 props, f->desired_tool_bar_string);
11966 UNGCPRO;
11967 }
11968
11969 /* Put a `display' property on the string for the images to display,
11970 put a `menu_item' property on tool-bar items with a value that
11971 is the index of the item in F's tool-bar item vector. */
11972 for (i = 0; i < f->n_tool_bar_items; ++i)
11973 {
11974 #define PROP(IDX) \
11975 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11976
11977 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11978 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11979 int hmargin, vmargin, relief, idx, end;
11980
11981 /* If image is a vector, choose the image according to the
11982 button state. */
11983 image = PROP (TOOL_BAR_ITEM_IMAGES);
11984 if (VECTORP (image))
11985 {
11986 if (enabled_p)
11987 idx = (selected_p
11988 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11989 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11990 else
11991 idx = (selected_p
11992 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11993 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11994
11995 eassert (ASIZE (image) >= idx);
11996 image = AREF (image, idx);
11997 }
11998 else
11999 idx = -1;
12000
12001 /* Ignore invalid image specifications. */
12002 if (!valid_image_p (image))
12003 continue;
12004
12005 /* Display the tool-bar button pressed, or depressed. */
12006 plist = Fcopy_sequence (XCDR (image));
12007
12008 /* Compute margin and relief to draw. */
12009 relief = (tool_bar_button_relief >= 0
12010 ? tool_bar_button_relief
12011 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12012 hmargin = vmargin = relief;
12013
12014 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12015 INT_MAX - max (hmargin, vmargin)))
12016 {
12017 hmargin += XFASTINT (Vtool_bar_button_margin);
12018 vmargin += XFASTINT (Vtool_bar_button_margin);
12019 }
12020 else if (CONSP (Vtool_bar_button_margin))
12021 {
12022 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12023 INT_MAX - hmargin))
12024 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12025
12026 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12027 INT_MAX - vmargin))
12028 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12029 }
12030
12031 if (auto_raise_tool_bar_buttons_p)
12032 {
12033 /* Add a `:relief' property to the image spec if the item is
12034 selected. */
12035 if (selected_p)
12036 {
12037 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12038 hmargin -= relief;
12039 vmargin -= relief;
12040 }
12041 }
12042 else
12043 {
12044 /* If image is selected, display it pressed, i.e. with a
12045 negative relief. If it's not selected, display it with a
12046 raised relief. */
12047 plist = Fplist_put (plist, QCrelief,
12048 (selected_p
12049 ? make_number (-relief)
12050 : make_number (relief)));
12051 hmargin -= relief;
12052 vmargin -= relief;
12053 }
12054
12055 /* Put a margin around the image. */
12056 if (hmargin || vmargin)
12057 {
12058 if (hmargin == vmargin)
12059 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12060 else
12061 plist = Fplist_put (plist, QCmargin,
12062 Fcons (make_number (hmargin),
12063 make_number (vmargin)));
12064 }
12065
12066 /* If button is not enabled, and we don't have special images
12067 for the disabled state, make the image appear disabled by
12068 applying an appropriate algorithm to it. */
12069 if (!enabled_p && idx < 0)
12070 plist = Fplist_put (plist, QCconversion, Qdisabled);
12071
12072 /* Put a `display' text property on the string for the image to
12073 display. Put a `menu-item' property on the string that gives
12074 the start of this item's properties in the tool-bar items
12075 vector. */
12076 image = Fcons (Qimage, plist);
12077 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12078 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12079 struct gcpro gcpro1;
12080 GCPRO1 (props);
12081
12082 /* Let the last image hide all remaining spaces in the tool bar
12083 string. The string can be longer than needed when we reuse a
12084 previous string. */
12085 if (i + 1 == f->n_tool_bar_items)
12086 end = SCHARS (f->desired_tool_bar_string);
12087 else
12088 end = i + 1;
12089 Fadd_text_properties (make_number (i), make_number (end),
12090 props, f->desired_tool_bar_string);
12091 UNGCPRO;
12092 #undef PROP
12093 }
12094
12095 UNGCPRO;
12096 }
12097
12098
12099 /* Display one line of the tool-bar of frame IT->f.
12100
12101 HEIGHT specifies the desired height of the tool-bar line.
12102 If the actual height of the glyph row is less than HEIGHT, the
12103 row's height is increased to HEIGHT, and the icons are centered
12104 vertically in the new height.
12105
12106 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12107 count a final empty row in case the tool-bar width exactly matches
12108 the window width.
12109 */
12110
12111 static void
12112 display_tool_bar_line (struct it *it, int height)
12113 {
12114 struct glyph_row *row = it->glyph_row;
12115 int max_x = it->last_visible_x;
12116 struct glyph *last;
12117
12118 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12119 clear_glyph_row (row);
12120 row->enabled_p = true;
12121 row->y = it->current_y;
12122
12123 /* Note that this isn't made use of if the face hasn't a box,
12124 so there's no need to check the face here. */
12125 it->start_of_box_run_p = 1;
12126
12127 while (it->current_x < max_x)
12128 {
12129 int x, n_glyphs_before, i, nglyphs;
12130 struct it it_before;
12131
12132 /* Get the next display element. */
12133 if (!get_next_display_element (it))
12134 {
12135 /* Don't count empty row if we are counting needed tool-bar lines. */
12136 if (height < 0 && !it->hpos)
12137 return;
12138 break;
12139 }
12140
12141 /* Produce glyphs. */
12142 n_glyphs_before = row->used[TEXT_AREA];
12143 it_before = *it;
12144
12145 PRODUCE_GLYPHS (it);
12146
12147 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12148 i = 0;
12149 x = it_before.current_x;
12150 while (i < nglyphs)
12151 {
12152 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12153
12154 if (x + glyph->pixel_width > max_x)
12155 {
12156 /* Glyph doesn't fit on line. Backtrack. */
12157 row->used[TEXT_AREA] = n_glyphs_before;
12158 *it = it_before;
12159 /* If this is the only glyph on this line, it will never fit on the
12160 tool-bar, so skip it. But ensure there is at least one glyph,
12161 so we don't accidentally disable the tool-bar. */
12162 if (n_glyphs_before == 0
12163 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12164 break;
12165 goto out;
12166 }
12167
12168 ++it->hpos;
12169 x += glyph->pixel_width;
12170 ++i;
12171 }
12172
12173 /* Stop at line end. */
12174 if (ITERATOR_AT_END_OF_LINE_P (it))
12175 break;
12176
12177 set_iterator_to_next (it, 1);
12178 }
12179
12180 out:;
12181
12182 row->displays_text_p = row->used[TEXT_AREA] != 0;
12183
12184 /* Use default face for the border below the tool bar.
12185
12186 FIXME: When auto-resize-tool-bars is grow-only, there is
12187 no additional border below the possibly empty tool-bar lines.
12188 So to make the extra empty lines look "normal", we have to
12189 use the tool-bar face for the border too. */
12190 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12191 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12192 it->face_id = DEFAULT_FACE_ID;
12193
12194 extend_face_to_end_of_line (it);
12195 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12196 last->right_box_line_p = 1;
12197 if (last == row->glyphs[TEXT_AREA])
12198 last->left_box_line_p = 1;
12199
12200 /* Make line the desired height and center it vertically. */
12201 if ((height -= it->max_ascent + it->max_descent) > 0)
12202 {
12203 /* Don't add more than one line height. */
12204 height %= FRAME_LINE_HEIGHT (it->f);
12205 it->max_ascent += height / 2;
12206 it->max_descent += (height + 1) / 2;
12207 }
12208
12209 compute_line_metrics (it);
12210
12211 /* If line is empty, make it occupy the rest of the tool-bar. */
12212 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12213 {
12214 row->height = row->phys_height = it->last_visible_y - row->y;
12215 row->visible_height = row->height;
12216 row->ascent = row->phys_ascent = 0;
12217 row->extra_line_spacing = 0;
12218 }
12219
12220 row->full_width_p = 1;
12221 row->continued_p = 0;
12222 row->truncated_on_left_p = 0;
12223 row->truncated_on_right_p = 0;
12224
12225 it->current_x = it->hpos = 0;
12226 it->current_y += row->height;
12227 ++it->vpos;
12228 ++it->glyph_row;
12229 }
12230
12231
12232 /* Value is the number of pixels needed to make all tool-bar items of
12233 frame F visible. The actual number of glyph rows needed is
12234 returned in *N_ROWS if non-NULL. */
12235 static int
12236 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12237 {
12238 struct window *w = XWINDOW (f->tool_bar_window);
12239 struct it it;
12240 /* tool_bar_height is called from redisplay_tool_bar after building
12241 the desired matrix, so use (unused) mode-line row as temporary row to
12242 avoid destroying the first tool-bar row. */
12243 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12244
12245 /* Initialize an iterator for iteration over
12246 F->desired_tool_bar_string in the tool-bar window of frame F. */
12247 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12248 temp_row->reversed_p = false;
12249 it.first_visible_x = 0;
12250 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12251 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12252 it.paragraph_embedding = L2R;
12253
12254 while (!ITERATOR_AT_END_P (&it))
12255 {
12256 clear_glyph_row (temp_row);
12257 it.glyph_row = temp_row;
12258 display_tool_bar_line (&it, -1);
12259 }
12260 clear_glyph_row (temp_row);
12261
12262 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12263 if (n_rows)
12264 *n_rows = it.vpos > 0 ? it.vpos : -1;
12265
12266 if (pixelwise)
12267 return it.current_y;
12268 else
12269 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12270 }
12271
12272 #endif /* !USE_GTK && !HAVE_NS */
12273
12274 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12275 0, 2, 0,
12276 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12277 If FRAME is nil or omitted, use the selected frame. Optional argument
12278 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12279 (Lisp_Object frame, Lisp_Object pixelwise)
12280 {
12281 int height = 0;
12282
12283 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12284 struct frame *f = decode_any_frame (frame);
12285
12286 if (WINDOWP (f->tool_bar_window)
12287 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12288 {
12289 update_tool_bar (f, 1);
12290 if (f->n_tool_bar_items)
12291 {
12292 build_desired_tool_bar_string (f);
12293 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12294 }
12295 }
12296 #endif
12297
12298 return make_number (height);
12299 }
12300
12301
12302 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12303 height should be changed. */
12304 static int
12305 redisplay_tool_bar (struct frame *f)
12306 {
12307 #if defined (USE_GTK) || defined (HAVE_NS)
12308
12309 if (FRAME_EXTERNAL_TOOL_BAR (f))
12310 update_frame_tool_bar (f);
12311 return 0;
12312
12313 #else /* !USE_GTK && !HAVE_NS */
12314
12315 struct window *w;
12316 struct it it;
12317 struct glyph_row *row;
12318
12319 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12320 do anything. This means you must start with tool-bar-lines
12321 non-zero to get the auto-sizing effect. Or in other words, you
12322 can turn off tool-bars by specifying tool-bar-lines zero. */
12323 if (!WINDOWP (f->tool_bar_window)
12324 || (w = XWINDOW (f->tool_bar_window),
12325 WINDOW_TOTAL_LINES (w) == 0))
12326 return 0;
12327
12328 /* Set up an iterator for the tool-bar window. */
12329 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12330 it.first_visible_x = 0;
12331 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12332 row = it.glyph_row;
12333 row->reversed_p = false;
12334
12335 /* Build a string that represents the contents of the tool-bar. */
12336 build_desired_tool_bar_string (f);
12337 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12338 /* FIXME: This should be controlled by a user option. But it
12339 doesn't make sense to have an R2L tool bar if the menu bar cannot
12340 be drawn also R2L, and making the menu bar R2L is tricky due
12341 toolkit-specific code that implements it. If an R2L tool bar is
12342 ever supported, display_tool_bar_line should also be augmented to
12343 call unproduce_glyphs like display_line and display_string
12344 do. */
12345 it.paragraph_embedding = L2R;
12346
12347 if (f->n_tool_bar_rows == 0)
12348 {
12349 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12350
12351 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12352 {
12353 x_change_tool_bar_height (f, new_height);
12354 /* Always do that now. */
12355 clear_glyph_matrix (w->desired_matrix);
12356 f->fonts_changed = 1;
12357 return 1;
12358 }
12359 }
12360
12361 /* Display as many lines as needed to display all tool-bar items. */
12362
12363 if (f->n_tool_bar_rows > 0)
12364 {
12365 int border, rows, height, extra;
12366
12367 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12368 border = XINT (Vtool_bar_border);
12369 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12370 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12371 else if (EQ (Vtool_bar_border, Qborder_width))
12372 border = f->border_width;
12373 else
12374 border = 0;
12375 if (border < 0)
12376 border = 0;
12377
12378 rows = f->n_tool_bar_rows;
12379 height = max (1, (it.last_visible_y - border) / rows);
12380 extra = it.last_visible_y - border - height * rows;
12381
12382 while (it.current_y < it.last_visible_y)
12383 {
12384 int h = 0;
12385 if (extra > 0 && rows-- > 0)
12386 {
12387 h = (extra + rows - 1) / rows;
12388 extra -= h;
12389 }
12390 display_tool_bar_line (&it, height + h);
12391 }
12392 }
12393 else
12394 {
12395 while (it.current_y < it.last_visible_y)
12396 display_tool_bar_line (&it, 0);
12397 }
12398
12399 /* It doesn't make much sense to try scrolling in the tool-bar
12400 window, so don't do it. */
12401 w->desired_matrix->no_scrolling_p = 1;
12402 w->must_be_updated_p = 1;
12403
12404 if (!NILP (Vauto_resize_tool_bars))
12405 {
12406 int change_height_p = 0;
12407
12408 /* If we couldn't display everything, change the tool-bar's
12409 height if there is room for more. */
12410 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12411 change_height_p = 1;
12412
12413 /* We subtract 1 because display_tool_bar_line advances the
12414 glyph_row pointer before returning to its caller. We want to
12415 examine the last glyph row produced by
12416 display_tool_bar_line. */
12417 row = it.glyph_row - 1;
12418
12419 /* If there are blank lines at the end, except for a partially
12420 visible blank line at the end that is smaller than
12421 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12422 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12423 && row->height >= FRAME_LINE_HEIGHT (f))
12424 change_height_p = 1;
12425
12426 /* If row displays tool-bar items, but is partially visible,
12427 change the tool-bar's height. */
12428 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12429 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12430 change_height_p = 1;
12431
12432 /* Resize windows as needed by changing the `tool-bar-lines'
12433 frame parameter. */
12434 if (change_height_p)
12435 {
12436 int nrows;
12437 int new_height = tool_bar_height (f, &nrows, 1);
12438
12439 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12440 && !f->minimize_tool_bar_window_p)
12441 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12442 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12443 f->minimize_tool_bar_window_p = 0;
12444
12445 if (change_height_p)
12446 {
12447 x_change_tool_bar_height (f, new_height);
12448 clear_glyph_matrix (w->desired_matrix);
12449 f->n_tool_bar_rows = nrows;
12450 f->fonts_changed = 1;
12451
12452 return 1;
12453 }
12454 }
12455 }
12456
12457 f->minimize_tool_bar_window_p = 0;
12458 return 0;
12459
12460 #endif /* USE_GTK || HAVE_NS */
12461 }
12462
12463 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12464
12465 /* Get information about the tool-bar item which is displayed in GLYPH
12466 on frame F. Return in *PROP_IDX the index where tool-bar item
12467 properties start in F->tool_bar_items. Value is zero if
12468 GLYPH doesn't display a tool-bar item. */
12469
12470 static int
12471 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12472 {
12473 Lisp_Object prop;
12474 int success_p;
12475 int charpos;
12476
12477 /* This function can be called asynchronously, which means we must
12478 exclude any possibility that Fget_text_property signals an
12479 error. */
12480 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12481 charpos = max (0, charpos);
12482
12483 /* Get the text property `menu-item' at pos. The value of that
12484 property is the start index of this item's properties in
12485 F->tool_bar_items. */
12486 prop = Fget_text_property (make_number (charpos),
12487 Qmenu_item, f->current_tool_bar_string);
12488 if (INTEGERP (prop))
12489 {
12490 *prop_idx = XINT (prop);
12491 success_p = 1;
12492 }
12493 else
12494 success_p = 0;
12495
12496 return success_p;
12497 }
12498
12499 \f
12500 /* Get information about the tool-bar item at position X/Y on frame F.
12501 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12502 the current matrix of the tool-bar window of F, or NULL if not
12503 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12504 item in F->tool_bar_items. Value is
12505
12506 -1 if X/Y is not on a tool-bar item
12507 0 if X/Y is on the same item that was highlighted before.
12508 1 otherwise. */
12509
12510 static int
12511 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12512 int *hpos, int *vpos, int *prop_idx)
12513 {
12514 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12515 struct window *w = XWINDOW (f->tool_bar_window);
12516 int area;
12517
12518 /* Find the glyph under X/Y. */
12519 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12520 if (*glyph == NULL)
12521 return -1;
12522
12523 /* Get the start of this tool-bar item's properties in
12524 f->tool_bar_items. */
12525 if (!tool_bar_item_info (f, *glyph, prop_idx))
12526 return -1;
12527
12528 /* Is mouse on the highlighted item? */
12529 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12530 && *vpos >= hlinfo->mouse_face_beg_row
12531 && *vpos <= hlinfo->mouse_face_end_row
12532 && (*vpos > hlinfo->mouse_face_beg_row
12533 || *hpos >= hlinfo->mouse_face_beg_col)
12534 && (*vpos < hlinfo->mouse_face_end_row
12535 || *hpos < hlinfo->mouse_face_end_col
12536 || hlinfo->mouse_face_past_end))
12537 return 0;
12538
12539 return 1;
12540 }
12541
12542
12543 /* EXPORT:
12544 Handle mouse button event on the tool-bar of frame F, at
12545 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12546 0 for button release. MODIFIERS is event modifiers for button
12547 release. */
12548
12549 void
12550 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12551 int modifiers)
12552 {
12553 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12554 struct window *w = XWINDOW (f->tool_bar_window);
12555 int hpos, vpos, prop_idx;
12556 struct glyph *glyph;
12557 Lisp_Object enabled_p;
12558 int ts;
12559
12560 /* If not on the highlighted tool-bar item, and mouse-highlight is
12561 non-nil, return. This is so we generate the tool-bar button
12562 click only when the mouse button is released on the same item as
12563 where it was pressed. However, when mouse-highlight is disabled,
12564 generate the click when the button is released regardless of the
12565 highlight, since tool-bar items are not highlighted in that
12566 case. */
12567 frame_to_window_pixel_xy (w, &x, &y);
12568 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12569 if (ts == -1
12570 || (ts != 0 && !NILP (Vmouse_highlight)))
12571 return;
12572
12573 /* When mouse-highlight is off, generate the click for the item
12574 where the button was pressed, disregarding where it was
12575 released. */
12576 if (NILP (Vmouse_highlight) && !down_p)
12577 prop_idx = f->last_tool_bar_item;
12578
12579 /* If item is disabled, do nothing. */
12580 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12581 if (NILP (enabled_p))
12582 return;
12583
12584 if (down_p)
12585 {
12586 /* Show item in pressed state. */
12587 if (!NILP (Vmouse_highlight))
12588 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12589 f->last_tool_bar_item = prop_idx;
12590 }
12591 else
12592 {
12593 Lisp_Object key, frame;
12594 struct input_event event;
12595 EVENT_INIT (event);
12596
12597 /* Show item in released state. */
12598 if (!NILP (Vmouse_highlight))
12599 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12600
12601 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12602
12603 XSETFRAME (frame, f);
12604 event.kind = TOOL_BAR_EVENT;
12605 event.frame_or_window = frame;
12606 event.arg = frame;
12607 kbd_buffer_store_event (&event);
12608
12609 event.kind = TOOL_BAR_EVENT;
12610 event.frame_or_window = frame;
12611 event.arg = key;
12612 event.modifiers = modifiers;
12613 kbd_buffer_store_event (&event);
12614 f->last_tool_bar_item = -1;
12615 }
12616 }
12617
12618
12619 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12620 tool-bar window-relative coordinates X/Y. Called from
12621 note_mouse_highlight. */
12622
12623 static void
12624 note_tool_bar_highlight (struct frame *f, int x, int y)
12625 {
12626 Lisp_Object window = f->tool_bar_window;
12627 struct window *w = XWINDOW (window);
12628 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12629 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12630 int hpos, vpos;
12631 struct glyph *glyph;
12632 struct glyph_row *row;
12633 int i;
12634 Lisp_Object enabled_p;
12635 int prop_idx;
12636 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12637 int mouse_down_p, rc;
12638
12639 /* Function note_mouse_highlight is called with negative X/Y
12640 values when mouse moves outside of the frame. */
12641 if (x <= 0 || y <= 0)
12642 {
12643 clear_mouse_face (hlinfo);
12644 return;
12645 }
12646
12647 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12648 if (rc < 0)
12649 {
12650 /* Not on tool-bar item. */
12651 clear_mouse_face (hlinfo);
12652 return;
12653 }
12654 else if (rc == 0)
12655 /* On same tool-bar item as before. */
12656 goto set_help_echo;
12657
12658 clear_mouse_face (hlinfo);
12659
12660 /* Mouse is down, but on different tool-bar item? */
12661 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12662 && f == dpyinfo->last_mouse_frame);
12663
12664 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12665 return;
12666
12667 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12668
12669 /* If tool-bar item is not enabled, don't highlight it. */
12670 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12671 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12672 {
12673 /* Compute the x-position of the glyph. In front and past the
12674 image is a space. We include this in the highlighted area. */
12675 row = MATRIX_ROW (w->current_matrix, vpos);
12676 for (i = x = 0; i < hpos; ++i)
12677 x += row->glyphs[TEXT_AREA][i].pixel_width;
12678
12679 /* Record this as the current active region. */
12680 hlinfo->mouse_face_beg_col = hpos;
12681 hlinfo->mouse_face_beg_row = vpos;
12682 hlinfo->mouse_face_beg_x = x;
12683 hlinfo->mouse_face_past_end = 0;
12684
12685 hlinfo->mouse_face_end_col = hpos + 1;
12686 hlinfo->mouse_face_end_row = vpos;
12687 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12688 hlinfo->mouse_face_window = window;
12689 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12690
12691 /* Display it as active. */
12692 show_mouse_face (hlinfo, draw);
12693 }
12694
12695 set_help_echo:
12696
12697 /* Set help_echo_string to a help string to display for this tool-bar item.
12698 XTread_socket does the rest. */
12699 help_echo_object = help_echo_window = Qnil;
12700 help_echo_pos = -1;
12701 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12702 if (NILP (help_echo_string))
12703 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12704 }
12705
12706 #endif /* !USE_GTK && !HAVE_NS */
12707
12708 #endif /* HAVE_WINDOW_SYSTEM */
12709
12710
12711 \f
12712 /************************************************************************
12713 Horizontal scrolling
12714 ************************************************************************/
12715
12716 static int hscroll_window_tree (Lisp_Object);
12717 static int hscroll_windows (Lisp_Object);
12718
12719 /* For all leaf windows in the window tree rooted at WINDOW, set their
12720 hscroll value so that PT is (i) visible in the window, and (ii) so
12721 that it is not within a certain margin at the window's left and
12722 right border. Value is non-zero if any window's hscroll has been
12723 changed. */
12724
12725 static int
12726 hscroll_window_tree (Lisp_Object window)
12727 {
12728 int hscrolled_p = 0;
12729 int hscroll_relative_p = FLOATP (Vhscroll_step);
12730 int hscroll_step_abs = 0;
12731 double hscroll_step_rel = 0;
12732
12733 if (hscroll_relative_p)
12734 {
12735 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12736 if (hscroll_step_rel < 0)
12737 {
12738 hscroll_relative_p = 0;
12739 hscroll_step_abs = 0;
12740 }
12741 }
12742 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12743 {
12744 hscroll_step_abs = XINT (Vhscroll_step);
12745 if (hscroll_step_abs < 0)
12746 hscroll_step_abs = 0;
12747 }
12748 else
12749 hscroll_step_abs = 0;
12750
12751 while (WINDOWP (window))
12752 {
12753 struct window *w = XWINDOW (window);
12754
12755 if (WINDOWP (w->contents))
12756 hscrolled_p |= hscroll_window_tree (w->contents);
12757 else if (w->cursor.vpos >= 0)
12758 {
12759 int h_margin;
12760 int text_area_width;
12761 struct glyph_row *cursor_row;
12762 struct glyph_row *bottom_row;
12763 int row_r2l_p;
12764
12765 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12766 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12767 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12768 else
12769 cursor_row = bottom_row - 1;
12770
12771 if (!cursor_row->enabled_p)
12772 {
12773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12774 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12775 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12776 else
12777 cursor_row = bottom_row - 1;
12778 }
12779 row_r2l_p = cursor_row->reversed_p;
12780
12781 text_area_width = window_box_width (w, TEXT_AREA);
12782
12783 /* Scroll when cursor is inside this scroll margin. */
12784 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12785
12786 /* If the position of this window's point has explicitly
12787 changed, no more suspend auto hscrolling. */
12788 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12789 w->suspend_auto_hscroll = 0;
12790
12791 /* Remember window point. */
12792 Fset_marker (w->old_pointm,
12793 ((w == XWINDOW (selected_window))
12794 ? make_number (BUF_PT (XBUFFER (w->contents)))
12795 : Fmarker_position (w->pointm)),
12796 w->contents);
12797
12798 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12799 && w->suspend_auto_hscroll == 0
12800 /* In some pathological cases, like restoring a window
12801 configuration into a frame that is much smaller than
12802 the one from which the configuration was saved, we
12803 get glyph rows whose start and end have zero buffer
12804 positions, which we cannot handle below. Just skip
12805 such windows. */
12806 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12807 /* For left-to-right rows, hscroll when cursor is either
12808 (i) inside the right hscroll margin, or (ii) if it is
12809 inside the left margin and the window is already
12810 hscrolled. */
12811 && ((!row_r2l_p
12812 && ((w->hscroll && w->cursor.x <= h_margin)
12813 || (cursor_row->enabled_p
12814 && cursor_row->truncated_on_right_p
12815 && (w->cursor.x >= text_area_width - h_margin))))
12816 /* For right-to-left rows, the logic is similar,
12817 except that rules for scrolling to left and right
12818 are reversed. E.g., if cursor.x <= h_margin, we
12819 need to hscroll "to the right" unconditionally,
12820 and that will scroll the screen to the left so as
12821 to reveal the next portion of the row. */
12822 || (row_r2l_p
12823 && ((cursor_row->enabled_p
12824 /* FIXME: It is confusing to set the
12825 truncated_on_right_p flag when R2L rows
12826 are actually truncated on the left. */
12827 && cursor_row->truncated_on_right_p
12828 && w->cursor.x <= h_margin)
12829 || (w->hscroll
12830 && (w->cursor.x >= text_area_width - h_margin))))))
12831 {
12832 struct it it;
12833 ptrdiff_t hscroll;
12834 struct buffer *saved_current_buffer;
12835 ptrdiff_t pt;
12836 int wanted_x;
12837
12838 /* Find point in a display of infinite width. */
12839 saved_current_buffer = current_buffer;
12840 current_buffer = XBUFFER (w->contents);
12841
12842 if (w == XWINDOW (selected_window))
12843 pt = PT;
12844 else
12845 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12846
12847 /* Move iterator to pt starting at cursor_row->start in
12848 a line with infinite width. */
12849 init_to_row_start (&it, w, cursor_row);
12850 it.last_visible_x = INFINITY;
12851 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12852 current_buffer = saved_current_buffer;
12853
12854 /* Position cursor in window. */
12855 if (!hscroll_relative_p && hscroll_step_abs == 0)
12856 hscroll = max (0, (it.current_x
12857 - (ITERATOR_AT_END_OF_LINE_P (&it)
12858 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12859 : (text_area_width / 2))))
12860 / FRAME_COLUMN_WIDTH (it.f);
12861 else if ((!row_r2l_p
12862 && w->cursor.x >= text_area_width - h_margin)
12863 || (row_r2l_p && w->cursor.x <= h_margin))
12864 {
12865 if (hscroll_relative_p)
12866 wanted_x = text_area_width * (1 - hscroll_step_rel)
12867 - h_margin;
12868 else
12869 wanted_x = text_area_width
12870 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12871 - h_margin;
12872 hscroll
12873 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12874 }
12875 else
12876 {
12877 if (hscroll_relative_p)
12878 wanted_x = text_area_width * hscroll_step_rel
12879 + h_margin;
12880 else
12881 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12882 + h_margin;
12883 hscroll
12884 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12885 }
12886 hscroll = max (hscroll, w->min_hscroll);
12887
12888 /* Don't prevent redisplay optimizations if hscroll
12889 hasn't changed, as it will unnecessarily slow down
12890 redisplay. */
12891 if (w->hscroll != hscroll)
12892 {
12893 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12894 w->hscroll = hscroll;
12895 hscrolled_p = 1;
12896 }
12897 }
12898 }
12899
12900 window = w->next;
12901 }
12902
12903 /* Value is non-zero if hscroll of any leaf window has been changed. */
12904 return hscrolled_p;
12905 }
12906
12907
12908 /* Set hscroll so that cursor is visible and not inside horizontal
12909 scroll margins for all windows in the tree rooted at WINDOW. See
12910 also hscroll_window_tree above. Value is non-zero if any window's
12911 hscroll has been changed. If it has, desired matrices on the frame
12912 of WINDOW are cleared. */
12913
12914 static int
12915 hscroll_windows (Lisp_Object window)
12916 {
12917 int hscrolled_p = hscroll_window_tree (window);
12918 if (hscrolled_p)
12919 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12920 return hscrolled_p;
12921 }
12922
12923
12924 \f
12925 /************************************************************************
12926 Redisplay
12927 ************************************************************************/
12928
12929 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12930 to a non-zero value. This is sometimes handy to have in a debugger
12931 session. */
12932
12933 #ifdef GLYPH_DEBUG
12934
12935 /* First and last unchanged row for try_window_id. */
12936
12937 static int debug_first_unchanged_at_end_vpos;
12938 static int debug_last_unchanged_at_beg_vpos;
12939
12940 /* Delta vpos and y. */
12941
12942 static int debug_dvpos, debug_dy;
12943
12944 /* Delta in characters and bytes for try_window_id. */
12945
12946 static ptrdiff_t debug_delta, debug_delta_bytes;
12947
12948 /* Values of window_end_pos and window_end_vpos at the end of
12949 try_window_id. */
12950
12951 static ptrdiff_t debug_end_vpos;
12952
12953 /* Append a string to W->desired_matrix->method. FMT is a printf
12954 format string. If trace_redisplay_p is true also printf the
12955 resulting string to stderr. */
12956
12957 static void debug_method_add (struct window *, char const *, ...)
12958 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12959
12960 static void
12961 debug_method_add (struct window *w, char const *fmt, ...)
12962 {
12963 void *ptr = w;
12964 char *method = w->desired_matrix->method;
12965 int len = strlen (method);
12966 int size = sizeof w->desired_matrix->method;
12967 int remaining = size - len - 1;
12968 va_list ap;
12969
12970 if (len && remaining)
12971 {
12972 method[len] = '|';
12973 --remaining, ++len;
12974 }
12975
12976 va_start (ap, fmt);
12977 vsnprintf (method + len, remaining + 1, fmt, ap);
12978 va_end (ap);
12979
12980 if (trace_redisplay_p)
12981 fprintf (stderr, "%p (%s): %s\n",
12982 ptr,
12983 ((BUFFERP (w->contents)
12984 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12985 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12986 : "no buffer"),
12987 method + len);
12988 }
12989
12990 #endif /* GLYPH_DEBUG */
12991
12992
12993 /* Value is non-zero if all changes in window W, which displays
12994 current_buffer, are in the text between START and END. START is a
12995 buffer position, END is given as a distance from Z. Used in
12996 redisplay_internal for display optimization. */
12997
12998 static int
12999 text_outside_line_unchanged_p (struct window *w,
13000 ptrdiff_t start, ptrdiff_t end)
13001 {
13002 int unchanged_p = 1;
13003
13004 /* If text or overlays have changed, see where. */
13005 if (window_outdated (w))
13006 {
13007 /* Gap in the line? */
13008 if (GPT < start || Z - GPT < end)
13009 unchanged_p = 0;
13010
13011 /* Changes start in front of the line, or end after it? */
13012 if (unchanged_p
13013 && (BEG_UNCHANGED < start - 1
13014 || END_UNCHANGED < end))
13015 unchanged_p = 0;
13016
13017 /* If selective display, can't optimize if changes start at the
13018 beginning of the line. */
13019 if (unchanged_p
13020 && INTEGERP (BVAR (current_buffer, selective_display))
13021 && XINT (BVAR (current_buffer, selective_display)) > 0
13022 && (BEG_UNCHANGED < start || GPT <= start))
13023 unchanged_p = 0;
13024
13025 /* If there are overlays at the start or end of the line, these
13026 may have overlay strings with newlines in them. A change at
13027 START, for instance, may actually concern the display of such
13028 overlay strings as well, and they are displayed on different
13029 lines. So, quickly rule out this case. (For the future, it
13030 might be desirable to implement something more telling than
13031 just BEG/END_UNCHANGED.) */
13032 if (unchanged_p)
13033 {
13034 if (BEG + BEG_UNCHANGED == start
13035 && overlay_touches_p (start))
13036 unchanged_p = 0;
13037 if (END_UNCHANGED == end
13038 && overlay_touches_p (Z - end))
13039 unchanged_p = 0;
13040 }
13041
13042 /* Under bidi reordering, adding or deleting a character in the
13043 beginning of a paragraph, before the first strong directional
13044 character, can change the base direction of the paragraph (unless
13045 the buffer specifies a fixed paragraph direction), which will
13046 require to redisplay the whole paragraph. It might be worthwhile
13047 to find the paragraph limits and widen the range of redisplayed
13048 lines to that, but for now just give up this optimization. */
13049 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13050 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13051 unchanged_p = 0;
13052 }
13053
13054 return unchanged_p;
13055 }
13056
13057
13058 /* Do a frame update, taking possible shortcuts into account. This is
13059 the main external entry point for redisplay.
13060
13061 If the last redisplay displayed an echo area message and that message
13062 is no longer requested, we clear the echo area or bring back the
13063 mini-buffer if that is in use. */
13064
13065 void
13066 redisplay (void)
13067 {
13068 redisplay_internal ();
13069 }
13070
13071
13072 static Lisp_Object
13073 overlay_arrow_string_or_property (Lisp_Object var)
13074 {
13075 Lisp_Object val;
13076
13077 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13078 return val;
13079
13080 return Voverlay_arrow_string;
13081 }
13082
13083 /* Return 1 if there are any overlay-arrows in current_buffer. */
13084 static int
13085 overlay_arrow_in_current_buffer_p (void)
13086 {
13087 Lisp_Object vlist;
13088
13089 for (vlist = Voverlay_arrow_variable_list;
13090 CONSP (vlist);
13091 vlist = XCDR (vlist))
13092 {
13093 Lisp_Object var = XCAR (vlist);
13094 Lisp_Object val;
13095
13096 if (!SYMBOLP (var))
13097 continue;
13098 val = find_symbol_value (var);
13099 if (MARKERP (val)
13100 && current_buffer == XMARKER (val)->buffer)
13101 return 1;
13102 }
13103 return 0;
13104 }
13105
13106
13107 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13108 has changed. */
13109
13110 static int
13111 overlay_arrows_changed_p (void)
13112 {
13113 Lisp_Object vlist;
13114
13115 for (vlist = Voverlay_arrow_variable_list;
13116 CONSP (vlist);
13117 vlist = XCDR (vlist))
13118 {
13119 Lisp_Object var = XCAR (vlist);
13120 Lisp_Object val, pstr;
13121
13122 if (!SYMBOLP (var))
13123 continue;
13124 val = find_symbol_value (var);
13125 if (!MARKERP (val))
13126 continue;
13127 if (! EQ (COERCE_MARKER (val),
13128 Fget (var, Qlast_arrow_position))
13129 || ! (pstr = overlay_arrow_string_or_property (var),
13130 EQ (pstr, Fget (var, Qlast_arrow_string))))
13131 return 1;
13132 }
13133 return 0;
13134 }
13135
13136 /* Mark overlay arrows to be updated on next redisplay. */
13137
13138 static void
13139 update_overlay_arrows (int up_to_date)
13140 {
13141 Lisp_Object vlist;
13142
13143 for (vlist = Voverlay_arrow_variable_list;
13144 CONSP (vlist);
13145 vlist = XCDR (vlist))
13146 {
13147 Lisp_Object var = XCAR (vlist);
13148
13149 if (!SYMBOLP (var))
13150 continue;
13151
13152 if (up_to_date > 0)
13153 {
13154 Lisp_Object val = find_symbol_value (var);
13155 Fput (var, Qlast_arrow_position,
13156 COERCE_MARKER (val));
13157 Fput (var, Qlast_arrow_string,
13158 overlay_arrow_string_or_property (var));
13159 }
13160 else if (up_to_date < 0
13161 || !NILP (Fget (var, Qlast_arrow_position)))
13162 {
13163 Fput (var, Qlast_arrow_position, Qt);
13164 Fput (var, Qlast_arrow_string, Qt);
13165 }
13166 }
13167 }
13168
13169
13170 /* Return overlay arrow string to display at row.
13171 Return integer (bitmap number) for arrow bitmap in left fringe.
13172 Return nil if no overlay arrow. */
13173
13174 static Lisp_Object
13175 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
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 Lisp_Object val;
13185
13186 if (!SYMBOLP (var))
13187 continue;
13188
13189 val = find_symbol_value (var);
13190
13191 if (MARKERP (val)
13192 && current_buffer == XMARKER (val)->buffer
13193 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13194 {
13195 if (FRAME_WINDOW_P (it->f)
13196 /* FIXME: if ROW->reversed_p is set, this should test
13197 the right fringe, not the left one. */
13198 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13199 {
13200 #ifdef HAVE_WINDOW_SYSTEM
13201 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13202 {
13203 int fringe_bitmap;
13204 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13205 return make_number (fringe_bitmap);
13206 }
13207 #endif
13208 return make_number (-1); /* Use default arrow bitmap. */
13209 }
13210 return overlay_arrow_string_or_property (var);
13211 }
13212 }
13213
13214 return Qnil;
13215 }
13216
13217 /* Return 1 if point moved out of or into a composition. Otherwise
13218 return 0. PREV_BUF and PREV_PT are the last point buffer and
13219 position. BUF and PT are the current point buffer and position. */
13220
13221 static int
13222 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13223 struct buffer *buf, ptrdiff_t pt)
13224 {
13225 ptrdiff_t start, end;
13226 Lisp_Object prop;
13227 Lisp_Object buffer;
13228
13229 XSETBUFFER (buffer, buf);
13230 /* Check a composition at the last point if point moved within the
13231 same buffer. */
13232 if (prev_buf == buf)
13233 {
13234 if (prev_pt == pt)
13235 /* Point didn't move. */
13236 return 0;
13237
13238 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13239 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13240 && composition_valid_p (start, end, prop)
13241 && start < prev_pt && end > prev_pt)
13242 /* The last point was within the composition. Return 1 iff
13243 point moved out of the composition. */
13244 return (pt <= start || pt >= end);
13245 }
13246
13247 /* Check a composition at the current point. */
13248 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13249 && find_composition (pt, -1, &start, &end, &prop, buffer)
13250 && composition_valid_p (start, end, prop)
13251 && start < pt && end > pt);
13252 }
13253
13254 /* Reconsider the clip changes of buffer which is displayed in W. */
13255
13256 static void
13257 reconsider_clip_changes (struct window *w)
13258 {
13259 struct buffer *b = XBUFFER (w->contents);
13260
13261 if (b->clip_changed
13262 && w->window_end_valid
13263 && w->current_matrix->buffer == b
13264 && w->current_matrix->zv == BUF_ZV (b)
13265 && w->current_matrix->begv == BUF_BEGV (b))
13266 b->clip_changed = 0;
13267
13268 /* If display wasn't paused, and W is not a tool bar window, see if
13269 point has been moved into or out of a composition. In that case,
13270 we set b->clip_changed to 1 to force updating the screen. If
13271 b->clip_changed has already been set to 1, we can skip this
13272 check. */
13273 if (!b->clip_changed && w->window_end_valid)
13274 {
13275 ptrdiff_t pt = (w == XWINDOW (selected_window)
13276 ? PT : marker_position (w->pointm));
13277
13278 if ((w->current_matrix->buffer != b || pt != w->last_point)
13279 && check_point_in_composition (w->current_matrix->buffer,
13280 w->last_point, b, pt))
13281 b->clip_changed = 1;
13282 }
13283 }
13284
13285 static void
13286 propagate_buffer_redisplay (void)
13287 { /* Resetting b->text->redisplay is problematic!
13288 We can't just reset it in the case that some window that displays
13289 it has not been redisplayed; and such a window can stay
13290 unredisplayed for a long time if it's currently invisible.
13291 But we do want to reset it at the end of redisplay otherwise
13292 its displayed windows will keep being redisplayed over and over
13293 again.
13294 So we copy all b->text->redisplay flags up to their windows here,
13295 such that mark_window_display_accurate can safely reset
13296 b->text->redisplay. */
13297 Lisp_Object ws = window_list ();
13298 for (; CONSP (ws); ws = XCDR (ws))
13299 {
13300 struct window *thisw = XWINDOW (XCAR (ws));
13301 struct buffer *thisb = XBUFFER (thisw->contents);
13302 if (thisb->text->redisplay)
13303 thisw->redisplay = true;
13304 }
13305 }
13306
13307 #define STOP_POLLING \
13308 do { if (! polling_stopped_here) stop_polling (); \
13309 polling_stopped_here = 1; } while (0)
13310
13311 #define RESUME_POLLING \
13312 do { if (polling_stopped_here) start_polling (); \
13313 polling_stopped_here = 0; } while (0)
13314
13315
13316 /* Perhaps in the future avoid recentering windows if it
13317 is not necessary; currently that causes some problems. */
13318
13319 static void
13320 redisplay_internal (void)
13321 {
13322 struct window *w = XWINDOW (selected_window);
13323 struct window *sw;
13324 struct frame *fr;
13325 bool pending;
13326 bool must_finish = 0, match_p;
13327 struct text_pos tlbufpos, tlendpos;
13328 int number_of_visible_frames;
13329 ptrdiff_t count;
13330 struct frame *sf;
13331 int polling_stopped_here = 0;
13332 Lisp_Object tail, frame;
13333
13334 /* True means redisplay has to consider all windows on all
13335 frames. False, only selected_window is considered. */
13336 bool consider_all_windows_p;
13337
13338 /* True means redisplay has to redisplay the miniwindow. */
13339 bool update_miniwindow_p = false;
13340
13341 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13342
13343 /* No redisplay if running in batch mode or frame is not yet fully
13344 initialized, or redisplay is explicitly turned off by setting
13345 Vinhibit_redisplay. */
13346 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13347 || !NILP (Vinhibit_redisplay))
13348 return;
13349
13350 /* Don't examine these until after testing Vinhibit_redisplay.
13351 When Emacs is shutting down, perhaps because its connection to
13352 X has dropped, we should not look at them at all. */
13353 fr = XFRAME (w->frame);
13354 sf = SELECTED_FRAME ();
13355
13356 if (!fr->glyphs_initialized_p)
13357 return;
13358
13359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13360 if (popup_activated ())
13361 return;
13362 #endif
13363
13364 /* I don't think this happens but let's be paranoid. */
13365 if (redisplaying_p)
13366 return;
13367
13368 /* Record a function that clears redisplaying_p
13369 when we leave this function. */
13370 count = SPECPDL_INDEX ();
13371 record_unwind_protect_void (unwind_redisplay);
13372 redisplaying_p = 1;
13373 specbind (Qinhibit_free_realized_faces, Qnil);
13374
13375 /* Record this function, so it appears on the profiler's backtraces. */
13376 record_in_backtrace (Qredisplay_internal, 0, 0);
13377
13378 FOR_EACH_FRAME (tail, frame)
13379 XFRAME (frame)->already_hscrolled_p = 0;
13380
13381 retry:
13382 /* Remember the currently selected window. */
13383 sw = w;
13384
13385 pending = false;
13386 last_escape_glyph_frame = NULL;
13387 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13388 last_glyphless_glyph_frame = NULL;
13389 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13390
13391 /* If face_change_count is non-zero, init_iterator will free all
13392 realized faces, which includes the faces referenced from current
13393 matrices. So, we can't reuse current matrices in this case. */
13394 if (face_change_count)
13395 windows_or_buffers_changed = 47;
13396
13397 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13398 && FRAME_TTY (sf)->previous_frame != sf)
13399 {
13400 /* Since frames on a single ASCII terminal share the same
13401 display area, displaying a different frame means redisplay
13402 the whole thing. */
13403 SET_FRAME_GARBAGED (sf);
13404 #ifndef DOS_NT
13405 set_tty_color_mode (FRAME_TTY (sf), sf);
13406 #endif
13407 FRAME_TTY (sf)->previous_frame = sf;
13408 }
13409
13410 /* Set the visible flags for all frames. Do this before checking for
13411 resized or garbaged frames; they want to know if their frames are
13412 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13413 number_of_visible_frames = 0;
13414
13415 FOR_EACH_FRAME (tail, frame)
13416 {
13417 struct frame *f = XFRAME (frame);
13418
13419 if (FRAME_VISIBLE_P (f))
13420 {
13421 ++number_of_visible_frames;
13422 /* Adjust matrices for visible frames only. */
13423 if (f->fonts_changed)
13424 {
13425 adjust_frame_glyphs (f);
13426 f->fonts_changed = 0;
13427 }
13428 /* If cursor type has been changed on the frame
13429 other than selected, consider all frames. */
13430 if (f != sf && f->cursor_type_changed)
13431 update_mode_lines = 31;
13432 }
13433 clear_desired_matrices (f);
13434 }
13435
13436 /* Notice any pending interrupt request to change frame size. */
13437 do_pending_window_change (true);
13438
13439 /* do_pending_window_change could change the selected_window due to
13440 frame resizing which makes the selected window too small. */
13441 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13442 sw = w;
13443
13444 /* Clear frames marked as garbaged. */
13445 clear_garbaged_frames ();
13446
13447 /* Build menubar and tool-bar items. */
13448 if (NILP (Vmemory_full))
13449 prepare_menu_bars ();
13450
13451 reconsider_clip_changes (w);
13452
13453 /* In most cases selected window displays current buffer. */
13454 match_p = XBUFFER (w->contents) == current_buffer;
13455 if (match_p)
13456 {
13457 /* Detect case that we need to write or remove a star in the mode line. */
13458 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13459 w->update_mode_line = 1;
13460
13461 if (mode_line_update_needed (w))
13462 w->update_mode_line = 1;
13463
13464 /* If reconsider_clip_changes above decided that the narrowing
13465 in the current buffer changed, make sure all other windows
13466 showing that buffer will be redisplayed. */
13467 if (current_buffer->clip_changed)
13468 bset_update_mode_line (current_buffer);
13469 }
13470
13471 /* Normally the message* functions will have already displayed and
13472 updated the echo area, but the frame may have been trashed, or
13473 the update may have been preempted, so display the echo area
13474 again here. Checking message_cleared_p captures the case that
13475 the echo area should be cleared. */
13476 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13477 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13478 || (message_cleared_p
13479 && minibuf_level == 0
13480 /* If the mini-window is currently selected, this means the
13481 echo-area doesn't show through. */
13482 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13483 {
13484 int window_height_changed_p = echo_area_display (false);
13485
13486 if (message_cleared_p)
13487 update_miniwindow_p = true;
13488
13489 must_finish = 1;
13490
13491 /* If we don't display the current message, don't clear the
13492 message_cleared_p flag, because, if we did, we wouldn't clear
13493 the echo area in the next redisplay which doesn't preserve
13494 the echo area. */
13495 if (!display_last_displayed_message_p)
13496 message_cleared_p = 0;
13497
13498 if (window_height_changed_p)
13499 {
13500 windows_or_buffers_changed = 50;
13501
13502 /* If window configuration was changed, frames may have been
13503 marked garbaged. Clear them or we will experience
13504 surprises wrt scrolling. */
13505 clear_garbaged_frames ();
13506 }
13507 }
13508 else if (EQ (selected_window, minibuf_window)
13509 && (current_buffer->clip_changed || window_outdated (w))
13510 && resize_mini_window (w, 0))
13511 {
13512 /* Resized active mini-window to fit the size of what it is
13513 showing if its contents might have changed. */
13514 must_finish = 1;
13515
13516 /* If window configuration was changed, frames may have been
13517 marked garbaged. Clear them or we will experience
13518 surprises wrt scrolling. */
13519 clear_garbaged_frames ();
13520 }
13521
13522 if (windows_or_buffers_changed && !update_mode_lines)
13523 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13524 only the windows's contents needs to be refreshed, or whether the
13525 mode-lines also need a refresh. */
13526 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13527 ? REDISPLAY_SOME : 32);
13528
13529 /* If specs for an arrow have changed, do thorough redisplay
13530 to ensure we remove any arrow that should no longer exist. */
13531 if (overlay_arrows_changed_p ())
13532 /* Apparently, this is the only case where we update other windows,
13533 without updating other mode-lines. */
13534 windows_or_buffers_changed = 49;
13535
13536 consider_all_windows_p = (update_mode_lines
13537 || windows_or_buffers_changed);
13538
13539 #define AINC(a,i) \
13540 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13541 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13542
13543 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13544 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13545
13546 /* Optimize the case that only the line containing the cursor in the
13547 selected window has changed. Variables starting with this_ are
13548 set in display_line and record information about the line
13549 containing the cursor. */
13550 tlbufpos = this_line_start_pos;
13551 tlendpos = this_line_end_pos;
13552 if (!consider_all_windows_p
13553 && CHARPOS (tlbufpos) > 0
13554 && !w->update_mode_line
13555 && !current_buffer->clip_changed
13556 && !current_buffer->prevent_redisplay_optimizations_p
13557 && FRAME_VISIBLE_P (XFRAME (w->frame))
13558 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13559 && !XFRAME (w->frame)->cursor_type_changed
13560 /* Make sure recorded data applies to current buffer, etc. */
13561 && this_line_buffer == current_buffer
13562 && match_p
13563 && !w->force_start
13564 && !w->optional_new_start
13565 /* Point must be on the line that we have info recorded about. */
13566 && PT >= CHARPOS (tlbufpos)
13567 && PT <= Z - CHARPOS (tlendpos)
13568 /* All text outside that line, including its final newline,
13569 must be unchanged. */
13570 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13571 CHARPOS (tlendpos)))
13572 {
13573 if (CHARPOS (tlbufpos) > BEGV
13574 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13575 && (CHARPOS (tlbufpos) == ZV
13576 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13577 /* Former continuation line has disappeared by becoming empty. */
13578 goto cancel;
13579 else if (window_outdated (w) || MINI_WINDOW_P (w))
13580 {
13581 /* We have to handle the case of continuation around a
13582 wide-column character (see the comment in indent.c around
13583 line 1340).
13584
13585 For instance, in the following case:
13586
13587 -------- Insert --------
13588 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13589 J_I_ ==> J_I_ `^^' are cursors.
13590 ^^ ^^
13591 -------- --------
13592
13593 As we have to redraw the line above, we cannot use this
13594 optimization. */
13595
13596 struct it it;
13597 int line_height_before = this_line_pixel_height;
13598
13599 /* Note that start_display will handle the case that the
13600 line starting at tlbufpos is a continuation line. */
13601 start_display (&it, w, tlbufpos);
13602
13603 /* Implementation note: It this still necessary? */
13604 if (it.current_x != this_line_start_x)
13605 goto cancel;
13606
13607 TRACE ((stderr, "trying display optimization 1\n"));
13608 w->cursor.vpos = -1;
13609 overlay_arrow_seen = 0;
13610 it.vpos = this_line_vpos;
13611 it.current_y = this_line_y;
13612 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13613 display_line (&it);
13614
13615 /* If line contains point, is not continued,
13616 and ends at same distance from eob as before, we win. */
13617 if (w->cursor.vpos >= 0
13618 /* Line is not continued, otherwise this_line_start_pos
13619 would have been set to 0 in display_line. */
13620 && CHARPOS (this_line_start_pos)
13621 /* Line ends as before. */
13622 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13623 /* Line has same height as before. Otherwise other lines
13624 would have to be shifted up or down. */
13625 && this_line_pixel_height == line_height_before)
13626 {
13627 /* If this is not the window's last line, we must adjust
13628 the charstarts of the lines below. */
13629 if (it.current_y < it.last_visible_y)
13630 {
13631 struct glyph_row *row
13632 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13633 ptrdiff_t delta, delta_bytes;
13634
13635 /* We used to distinguish between two cases here,
13636 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13637 when the line ends in a newline or the end of the
13638 buffer's accessible portion. But both cases did
13639 the same, so they were collapsed. */
13640 delta = (Z
13641 - CHARPOS (tlendpos)
13642 - MATRIX_ROW_START_CHARPOS (row));
13643 delta_bytes = (Z_BYTE
13644 - BYTEPOS (tlendpos)
13645 - MATRIX_ROW_START_BYTEPOS (row));
13646
13647 increment_matrix_positions (w->current_matrix,
13648 this_line_vpos + 1,
13649 w->current_matrix->nrows,
13650 delta, delta_bytes);
13651 }
13652
13653 /* If this row displays text now but previously didn't,
13654 or vice versa, w->window_end_vpos may have to be
13655 adjusted. */
13656 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13657 {
13658 if (w->window_end_vpos < this_line_vpos)
13659 w->window_end_vpos = this_line_vpos;
13660 }
13661 else if (w->window_end_vpos == this_line_vpos
13662 && this_line_vpos > 0)
13663 w->window_end_vpos = this_line_vpos - 1;
13664 w->window_end_valid = 0;
13665
13666 /* Update hint: No need to try to scroll in update_window. */
13667 w->desired_matrix->no_scrolling_p = 1;
13668
13669 #ifdef GLYPH_DEBUG
13670 *w->desired_matrix->method = 0;
13671 debug_method_add (w, "optimization 1");
13672 #endif
13673 #ifdef HAVE_WINDOW_SYSTEM
13674 update_window_fringes (w, 0);
13675 #endif
13676 goto update;
13677 }
13678 else
13679 goto cancel;
13680 }
13681 else if (/* Cursor position hasn't changed. */
13682 PT == w->last_point
13683 /* Make sure the cursor was last displayed
13684 in this window. Otherwise we have to reposition it. */
13685
13686 /* PXW: Must be converted to pixels, probably. */
13687 && 0 <= w->cursor.vpos
13688 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13689 {
13690 if (!must_finish)
13691 {
13692 do_pending_window_change (true);
13693 /* If selected_window changed, redisplay again. */
13694 if (WINDOWP (selected_window)
13695 && (w = XWINDOW (selected_window)) != sw)
13696 goto retry;
13697
13698 /* We used to always goto end_of_redisplay here, but this
13699 isn't enough if we have a blinking cursor. */
13700 if (w->cursor_off_p == w->last_cursor_off_p)
13701 goto end_of_redisplay;
13702 }
13703 goto update;
13704 }
13705 /* If highlighting the region, or if the cursor is in the echo area,
13706 then we can't just move the cursor. */
13707 else if (NILP (Vshow_trailing_whitespace)
13708 && !cursor_in_echo_area)
13709 {
13710 struct it it;
13711 struct glyph_row *row;
13712
13713 /* Skip from tlbufpos to PT and see where it is. Note that
13714 PT may be in invisible text. If so, we will end at the
13715 next visible position. */
13716 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13717 NULL, DEFAULT_FACE_ID);
13718 it.current_x = this_line_start_x;
13719 it.current_y = this_line_y;
13720 it.vpos = this_line_vpos;
13721
13722 /* The call to move_it_to stops in front of PT, but
13723 moves over before-strings. */
13724 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13725
13726 if (it.vpos == this_line_vpos
13727 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13728 row->enabled_p))
13729 {
13730 eassert (this_line_vpos == it.vpos);
13731 eassert (this_line_y == it.current_y);
13732 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13733 #ifdef GLYPH_DEBUG
13734 *w->desired_matrix->method = 0;
13735 debug_method_add (w, "optimization 3");
13736 #endif
13737 goto update;
13738 }
13739 else
13740 goto cancel;
13741 }
13742
13743 cancel:
13744 /* Text changed drastically or point moved off of line. */
13745 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13746 }
13747
13748 CHARPOS (this_line_start_pos) = 0;
13749 ++clear_face_cache_count;
13750 #ifdef HAVE_WINDOW_SYSTEM
13751 ++clear_image_cache_count;
13752 #endif
13753
13754 /* Build desired matrices, and update the display. If
13755 consider_all_windows_p is non-zero, do it for all windows on all
13756 frames. Otherwise do it for selected_window, only. */
13757
13758 if (consider_all_windows_p)
13759 {
13760 FOR_EACH_FRAME (tail, frame)
13761 XFRAME (frame)->updated_p = 0;
13762
13763 propagate_buffer_redisplay ();
13764
13765 FOR_EACH_FRAME (tail, frame)
13766 {
13767 struct frame *f = XFRAME (frame);
13768
13769 /* We don't have to do anything for unselected terminal
13770 frames. */
13771 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13772 && !EQ (FRAME_TTY (f)->top_frame, frame))
13773 continue;
13774
13775 retry_frame:
13776
13777 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13778 {
13779 bool gcscrollbars
13780 /* Only GC scrollbars when we redisplay the whole frame. */
13781 = f->redisplay || !REDISPLAY_SOME_P ();
13782 /* Mark all the scroll bars to be removed; we'll redeem
13783 the ones we want when we redisplay their windows. */
13784 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13785 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13786
13787 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13788 redisplay_windows (FRAME_ROOT_WINDOW (f));
13789 /* Remember that the invisible frames need to be redisplayed next
13790 time they're visible. */
13791 else if (!REDISPLAY_SOME_P ())
13792 f->redisplay = true;
13793
13794 /* The X error handler may have deleted that frame. */
13795 if (!FRAME_LIVE_P (f))
13796 continue;
13797
13798 /* Any scroll bars which redisplay_windows should have
13799 nuked should now go away. */
13800 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13801 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13802
13803 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13804 {
13805 /* If fonts changed on visible frame, display again. */
13806 if (f->fonts_changed)
13807 {
13808 adjust_frame_glyphs (f);
13809 f->fonts_changed = false;
13810 goto retry_frame;
13811 }
13812
13813 /* See if we have to hscroll. */
13814 if (!f->already_hscrolled_p)
13815 {
13816 f->already_hscrolled_p = true;
13817 if (hscroll_windows (f->root_window))
13818 goto retry_frame;
13819 }
13820
13821 /* Prevent various kinds of signals during display
13822 update. stdio is not robust about handling
13823 signals, which can cause an apparent I/O error. */
13824 if (interrupt_input)
13825 unrequest_sigio ();
13826 STOP_POLLING;
13827
13828 pending |= update_frame (f, false, false);
13829 f->cursor_type_changed = false;
13830 f->updated_p = true;
13831 }
13832 }
13833 }
13834
13835 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13836
13837 if (!pending)
13838 {
13839 /* Do the mark_window_display_accurate after all windows have
13840 been redisplayed because this call resets flags in buffers
13841 which are needed for proper redisplay. */
13842 FOR_EACH_FRAME (tail, frame)
13843 {
13844 struct frame *f = XFRAME (frame);
13845 if (f->updated_p)
13846 {
13847 f->redisplay = false;
13848 mark_window_display_accurate (f->root_window, 1);
13849 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13850 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13851 }
13852 }
13853 }
13854 }
13855 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13856 {
13857 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13858 struct frame *mini_frame;
13859
13860 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13861 /* Use list_of_error, not Qerror, so that
13862 we catch only errors and don't run the debugger. */
13863 internal_condition_case_1 (redisplay_window_1, selected_window,
13864 list_of_error,
13865 redisplay_window_error);
13866 if (update_miniwindow_p)
13867 internal_condition_case_1 (redisplay_window_1, mini_window,
13868 list_of_error,
13869 redisplay_window_error);
13870
13871 /* Compare desired and current matrices, perform output. */
13872
13873 update:
13874 /* If fonts changed, display again. */
13875 if (sf->fonts_changed)
13876 goto retry;
13877
13878 /* Prevent various kinds of signals during display update.
13879 stdio is not robust about handling signals,
13880 which can cause an apparent I/O error. */
13881 if (interrupt_input)
13882 unrequest_sigio ();
13883 STOP_POLLING;
13884
13885 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13886 {
13887 if (hscroll_windows (selected_window))
13888 goto retry;
13889
13890 XWINDOW (selected_window)->must_be_updated_p = true;
13891 pending = update_frame (sf, false, false);
13892 sf->cursor_type_changed = false;
13893 }
13894
13895 /* We may have called echo_area_display at the top of this
13896 function. If the echo area is on another frame, that may
13897 have put text on a frame other than the selected one, so the
13898 above call to update_frame would not have caught it. Catch
13899 it here. */
13900 mini_window = FRAME_MINIBUF_WINDOW (sf);
13901 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13902
13903 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13904 {
13905 XWINDOW (mini_window)->must_be_updated_p = true;
13906 pending |= update_frame (mini_frame, false, false);
13907 mini_frame->cursor_type_changed = false;
13908 if (!pending && hscroll_windows (mini_window))
13909 goto retry;
13910 }
13911 }
13912
13913 /* If display was paused because of pending input, make sure we do a
13914 thorough update the next time. */
13915 if (pending)
13916 {
13917 /* Prevent the optimization at the beginning of
13918 redisplay_internal that tries a single-line update of the
13919 line containing the cursor in the selected window. */
13920 CHARPOS (this_line_start_pos) = 0;
13921
13922 /* Let the overlay arrow be updated the next time. */
13923 update_overlay_arrows (0);
13924
13925 /* If we pause after scrolling, some rows in the current
13926 matrices of some windows are not valid. */
13927 if (!WINDOW_FULL_WIDTH_P (w)
13928 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13929 update_mode_lines = 36;
13930 }
13931 else
13932 {
13933 if (!consider_all_windows_p)
13934 {
13935 /* This has already been done above if
13936 consider_all_windows_p is set. */
13937 if (XBUFFER (w->contents)->text->redisplay
13938 && buffer_window_count (XBUFFER (w->contents)) > 1)
13939 /* This can happen if b->text->redisplay was set during
13940 jit-lock. */
13941 propagate_buffer_redisplay ();
13942 mark_window_display_accurate_1 (w, 1);
13943
13944 /* Say overlay arrows are up to date. */
13945 update_overlay_arrows (1);
13946
13947 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13948 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13949 }
13950
13951 update_mode_lines = 0;
13952 windows_or_buffers_changed = 0;
13953 }
13954
13955 /* Start SIGIO interrupts coming again. Having them off during the
13956 code above makes it less likely one will discard output, but not
13957 impossible, since there might be stuff in the system buffer here.
13958 But it is much hairier to try to do anything about that. */
13959 if (interrupt_input)
13960 request_sigio ();
13961 RESUME_POLLING;
13962
13963 /* If a frame has become visible which was not before, redisplay
13964 again, so that we display it. Expose events for such a frame
13965 (which it gets when becoming visible) don't call the parts of
13966 redisplay constructing glyphs, so simply exposing a frame won't
13967 display anything in this case. So, we have to display these
13968 frames here explicitly. */
13969 if (!pending)
13970 {
13971 int new_count = 0;
13972
13973 FOR_EACH_FRAME (tail, frame)
13974 {
13975 if (XFRAME (frame)->visible)
13976 new_count++;
13977 }
13978
13979 if (new_count != number_of_visible_frames)
13980 windows_or_buffers_changed = 52;
13981 }
13982
13983 /* Change frame size now if a change is pending. */
13984 do_pending_window_change (true);
13985
13986 /* If we just did a pending size change, or have additional
13987 visible frames, or selected_window changed, redisplay again. */
13988 if ((windows_or_buffers_changed && !pending)
13989 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13990 goto retry;
13991
13992 /* Clear the face and image caches.
13993
13994 We used to do this only if consider_all_windows_p. But the cache
13995 needs to be cleared if a timer creates images in the current
13996 buffer (e.g. the test case in Bug#6230). */
13997
13998 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13999 {
14000 clear_face_cache (false);
14001 clear_face_cache_count = 0;
14002 }
14003
14004 #ifdef HAVE_WINDOW_SYSTEM
14005 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14006 {
14007 clear_image_caches (Qnil);
14008 clear_image_cache_count = 0;
14009 }
14010 #endif /* HAVE_WINDOW_SYSTEM */
14011
14012 end_of_redisplay:
14013 #ifdef HAVE_NS
14014 ns_set_doc_edited ();
14015 #endif
14016 if (interrupt_input && interrupts_deferred)
14017 request_sigio ();
14018
14019 unbind_to (count, Qnil);
14020 RESUME_POLLING;
14021 }
14022
14023
14024 /* Redisplay, but leave alone any recent echo area message unless
14025 another message has been requested in its place.
14026
14027 This is useful in situations where you need to redisplay but no
14028 user action has occurred, making it inappropriate for the message
14029 area to be cleared. See tracking_off and
14030 wait_reading_process_output for examples of these situations.
14031
14032 FROM_WHERE is an integer saying from where this function was
14033 called. This is useful for debugging. */
14034
14035 void
14036 redisplay_preserve_echo_area (int from_where)
14037 {
14038 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14039
14040 if (!NILP (echo_area_buffer[1]))
14041 {
14042 /* We have a previously displayed message, but no current
14043 message. Redisplay the previous message. */
14044 display_last_displayed_message_p = true;
14045 redisplay_internal ();
14046 display_last_displayed_message_p = false;
14047 }
14048 else
14049 redisplay_internal ();
14050
14051 flush_frame (SELECTED_FRAME ());
14052 }
14053
14054
14055 /* Function registered with record_unwind_protect in redisplay_internal. */
14056
14057 static void
14058 unwind_redisplay (void)
14059 {
14060 redisplaying_p = 0;
14061 }
14062
14063
14064 /* Mark the display of leaf window W as accurate or inaccurate.
14065 If ACCURATE_P is non-zero mark display of W as accurate. If
14066 ACCURATE_P is zero, arrange for W to be redisplayed the next
14067 time redisplay_internal is called. */
14068
14069 static void
14070 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14071 {
14072 struct buffer *b = XBUFFER (w->contents);
14073
14074 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14075 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14076 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14077
14078 if (accurate_p)
14079 {
14080 b->clip_changed = false;
14081 b->prevent_redisplay_optimizations_p = false;
14082 eassert (buffer_window_count (b) > 0);
14083 /* Resetting b->text->redisplay is problematic!
14084 In order to make it safer to do it here, redisplay_internal must
14085 have copied all b->text->redisplay to their respective windows. */
14086 b->text->redisplay = false;
14087
14088 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14089 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14090 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14091 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14092
14093 w->current_matrix->buffer = b;
14094 w->current_matrix->begv = BUF_BEGV (b);
14095 w->current_matrix->zv = BUF_ZV (b);
14096
14097 w->last_cursor_vpos = w->cursor.vpos;
14098 w->last_cursor_off_p = w->cursor_off_p;
14099
14100 if (w == XWINDOW (selected_window))
14101 w->last_point = BUF_PT (b);
14102 else
14103 w->last_point = marker_position (w->pointm);
14104
14105 w->window_end_valid = true;
14106 w->update_mode_line = false;
14107 }
14108
14109 w->redisplay = !accurate_p;
14110 }
14111
14112
14113 /* Mark the display of windows in the window tree rooted at WINDOW as
14114 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14115 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14116 be redisplayed the next time redisplay_internal is called. */
14117
14118 void
14119 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14120 {
14121 struct window *w;
14122
14123 for (; !NILP (window); window = w->next)
14124 {
14125 w = XWINDOW (window);
14126 if (WINDOWP (w->contents))
14127 mark_window_display_accurate (w->contents, accurate_p);
14128 else
14129 mark_window_display_accurate_1 (w, accurate_p);
14130 }
14131
14132 if (accurate_p)
14133 update_overlay_arrows (1);
14134 else
14135 /* Force a thorough redisplay the next time by setting
14136 last_arrow_position and last_arrow_string to t, which is
14137 unequal to any useful value of Voverlay_arrow_... */
14138 update_overlay_arrows (-1);
14139 }
14140
14141
14142 /* Return value in display table DP (Lisp_Char_Table *) for character
14143 C. Since a display table doesn't have any parent, we don't have to
14144 follow parent. Do not call this function directly but use the
14145 macro DISP_CHAR_VECTOR. */
14146
14147 Lisp_Object
14148 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14149 {
14150 Lisp_Object val;
14151
14152 if (ASCII_CHAR_P (c))
14153 {
14154 val = dp->ascii;
14155 if (SUB_CHAR_TABLE_P (val))
14156 val = XSUB_CHAR_TABLE (val)->contents[c];
14157 }
14158 else
14159 {
14160 Lisp_Object table;
14161
14162 XSETCHAR_TABLE (table, dp);
14163 val = char_table_ref (table, c);
14164 }
14165 if (NILP (val))
14166 val = dp->defalt;
14167 return val;
14168 }
14169
14170
14171 \f
14172 /***********************************************************************
14173 Window Redisplay
14174 ***********************************************************************/
14175
14176 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14177
14178 static void
14179 redisplay_windows (Lisp_Object window)
14180 {
14181 while (!NILP (window))
14182 {
14183 struct window *w = XWINDOW (window);
14184
14185 if (WINDOWP (w->contents))
14186 redisplay_windows (w->contents);
14187 else if (BUFFERP (w->contents))
14188 {
14189 displayed_buffer = XBUFFER (w->contents);
14190 /* Use list_of_error, not Qerror, so that
14191 we catch only errors and don't run the debugger. */
14192 internal_condition_case_1 (redisplay_window_0, window,
14193 list_of_error,
14194 redisplay_window_error);
14195 }
14196
14197 window = w->next;
14198 }
14199 }
14200
14201 static Lisp_Object
14202 redisplay_window_error (Lisp_Object ignore)
14203 {
14204 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14205 return Qnil;
14206 }
14207
14208 static Lisp_Object
14209 redisplay_window_0 (Lisp_Object window)
14210 {
14211 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14212 redisplay_window (window, false);
14213 return Qnil;
14214 }
14215
14216 static Lisp_Object
14217 redisplay_window_1 (Lisp_Object window)
14218 {
14219 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14220 redisplay_window (window, true);
14221 return Qnil;
14222 }
14223 \f
14224
14225 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14226 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14227 which positions recorded in ROW differ from current buffer
14228 positions.
14229
14230 Return 0 if cursor is not on this row, 1 otherwise. */
14231
14232 static int
14233 set_cursor_from_row (struct window *w, struct glyph_row *row,
14234 struct glyph_matrix *matrix,
14235 ptrdiff_t delta, ptrdiff_t delta_bytes,
14236 int dy, int dvpos)
14237 {
14238 struct glyph *glyph = row->glyphs[TEXT_AREA];
14239 struct glyph *end = glyph + row->used[TEXT_AREA];
14240 struct glyph *cursor = NULL;
14241 /* The last known character position in row. */
14242 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14243 int x = row->x;
14244 ptrdiff_t pt_old = PT - delta;
14245 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14246 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14247 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14248 /* A glyph beyond the edge of TEXT_AREA which we should never
14249 touch. */
14250 struct glyph *glyphs_end = end;
14251 /* Non-zero means we've found a match for cursor position, but that
14252 glyph has the avoid_cursor_p flag set. */
14253 int match_with_avoid_cursor = 0;
14254 /* Non-zero means we've seen at least one glyph that came from a
14255 display string. */
14256 int string_seen = 0;
14257 /* Largest and smallest buffer positions seen so far during scan of
14258 glyph row. */
14259 ptrdiff_t bpos_max = pos_before;
14260 ptrdiff_t bpos_min = pos_after;
14261 /* Last buffer position covered by an overlay string with an integer
14262 `cursor' property. */
14263 ptrdiff_t bpos_covered = 0;
14264 /* Non-zero means the display string on which to display the cursor
14265 comes from a text property, not from an overlay. */
14266 int string_from_text_prop = 0;
14267
14268 /* Don't even try doing anything if called for a mode-line or
14269 header-line row, since the rest of the code isn't prepared to
14270 deal with such calamities. */
14271 eassert (!row->mode_line_p);
14272 if (row->mode_line_p)
14273 return 0;
14274
14275 /* Skip over glyphs not having an object at the start and the end of
14276 the row. These are special glyphs like truncation marks on
14277 terminal frames. */
14278 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14279 {
14280 if (!row->reversed_p)
14281 {
14282 while (glyph < end
14283 && INTEGERP (glyph->object)
14284 && glyph->charpos < 0)
14285 {
14286 x += glyph->pixel_width;
14287 ++glyph;
14288 }
14289 while (end > glyph
14290 && INTEGERP ((end - 1)->object)
14291 /* CHARPOS is zero for blanks and stretch glyphs
14292 inserted by extend_face_to_end_of_line. */
14293 && (end - 1)->charpos <= 0)
14294 --end;
14295 glyph_before = glyph - 1;
14296 glyph_after = end;
14297 }
14298 else
14299 {
14300 struct glyph *g;
14301
14302 /* If the glyph row is reversed, we need to process it from back
14303 to front, so swap the edge pointers. */
14304 glyphs_end = end = glyph - 1;
14305 glyph += row->used[TEXT_AREA] - 1;
14306
14307 while (glyph > end + 1
14308 && INTEGERP (glyph->object)
14309 && glyph->charpos < 0)
14310 {
14311 --glyph;
14312 x -= glyph->pixel_width;
14313 }
14314 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14315 --glyph;
14316 /* By default, in reversed rows we put the cursor on the
14317 rightmost (first in the reading order) glyph. */
14318 for (g = end + 1; g < glyph; g++)
14319 x += g->pixel_width;
14320 while (end < glyph
14321 && INTEGERP ((end + 1)->object)
14322 && (end + 1)->charpos <= 0)
14323 ++end;
14324 glyph_before = glyph + 1;
14325 glyph_after = end;
14326 }
14327 }
14328 else if (row->reversed_p)
14329 {
14330 /* In R2L rows that don't display text, put the cursor on the
14331 rightmost glyph. Case in point: an empty last line that is
14332 part of an R2L paragraph. */
14333 cursor = end - 1;
14334 /* Avoid placing the cursor on the last glyph of the row, where
14335 on terminal frames we hold the vertical border between
14336 adjacent windows. */
14337 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14338 && !WINDOW_RIGHTMOST_P (w)
14339 && cursor == row->glyphs[LAST_AREA] - 1)
14340 cursor--;
14341 x = -1; /* will be computed below, at label compute_x */
14342 }
14343
14344 /* Step 1: Try to find the glyph whose character position
14345 corresponds to point. If that's not possible, find 2 glyphs
14346 whose character positions are the closest to point, one before
14347 point, the other after it. */
14348 if (!row->reversed_p)
14349 while (/* not marched to end of glyph row */
14350 glyph < end
14351 /* glyph was not inserted by redisplay for internal purposes */
14352 && !INTEGERP (glyph->object))
14353 {
14354 if (BUFFERP (glyph->object))
14355 {
14356 ptrdiff_t dpos = glyph->charpos - pt_old;
14357
14358 if (glyph->charpos > bpos_max)
14359 bpos_max = glyph->charpos;
14360 if (glyph->charpos < bpos_min)
14361 bpos_min = glyph->charpos;
14362 if (!glyph->avoid_cursor_p)
14363 {
14364 /* If we hit point, we've found the glyph on which to
14365 display the cursor. */
14366 if (dpos == 0)
14367 {
14368 match_with_avoid_cursor = 0;
14369 break;
14370 }
14371 /* See if we've found a better approximation to
14372 POS_BEFORE or to POS_AFTER. */
14373 if (0 > dpos && dpos > pos_before - pt_old)
14374 {
14375 pos_before = glyph->charpos;
14376 glyph_before = glyph;
14377 }
14378 else if (0 < dpos && dpos < pos_after - pt_old)
14379 {
14380 pos_after = glyph->charpos;
14381 glyph_after = glyph;
14382 }
14383 }
14384 else if (dpos == 0)
14385 match_with_avoid_cursor = 1;
14386 }
14387 else if (STRINGP (glyph->object))
14388 {
14389 Lisp_Object chprop;
14390 ptrdiff_t glyph_pos = glyph->charpos;
14391
14392 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14393 glyph->object);
14394 if (!NILP (chprop))
14395 {
14396 /* If the string came from a `display' text property,
14397 look up the buffer position of that property and
14398 use that position to update bpos_max, as if we
14399 actually saw such a position in one of the row's
14400 glyphs. This helps with supporting integer values
14401 of `cursor' property on the display string in
14402 situations where most or all of the row's buffer
14403 text is completely covered by display properties,
14404 so that no glyph with valid buffer positions is
14405 ever seen in the row. */
14406 ptrdiff_t prop_pos =
14407 string_buffer_position_lim (glyph->object, pos_before,
14408 pos_after, 0);
14409
14410 if (prop_pos >= pos_before)
14411 bpos_max = prop_pos;
14412 }
14413 if (INTEGERP (chprop))
14414 {
14415 bpos_covered = bpos_max + XINT (chprop);
14416 /* If the `cursor' property covers buffer positions up
14417 to and including point, we should display cursor on
14418 this glyph. Note that, if a `cursor' property on one
14419 of the string's characters has an integer value, we
14420 will break out of the loop below _before_ we get to
14421 the position match above. IOW, integer values of
14422 the `cursor' property override the "exact match for
14423 point" strategy of positioning the cursor. */
14424 /* Implementation note: bpos_max == pt_old when, e.g.,
14425 we are in an empty line, where bpos_max is set to
14426 MATRIX_ROW_START_CHARPOS, see above. */
14427 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14428 {
14429 cursor = glyph;
14430 break;
14431 }
14432 }
14433
14434 string_seen = 1;
14435 }
14436 x += glyph->pixel_width;
14437 ++glyph;
14438 }
14439 else if (glyph > end) /* row is reversed */
14440 while (!INTEGERP (glyph->object))
14441 {
14442 if (BUFFERP (glyph->object))
14443 {
14444 ptrdiff_t dpos = glyph->charpos - pt_old;
14445
14446 if (glyph->charpos > bpos_max)
14447 bpos_max = glyph->charpos;
14448 if (glyph->charpos < bpos_min)
14449 bpos_min = glyph->charpos;
14450 if (!glyph->avoid_cursor_p)
14451 {
14452 if (dpos == 0)
14453 {
14454 match_with_avoid_cursor = 0;
14455 break;
14456 }
14457 if (0 > dpos && dpos > pos_before - pt_old)
14458 {
14459 pos_before = glyph->charpos;
14460 glyph_before = glyph;
14461 }
14462 else if (0 < dpos && dpos < pos_after - pt_old)
14463 {
14464 pos_after = glyph->charpos;
14465 glyph_after = glyph;
14466 }
14467 }
14468 else if (dpos == 0)
14469 match_with_avoid_cursor = 1;
14470 }
14471 else if (STRINGP (glyph->object))
14472 {
14473 Lisp_Object chprop;
14474 ptrdiff_t glyph_pos = glyph->charpos;
14475
14476 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14477 glyph->object);
14478 if (!NILP (chprop))
14479 {
14480 ptrdiff_t prop_pos =
14481 string_buffer_position_lim (glyph->object, pos_before,
14482 pos_after, 0);
14483
14484 if (prop_pos >= pos_before)
14485 bpos_max = prop_pos;
14486 }
14487 if (INTEGERP (chprop))
14488 {
14489 bpos_covered = bpos_max + XINT (chprop);
14490 /* If the `cursor' property covers buffer positions up
14491 to and including point, we should display cursor on
14492 this glyph. */
14493 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14494 {
14495 cursor = glyph;
14496 break;
14497 }
14498 }
14499 string_seen = 1;
14500 }
14501 --glyph;
14502 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14503 {
14504 x--; /* can't use any pixel_width */
14505 break;
14506 }
14507 x -= glyph->pixel_width;
14508 }
14509
14510 /* Step 2: If we didn't find an exact match for point, we need to
14511 look for a proper place to put the cursor among glyphs between
14512 GLYPH_BEFORE and GLYPH_AFTER. */
14513 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14514 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14515 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14516 {
14517 /* An empty line has a single glyph whose OBJECT is zero and
14518 whose CHARPOS is the position of a newline on that line.
14519 Note that on a TTY, there are more glyphs after that, which
14520 were produced by extend_face_to_end_of_line, but their
14521 CHARPOS is zero or negative. */
14522 int empty_line_p =
14523 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14524 && INTEGERP (glyph->object) && glyph->charpos > 0
14525 /* On a TTY, continued and truncated rows also have a glyph at
14526 their end whose OBJECT is zero and whose CHARPOS is
14527 positive (the continuation and truncation glyphs), but such
14528 rows are obviously not "empty". */
14529 && !(row->continued_p || row->truncated_on_right_p);
14530
14531 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14532 {
14533 ptrdiff_t ellipsis_pos;
14534
14535 /* Scan back over the ellipsis glyphs. */
14536 if (!row->reversed_p)
14537 {
14538 ellipsis_pos = (glyph - 1)->charpos;
14539 while (glyph > row->glyphs[TEXT_AREA]
14540 && (glyph - 1)->charpos == ellipsis_pos)
14541 glyph--, x -= glyph->pixel_width;
14542 /* That loop always goes one position too far, including
14543 the glyph before the ellipsis. So scan forward over
14544 that one. */
14545 x += glyph->pixel_width;
14546 glyph++;
14547 }
14548 else /* row is reversed */
14549 {
14550 ellipsis_pos = (glyph + 1)->charpos;
14551 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14552 && (glyph + 1)->charpos == ellipsis_pos)
14553 glyph++, x += glyph->pixel_width;
14554 x -= glyph->pixel_width;
14555 glyph--;
14556 }
14557 }
14558 else if (match_with_avoid_cursor)
14559 {
14560 cursor = glyph_after;
14561 x = -1;
14562 }
14563 else if (string_seen)
14564 {
14565 int incr = row->reversed_p ? -1 : +1;
14566
14567 /* Need to find the glyph that came out of a string which is
14568 present at point. That glyph is somewhere between
14569 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14570 positioned between POS_BEFORE and POS_AFTER in the
14571 buffer. */
14572 struct glyph *start, *stop;
14573 ptrdiff_t pos = pos_before;
14574
14575 x = -1;
14576
14577 /* If the row ends in a newline from a display string,
14578 reordering could have moved the glyphs belonging to the
14579 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14580 in this case we extend the search to the last glyph in
14581 the row that was not inserted by redisplay. */
14582 if (row->ends_in_newline_from_string_p)
14583 {
14584 glyph_after = end;
14585 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14586 }
14587
14588 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14589 correspond to POS_BEFORE and POS_AFTER, respectively. We
14590 need START and STOP in the order that corresponds to the
14591 row's direction as given by its reversed_p flag. If the
14592 directionality of characters between POS_BEFORE and
14593 POS_AFTER is the opposite of the row's base direction,
14594 these characters will have been reordered for display,
14595 and we need to reverse START and STOP. */
14596 if (!row->reversed_p)
14597 {
14598 start = min (glyph_before, glyph_after);
14599 stop = max (glyph_before, glyph_after);
14600 }
14601 else
14602 {
14603 start = max (glyph_before, glyph_after);
14604 stop = min (glyph_before, glyph_after);
14605 }
14606 for (glyph = start + incr;
14607 row->reversed_p ? glyph > stop : glyph < stop; )
14608 {
14609
14610 /* Any glyphs that come from the buffer are here because
14611 of bidi reordering. Skip them, and only pay
14612 attention to glyphs that came from some string. */
14613 if (STRINGP (glyph->object))
14614 {
14615 Lisp_Object str;
14616 ptrdiff_t tem;
14617 /* If the display property covers the newline, we
14618 need to search for it one position farther. */
14619 ptrdiff_t lim = pos_after
14620 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14621
14622 string_from_text_prop = 0;
14623 str = glyph->object;
14624 tem = string_buffer_position_lim (str, pos, lim, 0);
14625 if (tem == 0 /* from overlay */
14626 || pos <= tem)
14627 {
14628 /* If the string from which this glyph came is
14629 found in the buffer at point, or at position
14630 that is closer to point than pos_after, then
14631 we've found the glyph we've been looking for.
14632 If it comes from an overlay (tem == 0), and
14633 it has the `cursor' property on one of its
14634 glyphs, record that glyph as a candidate for
14635 displaying the cursor. (As in the
14636 unidirectional version, we will display the
14637 cursor on the last candidate we find.) */
14638 if (tem == 0
14639 || tem == pt_old
14640 || (tem - pt_old > 0 && tem < pos_after))
14641 {
14642 /* The glyphs from this string could have
14643 been reordered. Find the one with the
14644 smallest string position. Or there could
14645 be a character in the string with the
14646 `cursor' property, which means display
14647 cursor on that character's glyph. */
14648 ptrdiff_t strpos = glyph->charpos;
14649
14650 if (tem)
14651 {
14652 cursor = glyph;
14653 string_from_text_prop = 1;
14654 }
14655 for ( ;
14656 (row->reversed_p ? glyph > stop : glyph < stop)
14657 && EQ (glyph->object, str);
14658 glyph += incr)
14659 {
14660 Lisp_Object cprop;
14661 ptrdiff_t gpos = glyph->charpos;
14662
14663 cprop = Fget_char_property (make_number (gpos),
14664 Qcursor,
14665 glyph->object);
14666 if (!NILP (cprop))
14667 {
14668 cursor = glyph;
14669 break;
14670 }
14671 if (tem && glyph->charpos < strpos)
14672 {
14673 strpos = glyph->charpos;
14674 cursor = glyph;
14675 }
14676 }
14677
14678 if (tem == pt_old
14679 || (tem - pt_old > 0 && tem < pos_after))
14680 goto compute_x;
14681 }
14682 if (tem)
14683 pos = tem + 1; /* don't find previous instances */
14684 }
14685 /* This string is not what we want; skip all of the
14686 glyphs that came from it. */
14687 while ((row->reversed_p ? glyph > stop : glyph < stop)
14688 && EQ (glyph->object, str))
14689 glyph += incr;
14690 }
14691 else
14692 glyph += incr;
14693 }
14694
14695 /* If we reached the end of the line, and END was from a string,
14696 the cursor is not on this line. */
14697 if (cursor == NULL
14698 && (row->reversed_p ? glyph <= end : glyph >= end)
14699 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14700 && STRINGP (end->object)
14701 && row->continued_p)
14702 return 0;
14703 }
14704 /* A truncated row may not include PT among its character positions.
14705 Setting the cursor inside the scroll margin will trigger
14706 recalculation of hscroll in hscroll_window_tree. But if a
14707 display string covers point, defer to the string-handling
14708 code below to figure this out. */
14709 else if (row->truncated_on_left_p && pt_old < bpos_min)
14710 {
14711 cursor = glyph_before;
14712 x = -1;
14713 }
14714 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14715 /* Zero-width characters produce no glyphs. */
14716 || (!empty_line_p
14717 && (row->reversed_p
14718 ? glyph_after > glyphs_end
14719 : glyph_after < glyphs_end)))
14720 {
14721 cursor = glyph_after;
14722 x = -1;
14723 }
14724 }
14725
14726 compute_x:
14727 if (cursor != NULL)
14728 glyph = cursor;
14729 else if (glyph == glyphs_end
14730 && pos_before == pos_after
14731 && STRINGP ((row->reversed_p
14732 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14733 : row->glyphs[TEXT_AREA])->object))
14734 {
14735 /* If all the glyphs of this row came from strings, put the
14736 cursor on the first glyph of the row. This avoids having the
14737 cursor outside of the text area in this very rare and hard
14738 use case. */
14739 glyph =
14740 row->reversed_p
14741 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14742 : row->glyphs[TEXT_AREA];
14743 }
14744 if (x < 0)
14745 {
14746 struct glyph *g;
14747
14748 /* Need to compute x that corresponds to GLYPH. */
14749 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14750 {
14751 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14752 emacs_abort ();
14753 x += g->pixel_width;
14754 }
14755 }
14756
14757 /* ROW could be part of a continued line, which, under bidi
14758 reordering, might have other rows whose start and end charpos
14759 occlude point. Only set w->cursor if we found a better
14760 approximation to the cursor position than we have from previously
14761 examined candidate rows belonging to the same continued line. */
14762 if (/* We already have a candidate row. */
14763 w->cursor.vpos >= 0
14764 /* That candidate is not the row we are processing. */
14765 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14766 /* Make sure cursor.vpos specifies a row whose start and end
14767 charpos occlude point, and it is valid candidate for being a
14768 cursor-row. This is because some callers of this function
14769 leave cursor.vpos at the row where the cursor was displayed
14770 during the last redisplay cycle. */
14771 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14772 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14773 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14774 {
14775 struct glyph *g1
14776 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14777
14778 /* Don't consider glyphs that are outside TEXT_AREA. */
14779 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14780 return 0;
14781 /* Keep the candidate whose buffer position is the closest to
14782 point or has the `cursor' property. */
14783 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14784 w->cursor.hpos >= 0
14785 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14786 && ((BUFFERP (g1->object)
14787 && (g1->charpos == pt_old /* An exact match always wins. */
14788 || (BUFFERP (glyph->object)
14789 && eabs (g1->charpos - pt_old)
14790 < eabs (glyph->charpos - pt_old))))
14791 /* Previous candidate is a glyph from a string that has
14792 a non-nil `cursor' property. */
14793 || (STRINGP (g1->object)
14794 && (!NILP (Fget_char_property (make_number (g1->charpos),
14795 Qcursor, g1->object))
14796 /* Previous candidate is from the same display
14797 string as this one, and the display string
14798 came from a text property. */
14799 || (EQ (g1->object, glyph->object)
14800 && string_from_text_prop)
14801 /* this candidate is from newline and its
14802 position is not an exact match */
14803 || (INTEGERP (glyph->object)
14804 && glyph->charpos != pt_old)))))
14805 return 0;
14806 /* If this candidate gives an exact match, use that. */
14807 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14808 /* If this candidate is a glyph created for the
14809 terminating newline of a line, and point is on that
14810 newline, it wins because it's an exact match. */
14811 || (!row->continued_p
14812 && INTEGERP (glyph->object)
14813 && glyph->charpos == 0
14814 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14815 /* Otherwise, keep the candidate that comes from a row
14816 spanning less buffer positions. This may win when one or
14817 both candidate positions are on glyphs that came from
14818 display strings, for which we cannot compare buffer
14819 positions. */
14820 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14821 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14822 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14823 return 0;
14824 }
14825 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14826 w->cursor.x = x;
14827 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14828 w->cursor.y = row->y + dy;
14829
14830 if (w == XWINDOW (selected_window))
14831 {
14832 if (!row->continued_p
14833 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14834 && row->x == 0)
14835 {
14836 this_line_buffer = XBUFFER (w->contents);
14837
14838 CHARPOS (this_line_start_pos)
14839 = MATRIX_ROW_START_CHARPOS (row) + delta;
14840 BYTEPOS (this_line_start_pos)
14841 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14842
14843 CHARPOS (this_line_end_pos)
14844 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14845 BYTEPOS (this_line_end_pos)
14846 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14847
14848 this_line_y = w->cursor.y;
14849 this_line_pixel_height = row->height;
14850 this_line_vpos = w->cursor.vpos;
14851 this_line_start_x = row->x;
14852 }
14853 else
14854 CHARPOS (this_line_start_pos) = 0;
14855 }
14856
14857 return 1;
14858 }
14859
14860
14861 /* Run window scroll functions, if any, for WINDOW with new window
14862 start STARTP. Sets the window start of WINDOW to that position.
14863
14864 We assume that the window's buffer is really current. */
14865
14866 static struct text_pos
14867 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14868 {
14869 struct window *w = XWINDOW (window);
14870 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14871
14872 eassert (current_buffer == XBUFFER (w->contents));
14873
14874 if (!NILP (Vwindow_scroll_functions))
14875 {
14876 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14877 make_number (CHARPOS (startp)));
14878 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14879 /* In case the hook functions switch buffers. */
14880 set_buffer_internal (XBUFFER (w->contents));
14881 }
14882
14883 return startp;
14884 }
14885
14886
14887 /* Make sure the line containing the cursor is fully visible.
14888 A value of 1 means there is nothing to be done.
14889 (Either the line is fully visible, or it cannot be made so,
14890 or we cannot tell.)
14891
14892 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14893 is higher than window.
14894
14895 If CURRENT_MATRIX_P is non-zero, use the information from the
14896 window's current glyph matrix; otherwise use the desired glyph
14897 matrix.
14898
14899 A value of 0 means the caller should do scrolling
14900 as if point had gone off the screen. */
14901
14902 static int
14903 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14904 {
14905 struct glyph_matrix *matrix;
14906 struct glyph_row *row;
14907 int window_height;
14908
14909 if (!make_cursor_line_fully_visible_p)
14910 return 1;
14911
14912 /* It's not always possible to find the cursor, e.g, when a window
14913 is full of overlay strings. Don't do anything in that case. */
14914 if (w->cursor.vpos < 0)
14915 return 1;
14916
14917 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14918 row = MATRIX_ROW (matrix, w->cursor.vpos);
14919
14920 /* If the cursor row is not partially visible, there's nothing to do. */
14921 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14922 return 1;
14923
14924 /* If the row the cursor is in is taller than the window's height,
14925 it's not clear what to do, so do nothing. */
14926 window_height = window_box_height (w);
14927 if (row->height >= window_height)
14928 {
14929 if (!force_p || MINI_WINDOW_P (w)
14930 || w->vscroll || w->cursor.vpos == 0)
14931 return 1;
14932 }
14933 return 0;
14934 }
14935
14936
14937 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14938 non-zero means only WINDOW is redisplayed in redisplay_internal.
14939 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14940 in redisplay_window to bring a partially visible line into view in
14941 the case that only the cursor has moved.
14942
14943 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14944 last screen line's vertical height extends past the end of the screen.
14945
14946 Value is
14947
14948 1 if scrolling succeeded
14949
14950 0 if scrolling didn't find point.
14951
14952 -1 if new fonts have been loaded so that we must interrupt
14953 redisplay, adjust glyph matrices, and try again. */
14954
14955 enum
14956 {
14957 SCROLLING_SUCCESS,
14958 SCROLLING_FAILED,
14959 SCROLLING_NEED_LARGER_MATRICES
14960 };
14961
14962 /* If scroll-conservatively is more than this, never recenter.
14963
14964 If you change this, don't forget to update the doc string of
14965 `scroll-conservatively' and the Emacs manual. */
14966 #define SCROLL_LIMIT 100
14967
14968 static int
14969 try_scrolling (Lisp_Object window, int just_this_one_p,
14970 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14971 int temp_scroll_step, int last_line_misfit)
14972 {
14973 struct window *w = XWINDOW (window);
14974 struct frame *f = XFRAME (w->frame);
14975 struct text_pos pos, startp;
14976 struct it it;
14977 int this_scroll_margin, scroll_max, rc, height;
14978 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14979 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14980 Lisp_Object aggressive;
14981 /* We will never try scrolling more than this number of lines. */
14982 int scroll_limit = SCROLL_LIMIT;
14983 int frame_line_height = default_line_pixel_height (w);
14984 int window_total_lines
14985 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14986
14987 #ifdef GLYPH_DEBUG
14988 debug_method_add (w, "try_scrolling");
14989 #endif
14990
14991 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14992
14993 /* Compute scroll margin height in pixels. We scroll when point is
14994 within this distance from the top or bottom of the window. */
14995 if (scroll_margin > 0)
14996 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14997 * frame_line_height;
14998 else
14999 this_scroll_margin = 0;
15000
15001 /* Force arg_scroll_conservatively to have a reasonable value, to
15002 avoid scrolling too far away with slow move_it_* functions. Note
15003 that the user can supply scroll-conservatively equal to
15004 `most-positive-fixnum', which can be larger than INT_MAX. */
15005 if (arg_scroll_conservatively > scroll_limit)
15006 {
15007 arg_scroll_conservatively = scroll_limit + 1;
15008 scroll_max = scroll_limit * frame_line_height;
15009 }
15010 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15011 /* Compute how much we should try to scroll maximally to bring
15012 point into view. */
15013 scroll_max = (max (scroll_step,
15014 max (arg_scroll_conservatively, temp_scroll_step))
15015 * frame_line_height);
15016 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15017 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15018 /* We're trying to scroll because of aggressive scrolling but no
15019 scroll_step is set. Choose an arbitrary one. */
15020 scroll_max = 10 * frame_line_height;
15021 else
15022 scroll_max = 0;
15023
15024 too_near_end:
15025
15026 /* Decide whether to scroll down. */
15027 if (PT > CHARPOS (startp))
15028 {
15029 int scroll_margin_y;
15030
15031 /* Compute the pixel ypos of the scroll margin, then move IT to
15032 either that ypos or PT, whichever comes first. */
15033 start_display (&it, w, startp);
15034 scroll_margin_y = it.last_visible_y - this_scroll_margin
15035 - frame_line_height * extra_scroll_margin_lines;
15036 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15037 (MOVE_TO_POS | MOVE_TO_Y));
15038
15039 if (PT > CHARPOS (it.current.pos))
15040 {
15041 int y0 = line_bottom_y (&it);
15042 /* Compute how many pixels below window bottom to stop searching
15043 for PT. This avoids costly search for PT that is far away if
15044 the user limited scrolling by a small number of lines, but
15045 always finds PT if scroll_conservatively is set to a large
15046 number, such as most-positive-fixnum. */
15047 int slack = max (scroll_max, 10 * frame_line_height);
15048 int y_to_move = it.last_visible_y + slack;
15049
15050 /* Compute the distance from the scroll margin to PT or to
15051 the scroll limit, whichever comes first. This should
15052 include the height of the cursor line, to make that line
15053 fully visible. */
15054 move_it_to (&it, PT, -1, y_to_move,
15055 -1, MOVE_TO_POS | MOVE_TO_Y);
15056 dy = line_bottom_y (&it) - y0;
15057
15058 if (dy > scroll_max)
15059 return SCROLLING_FAILED;
15060
15061 if (dy > 0)
15062 scroll_down_p = 1;
15063 }
15064 }
15065
15066 if (scroll_down_p)
15067 {
15068 /* Point is in or below the bottom scroll margin, so move the
15069 window start down. If scrolling conservatively, move it just
15070 enough down to make point visible. If scroll_step is set,
15071 move it down by scroll_step. */
15072 if (arg_scroll_conservatively)
15073 amount_to_scroll
15074 = min (max (dy, frame_line_height),
15075 frame_line_height * arg_scroll_conservatively);
15076 else if (scroll_step || temp_scroll_step)
15077 amount_to_scroll = scroll_max;
15078 else
15079 {
15080 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15081 height = WINDOW_BOX_TEXT_HEIGHT (w);
15082 if (NUMBERP (aggressive))
15083 {
15084 double float_amount = XFLOATINT (aggressive) * height;
15085 int aggressive_scroll = float_amount;
15086 if (aggressive_scroll == 0 && float_amount > 0)
15087 aggressive_scroll = 1;
15088 /* Don't let point enter the scroll margin near top of
15089 the window. This could happen if the value of
15090 scroll_up_aggressively is too large and there are
15091 non-zero margins, because scroll_up_aggressively
15092 means put point that fraction of window height
15093 _from_the_bottom_margin_. */
15094 if (aggressive_scroll + 2 * this_scroll_margin > height)
15095 aggressive_scroll = height - 2 * this_scroll_margin;
15096 amount_to_scroll = dy + aggressive_scroll;
15097 }
15098 }
15099
15100 if (amount_to_scroll <= 0)
15101 return SCROLLING_FAILED;
15102
15103 start_display (&it, w, startp);
15104 if (arg_scroll_conservatively <= scroll_limit)
15105 move_it_vertically (&it, amount_to_scroll);
15106 else
15107 {
15108 /* Extra precision for users who set scroll-conservatively
15109 to a large number: make sure the amount we scroll
15110 the window start is never less than amount_to_scroll,
15111 which was computed as distance from window bottom to
15112 point. This matters when lines at window top and lines
15113 below window bottom have different height. */
15114 struct it it1;
15115 void *it1data = NULL;
15116 /* We use a temporary it1 because line_bottom_y can modify
15117 its argument, if it moves one line down; see there. */
15118 int start_y;
15119
15120 SAVE_IT (it1, it, it1data);
15121 start_y = line_bottom_y (&it1);
15122 do {
15123 RESTORE_IT (&it, &it, it1data);
15124 move_it_by_lines (&it, 1);
15125 SAVE_IT (it1, it, it1data);
15126 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15127 }
15128
15129 /* If STARTP is unchanged, move it down another screen line. */
15130 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15131 move_it_by_lines (&it, 1);
15132 startp = it.current.pos;
15133 }
15134 else
15135 {
15136 struct text_pos scroll_margin_pos = startp;
15137 int y_offset = 0;
15138
15139 /* See if point is inside the scroll margin at the top of the
15140 window. */
15141 if (this_scroll_margin)
15142 {
15143 int y_start;
15144
15145 start_display (&it, w, startp);
15146 y_start = it.current_y;
15147 move_it_vertically (&it, this_scroll_margin);
15148 scroll_margin_pos = it.current.pos;
15149 /* If we didn't move enough before hitting ZV, request
15150 additional amount of scroll, to move point out of the
15151 scroll margin. */
15152 if (IT_CHARPOS (it) == ZV
15153 && it.current_y - y_start < this_scroll_margin)
15154 y_offset = this_scroll_margin - (it.current_y - y_start);
15155 }
15156
15157 if (PT < CHARPOS (scroll_margin_pos))
15158 {
15159 /* Point is in the scroll margin at the top of the window or
15160 above what is displayed in the window. */
15161 int y0, y_to_move;
15162
15163 /* Compute the vertical distance from PT to the scroll
15164 margin position. Move as far as scroll_max allows, or
15165 one screenful, or 10 screen lines, whichever is largest.
15166 Give up if distance is greater than scroll_max or if we
15167 didn't reach the scroll margin position. */
15168 SET_TEXT_POS (pos, PT, PT_BYTE);
15169 start_display (&it, w, pos);
15170 y0 = it.current_y;
15171 y_to_move = max (it.last_visible_y,
15172 max (scroll_max, 10 * frame_line_height));
15173 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15174 y_to_move, -1,
15175 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15176 dy = it.current_y - y0;
15177 if (dy > scroll_max
15178 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15179 return SCROLLING_FAILED;
15180
15181 /* Additional scroll for when ZV was too close to point. */
15182 dy += y_offset;
15183
15184 /* Compute new window start. */
15185 start_display (&it, w, startp);
15186
15187 if (arg_scroll_conservatively)
15188 amount_to_scroll = max (dy, frame_line_height
15189 * max (scroll_step, temp_scroll_step));
15190 else if (scroll_step || temp_scroll_step)
15191 amount_to_scroll = scroll_max;
15192 else
15193 {
15194 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15195 height = WINDOW_BOX_TEXT_HEIGHT (w);
15196 if (NUMBERP (aggressive))
15197 {
15198 double float_amount = XFLOATINT (aggressive) * height;
15199 int aggressive_scroll = float_amount;
15200 if (aggressive_scroll == 0 && float_amount > 0)
15201 aggressive_scroll = 1;
15202 /* Don't let point enter the scroll margin near
15203 bottom of the window, if the value of
15204 scroll_down_aggressively happens to be too
15205 large. */
15206 if (aggressive_scroll + 2 * this_scroll_margin > height)
15207 aggressive_scroll = height - 2 * this_scroll_margin;
15208 amount_to_scroll = dy + aggressive_scroll;
15209 }
15210 }
15211
15212 if (amount_to_scroll <= 0)
15213 return SCROLLING_FAILED;
15214
15215 move_it_vertically_backward (&it, amount_to_scroll);
15216 startp = it.current.pos;
15217 }
15218 }
15219
15220 /* Run window scroll functions. */
15221 startp = run_window_scroll_functions (window, startp);
15222
15223 /* Display the window. Give up if new fonts are loaded, or if point
15224 doesn't appear. */
15225 if (!try_window (window, startp, 0))
15226 rc = SCROLLING_NEED_LARGER_MATRICES;
15227 else if (w->cursor.vpos < 0)
15228 {
15229 clear_glyph_matrix (w->desired_matrix);
15230 rc = SCROLLING_FAILED;
15231 }
15232 else
15233 {
15234 /* Maybe forget recorded base line for line number display. */
15235 if (!just_this_one_p
15236 || current_buffer->clip_changed
15237 || BEG_UNCHANGED < CHARPOS (startp))
15238 w->base_line_number = 0;
15239
15240 /* If cursor ends up on a partially visible line,
15241 treat that as being off the bottom of the screen. */
15242 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15243 /* It's possible that the cursor is on the first line of the
15244 buffer, which is partially obscured due to a vscroll
15245 (Bug#7537). In that case, avoid looping forever. */
15246 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15247 {
15248 clear_glyph_matrix (w->desired_matrix);
15249 ++extra_scroll_margin_lines;
15250 goto too_near_end;
15251 }
15252 rc = SCROLLING_SUCCESS;
15253 }
15254
15255 return rc;
15256 }
15257
15258
15259 /* Compute a suitable window start for window W if display of W starts
15260 on a continuation line. Value is non-zero if a new window start
15261 was computed.
15262
15263 The new window start will be computed, based on W's width, starting
15264 from the start of the continued line. It is the start of the
15265 screen line with the minimum distance from the old start W->start. */
15266
15267 static int
15268 compute_window_start_on_continuation_line (struct window *w)
15269 {
15270 struct text_pos pos, start_pos;
15271 int window_start_changed_p = 0;
15272
15273 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15274
15275 /* If window start is on a continuation line... Window start may be
15276 < BEGV in case there's invisible text at the start of the
15277 buffer (M-x rmail, for example). */
15278 if (CHARPOS (start_pos) > BEGV
15279 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15280 {
15281 struct it it;
15282 struct glyph_row *row;
15283
15284 /* Handle the case that the window start is out of range. */
15285 if (CHARPOS (start_pos) < BEGV)
15286 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15287 else if (CHARPOS (start_pos) > ZV)
15288 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15289
15290 /* Find the start of the continued line. This should be fast
15291 because find_newline is fast (newline cache). */
15292 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15293 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15294 row, DEFAULT_FACE_ID);
15295 reseat_at_previous_visible_line_start (&it);
15296
15297 /* If the line start is "too far" away from the window start,
15298 say it takes too much time to compute a new window start. */
15299 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15300 /* PXW: Do we need upper bounds here? */
15301 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15302 {
15303 int min_distance, distance;
15304
15305 /* Move forward by display lines to find the new window
15306 start. If window width was enlarged, the new start can
15307 be expected to be > the old start. If window width was
15308 decreased, the new window start will be < the old start.
15309 So, we're looking for the display line start with the
15310 minimum distance from the old window start. */
15311 pos = it.current.pos;
15312 min_distance = INFINITY;
15313 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15314 distance < min_distance)
15315 {
15316 min_distance = distance;
15317 pos = it.current.pos;
15318 if (it.line_wrap == WORD_WRAP)
15319 {
15320 /* Under WORD_WRAP, move_it_by_lines is likely to
15321 overshoot and stop not at the first, but the
15322 second character from the left margin. So in
15323 that case, we need a more tight control on the X
15324 coordinate of the iterator than move_it_by_lines
15325 promises in its contract. The method is to first
15326 go to the last (rightmost) visible character of a
15327 line, then move to the leftmost character on the
15328 next line in a separate call. */
15329 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15330 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15331 move_it_to (&it, ZV, 0,
15332 it.current_y + it.max_ascent + it.max_descent, -1,
15333 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15334 }
15335 else
15336 move_it_by_lines (&it, 1);
15337 }
15338
15339 /* Set the window start there. */
15340 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15341 window_start_changed_p = 1;
15342 }
15343 }
15344
15345 return window_start_changed_p;
15346 }
15347
15348
15349 /* Try cursor movement in case text has not changed in window WINDOW,
15350 with window start STARTP. Value is
15351
15352 CURSOR_MOVEMENT_SUCCESS if successful
15353
15354 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15355
15356 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15357 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15358 we want to scroll as if scroll-step were set to 1. See the code.
15359
15360 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15361 which case we have to abort this redisplay, and adjust matrices
15362 first. */
15363
15364 enum
15365 {
15366 CURSOR_MOVEMENT_SUCCESS,
15367 CURSOR_MOVEMENT_CANNOT_BE_USED,
15368 CURSOR_MOVEMENT_MUST_SCROLL,
15369 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15370 };
15371
15372 static int
15373 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15374 {
15375 struct window *w = XWINDOW (window);
15376 struct frame *f = XFRAME (w->frame);
15377 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15378
15379 #ifdef GLYPH_DEBUG
15380 if (inhibit_try_cursor_movement)
15381 return rc;
15382 #endif
15383
15384 /* Previously, there was a check for Lisp integer in the
15385 if-statement below. Now, this field is converted to
15386 ptrdiff_t, thus zero means invalid position in a buffer. */
15387 eassert (w->last_point > 0);
15388 /* Likewise there was a check whether window_end_vpos is nil or larger
15389 than the window. Now window_end_vpos is int and so never nil, but
15390 let's leave eassert to check whether it fits in the window. */
15391 eassert (w->window_end_vpos < w->current_matrix->nrows);
15392
15393 /* Handle case where text has not changed, only point, and it has
15394 not moved off the frame. */
15395 if (/* Point may be in this window. */
15396 PT >= CHARPOS (startp)
15397 /* Selective display hasn't changed. */
15398 && !current_buffer->clip_changed
15399 /* Function force-mode-line-update is used to force a thorough
15400 redisplay. It sets either windows_or_buffers_changed or
15401 update_mode_lines. So don't take a shortcut here for these
15402 cases. */
15403 && !update_mode_lines
15404 && !windows_or_buffers_changed
15405 && !f->cursor_type_changed
15406 && NILP (Vshow_trailing_whitespace)
15407 /* This code is not used for mini-buffer for the sake of the case
15408 of redisplaying to replace an echo area message; since in
15409 that case the mini-buffer contents per se are usually
15410 unchanged. This code is of no real use in the mini-buffer
15411 since the handling of this_line_start_pos, etc., in redisplay
15412 handles the same cases. */
15413 && !EQ (window, minibuf_window)
15414 && (FRAME_WINDOW_P (f)
15415 || !overlay_arrow_in_current_buffer_p ()))
15416 {
15417 int this_scroll_margin, top_scroll_margin;
15418 struct glyph_row *row = NULL;
15419 int frame_line_height = default_line_pixel_height (w);
15420 int window_total_lines
15421 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15422
15423 #ifdef GLYPH_DEBUG
15424 debug_method_add (w, "cursor movement");
15425 #endif
15426
15427 /* Scroll if point within this distance from the top or bottom
15428 of the window. This is a pixel value. */
15429 if (scroll_margin > 0)
15430 {
15431 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15432 this_scroll_margin *= frame_line_height;
15433 }
15434 else
15435 this_scroll_margin = 0;
15436
15437 top_scroll_margin = this_scroll_margin;
15438 if (WINDOW_WANTS_HEADER_LINE_P (w))
15439 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15440
15441 /* Start with the row the cursor was displayed during the last
15442 not paused redisplay. Give up if that row is not valid. */
15443 if (w->last_cursor_vpos < 0
15444 || w->last_cursor_vpos >= w->current_matrix->nrows)
15445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15446 else
15447 {
15448 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15449 if (row->mode_line_p)
15450 ++row;
15451 if (!row->enabled_p)
15452 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15453 }
15454
15455 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15456 {
15457 int scroll_p = 0, must_scroll = 0;
15458 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15459
15460 if (PT > w->last_point)
15461 {
15462 /* Point has moved forward. */
15463 while (MATRIX_ROW_END_CHARPOS (row) < PT
15464 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15465 {
15466 eassert (row->enabled_p);
15467 ++row;
15468 }
15469
15470 /* If the end position of a row equals the start
15471 position of the next row, and PT is at that position,
15472 we would rather display cursor in the next line. */
15473 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15474 && MATRIX_ROW_END_CHARPOS (row) == PT
15475 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15476 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15477 && !cursor_row_p (row))
15478 ++row;
15479
15480 /* If within the scroll margin, scroll. Note that
15481 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15482 the next line would be drawn, and that
15483 this_scroll_margin can be zero. */
15484 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15485 || PT > MATRIX_ROW_END_CHARPOS (row)
15486 /* Line is completely visible last line in window
15487 and PT is to be set in the next line. */
15488 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15489 && PT == MATRIX_ROW_END_CHARPOS (row)
15490 && !row->ends_at_zv_p
15491 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15492 scroll_p = 1;
15493 }
15494 else if (PT < w->last_point)
15495 {
15496 /* Cursor has to be moved backward. Note that PT >=
15497 CHARPOS (startp) because of the outer if-statement. */
15498 while (!row->mode_line_p
15499 && (MATRIX_ROW_START_CHARPOS (row) > PT
15500 || (MATRIX_ROW_START_CHARPOS (row) == PT
15501 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15502 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15503 row > w->current_matrix->rows
15504 && (row-1)->ends_in_newline_from_string_p))))
15505 && (row->y > top_scroll_margin
15506 || CHARPOS (startp) == BEGV))
15507 {
15508 eassert (row->enabled_p);
15509 --row;
15510 }
15511
15512 /* Consider the following case: Window starts at BEGV,
15513 there is invisible, intangible text at BEGV, so that
15514 display starts at some point START > BEGV. It can
15515 happen that we are called with PT somewhere between
15516 BEGV and START. Try to handle that case. */
15517 if (row < w->current_matrix->rows
15518 || row->mode_line_p)
15519 {
15520 row = w->current_matrix->rows;
15521 if (row->mode_line_p)
15522 ++row;
15523 }
15524
15525 /* Due to newlines in overlay strings, we may have to
15526 skip forward over overlay strings. */
15527 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15528 && MATRIX_ROW_END_CHARPOS (row) == PT
15529 && !cursor_row_p (row))
15530 ++row;
15531
15532 /* If within the scroll margin, scroll. */
15533 if (row->y < top_scroll_margin
15534 && CHARPOS (startp) != BEGV)
15535 scroll_p = 1;
15536 }
15537 else
15538 {
15539 /* Cursor did not move. So don't scroll even if cursor line
15540 is partially visible, as it was so before. */
15541 rc = CURSOR_MOVEMENT_SUCCESS;
15542 }
15543
15544 if (PT < MATRIX_ROW_START_CHARPOS (row)
15545 || PT > MATRIX_ROW_END_CHARPOS (row))
15546 {
15547 /* if PT is not in the glyph row, give up. */
15548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15549 must_scroll = 1;
15550 }
15551 else if (rc != CURSOR_MOVEMENT_SUCCESS
15552 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15553 {
15554 struct glyph_row *row1;
15555
15556 /* If rows are bidi-reordered and point moved, back up
15557 until we find a row that does not belong to a
15558 continuation line. This is because we must consider
15559 all rows of a continued line as candidates for the
15560 new cursor positioning, since row start and end
15561 positions change non-linearly with vertical position
15562 in such rows. */
15563 /* FIXME: Revisit this when glyph ``spilling'' in
15564 continuation lines' rows is implemented for
15565 bidi-reordered rows. */
15566 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15567 MATRIX_ROW_CONTINUATION_LINE_P (row);
15568 --row)
15569 {
15570 /* If we hit the beginning of the displayed portion
15571 without finding the first row of a continued
15572 line, give up. */
15573 if (row <= row1)
15574 {
15575 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15576 break;
15577 }
15578 eassert (row->enabled_p);
15579 }
15580 }
15581 if (must_scroll)
15582 ;
15583 else if (rc != CURSOR_MOVEMENT_SUCCESS
15584 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15585 /* Make sure this isn't a header line by any chance, since
15586 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15587 && !row->mode_line_p
15588 && make_cursor_line_fully_visible_p)
15589 {
15590 if (PT == MATRIX_ROW_END_CHARPOS (row)
15591 && !row->ends_at_zv_p
15592 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15594 else if (row->height > window_box_height (w))
15595 {
15596 /* If we end up in a partially visible line, let's
15597 make it fully visible, except when it's taller
15598 than the window, in which case we can't do much
15599 about it. */
15600 *scroll_step = 1;
15601 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15602 }
15603 else
15604 {
15605 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15606 if (!cursor_row_fully_visible_p (w, 0, 1))
15607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15608 else
15609 rc = CURSOR_MOVEMENT_SUCCESS;
15610 }
15611 }
15612 else if (scroll_p)
15613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15614 else if (rc != CURSOR_MOVEMENT_SUCCESS
15615 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15616 {
15617 /* With bidi-reordered rows, there could be more than
15618 one candidate row whose start and end positions
15619 occlude point. We need to let set_cursor_from_row
15620 find the best candidate. */
15621 /* FIXME: Revisit this when glyph ``spilling'' in
15622 continuation lines' rows is implemented for
15623 bidi-reordered rows. */
15624 int rv = 0;
15625
15626 do
15627 {
15628 int at_zv_p = 0, exact_match_p = 0;
15629
15630 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15631 && PT <= MATRIX_ROW_END_CHARPOS (row)
15632 && cursor_row_p (row))
15633 rv |= set_cursor_from_row (w, row, w->current_matrix,
15634 0, 0, 0, 0);
15635 /* As soon as we've found the exact match for point,
15636 or the first suitable row whose ends_at_zv_p flag
15637 is set, we are done. */
15638 if (rv)
15639 {
15640 at_zv_p = MATRIX_ROW (w->current_matrix,
15641 w->cursor.vpos)->ends_at_zv_p;
15642 if (!at_zv_p
15643 && w->cursor.hpos >= 0
15644 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15645 w->cursor.vpos))
15646 {
15647 struct glyph_row *candidate =
15648 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15649 struct glyph *g =
15650 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15651 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15652
15653 exact_match_p =
15654 (BUFFERP (g->object) && g->charpos == PT)
15655 || (INTEGERP (g->object)
15656 && (g->charpos == PT
15657 || (g->charpos == 0 && endpos - 1 == PT)));
15658 }
15659 if (at_zv_p || exact_match_p)
15660 {
15661 rc = CURSOR_MOVEMENT_SUCCESS;
15662 break;
15663 }
15664 }
15665 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15666 break;
15667 ++row;
15668 }
15669 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15670 || row->continued_p)
15671 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15672 || (MATRIX_ROW_START_CHARPOS (row) == PT
15673 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15674 /* If we didn't find any candidate rows, or exited the
15675 loop before all the candidates were examined, signal
15676 to the caller that this method failed. */
15677 if (rc != CURSOR_MOVEMENT_SUCCESS
15678 && !(rv
15679 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15680 && !row->continued_p))
15681 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15682 else if (rv)
15683 rc = CURSOR_MOVEMENT_SUCCESS;
15684 }
15685 else
15686 {
15687 do
15688 {
15689 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15690 {
15691 rc = CURSOR_MOVEMENT_SUCCESS;
15692 break;
15693 }
15694 ++row;
15695 }
15696 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15697 && MATRIX_ROW_START_CHARPOS (row) == PT
15698 && cursor_row_p (row));
15699 }
15700 }
15701 }
15702
15703 return rc;
15704 }
15705
15706
15707 void
15708 set_vertical_scroll_bar (struct window *w)
15709 {
15710 ptrdiff_t start, end, whole;
15711
15712 /* Calculate the start and end positions for the current window.
15713 At some point, it would be nice to choose between scrollbars
15714 which reflect the whole buffer size, with special markers
15715 indicating narrowing, and scrollbars which reflect only the
15716 visible region.
15717
15718 Note that mini-buffers sometimes aren't displaying any text. */
15719 if (!MINI_WINDOW_P (w)
15720 || (w == XWINDOW (minibuf_window)
15721 && NILP (echo_area_buffer[0])))
15722 {
15723 struct buffer *buf = XBUFFER (w->contents);
15724 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15725 start = marker_position (w->start) - BUF_BEGV (buf);
15726 /* I don't think this is guaranteed to be right. For the
15727 moment, we'll pretend it is. */
15728 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15729
15730 if (end < start)
15731 end = start;
15732 if (whole < (end - start))
15733 whole = end - start;
15734 }
15735 else
15736 start = end = whole = 0;
15737
15738 /* Indicate what this scroll bar ought to be displaying now. */
15739 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15740 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15741 (w, end - start, whole, start);
15742 }
15743
15744
15745 void
15746 set_horizontal_scroll_bar (struct window *w)
15747 {
15748 int start, end, whole, portion;
15749
15750 if (!MINI_WINDOW_P (w)
15751 || (w == XWINDOW (minibuf_window)
15752 && NILP (echo_area_buffer[0])))
15753 {
15754 struct buffer *b = XBUFFER (w->contents);
15755 struct buffer *old_buffer = NULL;
15756 struct it it;
15757 struct text_pos startp;
15758
15759 if (b != current_buffer)
15760 {
15761 old_buffer = current_buffer;
15762 set_buffer_internal (b);
15763 }
15764
15765 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15766 start_display (&it, w, startp);
15767 it.last_visible_x = INT_MAX;
15768 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15769 MOVE_TO_X | MOVE_TO_Y);
15770 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15771 window_box_height (w), -1,
15772 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15773
15774 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15775 end = start + window_box_width (w, TEXT_AREA);
15776 portion = end - start;
15777 /* After enlarging a horizontally scrolled window such that it
15778 gets at least as wide as the text it contains, make sure that
15779 the thumb doesn't fill the entire scroll bar so we can still
15780 drag it back to see the entire text. */
15781 whole = max (whole, end);
15782
15783 if (it.bidi_p)
15784 {
15785 Lisp_Object pdir;
15786
15787 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15788 if (EQ (pdir, Qright_to_left))
15789 {
15790 start = whole - end;
15791 end = start + portion;
15792 }
15793 }
15794
15795 if (old_buffer)
15796 set_buffer_internal (old_buffer);
15797 }
15798 else
15799 start = end = whole = portion = 0;
15800
15801 w->hscroll_whole = whole;
15802
15803 /* Indicate what this scroll bar ought to be displaying now. */
15804 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15805 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15806 (w, portion, whole, start);
15807 }
15808
15809
15810 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15811 selected_window is redisplayed.
15812
15813 We can return without actually redisplaying the window if fonts has been
15814 changed on window's frame. In that case, redisplay_internal will retry.
15815
15816 As one of the important parts of redisplaying a window, we need to
15817 decide whether the previous window-start position (stored in the
15818 window's w->start marker position) is still valid, and if it isn't,
15819 recompute it. Some details about that:
15820
15821 . The previous window-start could be in a continuation line, in
15822 which case we need to recompute it when the window width
15823 changes. See compute_window_start_on_continuation_line and its
15824 call below.
15825
15826 . The text that changed since last redisplay could include the
15827 previous window-start position. In that case, we try to salvage
15828 what we can from the current glyph matrix by calling
15829 try_scrolling, which see.
15830
15831 . Some Emacs command could force us to use a specific window-start
15832 position by setting the window's force_start flag, or gently
15833 propose doing that by setting the window's optional_new_start
15834 flag. In these cases, we try using the specified start point if
15835 that succeeds (i.e. the window desired matrix is successfully
15836 recomputed, and point location is within the window). In case
15837 of optional_new_start, we first check if the specified start
15838 position is feasible, i.e. if it will allow point to be
15839 displayed in the window. If using the specified start point
15840 fails, e.g., if new fonts are needed to be loaded, we abort the
15841 redisplay cycle and leave it up to the next cycle to figure out
15842 things.
15843
15844 . Note that the window's force_start flag is sometimes set by
15845 redisplay itself, when it decides that the previous window start
15846 point is fine and should be kept. Search for "goto force_start"
15847 below to see the details. Like the values of window-start
15848 specified outside of redisplay, these internally-deduced values
15849 are tested for feasibility, and ignored if found to be
15850 unfeasible.
15851
15852 . Note that the function try_window, used to completely redisplay
15853 a window, accepts the window's start point as its argument.
15854 This is used several times in the redisplay code to control
15855 where the window start will be, according to user options such
15856 as scroll-conservatively, and also to ensure the screen line
15857 showing point will be fully (as opposed to partially) visible on
15858 display. */
15859
15860 static void
15861 redisplay_window (Lisp_Object window, bool just_this_one_p)
15862 {
15863 struct window *w = XWINDOW (window);
15864 struct frame *f = XFRAME (w->frame);
15865 struct buffer *buffer = XBUFFER (w->contents);
15866 struct buffer *old = current_buffer;
15867 struct text_pos lpoint, opoint, startp;
15868 int update_mode_line;
15869 int tem;
15870 struct it it;
15871 /* Record it now because it's overwritten. */
15872 bool current_matrix_up_to_date_p = false;
15873 bool used_current_matrix_p = false;
15874 /* This is less strict than current_matrix_up_to_date_p.
15875 It indicates that the buffer contents and narrowing are unchanged. */
15876 bool buffer_unchanged_p = false;
15877 int temp_scroll_step = 0;
15878 ptrdiff_t count = SPECPDL_INDEX ();
15879 int rc;
15880 int centering_position = -1;
15881 int last_line_misfit = 0;
15882 ptrdiff_t beg_unchanged, end_unchanged;
15883 int frame_line_height;
15884
15885 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15886 opoint = lpoint;
15887
15888 #ifdef GLYPH_DEBUG
15889 *w->desired_matrix->method = 0;
15890 #endif
15891
15892 if (!just_this_one_p
15893 && REDISPLAY_SOME_P ()
15894 && !w->redisplay
15895 && !f->redisplay
15896 && !buffer->text->redisplay
15897 && BUF_PT (buffer) == w->last_point)
15898 return;
15899
15900 /* Make sure that both W's markers are valid. */
15901 eassert (XMARKER (w->start)->buffer == buffer);
15902 eassert (XMARKER (w->pointm)->buffer == buffer);
15903
15904 /* We come here again if we need to run window-text-change-functions
15905 below. */
15906 restart:
15907 reconsider_clip_changes (w);
15908 frame_line_height = default_line_pixel_height (w);
15909
15910 /* Has the mode line to be updated? */
15911 update_mode_line = (w->update_mode_line
15912 || update_mode_lines
15913 || buffer->clip_changed
15914 || buffer->prevent_redisplay_optimizations_p);
15915
15916 if (!just_this_one_p)
15917 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15918 cleverly elsewhere. */
15919 w->must_be_updated_p = true;
15920
15921 if (MINI_WINDOW_P (w))
15922 {
15923 if (w == XWINDOW (echo_area_window)
15924 && !NILP (echo_area_buffer[0]))
15925 {
15926 if (update_mode_line)
15927 /* We may have to update a tty frame's menu bar or a
15928 tool-bar. Example `M-x C-h C-h C-g'. */
15929 goto finish_menu_bars;
15930 else
15931 /* We've already displayed the echo area glyphs in this window. */
15932 goto finish_scroll_bars;
15933 }
15934 else if ((w != XWINDOW (minibuf_window)
15935 || minibuf_level == 0)
15936 /* When buffer is nonempty, redisplay window normally. */
15937 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15938 /* Quail displays non-mini buffers in minibuffer window.
15939 In that case, redisplay the window normally. */
15940 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15941 {
15942 /* W is a mini-buffer window, but it's not active, so clear
15943 it. */
15944 int yb = window_text_bottom_y (w);
15945 struct glyph_row *row;
15946 int y;
15947
15948 for (y = 0, row = w->desired_matrix->rows;
15949 y < yb;
15950 y += row->height, ++row)
15951 blank_row (w, row, y);
15952 goto finish_scroll_bars;
15953 }
15954
15955 clear_glyph_matrix (w->desired_matrix);
15956 }
15957
15958 /* Otherwise set up data on this window; select its buffer and point
15959 value. */
15960 /* Really select the buffer, for the sake of buffer-local
15961 variables. */
15962 set_buffer_internal_1 (XBUFFER (w->contents));
15963
15964 current_matrix_up_to_date_p
15965 = (w->window_end_valid
15966 && !current_buffer->clip_changed
15967 && !current_buffer->prevent_redisplay_optimizations_p
15968 && !window_outdated (w));
15969
15970 /* Run the window-text-change-functions
15971 if it is possible that the text on the screen has changed
15972 (either due to modification of the text, or any other reason). */
15973 if (!current_matrix_up_to_date_p
15974 && !NILP (Vwindow_text_change_functions))
15975 {
15976 safe_run_hooks (Qwindow_text_change_functions);
15977 goto restart;
15978 }
15979
15980 beg_unchanged = BEG_UNCHANGED;
15981 end_unchanged = END_UNCHANGED;
15982
15983 SET_TEXT_POS (opoint, PT, PT_BYTE);
15984
15985 specbind (Qinhibit_point_motion_hooks, Qt);
15986
15987 buffer_unchanged_p
15988 = (w->window_end_valid
15989 && !current_buffer->clip_changed
15990 && !window_outdated (w));
15991
15992 /* When windows_or_buffers_changed is non-zero, we can't rely
15993 on the window end being valid, so set it to zero there. */
15994 if (windows_or_buffers_changed)
15995 {
15996 /* If window starts on a continuation line, maybe adjust the
15997 window start in case the window's width changed. */
15998 if (XMARKER (w->start)->buffer == current_buffer)
15999 compute_window_start_on_continuation_line (w);
16000
16001 w->window_end_valid = false;
16002 /* If so, we also can't rely on current matrix
16003 and should not fool try_cursor_movement below. */
16004 current_matrix_up_to_date_p = false;
16005 }
16006
16007 /* Some sanity checks. */
16008 CHECK_WINDOW_END (w);
16009 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16010 emacs_abort ();
16011 if (BYTEPOS (opoint) < CHARPOS (opoint))
16012 emacs_abort ();
16013
16014 if (mode_line_update_needed (w))
16015 update_mode_line = 1;
16016
16017 /* Point refers normally to the selected window. For any other
16018 window, set up appropriate value. */
16019 if (!EQ (window, selected_window))
16020 {
16021 ptrdiff_t new_pt = marker_position (w->pointm);
16022 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16023
16024 if (new_pt < BEGV)
16025 {
16026 new_pt = BEGV;
16027 new_pt_byte = BEGV_BYTE;
16028 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16029 }
16030 else if (new_pt > (ZV - 1))
16031 {
16032 new_pt = ZV;
16033 new_pt_byte = ZV_BYTE;
16034 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16035 }
16036
16037 /* We don't use SET_PT so that the point-motion hooks don't run. */
16038 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16039 }
16040
16041 /* If any of the character widths specified in the display table
16042 have changed, invalidate the width run cache. It's true that
16043 this may be a bit late to catch such changes, but the rest of
16044 redisplay goes (non-fatally) haywire when the display table is
16045 changed, so why should we worry about doing any better? */
16046 if (current_buffer->width_run_cache
16047 || (current_buffer->base_buffer
16048 && current_buffer->base_buffer->width_run_cache))
16049 {
16050 struct Lisp_Char_Table *disptab = buffer_display_table ();
16051
16052 if (! disptab_matches_widthtab
16053 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16054 {
16055 struct buffer *buf = current_buffer;
16056
16057 if (buf->base_buffer)
16058 buf = buf->base_buffer;
16059 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16060 recompute_width_table (current_buffer, disptab);
16061 }
16062 }
16063
16064 /* If window-start is screwed up, choose a new one. */
16065 if (XMARKER (w->start)->buffer != current_buffer)
16066 goto recenter;
16067
16068 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16069
16070 /* If someone specified a new starting point but did not insist,
16071 check whether it can be used. */
16072 if ((w->optional_new_start || window_frozen_p (w))
16073 && CHARPOS (startp) >= BEGV
16074 && CHARPOS (startp) <= ZV)
16075 {
16076 ptrdiff_t it_charpos;
16077
16078 w->optional_new_start = 0;
16079 start_display (&it, w, startp);
16080 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16081 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16082 /* Record IT's position now, since line_bottom_y might change
16083 that. */
16084 it_charpos = IT_CHARPOS (it);
16085 /* Make sure we set the force_start flag only if the cursor row
16086 will be fully visible. Otherwise, the code under force_start
16087 label below will try to move point back into view, which is
16088 not what the code which sets optional_new_start wants. */
16089 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16090 && !w->force_start)
16091 {
16092 if (it_charpos == PT)
16093 w->force_start = 1;
16094 /* IT may overshoot PT if text at PT is invisible. */
16095 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16096 w->force_start = 1;
16097 #ifdef GLYPH_DEBUG
16098 if (w->force_start)
16099 {
16100 if (window_frozen_p (w))
16101 debug_method_add (w, "set force_start from frozen window start");
16102 else
16103 debug_method_add (w, "set force_start from optional_new_start");
16104 }
16105 #endif
16106 }
16107 }
16108
16109 force_start:
16110
16111 /* Handle case where place to start displaying has been specified,
16112 unless the specified location is outside the accessible range. */
16113 if (w->force_start)
16114 {
16115 /* We set this later on if we have to adjust point. */
16116 int new_vpos = -1;
16117
16118 w->force_start = 0;
16119 w->vscroll = 0;
16120 w->window_end_valid = 0;
16121
16122 /* Forget any recorded base line for line number display. */
16123 if (!buffer_unchanged_p)
16124 w->base_line_number = 0;
16125
16126 /* Redisplay the mode line. Select the buffer properly for that.
16127 Also, run the hook window-scroll-functions
16128 because we have scrolled. */
16129 /* Note, we do this after clearing force_start because
16130 if there's an error, it is better to forget about force_start
16131 than to get into an infinite loop calling the hook functions
16132 and having them get more errors. */
16133 if (!update_mode_line
16134 || ! NILP (Vwindow_scroll_functions))
16135 {
16136 update_mode_line = 1;
16137 w->update_mode_line = 1;
16138 startp = run_window_scroll_functions (window, startp);
16139 }
16140
16141 if (CHARPOS (startp) < BEGV)
16142 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16143 else if (CHARPOS (startp) > ZV)
16144 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16145
16146 /* Redisplay, then check if cursor has been set during the
16147 redisplay. Give up if new fonts were loaded. */
16148 /* We used to issue a CHECK_MARGINS argument to try_window here,
16149 but this causes scrolling to fail when point begins inside
16150 the scroll margin (bug#148) -- cyd */
16151 if (!try_window (window, startp, 0))
16152 {
16153 w->force_start = 1;
16154 clear_glyph_matrix (w->desired_matrix);
16155 goto need_larger_matrices;
16156 }
16157
16158 if (w->cursor.vpos < 0)
16159 {
16160 /* If point does not appear, try to move point so it does
16161 appear. The desired matrix has been built above, so we
16162 can use it here. */
16163 new_vpos = window_box_height (w) / 2;
16164 }
16165
16166 if (!cursor_row_fully_visible_p (w, 0, 0))
16167 {
16168 /* Point does appear, but on a line partly visible at end of window.
16169 Move it back to a fully-visible line. */
16170 new_vpos = window_box_height (w);
16171 /* But if window_box_height suggests a Y coordinate that is
16172 not less than we already have, that line will clearly not
16173 be fully visible, so give up and scroll the display.
16174 This can happen when the default face uses a font whose
16175 dimensions are different from the frame's default
16176 font. */
16177 if (new_vpos >= w->cursor.y)
16178 {
16179 w->cursor.vpos = -1;
16180 clear_glyph_matrix (w->desired_matrix);
16181 goto try_to_scroll;
16182 }
16183 }
16184 else if (w->cursor.vpos >= 0)
16185 {
16186 /* Some people insist on not letting point enter the scroll
16187 margin, even though this part handles windows that didn't
16188 scroll at all. */
16189 int window_total_lines
16190 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16191 int margin = min (scroll_margin, window_total_lines / 4);
16192 int pixel_margin = margin * frame_line_height;
16193 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16194
16195 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16196 below, which finds the row to move point to, advances by
16197 the Y coordinate of the _next_ row, see the definition of
16198 MATRIX_ROW_BOTTOM_Y. */
16199 if (w->cursor.vpos < margin + header_line)
16200 {
16201 w->cursor.vpos = -1;
16202 clear_glyph_matrix (w->desired_matrix);
16203 goto try_to_scroll;
16204 }
16205 else
16206 {
16207 int window_height = window_box_height (w);
16208
16209 if (header_line)
16210 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16211 if (w->cursor.y >= window_height - pixel_margin)
16212 {
16213 w->cursor.vpos = -1;
16214 clear_glyph_matrix (w->desired_matrix);
16215 goto try_to_scroll;
16216 }
16217 }
16218 }
16219
16220 /* If we need to move point for either of the above reasons,
16221 now actually do it. */
16222 if (new_vpos >= 0)
16223 {
16224 struct glyph_row *row;
16225
16226 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16227 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16228 ++row;
16229
16230 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16231 MATRIX_ROW_START_BYTEPOS (row));
16232
16233 if (w != XWINDOW (selected_window))
16234 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16235 else if (current_buffer == old)
16236 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16237
16238 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16239
16240 /* Re-run pre-redisplay-function so it can update the region
16241 according to the new position of point. */
16242 /* Other than the cursor, w's redisplay is done so we can set its
16243 redisplay to false. Also the buffer's redisplay can be set to
16244 false, since propagate_buffer_redisplay should have already
16245 propagated its info to `w' anyway. */
16246 w->redisplay = false;
16247 XBUFFER (w->contents)->text->redisplay = false;
16248 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16249
16250 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16251 {
16252 /* pre-redisplay-function made changes (e.g. move the region)
16253 that require another round of redisplay. */
16254 clear_glyph_matrix (w->desired_matrix);
16255 if (!try_window (window, startp, 0))
16256 goto need_larger_matrices;
16257 }
16258 }
16259 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16260 {
16261 clear_glyph_matrix (w->desired_matrix);
16262 goto try_to_scroll;
16263 }
16264
16265 #ifdef GLYPH_DEBUG
16266 debug_method_add (w, "forced window start");
16267 #endif
16268 goto done;
16269 }
16270
16271 /* Handle case where text has not changed, only point, and it has
16272 not moved off the frame, and we are not retrying after hscroll.
16273 (current_matrix_up_to_date_p is nonzero when retrying.) */
16274 if (current_matrix_up_to_date_p
16275 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16276 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16277 {
16278 switch (rc)
16279 {
16280 case CURSOR_MOVEMENT_SUCCESS:
16281 used_current_matrix_p = 1;
16282 goto done;
16283
16284 case CURSOR_MOVEMENT_MUST_SCROLL:
16285 goto try_to_scroll;
16286
16287 default:
16288 emacs_abort ();
16289 }
16290 }
16291 /* If current starting point was originally the beginning of a line
16292 but no longer is, find a new starting point. */
16293 else if (w->start_at_line_beg
16294 && !(CHARPOS (startp) <= BEGV
16295 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16296 {
16297 #ifdef GLYPH_DEBUG
16298 debug_method_add (w, "recenter 1");
16299 #endif
16300 goto recenter;
16301 }
16302
16303 /* Try scrolling with try_window_id. Value is > 0 if update has
16304 been done, it is -1 if we know that the same window start will
16305 not work. It is 0 if unsuccessful for some other reason. */
16306 else if ((tem = try_window_id (w)) != 0)
16307 {
16308 #ifdef GLYPH_DEBUG
16309 debug_method_add (w, "try_window_id %d", tem);
16310 #endif
16311
16312 if (f->fonts_changed)
16313 goto need_larger_matrices;
16314 if (tem > 0)
16315 goto done;
16316
16317 /* Otherwise try_window_id has returned -1 which means that we
16318 don't want the alternative below this comment to execute. */
16319 }
16320 else if (CHARPOS (startp) >= BEGV
16321 && CHARPOS (startp) <= ZV
16322 && PT >= CHARPOS (startp)
16323 && (CHARPOS (startp) < ZV
16324 /* Avoid starting at end of buffer. */
16325 || CHARPOS (startp) == BEGV
16326 || !window_outdated (w)))
16327 {
16328 int d1, d2, d5, d6;
16329 int rtop, rbot;
16330
16331 /* If first window line is a continuation line, and window start
16332 is inside the modified region, but the first change is before
16333 current window start, we must select a new window start.
16334
16335 However, if this is the result of a down-mouse event (e.g. by
16336 extending the mouse-drag-overlay), we don't want to select a
16337 new window start, since that would change the position under
16338 the mouse, resulting in an unwanted mouse-movement rather
16339 than a simple mouse-click. */
16340 if (!w->start_at_line_beg
16341 && NILP (do_mouse_tracking)
16342 && CHARPOS (startp) > BEGV
16343 && CHARPOS (startp) > BEG + beg_unchanged
16344 && CHARPOS (startp) <= Z - end_unchanged
16345 /* Even if w->start_at_line_beg is nil, a new window may
16346 start at a line_beg, since that's how set_buffer_window
16347 sets it. So, we need to check the return value of
16348 compute_window_start_on_continuation_line. (See also
16349 bug#197). */
16350 && XMARKER (w->start)->buffer == current_buffer
16351 && compute_window_start_on_continuation_line (w)
16352 /* It doesn't make sense to force the window start like we
16353 do at label force_start if it is already known that point
16354 will not be fully visible in the resulting window, because
16355 doing so will move point from its correct position
16356 instead of scrolling the window to bring point into view.
16357 See bug#9324. */
16358 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16359 /* A very tall row could need more than the window height,
16360 in which case we accept that it is partially visible. */
16361 && (rtop != 0) == (rbot != 0))
16362 {
16363 w->force_start = 1;
16364 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16365 #ifdef GLYPH_DEBUG
16366 debug_method_add (w, "recomputed window start in continuation line");
16367 #endif
16368 goto force_start;
16369 }
16370
16371 #ifdef GLYPH_DEBUG
16372 debug_method_add (w, "same window start");
16373 #endif
16374
16375 /* Try to redisplay starting at same place as before.
16376 If point has not moved off frame, accept the results. */
16377 if (!current_matrix_up_to_date_p
16378 /* Don't use try_window_reusing_current_matrix in this case
16379 because a window scroll function can have changed the
16380 buffer. */
16381 || !NILP (Vwindow_scroll_functions)
16382 || MINI_WINDOW_P (w)
16383 || !(used_current_matrix_p
16384 = try_window_reusing_current_matrix (w)))
16385 {
16386 IF_DEBUG (debug_method_add (w, "1"));
16387 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16388 /* -1 means we need to scroll.
16389 0 means we need new matrices, but fonts_changed
16390 is set in that case, so we will detect it below. */
16391 goto try_to_scroll;
16392 }
16393
16394 if (f->fonts_changed)
16395 goto need_larger_matrices;
16396
16397 if (w->cursor.vpos >= 0)
16398 {
16399 if (!just_this_one_p
16400 || current_buffer->clip_changed
16401 || BEG_UNCHANGED < CHARPOS (startp))
16402 /* Forget any recorded base line for line number display. */
16403 w->base_line_number = 0;
16404
16405 if (!cursor_row_fully_visible_p (w, 1, 0))
16406 {
16407 clear_glyph_matrix (w->desired_matrix);
16408 last_line_misfit = 1;
16409 }
16410 /* Drop through and scroll. */
16411 else
16412 goto done;
16413 }
16414 else
16415 clear_glyph_matrix (w->desired_matrix);
16416 }
16417
16418 try_to_scroll:
16419
16420 /* Redisplay the mode line. Select the buffer properly for that. */
16421 if (!update_mode_line)
16422 {
16423 update_mode_line = 1;
16424 w->update_mode_line = 1;
16425 }
16426
16427 /* Try to scroll by specified few lines. */
16428 if ((scroll_conservatively
16429 || emacs_scroll_step
16430 || temp_scroll_step
16431 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16432 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16433 && CHARPOS (startp) >= BEGV
16434 && CHARPOS (startp) <= ZV)
16435 {
16436 /* The function returns -1 if new fonts were loaded, 1 if
16437 successful, 0 if not successful. */
16438 int ss = try_scrolling (window, just_this_one_p,
16439 scroll_conservatively,
16440 emacs_scroll_step,
16441 temp_scroll_step, last_line_misfit);
16442 switch (ss)
16443 {
16444 case SCROLLING_SUCCESS:
16445 goto done;
16446
16447 case SCROLLING_NEED_LARGER_MATRICES:
16448 goto need_larger_matrices;
16449
16450 case SCROLLING_FAILED:
16451 break;
16452
16453 default:
16454 emacs_abort ();
16455 }
16456 }
16457
16458 /* Finally, just choose a place to start which positions point
16459 according to user preferences. */
16460
16461 recenter:
16462
16463 #ifdef GLYPH_DEBUG
16464 debug_method_add (w, "recenter");
16465 #endif
16466
16467 /* Forget any previously recorded base line for line number display. */
16468 if (!buffer_unchanged_p)
16469 w->base_line_number = 0;
16470
16471 /* Determine the window start relative to point. */
16472 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16473 it.current_y = it.last_visible_y;
16474 if (centering_position < 0)
16475 {
16476 int window_total_lines
16477 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16478 int margin
16479 = scroll_margin > 0
16480 ? min (scroll_margin, window_total_lines / 4)
16481 : 0;
16482 ptrdiff_t margin_pos = CHARPOS (startp);
16483 Lisp_Object aggressive;
16484 int scrolling_up;
16485
16486 /* If there is a scroll margin at the top of the window, find
16487 its character position. */
16488 if (margin
16489 /* Cannot call start_display if startp is not in the
16490 accessible region of the buffer. This can happen when we
16491 have just switched to a different buffer and/or changed
16492 its restriction. In that case, startp is initialized to
16493 the character position 1 (BEGV) because we did not yet
16494 have chance to display the buffer even once. */
16495 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16496 {
16497 struct it it1;
16498 void *it1data = NULL;
16499
16500 SAVE_IT (it1, it, it1data);
16501 start_display (&it1, w, startp);
16502 move_it_vertically (&it1, margin * frame_line_height);
16503 margin_pos = IT_CHARPOS (it1);
16504 RESTORE_IT (&it, &it, it1data);
16505 }
16506 scrolling_up = PT > margin_pos;
16507 aggressive =
16508 scrolling_up
16509 ? BVAR (current_buffer, scroll_up_aggressively)
16510 : BVAR (current_buffer, scroll_down_aggressively);
16511
16512 if (!MINI_WINDOW_P (w)
16513 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16514 {
16515 int pt_offset = 0;
16516
16517 /* Setting scroll-conservatively overrides
16518 scroll-*-aggressively. */
16519 if (!scroll_conservatively && NUMBERP (aggressive))
16520 {
16521 double float_amount = XFLOATINT (aggressive);
16522
16523 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16524 if (pt_offset == 0 && float_amount > 0)
16525 pt_offset = 1;
16526 if (pt_offset && margin > 0)
16527 margin -= 1;
16528 }
16529 /* Compute how much to move the window start backward from
16530 point so that point will be displayed where the user
16531 wants it. */
16532 if (scrolling_up)
16533 {
16534 centering_position = it.last_visible_y;
16535 if (pt_offset)
16536 centering_position -= pt_offset;
16537 centering_position -=
16538 frame_line_height * (1 + margin + (last_line_misfit != 0))
16539 + WINDOW_HEADER_LINE_HEIGHT (w);
16540 /* Don't let point enter the scroll margin near top of
16541 the window. */
16542 if (centering_position < margin * frame_line_height)
16543 centering_position = margin * frame_line_height;
16544 }
16545 else
16546 centering_position = margin * frame_line_height + pt_offset;
16547 }
16548 else
16549 /* Set the window start half the height of the window backward
16550 from point. */
16551 centering_position = window_box_height (w) / 2;
16552 }
16553 move_it_vertically_backward (&it, centering_position);
16554
16555 eassert (IT_CHARPOS (it) >= BEGV);
16556
16557 /* The function move_it_vertically_backward may move over more
16558 than the specified y-distance. If it->w is small, e.g. a
16559 mini-buffer window, we may end up in front of the window's
16560 display area. Start displaying at the start of the line
16561 containing PT in this case. */
16562 if (it.current_y <= 0)
16563 {
16564 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16565 move_it_vertically_backward (&it, 0);
16566 it.current_y = 0;
16567 }
16568
16569 it.current_x = it.hpos = 0;
16570
16571 /* Set the window start position here explicitly, to avoid an
16572 infinite loop in case the functions in window-scroll-functions
16573 get errors. */
16574 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16575
16576 /* Run scroll hooks. */
16577 startp = run_window_scroll_functions (window, it.current.pos);
16578
16579 /* Redisplay the window. */
16580 if (!current_matrix_up_to_date_p
16581 || windows_or_buffers_changed
16582 || f->cursor_type_changed
16583 /* Don't use try_window_reusing_current_matrix in this case
16584 because it can have changed the buffer. */
16585 || !NILP (Vwindow_scroll_functions)
16586 || !just_this_one_p
16587 || MINI_WINDOW_P (w)
16588 || !(used_current_matrix_p
16589 = try_window_reusing_current_matrix (w)))
16590 try_window (window, startp, 0);
16591
16592 /* If new fonts have been loaded (due to fontsets), give up. We
16593 have to start a new redisplay since we need to re-adjust glyph
16594 matrices. */
16595 if (f->fonts_changed)
16596 goto need_larger_matrices;
16597
16598 /* If cursor did not appear assume that the middle of the window is
16599 in the first line of the window. Do it again with the next line.
16600 (Imagine a window of height 100, displaying two lines of height
16601 60. Moving back 50 from it->last_visible_y will end in the first
16602 line.) */
16603 if (w->cursor.vpos < 0)
16604 {
16605 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16606 {
16607 clear_glyph_matrix (w->desired_matrix);
16608 move_it_by_lines (&it, 1);
16609 try_window (window, it.current.pos, 0);
16610 }
16611 else if (PT < IT_CHARPOS (it))
16612 {
16613 clear_glyph_matrix (w->desired_matrix);
16614 move_it_by_lines (&it, -1);
16615 try_window (window, it.current.pos, 0);
16616 }
16617 else
16618 {
16619 /* Not much we can do about it. */
16620 }
16621 }
16622
16623 /* Consider the following case: Window starts at BEGV, there is
16624 invisible, intangible text at BEGV, so that display starts at
16625 some point START > BEGV. It can happen that we are called with
16626 PT somewhere between BEGV and START. Try to handle that case,
16627 and similar ones. */
16628 if (w->cursor.vpos < 0)
16629 {
16630 /* First, try locating the proper glyph row for PT. */
16631 struct glyph_row *row =
16632 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16633
16634 /* Sometimes point is at the beginning of invisible text that is
16635 before the 1st character displayed in the row. In that case,
16636 row_containing_pos fails to find the row, because no glyphs
16637 with appropriate buffer positions are present in the row.
16638 Therefore, we next try to find the row which shows the 1st
16639 position after the invisible text. */
16640 if (!row)
16641 {
16642 Lisp_Object val =
16643 get_char_property_and_overlay (make_number (PT), Qinvisible,
16644 Qnil, NULL);
16645
16646 if (TEXT_PROP_MEANS_INVISIBLE (val))
16647 {
16648 ptrdiff_t alt_pos;
16649 Lisp_Object invis_end =
16650 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16651 Qnil, Qnil);
16652
16653 if (NATNUMP (invis_end))
16654 alt_pos = XFASTINT (invis_end);
16655 else
16656 alt_pos = ZV;
16657 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16658 NULL, 0);
16659 }
16660 }
16661 /* Finally, fall back on the first row of the window after the
16662 header line (if any). This is slightly better than not
16663 displaying the cursor at all. */
16664 if (!row)
16665 {
16666 row = w->current_matrix->rows;
16667 if (row->mode_line_p)
16668 ++row;
16669 }
16670 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16671 }
16672
16673 if (!cursor_row_fully_visible_p (w, 0, 0))
16674 {
16675 /* If vscroll is enabled, disable it and try again. */
16676 if (w->vscroll)
16677 {
16678 w->vscroll = 0;
16679 clear_glyph_matrix (w->desired_matrix);
16680 goto recenter;
16681 }
16682
16683 /* Users who set scroll-conservatively to a large number want
16684 point just above/below the scroll margin. If we ended up
16685 with point's row partially visible, move the window start to
16686 make that row fully visible and out of the margin. */
16687 if (scroll_conservatively > SCROLL_LIMIT)
16688 {
16689 int window_total_lines
16690 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16691 int margin =
16692 scroll_margin > 0
16693 ? min (scroll_margin, window_total_lines / 4)
16694 : 0;
16695 int move_down = w->cursor.vpos >= window_total_lines / 2;
16696
16697 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16698 clear_glyph_matrix (w->desired_matrix);
16699 if (1 == try_window (window, it.current.pos,
16700 TRY_WINDOW_CHECK_MARGINS))
16701 goto done;
16702 }
16703
16704 /* If centering point failed to make the whole line visible,
16705 put point at the top instead. That has to make the whole line
16706 visible, if it can be done. */
16707 if (centering_position == 0)
16708 goto done;
16709
16710 clear_glyph_matrix (w->desired_matrix);
16711 centering_position = 0;
16712 goto recenter;
16713 }
16714
16715 done:
16716
16717 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16718 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16719 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16720
16721 /* Display the mode line, if we must. */
16722 if ((update_mode_line
16723 /* If window not full width, must redo its mode line
16724 if (a) the window to its side is being redone and
16725 (b) we do a frame-based redisplay. This is a consequence
16726 of how inverted lines are drawn in frame-based redisplay. */
16727 || (!just_this_one_p
16728 && !FRAME_WINDOW_P (f)
16729 && !WINDOW_FULL_WIDTH_P (w))
16730 /* Line number to display. */
16731 || w->base_line_pos > 0
16732 /* Column number is displayed and different from the one displayed. */
16733 || (w->column_number_displayed != -1
16734 && (w->column_number_displayed != current_column ())))
16735 /* This means that the window has a mode line. */
16736 && (WINDOW_WANTS_MODELINE_P (w)
16737 || WINDOW_WANTS_HEADER_LINE_P (w)))
16738 {
16739
16740 display_mode_lines (w);
16741
16742 /* If mode line height has changed, arrange for a thorough
16743 immediate redisplay using the correct mode line height. */
16744 if (WINDOW_WANTS_MODELINE_P (w)
16745 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16746 {
16747 f->fonts_changed = 1;
16748 w->mode_line_height = -1;
16749 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16750 = DESIRED_MODE_LINE_HEIGHT (w);
16751 }
16752
16753 /* If header line height has changed, arrange for a thorough
16754 immediate redisplay using the correct header line height. */
16755 if (WINDOW_WANTS_HEADER_LINE_P (w)
16756 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16757 {
16758 f->fonts_changed = 1;
16759 w->header_line_height = -1;
16760 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16761 = DESIRED_HEADER_LINE_HEIGHT (w);
16762 }
16763
16764 if (f->fonts_changed)
16765 goto need_larger_matrices;
16766 }
16767
16768 if (!line_number_displayed && w->base_line_pos != -1)
16769 {
16770 w->base_line_pos = 0;
16771 w->base_line_number = 0;
16772 }
16773
16774 finish_menu_bars:
16775
16776 /* When we reach a frame's selected window, redo the frame's menu bar. */
16777 if (update_mode_line
16778 && EQ (FRAME_SELECTED_WINDOW (f), window))
16779 {
16780 int redisplay_menu_p = 0;
16781
16782 if (FRAME_WINDOW_P (f))
16783 {
16784 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16785 || defined (HAVE_NS) || defined (USE_GTK)
16786 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16787 #else
16788 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16789 #endif
16790 }
16791 else
16792 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16793
16794 if (redisplay_menu_p)
16795 display_menu_bar (w);
16796
16797 #ifdef HAVE_WINDOW_SYSTEM
16798 if (FRAME_WINDOW_P (f))
16799 {
16800 #if defined (USE_GTK) || defined (HAVE_NS)
16801 if (FRAME_EXTERNAL_TOOL_BAR (f))
16802 redisplay_tool_bar (f);
16803 #else
16804 if (WINDOWP (f->tool_bar_window)
16805 && (FRAME_TOOL_BAR_LINES (f) > 0
16806 || !NILP (Vauto_resize_tool_bars))
16807 && redisplay_tool_bar (f))
16808 ignore_mouse_drag_p = 1;
16809 #endif
16810 }
16811 #endif
16812 }
16813
16814 #ifdef HAVE_WINDOW_SYSTEM
16815 if (FRAME_WINDOW_P (f)
16816 && update_window_fringes (w, (just_this_one_p
16817 || (!used_current_matrix_p && !overlay_arrow_seen)
16818 || w->pseudo_window_p)))
16819 {
16820 update_begin (f);
16821 block_input ();
16822 if (draw_window_fringes (w, 1))
16823 {
16824 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16825 x_draw_right_divider (w);
16826 else
16827 x_draw_vertical_border (w);
16828 }
16829 unblock_input ();
16830 update_end (f);
16831 }
16832
16833 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16834 x_draw_bottom_divider (w);
16835 #endif /* HAVE_WINDOW_SYSTEM */
16836
16837 /* We go to this label, with fonts_changed set, if it is
16838 necessary to try again using larger glyph matrices.
16839 We have to redeem the scroll bar even in this case,
16840 because the loop in redisplay_internal expects that. */
16841 need_larger_matrices:
16842 ;
16843 finish_scroll_bars:
16844
16845 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16846 {
16847 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16848 /* Set the thumb's position and size. */
16849 set_vertical_scroll_bar (w);
16850
16851 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16852 /* Set the thumb's position and size. */
16853 set_horizontal_scroll_bar (w);
16854
16855 /* Note that we actually used the scroll bar attached to this
16856 window, so it shouldn't be deleted at the end of redisplay. */
16857 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16858 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16859 }
16860
16861 /* Restore current_buffer and value of point in it. The window
16862 update may have changed the buffer, so first make sure `opoint'
16863 is still valid (Bug#6177). */
16864 if (CHARPOS (opoint) < BEGV)
16865 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16866 else if (CHARPOS (opoint) > ZV)
16867 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16868 else
16869 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16870
16871 set_buffer_internal_1 (old);
16872 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16873 shorter. This can be caused by log truncation in *Messages*. */
16874 if (CHARPOS (lpoint) <= ZV)
16875 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16876
16877 unbind_to (count, Qnil);
16878 }
16879
16880
16881 /* Build the complete desired matrix of WINDOW with a window start
16882 buffer position POS.
16883
16884 Value is 1 if successful. It is zero if fonts were loaded during
16885 redisplay which makes re-adjusting glyph matrices necessary, and -1
16886 if point would appear in the scroll margins.
16887 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16888 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16889 set in FLAGS.) */
16890
16891 int
16892 try_window (Lisp_Object window, struct text_pos pos, int flags)
16893 {
16894 struct window *w = XWINDOW (window);
16895 struct it it;
16896 struct glyph_row *last_text_row = NULL;
16897 struct frame *f = XFRAME (w->frame);
16898 int frame_line_height = default_line_pixel_height (w);
16899
16900 /* Make POS the new window start. */
16901 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16902
16903 /* Mark cursor position as unknown. No overlay arrow seen. */
16904 w->cursor.vpos = -1;
16905 overlay_arrow_seen = 0;
16906
16907 /* Initialize iterator and info to start at POS. */
16908 start_display (&it, w, pos);
16909 it.glyph_row->reversed_p = false;
16910
16911 /* Display all lines of W. */
16912 while (it.current_y < it.last_visible_y)
16913 {
16914 if (display_line (&it))
16915 last_text_row = it.glyph_row - 1;
16916 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16917 return 0;
16918 }
16919
16920 /* Don't let the cursor end in the scroll margins. */
16921 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16922 && !MINI_WINDOW_P (w))
16923 {
16924 int this_scroll_margin;
16925 int window_total_lines
16926 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16927
16928 if (scroll_margin > 0)
16929 {
16930 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16931 this_scroll_margin *= frame_line_height;
16932 }
16933 else
16934 this_scroll_margin = 0;
16935
16936 if ((w->cursor.y >= 0 /* not vscrolled */
16937 && w->cursor.y < this_scroll_margin
16938 && CHARPOS (pos) > BEGV
16939 && IT_CHARPOS (it) < ZV)
16940 /* rms: considering make_cursor_line_fully_visible_p here
16941 seems to give wrong results. We don't want to recenter
16942 when the last line is partly visible, we want to allow
16943 that case to be handled in the usual way. */
16944 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16945 {
16946 w->cursor.vpos = -1;
16947 clear_glyph_matrix (w->desired_matrix);
16948 return -1;
16949 }
16950 }
16951
16952 /* If bottom moved off end of frame, change mode line percentage. */
16953 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16954 w->update_mode_line = 1;
16955
16956 /* Set window_end_pos to the offset of the last character displayed
16957 on the window from the end of current_buffer. Set
16958 window_end_vpos to its row number. */
16959 if (last_text_row)
16960 {
16961 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16962 adjust_window_ends (w, last_text_row, 0);
16963 eassert
16964 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16965 w->window_end_vpos)));
16966 }
16967 else
16968 {
16969 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16970 w->window_end_pos = Z - ZV;
16971 w->window_end_vpos = 0;
16972 }
16973
16974 /* But that is not valid info until redisplay finishes. */
16975 w->window_end_valid = 0;
16976 return 1;
16977 }
16978
16979
16980 \f
16981 /************************************************************************
16982 Window redisplay reusing current matrix when buffer has not changed
16983 ************************************************************************/
16984
16985 /* Try redisplay of window W showing an unchanged buffer with a
16986 different window start than the last time it was displayed by
16987 reusing its current matrix. Value is non-zero if successful.
16988 W->start is the new window start. */
16989
16990 static int
16991 try_window_reusing_current_matrix (struct window *w)
16992 {
16993 struct frame *f = XFRAME (w->frame);
16994 struct glyph_row *bottom_row;
16995 struct it it;
16996 struct run run;
16997 struct text_pos start, new_start;
16998 int nrows_scrolled, i;
16999 struct glyph_row *last_text_row;
17000 struct glyph_row *last_reused_text_row;
17001 struct glyph_row *start_row;
17002 int start_vpos, min_y, max_y;
17003
17004 #ifdef GLYPH_DEBUG
17005 if (inhibit_try_window_reusing)
17006 return 0;
17007 #endif
17008
17009 if (/* This function doesn't handle terminal frames. */
17010 !FRAME_WINDOW_P (f)
17011 /* Don't try to reuse the display if windows have been split
17012 or such. */
17013 || windows_or_buffers_changed
17014 || f->cursor_type_changed)
17015 return 0;
17016
17017 /* Can't do this if showing trailing whitespace. */
17018 if (!NILP (Vshow_trailing_whitespace))
17019 return 0;
17020
17021 /* If top-line visibility has changed, give up. */
17022 if (WINDOW_WANTS_HEADER_LINE_P (w)
17023 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17024 return 0;
17025
17026 /* Give up if old or new display is scrolled vertically. We could
17027 make this function handle this, but right now it doesn't. */
17028 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17029 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17030 return 0;
17031
17032 /* The variable new_start now holds the new window start. The old
17033 start `start' can be determined from the current matrix. */
17034 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17035 start = start_row->minpos;
17036 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17037
17038 /* Clear the desired matrix for the display below. */
17039 clear_glyph_matrix (w->desired_matrix);
17040
17041 if (CHARPOS (new_start) <= CHARPOS (start))
17042 {
17043 /* Don't use this method if the display starts with an ellipsis
17044 displayed for invisible text. It's not easy to handle that case
17045 below, and it's certainly not worth the effort since this is
17046 not a frequent case. */
17047 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17048 return 0;
17049
17050 IF_DEBUG (debug_method_add (w, "twu1"));
17051
17052 /* Display up to a row that can be reused. The variable
17053 last_text_row is set to the last row displayed that displays
17054 text. Note that it.vpos == 0 if or if not there is a
17055 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17056 start_display (&it, w, new_start);
17057 w->cursor.vpos = -1;
17058 last_text_row = last_reused_text_row = NULL;
17059
17060 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17061 {
17062 /* If we have reached into the characters in the START row,
17063 that means the line boundaries have changed. So we
17064 can't start copying with the row START. Maybe it will
17065 work to start copying with the following row. */
17066 while (IT_CHARPOS (it) > CHARPOS (start))
17067 {
17068 /* Advance to the next row as the "start". */
17069 start_row++;
17070 start = start_row->minpos;
17071 /* If there are no more rows to try, or just one, give up. */
17072 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17073 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17074 || CHARPOS (start) == ZV)
17075 {
17076 clear_glyph_matrix (w->desired_matrix);
17077 return 0;
17078 }
17079
17080 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17081 }
17082 /* If we have reached alignment, we can copy the rest of the
17083 rows. */
17084 if (IT_CHARPOS (it) == CHARPOS (start)
17085 /* Don't accept "alignment" inside a display vector,
17086 since start_row could have started in the middle of
17087 that same display vector (thus their character
17088 positions match), and we have no way of telling if
17089 that is the case. */
17090 && it.current.dpvec_index < 0)
17091 break;
17092
17093 it.glyph_row->reversed_p = false;
17094 if (display_line (&it))
17095 last_text_row = it.glyph_row - 1;
17096
17097 }
17098
17099 /* A value of current_y < last_visible_y means that we stopped
17100 at the previous window start, which in turn means that we
17101 have at least one reusable row. */
17102 if (it.current_y < it.last_visible_y)
17103 {
17104 struct glyph_row *row;
17105
17106 /* IT.vpos always starts from 0; it counts text lines. */
17107 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17108
17109 /* Find PT if not already found in the lines displayed. */
17110 if (w->cursor.vpos < 0)
17111 {
17112 int dy = it.current_y - start_row->y;
17113
17114 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17115 row = row_containing_pos (w, PT, row, NULL, dy);
17116 if (row)
17117 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17118 dy, nrows_scrolled);
17119 else
17120 {
17121 clear_glyph_matrix (w->desired_matrix);
17122 return 0;
17123 }
17124 }
17125
17126 /* Scroll the display. Do it before the current matrix is
17127 changed. The problem here is that update has not yet
17128 run, i.e. part of the current matrix is not up to date.
17129 scroll_run_hook will clear the cursor, and use the
17130 current matrix to get the height of the row the cursor is
17131 in. */
17132 run.current_y = start_row->y;
17133 run.desired_y = it.current_y;
17134 run.height = it.last_visible_y - it.current_y;
17135
17136 if (run.height > 0 && run.current_y != run.desired_y)
17137 {
17138 update_begin (f);
17139 FRAME_RIF (f)->update_window_begin_hook (w);
17140 FRAME_RIF (f)->clear_window_mouse_face (w);
17141 FRAME_RIF (f)->scroll_run_hook (w, &run);
17142 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17143 update_end (f);
17144 }
17145
17146 /* Shift current matrix down by nrows_scrolled lines. */
17147 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17148 rotate_matrix (w->current_matrix,
17149 start_vpos,
17150 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17151 nrows_scrolled);
17152
17153 /* Disable lines that must be updated. */
17154 for (i = 0; i < nrows_scrolled; ++i)
17155 (start_row + i)->enabled_p = false;
17156
17157 /* Re-compute Y positions. */
17158 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17159 max_y = it.last_visible_y;
17160 for (row = start_row + nrows_scrolled;
17161 row < bottom_row;
17162 ++row)
17163 {
17164 row->y = it.current_y;
17165 row->visible_height = row->height;
17166
17167 if (row->y < min_y)
17168 row->visible_height -= min_y - row->y;
17169 if (row->y + row->height > max_y)
17170 row->visible_height -= row->y + row->height - max_y;
17171 if (row->fringe_bitmap_periodic_p)
17172 row->redraw_fringe_bitmaps_p = 1;
17173
17174 it.current_y += row->height;
17175
17176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17177 last_reused_text_row = row;
17178 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17179 break;
17180 }
17181
17182 /* Disable lines in the current matrix which are now
17183 below the window. */
17184 for (++row; row < bottom_row; ++row)
17185 row->enabled_p = row->mode_line_p = 0;
17186 }
17187
17188 /* Update window_end_pos etc.; last_reused_text_row is the last
17189 reused row from the current matrix containing text, if any.
17190 The value of last_text_row is the last displayed line
17191 containing text. */
17192 if (last_reused_text_row)
17193 adjust_window_ends (w, last_reused_text_row, 1);
17194 else if (last_text_row)
17195 adjust_window_ends (w, last_text_row, 0);
17196 else
17197 {
17198 /* This window must be completely empty. */
17199 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17200 w->window_end_pos = Z - ZV;
17201 w->window_end_vpos = 0;
17202 }
17203 w->window_end_valid = 0;
17204
17205 /* Update hint: don't try scrolling again in update_window. */
17206 w->desired_matrix->no_scrolling_p = 1;
17207
17208 #ifdef GLYPH_DEBUG
17209 debug_method_add (w, "try_window_reusing_current_matrix 1");
17210 #endif
17211 return 1;
17212 }
17213 else if (CHARPOS (new_start) > CHARPOS (start))
17214 {
17215 struct glyph_row *pt_row, *row;
17216 struct glyph_row *first_reusable_row;
17217 struct glyph_row *first_row_to_display;
17218 int dy;
17219 int yb = window_text_bottom_y (w);
17220
17221 /* Find the row starting at new_start, if there is one. Don't
17222 reuse a partially visible line at the end. */
17223 first_reusable_row = start_row;
17224 while (first_reusable_row->enabled_p
17225 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17226 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17227 < CHARPOS (new_start)))
17228 ++first_reusable_row;
17229
17230 /* Give up if there is no row to reuse. */
17231 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17232 || !first_reusable_row->enabled_p
17233 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17234 != CHARPOS (new_start)))
17235 return 0;
17236
17237 /* We can reuse fully visible rows beginning with
17238 first_reusable_row to the end of the window. Set
17239 first_row_to_display to the first row that cannot be reused.
17240 Set pt_row to the row containing point, if there is any. */
17241 pt_row = NULL;
17242 for (first_row_to_display = first_reusable_row;
17243 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17244 ++first_row_to_display)
17245 {
17246 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17247 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17248 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17249 && first_row_to_display->ends_at_zv_p
17250 && pt_row == NULL)))
17251 pt_row = first_row_to_display;
17252 }
17253
17254 /* Start displaying at the start of first_row_to_display. */
17255 eassert (first_row_to_display->y < yb);
17256 init_to_row_start (&it, w, first_row_to_display);
17257
17258 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17259 - start_vpos);
17260 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17261 - nrows_scrolled);
17262 it.current_y = (first_row_to_display->y - first_reusable_row->y
17263 + WINDOW_HEADER_LINE_HEIGHT (w));
17264
17265 /* Display lines beginning with first_row_to_display in the
17266 desired matrix. Set last_text_row to the last row displayed
17267 that displays text. */
17268 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17269 if (pt_row == NULL)
17270 w->cursor.vpos = -1;
17271 last_text_row = NULL;
17272 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17273 if (display_line (&it))
17274 last_text_row = it.glyph_row - 1;
17275
17276 /* If point is in a reused row, adjust y and vpos of the cursor
17277 position. */
17278 if (pt_row)
17279 {
17280 w->cursor.vpos -= nrows_scrolled;
17281 w->cursor.y -= first_reusable_row->y - start_row->y;
17282 }
17283
17284 /* Give up if point isn't in a row displayed or reused. (This
17285 also handles the case where w->cursor.vpos < nrows_scrolled
17286 after the calls to display_line, which can happen with scroll
17287 margins. See bug#1295.) */
17288 if (w->cursor.vpos < 0)
17289 {
17290 clear_glyph_matrix (w->desired_matrix);
17291 return 0;
17292 }
17293
17294 /* Scroll the display. */
17295 run.current_y = first_reusable_row->y;
17296 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17297 run.height = it.last_visible_y - run.current_y;
17298 dy = run.current_y - run.desired_y;
17299
17300 if (run.height)
17301 {
17302 update_begin (f);
17303 FRAME_RIF (f)->update_window_begin_hook (w);
17304 FRAME_RIF (f)->clear_window_mouse_face (w);
17305 FRAME_RIF (f)->scroll_run_hook (w, &run);
17306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17307 update_end (f);
17308 }
17309
17310 /* Adjust Y positions of reused rows. */
17311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17312 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17313 max_y = it.last_visible_y;
17314 for (row = first_reusable_row; row < first_row_to_display; ++row)
17315 {
17316 row->y -= dy;
17317 row->visible_height = row->height;
17318 if (row->y < min_y)
17319 row->visible_height -= min_y - row->y;
17320 if (row->y + row->height > max_y)
17321 row->visible_height -= row->y + row->height - max_y;
17322 if (row->fringe_bitmap_periodic_p)
17323 row->redraw_fringe_bitmaps_p = 1;
17324 }
17325
17326 /* Scroll the current matrix. */
17327 eassert (nrows_scrolled > 0);
17328 rotate_matrix (w->current_matrix,
17329 start_vpos,
17330 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17331 -nrows_scrolled);
17332
17333 /* Disable rows not reused. */
17334 for (row -= nrows_scrolled; row < bottom_row; ++row)
17335 row->enabled_p = false;
17336
17337 /* Point may have moved to a different line, so we cannot assume that
17338 the previous cursor position is valid; locate the correct row. */
17339 if (pt_row)
17340 {
17341 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17342 row < bottom_row
17343 && PT >= MATRIX_ROW_END_CHARPOS (row)
17344 && !row->ends_at_zv_p;
17345 row++)
17346 {
17347 w->cursor.vpos++;
17348 w->cursor.y = row->y;
17349 }
17350 if (row < bottom_row)
17351 {
17352 /* Can't simply scan the row for point with
17353 bidi-reordered glyph rows. Let set_cursor_from_row
17354 figure out where to put the cursor, and if it fails,
17355 give up. */
17356 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17357 {
17358 if (!set_cursor_from_row (w, row, w->current_matrix,
17359 0, 0, 0, 0))
17360 {
17361 clear_glyph_matrix (w->desired_matrix);
17362 return 0;
17363 }
17364 }
17365 else
17366 {
17367 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17368 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17369
17370 for (; glyph < end
17371 && (!BUFFERP (glyph->object)
17372 || glyph->charpos < PT);
17373 glyph++)
17374 {
17375 w->cursor.hpos++;
17376 w->cursor.x += glyph->pixel_width;
17377 }
17378 }
17379 }
17380 }
17381
17382 /* Adjust window end. A null value of last_text_row means that
17383 the window end is in reused rows which in turn means that
17384 only its vpos can have changed. */
17385 if (last_text_row)
17386 adjust_window_ends (w, last_text_row, 0);
17387 else
17388 w->window_end_vpos -= nrows_scrolled;
17389
17390 w->window_end_valid = 0;
17391 w->desired_matrix->no_scrolling_p = 1;
17392
17393 #ifdef GLYPH_DEBUG
17394 debug_method_add (w, "try_window_reusing_current_matrix 2");
17395 #endif
17396 return 1;
17397 }
17398
17399 return 0;
17400 }
17401
17402
17403 \f
17404 /************************************************************************
17405 Window redisplay reusing current matrix when buffer has changed
17406 ************************************************************************/
17407
17408 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17409 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17410 ptrdiff_t *, ptrdiff_t *);
17411 static struct glyph_row *
17412 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17413 struct glyph_row *);
17414
17415
17416 /* Return the last row in MATRIX displaying text. If row START is
17417 non-null, start searching with that row. IT gives the dimensions
17418 of the display. Value is null if matrix is empty; otherwise it is
17419 a pointer to the row found. */
17420
17421 static struct glyph_row *
17422 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17423 struct glyph_row *start)
17424 {
17425 struct glyph_row *row, *row_found;
17426
17427 /* Set row_found to the last row in IT->w's current matrix
17428 displaying text. The loop looks funny but think of partially
17429 visible lines. */
17430 row_found = NULL;
17431 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17432 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17433 {
17434 eassert (row->enabled_p);
17435 row_found = row;
17436 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17437 break;
17438 ++row;
17439 }
17440
17441 return row_found;
17442 }
17443
17444
17445 /* Return the last row in the current matrix of W that is not affected
17446 by changes at the start of current_buffer that occurred since W's
17447 current matrix was built. Value is null if no such row exists.
17448
17449 BEG_UNCHANGED us the number of characters unchanged at the start of
17450 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17451 first changed character in current_buffer. Characters at positions <
17452 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17453 when the current matrix was built. */
17454
17455 static struct glyph_row *
17456 find_last_unchanged_at_beg_row (struct window *w)
17457 {
17458 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17459 struct glyph_row *row;
17460 struct glyph_row *row_found = NULL;
17461 int yb = window_text_bottom_y (w);
17462
17463 /* Find the last row displaying unchanged text. */
17464 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17465 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17466 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17467 ++row)
17468 {
17469 if (/* If row ends before first_changed_pos, it is unchanged,
17470 except in some case. */
17471 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17472 /* When row ends in ZV and we write at ZV it is not
17473 unchanged. */
17474 && !row->ends_at_zv_p
17475 /* When first_changed_pos is the end of a continued line,
17476 row is not unchanged because it may be no longer
17477 continued. */
17478 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17479 && (row->continued_p
17480 || row->exact_window_width_line_p))
17481 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17482 needs to be recomputed, so don't consider this row as
17483 unchanged. This happens when the last line was
17484 bidi-reordered and was killed immediately before this
17485 redisplay cycle. In that case, ROW->end stores the
17486 buffer position of the first visual-order character of
17487 the killed text, which is now beyond ZV. */
17488 && CHARPOS (row->end.pos) <= ZV)
17489 row_found = row;
17490
17491 /* Stop if last visible row. */
17492 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17493 break;
17494 }
17495
17496 return row_found;
17497 }
17498
17499
17500 /* Find the first glyph row in the current matrix of W that is not
17501 affected by changes at the end of current_buffer since the
17502 time W's current matrix was built.
17503
17504 Return in *DELTA the number of chars by which buffer positions in
17505 unchanged text at the end of current_buffer must be adjusted.
17506
17507 Return in *DELTA_BYTES the corresponding number of bytes.
17508
17509 Value is null if no such row exists, i.e. all rows are affected by
17510 changes. */
17511
17512 static struct glyph_row *
17513 find_first_unchanged_at_end_row (struct window *w,
17514 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17515 {
17516 struct glyph_row *row;
17517 struct glyph_row *row_found = NULL;
17518
17519 *delta = *delta_bytes = 0;
17520
17521 /* Display must not have been paused, otherwise the current matrix
17522 is not up to date. */
17523 eassert (w->window_end_valid);
17524
17525 /* A value of window_end_pos >= END_UNCHANGED means that the window
17526 end is in the range of changed text. If so, there is no
17527 unchanged row at the end of W's current matrix. */
17528 if (w->window_end_pos >= END_UNCHANGED)
17529 return NULL;
17530
17531 /* Set row to the last row in W's current matrix displaying text. */
17532 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17533
17534 /* If matrix is entirely empty, no unchanged row exists. */
17535 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17536 {
17537 /* The value of row is the last glyph row in the matrix having a
17538 meaningful buffer position in it. The end position of row
17539 corresponds to window_end_pos. This allows us to translate
17540 buffer positions in the current matrix to current buffer
17541 positions for characters not in changed text. */
17542 ptrdiff_t Z_old =
17543 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17544 ptrdiff_t Z_BYTE_old =
17545 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17546 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17547 struct glyph_row *first_text_row
17548 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17549
17550 *delta = Z - Z_old;
17551 *delta_bytes = Z_BYTE - Z_BYTE_old;
17552
17553 /* Set last_unchanged_pos to the buffer position of the last
17554 character in the buffer that has not been changed. Z is the
17555 index + 1 of the last character in current_buffer, i.e. by
17556 subtracting END_UNCHANGED we get the index of the last
17557 unchanged character, and we have to add BEG to get its buffer
17558 position. */
17559 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17560 last_unchanged_pos_old = last_unchanged_pos - *delta;
17561
17562 /* Search backward from ROW for a row displaying a line that
17563 starts at a minimum position >= last_unchanged_pos_old. */
17564 for (; row > first_text_row; --row)
17565 {
17566 /* This used to abort, but it can happen.
17567 It is ok to just stop the search instead here. KFS. */
17568 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17569 break;
17570
17571 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17572 row_found = row;
17573 }
17574 }
17575
17576 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17577
17578 return row_found;
17579 }
17580
17581
17582 /* Make sure that glyph rows in the current matrix of window W
17583 reference the same glyph memory as corresponding rows in the
17584 frame's frame matrix. This function is called after scrolling W's
17585 current matrix on a terminal frame in try_window_id and
17586 try_window_reusing_current_matrix. */
17587
17588 static void
17589 sync_frame_with_window_matrix_rows (struct window *w)
17590 {
17591 struct frame *f = XFRAME (w->frame);
17592 struct glyph_row *window_row, *window_row_end, *frame_row;
17593
17594 /* Preconditions: W must be a leaf window and full-width. Its frame
17595 must have a frame matrix. */
17596 eassert (BUFFERP (w->contents));
17597 eassert (WINDOW_FULL_WIDTH_P (w));
17598 eassert (!FRAME_WINDOW_P (f));
17599
17600 /* If W is a full-width window, glyph pointers in W's current matrix
17601 have, by definition, to be the same as glyph pointers in the
17602 corresponding frame matrix. Note that frame matrices have no
17603 marginal areas (see build_frame_matrix). */
17604 window_row = w->current_matrix->rows;
17605 window_row_end = window_row + w->current_matrix->nrows;
17606 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17607 while (window_row < window_row_end)
17608 {
17609 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17610 struct glyph *end = window_row->glyphs[LAST_AREA];
17611
17612 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17613 frame_row->glyphs[TEXT_AREA] = start;
17614 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17615 frame_row->glyphs[LAST_AREA] = end;
17616
17617 /* Disable frame rows whose corresponding window rows have
17618 been disabled in try_window_id. */
17619 if (!window_row->enabled_p)
17620 frame_row->enabled_p = false;
17621
17622 ++window_row, ++frame_row;
17623 }
17624 }
17625
17626
17627 /* Find the glyph row in window W containing CHARPOS. Consider all
17628 rows between START and END (not inclusive). END null means search
17629 all rows to the end of the display area of W. Value is the row
17630 containing CHARPOS or null. */
17631
17632 struct glyph_row *
17633 row_containing_pos (struct window *w, ptrdiff_t charpos,
17634 struct glyph_row *start, struct glyph_row *end, int dy)
17635 {
17636 struct glyph_row *row = start;
17637 struct glyph_row *best_row = NULL;
17638 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17639 int last_y;
17640
17641 /* If we happen to start on a header-line, skip that. */
17642 if (row->mode_line_p)
17643 ++row;
17644
17645 if ((end && row >= end) || !row->enabled_p)
17646 return NULL;
17647
17648 last_y = window_text_bottom_y (w) - dy;
17649
17650 while (1)
17651 {
17652 /* Give up if we have gone too far. */
17653 if (end && row >= end)
17654 return NULL;
17655 /* This formerly returned if they were equal.
17656 I think that both quantities are of a "last plus one" type;
17657 if so, when they are equal, the row is within the screen. -- rms. */
17658 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17659 return NULL;
17660
17661 /* If it is in this row, return this row. */
17662 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17663 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17664 /* The end position of a row equals the start
17665 position of the next row. If CHARPOS is there, we
17666 would rather consider it displayed in the next
17667 line, except when this line ends in ZV. */
17668 && !row_for_charpos_p (row, charpos)))
17669 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17670 {
17671 struct glyph *g;
17672
17673 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17674 || (!best_row && !row->continued_p))
17675 return row;
17676 /* In bidi-reordered rows, there could be several rows whose
17677 edges surround CHARPOS, all of these rows belonging to
17678 the same continued line. We need to find the row which
17679 fits CHARPOS the best. */
17680 for (g = row->glyphs[TEXT_AREA];
17681 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17682 g++)
17683 {
17684 if (!STRINGP (g->object))
17685 {
17686 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17687 {
17688 mindif = eabs (g->charpos - charpos);
17689 best_row = row;
17690 /* Exact match always wins. */
17691 if (mindif == 0)
17692 return best_row;
17693 }
17694 }
17695 }
17696 }
17697 else if (best_row && !row->continued_p)
17698 return best_row;
17699 ++row;
17700 }
17701 }
17702
17703
17704 /* Try to redisplay window W by reusing its existing display. W's
17705 current matrix must be up to date when this function is called,
17706 i.e. window_end_valid must be nonzero.
17707
17708 Value is
17709
17710 >= 1 if successful, i.e. display has been updated
17711 specifically:
17712 1 means the changes were in front of a newline that precedes
17713 the window start, and the whole current matrix was reused
17714 2 means the changes were after the last position displayed
17715 in the window, and the whole current matrix was reused
17716 3 means portions of the current matrix were reused, while
17717 some of the screen lines were redrawn
17718 -1 if redisplay with same window start is known not to succeed
17719 0 if otherwise unsuccessful
17720
17721 The following steps are performed:
17722
17723 1. Find the last row in the current matrix of W that is not
17724 affected by changes at the start of current_buffer. If no such row
17725 is found, give up.
17726
17727 2. Find the first row in W's current matrix that is not affected by
17728 changes at the end of current_buffer. Maybe there is no such row.
17729
17730 3. Display lines beginning with the row + 1 found in step 1 to the
17731 row found in step 2 or, if step 2 didn't find a row, to the end of
17732 the window.
17733
17734 4. If cursor is not known to appear on the window, give up.
17735
17736 5. If display stopped at the row found in step 2, scroll the
17737 display and current matrix as needed.
17738
17739 6. Maybe display some lines at the end of W, if we must. This can
17740 happen under various circumstances, like a partially visible line
17741 becoming fully visible, or because newly displayed lines are displayed
17742 in smaller font sizes.
17743
17744 7. Update W's window end information. */
17745
17746 static int
17747 try_window_id (struct window *w)
17748 {
17749 struct frame *f = XFRAME (w->frame);
17750 struct glyph_matrix *current_matrix = w->current_matrix;
17751 struct glyph_matrix *desired_matrix = w->desired_matrix;
17752 struct glyph_row *last_unchanged_at_beg_row;
17753 struct glyph_row *first_unchanged_at_end_row;
17754 struct glyph_row *row;
17755 struct glyph_row *bottom_row;
17756 int bottom_vpos;
17757 struct it it;
17758 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17759 int dvpos, dy;
17760 struct text_pos start_pos;
17761 struct run run;
17762 int first_unchanged_at_end_vpos = 0;
17763 struct glyph_row *last_text_row, *last_text_row_at_end;
17764 struct text_pos start;
17765 ptrdiff_t first_changed_charpos, last_changed_charpos;
17766
17767 #ifdef GLYPH_DEBUG
17768 if (inhibit_try_window_id)
17769 return 0;
17770 #endif
17771
17772 /* This is handy for debugging. */
17773 #if 0
17774 #define GIVE_UP(X) \
17775 do { \
17776 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17777 return 0; \
17778 } while (0)
17779 #else
17780 #define GIVE_UP(X) return 0
17781 #endif
17782
17783 SET_TEXT_POS_FROM_MARKER (start, w->start);
17784
17785 /* Don't use this for mini-windows because these can show
17786 messages and mini-buffers, and we don't handle that here. */
17787 if (MINI_WINDOW_P (w))
17788 GIVE_UP (1);
17789
17790 /* This flag is used to prevent redisplay optimizations. */
17791 if (windows_or_buffers_changed || f->cursor_type_changed)
17792 GIVE_UP (2);
17793
17794 /* This function's optimizations cannot be used if overlays have
17795 changed in the buffer displayed by the window, so give up if they
17796 have. */
17797 if (w->last_overlay_modified != OVERLAY_MODIFF)
17798 GIVE_UP (21);
17799
17800 /* Verify that narrowing has not changed.
17801 Also verify that we were not told to prevent redisplay optimizations.
17802 It would be nice to further
17803 reduce the number of cases where this prevents try_window_id. */
17804 if (current_buffer->clip_changed
17805 || current_buffer->prevent_redisplay_optimizations_p)
17806 GIVE_UP (3);
17807
17808 /* Window must either use window-based redisplay or be full width. */
17809 if (!FRAME_WINDOW_P (f)
17810 && (!FRAME_LINE_INS_DEL_OK (f)
17811 || !WINDOW_FULL_WIDTH_P (w)))
17812 GIVE_UP (4);
17813
17814 /* Give up if point is known NOT to appear in W. */
17815 if (PT < CHARPOS (start))
17816 GIVE_UP (5);
17817
17818 /* Another way to prevent redisplay optimizations. */
17819 if (w->last_modified == 0)
17820 GIVE_UP (6);
17821
17822 /* Verify that window is not hscrolled. */
17823 if (w->hscroll != 0)
17824 GIVE_UP (7);
17825
17826 /* Verify that display wasn't paused. */
17827 if (!w->window_end_valid)
17828 GIVE_UP (8);
17829
17830 /* Likewise if highlighting trailing whitespace. */
17831 if (!NILP (Vshow_trailing_whitespace))
17832 GIVE_UP (11);
17833
17834 /* Can't use this if overlay arrow position and/or string have
17835 changed. */
17836 if (overlay_arrows_changed_p ())
17837 GIVE_UP (12);
17838
17839 /* When word-wrap is on, adding a space to the first word of a
17840 wrapped line can change the wrap position, altering the line
17841 above it. It might be worthwhile to handle this more
17842 intelligently, but for now just redisplay from scratch. */
17843 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17844 GIVE_UP (21);
17845
17846 /* Under bidi reordering, adding or deleting a character in the
17847 beginning of a paragraph, before the first strong directional
17848 character, can change the base direction of the paragraph (unless
17849 the buffer specifies a fixed paragraph direction), which will
17850 require to redisplay the whole paragraph. It might be worthwhile
17851 to find the paragraph limits and widen the range of redisplayed
17852 lines to that, but for now just give up this optimization and
17853 redisplay from scratch. */
17854 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17855 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17856 GIVE_UP (22);
17857
17858 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17859 only if buffer has really changed. The reason is that the gap is
17860 initially at Z for freshly visited files. The code below would
17861 set end_unchanged to 0 in that case. */
17862 if (MODIFF > SAVE_MODIFF
17863 /* This seems to happen sometimes after saving a buffer. */
17864 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17865 {
17866 if (GPT - BEG < BEG_UNCHANGED)
17867 BEG_UNCHANGED = GPT - BEG;
17868 if (Z - GPT < END_UNCHANGED)
17869 END_UNCHANGED = Z - GPT;
17870 }
17871
17872 /* The position of the first and last character that has been changed. */
17873 first_changed_charpos = BEG + BEG_UNCHANGED;
17874 last_changed_charpos = Z - END_UNCHANGED;
17875
17876 /* If window starts after a line end, and the last change is in
17877 front of that newline, then changes don't affect the display.
17878 This case happens with stealth-fontification. Note that although
17879 the display is unchanged, glyph positions in the matrix have to
17880 be adjusted, of course. */
17881 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17882 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17883 && ((last_changed_charpos < CHARPOS (start)
17884 && CHARPOS (start) == BEGV)
17885 || (last_changed_charpos < CHARPOS (start) - 1
17886 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17887 {
17888 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17889 struct glyph_row *r0;
17890
17891 /* Compute how many chars/bytes have been added to or removed
17892 from the buffer. */
17893 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17894 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17895 Z_delta = Z - Z_old;
17896 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17897
17898 /* Give up if PT is not in the window. Note that it already has
17899 been checked at the start of try_window_id that PT is not in
17900 front of the window start. */
17901 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17902 GIVE_UP (13);
17903
17904 /* If window start is unchanged, we can reuse the whole matrix
17905 as is, after adjusting glyph positions. No need to compute
17906 the window end again, since its offset from Z hasn't changed. */
17907 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17908 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17909 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17910 /* PT must not be in a partially visible line. */
17911 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17912 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17913 {
17914 /* Adjust positions in the glyph matrix. */
17915 if (Z_delta || Z_delta_bytes)
17916 {
17917 struct glyph_row *r1
17918 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17919 increment_matrix_positions (w->current_matrix,
17920 MATRIX_ROW_VPOS (r0, current_matrix),
17921 MATRIX_ROW_VPOS (r1, current_matrix),
17922 Z_delta, Z_delta_bytes);
17923 }
17924
17925 /* Set the cursor. */
17926 row = row_containing_pos (w, PT, r0, NULL, 0);
17927 if (row)
17928 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17929 return 1;
17930 }
17931 }
17932
17933 /* Handle the case that changes are all below what is displayed in
17934 the window, and that PT is in the window. This shortcut cannot
17935 be taken if ZV is visible in the window, and text has been added
17936 there that is visible in the window. */
17937 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17938 /* ZV is not visible in the window, or there are no
17939 changes at ZV, actually. */
17940 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17941 || first_changed_charpos == last_changed_charpos))
17942 {
17943 struct glyph_row *r0;
17944
17945 /* Give up if PT is not in the window. Note that it already has
17946 been checked at the start of try_window_id that PT is not in
17947 front of the window start. */
17948 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17949 GIVE_UP (14);
17950
17951 /* If window start is unchanged, we can reuse the whole matrix
17952 as is, without changing glyph positions since no text has
17953 been added/removed in front of the window end. */
17954 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17955 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17956 /* PT must not be in a partially visible line. */
17957 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17958 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17959 {
17960 /* We have to compute the window end anew since text
17961 could have been added/removed after it. */
17962 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17964
17965 /* Set the cursor. */
17966 row = row_containing_pos (w, PT, r0, NULL, 0);
17967 if (row)
17968 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17969 return 2;
17970 }
17971 }
17972
17973 /* Give up if window start is in the changed area.
17974
17975 The condition used to read
17976
17977 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17978
17979 but why that was tested escapes me at the moment. */
17980 if (CHARPOS (start) >= first_changed_charpos
17981 && CHARPOS (start) <= last_changed_charpos)
17982 GIVE_UP (15);
17983
17984 /* Check that window start agrees with the start of the first glyph
17985 row in its current matrix. Check this after we know the window
17986 start is not in changed text, otherwise positions would not be
17987 comparable. */
17988 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17989 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17990 GIVE_UP (16);
17991
17992 /* Give up if the window ends in strings. Overlay strings
17993 at the end are difficult to handle, so don't try. */
17994 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17995 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17996 GIVE_UP (20);
17997
17998 /* Compute the position at which we have to start displaying new
17999 lines. Some of the lines at the top of the window might be
18000 reusable because they are not displaying changed text. Find the
18001 last row in W's current matrix not affected by changes at the
18002 start of current_buffer. Value is null if changes start in the
18003 first line of window. */
18004 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18005 if (last_unchanged_at_beg_row)
18006 {
18007 /* Avoid starting to display in the middle of a character, a TAB
18008 for instance. This is easier than to set up the iterator
18009 exactly, and it's not a frequent case, so the additional
18010 effort wouldn't really pay off. */
18011 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18012 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18013 && last_unchanged_at_beg_row > w->current_matrix->rows)
18014 --last_unchanged_at_beg_row;
18015
18016 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18017 GIVE_UP (17);
18018
18019 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18020 GIVE_UP (18);
18021 start_pos = it.current.pos;
18022
18023 /* Start displaying new lines in the desired matrix at the same
18024 vpos we would use in the current matrix, i.e. below
18025 last_unchanged_at_beg_row. */
18026 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18027 current_matrix);
18028 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18029 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18030
18031 eassert (it.hpos == 0 && it.current_x == 0);
18032 }
18033 else
18034 {
18035 /* There are no reusable lines at the start of the window.
18036 Start displaying in the first text line. */
18037 start_display (&it, w, start);
18038 it.vpos = it.first_vpos;
18039 start_pos = it.current.pos;
18040 }
18041
18042 /* Find the first row that is not affected by changes at the end of
18043 the buffer. Value will be null if there is no unchanged row, in
18044 which case we must redisplay to the end of the window. delta
18045 will be set to the value by which buffer positions beginning with
18046 first_unchanged_at_end_row have to be adjusted due to text
18047 changes. */
18048 first_unchanged_at_end_row
18049 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18050 IF_DEBUG (debug_delta = delta);
18051 IF_DEBUG (debug_delta_bytes = delta_bytes);
18052
18053 /* Set stop_pos to the buffer position up to which we will have to
18054 display new lines. If first_unchanged_at_end_row != NULL, this
18055 is the buffer position of the start of the line displayed in that
18056 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18057 that we don't stop at a buffer position. */
18058 stop_pos = 0;
18059 if (first_unchanged_at_end_row)
18060 {
18061 eassert (last_unchanged_at_beg_row == NULL
18062 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18063
18064 /* If this is a continuation line, move forward to the next one
18065 that isn't. Changes in lines above affect this line.
18066 Caution: this may move first_unchanged_at_end_row to a row
18067 not displaying text. */
18068 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18069 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18070 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18071 < it.last_visible_y))
18072 ++first_unchanged_at_end_row;
18073
18074 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18075 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18076 >= it.last_visible_y))
18077 first_unchanged_at_end_row = NULL;
18078 else
18079 {
18080 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18081 + delta);
18082 first_unchanged_at_end_vpos
18083 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18084 eassert (stop_pos >= Z - END_UNCHANGED);
18085 }
18086 }
18087 else if (last_unchanged_at_beg_row == NULL)
18088 GIVE_UP (19);
18089
18090
18091 #ifdef GLYPH_DEBUG
18092
18093 /* Either there is no unchanged row at the end, or the one we have
18094 now displays text. This is a necessary condition for the window
18095 end pos calculation at the end of this function. */
18096 eassert (first_unchanged_at_end_row == NULL
18097 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18098
18099 debug_last_unchanged_at_beg_vpos
18100 = (last_unchanged_at_beg_row
18101 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18102 : -1);
18103 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18104
18105 #endif /* GLYPH_DEBUG */
18106
18107
18108 /* Display new lines. Set last_text_row to the last new line
18109 displayed which has text on it, i.e. might end up as being the
18110 line where the window_end_vpos is. */
18111 w->cursor.vpos = -1;
18112 last_text_row = NULL;
18113 overlay_arrow_seen = 0;
18114 if (it.current_y < it.last_visible_y
18115 && !f->fonts_changed
18116 && (first_unchanged_at_end_row == NULL
18117 || IT_CHARPOS (it) < stop_pos))
18118 it.glyph_row->reversed_p = false;
18119 while (it.current_y < it.last_visible_y
18120 && !f->fonts_changed
18121 && (first_unchanged_at_end_row == NULL
18122 || IT_CHARPOS (it) < stop_pos))
18123 {
18124 if (display_line (&it))
18125 last_text_row = it.glyph_row - 1;
18126 }
18127
18128 if (f->fonts_changed)
18129 return -1;
18130
18131
18132 /* Compute differences in buffer positions, y-positions etc. for
18133 lines reused at the bottom of the window. Compute what we can
18134 scroll. */
18135 if (first_unchanged_at_end_row
18136 /* No lines reused because we displayed everything up to the
18137 bottom of the window. */
18138 && it.current_y < it.last_visible_y)
18139 {
18140 dvpos = (it.vpos
18141 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18142 current_matrix));
18143 dy = it.current_y - first_unchanged_at_end_row->y;
18144 run.current_y = first_unchanged_at_end_row->y;
18145 run.desired_y = run.current_y + dy;
18146 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18147 }
18148 else
18149 {
18150 delta = delta_bytes = dvpos = dy
18151 = run.current_y = run.desired_y = run.height = 0;
18152 first_unchanged_at_end_row = NULL;
18153 }
18154 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18155
18156
18157 /* Find the cursor if not already found. We have to decide whether
18158 PT will appear on this window (it sometimes doesn't, but this is
18159 not a very frequent case.) This decision has to be made before
18160 the current matrix is altered. A value of cursor.vpos < 0 means
18161 that PT is either in one of the lines beginning at
18162 first_unchanged_at_end_row or below the window. Don't care for
18163 lines that might be displayed later at the window end; as
18164 mentioned, this is not a frequent case. */
18165 if (w->cursor.vpos < 0)
18166 {
18167 /* Cursor in unchanged rows at the top? */
18168 if (PT < CHARPOS (start_pos)
18169 && last_unchanged_at_beg_row)
18170 {
18171 row = row_containing_pos (w, PT,
18172 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18173 last_unchanged_at_beg_row + 1, 0);
18174 if (row)
18175 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18176 }
18177
18178 /* Start from first_unchanged_at_end_row looking for PT. */
18179 else if (first_unchanged_at_end_row)
18180 {
18181 row = row_containing_pos (w, PT - delta,
18182 first_unchanged_at_end_row, NULL, 0);
18183 if (row)
18184 set_cursor_from_row (w, row, w->current_matrix, delta,
18185 delta_bytes, dy, dvpos);
18186 }
18187
18188 /* Give up if cursor was not found. */
18189 if (w->cursor.vpos < 0)
18190 {
18191 clear_glyph_matrix (w->desired_matrix);
18192 return -1;
18193 }
18194 }
18195
18196 /* Don't let the cursor end in the scroll margins. */
18197 {
18198 int this_scroll_margin, cursor_height;
18199 int frame_line_height = default_line_pixel_height (w);
18200 int window_total_lines
18201 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18202
18203 this_scroll_margin =
18204 max (0, min (scroll_margin, window_total_lines / 4));
18205 this_scroll_margin *= frame_line_height;
18206 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18207
18208 if ((w->cursor.y < this_scroll_margin
18209 && CHARPOS (start) > BEGV)
18210 /* Old redisplay didn't take scroll margin into account at the bottom,
18211 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18212 || (w->cursor.y + (make_cursor_line_fully_visible_p
18213 ? cursor_height + this_scroll_margin
18214 : 1)) > it.last_visible_y)
18215 {
18216 w->cursor.vpos = -1;
18217 clear_glyph_matrix (w->desired_matrix);
18218 return -1;
18219 }
18220 }
18221
18222 /* Scroll the display. Do it before changing the current matrix so
18223 that xterm.c doesn't get confused about where the cursor glyph is
18224 found. */
18225 if (dy && run.height)
18226 {
18227 update_begin (f);
18228
18229 if (FRAME_WINDOW_P (f))
18230 {
18231 FRAME_RIF (f)->update_window_begin_hook (w);
18232 FRAME_RIF (f)->clear_window_mouse_face (w);
18233 FRAME_RIF (f)->scroll_run_hook (w, &run);
18234 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18235 }
18236 else
18237 {
18238 /* Terminal frame. In this case, dvpos gives the number of
18239 lines to scroll by; dvpos < 0 means scroll up. */
18240 int from_vpos
18241 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18242 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18243 int end = (WINDOW_TOP_EDGE_LINE (w)
18244 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18245 + window_internal_height (w));
18246
18247 #if defined (HAVE_GPM) || defined (MSDOS)
18248 x_clear_window_mouse_face (w);
18249 #endif
18250 /* Perform the operation on the screen. */
18251 if (dvpos > 0)
18252 {
18253 /* Scroll last_unchanged_at_beg_row to the end of the
18254 window down dvpos lines. */
18255 set_terminal_window (f, end);
18256
18257 /* On dumb terminals delete dvpos lines at the end
18258 before inserting dvpos empty lines. */
18259 if (!FRAME_SCROLL_REGION_OK (f))
18260 ins_del_lines (f, end - dvpos, -dvpos);
18261
18262 /* Insert dvpos empty lines in front of
18263 last_unchanged_at_beg_row. */
18264 ins_del_lines (f, from, dvpos);
18265 }
18266 else if (dvpos < 0)
18267 {
18268 /* Scroll up last_unchanged_at_beg_vpos to the end of
18269 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18270 set_terminal_window (f, end);
18271
18272 /* Delete dvpos lines in front of
18273 last_unchanged_at_beg_vpos. ins_del_lines will set
18274 the cursor to the given vpos and emit |dvpos| delete
18275 line sequences. */
18276 ins_del_lines (f, from + dvpos, dvpos);
18277
18278 /* On a dumb terminal insert dvpos empty lines at the
18279 end. */
18280 if (!FRAME_SCROLL_REGION_OK (f))
18281 ins_del_lines (f, end + dvpos, -dvpos);
18282 }
18283
18284 set_terminal_window (f, 0);
18285 }
18286
18287 update_end (f);
18288 }
18289
18290 /* Shift reused rows of the current matrix to the right position.
18291 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18292 text. */
18293 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18294 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18295 if (dvpos < 0)
18296 {
18297 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18298 bottom_vpos, dvpos);
18299 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18300 bottom_vpos);
18301 }
18302 else if (dvpos > 0)
18303 {
18304 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18305 bottom_vpos, dvpos);
18306 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18307 first_unchanged_at_end_vpos + dvpos);
18308 }
18309
18310 /* For frame-based redisplay, make sure that current frame and window
18311 matrix are in sync with respect to glyph memory. */
18312 if (!FRAME_WINDOW_P (f))
18313 sync_frame_with_window_matrix_rows (w);
18314
18315 /* Adjust buffer positions in reused rows. */
18316 if (delta || delta_bytes)
18317 increment_matrix_positions (current_matrix,
18318 first_unchanged_at_end_vpos + dvpos,
18319 bottom_vpos, delta, delta_bytes);
18320
18321 /* Adjust Y positions. */
18322 if (dy)
18323 shift_glyph_matrix (w, current_matrix,
18324 first_unchanged_at_end_vpos + dvpos,
18325 bottom_vpos, dy);
18326
18327 if (first_unchanged_at_end_row)
18328 {
18329 first_unchanged_at_end_row += dvpos;
18330 if (first_unchanged_at_end_row->y >= it.last_visible_y
18331 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18332 first_unchanged_at_end_row = NULL;
18333 }
18334
18335 /* If scrolling up, there may be some lines to display at the end of
18336 the window. */
18337 last_text_row_at_end = NULL;
18338 if (dy < 0)
18339 {
18340 /* Scrolling up can leave for example a partially visible line
18341 at the end of the window to be redisplayed. */
18342 /* Set last_row to the glyph row in the current matrix where the
18343 window end line is found. It has been moved up or down in
18344 the matrix by dvpos. */
18345 int last_vpos = w->window_end_vpos + dvpos;
18346 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18347
18348 /* If last_row is the window end line, it should display text. */
18349 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18350
18351 /* If window end line was partially visible before, begin
18352 displaying at that line. Otherwise begin displaying with the
18353 line following it. */
18354 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18355 {
18356 init_to_row_start (&it, w, last_row);
18357 it.vpos = last_vpos;
18358 it.current_y = last_row->y;
18359 }
18360 else
18361 {
18362 init_to_row_end (&it, w, last_row);
18363 it.vpos = 1 + last_vpos;
18364 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18365 ++last_row;
18366 }
18367
18368 /* We may start in a continuation line. If so, we have to
18369 get the right continuation_lines_width and current_x. */
18370 it.continuation_lines_width = last_row->continuation_lines_width;
18371 it.hpos = it.current_x = 0;
18372
18373 /* Display the rest of the lines at the window end. */
18374 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18375 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18376 {
18377 /* Is it always sure that the display agrees with lines in
18378 the current matrix? I don't think so, so we mark rows
18379 displayed invalid in the current matrix by setting their
18380 enabled_p flag to zero. */
18381 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18382 if (display_line (&it))
18383 last_text_row_at_end = it.glyph_row - 1;
18384 }
18385 }
18386
18387 /* Update window_end_pos and window_end_vpos. */
18388 if (first_unchanged_at_end_row && !last_text_row_at_end)
18389 {
18390 /* Window end line if one of the preserved rows from the current
18391 matrix. Set row to the last row displaying text in current
18392 matrix starting at first_unchanged_at_end_row, after
18393 scrolling. */
18394 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18395 row = find_last_row_displaying_text (w->current_matrix, &it,
18396 first_unchanged_at_end_row);
18397 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18398 adjust_window_ends (w, row, 1);
18399 eassert (w->window_end_bytepos >= 0);
18400 IF_DEBUG (debug_method_add (w, "A"));
18401 }
18402 else if (last_text_row_at_end)
18403 {
18404 adjust_window_ends (w, last_text_row_at_end, 0);
18405 eassert (w->window_end_bytepos >= 0);
18406 IF_DEBUG (debug_method_add (w, "B"));
18407 }
18408 else if (last_text_row)
18409 {
18410 /* We have displayed either to the end of the window or at the
18411 end of the window, i.e. the last row with text is to be found
18412 in the desired matrix. */
18413 adjust_window_ends (w, last_text_row, 0);
18414 eassert (w->window_end_bytepos >= 0);
18415 }
18416 else if (first_unchanged_at_end_row == NULL
18417 && last_text_row == NULL
18418 && last_text_row_at_end == NULL)
18419 {
18420 /* Displayed to end of window, but no line containing text was
18421 displayed. Lines were deleted at the end of the window. */
18422 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18423 int vpos = w->window_end_vpos;
18424 struct glyph_row *current_row = current_matrix->rows + vpos;
18425 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18426
18427 for (row = NULL;
18428 row == NULL && vpos >= first_vpos;
18429 --vpos, --current_row, --desired_row)
18430 {
18431 if (desired_row->enabled_p)
18432 {
18433 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18434 row = desired_row;
18435 }
18436 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18437 row = current_row;
18438 }
18439
18440 eassert (row != NULL);
18441 w->window_end_vpos = vpos + 1;
18442 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18443 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18444 eassert (w->window_end_bytepos >= 0);
18445 IF_DEBUG (debug_method_add (w, "C"));
18446 }
18447 else
18448 emacs_abort ();
18449
18450 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18451 debug_end_vpos = w->window_end_vpos));
18452
18453 /* Record that display has not been completed. */
18454 w->window_end_valid = 0;
18455 w->desired_matrix->no_scrolling_p = 1;
18456 return 3;
18457
18458 #undef GIVE_UP
18459 }
18460
18461
18462 \f
18463 /***********************************************************************
18464 More debugging support
18465 ***********************************************************************/
18466
18467 #ifdef GLYPH_DEBUG
18468
18469 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18470 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18471 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18472
18473
18474 /* Dump the contents of glyph matrix MATRIX on stderr.
18475
18476 GLYPHS 0 means don't show glyph contents.
18477 GLYPHS 1 means show glyphs in short form
18478 GLYPHS > 1 means show glyphs in long form. */
18479
18480 void
18481 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18482 {
18483 int i;
18484 for (i = 0; i < matrix->nrows; ++i)
18485 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18486 }
18487
18488
18489 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18490 the glyph row and area where the glyph comes from. */
18491
18492 void
18493 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18494 {
18495 if (glyph->type == CHAR_GLYPH
18496 || glyph->type == GLYPHLESS_GLYPH)
18497 {
18498 fprintf (stderr,
18499 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18500 glyph - row->glyphs[TEXT_AREA],
18501 (glyph->type == CHAR_GLYPH
18502 ? 'C'
18503 : 'G'),
18504 glyph->charpos,
18505 (BUFFERP (glyph->object)
18506 ? 'B'
18507 : (STRINGP (glyph->object)
18508 ? 'S'
18509 : (INTEGERP (glyph->object)
18510 ? '0'
18511 : '-'))),
18512 glyph->pixel_width,
18513 glyph->u.ch,
18514 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18515 ? glyph->u.ch
18516 : '.'),
18517 glyph->face_id,
18518 glyph->left_box_line_p,
18519 glyph->right_box_line_p);
18520 }
18521 else if (glyph->type == STRETCH_GLYPH)
18522 {
18523 fprintf (stderr,
18524 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18525 glyph - row->glyphs[TEXT_AREA],
18526 'S',
18527 glyph->charpos,
18528 (BUFFERP (glyph->object)
18529 ? 'B'
18530 : (STRINGP (glyph->object)
18531 ? 'S'
18532 : (INTEGERP (glyph->object)
18533 ? '0'
18534 : '-'))),
18535 glyph->pixel_width,
18536 0,
18537 ' ',
18538 glyph->face_id,
18539 glyph->left_box_line_p,
18540 glyph->right_box_line_p);
18541 }
18542 else if (glyph->type == IMAGE_GLYPH)
18543 {
18544 fprintf (stderr,
18545 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18546 glyph - row->glyphs[TEXT_AREA],
18547 'I',
18548 glyph->charpos,
18549 (BUFFERP (glyph->object)
18550 ? 'B'
18551 : (STRINGP (glyph->object)
18552 ? 'S'
18553 : (INTEGERP (glyph->object)
18554 ? '0'
18555 : '-'))),
18556 glyph->pixel_width,
18557 glyph->u.img_id,
18558 '.',
18559 glyph->face_id,
18560 glyph->left_box_line_p,
18561 glyph->right_box_line_p);
18562 }
18563 else if (glyph->type == COMPOSITE_GLYPH)
18564 {
18565 fprintf (stderr,
18566 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18567 glyph - row->glyphs[TEXT_AREA],
18568 '+',
18569 glyph->charpos,
18570 (BUFFERP (glyph->object)
18571 ? 'B'
18572 : (STRINGP (glyph->object)
18573 ? 'S'
18574 : (INTEGERP (glyph->object)
18575 ? '0'
18576 : '-'))),
18577 glyph->pixel_width,
18578 glyph->u.cmp.id);
18579 if (glyph->u.cmp.automatic)
18580 fprintf (stderr,
18581 "[%d-%d]",
18582 glyph->slice.cmp.from, glyph->slice.cmp.to);
18583 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18584 glyph->face_id,
18585 glyph->left_box_line_p,
18586 glyph->right_box_line_p);
18587 }
18588 }
18589
18590
18591 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18592 GLYPHS 0 means don't show glyph contents.
18593 GLYPHS 1 means show glyphs in short form
18594 GLYPHS > 1 means show glyphs in long form. */
18595
18596 void
18597 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18598 {
18599 if (glyphs != 1)
18600 {
18601 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18602 fprintf (stderr, "==============================================================================\n");
18603
18604 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18605 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18606 vpos,
18607 MATRIX_ROW_START_CHARPOS (row),
18608 MATRIX_ROW_END_CHARPOS (row),
18609 row->used[TEXT_AREA],
18610 row->contains_overlapping_glyphs_p,
18611 row->enabled_p,
18612 row->truncated_on_left_p,
18613 row->truncated_on_right_p,
18614 row->continued_p,
18615 MATRIX_ROW_CONTINUATION_LINE_P (row),
18616 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18617 row->ends_at_zv_p,
18618 row->fill_line_p,
18619 row->ends_in_middle_of_char_p,
18620 row->starts_in_middle_of_char_p,
18621 row->mouse_face_p,
18622 row->x,
18623 row->y,
18624 row->pixel_width,
18625 row->height,
18626 row->visible_height,
18627 row->ascent,
18628 row->phys_ascent);
18629 /* The next 3 lines should align to "Start" in the header. */
18630 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18631 row->end.overlay_string_index,
18632 row->continuation_lines_width);
18633 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18634 CHARPOS (row->start.string_pos),
18635 CHARPOS (row->end.string_pos));
18636 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18637 row->end.dpvec_index);
18638 }
18639
18640 if (glyphs > 1)
18641 {
18642 int area;
18643
18644 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18645 {
18646 struct glyph *glyph = row->glyphs[area];
18647 struct glyph *glyph_end = glyph + row->used[area];
18648
18649 /* Glyph for a line end in text. */
18650 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18651 ++glyph_end;
18652
18653 if (glyph < glyph_end)
18654 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18655
18656 for (; glyph < glyph_end; ++glyph)
18657 dump_glyph (row, glyph, area);
18658 }
18659 }
18660 else if (glyphs == 1)
18661 {
18662 int area;
18663 char s[SHRT_MAX + 4];
18664
18665 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18666 {
18667 int i;
18668
18669 for (i = 0; i < row->used[area]; ++i)
18670 {
18671 struct glyph *glyph = row->glyphs[area] + i;
18672 if (i == row->used[area] - 1
18673 && area == TEXT_AREA
18674 && INTEGERP (glyph->object)
18675 && glyph->type == CHAR_GLYPH
18676 && glyph->u.ch == ' ')
18677 {
18678 strcpy (&s[i], "[\\n]");
18679 i += 4;
18680 }
18681 else if (glyph->type == CHAR_GLYPH
18682 && glyph->u.ch < 0x80
18683 && glyph->u.ch >= ' ')
18684 s[i] = glyph->u.ch;
18685 else
18686 s[i] = '.';
18687 }
18688
18689 s[i] = '\0';
18690 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18691 }
18692 }
18693 }
18694
18695
18696 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18697 Sdump_glyph_matrix, 0, 1, "p",
18698 doc: /* Dump the current matrix of the selected window to stderr.
18699 Shows contents of glyph row structures. With non-nil
18700 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18701 glyphs in short form, otherwise show glyphs in long form.
18702
18703 Interactively, no argument means show glyphs in short form;
18704 with numeric argument, its value is passed as the GLYPHS flag. */)
18705 (Lisp_Object glyphs)
18706 {
18707 struct window *w = XWINDOW (selected_window);
18708 struct buffer *buffer = XBUFFER (w->contents);
18709
18710 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18711 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18712 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18713 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18714 fprintf (stderr, "=============================================\n");
18715 dump_glyph_matrix (w->current_matrix,
18716 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18717 return Qnil;
18718 }
18719
18720
18721 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18722 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18723 Only text-mode frames have frame glyph matrices. */)
18724 (void)
18725 {
18726 struct frame *f = XFRAME (selected_frame);
18727
18728 if (f->current_matrix)
18729 dump_glyph_matrix (f->current_matrix, 1);
18730 else
18731 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18732 return Qnil;
18733 }
18734
18735
18736 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18737 doc: /* Dump glyph row ROW to stderr.
18738 GLYPH 0 means don't dump glyphs.
18739 GLYPH 1 means dump glyphs in short form.
18740 GLYPH > 1 or omitted means dump glyphs in long form. */)
18741 (Lisp_Object row, Lisp_Object glyphs)
18742 {
18743 struct glyph_matrix *matrix;
18744 EMACS_INT vpos;
18745
18746 CHECK_NUMBER (row);
18747 matrix = XWINDOW (selected_window)->current_matrix;
18748 vpos = XINT (row);
18749 if (vpos >= 0 && vpos < matrix->nrows)
18750 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18751 vpos,
18752 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18753 return Qnil;
18754 }
18755
18756
18757 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18758 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18759 GLYPH 0 means don't dump glyphs.
18760 GLYPH 1 means dump glyphs in short form.
18761 GLYPH > 1 or omitted means dump glyphs in long form.
18762
18763 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18764 do nothing. */)
18765 (Lisp_Object row, Lisp_Object glyphs)
18766 {
18767 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18768 struct frame *sf = SELECTED_FRAME ();
18769 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18770 EMACS_INT vpos;
18771
18772 CHECK_NUMBER (row);
18773 vpos = XINT (row);
18774 if (vpos >= 0 && vpos < m->nrows)
18775 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18776 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18777 #endif
18778 return Qnil;
18779 }
18780
18781
18782 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18783 doc: /* Toggle tracing of redisplay.
18784 With ARG, turn tracing on if and only if ARG is positive. */)
18785 (Lisp_Object arg)
18786 {
18787 if (NILP (arg))
18788 trace_redisplay_p = !trace_redisplay_p;
18789 else
18790 {
18791 arg = Fprefix_numeric_value (arg);
18792 trace_redisplay_p = XINT (arg) > 0;
18793 }
18794
18795 return Qnil;
18796 }
18797
18798
18799 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18800 doc: /* Like `format', but print result to stderr.
18801 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18802 (ptrdiff_t nargs, Lisp_Object *args)
18803 {
18804 Lisp_Object s = Fformat (nargs, args);
18805 fprintf (stderr, "%s", SDATA (s));
18806 return Qnil;
18807 }
18808
18809 #endif /* GLYPH_DEBUG */
18810
18811
18812 \f
18813 /***********************************************************************
18814 Building Desired Matrix Rows
18815 ***********************************************************************/
18816
18817 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18818 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18819
18820 static struct glyph_row *
18821 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18822 {
18823 struct frame *f = XFRAME (WINDOW_FRAME (w));
18824 struct buffer *buffer = XBUFFER (w->contents);
18825 struct buffer *old = current_buffer;
18826 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18827 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18828 const unsigned char *arrow_end = arrow_string + arrow_len;
18829 const unsigned char *p;
18830 struct it it;
18831 bool multibyte_p;
18832 int n_glyphs_before;
18833
18834 set_buffer_temp (buffer);
18835 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18836 scratch_glyph_row.reversed_p = false;
18837 it.glyph_row->used[TEXT_AREA] = 0;
18838 SET_TEXT_POS (it.position, 0, 0);
18839
18840 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18841 p = arrow_string;
18842 while (p < arrow_end)
18843 {
18844 Lisp_Object face, ilisp;
18845
18846 /* Get the next character. */
18847 if (multibyte_p)
18848 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18849 else
18850 {
18851 it.c = it.char_to_display = *p, it.len = 1;
18852 if (! ASCII_CHAR_P (it.c))
18853 it.char_to_display = BYTE8_TO_CHAR (it.c);
18854 }
18855 p += it.len;
18856
18857 /* Get its face. */
18858 ilisp = make_number (p - arrow_string);
18859 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18860 it.face_id = compute_char_face (f, it.char_to_display, face);
18861
18862 /* Compute its width, get its glyphs. */
18863 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18864 SET_TEXT_POS (it.position, -1, -1);
18865 PRODUCE_GLYPHS (&it);
18866
18867 /* If this character doesn't fit any more in the line, we have
18868 to remove some glyphs. */
18869 if (it.current_x > it.last_visible_x)
18870 {
18871 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18872 break;
18873 }
18874 }
18875
18876 set_buffer_temp (old);
18877 return it.glyph_row;
18878 }
18879
18880
18881 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18882 glyphs to insert is determined by produce_special_glyphs. */
18883
18884 static void
18885 insert_left_trunc_glyphs (struct it *it)
18886 {
18887 struct it truncate_it;
18888 struct glyph *from, *end, *to, *toend;
18889
18890 eassert (!FRAME_WINDOW_P (it->f)
18891 || (!it->glyph_row->reversed_p
18892 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18893 || (it->glyph_row->reversed_p
18894 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18895
18896 /* Get the truncation glyphs. */
18897 truncate_it = *it;
18898 truncate_it.current_x = 0;
18899 truncate_it.face_id = DEFAULT_FACE_ID;
18900 truncate_it.glyph_row = &scratch_glyph_row;
18901 truncate_it.area = TEXT_AREA;
18902 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18903 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18904 truncate_it.object = make_number (0);
18905 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18906
18907 /* Overwrite glyphs from IT with truncation glyphs. */
18908 if (!it->glyph_row->reversed_p)
18909 {
18910 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18911
18912 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18913 end = from + tused;
18914 to = it->glyph_row->glyphs[TEXT_AREA];
18915 toend = to + it->glyph_row->used[TEXT_AREA];
18916 if (FRAME_WINDOW_P (it->f))
18917 {
18918 /* On GUI frames, when variable-size fonts are displayed,
18919 the truncation glyphs may need more pixels than the row's
18920 glyphs they overwrite. We overwrite more glyphs to free
18921 enough screen real estate, and enlarge the stretch glyph
18922 on the right (see display_line), if there is one, to
18923 preserve the screen position of the truncation glyphs on
18924 the right. */
18925 int w = 0;
18926 struct glyph *g = to;
18927 short used;
18928
18929 /* The first glyph could be partially visible, in which case
18930 it->glyph_row->x will be negative. But we want the left
18931 truncation glyphs to be aligned at the left margin of the
18932 window, so we override the x coordinate at which the row
18933 will begin. */
18934 it->glyph_row->x = 0;
18935 while (g < toend && w < it->truncation_pixel_width)
18936 {
18937 w += g->pixel_width;
18938 ++g;
18939 }
18940 if (g - to - tused > 0)
18941 {
18942 memmove (to + tused, g, (toend - g) * sizeof(*g));
18943 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18944 }
18945 used = it->glyph_row->used[TEXT_AREA];
18946 if (it->glyph_row->truncated_on_right_p
18947 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18948 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18949 == STRETCH_GLYPH)
18950 {
18951 int extra = w - it->truncation_pixel_width;
18952
18953 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18954 }
18955 }
18956
18957 while (from < end)
18958 *to++ = *from++;
18959
18960 /* There may be padding glyphs left over. Overwrite them too. */
18961 if (!FRAME_WINDOW_P (it->f))
18962 {
18963 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18964 {
18965 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18966 while (from < end)
18967 *to++ = *from++;
18968 }
18969 }
18970
18971 if (to > toend)
18972 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18973 }
18974 else
18975 {
18976 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18977
18978 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18979 that back to front. */
18980 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18981 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18982 toend = it->glyph_row->glyphs[TEXT_AREA];
18983 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18984 if (FRAME_WINDOW_P (it->f))
18985 {
18986 int w = 0;
18987 struct glyph *g = to;
18988
18989 while (g >= toend && w < it->truncation_pixel_width)
18990 {
18991 w += g->pixel_width;
18992 --g;
18993 }
18994 if (to - g - tused > 0)
18995 to = g + tused;
18996 if (it->glyph_row->truncated_on_right_p
18997 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18998 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18999 {
19000 int extra = w - it->truncation_pixel_width;
19001
19002 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19003 }
19004 }
19005
19006 while (from >= end && to >= toend)
19007 *to-- = *from--;
19008 if (!FRAME_WINDOW_P (it->f))
19009 {
19010 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19011 {
19012 from =
19013 truncate_it.glyph_row->glyphs[TEXT_AREA]
19014 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19015 while (from >= end && to >= toend)
19016 *to-- = *from--;
19017 }
19018 }
19019 if (from >= end)
19020 {
19021 /* Need to free some room before prepending additional
19022 glyphs. */
19023 int move_by = from - end + 1;
19024 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19025 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19026
19027 for ( ; g >= g0; g--)
19028 g[move_by] = *g;
19029 while (from >= end)
19030 *to-- = *from--;
19031 it->glyph_row->used[TEXT_AREA] += move_by;
19032 }
19033 }
19034 }
19035
19036 /* Compute the hash code for ROW. */
19037 unsigned
19038 row_hash (struct glyph_row *row)
19039 {
19040 int area, k;
19041 unsigned hashval = 0;
19042
19043 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19044 for (k = 0; k < row->used[area]; ++k)
19045 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19046 + row->glyphs[area][k].u.val
19047 + row->glyphs[area][k].face_id
19048 + row->glyphs[area][k].padding_p
19049 + (row->glyphs[area][k].type << 2));
19050
19051 return hashval;
19052 }
19053
19054 /* Compute the pixel height and width of IT->glyph_row.
19055
19056 Most of the time, ascent and height of a display line will be equal
19057 to the max_ascent and max_height values of the display iterator
19058 structure. This is not the case if
19059
19060 1. We hit ZV without displaying anything. In this case, max_ascent
19061 and max_height will be zero.
19062
19063 2. We have some glyphs that don't contribute to the line height.
19064 (The glyph row flag contributes_to_line_height_p is for future
19065 pixmap extensions).
19066
19067 The first case is easily covered by using default values because in
19068 these cases, the line height does not really matter, except that it
19069 must not be zero. */
19070
19071 static void
19072 compute_line_metrics (struct it *it)
19073 {
19074 struct glyph_row *row = it->glyph_row;
19075
19076 if (FRAME_WINDOW_P (it->f))
19077 {
19078 int i, min_y, max_y;
19079
19080 /* The line may consist of one space only, that was added to
19081 place the cursor on it. If so, the row's height hasn't been
19082 computed yet. */
19083 if (row->height == 0)
19084 {
19085 if (it->max_ascent + it->max_descent == 0)
19086 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19087 row->ascent = it->max_ascent;
19088 row->height = it->max_ascent + it->max_descent;
19089 row->phys_ascent = it->max_phys_ascent;
19090 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19091 row->extra_line_spacing = it->max_extra_line_spacing;
19092 }
19093
19094 /* Compute the width of this line. */
19095 row->pixel_width = row->x;
19096 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19097 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19098
19099 eassert (row->pixel_width >= 0);
19100 eassert (row->ascent >= 0 && row->height > 0);
19101
19102 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19103 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19104
19105 /* If first line's physical ascent is larger than its logical
19106 ascent, use the physical ascent, and make the row taller.
19107 This makes accented characters fully visible. */
19108 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19109 && row->phys_ascent > row->ascent)
19110 {
19111 row->height += row->phys_ascent - row->ascent;
19112 row->ascent = row->phys_ascent;
19113 }
19114
19115 /* Compute how much of the line is visible. */
19116 row->visible_height = row->height;
19117
19118 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19119 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19120
19121 if (row->y < min_y)
19122 row->visible_height -= min_y - row->y;
19123 if (row->y + row->height > max_y)
19124 row->visible_height -= row->y + row->height - max_y;
19125 }
19126 else
19127 {
19128 row->pixel_width = row->used[TEXT_AREA];
19129 if (row->continued_p)
19130 row->pixel_width -= it->continuation_pixel_width;
19131 else if (row->truncated_on_right_p)
19132 row->pixel_width -= it->truncation_pixel_width;
19133 row->ascent = row->phys_ascent = 0;
19134 row->height = row->phys_height = row->visible_height = 1;
19135 row->extra_line_spacing = 0;
19136 }
19137
19138 /* Compute a hash code for this row. */
19139 row->hash = row_hash (row);
19140
19141 it->max_ascent = it->max_descent = 0;
19142 it->max_phys_ascent = it->max_phys_descent = 0;
19143 }
19144
19145
19146 /* Append one space to the glyph row of iterator IT if doing a
19147 window-based redisplay. The space has the same face as
19148 IT->face_id. Value is non-zero if a space was added.
19149
19150 This function is called to make sure that there is always one glyph
19151 at the end of a glyph row that the cursor can be set on under
19152 window-systems. (If there weren't such a glyph we would not know
19153 how wide and tall a box cursor should be displayed).
19154
19155 At the same time this space let's a nicely handle clearing to the
19156 end of the line if the row ends in italic text. */
19157
19158 static int
19159 append_space_for_newline (struct it *it, int default_face_p)
19160 {
19161 if (FRAME_WINDOW_P (it->f))
19162 {
19163 int n = it->glyph_row->used[TEXT_AREA];
19164
19165 if (it->glyph_row->glyphs[TEXT_AREA] + n
19166 < it->glyph_row->glyphs[1 + TEXT_AREA])
19167 {
19168 /* Save some values that must not be changed.
19169 Must save IT->c and IT->len because otherwise
19170 ITERATOR_AT_END_P wouldn't work anymore after
19171 append_space_for_newline has been called. */
19172 enum display_element_type saved_what = it->what;
19173 int saved_c = it->c, saved_len = it->len;
19174 int saved_char_to_display = it->char_to_display;
19175 int saved_x = it->current_x;
19176 int saved_face_id = it->face_id;
19177 int saved_box_end = it->end_of_box_run_p;
19178 struct text_pos saved_pos;
19179 Lisp_Object saved_object;
19180 struct face *face;
19181
19182 saved_object = it->object;
19183 saved_pos = it->position;
19184
19185 it->what = IT_CHARACTER;
19186 memset (&it->position, 0, sizeof it->position);
19187 it->object = make_number (0);
19188 it->c = it->char_to_display = ' ';
19189 it->len = 1;
19190
19191 /* If the default face was remapped, be sure to use the
19192 remapped face for the appended newline. */
19193 if (default_face_p)
19194 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19195 else if (it->face_before_selective_p)
19196 it->face_id = it->saved_face_id;
19197 face = FACE_FROM_ID (it->f, it->face_id);
19198 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19199 /* In R2L rows, we will prepend a stretch glyph that will
19200 have the end_of_box_run_p flag set for it, so there's no
19201 need for the appended newline glyph to have that flag
19202 set. */
19203 if (it->glyph_row->reversed_p
19204 /* But if the appended newline glyph goes all the way to
19205 the end of the row, there will be no stretch glyph,
19206 so leave the box flag set. */
19207 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19208 it->end_of_box_run_p = 0;
19209
19210 PRODUCE_GLYPHS (it);
19211
19212 it->override_ascent = -1;
19213 it->constrain_row_ascent_descent_p = 0;
19214 it->current_x = saved_x;
19215 it->object = saved_object;
19216 it->position = saved_pos;
19217 it->what = saved_what;
19218 it->face_id = saved_face_id;
19219 it->len = saved_len;
19220 it->c = saved_c;
19221 it->char_to_display = saved_char_to_display;
19222 it->end_of_box_run_p = saved_box_end;
19223 return 1;
19224 }
19225 }
19226
19227 return 0;
19228 }
19229
19230
19231 /* Extend the face of the last glyph in the text area of IT->glyph_row
19232 to the end of the display line. Called from display_line. If the
19233 glyph row is empty, add a space glyph to it so that we know the
19234 face to draw. Set the glyph row flag fill_line_p. If the glyph
19235 row is R2L, prepend a stretch glyph to cover the empty space to the
19236 left of the leftmost glyph. */
19237
19238 static void
19239 extend_face_to_end_of_line (struct it *it)
19240 {
19241 struct face *face, *default_face;
19242 struct frame *f = it->f;
19243
19244 /* If line is already filled, do nothing. Non window-system frames
19245 get a grace of one more ``pixel'' because their characters are
19246 1-``pixel'' wide, so they hit the equality too early. This grace
19247 is needed only for R2L rows that are not continued, to produce
19248 one extra blank where we could display the cursor. */
19249 if ((it->current_x >= it->last_visible_x
19250 + (!FRAME_WINDOW_P (f)
19251 && it->glyph_row->reversed_p
19252 && !it->glyph_row->continued_p))
19253 /* If the window has display margins, we will need to extend
19254 their face even if the text area is filled. */
19255 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19256 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19257 return;
19258
19259 /* The default face, possibly remapped. */
19260 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19261
19262 /* Face extension extends the background and box of IT->face_id
19263 to the end of the line. If the background equals the background
19264 of the frame, we don't have to do anything. */
19265 if (it->face_before_selective_p)
19266 face = FACE_FROM_ID (f, it->saved_face_id);
19267 else
19268 face = FACE_FROM_ID (f, it->face_id);
19269
19270 if (FRAME_WINDOW_P (f)
19271 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19272 && face->box == FACE_NO_BOX
19273 && face->background == FRAME_BACKGROUND_PIXEL (f)
19274 #ifdef HAVE_WINDOW_SYSTEM
19275 && !face->stipple
19276 #endif
19277 && !it->glyph_row->reversed_p)
19278 return;
19279
19280 /* Set the glyph row flag indicating that the face of the last glyph
19281 in the text area has to be drawn to the end of the text area. */
19282 it->glyph_row->fill_line_p = 1;
19283
19284 /* If current character of IT is not ASCII, make sure we have the
19285 ASCII face. This will be automatically undone the next time
19286 get_next_display_element returns a multibyte character. Note
19287 that the character will always be single byte in unibyte
19288 text. */
19289 if (!ASCII_CHAR_P (it->c))
19290 {
19291 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19292 }
19293
19294 if (FRAME_WINDOW_P (f))
19295 {
19296 /* If the row is empty, add a space with the current face of IT,
19297 so that we know which face to draw. */
19298 if (it->glyph_row->used[TEXT_AREA] == 0)
19299 {
19300 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19301 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19302 it->glyph_row->used[TEXT_AREA] = 1;
19303 }
19304 /* Mode line and the header line don't have margins, and
19305 likewise the frame's tool-bar window, if there is any. */
19306 if (!(it->glyph_row->mode_line_p
19307 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19308 || (WINDOWP (f->tool_bar_window)
19309 && it->w == XWINDOW (f->tool_bar_window))
19310 #endif
19311 ))
19312 {
19313 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19314 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19315 {
19316 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19317 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19318 default_face->id;
19319 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19320 }
19321 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19322 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19323 {
19324 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19325 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19326 default_face->id;
19327 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19328 }
19329 }
19330 #ifdef HAVE_WINDOW_SYSTEM
19331 if (it->glyph_row->reversed_p)
19332 {
19333 /* Prepend a stretch glyph to the row, such that the
19334 rightmost glyph will be drawn flushed all the way to the
19335 right margin of the window. The stretch glyph that will
19336 occupy the empty space, if any, to the left of the
19337 glyphs. */
19338 struct font *font = face->font ? face->font : FRAME_FONT (f);
19339 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19340 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19341 struct glyph *g;
19342 int row_width, stretch_ascent, stretch_width;
19343 struct text_pos saved_pos;
19344 int saved_face_id, saved_avoid_cursor, saved_box_start;
19345
19346 for (row_width = 0, g = row_start; g < row_end; g++)
19347 row_width += g->pixel_width;
19348
19349 /* FIXME: There are various minor display glitches in R2L
19350 rows when only one of the fringes is missing. The
19351 strange condition below produces the least bad effect. */
19352 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19353 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19354 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19355 stretch_width = window_box_width (it->w, TEXT_AREA);
19356 else
19357 stretch_width = it->last_visible_x - it->first_visible_x;
19358 stretch_width -= row_width;
19359
19360 if (stretch_width > 0)
19361 {
19362 stretch_ascent =
19363 (((it->ascent + it->descent)
19364 * FONT_BASE (font)) / FONT_HEIGHT (font));
19365 saved_pos = it->position;
19366 memset (&it->position, 0, sizeof it->position);
19367 saved_avoid_cursor = it->avoid_cursor_p;
19368 it->avoid_cursor_p = 1;
19369 saved_face_id = it->face_id;
19370 saved_box_start = it->start_of_box_run_p;
19371 /* The last row's stretch glyph should get the default
19372 face, to avoid painting the rest of the window with
19373 the region face, if the region ends at ZV. */
19374 if (it->glyph_row->ends_at_zv_p)
19375 it->face_id = default_face->id;
19376 else
19377 it->face_id = face->id;
19378 it->start_of_box_run_p = 0;
19379 append_stretch_glyph (it, make_number (0), stretch_width,
19380 it->ascent + it->descent, stretch_ascent);
19381 it->position = saved_pos;
19382 it->avoid_cursor_p = saved_avoid_cursor;
19383 it->face_id = saved_face_id;
19384 it->start_of_box_run_p = saved_box_start;
19385 }
19386 /* If stretch_width comes out negative, it means that the
19387 last glyph is only partially visible. In R2L rows, we
19388 want the leftmost glyph to be partially visible, so we
19389 need to give the row the corresponding left offset. */
19390 if (stretch_width < 0)
19391 it->glyph_row->x = stretch_width;
19392 }
19393 #endif /* HAVE_WINDOW_SYSTEM */
19394 }
19395 else
19396 {
19397 /* Save some values that must not be changed. */
19398 int saved_x = it->current_x;
19399 struct text_pos saved_pos;
19400 Lisp_Object saved_object;
19401 enum display_element_type saved_what = it->what;
19402 int saved_face_id = it->face_id;
19403
19404 saved_object = it->object;
19405 saved_pos = it->position;
19406
19407 it->what = IT_CHARACTER;
19408 memset (&it->position, 0, sizeof it->position);
19409 it->object = make_number (0);
19410 it->c = it->char_to_display = ' ';
19411 it->len = 1;
19412
19413 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19414 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19415 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19416 && !it->glyph_row->mode_line_p
19417 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19418 {
19419 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19420 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19421
19422 for (it->current_x = 0; g < e; g++)
19423 it->current_x += g->pixel_width;
19424
19425 it->area = LEFT_MARGIN_AREA;
19426 it->face_id = default_face->id;
19427 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19428 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19429 {
19430 PRODUCE_GLYPHS (it);
19431 /* term.c:produce_glyphs advances it->current_x only for
19432 TEXT_AREA. */
19433 it->current_x += it->pixel_width;
19434 }
19435
19436 it->current_x = saved_x;
19437 it->area = TEXT_AREA;
19438 }
19439
19440 /* The last row's blank glyphs should get the default face, to
19441 avoid painting the rest of the window with the region face,
19442 if the region ends at ZV. */
19443 if (it->glyph_row->ends_at_zv_p)
19444 it->face_id = default_face->id;
19445 else
19446 it->face_id = face->id;
19447 PRODUCE_GLYPHS (it);
19448
19449 while (it->current_x <= it->last_visible_x)
19450 PRODUCE_GLYPHS (it);
19451
19452 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19453 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19454 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19455 && !it->glyph_row->mode_line_p
19456 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19457 {
19458 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19459 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19460
19461 for ( ; g < e; g++)
19462 it->current_x += g->pixel_width;
19463
19464 it->area = RIGHT_MARGIN_AREA;
19465 it->face_id = default_face->id;
19466 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19467 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19468 {
19469 PRODUCE_GLYPHS (it);
19470 it->current_x += it->pixel_width;
19471 }
19472
19473 it->area = TEXT_AREA;
19474 }
19475
19476 /* Don't count these blanks really. It would let us insert a left
19477 truncation glyph below and make us set the cursor on them, maybe. */
19478 it->current_x = saved_x;
19479 it->object = saved_object;
19480 it->position = saved_pos;
19481 it->what = saved_what;
19482 it->face_id = saved_face_id;
19483 }
19484 }
19485
19486
19487 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19488 trailing whitespace. */
19489
19490 static int
19491 trailing_whitespace_p (ptrdiff_t charpos)
19492 {
19493 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19494 int c = 0;
19495
19496 while (bytepos < ZV_BYTE
19497 && (c = FETCH_CHAR (bytepos),
19498 c == ' ' || c == '\t'))
19499 ++bytepos;
19500
19501 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19502 {
19503 if (bytepos != PT_BYTE)
19504 return 1;
19505 }
19506 return 0;
19507 }
19508
19509
19510 /* Highlight trailing whitespace, if any, in ROW. */
19511
19512 static void
19513 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19514 {
19515 int used = row->used[TEXT_AREA];
19516
19517 if (used)
19518 {
19519 struct glyph *start = row->glyphs[TEXT_AREA];
19520 struct glyph *glyph = start + used - 1;
19521
19522 if (row->reversed_p)
19523 {
19524 /* Right-to-left rows need to be processed in the opposite
19525 direction, so swap the edge pointers. */
19526 glyph = start;
19527 start = row->glyphs[TEXT_AREA] + used - 1;
19528 }
19529
19530 /* Skip over glyphs inserted to display the cursor at the
19531 end of a line, for extending the face of the last glyph
19532 to the end of the line on terminals, and for truncation
19533 and continuation glyphs. */
19534 if (!row->reversed_p)
19535 {
19536 while (glyph >= start
19537 && glyph->type == CHAR_GLYPH
19538 && INTEGERP (glyph->object))
19539 --glyph;
19540 }
19541 else
19542 {
19543 while (glyph <= start
19544 && glyph->type == CHAR_GLYPH
19545 && INTEGERP (glyph->object))
19546 ++glyph;
19547 }
19548
19549 /* If last glyph is a space or stretch, and it's trailing
19550 whitespace, set the face of all trailing whitespace glyphs in
19551 IT->glyph_row to `trailing-whitespace'. */
19552 if ((row->reversed_p ? glyph <= start : glyph >= start)
19553 && BUFFERP (glyph->object)
19554 && (glyph->type == STRETCH_GLYPH
19555 || (glyph->type == CHAR_GLYPH
19556 && glyph->u.ch == ' '))
19557 && trailing_whitespace_p (glyph->charpos))
19558 {
19559 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19560 if (face_id < 0)
19561 return;
19562
19563 if (!row->reversed_p)
19564 {
19565 while (glyph >= start
19566 && BUFFERP (glyph->object)
19567 && (glyph->type == STRETCH_GLYPH
19568 || (glyph->type == CHAR_GLYPH
19569 && glyph->u.ch == ' ')))
19570 (glyph--)->face_id = face_id;
19571 }
19572 else
19573 {
19574 while (glyph <= start
19575 && BUFFERP (glyph->object)
19576 && (glyph->type == STRETCH_GLYPH
19577 || (glyph->type == CHAR_GLYPH
19578 && glyph->u.ch == ' ')))
19579 (glyph++)->face_id = face_id;
19580 }
19581 }
19582 }
19583 }
19584
19585
19586 /* Value is non-zero if glyph row ROW should be
19587 considered to hold the buffer position CHARPOS. */
19588
19589 static int
19590 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19591 {
19592 int result = 1;
19593
19594 if (charpos == CHARPOS (row->end.pos)
19595 || charpos == MATRIX_ROW_END_CHARPOS (row))
19596 {
19597 /* Suppose the row ends on a string.
19598 Unless the row is continued, that means it ends on a newline
19599 in the string. If it's anything other than a display string
19600 (e.g., a before-string from an overlay), we don't want the
19601 cursor there. (This heuristic seems to give the optimal
19602 behavior for the various types of multi-line strings.)
19603 One exception: if the string has `cursor' property on one of
19604 its characters, we _do_ want the cursor there. */
19605 if (CHARPOS (row->end.string_pos) >= 0)
19606 {
19607 if (row->continued_p)
19608 result = 1;
19609 else
19610 {
19611 /* Check for `display' property. */
19612 struct glyph *beg = row->glyphs[TEXT_AREA];
19613 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19614 struct glyph *glyph;
19615
19616 result = 0;
19617 for (glyph = end; glyph >= beg; --glyph)
19618 if (STRINGP (glyph->object))
19619 {
19620 Lisp_Object prop
19621 = Fget_char_property (make_number (charpos),
19622 Qdisplay, Qnil);
19623 result =
19624 (!NILP (prop)
19625 && display_prop_string_p (prop, glyph->object));
19626 /* If there's a `cursor' property on one of the
19627 string's characters, this row is a cursor row,
19628 even though this is not a display string. */
19629 if (!result)
19630 {
19631 Lisp_Object s = glyph->object;
19632
19633 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19634 {
19635 ptrdiff_t gpos = glyph->charpos;
19636
19637 if (!NILP (Fget_char_property (make_number (gpos),
19638 Qcursor, s)))
19639 {
19640 result = 1;
19641 break;
19642 }
19643 }
19644 }
19645 break;
19646 }
19647 }
19648 }
19649 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19650 {
19651 /* If the row ends in middle of a real character,
19652 and the line is continued, we want the cursor here.
19653 That's because CHARPOS (ROW->end.pos) would equal
19654 PT if PT is before the character. */
19655 if (!row->ends_in_ellipsis_p)
19656 result = row->continued_p;
19657 else
19658 /* If the row ends in an ellipsis, then
19659 CHARPOS (ROW->end.pos) will equal point after the
19660 invisible text. We want that position to be displayed
19661 after the ellipsis. */
19662 result = 0;
19663 }
19664 /* If the row ends at ZV, display the cursor at the end of that
19665 row instead of at the start of the row below. */
19666 else if (row->ends_at_zv_p)
19667 result = 1;
19668 else
19669 result = 0;
19670 }
19671
19672 return result;
19673 }
19674
19675 /* Value is non-zero if glyph row ROW should be
19676 used to hold the cursor. */
19677
19678 static int
19679 cursor_row_p (struct glyph_row *row)
19680 {
19681 return row_for_charpos_p (row, PT);
19682 }
19683
19684 \f
19685
19686 /* Push the property PROP so that it will be rendered at the current
19687 position in IT. Return 1 if PROP was successfully pushed, 0
19688 otherwise. Called from handle_line_prefix to handle the
19689 `line-prefix' and `wrap-prefix' properties. */
19690
19691 static int
19692 push_prefix_prop (struct it *it, Lisp_Object prop)
19693 {
19694 struct text_pos pos =
19695 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19696
19697 eassert (it->method == GET_FROM_BUFFER
19698 || it->method == GET_FROM_DISPLAY_VECTOR
19699 || it->method == GET_FROM_STRING);
19700
19701 /* We need to save the current buffer/string position, so it will be
19702 restored by pop_it, because iterate_out_of_display_property
19703 depends on that being set correctly, but some situations leave
19704 it->position not yet set when this function is called. */
19705 push_it (it, &pos);
19706
19707 if (STRINGP (prop))
19708 {
19709 if (SCHARS (prop) == 0)
19710 {
19711 pop_it (it);
19712 return 0;
19713 }
19714
19715 it->string = prop;
19716 it->string_from_prefix_prop_p = 1;
19717 it->multibyte_p = STRING_MULTIBYTE (it->string);
19718 it->current.overlay_string_index = -1;
19719 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19720 it->end_charpos = it->string_nchars = SCHARS (it->string);
19721 it->method = GET_FROM_STRING;
19722 it->stop_charpos = 0;
19723 it->prev_stop = 0;
19724 it->base_level_stop = 0;
19725
19726 /* Force paragraph direction to be that of the parent
19727 buffer/string. */
19728 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19729 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19730 else
19731 it->paragraph_embedding = L2R;
19732
19733 /* Set up the bidi iterator for this display string. */
19734 if (it->bidi_p)
19735 {
19736 it->bidi_it.string.lstring = it->string;
19737 it->bidi_it.string.s = NULL;
19738 it->bidi_it.string.schars = it->end_charpos;
19739 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19740 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19741 it->bidi_it.string.unibyte = !it->multibyte_p;
19742 it->bidi_it.w = it->w;
19743 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19744 }
19745 }
19746 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19747 {
19748 it->method = GET_FROM_STRETCH;
19749 it->object = prop;
19750 }
19751 #ifdef HAVE_WINDOW_SYSTEM
19752 else if (IMAGEP (prop))
19753 {
19754 it->what = IT_IMAGE;
19755 it->image_id = lookup_image (it->f, prop);
19756 it->method = GET_FROM_IMAGE;
19757 }
19758 #endif /* HAVE_WINDOW_SYSTEM */
19759 else
19760 {
19761 pop_it (it); /* bogus display property, give up */
19762 return 0;
19763 }
19764
19765 return 1;
19766 }
19767
19768 /* Return the character-property PROP at the current position in IT. */
19769
19770 static Lisp_Object
19771 get_it_property (struct it *it, Lisp_Object prop)
19772 {
19773 Lisp_Object position, object = it->object;
19774
19775 if (STRINGP (object))
19776 position = make_number (IT_STRING_CHARPOS (*it));
19777 else if (BUFFERP (object))
19778 {
19779 position = make_number (IT_CHARPOS (*it));
19780 object = it->window;
19781 }
19782 else
19783 return Qnil;
19784
19785 return Fget_char_property (position, prop, object);
19786 }
19787
19788 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19789
19790 static void
19791 handle_line_prefix (struct it *it)
19792 {
19793 Lisp_Object prefix;
19794
19795 if (it->continuation_lines_width > 0)
19796 {
19797 prefix = get_it_property (it, Qwrap_prefix);
19798 if (NILP (prefix))
19799 prefix = Vwrap_prefix;
19800 }
19801 else
19802 {
19803 prefix = get_it_property (it, Qline_prefix);
19804 if (NILP (prefix))
19805 prefix = Vline_prefix;
19806 }
19807 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19808 {
19809 /* If the prefix is wider than the window, and we try to wrap
19810 it, it would acquire its own wrap prefix, and so on till the
19811 iterator stack overflows. So, don't wrap the prefix. */
19812 it->line_wrap = TRUNCATE;
19813 it->avoid_cursor_p = 1;
19814 }
19815 }
19816
19817 \f
19818
19819 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19820 only for R2L lines from display_line and display_string, when they
19821 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19822 the line/string needs to be continued on the next glyph row. */
19823 static void
19824 unproduce_glyphs (struct it *it, int n)
19825 {
19826 struct glyph *glyph, *end;
19827
19828 eassert (it->glyph_row);
19829 eassert (it->glyph_row->reversed_p);
19830 eassert (it->area == TEXT_AREA);
19831 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19832
19833 if (n > it->glyph_row->used[TEXT_AREA])
19834 n = it->glyph_row->used[TEXT_AREA];
19835 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19836 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19837 for ( ; glyph < end; glyph++)
19838 glyph[-n] = *glyph;
19839 }
19840
19841 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19842 and ROW->maxpos. */
19843 static void
19844 find_row_edges (struct it *it, struct glyph_row *row,
19845 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19846 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19847 {
19848 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19849 lines' rows is implemented for bidi-reordered rows. */
19850
19851 /* ROW->minpos is the value of min_pos, the minimal buffer position
19852 we have in ROW, or ROW->start.pos if that is smaller. */
19853 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19854 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19855 else
19856 /* We didn't find buffer positions smaller than ROW->start, or
19857 didn't find _any_ valid buffer positions in any of the glyphs,
19858 so we must trust the iterator's computed positions. */
19859 row->minpos = row->start.pos;
19860 if (max_pos <= 0)
19861 {
19862 max_pos = CHARPOS (it->current.pos);
19863 max_bpos = BYTEPOS (it->current.pos);
19864 }
19865
19866 /* Here are the various use-cases for ending the row, and the
19867 corresponding values for ROW->maxpos:
19868
19869 Line ends in a newline from buffer eol_pos + 1
19870 Line is continued from buffer max_pos + 1
19871 Line is truncated on right it->current.pos
19872 Line ends in a newline from string max_pos + 1(*)
19873 (*) + 1 only when line ends in a forward scan
19874 Line is continued from string max_pos
19875 Line is continued from display vector max_pos
19876 Line is entirely from a string min_pos == max_pos
19877 Line is entirely from a display vector min_pos == max_pos
19878 Line that ends at ZV ZV
19879
19880 If you discover other use-cases, please add them here as
19881 appropriate. */
19882 if (row->ends_at_zv_p)
19883 row->maxpos = it->current.pos;
19884 else if (row->used[TEXT_AREA])
19885 {
19886 int seen_this_string = 0;
19887 struct glyph_row *r1 = row - 1;
19888
19889 /* Did we see the same display string on the previous row? */
19890 if (STRINGP (it->object)
19891 /* this is not the first row */
19892 && row > it->w->desired_matrix->rows
19893 /* previous row is not the header line */
19894 && !r1->mode_line_p
19895 /* previous row also ends in a newline from a string */
19896 && r1->ends_in_newline_from_string_p)
19897 {
19898 struct glyph *start, *end;
19899
19900 /* Search for the last glyph of the previous row that came
19901 from buffer or string. Depending on whether the row is
19902 L2R or R2L, we need to process it front to back or the
19903 other way round. */
19904 if (!r1->reversed_p)
19905 {
19906 start = r1->glyphs[TEXT_AREA];
19907 end = start + r1->used[TEXT_AREA];
19908 /* Glyphs inserted by redisplay have an integer (zero)
19909 as their object. */
19910 while (end > start
19911 && INTEGERP ((end - 1)->object)
19912 && (end - 1)->charpos <= 0)
19913 --end;
19914 if (end > start)
19915 {
19916 if (EQ ((end - 1)->object, it->object))
19917 seen_this_string = 1;
19918 }
19919 else
19920 /* If all the glyphs of the previous row were inserted
19921 by redisplay, it means the previous row was
19922 produced from a single newline, which is only
19923 possible if that newline came from the same string
19924 as the one which produced this ROW. */
19925 seen_this_string = 1;
19926 }
19927 else
19928 {
19929 end = r1->glyphs[TEXT_AREA] - 1;
19930 start = end + r1->used[TEXT_AREA];
19931 while (end < start
19932 && INTEGERP ((end + 1)->object)
19933 && (end + 1)->charpos <= 0)
19934 ++end;
19935 if (end < start)
19936 {
19937 if (EQ ((end + 1)->object, it->object))
19938 seen_this_string = 1;
19939 }
19940 else
19941 seen_this_string = 1;
19942 }
19943 }
19944 /* Take note of each display string that covers a newline only
19945 once, the first time we see it. This is for when a display
19946 string includes more than one newline in it. */
19947 if (row->ends_in_newline_from_string_p && !seen_this_string)
19948 {
19949 /* If we were scanning the buffer forward when we displayed
19950 the string, we want to account for at least one buffer
19951 position that belongs to this row (position covered by
19952 the display string), so that cursor positioning will
19953 consider this row as a candidate when point is at the end
19954 of the visual line represented by this row. This is not
19955 required when scanning back, because max_pos will already
19956 have a much larger value. */
19957 if (CHARPOS (row->end.pos) > max_pos)
19958 INC_BOTH (max_pos, max_bpos);
19959 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19960 }
19961 else if (CHARPOS (it->eol_pos) > 0)
19962 SET_TEXT_POS (row->maxpos,
19963 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19964 else if (row->continued_p)
19965 {
19966 /* If max_pos is different from IT's current position, it
19967 means IT->method does not belong to the display element
19968 at max_pos. However, it also means that the display
19969 element at max_pos was displayed in its entirety on this
19970 line, which is equivalent to saying that the next line
19971 starts at the next buffer position. */
19972 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19973 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19974 else
19975 {
19976 INC_BOTH (max_pos, max_bpos);
19977 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19978 }
19979 }
19980 else if (row->truncated_on_right_p)
19981 /* display_line already called reseat_at_next_visible_line_start,
19982 which puts the iterator at the beginning of the next line, in
19983 the logical order. */
19984 row->maxpos = it->current.pos;
19985 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19986 /* A line that is entirely from a string/image/stretch... */
19987 row->maxpos = row->minpos;
19988 else
19989 emacs_abort ();
19990 }
19991 else
19992 row->maxpos = it->current.pos;
19993 }
19994
19995 /* Construct the glyph row IT->glyph_row in the desired matrix of
19996 IT->w from text at the current position of IT. See dispextern.h
19997 for an overview of struct it. Value is non-zero if
19998 IT->glyph_row displays text, as opposed to a line displaying ZV
19999 only. */
20000
20001 static int
20002 display_line (struct it *it)
20003 {
20004 struct glyph_row *row = it->glyph_row;
20005 Lisp_Object overlay_arrow_string;
20006 struct it wrap_it;
20007 void *wrap_data = NULL;
20008 int may_wrap = 0, wrap_x IF_LINT (= 0);
20009 int wrap_row_used = -1;
20010 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20011 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20012 int wrap_row_extra_line_spacing IF_LINT (= 0);
20013 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20014 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20015 int cvpos;
20016 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20017 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20018 bool pending_handle_line_prefix = false;
20019
20020 /* We always start displaying at hpos zero even if hscrolled. */
20021 eassert (it->hpos == 0 && it->current_x == 0);
20022
20023 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20024 >= it->w->desired_matrix->nrows)
20025 {
20026 it->w->nrows_scale_factor++;
20027 it->f->fonts_changed = 1;
20028 return 0;
20029 }
20030
20031 /* Clear the result glyph row and enable it. */
20032 prepare_desired_row (it->w, row, false);
20033
20034 row->y = it->current_y;
20035 row->start = it->start;
20036 row->continuation_lines_width = it->continuation_lines_width;
20037 row->displays_text_p = 1;
20038 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20039 it->starts_in_middle_of_char_p = 0;
20040
20041 /* Arrange the overlays nicely for our purposes. Usually, we call
20042 display_line on only one line at a time, in which case this
20043 can't really hurt too much, or we call it on lines which appear
20044 one after another in the buffer, in which case all calls to
20045 recenter_overlay_lists but the first will be pretty cheap. */
20046 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20047
20048 /* Move over display elements that are not visible because we are
20049 hscrolled. This may stop at an x-position < IT->first_visible_x
20050 if the first glyph is partially visible or if we hit a line end. */
20051 if (it->current_x < it->first_visible_x)
20052 {
20053 enum move_it_result move_result;
20054
20055 this_line_min_pos = row->start.pos;
20056 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20057 MOVE_TO_POS | MOVE_TO_X);
20058 /* If we are under a large hscroll, move_it_in_display_line_to
20059 could hit the end of the line without reaching
20060 it->first_visible_x. Pretend that we did reach it. This is
20061 especially important on a TTY, where we will call
20062 extend_face_to_end_of_line, which needs to know how many
20063 blank glyphs to produce. */
20064 if (it->current_x < it->first_visible_x
20065 && (move_result == MOVE_NEWLINE_OR_CR
20066 || move_result == MOVE_POS_MATCH_OR_ZV))
20067 it->current_x = it->first_visible_x;
20068
20069 /* Record the smallest positions seen while we moved over
20070 display elements that are not visible. This is needed by
20071 redisplay_internal for optimizing the case where the cursor
20072 stays inside the same line. The rest of this function only
20073 considers positions that are actually displayed, so
20074 RECORD_MAX_MIN_POS will not otherwise record positions that
20075 are hscrolled to the left of the left edge of the window. */
20076 min_pos = CHARPOS (this_line_min_pos);
20077 min_bpos = BYTEPOS (this_line_min_pos);
20078 }
20079 else if (it->area == TEXT_AREA)
20080 {
20081 /* We only do this when not calling move_it_in_display_line_to
20082 above, because that function calls itself handle_line_prefix. */
20083 handle_line_prefix (it);
20084 }
20085 else
20086 {
20087 /* Line-prefix and wrap-prefix are always displayed in the text
20088 area. But if this is the first call to display_line after
20089 init_iterator, the iterator might have been set up to write
20090 into a marginal area, e.g. if the line begins with some
20091 display property that writes to the margins. So we need to
20092 wait with the call to handle_line_prefix until whatever
20093 writes to the margin has done its job. */
20094 pending_handle_line_prefix = true;
20095 }
20096
20097 /* Get the initial row height. This is either the height of the
20098 text hscrolled, if there is any, or zero. */
20099 row->ascent = it->max_ascent;
20100 row->height = it->max_ascent + it->max_descent;
20101 row->phys_ascent = it->max_phys_ascent;
20102 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20103 row->extra_line_spacing = it->max_extra_line_spacing;
20104
20105 /* Utility macro to record max and min buffer positions seen until now. */
20106 #define RECORD_MAX_MIN_POS(IT) \
20107 do \
20108 { \
20109 int composition_p = !STRINGP ((IT)->string) \
20110 && ((IT)->what == IT_COMPOSITION); \
20111 ptrdiff_t current_pos = \
20112 composition_p ? (IT)->cmp_it.charpos \
20113 : IT_CHARPOS (*(IT)); \
20114 ptrdiff_t current_bpos = \
20115 composition_p ? CHAR_TO_BYTE (current_pos) \
20116 : IT_BYTEPOS (*(IT)); \
20117 if (current_pos < min_pos) \
20118 { \
20119 min_pos = current_pos; \
20120 min_bpos = current_bpos; \
20121 } \
20122 if (IT_CHARPOS (*it) > max_pos) \
20123 { \
20124 max_pos = IT_CHARPOS (*it); \
20125 max_bpos = IT_BYTEPOS (*it); \
20126 } \
20127 } \
20128 while (0)
20129
20130 /* Loop generating characters. The loop is left with IT on the next
20131 character to display. */
20132 while (1)
20133 {
20134 int n_glyphs_before, hpos_before, x_before;
20135 int x, nglyphs;
20136 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20137
20138 /* Retrieve the next thing to display. Value is zero if end of
20139 buffer reached. */
20140 if (!get_next_display_element (it))
20141 {
20142 /* Maybe add a space at the end of this line that is used to
20143 display the cursor there under X. Set the charpos of the
20144 first glyph of blank lines not corresponding to any text
20145 to -1. */
20146 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20147 row->exact_window_width_line_p = 1;
20148 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20149 || row->used[TEXT_AREA] == 0)
20150 {
20151 row->glyphs[TEXT_AREA]->charpos = -1;
20152 row->displays_text_p = 0;
20153
20154 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20155 && (!MINI_WINDOW_P (it->w)
20156 || (minibuf_level && EQ (it->window, minibuf_window))))
20157 row->indicate_empty_line_p = 1;
20158 }
20159
20160 it->continuation_lines_width = 0;
20161 row->ends_at_zv_p = 1;
20162 /* A row that displays right-to-left text must always have
20163 its last face extended all the way to the end of line,
20164 even if this row ends in ZV, because we still write to
20165 the screen left to right. We also need to extend the
20166 last face if the default face is remapped to some
20167 different face, otherwise the functions that clear
20168 portions of the screen will clear with the default face's
20169 background color. */
20170 if (row->reversed_p
20171 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20172 extend_face_to_end_of_line (it);
20173 break;
20174 }
20175
20176 /* Now, get the metrics of what we want to display. This also
20177 generates glyphs in `row' (which is IT->glyph_row). */
20178 n_glyphs_before = row->used[TEXT_AREA];
20179 x = it->current_x;
20180
20181 /* Remember the line height so far in case the next element doesn't
20182 fit on the line. */
20183 if (it->line_wrap != TRUNCATE)
20184 {
20185 ascent = it->max_ascent;
20186 descent = it->max_descent;
20187 phys_ascent = it->max_phys_ascent;
20188 phys_descent = it->max_phys_descent;
20189
20190 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20191 {
20192 if (IT_DISPLAYING_WHITESPACE (it))
20193 may_wrap = 1;
20194 else if (may_wrap)
20195 {
20196 SAVE_IT (wrap_it, *it, wrap_data);
20197 wrap_x = x;
20198 wrap_row_used = row->used[TEXT_AREA];
20199 wrap_row_ascent = row->ascent;
20200 wrap_row_height = row->height;
20201 wrap_row_phys_ascent = row->phys_ascent;
20202 wrap_row_phys_height = row->phys_height;
20203 wrap_row_extra_line_spacing = row->extra_line_spacing;
20204 wrap_row_min_pos = min_pos;
20205 wrap_row_min_bpos = min_bpos;
20206 wrap_row_max_pos = max_pos;
20207 wrap_row_max_bpos = max_bpos;
20208 may_wrap = 0;
20209 }
20210 }
20211 }
20212
20213 PRODUCE_GLYPHS (it);
20214
20215 /* If this display element was in marginal areas, continue with
20216 the next one. */
20217 if (it->area != TEXT_AREA)
20218 {
20219 row->ascent = max (row->ascent, it->max_ascent);
20220 row->height = max (row->height, it->max_ascent + it->max_descent);
20221 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20222 row->phys_height = max (row->phys_height,
20223 it->max_phys_ascent + it->max_phys_descent);
20224 row->extra_line_spacing = max (row->extra_line_spacing,
20225 it->max_extra_line_spacing);
20226 set_iterator_to_next (it, 1);
20227 /* If we didn't handle the line/wrap prefix above, and the
20228 call to set_iterator_to_next just switched to TEXT_AREA,
20229 process the prefix now. */
20230 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20231 {
20232 pending_handle_line_prefix = false;
20233 handle_line_prefix (it);
20234 }
20235 continue;
20236 }
20237
20238 /* Does the display element fit on the line? If we truncate
20239 lines, we should draw past the right edge of the window. If
20240 we don't truncate, we want to stop so that we can display the
20241 continuation glyph before the right margin. If lines are
20242 continued, there are two possible strategies for characters
20243 resulting in more than 1 glyph (e.g. tabs): Display as many
20244 glyphs as possible in this line and leave the rest for the
20245 continuation line, or display the whole element in the next
20246 line. Original redisplay did the former, so we do it also. */
20247 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20248 hpos_before = it->hpos;
20249 x_before = x;
20250
20251 if (/* Not a newline. */
20252 nglyphs > 0
20253 /* Glyphs produced fit entirely in the line. */
20254 && it->current_x < it->last_visible_x)
20255 {
20256 it->hpos += nglyphs;
20257 row->ascent = max (row->ascent, it->max_ascent);
20258 row->height = max (row->height, it->max_ascent + it->max_descent);
20259 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20260 row->phys_height = max (row->phys_height,
20261 it->max_phys_ascent + it->max_phys_descent);
20262 row->extra_line_spacing = max (row->extra_line_spacing,
20263 it->max_extra_line_spacing);
20264 if (it->current_x - it->pixel_width < it->first_visible_x
20265 /* In R2L rows, we arrange in extend_face_to_end_of_line
20266 to add a right offset to the line, by a suitable
20267 change to the stretch glyph that is the leftmost
20268 glyph of the line. */
20269 && !row->reversed_p)
20270 row->x = x - it->first_visible_x;
20271 /* Record the maximum and minimum buffer positions seen so
20272 far in glyphs that will be displayed by this row. */
20273 if (it->bidi_p)
20274 RECORD_MAX_MIN_POS (it);
20275 }
20276 else
20277 {
20278 int i, new_x;
20279 struct glyph *glyph;
20280
20281 for (i = 0; i < nglyphs; ++i, x = new_x)
20282 {
20283 /* Identify the glyphs added by the last call to
20284 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20285 the previous glyphs. */
20286 if (!row->reversed_p)
20287 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20288 else
20289 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20290 new_x = x + glyph->pixel_width;
20291
20292 if (/* Lines are continued. */
20293 it->line_wrap != TRUNCATE
20294 && (/* Glyph doesn't fit on the line. */
20295 new_x > it->last_visible_x
20296 /* Or it fits exactly on a window system frame. */
20297 || (new_x == it->last_visible_x
20298 && FRAME_WINDOW_P (it->f)
20299 && (row->reversed_p
20300 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20301 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20302 {
20303 /* End of a continued line. */
20304
20305 if (it->hpos == 0
20306 || (new_x == it->last_visible_x
20307 && FRAME_WINDOW_P (it->f)
20308 && (row->reversed_p
20309 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20310 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20311 {
20312 /* Current glyph is the only one on the line or
20313 fits exactly on the line. We must continue
20314 the line because we can't draw the cursor
20315 after the glyph. */
20316 row->continued_p = 1;
20317 it->current_x = new_x;
20318 it->continuation_lines_width += new_x;
20319 ++it->hpos;
20320 if (i == nglyphs - 1)
20321 {
20322 /* If line-wrap is on, check if a previous
20323 wrap point was found. */
20324 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20325 && wrap_row_used > 0
20326 /* Even if there is a previous wrap
20327 point, continue the line here as
20328 usual, if (i) the previous character
20329 was a space or tab AND (ii) the
20330 current character is not. */
20331 && (!may_wrap
20332 || IT_DISPLAYING_WHITESPACE (it)))
20333 goto back_to_wrap;
20334
20335 /* Record the maximum and minimum buffer
20336 positions seen so far in glyphs that will be
20337 displayed by this row. */
20338 if (it->bidi_p)
20339 RECORD_MAX_MIN_POS (it);
20340 set_iterator_to_next (it, 1);
20341 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20342 {
20343 if (!get_next_display_element (it))
20344 {
20345 row->exact_window_width_line_p = 1;
20346 it->continuation_lines_width = 0;
20347 row->continued_p = 0;
20348 row->ends_at_zv_p = 1;
20349 }
20350 else if (ITERATOR_AT_END_OF_LINE_P (it))
20351 {
20352 row->continued_p = 0;
20353 row->exact_window_width_line_p = 1;
20354 }
20355 /* If line-wrap is on, check if a
20356 previous wrap point was found. */
20357 else if (wrap_row_used > 0
20358 /* Even if there is a previous wrap
20359 point, continue the line here as
20360 usual, if (i) the previous character
20361 was a space or tab AND (ii) the
20362 current character is not. */
20363 && (!may_wrap
20364 || IT_DISPLAYING_WHITESPACE (it)))
20365 goto back_to_wrap;
20366
20367 }
20368 }
20369 else if (it->bidi_p)
20370 RECORD_MAX_MIN_POS (it);
20371 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20372 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20373 extend_face_to_end_of_line (it);
20374 }
20375 else if (CHAR_GLYPH_PADDING_P (*glyph)
20376 && !FRAME_WINDOW_P (it->f))
20377 {
20378 /* A padding glyph that doesn't fit on this line.
20379 This means the whole character doesn't fit
20380 on the line. */
20381 if (row->reversed_p)
20382 unproduce_glyphs (it, row->used[TEXT_AREA]
20383 - n_glyphs_before);
20384 row->used[TEXT_AREA] = n_glyphs_before;
20385
20386 /* Fill the rest of the row with continuation
20387 glyphs like in 20.x. */
20388 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20389 < row->glyphs[1 + TEXT_AREA])
20390 produce_special_glyphs (it, IT_CONTINUATION);
20391
20392 row->continued_p = 1;
20393 it->current_x = x_before;
20394 it->continuation_lines_width += x_before;
20395
20396 /* Restore the height to what it was before the
20397 element not fitting on the line. */
20398 it->max_ascent = ascent;
20399 it->max_descent = descent;
20400 it->max_phys_ascent = phys_ascent;
20401 it->max_phys_descent = phys_descent;
20402 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20403 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20404 extend_face_to_end_of_line (it);
20405 }
20406 else if (wrap_row_used > 0)
20407 {
20408 back_to_wrap:
20409 if (row->reversed_p)
20410 unproduce_glyphs (it,
20411 row->used[TEXT_AREA] - wrap_row_used);
20412 RESTORE_IT (it, &wrap_it, wrap_data);
20413 it->continuation_lines_width += wrap_x;
20414 row->used[TEXT_AREA] = wrap_row_used;
20415 row->ascent = wrap_row_ascent;
20416 row->height = wrap_row_height;
20417 row->phys_ascent = wrap_row_phys_ascent;
20418 row->phys_height = wrap_row_phys_height;
20419 row->extra_line_spacing = wrap_row_extra_line_spacing;
20420 min_pos = wrap_row_min_pos;
20421 min_bpos = wrap_row_min_bpos;
20422 max_pos = wrap_row_max_pos;
20423 max_bpos = wrap_row_max_bpos;
20424 row->continued_p = 1;
20425 row->ends_at_zv_p = 0;
20426 row->exact_window_width_line_p = 0;
20427 it->continuation_lines_width += x;
20428
20429 /* Make sure that a non-default face is extended
20430 up to the right margin of the window. */
20431 extend_face_to_end_of_line (it);
20432 }
20433 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20434 {
20435 /* A TAB that extends past the right edge of the
20436 window. This produces a single glyph on
20437 window system frames. We leave the glyph in
20438 this row and let it fill the row, but don't
20439 consume the TAB. */
20440 if ((row->reversed_p
20441 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20442 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20443 produce_special_glyphs (it, IT_CONTINUATION);
20444 it->continuation_lines_width += it->last_visible_x;
20445 row->ends_in_middle_of_char_p = 1;
20446 row->continued_p = 1;
20447 glyph->pixel_width = it->last_visible_x - x;
20448 it->starts_in_middle_of_char_p = 1;
20449 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20450 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20451 extend_face_to_end_of_line (it);
20452 }
20453 else
20454 {
20455 /* Something other than a TAB that draws past
20456 the right edge of the window. Restore
20457 positions to values before the element. */
20458 if (row->reversed_p)
20459 unproduce_glyphs (it, row->used[TEXT_AREA]
20460 - (n_glyphs_before + i));
20461 row->used[TEXT_AREA] = n_glyphs_before + i;
20462
20463 /* Display continuation glyphs. */
20464 it->current_x = x_before;
20465 it->continuation_lines_width += x;
20466 if (!FRAME_WINDOW_P (it->f)
20467 || (row->reversed_p
20468 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20469 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20470 produce_special_glyphs (it, IT_CONTINUATION);
20471 row->continued_p = 1;
20472
20473 extend_face_to_end_of_line (it);
20474
20475 if (nglyphs > 1 && i > 0)
20476 {
20477 row->ends_in_middle_of_char_p = 1;
20478 it->starts_in_middle_of_char_p = 1;
20479 }
20480
20481 /* Restore the height to what it was before the
20482 element not fitting on the line. */
20483 it->max_ascent = ascent;
20484 it->max_descent = descent;
20485 it->max_phys_ascent = phys_ascent;
20486 it->max_phys_descent = phys_descent;
20487 }
20488
20489 break;
20490 }
20491 else if (new_x > it->first_visible_x)
20492 {
20493 /* Increment number of glyphs actually displayed. */
20494 ++it->hpos;
20495
20496 /* Record the maximum and minimum buffer positions
20497 seen so far in glyphs that will be displayed by
20498 this row. */
20499 if (it->bidi_p)
20500 RECORD_MAX_MIN_POS (it);
20501
20502 if (x < it->first_visible_x && !row->reversed_p)
20503 /* Glyph is partially visible, i.e. row starts at
20504 negative X position. Don't do that in R2L
20505 rows, where we arrange to add a right offset to
20506 the line in extend_face_to_end_of_line, by a
20507 suitable change to the stretch glyph that is
20508 the leftmost glyph of the line. */
20509 row->x = x - it->first_visible_x;
20510 /* When the last glyph of an R2L row only fits
20511 partially on the line, we need to set row->x to a
20512 negative offset, so that the leftmost glyph is
20513 the one that is partially visible. But if we are
20514 going to produce the truncation glyph, this will
20515 be taken care of in produce_special_glyphs. */
20516 if (row->reversed_p
20517 && new_x > it->last_visible_x
20518 && !(it->line_wrap == TRUNCATE
20519 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20520 {
20521 eassert (FRAME_WINDOW_P (it->f));
20522 row->x = it->last_visible_x - new_x;
20523 }
20524 }
20525 else
20526 {
20527 /* Glyph is completely off the left margin of the
20528 window. This should not happen because of the
20529 move_it_in_display_line at the start of this
20530 function, unless the text display area of the
20531 window is empty. */
20532 eassert (it->first_visible_x <= it->last_visible_x);
20533 }
20534 }
20535 /* Even if this display element produced no glyphs at all,
20536 we want to record its position. */
20537 if (it->bidi_p && nglyphs == 0)
20538 RECORD_MAX_MIN_POS (it);
20539
20540 row->ascent = max (row->ascent, it->max_ascent);
20541 row->height = max (row->height, it->max_ascent + it->max_descent);
20542 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20543 row->phys_height = max (row->phys_height,
20544 it->max_phys_ascent + it->max_phys_descent);
20545 row->extra_line_spacing = max (row->extra_line_spacing,
20546 it->max_extra_line_spacing);
20547
20548 /* End of this display line if row is continued. */
20549 if (row->continued_p || row->ends_at_zv_p)
20550 break;
20551 }
20552
20553 at_end_of_line:
20554 /* Is this a line end? If yes, we're also done, after making
20555 sure that a non-default face is extended up to the right
20556 margin of the window. */
20557 if (ITERATOR_AT_END_OF_LINE_P (it))
20558 {
20559 int used_before = row->used[TEXT_AREA];
20560
20561 row->ends_in_newline_from_string_p = STRINGP (it->object);
20562
20563 /* Add a space at the end of the line that is used to
20564 display the cursor there. */
20565 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20566 append_space_for_newline (it, 0);
20567
20568 /* Extend the face to the end of the line. */
20569 extend_face_to_end_of_line (it);
20570
20571 /* Make sure we have the position. */
20572 if (used_before == 0)
20573 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20574
20575 /* Record the position of the newline, for use in
20576 find_row_edges. */
20577 it->eol_pos = it->current.pos;
20578
20579 /* Consume the line end. This skips over invisible lines. */
20580 set_iterator_to_next (it, 1);
20581 it->continuation_lines_width = 0;
20582 break;
20583 }
20584
20585 /* Proceed with next display element. Note that this skips
20586 over lines invisible because of selective display. */
20587 set_iterator_to_next (it, 1);
20588
20589 /* If we truncate lines, we are done when the last displayed
20590 glyphs reach past the right margin of the window. */
20591 if (it->line_wrap == TRUNCATE
20592 && ((FRAME_WINDOW_P (it->f)
20593 /* Images are preprocessed in produce_image_glyph such
20594 that they are cropped at the right edge of the
20595 window, so an image glyph will always end exactly at
20596 last_visible_x, even if there's no right fringe. */
20597 && ((row->reversed_p
20598 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20599 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20600 || it->what == IT_IMAGE))
20601 ? (it->current_x >= it->last_visible_x)
20602 : (it->current_x > it->last_visible_x)))
20603 {
20604 /* Maybe add truncation glyphs. */
20605 if (!FRAME_WINDOW_P (it->f)
20606 || (row->reversed_p
20607 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20608 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20609 {
20610 int i, n;
20611
20612 if (!row->reversed_p)
20613 {
20614 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20615 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20616 break;
20617 }
20618 else
20619 {
20620 for (i = 0; i < row->used[TEXT_AREA]; i++)
20621 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20622 break;
20623 /* Remove any padding glyphs at the front of ROW, to
20624 make room for the truncation glyphs we will be
20625 adding below. The loop below always inserts at
20626 least one truncation glyph, so also remove the
20627 last glyph added to ROW. */
20628 unproduce_glyphs (it, i + 1);
20629 /* Adjust i for the loop below. */
20630 i = row->used[TEXT_AREA] - (i + 1);
20631 }
20632
20633 /* produce_special_glyphs overwrites the last glyph, so
20634 we don't want that if we want to keep that last
20635 glyph, which means it's an image. */
20636 if (it->current_x > it->last_visible_x)
20637 {
20638 it->current_x = x_before;
20639 if (!FRAME_WINDOW_P (it->f))
20640 {
20641 for (n = row->used[TEXT_AREA]; i < n; ++i)
20642 {
20643 row->used[TEXT_AREA] = i;
20644 produce_special_glyphs (it, IT_TRUNCATION);
20645 }
20646 }
20647 else
20648 {
20649 row->used[TEXT_AREA] = i;
20650 produce_special_glyphs (it, IT_TRUNCATION);
20651 }
20652 it->hpos = hpos_before;
20653 }
20654 }
20655 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20656 {
20657 /* Don't truncate if we can overflow newline into fringe. */
20658 if (!get_next_display_element (it))
20659 {
20660 it->continuation_lines_width = 0;
20661 row->ends_at_zv_p = 1;
20662 row->exact_window_width_line_p = 1;
20663 break;
20664 }
20665 if (ITERATOR_AT_END_OF_LINE_P (it))
20666 {
20667 row->exact_window_width_line_p = 1;
20668 goto at_end_of_line;
20669 }
20670 it->current_x = x_before;
20671 it->hpos = hpos_before;
20672 }
20673
20674 row->truncated_on_right_p = 1;
20675 it->continuation_lines_width = 0;
20676 reseat_at_next_visible_line_start (it, 0);
20677 /* We insist below that IT's position be at ZV because in
20678 bidi-reordered lines the character at visible line start
20679 might not be the character that follows the newline in
20680 the logical order. */
20681 if (IT_BYTEPOS (*it) > BEG_BYTE)
20682 row->ends_at_zv_p =
20683 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20684 else
20685 row->ends_at_zv_p = false;
20686 break;
20687 }
20688 }
20689
20690 if (wrap_data)
20691 bidi_unshelve_cache (wrap_data, 1);
20692
20693 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20694 at the left window margin. */
20695 if (it->first_visible_x
20696 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20697 {
20698 if (!FRAME_WINDOW_P (it->f)
20699 || (((row->reversed_p
20700 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20701 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20702 /* Don't let insert_left_trunc_glyphs overwrite the
20703 first glyph of the row if it is an image. */
20704 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20705 insert_left_trunc_glyphs (it);
20706 row->truncated_on_left_p = 1;
20707 }
20708
20709 /* Remember the position at which this line ends.
20710
20711 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20712 cannot be before the call to find_row_edges below, since that is
20713 where these positions are determined. */
20714 row->end = it->current;
20715 if (!it->bidi_p)
20716 {
20717 row->minpos = row->start.pos;
20718 row->maxpos = row->end.pos;
20719 }
20720 else
20721 {
20722 /* ROW->minpos and ROW->maxpos must be the smallest and
20723 `1 + the largest' buffer positions in ROW. But if ROW was
20724 bidi-reordered, these two positions can be anywhere in the
20725 row, so we must determine them now. */
20726 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20727 }
20728
20729 /* If the start of this line is the overlay arrow-position, then
20730 mark this glyph row as the one containing the overlay arrow.
20731 This is clearly a mess with variable size fonts. It would be
20732 better to let it be displayed like cursors under X. */
20733 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20734 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20735 !NILP (overlay_arrow_string)))
20736 {
20737 /* Overlay arrow in window redisplay is a fringe bitmap. */
20738 if (STRINGP (overlay_arrow_string))
20739 {
20740 struct glyph_row *arrow_row
20741 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20742 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20743 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20744 struct glyph *p = row->glyphs[TEXT_AREA];
20745 struct glyph *p2, *end;
20746
20747 /* Copy the arrow glyphs. */
20748 while (glyph < arrow_end)
20749 *p++ = *glyph++;
20750
20751 /* Throw away padding glyphs. */
20752 p2 = p;
20753 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20754 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20755 ++p2;
20756 if (p2 > p)
20757 {
20758 while (p2 < end)
20759 *p++ = *p2++;
20760 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20761 }
20762 }
20763 else
20764 {
20765 eassert (INTEGERP (overlay_arrow_string));
20766 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20767 }
20768 overlay_arrow_seen = 1;
20769 }
20770
20771 /* Highlight trailing whitespace. */
20772 if (!NILP (Vshow_trailing_whitespace))
20773 highlight_trailing_whitespace (it->f, it->glyph_row);
20774
20775 /* Compute pixel dimensions of this line. */
20776 compute_line_metrics (it);
20777
20778 /* Implementation note: No changes in the glyphs of ROW or in their
20779 faces can be done past this point, because compute_line_metrics
20780 computes ROW's hash value and stores it within the glyph_row
20781 structure. */
20782
20783 /* Record whether this row ends inside an ellipsis. */
20784 row->ends_in_ellipsis_p
20785 = (it->method == GET_FROM_DISPLAY_VECTOR
20786 && it->ellipsis_p);
20787
20788 /* Save fringe bitmaps in this row. */
20789 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20790 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20791 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20792 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20793
20794 it->left_user_fringe_bitmap = 0;
20795 it->left_user_fringe_face_id = 0;
20796 it->right_user_fringe_bitmap = 0;
20797 it->right_user_fringe_face_id = 0;
20798
20799 /* Maybe set the cursor. */
20800 cvpos = it->w->cursor.vpos;
20801 if ((cvpos < 0
20802 /* In bidi-reordered rows, keep checking for proper cursor
20803 position even if one has been found already, because buffer
20804 positions in such rows change non-linearly with ROW->VPOS,
20805 when a line is continued. One exception: when we are at ZV,
20806 display cursor on the first suitable glyph row, since all
20807 the empty rows after that also have their position set to ZV. */
20808 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20809 lines' rows is implemented for bidi-reordered rows. */
20810 || (it->bidi_p
20811 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20812 && PT >= MATRIX_ROW_START_CHARPOS (row)
20813 && PT <= MATRIX_ROW_END_CHARPOS (row)
20814 && cursor_row_p (row))
20815 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20816
20817 /* Prepare for the next line. This line starts horizontally at (X
20818 HPOS) = (0 0). Vertical positions are incremented. As a
20819 convenience for the caller, IT->glyph_row is set to the next
20820 row to be used. */
20821 it->current_x = it->hpos = 0;
20822 it->current_y += row->height;
20823 SET_TEXT_POS (it->eol_pos, 0, 0);
20824 ++it->vpos;
20825 ++it->glyph_row;
20826 /* The next row should by default use the same value of the
20827 reversed_p flag as this one. set_iterator_to_next decides when
20828 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20829 the flag accordingly. */
20830 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20831 it->glyph_row->reversed_p = row->reversed_p;
20832 it->start = row->end;
20833 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20834
20835 #undef RECORD_MAX_MIN_POS
20836 }
20837
20838 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20839 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20840 doc: /* Return paragraph direction at point in BUFFER.
20841 Value is either `left-to-right' or `right-to-left'.
20842 If BUFFER is omitted or nil, it defaults to the current buffer.
20843
20844 Paragraph direction determines how the text in the paragraph is displayed.
20845 In left-to-right paragraphs, text begins at the left margin of the window
20846 and the reading direction is generally left to right. In right-to-left
20847 paragraphs, text begins at the right margin and is read from right to left.
20848
20849 See also `bidi-paragraph-direction'. */)
20850 (Lisp_Object buffer)
20851 {
20852 struct buffer *buf = current_buffer;
20853 struct buffer *old = buf;
20854
20855 if (! NILP (buffer))
20856 {
20857 CHECK_BUFFER (buffer);
20858 buf = XBUFFER (buffer);
20859 }
20860
20861 if (NILP (BVAR (buf, bidi_display_reordering))
20862 || NILP (BVAR (buf, enable_multibyte_characters))
20863 /* When we are loading loadup.el, the character property tables
20864 needed for bidi iteration are not yet available. */
20865 || !NILP (Vpurify_flag))
20866 return Qleft_to_right;
20867 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20868 return BVAR (buf, bidi_paragraph_direction);
20869 else
20870 {
20871 /* Determine the direction from buffer text. We could try to
20872 use current_matrix if it is up to date, but this seems fast
20873 enough as it is. */
20874 struct bidi_it itb;
20875 ptrdiff_t pos = BUF_PT (buf);
20876 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20877 int c;
20878 void *itb_data = bidi_shelve_cache ();
20879
20880 set_buffer_temp (buf);
20881 /* bidi_paragraph_init finds the base direction of the paragraph
20882 by searching forward from paragraph start. We need the base
20883 direction of the current or _previous_ paragraph, so we need
20884 to make sure we are within that paragraph. To that end, find
20885 the previous non-empty line. */
20886 if (pos >= ZV && pos > BEGV)
20887 DEC_BOTH (pos, bytepos);
20888 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20889 if (fast_looking_at (trailing_white_space,
20890 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20891 {
20892 while ((c = FETCH_BYTE (bytepos)) == '\n'
20893 || c == ' ' || c == '\t' || c == '\f')
20894 {
20895 if (bytepos <= BEGV_BYTE)
20896 break;
20897 bytepos--;
20898 pos--;
20899 }
20900 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20901 bytepos--;
20902 }
20903 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20904 itb.paragraph_dir = NEUTRAL_DIR;
20905 itb.string.s = NULL;
20906 itb.string.lstring = Qnil;
20907 itb.string.bufpos = 0;
20908 itb.string.from_disp_str = 0;
20909 itb.string.unibyte = 0;
20910 /* We have no window to use here for ignoring window-specific
20911 overlays. Using NULL for window pointer will cause
20912 compute_display_string_pos to use the current buffer. */
20913 itb.w = NULL;
20914 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20915 bidi_unshelve_cache (itb_data, 0);
20916 set_buffer_temp (old);
20917 switch (itb.paragraph_dir)
20918 {
20919 case L2R:
20920 return Qleft_to_right;
20921 break;
20922 case R2L:
20923 return Qright_to_left;
20924 break;
20925 default:
20926 emacs_abort ();
20927 }
20928 }
20929 }
20930
20931 DEFUN ("bidi-find-overridden-directionality",
20932 Fbidi_find_overridden_directionality,
20933 Sbidi_find_overridden_directionality, 2, 3, 0,
20934 doc: /* Return position between FROM and TO where directionality was overridden.
20935
20936 This function returns the first character position in the specified
20937 region of OBJECT where there is a character whose `bidi-class' property
20938 is `L', but which was forced to display as `R' by a directional
20939 override, and likewise with characters whose `bidi-class' is `R'
20940 or `AL' that were forced to display as `L'.
20941
20942 If no such character is found, the function returns nil.
20943
20944 OBJECT is a Lisp string or buffer to search for overridden
20945 directionality, and defaults to the current buffer if nil or omitted.
20946 OBJECT can also be a window, in which case the function will search
20947 the buffer displayed in that window. Passing the window instead of
20948 a buffer is preferable when the buffer is displayed in some window,
20949 because this function will then be able to correctly account for
20950 window-specific overlays, which can affect the results.
20951
20952 Strong directional characters `L', `R', and `AL' can have their
20953 intrinsic directionality overridden by directional override
20954 control characters RLO \(u+202e) and LRO \(u+202d). See the
20955 function `get-char-code-property' for a way to inquire about
20956 the `bidi-class' property of a character. */)
20957 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20958 {
20959 struct buffer *buf = current_buffer;
20960 struct buffer *old = buf;
20961 struct window *w = NULL;
20962 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20963 struct bidi_it itb;
20964 ptrdiff_t from_pos, to_pos, from_bpos;
20965 void *itb_data;
20966
20967 if (!NILP (object))
20968 {
20969 if (BUFFERP (object))
20970 buf = XBUFFER (object);
20971 else if (WINDOWP (object))
20972 {
20973 w = decode_live_window (object);
20974 buf = XBUFFER (w->contents);
20975 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20976 }
20977 else
20978 CHECK_STRING (object);
20979 }
20980
20981 if (STRINGP (object))
20982 {
20983 /* Characters in unibyte strings are always treated by bidi.c as
20984 strong LTR. */
20985 if (!STRING_MULTIBYTE (object)
20986 /* When we are loading loadup.el, the character property
20987 tables needed for bidi iteration are not yet
20988 available. */
20989 || !NILP (Vpurify_flag))
20990 return Qnil;
20991
20992 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
20993 if (from_pos >= SCHARS (object))
20994 return Qnil;
20995
20996 /* Set up the bidi iterator. */
20997 itb_data = bidi_shelve_cache ();
20998 itb.paragraph_dir = NEUTRAL_DIR;
20999 itb.string.lstring = object;
21000 itb.string.s = NULL;
21001 itb.string.schars = SCHARS (object);
21002 itb.string.bufpos = 0;
21003 itb.string.from_disp_str = 0;
21004 itb.string.unibyte = 0;
21005 itb.w = w;
21006 bidi_init_it (0, 0, frame_window_p, &itb);
21007 }
21008 else
21009 {
21010 /* Nothing this fancy can happen in unibyte buffers, or in a
21011 buffer that disabled reordering, or if FROM is at EOB. */
21012 if (NILP (BVAR (buf, bidi_display_reordering))
21013 || NILP (BVAR (buf, enable_multibyte_characters))
21014 /* When we are loading loadup.el, the character property
21015 tables needed for bidi iteration are not yet
21016 available. */
21017 || !NILP (Vpurify_flag))
21018 return Qnil;
21019
21020 set_buffer_temp (buf);
21021 validate_region (&from, &to);
21022 from_pos = XINT (from);
21023 to_pos = XINT (to);
21024 if (from_pos >= ZV)
21025 return Qnil;
21026
21027 /* Set up the bidi iterator. */
21028 itb_data = bidi_shelve_cache ();
21029 from_bpos = CHAR_TO_BYTE (from_pos);
21030 if (from_pos == BEGV)
21031 {
21032 itb.charpos = BEGV;
21033 itb.bytepos = BEGV_BYTE;
21034 }
21035 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21036 {
21037 itb.charpos = from_pos;
21038 itb.bytepos = from_bpos;
21039 }
21040 else
21041 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21042 -1, &itb.bytepos);
21043 itb.paragraph_dir = NEUTRAL_DIR;
21044 itb.string.s = NULL;
21045 itb.string.lstring = Qnil;
21046 itb.string.bufpos = 0;
21047 itb.string.from_disp_str = 0;
21048 itb.string.unibyte = 0;
21049 itb.w = w;
21050 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21051 }
21052
21053 ptrdiff_t found;
21054 do {
21055 /* For the purposes of this function, the actual base direction of
21056 the paragraph doesn't matter, so just set it to L2R. */
21057 bidi_paragraph_init (L2R, &itb, 0);
21058 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21059 ;
21060 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21061
21062 bidi_unshelve_cache (itb_data, 0);
21063 set_buffer_temp (old);
21064
21065 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21066 }
21067
21068 DEFUN ("move-point-visually", Fmove_point_visually,
21069 Smove_point_visually, 1, 1, 0,
21070 doc: /* Move point in the visual order in the specified DIRECTION.
21071 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21072 left.
21073
21074 Value is the new character position of point. */)
21075 (Lisp_Object direction)
21076 {
21077 struct window *w = XWINDOW (selected_window);
21078 struct buffer *b = XBUFFER (w->contents);
21079 struct glyph_row *row;
21080 int dir;
21081 Lisp_Object paragraph_dir;
21082
21083 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21084 (!(ROW)->continued_p \
21085 && INTEGERP ((GLYPH)->object) \
21086 && (GLYPH)->type == CHAR_GLYPH \
21087 && (GLYPH)->u.ch == ' ' \
21088 && (GLYPH)->charpos >= 0 \
21089 && !(GLYPH)->avoid_cursor_p)
21090
21091 CHECK_NUMBER (direction);
21092 dir = XINT (direction);
21093 if (dir > 0)
21094 dir = 1;
21095 else
21096 dir = -1;
21097
21098 /* If current matrix is up-to-date, we can use the information
21099 recorded in the glyphs, at least as long as the goal is on the
21100 screen. */
21101 if (w->window_end_valid
21102 && !windows_or_buffers_changed
21103 && b
21104 && !b->clip_changed
21105 && !b->prevent_redisplay_optimizations_p
21106 && !window_outdated (w)
21107 /* We rely below on the cursor coordinates to be up to date, but
21108 we cannot trust them if some command moved point since the
21109 last complete redisplay. */
21110 && w->last_point == BUF_PT (b)
21111 && w->cursor.vpos >= 0
21112 && w->cursor.vpos < w->current_matrix->nrows
21113 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21114 {
21115 struct glyph *g = row->glyphs[TEXT_AREA];
21116 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21117 struct glyph *gpt = g + w->cursor.hpos;
21118
21119 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21120 {
21121 if (BUFFERP (g->object) && g->charpos != PT)
21122 {
21123 SET_PT (g->charpos);
21124 w->cursor.vpos = -1;
21125 return make_number (PT);
21126 }
21127 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
21128 {
21129 ptrdiff_t new_pos;
21130
21131 if (BUFFERP (gpt->object))
21132 {
21133 new_pos = PT;
21134 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21135 new_pos += (row->reversed_p ? -dir : dir);
21136 else
21137 new_pos -= (row->reversed_p ? -dir : dir);
21138 }
21139 else if (BUFFERP (g->object))
21140 new_pos = g->charpos;
21141 else
21142 break;
21143 SET_PT (new_pos);
21144 w->cursor.vpos = -1;
21145 return make_number (PT);
21146 }
21147 else if (ROW_GLYPH_NEWLINE_P (row, g))
21148 {
21149 /* Glyphs inserted at the end of a non-empty line for
21150 positioning the cursor have zero charpos, so we must
21151 deduce the value of point by other means. */
21152 if (g->charpos > 0)
21153 SET_PT (g->charpos);
21154 else if (row->ends_at_zv_p && PT != ZV)
21155 SET_PT (ZV);
21156 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21157 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21158 else
21159 break;
21160 w->cursor.vpos = -1;
21161 return make_number (PT);
21162 }
21163 }
21164 if (g == e || INTEGERP (g->object))
21165 {
21166 if (row->truncated_on_left_p || row->truncated_on_right_p)
21167 goto simulate_display;
21168 if (!row->reversed_p)
21169 row += dir;
21170 else
21171 row -= dir;
21172 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21173 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21174 goto simulate_display;
21175
21176 if (dir > 0)
21177 {
21178 if (row->reversed_p && !row->continued_p)
21179 {
21180 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21181 w->cursor.vpos = -1;
21182 return make_number (PT);
21183 }
21184 g = row->glyphs[TEXT_AREA];
21185 e = g + row->used[TEXT_AREA];
21186 for ( ; g < e; g++)
21187 {
21188 if (BUFFERP (g->object)
21189 /* Empty lines have only one glyph, which stands
21190 for the newline, and whose charpos is the
21191 buffer position of the newline. */
21192 || ROW_GLYPH_NEWLINE_P (row, g)
21193 /* When the buffer ends in a newline, the line at
21194 EOB also has one glyph, but its charpos is -1. */
21195 || (row->ends_at_zv_p
21196 && !row->reversed_p
21197 && INTEGERP (g->object)
21198 && g->type == CHAR_GLYPH
21199 && g->u.ch == ' '))
21200 {
21201 if (g->charpos > 0)
21202 SET_PT (g->charpos);
21203 else if (!row->reversed_p
21204 && row->ends_at_zv_p
21205 && PT != ZV)
21206 SET_PT (ZV);
21207 else
21208 continue;
21209 w->cursor.vpos = -1;
21210 return make_number (PT);
21211 }
21212 }
21213 }
21214 else
21215 {
21216 if (!row->reversed_p && !row->continued_p)
21217 {
21218 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21219 w->cursor.vpos = -1;
21220 return make_number (PT);
21221 }
21222 e = row->glyphs[TEXT_AREA];
21223 g = e + row->used[TEXT_AREA] - 1;
21224 for ( ; g >= e; g--)
21225 {
21226 if (BUFFERP (g->object)
21227 || (ROW_GLYPH_NEWLINE_P (row, g)
21228 && g->charpos > 0)
21229 /* Empty R2L lines on GUI frames have the buffer
21230 position of the newline stored in the stretch
21231 glyph. */
21232 || g->type == STRETCH_GLYPH
21233 || (row->ends_at_zv_p
21234 && row->reversed_p
21235 && INTEGERP (g->object)
21236 && g->type == CHAR_GLYPH
21237 && g->u.ch == ' '))
21238 {
21239 if (g->charpos > 0)
21240 SET_PT (g->charpos);
21241 else if (row->reversed_p
21242 && row->ends_at_zv_p
21243 && PT != ZV)
21244 SET_PT (ZV);
21245 else
21246 continue;
21247 w->cursor.vpos = -1;
21248 return make_number (PT);
21249 }
21250 }
21251 }
21252 }
21253 }
21254
21255 simulate_display:
21256
21257 /* If we wind up here, we failed to move by using the glyphs, so we
21258 need to simulate display instead. */
21259
21260 if (b)
21261 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21262 else
21263 paragraph_dir = Qleft_to_right;
21264 if (EQ (paragraph_dir, Qright_to_left))
21265 dir = -dir;
21266 if (PT <= BEGV && dir < 0)
21267 xsignal0 (Qbeginning_of_buffer);
21268 else if (PT >= ZV && dir > 0)
21269 xsignal0 (Qend_of_buffer);
21270 else
21271 {
21272 struct text_pos pt;
21273 struct it it;
21274 int pt_x, target_x, pixel_width, pt_vpos;
21275 bool at_eol_p;
21276 bool overshoot_expected = false;
21277 bool target_is_eol_p = false;
21278
21279 /* Setup the arena. */
21280 SET_TEXT_POS (pt, PT, PT_BYTE);
21281 start_display (&it, w, pt);
21282
21283 if (it.cmp_it.id < 0
21284 && it.method == GET_FROM_STRING
21285 && it.area == TEXT_AREA
21286 && it.string_from_display_prop_p
21287 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21288 overshoot_expected = true;
21289
21290 /* Find the X coordinate of point. We start from the beginning
21291 of this or previous line to make sure we are before point in
21292 the logical order (since the move_it_* functions can only
21293 move forward). */
21294 reseat:
21295 reseat_at_previous_visible_line_start (&it);
21296 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21297 if (IT_CHARPOS (it) != PT)
21298 {
21299 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21300 -1, -1, -1, MOVE_TO_POS);
21301 /* If we missed point because the character there is
21302 displayed out of a display vector that has more than one
21303 glyph, retry expecting overshoot. */
21304 if (it.method == GET_FROM_DISPLAY_VECTOR
21305 && it.current.dpvec_index > 0
21306 && !overshoot_expected)
21307 {
21308 overshoot_expected = true;
21309 goto reseat;
21310 }
21311 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21312 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21313 }
21314 pt_x = it.current_x;
21315 pt_vpos = it.vpos;
21316 if (dir > 0 || overshoot_expected)
21317 {
21318 struct glyph_row *row = it.glyph_row;
21319
21320 /* When point is at beginning of line, we don't have
21321 information about the glyph there loaded into struct
21322 it. Calling get_next_display_element fixes that. */
21323 if (pt_x == 0)
21324 get_next_display_element (&it);
21325 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21326 it.glyph_row = NULL;
21327 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21328 it.glyph_row = row;
21329 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21330 it, lest it will become out of sync with it's buffer
21331 position. */
21332 it.current_x = pt_x;
21333 }
21334 else
21335 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21336 pixel_width = it.pixel_width;
21337 if (overshoot_expected && at_eol_p)
21338 pixel_width = 0;
21339 else if (pixel_width <= 0)
21340 pixel_width = 1;
21341
21342 /* If there's a display string (or something similar) at point,
21343 we are actually at the glyph to the left of point, so we need
21344 to correct the X coordinate. */
21345 if (overshoot_expected)
21346 {
21347 if (it.bidi_p)
21348 pt_x += pixel_width * it.bidi_it.scan_dir;
21349 else
21350 pt_x += pixel_width;
21351 }
21352
21353 /* Compute target X coordinate, either to the left or to the
21354 right of point. On TTY frames, all characters have the same
21355 pixel width of 1, so we can use that. On GUI frames we don't
21356 have an easy way of getting at the pixel width of the
21357 character to the left of point, so we use a different method
21358 of getting to that place. */
21359 if (dir > 0)
21360 target_x = pt_x + pixel_width;
21361 else
21362 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21363
21364 /* Target X coordinate could be one line above or below the line
21365 of point, in which case we need to adjust the target X
21366 coordinate. Also, if moving to the left, we need to begin at
21367 the left edge of the point's screen line. */
21368 if (dir < 0)
21369 {
21370 if (pt_x > 0)
21371 {
21372 start_display (&it, w, pt);
21373 reseat_at_previous_visible_line_start (&it);
21374 it.current_x = it.current_y = it.hpos = 0;
21375 if (pt_vpos != 0)
21376 move_it_by_lines (&it, pt_vpos);
21377 }
21378 else
21379 {
21380 move_it_by_lines (&it, -1);
21381 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21382 target_is_eol_p = true;
21383 /* Under word-wrap, we don't know the x coordinate of
21384 the last character displayed on the previous line,
21385 which immediately precedes the wrap point. To find
21386 out its x coordinate, we try moving to the right
21387 margin of the window, which will stop at the wrap
21388 point, and then reset target_x to point at the
21389 character that precedes the wrap point. This is not
21390 needed on GUI frames, because (see below) there we
21391 move from the left margin one grapheme cluster at a
21392 time, and stop when we hit the wrap point. */
21393 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21394 {
21395 void *it_data = NULL;
21396 struct it it2;
21397
21398 SAVE_IT (it2, it, it_data);
21399 move_it_in_display_line_to (&it, ZV, target_x,
21400 MOVE_TO_POS | MOVE_TO_X);
21401 /* If we arrived at target_x, that _is_ the last
21402 character on the previous line. */
21403 if (it.current_x != target_x)
21404 target_x = it.current_x - 1;
21405 RESTORE_IT (&it, &it2, it_data);
21406 }
21407 }
21408 }
21409 else
21410 {
21411 if (at_eol_p
21412 || (target_x >= it.last_visible_x
21413 && it.line_wrap != TRUNCATE))
21414 {
21415 if (pt_x > 0)
21416 move_it_by_lines (&it, 0);
21417 move_it_by_lines (&it, 1);
21418 target_x = 0;
21419 }
21420 }
21421
21422 /* Move to the target X coordinate. */
21423 #ifdef HAVE_WINDOW_SYSTEM
21424 /* On GUI frames, as we don't know the X coordinate of the
21425 character to the left of point, moving point to the left
21426 requires walking, one grapheme cluster at a time, until we
21427 find ourself at a place immediately to the left of the
21428 character at point. */
21429 if (FRAME_WINDOW_P (it.f) && dir < 0)
21430 {
21431 struct text_pos new_pos;
21432 enum move_it_result rc = MOVE_X_REACHED;
21433
21434 if (it.current_x == 0)
21435 get_next_display_element (&it);
21436 if (it.what == IT_COMPOSITION)
21437 {
21438 new_pos.charpos = it.cmp_it.charpos;
21439 new_pos.bytepos = -1;
21440 }
21441 else
21442 new_pos = it.current.pos;
21443
21444 while (it.current_x + it.pixel_width <= target_x
21445 && (rc == MOVE_X_REACHED
21446 /* Under word-wrap, move_it_in_display_line_to
21447 stops at correct coordinates, but sometimes
21448 returns MOVE_POS_MATCH_OR_ZV. */
21449 || (it.line_wrap == WORD_WRAP
21450 && rc == MOVE_POS_MATCH_OR_ZV)))
21451 {
21452 int new_x = it.current_x + it.pixel_width;
21453
21454 /* For composed characters, we want the position of the
21455 first character in the grapheme cluster (usually, the
21456 composition's base character), whereas it.current
21457 might give us the position of the _last_ one, e.g. if
21458 the composition is rendered in reverse due to bidi
21459 reordering. */
21460 if (it.what == IT_COMPOSITION)
21461 {
21462 new_pos.charpos = it.cmp_it.charpos;
21463 new_pos.bytepos = -1;
21464 }
21465 else
21466 new_pos = it.current.pos;
21467 if (new_x == it.current_x)
21468 new_x++;
21469 rc = move_it_in_display_line_to (&it, ZV, new_x,
21470 MOVE_TO_POS | MOVE_TO_X);
21471 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21472 break;
21473 }
21474 /* The previous position we saw in the loop is the one we
21475 want. */
21476 if (new_pos.bytepos == -1)
21477 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21478 it.current.pos = new_pos;
21479 }
21480 else
21481 #endif
21482 if (it.current_x != target_x)
21483 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21484
21485 /* When lines are truncated, the above loop will stop at the
21486 window edge. But we want to get to the end of line, even if
21487 it is beyond the window edge; automatic hscroll will then
21488 scroll the window to show point as appropriate. */
21489 if (target_is_eol_p && it.line_wrap == TRUNCATE
21490 && get_next_display_element (&it))
21491 {
21492 struct text_pos new_pos = it.current.pos;
21493
21494 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21495 {
21496 set_iterator_to_next (&it, 0);
21497 if (it.method == GET_FROM_BUFFER)
21498 new_pos = it.current.pos;
21499 if (!get_next_display_element (&it))
21500 break;
21501 }
21502
21503 it.current.pos = new_pos;
21504 }
21505
21506 /* If we ended up in a display string that covers point, move to
21507 buffer position to the right in the visual order. */
21508 if (dir > 0)
21509 {
21510 while (IT_CHARPOS (it) == PT)
21511 {
21512 set_iterator_to_next (&it, 0);
21513 if (!get_next_display_element (&it))
21514 break;
21515 }
21516 }
21517
21518 /* Move point to that position. */
21519 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21520 }
21521
21522 return make_number (PT);
21523
21524 #undef ROW_GLYPH_NEWLINE_P
21525 }
21526
21527 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21528 Sbidi_resolved_levels, 0, 1, 0,
21529 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21530
21531 The resolved levels are produced by the Emacs bidi reordering engine
21532 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21533 read the Unicode Standard Annex 9 (UAX#9) for background information
21534 about these levels.
21535
21536 VPOS is the zero-based number of the current window's screen line
21537 for which to produce the resolved levels. If VPOS is nil or omitted,
21538 it defaults to the screen line of point. If the window displays a
21539 header line, VPOS of zero will report on the header line, and first
21540 line of text in the window will have VPOS of 1.
21541
21542 Value is an array of resolved levels, indexed by glyph number.
21543 Glyphs are numbered from zero starting from the beginning of the
21544 screen line, i.e. the left edge of the window for left-to-right lines
21545 and from the right edge for right-to-left lines. The resolved levels
21546 are produced only for the window's text area; text in display margins
21547 is not included.
21548
21549 If the selected window's display is not up-to-date, or if the specified
21550 screen line does not display text, this function returns nil. It is
21551 highly recommended to bind this function to some simple key, like F8,
21552 in order to avoid these problems.
21553
21554 This function exists mainly for testing the correctness of the
21555 Emacs UBA implementation, in particular with the test suite. */)
21556 (Lisp_Object vpos)
21557 {
21558 struct window *w = XWINDOW (selected_window);
21559 struct buffer *b = XBUFFER (w->contents);
21560 int nrow;
21561 struct glyph_row *row;
21562
21563 if (NILP (vpos))
21564 {
21565 int d1, d2, d3, d4, d5;
21566
21567 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21568 }
21569 else
21570 {
21571 CHECK_NUMBER_COERCE_MARKER (vpos);
21572 nrow = XINT (vpos);
21573 }
21574
21575 /* We require up-to-date glyph matrix for this window. */
21576 if (w->window_end_valid
21577 && !windows_or_buffers_changed
21578 && b
21579 && !b->clip_changed
21580 && !b->prevent_redisplay_optimizations_p
21581 && !window_outdated (w)
21582 && nrow >= 0
21583 && nrow < w->current_matrix->nrows
21584 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21585 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21586 {
21587 struct glyph *g, *e, *g1;
21588 int nglyphs, i;
21589 Lisp_Object levels;
21590
21591 if (!row->reversed_p) /* Left-to-right glyph row. */
21592 {
21593 g = g1 = row->glyphs[TEXT_AREA];
21594 e = g + row->used[TEXT_AREA];
21595
21596 /* Skip over glyphs at the start of the row that was
21597 generated by redisplay for its own needs. */
21598 while (g < e
21599 && INTEGERP (g->object)
21600 && g->charpos < 0)
21601 g++;
21602 g1 = g;
21603
21604 /* Count the "interesting" glyphs in this row. */
21605 for (nglyphs = 0; g < e && !INTEGERP (g->object); g++)
21606 nglyphs++;
21607
21608 /* Create and fill the array. */
21609 levels = make_uninit_vector (nglyphs);
21610 for (i = 0; g1 < g; i++, g1++)
21611 ASET (levels, i, make_number (g1->resolved_level));
21612 }
21613 else /* Right-to-left glyph row. */
21614 {
21615 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21616 e = row->glyphs[TEXT_AREA] - 1;
21617 while (g > e
21618 && INTEGERP (g->object)
21619 && g->charpos < 0)
21620 g--;
21621 g1 = g;
21622 for (nglyphs = 0; g > e && !INTEGERP (g->object); g--)
21623 nglyphs++;
21624 levels = make_uninit_vector (nglyphs);
21625 for (i = 0; g1 > g; i++, g1--)
21626 ASET (levels, i, make_number (g1->resolved_level));
21627 }
21628 return levels;
21629 }
21630 else
21631 return Qnil;
21632 }
21633
21634
21635 \f
21636 /***********************************************************************
21637 Menu Bar
21638 ***********************************************************************/
21639
21640 /* Redisplay the menu bar in the frame for window W.
21641
21642 The menu bar of X frames that don't have X toolkit support is
21643 displayed in a special window W->frame->menu_bar_window.
21644
21645 The menu bar of terminal frames is treated specially as far as
21646 glyph matrices are concerned. Menu bar lines are not part of
21647 windows, so the update is done directly on the frame matrix rows
21648 for the menu bar. */
21649
21650 static void
21651 display_menu_bar (struct window *w)
21652 {
21653 struct frame *f = XFRAME (WINDOW_FRAME (w));
21654 struct it it;
21655 Lisp_Object items;
21656 int i;
21657
21658 /* Don't do all this for graphical frames. */
21659 #ifdef HAVE_NTGUI
21660 if (FRAME_W32_P (f))
21661 return;
21662 #endif
21663 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21664 if (FRAME_X_P (f))
21665 return;
21666 #endif
21667
21668 #ifdef HAVE_NS
21669 if (FRAME_NS_P (f))
21670 return;
21671 #endif /* HAVE_NS */
21672
21673 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21674 eassert (!FRAME_WINDOW_P (f));
21675 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21676 it.first_visible_x = 0;
21677 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21678 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21679 if (FRAME_WINDOW_P (f))
21680 {
21681 /* Menu bar lines are displayed in the desired matrix of the
21682 dummy window menu_bar_window. */
21683 struct window *menu_w;
21684 menu_w = XWINDOW (f->menu_bar_window);
21685 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21686 MENU_FACE_ID);
21687 it.first_visible_x = 0;
21688 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21689 }
21690 else
21691 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21692 {
21693 /* This is a TTY frame, i.e. character hpos/vpos are used as
21694 pixel x/y. */
21695 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21696 MENU_FACE_ID);
21697 it.first_visible_x = 0;
21698 it.last_visible_x = FRAME_COLS (f);
21699 }
21700
21701 /* FIXME: This should be controlled by a user option. See the
21702 comments in redisplay_tool_bar and display_mode_line about
21703 this. */
21704 it.paragraph_embedding = L2R;
21705
21706 /* Clear all rows of the menu bar. */
21707 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21708 {
21709 struct glyph_row *row = it.glyph_row + i;
21710 clear_glyph_row (row);
21711 row->enabled_p = true;
21712 row->full_width_p = 1;
21713 row->reversed_p = false;
21714 }
21715
21716 /* Display all items of the menu bar. */
21717 items = FRAME_MENU_BAR_ITEMS (it.f);
21718 for (i = 0; i < ASIZE (items); i += 4)
21719 {
21720 Lisp_Object string;
21721
21722 /* Stop at nil string. */
21723 string = AREF (items, i + 1);
21724 if (NILP (string))
21725 break;
21726
21727 /* Remember where item was displayed. */
21728 ASET (items, i + 3, make_number (it.hpos));
21729
21730 /* Display the item, pad with one space. */
21731 if (it.current_x < it.last_visible_x)
21732 display_string (NULL, string, Qnil, 0, 0, &it,
21733 SCHARS (string) + 1, 0, 0, -1);
21734 }
21735
21736 /* Fill out the line with spaces. */
21737 if (it.current_x < it.last_visible_x)
21738 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21739
21740 /* Compute the total height of the lines. */
21741 compute_line_metrics (&it);
21742 }
21743
21744 /* Deep copy of a glyph row, including the glyphs. */
21745 static void
21746 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21747 {
21748 struct glyph *pointers[1 + LAST_AREA];
21749 int to_used = to->used[TEXT_AREA];
21750
21751 /* Save glyph pointers of TO. */
21752 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21753
21754 /* Do a structure assignment. */
21755 *to = *from;
21756
21757 /* Restore original glyph pointers of TO. */
21758 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21759
21760 /* Copy the glyphs. */
21761 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21762 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21763
21764 /* If we filled only part of the TO row, fill the rest with
21765 space_glyph (which will display as empty space). */
21766 if (to_used > from->used[TEXT_AREA])
21767 fill_up_frame_row_with_spaces (to, to_used);
21768 }
21769
21770 /* Display one menu item on a TTY, by overwriting the glyphs in the
21771 frame F's desired glyph matrix with glyphs produced from the menu
21772 item text. Called from term.c to display TTY drop-down menus one
21773 item at a time.
21774
21775 ITEM_TEXT is the menu item text as a C string.
21776
21777 FACE_ID is the face ID to be used for this menu item. FACE_ID
21778 could specify one of 3 faces: a face for an enabled item, a face
21779 for a disabled item, or a face for a selected item.
21780
21781 X and Y are coordinates of the first glyph in the frame's desired
21782 matrix to be overwritten by the menu item. Since this is a TTY, Y
21783 is the zero-based number of the glyph row and X is the zero-based
21784 glyph number in the row, starting from left, where to start
21785 displaying the item.
21786
21787 SUBMENU non-zero means this menu item drops down a submenu, which
21788 should be indicated by displaying a proper visual cue after the
21789 item text. */
21790
21791 void
21792 display_tty_menu_item (const char *item_text, int width, int face_id,
21793 int x, int y, int submenu)
21794 {
21795 struct it it;
21796 struct frame *f = SELECTED_FRAME ();
21797 struct window *w = XWINDOW (f->selected_window);
21798 int saved_used, saved_truncated, saved_width, saved_reversed;
21799 struct glyph_row *row;
21800 size_t item_len = strlen (item_text);
21801
21802 eassert (FRAME_TERMCAP_P (f));
21803
21804 /* Don't write beyond the matrix's last row. This can happen for
21805 TTY screens that are not high enough to show the entire menu.
21806 (This is actually a bit of defensive programming, as
21807 tty_menu_display already limits the number of menu items to one
21808 less than the number of screen lines.) */
21809 if (y >= f->desired_matrix->nrows)
21810 return;
21811
21812 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21813 it.first_visible_x = 0;
21814 it.last_visible_x = FRAME_COLS (f) - 1;
21815 row = it.glyph_row;
21816 /* Start with the row contents from the current matrix. */
21817 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21818 saved_width = row->full_width_p;
21819 row->full_width_p = 1;
21820 saved_reversed = row->reversed_p;
21821 row->reversed_p = 0;
21822 row->enabled_p = true;
21823
21824 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21825 desired face. */
21826 eassert (x < f->desired_matrix->matrix_w);
21827 it.current_x = it.hpos = x;
21828 it.current_y = it.vpos = y;
21829 saved_used = row->used[TEXT_AREA];
21830 saved_truncated = row->truncated_on_right_p;
21831 row->used[TEXT_AREA] = x;
21832 it.face_id = face_id;
21833 it.line_wrap = TRUNCATE;
21834
21835 /* FIXME: This should be controlled by a user option. See the
21836 comments in redisplay_tool_bar and display_mode_line about this.
21837 Also, if paragraph_embedding could ever be R2L, changes will be
21838 needed to avoid shifting to the right the row characters in
21839 term.c:append_glyph. */
21840 it.paragraph_embedding = L2R;
21841
21842 /* Pad with a space on the left. */
21843 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21844 width--;
21845 /* Display the menu item, pad with spaces to WIDTH. */
21846 if (submenu)
21847 {
21848 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21849 item_len, 0, FRAME_COLS (f) - 1, -1);
21850 width -= item_len;
21851 /* Indicate with " >" that there's a submenu. */
21852 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21853 FRAME_COLS (f) - 1, -1);
21854 }
21855 else
21856 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21857 width, 0, FRAME_COLS (f) - 1, -1);
21858
21859 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21860 row->truncated_on_right_p = saved_truncated;
21861 row->hash = row_hash (row);
21862 row->full_width_p = saved_width;
21863 row->reversed_p = saved_reversed;
21864 }
21865 \f
21866 /***********************************************************************
21867 Mode Line
21868 ***********************************************************************/
21869
21870 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21871 FORCE is non-zero, redisplay mode lines unconditionally.
21872 Otherwise, redisplay only mode lines that are garbaged. Value is
21873 the number of windows whose mode lines were redisplayed. */
21874
21875 static int
21876 redisplay_mode_lines (Lisp_Object window, bool force)
21877 {
21878 int nwindows = 0;
21879
21880 while (!NILP (window))
21881 {
21882 struct window *w = XWINDOW (window);
21883
21884 if (WINDOWP (w->contents))
21885 nwindows += redisplay_mode_lines (w->contents, force);
21886 else if (force
21887 || FRAME_GARBAGED_P (XFRAME (w->frame))
21888 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21889 {
21890 struct text_pos lpoint;
21891 struct buffer *old = current_buffer;
21892
21893 /* Set the window's buffer for the mode line display. */
21894 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21895 set_buffer_internal_1 (XBUFFER (w->contents));
21896
21897 /* Point refers normally to the selected window. For any
21898 other window, set up appropriate value. */
21899 if (!EQ (window, selected_window))
21900 {
21901 struct text_pos pt;
21902
21903 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21904 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21905 }
21906
21907 /* Display mode lines. */
21908 clear_glyph_matrix (w->desired_matrix);
21909 if (display_mode_lines (w))
21910 ++nwindows;
21911
21912 /* Restore old settings. */
21913 set_buffer_internal_1 (old);
21914 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21915 }
21916
21917 window = w->next;
21918 }
21919
21920 return nwindows;
21921 }
21922
21923
21924 /* Display the mode and/or header line of window W. Value is the
21925 sum number of mode lines and header lines displayed. */
21926
21927 static int
21928 display_mode_lines (struct window *w)
21929 {
21930 Lisp_Object old_selected_window = selected_window;
21931 Lisp_Object old_selected_frame = selected_frame;
21932 Lisp_Object new_frame = w->frame;
21933 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21934 int n = 0;
21935
21936 selected_frame = new_frame;
21937 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21938 or window's point, then we'd need select_window_1 here as well. */
21939 XSETWINDOW (selected_window, w);
21940 XFRAME (new_frame)->selected_window = selected_window;
21941
21942 /* These will be set while the mode line specs are processed. */
21943 line_number_displayed = 0;
21944 w->column_number_displayed = -1;
21945
21946 if (WINDOW_WANTS_MODELINE_P (w))
21947 {
21948 struct window *sel_w = XWINDOW (old_selected_window);
21949
21950 /* Select mode line face based on the real selected window. */
21951 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21952 BVAR (current_buffer, mode_line_format));
21953 ++n;
21954 }
21955
21956 if (WINDOW_WANTS_HEADER_LINE_P (w))
21957 {
21958 display_mode_line (w, HEADER_LINE_FACE_ID,
21959 BVAR (current_buffer, header_line_format));
21960 ++n;
21961 }
21962
21963 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21964 selected_frame = old_selected_frame;
21965 selected_window = old_selected_window;
21966 if (n > 0)
21967 w->must_be_updated_p = true;
21968 return n;
21969 }
21970
21971
21972 /* Display mode or header line of window W. FACE_ID specifies which
21973 line to display; it is either MODE_LINE_FACE_ID or
21974 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21975 display. Value is the pixel height of the mode/header line
21976 displayed. */
21977
21978 static int
21979 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21980 {
21981 struct it it;
21982 struct face *face;
21983 ptrdiff_t count = SPECPDL_INDEX ();
21984
21985 init_iterator (&it, w, -1, -1, NULL, face_id);
21986 /* Don't extend on a previously drawn mode-line.
21987 This may happen if called from pos_visible_p. */
21988 it.glyph_row->enabled_p = false;
21989 prepare_desired_row (w, it.glyph_row, true);
21990
21991 it.glyph_row->mode_line_p = 1;
21992
21993 /* FIXME: This should be controlled by a user option. But
21994 supporting such an option is not trivial, since the mode line is
21995 made up of many separate strings. */
21996 it.paragraph_embedding = L2R;
21997
21998 record_unwind_protect (unwind_format_mode_line,
21999 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22000
22001 mode_line_target = MODE_LINE_DISPLAY;
22002
22003 /* Temporarily make frame's keyboard the current kboard so that
22004 kboard-local variables in the mode_line_format will get the right
22005 values. */
22006 push_kboard (FRAME_KBOARD (it.f));
22007 record_unwind_save_match_data ();
22008 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22009 pop_kboard ();
22010
22011 unbind_to (count, Qnil);
22012
22013 /* Fill up with spaces. */
22014 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22015
22016 compute_line_metrics (&it);
22017 it.glyph_row->full_width_p = 1;
22018 it.glyph_row->continued_p = 0;
22019 it.glyph_row->truncated_on_left_p = 0;
22020 it.glyph_row->truncated_on_right_p = 0;
22021
22022 /* Make a 3D mode-line have a shadow at its right end. */
22023 face = FACE_FROM_ID (it.f, face_id);
22024 extend_face_to_end_of_line (&it);
22025 if (face->box != FACE_NO_BOX)
22026 {
22027 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22028 + it.glyph_row->used[TEXT_AREA] - 1);
22029 last->right_box_line_p = 1;
22030 }
22031
22032 return it.glyph_row->height;
22033 }
22034
22035 /* Move element ELT in LIST to the front of LIST.
22036 Return the updated list. */
22037
22038 static Lisp_Object
22039 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22040 {
22041 register Lisp_Object tail, prev;
22042 register Lisp_Object tem;
22043
22044 tail = list;
22045 prev = Qnil;
22046 while (CONSP (tail))
22047 {
22048 tem = XCAR (tail);
22049
22050 if (EQ (elt, tem))
22051 {
22052 /* Splice out the link TAIL. */
22053 if (NILP (prev))
22054 list = XCDR (tail);
22055 else
22056 Fsetcdr (prev, XCDR (tail));
22057
22058 /* Now make it the first. */
22059 Fsetcdr (tail, list);
22060 return tail;
22061 }
22062 else
22063 prev = tail;
22064 tail = XCDR (tail);
22065 QUIT;
22066 }
22067
22068 /* Not found--return unchanged LIST. */
22069 return list;
22070 }
22071
22072 /* Contribute ELT to the mode line for window IT->w. How it
22073 translates into text depends on its data type.
22074
22075 IT describes the display environment in which we display, as usual.
22076
22077 DEPTH is the depth in recursion. It is used to prevent
22078 infinite recursion here.
22079
22080 FIELD_WIDTH is the number of characters the display of ELT should
22081 occupy in the mode line, and PRECISION is the maximum number of
22082 characters to display from ELT's representation. See
22083 display_string for details.
22084
22085 Returns the hpos of the end of the text generated by ELT.
22086
22087 PROPS is a property list to add to any string we encounter.
22088
22089 If RISKY is nonzero, remove (disregard) any properties in any string
22090 we encounter, and ignore :eval and :propertize.
22091
22092 The global variable `mode_line_target' determines whether the
22093 output is passed to `store_mode_line_noprop',
22094 `store_mode_line_string', or `display_string'. */
22095
22096 static int
22097 display_mode_element (struct it *it, int depth, int field_width, int precision,
22098 Lisp_Object elt, Lisp_Object props, int risky)
22099 {
22100 int n = 0, field, prec;
22101 int literal = 0;
22102
22103 tail_recurse:
22104 if (depth > 100)
22105 elt = build_string ("*too-deep*");
22106
22107 depth++;
22108
22109 switch (XTYPE (elt))
22110 {
22111 case Lisp_String:
22112 {
22113 /* A string: output it and check for %-constructs within it. */
22114 unsigned char c;
22115 ptrdiff_t offset = 0;
22116
22117 if (SCHARS (elt) > 0
22118 && (!NILP (props) || risky))
22119 {
22120 Lisp_Object oprops, aelt;
22121 oprops = Ftext_properties_at (make_number (0), elt);
22122
22123 /* If the starting string's properties are not what
22124 we want, translate the string. Also, if the string
22125 is risky, do that anyway. */
22126
22127 if (NILP (Fequal (props, oprops)) || risky)
22128 {
22129 /* If the starting string has properties,
22130 merge the specified ones onto the existing ones. */
22131 if (! NILP (oprops) && !risky)
22132 {
22133 Lisp_Object tem;
22134
22135 oprops = Fcopy_sequence (oprops);
22136 tem = props;
22137 while (CONSP (tem))
22138 {
22139 oprops = Fplist_put (oprops, XCAR (tem),
22140 XCAR (XCDR (tem)));
22141 tem = XCDR (XCDR (tem));
22142 }
22143 props = oprops;
22144 }
22145
22146 aelt = Fassoc (elt, mode_line_proptrans_alist);
22147 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22148 {
22149 /* AELT is what we want. Move it to the front
22150 without consing. */
22151 elt = XCAR (aelt);
22152 mode_line_proptrans_alist
22153 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22154 }
22155 else
22156 {
22157 Lisp_Object tem;
22158
22159 /* If AELT has the wrong props, it is useless.
22160 so get rid of it. */
22161 if (! NILP (aelt))
22162 mode_line_proptrans_alist
22163 = Fdelq (aelt, mode_line_proptrans_alist);
22164
22165 elt = Fcopy_sequence (elt);
22166 Fset_text_properties (make_number (0), Flength (elt),
22167 props, elt);
22168 /* Add this item to mode_line_proptrans_alist. */
22169 mode_line_proptrans_alist
22170 = Fcons (Fcons (elt, props),
22171 mode_line_proptrans_alist);
22172 /* Truncate mode_line_proptrans_alist
22173 to at most 50 elements. */
22174 tem = Fnthcdr (make_number (50),
22175 mode_line_proptrans_alist);
22176 if (! NILP (tem))
22177 XSETCDR (tem, Qnil);
22178 }
22179 }
22180 }
22181
22182 offset = 0;
22183
22184 if (literal)
22185 {
22186 prec = precision - n;
22187 switch (mode_line_target)
22188 {
22189 case MODE_LINE_NOPROP:
22190 case MODE_LINE_TITLE:
22191 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22192 break;
22193 case MODE_LINE_STRING:
22194 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22195 break;
22196 case MODE_LINE_DISPLAY:
22197 n += display_string (NULL, elt, Qnil, 0, 0, it,
22198 0, prec, 0, STRING_MULTIBYTE (elt));
22199 break;
22200 }
22201
22202 break;
22203 }
22204
22205 /* Handle the non-literal case. */
22206
22207 while ((precision <= 0 || n < precision)
22208 && SREF (elt, offset) != 0
22209 && (mode_line_target != MODE_LINE_DISPLAY
22210 || it->current_x < it->last_visible_x))
22211 {
22212 ptrdiff_t last_offset = offset;
22213
22214 /* Advance to end of string or next format specifier. */
22215 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22216 ;
22217
22218 if (offset - 1 != last_offset)
22219 {
22220 ptrdiff_t nchars, nbytes;
22221
22222 /* Output to end of string or up to '%'. Field width
22223 is length of string. Don't output more than
22224 PRECISION allows us. */
22225 offset--;
22226
22227 prec = c_string_width (SDATA (elt) + last_offset,
22228 offset - last_offset, precision - n,
22229 &nchars, &nbytes);
22230
22231 switch (mode_line_target)
22232 {
22233 case MODE_LINE_NOPROP:
22234 case MODE_LINE_TITLE:
22235 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22236 break;
22237 case MODE_LINE_STRING:
22238 {
22239 ptrdiff_t bytepos = last_offset;
22240 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22241 ptrdiff_t endpos = (precision <= 0
22242 ? string_byte_to_char (elt, offset)
22243 : charpos + nchars);
22244
22245 n += store_mode_line_string (NULL,
22246 Fsubstring (elt, make_number (charpos),
22247 make_number (endpos)),
22248 0, 0, 0, Qnil);
22249 }
22250 break;
22251 case MODE_LINE_DISPLAY:
22252 {
22253 ptrdiff_t bytepos = last_offset;
22254 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22255
22256 if (precision <= 0)
22257 nchars = string_byte_to_char (elt, offset) - charpos;
22258 n += display_string (NULL, elt, Qnil, 0, charpos,
22259 it, 0, nchars, 0,
22260 STRING_MULTIBYTE (elt));
22261 }
22262 break;
22263 }
22264 }
22265 else /* c == '%' */
22266 {
22267 ptrdiff_t percent_position = offset;
22268
22269 /* Get the specified minimum width. Zero means
22270 don't pad. */
22271 field = 0;
22272 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22273 field = field * 10 + c - '0';
22274
22275 /* Don't pad beyond the total padding allowed. */
22276 if (field_width - n > 0 && field > field_width - n)
22277 field = field_width - n;
22278
22279 /* Note that either PRECISION <= 0 or N < PRECISION. */
22280 prec = precision - n;
22281
22282 if (c == 'M')
22283 n += display_mode_element (it, depth, field, prec,
22284 Vglobal_mode_string, props,
22285 risky);
22286 else if (c != 0)
22287 {
22288 bool multibyte;
22289 ptrdiff_t bytepos, charpos;
22290 const char *spec;
22291 Lisp_Object string;
22292
22293 bytepos = percent_position;
22294 charpos = (STRING_MULTIBYTE (elt)
22295 ? string_byte_to_char (elt, bytepos)
22296 : bytepos);
22297 spec = decode_mode_spec (it->w, c, field, &string);
22298 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22299
22300 switch (mode_line_target)
22301 {
22302 case MODE_LINE_NOPROP:
22303 case MODE_LINE_TITLE:
22304 n += store_mode_line_noprop (spec, field, prec);
22305 break;
22306 case MODE_LINE_STRING:
22307 {
22308 Lisp_Object tem = build_string (spec);
22309 props = Ftext_properties_at (make_number (charpos), elt);
22310 /* Should only keep face property in props */
22311 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22312 }
22313 break;
22314 case MODE_LINE_DISPLAY:
22315 {
22316 int nglyphs_before, nwritten;
22317
22318 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22319 nwritten = display_string (spec, string, elt,
22320 charpos, 0, it,
22321 field, prec, 0,
22322 multibyte);
22323
22324 /* Assign to the glyphs written above the
22325 string where the `%x' came from, position
22326 of the `%'. */
22327 if (nwritten > 0)
22328 {
22329 struct glyph *glyph
22330 = (it->glyph_row->glyphs[TEXT_AREA]
22331 + nglyphs_before);
22332 int i;
22333
22334 for (i = 0; i < nwritten; ++i)
22335 {
22336 glyph[i].object = elt;
22337 glyph[i].charpos = charpos;
22338 }
22339
22340 n += nwritten;
22341 }
22342 }
22343 break;
22344 }
22345 }
22346 else /* c == 0 */
22347 break;
22348 }
22349 }
22350 }
22351 break;
22352
22353 case Lisp_Symbol:
22354 /* A symbol: process the value of the symbol recursively
22355 as if it appeared here directly. Avoid error if symbol void.
22356 Special case: if value of symbol is a string, output the string
22357 literally. */
22358 {
22359 register Lisp_Object tem;
22360
22361 /* If the variable is not marked as risky to set
22362 then its contents are risky to use. */
22363 if (NILP (Fget (elt, Qrisky_local_variable)))
22364 risky = 1;
22365
22366 tem = Fboundp (elt);
22367 if (!NILP (tem))
22368 {
22369 tem = Fsymbol_value (elt);
22370 /* If value is a string, output that string literally:
22371 don't check for % within it. */
22372 if (STRINGP (tem))
22373 literal = 1;
22374
22375 if (!EQ (tem, elt))
22376 {
22377 /* Give up right away for nil or t. */
22378 elt = tem;
22379 goto tail_recurse;
22380 }
22381 }
22382 }
22383 break;
22384
22385 case Lisp_Cons:
22386 {
22387 register Lisp_Object car, tem;
22388
22389 /* A cons cell: five distinct cases.
22390 If first element is :eval or :propertize, do something special.
22391 If first element is a string or a cons, process all the elements
22392 and effectively concatenate them.
22393 If first element is a negative number, truncate displaying cdr to
22394 at most that many characters. If positive, pad (with spaces)
22395 to at least that many characters.
22396 If first element is a symbol, process the cadr or caddr recursively
22397 according to whether the symbol's value is non-nil or nil. */
22398 car = XCAR (elt);
22399 if (EQ (car, QCeval))
22400 {
22401 /* An element of the form (:eval FORM) means evaluate FORM
22402 and use the result as mode line elements. */
22403
22404 if (risky)
22405 break;
22406
22407 if (CONSP (XCDR (elt)))
22408 {
22409 Lisp_Object spec;
22410 spec = safe__eval (true, XCAR (XCDR (elt)));
22411 n += display_mode_element (it, depth, field_width - n,
22412 precision - n, spec, props,
22413 risky);
22414 }
22415 }
22416 else if (EQ (car, QCpropertize))
22417 {
22418 /* An element of the form (:propertize ELT PROPS...)
22419 means display ELT but applying properties PROPS. */
22420
22421 if (risky)
22422 break;
22423
22424 if (CONSP (XCDR (elt)))
22425 n += display_mode_element (it, depth, field_width - n,
22426 precision - n, XCAR (XCDR (elt)),
22427 XCDR (XCDR (elt)), risky);
22428 }
22429 else if (SYMBOLP (car))
22430 {
22431 tem = Fboundp (car);
22432 elt = XCDR (elt);
22433 if (!CONSP (elt))
22434 goto invalid;
22435 /* elt is now the cdr, and we know it is a cons cell.
22436 Use its car if CAR has a non-nil value. */
22437 if (!NILP (tem))
22438 {
22439 tem = Fsymbol_value (car);
22440 if (!NILP (tem))
22441 {
22442 elt = XCAR (elt);
22443 goto tail_recurse;
22444 }
22445 }
22446 /* Symbol's value is nil (or symbol is unbound)
22447 Get the cddr of the original list
22448 and if possible find the caddr and use that. */
22449 elt = XCDR (elt);
22450 if (NILP (elt))
22451 break;
22452 else if (!CONSP (elt))
22453 goto invalid;
22454 elt = XCAR (elt);
22455 goto tail_recurse;
22456 }
22457 else if (INTEGERP (car))
22458 {
22459 register int lim = XINT (car);
22460 elt = XCDR (elt);
22461 if (lim < 0)
22462 {
22463 /* Negative int means reduce maximum width. */
22464 if (precision <= 0)
22465 precision = -lim;
22466 else
22467 precision = min (precision, -lim);
22468 }
22469 else if (lim > 0)
22470 {
22471 /* Padding specified. Don't let it be more than
22472 current maximum. */
22473 if (precision > 0)
22474 lim = min (precision, lim);
22475
22476 /* If that's more padding than already wanted, queue it.
22477 But don't reduce padding already specified even if
22478 that is beyond the current truncation point. */
22479 field_width = max (lim, field_width);
22480 }
22481 goto tail_recurse;
22482 }
22483 else if (STRINGP (car) || CONSP (car))
22484 {
22485 Lisp_Object halftail = elt;
22486 int len = 0;
22487
22488 while (CONSP (elt)
22489 && (precision <= 0 || n < precision))
22490 {
22491 n += display_mode_element (it, depth,
22492 /* Do padding only after the last
22493 element in the list. */
22494 (! CONSP (XCDR (elt))
22495 ? field_width - n
22496 : 0),
22497 precision - n, XCAR (elt),
22498 props, risky);
22499 elt = XCDR (elt);
22500 len++;
22501 if ((len & 1) == 0)
22502 halftail = XCDR (halftail);
22503 /* Check for cycle. */
22504 if (EQ (halftail, elt))
22505 break;
22506 }
22507 }
22508 }
22509 break;
22510
22511 default:
22512 invalid:
22513 elt = build_string ("*invalid*");
22514 goto tail_recurse;
22515 }
22516
22517 /* Pad to FIELD_WIDTH. */
22518 if (field_width > 0 && n < field_width)
22519 {
22520 switch (mode_line_target)
22521 {
22522 case MODE_LINE_NOPROP:
22523 case MODE_LINE_TITLE:
22524 n += store_mode_line_noprop ("", field_width - n, 0);
22525 break;
22526 case MODE_LINE_STRING:
22527 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22528 break;
22529 case MODE_LINE_DISPLAY:
22530 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22531 0, 0, 0);
22532 break;
22533 }
22534 }
22535
22536 return n;
22537 }
22538
22539 /* Store a mode-line string element in mode_line_string_list.
22540
22541 If STRING is non-null, display that C string. Otherwise, the Lisp
22542 string LISP_STRING is displayed.
22543
22544 FIELD_WIDTH is the minimum number of output glyphs to produce.
22545 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22546 with spaces. FIELD_WIDTH <= 0 means don't pad.
22547
22548 PRECISION is the maximum number of characters to output from
22549 STRING. PRECISION <= 0 means don't truncate the string.
22550
22551 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22552 properties to the string.
22553
22554 PROPS are the properties to add to the string.
22555 The mode_line_string_face face property is always added to the string.
22556 */
22557
22558 static int
22559 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22560 int field_width, int precision, Lisp_Object props)
22561 {
22562 ptrdiff_t len;
22563 int n = 0;
22564
22565 if (string != NULL)
22566 {
22567 len = strlen (string);
22568 if (precision > 0 && len > precision)
22569 len = precision;
22570 lisp_string = make_string (string, len);
22571 if (NILP (props))
22572 props = mode_line_string_face_prop;
22573 else if (!NILP (mode_line_string_face))
22574 {
22575 Lisp_Object face = Fplist_get (props, Qface);
22576 props = Fcopy_sequence (props);
22577 if (NILP (face))
22578 face = mode_line_string_face;
22579 else
22580 face = list2 (face, mode_line_string_face);
22581 props = Fplist_put (props, Qface, face);
22582 }
22583 Fadd_text_properties (make_number (0), make_number (len),
22584 props, lisp_string);
22585 }
22586 else
22587 {
22588 len = XFASTINT (Flength (lisp_string));
22589 if (precision > 0 && len > precision)
22590 {
22591 len = precision;
22592 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22593 precision = -1;
22594 }
22595 if (!NILP (mode_line_string_face))
22596 {
22597 Lisp_Object face;
22598 if (NILP (props))
22599 props = Ftext_properties_at (make_number (0), lisp_string);
22600 face = Fplist_get (props, Qface);
22601 if (NILP (face))
22602 face = mode_line_string_face;
22603 else
22604 face = list2 (face, mode_line_string_face);
22605 props = list2 (Qface, face);
22606 if (copy_string)
22607 lisp_string = Fcopy_sequence (lisp_string);
22608 }
22609 if (!NILP (props))
22610 Fadd_text_properties (make_number (0), make_number (len),
22611 props, lisp_string);
22612 }
22613
22614 if (len > 0)
22615 {
22616 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22617 n += len;
22618 }
22619
22620 if (field_width > len)
22621 {
22622 field_width -= len;
22623 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22624 if (!NILP (props))
22625 Fadd_text_properties (make_number (0), make_number (field_width),
22626 props, lisp_string);
22627 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22628 n += field_width;
22629 }
22630
22631 return n;
22632 }
22633
22634
22635 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22636 1, 4, 0,
22637 doc: /* Format a string out of a mode line format specification.
22638 First arg FORMAT specifies the mode line format (see `mode-line-format'
22639 for details) to use.
22640
22641 By default, the format is evaluated for the currently selected window.
22642
22643 Optional second arg FACE specifies the face property to put on all
22644 characters for which no face is specified. The value nil means the
22645 default face. The value t means whatever face the window's mode line
22646 currently uses (either `mode-line' or `mode-line-inactive',
22647 depending on whether the window is the selected window or not).
22648 An integer value means the value string has no text
22649 properties.
22650
22651 Optional third and fourth args WINDOW and BUFFER specify the window
22652 and buffer to use as the context for the formatting (defaults
22653 are the selected window and the WINDOW's buffer). */)
22654 (Lisp_Object format, Lisp_Object face,
22655 Lisp_Object window, Lisp_Object buffer)
22656 {
22657 struct it it;
22658 int len;
22659 struct window *w;
22660 struct buffer *old_buffer = NULL;
22661 int face_id;
22662 int no_props = INTEGERP (face);
22663 ptrdiff_t count = SPECPDL_INDEX ();
22664 Lisp_Object str;
22665 int string_start = 0;
22666
22667 w = decode_any_window (window);
22668 XSETWINDOW (window, w);
22669
22670 if (NILP (buffer))
22671 buffer = w->contents;
22672 CHECK_BUFFER (buffer);
22673
22674 /* Make formatting the modeline a non-op when noninteractive, otherwise
22675 there will be problems later caused by a partially initialized frame. */
22676 if (NILP (format) || noninteractive)
22677 return empty_unibyte_string;
22678
22679 if (no_props)
22680 face = Qnil;
22681
22682 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22683 : EQ (face, Qt) ? (EQ (window, selected_window)
22684 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22685 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22686 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22687 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22688 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22689 : DEFAULT_FACE_ID;
22690
22691 old_buffer = current_buffer;
22692
22693 /* Save things including mode_line_proptrans_alist,
22694 and set that to nil so that we don't alter the outer value. */
22695 record_unwind_protect (unwind_format_mode_line,
22696 format_mode_line_unwind_data
22697 (XFRAME (WINDOW_FRAME (w)),
22698 old_buffer, selected_window, 1));
22699 mode_line_proptrans_alist = Qnil;
22700
22701 Fselect_window (window, Qt);
22702 set_buffer_internal_1 (XBUFFER (buffer));
22703
22704 init_iterator (&it, w, -1, -1, NULL, face_id);
22705
22706 if (no_props)
22707 {
22708 mode_line_target = MODE_LINE_NOPROP;
22709 mode_line_string_face_prop = Qnil;
22710 mode_line_string_list = Qnil;
22711 string_start = MODE_LINE_NOPROP_LEN (0);
22712 }
22713 else
22714 {
22715 mode_line_target = MODE_LINE_STRING;
22716 mode_line_string_list = Qnil;
22717 mode_line_string_face = face;
22718 mode_line_string_face_prop
22719 = NILP (face) ? Qnil : list2 (Qface, face);
22720 }
22721
22722 push_kboard (FRAME_KBOARD (it.f));
22723 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22724 pop_kboard ();
22725
22726 if (no_props)
22727 {
22728 len = MODE_LINE_NOPROP_LEN (string_start);
22729 str = make_string (mode_line_noprop_buf + string_start, len);
22730 }
22731 else
22732 {
22733 mode_line_string_list = Fnreverse (mode_line_string_list);
22734 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22735 empty_unibyte_string);
22736 }
22737
22738 unbind_to (count, Qnil);
22739 return str;
22740 }
22741
22742 /* Write a null-terminated, right justified decimal representation of
22743 the positive integer D to BUF using a minimal field width WIDTH. */
22744
22745 static void
22746 pint2str (register char *buf, register int width, register ptrdiff_t d)
22747 {
22748 register char *p = buf;
22749
22750 if (d <= 0)
22751 *p++ = '0';
22752 else
22753 {
22754 while (d > 0)
22755 {
22756 *p++ = d % 10 + '0';
22757 d /= 10;
22758 }
22759 }
22760
22761 for (width -= (int) (p - buf); width > 0; --width)
22762 *p++ = ' ';
22763 *p-- = '\0';
22764 while (p > buf)
22765 {
22766 d = *buf;
22767 *buf++ = *p;
22768 *p-- = d;
22769 }
22770 }
22771
22772 /* Write a null-terminated, right justified decimal and "human
22773 readable" representation of the nonnegative integer D to BUF using
22774 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22775
22776 static const char power_letter[] =
22777 {
22778 0, /* no letter */
22779 'k', /* kilo */
22780 'M', /* mega */
22781 'G', /* giga */
22782 'T', /* tera */
22783 'P', /* peta */
22784 'E', /* exa */
22785 'Z', /* zetta */
22786 'Y' /* yotta */
22787 };
22788
22789 static void
22790 pint2hrstr (char *buf, int width, ptrdiff_t d)
22791 {
22792 /* We aim to represent the nonnegative integer D as
22793 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22794 ptrdiff_t quotient = d;
22795 int remainder = 0;
22796 /* -1 means: do not use TENTHS. */
22797 int tenths = -1;
22798 int exponent = 0;
22799
22800 /* Length of QUOTIENT.TENTHS as a string. */
22801 int length;
22802
22803 char * psuffix;
22804 char * p;
22805
22806 if (quotient >= 1000)
22807 {
22808 /* Scale to the appropriate EXPONENT. */
22809 do
22810 {
22811 remainder = quotient % 1000;
22812 quotient /= 1000;
22813 exponent++;
22814 }
22815 while (quotient >= 1000);
22816
22817 /* Round to nearest and decide whether to use TENTHS or not. */
22818 if (quotient <= 9)
22819 {
22820 tenths = remainder / 100;
22821 if (remainder % 100 >= 50)
22822 {
22823 if (tenths < 9)
22824 tenths++;
22825 else
22826 {
22827 quotient++;
22828 if (quotient == 10)
22829 tenths = -1;
22830 else
22831 tenths = 0;
22832 }
22833 }
22834 }
22835 else
22836 if (remainder >= 500)
22837 {
22838 if (quotient < 999)
22839 quotient++;
22840 else
22841 {
22842 quotient = 1;
22843 exponent++;
22844 tenths = 0;
22845 }
22846 }
22847 }
22848
22849 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22850 if (tenths == -1 && quotient <= 99)
22851 if (quotient <= 9)
22852 length = 1;
22853 else
22854 length = 2;
22855 else
22856 length = 3;
22857 p = psuffix = buf + max (width, length);
22858
22859 /* Print EXPONENT. */
22860 *psuffix++ = power_letter[exponent];
22861 *psuffix = '\0';
22862
22863 /* Print TENTHS. */
22864 if (tenths >= 0)
22865 {
22866 *--p = '0' + tenths;
22867 *--p = '.';
22868 }
22869
22870 /* Print QUOTIENT. */
22871 do
22872 {
22873 int digit = quotient % 10;
22874 *--p = '0' + digit;
22875 }
22876 while ((quotient /= 10) != 0);
22877
22878 /* Print leading spaces. */
22879 while (buf < p)
22880 *--p = ' ';
22881 }
22882
22883 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22884 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22885 type of CODING_SYSTEM. Return updated pointer into BUF. */
22886
22887 static unsigned char invalid_eol_type[] = "(*invalid*)";
22888
22889 static char *
22890 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22891 {
22892 Lisp_Object val;
22893 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22894 const unsigned char *eol_str;
22895 int eol_str_len;
22896 /* The EOL conversion we are using. */
22897 Lisp_Object eoltype;
22898
22899 val = CODING_SYSTEM_SPEC (coding_system);
22900 eoltype = Qnil;
22901
22902 if (!VECTORP (val)) /* Not yet decided. */
22903 {
22904 *buf++ = multibyte ? '-' : ' ';
22905 if (eol_flag)
22906 eoltype = eol_mnemonic_undecided;
22907 /* Don't mention EOL conversion if it isn't decided. */
22908 }
22909 else
22910 {
22911 Lisp_Object attrs;
22912 Lisp_Object eolvalue;
22913
22914 attrs = AREF (val, 0);
22915 eolvalue = AREF (val, 2);
22916
22917 *buf++ = multibyte
22918 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22919 : ' ';
22920
22921 if (eol_flag)
22922 {
22923 /* The EOL conversion that is normal on this system. */
22924
22925 if (NILP (eolvalue)) /* Not yet decided. */
22926 eoltype = eol_mnemonic_undecided;
22927 else if (VECTORP (eolvalue)) /* Not yet decided. */
22928 eoltype = eol_mnemonic_undecided;
22929 else /* eolvalue is Qunix, Qdos, or Qmac. */
22930 eoltype = (EQ (eolvalue, Qunix)
22931 ? eol_mnemonic_unix
22932 : (EQ (eolvalue, Qdos) == 1
22933 ? eol_mnemonic_dos : eol_mnemonic_mac));
22934 }
22935 }
22936
22937 if (eol_flag)
22938 {
22939 /* Mention the EOL conversion if it is not the usual one. */
22940 if (STRINGP (eoltype))
22941 {
22942 eol_str = SDATA (eoltype);
22943 eol_str_len = SBYTES (eoltype);
22944 }
22945 else if (CHARACTERP (eoltype))
22946 {
22947 int c = XFASTINT (eoltype);
22948 return buf + CHAR_STRING (c, (unsigned char *) buf);
22949 }
22950 else
22951 {
22952 eol_str = invalid_eol_type;
22953 eol_str_len = sizeof (invalid_eol_type) - 1;
22954 }
22955 memcpy (buf, eol_str, eol_str_len);
22956 buf += eol_str_len;
22957 }
22958
22959 return buf;
22960 }
22961
22962 /* Return a string for the output of a mode line %-spec for window W,
22963 generated by character C. FIELD_WIDTH > 0 means pad the string
22964 returned with spaces to that value. Return a Lisp string in
22965 *STRING if the resulting string is taken from that Lisp string.
22966
22967 Note we operate on the current buffer for most purposes. */
22968
22969 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22970
22971 static const char *
22972 decode_mode_spec (struct window *w, register int c, int field_width,
22973 Lisp_Object *string)
22974 {
22975 Lisp_Object obj;
22976 struct frame *f = XFRAME (WINDOW_FRAME (w));
22977 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22978 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22979 produce strings from numerical values, so limit preposterously
22980 large values of FIELD_WIDTH to avoid overrunning the buffer's
22981 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22982 bytes plus the terminating null. */
22983 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22984 struct buffer *b = current_buffer;
22985
22986 obj = Qnil;
22987 *string = Qnil;
22988
22989 switch (c)
22990 {
22991 case '*':
22992 if (!NILP (BVAR (b, read_only)))
22993 return "%";
22994 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22995 return "*";
22996 return "-";
22997
22998 case '+':
22999 /* This differs from %* only for a modified read-only buffer. */
23000 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23001 return "*";
23002 if (!NILP (BVAR (b, read_only)))
23003 return "%";
23004 return "-";
23005
23006 case '&':
23007 /* This differs from %* in ignoring read-only-ness. */
23008 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23009 return "*";
23010 return "-";
23011
23012 case '%':
23013 return "%";
23014
23015 case '[':
23016 {
23017 int i;
23018 char *p;
23019
23020 if (command_loop_level > 5)
23021 return "[[[... ";
23022 p = decode_mode_spec_buf;
23023 for (i = 0; i < command_loop_level; i++)
23024 *p++ = '[';
23025 *p = 0;
23026 return decode_mode_spec_buf;
23027 }
23028
23029 case ']':
23030 {
23031 int i;
23032 char *p;
23033
23034 if (command_loop_level > 5)
23035 return " ...]]]";
23036 p = decode_mode_spec_buf;
23037 for (i = 0; i < command_loop_level; i++)
23038 *p++ = ']';
23039 *p = 0;
23040 return decode_mode_spec_buf;
23041 }
23042
23043 case '-':
23044 {
23045 register int i;
23046
23047 /* Let lots_of_dashes be a string of infinite length. */
23048 if (mode_line_target == MODE_LINE_NOPROP
23049 || mode_line_target == MODE_LINE_STRING)
23050 return "--";
23051 if (field_width <= 0
23052 || field_width > sizeof (lots_of_dashes))
23053 {
23054 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23055 decode_mode_spec_buf[i] = '-';
23056 decode_mode_spec_buf[i] = '\0';
23057 return decode_mode_spec_buf;
23058 }
23059 else
23060 return lots_of_dashes;
23061 }
23062
23063 case 'b':
23064 obj = BVAR (b, name);
23065 break;
23066
23067 case 'c':
23068 /* %c and %l are ignored in `frame-title-format'.
23069 (In redisplay_internal, the frame title is drawn _before_ the
23070 windows are updated, so the stuff which depends on actual
23071 window contents (such as %l) may fail to render properly, or
23072 even crash emacs.) */
23073 if (mode_line_target == MODE_LINE_TITLE)
23074 return "";
23075 else
23076 {
23077 ptrdiff_t col = current_column ();
23078 w->column_number_displayed = col;
23079 pint2str (decode_mode_spec_buf, width, col);
23080 return decode_mode_spec_buf;
23081 }
23082
23083 case 'e':
23084 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23085 {
23086 if (NILP (Vmemory_full))
23087 return "";
23088 else
23089 return "!MEM FULL! ";
23090 }
23091 #else
23092 return "";
23093 #endif
23094
23095 case 'F':
23096 /* %F displays the frame name. */
23097 if (!NILP (f->title))
23098 return SSDATA (f->title);
23099 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23100 return SSDATA (f->name);
23101 return "Emacs";
23102
23103 case 'f':
23104 obj = BVAR (b, filename);
23105 break;
23106
23107 case 'i':
23108 {
23109 ptrdiff_t size = ZV - BEGV;
23110 pint2str (decode_mode_spec_buf, width, size);
23111 return decode_mode_spec_buf;
23112 }
23113
23114 case 'I':
23115 {
23116 ptrdiff_t size = ZV - BEGV;
23117 pint2hrstr (decode_mode_spec_buf, width, size);
23118 return decode_mode_spec_buf;
23119 }
23120
23121 case 'l':
23122 {
23123 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23124 ptrdiff_t topline, nlines, height;
23125 ptrdiff_t junk;
23126
23127 /* %c and %l are ignored in `frame-title-format'. */
23128 if (mode_line_target == MODE_LINE_TITLE)
23129 return "";
23130
23131 startpos = marker_position (w->start);
23132 startpos_byte = marker_byte_position (w->start);
23133 height = WINDOW_TOTAL_LINES (w);
23134
23135 /* If we decided that this buffer isn't suitable for line numbers,
23136 don't forget that too fast. */
23137 if (w->base_line_pos == -1)
23138 goto no_value;
23139
23140 /* If the buffer is very big, don't waste time. */
23141 if (INTEGERP (Vline_number_display_limit)
23142 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23143 {
23144 w->base_line_pos = 0;
23145 w->base_line_number = 0;
23146 goto no_value;
23147 }
23148
23149 if (w->base_line_number > 0
23150 && w->base_line_pos > 0
23151 && w->base_line_pos <= startpos)
23152 {
23153 line = w->base_line_number;
23154 linepos = w->base_line_pos;
23155 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23156 }
23157 else
23158 {
23159 line = 1;
23160 linepos = BUF_BEGV (b);
23161 linepos_byte = BUF_BEGV_BYTE (b);
23162 }
23163
23164 /* Count lines from base line to window start position. */
23165 nlines = display_count_lines (linepos_byte,
23166 startpos_byte,
23167 startpos, &junk);
23168
23169 topline = nlines + line;
23170
23171 /* Determine a new base line, if the old one is too close
23172 or too far away, or if we did not have one.
23173 "Too close" means it's plausible a scroll-down would
23174 go back past it. */
23175 if (startpos == BUF_BEGV (b))
23176 {
23177 w->base_line_number = topline;
23178 w->base_line_pos = BUF_BEGV (b);
23179 }
23180 else if (nlines < height + 25 || nlines > height * 3 + 50
23181 || linepos == BUF_BEGV (b))
23182 {
23183 ptrdiff_t limit = BUF_BEGV (b);
23184 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23185 ptrdiff_t position;
23186 ptrdiff_t distance =
23187 (height * 2 + 30) * line_number_display_limit_width;
23188
23189 if (startpos - distance > limit)
23190 {
23191 limit = startpos - distance;
23192 limit_byte = CHAR_TO_BYTE (limit);
23193 }
23194
23195 nlines = display_count_lines (startpos_byte,
23196 limit_byte,
23197 - (height * 2 + 30),
23198 &position);
23199 /* If we couldn't find the lines we wanted within
23200 line_number_display_limit_width chars per line,
23201 give up on line numbers for this window. */
23202 if (position == limit_byte && limit == startpos - distance)
23203 {
23204 w->base_line_pos = -1;
23205 w->base_line_number = 0;
23206 goto no_value;
23207 }
23208
23209 w->base_line_number = topline - nlines;
23210 w->base_line_pos = BYTE_TO_CHAR (position);
23211 }
23212
23213 /* Now count lines from the start pos to point. */
23214 nlines = display_count_lines (startpos_byte,
23215 PT_BYTE, PT, &junk);
23216
23217 /* Record that we did display the line number. */
23218 line_number_displayed = 1;
23219
23220 /* Make the string to show. */
23221 pint2str (decode_mode_spec_buf, width, topline + nlines);
23222 return decode_mode_spec_buf;
23223 no_value:
23224 {
23225 char *p = decode_mode_spec_buf;
23226 int pad = width - 2;
23227 while (pad-- > 0)
23228 *p++ = ' ';
23229 *p++ = '?';
23230 *p++ = '?';
23231 *p = '\0';
23232 return decode_mode_spec_buf;
23233 }
23234 }
23235 break;
23236
23237 case 'm':
23238 obj = BVAR (b, mode_name);
23239 break;
23240
23241 case 'n':
23242 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23243 return " Narrow";
23244 break;
23245
23246 case 'p':
23247 {
23248 ptrdiff_t pos = marker_position (w->start);
23249 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23250
23251 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23252 {
23253 if (pos <= BUF_BEGV (b))
23254 return "All";
23255 else
23256 return "Bottom";
23257 }
23258 else if (pos <= BUF_BEGV (b))
23259 return "Top";
23260 else
23261 {
23262 if (total > 1000000)
23263 /* Do it differently for a large value, to avoid overflow. */
23264 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23265 else
23266 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23267 /* We can't normally display a 3-digit number,
23268 so get us a 2-digit number that is close. */
23269 if (total == 100)
23270 total = 99;
23271 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23272 return decode_mode_spec_buf;
23273 }
23274 }
23275
23276 /* Display percentage of size above the bottom of the screen. */
23277 case 'P':
23278 {
23279 ptrdiff_t toppos = marker_position (w->start);
23280 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23281 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23282
23283 if (botpos >= BUF_ZV (b))
23284 {
23285 if (toppos <= BUF_BEGV (b))
23286 return "All";
23287 else
23288 return "Bottom";
23289 }
23290 else
23291 {
23292 if (total > 1000000)
23293 /* Do it differently for a large value, to avoid overflow. */
23294 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23295 else
23296 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23297 /* We can't normally display a 3-digit number,
23298 so get us a 2-digit number that is close. */
23299 if (total == 100)
23300 total = 99;
23301 if (toppos <= BUF_BEGV (b))
23302 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23303 else
23304 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23305 return decode_mode_spec_buf;
23306 }
23307 }
23308
23309 case 's':
23310 /* status of process */
23311 obj = Fget_buffer_process (Fcurrent_buffer ());
23312 if (NILP (obj))
23313 return "no process";
23314 #ifndef MSDOS
23315 obj = Fsymbol_name (Fprocess_status (obj));
23316 #endif
23317 break;
23318
23319 case '@':
23320 {
23321 ptrdiff_t count = inhibit_garbage_collection ();
23322 Lisp_Object curdir = BVAR (current_buffer, directory);
23323 Lisp_Object val = Qnil;
23324
23325 if (STRINGP (curdir))
23326 val = call1 (intern ("file-remote-p"), curdir);
23327
23328 unbind_to (count, Qnil);
23329
23330 if (NILP (val))
23331 return "-";
23332 else
23333 return "@";
23334 }
23335
23336 case 'z':
23337 /* coding-system (not including end-of-line format) */
23338 case 'Z':
23339 /* coding-system (including end-of-line type) */
23340 {
23341 int eol_flag = (c == 'Z');
23342 char *p = decode_mode_spec_buf;
23343
23344 if (! FRAME_WINDOW_P (f))
23345 {
23346 /* No need to mention EOL here--the terminal never needs
23347 to do EOL conversion. */
23348 p = decode_mode_spec_coding (CODING_ID_NAME
23349 (FRAME_KEYBOARD_CODING (f)->id),
23350 p, 0);
23351 p = decode_mode_spec_coding (CODING_ID_NAME
23352 (FRAME_TERMINAL_CODING (f)->id),
23353 p, 0);
23354 }
23355 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23356 p, eol_flag);
23357
23358 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23359 #ifdef subprocesses
23360 obj = Fget_buffer_process (Fcurrent_buffer ());
23361 if (PROCESSP (obj))
23362 {
23363 p = decode_mode_spec_coding
23364 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23365 p = decode_mode_spec_coding
23366 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23367 }
23368 #endif /* subprocesses */
23369 #endif /* 0 */
23370 *p = 0;
23371 return decode_mode_spec_buf;
23372 }
23373 }
23374
23375 if (STRINGP (obj))
23376 {
23377 *string = obj;
23378 return SSDATA (obj);
23379 }
23380 else
23381 return "";
23382 }
23383
23384
23385 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23386 means count lines back from START_BYTE. But don't go beyond
23387 LIMIT_BYTE. Return the number of lines thus found (always
23388 nonnegative).
23389
23390 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23391 either the position COUNT lines after/before START_BYTE, if we
23392 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23393 COUNT lines. */
23394
23395 static ptrdiff_t
23396 display_count_lines (ptrdiff_t start_byte,
23397 ptrdiff_t limit_byte, ptrdiff_t count,
23398 ptrdiff_t *byte_pos_ptr)
23399 {
23400 register unsigned char *cursor;
23401 unsigned char *base;
23402
23403 register ptrdiff_t ceiling;
23404 register unsigned char *ceiling_addr;
23405 ptrdiff_t orig_count = count;
23406
23407 /* If we are not in selective display mode,
23408 check only for newlines. */
23409 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23410 && !INTEGERP (BVAR (current_buffer, selective_display)));
23411
23412 if (count > 0)
23413 {
23414 while (start_byte < limit_byte)
23415 {
23416 ceiling = BUFFER_CEILING_OF (start_byte);
23417 ceiling = min (limit_byte - 1, ceiling);
23418 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23419 base = (cursor = BYTE_POS_ADDR (start_byte));
23420
23421 do
23422 {
23423 if (selective_display)
23424 {
23425 while (*cursor != '\n' && *cursor != 015
23426 && ++cursor != ceiling_addr)
23427 continue;
23428 if (cursor == ceiling_addr)
23429 break;
23430 }
23431 else
23432 {
23433 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23434 if (! cursor)
23435 break;
23436 }
23437
23438 cursor++;
23439
23440 if (--count == 0)
23441 {
23442 start_byte += cursor - base;
23443 *byte_pos_ptr = start_byte;
23444 return orig_count;
23445 }
23446 }
23447 while (cursor < ceiling_addr);
23448
23449 start_byte += ceiling_addr - base;
23450 }
23451 }
23452 else
23453 {
23454 while (start_byte > limit_byte)
23455 {
23456 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23457 ceiling = max (limit_byte, ceiling);
23458 ceiling_addr = BYTE_POS_ADDR (ceiling);
23459 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23460 while (1)
23461 {
23462 if (selective_display)
23463 {
23464 while (--cursor >= ceiling_addr
23465 && *cursor != '\n' && *cursor != 015)
23466 continue;
23467 if (cursor < ceiling_addr)
23468 break;
23469 }
23470 else
23471 {
23472 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23473 if (! cursor)
23474 break;
23475 }
23476
23477 if (++count == 0)
23478 {
23479 start_byte += cursor - base + 1;
23480 *byte_pos_ptr = start_byte;
23481 /* When scanning backwards, we should
23482 not count the newline posterior to which we stop. */
23483 return - orig_count - 1;
23484 }
23485 }
23486 start_byte += ceiling_addr - base;
23487 }
23488 }
23489
23490 *byte_pos_ptr = limit_byte;
23491
23492 if (count < 0)
23493 return - orig_count + count;
23494 return orig_count - count;
23495
23496 }
23497
23498
23499 \f
23500 /***********************************************************************
23501 Displaying strings
23502 ***********************************************************************/
23503
23504 /* Display a NUL-terminated string, starting with index START.
23505
23506 If STRING is non-null, display that C string. Otherwise, the Lisp
23507 string LISP_STRING is displayed. There's a case that STRING is
23508 non-null and LISP_STRING is not nil. It means STRING is a string
23509 data of LISP_STRING. In that case, we display LISP_STRING while
23510 ignoring its text properties.
23511
23512 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23513 FACE_STRING. Display STRING or LISP_STRING with the face at
23514 FACE_STRING_POS in FACE_STRING:
23515
23516 Display the string in the environment given by IT, but use the
23517 standard display table, temporarily.
23518
23519 FIELD_WIDTH is the minimum number of output glyphs to produce.
23520 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23521 with spaces. If STRING has more characters, more than FIELD_WIDTH
23522 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23523
23524 PRECISION is the maximum number of characters to output from
23525 STRING. PRECISION < 0 means don't truncate the string.
23526
23527 This is roughly equivalent to printf format specifiers:
23528
23529 FIELD_WIDTH PRECISION PRINTF
23530 ----------------------------------------
23531 -1 -1 %s
23532 -1 10 %.10s
23533 10 -1 %10s
23534 20 10 %20.10s
23535
23536 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23537 display them, and < 0 means obey the current buffer's value of
23538 enable_multibyte_characters.
23539
23540 Value is the number of columns displayed. */
23541
23542 static int
23543 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23544 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23545 int field_width, int precision, int max_x, int multibyte)
23546 {
23547 int hpos_at_start = it->hpos;
23548 int saved_face_id = it->face_id;
23549 struct glyph_row *row = it->glyph_row;
23550 ptrdiff_t it_charpos;
23551
23552 /* Initialize the iterator IT for iteration over STRING beginning
23553 with index START. */
23554 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23555 precision, field_width, multibyte);
23556 if (string && STRINGP (lisp_string))
23557 /* LISP_STRING is the one returned by decode_mode_spec. We should
23558 ignore its text properties. */
23559 it->stop_charpos = it->end_charpos;
23560
23561 /* If displaying STRING, set up the face of the iterator from
23562 FACE_STRING, if that's given. */
23563 if (STRINGP (face_string))
23564 {
23565 ptrdiff_t endptr;
23566 struct face *face;
23567
23568 it->face_id
23569 = face_at_string_position (it->w, face_string, face_string_pos,
23570 0, &endptr, it->base_face_id, 0);
23571 face = FACE_FROM_ID (it->f, it->face_id);
23572 it->face_box_p = face->box != FACE_NO_BOX;
23573 }
23574
23575 /* Set max_x to the maximum allowed X position. Don't let it go
23576 beyond the right edge of the window. */
23577 if (max_x <= 0)
23578 max_x = it->last_visible_x;
23579 else
23580 max_x = min (max_x, it->last_visible_x);
23581
23582 /* Skip over display elements that are not visible. because IT->w is
23583 hscrolled. */
23584 if (it->current_x < it->first_visible_x)
23585 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23586 MOVE_TO_POS | MOVE_TO_X);
23587
23588 row->ascent = it->max_ascent;
23589 row->height = it->max_ascent + it->max_descent;
23590 row->phys_ascent = it->max_phys_ascent;
23591 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23592 row->extra_line_spacing = it->max_extra_line_spacing;
23593
23594 if (STRINGP (it->string))
23595 it_charpos = IT_STRING_CHARPOS (*it);
23596 else
23597 it_charpos = IT_CHARPOS (*it);
23598
23599 /* This condition is for the case that we are called with current_x
23600 past last_visible_x. */
23601 while (it->current_x < max_x)
23602 {
23603 int x_before, x, n_glyphs_before, i, nglyphs;
23604
23605 /* Get the next display element. */
23606 if (!get_next_display_element (it))
23607 break;
23608
23609 /* Produce glyphs. */
23610 x_before = it->current_x;
23611 n_glyphs_before = row->used[TEXT_AREA];
23612 PRODUCE_GLYPHS (it);
23613
23614 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23615 i = 0;
23616 x = x_before;
23617 while (i < nglyphs)
23618 {
23619 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23620
23621 if (it->line_wrap != TRUNCATE
23622 && x + glyph->pixel_width > max_x)
23623 {
23624 /* End of continued line or max_x reached. */
23625 if (CHAR_GLYPH_PADDING_P (*glyph))
23626 {
23627 /* A wide character is unbreakable. */
23628 if (row->reversed_p)
23629 unproduce_glyphs (it, row->used[TEXT_AREA]
23630 - n_glyphs_before);
23631 row->used[TEXT_AREA] = n_glyphs_before;
23632 it->current_x = x_before;
23633 }
23634 else
23635 {
23636 if (row->reversed_p)
23637 unproduce_glyphs (it, row->used[TEXT_AREA]
23638 - (n_glyphs_before + i));
23639 row->used[TEXT_AREA] = n_glyphs_before + i;
23640 it->current_x = x;
23641 }
23642 break;
23643 }
23644 else if (x + glyph->pixel_width >= it->first_visible_x)
23645 {
23646 /* Glyph is at least partially visible. */
23647 ++it->hpos;
23648 if (x < it->first_visible_x)
23649 row->x = x - it->first_visible_x;
23650 }
23651 else
23652 {
23653 /* Glyph is off the left margin of the display area.
23654 Should not happen. */
23655 emacs_abort ();
23656 }
23657
23658 row->ascent = max (row->ascent, it->max_ascent);
23659 row->height = max (row->height, it->max_ascent + it->max_descent);
23660 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23661 row->phys_height = max (row->phys_height,
23662 it->max_phys_ascent + it->max_phys_descent);
23663 row->extra_line_spacing = max (row->extra_line_spacing,
23664 it->max_extra_line_spacing);
23665 x += glyph->pixel_width;
23666 ++i;
23667 }
23668
23669 /* Stop if max_x reached. */
23670 if (i < nglyphs)
23671 break;
23672
23673 /* Stop at line ends. */
23674 if (ITERATOR_AT_END_OF_LINE_P (it))
23675 {
23676 it->continuation_lines_width = 0;
23677 break;
23678 }
23679
23680 set_iterator_to_next (it, 1);
23681 if (STRINGP (it->string))
23682 it_charpos = IT_STRING_CHARPOS (*it);
23683 else
23684 it_charpos = IT_CHARPOS (*it);
23685
23686 /* Stop if truncating at the right edge. */
23687 if (it->line_wrap == TRUNCATE
23688 && it->current_x >= it->last_visible_x)
23689 {
23690 /* Add truncation mark, but don't do it if the line is
23691 truncated at a padding space. */
23692 if (it_charpos < it->string_nchars)
23693 {
23694 if (!FRAME_WINDOW_P (it->f))
23695 {
23696 int ii, n;
23697
23698 if (it->current_x > it->last_visible_x)
23699 {
23700 if (!row->reversed_p)
23701 {
23702 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23703 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23704 break;
23705 }
23706 else
23707 {
23708 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23709 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23710 break;
23711 unproduce_glyphs (it, ii + 1);
23712 ii = row->used[TEXT_AREA] - (ii + 1);
23713 }
23714 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23715 {
23716 row->used[TEXT_AREA] = ii;
23717 produce_special_glyphs (it, IT_TRUNCATION);
23718 }
23719 }
23720 produce_special_glyphs (it, IT_TRUNCATION);
23721 }
23722 row->truncated_on_right_p = 1;
23723 }
23724 break;
23725 }
23726 }
23727
23728 /* Maybe insert a truncation at the left. */
23729 if (it->first_visible_x
23730 && it_charpos > 0)
23731 {
23732 if (!FRAME_WINDOW_P (it->f)
23733 || (row->reversed_p
23734 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23735 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23736 insert_left_trunc_glyphs (it);
23737 row->truncated_on_left_p = 1;
23738 }
23739
23740 it->face_id = saved_face_id;
23741
23742 /* Value is number of columns displayed. */
23743 return it->hpos - hpos_at_start;
23744 }
23745
23746
23747 \f
23748 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23749 appears as an element of LIST or as the car of an element of LIST.
23750 If PROPVAL is a list, compare each element against LIST in that
23751 way, and return 1/2 if any element of PROPVAL is found in LIST.
23752 Otherwise return 0. This function cannot quit.
23753 The return value is 2 if the text is invisible but with an ellipsis
23754 and 1 if it's invisible and without an ellipsis. */
23755
23756 int
23757 invisible_p (register Lisp_Object propval, Lisp_Object list)
23758 {
23759 register Lisp_Object tail, proptail;
23760
23761 for (tail = list; CONSP (tail); tail = XCDR (tail))
23762 {
23763 register Lisp_Object tem;
23764 tem = XCAR (tail);
23765 if (EQ (propval, tem))
23766 return 1;
23767 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23768 return NILP (XCDR (tem)) ? 1 : 2;
23769 }
23770
23771 if (CONSP (propval))
23772 {
23773 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23774 {
23775 Lisp_Object propelt;
23776 propelt = XCAR (proptail);
23777 for (tail = list; CONSP (tail); tail = XCDR (tail))
23778 {
23779 register Lisp_Object tem;
23780 tem = XCAR (tail);
23781 if (EQ (propelt, tem))
23782 return 1;
23783 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23784 return NILP (XCDR (tem)) ? 1 : 2;
23785 }
23786 }
23787 }
23788
23789 return 0;
23790 }
23791
23792 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23793 doc: /* Non-nil if the property makes the text invisible.
23794 POS-OR-PROP can be a marker or number, in which case it is taken to be
23795 a position in the current buffer and the value of the `invisible' property
23796 is checked; or it can be some other value, which is then presumed to be the
23797 value of the `invisible' property of the text of interest.
23798 The non-nil value returned can be t for truly invisible text or something
23799 else if the text is replaced by an ellipsis. */)
23800 (Lisp_Object pos_or_prop)
23801 {
23802 Lisp_Object prop
23803 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23804 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23805 : pos_or_prop);
23806 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23807 return (invis == 0 ? Qnil
23808 : invis == 1 ? Qt
23809 : make_number (invis));
23810 }
23811
23812 /* Calculate a width or height in pixels from a specification using
23813 the following elements:
23814
23815 SPEC ::=
23816 NUM - a (fractional) multiple of the default font width/height
23817 (NUM) - specifies exactly NUM pixels
23818 UNIT - a fixed number of pixels, see below.
23819 ELEMENT - size of a display element in pixels, see below.
23820 (NUM . SPEC) - equals NUM * SPEC
23821 (+ SPEC SPEC ...) - add pixel values
23822 (- SPEC SPEC ...) - subtract pixel values
23823 (- SPEC) - negate pixel value
23824
23825 NUM ::=
23826 INT or FLOAT - a number constant
23827 SYMBOL - use symbol's (buffer local) variable binding.
23828
23829 UNIT ::=
23830 in - pixels per inch *)
23831 mm - pixels per 1/1000 meter *)
23832 cm - pixels per 1/100 meter *)
23833 width - width of current font in pixels.
23834 height - height of current font in pixels.
23835
23836 *) using the ratio(s) defined in display-pixels-per-inch.
23837
23838 ELEMENT ::=
23839
23840 left-fringe - left fringe width in pixels
23841 right-fringe - right fringe width in pixels
23842
23843 left-margin - left margin width in pixels
23844 right-margin - right margin width in pixels
23845
23846 scroll-bar - scroll-bar area width in pixels
23847
23848 Examples:
23849
23850 Pixels corresponding to 5 inches:
23851 (5 . in)
23852
23853 Total width of non-text areas on left side of window (if scroll-bar is on left):
23854 '(space :width (+ left-fringe left-margin scroll-bar))
23855
23856 Align to first text column (in header line):
23857 '(space :align-to 0)
23858
23859 Align to middle of text area minus half the width of variable `my-image'
23860 containing a loaded image:
23861 '(space :align-to (0.5 . (- text my-image)))
23862
23863 Width of left margin minus width of 1 character in the default font:
23864 '(space :width (- left-margin 1))
23865
23866 Width of left margin minus width of 2 characters in the current font:
23867 '(space :width (- left-margin (2 . width)))
23868
23869 Center 1 character over left-margin (in header line):
23870 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23871
23872 Different ways to express width of left fringe plus left margin minus one pixel:
23873 '(space :width (- (+ left-fringe left-margin) (1)))
23874 '(space :width (+ left-fringe left-margin (- (1))))
23875 '(space :width (+ left-fringe left-margin (-1)))
23876
23877 */
23878
23879 static int
23880 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23881 struct font *font, int width_p, int *align_to)
23882 {
23883 double pixels;
23884
23885 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23886 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23887
23888 if (NILP (prop))
23889 return OK_PIXELS (0);
23890
23891 eassert (FRAME_LIVE_P (it->f));
23892
23893 if (SYMBOLP (prop))
23894 {
23895 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23896 {
23897 char *unit = SSDATA (SYMBOL_NAME (prop));
23898
23899 if (unit[0] == 'i' && unit[1] == 'n')
23900 pixels = 1.0;
23901 else if (unit[0] == 'm' && unit[1] == 'm')
23902 pixels = 25.4;
23903 else if (unit[0] == 'c' && unit[1] == 'm')
23904 pixels = 2.54;
23905 else
23906 pixels = 0;
23907 if (pixels > 0)
23908 {
23909 double ppi = (width_p ? FRAME_RES_X (it->f)
23910 : FRAME_RES_Y (it->f));
23911
23912 if (ppi > 0)
23913 return OK_PIXELS (ppi / pixels);
23914 return 0;
23915 }
23916 }
23917
23918 #ifdef HAVE_WINDOW_SYSTEM
23919 if (EQ (prop, Qheight))
23920 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23921 if (EQ (prop, Qwidth))
23922 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23923 #else
23924 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23925 return OK_PIXELS (1);
23926 #endif
23927
23928 if (EQ (prop, Qtext))
23929 return OK_PIXELS (width_p
23930 ? window_box_width (it->w, TEXT_AREA)
23931 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23932
23933 if (align_to && *align_to < 0)
23934 {
23935 *res = 0;
23936 if (EQ (prop, Qleft))
23937 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23938 if (EQ (prop, Qright))
23939 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23940 if (EQ (prop, Qcenter))
23941 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23942 + window_box_width (it->w, TEXT_AREA) / 2);
23943 if (EQ (prop, Qleft_fringe))
23944 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23945 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23946 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23947 if (EQ (prop, Qright_fringe))
23948 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23949 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23950 : window_box_right_offset (it->w, TEXT_AREA));
23951 if (EQ (prop, Qleft_margin))
23952 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23953 if (EQ (prop, Qright_margin))
23954 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23955 if (EQ (prop, Qscroll_bar))
23956 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23957 ? 0
23958 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23959 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23960 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23961 : 0)));
23962 }
23963 else
23964 {
23965 if (EQ (prop, Qleft_fringe))
23966 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23967 if (EQ (prop, Qright_fringe))
23968 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23969 if (EQ (prop, Qleft_margin))
23970 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23971 if (EQ (prop, Qright_margin))
23972 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23973 if (EQ (prop, Qscroll_bar))
23974 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23975 }
23976
23977 prop = buffer_local_value (prop, it->w->contents);
23978 if (EQ (prop, Qunbound))
23979 prop = Qnil;
23980 }
23981
23982 if (INTEGERP (prop) || FLOATP (prop))
23983 {
23984 int base_unit = (width_p
23985 ? FRAME_COLUMN_WIDTH (it->f)
23986 : FRAME_LINE_HEIGHT (it->f));
23987 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23988 }
23989
23990 if (CONSP (prop))
23991 {
23992 Lisp_Object car = XCAR (prop);
23993 Lisp_Object cdr = XCDR (prop);
23994
23995 if (SYMBOLP (car))
23996 {
23997 #ifdef HAVE_WINDOW_SYSTEM
23998 if (FRAME_WINDOW_P (it->f)
23999 && valid_image_p (prop))
24000 {
24001 ptrdiff_t id = lookup_image (it->f, prop);
24002 struct image *img = IMAGE_FROM_ID (it->f, id);
24003
24004 return OK_PIXELS (width_p ? img->width : img->height);
24005 }
24006 #endif
24007 if (EQ (car, Qplus) || EQ (car, Qminus))
24008 {
24009 int first = 1;
24010 double px;
24011
24012 pixels = 0;
24013 while (CONSP (cdr))
24014 {
24015 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24016 font, width_p, align_to))
24017 return 0;
24018 if (first)
24019 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24020 else
24021 pixels += px;
24022 cdr = XCDR (cdr);
24023 }
24024 if (EQ (car, Qminus))
24025 pixels = -pixels;
24026 return OK_PIXELS (pixels);
24027 }
24028
24029 car = buffer_local_value (car, it->w->contents);
24030 if (EQ (car, Qunbound))
24031 car = Qnil;
24032 }
24033
24034 if (INTEGERP (car) || FLOATP (car))
24035 {
24036 double fact;
24037 pixels = XFLOATINT (car);
24038 if (NILP (cdr))
24039 return OK_PIXELS (pixels);
24040 if (calc_pixel_width_or_height (&fact, it, cdr,
24041 font, width_p, align_to))
24042 return OK_PIXELS (pixels * fact);
24043 return 0;
24044 }
24045
24046 return 0;
24047 }
24048
24049 return 0;
24050 }
24051
24052 \f
24053 /***********************************************************************
24054 Glyph Display
24055 ***********************************************************************/
24056
24057 #ifdef HAVE_WINDOW_SYSTEM
24058
24059 #ifdef GLYPH_DEBUG
24060
24061 void
24062 dump_glyph_string (struct glyph_string *s)
24063 {
24064 fprintf (stderr, "glyph string\n");
24065 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24066 s->x, s->y, s->width, s->height);
24067 fprintf (stderr, " ybase = %d\n", s->ybase);
24068 fprintf (stderr, " hl = %d\n", s->hl);
24069 fprintf (stderr, " left overhang = %d, right = %d\n",
24070 s->left_overhang, s->right_overhang);
24071 fprintf (stderr, " nchars = %d\n", s->nchars);
24072 fprintf (stderr, " extends to end of line = %d\n",
24073 s->extends_to_end_of_line_p);
24074 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24075 fprintf (stderr, " bg width = %d\n", s->background_width);
24076 }
24077
24078 #endif /* GLYPH_DEBUG */
24079
24080 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24081 of XChar2b structures for S; it can't be allocated in
24082 init_glyph_string because it must be allocated via `alloca'. W
24083 is the window on which S is drawn. ROW and AREA are the glyph row
24084 and area within the row from which S is constructed. START is the
24085 index of the first glyph structure covered by S. HL is a
24086 face-override for drawing S. */
24087
24088 #ifdef HAVE_NTGUI
24089 #define OPTIONAL_HDC(hdc) HDC hdc,
24090 #define DECLARE_HDC(hdc) HDC hdc;
24091 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24092 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24093 #endif
24094
24095 #ifndef OPTIONAL_HDC
24096 #define OPTIONAL_HDC(hdc)
24097 #define DECLARE_HDC(hdc)
24098 #define ALLOCATE_HDC(hdc, f)
24099 #define RELEASE_HDC(hdc, f)
24100 #endif
24101
24102 static void
24103 init_glyph_string (struct glyph_string *s,
24104 OPTIONAL_HDC (hdc)
24105 XChar2b *char2b, struct window *w, struct glyph_row *row,
24106 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24107 {
24108 memset (s, 0, sizeof *s);
24109 s->w = w;
24110 s->f = XFRAME (w->frame);
24111 #ifdef HAVE_NTGUI
24112 s->hdc = hdc;
24113 #endif
24114 s->display = FRAME_X_DISPLAY (s->f);
24115 s->window = FRAME_X_WINDOW (s->f);
24116 s->char2b = char2b;
24117 s->hl = hl;
24118 s->row = row;
24119 s->area = area;
24120 s->first_glyph = row->glyphs[area] + start;
24121 s->height = row->height;
24122 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24123 s->ybase = s->y + row->ascent;
24124 }
24125
24126
24127 /* Append the list of glyph strings with head H and tail T to the list
24128 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24129
24130 static void
24131 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24132 struct glyph_string *h, struct glyph_string *t)
24133 {
24134 if (h)
24135 {
24136 if (*head)
24137 (*tail)->next = h;
24138 else
24139 *head = h;
24140 h->prev = *tail;
24141 *tail = t;
24142 }
24143 }
24144
24145
24146 /* Prepend the list of glyph strings with head H and tail T to the
24147 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24148 result. */
24149
24150 static void
24151 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24152 struct glyph_string *h, struct glyph_string *t)
24153 {
24154 if (h)
24155 {
24156 if (*head)
24157 (*head)->prev = t;
24158 else
24159 *tail = t;
24160 t->next = *head;
24161 *head = h;
24162 }
24163 }
24164
24165
24166 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24167 Set *HEAD and *TAIL to the resulting list. */
24168
24169 static void
24170 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24171 struct glyph_string *s)
24172 {
24173 s->next = s->prev = NULL;
24174 append_glyph_string_lists (head, tail, s, s);
24175 }
24176
24177
24178 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24179 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24180 make sure that X resources for the face returned are allocated.
24181 Value is a pointer to a realized face that is ready for display if
24182 DISPLAY_P is non-zero. */
24183
24184 static struct face *
24185 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24186 XChar2b *char2b, int display_p)
24187 {
24188 struct face *face = FACE_FROM_ID (f, face_id);
24189 unsigned code = 0;
24190
24191 if (face->font)
24192 {
24193 code = face->font->driver->encode_char (face->font, c);
24194
24195 if (code == FONT_INVALID_CODE)
24196 code = 0;
24197 }
24198 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24199
24200 /* Make sure X resources of the face are allocated. */
24201 #ifdef HAVE_X_WINDOWS
24202 if (display_p)
24203 #endif
24204 {
24205 eassert (face != NULL);
24206 prepare_face_for_display (f, face);
24207 }
24208
24209 return face;
24210 }
24211
24212
24213 /* Get face and two-byte form of character glyph GLYPH on frame F.
24214 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24215 a pointer to a realized face that is ready for display. */
24216
24217 static struct face *
24218 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24219 XChar2b *char2b, int *two_byte_p)
24220 {
24221 struct face *face;
24222 unsigned code = 0;
24223
24224 eassert (glyph->type == CHAR_GLYPH);
24225 face = FACE_FROM_ID (f, glyph->face_id);
24226
24227 /* Make sure X resources of the face are allocated. */
24228 eassert (face != NULL);
24229 prepare_face_for_display (f, face);
24230
24231 if (two_byte_p)
24232 *two_byte_p = 0;
24233
24234 if (face->font)
24235 {
24236 if (CHAR_BYTE8_P (glyph->u.ch))
24237 code = CHAR_TO_BYTE8 (glyph->u.ch);
24238 else
24239 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24240
24241 if (code == FONT_INVALID_CODE)
24242 code = 0;
24243 }
24244
24245 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24246 return face;
24247 }
24248
24249
24250 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24251 Return 1 if FONT has a glyph for C, otherwise return 0. */
24252
24253 static int
24254 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24255 {
24256 unsigned code;
24257
24258 if (CHAR_BYTE8_P (c))
24259 code = CHAR_TO_BYTE8 (c);
24260 else
24261 code = font->driver->encode_char (font, c);
24262
24263 if (code == FONT_INVALID_CODE)
24264 return 0;
24265 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24266 return 1;
24267 }
24268
24269
24270 /* Fill glyph string S with composition components specified by S->cmp.
24271
24272 BASE_FACE is the base face of the composition.
24273 S->cmp_from is the index of the first component for S.
24274
24275 OVERLAPS non-zero means S should draw the foreground only, and use
24276 its physical height for clipping. See also draw_glyphs.
24277
24278 Value is the index of a component not in S. */
24279
24280 static int
24281 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24282 int overlaps)
24283 {
24284 int i;
24285 /* For all glyphs of this composition, starting at the offset
24286 S->cmp_from, until we reach the end of the definition or encounter a
24287 glyph that requires the different face, add it to S. */
24288 struct face *face;
24289
24290 eassert (s);
24291
24292 s->for_overlaps = overlaps;
24293 s->face = NULL;
24294 s->font = NULL;
24295 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24296 {
24297 int c = COMPOSITION_GLYPH (s->cmp, i);
24298
24299 /* TAB in a composition means display glyphs with padding space
24300 on the left or right. */
24301 if (c != '\t')
24302 {
24303 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24304 -1, Qnil);
24305
24306 face = get_char_face_and_encoding (s->f, c, face_id,
24307 s->char2b + i, 1);
24308 if (face)
24309 {
24310 if (! s->face)
24311 {
24312 s->face = face;
24313 s->font = s->face->font;
24314 }
24315 else if (s->face != face)
24316 break;
24317 }
24318 }
24319 ++s->nchars;
24320 }
24321 s->cmp_to = i;
24322
24323 if (s->face == NULL)
24324 {
24325 s->face = base_face->ascii_face;
24326 s->font = s->face->font;
24327 }
24328
24329 /* All glyph strings for the same composition has the same width,
24330 i.e. the width set for the first component of the composition. */
24331 s->width = s->first_glyph->pixel_width;
24332
24333 /* If the specified font could not be loaded, use the frame's
24334 default font, but record the fact that we couldn't load it in
24335 the glyph string so that we can draw rectangles for the
24336 characters of the glyph string. */
24337 if (s->font == NULL)
24338 {
24339 s->font_not_found_p = 1;
24340 s->font = FRAME_FONT (s->f);
24341 }
24342
24343 /* Adjust base line for subscript/superscript text. */
24344 s->ybase += s->first_glyph->voffset;
24345
24346 /* This glyph string must always be drawn with 16-bit functions. */
24347 s->two_byte_p = 1;
24348
24349 return s->cmp_to;
24350 }
24351
24352 static int
24353 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24354 int start, int end, int overlaps)
24355 {
24356 struct glyph *glyph, *last;
24357 Lisp_Object lgstring;
24358 int i;
24359
24360 s->for_overlaps = overlaps;
24361 glyph = s->row->glyphs[s->area] + start;
24362 last = s->row->glyphs[s->area] + end;
24363 s->cmp_id = glyph->u.cmp.id;
24364 s->cmp_from = glyph->slice.cmp.from;
24365 s->cmp_to = glyph->slice.cmp.to + 1;
24366 s->face = FACE_FROM_ID (s->f, face_id);
24367 lgstring = composition_gstring_from_id (s->cmp_id);
24368 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24369 glyph++;
24370 while (glyph < last
24371 && glyph->u.cmp.automatic
24372 && glyph->u.cmp.id == s->cmp_id
24373 && s->cmp_to == glyph->slice.cmp.from)
24374 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24375
24376 for (i = s->cmp_from; i < s->cmp_to; i++)
24377 {
24378 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24379 unsigned code = LGLYPH_CODE (lglyph);
24380
24381 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24382 }
24383 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24384 return glyph - s->row->glyphs[s->area];
24385 }
24386
24387
24388 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24389 See the comment of fill_glyph_string for arguments.
24390 Value is the index of the first glyph not in S. */
24391
24392
24393 static int
24394 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24395 int start, int end, int overlaps)
24396 {
24397 struct glyph *glyph, *last;
24398 int voffset;
24399
24400 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24401 s->for_overlaps = overlaps;
24402 glyph = s->row->glyphs[s->area] + start;
24403 last = s->row->glyphs[s->area] + end;
24404 voffset = glyph->voffset;
24405 s->face = FACE_FROM_ID (s->f, face_id);
24406 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24407 s->nchars = 1;
24408 s->width = glyph->pixel_width;
24409 glyph++;
24410 while (glyph < last
24411 && glyph->type == GLYPHLESS_GLYPH
24412 && glyph->voffset == voffset
24413 && glyph->face_id == face_id)
24414 {
24415 s->nchars++;
24416 s->width += glyph->pixel_width;
24417 glyph++;
24418 }
24419 s->ybase += voffset;
24420 return glyph - s->row->glyphs[s->area];
24421 }
24422
24423
24424 /* Fill glyph string S from a sequence of character glyphs.
24425
24426 FACE_ID is the face id of the string. START is the index of the
24427 first glyph to consider, END is the index of the last + 1.
24428 OVERLAPS non-zero means S should draw the foreground only, and use
24429 its physical height for clipping. See also draw_glyphs.
24430
24431 Value is the index of the first glyph not in S. */
24432
24433 static int
24434 fill_glyph_string (struct glyph_string *s, int face_id,
24435 int start, int end, int overlaps)
24436 {
24437 struct glyph *glyph, *last;
24438 int voffset;
24439 int glyph_not_available_p;
24440
24441 eassert (s->f == XFRAME (s->w->frame));
24442 eassert (s->nchars == 0);
24443 eassert (start >= 0 && end > start);
24444
24445 s->for_overlaps = overlaps;
24446 glyph = s->row->glyphs[s->area] + start;
24447 last = s->row->glyphs[s->area] + end;
24448 voffset = glyph->voffset;
24449 s->padding_p = glyph->padding_p;
24450 glyph_not_available_p = glyph->glyph_not_available_p;
24451
24452 while (glyph < last
24453 && glyph->type == CHAR_GLYPH
24454 && glyph->voffset == voffset
24455 /* Same face id implies same font, nowadays. */
24456 && glyph->face_id == face_id
24457 && glyph->glyph_not_available_p == glyph_not_available_p)
24458 {
24459 int two_byte_p;
24460
24461 s->face = get_glyph_face_and_encoding (s->f, glyph,
24462 s->char2b + s->nchars,
24463 &two_byte_p);
24464 s->two_byte_p = two_byte_p;
24465 ++s->nchars;
24466 eassert (s->nchars <= end - start);
24467 s->width += glyph->pixel_width;
24468 if (glyph++->padding_p != s->padding_p)
24469 break;
24470 }
24471
24472 s->font = s->face->font;
24473
24474 /* If the specified font could not be loaded, use the frame's font,
24475 but record the fact that we couldn't load it in
24476 S->font_not_found_p so that we can draw rectangles for the
24477 characters of the glyph string. */
24478 if (s->font == NULL || glyph_not_available_p)
24479 {
24480 s->font_not_found_p = 1;
24481 s->font = FRAME_FONT (s->f);
24482 }
24483
24484 /* Adjust base line for subscript/superscript text. */
24485 s->ybase += voffset;
24486
24487 eassert (s->face && s->face->gc);
24488 return glyph - s->row->glyphs[s->area];
24489 }
24490
24491
24492 /* Fill glyph string S from image glyph S->first_glyph. */
24493
24494 static void
24495 fill_image_glyph_string (struct glyph_string *s)
24496 {
24497 eassert (s->first_glyph->type == IMAGE_GLYPH);
24498 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24499 eassert (s->img);
24500 s->slice = s->first_glyph->slice.img;
24501 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24502 s->font = s->face->font;
24503 s->width = s->first_glyph->pixel_width;
24504
24505 /* Adjust base line for subscript/superscript text. */
24506 s->ybase += s->first_glyph->voffset;
24507 }
24508
24509
24510 /* Fill glyph string S from a sequence of stretch glyphs.
24511
24512 START is the index of the first glyph to consider,
24513 END is the index of the last + 1.
24514
24515 Value is the index of the first glyph not in S. */
24516
24517 static int
24518 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24519 {
24520 struct glyph *glyph, *last;
24521 int voffset, face_id;
24522
24523 eassert (s->first_glyph->type == STRETCH_GLYPH);
24524
24525 glyph = s->row->glyphs[s->area] + start;
24526 last = s->row->glyphs[s->area] + end;
24527 face_id = glyph->face_id;
24528 s->face = FACE_FROM_ID (s->f, face_id);
24529 s->font = s->face->font;
24530 s->width = glyph->pixel_width;
24531 s->nchars = 1;
24532 voffset = glyph->voffset;
24533
24534 for (++glyph;
24535 (glyph < last
24536 && glyph->type == STRETCH_GLYPH
24537 && glyph->voffset == voffset
24538 && glyph->face_id == face_id);
24539 ++glyph)
24540 s->width += glyph->pixel_width;
24541
24542 /* Adjust base line for subscript/superscript text. */
24543 s->ybase += voffset;
24544
24545 /* The case that face->gc == 0 is handled when drawing the glyph
24546 string by calling prepare_face_for_display. */
24547 eassert (s->face);
24548 return glyph - s->row->glyphs[s->area];
24549 }
24550
24551 static struct font_metrics *
24552 get_per_char_metric (struct font *font, XChar2b *char2b)
24553 {
24554 static struct font_metrics metrics;
24555 unsigned code;
24556
24557 if (! font)
24558 return NULL;
24559 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24560 if (code == FONT_INVALID_CODE)
24561 return NULL;
24562 font->driver->text_extents (font, &code, 1, &metrics);
24563 return &metrics;
24564 }
24565
24566 /* EXPORT for RIF:
24567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24569 assumed to be zero. */
24570
24571 void
24572 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24573 {
24574 *left = *right = 0;
24575
24576 if (glyph->type == CHAR_GLYPH)
24577 {
24578 struct face *face;
24579 XChar2b char2b;
24580 struct font_metrics *pcm;
24581
24582 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24583 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24584 {
24585 if (pcm->rbearing > pcm->width)
24586 *right = pcm->rbearing - pcm->width;
24587 if (pcm->lbearing < 0)
24588 *left = -pcm->lbearing;
24589 }
24590 }
24591 else if (glyph->type == COMPOSITE_GLYPH)
24592 {
24593 if (! glyph->u.cmp.automatic)
24594 {
24595 struct composition *cmp = composition_table[glyph->u.cmp.id];
24596
24597 if (cmp->rbearing > cmp->pixel_width)
24598 *right = cmp->rbearing - cmp->pixel_width;
24599 if (cmp->lbearing < 0)
24600 *left = - cmp->lbearing;
24601 }
24602 else
24603 {
24604 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24605 struct font_metrics metrics;
24606
24607 composition_gstring_width (gstring, glyph->slice.cmp.from,
24608 glyph->slice.cmp.to + 1, &metrics);
24609 if (metrics.rbearing > metrics.width)
24610 *right = metrics.rbearing - metrics.width;
24611 if (metrics.lbearing < 0)
24612 *left = - metrics.lbearing;
24613 }
24614 }
24615 }
24616
24617
24618 /* Return the index of the first glyph preceding glyph string S that
24619 is overwritten by S because of S's left overhang. Value is -1
24620 if no glyphs are overwritten. */
24621
24622 static int
24623 left_overwritten (struct glyph_string *s)
24624 {
24625 int k;
24626
24627 if (s->left_overhang)
24628 {
24629 int x = 0, i;
24630 struct glyph *glyphs = s->row->glyphs[s->area];
24631 int first = s->first_glyph - glyphs;
24632
24633 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24634 x -= glyphs[i].pixel_width;
24635
24636 k = i + 1;
24637 }
24638 else
24639 k = -1;
24640
24641 return k;
24642 }
24643
24644
24645 /* Return the index of the first glyph preceding glyph string S that
24646 is overwriting S because of its right overhang. Value is -1 if no
24647 glyph in front of S overwrites S. */
24648
24649 static int
24650 left_overwriting (struct glyph_string *s)
24651 {
24652 int i, k, x;
24653 struct glyph *glyphs = s->row->glyphs[s->area];
24654 int first = s->first_glyph - glyphs;
24655
24656 k = -1;
24657 x = 0;
24658 for (i = first - 1; i >= 0; --i)
24659 {
24660 int left, right;
24661 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24662 if (x + right > 0)
24663 k = i;
24664 x -= glyphs[i].pixel_width;
24665 }
24666
24667 return k;
24668 }
24669
24670
24671 /* Return the index of the last glyph following glyph string S that is
24672 overwritten by S because of S's right overhang. Value is -1 if
24673 no such glyph is found. */
24674
24675 static int
24676 right_overwritten (struct glyph_string *s)
24677 {
24678 int k = -1;
24679
24680 if (s->right_overhang)
24681 {
24682 int x = 0, i;
24683 struct glyph *glyphs = s->row->glyphs[s->area];
24684 int first = (s->first_glyph - glyphs
24685 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24686 int end = s->row->used[s->area];
24687
24688 for (i = first; i < end && s->right_overhang > x; ++i)
24689 x += glyphs[i].pixel_width;
24690
24691 k = i;
24692 }
24693
24694 return k;
24695 }
24696
24697
24698 /* Return the index of the last glyph following glyph string S that
24699 overwrites S because of its left overhang. Value is negative
24700 if no such glyph is found. */
24701
24702 static int
24703 right_overwriting (struct glyph_string *s)
24704 {
24705 int i, k, x;
24706 int end = s->row->used[s->area];
24707 struct glyph *glyphs = s->row->glyphs[s->area];
24708 int first = (s->first_glyph - glyphs
24709 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24710
24711 k = -1;
24712 x = 0;
24713 for (i = first; i < end; ++i)
24714 {
24715 int left, right;
24716 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24717 if (x - left < 0)
24718 k = i;
24719 x += glyphs[i].pixel_width;
24720 }
24721
24722 return k;
24723 }
24724
24725
24726 /* Set background width of glyph string S. START is the index of the
24727 first glyph following S. LAST_X is the right-most x-position + 1
24728 in the drawing area. */
24729
24730 static void
24731 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24732 {
24733 /* If the face of this glyph string has to be drawn to the end of
24734 the drawing area, set S->extends_to_end_of_line_p. */
24735
24736 if (start == s->row->used[s->area]
24737 && ((s->row->fill_line_p
24738 && (s->hl == DRAW_NORMAL_TEXT
24739 || s->hl == DRAW_IMAGE_RAISED
24740 || s->hl == DRAW_IMAGE_SUNKEN))
24741 || s->hl == DRAW_MOUSE_FACE))
24742 s->extends_to_end_of_line_p = 1;
24743
24744 /* If S extends its face to the end of the line, set its
24745 background_width to the distance to the right edge of the drawing
24746 area. */
24747 if (s->extends_to_end_of_line_p)
24748 s->background_width = last_x - s->x + 1;
24749 else
24750 s->background_width = s->width;
24751 }
24752
24753
24754 /* Compute overhangs and x-positions for glyph string S and its
24755 predecessors, or successors. X is the starting x-position for S.
24756 BACKWARD_P non-zero means process predecessors. */
24757
24758 static void
24759 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24760 {
24761 if (backward_p)
24762 {
24763 while (s)
24764 {
24765 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24766 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24767 x -= s->width;
24768 s->x = x;
24769 s = s->prev;
24770 }
24771 }
24772 else
24773 {
24774 while (s)
24775 {
24776 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24777 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24778 s->x = x;
24779 x += s->width;
24780 s = s->next;
24781 }
24782 }
24783 }
24784
24785
24786
24787 /* The following macros are only called from draw_glyphs below.
24788 They reference the following parameters of that function directly:
24789 `w', `row', `area', and `overlap_p'
24790 as well as the following local variables:
24791 `s', `f', and `hdc' (in W32) */
24792
24793 #ifdef HAVE_NTGUI
24794 /* On W32, silently add local `hdc' variable to argument list of
24795 init_glyph_string. */
24796 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24797 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24798 #else
24799 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24800 init_glyph_string (s, char2b, w, row, area, start, hl)
24801 #endif
24802
24803 /* Add a glyph string for a stretch glyph to the list of strings
24804 between HEAD and TAIL. START is the index of the stretch glyph in
24805 row area AREA of glyph row ROW. END is the index of the last glyph
24806 in that glyph row area. X is the current output position assigned
24807 to the new glyph string constructed. HL overrides that face of the
24808 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24809 is the right-most x-position of the drawing area. */
24810
24811 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24812 and below -- keep them on one line. */
24813 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24814 do \
24815 { \
24816 s = alloca (sizeof *s); \
24817 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24818 START = fill_stretch_glyph_string (s, START, END); \
24819 append_glyph_string (&HEAD, &TAIL, s); \
24820 s->x = (X); \
24821 } \
24822 while (0)
24823
24824
24825 /* Add a glyph string for an image glyph to the list of strings
24826 between HEAD and TAIL. START is the index of the image glyph in
24827 row area AREA of glyph row ROW. END is the index of the last glyph
24828 in that glyph row area. X is the current output position assigned
24829 to the new glyph string constructed. HL overrides that face of the
24830 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24831 is the right-most x-position of the drawing area. */
24832
24833 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24834 do \
24835 { \
24836 s = alloca (sizeof *s); \
24837 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24838 fill_image_glyph_string (s); \
24839 append_glyph_string (&HEAD, &TAIL, s); \
24840 ++START; \
24841 s->x = (X); \
24842 } \
24843 while (0)
24844
24845
24846 /* Add a glyph string for a sequence of character glyphs to the list
24847 of strings between HEAD and TAIL. START is the index of the first
24848 glyph in row area AREA of glyph row ROW that is part of the new
24849 glyph string. END is the index of the last glyph in that glyph row
24850 area. X is the current output position assigned to the new glyph
24851 string constructed. HL overrides that face of the glyph; e.g. it
24852 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24853 right-most x-position of the drawing area. */
24854
24855 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24856 do \
24857 { \
24858 int face_id; \
24859 XChar2b *char2b; \
24860 \
24861 face_id = (row)->glyphs[area][START].face_id; \
24862 \
24863 s = alloca (sizeof *s); \
24864 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24865 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24866 append_glyph_string (&HEAD, &TAIL, s); \
24867 s->x = (X); \
24868 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24869 } \
24870 while (0)
24871
24872
24873 /* Add a glyph string for a composite sequence to the list of strings
24874 between HEAD and TAIL. START is the index of the first glyph in
24875 row area AREA of glyph row ROW that is part of the new glyph
24876 string. END is the index of the last glyph in that glyph row area.
24877 X is the current output position assigned to the new glyph string
24878 constructed. HL overrides that face of the glyph; e.g. it is
24879 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24880 x-position of the drawing area. */
24881
24882 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24883 do { \
24884 int face_id = (row)->glyphs[area][START].face_id; \
24885 struct face *base_face = FACE_FROM_ID (f, face_id); \
24886 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24887 struct composition *cmp = composition_table[cmp_id]; \
24888 XChar2b *char2b; \
24889 struct glyph_string *first_s = NULL; \
24890 int n; \
24891 \
24892 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24893 \
24894 /* Make glyph_strings for each glyph sequence that is drawable by \
24895 the same face, and append them to HEAD/TAIL. */ \
24896 for (n = 0; n < cmp->glyph_len;) \
24897 { \
24898 s = alloca (sizeof *s); \
24899 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24900 append_glyph_string (&(HEAD), &(TAIL), s); \
24901 s->cmp = cmp; \
24902 s->cmp_from = n; \
24903 s->x = (X); \
24904 if (n == 0) \
24905 first_s = s; \
24906 n = fill_composite_glyph_string (s, base_face, overlaps); \
24907 } \
24908 \
24909 ++START; \
24910 s = first_s; \
24911 } while (0)
24912
24913
24914 /* Add a glyph string for a glyph-string sequence to the list of strings
24915 between HEAD and TAIL. */
24916
24917 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24918 do { \
24919 int face_id; \
24920 XChar2b *char2b; \
24921 Lisp_Object gstring; \
24922 \
24923 face_id = (row)->glyphs[area][START].face_id; \
24924 gstring = (composition_gstring_from_id \
24925 ((row)->glyphs[area][START].u.cmp.id)); \
24926 s = alloca (sizeof *s); \
24927 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24928 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24929 append_glyph_string (&(HEAD), &(TAIL), s); \
24930 s->x = (X); \
24931 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24932 } while (0)
24933
24934
24935 /* Add a glyph string for a sequence of glyphless character's glyphs
24936 to the list of strings between HEAD and TAIL. The meanings of
24937 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24938
24939 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24940 do \
24941 { \
24942 int face_id; \
24943 \
24944 face_id = (row)->glyphs[area][START].face_id; \
24945 \
24946 s = alloca (sizeof *s); \
24947 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24948 append_glyph_string (&HEAD, &TAIL, s); \
24949 s->x = (X); \
24950 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24951 overlaps); \
24952 } \
24953 while (0)
24954
24955
24956 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24957 of AREA of glyph row ROW on window W between indices START and END.
24958 HL overrides the face for drawing glyph strings, e.g. it is
24959 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24960 x-positions of the drawing area.
24961
24962 This is an ugly monster macro construct because we must use alloca
24963 to allocate glyph strings (because draw_glyphs can be called
24964 asynchronously). */
24965
24966 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24967 do \
24968 { \
24969 HEAD = TAIL = NULL; \
24970 while (START < END) \
24971 { \
24972 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24973 switch (first_glyph->type) \
24974 { \
24975 case CHAR_GLYPH: \
24976 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24977 HL, X, LAST_X); \
24978 break; \
24979 \
24980 case COMPOSITE_GLYPH: \
24981 if (first_glyph->u.cmp.automatic) \
24982 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24983 HL, X, LAST_X); \
24984 else \
24985 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24986 HL, X, LAST_X); \
24987 break; \
24988 \
24989 case STRETCH_GLYPH: \
24990 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24991 HL, X, LAST_X); \
24992 break; \
24993 \
24994 case IMAGE_GLYPH: \
24995 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24996 HL, X, LAST_X); \
24997 break; \
24998 \
24999 case GLYPHLESS_GLYPH: \
25000 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25001 HL, X, LAST_X); \
25002 break; \
25003 \
25004 default: \
25005 emacs_abort (); \
25006 } \
25007 \
25008 if (s) \
25009 { \
25010 set_glyph_string_background_width (s, START, LAST_X); \
25011 (X) += s->width; \
25012 } \
25013 } \
25014 } while (0)
25015
25016
25017 /* Draw glyphs between START and END in AREA of ROW on window W,
25018 starting at x-position X. X is relative to AREA in W. HL is a
25019 face-override with the following meaning:
25020
25021 DRAW_NORMAL_TEXT draw normally
25022 DRAW_CURSOR draw in cursor face
25023 DRAW_MOUSE_FACE draw in mouse face.
25024 DRAW_INVERSE_VIDEO draw in mode line face
25025 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25026 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25027
25028 If OVERLAPS is non-zero, draw only the foreground of characters and
25029 clip to the physical height of ROW. Non-zero value also defines
25030 the overlapping part to be drawn:
25031
25032 OVERLAPS_PRED overlap with preceding rows
25033 OVERLAPS_SUCC overlap with succeeding rows
25034 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25035 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25036
25037 Value is the x-position reached, relative to AREA of W. */
25038
25039 static int
25040 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25041 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25042 enum draw_glyphs_face hl, int overlaps)
25043 {
25044 struct glyph_string *head, *tail;
25045 struct glyph_string *s;
25046 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25047 int i, j, x_reached, last_x, area_left = 0;
25048 struct frame *f = XFRAME (WINDOW_FRAME (w));
25049 DECLARE_HDC (hdc);
25050
25051 ALLOCATE_HDC (hdc, f);
25052
25053 /* Let's rather be paranoid than getting a SEGV. */
25054 end = min (end, row->used[area]);
25055 start = clip_to_bounds (0, start, end);
25056
25057 /* Translate X to frame coordinates. Set last_x to the right
25058 end of the drawing area. */
25059 if (row->full_width_p)
25060 {
25061 /* X is relative to the left edge of W, without scroll bars
25062 or fringes. */
25063 area_left = WINDOW_LEFT_EDGE_X (w);
25064 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25065 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25066 }
25067 else
25068 {
25069 area_left = window_box_left (w, area);
25070 last_x = area_left + window_box_width (w, area);
25071 }
25072 x += area_left;
25073
25074 /* Build a doubly-linked list of glyph_string structures between
25075 head and tail from what we have to draw. Note that the macro
25076 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25077 the reason we use a separate variable `i'. */
25078 i = start;
25079 USE_SAFE_ALLOCA;
25080 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25081 if (tail)
25082 x_reached = tail->x + tail->background_width;
25083 else
25084 x_reached = x;
25085
25086 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25087 the row, redraw some glyphs in front or following the glyph
25088 strings built above. */
25089 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25090 {
25091 struct glyph_string *h, *t;
25092 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25093 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25094 int check_mouse_face = 0;
25095 int dummy_x = 0;
25096
25097 /* If mouse highlighting is on, we may need to draw adjacent
25098 glyphs using mouse-face highlighting. */
25099 if (area == TEXT_AREA && row->mouse_face_p
25100 && hlinfo->mouse_face_beg_row >= 0
25101 && hlinfo->mouse_face_end_row >= 0)
25102 {
25103 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25104
25105 if (row_vpos >= hlinfo->mouse_face_beg_row
25106 && row_vpos <= hlinfo->mouse_face_end_row)
25107 {
25108 check_mouse_face = 1;
25109 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25110 ? hlinfo->mouse_face_beg_col : 0;
25111 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25112 ? hlinfo->mouse_face_end_col
25113 : row->used[TEXT_AREA];
25114 }
25115 }
25116
25117 /* Compute overhangs for all glyph strings. */
25118 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25119 for (s = head; s; s = s->next)
25120 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25121
25122 /* Prepend glyph strings for glyphs in front of the first glyph
25123 string that are overwritten because of the first glyph
25124 string's left overhang. The background of all strings
25125 prepended must be drawn because the first glyph string
25126 draws over it. */
25127 i = left_overwritten (head);
25128 if (i >= 0)
25129 {
25130 enum draw_glyphs_face overlap_hl;
25131
25132 /* If this row contains mouse highlighting, attempt to draw
25133 the overlapped glyphs with the correct highlight. This
25134 code fails if the overlap encompasses more than one glyph
25135 and mouse-highlight spans only some of these glyphs.
25136 However, making it work perfectly involves a lot more
25137 code, and I don't know if the pathological case occurs in
25138 practice, so we'll stick to this for now. --- cyd */
25139 if (check_mouse_face
25140 && mouse_beg_col < start && mouse_end_col > i)
25141 overlap_hl = DRAW_MOUSE_FACE;
25142 else
25143 overlap_hl = DRAW_NORMAL_TEXT;
25144
25145 if (hl != overlap_hl)
25146 clip_head = head;
25147 j = i;
25148 BUILD_GLYPH_STRINGS (j, start, h, t,
25149 overlap_hl, dummy_x, last_x);
25150 start = i;
25151 compute_overhangs_and_x (t, head->x, 1);
25152 prepend_glyph_string_lists (&head, &tail, h, t);
25153 if (clip_head == NULL)
25154 clip_head = head;
25155 }
25156
25157 /* Prepend glyph strings for glyphs in front of the first glyph
25158 string that overwrite that glyph string because of their
25159 right overhang. For these strings, only the foreground must
25160 be drawn, because it draws over the glyph string at `head'.
25161 The background must not be drawn because this would overwrite
25162 right overhangs of preceding glyphs for which no glyph
25163 strings exist. */
25164 i = left_overwriting (head);
25165 if (i >= 0)
25166 {
25167 enum draw_glyphs_face overlap_hl;
25168
25169 if (check_mouse_face
25170 && mouse_beg_col < start && mouse_end_col > i)
25171 overlap_hl = DRAW_MOUSE_FACE;
25172 else
25173 overlap_hl = DRAW_NORMAL_TEXT;
25174
25175 if (hl == overlap_hl || clip_head == NULL)
25176 clip_head = head;
25177 BUILD_GLYPH_STRINGS (i, start, h, t,
25178 overlap_hl, dummy_x, last_x);
25179 for (s = h; s; s = s->next)
25180 s->background_filled_p = 1;
25181 compute_overhangs_and_x (t, head->x, 1);
25182 prepend_glyph_string_lists (&head, &tail, h, t);
25183 }
25184
25185 /* Append glyphs strings for glyphs following the last glyph
25186 string tail that are overwritten by tail. The background of
25187 these strings has to be drawn because tail's foreground draws
25188 over it. */
25189 i = right_overwritten (tail);
25190 if (i >= 0)
25191 {
25192 enum draw_glyphs_face overlap_hl;
25193
25194 if (check_mouse_face
25195 && mouse_beg_col < i && mouse_end_col > end)
25196 overlap_hl = DRAW_MOUSE_FACE;
25197 else
25198 overlap_hl = DRAW_NORMAL_TEXT;
25199
25200 if (hl != overlap_hl)
25201 clip_tail = tail;
25202 BUILD_GLYPH_STRINGS (end, i, h, t,
25203 overlap_hl, x, last_x);
25204 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25205 we don't have `end = i;' here. */
25206 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25207 append_glyph_string_lists (&head, &tail, h, t);
25208 if (clip_tail == NULL)
25209 clip_tail = tail;
25210 }
25211
25212 /* Append glyph strings for glyphs following the last glyph
25213 string tail that overwrite tail. The foreground of such
25214 glyphs has to be drawn because it writes into the background
25215 of tail. The background must not be drawn because it could
25216 paint over the foreground of following glyphs. */
25217 i = right_overwriting (tail);
25218 if (i >= 0)
25219 {
25220 enum draw_glyphs_face overlap_hl;
25221 if (check_mouse_face
25222 && mouse_beg_col < i && mouse_end_col > end)
25223 overlap_hl = DRAW_MOUSE_FACE;
25224 else
25225 overlap_hl = DRAW_NORMAL_TEXT;
25226
25227 if (hl == overlap_hl || clip_tail == NULL)
25228 clip_tail = tail;
25229 i++; /* We must include the Ith glyph. */
25230 BUILD_GLYPH_STRINGS (end, i, h, t,
25231 overlap_hl, x, last_x);
25232 for (s = h; s; s = s->next)
25233 s->background_filled_p = 1;
25234 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25235 append_glyph_string_lists (&head, &tail, h, t);
25236 }
25237 if (clip_head || clip_tail)
25238 for (s = head; s; s = s->next)
25239 {
25240 s->clip_head = clip_head;
25241 s->clip_tail = clip_tail;
25242 }
25243 }
25244
25245 /* Draw all strings. */
25246 for (s = head; s; s = s->next)
25247 FRAME_RIF (f)->draw_glyph_string (s);
25248
25249 #ifndef HAVE_NS
25250 /* When focus a sole frame and move horizontally, this sets on_p to 0
25251 causing a failure to erase prev cursor position. */
25252 if (area == TEXT_AREA
25253 && !row->full_width_p
25254 /* When drawing overlapping rows, only the glyph strings'
25255 foreground is drawn, which doesn't erase a cursor
25256 completely. */
25257 && !overlaps)
25258 {
25259 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25260 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25261 : (tail ? tail->x + tail->background_width : x));
25262 x0 -= area_left;
25263 x1 -= area_left;
25264
25265 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25266 row->y, MATRIX_ROW_BOTTOM_Y (row));
25267 }
25268 #endif
25269
25270 /* Value is the x-position up to which drawn, relative to AREA of W.
25271 This doesn't include parts drawn because of overhangs. */
25272 if (row->full_width_p)
25273 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25274 else
25275 x_reached -= area_left;
25276
25277 RELEASE_HDC (hdc, f);
25278
25279 SAFE_FREE ();
25280 return x_reached;
25281 }
25282
25283 /* Expand row matrix if too narrow. Don't expand if area
25284 is not present. */
25285
25286 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25287 { \
25288 if (!it->f->fonts_changed \
25289 && (it->glyph_row->glyphs[area] \
25290 < it->glyph_row->glyphs[area + 1])) \
25291 { \
25292 it->w->ncols_scale_factor++; \
25293 it->f->fonts_changed = 1; \
25294 } \
25295 }
25296
25297 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25298 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25299
25300 static void
25301 append_glyph (struct it *it)
25302 {
25303 struct glyph *glyph;
25304 enum glyph_row_area area = it->area;
25305
25306 eassert (it->glyph_row);
25307 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25308
25309 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25310 if (glyph < it->glyph_row->glyphs[area + 1])
25311 {
25312 /* If the glyph row is reversed, we need to prepend the glyph
25313 rather than append it. */
25314 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25315 {
25316 struct glyph *g;
25317
25318 /* Make room for the additional glyph. */
25319 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25320 g[1] = *g;
25321 glyph = it->glyph_row->glyphs[area];
25322 }
25323 glyph->charpos = CHARPOS (it->position);
25324 glyph->object = it->object;
25325 if (it->pixel_width > 0)
25326 {
25327 glyph->pixel_width = it->pixel_width;
25328 glyph->padding_p = 0;
25329 }
25330 else
25331 {
25332 /* Assure at least 1-pixel width. Otherwise, cursor can't
25333 be displayed correctly. */
25334 glyph->pixel_width = 1;
25335 glyph->padding_p = 1;
25336 }
25337 glyph->ascent = it->ascent;
25338 glyph->descent = it->descent;
25339 glyph->voffset = it->voffset;
25340 glyph->type = CHAR_GLYPH;
25341 glyph->avoid_cursor_p = it->avoid_cursor_p;
25342 glyph->multibyte_p = it->multibyte_p;
25343 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25344 {
25345 /* In R2L rows, the left and the right box edges need to be
25346 drawn in reverse direction. */
25347 glyph->right_box_line_p = it->start_of_box_run_p;
25348 glyph->left_box_line_p = it->end_of_box_run_p;
25349 }
25350 else
25351 {
25352 glyph->left_box_line_p = it->start_of_box_run_p;
25353 glyph->right_box_line_p = it->end_of_box_run_p;
25354 }
25355 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25356 || it->phys_descent > it->descent);
25357 glyph->glyph_not_available_p = it->glyph_not_available_p;
25358 glyph->face_id = it->face_id;
25359 glyph->u.ch = it->char_to_display;
25360 glyph->slice.img = null_glyph_slice;
25361 glyph->font_type = FONT_TYPE_UNKNOWN;
25362 if (it->bidi_p)
25363 {
25364 glyph->resolved_level = it->bidi_it.resolved_level;
25365 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25366 glyph->bidi_type = it->bidi_it.type;
25367 }
25368 else
25369 {
25370 glyph->resolved_level = 0;
25371 glyph->bidi_type = UNKNOWN_BT;
25372 }
25373 ++it->glyph_row->used[area];
25374 }
25375 else
25376 IT_EXPAND_MATRIX_WIDTH (it, area);
25377 }
25378
25379 /* Store one glyph for the composition IT->cmp_it.id in
25380 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25381 non-null. */
25382
25383 static void
25384 append_composite_glyph (struct it *it)
25385 {
25386 struct glyph *glyph;
25387 enum glyph_row_area area = it->area;
25388
25389 eassert (it->glyph_row);
25390
25391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25392 if (glyph < it->glyph_row->glyphs[area + 1])
25393 {
25394 /* If the glyph row is reversed, we need to prepend the glyph
25395 rather than append it. */
25396 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25397 {
25398 struct glyph *g;
25399
25400 /* Make room for the new glyph. */
25401 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25402 g[1] = *g;
25403 glyph = it->glyph_row->glyphs[it->area];
25404 }
25405 glyph->charpos = it->cmp_it.charpos;
25406 glyph->object = it->object;
25407 glyph->pixel_width = it->pixel_width;
25408 glyph->ascent = it->ascent;
25409 glyph->descent = it->descent;
25410 glyph->voffset = it->voffset;
25411 glyph->type = COMPOSITE_GLYPH;
25412 if (it->cmp_it.ch < 0)
25413 {
25414 glyph->u.cmp.automatic = 0;
25415 glyph->u.cmp.id = it->cmp_it.id;
25416 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25417 }
25418 else
25419 {
25420 glyph->u.cmp.automatic = 1;
25421 glyph->u.cmp.id = it->cmp_it.id;
25422 glyph->slice.cmp.from = it->cmp_it.from;
25423 glyph->slice.cmp.to = it->cmp_it.to - 1;
25424 }
25425 glyph->avoid_cursor_p = it->avoid_cursor_p;
25426 glyph->multibyte_p = it->multibyte_p;
25427 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25428 {
25429 /* In R2L rows, the left and the right box edges need to be
25430 drawn in reverse direction. */
25431 glyph->right_box_line_p = it->start_of_box_run_p;
25432 glyph->left_box_line_p = it->end_of_box_run_p;
25433 }
25434 else
25435 {
25436 glyph->left_box_line_p = it->start_of_box_run_p;
25437 glyph->right_box_line_p = it->end_of_box_run_p;
25438 }
25439 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25440 || it->phys_descent > it->descent);
25441 glyph->padding_p = 0;
25442 glyph->glyph_not_available_p = 0;
25443 glyph->face_id = it->face_id;
25444 glyph->font_type = FONT_TYPE_UNKNOWN;
25445 if (it->bidi_p)
25446 {
25447 glyph->resolved_level = it->bidi_it.resolved_level;
25448 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25449 glyph->bidi_type = it->bidi_it.type;
25450 }
25451 ++it->glyph_row->used[area];
25452 }
25453 else
25454 IT_EXPAND_MATRIX_WIDTH (it, area);
25455 }
25456
25457
25458 /* Change IT->ascent and IT->height according to the setting of
25459 IT->voffset. */
25460
25461 static void
25462 take_vertical_position_into_account (struct it *it)
25463 {
25464 if (it->voffset)
25465 {
25466 if (it->voffset < 0)
25467 /* Increase the ascent so that we can display the text higher
25468 in the line. */
25469 it->ascent -= it->voffset;
25470 else
25471 /* Increase the descent so that we can display the text lower
25472 in the line. */
25473 it->descent += it->voffset;
25474 }
25475 }
25476
25477
25478 /* Produce glyphs/get display metrics for the image IT is loaded with.
25479 See the description of struct display_iterator in dispextern.h for
25480 an overview of struct display_iterator. */
25481
25482 static void
25483 produce_image_glyph (struct it *it)
25484 {
25485 struct image *img;
25486 struct face *face;
25487 int glyph_ascent, crop;
25488 struct glyph_slice slice;
25489
25490 eassert (it->what == IT_IMAGE);
25491
25492 face = FACE_FROM_ID (it->f, it->face_id);
25493 eassert (face);
25494 /* Make sure X resources of the face is loaded. */
25495 prepare_face_for_display (it->f, face);
25496
25497 if (it->image_id < 0)
25498 {
25499 /* Fringe bitmap. */
25500 it->ascent = it->phys_ascent = 0;
25501 it->descent = it->phys_descent = 0;
25502 it->pixel_width = 0;
25503 it->nglyphs = 0;
25504 return;
25505 }
25506
25507 img = IMAGE_FROM_ID (it->f, it->image_id);
25508 eassert (img);
25509 /* Make sure X resources of the image is loaded. */
25510 prepare_image_for_display (it->f, img);
25511
25512 slice.x = slice.y = 0;
25513 slice.width = img->width;
25514 slice.height = img->height;
25515
25516 if (INTEGERP (it->slice.x))
25517 slice.x = XINT (it->slice.x);
25518 else if (FLOATP (it->slice.x))
25519 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25520
25521 if (INTEGERP (it->slice.y))
25522 slice.y = XINT (it->slice.y);
25523 else if (FLOATP (it->slice.y))
25524 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25525
25526 if (INTEGERP (it->slice.width))
25527 slice.width = XINT (it->slice.width);
25528 else if (FLOATP (it->slice.width))
25529 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25530
25531 if (INTEGERP (it->slice.height))
25532 slice.height = XINT (it->slice.height);
25533 else if (FLOATP (it->slice.height))
25534 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25535
25536 if (slice.x >= img->width)
25537 slice.x = img->width;
25538 if (slice.y >= img->height)
25539 slice.y = img->height;
25540 if (slice.x + slice.width >= img->width)
25541 slice.width = img->width - slice.x;
25542 if (slice.y + slice.height > img->height)
25543 slice.height = img->height - slice.y;
25544
25545 if (slice.width == 0 || slice.height == 0)
25546 return;
25547
25548 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25549
25550 it->descent = slice.height - glyph_ascent;
25551 if (slice.y == 0)
25552 it->descent += img->vmargin;
25553 if (slice.y + slice.height == img->height)
25554 it->descent += img->vmargin;
25555 it->phys_descent = it->descent;
25556
25557 it->pixel_width = slice.width;
25558 if (slice.x == 0)
25559 it->pixel_width += img->hmargin;
25560 if (slice.x + slice.width == img->width)
25561 it->pixel_width += img->hmargin;
25562
25563 /* It's quite possible for images to have an ascent greater than
25564 their height, so don't get confused in that case. */
25565 if (it->descent < 0)
25566 it->descent = 0;
25567
25568 it->nglyphs = 1;
25569
25570 if (face->box != FACE_NO_BOX)
25571 {
25572 if (face->box_line_width > 0)
25573 {
25574 if (slice.y == 0)
25575 it->ascent += face->box_line_width;
25576 if (slice.y + slice.height == img->height)
25577 it->descent += face->box_line_width;
25578 }
25579
25580 if (it->start_of_box_run_p && slice.x == 0)
25581 it->pixel_width += eabs (face->box_line_width);
25582 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25583 it->pixel_width += eabs (face->box_line_width);
25584 }
25585
25586 take_vertical_position_into_account (it);
25587
25588 /* Automatically crop wide image glyphs at right edge so we can
25589 draw the cursor on same display row. */
25590 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25591 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25592 {
25593 it->pixel_width -= crop;
25594 slice.width -= crop;
25595 }
25596
25597 if (it->glyph_row)
25598 {
25599 struct glyph *glyph;
25600 enum glyph_row_area area = it->area;
25601
25602 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25603 if (glyph < it->glyph_row->glyphs[area + 1])
25604 {
25605 glyph->charpos = CHARPOS (it->position);
25606 glyph->object = it->object;
25607 glyph->pixel_width = it->pixel_width;
25608 glyph->ascent = glyph_ascent;
25609 glyph->descent = it->descent;
25610 glyph->voffset = it->voffset;
25611 glyph->type = IMAGE_GLYPH;
25612 glyph->avoid_cursor_p = it->avoid_cursor_p;
25613 glyph->multibyte_p = it->multibyte_p;
25614 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25615 {
25616 /* In R2L rows, the left and the right box edges need to be
25617 drawn in reverse direction. */
25618 glyph->right_box_line_p = it->start_of_box_run_p;
25619 glyph->left_box_line_p = it->end_of_box_run_p;
25620 }
25621 else
25622 {
25623 glyph->left_box_line_p = it->start_of_box_run_p;
25624 glyph->right_box_line_p = it->end_of_box_run_p;
25625 }
25626 glyph->overlaps_vertically_p = 0;
25627 glyph->padding_p = 0;
25628 glyph->glyph_not_available_p = 0;
25629 glyph->face_id = it->face_id;
25630 glyph->u.img_id = img->id;
25631 glyph->slice.img = slice;
25632 glyph->font_type = FONT_TYPE_UNKNOWN;
25633 if (it->bidi_p)
25634 {
25635 glyph->resolved_level = it->bidi_it.resolved_level;
25636 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25637 glyph->bidi_type = it->bidi_it.type;
25638 }
25639 ++it->glyph_row->used[area];
25640 }
25641 else
25642 IT_EXPAND_MATRIX_WIDTH (it, area);
25643 }
25644 }
25645
25646
25647 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25648 of the glyph, WIDTH and HEIGHT are the width and height of the
25649 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25650
25651 static void
25652 append_stretch_glyph (struct it *it, Lisp_Object object,
25653 int width, int height, int ascent)
25654 {
25655 struct glyph *glyph;
25656 enum glyph_row_area area = it->area;
25657
25658 eassert (ascent >= 0 && ascent <= height);
25659
25660 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25661 if (glyph < it->glyph_row->glyphs[area + 1])
25662 {
25663 /* If the glyph row is reversed, we need to prepend the glyph
25664 rather than append it. */
25665 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25666 {
25667 struct glyph *g;
25668
25669 /* Make room for the additional glyph. */
25670 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25671 g[1] = *g;
25672 glyph = it->glyph_row->glyphs[area];
25673
25674 /* Decrease the width of the first glyph of the row that
25675 begins before first_visible_x (e.g., due to hscroll).
25676 This is so the overall width of the row becomes smaller
25677 by the scroll amount, and the stretch glyph appended by
25678 extend_face_to_end_of_line will be wider, to shift the
25679 row glyphs to the right. (In L2R rows, the corresponding
25680 left-shift effect is accomplished by setting row->x to a
25681 negative value, which won't work with R2L rows.)
25682
25683 This must leave us with a positive value of WIDTH, since
25684 otherwise the call to move_it_in_display_line_to at the
25685 beginning of display_line would have got past the entire
25686 first glyph, and then it->current_x would have been
25687 greater or equal to it->first_visible_x. */
25688 if (it->current_x < it->first_visible_x)
25689 width -= it->first_visible_x - it->current_x;
25690 eassert (width > 0);
25691 }
25692 glyph->charpos = CHARPOS (it->position);
25693 glyph->object = object;
25694 glyph->pixel_width = width;
25695 glyph->ascent = ascent;
25696 glyph->descent = height - ascent;
25697 glyph->voffset = it->voffset;
25698 glyph->type = STRETCH_GLYPH;
25699 glyph->avoid_cursor_p = it->avoid_cursor_p;
25700 glyph->multibyte_p = it->multibyte_p;
25701 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25702 {
25703 /* In R2L rows, the left and the right box edges need to be
25704 drawn in reverse direction. */
25705 glyph->right_box_line_p = it->start_of_box_run_p;
25706 glyph->left_box_line_p = it->end_of_box_run_p;
25707 }
25708 else
25709 {
25710 glyph->left_box_line_p = it->start_of_box_run_p;
25711 glyph->right_box_line_p = it->end_of_box_run_p;
25712 }
25713 glyph->overlaps_vertically_p = 0;
25714 glyph->padding_p = 0;
25715 glyph->glyph_not_available_p = 0;
25716 glyph->face_id = it->face_id;
25717 glyph->u.stretch.ascent = ascent;
25718 glyph->u.stretch.height = height;
25719 glyph->slice.img = null_glyph_slice;
25720 glyph->font_type = FONT_TYPE_UNKNOWN;
25721 if (it->bidi_p)
25722 {
25723 glyph->resolved_level = it->bidi_it.resolved_level;
25724 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25725 glyph->bidi_type = it->bidi_it.type;
25726 }
25727 else
25728 {
25729 glyph->resolved_level = 0;
25730 glyph->bidi_type = UNKNOWN_BT;
25731 }
25732 ++it->glyph_row->used[area];
25733 }
25734 else
25735 IT_EXPAND_MATRIX_WIDTH (it, area);
25736 }
25737
25738 #endif /* HAVE_WINDOW_SYSTEM */
25739
25740 /* Produce a stretch glyph for iterator IT. IT->object is the value
25741 of the glyph property displayed. The value must be a list
25742 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25743 being recognized:
25744
25745 1. `:width WIDTH' specifies that the space should be WIDTH *
25746 canonical char width wide. WIDTH may be an integer or floating
25747 point number.
25748
25749 2. `:relative-width FACTOR' specifies that the width of the stretch
25750 should be computed from the width of the first character having the
25751 `glyph' property, and should be FACTOR times that width.
25752
25753 3. `:align-to HPOS' specifies that the space should be wide enough
25754 to reach HPOS, a value in canonical character units.
25755
25756 Exactly one of the above pairs must be present.
25757
25758 4. `:height HEIGHT' specifies that the height of the stretch produced
25759 should be HEIGHT, measured in canonical character units.
25760
25761 5. `:relative-height FACTOR' specifies that the height of the
25762 stretch should be FACTOR times the height of the characters having
25763 the glyph property.
25764
25765 Either none or exactly one of 4 or 5 must be present.
25766
25767 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25768 of the stretch should be used for the ascent of the stretch.
25769 ASCENT must be in the range 0 <= ASCENT <= 100. */
25770
25771 void
25772 produce_stretch_glyph (struct it *it)
25773 {
25774 /* (space :width WIDTH :height HEIGHT ...) */
25775 Lisp_Object prop, plist;
25776 int width = 0, height = 0, align_to = -1;
25777 int zero_width_ok_p = 0;
25778 double tem;
25779 struct font *font = NULL;
25780
25781 #ifdef HAVE_WINDOW_SYSTEM
25782 int ascent = 0;
25783 int zero_height_ok_p = 0;
25784
25785 if (FRAME_WINDOW_P (it->f))
25786 {
25787 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25788 font = face->font ? face->font : FRAME_FONT (it->f);
25789 prepare_face_for_display (it->f, face);
25790 }
25791 #endif
25792
25793 /* List should start with `space'. */
25794 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25795 plist = XCDR (it->object);
25796
25797 /* Compute the width of the stretch. */
25798 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25799 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25800 {
25801 /* Absolute width `:width WIDTH' specified and valid. */
25802 zero_width_ok_p = 1;
25803 width = (int)tem;
25804 }
25805 #ifdef HAVE_WINDOW_SYSTEM
25806 else if (FRAME_WINDOW_P (it->f)
25807 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25808 {
25809 /* Relative width `:relative-width FACTOR' specified and valid.
25810 Compute the width of the characters having the `glyph'
25811 property. */
25812 struct it it2;
25813 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25814
25815 it2 = *it;
25816 if (it->multibyte_p)
25817 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25818 else
25819 {
25820 it2.c = it2.char_to_display = *p, it2.len = 1;
25821 if (! ASCII_CHAR_P (it2.c))
25822 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25823 }
25824
25825 it2.glyph_row = NULL;
25826 it2.what = IT_CHARACTER;
25827 x_produce_glyphs (&it2);
25828 width = NUMVAL (prop) * it2.pixel_width;
25829 }
25830 #endif /* HAVE_WINDOW_SYSTEM */
25831 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25832 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25833 {
25834 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25835 align_to = (align_to < 0
25836 ? 0
25837 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25838 else if (align_to < 0)
25839 align_to = window_box_left_offset (it->w, TEXT_AREA);
25840 width = max (0, (int)tem + align_to - it->current_x);
25841 zero_width_ok_p = 1;
25842 }
25843 else
25844 /* Nothing specified -> width defaults to canonical char width. */
25845 width = FRAME_COLUMN_WIDTH (it->f);
25846
25847 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25848 width = 1;
25849
25850 #ifdef HAVE_WINDOW_SYSTEM
25851 /* Compute height. */
25852 if (FRAME_WINDOW_P (it->f))
25853 {
25854 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25855 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25856 {
25857 height = (int)tem;
25858 zero_height_ok_p = 1;
25859 }
25860 else if (prop = Fplist_get (plist, QCrelative_height),
25861 NUMVAL (prop) > 0)
25862 height = FONT_HEIGHT (font) * NUMVAL (prop);
25863 else
25864 height = FONT_HEIGHT (font);
25865
25866 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25867 height = 1;
25868
25869 /* Compute percentage of height used for ascent. If
25870 `:ascent ASCENT' is present and valid, use that. Otherwise,
25871 derive the ascent from the font in use. */
25872 if (prop = Fplist_get (plist, QCascent),
25873 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25874 ascent = height * NUMVAL (prop) / 100.0;
25875 else if (!NILP (prop)
25876 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25877 ascent = min (max (0, (int)tem), height);
25878 else
25879 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25880 }
25881 else
25882 #endif /* HAVE_WINDOW_SYSTEM */
25883 height = 1;
25884
25885 if (width > 0 && it->line_wrap != TRUNCATE
25886 && it->current_x + width > it->last_visible_x)
25887 {
25888 width = it->last_visible_x - it->current_x;
25889 #ifdef HAVE_WINDOW_SYSTEM
25890 /* Subtract one more pixel from the stretch width, but only on
25891 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25892 width -= FRAME_WINDOW_P (it->f);
25893 #endif
25894 }
25895
25896 if (width > 0 && height > 0 && it->glyph_row)
25897 {
25898 Lisp_Object o_object = it->object;
25899 Lisp_Object object = it->stack[it->sp - 1].string;
25900 int n = width;
25901
25902 if (!STRINGP (object))
25903 object = it->w->contents;
25904 #ifdef HAVE_WINDOW_SYSTEM
25905 if (FRAME_WINDOW_P (it->f))
25906 append_stretch_glyph (it, object, width, height, ascent);
25907 else
25908 #endif
25909 {
25910 it->object = object;
25911 it->char_to_display = ' ';
25912 it->pixel_width = it->len = 1;
25913 while (n--)
25914 tty_append_glyph (it);
25915 it->object = o_object;
25916 }
25917 }
25918
25919 it->pixel_width = width;
25920 #ifdef HAVE_WINDOW_SYSTEM
25921 if (FRAME_WINDOW_P (it->f))
25922 {
25923 it->ascent = it->phys_ascent = ascent;
25924 it->descent = it->phys_descent = height - it->ascent;
25925 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25926 take_vertical_position_into_account (it);
25927 }
25928 else
25929 #endif
25930 it->nglyphs = width;
25931 }
25932
25933 /* Get information about special display element WHAT in an
25934 environment described by IT. WHAT is one of IT_TRUNCATION or
25935 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25936 non-null glyph_row member. This function ensures that fields like
25937 face_id, c, len of IT are left untouched. */
25938
25939 static void
25940 produce_special_glyphs (struct it *it, enum display_element_type what)
25941 {
25942 struct it temp_it;
25943 Lisp_Object gc;
25944 GLYPH glyph;
25945
25946 temp_it = *it;
25947 temp_it.object = make_number (0);
25948 memset (&temp_it.current, 0, sizeof temp_it.current);
25949
25950 if (what == IT_CONTINUATION)
25951 {
25952 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25953 if (it->bidi_it.paragraph_dir == R2L)
25954 SET_GLYPH_FROM_CHAR (glyph, '/');
25955 else
25956 SET_GLYPH_FROM_CHAR (glyph, '\\');
25957 if (it->dp
25958 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25959 {
25960 /* FIXME: Should we mirror GC for R2L lines? */
25961 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25962 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25963 }
25964 }
25965 else if (what == IT_TRUNCATION)
25966 {
25967 /* Truncation glyph. */
25968 SET_GLYPH_FROM_CHAR (glyph, '$');
25969 if (it->dp
25970 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25971 {
25972 /* FIXME: Should we mirror GC for R2L lines? */
25973 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25974 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25975 }
25976 }
25977 else
25978 emacs_abort ();
25979
25980 #ifdef HAVE_WINDOW_SYSTEM
25981 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25982 is turned off, we precede the truncation/continuation glyphs by a
25983 stretch glyph whose width is computed such that these special
25984 glyphs are aligned at the window margin, even when very different
25985 fonts are used in different glyph rows. */
25986 if (FRAME_WINDOW_P (temp_it.f)
25987 /* init_iterator calls this with it->glyph_row == NULL, and it
25988 wants only the pixel width of the truncation/continuation
25989 glyphs. */
25990 && temp_it.glyph_row
25991 /* insert_left_trunc_glyphs calls us at the beginning of the
25992 row, and it has its own calculation of the stretch glyph
25993 width. */
25994 && temp_it.glyph_row->used[TEXT_AREA] > 0
25995 && (temp_it.glyph_row->reversed_p
25996 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25997 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25998 {
25999 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26000
26001 if (stretch_width > 0)
26002 {
26003 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26004 struct font *font =
26005 face->font ? face->font : FRAME_FONT (temp_it.f);
26006 int stretch_ascent =
26007 (((temp_it.ascent + temp_it.descent)
26008 * FONT_BASE (font)) / FONT_HEIGHT (font));
26009
26010 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
26011 temp_it.ascent + temp_it.descent,
26012 stretch_ascent);
26013 }
26014 }
26015 #endif
26016
26017 temp_it.dp = NULL;
26018 temp_it.what = IT_CHARACTER;
26019 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26020 temp_it.face_id = GLYPH_FACE (glyph);
26021 temp_it.len = CHAR_BYTES (temp_it.c);
26022
26023 PRODUCE_GLYPHS (&temp_it);
26024 it->pixel_width = temp_it.pixel_width;
26025 it->nglyphs = temp_it.nglyphs;
26026 }
26027
26028 #ifdef HAVE_WINDOW_SYSTEM
26029
26030 /* Calculate line-height and line-spacing properties.
26031 An integer value specifies explicit pixel value.
26032 A float value specifies relative value to current face height.
26033 A cons (float . face-name) specifies relative value to
26034 height of specified face font.
26035
26036 Returns height in pixels, or nil. */
26037
26038
26039 static Lisp_Object
26040 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26041 int boff, int override)
26042 {
26043 Lisp_Object face_name = Qnil;
26044 int ascent, descent, height;
26045
26046 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26047 return val;
26048
26049 if (CONSP (val))
26050 {
26051 face_name = XCAR (val);
26052 val = XCDR (val);
26053 if (!NUMBERP (val))
26054 val = make_number (1);
26055 if (NILP (face_name))
26056 {
26057 height = it->ascent + it->descent;
26058 goto scale;
26059 }
26060 }
26061
26062 if (NILP (face_name))
26063 {
26064 font = FRAME_FONT (it->f);
26065 boff = FRAME_BASELINE_OFFSET (it->f);
26066 }
26067 else if (EQ (face_name, Qt))
26068 {
26069 override = 0;
26070 }
26071 else
26072 {
26073 int face_id;
26074 struct face *face;
26075
26076 face_id = lookup_named_face (it->f, face_name, 0);
26077 if (face_id < 0)
26078 return make_number (-1);
26079
26080 face = FACE_FROM_ID (it->f, face_id);
26081 font = face->font;
26082 if (font == NULL)
26083 return make_number (-1);
26084 boff = font->baseline_offset;
26085 if (font->vertical_centering)
26086 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26087 }
26088
26089 ascent = FONT_BASE (font) + boff;
26090 descent = FONT_DESCENT (font) - boff;
26091
26092 if (override)
26093 {
26094 it->override_ascent = ascent;
26095 it->override_descent = descent;
26096 it->override_boff = boff;
26097 }
26098
26099 height = ascent + descent;
26100
26101 scale:
26102 if (FLOATP (val))
26103 height = (int)(XFLOAT_DATA (val) * height);
26104 else if (INTEGERP (val))
26105 height *= XINT (val);
26106
26107 return make_number (height);
26108 }
26109
26110
26111 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26112 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26113 and only if this is for a character for which no font was found.
26114
26115 If the display method (it->glyphless_method) is
26116 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26117 length of the acronym or the hexadecimal string, UPPER_XOFF and
26118 UPPER_YOFF are pixel offsets for the upper part of the string,
26119 LOWER_XOFF and LOWER_YOFF are for the lower part.
26120
26121 For the other display methods, LEN through LOWER_YOFF are zero. */
26122
26123 static void
26124 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26125 short upper_xoff, short upper_yoff,
26126 short lower_xoff, short lower_yoff)
26127 {
26128 struct glyph *glyph;
26129 enum glyph_row_area area = it->area;
26130
26131 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26132 if (glyph < it->glyph_row->glyphs[area + 1])
26133 {
26134 /* If the glyph row is reversed, we need to prepend the glyph
26135 rather than append it. */
26136 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26137 {
26138 struct glyph *g;
26139
26140 /* Make room for the additional glyph. */
26141 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26142 g[1] = *g;
26143 glyph = it->glyph_row->glyphs[area];
26144 }
26145 glyph->charpos = CHARPOS (it->position);
26146 glyph->object = it->object;
26147 glyph->pixel_width = it->pixel_width;
26148 glyph->ascent = it->ascent;
26149 glyph->descent = it->descent;
26150 glyph->voffset = it->voffset;
26151 glyph->type = GLYPHLESS_GLYPH;
26152 glyph->u.glyphless.method = it->glyphless_method;
26153 glyph->u.glyphless.for_no_font = for_no_font;
26154 glyph->u.glyphless.len = len;
26155 glyph->u.glyphless.ch = it->c;
26156 glyph->slice.glyphless.upper_xoff = upper_xoff;
26157 glyph->slice.glyphless.upper_yoff = upper_yoff;
26158 glyph->slice.glyphless.lower_xoff = lower_xoff;
26159 glyph->slice.glyphless.lower_yoff = lower_yoff;
26160 glyph->avoid_cursor_p = it->avoid_cursor_p;
26161 glyph->multibyte_p = it->multibyte_p;
26162 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26163 {
26164 /* In R2L rows, the left and the right box edges need to be
26165 drawn in reverse direction. */
26166 glyph->right_box_line_p = it->start_of_box_run_p;
26167 glyph->left_box_line_p = it->end_of_box_run_p;
26168 }
26169 else
26170 {
26171 glyph->left_box_line_p = it->start_of_box_run_p;
26172 glyph->right_box_line_p = it->end_of_box_run_p;
26173 }
26174 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26175 || it->phys_descent > it->descent);
26176 glyph->padding_p = 0;
26177 glyph->glyph_not_available_p = 0;
26178 glyph->face_id = face_id;
26179 glyph->font_type = FONT_TYPE_UNKNOWN;
26180 if (it->bidi_p)
26181 {
26182 glyph->resolved_level = it->bidi_it.resolved_level;
26183 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26184 glyph->bidi_type = it->bidi_it.type;
26185 }
26186 ++it->glyph_row->used[area];
26187 }
26188 else
26189 IT_EXPAND_MATRIX_WIDTH (it, area);
26190 }
26191
26192
26193 /* Produce a glyph for a glyphless character for iterator IT.
26194 IT->glyphless_method specifies which method to use for displaying
26195 the character. See the description of enum
26196 glyphless_display_method in dispextern.h for the detail.
26197
26198 FOR_NO_FONT is nonzero if and only if this is for a character for
26199 which no font was found. ACRONYM, if non-nil, is an acronym string
26200 for the character. */
26201
26202 static void
26203 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26204 {
26205 int face_id;
26206 struct face *face;
26207 struct font *font;
26208 int base_width, base_height, width, height;
26209 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26210 int len;
26211
26212 /* Get the metrics of the base font. We always refer to the current
26213 ASCII face. */
26214 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26215 font = face->font ? face->font : FRAME_FONT (it->f);
26216 it->ascent = FONT_BASE (font) + font->baseline_offset;
26217 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26218 base_height = it->ascent + it->descent;
26219 base_width = font->average_width;
26220
26221 face_id = merge_glyphless_glyph_face (it);
26222
26223 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26224 {
26225 it->pixel_width = THIN_SPACE_WIDTH;
26226 len = 0;
26227 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26228 }
26229 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26230 {
26231 width = CHAR_WIDTH (it->c);
26232 if (width == 0)
26233 width = 1;
26234 else if (width > 4)
26235 width = 4;
26236 it->pixel_width = base_width * width;
26237 len = 0;
26238 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26239 }
26240 else
26241 {
26242 char buf[7];
26243 const char *str;
26244 unsigned int code[6];
26245 int upper_len;
26246 int ascent, descent;
26247 struct font_metrics metrics_upper, metrics_lower;
26248
26249 face = FACE_FROM_ID (it->f, face_id);
26250 font = face->font ? face->font : FRAME_FONT (it->f);
26251 prepare_face_for_display (it->f, face);
26252
26253 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26254 {
26255 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26256 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26257 if (CONSP (acronym))
26258 acronym = XCAR (acronym);
26259 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26260 }
26261 else
26262 {
26263 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26264 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26265 str = buf;
26266 }
26267 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26268 code[len] = font->driver->encode_char (font, str[len]);
26269 upper_len = (len + 1) / 2;
26270 font->driver->text_extents (font, code, upper_len,
26271 &metrics_upper);
26272 font->driver->text_extents (font, code + upper_len, len - upper_len,
26273 &metrics_lower);
26274
26275
26276
26277 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26278 width = max (metrics_upper.width, metrics_lower.width) + 4;
26279 upper_xoff = upper_yoff = 2; /* the typical case */
26280 if (base_width >= width)
26281 {
26282 /* Align the upper to the left, the lower to the right. */
26283 it->pixel_width = base_width;
26284 lower_xoff = base_width - 2 - metrics_lower.width;
26285 }
26286 else
26287 {
26288 /* Center the shorter one. */
26289 it->pixel_width = width;
26290 if (metrics_upper.width >= metrics_lower.width)
26291 lower_xoff = (width - metrics_lower.width) / 2;
26292 else
26293 {
26294 /* FIXME: This code doesn't look right. It formerly was
26295 missing the "lower_xoff = 0;", which couldn't have
26296 been right since it left lower_xoff uninitialized. */
26297 lower_xoff = 0;
26298 upper_xoff = (width - metrics_upper.width) / 2;
26299 }
26300 }
26301
26302 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26303 top, bottom, and between upper and lower strings. */
26304 height = (metrics_upper.ascent + metrics_upper.descent
26305 + metrics_lower.ascent + metrics_lower.descent) + 5;
26306 /* Center vertically.
26307 H:base_height, D:base_descent
26308 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26309
26310 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26311 descent = D - H/2 + h/2;
26312 lower_yoff = descent - 2 - ld;
26313 upper_yoff = lower_yoff - la - 1 - ud; */
26314 ascent = - (it->descent - (base_height + height + 1) / 2);
26315 descent = it->descent - (base_height - height) / 2;
26316 lower_yoff = descent - 2 - metrics_lower.descent;
26317 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26318 - metrics_upper.descent);
26319 /* Don't make the height shorter than the base height. */
26320 if (height > base_height)
26321 {
26322 it->ascent = ascent;
26323 it->descent = descent;
26324 }
26325 }
26326
26327 it->phys_ascent = it->ascent;
26328 it->phys_descent = it->descent;
26329 if (it->glyph_row)
26330 append_glyphless_glyph (it, face_id, for_no_font, len,
26331 upper_xoff, upper_yoff,
26332 lower_xoff, lower_yoff);
26333 it->nglyphs = 1;
26334 take_vertical_position_into_account (it);
26335 }
26336
26337
26338 /* RIF:
26339 Produce glyphs/get display metrics for the display element IT is
26340 loaded with. See the description of struct it in dispextern.h
26341 for an overview of struct it. */
26342
26343 void
26344 x_produce_glyphs (struct it *it)
26345 {
26346 int extra_line_spacing = it->extra_line_spacing;
26347
26348 it->glyph_not_available_p = 0;
26349
26350 if (it->what == IT_CHARACTER)
26351 {
26352 XChar2b char2b;
26353 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26354 struct font *font = face->font;
26355 struct font_metrics *pcm = NULL;
26356 int boff; /* Baseline offset. */
26357
26358 if (font == NULL)
26359 {
26360 /* When no suitable font is found, display this character by
26361 the method specified in the first extra slot of
26362 Vglyphless_char_display. */
26363 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26364
26365 eassert (it->what == IT_GLYPHLESS);
26366 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26367 goto done;
26368 }
26369
26370 boff = font->baseline_offset;
26371 if (font->vertical_centering)
26372 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26373
26374 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26375 {
26376 int stretched_p;
26377
26378 it->nglyphs = 1;
26379
26380 if (it->override_ascent >= 0)
26381 {
26382 it->ascent = it->override_ascent;
26383 it->descent = it->override_descent;
26384 boff = it->override_boff;
26385 }
26386 else
26387 {
26388 it->ascent = FONT_BASE (font) + boff;
26389 it->descent = FONT_DESCENT (font) - boff;
26390 }
26391
26392 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26393 {
26394 pcm = get_per_char_metric (font, &char2b);
26395 if (pcm->width == 0
26396 && pcm->rbearing == 0 && pcm->lbearing == 0)
26397 pcm = NULL;
26398 }
26399
26400 if (pcm)
26401 {
26402 it->phys_ascent = pcm->ascent + boff;
26403 it->phys_descent = pcm->descent - boff;
26404 it->pixel_width = pcm->width;
26405 }
26406 else
26407 {
26408 it->glyph_not_available_p = 1;
26409 it->phys_ascent = it->ascent;
26410 it->phys_descent = it->descent;
26411 it->pixel_width = font->space_width;
26412 }
26413
26414 if (it->constrain_row_ascent_descent_p)
26415 {
26416 if (it->descent > it->max_descent)
26417 {
26418 it->ascent += it->descent - it->max_descent;
26419 it->descent = it->max_descent;
26420 }
26421 if (it->ascent > it->max_ascent)
26422 {
26423 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26424 it->ascent = it->max_ascent;
26425 }
26426 it->phys_ascent = min (it->phys_ascent, it->ascent);
26427 it->phys_descent = min (it->phys_descent, it->descent);
26428 extra_line_spacing = 0;
26429 }
26430
26431 /* If this is a space inside a region of text with
26432 `space-width' property, change its width. */
26433 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26434 if (stretched_p)
26435 it->pixel_width *= XFLOATINT (it->space_width);
26436
26437 /* If face has a box, add the box thickness to the character
26438 height. If character has a box line to the left and/or
26439 right, add the box line width to the character's width. */
26440 if (face->box != FACE_NO_BOX)
26441 {
26442 int thick = face->box_line_width;
26443
26444 if (thick > 0)
26445 {
26446 it->ascent += thick;
26447 it->descent += thick;
26448 }
26449 else
26450 thick = -thick;
26451
26452 if (it->start_of_box_run_p)
26453 it->pixel_width += thick;
26454 if (it->end_of_box_run_p)
26455 it->pixel_width += thick;
26456 }
26457
26458 /* If face has an overline, add the height of the overline
26459 (1 pixel) and a 1 pixel margin to the character height. */
26460 if (face->overline_p)
26461 it->ascent += overline_margin;
26462
26463 if (it->constrain_row_ascent_descent_p)
26464 {
26465 if (it->ascent > it->max_ascent)
26466 it->ascent = it->max_ascent;
26467 if (it->descent > it->max_descent)
26468 it->descent = it->max_descent;
26469 }
26470
26471 take_vertical_position_into_account (it);
26472
26473 /* If we have to actually produce glyphs, do it. */
26474 if (it->glyph_row)
26475 {
26476 if (stretched_p)
26477 {
26478 /* Translate a space with a `space-width' property
26479 into a stretch glyph. */
26480 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26481 / FONT_HEIGHT (font));
26482 append_stretch_glyph (it, it->object, it->pixel_width,
26483 it->ascent + it->descent, ascent);
26484 }
26485 else
26486 append_glyph (it);
26487
26488 /* If characters with lbearing or rbearing are displayed
26489 in this line, record that fact in a flag of the
26490 glyph row. This is used to optimize X output code. */
26491 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26492 it->glyph_row->contains_overlapping_glyphs_p = 1;
26493 }
26494 if (! stretched_p && it->pixel_width == 0)
26495 /* We assure that all visible glyphs have at least 1-pixel
26496 width. */
26497 it->pixel_width = 1;
26498 }
26499 else if (it->char_to_display == '\n')
26500 {
26501 /* A newline has no width, but we need the height of the
26502 line. But if previous part of the line sets a height,
26503 don't increase that height. */
26504
26505 Lisp_Object height;
26506 Lisp_Object total_height = Qnil;
26507
26508 it->override_ascent = -1;
26509 it->pixel_width = 0;
26510 it->nglyphs = 0;
26511
26512 height = get_it_property (it, Qline_height);
26513 /* Split (line-height total-height) list. */
26514 if (CONSP (height)
26515 && CONSP (XCDR (height))
26516 && NILP (XCDR (XCDR (height))))
26517 {
26518 total_height = XCAR (XCDR (height));
26519 height = XCAR (height);
26520 }
26521 height = calc_line_height_property (it, height, font, boff, 1);
26522
26523 if (it->override_ascent >= 0)
26524 {
26525 it->ascent = it->override_ascent;
26526 it->descent = it->override_descent;
26527 boff = it->override_boff;
26528 }
26529 else
26530 {
26531 it->ascent = FONT_BASE (font) + boff;
26532 it->descent = FONT_DESCENT (font) - boff;
26533 }
26534
26535 if (EQ (height, Qt))
26536 {
26537 if (it->descent > it->max_descent)
26538 {
26539 it->ascent += it->descent - it->max_descent;
26540 it->descent = it->max_descent;
26541 }
26542 if (it->ascent > it->max_ascent)
26543 {
26544 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26545 it->ascent = it->max_ascent;
26546 }
26547 it->phys_ascent = min (it->phys_ascent, it->ascent);
26548 it->phys_descent = min (it->phys_descent, it->descent);
26549 it->constrain_row_ascent_descent_p = 1;
26550 extra_line_spacing = 0;
26551 }
26552 else
26553 {
26554 Lisp_Object spacing;
26555
26556 it->phys_ascent = it->ascent;
26557 it->phys_descent = it->descent;
26558
26559 if ((it->max_ascent > 0 || it->max_descent > 0)
26560 && face->box != FACE_NO_BOX
26561 && face->box_line_width > 0)
26562 {
26563 it->ascent += face->box_line_width;
26564 it->descent += face->box_line_width;
26565 }
26566 if (!NILP (height)
26567 && XINT (height) > it->ascent + it->descent)
26568 it->ascent = XINT (height) - it->descent;
26569
26570 if (!NILP (total_height))
26571 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26572 else
26573 {
26574 spacing = get_it_property (it, Qline_spacing);
26575 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26576 }
26577 if (INTEGERP (spacing))
26578 {
26579 extra_line_spacing = XINT (spacing);
26580 if (!NILP (total_height))
26581 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26582 }
26583 }
26584 }
26585 else /* i.e. (it->char_to_display == '\t') */
26586 {
26587 if (font->space_width > 0)
26588 {
26589 int tab_width = it->tab_width * font->space_width;
26590 int x = it->current_x + it->continuation_lines_width;
26591 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26592
26593 /* If the distance from the current position to the next tab
26594 stop is less than a space character width, use the
26595 tab stop after that. */
26596 if (next_tab_x - x < font->space_width)
26597 next_tab_x += tab_width;
26598
26599 it->pixel_width = next_tab_x - x;
26600 it->nglyphs = 1;
26601 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26602 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26603
26604 if (it->glyph_row)
26605 {
26606 append_stretch_glyph (it, it->object, it->pixel_width,
26607 it->ascent + it->descent, it->ascent);
26608 }
26609 }
26610 else
26611 {
26612 it->pixel_width = 0;
26613 it->nglyphs = 1;
26614 }
26615 }
26616 }
26617 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26618 {
26619 /* A static composition.
26620
26621 Note: A composition is represented as one glyph in the
26622 glyph matrix. There are no padding glyphs.
26623
26624 Important note: pixel_width, ascent, and descent are the
26625 values of what is drawn by draw_glyphs (i.e. the values of
26626 the overall glyphs composed). */
26627 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26628 int boff; /* baseline offset */
26629 struct composition *cmp = composition_table[it->cmp_it.id];
26630 int glyph_len = cmp->glyph_len;
26631 struct font *font = face->font;
26632
26633 it->nglyphs = 1;
26634
26635 /* If we have not yet calculated pixel size data of glyphs of
26636 the composition for the current face font, calculate them
26637 now. Theoretically, we have to check all fonts for the
26638 glyphs, but that requires much time and memory space. So,
26639 here we check only the font of the first glyph. This may
26640 lead to incorrect display, but it's very rare, and C-l
26641 (recenter-top-bottom) can correct the display anyway. */
26642 if (! cmp->font || cmp->font != font)
26643 {
26644 /* Ascent and descent of the font of the first character
26645 of this composition (adjusted by baseline offset).
26646 Ascent and descent of overall glyphs should not be less
26647 than these, respectively. */
26648 int font_ascent, font_descent, font_height;
26649 /* Bounding box of the overall glyphs. */
26650 int leftmost, rightmost, lowest, highest;
26651 int lbearing, rbearing;
26652 int i, width, ascent, descent;
26653 int left_padded = 0, right_padded = 0;
26654 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26655 XChar2b char2b;
26656 struct font_metrics *pcm;
26657 int font_not_found_p;
26658 ptrdiff_t pos;
26659
26660 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26661 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26662 break;
26663 if (glyph_len < cmp->glyph_len)
26664 right_padded = 1;
26665 for (i = 0; i < glyph_len; i++)
26666 {
26667 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26668 break;
26669 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26670 }
26671 if (i > 0)
26672 left_padded = 1;
26673
26674 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26675 : IT_CHARPOS (*it));
26676 /* If no suitable font is found, use the default font. */
26677 font_not_found_p = font == NULL;
26678 if (font_not_found_p)
26679 {
26680 face = face->ascii_face;
26681 font = face->font;
26682 }
26683 boff = font->baseline_offset;
26684 if (font->vertical_centering)
26685 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26686 font_ascent = FONT_BASE (font) + boff;
26687 font_descent = FONT_DESCENT (font) - boff;
26688 font_height = FONT_HEIGHT (font);
26689
26690 cmp->font = font;
26691
26692 pcm = NULL;
26693 if (! font_not_found_p)
26694 {
26695 get_char_face_and_encoding (it->f, c, it->face_id,
26696 &char2b, 0);
26697 pcm = get_per_char_metric (font, &char2b);
26698 }
26699
26700 /* Initialize the bounding box. */
26701 if (pcm)
26702 {
26703 width = cmp->glyph_len > 0 ? pcm->width : 0;
26704 ascent = pcm->ascent;
26705 descent = pcm->descent;
26706 lbearing = pcm->lbearing;
26707 rbearing = pcm->rbearing;
26708 }
26709 else
26710 {
26711 width = cmp->glyph_len > 0 ? font->space_width : 0;
26712 ascent = FONT_BASE (font);
26713 descent = FONT_DESCENT (font);
26714 lbearing = 0;
26715 rbearing = width;
26716 }
26717
26718 rightmost = width;
26719 leftmost = 0;
26720 lowest = - descent + boff;
26721 highest = ascent + boff;
26722
26723 if (! font_not_found_p
26724 && font->default_ascent
26725 && CHAR_TABLE_P (Vuse_default_ascent)
26726 && !NILP (Faref (Vuse_default_ascent,
26727 make_number (it->char_to_display))))
26728 highest = font->default_ascent + boff;
26729
26730 /* Draw the first glyph at the normal position. It may be
26731 shifted to right later if some other glyphs are drawn
26732 at the left. */
26733 cmp->offsets[i * 2] = 0;
26734 cmp->offsets[i * 2 + 1] = boff;
26735 cmp->lbearing = lbearing;
26736 cmp->rbearing = rbearing;
26737
26738 /* Set cmp->offsets for the remaining glyphs. */
26739 for (i++; i < glyph_len; i++)
26740 {
26741 int left, right, btm, top;
26742 int ch = COMPOSITION_GLYPH (cmp, i);
26743 int face_id;
26744 struct face *this_face;
26745
26746 if (ch == '\t')
26747 ch = ' ';
26748 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26749 this_face = FACE_FROM_ID (it->f, face_id);
26750 font = this_face->font;
26751
26752 if (font == NULL)
26753 pcm = NULL;
26754 else
26755 {
26756 get_char_face_and_encoding (it->f, ch, face_id,
26757 &char2b, 0);
26758 pcm = get_per_char_metric (font, &char2b);
26759 }
26760 if (! pcm)
26761 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26762 else
26763 {
26764 width = pcm->width;
26765 ascent = pcm->ascent;
26766 descent = pcm->descent;
26767 lbearing = pcm->lbearing;
26768 rbearing = pcm->rbearing;
26769 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26770 {
26771 /* Relative composition with or without
26772 alternate chars. */
26773 left = (leftmost + rightmost - width) / 2;
26774 btm = - descent + boff;
26775 if (font->relative_compose
26776 && (! CHAR_TABLE_P (Vignore_relative_composition)
26777 || NILP (Faref (Vignore_relative_composition,
26778 make_number (ch)))))
26779 {
26780
26781 if (- descent >= font->relative_compose)
26782 /* One extra pixel between two glyphs. */
26783 btm = highest + 1;
26784 else if (ascent <= 0)
26785 /* One extra pixel between two glyphs. */
26786 btm = lowest - 1 - ascent - descent;
26787 }
26788 }
26789 else
26790 {
26791 /* A composition rule is specified by an integer
26792 value that encodes global and new reference
26793 points (GREF and NREF). GREF and NREF are
26794 specified by numbers as below:
26795
26796 0---1---2 -- ascent
26797 | |
26798 | |
26799 | |
26800 9--10--11 -- center
26801 | |
26802 ---3---4---5--- baseline
26803 | |
26804 6---7---8 -- descent
26805 */
26806 int rule = COMPOSITION_RULE (cmp, i);
26807 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26808
26809 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26810 grefx = gref % 3, nrefx = nref % 3;
26811 grefy = gref / 3, nrefy = nref / 3;
26812 if (xoff)
26813 xoff = font_height * (xoff - 128) / 256;
26814 if (yoff)
26815 yoff = font_height * (yoff - 128) / 256;
26816
26817 left = (leftmost
26818 + grefx * (rightmost - leftmost) / 2
26819 - nrefx * width / 2
26820 + xoff);
26821
26822 btm = ((grefy == 0 ? highest
26823 : grefy == 1 ? 0
26824 : grefy == 2 ? lowest
26825 : (highest + lowest) / 2)
26826 - (nrefy == 0 ? ascent + descent
26827 : nrefy == 1 ? descent - boff
26828 : nrefy == 2 ? 0
26829 : (ascent + descent) / 2)
26830 + yoff);
26831 }
26832
26833 cmp->offsets[i * 2] = left;
26834 cmp->offsets[i * 2 + 1] = btm + descent;
26835
26836 /* Update the bounding box of the overall glyphs. */
26837 if (width > 0)
26838 {
26839 right = left + width;
26840 if (left < leftmost)
26841 leftmost = left;
26842 if (right > rightmost)
26843 rightmost = right;
26844 }
26845 top = btm + descent + ascent;
26846 if (top > highest)
26847 highest = top;
26848 if (btm < lowest)
26849 lowest = btm;
26850
26851 if (cmp->lbearing > left + lbearing)
26852 cmp->lbearing = left + lbearing;
26853 if (cmp->rbearing < left + rbearing)
26854 cmp->rbearing = left + rbearing;
26855 }
26856 }
26857
26858 /* If there are glyphs whose x-offsets are negative,
26859 shift all glyphs to the right and make all x-offsets
26860 non-negative. */
26861 if (leftmost < 0)
26862 {
26863 for (i = 0; i < cmp->glyph_len; i++)
26864 cmp->offsets[i * 2] -= leftmost;
26865 rightmost -= leftmost;
26866 cmp->lbearing -= leftmost;
26867 cmp->rbearing -= leftmost;
26868 }
26869
26870 if (left_padded && cmp->lbearing < 0)
26871 {
26872 for (i = 0; i < cmp->glyph_len; i++)
26873 cmp->offsets[i * 2] -= cmp->lbearing;
26874 rightmost -= cmp->lbearing;
26875 cmp->rbearing -= cmp->lbearing;
26876 cmp->lbearing = 0;
26877 }
26878 if (right_padded && rightmost < cmp->rbearing)
26879 {
26880 rightmost = cmp->rbearing;
26881 }
26882
26883 cmp->pixel_width = rightmost;
26884 cmp->ascent = highest;
26885 cmp->descent = - lowest;
26886 if (cmp->ascent < font_ascent)
26887 cmp->ascent = font_ascent;
26888 if (cmp->descent < font_descent)
26889 cmp->descent = font_descent;
26890 }
26891
26892 if (it->glyph_row
26893 && (cmp->lbearing < 0
26894 || cmp->rbearing > cmp->pixel_width))
26895 it->glyph_row->contains_overlapping_glyphs_p = 1;
26896
26897 it->pixel_width = cmp->pixel_width;
26898 it->ascent = it->phys_ascent = cmp->ascent;
26899 it->descent = it->phys_descent = cmp->descent;
26900 if (face->box != FACE_NO_BOX)
26901 {
26902 int thick = face->box_line_width;
26903
26904 if (thick > 0)
26905 {
26906 it->ascent += thick;
26907 it->descent += thick;
26908 }
26909 else
26910 thick = - thick;
26911
26912 if (it->start_of_box_run_p)
26913 it->pixel_width += thick;
26914 if (it->end_of_box_run_p)
26915 it->pixel_width += thick;
26916 }
26917
26918 /* If face has an overline, add the height of the overline
26919 (1 pixel) and a 1 pixel margin to the character height. */
26920 if (face->overline_p)
26921 it->ascent += overline_margin;
26922
26923 take_vertical_position_into_account (it);
26924 if (it->ascent < 0)
26925 it->ascent = 0;
26926 if (it->descent < 0)
26927 it->descent = 0;
26928
26929 if (it->glyph_row && cmp->glyph_len > 0)
26930 append_composite_glyph (it);
26931 }
26932 else if (it->what == IT_COMPOSITION)
26933 {
26934 /* A dynamic (automatic) composition. */
26935 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26936 Lisp_Object gstring;
26937 struct font_metrics metrics;
26938
26939 it->nglyphs = 1;
26940
26941 gstring = composition_gstring_from_id (it->cmp_it.id);
26942 it->pixel_width
26943 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26944 &metrics);
26945 if (it->glyph_row
26946 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26947 it->glyph_row->contains_overlapping_glyphs_p = 1;
26948 it->ascent = it->phys_ascent = metrics.ascent;
26949 it->descent = it->phys_descent = metrics.descent;
26950 if (face->box != FACE_NO_BOX)
26951 {
26952 int thick = face->box_line_width;
26953
26954 if (thick > 0)
26955 {
26956 it->ascent += thick;
26957 it->descent += thick;
26958 }
26959 else
26960 thick = - thick;
26961
26962 if (it->start_of_box_run_p)
26963 it->pixel_width += thick;
26964 if (it->end_of_box_run_p)
26965 it->pixel_width += thick;
26966 }
26967 /* If face has an overline, add the height of the overline
26968 (1 pixel) and a 1 pixel margin to the character height. */
26969 if (face->overline_p)
26970 it->ascent += overline_margin;
26971 take_vertical_position_into_account (it);
26972 if (it->ascent < 0)
26973 it->ascent = 0;
26974 if (it->descent < 0)
26975 it->descent = 0;
26976
26977 if (it->glyph_row)
26978 append_composite_glyph (it);
26979 }
26980 else if (it->what == IT_GLYPHLESS)
26981 produce_glyphless_glyph (it, 0, Qnil);
26982 else if (it->what == IT_IMAGE)
26983 produce_image_glyph (it);
26984 else if (it->what == IT_STRETCH)
26985 produce_stretch_glyph (it);
26986
26987 done:
26988 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26989 because this isn't true for images with `:ascent 100'. */
26990 eassert (it->ascent >= 0 && it->descent >= 0);
26991 if (it->area == TEXT_AREA)
26992 it->current_x += it->pixel_width;
26993
26994 if (extra_line_spacing > 0)
26995 {
26996 it->descent += extra_line_spacing;
26997 if (extra_line_spacing > it->max_extra_line_spacing)
26998 it->max_extra_line_spacing = extra_line_spacing;
26999 }
27000
27001 it->max_ascent = max (it->max_ascent, it->ascent);
27002 it->max_descent = max (it->max_descent, it->descent);
27003 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27004 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27005 }
27006
27007 /* EXPORT for RIF:
27008 Output LEN glyphs starting at START at the nominal cursor position.
27009 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27010 being updated, and UPDATED_AREA is the area of that row being updated. */
27011
27012 void
27013 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27014 struct glyph *start, enum glyph_row_area updated_area, int len)
27015 {
27016 int x, hpos, chpos = w->phys_cursor.hpos;
27017
27018 eassert (updated_row);
27019 /* When the window is hscrolled, cursor hpos can legitimately be out
27020 of bounds, but we draw the cursor at the corresponding window
27021 margin in that case. */
27022 if (!updated_row->reversed_p && chpos < 0)
27023 chpos = 0;
27024 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27025 chpos = updated_row->used[TEXT_AREA] - 1;
27026
27027 block_input ();
27028
27029 /* Write glyphs. */
27030
27031 hpos = start - updated_row->glyphs[updated_area];
27032 x = draw_glyphs (w, w->output_cursor.x,
27033 updated_row, updated_area,
27034 hpos, hpos + len,
27035 DRAW_NORMAL_TEXT, 0);
27036
27037 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27038 if (updated_area == TEXT_AREA
27039 && w->phys_cursor_on_p
27040 && w->phys_cursor.vpos == w->output_cursor.vpos
27041 && chpos >= hpos
27042 && chpos < hpos + len)
27043 w->phys_cursor_on_p = 0;
27044
27045 unblock_input ();
27046
27047 /* Advance the output cursor. */
27048 w->output_cursor.hpos += len;
27049 w->output_cursor.x = x;
27050 }
27051
27052
27053 /* EXPORT for RIF:
27054 Insert LEN glyphs from START at the nominal cursor position. */
27055
27056 void
27057 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27058 struct glyph *start, enum glyph_row_area updated_area, int len)
27059 {
27060 struct frame *f;
27061 int line_height, shift_by_width, shifted_region_width;
27062 struct glyph_row *row;
27063 struct glyph *glyph;
27064 int frame_x, frame_y;
27065 ptrdiff_t hpos;
27066
27067 eassert (updated_row);
27068 block_input ();
27069 f = XFRAME (WINDOW_FRAME (w));
27070
27071 /* Get the height of the line we are in. */
27072 row = updated_row;
27073 line_height = row->height;
27074
27075 /* Get the width of the glyphs to insert. */
27076 shift_by_width = 0;
27077 for (glyph = start; glyph < start + len; ++glyph)
27078 shift_by_width += glyph->pixel_width;
27079
27080 /* Get the width of the region to shift right. */
27081 shifted_region_width = (window_box_width (w, updated_area)
27082 - w->output_cursor.x
27083 - shift_by_width);
27084
27085 /* Shift right. */
27086 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27087 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27088
27089 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27090 line_height, shift_by_width);
27091
27092 /* Write the glyphs. */
27093 hpos = start - row->glyphs[updated_area];
27094 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27095 hpos, hpos + len,
27096 DRAW_NORMAL_TEXT, 0);
27097
27098 /* Advance the output cursor. */
27099 w->output_cursor.hpos += len;
27100 w->output_cursor.x += shift_by_width;
27101 unblock_input ();
27102 }
27103
27104
27105 /* EXPORT for RIF:
27106 Erase the current text line from the nominal cursor position
27107 (inclusive) to pixel column TO_X (exclusive). The idea is that
27108 everything from TO_X onward is already erased.
27109
27110 TO_X is a pixel position relative to UPDATED_AREA of currently
27111 updated window W. TO_X == -1 means clear to the end of this area. */
27112
27113 void
27114 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27115 enum glyph_row_area updated_area, int to_x)
27116 {
27117 struct frame *f;
27118 int max_x, min_y, max_y;
27119 int from_x, from_y, to_y;
27120
27121 eassert (updated_row);
27122 f = XFRAME (w->frame);
27123
27124 if (updated_row->full_width_p)
27125 max_x = (WINDOW_PIXEL_WIDTH (w)
27126 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27127 else
27128 max_x = window_box_width (w, updated_area);
27129 max_y = window_text_bottom_y (w);
27130
27131 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27132 of window. For TO_X > 0, truncate to end of drawing area. */
27133 if (to_x == 0)
27134 return;
27135 else if (to_x < 0)
27136 to_x = max_x;
27137 else
27138 to_x = min (to_x, max_x);
27139
27140 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27141
27142 /* Notice if the cursor will be cleared by this operation. */
27143 if (!updated_row->full_width_p)
27144 notice_overwritten_cursor (w, updated_area,
27145 w->output_cursor.x, -1,
27146 updated_row->y,
27147 MATRIX_ROW_BOTTOM_Y (updated_row));
27148
27149 from_x = w->output_cursor.x;
27150
27151 /* Translate to frame coordinates. */
27152 if (updated_row->full_width_p)
27153 {
27154 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27155 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27156 }
27157 else
27158 {
27159 int area_left = window_box_left (w, updated_area);
27160 from_x += area_left;
27161 to_x += area_left;
27162 }
27163
27164 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27165 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27166 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27167
27168 /* Prevent inadvertently clearing to end of the X window. */
27169 if (to_x > from_x && to_y > from_y)
27170 {
27171 block_input ();
27172 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27173 to_x - from_x, to_y - from_y);
27174 unblock_input ();
27175 }
27176 }
27177
27178 #endif /* HAVE_WINDOW_SYSTEM */
27179
27180
27181 \f
27182 /***********************************************************************
27183 Cursor types
27184 ***********************************************************************/
27185
27186 /* Value is the internal representation of the specified cursor type
27187 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27188 of the bar cursor. */
27189
27190 static enum text_cursor_kinds
27191 get_specified_cursor_type (Lisp_Object arg, int *width)
27192 {
27193 enum text_cursor_kinds type;
27194
27195 if (NILP (arg))
27196 return NO_CURSOR;
27197
27198 if (EQ (arg, Qbox))
27199 return FILLED_BOX_CURSOR;
27200
27201 if (EQ (arg, Qhollow))
27202 return HOLLOW_BOX_CURSOR;
27203
27204 if (EQ (arg, Qbar))
27205 {
27206 *width = 2;
27207 return BAR_CURSOR;
27208 }
27209
27210 if (CONSP (arg)
27211 && EQ (XCAR (arg), Qbar)
27212 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27213 {
27214 *width = XINT (XCDR (arg));
27215 return BAR_CURSOR;
27216 }
27217
27218 if (EQ (arg, Qhbar))
27219 {
27220 *width = 2;
27221 return HBAR_CURSOR;
27222 }
27223
27224 if (CONSP (arg)
27225 && EQ (XCAR (arg), Qhbar)
27226 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27227 {
27228 *width = XINT (XCDR (arg));
27229 return HBAR_CURSOR;
27230 }
27231
27232 /* Treat anything unknown as "hollow box cursor".
27233 It was bad to signal an error; people have trouble fixing
27234 .Xdefaults with Emacs, when it has something bad in it. */
27235 type = HOLLOW_BOX_CURSOR;
27236
27237 return type;
27238 }
27239
27240 /* Set the default cursor types for specified frame. */
27241 void
27242 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27243 {
27244 int width = 1;
27245 Lisp_Object tem;
27246
27247 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27248 FRAME_CURSOR_WIDTH (f) = width;
27249
27250 /* By default, set up the blink-off state depending on the on-state. */
27251
27252 tem = Fassoc (arg, Vblink_cursor_alist);
27253 if (!NILP (tem))
27254 {
27255 FRAME_BLINK_OFF_CURSOR (f)
27256 = get_specified_cursor_type (XCDR (tem), &width);
27257 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27258 }
27259 else
27260 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27261
27262 /* Make sure the cursor gets redrawn. */
27263 f->cursor_type_changed = 1;
27264 }
27265
27266
27267 #ifdef HAVE_WINDOW_SYSTEM
27268
27269 /* Return the cursor we want to be displayed in window W. Return
27270 width of bar/hbar cursor through WIDTH arg. Return with
27271 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27272 (i.e. if the `system caret' should track this cursor).
27273
27274 In a mini-buffer window, we want the cursor only to appear if we
27275 are reading input from this window. For the selected window, we
27276 want the cursor type given by the frame parameter or buffer local
27277 setting of cursor-type. If explicitly marked off, draw no cursor.
27278 In all other cases, we want a hollow box cursor. */
27279
27280 static enum text_cursor_kinds
27281 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27282 int *active_cursor)
27283 {
27284 struct frame *f = XFRAME (w->frame);
27285 struct buffer *b = XBUFFER (w->contents);
27286 int cursor_type = DEFAULT_CURSOR;
27287 Lisp_Object alt_cursor;
27288 int non_selected = 0;
27289
27290 *active_cursor = 1;
27291
27292 /* Echo area */
27293 if (cursor_in_echo_area
27294 && FRAME_HAS_MINIBUF_P (f)
27295 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27296 {
27297 if (w == XWINDOW (echo_area_window))
27298 {
27299 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27300 {
27301 *width = FRAME_CURSOR_WIDTH (f);
27302 return FRAME_DESIRED_CURSOR (f);
27303 }
27304 else
27305 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27306 }
27307
27308 *active_cursor = 0;
27309 non_selected = 1;
27310 }
27311
27312 /* Detect a nonselected window or nonselected frame. */
27313 else if (w != XWINDOW (f->selected_window)
27314 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27315 {
27316 *active_cursor = 0;
27317
27318 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27319 return NO_CURSOR;
27320
27321 non_selected = 1;
27322 }
27323
27324 /* Never display a cursor in a window in which cursor-type is nil. */
27325 if (NILP (BVAR (b, cursor_type)))
27326 return NO_CURSOR;
27327
27328 /* Get the normal cursor type for this window. */
27329 if (EQ (BVAR (b, cursor_type), Qt))
27330 {
27331 cursor_type = FRAME_DESIRED_CURSOR (f);
27332 *width = FRAME_CURSOR_WIDTH (f);
27333 }
27334 else
27335 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27336
27337 /* Use cursor-in-non-selected-windows instead
27338 for non-selected window or frame. */
27339 if (non_selected)
27340 {
27341 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27342 if (!EQ (Qt, alt_cursor))
27343 return get_specified_cursor_type (alt_cursor, width);
27344 /* t means modify the normal cursor type. */
27345 if (cursor_type == FILLED_BOX_CURSOR)
27346 cursor_type = HOLLOW_BOX_CURSOR;
27347 else if (cursor_type == BAR_CURSOR && *width > 1)
27348 --*width;
27349 return cursor_type;
27350 }
27351
27352 /* Use normal cursor if not blinked off. */
27353 if (!w->cursor_off_p)
27354 {
27355 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27356 {
27357 if (cursor_type == FILLED_BOX_CURSOR)
27358 {
27359 /* Using a block cursor on large images can be very annoying.
27360 So use a hollow cursor for "large" images.
27361 If image is not transparent (no mask), also use hollow cursor. */
27362 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27363 if (img != NULL && IMAGEP (img->spec))
27364 {
27365 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27366 where N = size of default frame font size.
27367 This should cover most of the "tiny" icons people may use. */
27368 if (!img->mask
27369 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27370 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27371 cursor_type = HOLLOW_BOX_CURSOR;
27372 }
27373 }
27374 else if (cursor_type != NO_CURSOR)
27375 {
27376 /* Display current only supports BOX and HOLLOW cursors for images.
27377 So for now, unconditionally use a HOLLOW cursor when cursor is
27378 not a solid box cursor. */
27379 cursor_type = HOLLOW_BOX_CURSOR;
27380 }
27381 }
27382 return cursor_type;
27383 }
27384
27385 /* Cursor is blinked off, so determine how to "toggle" it. */
27386
27387 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27388 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27389 return get_specified_cursor_type (XCDR (alt_cursor), width);
27390
27391 /* Then see if frame has specified a specific blink off cursor type. */
27392 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27393 {
27394 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27395 return FRAME_BLINK_OFF_CURSOR (f);
27396 }
27397
27398 #if 0
27399 /* Some people liked having a permanently visible blinking cursor,
27400 while others had very strong opinions against it. So it was
27401 decided to remove it. KFS 2003-09-03 */
27402
27403 /* Finally perform built-in cursor blinking:
27404 filled box <-> hollow box
27405 wide [h]bar <-> narrow [h]bar
27406 narrow [h]bar <-> no cursor
27407 other type <-> no cursor */
27408
27409 if (cursor_type == FILLED_BOX_CURSOR)
27410 return HOLLOW_BOX_CURSOR;
27411
27412 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27413 {
27414 *width = 1;
27415 return cursor_type;
27416 }
27417 #endif
27418
27419 return NO_CURSOR;
27420 }
27421
27422
27423 /* Notice when the text cursor of window W has been completely
27424 overwritten by a drawing operation that outputs glyphs in AREA
27425 starting at X0 and ending at X1 in the line starting at Y0 and
27426 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27427 the rest of the line after X0 has been written. Y coordinates
27428 are window-relative. */
27429
27430 static void
27431 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27432 int x0, int x1, int y0, int y1)
27433 {
27434 int cx0, cx1, cy0, cy1;
27435 struct glyph_row *row;
27436
27437 if (!w->phys_cursor_on_p)
27438 return;
27439 if (area != TEXT_AREA)
27440 return;
27441
27442 if (w->phys_cursor.vpos < 0
27443 || w->phys_cursor.vpos >= w->current_matrix->nrows
27444 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27445 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27446 return;
27447
27448 if (row->cursor_in_fringe_p)
27449 {
27450 row->cursor_in_fringe_p = 0;
27451 draw_fringe_bitmap (w, row, row->reversed_p);
27452 w->phys_cursor_on_p = 0;
27453 return;
27454 }
27455
27456 cx0 = w->phys_cursor.x;
27457 cx1 = cx0 + w->phys_cursor_width;
27458 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27459 return;
27460
27461 /* The cursor image will be completely removed from the
27462 screen if the output area intersects the cursor area in
27463 y-direction. When we draw in [y0 y1[, and some part of
27464 the cursor is at y < y0, that part must have been drawn
27465 before. When scrolling, the cursor is erased before
27466 actually scrolling, so we don't come here. When not
27467 scrolling, the rows above the old cursor row must have
27468 changed, and in this case these rows must have written
27469 over the cursor image.
27470
27471 Likewise if part of the cursor is below y1, with the
27472 exception of the cursor being in the first blank row at
27473 the buffer and window end because update_text_area
27474 doesn't draw that row. (Except when it does, but
27475 that's handled in update_text_area.) */
27476
27477 cy0 = w->phys_cursor.y;
27478 cy1 = cy0 + w->phys_cursor_height;
27479 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27480 return;
27481
27482 w->phys_cursor_on_p = 0;
27483 }
27484
27485 #endif /* HAVE_WINDOW_SYSTEM */
27486
27487 \f
27488 /************************************************************************
27489 Mouse Face
27490 ************************************************************************/
27491
27492 #ifdef HAVE_WINDOW_SYSTEM
27493
27494 /* EXPORT for RIF:
27495 Fix the display of area AREA of overlapping row ROW in window W
27496 with respect to the overlapping part OVERLAPS. */
27497
27498 void
27499 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27500 enum glyph_row_area area, int overlaps)
27501 {
27502 int i, x;
27503
27504 block_input ();
27505
27506 x = 0;
27507 for (i = 0; i < row->used[area];)
27508 {
27509 if (row->glyphs[area][i].overlaps_vertically_p)
27510 {
27511 int start = i, start_x = x;
27512
27513 do
27514 {
27515 x += row->glyphs[area][i].pixel_width;
27516 ++i;
27517 }
27518 while (i < row->used[area]
27519 && row->glyphs[area][i].overlaps_vertically_p);
27520
27521 draw_glyphs (w, start_x, row, area,
27522 start, i,
27523 DRAW_NORMAL_TEXT, overlaps);
27524 }
27525 else
27526 {
27527 x += row->glyphs[area][i].pixel_width;
27528 ++i;
27529 }
27530 }
27531
27532 unblock_input ();
27533 }
27534
27535
27536 /* EXPORT:
27537 Draw the cursor glyph of window W in glyph row ROW. See the
27538 comment of draw_glyphs for the meaning of HL. */
27539
27540 void
27541 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27542 enum draw_glyphs_face hl)
27543 {
27544 /* If cursor hpos is out of bounds, don't draw garbage. This can
27545 happen in mini-buffer windows when switching between echo area
27546 glyphs and mini-buffer. */
27547 if ((row->reversed_p
27548 ? (w->phys_cursor.hpos >= 0)
27549 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27550 {
27551 int on_p = w->phys_cursor_on_p;
27552 int x1;
27553 int hpos = w->phys_cursor.hpos;
27554
27555 /* When the window is hscrolled, cursor hpos can legitimately be
27556 out of bounds, but we draw the cursor at the corresponding
27557 window margin in that case. */
27558 if (!row->reversed_p && hpos < 0)
27559 hpos = 0;
27560 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27561 hpos = row->used[TEXT_AREA] - 1;
27562
27563 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27564 hl, 0);
27565 w->phys_cursor_on_p = on_p;
27566
27567 if (hl == DRAW_CURSOR)
27568 w->phys_cursor_width = x1 - w->phys_cursor.x;
27569 /* When we erase the cursor, and ROW is overlapped by other
27570 rows, make sure that these overlapping parts of other rows
27571 are redrawn. */
27572 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27573 {
27574 w->phys_cursor_width = x1 - w->phys_cursor.x;
27575
27576 if (row > w->current_matrix->rows
27577 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27578 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27579 OVERLAPS_ERASED_CURSOR);
27580
27581 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27582 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27583 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27584 OVERLAPS_ERASED_CURSOR);
27585 }
27586 }
27587 }
27588
27589
27590 /* Erase the image of a cursor of window W from the screen. */
27591
27592 void
27593 erase_phys_cursor (struct window *w)
27594 {
27595 struct frame *f = XFRAME (w->frame);
27596 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27597 int hpos = w->phys_cursor.hpos;
27598 int vpos = w->phys_cursor.vpos;
27599 int mouse_face_here_p = 0;
27600 struct glyph_matrix *active_glyphs = w->current_matrix;
27601 struct glyph_row *cursor_row;
27602 struct glyph *cursor_glyph;
27603 enum draw_glyphs_face hl;
27604
27605 /* No cursor displayed or row invalidated => nothing to do on the
27606 screen. */
27607 if (w->phys_cursor_type == NO_CURSOR)
27608 goto mark_cursor_off;
27609
27610 /* VPOS >= active_glyphs->nrows means that window has been resized.
27611 Don't bother to erase the cursor. */
27612 if (vpos >= active_glyphs->nrows)
27613 goto mark_cursor_off;
27614
27615 /* If row containing cursor is marked invalid, there is nothing we
27616 can do. */
27617 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27618 if (!cursor_row->enabled_p)
27619 goto mark_cursor_off;
27620
27621 /* If line spacing is > 0, old cursor may only be partially visible in
27622 window after split-window. So adjust visible height. */
27623 cursor_row->visible_height = min (cursor_row->visible_height,
27624 window_text_bottom_y (w) - cursor_row->y);
27625
27626 /* If row is completely invisible, don't attempt to delete a cursor which
27627 isn't there. This can happen if cursor is at top of a window, and
27628 we switch to a buffer with a header line in that window. */
27629 if (cursor_row->visible_height <= 0)
27630 goto mark_cursor_off;
27631
27632 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27633 if (cursor_row->cursor_in_fringe_p)
27634 {
27635 cursor_row->cursor_in_fringe_p = 0;
27636 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27637 goto mark_cursor_off;
27638 }
27639
27640 /* This can happen when the new row is shorter than the old one.
27641 In this case, either draw_glyphs or clear_end_of_line
27642 should have cleared the cursor. Note that we wouldn't be
27643 able to erase the cursor in this case because we don't have a
27644 cursor glyph at hand. */
27645 if ((cursor_row->reversed_p
27646 ? (w->phys_cursor.hpos < 0)
27647 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27648 goto mark_cursor_off;
27649
27650 /* When the window is hscrolled, cursor hpos can legitimately be out
27651 of bounds, but we draw the cursor at the corresponding window
27652 margin in that case. */
27653 if (!cursor_row->reversed_p && hpos < 0)
27654 hpos = 0;
27655 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27656 hpos = cursor_row->used[TEXT_AREA] - 1;
27657
27658 /* If the cursor is in the mouse face area, redisplay that when
27659 we clear the cursor. */
27660 if (! NILP (hlinfo->mouse_face_window)
27661 && coords_in_mouse_face_p (w, hpos, vpos)
27662 /* Don't redraw the cursor's spot in mouse face if it is at the
27663 end of a line (on a newline). The cursor appears there, but
27664 mouse highlighting does not. */
27665 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27666 mouse_face_here_p = 1;
27667
27668 /* Maybe clear the display under the cursor. */
27669 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27670 {
27671 int x, y;
27672 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27673 int width;
27674
27675 cursor_glyph = get_phys_cursor_glyph (w);
27676 if (cursor_glyph == NULL)
27677 goto mark_cursor_off;
27678
27679 width = cursor_glyph->pixel_width;
27680 x = w->phys_cursor.x;
27681 if (x < 0)
27682 {
27683 width += x;
27684 x = 0;
27685 }
27686 width = min (width, window_box_width (w, TEXT_AREA) - x);
27687 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27688 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27689
27690 if (width > 0)
27691 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27692 }
27693
27694 /* Erase the cursor by redrawing the character underneath it. */
27695 if (mouse_face_here_p)
27696 hl = DRAW_MOUSE_FACE;
27697 else
27698 hl = DRAW_NORMAL_TEXT;
27699 draw_phys_cursor_glyph (w, cursor_row, hl);
27700
27701 mark_cursor_off:
27702 w->phys_cursor_on_p = 0;
27703 w->phys_cursor_type = NO_CURSOR;
27704 }
27705
27706
27707 /* EXPORT:
27708 Display or clear cursor of window W. If ON is zero, clear the
27709 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27710 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27711
27712 void
27713 display_and_set_cursor (struct window *w, bool on,
27714 int hpos, int vpos, int x, int y)
27715 {
27716 struct frame *f = XFRAME (w->frame);
27717 int new_cursor_type;
27718 int new_cursor_width;
27719 int active_cursor;
27720 struct glyph_row *glyph_row;
27721 struct glyph *glyph;
27722
27723 /* This is pointless on invisible frames, and dangerous on garbaged
27724 windows and frames; in the latter case, the frame or window may
27725 be in the midst of changing its size, and x and y may be off the
27726 window. */
27727 if (! FRAME_VISIBLE_P (f)
27728 || FRAME_GARBAGED_P (f)
27729 || vpos >= w->current_matrix->nrows
27730 || hpos >= w->current_matrix->matrix_w)
27731 return;
27732
27733 /* If cursor is off and we want it off, return quickly. */
27734 if (!on && !w->phys_cursor_on_p)
27735 return;
27736
27737 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27738 /* If cursor row is not enabled, we don't really know where to
27739 display the cursor. */
27740 if (!glyph_row->enabled_p)
27741 {
27742 w->phys_cursor_on_p = 0;
27743 return;
27744 }
27745
27746 glyph = NULL;
27747 if (!glyph_row->exact_window_width_line_p
27748 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27749 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27750
27751 eassert (input_blocked_p ());
27752
27753 /* Set new_cursor_type to the cursor we want to be displayed. */
27754 new_cursor_type = get_window_cursor_type (w, glyph,
27755 &new_cursor_width, &active_cursor);
27756
27757 /* If cursor is currently being shown and we don't want it to be or
27758 it is in the wrong place, or the cursor type is not what we want,
27759 erase it. */
27760 if (w->phys_cursor_on_p
27761 && (!on
27762 || w->phys_cursor.x != x
27763 || w->phys_cursor.y != y
27764 /* HPOS can be negative in R2L rows whose
27765 exact_window_width_line_p flag is set (i.e. their newline
27766 would "overflow into the fringe"). */
27767 || hpos < 0
27768 || new_cursor_type != w->phys_cursor_type
27769 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27770 && new_cursor_width != w->phys_cursor_width)))
27771 erase_phys_cursor (w);
27772
27773 /* Don't check phys_cursor_on_p here because that flag is only set
27774 to zero in some cases where we know that the cursor has been
27775 completely erased, to avoid the extra work of erasing the cursor
27776 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27777 still not be visible, or it has only been partly erased. */
27778 if (on)
27779 {
27780 w->phys_cursor_ascent = glyph_row->ascent;
27781 w->phys_cursor_height = glyph_row->height;
27782
27783 /* Set phys_cursor_.* before x_draw_.* is called because some
27784 of them may need the information. */
27785 w->phys_cursor.x = x;
27786 w->phys_cursor.y = glyph_row->y;
27787 w->phys_cursor.hpos = hpos;
27788 w->phys_cursor.vpos = vpos;
27789 }
27790
27791 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27792 new_cursor_type, new_cursor_width,
27793 on, active_cursor);
27794 }
27795
27796
27797 /* Switch the display of W's cursor on or off, according to the value
27798 of ON. */
27799
27800 static void
27801 update_window_cursor (struct window *w, bool on)
27802 {
27803 /* Don't update cursor in windows whose frame is in the process
27804 of being deleted. */
27805 if (w->current_matrix)
27806 {
27807 int hpos = w->phys_cursor.hpos;
27808 int vpos = w->phys_cursor.vpos;
27809 struct glyph_row *row;
27810
27811 if (vpos >= w->current_matrix->nrows
27812 || hpos >= w->current_matrix->matrix_w)
27813 return;
27814
27815 row = MATRIX_ROW (w->current_matrix, vpos);
27816
27817 /* When the window is hscrolled, cursor hpos can legitimately be
27818 out of bounds, but we draw the cursor at the corresponding
27819 window margin in that case. */
27820 if (!row->reversed_p && hpos < 0)
27821 hpos = 0;
27822 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27823 hpos = row->used[TEXT_AREA] - 1;
27824
27825 block_input ();
27826 display_and_set_cursor (w, on, hpos, vpos,
27827 w->phys_cursor.x, w->phys_cursor.y);
27828 unblock_input ();
27829 }
27830 }
27831
27832
27833 /* Call update_window_cursor with parameter ON_P on all leaf windows
27834 in the window tree rooted at W. */
27835
27836 static void
27837 update_cursor_in_window_tree (struct window *w, bool on_p)
27838 {
27839 while (w)
27840 {
27841 if (WINDOWP (w->contents))
27842 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27843 else
27844 update_window_cursor (w, on_p);
27845
27846 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27847 }
27848 }
27849
27850
27851 /* EXPORT:
27852 Display the cursor on window W, or clear it, according to ON_P.
27853 Don't change the cursor's position. */
27854
27855 void
27856 x_update_cursor (struct frame *f, bool on_p)
27857 {
27858 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27859 }
27860
27861
27862 /* EXPORT:
27863 Clear the cursor of window W to background color, and mark the
27864 cursor as not shown. This is used when the text where the cursor
27865 is about to be rewritten. */
27866
27867 void
27868 x_clear_cursor (struct window *w)
27869 {
27870 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27871 update_window_cursor (w, 0);
27872 }
27873
27874 #endif /* HAVE_WINDOW_SYSTEM */
27875
27876 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27877 and MSDOS. */
27878 static void
27879 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27880 int start_hpos, int end_hpos,
27881 enum draw_glyphs_face draw)
27882 {
27883 #ifdef HAVE_WINDOW_SYSTEM
27884 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27885 {
27886 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27887 return;
27888 }
27889 #endif
27890 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27891 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27892 #endif
27893 }
27894
27895 /* Display the active region described by mouse_face_* according to DRAW. */
27896
27897 static void
27898 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27899 {
27900 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27901 struct frame *f = XFRAME (WINDOW_FRAME (w));
27902
27903 if (/* If window is in the process of being destroyed, don't bother
27904 to do anything. */
27905 w->current_matrix != NULL
27906 /* Don't update mouse highlight if hidden. */
27907 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27908 /* Recognize when we are called to operate on rows that don't exist
27909 anymore. This can happen when a window is split. */
27910 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27911 {
27912 int phys_cursor_on_p = w->phys_cursor_on_p;
27913 struct glyph_row *row, *first, *last;
27914
27915 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27916 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27917
27918 for (row = first; row <= last && row->enabled_p; ++row)
27919 {
27920 int start_hpos, end_hpos, start_x;
27921
27922 /* For all but the first row, the highlight starts at column 0. */
27923 if (row == first)
27924 {
27925 /* R2L rows have BEG and END in reversed order, but the
27926 screen drawing geometry is always left to right. So
27927 we need to mirror the beginning and end of the
27928 highlighted area in R2L rows. */
27929 if (!row->reversed_p)
27930 {
27931 start_hpos = hlinfo->mouse_face_beg_col;
27932 start_x = hlinfo->mouse_face_beg_x;
27933 }
27934 else if (row == last)
27935 {
27936 start_hpos = hlinfo->mouse_face_end_col;
27937 start_x = hlinfo->mouse_face_end_x;
27938 }
27939 else
27940 {
27941 start_hpos = 0;
27942 start_x = 0;
27943 }
27944 }
27945 else if (row->reversed_p && row == last)
27946 {
27947 start_hpos = hlinfo->mouse_face_end_col;
27948 start_x = hlinfo->mouse_face_end_x;
27949 }
27950 else
27951 {
27952 start_hpos = 0;
27953 start_x = 0;
27954 }
27955
27956 if (row == last)
27957 {
27958 if (!row->reversed_p)
27959 end_hpos = hlinfo->mouse_face_end_col;
27960 else if (row == first)
27961 end_hpos = hlinfo->mouse_face_beg_col;
27962 else
27963 {
27964 end_hpos = row->used[TEXT_AREA];
27965 if (draw == DRAW_NORMAL_TEXT)
27966 row->fill_line_p = 1; /* Clear to end of line */
27967 }
27968 }
27969 else if (row->reversed_p && row == first)
27970 end_hpos = hlinfo->mouse_face_beg_col;
27971 else
27972 {
27973 end_hpos = row->used[TEXT_AREA];
27974 if (draw == DRAW_NORMAL_TEXT)
27975 row->fill_line_p = 1; /* Clear to end of line */
27976 }
27977
27978 if (end_hpos > start_hpos)
27979 {
27980 draw_row_with_mouse_face (w, start_x, row,
27981 start_hpos, end_hpos, draw);
27982
27983 row->mouse_face_p
27984 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27985 }
27986 }
27987
27988 #ifdef HAVE_WINDOW_SYSTEM
27989 /* When we've written over the cursor, arrange for it to
27990 be displayed again. */
27991 if (FRAME_WINDOW_P (f)
27992 && phys_cursor_on_p && !w->phys_cursor_on_p)
27993 {
27994 int hpos = w->phys_cursor.hpos;
27995
27996 /* When the window is hscrolled, cursor hpos can legitimately be
27997 out of bounds, but we draw the cursor at the corresponding
27998 window margin in that case. */
27999 if (!row->reversed_p && hpos < 0)
28000 hpos = 0;
28001 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28002 hpos = row->used[TEXT_AREA] - 1;
28003
28004 block_input ();
28005 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28006 w->phys_cursor.x, w->phys_cursor.y);
28007 unblock_input ();
28008 }
28009 #endif /* HAVE_WINDOW_SYSTEM */
28010 }
28011
28012 #ifdef HAVE_WINDOW_SYSTEM
28013 /* Change the mouse cursor. */
28014 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28015 {
28016 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28017 if (draw == DRAW_NORMAL_TEXT
28018 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28019 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28020 else
28021 #endif
28022 if (draw == DRAW_MOUSE_FACE)
28023 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28024 else
28025 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28026 }
28027 #endif /* HAVE_WINDOW_SYSTEM */
28028 }
28029
28030 /* EXPORT:
28031 Clear out the mouse-highlighted active region.
28032 Redraw it un-highlighted first. Value is non-zero if mouse
28033 face was actually drawn unhighlighted. */
28034
28035 int
28036 clear_mouse_face (Mouse_HLInfo *hlinfo)
28037 {
28038 int cleared = 0;
28039
28040 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28041 {
28042 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28043 cleared = 1;
28044 }
28045
28046 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28047 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28048 hlinfo->mouse_face_window = Qnil;
28049 hlinfo->mouse_face_overlay = Qnil;
28050 return cleared;
28051 }
28052
28053 /* Return true if the coordinates HPOS and VPOS on windows W are
28054 within the mouse face on that window. */
28055 static bool
28056 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28057 {
28058 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28059
28060 /* Quickly resolve the easy cases. */
28061 if (!(WINDOWP (hlinfo->mouse_face_window)
28062 && XWINDOW (hlinfo->mouse_face_window) == w))
28063 return false;
28064 if (vpos < hlinfo->mouse_face_beg_row
28065 || vpos > hlinfo->mouse_face_end_row)
28066 return false;
28067 if (vpos > hlinfo->mouse_face_beg_row
28068 && vpos < hlinfo->mouse_face_end_row)
28069 return true;
28070
28071 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28072 {
28073 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28074 {
28075 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28076 return true;
28077 }
28078 else if ((vpos == hlinfo->mouse_face_beg_row
28079 && hpos >= hlinfo->mouse_face_beg_col)
28080 || (vpos == hlinfo->mouse_face_end_row
28081 && hpos < hlinfo->mouse_face_end_col))
28082 return true;
28083 }
28084 else
28085 {
28086 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28087 {
28088 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28089 return true;
28090 }
28091 else if ((vpos == hlinfo->mouse_face_beg_row
28092 && hpos <= hlinfo->mouse_face_beg_col)
28093 || (vpos == hlinfo->mouse_face_end_row
28094 && hpos > hlinfo->mouse_face_end_col))
28095 return true;
28096 }
28097 return false;
28098 }
28099
28100
28101 /* EXPORT:
28102 True if physical cursor of window W is within mouse face. */
28103
28104 bool
28105 cursor_in_mouse_face_p (struct window *w)
28106 {
28107 int hpos = w->phys_cursor.hpos;
28108 int vpos = w->phys_cursor.vpos;
28109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28110
28111 /* When the window is hscrolled, cursor hpos can legitimately be out
28112 of bounds, but we draw the cursor at the corresponding window
28113 margin in that case. */
28114 if (!row->reversed_p && hpos < 0)
28115 hpos = 0;
28116 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28117 hpos = row->used[TEXT_AREA] - 1;
28118
28119 return coords_in_mouse_face_p (w, hpos, vpos);
28120 }
28121
28122
28123 \f
28124 /* Find the glyph rows START_ROW and END_ROW of window W that display
28125 characters between buffer positions START_CHARPOS and END_CHARPOS
28126 (excluding END_CHARPOS). DISP_STRING is a display string that
28127 covers these buffer positions. This is similar to
28128 row_containing_pos, but is more accurate when bidi reordering makes
28129 buffer positions change non-linearly with glyph rows. */
28130 static void
28131 rows_from_pos_range (struct window *w,
28132 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28133 Lisp_Object disp_string,
28134 struct glyph_row **start, struct glyph_row **end)
28135 {
28136 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28137 int last_y = window_text_bottom_y (w);
28138 struct glyph_row *row;
28139
28140 *start = NULL;
28141 *end = NULL;
28142
28143 while (!first->enabled_p
28144 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28145 first++;
28146
28147 /* Find the START row. */
28148 for (row = first;
28149 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28150 row++)
28151 {
28152 /* A row can potentially be the START row if the range of the
28153 characters it displays intersects the range
28154 [START_CHARPOS..END_CHARPOS). */
28155 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28156 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28157 /* See the commentary in row_containing_pos, for the
28158 explanation of the complicated way to check whether
28159 some position is beyond the end of the characters
28160 displayed by a row. */
28161 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28162 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28163 && !row->ends_at_zv_p
28164 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28165 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28166 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28167 && !row->ends_at_zv_p
28168 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28169 {
28170 /* Found a candidate row. Now make sure at least one of the
28171 glyphs it displays has a charpos from the range
28172 [START_CHARPOS..END_CHARPOS).
28173
28174 This is not obvious because bidi reordering could make
28175 buffer positions of a row be 1,2,3,102,101,100, and if we
28176 want to highlight characters in [50..60), we don't want
28177 this row, even though [50..60) does intersect [1..103),
28178 the range of character positions given by the row's start
28179 and end positions. */
28180 struct glyph *g = row->glyphs[TEXT_AREA];
28181 struct glyph *e = g + row->used[TEXT_AREA];
28182
28183 while (g < e)
28184 {
28185 if (((BUFFERP (g->object) || INTEGERP (g->object))
28186 && start_charpos <= g->charpos && g->charpos < end_charpos)
28187 /* A glyph that comes from DISP_STRING is by
28188 definition to be highlighted. */
28189 || EQ (g->object, disp_string))
28190 *start = row;
28191 g++;
28192 }
28193 if (*start)
28194 break;
28195 }
28196 }
28197
28198 /* Find the END row. */
28199 if (!*start
28200 /* If the last row is partially visible, start looking for END
28201 from that row, instead of starting from FIRST. */
28202 && !(row->enabled_p
28203 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28204 row = first;
28205 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28206 {
28207 struct glyph_row *next = row + 1;
28208 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28209
28210 if (!next->enabled_p
28211 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28212 /* The first row >= START whose range of displayed characters
28213 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28214 is the row END + 1. */
28215 || (start_charpos < next_start
28216 && end_charpos < next_start)
28217 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28218 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28219 && !next->ends_at_zv_p
28220 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28221 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28222 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28223 && !next->ends_at_zv_p
28224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28225 {
28226 *end = row;
28227 break;
28228 }
28229 else
28230 {
28231 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28232 but none of the characters it displays are in the range, it is
28233 also END + 1. */
28234 struct glyph *g = next->glyphs[TEXT_AREA];
28235 struct glyph *s = g;
28236 struct glyph *e = g + next->used[TEXT_AREA];
28237
28238 while (g < e)
28239 {
28240 if (((BUFFERP (g->object) || INTEGERP (g->object))
28241 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28242 /* If the buffer position of the first glyph in
28243 the row is equal to END_CHARPOS, it means
28244 the last character to be highlighted is the
28245 newline of ROW, and we must consider NEXT as
28246 END, not END+1. */
28247 || (((!next->reversed_p && g == s)
28248 || (next->reversed_p && g == e - 1))
28249 && (g->charpos == end_charpos
28250 /* Special case for when NEXT is an
28251 empty line at ZV. */
28252 || (g->charpos == -1
28253 && !row->ends_at_zv_p
28254 && next_start == end_charpos)))))
28255 /* A glyph that comes from DISP_STRING is by
28256 definition to be highlighted. */
28257 || EQ (g->object, disp_string))
28258 break;
28259 g++;
28260 }
28261 if (g == e)
28262 {
28263 *end = row;
28264 break;
28265 }
28266 /* The first row that ends at ZV must be the last to be
28267 highlighted. */
28268 else if (next->ends_at_zv_p)
28269 {
28270 *end = next;
28271 break;
28272 }
28273 }
28274 }
28275 }
28276
28277 /* This function sets the mouse_face_* elements of HLINFO, assuming
28278 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28279 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28280 for the overlay or run of text properties specifying the mouse
28281 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28282 before-string and after-string that must also be highlighted.
28283 DISP_STRING, if non-nil, is a display string that may cover some
28284 or all of the highlighted text. */
28285
28286 static void
28287 mouse_face_from_buffer_pos (Lisp_Object window,
28288 Mouse_HLInfo *hlinfo,
28289 ptrdiff_t mouse_charpos,
28290 ptrdiff_t start_charpos,
28291 ptrdiff_t end_charpos,
28292 Lisp_Object before_string,
28293 Lisp_Object after_string,
28294 Lisp_Object disp_string)
28295 {
28296 struct window *w = XWINDOW (window);
28297 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28298 struct glyph_row *r1, *r2;
28299 struct glyph *glyph, *end;
28300 ptrdiff_t ignore, pos;
28301 int x;
28302
28303 eassert (NILP (disp_string) || STRINGP (disp_string));
28304 eassert (NILP (before_string) || STRINGP (before_string));
28305 eassert (NILP (after_string) || STRINGP (after_string));
28306
28307 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28308 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28309 if (r1 == NULL)
28310 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28311 /* If the before-string or display-string contains newlines,
28312 rows_from_pos_range skips to its last row. Move back. */
28313 if (!NILP (before_string) || !NILP (disp_string))
28314 {
28315 struct glyph_row *prev;
28316 while ((prev = r1 - 1, prev >= first)
28317 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28318 && prev->used[TEXT_AREA] > 0)
28319 {
28320 struct glyph *beg = prev->glyphs[TEXT_AREA];
28321 glyph = beg + prev->used[TEXT_AREA];
28322 while (--glyph >= beg && INTEGERP (glyph->object));
28323 if (glyph < beg
28324 || !(EQ (glyph->object, before_string)
28325 || EQ (glyph->object, disp_string)))
28326 break;
28327 r1 = prev;
28328 }
28329 }
28330 if (r2 == NULL)
28331 {
28332 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28333 hlinfo->mouse_face_past_end = 1;
28334 }
28335 else if (!NILP (after_string))
28336 {
28337 /* If the after-string has newlines, advance to its last row. */
28338 struct glyph_row *next;
28339 struct glyph_row *last
28340 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28341
28342 for (next = r2 + 1;
28343 next <= last
28344 && next->used[TEXT_AREA] > 0
28345 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28346 ++next)
28347 r2 = next;
28348 }
28349 /* The rest of the display engine assumes that mouse_face_beg_row is
28350 either above mouse_face_end_row or identical to it. But with
28351 bidi-reordered continued lines, the row for START_CHARPOS could
28352 be below the row for END_CHARPOS. If so, swap the rows and store
28353 them in correct order. */
28354 if (r1->y > r2->y)
28355 {
28356 struct glyph_row *tem = r2;
28357
28358 r2 = r1;
28359 r1 = tem;
28360 }
28361
28362 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28363 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28364
28365 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28366 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28367 could be anywhere in the row and in any order. The strategy
28368 below is to find the leftmost and the rightmost glyph that
28369 belongs to either of these 3 strings, or whose position is
28370 between START_CHARPOS and END_CHARPOS, and highlight all the
28371 glyphs between those two. This may cover more than just the text
28372 between START_CHARPOS and END_CHARPOS if the range of characters
28373 strides the bidi level boundary, e.g. if the beginning is in R2L
28374 text while the end is in L2R text or vice versa. */
28375 if (!r1->reversed_p)
28376 {
28377 /* This row is in a left to right paragraph. Scan it left to
28378 right. */
28379 glyph = r1->glyphs[TEXT_AREA];
28380 end = glyph + r1->used[TEXT_AREA];
28381 x = r1->x;
28382
28383 /* Skip truncation glyphs at the start of the glyph row. */
28384 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28385 for (; glyph < end
28386 && INTEGERP (glyph->object)
28387 && glyph->charpos < 0;
28388 ++glyph)
28389 x += glyph->pixel_width;
28390
28391 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28392 or DISP_STRING, and the first glyph from buffer whose
28393 position is between START_CHARPOS and END_CHARPOS. */
28394 for (; glyph < end
28395 && !INTEGERP (glyph->object)
28396 && !EQ (glyph->object, disp_string)
28397 && !(BUFFERP (glyph->object)
28398 && (glyph->charpos >= start_charpos
28399 && glyph->charpos < end_charpos));
28400 ++glyph)
28401 {
28402 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28403 are present at buffer positions between START_CHARPOS and
28404 END_CHARPOS, or if they come from an overlay. */
28405 if (EQ (glyph->object, before_string))
28406 {
28407 pos = string_buffer_position (before_string,
28408 start_charpos);
28409 /* If pos == 0, it means before_string came from an
28410 overlay, not from a buffer position. */
28411 if (!pos || (pos >= start_charpos && pos < end_charpos))
28412 break;
28413 }
28414 else if (EQ (glyph->object, after_string))
28415 {
28416 pos = string_buffer_position (after_string, end_charpos);
28417 if (!pos || (pos >= start_charpos && pos < end_charpos))
28418 break;
28419 }
28420 x += glyph->pixel_width;
28421 }
28422 hlinfo->mouse_face_beg_x = x;
28423 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28424 }
28425 else
28426 {
28427 /* This row is in a right to left paragraph. Scan it right to
28428 left. */
28429 struct glyph *g;
28430
28431 end = r1->glyphs[TEXT_AREA] - 1;
28432 glyph = end + r1->used[TEXT_AREA];
28433
28434 /* Skip truncation glyphs at the start of the glyph row. */
28435 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28436 for (; glyph > end
28437 && INTEGERP (glyph->object)
28438 && glyph->charpos < 0;
28439 --glyph)
28440 ;
28441
28442 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28443 or DISP_STRING, and the first glyph from buffer whose
28444 position is between START_CHARPOS and END_CHARPOS. */
28445 for (; glyph > end
28446 && !INTEGERP (glyph->object)
28447 && !EQ (glyph->object, disp_string)
28448 && !(BUFFERP (glyph->object)
28449 && (glyph->charpos >= start_charpos
28450 && glyph->charpos < end_charpos));
28451 --glyph)
28452 {
28453 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28454 are present at buffer positions between START_CHARPOS and
28455 END_CHARPOS, or if they come from an overlay. */
28456 if (EQ (glyph->object, before_string))
28457 {
28458 pos = string_buffer_position (before_string, start_charpos);
28459 /* If pos == 0, it means before_string came from an
28460 overlay, not from a buffer position. */
28461 if (!pos || (pos >= start_charpos && pos < end_charpos))
28462 break;
28463 }
28464 else if (EQ (glyph->object, after_string))
28465 {
28466 pos = string_buffer_position (after_string, end_charpos);
28467 if (!pos || (pos >= start_charpos && pos < end_charpos))
28468 break;
28469 }
28470 }
28471
28472 glyph++; /* first glyph to the right of the highlighted area */
28473 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28474 x += g->pixel_width;
28475 hlinfo->mouse_face_beg_x = x;
28476 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28477 }
28478
28479 /* If the highlight ends in a different row, compute GLYPH and END
28480 for the end row. Otherwise, reuse the values computed above for
28481 the row where the highlight begins. */
28482 if (r2 != r1)
28483 {
28484 if (!r2->reversed_p)
28485 {
28486 glyph = r2->glyphs[TEXT_AREA];
28487 end = glyph + r2->used[TEXT_AREA];
28488 x = r2->x;
28489 }
28490 else
28491 {
28492 end = r2->glyphs[TEXT_AREA] - 1;
28493 glyph = end + r2->used[TEXT_AREA];
28494 }
28495 }
28496
28497 if (!r2->reversed_p)
28498 {
28499 /* Skip truncation and continuation glyphs near the end of the
28500 row, and also blanks and stretch glyphs inserted by
28501 extend_face_to_end_of_line. */
28502 while (end > glyph
28503 && INTEGERP ((end - 1)->object))
28504 --end;
28505 /* Scan the rest of the glyph row from the end, looking for the
28506 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28507 DISP_STRING, or whose position is between START_CHARPOS
28508 and END_CHARPOS */
28509 for (--end;
28510 end > glyph
28511 && !INTEGERP (end->object)
28512 && !EQ (end->object, disp_string)
28513 && !(BUFFERP (end->object)
28514 && (end->charpos >= start_charpos
28515 && end->charpos < end_charpos));
28516 --end)
28517 {
28518 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28519 are present at buffer positions between START_CHARPOS and
28520 END_CHARPOS, or if they come from an overlay. */
28521 if (EQ (end->object, before_string))
28522 {
28523 pos = string_buffer_position (before_string, start_charpos);
28524 if (!pos || (pos >= start_charpos && pos < end_charpos))
28525 break;
28526 }
28527 else if (EQ (end->object, after_string))
28528 {
28529 pos = string_buffer_position (after_string, end_charpos);
28530 if (!pos || (pos >= start_charpos && pos < end_charpos))
28531 break;
28532 }
28533 }
28534 /* Find the X coordinate of the last glyph to be highlighted. */
28535 for (; glyph <= end; ++glyph)
28536 x += glyph->pixel_width;
28537
28538 hlinfo->mouse_face_end_x = x;
28539 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28540 }
28541 else
28542 {
28543 /* Skip truncation and continuation glyphs near the end of the
28544 row, and also blanks and stretch glyphs inserted by
28545 extend_face_to_end_of_line. */
28546 x = r2->x;
28547 end++;
28548 while (end < glyph
28549 && INTEGERP (end->object))
28550 {
28551 x += end->pixel_width;
28552 ++end;
28553 }
28554 /* Scan the rest of the glyph row from the end, looking for the
28555 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28556 DISP_STRING, or whose position is between START_CHARPOS
28557 and END_CHARPOS */
28558 for ( ;
28559 end < glyph
28560 && !INTEGERP (end->object)
28561 && !EQ (end->object, disp_string)
28562 && !(BUFFERP (end->object)
28563 && (end->charpos >= start_charpos
28564 && end->charpos < end_charpos));
28565 ++end)
28566 {
28567 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28568 are present at buffer positions between START_CHARPOS and
28569 END_CHARPOS, or if they come from an overlay. */
28570 if (EQ (end->object, before_string))
28571 {
28572 pos = string_buffer_position (before_string, start_charpos);
28573 if (!pos || (pos >= start_charpos && pos < end_charpos))
28574 break;
28575 }
28576 else if (EQ (end->object, after_string))
28577 {
28578 pos = string_buffer_position (after_string, end_charpos);
28579 if (!pos || (pos >= start_charpos && pos < end_charpos))
28580 break;
28581 }
28582 x += end->pixel_width;
28583 }
28584 /* If we exited the above loop because we arrived at the last
28585 glyph of the row, and its buffer position is still not in
28586 range, it means the last character in range is the preceding
28587 newline. Bump the end column and x values to get past the
28588 last glyph. */
28589 if (end == glyph
28590 && BUFFERP (end->object)
28591 && (end->charpos < start_charpos
28592 || end->charpos >= end_charpos))
28593 {
28594 x += end->pixel_width;
28595 ++end;
28596 }
28597 hlinfo->mouse_face_end_x = x;
28598 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28599 }
28600
28601 hlinfo->mouse_face_window = window;
28602 hlinfo->mouse_face_face_id
28603 = face_at_buffer_position (w, mouse_charpos, &ignore,
28604 mouse_charpos + 1,
28605 !hlinfo->mouse_face_hidden, -1);
28606 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28607 }
28608
28609 /* The following function is not used anymore (replaced with
28610 mouse_face_from_string_pos), but I leave it here for the time
28611 being, in case someone would. */
28612
28613 #if 0 /* not used */
28614
28615 /* Find the position of the glyph for position POS in OBJECT in
28616 window W's current matrix, and return in *X, *Y the pixel
28617 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28618
28619 RIGHT_P non-zero means return the position of the right edge of the
28620 glyph, RIGHT_P zero means return the left edge position.
28621
28622 If no glyph for POS exists in the matrix, return the position of
28623 the glyph with the next smaller position that is in the matrix, if
28624 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28625 exists in the matrix, return the position of the glyph with the
28626 next larger position in OBJECT.
28627
28628 Value is non-zero if a glyph was found. */
28629
28630 static int
28631 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28632 int *hpos, int *vpos, int *x, int *y, int right_p)
28633 {
28634 int yb = window_text_bottom_y (w);
28635 struct glyph_row *r;
28636 struct glyph *best_glyph = NULL;
28637 struct glyph_row *best_row = NULL;
28638 int best_x = 0;
28639
28640 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28641 r->enabled_p && r->y < yb;
28642 ++r)
28643 {
28644 struct glyph *g = r->glyphs[TEXT_AREA];
28645 struct glyph *e = g + r->used[TEXT_AREA];
28646 int gx;
28647
28648 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28649 if (EQ (g->object, object))
28650 {
28651 if (g->charpos == pos)
28652 {
28653 best_glyph = g;
28654 best_x = gx;
28655 best_row = r;
28656 goto found;
28657 }
28658 else if (best_glyph == NULL
28659 || ((eabs (g->charpos - pos)
28660 < eabs (best_glyph->charpos - pos))
28661 && (right_p
28662 ? g->charpos < pos
28663 : g->charpos > pos)))
28664 {
28665 best_glyph = g;
28666 best_x = gx;
28667 best_row = r;
28668 }
28669 }
28670 }
28671
28672 found:
28673
28674 if (best_glyph)
28675 {
28676 *x = best_x;
28677 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28678
28679 if (right_p)
28680 {
28681 *x += best_glyph->pixel_width;
28682 ++*hpos;
28683 }
28684
28685 *y = best_row->y;
28686 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28687 }
28688
28689 return best_glyph != NULL;
28690 }
28691 #endif /* not used */
28692
28693 /* Find the positions of the first and the last glyphs in window W's
28694 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28695 (assumed to be a string), and return in HLINFO's mouse_face_*
28696 members the pixel and column/row coordinates of those glyphs. */
28697
28698 static void
28699 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28700 Lisp_Object object,
28701 ptrdiff_t startpos, ptrdiff_t endpos)
28702 {
28703 int yb = window_text_bottom_y (w);
28704 struct glyph_row *r;
28705 struct glyph *g, *e;
28706 int gx;
28707 int found = 0;
28708
28709 /* Find the glyph row with at least one position in the range
28710 [STARTPOS..ENDPOS), and the first glyph in that row whose
28711 position belongs to that range. */
28712 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28713 r->enabled_p && r->y < yb;
28714 ++r)
28715 {
28716 if (!r->reversed_p)
28717 {
28718 g = r->glyphs[TEXT_AREA];
28719 e = g + r->used[TEXT_AREA];
28720 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28721 if (EQ (g->object, object)
28722 && startpos <= g->charpos && g->charpos < endpos)
28723 {
28724 hlinfo->mouse_face_beg_row
28725 = MATRIX_ROW_VPOS (r, w->current_matrix);
28726 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28727 hlinfo->mouse_face_beg_x = gx;
28728 found = 1;
28729 break;
28730 }
28731 }
28732 else
28733 {
28734 struct glyph *g1;
28735
28736 e = r->glyphs[TEXT_AREA];
28737 g = e + r->used[TEXT_AREA];
28738 for ( ; g > e; --g)
28739 if (EQ ((g-1)->object, object)
28740 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28741 {
28742 hlinfo->mouse_face_beg_row
28743 = MATRIX_ROW_VPOS (r, w->current_matrix);
28744 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28745 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28746 gx += g1->pixel_width;
28747 hlinfo->mouse_face_beg_x = gx;
28748 found = 1;
28749 break;
28750 }
28751 }
28752 if (found)
28753 break;
28754 }
28755
28756 if (!found)
28757 return;
28758
28759 /* Starting with the next row, look for the first row which does NOT
28760 include any glyphs whose positions are in the range. */
28761 for (++r; r->enabled_p && r->y < yb; ++r)
28762 {
28763 g = r->glyphs[TEXT_AREA];
28764 e = g + r->used[TEXT_AREA];
28765 found = 0;
28766 for ( ; g < e; ++g)
28767 if (EQ (g->object, object)
28768 && startpos <= g->charpos && g->charpos < endpos)
28769 {
28770 found = 1;
28771 break;
28772 }
28773 if (!found)
28774 break;
28775 }
28776
28777 /* The highlighted region ends on the previous row. */
28778 r--;
28779
28780 /* Set the end row. */
28781 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28782
28783 /* Compute and set the end column and the end column's horizontal
28784 pixel coordinate. */
28785 if (!r->reversed_p)
28786 {
28787 g = r->glyphs[TEXT_AREA];
28788 e = g + r->used[TEXT_AREA];
28789 for ( ; e > g; --e)
28790 if (EQ ((e-1)->object, object)
28791 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28792 break;
28793 hlinfo->mouse_face_end_col = e - g;
28794
28795 for (gx = r->x; g < e; ++g)
28796 gx += g->pixel_width;
28797 hlinfo->mouse_face_end_x = gx;
28798 }
28799 else
28800 {
28801 e = r->glyphs[TEXT_AREA];
28802 g = e + r->used[TEXT_AREA];
28803 for (gx = r->x ; e < g; ++e)
28804 {
28805 if (EQ (e->object, object)
28806 && startpos <= e->charpos && e->charpos < endpos)
28807 break;
28808 gx += e->pixel_width;
28809 }
28810 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28811 hlinfo->mouse_face_end_x = gx;
28812 }
28813 }
28814
28815 #ifdef HAVE_WINDOW_SYSTEM
28816
28817 /* See if position X, Y is within a hot-spot of an image. */
28818
28819 static int
28820 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28821 {
28822 if (!CONSP (hot_spot))
28823 return 0;
28824
28825 if (EQ (XCAR (hot_spot), Qrect))
28826 {
28827 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28828 Lisp_Object rect = XCDR (hot_spot);
28829 Lisp_Object tem;
28830 if (!CONSP (rect))
28831 return 0;
28832 if (!CONSP (XCAR (rect)))
28833 return 0;
28834 if (!CONSP (XCDR (rect)))
28835 return 0;
28836 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28837 return 0;
28838 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28839 return 0;
28840 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28841 return 0;
28842 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28843 return 0;
28844 return 1;
28845 }
28846 else if (EQ (XCAR (hot_spot), Qcircle))
28847 {
28848 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28849 Lisp_Object circ = XCDR (hot_spot);
28850 Lisp_Object lr, lx0, ly0;
28851 if (CONSP (circ)
28852 && CONSP (XCAR (circ))
28853 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28854 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28855 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28856 {
28857 double r = XFLOATINT (lr);
28858 double dx = XINT (lx0) - x;
28859 double dy = XINT (ly0) - y;
28860 return (dx * dx + dy * dy <= r * r);
28861 }
28862 }
28863 else if (EQ (XCAR (hot_spot), Qpoly))
28864 {
28865 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28866 if (VECTORP (XCDR (hot_spot)))
28867 {
28868 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28869 Lisp_Object *poly = v->contents;
28870 ptrdiff_t n = v->header.size;
28871 ptrdiff_t i;
28872 int inside = 0;
28873 Lisp_Object lx, ly;
28874 int x0, y0;
28875
28876 /* Need an even number of coordinates, and at least 3 edges. */
28877 if (n < 6 || n & 1)
28878 return 0;
28879
28880 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28881 If count is odd, we are inside polygon. Pixels on edges
28882 may or may not be included depending on actual geometry of the
28883 polygon. */
28884 if ((lx = poly[n-2], !INTEGERP (lx))
28885 || (ly = poly[n-1], !INTEGERP (lx)))
28886 return 0;
28887 x0 = XINT (lx), y0 = XINT (ly);
28888 for (i = 0; i < n; i += 2)
28889 {
28890 int x1 = x0, y1 = y0;
28891 if ((lx = poly[i], !INTEGERP (lx))
28892 || (ly = poly[i+1], !INTEGERP (ly)))
28893 return 0;
28894 x0 = XINT (lx), y0 = XINT (ly);
28895
28896 /* Does this segment cross the X line? */
28897 if (x0 >= x)
28898 {
28899 if (x1 >= x)
28900 continue;
28901 }
28902 else if (x1 < x)
28903 continue;
28904 if (y > y0 && y > y1)
28905 continue;
28906 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28907 inside = !inside;
28908 }
28909 return inside;
28910 }
28911 }
28912 return 0;
28913 }
28914
28915 Lisp_Object
28916 find_hot_spot (Lisp_Object map, int x, int y)
28917 {
28918 while (CONSP (map))
28919 {
28920 if (CONSP (XCAR (map))
28921 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28922 return XCAR (map);
28923 map = XCDR (map);
28924 }
28925
28926 return Qnil;
28927 }
28928
28929 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28930 3, 3, 0,
28931 doc: /* Lookup in image map MAP coordinates X and Y.
28932 An image map is an alist where each element has the format (AREA ID PLIST).
28933 An AREA is specified as either a rectangle, a circle, or a polygon:
28934 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28935 pixel coordinates of the upper left and bottom right corners.
28936 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28937 and the radius of the circle; r may be a float or integer.
28938 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28939 vector describes one corner in the polygon.
28940 Returns the alist element for the first matching AREA in MAP. */)
28941 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28942 {
28943 if (NILP (map))
28944 return Qnil;
28945
28946 CHECK_NUMBER (x);
28947 CHECK_NUMBER (y);
28948
28949 return find_hot_spot (map,
28950 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28951 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28952 }
28953
28954
28955 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28956 static void
28957 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28958 {
28959 /* Do not change cursor shape while dragging mouse. */
28960 if (!NILP (do_mouse_tracking))
28961 return;
28962
28963 if (!NILP (pointer))
28964 {
28965 if (EQ (pointer, Qarrow))
28966 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28967 else if (EQ (pointer, Qhand))
28968 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28969 else if (EQ (pointer, Qtext))
28970 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28971 else if (EQ (pointer, intern ("hdrag")))
28972 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28973 else if (EQ (pointer, intern ("nhdrag")))
28974 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28975 #ifdef HAVE_X_WINDOWS
28976 else if (EQ (pointer, intern ("vdrag")))
28977 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28978 #endif
28979 else if (EQ (pointer, intern ("hourglass")))
28980 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28981 else if (EQ (pointer, Qmodeline))
28982 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28983 else
28984 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28985 }
28986
28987 if (cursor != No_Cursor)
28988 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28989 }
28990
28991 #endif /* HAVE_WINDOW_SYSTEM */
28992
28993 /* Take proper action when mouse has moved to the mode or header line
28994 or marginal area AREA of window W, x-position X and y-position Y.
28995 X is relative to the start of the text display area of W, so the
28996 width of bitmap areas and scroll bars must be subtracted to get a
28997 position relative to the start of the mode line. */
28998
28999 static void
29000 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29001 enum window_part area)
29002 {
29003 struct window *w = XWINDOW (window);
29004 struct frame *f = XFRAME (w->frame);
29005 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29006 #ifdef HAVE_WINDOW_SYSTEM
29007 Display_Info *dpyinfo;
29008 #endif
29009 Cursor cursor = No_Cursor;
29010 Lisp_Object pointer = Qnil;
29011 int dx, dy, width, height;
29012 ptrdiff_t charpos;
29013 Lisp_Object string, object = Qnil;
29014 Lisp_Object pos IF_LINT (= Qnil), help;
29015
29016 Lisp_Object mouse_face;
29017 int original_x_pixel = x;
29018 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29019 struct glyph_row *row IF_LINT (= 0);
29020
29021 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29022 {
29023 int x0;
29024 struct glyph *end;
29025
29026 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29027 returns them in row/column units! */
29028 string = mode_line_string (w, area, &x, &y, &charpos,
29029 &object, &dx, &dy, &width, &height);
29030
29031 row = (area == ON_MODE_LINE
29032 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29033 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29034
29035 /* Find the glyph under the mouse pointer. */
29036 if (row->mode_line_p && row->enabled_p)
29037 {
29038 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29039 end = glyph + row->used[TEXT_AREA];
29040
29041 for (x0 = original_x_pixel;
29042 glyph < end && x0 >= glyph->pixel_width;
29043 ++glyph)
29044 x0 -= glyph->pixel_width;
29045
29046 if (glyph >= end)
29047 glyph = NULL;
29048 }
29049 }
29050 else
29051 {
29052 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29053 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29054 returns them in row/column units! */
29055 string = marginal_area_string (w, area, &x, &y, &charpos,
29056 &object, &dx, &dy, &width, &height);
29057 }
29058
29059 help = Qnil;
29060
29061 #ifdef HAVE_WINDOW_SYSTEM
29062 if (IMAGEP (object))
29063 {
29064 Lisp_Object image_map, hotspot;
29065 if ((image_map = Fplist_get (XCDR (object), QCmap),
29066 !NILP (image_map))
29067 && (hotspot = find_hot_spot (image_map, dx, dy),
29068 CONSP (hotspot))
29069 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29070 {
29071 Lisp_Object plist;
29072
29073 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29074 If so, we could look for mouse-enter, mouse-leave
29075 properties in PLIST (and do something...). */
29076 hotspot = XCDR (hotspot);
29077 if (CONSP (hotspot)
29078 && (plist = XCAR (hotspot), CONSP (plist)))
29079 {
29080 pointer = Fplist_get (plist, Qpointer);
29081 if (NILP (pointer))
29082 pointer = Qhand;
29083 help = Fplist_get (plist, Qhelp_echo);
29084 if (!NILP (help))
29085 {
29086 help_echo_string = help;
29087 XSETWINDOW (help_echo_window, w);
29088 help_echo_object = w->contents;
29089 help_echo_pos = charpos;
29090 }
29091 }
29092 }
29093 if (NILP (pointer))
29094 pointer = Fplist_get (XCDR (object), QCpointer);
29095 }
29096 #endif /* HAVE_WINDOW_SYSTEM */
29097
29098 if (STRINGP (string))
29099 pos = make_number (charpos);
29100
29101 /* Set the help text and mouse pointer. If the mouse is on a part
29102 of the mode line without any text (e.g. past the right edge of
29103 the mode line text), use the default help text and pointer. */
29104 if (STRINGP (string) || area == ON_MODE_LINE)
29105 {
29106 /* Arrange to display the help by setting the global variables
29107 help_echo_string, help_echo_object, and help_echo_pos. */
29108 if (NILP (help))
29109 {
29110 if (STRINGP (string))
29111 help = Fget_text_property (pos, Qhelp_echo, string);
29112
29113 if (!NILP (help))
29114 {
29115 help_echo_string = help;
29116 XSETWINDOW (help_echo_window, w);
29117 help_echo_object = string;
29118 help_echo_pos = charpos;
29119 }
29120 else if (area == ON_MODE_LINE)
29121 {
29122 Lisp_Object default_help
29123 = buffer_local_value (Qmode_line_default_help_echo,
29124 w->contents);
29125
29126 if (STRINGP (default_help))
29127 {
29128 help_echo_string = default_help;
29129 XSETWINDOW (help_echo_window, w);
29130 help_echo_object = Qnil;
29131 help_echo_pos = -1;
29132 }
29133 }
29134 }
29135
29136 #ifdef HAVE_WINDOW_SYSTEM
29137 /* Change the mouse pointer according to what is under it. */
29138 if (FRAME_WINDOW_P (f))
29139 {
29140 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29141 || minibuf_level
29142 || NILP (Vresize_mini_windows));
29143
29144 dpyinfo = FRAME_DISPLAY_INFO (f);
29145 if (STRINGP (string))
29146 {
29147 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29148
29149 if (NILP (pointer))
29150 pointer = Fget_text_property (pos, Qpointer, string);
29151
29152 /* Change the mouse pointer according to what is under X/Y. */
29153 if (NILP (pointer)
29154 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29155 {
29156 Lisp_Object map;
29157 map = Fget_text_property (pos, Qlocal_map, string);
29158 if (!KEYMAPP (map))
29159 map = Fget_text_property (pos, Qkeymap, string);
29160 if (!KEYMAPP (map) && draggable)
29161 cursor = dpyinfo->vertical_scroll_bar_cursor;
29162 }
29163 }
29164 else if (draggable)
29165 /* Default mode-line pointer. */
29166 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29167 }
29168 #endif
29169 }
29170
29171 /* Change the mouse face according to what is under X/Y. */
29172 if (STRINGP (string))
29173 {
29174 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29175 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29176 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29177 && glyph)
29178 {
29179 Lisp_Object b, e;
29180
29181 struct glyph * tmp_glyph;
29182
29183 int gpos;
29184 int gseq_length;
29185 int total_pixel_width;
29186 ptrdiff_t begpos, endpos, ignore;
29187
29188 int vpos, hpos;
29189
29190 b = Fprevious_single_property_change (make_number (charpos + 1),
29191 Qmouse_face, string, Qnil);
29192 if (NILP (b))
29193 begpos = 0;
29194 else
29195 begpos = XINT (b);
29196
29197 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29198 if (NILP (e))
29199 endpos = SCHARS (string);
29200 else
29201 endpos = XINT (e);
29202
29203 /* Calculate the glyph position GPOS of GLYPH in the
29204 displayed string, relative to the beginning of the
29205 highlighted part of the string.
29206
29207 Note: GPOS is different from CHARPOS. CHARPOS is the
29208 position of GLYPH in the internal string object. A mode
29209 line string format has structures which are converted to
29210 a flattened string by the Emacs Lisp interpreter. The
29211 internal string is an element of those structures. The
29212 displayed string is the flattened string. */
29213 tmp_glyph = row_start_glyph;
29214 while (tmp_glyph < glyph
29215 && (!(EQ (tmp_glyph->object, glyph->object)
29216 && begpos <= tmp_glyph->charpos
29217 && tmp_glyph->charpos < endpos)))
29218 tmp_glyph++;
29219 gpos = glyph - tmp_glyph;
29220
29221 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29222 the highlighted part of the displayed string to which
29223 GLYPH belongs. Note: GSEQ_LENGTH is different from
29224 SCHARS (STRING), because the latter returns the length of
29225 the internal string. */
29226 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29227 tmp_glyph > glyph
29228 && (!(EQ (tmp_glyph->object, glyph->object)
29229 && begpos <= tmp_glyph->charpos
29230 && tmp_glyph->charpos < endpos));
29231 tmp_glyph--)
29232 ;
29233 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29234
29235 /* Calculate the total pixel width of all the glyphs between
29236 the beginning of the highlighted area and GLYPH. */
29237 total_pixel_width = 0;
29238 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29239 total_pixel_width += tmp_glyph->pixel_width;
29240
29241 /* Pre calculation of re-rendering position. Note: X is in
29242 column units here, after the call to mode_line_string or
29243 marginal_area_string. */
29244 hpos = x - gpos;
29245 vpos = (area == ON_MODE_LINE
29246 ? (w->current_matrix)->nrows - 1
29247 : 0);
29248
29249 /* If GLYPH's position is included in the region that is
29250 already drawn in mouse face, we have nothing to do. */
29251 if ( EQ (window, hlinfo->mouse_face_window)
29252 && (!row->reversed_p
29253 ? (hlinfo->mouse_face_beg_col <= hpos
29254 && hpos < hlinfo->mouse_face_end_col)
29255 /* In R2L rows we swap BEG and END, see below. */
29256 : (hlinfo->mouse_face_end_col <= hpos
29257 && hpos < hlinfo->mouse_face_beg_col))
29258 && hlinfo->mouse_face_beg_row == vpos )
29259 return;
29260
29261 if (clear_mouse_face (hlinfo))
29262 cursor = No_Cursor;
29263
29264 if (!row->reversed_p)
29265 {
29266 hlinfo->mouse_face_beg_col = hpos;
29267 hlinfo->mouse_face_beg_x = original_x_pixel
29268 - (total_pixel_width + dx);
29269 hlinfo->mouse_face_end_col = hpos + gseq_length;
29270 hlinfo->mouse_face_end_x = 0;
29271 }
29272 else
29273 {
29274 /* In R2L rows, show_mouse_face expects BEG and END
29275 coordinates to be swapped. */
29276 hlinfo->mouse_face_end_col = hpos;
29277 hlinfo->mouse_face_end_x = original_x_pixel
29278 - (total_pixel_width + dx);
29279 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29280 hlinfo->mouse_face_beg_x = 0;
29281 }
29282
29283 hlinfo->mouse_face_beg_row = vpos;
29284 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29285 hlinfo->mouse_face_past_end = 0;
29286 hlinfo->mouse_face_window = window;
29287
29288 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29289 charpos,
29290 0, &ignore,
29291 glyph->face_id,
29292 1);
29293 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29294
29295 if (NILP (pointer))
29296 pointer = Qhand;
29297 }
29298 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29299 clear_mouse_face (hlinfo);
29300 }
29301 #ifdef HAVE_WINDOW_SYSTEM
29302 if (FRAME_WINDOW_P (f))
29303 define_frame_cursor1 (f, cursor, pointer);
29304 #endif
29305 }
29306
29307
29308 /* EXPORT:
29309 Take proper action when the mouse has moved to position X, Y on
29310 frame F with regards to highlighting portions of display that have
29311 mouse-face properties. Also de-highlight portions of display where
29312 the mouse was before, set the mouse pointer shape as appropriate
29313 for the mouse coordinates, and activate help echo (tooltips).
29314 X and Y can be negative or out of range. */
29315
29316 void
29317 note_mouse_highlight (struct frame *f, int x, int y)
29318 {
29319 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29320 enum window_part part = ON_NOTHING;
29321 Lisp_Object window;
29322 struct window *w;
29323 Cursor cursor = No_Cursor;
29324 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29325 struct buffer *b;
29326
29327 /* When a menu is active, don't highlight because this looks odd. */
29328 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29329 if (popup_activated ())
29330 return;
29331 #endif
29332
29333 if (!f->glyphs_initialized_p
29334 || f->pointer_invisible)
29335 return;
29336
29337 hlinfo->mouse_face_mouse_x = x;
29338 hlinfo->mouse_face_mouse_y = y;
29339 hlinfo->mouse_face_mouse_frame = f;
29340
29341 if (hlinfo->mouse_face_defer)
29342 return;
29343
29344 /* Which window is that in? */
29345 window = window_from_coordinates (f, x, y, &part, 1);
29346
29347 /* If displaying active text in another window, clear that. */
29348 if (! EQ (window, hlinfo->mouse_face_window)
29349 /* Also clear if we move out of text area in same window. */
29350 || (!NILP (hlinfo->mouse_face_window)
29351 && !NILP (window)
29352 && part != ON_TEXT
29353 && part != ON_MODE_LINE
29354 && part != ON_HEADER_LINE))
29355 clear_mouse_face (hlinfo);
29356
29357 /* Not on a window -> return. */
29358 if (!WINDOWP (window))
29359 return;
29360
29361 /* Reset help_echo_string. It will get recomputed below. */
29362 help_echo_string = Qnil;
29363
29364 /* Convert to window-relative pixel coordinates. */
29365 w = XWINDOW (window);
29366 frame_to_window_pixel_xy (w, &x, &y);
29367
29368 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29369 /* Handle tool-bar window differently since it doesn't display a
29370 buffer. */
29371 if (EQ (window, f->tool_bar_window))
29372 {
29373 note_tool_bar_highlight (f, x, y);
29374 return;
29375 }
29376 #endif
29377
29378 /* Mouse is on the mode, header line or margin? */
29379 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29380 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29381 {
29382 note_mode_line_or_margin_highlight (window, x, y, part);
29383
29384 #ifdef HAVE_WINDOW_SYSTEM
29385 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29386 {
29387 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29388 /* Show non-text cursor (Bug#16647). */
29389 goto set_cursor;
29390 }
29391 else
29392 #endif
29393 return;
29394 }
29395
29396 #ifdef HAVE_WINDOW_SYSTEM
29397 if (part == ON_VERTICAL_BORDER)
29398 {
29399 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29400 help_echo_string = build_string ("drag-mouse-1: resize");
29401 }
29402 else if (part == ON_RIGHT_DIVIDER)
29403 {
29404 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29405 help_echo_string = build_string ("drag-mouse-1: resize");
29406 }
29407 else if (part == ON_BOTTOM_DIVIDER)
29408 if (! WINDOW_BOTTOMMOST_P (w)
29409 || minibuf_level
29410 || NILP (Vresize_mini_windows))
29411 {
29412 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29413 help_echo_string = build_string ("drag-mouse-1: resize");
29414 }
29415 else
29416 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29417 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29418 || part == ON_VERTICAL_SCROLL_BAR
29419 || part == ON_HORIZONTAL_SCROLL_BAR)
29420 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29421 else
29422 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29423 #endif
29424
29425 /* Are we in a window whose display is up to date?
29426 And verify the buffer's text has not changed. */
29427 b = XBUFFER (w->contents);
29428 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29429 {
29430 int hpos, vpos, dx, dy, area = LAST_AREA;
29431 ptrdiff_t pos;
29432 struct glyph *glyph;
29433 Lisp_Object object;
29434 Lisp_Object mouse_face = Qnil, position;
29435 Lisp_Object *overlay_vec = NULL;
29436 ptrdiff_t i, noverlays;
29437 struct buffer *obuf;
29438 ptrdiff_t obegv, ozv;
29439 int same_region;
29440
29441 /* Find the glyph under X/Y. */
29442 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29443
29444 #ifdef HAVE_WINDOW_SYSTEM
29445 /* Look for :pointer property on image. */
29446 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29447 {
29448 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29449 if (img != NULL && IMAGEP (img->spec))
29450 {
29451 Lisp_Object image_map, hotspot;
29452 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29453 !NILP (image_map))
29454 && (hotspot = find_hot_spot (image_map,
29455 glyph->slice.img.x + dx,
29456 glyph->slice.img.y + dy),
29457 CONSP (hotspot))
29458 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29459 {
29460 Lisp_Object plist;
29461
29462 /* Could check XCAR (hotspot) to see if we enter/leave
29463 this hot-spot.
29464 If so, we could look for mouse-enter, mouse-leave
29465 properties in PLIST (and do something...). */
29466 hotspot = XCDR (hotspot);
29467 if (CONSP (hotspot)
29468 && (plist = XCAR (hotspot), CONSP (plist)))
29469 {
29470 pointer = Fplist_get (plist, Qpointer);
29471 if (NILP (pointer))
29472 pointer = Qhand;
29473 help_echo_string = Fplist_get (plist, Qhelp_echo);
29474 if (!NILP (help_echo_string))
29475 {
29476 help_echo_window = window;
29477 help_echo_object = glyph->object;
29478 help_echo_pos = glyph->charpos;
29479 }
29480 }
29481 }
29482 if (NILP (pointer))
29483 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29484 }
29485 }
29486 #endif /* HAVE_WINDOW_SYSTEM */
29487
29488 /* Clear mouse face if X/Y not over text. */
29489 if (glyph == NULL
29490 || area != TEXT_AREA
29491 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29492 /* Glyph's OBJECT is an integer for glyphs inserted by the
29493 display engine for its internal purposes, like truncation
29494 and continuation glyphs and blanks beyond the end of
29495 line's text on text terminals. If we are over such a
29496 glyph, we are not over any text. */
29497 || INTEGERP (glyph->object)
29498 /* R2L rows have a stretch glyph at their front, which
29499 stands for no text, whereas L2R rows have no glyphs at
29500 all beyond the end of text. Treat such stretch glyphs
29501 like we do with NULL glyphs in L2R rows. */
29502 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29503 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29504 && glyph->type == STRETCH_GLYPH
29505 && glyph->avoid_cursor_p))
29506 {
29507 if (clear_mouse_face (hlinfo))
29508 cursor = No_Cursor;
29509 #ifdef HAVE_WINDOW_SYSTEM
29510 if (FRAME_WINDOW_P (f) && NILP (pointer))
29511 {
29512 if (area != TEXT_AREA)
29513 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29514 else
29515 pointer = Vvoid_text_area_pointer;
29516 }
29517 #endif
29518 goto set_cursor;
29519 }
29520
29521 pos = glyph->charpos;
29522 object = glyph->object;
29523 if (!STRINGP (object) && !BUFFERP (object))
29524 goto set_cursor;
29525
29526 /* If we get an out-of-range value, return now; avoid an error. */
29527 if (BUFFERP (object) && pos > BUF_Z (b))
29528 goto set_cursor;
29529
29530 /* Make the window's buffer temporarily current for
29531 overlays_at and compute_char_face. */
29532 obuf = current_buffer;
29533 current_buffer = b;
29534 obegv = BEGV;
29535 ozv = ZV;
29536 BEGV = BEG;
29537 ZV = Z;
29538
29539 /* Is this char mouse-active or does it have help-echo? */
29540 position = make_number (pos);
29541
29542 USE_SAFE_ALLOCA;
29543
29544 if (BUFFERP (object))
29545 {
29546 /* Put all the overlays we want in a vector in overlay_vec. */
29547 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29548 /* Sort overlays into increasing priority order. */
29549 noverlays = sort_overlays (overlay_vec, noverlays, w);
29550 }
29551 else
29552 noverlays = 0;
29553
29554 if (NILP (Vmouse_highlight))
29555 {
29556 clear_mouse_face (hlinfo);
29557 goto check_help_echo;
29558 }
29559
29560 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29561
29562 if (same_region)
29563 cursor = No_Cursor;
29564
29565 /* Check mouse-face highlighting. */
29566 if (! same_region
29567 /* If there exists an overlay with mouse-face overlapping
29568 the one we are currently highlighting, we have to
29569 check if we enter the overlapping overlay, and then
29570 highlight only that. */
29571 || (OVERLAYP (hlinfo->mouse_face_overlay)
29572 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29573 {
29574 /* Find the highest priority overlay with a mouse-face. */
29575 Lisp_Object overlay = Qnil;
29576 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29577 {
29578 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29579 if (!NILP (mouse_face))
29580 overlay = overlay_vec[i];
29581 }
29582
29583 /* If we're highlighting the same overlay as before, there's
29584 no need to do that again. */
29585 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29586 goto check_help_echo;
29587 hlinfo->mouse_face_overlay = overlay;
29588
29589 /* Clear the display of the old active region, if any. */
29590 if (clear_mouse_face (hlinfo))
29591 cursor = No_Cursor;
29592
29593 /* If no overlay applies, get a text property. */
29594 if (NILP (overlay))
29595 mouse_face = Fget_text_property (position, Qmouse_face, object);
29596
29597 /* Next, compute the bounds of the mouse highlighting and
29598 display it. */
29599 if (!NILP (mouse_face) && STRINGP (object))
29600 {
29601 /* The mouse-highlighting comes from a display string
29602 with a mouse-face. */
29603 Lisp_Object s, e;
29604 ptrdiff_t ignore;
29605
29606 s = Fprevious_single_property_change
29607 (make_number (pos + 1), Qmouse_face, object, Qnil);
29608 e = Fnext_single_property_change
29609 (position, Qmouse_face, object, Qnil);
29610 if (NILP (s))
29611 s = make_number (0);
29612 if (NILP (e))
29613 e = make_number (SCHARS (object));
29614 mouse_face_from_string_pos (w, hlinfo, object,
29615 XINT (s), XINT (e));
29616 hlinfo->mouse_face_past_end = 0;
29617 hlinfo->mouse_face_window = window;
29618 hlinfo->mouse_face_face_id
29619 = face_at_string_position (w, object, pos, 0, &ignore,
29620 glyph->face_id, 1);
29621 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29622 cursor = No_Cursor;
29623 }
29624 else
29625 {
29626 /* The mouse-highlighting, if any, comes from an overlay
29627 or text property in the buffer. */
29628 Lisp_Object buffer IF_LINT (= Qnil);
29629 Lisp_Object disp_string IF_LINT (= Qnil);
29630
29631 if (STRINGP (object))
29632 {
29633 /* If we are on a display string with no mouse-face,
29634 check if the text under it has one. */
29635 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29636 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29637 pos = string_buffer_position (object, start);
29638 if (pos > 0)
29639 {
29640 mouse_face = get_char_property_and_overlay
29641 (make_number (pos), Qmouse_face, w->contents, &overlay);
29642 buffer = w->contents;
29643 disp_string = object;
29644 }
29645 }
29646 else
29647 {
29648 buffer = object;
29649 disp_string = Qnil;
29650 }
29651
29652 if (!NILP (mouse_face))
29653 {
29654 Lisp_Object before, after;
29655 Lisp_Object before_string, after_string;
29656 /* To correctly find the limits of mouse highlight
29657 in a bidi-reordered buffer, we must not use the
29658 optimization of limiting the search in
29659 previous-single-property-change and
29660 next-single-property-change, because
29661 rows_from_pos_range needs the real start and end
29662 positions to DTRT in this case. That's because
29663 the first row visible in a window does not
29664 necessarily display the character whose position
29665 is the smallest. */
29666 Lisp_Object lim1
29667 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29668 ? Fmarker_position (w->start)
29669 : Qnil;
29670 Lisp_Object lim2
29671 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29672 ? make_number (BUF_Z (XBUFFER (buffer))
29673 - w->window_end_pos)
29674 : Qnil;
29675
29676 if (NILP (overlay))
29677 {
29678 /* Handle the text property case. */
29679 before = Fprevious_single_property_change
29680 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29681 after = Fnext_single_property_change
29682 (make_number (pos), Qmouse_face, buffer, lim2);
29683 before_string = after_string = Qnil;
29684 }
29685 else
29686 {
29687 /* Handle the overlay case. */
29688 before = Foverlay_start (overlay);
29689 after = Foverlay_end (overlay);
29690 before_string = Foverlay_get (overlay, Qbefore_string);
29691 after_string = Foverlay_get (overlay, Qafter_string);
29692
29693 if (!STRINGP (before_string)) before_string = Qnil;
29694 if (!STRINGP (after_string)) after_string = Qnil;
29695 }
29696
29697 mouse_face_from_buffer_pos (window, hlinfo, pos,
29698 NILP (before)
29699 ? 1
29700 : XFASTINT (before),
29701 NILP (after)
29702 ? BUF_Z (XBUFFER (buffer))
29703 : XFASTINT (after),
29704 before_string, after_string,
29705 disp_string);
29706 cursor = No_Cursor;
29707 }
29708 }
29709 }
29710
29711 check_help_echo:
29712
29713 /* Look for a `help-echo' property. */
29714 if (NILP (help_echo_string)) {
29715 Lisp_Object help, overlay;
29716
29717 /* Check overlays first. */
29718 help = overlay = Qnil;
29719 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29720 {
29721 overlay = overlay_vec[i];
29722 help = Foverlay_get (overlay, Qhelp_echo);
29723 }
29724
29725 if (!NILP (help))
29726 {
29727 help_echo_string = help;
29728 help_echo_window = window;
29729 help_echo_object = overlay;
29730 help_echo_pos = pos;
29731 }
29732 else
29733 {
29734 Lisp_Object obj = glyph->object;
29735 ptrdiff_t charpos = glyph->charpos;
29736
29737 /* Try text properties. */
29738 if (STRINGP (obj)
29739 && charpos >= 0
29740 && charpos < SCHARS (obj))
29741 {
29742 help = Fget_text_property (make_number (charpos),
29743 Qhelp_echo, obj);
29744 if (NILP (help))
29745 {
29746 /* If the string itself doesn't specify a help-echo,
29747 see if the buffer text ``under'' it does. */
29748 struct glyph_row *r
29749 = MATRIX_ROW (w->current_matrix, vpos);
29750 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29751 ptrdiff_t p = string_buffer_position (obj, start);
29752 if (p > 0)
29753 {
29754 help = Fget_char_property (make_number (p),
29755 Qhelp_echo, w->contents);
29756 if (!NILP (help))
29757 {
29758 charpos = p;
29759 obj = w->contents;
29760 }
29761 }
29762 }
29763 }
29764 else if (BUFFERP (obj)
29765 && charpos >= BEGV
29766 && charpos < ZV)
29767 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29768 obj);
29769
29770 if (!NILP (help))
29771 {
29772 help_echo_string = help;
29773 help_echo_window = window;
29774 help_echo_object = obj;
29775 help_echo_pos = charpos;
29776 }
29777 }
29778 }
29779
29780 #ifdef HAVE_WINDOW_SYSTEM
29781 /* Look for a `pointer' property. */
29782 if (FRAME_WINDOW_P (f) && NILP (pointer))
29783 {
29784 /* Check overlays first. */
29785 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29786 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29787
29788 if (NILP (pointer))
29789 {
29790 Lisp_Object obj = glyph->object;
29791 ptrdiff_t charpos = glyph->charpos;
29792
29793 /* Try text properties. */
29794 if (STRINGP (obj)
29795 && charpos >= 0
29796 && charpos < SCHARS (obj))
29797 {
29798 pointer = Fget_text_property (make_number (charpos),
29799 Qpointer, obj);
29800 if (NILP (pointer))
29801 {
29802 /* If the string itself doesn't specify a pointer,
29803 see if the buffer text ``under'' it does. */
29804 struct glyph_row *r
29805 = MATRIX_ROW (w->current_matrix, vpos);
29806 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29807 ptrdiff_t p = string_buffer_position (obj, start);
29808 if (p > 0)
29809 pointer = Fget_char_property (make_number (p),
29810 Qpointer, w->contents);
29811 }
29812 }
29813 else if (BUFFERP (obj)
29814 && charpos >= BEGV
29815 && charpos < ZV)
29816 pointer = Fget_text_property (make_number (charpos),
29817 Qpointer, obj);
29818 }
29819 }
29820 #endif /* HAVE_WINDOW_SYSTEM */
29821
29822 BEGV = obegv;
29823 ZV = ozv;
29824 current_buffer = obuf;
29825 SAFE_FREE ();
29826 }
29827
29828 set_cursor:
29829
29830 #ifdef HAVE_WINDOW_SYSTEM
29831 if (FRAME_WINDOW_P (f))
29832 define_frame_cursor1 (f, cursor, pointer);
29833 #else
29834 /* This is here to prevent a compiler error, about "label at end of
29835 compound statement". */
29836 return;
29837 #endif
29838 }
29839
29840
29841 /* EXPORT for RIF:
29842 Clear any mouse-face on window W. This function is part of the
29843 redisplay interface, and is called from try_window_id and similar
29844 functions to ensure the mouse-highlight is off. */
29845
29846 void
29847 x_clear_window_mouse_face (struct window *w)
29848 {
29849 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29850 Lisp_Object window;
29851
29852 block_input ();
29853 XSETWINDOW (window, w);
29854 if (EQ (window, hlinfo->mouse_face_window))
29855 clear_mouse_face (hlinfo);
29856 unblock_input ();
29857 }
29858
29859
29860 /* EXPORT:
29861 Just discard the mouse face information for frame F, if any.
29862 This is used when the size of F is changed. */
29863
29864 void
29865 cancel_mouse_face (struct frame *f)
29866 {
29867 Lisp_Object window;
29868 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29869
29870 window = hlinfo->mouse_face_window;
29871 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29872 reset_mouse_highlight (hlinfo);
29873 }
29874
29875
29876 \f
29877 /***********************************************************************
29878 Exposure Events
29879 ***********************************************************************/
29880
29881 #ifdef HAVE_WINDOW_SYSTEM
29882
29883 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29884 which intersects rectangle R. R is in window-relative coordinates. */
29885
29886 static void
29887 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29888 enum glyph_row_area area)
29889 {
29890 struct glyph *first = row->glyphs[area];
29891 struct glyph *end = row->glyphs[area] + row->used[area];
29892 struct glyph *last;
29893 int first_x, start_x, x;
29894
29895 if (area == TEXT_AREA && row->fill_line_p)
29896 /* If row extends face to end of line write the whole line. */
29897 draw_glyphs (w, 0, row, area,
29898 0, row->used[area],
29899 DRAW_NORMAL_TEXT, 0);
29900 else
29901 {
29902 /* Set START_X to the window-relative start position for drawing glyphs of
29903 AREA. The first glyph of the text area can be partially visible.
29904 The first glyphs of other areas cannot. */
29905 start_x = window_box_left_offset (w, area);
29906 x = start_x;
29907 if (area == TEXT_AREA)
29908 x += row->x;
29909
29910 /* Find the first glyph that must be redrawn. */
29911 while (first < end
29912 && x + first->pixel_width < r->x)
29913 {
29914 x += first->pixel_width;
29915 ++first;
29916 }
29917
29918 /* Find the last one. */
29919 last = first;
29920 first_x = x;
29921 while (last < end
29922 && x < r->x + r->width)
29923 {
29924 x += last->pixel_width;
29925 ++last;
29926 }
29927
29928 /* Repaint. */
29929 if (last > first)
29930 draw_glyphs (w, first_x - start_x, row, area,
29931 first - row->glyphs[area], last - row->glyphs[area],
29932 DRAW_NORMAL_TEXT, 0);
29933 }
29934 }
29935
29936
29937 /* Redraw the parts of the glyph row ROW on window W intersecting
29938 rectangle R. R is in window-relative coordinates. Value is
29939 non-zero if mouse-face was overwritten. */
29940
29941 static int
29942 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29943 {
29944 eassert (row->enabled_p);
29945
29946 if (row->mode_line_p || w->pseudo_window_p)
29947 draw_glyphs (w, 0, row, TEXT_AREA,
29948 0, row->used[TEXT_AREA],
29949 DRAW_NORMAL_TEXT, 0);
29950 else
29951 {
29952 if (row->used[LEFT_MARGIN_AREA])
29953 expose_area (w, row, r, LEFT_MARGIN_AREA);
29954 if (row->used[TEXT_AREA])
29955 expose_area (w, row, r, TEXT_AREA);
29956 if (row->used[RIGHT_MARGIN_AREA])
29957 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29958 draw_row_fringe_bitmaps (w, row);
29959 }
29960
29961 return row->mouse_face_p;
29962 }
29963
29964
29965 /* Redraw those parts of glyphs rows during expose event handling that
29966 overlap other rows. Redrawing of an exposed line writes over parts
29967 of lines overlapping that exposed line; this function fixes that.
29968
29969 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29970 row in W's current matrix that is exposed and overlaps other rows.
29971 LAST_OVERLAPPING_ROW is the last such row. */
29972
29973 static void
29974 expose_overlaps (struct window *w,
29975 struct glyph_row *first_overlapping_row,
29976 struct glyph_row *last_overlapping_row,
29977 XRectangle *r)
29978 {
29979 struct glyph_row *row;
29980
29981 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29982 if (row->overlapping_p)
29983 {
29984 eassert (row->enabled_p && !row->mode_line_p);
29985
29986 row->clip = r;
29987 if (row->used[LEFT_MARGIN_AREA])
29988 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29989
29990 if (row->used[TEXT_AREA])
29991 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29992
29993 if (row->used[RIGHT_MARGIN_AREA])
29994 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29995 row->clip = NULL;
29996 }
29997 }
29998
29999
30000 /* Return non-zero if W's cursor intersects rectangle R. */
30001
30002 static int
30003 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30004 {
30005 XRectangle cr, result;
30006 struct glyph *cursor_glyph;
30007 struct glyph_row *row;
30008
30009 if (w->phys_cursor.vpos >= 0
30010 && w->phys_cursor.vpos < w->current_matrix->nrows
30011 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30012 row->enabled_p)
30013 && row->cursor_in_fringe_p)
30014 {
30015 /* Cursor is in the fringe. */
30016 cr.x = window_box_right_offset (w,
30017 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30018 ? RIGHT_MARGIN_AREA
30019 : TEXT_AREA));
30020 cr.y = row->y;
30021 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30022 cr.height = row->height;
30023 return x_intersect_rectangles (&cr, r, &result);
30024 }
30025
30026 cursor_glyph = get_phys_cursor_glyph (w);
30027 if (cursor_glyph)
30028 {
30029 /* r is relative to W's box, but w->phys_cursor.x is relative
30030 to left edge of W's TEXT area. Adjust it. */
30031 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30032 cr.y = w->phys_cursor.y;
30033 cr.width = cursor_glyph->pixel_width;
30034 cr.height = w->phys_cursor_height;
30035 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30036 I assume the effect is the same -- and this is portable. */
30037 return x_intersect_rectangles (&cr, r, &result);
30038 }
30039 /* If we don't understand the format, pretend we're not in the hot-spot. */
30040 return 0;
30041 }
30042
30043
30044 /* EXPORT:
30045 Draw a vertical window border to the right of window W if W doesn't
30046 have vertical scroll bars. */
30047
30048 void
30049 x_draw_vertical_border (struct window *w)
30050 {
30051 struct frame *f = XFRAME (WINDOW_FRAME (w));
30052
30053 /* We could do better, if we knew what type of scroll-bar the adjacent
30054 windows (on either side) have... But we don't :-(
30055 However, I think this works ok. ++KFS 2003-04-25 */
30056
30057 /* Redraw borders between horizontally adjacent windows. Don't
30058 do it for frames with vertical scroll bars because either the
30059 right scroll bar of a window, or the left scroll bar of its
30060 neighbor will suffice as a border. */
30061 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30062 return;
30063
30064 /* Note: It is necessary to redraw both the left and the right
30065 borders, for when only this single window W is being
30066 redisplayed. */
30067 if (!WINDOW_RIGHTMOST_P (w)
30068 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30069 {
30070 int x0, x1, y0, y1;
30071
30072 window_box_edges (w, &x0, &y0, &x1, &y1);
30073 y1 -= 1;
30074
30075 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30076 x1 -= 1;
30077
30078 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30079 }
30080
30081 if (!WINDOW_LEFTMOST_P (w)
30082 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30083 {
30084 int x0, x1, y0, y1;
30085
30086 window_box_edges (w, &x0, &y0, &x1, &y1);
30087 y1 -= 1;
30088
30089 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30090 x0 -= 1;
30091
30092 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30093 }
30094 }
30095
30096
30097 /* Draw window dividers for window W. */
30098
30099 void
30100 x_draw_right_divider (struct window *w)
30101 {
30102 struct frame *f = WINDOW_XFRAME (w);
30103
30104 if (w->mini || w->pseudo_window_p)
30105 return;
30106 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30107 {
30108 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30109 int x1 = WINDOW_RIGHT_EDGE_X (w);
30110 int y0 = WINDOW_TOP_EDGE_Y (w);
30111 /* The bottom divider prevails. */
30112 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30113
30114 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30115 }
30116 }
30117
30118 static void
30119 x_draw_bottom_divider (struct window *w)
30120 {
30121 struct frame *f = XFRAME (WINDOW_FRAME (w));
30122
30123 if (w->mini || w->pseudo_window_p)
30124 return;
30125 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30126 {
30127 int x0 = WINDOW_LEFT_EDGE_X (w);
30128 int x1 = WINDOW_RIGHT_EDGE_X (w);
30129 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30130 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30131
30132 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30133 }
30134 }
30135
30136 /* Redraw the part of window W intersection rectangle FR. Pixel
30137 coordinates in FR are frame-relative. Call this function with
30138 input blocked. Value is non-zero if the exposure overwrites
30139 mouse-face. */
30140
30141 static int
30142 expose_window (struct window *w, XRectangle *fr)
30143 {
30144 struct frame *f = XFRAME (w->frame);
30145 XRectangle wr, r;
30146 int mouse_face_overwritten_p = 0;
30147
30148 /* If window is not yet fully initialized, do nothing. This can
30149 happen when toolkit scroll bars are used and a window is split.
30150 Reconfiguring the scroll bar will generate an expose for a newly
30151 created window. */
30152 if (w->current_matrix == NULL)
30153 return 0;
30154
30155 /* When we're currently updating the window, display and current
30156 matrix usually don't agree. Arrange for a thorough display
30157 later. */
30158 if (w->must_be_updated_p)
30159 {
30160 SET_FRAME_GARBAGED (f);
30161 return 0;
30162 }
30163
30164 /* Frame-relative pixel rectangle of W. */
30165 wr.x = WINDOW_LEFT_EDGE_X (w);
30166 wr.y = WINDOW_TOP_EDGE_Y (w);
30167 wr.width = WINDOW_PIXEL_WIDTH (w);
30168 wr.height = WINDOW_PIXEL_HEIGHT (w);
30169
30170 if (x_intersect_rectangles (fr, &wr, &r))
30171 {
30172 int yb = window_text_bottom_y (w);
30173 struct glyph_row *row;
30174 int cursor_cleared_p, phys_cursor_on_p;
30175 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30176
30177 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30178 r.x, r.y, r.width, r.height));
30179
30180 /* Convert to window coordinates. */
30181 r.x -= WINDOW_LEFT_EDGE_X (w);
30182 r.y -= WINDOW_TOP_EDGE_Y (w);
30183
30184 /* Turn off the cursor. */
30185 if (!w->pseudo_window_p
30186 && phys_cursor_in_rect_p (w, &r))
30187 {
30188 x_clear_cursor (w);
30189 cursor_cleared_p = 1;
30190 }
30191 else
30192 cursor_cleared_p = 0;
30193
30194 /* If the row containing the cursor extends face to end of line,
30195 then expose_area might overwrite the cursor outside the
30196 rectangle and thus notice_overwritten_cursor might clear
30197 w->phys_cursor_on_p. We remember the original value and
30198 check later if it is changed. */
30199 phys_cursor_on_p = w->phys_cursor_on_p;
30200
30201 /* Update lines intersecting rectangle R. */
30202 first_overlapping_row = last_overlapping_row = NULL;
30203 for (row = w->current_matrix->rows;
30204 row->enabled_p;
30205 ++row)
30206 {
30207 int y0 = row->y;
30208 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30209
30210 if ((y0 >= r.y && y0 < r.y + r.height)
30211 || (y1 > r.y && y1 < r.y + r.height)
30212 || (r.y >= y0 && r.y < y1)
30213 || (r.y + r.height > y0 && r.y + r.height < y1))
30214 {
30215 /* A header line may be overlapping, but there is no need
30216 to fix overlapping areas for them. KFS 2005-02-12 */
30217 if (row->overlapping_p && !row->mode_line_p)
30218 {
30219 if (first_overlapping_row == NULL)
30220 first_overlapping_row = row;
30221 last_overlapping_row = row;
30222 }
30223
30224 row->clip = fr;
30225 if (expose_line (w, row, &r))
30226 mouse_face_overwritten_p = 1;
30227 row->clip = NULL;
30228 }
30229 else if (row->overlapping_p)
30230 {
30231 /* We must redraw a row overlapping the exposed area. */
30232 if (y0 < r.y
30233 ? y0 + row->phys_height > r.y
30234 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30235 {
30236 if (first_overlapping_row == NULL)
30237 first_overlapping_row = row;
30238 last_overlapping_row = row;
30239 }
30240 }
30241
30242 if (y1 >= yb)
30243 break;
30244 }
30245
30246 /* Display the mode line if there is one. */
30247 if (WINDOW_WANTS_MODELINE_P (w)
30248 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30249 row->enabled_p)
30250 && row->y < r.y + r.height)
30251 {
30252 if (expose_line (w, row, &r))
30253 mouse_face_overwritten_p = 1;
30254 }
30255
30256 if (!w->pseudo_window_p)
30257 {
30258 /* Fix the display of overlapping rows. */
30259 if (first_overlapping_row)
30260 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30261 fr);
30262
30263 /* Draw border between windows. */
30264 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30265 x_draw_right_divider (w);
30266 else
30267 x_draw_vertical_border (w);
30268
30269 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30270 x_draw_bottom_divider (w);
30271
30272 /* Turn the cursor on again. */
30273 if (cursor_cleared_p
30274 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30275 update_window_cursor (w, 1);
30276 }
30277 }
30278
30279 return mouse_face_overwritten_p;
30280 }
30281
30282
30283
30284 /* Redraw (parts) of all windows in the window tree rooted at W that
30285 intersect R. R contains frame pixel coordinates. Value is
30286 non-zero if the exposure overwrites mouse-face. */
30287
30288 static int
30289 expose_window_tree (struct window *w, XRectangle *r)
30290 {
30291 struct frame *f = XFRAME (w->frame);
30292 int mouse_face_overwritten_p = 0;
30293
30294 while (w && !FRAME_GARBAGED_P (f))
30295 {
30296 if (WINDOWP (w->contents))
30297 mouse_face_overwritten_p
30298 |= expose_window_tree (XWINDOW (w->contents), r);
30299 else
30300 mouse_face_overwritten_p |= expose_window (w, r);
30301
30302 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30303 }
30304
30305 return mouse_face_overwritten_p;
30306 }
30307
30308
30309 /* EXPORT:
30310 Redisplay an exposed area of frame F. X and Y are the upper-left
30311 corner of the exposed rectangle. W and H are width and height of
30312 the exposed area. All are pixel values. W or H zero means redraw
30313 the entire frame. */
30314
30315 void
30316 expose_frame (struct frame *f, int x, int y, int w, int h)
30317 {
30318 XRectangle r;
30319 int mouse_face_overwritten_p = 0;
30320
30321 TRACE ((stderr, "expose_frame "));
30322
30323 /* No need to redraw if frame will be redrawn soon. */
30324 if (FRAME_GARBAGED_P (f))
30325 {
30326 TRACE ((stderr, " garbaged\n"));
30327 return;
30328 }
30329
30330 /* If basic faces haven't been realized yet, there is no point in
30331 trying to redraw anything. This can happen when we get an expose
30332 event while Emacs is starting, e.g. by moving another window. */
30333 if (FRAME_FACE_CACHE (f) == NULL
30334 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30335 {
30336 TRACE ((stderr, " no faces\n"));
30337 return;
30338 }
30339
30340 if (w == 0 || h == 0)
30341 {
30342 r.x = r.y = 0;
30343 r.width = FRAME_TEXT_WIDTH (f);
30344 r.height = FRAME_TEXT_HEIGHT (f);
30345 }
30346 else
30347 {
30348 r.x = x;
30349 r.y = y;
30350 r.width = w;
30351 r.height = h;
30352 }
30353
30354 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30355 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30356
30357 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30358 if (WINDOWP (f->tool_bar_window))
30359 mouse_face_overwritten_p
30360 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30361 #endif
30362
30363 #ifdef HAVE_X_WINDOWS
30364 #ifndef MSDOS
30365 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30366 if (WINDOWP (f->menu_bar_window))
30367 mouse_face_overwritten_p
30368 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30369 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30370 #endif
30371 #endif
30372
30373 /* Some window managers support a focus-follows-mouse style with
30374 delayed raising of frames. Imagine a partially obscured frame,
30375 and moving the mouse into partially obscured mouse-face on that
30376 frame. The visible part of the mouse-face will be highlighted,
30377 then the WM raises the obscured frame. With at least one WM, KDE
30378 2.1, Emacs is not getting any event for the raising of the frame
30379 (even tried with SubstructureRedirectMask), only Expose events.
30380 These expose events will draw text normally, i.e. not
30381 highlighted. Which means we must redo the highlight here.
30382 Subsume it under ``we love X''. --gerd 2001-08-15 */
30383 /* Included in Windows version because Windows most likely does not
30384 do the right thing if any third party tool offers
30385 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30386 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30387 {
30388 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30389 if (f == hlinfo->mouse_face_mouse_frame)
30390 {
30391 int mouse_x = hlinfo->mouse_face_mouse_x;
30392 int mouse_y = hlinfo->mouse_face_mouse_y;
30393 clear_mouse_face (hlinfo);
30394 note_mouse_highlight (f, mouse_x, mouse_y);
30395 }
30396 }
30397 }
30398
30399
30400 /* EXPORT:
30401 Determine the intersection of two rectangles R1 and R2. Return
30402 the intersection in *RESULT. Value is non-zero if RESULT is not
30403 empty. */
30404
30405 int
30406 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30407 {
30408 XRectangle *left, *right;
30409 XRectangle *upper, *lower;
30410 int intersection_p = 0;
30411
30412 /* Rearrange so that R1 is the left-most rectangle. */
30413 if (r1->x < r2->x)
30414 left = r1, right = r2;
30415 else
30416 left = r2, right = r1;
30417
30418 /* X0 of the intersection is right.x0, if this is inside R1,
30419 otherwise there is no intersection. */
30420 if (right->x <= left->x + left->width)
30421 {
30422 result->x = right->x;
30423
30424 /* The right end of the intersection is the minimum of
30425 the right ends of left and right. */
30426 result->width = (min (left->x + left->width, right->x + right->width)
30427 - result->x);
30428
30429 /* Same game for Y. */
30430 if (r1->y < r2->y)
30431 upper = r1, lower = r2;
30432 else
30433 upper = r2, lower = r1;
30434
30435 /* The upper end of the intersection is lower.y0, if this is inside
30436 of upper. Otherwise, there is no intersection. */
30437 if (lower->y <= upper->y + upper->height)
30438 {
30439 result->y = lower->y;
30440
30441 /* The lower end of the intersection is the minimum of the lower
30442 ends of upper and lower. */
30443 result->height = (min (lower->y + lower->height,
30444 upper->y + upper->height)
30445 - result->y);
30446 intersection_p = 1;
30447 }
30448 }
30449
30450 return intersection_p;
30451 }
30452
30453 #endif /* HAVE_WINDOW_SYSTEM */
30454
30455 \f
30456 /***********************************************************************
30457 Initialization
30458 ***********************************************************************/
30459
30460 void
30461 syms_of_xdisp (void)
30462 {
30463 Vwith_echo_area_save_vector = Qnil;
30464 staticpro (&Vwith_echo_area_save_vector);
30465
30466 Vmessage_stack = Qnil;
30467 staticpro (&Vmessage_stack);
30468
30469 /* Non-nil means don't actually do any redisplay. */
30470 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30471
30472 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30473
30474 message_dolog_marker1 = Fmake_marker ();
30475 staticpro (&message_dolog_marker1);
30476 message_dolog_marker2 = Fmake_marker ();
30477 staticpro (&message_dolog_marker2);
30478 message_dolog_marker3 = Fmake_marker ();
30479 staticpro (&message_dolog_marker3);
30480
30481 #ifdef GLYPH_DEBUG
30482 defsubr (&Sdump_frame_glyph_matrix);
30483 defsubr (&Sdump_glyph_matrix);
30484 defsubr (&Sdump_glyph_row);
30485 defsubr (&Sdump_tool_bar_row);
30486 defsubr (&Strace_redisplay);
30487 defsubr (&Strace_to_stderr);
30488 #endif
30489 #ifdef HAVE_WINDOW_SYSTEM
30490 defsubr (&Stool_bar_height);
30491 defsubr (&Slookup_image_map);
30492 #endif
30493 defsubr (&Sline_pixel_height);
30494 defsubr (&Sformat_mode_line);
30495 defsubr (&Sinvisible_p);
30496 defsubr (&Scurrent_bidi_paragraph_direction);
30497 defsubr (&Swindow_text_pixel_size);
30498 defsubr (&Smove_point_visually);
30499 defsubr (&Sbidi_find_overridden_directionality);
30500
30501 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30502 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30503 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30504 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30505 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30506 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30507 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30508 DEFSYM (Qeval, "eval");
30509 DEFSYM (QCdata, ":data");
30510
30511 /* Names of text properties relevant for redisplay. */
30512 DEFSYM (Qdisplay, "display");
30513 DEFSYM (Qspace_width, "space-width");
30514 DEFSYM (Qraise, "raise");
30515 DEFSYM (Qslice, "slice");
30516 DEFSYM (Qspace, "space");
30517 DEFSYM (Qmargin, "margin");
30518 DEFSYM (Qpointer, "pointer");
30519 DEFSYM (Qleft_margin, "left-margin");
30520 DEFSYM (Qright_margin, "right-margin");
30521 DEFSYM (Qcenter, "center");
30522 DEFSYM (Qline_height, "line-height");
30523 DEFSYM (QCalign_to, ":align-to");
30524 DEFSYM (QCrelative_width, ":relative-width");
30525 DEFSYM (QCrelative_height, ":relative-height");
30526 DEFSYM (QCeval, ":eval");
30527 DEFSYM (QCpropertize, ":propertize");
30528 DEFSYM (QCfile, ":file");
30529 DEFSYM (Qfontified, "fontified");
30530 DEFSYM (Qfontification_functions, "fontification-functions");
30531
30532 /* Name of the face used to highlight trailing whitespace. */
30533 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30534
30535 /* Name and number of the face used to highlight escape glyphs. */
30536 DEFSYM (Qescape_glyph, "escape-glyph");
30537
30538 /* Name and number of the face used to highlight non-breaking spaces. */
30539 DEFSYM (Qnobreak_space, "nobreak-space");
30540
30541 /* The symbol 'image' which is the car of the lists used to represent
30542 images in Lisp. Also a tool bar style. */
30543 DEFSYM (Qimage, "image");
30544
30545 /* Tool bar styles. */
30546 DEFSYM (Qtext, "text");
30547 DEFSYM (Qboth, "both");
30548 DEFSYM (Qboth_horiz, "both-horiz");
30549 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30550
30551 /* The image map types. */
30552 DEFSYM (QCmap, ":map");
30553 DEFSYM (QCpointer, ":pointer");
30554 DEFSYM (Qrect, "rect");
30555 DEFSYM (Qcircle, "circle");
30556 DEFSYM (Qpoly, "poly");
30557
30558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30559 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30560 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30561
30562 DEFSYM (Qgrow_only, "grow-only");
30563 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30564 DEFSYM (Qposition, "position");
30565 DEFSYM (Qbuffer_position, "buffer-position");
30566 DEFSYM (Qobject, "object");
30567
30568 /* Cursor shapes. */
30569 DEFSYM (Qbar, "bar");
30570 DEFSYM (Qhbar, "hbar");
30571 DEFSYM (Qbox, "box");
30572 DEFSYM (Qhollow, "hollow");
30573
30574 /* Pointer shapes. */
30575 DEFSYM (Qhand, "hand");
30576 DEFSYM (Qarrow, "arrow");
30577 /* also Qtext */
30578
30579 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30580
30581 list_of_error = list1 (list2 (intern_c_string ("error"),
30582 intern_c_string ("void-variable")));
30583 staticpro (&list_of_error);
30584
30585 /* Values of those variables at last redisplay are stored as
30586 properties on 'overlay-arrow-position' symbol. However, if
30587 Voverlay_arrow_position is a marker, last-arrow-position is its
30588 numerical position. */
30589 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30590 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30591
30592 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30593 properties on a symbol in overlay-arrow-variable-list. */
30594 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30595 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30596
30597 echo_buffer[0] = echo_buffer[1] = Qnil;
30598 staticpro (&echo_buffer[0]);
30599 staticpro (&echo_buffer[1]);
30600
30601 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30602 staticpro (&echo_area_buffer[0]);
30603 staticpro (&echo_area_buffer[1]);
30604
30605 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30606 staticpro (&Vmessages_buffer_name);
30607
30608 mode_line_proptrans_alist = Qnil;
30609 staticpro (&mode_line_proptrans_alist);
30610 mode_line_string_list = Qnil;
30611 staticpro (&mode_line_string_list);
30612 mode_line_string_face = Qnil;
30613 staticpro (&mode_line_string_face);
30614 mode_line_string_face_prop = Qnil;
30615 staticpro (&mode_line_string_face_prop);
30616 Vmode_line_unwind_vector = Qnil;
30617 staticpro (&Vmode_line_unwind_vector);
30618
30619 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30620
30621 help_echo_string = Qnil;
30622 staticpro (&help_echo_string);
30623 help_echo_object = Qnil;
30624 staticpro (&help_echo_object);
30625 help_echo_window = Qnil;
30626 staticpro (&help_echo_window);
30627 previous_help_echo_string = Qnil;
30628 staticpro (&previous_help_echo_string);
30629 help_echo_pos = -1;
30630
30631 DEFSYM (Qright_to_left, "right-to-left");
30632 DEFSYM (Qleft_to_right, "left-to-right");
30633 defsubr (&Sbidi_resolved_levels);
30634
30635 #ifdef HAVE_WINDOW_SYSTEM
30636 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30637 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30638 For example, if a block cursor is over a tab, it will be drawn as
30639 wide as that tab on the display. */);
30640 x_stretch_cursor_p = 0;
30641 #endif
30642
30643 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30644 doc: /* Non-nil means highlight trailing whitespace.
30645 The face used for trailing whitespace is `trailing-whitespace'. */);
30646 Vshow_trailing_whitespace = Qnil;
30647
30648 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30649 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30650 If the value is t, Emacs highlights non-ASCII chars which have the
30651 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30652 or `escape-glyph' face respectively.
30653
30654 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30655 U+2011 (non-breaking hyphen) are affected.
30656
30657 Any other non-nil value means to display these characters as a escape
30658 glyph followed by an ordinary space or hyphen.
30659
30660 A value of nil means no special handling of these characters. */);
30661 Vnobreak_char_display = Qt;
30662
30663 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30664 doc: /* The pointer shape to show in void text areas.
30665 A value of nil means to show the text pointer. Other options are
30666 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30667 `hourglass'. */);
30668 Vvoid_text_area_pointer = Qarrow;
30669
30670 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30671 doc: /* Non-nil means don't actually do any redisplay.
30672 This is used for internal purposes. */);
30673 Vinhibit_redisplay = Qnil;
30674
30675 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30676 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30677 Vglobal_mode_string = Qnil;
30678
30679 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30680 doc: /* Marker for where to display an arrow on top of the buffer text.
30681 This must be the beginning of a line in order to work.
30682 See also `overlay-arrow-string'. */);
30683 Voverlay_arrow_position = Qnil;
30684
30685 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30686 doc: /* String to display as an arrow in non-window frames.
30687 See also `overlay-arrow-position'. */);
30688 Voverlay_arrow_string = build_pure_c_string ("=>");
30689
30690 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30691 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30692 The symbols on this list are examined during redisplay to determine
30693 where to display overlay arrows. */);
30694 Voverlay_arrow_variable_list
30695 = list1 (intern_c_string ("overlay-arrow-position"));
30696
30697 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30698 doc: /* The number of lines to try scrolling a window by when point moves out.
30699 If that fails to bring point back on frame, point is centered instead.
30700 If this is zero, point is always centered after it moves off frame.
30701 If you want scrolling to always be a line at a time, you should set
30702 `scroll-conservatively' to a large value rather than set this to 1. */);
30703
30704 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30705 doc: /* Scroll up to this many lines, to bring point back on screen.
30706 If point moves off-screen, redisplay will scroll by up to
30707 `scroll-conservatively' lines in order to bring point just barely
30708 onto the screen again. If that cannot be done, then redisplay
30709 recenters point as usual.
30710
30711 If the value is greater than 100, redisplay will never recenter point,
30712 but will always scroll just enough text to bring point into view, even
30713 if you move far away.
30714
30715 A value of zero means always recenter point if it moves off screen. */);
30716 scroll_conservatively = 0;
30717
30718 DEFVAR_INT ("scroll-margin", scroll_margin,
30719 doc: /* Number of lines of margin at the top and bottom of a window.
30720 Recenter the window whenever point gets within this many lines
30721 of the top or bottom of the window. */);
30722 scroll_margin = 0;
30723
30724 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30725 doc: /* Pixels per inch value for non-window system displays.
30726 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30727 Vdisplay_pixels_per_inch = make_float (72.0);
30728
30729 #ifdef GLYPH_DEBUG
30730 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30731 #endif
30732
30733 DEFVAR_LISP ("truncate-partial-width-windows",
30734 Vtruncate_partial_width_windows,
30735 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30736 For an integer value, truncate lines in each window narrower than the
30737 full frame width, provided the window width is less than that integer;
30738 otherwise, respect the value of `truncate-lines'.
30739
30740 For any other non-nil value, truncate lines in all windows that do
30741 not span the full frame width.
30742
30743 A value of nil means to respect the value of `truncate-lines'.
30744
30745 If `word-wrap' is enabled, you might want to reduce this. */);
30746 Vtruncate_partial_width_windows = make_number (50);
30747
30748 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30749 doc: /* Maximum buffer size for which line number should be displayed.
30750 If the buffer is bigger than this, the line number does not appear
30751 in the mode line. A value of nil means no limit. */);
30752 Vline_number_display_limit = Qnil;
30753
30754 DEFVAR_INT ("line-number-display-limit-width",
30755 line_number_display_limit_width,
30756 doc: /* Maximum line width (in characters) for line number display.
30757 If the average length of the lines near point is bigger than this, then the
30758 line number may be omitted from the mode line. */);
30759 line_number_display_limit_width = 200;
30760
30761 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30762 doc: /* Non-nil means highlight region even in nonselected windows. */);
30763 highlight_nonselected_windows = 0;
30764
30765 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30766 doc: /* Non-nil if more than one frame is visible on this display.
30767 Minibuffer-only frames don't count, but iconified frames do.
30768 This variable is not guaranteed to be accurate except while processing
30769 `frame-title-format' and `icon-title-format'. */);
30770
30771 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30772 doc: /* Template for displaying the title bar of visible frames.
30773 \(Assuming the window manager supports this feature.)
30774
30775 This variable has the same structure as `mode-line-format', except that
30776 the %c and %l constructs are ignored. It is used only on frames for
30777 which no explicit name has been set \(see `modify-frame-parameters'). */);
30778
30779 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30780 doc: /* Template for displaying the title bar of an iconified frame.
30781 \(Assuming the window manager supports this feature.)
30782 This variable has the same structure as `mode-line-format' (which see),
30783 and is used only on frames for which no explicit name has been set
30784 \(see `modify-frame-parameters'). */);
30785 Vicon_title_format
30786 = Vframe_title_format
30787 = listn (CONSTYPE_PURE, 3,
30788 intern_c_string ("multiple-frames"),
30789 build_pure_c_string ("%b"),
30790 listn (CONSTYPE_PURE, 4,
30791 empty_unibyte_string,
30792 intern_c_string ("invocation-name"),
30793 build_pure_c_string ("@"),
30794 intern_c_string ("system-name")));
30795
30796 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30797 doc: /* Maximum number of lines to keep in the message log buffer.
30798 If nil, disable message logging. If t, log messages but don't truncate
30799 the buffer when it becomes large. */);
30800 Vmessage_log_max = make_number (1000);
30801
30802 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30803 doc: /* Functions called before redisplay, if window sizes have changed.
30804 The value should be a list of functions that take one argument.
30805 Just before redisplay, for each frame, if any of its windows have changed
30806 size since the last redisplay, or have been split or deleted,
30807 all the functions in the list are called, with the frame as argument. */);
30808 Vwindow_size_change_functions = Qnil;
30809
30810 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30811 doc: /* List of functions to call before redisplaying a window with scrolling.
30812 Each function is called with two arguments, the window and its new
30813 display-start position.
30814 These functions are called whenever the `window-start' marker is modified,
30815 either to point into another buffer (e.g. via `set-window-buffer') or another
30816 place in the same buffer.
30817 Note that the value of `window-end' is not valid when these functions are
30818 called.
30819
30820 Warning: Do not use this feature to alter the way the window
30821 is scrolled. It is not designed for that, and such use probably won't
30822 work. */);
30823 Vwindow_scroll_functions = Qnil;
30824
30825 DEFVAR_LISP ("window-text-change-functions",
30826 Vwindow_text_change_functions,
30827 doc: /* Functions to call in redisplay when text in the window might change. */);
30828 Vwindow_text_change_functions = Qnil;
30829
30830 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30831 doc: /* Functions called when redisplay of a window reaches the end trigger.
30832 Each function is called with two arguments, the window and the end trigger value.
30833 See `set-window-redisplay-end-trigger'. */);
30834 Vredisplay_end_trigger_functions = Qnil;
30835
30836 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30837 doc: /* Non-nil means autoselect window with mouse pointer.
30838 If nil, do not autoselect windows.
30839 A positive number means delay autoselection by that many seconds: a
30840 window is autoselected only after the mouse has remained in that
30841 window for the duration of the delay.
30842 A negative number has a similar effect, but causes windows to be
30843 autoselected only after the mouse has stopped moving. \(Because of
30844 the way Emacs compares mouse events, you will occasionally wait twice
30845 that time before the window gets selected.\)
30846 Any other value means to autoselect window instantaneously when the
30847 mouse pointer enters it.
30848
30849 Autoselection selects the minibuffer only if it is active, and never
30850 unselects the minibuffer if it is active.
30851
30852 When customizing this variable make sure that the actual value of
30853 `focus-follows-mouse' matches the behavior of your window manager. */);
30854 Vmouse_autoselect_window = Qnil;
30855
30856 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30857 doc: /* Non-nil means automatically resize tool-bars.
30858 This dynamically changes the tool-bar's height to the minimum height
30859 that is needed to make all tool-bar items visible.
30860 If value is `grow-only', the tool-bar's height is only increased
30861 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30862 Vauto_resize_tool_bars = Qt;
30863
30864 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30865 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30866 auto_raise_tool_bar_buttons_p = 1;
30867
30868 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30869 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30870 make_cursor_line_fully_visible_p = 1;
30871
30872 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30873 doc: /* Border below tool-bar in pixels.
30874 If an integer, use it as the height of the border.
30875 If it is one of `internal-border-width' or `border-width', use the
30876 value of the corresponding frame parameter.
30877 Otherwise, no border is added below the tool-bar. */);
30878 Vtool_bar_border = Qinternal_border_width;
30879
30880 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30881 doc: /* Margin around tool-bar buttons in pixels.
30882 If an integer, use that for both horizontal and vertical margins.
30883 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30884 HORZ specifying the horizontal margin, and VERT specifying the
30885 vertical margin. */);
30886 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30887
30888 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30889 doc: /* Relief thickness of tool-bar buttons. */);
30890 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30891
30892 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30893 doc: /* Tool bar style to use.
30894 It can be one of
30895 image - show images only
30896 text - show text only
30897 both - show both, text below image
30898 both-horiz - show text to the right of the image
30899 text-image-horiz - show text to the left of the image
30900 any other - use system default or image if no system default.
30901
30902 This variable only affects the GTK+ toolkit version of Emacs. */);
30903 Vtool_bar_style = Qnil;
30904
30905 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30906 doc: /* Maximum number of characters a label can have to be shown.
30907 The tool bar style must also show labels for this to have any effect, see
30908 `tool-bar-style'. */);
30909 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30910
30911 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30912 doc: /* List of functions to call to fontify regions of text.
30913 Each function is called with one argument POS. Functions must
30914 fontify a region starting at POS in the current buffer, and give
30915 fontified regions the property `fontified'. */);
30916 Vfontification_functions = Qnil;
30917 Fmake_variable_buffer_local (Qfontification_functions);
30918
30919 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30920 unibyte_display_via_language_environment,
30921 doc: /* Non-nil means display unibyte text according to language environment.
30922 Specifically, this means that raw bytes in the range 160-255 decimal
30923 are displayed by converting them to the equivalent multibyte characters
30924 according to the current language environment. As a result, they are
30925 displayed according to the current fontset.
30926
30927 Note that this variable affects only how these bytes are displayed,
30928 but does not change the fact they are interpreted as raw bytes. */);
30929 unibyte_display_via_language_environment = 0;
30930
30931 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30932 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30933 If a float, it specifies a fraction of the mini-window frame's height.
30934 If an integer, it specifies a number of lines. */);
30935 Vmax_mini_window_height = make_float (0.25);
30936
30937 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30938 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30939 A value of nil means don't automatically resize mini-windows.
30940 A value of t means resize them to fit the text displayed in them.
30941 A value of `grow-only', the default, means let mini-windows grow only;
30942 they return to their normal size when the minibuffer is closed, or the
30943 echo area becomes empty. */);
30944 Vresize_mini_windows = Qgrow_only;
30945
30946 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30947 doc: /* Alist specifying how to blink the cursor off.
30948 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30949 `cursor-type' frame-parameter or variable equals ON-STATE,
30950 comparing using `equal', Emacs uses OFF-STATE to specify
30951 how to blink it off. ON-STATE and OFF-STATE are values for
30952 the `cursor-type' frame parameter.
30953
30954 If a frame's ON-STATE has no entry in this list,
30955 the frame's other specifications determine how to blink the cursor off. */);
30956 Vblink_cursor_alist = Qnil;
30957
30958 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30959 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30960 If non-nil, windows are automatically scrolled horizontally to make
30961 point visible. */);
30962 automatic_hscrolling_p = 1;
30963 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30964
30965 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30966 doc: /* How many columns away from the window edge point is allowed to get
30967 before automatic hscrolling will horizontally scroll the window. */);
30968 hscroll_margin = 5;
30969
30970 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30971 doc: /* How many columns to scroll the window when point gets too close to the edge.
30972 When point is less than `hscroll-margin' columns from the window
30973 edge, automatic hscrolling will scroll the window by the amount of columns
30974 determined by this variable. If its value is a positive integer, scroll that
30975 many columns. If it's a positive floating-point number, it specifies the
30976 fraction of the window's width to scroll. If it's nil or zero, point will be
30977 centered horizontally after the scroll. Any other value, including negative
30978 numbers, are treated as if the value were zero.
30979
30980 Automatic hscrolling always moves point outside the scroll margin, so if
30981 point was more than scroll step columns inside the margin, the window will
30982 scroll more than the value given by the scroll step.
30983
30984 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30985 and `scroll-right' overrides this variable's effect. */);
30986 Vhscroll_step = make_number (0);
30987
30988 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30989 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30990 Bind this around calls to `message' to let it take effect. */);
30991 message_truncate_lines = 0;
30992
30993 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30994 doc: /* Normal hook run to update the menu bar definitions.
30995 Redisplay runs this hook before it redisplays the menu bar.
30996 This is used to update menus such as Buffers, whose contents depend on
30997 various data. */);
30998 Vmenu_bar_update_hook = Qnil;
30999
31000 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31001 doc: /* Frame for which we are updating a menu.
31002 The enable predicate for a menu binding should check this variable. */);
31003 Vmenu_updating_frame = Qnil;
31004
31005 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31006 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31007 inhibit_menubar_update = 0;
31008
31009 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31010 doc: /* Prefix prepended to all continuation lines at display time.
31011 The value may be a string, an image, or a stretch-glyph; it is
31012 interpreted in the same way as the value of a `display' text property.
31013
31014 This variable is overridden by any `wrap-prefix' text or overlay
31015 property.
31016
31017 To add a prefix to non-continuation lines, use `line-prefix'. */);
31018 Vwrap_prefix = Qnil;
31019 DEFSYM (Qwrap_prefix, "wrap-prefix");
31020 Fmake_variable_buffer_local (Qwrap_prefix);
31021
31022 DEFVAR_LISP ("line-prefix", Vline_prefix,
31023 doc: /* Prefix prepended to all non-continuation lines at display time.
31024 The value may be a string, an image, or a stretch-glyph; it is
31025 interpreted in the same way as the value of a `display' text property.
31026
31027 This variable is overridden by any `line-prefix' text or overlay
31028 property.
31029
31030 To add a prefix to continuation lines, use `wrap-prefix'. */);
31031 Vline_prefix = Qnil;
31032 DEFSYM (Qline_prefix, "line-prefix");
31033 Fmake_variable_buffer_local (Qline_prefix);
31034
31035 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31036 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31037 inhibit_eval_during_redisplay = 0;
31038
31039 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31040 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31041 inhibit_free_realized_faces = 0;
31042
31043 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31044 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31045 Intended for use during debugging and for testing bidi display;
31046 see biditest.el in the test suite. */);
31047 inhibit_bidi_mirroring = 0;
31048
31049 #ifdef GLYPH_DEBUG
31050 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31051 doc: /* Inhibit try_window_id display optimization. */);
31052 inhibit_try_window_id = 0;
31053
31054 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31055 doc: /* Inhibit try_window_reusing display optimization. */);
31056 inhibit_try_window_reusing = 0;
31057
31058 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31059 doc: /* Inhibit try_cursor_movement display optimization. */);
31060 inhibit_try_cursor_movement = 0;
31061 #endif /* GLYPH_DEBUG */
31062
31063 DEFVAR_INT ("overline-margin", overline_margin,
31064 doc: /* Space between overline and text, in pixels.
31065 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31066 margin to the character height. */);
31067 overline_margin = 2;
31068
31069 DEFVAR_INT ("underline-minimum-offset",
31070 underline_minimum_offset,
31071 doc: /* Minimum distance between baseline and underline.
31072 This can improve legibility of underlined text at small font sizes,
31073 particularly when using variable `x-use-underline-position-properties'
31074 with fonts that specify an UNDERLINE_POSITION relatively close to the
31075 baseline. The default value is 1. */);
31076 underline_minimum_offset = 1;
31077
31078 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31079 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31080 This feature only works when on a window system that can change
31081 cursor shapes. */);
31082 display_hourglass_p = 1;
31083
31084 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31085 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31086 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31087
31088 #ifdef HAVE_WINDOW_SYSTEM
31089 hourglass_atimer = NULL;
31090 hourglass_shown_p = 0;
31091 #endif /* HAVE_WINDOW_SYSTEM */
31092
31093 /* Name of the face used to display glyphless characters. */
31094 DEFSYM (Qglyphless_char, "glyphless-char");
31095
31096 /* Method symbols for Vglyphless_char_display. */
31097 DEFSYM (Qhex_code, "hex-code");
31098 DEFSYM (Qempty_box, "empty-box");
31099 DEFSYM (Qthin_space, "thin-space");
31100 DEFSYM (Qzero_width, "zero-width");
31101
31102 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31103 doc: /* Function run just before redisplay.
31104 It is called with one argument, which is the set of windows that are to
31105 be redisplayed. This set can be nil (meaning, only the selected window),
31106 or t (meaning all windows). */);
31107 Vpre_redisplay_function = intern ("ignore");
31108
31109 /* Symbol for the purpose of Vglyphless_char_display. */
31110 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31111 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31112
31113 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31114 doc: /* Char-table defining glyphless characters.
31115 Each element, if non-nil, should be one of the following:
31116 an ASCII acronym string: display this string in a box
31117 `hex-code': display the hexadecimal code of a character in a box
31118 `empty-box': display as an empty box
31119 `thin-space': display as 1-pixel width space
31120 `zero-width': don't display
31121 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31122 display method for graphical terminals and text terminals respectively.
31123 GRAPHICAL and TEXT should each have one of the values listed above.
31124
31125 The char-table has one extra slot to control the display of a character for
31126 which no font is found. This slot only takes effect on graphical terminals.
31127 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31128 `thin-space'. The default is `empty-box'.
31129
31130 If a character has a non-nil entry in an active display table, the
31131 display table takes effect; in this case, Emacs does not consult
31132 `glyphless-char-display' at all. */);
31133 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31134 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31135 Qempty_box);
31136
31137 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31138 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31139 Vdebug_on_message = Qnil;
31140
31141 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31142 doc: /* */);
31143 Vredisplay__all_windows_cause
31144 = Fmake_vector (make_number (100), make_number (0));
31145
31146 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31147 doc: /* */);
31148 Vredisplay__mode_lines_cause
31149 = Fmake_vector (make_number (100), make_number (0));
31150 }
31151
31152
31153 /* Initialize this module when Emacs starts. */
31154
31155 void
31156 init_xdisp (void)
31157 {
31158 CHARPOS (this_line_start_pos) = 0;
31159
31160 if (!noninteractive)
31161 {
31162 struct window *m = XWINDOW (minibuf_window);
31163 Lisp_Object frame = m->frame;
31164 struct frame *f = XFRAME (frame);
31165 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31166 struct window *r = XWINDOW (root);
31167 int i;
31168
31169 echo_area_window = minibuf_window;
31170
31171 r->top_line = FRAME_TOP_MARGIN (f);
31172 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31173 r->total_cols = FRAME_COLS (f);
31174 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31175 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31176 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31177
31178 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31179 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31180 m->total_cols = FRAME_COLS (f);
31181 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31182 m->total_lines = 1;
31183 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31184
31185 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31186 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31187 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31188
31189 /* The default ellipsis glyphs `...'. */
31190 for (i = 0; i < 3; ++i)
31191 default_invis_vector[i] = make_number ('.');
31192 }
31193
31194 {
31195 /* Allocate the buffer for frame titles.
31196 Also used for `format-mode-line'. */
31197 int size = 100;
31198 mode_line_noprop_buf = xmalloc (size);
31199 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31200 mode_line_noprop_ptr = mode_line_noprop_buf;
31201 mode_line_target = MODE_LINE_DISPLAY;
31202 }
31203
31204 help_echo_showing_p = 0;
31205 }
31206
31207 #ifdef HAVE_WINDOW_SYSTEM
31208
31209 /* Platform-independent portion of hourglass implementation. */
31210
31211 /* Timer function of hourglass_atimer. */
31212
31213 static void
31214 show_hourglass (struct atimer *timer)
31215 {
31216 /* The timer implementation will cancel this timer automatically
31217 after this function has run. Set hourglass_atimer to null
31218 so that we know the timer doesn't have to be canceled. */
31219 hourglass_atimer = NULL;
31220
31221 if (!hourglass_shown_p)
31222 {
31223 Lisp_Object tail, frame;
31224
31225 block_input ();
31226
31227 FOR_EACH_FRAME (tail, frame)
31228 {
31229 struct frame *f = XFRAME (frame);
31230
31231 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31232 && FRAME_RIF (f)->show_hourglass)
31233 FRAME_RIF (f)->show_hourglass (f);
31234 }
31235
31236 hourglass_shown_p = 1;
31237 unblock_input ();
31238 }
31239 }
31240
31241 /* Cancel a currently active hourglass timer, and start a new one. */
31242
31243 void
31244 start_hourglass (void)
31245 {
31246 struct timespec delay;
31247
31248 cancel_hourglass ();
31249
31250 if (INTEGERP (Vhourglass_delay)
31251 && XINT (Vhourglass_delay) > 0)
31252 delay = make_timespec (min (XINT (Vhourglass_delay),
31253 TYPE_MAXIMUM (time_t)),
31254 0);
31255 else if (FLOATP (Vhourglass_delay)
31256 && XFLOAT_DATA (Vhourglass_delay) > 0)
31257 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31258 else
31259 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31260
31261 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31262 show_hourglass, NULL);
31263 }
31264
31265 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31266 shown. */
31267
31268 void
31269 cancel_hourglass (void)
31270 {
31271 if (hourglass_atimer)
31272 {
31273 cancel_atimer (hourglass_atimer);
31274 hourglass_atimer = NULL;
31275 }
31276
31277 if (hourglass_shown_p)
31278 {
31279 Lisp_Object tail, frame;
31280
31281 block_input ();
31282
31283 FOR_EACH_FRAME (tail, frame)
31284 {
31285 struct frame *f = XFRAME (frame);
31286
31287 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31288 && FRAME_RIF (f)->hide_hourglass)
31289 FRAME_RIF (f)->hide_hourglass (f);
31290 #ifdef HAVE_NTGUI
31291 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31292 else if (!FRAME_W32_P (f))
31293 w32_arrow_cursor ();
31294 #endif
31295 }
31296
31297 hourglass_shown_p = 0;
31298 unblock_input ();
31299 }
31300 }
31301
31302 #endif /* HAVE_WINDOW_SYSTEM */